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 | } |
| 404 | printf("} (len=%u)\n", ascii->length); |
| 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 | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 1924 | Py_ssize_t size; |
| 1925 | PyObject *copy; |
| 1926 | void *data; |
| 1927 | |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 1928 | if (!PyUnicode_Check(unicode)) { |
| 1929 | PyErr_BadInternalCall(); |
| 1930 | return NULL; |
| 1931 | } |
| 1932 | if (PyUnicode_READY(unicode)) |
| 1933 | return NULL; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 1934 | |
| 1935 | size = PyUnicode_GET_LENGTH(unicode); |
| 1936 | copy = PyUnicode_New(size, PyUnicode_MAX_CHAR_VALUE(unicode)); |
| 1937 | if (!copy) |
| 1938 | return NULL; |
| 1939 | assert(PyUnicode_KIND(copy) == PyUnicode_KIND(unicode)); |
| 1940 | |
| 1941 | data = PyUnicode_DATA(unicode); |
| 1942 | switch (PyUnicode_KIND(unicode)) |
| 1943 | { |
| 1944 | case PyUnicode_1BYTE_KIND: |
| 1945 | memcpy(PyUnicode_1BYTE_DATA(copy), data, size); |
| 1946 | break; |
| 1947 | case PyUnicode_2BYTE_KIND: |
| 1948 | memcpy(PyUnicode_2BYTE_DATA(copy), data, sizeof(Py_UCS2) * size); |
| 1949 | break; |
| 1950 | case PyUnicode_4BYTE_KIND: |
| 1951 | memcpy(PyUnicode_4BYTE_DATA(copy), data, sizeof(Py_UCS4) * size); |
| 1952 | break; |
| 1953 | default: |
| 1954 | assert(0); |
| 1955 | break; |
| 1956 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1957 | assert(_PyUnicode_CheckConsistency(copy, 1)); |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 1958 | return copy; |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 1959 | } |
| 1960 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1961 | |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1962 | /* Widen Unicode objects to larger buffers. Don't write terminating null |
| 1963 | character. Return NULL on error. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1964 | |
| 1965 | void* |
| 1966 | _PyUnicode_AsKind(PyObject *s, unsigned int kind) |
| 1967 | { |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1968 | Py_ssize_t len; |
| 1969 | void *result; |
| 1970 | unsigned int skind; |
| 1971 | |
| 1972 | if (PyUnicode_READY(s)) |
| 1973 | return NULL; |
| 1974 | |
| 1975 | len = PyUnicode_GET_LENGTH(s); |
| 1976 | skind = PyUnicode_KIND(s); |
| 1977 | if (skind >= kind) { |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 1978 | PyErr_SetString(PyExc_SystemError, "invalid widening attempt"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1979 | return NULL; |
| 1980 | } |
| 1981 | switch(kind) { |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1982 | case PyUnicode_2BYTE_KIND: |
| 1983 | result = PyMem_Malloc(len * sizeof(Py_UCS2)); |
| 1984 | if (!result) |
| 1985 | return PyErr_NoMemory(); |
| 1986 | assert(skind == PyUnicode_1BYTE_KIND); |
| 1987 | _PyUnicode_CONVERT_BYTES( |
| 1988 | Py_UCS1, Py_UCS2, |
| 1989 | PyUnicode_1BYTE_DATA(s), |
| 1990 | PyUnicode_1BYTE_DATA(s) + len, |
| 1991 | result); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1992 | return result; |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1993 | case PyUnicode_4BYTE_KIND: |
| 1994 | result = PyMem_Malloc(len * sizeof(Py_UCS4)); |
| 1995 | if (!result) |
| 1996 | return PyErr_NoMemory(); |
| 1997 | if (skind == PyUnicode_2BYTE_KIND) { |
| 1998 | _PyUnicode_CONVERT_BYTES( |
| 1999 | Py_UCS2, Py_UCS4, |
| 2000 | PyUnicode_2BYTE_DATA(s), |
| 2001 | PyUnicode_2BYTE_DATA(s) + len, |
| 2002 | result); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2003 | } |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2004 | else { |
| 2005 | assert(skind == PyUnicode_1BYTE_KIND); |
| 2006 | _PyUnicode_CONVERT_BYTES( |
| 2007 | Py_UCS1, Py_UCS4, |
| 2008 | PyUnicode_1BYTE_DATA(s), |
| 2009 | PyUnicode_1BYTE_DATA(s) + len, |
| 2010 | result); |
| 2011 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2012 | return result; |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2013 | default: |
| 2014 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2015 | } |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 2016 | PyErr_SetString(PyExc_SystemError, "invalid kind"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2017 | return NULL; |
| 2018 | } |
| 2019 | |
| 2020 | static Py_UCS4* |
| 2021 | as_ucs4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, |
| 2022 | int copy_null) |
| 2023 | { |
| 2024 | int kind; |
| 2025 | void *data; |
| 2026 | Py_ssize_t len, targetlen; |
| 2027 | if (PyUnicode_READY(string) == -1) |
| 2028 | return NULL; |
| 2029 | kind = PyUnicode_KIND(string); |
| 2030 | data = PyUnicode_DATA(string); |
| 2031 | len = PyUnicode_GET_LENGTH(string); |
| 2032 | targetlen = len; |
| 2033 | if (copy_null) |
| 2034 | targetlen++; |
| 2035 | if (!target) { |
| 2036 | if (PY_SSIZE_T_MAX / sizeof(Py_UCS4) < targetlen) { |
| 2037 | PyErr_NoMemory(); |
| 2038 | return NULL; |
| 2039 | } |
| 2040 | target = PyMem_Malloc(targetlen * sizeof(Py_UCS4)); |
| 2041 | if (!target) { |
| 2042 | PyErr_NoMemory(); |
| 2043 | return NULL; |
| 2044 | } |
| 2045 | } |
| 2046 | else { |
| 2047 | if (targetsize < targetlen) { |
| 2048 | PyErr_Format(PyExc_SystemError, |
| 2049 | "string is longer than the buffer"); |
| 2050 | if (copy_null && 0 < targetsize) |
| 2051 | target[0] = 0; |
| 2052 | return NULL; |
| 2053 | } |
| 2054 | } |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2055 | if (kind == PyUnicode_1BYTE_KIND) { |
| 2056 | Py_UCS1 *start = (Py_UCS1 *) data; |
| 2057 | _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] | 2058 | } |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2059 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 2060 | Py_UCS2 *start = (Py_UCS2 *) data; |
| 2061 | _PyUnicode_CONVERT_BYTES(Py_UCS2, Py_UCS4, start, start + len, target); |
| 2062 | } |
| 2063 | else { |
| 2064 | assert(kind == PyUnicode_4BYTE_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2065 | Py_MEMCPY(target, data, len * sizeof(Py_UCS4)); |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2066 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2067 | if (copy_null) |
| 2068 | target[len] = 0; |
| 2069 | return target; |
| 2070 | } |
| 2071 | |
| 2072 | Py_UCS4* |
| 2073 | PyUnicode_AsUCS4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, |
| 2074 | int copy_null) |
| 2075 | { |
Antoine Pitrou | de20b0b | 2011-11-10 21:47:38 +0100 | [diff] [blame] | 2076 | if (target == NULL || targetsize < 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2077 | PyErr_BadInternalCall(); |
| 2078 | return NULL; |
| 2079 | } |
| 2080 | return as_ucs4(string, target, targetsize, copy_null); |
| 2081 | } |
| 2082 | |
| 2083 | Py_UCS4* |
| 2084 | PyUnicode_AsUCS4Copy(PyObject *string) |
| 2085 | { |
| 2086 | return as_ucs4(string, NULL, 0, 1); |
| 2087 | } |
| 2088 | |
| 2089 | #ifdef HAVE_WCHAR_H |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 2090 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2091 | PyObject * |
| 2092 | PyUnicode_FromWideChar(register const wchar_t *w, Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2093 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2094 | if (w == NULL) { |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 2095 | if (size == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2096 | return PyUnicode_New(0, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2097 | PyErr_BadInternalCall(); |
| 2098 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2099 | } |
| 2100 | |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 2101 | if (size == -1) { |
| 2102 | size = wcslen(w); |
| 2103 | } |
| 2104 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2105 | return PyUnicode_FromUnicode(w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2106 | } |
| 2107 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2108 | #endif /* HAVE_WCHAR_H */ |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 2109 | |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2110 | static void |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2111 | makefmt(char *fmt, int longflag, int longlongflag, int size_tflag, |
| 2112 | int zeropad, int width, int precision, char c) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2113 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2114 | *fmt++ = '%'; |
| 2115 | if (width) { |
| 2116 | if (zeropad) |
| 2117 | *fmt++ = '0'; |
| 2118 | fmt += sprintf(fmt, "%d", width); |
| 2119 | } |
| 2120 | if (precision) |
| 2121 | fmt += sprintf(fmt, ".%d", precision); |
| 2122 | if (longflag) |
| 2123 | *fmt++ = 'l'; |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2124 | else if (longlongflag) { |
| 2125 | /* longlongflag should only ever be nonzero on machines with |
| 2126 | HAVE_LONG_LONG defined */ |
| 2127 | #ifdef HAVE_LONG_LONG |
| 2128 | char *f = PY_FORMAT_LONG_LONG; |
| 2129 | while (*f) |
| 2130 | *fmt++ = *f++; |
| 2131 | #else |
| 2132 | /* we shouldn't ever get here */ |
| 2133 | assert(0); |
| 2134 | *fmt++ = 'l'; |
| 2135 | #endif |
| 2136 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2137 | else if (size_tflag) { |
| 2138 | char *f = PY_FORMAT_SIZE_T; |
| 2139 | while (*f) |
| 2140 | *fmt++ = *f++; |
| 2141 | } |
| 2142 | *fmt++ = c; |
| 2143 | *fmt = '\0'; |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2144 | } |
| 2145 | |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2146 | /* helper for PyUnicode_FromFormatV() */ |
| 2147 | |
| 2148 | static const char* |
| 2149 | parse_format_flags(const char *f, |
| 2150 | int *p_width, int *p_precision, |
| 2151 | int *p_longflag, int *p_longlongflag, int *p_size_tflag) |
| 2152 | { |
| 2153 | int width, precision, longflag, longlongflag, size_tflag; |
| 2154 | |
| 2155 | /* parse the width.precision part, e.g. "%2.5s" => width=2, precision=5 */ |
| 2156 | f++; |
| 2157 | width = 0; |
| 2158 | while (Py_ISDIGIT((unsigned)*f)) |
| 2159 | width = (width*10) + *f++ - '0'; |
| 2160 | precision = 0; |
| 2161 | if (*f == '.') { |
| 2162 | f++; |
| 2163 | while (Py_ISDIGIT((unsigned)*f)) |
| 2164 | precision = (precision*10) + *f++ - '0'; |
| 2165 | if (*f == '%') { |
| 2166 | /* "%.3%s" => f points to "3" */ |
| 2167 | f--; |
| 2168 | } |
| 2169 | } |
| 2170 | if (*f == '\0') { |
| 2171 | /* bogus format "%.1" => go backward, f points to "1" */ |
| 2172 | f--; |
| 2173 | } |
| 2174 | if (p_width != NULL) |
| 2175 | *p_width = width; |
| 2176 | if (p_precision != NULL) |
| 2177 | *p_precision = precision; |
| 2178 | |
| 2179 | /* Handle %ld, %lu, %lld and %llu. */ |
| 2180 | longflag = 0; |
| 2181 | longlongflag = 0; |
Victor Stinner | e7faec1 | 2011-03-02 00:01:53 +0000 | [diff] [blame] | 2182 | size_tflag = 0; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2183 | |
| 2184 | if (*f == 'l') { |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2185 | if (f[1] == 'd' || f[1] == 'u' || f[1] == 'i') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2186 | longflag = 1; |
| 2187 | ++f; |
| 2188 | } |
| 2189 | #ifdef HAVE_LONG_LONG |
| 2190 | else if (f[1] == 'l' && |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2191 | (f[2] == 'd' || f[2] == 'u' || f[2] == 'i')) { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2192 | longlongflag = 1; |
| 2193 | f += 2; |
| 2194 | } |
| 2195 | #endif |
| 2196 | } |
| 2197 | /* handle the size_t flag. */ |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2198 | 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] | 2199 | size_tflag = 1; |
| 2200 | ++f; |
| 2201 | } |
| 2202 | if (p_longflag != NULL) |
| 2203 | *p_longflag = longflag; |
| 2204 | if (p_longlongflag != NULL) |
| 2205 | *p_longlongflag = longlongflag; |
| 2206 | if (p_size_tflag != NULL) |
| 2207 | *p_size_tflag = size_tflag; |
| 2208 | return f; |
| 2209 | } |
| 2210 | |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2211 | /* maximum number of characters required for output of %ld. 21 characters |
| 2212 | allows for 64-bit integers (in decimal) and an optional sign. */ |
| 2213 | #define MAX_LONG_CHARS 21 |
| 2214 | /* maximum number of characters required for output of %lld. |
| 2215 | We need at most ceil(log10(256)*SIZEOF_LONG_LONG) digits, |
| 2216 | plus 1 for the sign. 53/22 is an upper bound for log10(256). */ |
| 2217 | #define MAX_LONG_LONG_CHARS (2 + (SIZEOF_LONG_LONG*53-1) / 22) |
| 2218 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2219 | PyObject * |
| 2220 | PyUnicode_FromFormatV(const char *format, va_list vargs) |
| 2221 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2222 | va_list count; |
| 2223 | Py_ssize_t callcount = 0; |
| 2224 | PyObject **callresults = NULL; |
| 2225 | PyObject **callresult = NULL; |
| 2226 | Py_ssize_t n = 0; |
| 2227 | int width = 0; |
| 2228 | int precision = 0; |
| 2229 | int zeropad; |
| 2230 | const char* f; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2231 | PyObject *string; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2232 | /* used by sprintf */ |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2233 | char fmt[61]; /* should be enough for %0width.precisionlld */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2234 | Py_UCS4 maxchar = 127; /* result is ASCII by default */ |
| 2235 | Py_UCS4 argmaxchar; |
| 2236 | Py_ssize_t numbersize = 0; |
| 2237 | char *numberresults = NULL; |
| 2238 | char *numberresult = NULL; |
| 2239 | Py_ssize_t i; |
| 2240 | int kind; |
| 2241 | void *data; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2242 | |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 2243 | Py_VA_COPY(count, vargs); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2244 | /* step 1: count the number of %S/%R/%A/%s format specifications |
| 2245 | * (we call PyObject_Str()/PyObject_Repr()/PyObject_ASCII()/ |
| 2246 | * 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] | 2247 | * result in an array) |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 2248 | * also estimate a upper bound for all the number formats in the string, |
| 2249 | * 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] | 2250 | * buffer before putting everything together. */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2251 | for (f = format; *f; f++) { |
| 2252 | if (*f == '%') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2253 | int longlongflag; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2254 | /* skip width or width.precision (eg. "1.2" of "%1.2f") */ |
| 2255 | f = parse_format_flags(f, &width, NULL, NULL, &longlongflag, NULL); |
| 2256 | if (*f == 's' || *f=='S' || *f=='R' || *f=='A' || *f=='V') |
| 2257 | ++callcount; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2258 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2259 | else if (*f == 'd' || *f=='u' || *f=='i' || *f=='x' || *f=='p') { |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2260 | #ifdef HAVE_LONG_LONG |
| 2261 | if (longlongflag) { |
| 2262 | if (width < MAX_LONG_LONG_CHARS) |
| 2263 | width = MAX_LONG_LONG_CHARS; |
| 2264 | } |
| 2265 | else |
| 2266 | #endif |
| 2267 | /* MAX_LONG_CHARS is enough to hold a 64-bit integer, |
| 2268 | including sign. Decimal takes the most space. This |
| 2269 | isn't enough for octal. If a width is specified we |
| 2270 | need more (which we allocate later). */ |
| 2271 | if (width < MAX_LONG_CHARS) |
| 2272 | width = MAX_LONG_CHARS; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2273 | |
| 2274 | /* account for the size + '\0' to separate numbers |
| 2275 | inside of the numberresults buffer */ |
| 2276 | numbersize += (width + 1); |
| 2277 | } |
| 2278 | } |
| 2279 | else if ((unsigned char)*f > 127) { |
| 2280 | PyErr_Format(PyExc_ValueError, |
| 2281 | "PyUnicode_FromFormatV() expects an ASCII-encoded format " |
| 2282 | "string, got a non-ASCII byte: 0x%02x", |
| 2283 | (unsigned char)*f); |
| 2284 | return NULL; |
| 2285 | } |
| 2286 | } |
| 2287 | /* step 2: allocate memory for the results of |
| 2288 | * PyObject_Str()/PyObject_Repr()/PyUnicode_DecodeUTF8() calls */ |
| 2289 | if (callcount) { |
| 2290 | callresults = PyObject_Malloc(sizeof(PyObject *) * callcount); |
| 2291 | if (!callresults) { |
| 2292 | PyErr_NoMemory(); |
| 2293 | return NULL; |
| 2294 | } |
| 2295 | callresult = callresults; |
| 2296 | } |
| 2297 | /* step 2.5: allocate memory for the results of formating numbers */ |
| 2298 | if (numbersize) { |
| 2299 | numberresults = PyObject_Malloc(numbersize); |
| 2300 | if (!numberresults) { |
| 2301 | PyErr_NoMemory(); |
| 2302 | goto fail; |
| 2303 | } |
| 2304 | numberresult = numberresults; |
| 2305 | } |
| 2306 | |
| 2307 | /* step 3: format numbers and figure out how large a buffer we need */ |
| 2308 | for (f = format; *f; f++) { |
| 2309 | if (*f == '%') { |
| 2310 | const char* p; |
| 2311 | int longflag; |
| 2312 | int longlongflag; |
| 2313 | int size_tflag; |
| 2314 | int numprinted; |
| 2315 | |
| 2316 | p = f; |
| 2317 | zeropad = (f[1] == '0'); |
| 2318 | f = parse_format_flags(f, &width, &precision, |
| 2319 | &longflag, &longlongflag, &size_tflag); |
| 2320 | switch (*f) { |
| 2321 | case 'c': |
| 2322 | { |
| 2323 | Py_UCS4 ordinal = va_arg(count, int); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2324 | maxchar = Py_MAX(maxchar, ordinal); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2325 | n++; |
| 2326 | break; |
| 2327 | } |
| 2328 | case '%': |
| 2329 | n++; |
| 2330 | break; |
| 2331 | case 'i': |
| 2332 | case 'd': |
| 2333 | makefmt(fmt, longflag, longlongflag, size_tflag, zeropad, |
| 2334 | width, precision, *f); |
| 2335 | if (longflag) |
| 2336 | numprinted = sprintf(numberresult, fmt, |
| 2337 | va_arg(count, long)); |
| 2338 | #ifdef HAVE_LONG_LONG |
| 2339 | else if (longlongflag) |
| 2340 | numprinted = sprintf(numberresult, fmt, |
| 2341 | va_arg(count, PY_LONG_LONG)); |
| 2342 | #endif |
| 2343 | else if (size_tflag) |
| 2344 | numprinted = sprintf(numberresult, fmt, |
| 2345 | va_arg(count, Py_ssize_t)); |
| 2346 | else |
| 2347 | numprinted = sprintf(numberresult, fmt, |
| 2348 | va_arg(count, int)); |
| 2349 | n += numprinted; |
| 2350 | /* advance by +1 to skip over the '\0' */ |
| 2351 | numberresult += (numprinted + 1); |
| 2352 | assert(*(numberresult - 1) == '\0'); |
| 2353 | assert(*(numberresult - 2) != '\0'); |
| 2354 | assert(numprinted >= 0); |
| 2355 | assert(numberresult <= numberresults + numbersize); |
| 2356 | break; |
| 2357 | case 'u': |
| 2358 | makefmt(fmt, longflag, longlongflag, size_tflag, zeropad, |
| 2359 | width, precision, 'u'); |
| 2360 | if (longflag) |
| 2361 | numprinted = sprintf(numberresult, fmt, |
| 2362 | va_arg(count, unsigned long)); |
| 2363 | #ifdef HAVE_LONG_LONG |
| 2364 | else if (longlongflag) |
| 2365 | numprinted = sprintf(numberresult, fmt, |
| 2366 | va_arg(count, unsigned PY_LONG_LONG)); |
| 2367 | #endif |
| 2368 | else if (size_tflag) |
| 2369 | numprinted = sprintf(numberresult, fmt, |
| 2370 | va_arg(count, size_t)); |
| 2371 | else |
| 2372 | numprinted = sprintf(numberresult, fmt, |
| 2373 | va_arg(count, unsigned int)); |
| 2374 | n += numprinted; |
| 2375 | numberresult += (numprinted + 1); |
| 2376 | assert(*(numberresult - 1) == '\0'); |
| 2377 | assert(*(numberresult - 2) != '\0'); |
| 2378 | assert(numprinted >= 0); |
| 2379 | assert(numberresult <= numberresults + numbersize); |
| 2380 | break; |
| 2381 | case 'x': |
| 2382 | makefmt(fmt, 0, 0, 0, zeropad, width, precision, 'x'); |
| 2383 | numprinted = sprintf(numberresult, fmt, va_arg(count, int)); |
| 2384 | n += numprinted; |
| 2385 | numberresult += (numprinted + 1); |
| 2386 | assert(*(numberresult - 1) == '\0'); |
| 2387 | assert(*(numberresult - 2) != '\0'); |
| 2388 | assert(numprinted >= 0); |
| 2389 | assert(numberresult <= numberresults + numbersize); |
| 2390 | break; |
| 2391 | case 'p': |
| 2392 | numprinted = sprintf(numberresult, "%p", va_arg(count, void*)); |
| 2393 | /* %p is ill-defined: ensure leading 0x. */ |
| 2394 | if (numberresult[1] == 'X') |
| 2395 | numberresult[1] = 'x'; |
| 2396 | else if (numberresult[1] != 'x') { |
| 2397 | memmove(numberresult + 2, numberresult, |
| 2398 | strlen(numberresult) + 1); |
| 2399 | numberresult[0] = '0'; |
| 2400 | numberresult[1] = 'x'; |
| 2401 | numprinted += 2; |
| 2402 | } |
| 2403 | n += numprinted; |
| 2404 | numberresult += (numprinted + 1); |
| 2405 | assert(*(numberresult - 1) == '\0'); |
| 2406 | assert(*(numberresult - 2) != '\0'); |
| 2407 | assert(numprinted >= 0); |
| 2408 | assert(numberresult <= numberresults + numbersize); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2409 | break; |
| 2410 | case 's': |
| 2411 | { |
| 2412 | /* UTF-8 */ |
Georg Brandl | 780b2a6 | 2009-05-05 09:19:59 +0000 | [diff] [blame] | 2413 | const char *s = va_arg(count, const char*); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2414 | PyObject *str = PyUnicode_DecodeUTF8(s, strlen(s), "replace"); |
| 2415 | if (!str) |
| 2416 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2417 | /* since PyUnicode_DecodeUTF8 returns already flexible |
| 2418 | unicode objects, there is no need to call ready on them */ |
| 2419 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2420 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2421 | n += PyUnicode_GET_LENGTH(str); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2422 | /* Remember the str and switch to the next slot */ |
| 2423 | *callresult++ = str; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2424 | break; |
| 2425 | } |
| 2426 | case 'U': |
| 2427 | { |
| 2428 | PyObject *obj = va_arg(count, PyObject *); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 2429 | assert(obj && _PyUnicode_CHECK(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); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2435 | break; |
| 2436 | } |
| 2437 | case 'V': |
| 2438 | { |
| 2439 | PyObject *obj = va_arg(count, PyObject *); |
| 2440 | const char *str = va_arg(count, const char *); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2441 | PyObject *str_obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2442 | assert(obj || str); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 2443 | assert(!obj || _PyUnicode_CHECK(obj)); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2444 | if (obj) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2445 | if (PyUnicode_READY(obj) == -1) |
| 2446 | goto fail; |
| 2447 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2448 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2449 | n += PyUnicode_GET_LENGTH(obj); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2450 | *callresult++ = NULL; |
| 2451 | } |
| 2452 | else { |
| 2453 | str_obj = PyUnicode_DecodeUTF8(str, strlen(str), "replace"); |
| 2454 | if (!str_obj) |
| 2455 | goto fail; |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 2456 | if (PyUnicode_READY(str_obj)) { |
| 2457 | Py_DECREF(str_obj); |
| 2458 | goto fail; |
| 2459 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2460 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str_obj); |
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_obj); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2463 | *callresult++ = str_obj; |
| 2464 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2465 | break; |
| 2466 | } |
| 2467 | case 'S': |
| 2468 | { |
| 2469 | PyObject *obj = va_arg(count, PyObject *); |
| 2470 | PyObject *str; |
| 2471 | assert(obj); |
| 2472 | str = PyObject_Str(obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2473 | if (!str || PyUnicode_READY(str) == -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(str); |
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(str); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2478 | /* Remember the str and switch to the next slot */ |
| 2479 | *callresult++ = str; |
| 2480 | break; |
| 2481 | } |
| 2482 | case 'R': |
| 2483 | { |
| 2484 | PyObject *obj = va_arg(count, PyObject *); |
| 2485 | PyObject *repr; |
| 2486 | assert(obj); |
| 2487 | repr = PyObject_Repr(obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2488 | if (!repr || PyUnicode_READY(repr) == -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(repr); |
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(repr); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2493 | /* Remember the repr and switch to the next slot */ |
| 2494 | *callresult++ = repr; |
| 2495 | break; |
| 2496 | } |
| 2497 | case 'A': |
| 2498 | { |
| 2499 | PyObject *obj = va_arg(count, PyObject *); |
| 2500 | PyObject *ascii; |
| 2501 | assert(obj); |
| 2502 | ascii = PyObject_ASCII(obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2503 | if (!ascii || PyUnicode_READY(ascii) == -1) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2504 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2505 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(ascii); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2506 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2507 | n += PyUnicode_GET_LENGTH(ascii); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2508 | /* Remember the repr and switch to the next slot */ |
| 2509 | *callresult++ = ascii; |
| 2510 | break; |
| 2511 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2512 | default: |
| 2513 | /* if we stumble upon an unknown |
| 2514 | formatting code, copy the rest of |
| 2515 | the format string to the output |
| 2516 | string. (we cannot just skip the |
| 2517 | code, since there's no way to know |
| 2518 | what's in the argument list) */ |
| 2519 | n += strlen(p); |
| 2520 | goto expand; |
| 2521 | } |
| 2522 | } else |
| 2523 | n++; |
| 2524 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2525 | expand: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2526 | /* step 4: fill the buffer */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2527 | /* Since we've analyzed how much space we need, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2528 | we don't have to resize the string. |
| 2529 | There can be no errors beyond this point. */ |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2530 | string = PyUnicode_New(n, maxchar); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2531 | if (!string) |
| 2532 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2533 | kind = PyUnicode_KIND(string); |
| 2534 | data = PyUnicode_DATA(string); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2535 | callresult = callresults; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2536 | numberresult = numberresults; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2537 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2538 | for (i = 0, f = format; *f; f++) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2539 | if (*f == '%') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2540 | const char* p; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2541 | |
| 2542 | p = f; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2543 | f = parse_format_flags(f, NULL, NULL, NULL, NULL, NULL); |
| 2544 | /* checking for == because the last argument could be a empty |
| 2545 | string, which causes i to point to end, the assert at the end of |
| 2546 | the loop */ |
| 2547 | assert(i <= PyUnicode_GET_LENGTH(string)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2548 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2549 | switch (*f) { |
| 2550 | case 'c': |
Victor Stinner | 5ed8b2c | 2011-02-21 21:13:44 +0000 | [diff] [blame] | 2551 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2552 | const int ordinal = va_arg(vargs, int); |
| 2553 | PyUnicode_WRITE(kind, data, i++, ordinal); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2554 | break; |
Victor Stinner | 5ed8b2c | 2011-02-21 21:13:44 +0000 | [diff] [blame] | 2555 | } |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2556 | case 'i': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2557 | case 'd': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2558 | case 'u': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2559 | case 'x': |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2560 | case 'p': |
| 2561 | /* unused, since we already have the result */ |
| 2562 | if (*f == 'p') |
| 2563 | (void) va_arg(vargs, void *); |
| 2564 | else |
| 2565 | (void) va_arg(vargs, int); |
| 2566 | /* extract the result from numberresults and append. */ |
| 2567 | for (; *numberresult; ++i, ++numberresult) |
| 2568 | PyUnicode_WRITE(kind, data, i, *numberresult); |
| 2569 | /* skip over the separating '\0' */ |
| 2570 | assert(*numberresult == '\0'); |
| 2571 | numberresult++; |
| 2572 | assert(numberresult <= numberresults + numbersize); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2573 | break; |
| 2574 | case 's': |
| 2575 | { |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2576 | /* unused, since we already have the result */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2577 | Py_ssize_t size; |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2578 | (void) va_arg(vargs, char *); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2579 | size = PyUnicode_GET_LENGTH(*callresult); |
| 2580 | assert(PyUnicode_KIND(*callresult) <= PyUnicode_KIND(string)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2581 | copy_characters(string, i, *callresult, 0, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2582 | i += size; |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2583 | /* We're done with the unicode()/repr() => forget it */ |
| 2584 | Py_DECREF(*callresult); |
| 2585 | /* switch to next unicode()/repr() result */ |
| 2586 | ++callresult; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2587 | break; |
| 2588 | } |
| 2589 | case 'U': |
| 2590 | { |
| 2591 | PyObject *obj = va_arg(vargs, PyObject *); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2592 | Py_ssize_t size; |
| 2593 | assert(PyUnicode_KIND(obj) <= PyUnicode_KIND(string)); |
| 2594 | size = PyUnicode_GET_LENGTH(obj); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2595 | copy_characters(string, i, obj, 0, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2596 | i += size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2597 | break; |
| 2598 | } |
| 2599 | case 'V': |
| 2600 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2601 | Py_ssize_t size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2602 | PyObject *obj = va_arg(vargs, PyObject *); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2603 | va_arg(vargs, const char *); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2604 | if (obj) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2605 | size = PyUnicode_GET_LENGTH(obj); |
| 2606 | assert(PyUnicode_KIND(obj) <= PyUnicode_KIND(string)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2607 | copy_characters(string, i, obj, 0, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2608 | i += size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2609 | } else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2610 | size = PyUnicode_GET_LENGTH(*callresult); |
| 2611 | assert(PyUnicode_KIND(*callresult) <= |
| 2612 | PyUnicode_KIND(string)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2613 | copy_characters(string, i, *callresult, 0, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2614 | i += size; |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2615 | Py_DECREF(*callresult); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2616 | } |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2617 | ++callresult; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2618 | break; |
| 2619 | } |
| 2620 | case 'S': |
| 2621 | case 'R': |
Victor Stinner | 9a90900 | 2010-10-18 20:59:24 +0000 | [diff] [blame] | 2622 | case 'A': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2623 | { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2624 | Py_ssize_t size = PyUnicode_GET_LENGTH(*callresult); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2625 | /* unused, since we already have the result */ |
| 2626 | (void) va_arg(vargs, PyObject *); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2627 | assert(PyUnicode_KIND(*callresult) <= PyUnicode_KIND(string)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2628 | copy_characters(string, i, *callresult, 0, size); |
| 2629 | i += size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2630 | /* We're done with the unicode()/repr() => forget it */ |
| 2631 | Py_DECREF(*callresult); |
| 2632 | /* switch to next unicode()/repr() result */ |
| 2633 | ++callresult; |
| 2634 | break; |
| 2635 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2636 | case '%': |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2637 | PyUnicode_WRITE(kind, data, i++, '%'); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2638 | break; |
| 2639 | default: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2640 | for (; *p; ++p, ++i) |
| 2641 | PyUnicode_WRITE(kind, data, i, *p); |
| 2642 | assert(i == PyUnicode_GET_LENGTH(string)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2643 | goto end; |
| 2644 | } |
Victor Stinner | 1205f27 | 2010-09-11 00:54:47 +0000 | [diff] [blame] | 2645 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2646 | else { |
| 2647 | assert(i < PyUnicode_GET_LENGTH(string)); |
| 2648 | PyUnicode_WRITE(kind, data, i++, *f); |
| 2649 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2650 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2651 | assert(i == PyUnicode_GET_LENGTH(string)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2652 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2653 | end: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2654 | if (callresults) |
| 2655 | PyObject_Free(callresults); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2656 | if (numberresults) |
| 2657 | PyObject_Free(numberresults); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2658 | assert(_PyUnicode_CheckConsistency(string, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 2659 | return string; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2660 | fail: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2661 | if (callresults) { |
| 2662 | PyObject **callresult2 = callresults; |
| 2663 | while (callresult2 < callresult) { |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2664 | Py_XDECREF(*callresult2); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2665 | ++callresult2; |
| 2666 | } |
| 2667 | PyObject_Free(callresults); |
| 2668 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2669 | if (numberresults) |
| 2670 | PyObject_Free(numberresults); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2671 | return NULL; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2672 | } |
| 2673 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2674 | PyObject * |
| 2675 | PyUnicode_FromFormat(const char *format, ...) |
| 2676 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2677 | PyObject* ret; |
| 2678 | va_list vargs; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2679 | |
| 2680 | #ifdef HAVE_STDARG_PROTOTYPES |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2681 | va_start(vargs, format); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2682 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2683 | va_start(vargs); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2684 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2685 | ret = PyUnicode_FromFormatV(format, vargs); |
| 2686 | va_end(vargs); |
| 2687 | return ret; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2688 | } |
| 2689 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2690 | #ifdef HAVE_WCHAR_H |
| 2691 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2692 | /* Helper function for PyUnicode_AsWideChar() and PyUnicode_AsWideCharString(): |
| 2693 | convert a Unicode object to a wide character string. |
| 2694 | |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2695 | - 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] | 2696 | character) required to convert the unicode object. Ignore size argument. |
| 2697 | |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2698 | - Otherwise: return the number of wide characters (excluding the null |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2699 | character) written into w. Write at most size wide characters (including |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2700 | the null character). */ |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2701 | static Py_ssize_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2702 | unicode_aswidechar(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2703 | wchar_t *w, |
| 2704 | Py_ssize_t size) |
| 2705 | { |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2706 | Py_ssize_t res; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2707 | const wchar_t *wstr; |
| 2708 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2709 | wstr = PyUnicode_AsUnicodeAndSize(unicode, &res); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2710 | if (wstr == NULL) |
| 2711 | return -1; |
| 2712 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2713 | if (w != NULL) { |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2714 | if (size > res) |
| 2715 | size = res + 1; |
| 2716 | else |
| 2717 | res = size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2718 | Py_MEMCPY(w, wstr, size * sizeof(wchar_t)); |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2719 | return res; |
| 2720 | } |
| 2721 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2722 | return res + 1; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2723 | } |
| 2724 | |
| 2725 | Py_ssize_t |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2726 | PyUnicode_AsWideChar(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2727 | wchar_t *w, |
| 2728 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2729 | { |
| 2730 | if (unicode == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2731 | PyErr_BadInternalCall(); |
| 2732 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2733 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2734 | return unicode_aswidechar(unicode, w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2735 | } |
| 2736 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2737 | wchar_t* |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 2738 | PyUnicode_AsWideCharString(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2739 | Py_ssize_t *size) |
| 2740 | { |
| 2741 | wchar_t* buffer; |
| 2742 | Py_ssize_t buflen; |
| 2743 | |
| 2744 | if (unicode == NULL) { |
| 2745 | PyErr_BadInternalCall(); |
| 2746 | return NULL; |
| 2747 | } |
| 2748 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2749 | buflen = unicode_aswidechar(unicode, NULL, 0); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2750 | if (buflen == -1) |
| 2751 | return NULL; |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2752 | if (PY_SSIZE_T_MAX / sizeof(wchar_t) < buflen) { |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2753 | PyErr_NoMemory(); |
| 2754 | return NULL; |
| 2755 | } |
| 2756 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2757 | buffer = PyMem_MALLOC(buflen * sizeof(wchar_t)); |
| 2758 | if (buffer == NULL) { |
| 2759 | PyErr_NoMemory(); |
| 2760 | return NULL; |
| 2761 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2762 | buflen = unicode_aswidechar(unicode, buffer, buflen); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2763 | if (buflen == -1) |
| 2764 | return NULL; |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2765 | if (size != NULL) |
| 2766 | *size = buflen; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2767 | return buffer; |
| 2768 | } |
| 2769 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2770 | #endif /* HAVE_WCHAR_H */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2771 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2772 | PyObject * |
| 2773 | PyUnicode_FromOrdinal(int ordinal) |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2774 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2775 | PyObject *v; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2776 | if (ordinal < 0 || ordinal > 0x10ffff) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2777 | PyErr_SetString(PyExc_ValueError, |
| 2778 | "chr() arg not in range(0x110000)"); |
| 2779 | return NULL; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2780 | } |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 2781 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2782 | if (ordinal < 256) |
| 2783 | return get_latin1_char(ordinal); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2784 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2785 | v = PyUnicode_New(1, ordinal); |
| 2786 | if (v == NULL) |
| 2787 | return NULL; |
| 2788 | PyUnicode_WRITE(PyUnicode_KIND(v), PyUnicode_DATA(v), 0, ordinal); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2789 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2790 | return v; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2791 | } |
| 2792 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2793 | PyObject * |
| 2794 | PyUnicode_FromObject(register PyObject *obj) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2795 | { |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2796 | /* XXX Perhaps we should make this API an alias of |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2797 | PyObject_Str() instead ?! */ |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2798 | if (PyUnicode_CheckExact(obj)) { |
Victor Stinner | d3a83d5 | 2011-10-01 03:09:33 +0200 | [diff] [blame] | 2799 | if (PyUnicode_READY(obj)) |
| 2800 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2801 | Py_INCREF(obj); |
| 2802 | return obj; |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2803 | } |
| 2804 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2805 | /* For a Unicode subtype that's not a Unicode object, |
| 2806 | return a true Unicode object with the same data. */ |
Victor Stinner | 2219e0a | 2011-10-01 01:16:59 +0200 | [diff] [blame] | 2807 | return PyUnicode_Copy(obj); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2808 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2809 | PyErr_Format(PyExc_TypeError, |
| 2810 | "Can't convert '%.100s' object to str implicitly", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2811 | Py_TYPE(obj)->tp_name); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2812 | return NULL; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2813 | } |
| 2814 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2815 | PyObject * |
| 2816 | PyUnicode_FromEncodedObject(register PyObject *obj, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2817 | const char *encoding, |
| 2818 | const char *errors) |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2819 | { |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2820 | Py_buffer buffer; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2821 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2822 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2823 | if (obj == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2824 | PyErr_BadInternalCall(); |
| 2825 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2826 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2827 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2828 | /* Decoding bytes objects is the most common case and should be fast */ |
| 2829 | if (PyBytes_Check(obj)) { |
| 2830 | if (PyBytes_GET_SIZE(obj) == 0) { |
| 2831 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 2832 | v = unicode_empty; |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2833 | } |
| 2834 | else { |
| 2835 | v = PyUnicode_Decode( |
| 2836 | PyBytes_AS_STRING(obj), PyBytes_GET_SIZE(obj), |
| 2837 | encoding, errors); |
| 2838 | } |
| 2839 | return v; |
| 2840 | } |
| 2841 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2842 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2843 | PyErr_SetString(PyExc_TypeError, |
| 2844 | "decoding str is not supported"); |
| 2845 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2846 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2847 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2848 | /* Retrieve a bytes buffer view through the PEP 3118 buffer interface */ |
| 2849 | if (PyObject_GetBuffer(obj, &buffer, PyBUF_SIMPLE) < 0) { |
| 2850 | PyErr_Format(PyExc_TypeError, |
| 2851 | "coercing to str: need bytes, bytearray " |
| 2852 | "or buffer-like object, %.80s found", |
| 2853 | Py_TYPE(obj)->tp_name); |
| 2854 | return NULL; |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 2855 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2856 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2857 | if (buffer.len == 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2858 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 2859 | v = unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2860 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2861 | else |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2862 | v = PyUnicode_Decode((char*) buffer.buf, buffer.len, encoding, errors); |
Marc-André Lemburg | ad7c98e | 2001-01-17 17:09:53 +0000 | [diff] [blame] | 2863 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2864 | PyBuffer_Release(&buffer); |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2865 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2866 | } |
| 2867 | |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2868 | /* Convert encoding to lower case and replace '_' with '-' in order to |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2869 | catch e.g. UTF_8. Return 0 on error (encoding is longer than lower_len-1), |
| 2870 | 1 on success. */ |
| 2871 | static int |
| 2872 | normalize_encoding(const char *encoding, |
| 2873 | char *lower, |
| 2874 | size_t lower_len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2875 | { |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2876 | const char *e; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2877 | char *l; |
| 2878 | char *l_end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2879 | |
Benjamin Peterson | 7a6debe | 2011-10-15 09:25:28 -0400 | [diff] [blame] | 2880 | if (encoding == NULL) { |
| 2881 | strcpy(lower, "utf-8"); |
| 2882 | return 1; |
| 2883 | } |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2884 | e = encoding; |
| 2885 | l = lower; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2886 | l_end = &lower[lower_len - 1]; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2887 | while (*e) { |
| 2888 | if (l == l_end) |
| 2889 | return 0; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 2890 | if (Py_ISUPPER(*e)) { |
| 2891 | *l++ = Py_TOLOWER(*e++); |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2892 | } |
| 2893 | else if (*e == '_') { |
| 2894 | *l++ = '-'; |
| 2895 | e++; |
| 2896 | } |
| 2897 | else { |
| 2898 | *l++ = *e++; |
| 2899 | } |
| 2900 | } |
| 2901 | *l = '\0'; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2902 | return 1; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2903 | } |
| 2904 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2905 | PyObject * |
| 2906 | PyUnicode_Decode(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2907 | Py_ssize_t size, |
| 2908 | const char *encoding, |
| 2909 | const char *errors) |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2910 | { |
| 2911 | PyObject *buffer = NULL, *unicode; |
| 2912 | Py_buffer info; |
| 2913 | char lower[11]; /* Enough for any encoding shortcut */ |
| 2914 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 2915 | /* Shortcuts for common default encodings */ |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2916 | if (normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2917 | if ((strcmp(lower, "utf-8") == 0) || |
| 2918 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2919 | return PyUnicode_DecodeUTF8(s, size, errors); |
| 2920 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2921 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2922 | (strcmp(lower, "iso-8859-1") == 0)) |
| 2923 | return PyUnicode_DecodeLatin1(s, size, errors); |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 2924 | #ifdef HAVE_MBCS |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2925 | else if (strcmp(lower, "mbcs") == 0) |
| 2926 | return PyUnicode_DecodeMBCS(s, size, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 2927 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2928 | else if (strcmp(lower, "ascii") == 0) |
| 2929 | return PyUnicode_DecodeASCII(s, size, errors); |
| 2930 | else if (strcmp(lower, "utf-16") == 0) |
| 2931 | return PyUnicode_DecodeUTF16(s, size, errors, 0); |
| 2932 | else if (strcmp(lower, "utf-32") == 0) |
| 2933 | return PyUnicode_DecodeUTF32(s, size, errors, 0); |
| 2934 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2935 | |
| 2936 | /* Decode via the codec registry */ |
Guido van Rossum | be801ac | 2007-10-08 03:32:34 +0000 | [diff] [blame] | 2937 | buffer = NULL; |
Antoine Pitrou | c3b3924 | 2009-01-03 16:59:18 +0000 | [diff] [blame] | 2938 | 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] | 2939 | goto onError; |
Antoine Pitrou | ee58fa4 | 2008-08-19 18:22:14 +0000 | [diff] [blame] | 2940 | buffer = PyMemoryView_FromBuffer(&info); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2941 | if (buffer == NULL) |
| 2942 | goto onError; |
| 2943 | unicode = PyCodec_Decode(buffer, encoding, errors); |
| 2944 | if (unicode == NULL) |
| 2945 | goto onError; |
| 2946 | if (!PyUnicode_Check(unicode)) { |
| 2947 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 2948 | "decoder did not return a str object (type=%.400s)", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2949 | Py_TYPE(unicode)->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2950 | Py_DECREF(unicode); |
| 2951 | goto onError; |
| 2952 | } |
| 2953 | Py_DECREF(buffer); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 2954 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 2955 | if (_PyUnicode_READY_REPLACE(&unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2956 | Py_DECREF(unicode); |
| 2957 | return NULL; |
| 2958 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 2959 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2960 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2961 | return unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2962 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2963 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2964 | Py_XDECREF(buffer); |
| 2965 | return NULL; |
| 2966 | } |
| 2967 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2968 | PyObject * |
| 2969 | PyUnicode_AsDecodedObject(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2970 | const char *encoding, |
| 2971 | const char *errors) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2972 | { |
| 2973 | PyObject *v; |
| 2974 | |
| 2975 | if (!PyUnicode_Check(unicode)) { |
| 2976 | PyErr_BadArgument(); |
| 2977 | goto onError; |
| 2978 | } |
| 2979 | |
| 2980 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2981 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2982 | |
| 2983 | /* Decode via the codec registry */ |
| 2984 | v = PyCodec_Decode(unicode, encoding, errors); |
| 2985 | if (v == NULL) |
| 2986 | goto onError; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2987 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2988 | return v; |
| 2989 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2990 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2991 | return NULL; |
| 2992 | } |
| 2993 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2994 | PyObject * |
| 2995 | PyUnicode_AsDecodedUnicode(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2996 | const char *encoding, |
| 2997 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2998 | { |
| 2999 | PyObject *v; |
| 3000 | |
| 3001 | if (!PyUnicode_Check(unicode)) { |
| 3002 | PyErr_BadArgument(); |
| 3003 | goto onError; |
| 3004 | } |
| 3005 | |
| 3006 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3007 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3008 | |
| 3009 | /* Decode via the codec registry */ |
| 3010 | v = PyCodec_Decode(unicode, encoding, errors); |
| 3011 | if (v == NULL) |
| 3012 | goto onError; |
| 3013 | if (!PyUnicode_Check(v)) { |
| 3014 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3015 | "decoder did not return a str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3016 | Py_TYPE(v)->tp_name); |
| 3017 | Py_DECREF(v); |
| 3018 | goto onError; |
| 3019 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 3020 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3021 | return v; |
| 3022 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3023 | onError: |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3024 | return NULL; |
| 3025 | } |
| 3026 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3027 | PyObject * |
| 3028 | PyUnicode_Encode(const Py_UNICODE *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3029 | Py_ssize_t size, |
| 3030 | const char *encoding, |
| 3031 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3032 | { |
| 3033 | PyObject *v, *unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3034 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3035 | unicode = PyUnicode_FromUnicode(s, size); |
| 3036 | if (unicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3037 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3038 | v = PyUnicode_AsEncodedString(unicode, encoding, errors); |
| 3039 | Py_DECREF(unicode); |
| 3040 | return v; |
| 3041 | } |
| 3042 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3043 | PyObject * |
| 3044 | PyUnicode_AsEncodedObject(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3045 | const char *encoding, |
| 3046 | const char *errors) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3047 | { |
| 3048 | PyObject *v; |
| 3049 | |
| 3050 | if (!PyUnicode_Check(unicode)) { |
| 3051 | PyErr_BadArgument(); |
| 3052 | goto onError; |
| 3053 | } |
| 3054 | |
| 3055 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3056 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3057 | |
| 3058 | /* Encode via the codec registry */ |
| 3059 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3060 | if (v == NULL) |
| 3061 | goto onError; |
| 3062 | return v; |
| 3063 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3064 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3065 | return NULL; |
| 3066 | } |
| 3067 | |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3068 | PyObject * |
| 3069 | PyUnicode_EncodeFSDefault(PyObject *unicode) |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3070 | { |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3071 | #ifdef HAVE_MBCS |
Victor Stinner | ac931b1 | 2011-11-20 18:27:03 +0100 | [diff] [blame] | 3072 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, NULL); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3073 | #elif defined(__APPLE__) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3074 | return _PyUnicode_AsUTF8String(unicode, "surrogateescape"); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3075 | #else |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 3076 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 3077 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 3078 | cannot use it to encode and decode filenames before it is loaded. Load |
| 3079 | the Python codec requires to encode at least its own filename. Use the C |
| 3080 | version of the locale codec until the codec registry is initialized and |
| 3081 | the Python codec is loaded. |
| 3082 | |
| 3083 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 3084 | cannot only rely on it: check also interp->fscodec_initialized for |
| 3085 | subinterpreters. */ |
| 3086 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3087 | return PyUnicode_AsEncodedString(unicode, |
| 3088 | Py_FileSystemDefaultEncoding, |
| 3089 | "surrogateescape"); |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 3090 | } |
| 3091 | else { |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3092 | /* locale encoding with surrogateescape */ |
| 3093 | wchar_t *wchar; |
| 3094 | char *bytes; |
| 3095 | PyObject *bytes_obj; |
Victor Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 3096 | size_t error_pos; |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3097 | |
| 3098 | wchar = PyUnicode_AsWideCharString(unicode, NULL); |
| 3099 | if (wchar == NULL) |
| 3100 | return NULL; |
Victor Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 3101 | bytes = _Py_wchar2char(wchar, &error_pos); |
| 3102 | if (bytes == NULL) { |
| 3103 | if (error_pos != (size_t)-1) { |
| 3104 | char *errmsg = strerror(errno); |
| 3105 | PyObject *exc = NULL; |
| 3106 | if (errmsg == NULL) |
| 3107 | errmsg = "Py_wchar2char() failed"; |
| 3108 | raise_encode_exception(&exc, |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 3109 | "filesystemencoding", unicode, |
Victor Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 3110 | error_pos, error_pos+1, |
| 3111 | errmsg); |
| 3112 | Py_XDECREF(exc); |
| 3113 | } |
| 3114 | else |
| 3115 | PyErr_NoMemory(); |
| 3116 | PyMem_Free(wchar); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3117 | return NULL; |
Victor Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 3118 | } |
| 3119 | PyMem_Free(wchar); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3120 | |
| 3121 | bytes_obj = PyBytes_FromString(bytes); |
| 3122 | PyMem_Free(bytes); |
| 3123 | return bytes_obj; |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 3124 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3125 | #endif |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3126 | } |
| 3127 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3128 | PyObject * |
| 3129 | PyUnicode_AsEncodedString(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3130 | const char *encoding, |
| 3131 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3132 | { |
| 3133 | PyObject *v; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3134 | char lower[11]; /* Enough for any encoding shortcut */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3135 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3136 | if (!PyUnicode_Check(unicode)) { |
| 3137 | PyErr_BadArgument(); |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3138 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3139 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3140 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3141 | /* Shortcuts for common default encodings */ |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3142 | if (normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3143 | if ((strcmp(lower, "utf-8") == 0) || |
| 3144 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 3145 | { |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3146 | if (errors == NULL || strcmp(errors, "strict") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3147 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3148 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3149 | return _PyUnicode_AsUTF8String(unicode, errors); |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 3150 | } |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3151 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3152 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3153 | (strcmp(lower, "iso-8859-1") == 0)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3154 | return _PyUnicode_AsLatin1String(unicode, errors); |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3155 | #ifdef HAVE_MBCS |
Victor Stinner | ac931b1 | 2011-11-20 18:27:03 +0100 | [diff] [blame] | 3156 | else if (strcmp(lower, "mbcs") == 0) |
| 3157 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 3158 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3159 | else if (strcmp(lower, "ascii") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3160 | return _PyUnicode_AsASCIIString(unicode, errors); |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3161 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3162 | |
| 3163 | /* Encode via the codec registry */ |
| 3164 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3165 | if (v == NULL) |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3166 | return NULL; |
| 3167 | |
| 3168 | /* The normal path */ |
| 3169 | if (PyBytes_Check(v)) |
| 3170 | return v; |
| 3171 | |
| 3172 | /* 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] | 3173 | if (PyByteArray_Check(v)) { |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 3174 | int error; |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3175 | PyObject *b; |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 3176 | |
| 3177 | error = PyErr_WarnFormat(PyExc_RuntimeWarning, 1, |
| 3178 | "encoder %s returned bytearray instead of bytes", |
| 3179 | encoding); |
| 3180 | if (error) { |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3181 | Py_DECREF(v); |
| 3182 | return NULL; |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3183 | } |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3184 | |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3185 | b = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(v), Py_SIZE(v)); |
| 3186 | Py_DECREF(v); |
| 3187 | return b; |
| 3188 | } |
| 3189 | |
| 3190 | PyErr_Format(PyExc_TypeError, |
| 3191 | "encoder did not return a bytes object (type=%.400s)", |
| 3192 | Py_TYPE(v)->tp_name); |
| 3193 | Py_DECREF(v); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3194 | return NULL; |
| 3195 | } |
| 3196 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3197 | PyObject * |
| 3198 | PyUnicode_AsEncodedUnicode(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3199 | const char *encoding, |
| 3200 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3201 | { |
| 3202 | PyObject *v; |
| 3203 | |
| 3204 | if (!PyUnicode_Check(unicode)) { |
| 3205 | PyErr_BadArgument(); |
| 3206 | goto onError; |
| 3207 | } |
| 3208 | |
| 3209 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3210 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3211 | |
| 3212 | /* Encode via the codec registry */ |
| 3213 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3214 | if (v == NULL) |
| 3215 | goto onError; |
| 3216 | if (!PyUnicode_Check(v)) { |
| 3217 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3218 | "encoder did not return an str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3219 | Py_TYPE(v)->tp_name); |
| 3220 | Py_DECREF(v); |
| 3221 | goto onError; |
| 3222 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3223 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3224 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3225 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3226 | return NULL; |
| 3227 | } |
| 3228 | |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3229 | PyObject* |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3230 | PyUnicode_DecodeFSDefault(const char *s) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3231 | Py_ssize_t size = (Py_ssize_t)strlen(s); |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3232 | return PyUnicode_DecodeFSDefaultAndSize(s, size); |
| 3233 | } |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3234 | |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3235 | PyObject* |
| 3236 | PyUnicode_DecodeFSDefaultAndSize(const char *s, Py_ssize_t size) |
| 3237 | { |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3238 | #ifdef HAVE_MBCS |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3239 | return PyUnicode_DecodeMBCS(s, size, NULL); |
| 3240 | #elif defined(__APPLE__) |
| 3241 | return PyUnicode_DecodeUTF8(s, size, "surrogateescape"); |
| 3242 | #else |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 3243 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 3244 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 3245 | cannot use it to encode and decode filenames before it is loaded. Load |
| 3246 | the Python codec requires to encode at least its own filename. Use the C |
| 3247 | version of the locale codec until the codec registry is initialized and |
| 3248 | the Python codec is loaded. |
| 3249 | |
| 3250 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 3251 | cannot only rely on it: check also interp->fscodec_initialized for |
| 3252 | subinterpreters. */ |
| 3253 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3254 | return PyUnicode_Decode(s, size, |
| 3255 | Py_FileSystemDefaultEncoding, |
Victor Stinner | b9a20ad | 2010-04-30 16:37:52 +0000 | [diff] [blame] | 3256 | "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3257 | } |
| 3258 | else { |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3259 | /* locale encoding with surrogateescape */ |
| 3260 | wchar_t *wchar; |
| 3261 | PyObject *unicode; |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 3262 | size_t len; |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3263 | |
| 3264 | if (s[size] != '\0' || size != strlen(s)) { |
| 3265 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
| 3266 | return NULL; |
| 3267 | } |
| 3268 | |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 3269 | wchar = _Py_char2wchar(s, &len); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3270 | if (wchar == NULL) |
Victor Stinner | d5af0a5 | 2010-11-08 23:34:29 +0000 | [diff] [blame] | 3271 | return PyErr_NoMemory(); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3272 | |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 3273 | unicode = PyUnicode_FromWideChar(wchar, len); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3274 | PyMem_Free(wchar); |
| 3275 | return unicode; |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3276 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3277 | #endif |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3278 | } |
| 3279 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3280 | |
| 3281 | int |
| 3282 | PyUnicode_FSConverter(PyObject* arg, void* addr) |
| 3283 | { |
| 3284 | PyObject *output = NULL; |
| 3285 | Py_ssize_t size; |
| 3286 | void *data; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3287 | if (arg == NULL) { |
| 3288 | Py_DECREF(*(PyObject**)addr); |
| 3289 | return 1; |
| 3290 | } |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 3291 | if (PyBytes_Check(arg)) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3292 | output = arg; |
| 3293 | Py_INCREF(output); |
| 3294 | } |
| 3295 | else { |
| 3296 | arg = PyUnicode_FromObject(arg); |
| 3297 | if (!arg) |
| 3298 | return 0; |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3299 | output = PyUnicode_EncodeFSDefault(arg); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3300 | Py_DECREF(arg); |
| 3301 | if (!output) |
| 3302 | return 0; |
| 3303 | if (!PyBytes_Check(output)) { |
| 3304 | Py_DECREF(output); |
| 3305 | PyErr_SetString(PyExc_TypeError, "encoder failed to return bytes"); |
| 3306 | return 0; |
| 3307 | } |
| 3308 | } |
Victor Stinner | 0ea2a46 | 2010-04-30 00:22:08 +0000 | [diff] [blame] | 3309 | size = PyBytes_GET_SIZE(output); |
| 3310 | data = PyBytes_AS_STRING(output); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3311 | if (size != strlen(data)) { |
Benjamin Peterson | 7a6b44a | 2011-08-18 13:51:47 -0500 | [diff] [blame] | 3312 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3313 | Py_DECREF(output); |
| 3314 | return 0; |
| 3315 | } |
| 3316 | *(PyObject**)addr = output; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3317 | return Py_CLEANUP_SUPPORTED; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3318 | } |
| 3319 | |
| 3320 | |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3321 | int |
| 3322 | PyUnicode_FSDecoder(PyObject* arg, void* addr) |
| 3323 | { |
| 3324 | PyObject *output = NULL; |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3325 | if (arg == NULL) { |
| 3326 | Py_DECREF(*(PyObject**)addr); |
| 3327 | return 1; |
| 3328 | } |
| 3329 | if (PyUnicode_Check(arg)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3330 | if (PyUnicode_READY(arg)) |
| 3331 | return 0; |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3332 | output = arg; |
| 3333 | Py_INCREF(output); |
| 3334 | } |
| 3335 | else { |
| 3336 | arg = PyBytes_FromObject(arg); |
| 3337 | if (!arg) |
| 3338 | return 0; |
| 3339 | output = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AS_STRING(arg), |
| 3340 | PyBytes_GET_SIZE(arg)); |
| 3341 | Py_DECREF(arg); |
| 3342 | if (!output) |
| 3343 | return 0; |
| 3344 | if (!PyUnicode_Check(output)) { |
| 3345 | Py_DECREF(output); |
| 3346 | PyErr_SetString(PyExc_TypeError, "decoder failed to return unicode"); |
| 3347 | return 0; |
| 3348 | } |
| 3349 | } |
Victor Stinner | 065836e | 2011-10-27 01:56:33 +0200 | [diff] [blame] | 3350 | if (PyUnicode_READY(output) < 0) { |
| 3351 | Py_DECREF(output); |
| 3352 | return 0; |
| 3353 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3354 | if (findchar(PyUnicode_DATA(output), PyUnicode_KIND(output), |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 3355 | PyUnicode_GET_LENGTH(output), 0, 1) >= 0) { |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3356 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
| 3357 | Py_DECREF(output); |
| 3358 | return 0; |
| 3359 | } |
| 3360 | *(PyObject**)addr = output; |
| 3361 | return Py_CLEANUP_SUPPORTED; |
| 3362 | } |
| 3363 | |
| 3364 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3365 | char* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3366 | PyUnicode_AsUTF8AndSize(PyObject *unicode, Py_ssize_t *psize) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3367 | { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 3368 | PyObject *bytes; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3369 | |
Neal Norwitz | e0a0a6e | 2007-08-25 01:04:21 +0000 | [diff] [blame] | 3370 | if (!PyUnicode_Check(unicode)) { |
| 3371 | PyErr_BadArgument(); |
| 3372 | return NULL; |
| 3373 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3374 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3375 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3376 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3377 | if (PyUnicode_UTF8(unicode) == NULL) { |
| 3378 | assert(!PyUnicode_IS_COMPACT_ASCII(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3379 | bytes = _PyUnicode_AsUTF8String(unicode, "strict"); |
| 3380 | if (bytes == NULL) |
| 3381 | return NULL; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3382 | _PyUnicode_UTF8(unicode) = PyObject_MALLOC(PyBytes_GET_SIZE(bytes) + 1); |
| 3383 | if (_PyUnicode_UTF8(unicode) == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3384 | Py_DECREF(bytes); |
| 3385 | return NULL; |
| 3386 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3387 | _PyUnicode_UTF8_LENGTH(unicode) = PyBytes_GET_SIZE(bytes); |
| 3388 | Py_MEMCPY(_PyUnicode_UTF8(unicode), |
| 3389 | PyBytes_AS_STRING(bytes), |
| 3390 | _PyUnicode_UTF8_LENGTH(unicode) + 1); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3391 | Py_DECREF(bytes); |
| 3392 | } |
| 3393 | |
| 3394 | if (psize) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3395 | *psize = PyUnicode_UTF8_LENGTH(unicode); |
| 3396 | return PyUnicode_UTF8(unicode); |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 3397 | } |
| 3398 | |
| 3399 | char* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3400 | PyUnicode_AsUTF8(PyObject *unicode) |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 3401 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3402 | return PyUnicode_AsUTF8AndSize(unicode, NULL); |
| 3403 | } |
| 3404 | |
| 3405 | #ifdef Py_DEBUG |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 3406 | static int unicode_as_unicode_calls = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3407 | #endif |
| 3408 | |
| 3409 | |
| 3410 | Py_UNICODE * |
| 3411 | PyUnicode_AsUnicodeAndSize(PyObject *unicode, Py_ssize_t *size) |
| 3412 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3413 | const unsigned char *one_byte; |
| 3414 | #if SIZEOF_WCHAR_T == 4 |
| 3415 | const Py_UCS2 *two_bytes; |
| 3416 | #else |
| 3417 | const Py_UCS4 *four_bytes; |
| 3418 | const Py_UCS4 *ucs4_end; |
| 3419 | Py_ssize_t num_surrogates; |
| 3420 | #endif |
| 3421 | wchar_t *w; |
| 3422 | wchar_t *wchar_end; |
| 3423 | |
| 3424 | if (!PyUnicode_Check(unicode)) { |
| 3425 | PyErr_BadArgument(); |
| 3426 | return NULL; |
| 3427 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3428 | if (_PyUnicode_WSTR(unicode) == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3429 | /* Non-ASCII compact unicode object */ |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3430 | assert(_PyUnicode_KIND(unicode) != 0); |
| 3431 | assert(PyUnicode_IS_READY(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3432 | |
| 3433 | #ifdef Py_DEBUG |
| 3434 | ++unicode_as_unicode_calls; |
| 3435 | #endif |
| 3436 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3437 | if (PyUnicode_KIND(unicode) == PyUnicode_4BYTE_KIND) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3438 | #if SIZEOF_WCHAR_T == 2 |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3439 | four_bytes = PyUnicode_4BYTE_DATA(unicode); |
| 3440 | ucs4_end = four_bytes + _PyUnicode_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3441 | num_surrogates = 0; |
| 3442 | |
| 3443 | for (; four_bytes < ucs4_end; ++four_bytes) { |
| 3444 | if (*four_bytes > 0xFFFF) |
| 3445 | ++num_surrogates; |
| 3446 | } |
| 3447 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3448 | _PyUnicode_WSTR(unicode) = (wchar_t *) PyObject_MALLOC( |
| 3449 | sizeof(wchar_t) * (_PyUnicode_LENGTH(unicode) + 1 + num_surrogates)); |
| 3450 | if (!_PyUnicode_WSTR(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3451 | PyErr_NoMemory(); |
| 3452 | return NULL; |
| 3453 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3454 | _PyUnicode_WSTR_LENGTH(unicode) = _PyUnicode_LENGTH(unicode) + num_surrogates; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3455 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3456 | w = _PyUnicode_WSTR(unicode); |
| 3457 | wchar_end = w + _PyUnicode_WSTR_LENGTH(unicode); |
| 3458 | four_bytes = PyUnicode_4BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3459 | for (; four_bytes < ucs4_end; ++four_bytes, ++w) { |
| 3460 | if (*four_bytes > 0xFFFF) { |
| 3461 | /* encode surrogate pair in this case */ |
| 3462 | *w++ = 0xD800 | ((*four_bytes - 0x10000) >> 10); |
| 3463 | *w = 0xDC00 | ((*four_bytes - 0x10000) & 0x3FF); |
| 3464 | } |
| 3465 | else |
| 3466 | *w = *four_bytes; |
| 3467 | |
| 3468 | if (w > wchar_end) { |
| 3469 | assert(0 && "Miscalculated string end"); |
| 3470 | } |
| 3471 | } |
| 3472 | *w = 0; |
| 3473 | #else |
| 3474 | /* sizeof(wchar_t) == 4 */ |
| 3475 | Py_FatalError("Impossible unicode object state, wstr and str " |
| 3476 | "should share memory already."); |
| 3477 | return NULL; |
| 3478 | #endif |
| 3479 | } |
| 3480 | else { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3481 | _PyUnicode_WSTR(unicode) = (wchar_t *) PyObject_MALLOC(sizeof(wchar_t) * |
| 3482 | (_PyUnicode_LENGTH(unicode) + 1)); |
| 3483 | if (!_PyUnicode_WSTR(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3484 | PyErr_NoMemory(); |
| 3485 | return NULL; |
| 3486 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3487 | if (!PyUnicode_IS_COMPACT_ASCII(unicode)) |
| 3488 | _PyUnicode_WSTR_LENGTH(unicode) = _PyUnicode_LENGTH(unicode); |
| 3489 | w = _PyUnicode_WSTR(unicode); |
| 3490 | wchar_end = w + _PyUnicode_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3491 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3492 | if (PyUnicode_KIND(unicode) == PyUnicode_1BYTE_KIND) { |
| 3493 | one_byte = PyUnicode_1BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3494 | for (; w < wchar_end; ++one_byte, ++w) |
| 3495 | *w = *one_byte; |
| 3496 | /* null-terminate the wstr */ |
| 3497 | *w = 0; |
| 3498 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3499 | else if (PyUnicode_KIND(unicode) == PyUnicode_2BYTE_KIND) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3500 | #if SIZEOF_WCHAR_T == 4 |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3501 | two_bytes = PyUnicode_2BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3502 | for (; w < wchar_end; ++two_bytes, ++w) |
| 3503 | *w = *two_bytes; |
| 3504 | /* null-terminate the wstr */ |
| 3505 | *w = 0; |
| 3506 | #else |
| 3507 | /* sizeof(wchar_t) == 2 */ |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3508 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 3509 | _PyUnicode_WSTR(unicode) = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3510 | Py_FatalError("Impossible unicode object state, wstr " |
| 3511 | "and str should share memory already."); |
| 3512 | return NULL; |
| 3513 | #endif |
| 3514 | } |
| 3515 | else { |
| 3516 | assert(0 && "This should never happen."); |
| 3517 | } |
| 3518 | } |
| 3519 | } |
| 3520 | if (size != NULL) |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3521 | *size = PyUnicode_WSTR_LENGTH(unicode); |
| 3522 | return _PyUnicode_WSTR(unicode); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3523 | } |
| 3524 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3525 | Py_UNICODE * |
| 3526 | PyUnicode_AsUnicode(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3527 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3528 | return PyUnicode_AsUnicodeAndSize(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3529 | } |
| 3530 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3531 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3532 | Py_ssize_t |
| 3533 | PyUnicode_GetSize(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3534 | { |
| 3535 | if (!PyUnicode_Check(unicode)) { |
| 3536 | PyErr_BadArgument(); |
| 3537 | goto onError; |
| 3538 | } |
| 3539 | return PyUnicode_GET_SIZE(unicode); |
| 3540 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3541 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3542 | return -1; |
| 3543 | } |
| 3544 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3545 | Py_ssize_t |
| 3546 | PyUnicode_GetLength(PyObject *unicode) |
| 3547 | { |
Victor Stinner | 5a706cf | 2011-10-02 00:36:53 +0200 | [diff] [blame] | 3548 | if (!PyUnicode_Check(unicode) || PyUnicode_READY(unicode) == -1) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3549 | PyErr_BadArgument(); |
| 3550 | return -1; |
| 3551 | } |
| 3552 | |
| 3553 | return PyUnicode_GET_LENGTH(unicode); |
| 3554 | } |
| 3555 | |
| 3556 | Py_UCS4 |
| 3557 | PyUnicode_ReadChar(PyObject *unicode, Py_ssize_t index) |
| 3558 | { |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 3559 | if (!PyUnicode_Check(unicode) || PyUnicode_READY(unicode) == -1) { |
| 3560 | PyErr_BadArgument(); |
| 3561 | return (Py_UCS4)-1; |
| 3562 | } |
| 3563 | if (index < 0 || index >= _PyUnicode_LENGTH(unicode)) { |
| 3564 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3565 | return (Py_UCS4)-1; |
| 3566 | } |
| 3567 | return PyUnicode_READ_CHAR(unicode, index); |
| 3568 | } |
| 3569 | |
| 3570 | int |
| 3571 | PyUnicode_WriteChar(PyObject *unicode, Py_ssize_t index, Py_UCS4 ch) |
| 3572 | { |
| 3573 | if (!PyUnicode_Check(unicode) || !PyUnicode_IS_COMPACT(unicode)) { |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 3574 | PyErr_BadArgument(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3575 | return -1; |
| 3576 | } |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 3577 | if (index < 0 || index >= _PyUnicode_LENGTH(unicode)) { |
| 3578 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 3579 | return -1; |
| 3580 | } |
| 3581 | if (_PyUnicode_Dirty(unicode)) |
| 3582 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3583 | PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode), |
| 3584 | index, ch); |
| 3585 | return 0; |
| 3586 | } |
| 3587 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3588 | const char * |
| 3589 | PyUnicode_GetDefaultEncoding(void) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3590 | { |
Victor Stinner | 42cb462 | 2010-09-01 19:39:01 +0000 | [diff] [blame] | 3591 | return "utf-8"; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3592 | } |
| 3593 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 3594 | /* create or adjust a UnicodeDecodeError */ |
| 3595 | static void |
| 3596 | make_decode_exception(PyObject **exceptionObject, |
| 3597 | const char *encoding, |
| 3598 | const char *input, Py_ssize_t length, |
| 3599 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 3600 | const char *reason) |
| 3601 | { |
| 3602 | if (*exceptionObject == NULL) { |
| 3603 | *exceptionObject = PyUnicodeDecodeError_Create( |
| 3604 | encoding, input, length, startpos, endpos, reason); |
| 3605 | } |
| 3606 | else { |
| 3607 | if (PyUnicodeDecodeError_SetStart(*exceptionObject, startpos)) |
| 3608 | goto onError; |
| 3609 | if (PyUnicodeDecodeError_SetEnd(*exceptionObject, endpos)) |
| 3610 | goto onError; |
| 3611 | if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) |
| 3612 | goto onError; |
| 3613 | } |
| 3614 | return; |
| 3615 | |
| 3616 | onError: |
| 3617 | Py_DECREF(*exceptionObject); |
| 3618 | *exceptionObject = NULL; |
| 3619 | } |
| 3620 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3621 | /* error handling callback helper: |
| 3622 | build arguments, call the callback and check the arguments, |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 3623 | if no exception occurred, copy the replacement to the output |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3624 | and adjust various state variables. |
| 3625 | return 0 on success, -1 on error |
| 3626 | */ |
| 3627 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3628 | static int |
| 3629 | unicode_decode_call_errorhandler(const char *errors, PyObject **errorHandler, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3630 | const char *encoding, const char *reason, |
| 3631 | const char **input, const char **inend, Py_ssize_t *startinpos, |
| 3632 | Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 3633 | PyObject **output, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3634 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3635 | 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] | 3636 | |
| 3637 | PyObject *restuple = NULL; |
| 3638 | PyObject *repunicode = NULL; |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 3639 | Py_ssize_t outsize; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3640 | Py_ssize_t insize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3641 | Py_ssize_t requiredsize; |
| 3642 | Py_ssize_t newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3643 | PyObject *inputobj = NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3644 | int res = -1; |
| 3645 | |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 3646 | if (_PyUnicode_KIND(*output) != PyUnicode_WCHAR_KIND) |
| 3647 | outsize = PyUnicode_GET_LENGTH(*output); |
| 3648 | else |
| 3649 | outsize = _PyUnicode_WSTR_LENGTH(*output); |
| 3650 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3651 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3652 | *errorHandler = PyCodec_LookupError(errors); |
| 3653 | if (*errorHandler == NULL) |
| 3654 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3655 | } |
| 3656 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 3657 | make_decode_exception(exceptionObject, |
| 3658 | encoding, |
| 3659 | *input, *inend - *input, |
| 3660 | *startinpos, *endinpos, |
| 3661 | reason); |
| 3662 | if (*exceptionObject == NULL) |
| 3663 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3664 | |
| 3665 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 3666 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3667 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3668 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 3669 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3670 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3671 | } |
| 3672 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3673 | goto onError; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 3674 | if (PyUnicode_READY(repunicode) < 0) |
| 3675 | goto onError; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3676 | |
| 3677 | /* Copy back the bytes variables, which might have been modified by the |
| 3678 | callback */ |
| 3679 | inputobj = PyUnicodeDecodeError_GetObject(*exceptionObject); |
| 3680 | if (!inputobj) |
| 3681 | goto onError; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3682 | if (!PyBytes_Check(inputobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3683 | PyErr_Format(PyExc_TypeError, "exception attribute object must be bytes"); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3684 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3685 | *input = PyBytes_AS_STRING(inputobj); |
| 3686 | insize = PyBytes_GET_SIZE(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3687 | *inend = *input + insize; |
Walter Dörwald | 36f938f | 2007-08-10 10:11:43 +0000 | [diff] [blame] | 3688 | /* we can DECREF safely, as the exception has another reference, |
| 3689 | so the object won't go away. */ |
| 3690 | Py_DECREF(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3691 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3692 | if (newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3693 | newpos = insize+newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 3694 | if (newpos<0 || newpos>insize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3695 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", newpos); |
| 3696 | goto onError; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 3697 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3698 | |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 3699 | if (_PyUnicode_KIND(*output) != PyUnicode_WCHAR_KIND) { |
| 3700 | /* need more space? (at least enough for what we |
| 3701 | have+the replacement+the rest of the string (starting |
| 3702 | at the new input position), so we won't have to check space |
| 3703 | when there are no errors in the rest of the string) */ |
| 3704 | Py_ssize_t replen = PyUnicode_GET_LENGTH(repunicode); |
| 3705 | requiredsize = *outpos + replen + insize-newpos; |
| 3706 | if (requiredsize > outsize) { |
| 3707 | if (requiredsize<2*outsize) |
| 3708 | requiredsize = 2*outsize; |
| 3709 | if (unicode_resize(output, requiredsize) < 0) |
| 3710 | goto onError; |
| 3711 | } |
| 3712 | if (unicode_widen(output, PyUnicode_MAX_CHAR_VALUE(repunicode)) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3713 | goto onError; |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 3714 | copy_characters(*output, *outpos, repunicode, 0, replen); |
| 3715 | *outpos += replen; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3716 | } |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 3717 | else { |
| 3718 | wchar_t *repwstr; |
| 3719 | Py_ssize_t repwlen; |
| 3720 | repwstr = PyUnicode_AsUnicodeAndSize(repunicode, &repwlen); |
| 3721 | if (repwstr == NULL) |
| 3722 | goto onError; |
| 3723 | /* need more space? (at least enough for what we |
| 3724 | have+the replacement+the rest of the string (starting |
| 3725 | at the new input position), so we won't have to check space |
| 3726 | when there are no errors in the rest of the string) */ |
| 3727 | requiredsize = *outpos + repwlen + insize-newpos; |
| 3728 | if (requiredsize > outsize) { |
| 3729 | if (requiredsize < 2*outsize) |
| 3730 | requiredsize = 2*outsize; |
| 3731 | if (unicode_resize(output, requiredsize) < 0) |
| 3732 | goto onError; |
| 3733 | } |
| 3734 | wcsncpy(_PyUnicode_WSTR(*output) + *outpos, repwstr, repwlen); |
| 3735 | *outpos += repwlen; |
| 3736 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3737 | *endinpos = newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3738 | *inptr = *input + newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3739 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3740 | /* we made it! */ |
| 3741 | res = 0; |
| 3742 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3743 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3744 | Py_XDECREF(restuple); |
| 3745 | return res; |
| 3746 | } |
| 3747 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3748 | /* --- UTF-7 Codec -------------------------------------------------------- */ |
| 3749 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3750 | /* See RFC2152 for details. We encode conservatively and decode liberally. */ |
| 3751 | |
| 3752 | /* Three simple macros defining base-64. */ |
| 3753 | |
| 3754 | /* Is c a base-64 character? */ |
| 3755 | |
| 3756 | #define IS_BASE64(c) \ |
| 3757 | (((c) >= 'A' && (c) <= 'Z') || \ |
| 3758 | ((c) >= 'a' && (c) <= 'z') || \ |
| 3759 | ((c) >= '0' && (c) <= '9') || \ |
| 3760 | (c) == '+' || (c) == '/') |
| 3761 | |
| 3762 | /* given that c is a base-64 character, what is its base-64 value? */ |
| 3763 | |
| 3764 | #define FROM_BASE64(c) \ |
| 3765 | (((c) >= 'A' && (c) <= 'Z') ? (c) - 'A' : \ |
| 3766 | ((c) >= 'a' && (c) <= 'z') ? (c) - 'a' + 26 : \ |
| 3767 | ((c) >= '0' && (c) <= '9') ? (c) - '0' + 52 : \ |
| 3768 | (c) == '+' ? 62 : 63) |
| 3769 | |
| 3770 | /* What is the base-64 character of the bottom 6 bits of n? */ |
| 3771 | |
| 3772 | #define TO_BASE64(n) \ |
| 3773 | ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) |
| 3774 | |
| 3775 | /* DECODE_DIRECT: this byte encountered in a UTF-7 string should be |
| 3776 | * decoded as itself. We are permissive on decoding; the only ASCII |
| 3777 | * byte not decoding to itself is the + which begins a base64 |
| 3778 | * string. */ |
| 3779 | |
| 3780 | #define DECODE_DIRECT(c) \ |
| 3781 | ((c) <= 127 && (c) != '+') |
| 3782 | |
| 3783 | /* The UTF-7 encoder treats ASCII characters differently according to |
| 3784 | * whether they are Set D, Set O, Whitespace, or special (i.e. none of |
| 3785 | * the above). See RFC2152. This array identifies these different |
| 3786 | * sets: |
| 3787 | * 0 : "Set D" |
| 3788 | * alphanumeric and '(),-./:? |
| 3789 | * 1 : "Set O" |
| 3790 | * !"#$%&*;<=>@[]^_`{|} |
| 3791 | * 2 : "whitespace" |
| 3792 | * ht nl cr sp |
| 3793 | * 3 : special (must be base64 encoded) |
| 3794 | * everything else (i.e. +\~ and non-printing codes 0-8 11-12 14-31 127) |
| 3795 | */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3796 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3797 | static |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3798 | char utf7_category[128] = { |
| 3799 | /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */ |
| 3800 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 3, 3, 2, 3, 3, |
| 3801 | /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */ |
| 3802 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 3803 | /* sp ! " # $ % & ' ( ) * + , - . / */ |
| 3804 | 2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 3, 0, 0, 0, 0, |
| 3805 | /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */ |
| 3806 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, |
| 3807 | /* @ A B C D E F G H I J K L M N O */ |
| 3808 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 3809 | /* P Q R S T U V W X Y Z [ \ ] ^ _ */ |
| 3810 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 1, 1, 1, |
| 3811 | /* ` a b c d e f g h i j k l m n o */ |
| 3812 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 3813 | /* p q r s t u v w x y z { | } ~ del */ |
| 3814 | 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] | 3815 | }; |
| 3816 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3817 | /* ENCODE_DIRECT: this character should be encoded as itself. The |
| 3818 | * answer depends on whether we are encoding set O as itself, and also |
| 3819 | * on whether we are encoding whitespace as itself. RFC2152 makes it |
| 3820 | * clear that the answers to these questions vary between |
| 3821 | * applications, so this code needs to be flexible. */ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 3822 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3823 | #define ENCODE_DIRECT(c, directO, directWS) \ |
| 3824 | ((c) < 128 && (c) > 0 && \ |
| 3825 | ((utf7_category[(c)] == 0) || \ |
| 3826 | (directWS && (utf7_category[(c)] == 2)) || \ |
| 3827 | (directO && (utf7_category[(c)] == 1)))) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3828 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3829 | PyObject * |
| 3830 | PyUnicode_DecodeUTF7(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3831 | Py_ssize_t size, |
| 3832 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3833 | { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3834 | return PyUnicode_DecodeUTF7Stateful(s, size, errors, NULL); |
| 3835 | } |
| 3836 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3837 | /* The decoder. The only state we preserve is our read position, |
| 3838 | * i.e. how many characters we have consumed. So if we end in the |
| 3839 | * middle of a shift sequence we have to back off the read position |
| 3840 | * and the output to the beginning of the sequence, otherwise we lose |
| 3841 | * all the shift state (seen bits, number of bits seen, high |
| 3842 | * surrogate). */ |
| 3843 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3844 | PyObject * |
| 3845 | PyUnicode_DecodeUTF7Stateful(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3846 | Py_ssize_t size, |
| 3847 | const char *errors, |
| 3848 | Py_ssize_t *consumed) |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3849 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3850 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3851 | Py_ssize_t startinpos; |
| 3852 | Py_ssize_t endinpos; |
| 3853 | Py_ssize_t outpos; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3854 | const char *e; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 3855 | PyObject *unicode; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3856 | const char *errmsg = ""; |
| 3857 | int inShift = 0; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 3858 | Py_ssize_t shiftOutStart; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3859 | unsigned int base64bits = 0; |
| 3860 | unsigned long base64buffer = 0; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 3861 | Py_UCS4 surrogate = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3862 | PyObject *errorHandler = NULL; |
| 3863 | PyObject *exc = NULL; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3864 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 3865 | /* Start off assuming it's all ASCII. Widen later as necessary. */ |
| 3866 | unicode = PyUnicode_New(size, 127); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3867 | if (!unicode) |
| 3868 | return NULL; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3869 | if (size == 0) { |
| 3870 | if (consumed) |
| 3871 | *consumed = 0; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 3872 | return unicode; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3873 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3874 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 3875 | shiftOutStart = outpos = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3876 | e = s + size; |
| 3877 | |
| 3878 | while (s < e) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 3879 | Py_UCS4 ch; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3880 | restart: |
Antoine Pitrou | 5ffd9e9 | 2008-07-25 18:05:24 +0000 | [diff] [blame] | 3881 | ch = (unsigned char) *s; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3882 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3883 | if (inShift) { /* in a base-64 section */ |
| 3884 | if (IS_BASE64(ch)) { /* consume a base-64 character */ |
| 3885 | base64buffer = (base64buffer << 6) | FROM_BASE64(ch); |
| 3886 | base64bits += 6; |
| 3887 | s++; |
| 3888 | if (base64bits >= 16) { |
| 3889 | /* we have enough bits for a UTF-16 value */ |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 3890 | Py_UCS4 outCh = (Py_UCS4)(base64buffer >> (base64bits-16)); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3891 | base64bits -= 16; |
| 3892 | base64buffer &= (1 << base64bits) - 1; /* clear high bits */ |
| 3893 | if (surrogate) { |
| 3894 | /* expecting a second surrogate */ |
| 3895 | if (outCh >= 0xDC00 && outCh <= 0xDFFF) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 3896 | Py_UCS4 ch2 = (((surrogate & 0x3FF)<<10) |
| 3897 | | (outCh & 0x3FF)) + 0x10000; |
| 3898 | if (unicode_putchar(&unicode, &outpos, ch2) < 0) |
| 3899 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3900 | surrogate = 0; |
Antoine Pitrou | 5418ee0 | 2011-11-15 01:42:21 +0100 | [diff] [blame] | 3901 | continue; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3902 | } |
| 3903 | else { |
Antoine Pitrou | 78edf75 | 2011-11-15 01:44:16 +0100 | [diff] [blame] | 3904 | if (unicode_putchar(&unicode, &outpos, surrogate) < 0) |
| 3905 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3906 | surrogate = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3907 | } |
| 3908 | } |
Antoine Pitrou | 5418ee0 | 2011-11-15 01:42:21 +0100 | [diff] [blame] | 3909 | if (outCh >= 0xD800 && outCh <= 0xDBFF) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3910 | /* first surrogate */ |
| 3911 | surrogate = outCh; |
| 3912 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3913 | else { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 3914 | if (unicode_putchar(&unicode, &outpos, outCh) < 0) |
| 3915 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3916 | } |
| 3917 | } |
| 3918 | } |
| 3919 | else { /* now leaving a base-64 section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3920 | inShift = 0; |
| 3921 | s++; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3922 | if (surrogate) { |
Antoine Pitrou | 78edf75 | 2011-11-15 01:44:16 +0100 | [diff] [blame] | 3923 | if (unicode_putchar(&unicode, &outpos, surrogate) < 0) |
| 3924 | goto onError; |
Antoine Pitrou | 5418ee0 | 2011-11-15 01:42:21 +0100 | [diff] [blame] | 3925 | surrogate = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3926 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3927 | if (base64bits > 0) { /* left-over bits */ |
| 3928 | if (base64bits >= 6) { |
| 3929 | /* We've seen at least one base-64 character */ |
| 3930 | errmsg = "partial character in shift sequence"; |
| 3931 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3932 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3933 | else { |
| 3934 | /* Some bits remain; they should be zero */ |
| 3935 | if (base64buffer != 0) { |
| 3936 | errmsg = "non-zero padding bits in shift sequence"; |
| 3937 | goto utf7Error; |
| 3938 | } |
| 3939 | } |
| 3940 | } |
| 3941 | if (ch != '-') { |
| 3942 | /* '-' is absorbed; other terminating |
| 3943 | characters are preserved */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 3944 | if (unicode_putchar(&unicode, &outpos, ch) < 0) |
| 3945 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3946 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3947 | } |
| 3948 | } |
| 3949 | else if ( ch == '+' ) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3950 | startinpos = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3951 | s++; /* consume '+' */ |
| 3952 | if (s < e && *s == '-') { /* '+-' encodes '+' */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3953 | s++; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 3954 | if (unicode_putchar(&unicode, &outpos, '+') < 0) |
| 3955 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3956 | } |
| 3957 | else { /* begin base64-encoded section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3958 | inShift = 1; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 3959 | shiftOutStart = outpos; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3960 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3961 | } |
| 3962 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3963 | else if (DECODE_DIRECT(ch)) { /* character decodes as itself */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 3964 | if (unicode_putchar(&unicode, &outpos, ch) < 0) |
| 3965 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3966 | s++; |
| 3967 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3968 | else { |
| 3969 | startinpos = s-starts; |
| 3970 | s++; |
| 3971 | errmsg = "unexpected special character"; |
| 3972 | goto utf7Error; |
| 3973 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3974 | continue; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3975 | utf7Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3976 | endinpos = s-starts; |
| 3977 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3978 | errors, &errorHandler, |
| 3979 | "utf7", errmsg, |
| 3980 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 3981 | &unicode, &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3982 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3983 | } |
| 3984 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3985 | /* end of string */ |
| 3986 | |
| 3987 | if (inShift && !consumed) { /* in shift sequence, no more to follow */ |
| 3988 | /* if we're in an inconsistent state, that's an error */ |
| 3989 | if (surrogate || |
| 3990 | (base64bits >= 6) || |
| 3991 | (base64bits > 0 && base64buffer != 0)) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3992 | endinpos = size; |
| 3993 | if (unicode_decode_call_errorhandler( |
| 3994 | errors, &errorHandler, |
| 3995 | "utf7", "unterminated shift sequence", |
| 3996 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 3997 | &unicode, &outpos)) |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3998 | goto onError; |
| 3999 | if (s < e) |
| 4000 | goto restart; |
| 4001 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4002 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4003 | |
| 4004 | /* return state */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4005 | if (consumed) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4006 | if (inShift) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4007 | outpos = shiftOutStart; /* back off output */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4008 | *consumed = startinpos; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4009 | } |
| 4010 | else { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4011 | *consumed = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4012 | } |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4013 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4014 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4015 | if (unicode_resize(&unicode, outpos) < 0) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4016 | goto onError; |
| 4017 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4018 | Py_XDECREF(errorHandler); |
| 4019 | Py_XDECREF(exc); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 4020 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 4021 | if (_PyUnicode_READY_REPLACE(&unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4022 | Py_DECREF(unicode); |
| 4023 | return NULL; |
| 4024 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 4025 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 4026 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4027 | return unicode; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4028 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4029 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4030 | Py_XDECREF(errorHandler); |
| 4031 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4032 | Py_DECREF(unicode); |
| 4033 | return NULL; |
| 4034 | } |
| 4035 | |
| 4036 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4037 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4038 | _PyUnicode_EncodeUTF7(PyObject *str, |
| 4039 | int base64SetO, |
| 4040 | int base64WhiteSpace, |
| 4041 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4042 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4043 | int kind; |
| 4044 | void *data; |
| 4045 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4046 | PyObject *v; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4047 | Py_ssize_t allocated; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4048 | int inShift = 0; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4049 | Py_ssize_t i; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4050 | unsigned int base64bits = 0; |
| 4051 | unsigned long base64buffer = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4052 | char * out; |
| 4053 | char * start; |
| 4054 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4055 | if (PyUnicode_READY(str) < 0) |
| 4056 | return NULL; |
| 4057 | kind = PyUnicode_KIND(str); |
| 4058 | data = PyUnicode_DATA(str); |
| 4059 | len = PyUnicode_GET_LENGTH(str); |
| 4060 | |
| 4061 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4062 | return PyBytes_FromStringAndSize(NULL, 0); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4063 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4064 | /* It might be possible to tighten this worst case */ |
| 4065 | allocated = 8 * len; |
| 4066 | if (allocated / 8 != len) |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4067 | return PyErr_NoMemory(); |
| 4068 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4069 | v = PyBytes_FromStringAndSize(NULL, allocated); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4070 | if (v == NULL) |
| 4071 | return NULL; |
| 4072 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4073 | start = out = PyBytes_AS_STRING(v); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4074 | for (i = 0; i < len; ++i) { |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 4075 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4076 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4077 | if (inShift) { |
| 4078 | if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 4079 | /* shifting out */ |
| 4080 | if (base64bits) { /* output remaining bits */ |
| 4081 | *out++ = TO_BASE64(base64buffer << (6-base64bits)); |
| 4082 | base64buffer = 0; |
| 4083 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4084 | } |
| 4085 | inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4086 | /* Characters not in the BASE64 set implicitly unshift the sequence |
| 4087 | so no '-' is required, except if the character is itself a '-' */ |
| 4088 | if (IS_BASE64(ch) || ch == '-') { |
| 4089 | *out++ = '-'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4090 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4091 | *out++ = (char) ch; |
| 4092 | } |
| 4093 | else { |
| 4094 | goto encode_char; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4095 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4096 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4097 | else { /* not in a shift sequence */ |
| 4098 | if (ch == '+') { |
| 4099 | *out++ = '+'; |
| 4100 | *out++ = '-'; |
| 4101 | } |
| 4102 | else if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 4103 | *out++ = (char) ch; |
| 4104 | } |
| 4105 | else { |
| 4106 | *out++ = '+'; |
| 4107 | inShift = 1; |
| 4108 | goto encode_char; |
| 4109 | } |
| 4110 | } |
| 4111 | continue; |
| 4112 | encode_char: |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4113 | if (ch >= 0x10000) { |
| 4114 | /* code first surrogate */ |
| 4115 | base64bits += 16; |
| 4116 | base64buffer = (base64buffer << 16) | 0xd800 | ((ch-0x10000) >> 10); |
| 4117 | while (base64bits >= 6) { |
| 4118 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 4119 | base64bits -= 6; |
| 4120 | } |
| 4121 | /* prepare second surrogate */ |
| 4122 | ch = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 4123 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4124 | base64bits += 16; |
| 4125 | base64buffer = (base64buffer << 16) | ch; |
| 4126 | while (base64bits >= 6) { |
| 4127 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 4128 | base64bits -= 6; |
| 4129 | } |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 4130 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4131 | if (base64bits) |
| 4132 | *out++= TO_BASE64(base64buffer << (6-base64bits) ); |
| 4133 | if (inShift) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4134 | *out++ = '-'; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4135 | if (_PyBytes_Resize(&v, out - start) < 0) |
| 4136 | return NULL; |
| 4137 | return v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4138 | } |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4139 | PyObject * |
| 4140 | PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
| 4141 | Py_ssize_t size, |
| 4142 | int base64SetO, |
| 4143 | int base64WhiteSpace, |
| 4144 | const char *errors) |
| 4145 | { |
| 4146 | PyObject *result; |
| 4147 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 4148 | if (tmp == NULL) |
| 4149 | return NULL; |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 4150 | result = _PyUnicode_EncodeUTF7(tmp, base64SetO, |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4151 | base64WhiteSpace, errors); |
| 4152 | Py_DECREF(tmp); |
| 4153 | return result; |
| 4154 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4155 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4156 | #undef IS_BASE64 |
| 4157 | #undef FROM_BASE64 |
| 4158 | #undef TO_BASE64 |
| 4159 | #undef DECODE_DIRECT |
| 4160 | #undef ENCODE_DIRECT |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4161 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4162 | /* --- UTF-8 Codec -------------------------------------------------------- */ |
| 4163 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4164 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4165 | char utf8_code_length[256] = { |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4166 | /* Map UTF-8 encoded prefix byte to sequence length. Zero means |
| 4167 | illegal prefix. See RFC 3629 for details */ |
| 4168 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 00-0F */ |
| 4169 | 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] | 4170 | 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] | 4171 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 4172 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 4173 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 4174 | 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] | 4175 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 70-7F */ |
| 4176 | 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] | 4177 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4178 | 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] | 4179 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* B0-BF */ |
| 4180 | 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* C0-C1 + C2-CF */ |
| 4181 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* D0-DF */ |
| 4182 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* E0-EF */ |
| 4183 | 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] | 4184 | }; |
| 4185 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4186 | PyObject * |
| 4187 | PyUnicode_DecodeUTF8(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4188 | Py_ssize_t size, |
| 4189 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4190 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4191 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
| 4192 | } |
| 4193 | |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame^] | 4194 | #include "stringlib/ucs1lib.h" |
| 4195 | #include "stringlib/codecs.h" |
| 4196 | #include "stringlib/undef.h" |
| 4197 | |
| 4198 | #include "stringlib/ucs2lib.h" |
| 4199 | #include "stringlib/codecs.h" |
| 4200 | #include "stringlib/undef.h" |
| 4201 | |
| 4202 | #include "stringlib/ucs4lib.h" |
| 4203 | #include "stringlib/codecs.h" |
| 4204 | #include "stringlib/undef.h" |
| 4205 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4206 | /* Mask to check or force alignment of a pointer to C 'long' boundaries */ |
| 4207 | #define LONG_PTR_MASK (size_t) (SIZEOF_LONG - 1) |
| 4208 | |
| 4209 | /* Mask to quickly check whether a C 'long' contains a |
| 4210 | non-ASCII, UTF8-encoded char. */ |
| 4211 | #if (SIZEOF_LONG == 8) |
| 4212 | # define ASCII_CHAR_MASK 0x8080808080808080L |
| 4213 | #elif (SIZEOF_LONG == 4) |
| 4214 | # define ASCII_CHAR_MASK 0x80808080L |
| 4215 | #else |
| 4216 | # error C 'long' size should be either 4 or 8! |
| 4217 | #endif |
| 4218 | |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame^] | 4219 | /* Scans a UTF-8 string and returns the maximum character to be expected |
| 4220 | and the size of the decoded unicode string. |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4221 | |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame^] | 4222 | 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] | 4223 | PyUnicode_DecodeUTF8Stateful. |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4224 | */ |
| 4225 | static Py_UCS4 |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame^] | 4226 | utf8_max_char_size_and_char_count(const char *s, Py_ssize_t string_size, |
| 4227 | Py_ssize_t *unicode_size) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4228 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4229 | Py_ssize_t char_count = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4230 | const unsigned char *p = (const unsigned char *)s; |
| 4231 | const unsigned char *end = p + string_size; |
| 4232 | 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] | 4233 | |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame^] | 4234 | assert(unicode_size != NULL); |
| 4235 | |
| 4236 | /* By having a cascade of independent loops which fallback onto each |
| 4237 | other, we minimize the amount of work done in the average loop |
| 4238 | iteration, and we also maximize the CPU's ability to predict |
| 4239 | branches correctly (because a given condition will have always the |
| 4240 | same boolean outcome except perhaps in the last iteration of the |
| 4241 | corresponding loop). |
| 4242 | In the general case this brings us rather close to decoding |
| 4243 | performance pre-PEP 393, despite the two-pass decoding. |
| 4244 | |
| 4245 | Note that the pure ASCII loop is not duplicated once a non-ASCII |
| 4246 | character has been encountered. It is actually a pessimization (by |
| 4247 | a significant factor) to use this loop on text with many non-ASCII |
| 4248 | characters, and it is important to avoid bad performance on valid |
| 4249 | utf-8 data (invalid utf-8 being a different can of worms). |
| 4250 | */ |
| 4251 | |
| 4252 | /* ASCII */ |
| 4253 | for (; p < end; ++p) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4254 | /* Only check value if it's not a ASCII char... */ |
| 4255 | if (*p < 0x80) { |
| 4256 | /* Fast path, see below in PyUnicode_DecodeUTF8Stateful for |
| 4257 | an explanation. */ |
| 4258 | if (!((size_t) p & LONG_PTR_MASK)) { |
| 4259 | /* Help register allocation */ |
| 4260 | register const unsigned char *_p = p; |
| 4261 | while (_p < aligned_end) { |
| 4262 | unsigned long value = *(unsigned long *) _p; |
| 4263 | if (value & ASCII_CHAR_MASK) |
| 4264 | break; |
| 4265 | _p += SIZEOF_LONG; |
| 4266 | char_count += SIZEOF_LONG; |
| 4267 | } |
| 4268 | p = _p; |
| 4269 | if (p == end) |
| 4270 | break; |
| 4271 | } |
| 4272 | } |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame^] | 4273 | if (*p < 0x80) |
| 4274 | ++char_count; |
| 4275 | else |
| 4276 | goto _ucs1loop; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4277 | } |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame^] | 4278 | *unicode_size = char_count; |
| 4279 | return 127; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4280 | |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame^] | 4281 | _ucs1loop: |
| 4282 | for (; p < end; ++p) { |
| 4283 | if (*p < 0xc4) |
| 4284 | char_count += ((*p & 0xc0) != 0x80); |
| 4285 | else |
| 4286 | goto _ucs2loop; |
| 4287 | } |
| 4288 | *unicode_size = char_count; |
| 4289 | return 255; |
| 4290 | |
| 4291 | _ucs2loop: |
| 4292 | for (; p < end; ++p) { |
| 4293 | if (*p < 0xf0) |
| 4294 | char_count += ((*p & 0xc0) != 0x80); |
| 4295 | else |
| 4296 | goto _ucs4loop; |
| 4297 | } |
| 4298 | *unicode_size = char_count; |
| 4299 | return 65535; |
| 4300 | |
| 4301 | _ucs4loop: |
| 4302 | for (; p < end; ++p) { |
| 4303 | char_count += ((*p & 0xc0) != 0x80); |
| 4304 | } |
| 4305 | *unicode_size = char_count; |
| 4306 | return 65537; |
| 4307 | } |
| 4308 | |
| 4309 | /* Called when we encountered some error that wasn't detected in the original |
| 4310 | scan, e.g. an encoded surrogate character. The original maxchar computation |
| 4311 | may have been incorrect, so redo it. */ |
| 4312 | static int |
| 4313 | refit_partial_string(PyObject **unicode, int kind, void *data, Py_ssize_t n) |
| 4314 | { |
| 4315 | PyObject *tmp; |
| 4316 | Py_ssize_t k, maxchar; |
| 4317 | for (k = 0, maxchar = 0; k < n; k++) |
| 4318 | maxchar = Py_MAX(maxchar, PyUnicode_READ(kind, data, k)); |
| 4319 | tmp = PyUnicode_New(PyUnicode_GET_LENGTH(*unicode), maxchar); |
| 4320 | if (tmp == NULL) |
| 4321 | return -1; |
| 4322 | PyUnicode_CopyCharacters(tmp, 0, *unicode, 0, n); |
| 4323 | Py_DECREF(*unicode); |
| 4324 | *unicode = tmp; |
| 4325 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4326 | } |
| 4327 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4328 | /* Similar to PyUnicode_WRITE but may attempt to widen and resize the string |
| 4329 | in case of errors. Implicit parameters: unicode, kind, data, has_errors, |
| 4330 | onError. Potential resizing overallocates, so the result needs to shrink |
| 4331 | at the end. |
| 4332 | */ |
| 4333 | #define WRITE_MAYBE_FAIL(index, value) \ |
| 4334 | do { \ |
| 4335 | if (has_errors) { \ |
| 4336 | Py_ssize_t pos = index; \ |
| 4337 | if (pos > PyUnicode_GET_LENGTH(unicode) && \ |
| 4338 | unicode_resize(&unicode, pos + pos/8) < 0) \ |
| 4339 | goto onError; \ |
| 4340 | if (unicode_putchar(&unicode, &pos, value) < 0) \ |
| 4341 | goto onError; \ |
| 4342 | } \ |
| 4343 | else \ |
| 4344 | PyUnicode_WRITE(kind, data, index, value); \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4345 | } while (0) |
| 4346 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4347 | PyObject * |
| 4348 | PyUnicode_DecodeUTF8Stateful(const char *s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4349 | Py_ssize_t size, |
| 4350 | const char *errors, |
| 4351 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4352 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4353 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4354 | int n; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4355 | int k; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4356 | Py_ssize_t startinpos; |
| 4357 | Py_ssize_t endinpos; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4358 | const char *e, *aligned_end; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4359 | PyObject *unicode; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4360 | const char *errmsg = ""; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4361 | PyObject *errorHandler = NULL; |
| 4362 | PyObject *exc = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4363 | Py_UCS4 maxchar = 0; |
| 4364 | Py_ssize_t unicode_size; |
| 4365 | Py_ssize_t i; |
| 4366 | int kind; |
| 4367 | void *data; |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame^] | 4368 | int has_errors = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4369 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4370 | if (size == 0) { |
| 4371 | if (consumed) |
| 4372 | *consumed = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4373 | return (PyObject *)PyUnicode_New(0, 0); |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4374 | } |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame^] | 4375 | maxchar = utf8_max_char_size_and_char_count(s, size, &unicode_size); |
| 4376 | /* In case of errors, maxchar and size computation might be incorrect; |
| 4377 | code below refits and resizes as necessary. */ |
| 4378 | unicode = PyUnicode_New(unicode_size, maxchar); |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4379 | if (!unicode) |
| 4380 | return NULL; |
| 4381 | /* When the string is ASCII only, just use memcpy and return. |
| 4382 | unicode_size may be != size if there is an incomplete UTF-8 |
| 4383 | sequence at the end of the ASCII block. */ |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame^] | 4384 | if (maxchar < 128 && size == unicode_size) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4385 | Py_MEMCPY(PyUnicode_1BYTE_DATA(unicode), s, unicode_size); |
| 4386 | return unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4387 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4388 | kind = PyUnicode_KIND(unicode); |
| 4389 | data = PyUnicode_DATA(unicode); |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame^] | 4390 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4391 | /* Unpack UTF-8 encoded data */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4392 | i = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4393 | e = s + size; |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame^] | 4394 | switch (kind) { |
| 4395 | case PyUnicode_1BYTE_KIND: |
| 4396 | has_errors = ucs1lib_utf8_try_decode(s, e, (Py_UCS1 *) data, &s, &i); |
| 4397 | break; |
| 4398 | case PyUnicode_2BYTE_KIND: |
| 4399 | has_errors = ucs2lib_utf8_try_decode(s, e, (Py_UCS2 *) data, &s, &i); |
| 4400 | break; |
| 4401 | case PyUnicode_4BYTE_KIND: |
| 4402 | has_errors = ucs4lib_utf8_try_decode(s, e, (Py_UCS4 *) data, &s, &i); |
| 4403 | break; |
| 4404 | } |
| 4405 | if (!has_errors) { |
| 4406 | /* Ensure the unicode size calculation was correct */ |
| 4407 | assert(i == unicode_size); |
| 4408 | assert(s == e); |
| 4409 | if (consumed) |
| 4410 | *consumed = s-starts; |
| 4411 | return unicode; |
| 4412 | } |
| 4413 | /* Fall through to the generic decoding loop for the rest of |
| 4414 | the string */ |
| 4415 | if (refit_partial_string(&unicode, kind, data, i) < 0) |
| 4416 | goto onError; |
| 4417 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4418 | aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4419 | |
| 4420 | while (s < e) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 4421 | Py_UCS4 ch = (unsigned char)*s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4422 | |
| 4423 | if (ch < 0x80) { |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4424 | /* Fast path for runs of ASCII characters. Given that common UTF-8 |
| 4425 | input will consist of an overwhelming majority of ASCII |
| 4426 | characters, we try to optimize for this case by checking |
| 4427 | as many characters as a C 'long' can contain. |
| 4428 | First, check if we can do an aligned read, as most CPUs have |
| 4429 | a penalty for unaligned reads. |
| 4430 | */ |
| 4431 | if (!((size_t) s & LONG_PTR_MASK)) { |
| 4432 | /* Help register allocation */ |
| 4433 | register const char *_s = s; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4434 | register Py_ssize_t _i = i; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4435 | while (_s < aligned_end) { |
| 4436 | /* Read a whole long at a time (either 4 or 8 bytes), |
| 4437 | and do a fast unrolled copy if it only contains ASCII |
| 4438 | characters. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4439 | unsigned long value = *(unsigned long *) _s; |
| 4440 | if (value & ASCII_CHAR_MASK) |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4441 | break; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4442 | WRITE_MAYBE_FAIL(_i+0, _s[0]); |
| 4443 | WRITE_MAYBE_FAIL(_i+1, _s[1]); |
| 4444 | WRITE_MAYBE_FAIL(_i+2, _s[2]); |
| 4445 | WRITE_MAYBE_FAIL(_i+3, _s[3]); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4446 | #if (SIZEOF_LONG == 8) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4447 | WRITE_MAYBE_FAIL(_i+4, _s[4]); |
| 4448 | WRITE_MAYBE_FAIL(_i+5, _s[5]); |
| 4449 | WRITE_MAYBE_FAIL(_i+6, _s[6]); |
| 4450 | WRITE_MAYBE_FAIL(_i+7, _s[7]); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4451 | #endif |
| 4452 | _s += SIZEOF_LONG; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4453 | _i += SIZEOF_LONG; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4454 | } |
| 4455 | s = _s; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4456 | i = _i; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4457 | if (s == e) |
| 4458 | break; |
| 4459 | ch = (unsigned char)*s; |
| 4460 | } |
| 4461 | } |
| 4462 | |
| 4463 | if (ch < 0x80) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4464 | WRITE_MAYBE_FAIL(i++, ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4465 | s++; |
| 4466 | continue; |
| 4467 | } |
| 4468 | |
| 4469 | n = utf8_code_length[ch]; |
| 4470 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4471 | if (s + n > e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4472 | if (consumed) |
| 4473 | break; |
| 4474 | else { |
| 4475 | errmsg = "unexpected end of data"; |
| 4476 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4477 | endinpos = startinpos+1; |
| 4478 | for (k=1; (k < size-startinpos) && ((s[k]&0xC0) == 0x80); k++) |
| 4479 | endinpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4480 | goto utf8Error; |
| 4481 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4482 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4483 | |
| 4484 | switch (n) { |
| 4485 | |
| 4486 | case 0: |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4487 | errmsg = "invalid start byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4488 | startinpos = s-starts; |
| 4489 | endinpos = startinpos+1; |
| 4490 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4491 | |
| 4492 | case 1: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4493 | errmsg = "internal error"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4494 | startinpos = s-starts; |
| 4495 | endinpos = startinpos+1; |
| 4496 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4497 | |
| 4498 | case 2: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4499 | if ((s[1] & 0xc0) != 0x80) { |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4500 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4501 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4502 | endinpos = startinpos + 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4503 | goto utf8Error; |
| 4504 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4505 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4506 | assert ((ch > 0x007F) && (ch <= 0x07FF)); |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4507 | WRITE_MAYBE_FAIL(i++, ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4508 | break; |
| 4509 | |
| 4510 | case 3: |
Ezio Melotti | 9bf2b3a | 2010-07-03 04:52:19 +0000 | [diff] [blame] | 4511 | /* Decoding UTF-8 sequences in range \xed\xa0\x80-\xed\xbf\xbf |
| 4512 | will result in surrogates in range d800-dfff. Surrogates are |
| 4513 | not valid UTF-8 so they are rejected. |
| 4514 | See http://www.unicode.org/versions/Unicode5.2.0/ch03.pdf |
| 4515 | (table 3-7) and http://www.rfc-editor.org/rfc/rfc3629.txt */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4516 | if ((s[1] & 0xc0) != 0x80 || |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4517 | (s[2] & 0xc0) != 0x80 || |
| 4518 | ((unsigned char)s[0] == 0xE0 && |
| 4519 | (unsigned char)s[1] < 0xA0) || |
| 4520 | ((unsigned char)s[0] == 0xED && |
| 4521 | (unsigned char)s[1] > 0x9F)) { |
| 4522 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4523 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4524 | endinpos = startinpos + 1; |
| 4525 | |
| 4526 | /* if s[1] first two bits are 1 and 0, then the invalid |
| 4527 | continuation byte is s[2], so increment endinpos by 1, |
| 4528 | if not, s[1] is invalid and endinpos doesn't need to |
| 4529 | be incremented. */ |
| 4530 | if ((s[1] & 0xC0) == 0x80) |
| 4531 | endinpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4532 | goto utf8Error; |
| 4533 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4534 | ch = ((s[0] & 0x0f) << 12) + ((s[1] & 0x3f) << 6) + (s[2] & 0x3f); |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4535 | assert ((ch > 0x07FF) && (ch <= 0xFFFF)); |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4536 | WRITE_MAYBE_FAIL(i++, ch); |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 4537 | break; |
| 4538 | |
| 4539 | case 4: |
| 4540 | if ((s[1] & 0xc0) != 0x80 || |
| 4541 | (s[2] & 0xc0) != 0x80 || |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4542 | (s[3] & 0xc0) != 0x80 || |
| 4543 | ((unsigned char)s[0] == 0xF0 && |
| 4544 | (unsigned char)s[1] < 0x90) || |
| 4545 | ((unsigned char)s[0] == 0xF4 && |
| 4546 | (unsigned char)s[1] > 0x8F)) { |
| 4547 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4548 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4549 | endinpos = startinpos + 1; |
| 4550 | if ((s[1] & 0xC0) == 0x80) { |
| 4551 | endinpos++; |
| 4552 | if ((s[2] & 0xC0) == 0x80) |
| 4553 | endinpos++; |
| 4554 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4555 | goto utf8Error; |
| 4556 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 4557 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4558 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
| 4559 | assert ((ch > 0xFFFF) && (ch <= 0x10ffff)); |
| 4560 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4561 | WRITE_MAYBE_FAIL(i++, ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4562 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4563 | } |
| 4564 | s += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4565 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4566 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4567 | utf8Error: |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4568 | if (!has_errors) { |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame^] | 4569 | if (refit_partial_string(&unicode, kind, data, i) < 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4570 | goto onError; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4571 | has_errors = 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4572 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4573 | if (unicode_decode_call_errorhandler( |
| 4574 | errors, &errorHandler, |
| 4575 | "utf8", errmsg, |
| 4576 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4577 | &unicode, &i)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4578 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4579 | /* Update data because unicode_decode_call_errorhandler might have |
| 4580 | re-created or resized the unicode object. */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4581 | data = PyUnicode_DATA(unicode); |
| 4582 | kind = PyUnicode_KIND(unicode); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4583 | aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4584 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4585 | /* Ensure the unicode_size calculation above was correct: */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4586 | assert(has_errors || i == unicode_size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4587 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4588 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4589 | *consumed = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4590 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4591 | /* Adjust length and ready string when it contained errors and |
| 4592 | is of the old resizable kind. */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4593 | if (has_errors) { |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4594 | if (PyUnicode_Resize(&unicode, i) < 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4595 | goto onError; |
| 4596 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4597 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4598 | Py_XDECREF(errorHandler); |
| 4599 | Py_XDECREF(exc); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 4600 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4601 | return unicode; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4602 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4603 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4604 | Py_XDECREF(errorHandler); |
| 4605 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4606 | Py_DECREF(unicode); |
| 4607 | return NULL; |
| 4608 | } |
| 4609 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4610 | #undef WRITE_MAYBE_FAIL |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4611 | |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4612 | #ifdef __APPLE__ |
| 4613 | |
| 4614 | /* Simplified UTF-8 decoder using surrogateescape error handler, |
| 4615 | used to decode the command line arguments on Mac OS X. */ |
| 4616 | |
| 4617 | wchar_t* |
| 4618 | _Py_DecodeUTF8_surrogateescape(const char *s, Py_ssize_t size) |
| 4619 | { |
| 4620 | int n; |
| 4621 | const char *e; |
| 4622 | wchar_t *unicode, *p; |
| 4623 | |
| 4624 | /* Note: size will always be longer than the resulting Unicode |
| 4625 | character count */ |
| 4626 | if (PY_SSIZE_T_MAX / sizeof(wchar_t) < (size + 1)) { |
| 4627 | PyErr_NoMemory(); |
| 4628 | return NULL; |
| 4629 | } |
| 4630 | unicode = PyMem_Malloc((size + 1) * sizeof(wchar_t)); |
| 4631 | if (!unicode) |
| 4632 | return NULL; |
| 4633 | |
| 4634 | /* Unpack UTF-8 encoded data */ |
| 4635 | p = unicode; |
| 4636 | e = s + size; |
| 4637 | while (s < e) { |
| 4638 | Py_UCS4 ch = (unsigned char)*s; |
| 4639 | |
| 4640 | if (ch < 0x80) { |
| 4641 | *p++ = (wchar_t)ch; |
| 4642 | s++; |
| 4643 | continue; |
| 4644 | } |
| 4645 | |
| 4646 | n = utf8_code_length[ch]; |
| 4647 | if (s + n > e) { |
| 4648 | goto surrogateescape; |
| 4649 | } |
| 4650 | |
| 4651 | switch (n) { |
| 4652 | case 0: |
| 4653 | case 1: |
| 4654 | goto surrogateescape; |
| 4655 | |
| 4656 | case 2: |
| 4657 | if ((s[1] & 0xc0) != 0x80) |
| 4658 | goto surrogateescape; |
| 4659 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
| 4660 | assert ((ch > 0x007F) && (ch <= 0x07FF)); |
| 4661 | *p++ = (wchar_t)ch; |
| 4662 | break; |
| 4663 | |
| 4664 | case 3: |
| 4665 | /* Decoding UTF-8 sequences in range \xed\xa0\x80-\xed\xbf\xbf |
| 4666 | will result in surrogates in range d800-dfff. Surrogates are |
| 4667 | not valid UTF-8 so they are rejected. |
| 4668 | See http://www.unicode.org/versions/Unicode5.2.0/ch03.pdf |
| 4669 | (table 3-7) and http://www.rfc-editor.org/rfc/rfc3629.txt */ |
| 4670 | if ((s[1] & 0xc0) != 0x80 || |
| 4671 | (s[2] & 0xc0) != 0x80 || |
| 4672 | ((unsigned char)s[0] == 0xE0 && |
| 4673 | (unsigned char)s[1] < 0xA0) || |
| 4674 | ((unsigned char)s[0] == 0xED && |
| 4675 | (unsigned char)s[1] > 0x9F)) { |
| 4676 | |
| 4677 | goto surrogateescape; |
| 4678 | } |
| 4679 | ch = ((s[0] & 0x0f) << 12) + ((s[1] & 0x3f) << 6) + (s[2] & 0x3f); |
| 4680 | assert ((ch > 0x07FF) && (ch <= 0xFFFF)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4681 | *p++ = (wchar_t)ch; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4682 | break; |
| 4683 | |
| 4684 | case 4: |
| 4685 | if ((s[1] & 0xc0) != 0x80 || |
| 4686 | (s[2] & 0xc0) != 0x80 || |
| 4687 | (s[3] & 0xc0) != 0x80 || |
| 4688 | ((unsigned char)s[0] == 0xF0 && |
| 4689 | (unsigned char)s[1] < 0x90) || |
| 4690 | ((unsigned char)s[0] == 0xF4 && |
| 4691 | (unsigned char)s[1] > 0x8F)) { |
| 4692 | goto surrogateescape; |
| 4693 | } |
| 4694 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
| 4695 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
| 4696 | assert ((ch > 0xFFFF) && (ch <= 0x10ffff)); |
| 4697 | |
| 4698 | #if SIZEOF_WCHAR_T == 4 |
| 4699 | *p++ = (wchar_t)ch; |
| 4700 | #else |
| 4701 | /* compute and append the two surrogates: */ |
| 4702 | |
| 4703 | /* translate from 10000..10FFFF to 0..FFFF */ |
| 4704 | ch -= 0x10000; |
| 4705 | |
| 4706 | /* high surrogate = top 10 bits added to D800 */ |
| 4707 | *p++ = (wchar_t)(0xD800 + (ch >> 10)); |
| 4708 | |
| 4709 | /* low surrogate = bottom 10 bits added to DC00 */ |
| 4710 | *p++ = (wchar_t)(0xDC00 + (ch & 0x03FF)); |
| 4711 | #endif |
| 4712 | break; |
| 4713 | } |
| 4714 | s += n; |
| 4715 | continue; |
| 4716 | |
| 4717 | surrogateescape: |
| 4718 | *p++ = 0xDC00 + ch; |
| 4719 | s++; |
| 4720 | } |
| 4721 | *p = L'\0'; |
| 4722 | return unicode; |
| 4723 | } |
| 4724 | |
| 4725 | #endif /* __APPLE__ */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4726 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4727 | /* Primary internal function which creates utf8 encoded bytes objects. |
| 4728 | |
| 4729 | Allocation strategy: if the string is short, convert into a stack buffer |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4730 | and allocate exactly as much space needed at the end. Else allocate the |
| 4731 | maximum possible needed (4 result bytes per Unicode character), and return |
| 4732 | the excess memory at the end. |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4733 | */ |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 4734 | PyObject * |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4735 | _PyUnicode_AsUTF8String(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4736 | { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4737 | #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] | 4738 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4739 | Py_ssize_t i; /* index into s of next input byte */ |
| 4740 | PyObject *result; /* result string object */ |
| 4741 | char *p; /* next free byte in output buffer */ |
| 4742 | Py_ssize_t nallocated; /* number of result bytes allocated */ |
| 4743 | Py_ssize_t nneeded; /* number of result bytes needed */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4744 | char stackbuf[MAX_SHORT_UNICHARS * 4]; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4745 | PyObject *errorHandler = NULL; |
| 4746 | PyObject *exc = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4747 | int kind; |
| 4748 | void *data; |
| 4749 | Py_ssize_t size; |
Antoine Pitrou | 31b92a5 | 2011-11-12 18:35:19 +0100 | [diff] [blame] | 4750 | PyObject *rep = NULL; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 4751 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4752 | if (!PyUnicode_Check(unicode)) { |
| 4753 | PyErr_BadArgument(); |
| 4754 | return NULL; |
| 4755 | } |
| 4756 | |
| 4757 | if (PyUnicode_READY(unicode) == -1) |
| 4758 | return NULL; |
| 4759 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 4760 | if (PyUnicode_UTF8(unicode)) |
| 4761 | return PyBytes_FromStringAndSize(PyUnicode_UTF8(unicode), |
| 4762 | PyUnicode_UTF8_LENGTH(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4763 | |
| 4764 | kind = PyUnicode_KIND(unicode); |
| 4765 | data = PyUnicode_DATA(unicode); |
| 4766 | size = PyUnicode_GET_LENGTH(unicode); |
| 4767 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4768 | assert(size >= 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4769 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4770 | if (size <= MAX_SHORT_UNICHARS) { |
| 4771 | /* Write into the stack buffer; nallocated can't overflow. |
| 4772 | * At the end, we'll allocate exactly as much heap space as it |
| 4773 | * turns out we need. |
| 4774 | */ |
| 4775 | nallocated = Py_SAFE_DOWNCAST(sizeof(stackbuf), size_t, int); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4776 | result = NULL; /* will allocate after we're done */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4777 | p = stackbuf; |
| 4778 | } |
| 4779 | else { |
| 4780 | /* Overallocate on the heap, and give the excess back at the end. */ |
| 4781 | nallocated = size * 4; |
| 4782 | if (nallocated / 4 != size) /* overflow! */ |
| 4783 | return PyErr_NoMemory(); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4784 | result = PyBytes_FromStringAndSize(NULL, nallocated); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4785 | if (result == NULL) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4786 | return NULL; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4787 | p = PyBytes_AS_STRING(result); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4788 | } |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4789 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4790 | for (i = 0; i < size;) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4791 | Py_UCS4 ch = PyUnicode_READ(kind, data, i++); |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 4792 | |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4793 | if (ch < 0x80) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4794 | /* Encode ASCII */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4795 | *p++ = (char) ch; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 4796 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4797 | else if (ch < 0x0800) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4798 | /* Encode Latin-1 */ |
Marc-André Lemburg | dc724d6 | 2002-02-06 18:20:19 +0000 | [diff] [blame] | 4799 | *p++ = (char)(0xc0 | (ch >> 6)); |
| 4800 | *p++ = (char)(0x80 | (ch & 0x3f)); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4801 | } else if (0xD800 <= ch && ch <= 0xDFFF) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4802 | Py_ssize_t newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4803 | Py_ssize_t repsize, k, startpos; |
| 4804 | startpos = i-1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4805 | rep = unicode_encode_call_errorhandler( |
| 4806 | errors, &errorHandler, "utf-8", "surrogates not allowed", |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4807 | unicode, &exc, startpos, startpos+1, &newpos); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4808 | if (!rep) |
| 4809 | goto error; |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4810 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4811 | if (PyBytes_Check(rep)) |
| 4812 | repsize = PyBytes_GET_SIZE(rep); |
| 4813 | else |
Victor Stinner | 9e30aa5 | 2011-11-21 02:49:52 +0100 | [diff] [blame] | 4814 | repsize = PyUnicode_GET_LENGTH(rep); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4815 | |
| 4816 | if (repsize > 4) { |
| 4817 | Py_ssize_t offset; |
| 4818 | |
| 4819 | if (result == NULL) |
| 4820 | offset = p - stackbuf; |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4821 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4822 | offset = p - PyBytes_AS_STRING(result); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4823 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4824 | if (nallocated > PY_SSIZE_T_MAX - repsize + 4) { |
| 4825 | /* integer overflow */ |
| 4826 | PyErr_NoMemory(); |
| 4827 | goto error; |
| 4828 | } |
| 4829 | nallocated += repsize - 4; |
| 4830 | if (result != NULL) { |
| 4831 | if (_PyBytes_Resize(&result, nallocated) < 0) |
| 4832 | goto error; |
| 4833 | } else { |
| 4834 | result = PyBytes_FromStringAndSize(NULL, nallocated); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4835 | if (result == NULL) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4836 | goto error; |
| 4837 | Py_MEMCPY(PyBytes_AS_STRING(result), stackbuf, offset); |
| 4838 | } |
| 4839 | p = PyBytes_AS_STRING(result) + offset; |
| 4840 | } |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4841 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4842 | if (PyBytes_Check(rep)) { |
| 4843 | char *prep = PyBytes_AS_STRING(rep); |
| 4844 | for(k = repsize; k > 0; k--) |
| 4845 | *p++ = *prep++; |
| 4846 | } else /* rep is unicode */ { |
Victor Stinner | a98b28c | 2011-11-10 20:21:49 +0100 | [diff] [blame] | 4847 | enum PyUnicode_Kind repkind; |
| 4848 | void *repdata; |
| 4849 | |
Antoine Pitrou | 31b92a5 | 2011-11-12 18:35:19 +0100 | [diff] [blame] | 4850 | if (PyUnicode_READY(rep) < 0) |
Victor Stinner | a98b28c | 2011-11-10 20:21:49 +0100 | [diff] [blame] | 4851 | goto error; |
Victor Stinner | a98b28c | 2011-11-10 20:21:49 +0100 | [diff] [blame] | 4852 | repkind = PyUnicode_KIND(rep); |
| 4853 | repdata = PyUnicode_DATA(rep); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4854 | |
| 4855 | for(k=0; k<repsize; k++) { |
Victor Stinner | a98b28c | 2011-11-10 20:21:49 +0100 | [diff] [blame] | 4856 | Py_UCS4 c = PyUnicode_READ(repkind, repdata, k); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4857 | if (0x80 <= c) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 4858 | raise_encode_exception(&exc, "utf-8", |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4859 | unicode, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 4860 | i-1, i, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4861 | "surrogates not allowed"); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4862 | goto error; |
| 4863 | } |
Victor Stinner | a98b28c | 2011-11-10 20:21:49 +0100 | [diff] [blame] | 4864 | *p++ = (char)c; |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4865 | } |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4866 | } |
Antoine Pitrou | 31b92a5 | 2011-11-12 18:35:19 +0100 | [diff] [blame] | 4867 | Py_CLEAR(rep); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4868 | } else if (ch < 0x10000) { |
| 4869 | *p++ = (char)(0xe0 | (ch >> 12)); |
| 4870 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 4871 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 4872 | } else /* ch >= 0x10000 */ { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4873 | /* Encode UCS4 Unicode ordinals */ |
| 4874 | *p++ = (char)(0xf0 | (ch >> 18)); |
| 4875 | *p++ = (char)(0x80 | ((ch >> 12) & 0x3f)); |
| 4876 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 4877 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 4878 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4879 | } |
Tim Peters | 0eca65c | 2002-04-21 17:28:06 +0000 | [diff] [blame] | 4880 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4881 | if (result == NULL) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4882 | /* This was stack allocated. */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4883 | nneeded = p - stackbuf; |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4884 | assert(nneeded <= nallocated); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4885 | result = PyBytes_FromStringAndSize(stackbuf, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4886 | } |
| 4887 | else { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 4888 | /* Cut back to size actually needed. */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4889 | nneeded = p - PyBytes_AS_STRING(result); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4890 | assert(nneeded <= nallocated); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4891 | _PyBytes_Resize(&result, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4892 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4893 | |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4894 | Py_XDECREF(errorHandler); |
| 4895 | Py_XDECREF(exc); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4896 | return result; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4897 | error: |
Antoine Pitrou | 31b92a5 | 2011-11-12 18:35:19 +0100 | [diff] [blame] | 4898 | Py_XDECREF(rep); |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4899 | Py_XDECREF(errorHandler); |
| 4900 | Py_XDECREF(exc); |
| 4901 | Py_XDECREF(result); |
| 4902 | return NULL; |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4903 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4904 | #undef MAX_SHORT_UNICHARS |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4905 | } |
| 4906 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4907 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4908 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
| 4909 | Py_ssize_t size, |
| 4910 | const char *errors) |
| 4911 | { |
| 4912 | PyObject *v, *unicode; |
| 4913 | |
| 4914 | unicode = PyUnicode_FromUnicode(s, size); |
| 4915 | if (unicode == NULL) |
| 4916 | return NULL; |
| 4917 | v = _PyUnicode_AsUTF8String(unicode, errors); |
| 4918 | Py_DECREF(unicode); |
| 4919 | return v; |
| 4920 | } |
| 4921 | |
| 4922 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4923 | PyUnicode_AsUTF8String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4924 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4925 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4926 | } |
| 4927 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4928 | /* --- UTF-32 Codec ------------------------------------------------------- */ |
| 4929 | |
| 4930 | PyObject * |
| 4931 | PyUnicode_DecodeUTF32(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4932 | Py_ssize_t size, |
| 4933 | const char *errors, |
| 4934 | int *byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4935 | { |
| 4936 | return PyUnicode_DecodeUTF32Stateful(s, size, errors, byteorder, NULL); |
| 4937 | } |
| 4938 | |
| 4939 | PyObject * |
| 4940 | PyUnicode_DecodeUTF32Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4941 | Py_ssize_t size, |
| 4942 | const char *errors, |
| 4943 | int *byteorder, |
| 4944 | Py_ssize_t *consumed) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4945 | { |
| 4946 | const char *starts = s; |
| 4947 | Py_ssize_t startinpos; |
| 4948 | Py_ssize_t endinpos; |
| 4949 | Py_ssize_t outpos; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4950 | PyObject *unicode; |
Mark Dickinson | 7db923c | 2010-06-12 09:10:14 +0000 | [diff] [blame] | 4951 | const unsigned char *q, *e; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4952 | int bo = 0; /* assume native ordering by default */ |
| 4953 | const char *errmsg = ""; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4954 | /* Offsets from q for retrieving bytes in the right order. */ |
| 4955 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 4956 | int iorder[] = {0, 1, 2, 3}; |
| 4957 | #else |
| 4958 | int iorder[] = {3, 2, 1, 0}; |
| 4959 | #endif |
| 4960 | PyObject *errorHandler = NULL; |
| 4961 | PyObject *exc = NULL; |
Victor Stinner | 313a120 | 2010-06-11 23:56:51 +0000 | [diff] [blame] | 4962 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4963 | q = (unsigned char *)s; |
| 4964 | e = q + size; |
| 4965 | |
| 4966 | if (byteorder) |
| 4967 | bo = *byteorder; |
| 4968 | |
| 4969 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 4970 | byte order setting accordingly. In native mode, the leading BOM |
| 4971 | mark is skipped, in all other modes, it is copied to the output |
| 4972 | stream as-is (giving a ZWNBSP character). */ |
| 4973 | if (bo == 0) { |
| 4974 | if (size >= 4) { |
| 4975 | const Py_UCS4 bom = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4976 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4977 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4978 | if (bom == 0x0000FEFF) { |
| 4979 | q += 4; |
| 4980 | bo = -1; |
| 4981 | } |
| 4982 | else if (bom == 0xFFFE0000) { |
| 4983 | q += 4; |
| 4984 | bo = 1; |
| 4985 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4986 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4987 | if (bom == 0x0000FEFF) { |
| 4988 | q += 4; |
| 4989 | bo = 1; |
| 4990 | } |
| 4991 | else if (bom == 0xFFFE0000) { |
| 4992 | q += 4; |
| 4993 | bo = -1; |
| 4994 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4995 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4996 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4997 | } |
| 4998 | |
| 4999 | if (bo == -1) { |
| 5000 | /* force LE */ |
| 5001 | iorder[0] = 0; |
| 5002 | iorder[1] = 1; |
| 5003 | iorder[2] = 2; |
| 5004 | iorder[3] = 3; |
| 5005 | } |
| 5006 | else if (bo == 1) { |
| 5007 | /* force BE */ |
| 5008 | iorder[0] = 3; |
| 5009 | iorder[1] = 2; |
| 5010 | iorder[2] = 1; |
| 5011 | iorder[3] = 0; |
| 5012 | } |
| 5013 | |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 5014 | /* This might be one to much, because of a BOM */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5015 | unicode = PyUnicode_New((size+3)/4, 127); |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 5016 | if (!unicode) |
| 5017 | return NULL; |
| 5018 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5019 | return unicode; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5020 | outpos = 0; |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 5021 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5022 | while (q < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5023 | Py_UCS4 ch; |
| 5024 | /* remaining bytes at the end? (size should be divisible by 4) */ |
| 5025 | if (e-q<4) { |
| 5026 | if (consumed) |
| 5027 | break; |
| 5028 | errmsg = "truncated data"; |
| 5029 | startinpos = ((const char *)q)-starts; |
| 5030 | endinpos = ((const char *)e)-starts; |
| 5031 | goto utf32Error; |
| 5032 | /* The remaining input chars are ignored if the callback |
| 5033 | chooses to skip the input */ |
| 5034 | } |
| 5035 | ch = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
| 5036 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5037 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5038 | if (ch >= 0x110000) |
| 5039 | { |
| 5040 | errmsg = "codepoint not in range(0x110000)"; |
| 5041 | startinpos = ((const char *)q)-starts; |
| 5042 | endinpos = startinpos+4; |
| 5043 | goto utf32Error; |
| 5044 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5045 | if (unicode_putchar(&unicode, &outpos, ch) < 0) |
| 5046 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5047 | q += 4; |
| 5048 | continue; |
| 5049 | utf32Error: |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5050 | if (unicode_decode_call_errorhandler( |
| 5051 | errors, &errorHandler, |
| 5052 | "utf32", errmsg, |
| 5053 | &starts, (const char **)&e, &startinpos, &endinpos, &exc, (const char **)&q, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5054 | &unicode, &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5055 | goto onError; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5056 | } |
| 5057 | |
| 5058 | if (byteorder) |
| 5059 | *byteorder = bo; |
| 5060 | |
| 5061 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5062 | *consumed = (const char *)q-starts; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5063 | |
| 5064 | /* Adjust length */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5065 | if (PyUnicode_Resize(&unicode, outpos) < 0) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5066 | goto onError; |
| 5067 | |
| 5068 | Py_XDECREF(errorHandler); |
| 5069 | Py_XDECREF(exc); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 5070 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 5071 | if (_PyUnicode_READY_REPLACE(&unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5072 | Py_DECREF(unicode); |
| 5073 | return NULL; |
| 5074 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 5075 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 5076 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5077 | return unicode; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5078 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5079 | onError: |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5080 | Py_DECREF(unicode); |
| 5081 | Py_XDECREF(errorHandler); |
| 5082 | Py_XDECREF(exc); |
| 5083 | return NULL; |
| 5084 | } |
| 5085 | |
| 5086 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5087 | _PyUnicode_EncodeUTF32(PyObject *str, |
| 5088 | const char *errors, |
| 5089 | int byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5090 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5091 | int kind; |
| 5092 | void *data; |
| 5093 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5094 | PyObject *v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5095 | unsigned char *p; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5096 | Py_ssize_t nsize, bytesize, i; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5097 | /* Offsets from p for storing byte pairs in the right order. */ |
| 5098 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5099 | int iorder[] = {0, 1, 2, 3}; |
| 5100 | #else |
| 5101 | int iorder[] = {3, 2, 1, 0}; |
| 5102 | #endif |
| 5103 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5104 | #define STORECHAR(CH) \ |
| 5105 | do { \ |
| 5106 | p[iorder[3]] = ((CH) >> 24) & 0xff; \ |
| 5107 | p[iorder[2]] = ((CH) >> 16) & 0xff; \ |
| 5108 | p[iorder[1]] = ((CH) >> 8) & 0xff; \ |
| 5109 | p[iorder[0]] = (CH) & 0xff; \ |
| 5110 | p += 4; \ |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5111 | } while(0) |
| 5112 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5113 | if (!PyUnicode_Check(str)) { |
| 5114 | PyErr_BadArgument(); |
| 5115 | return NULL; |
| 5116 | } |
| 5117 | if (PyUnicode_READY(str) < 0) |
| 5118 | return NULL; |
| 5119 | kind = PyUnicode_KIND(str); |
| 5120 | data = PyUnicode_DATA(str); |
| 5121 | len = PyUnicode_GET_LENGTH(str); |
| 5122 | |
| 5123 | nsize = len + (byteorder == 0); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5124 | bytesize = nsize * 4; |
| 5125 | if (bytesize / 4 != nsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5126 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5127 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5128 | if (v == NULL) |
| 5129 | return NULL; |
| 5130 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5131 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5132 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5133 | STORECHAR(0xFEFF); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5134 | if (len == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5135 | goto done; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5136 | |
| 5137 | if (byteorder == -1) { |
| 5138 | /* force LE */ |
| 5139 | iorder[0] = 0; |
| 5140 | iorder[1] = 1; |
| 5141 | iorder[2] = 2; |
| 5142 | iorder[3] = 3; |
| 5143 | } |
| 5144 | else if (byteorder == 1) { |
| 5145 | /* force BE */ |
| 5146 | iorder[0] = 3; |
| 5147 | iorder[1] = 2; |
| 5148 | iorder[2] = 1; |
| 5149 | iorder[3] = 0; |
| 5150 | } |
| 5151 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5152 | for (i = 0; i < len; i++) |
| 5153 | STORECHAR(PyUnicode_READ(kind, data, i)); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5154 | |
| 5155 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5156 | return v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5157 | #undef STORECHAR |
| 5158 | } |
| 5159 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5160 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5161 | PyUnicode_EncodeUTF32(const Py_UNICODE *s, |
| 5162 | Py_ssize_t size, |
| 5163 | const char *errors, |
| 5164 | int byteorder) |
| 5165 | { |
| 5166 | PyObject *result; |
| 5167 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 5168 | if (tmp == NULL) |
| 5169 | return NULL; |
| 5170 | result = _PyUnicode_EncodeUTF32(tmp, errors, byteorder); |
| 5171 | Py_DECREF(tmp); |
| 5172 | return result; |
| 5173 | } |
| 5174 | |
| 5175 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5176 | PyUnicode_AsUTF32String(PyObject *unicode) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5177 | { |
Victor Stinner | b960b34 | 2011-11-20 19:12:52 +0100 | [diff] [blame] | 5178 | return _PyUnicode_EncodeUTF32(unicode, NULL, 0); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5179 | } |
| 5180 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5181 | /* --- UTF-16 Codec ------------------------------------------------------- */ |
| 5182 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5183 | PyObject * |
| 5184 | PyUnicode_DecodeUTF16(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5185 | Py_ssize_t size, |
| 5186 | const char *errors, |
| 5187 | int *byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5188 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5189 | return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL); |
| 5190 | } |
| 5191 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5192 | /* Two masks for fast checking of whether a C 'long' may contain |
| 5193 | UTF16-encoded surrogate characters. This is an efficient heuristic, |
| 5194 | assuming that non-surrogate characters with a code point >= 0x8000 are |
| 5195 | rare in most input. |
| 5196 | FAST_CHAR_MASK is used when the input is in native byte ordering, |
| 5197 | SWAPPED_FAST_CHAR_MASK when the input is in byteswapped ordering. |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5198 | */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5199 | #if (SIZEOF_LONG == 8) |
| 5200 | # define FAST_CHAR_MASK 0x8000800080008000L |
| 5201 | # define SWAPPED_FAST_CHAR_MASK 0x0080008000800080L |
| 5202 | #elif (SIZEOF_LONG == 4) |
| 5203 | # define FAST_CHAR_MASK 0x80008000L |
| 5204 | # define SWAPPED_FAST_CHAR_MASK 0x00800080L |
| 5205 | #else |
| 5206 | # error C 'long' size should be either 4 or 8! |
| 5207 | #endif |
| 5208 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5209 | PyObject * |
| 5210 | PyUnicode_DecodeUTF16Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5211 | Py_ssize_t size, |
| 5212 | const char *errors, |
| 5213 | int *byteorder, |
| 5214 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5215 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5216 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5217 | Py_ssize_t startinpos; |
| 5218 | Py_ssize_t endinpos; |
| 5219 | Py_ssize_t outpos; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5220 | PyObject *unicode; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5221 | const unsigned char *q, *e, *aligned_end; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5222 | int bo = 0; /* assume native ordering by default */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5223 | int native_ordering = 0; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 5224 | const char *errmsg = ""; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5225 | /* Offsets from q for retrieving byte pairs in the right order. */ |
| 5226 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5227 | int ihi = 1, ilo = 0; |
| 5228 | #else |
| 5229 | int ihi = 0, ilo = 1; |
| 5230 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5231 | PyObject *errorHandler = NULL; |
| 5232 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5233 | |
| 5234 | /* Note: size will always be longer than the resulting Unicode |
| 5235 | character count */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5236 | unicode = PyUnicode_New(size, 127); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5237 | if (!unicode) |
| 5238 | return NULL; |
| 5239 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5240 | return unicode; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5241 | outpos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5242 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5243 | q = (unsigned char *)s; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5244 | e = q + size - 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5245 | |
| 5246 | if (byteorder) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5247 | bo = *byteorder; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5248 | |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5249 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 5250 | byte order setting accordingly. In native mode, the leading BOM |
| 5251 | mark is skipped, in all other modes, it is copied to the output |
| 5252 | stream as-is (giving a ZWNBSP character). */ |
| 5253 | if (bo == 0) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5254 | if (size >= 2) { |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 5255 | const Py_UCS4 bom = (q[ihi] << 8) | q[ilo]; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5256 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5257 | if (bom == 0xFEFF) { |
| 5258 | q += 2; |
| 5259 | bo = -1; |
| 5260 | } |
| 5261 | else if (bom == 0xFFFE) { |
| 5262 | q += 2; |
| 5263 | bo = 1; |
| 5264 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5265 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5266 | if (bom == 0xFEFF) { |
| 5267 | q += 2; |
| 5268 | bo = 1; |
| 5269 | } |
| 5270 | else if (bom == 0xFFFE) { |
| 5271 | q += 2; |
| 5272 | bo = -1; |
| 5273 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5274 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5275 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5276 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5277 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5278 | if (bo == -1) { |
| 5279 | /* force LE */ |
| 5280 | ihi = 1; |
| 5281 | ilo = 0; |
| 5282 | } |
| 5283 | else if (bo == 1) { |
| 5284 | /* force BE */ |
| 5285 | ihi = 0; |
| 5286 | ilo = 1; |
| 5287 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5288 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5289 | native_ordering = ilo < ihi; |
| 5290 | #else |
| 5291 | native_ordering = ilo > ihi; |
| 5292 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5293 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5294 | aligned_end = (const unsigned char *) ((size_t) e & ~LONG_PTR_MASK); |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5295 | while (q < e) { |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 5296 | Py_UCS4 ch; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5297 | /* First check for possible aligned read of a C 'long'. Unaligned |
| 5298 | reads are more expensive, better to defer to another iteration. */ |
| 5299 | if (!((size_t) q & LONG_PTR_MASK)) { |
| 5300 | /* Fast path for runs of non-surrogate chars. */ |
| 5301 | register const unsigned char *_q = q; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5302 | int kind = PyUnicode_KIND(unicode); |
| 5303 | void *data = PyUnicode_DATA(unicode); |
| 5304 | while (_q < aligned_end) { |
| 5305 | unsigned long block = * (unsigned long *) _q; |
| 5306 | unsigned short *pblock = (unsigned short*)█ |
| 5307 | Py_UCS4 maxch; |
| 5308 | if (native_ordering) { |
| 5309 | /* Can use buffer directly */ |
| 5310 | if (block & FAST_CHAR_MASK) |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5311 | break; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5312 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5313 | else { |
| 5314 | /* Need to byte-swap */ |
| 5315 | unsigned char *_p = (unsigned char*)pblock; |
| 5316 | if (block & SWAPPED_FAST_CHAR_MASK) |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5317 | break; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5318 | _p[0] = _q[1]; |
| 5319 | _p[1] = _q[0]; |
| 5320 | _p[2] = _q[3]; |
| 5321 | _p[3] = _q[2]; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5322 | #if (SIZEOF_LONG == 8) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5323 | _p[4] = _q[5]; |
| 5324 | _p[5] = _q[4]; |
| 5325 | _p[6] = _q[7]; |
| 5326 | _p[7] = _q[6]; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5327 | #endif |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5328 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5329 | maxch = Py_MAX(pblock[0], pblock[1]); |
| 5330 | #if SIZEOF_LONG == 8 |
| 5331 | maxch = Py_MAX(maxch, Py_MAX(pblock[2], pblock[3])); |
| 5332 | #endif |
| 5333 | if (maxch > PyUnicode_MAX_CHAR_VALUE(unicode)) { |
| 5334 | if (unicode_widen(&unicode, maxch) < 0) |
| 5335 | goto onError; |
| 5336 | kind = PyUnicode_KIND(unicode); |
| 5337 | data = PyUnicode_DATA(unicode); |
| 5338 | } |
| 5339 | PyUnicode_WRITE(kind, data, outpos++, pblock[0]); |
| 5340 | PyUnicode_WRITE(kind, data, outpos++, pblock[1]); |
| 5341 | #if SIZEOF_LONG == 8 |
| 5342 | PyUnicode_WRITE(kind, data, outpos++, pblock[2]); |
| 5343 | PyUnicode_WRITE(kind, data, outpos++, pblock[3]); |
| 5344 | #endif |
| 5345 | _q += SIZEOF_LONG; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5346 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5347 | q = _q; |
| 5348 | if (q >= e) |
| 5349 | break; |
| 5350 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5351 | ch = (q[ihi] << 8) | q[ilo]; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5352 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5353 | q += 2; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5354 | |
| 5355 | if (ch < 0xD800 || ch > 0xDFFF) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5356 | if (unicode_putchar(&unicode, &outpos, ch) < 0) |
| 5357 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5358 | continue; |
| 5359 | } |
| 5360 | |
| 5361 | /* UTF-16 code pair: */ |
| 5362 | if (q > e) { |
| 5363 | errmsg = "unexpected end of data"; |
| 5364 | startinpos = (((const char *)q) - 2) - starts; |
| 5365 | endinpos = ((const char *)e) + 1 - starts; |
| 5366 | goto utf16Error; |
| 5367 | } |
Victor Stinner | 2e9cfad | 2011-11-20 18:40:27 +0100 | [diff] [blame] | 5368 | if (Py_UNICODE_IS_HIGH_SURROGATE(ch)) { |
| 5369 | Py_UCS4 ch2 = (q[ihi] << 8) | q[ilo]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5370 | q += 2; |
Victor Stinner | 2e9cfad | 2011-11-20 18:40:27 +0100 | [diff] [blame] | 5371 | if (Py_UNICODE_IS_LOW_SURROGATE(ch2)) { |
Victor Stinner | 62aa4d0 | 2011-11-09 00:03:45 +0100 | [diff] [blame] | 5372 | if (unicode_putchar(&unicode, &outpos, |
Victor Stinner | 2e9cfad | 2011-11-20 18:40:27 +0100 | [diff] [blame] | 5373 | Py_UNICODE_JOIN_SURROGATES(ch, ch2)) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5374 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5375 | continue; |
| 5376 | } |
| 5377 | else { |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5378 | errmsg = "illegal UTF-16 surrogate"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5379 | startinpos = (((const char *)q)-4)-starts; |
| 5380 | endinpos = startinpos+2; |
| 5381 | goto utf16Error; |
| 5382 | } |
| 5383 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5384 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5385 | errmsg = "illegal encoding"; |
| 5386 | startinpos = (((const char *)q)-2)-starts; |
| 5387 | endinpos = startinpos+2; |
| 5388 | /* Fall through to report the error */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5389 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5390 | utf16Error: |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5391 | if (unicode_decode_call_errorhandler( |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5392 | errors, |
| 5393 | &errorHandler, |
| 5394 | "utf16", errmsg, |
| 5395 | &starts, |
| 5396 | (const char **)&e, |
| 5397 | &startinpos, |
| 5398 | &endinpos, |
| 5399 | &exc, |
| 5400 | (const char **)&q, |
| 5401 | &unicode, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5402 | &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5403 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5404 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5405 | /* remaining byte at the end? (size should be even) */ |
| 5406 | if (e == q) { |
| 5407 | if (!consumed) { |
| 5408 | errmsg = "truncated data"; |
| 5409 | startinpos = ((const char *)q) - starts; |
| 5410 | endinpos = ((const char *)e) + 1 - starts; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5411 | if (unicode_decode_call_errorhandler( |
| 5412 | errors, |
| 5413 | &errorHandler, |
| 5414 | "utf16", errmsg, |
| 5415 | &starts, |
| 5416 | (const char **)&e, |
| 5417 | &startinpos, |
| 5418 | &endinpos, |
| 5419 | &exc, |
| 5420 | (const char **)&q, |
| 5421 | &unicode, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5422 | &outpos)) |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5423 | goto onError; |
| 5424 | /* The remaining input chars are ignored if the callback |
| 5425 | chooses to skip the input */ |
| 5426 | } |
| 5427 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5428 | |
| 5429 | if (byteorder) |
| 5430 | *byteorder = bo; |
| 5431 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5432 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5433 | *consumed = (const char *)q-starts; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5434 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5435 | /* Adjust length */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5436 | if (PyUnicode_Resize(&unicode, outpos) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5437 | goto onError; |
| 5438 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5439 | Py_XDECREF(errorHandler); |
| 5440 | Py_XDECREF(exc); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 5441 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5442 | return unicode; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5443 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5444 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5445 | Py_DECREF(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5446 | Py_XDECREF(errorHandler); |
| 5447 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5448 | return NULL; |
| 5449 | } |
| 5450 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5451 | #undef FAST_CHAR_MASK |
| 5452 | #undef SWAPPED_FAST_CHAR_MASK |
| 5453 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5454 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5455 | _PyUnicode_EncodeUTF16(PyObject *str, |
| 5456 | const char *errors, |
| 5457 | int byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5458 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5459 | int kind; |
| 5460 | void *data; |
| 5461 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5462 | PyObject *v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5463 | unsigned char *p; |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5464 | Py_ssize_t nsize, bytesize; |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5465 | Py_ssize_t i, pairs; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5466 | /* Offsets from p for storing byte pairs in the right order. */ |
| 5467 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5468 | int ihi = 1, ilo = 0; |
| 5469 | #else |
| 5470 | int ihi = 0, ilo = 1; |
| 5471 | #endif |
| 5472 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5473 | #define STORECHAR(CH) \ |
| 5474 | do { \ |
| 5475 | p[ihi] = ((CH) >> 8) & 0xff; \ |
| 5476 | p[ilo] = (CH) & 0xff; \ |
| 5477 | p += 2; \ |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5478 | } while(0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5479 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5480 | if (!PyUnicode_Check(str)) { |
| 5481 | PyErr_BadArgument(); |
| 5482 | return NULL; |
| 5483 | } |
| 5484 | if (PyUnicode_READY(str) < 0) |
| 5485 | return NULL; |
| 5486 | kind = PyUnicode_KIND(str); |
| 5487 | data = PyUnicode_DATA(str); |
| 5488 | len = PyUnicode_GET_LENGTH(str); |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 5489 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5490 | pairs = 0; |
| 5491 | if (kind == PyUnicode_4BYTE_KIND) |
| 5492 | for (i = 0; i < len; i++) |
| 5493 | if (PyUnicode_READ(kind, data, i) >= 0x10000) |
| 5494 | pairs++; |
| 5495 | /* 2 * (len + pairs + (byteorder == 0)) */ |
| 5496 | if (len > PY_SSIZE_T_MAX - pairs - (byteorder == 0)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5497 | return PyErr_NoMemory(); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5498 | nsize = len + pairs + (byteorder == 0); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5499 | bytesize = nsize * 2; |
| 5500 | if (bytesize / 2 != nsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5501 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5502 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5503 | if (v == NULL) |
| 5504 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5505 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5506 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5507 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5508 | STORECHAR(0xFEFF); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5509 | if (len == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5510 | goto done; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5511 | |
| 5512 | if (byteorder == -1) { |
| 5513 | /* force LE */ |
| 5514 | ihi = 1; |
| 5515 | ilo = 0; |
| 5516 | } |
| 5517 | else if (byteorder == 1) { |
| 5518 | /* force BE */ |
| 5519 | ihi = 0; |
| 5520 | ilo = 1; |
| 5521 | } |
| 5522 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5523 | for (i = 0; i < len; i++) { |
| 5524 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 5525 | Py_UCS4 ch2 = 0; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5526 | if (ch >= 0x10000) { |
| 5527 | ch2 = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 5528 | ch = 0xD800 | ((ch-0x10000) >> 10); |
| 5529 | } |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5530 | STORECHAR(ch); |
| 5531 | if (ch2) |
| 5532 | STORECHAR(ch2); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5533 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5534 | |
| 5535 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5536 | return v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5537 | #undef STORECHAR |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5538 | } |
| 5539 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5540 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5541 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
| 5542 | Py_ssize_t size, |
| 5543 | const char *errors, |
| 5544 | int byteorder) |
| 5545 | { |
| 5546 | PyObject *result; |
| 5547 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 5548 | if (tmp == NULL) |
| 5549 | return NULL; |
| 5550 | result = _PyUnicode_EncodeUTF16(tmp, errors, byteorder); |
| 5551 | Py_DECREF(tmp); |
| 5552 | return result; |
| 5553 | } |
| 5554 | |
| 5555 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5556 | PyUnicode_AsUTF16String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5557 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5558 | return _PyUnicode_EncodeUTF16(unicode, NULL, 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5559 | } |
| 5560 | |
| 5561 | /* --- Unicode Escape Codec ----------------------------------------------- */ |
| 5562 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5563 | /* Helper function for PyUnicode_DecodeUnicodeEscape, determines |
| 5564 | if all the escapes in the string make it still a valid ASCII string. |
| 5565 | Returns -1 if any escapes were found which cause the string to |
| 5566 | pop out of ASCII range. Otherwise returns the length of the |
| 5567 | required buffer to hold the string. |
| 5568 | */ |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 5569 | static Py_ssize_t |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5570 | length_of_escaped_ascii_string(const char *s, Py_ssize_t size) |
| 5571 | { |
| 5572 | const unsigned char *p = (const unsigned char *)s; |
| 5573 | const unsigned char *end = p + size; |
| 5574 | Py_ssize_t length = 0; |
| 5575 | |
| 5576 | if (size < 0) |
| 5577 | return -1; |
| 5578 | |
| 5579 | for (; p < end; ++p) { |
| 5580 | if (*p > 127) { |
| 5581 | /* Non-ASCII */ |
| 5582 | return -1; |
| 5583 | } |
| 5584 | else if (*p != '\\') { |
| 5585 | /* Normal character */ |
| 5586 | ++length; |
| 5587 | } |
| 5588 | else { |
| 5589 | /* Backslash-escape, check next char */ |
| 5590 | ++p; |
| 5591 | /* Escape sequence reaches till end of string or |
| 5592 | non-ASCII follow-up. */ |
| 5593 | if (p >= end || *p > 127) |
| 5594 | return -1; |
| 5595 | switch (*p) { |
| 5596 | case '\n': |
| 5597 | /* backslash + \n result in zero characters */ |
| 5598 | break; |
| 5599 | case '\\': case '\'': case '\"': |
| 5600 | case 'b': case 'f': case 't': |
| 5601 | case 'n': case 'r': case 'v': case 'a': |
| 5602 | ++length; |
| 5603 | break; |
| 5604 | case '0': case '1': case '2': case '3': |
| 5605 | case '4': case '5': case '6': case '7': |
| 5606 | case 'x': case 'u': case 'U': case 'N': |
| 5607 | /* these do not guarantee ASCII characters */ |
| 5608 | return -1; |
| 5609 | default: |
| 5610 | /* count the backslash + the other character */ |
| 5611 | length += 2; |
| 5612 | } |
| 5613 | } |
| 5614 | } |
| 5615 | return length; |
| 5616 | } |
| 5617 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 5618 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; |
Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 5619 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5620 | PyObject * |
| 5621 | PyUnicode_DecodeUnicodeEscape(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5622 | Py_ssize_t size, |
Victor Stinner | c17f540 | 2011-09-29 00:16:58 +0200 | [diff] [blame] | 5623 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5624 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5625 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5626 | Py_ssize_t startinpos; |
| 5627 | Py_ssize_t endinpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5628 | int j; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5629 | PyObject *v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5630 | const char *end; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5631 | char* message; |
| 5632 | Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5633 | PyObject *errorHandler = NULL; |
| 5634 | PyObject *exc = NULL; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5635 | Py_ssize_t len; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5636 | Py_ssize_t i; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5637 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5638 | len = length_of_escaped_ascii_string(s, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5639 | |
| 5640 | /* After length_of_escaped_ascii_string() there are two alternatives, |
| 5641 | either the string is pure ASCII with named escapes like \n, etc. |
| 5642 | and we determined it's exact size (common case) |
| 5643 | or it contains \x, \u, ... escape sequences. then we create a |
| 5644 | 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] | 5645 | if (len >= 0) { |
| 5646 | v = PyUnicode_New(len, 127); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5647 | if (!v) |
| 5648 | goto onError; |
| 5649 | assert(PyUnicode_KIND(v) == PyUnicode_1BYTE_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5650 | } |
| 5651 | else { |
| 5652 | /* Escaped strings will always be longer than the resulting |
| 5653 | Unicode string, so we start with size here and then reduce the |
| 5654 | length after conversion to the true value. |
| 5655 | (but if the error callback returns a long replacement string |
| 5656 | we'll have to allocate more space) */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5657 | v = PyUnicode_New(size, 127); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5658 | if (!v) |
| 5659 | goto onError; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5660 | len = size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5661 | } |
| 5662 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5663 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5664 | return v; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5665 | i = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5666 | end = s + size; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5667 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5668 | while (s < end) { |
| 5669 | unsigned char c; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 5670 | Py_UCS4 x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5671 | int digits; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5672 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5673 | /* The only case in which i == ascii_length is a backslash |
| 5674 | followed by a newline. */ |
| 5675 | assert(i <= len); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5676 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5677 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 5678 | if (*s != '\\') { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5679 | if (unicode_putchar(&v, &i, (unsigned char) *s++) < 0) |
| 5680 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5681 | continue; |
| 5682 | } |
| 5683 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5684 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5685 | /* \ - Escapes */ |
| 5686 | s++; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5687 | c = *s++; |
| 5688 | if (s > end) |
| 5689 | c = '\0'; /* Invalid after \ */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5690 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5691 | /* The only case in which i == ascii_length is a backslash |
| 5692 | followed by a newline. */ |
| 5693 | assert(i < len || (i == len && c == '\n')); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5694 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5695 | switch (c) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5696 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5697 | /* \x escapes */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5698 | #define WRITECHAR(ch) \ |
| 5699 | do { \ |
| 5700 | if (unicode_putchar(&v, &i, ch) < 0) \ |
| 5701 | goto onError; \ |
| 5702 | }while(0) |
| 5703 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5704 | case '\n': break; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5705 | case '\\': WRITECHAR('\\'); break; |
| 5706 | case '\'': WRITECHAR('\''); break; |
| 5707 | case '\"': WRITECHAR('\"'); break; |
| 5708 | case 'b': WRITECHAR('\b'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5709 | /* FF */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5710 | case 'f': WRITECHAR('\014'); break; |
| 5711 | case 't': WRITECHAR('\t'); break; |
| 5712 | case 'n': WRITECHAR('\n'); break; |
| 5713 | case 'r': WRITECHAR('\r'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5714 | /* VT */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5715 | case 'v': WRITECHAR('\013'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5716 | /* BEL, not classic C */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5717 | case 'a': WRITECHAR('\007'); break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5718 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5719 | /* \OOO (octal) escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5720 | case '0': case '1': case '2': case '3': |
| 5721 | case '4': case '5': case '6': case '7': |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5722 | x = s[-1] - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5723 | if (s < end && '0' <= *s && *s <= '7') { |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5724 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5725 | if (s < end && '0' <= *s && *s <= '7') |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5726 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5727 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5728 | WRITECHAR(x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5729 | break; |
| 5730 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5731 | /* hex escapes */ |
| 5732 | /* \xXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5733 | case 'x': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5734 | digits = 2; |
| 5735 | message = "truncated \\xXX escape"; |
| 5736 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5737 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5738 | /* \uXXXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5739 | case 'u': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5740 | digits = 4; |
| 5741 | message = "truncated \\uXXXX escape"; |
| 5742 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5743 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5744 | /* \UXXXXXXXX */ |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5745 | case 'U': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5746 | digits = 8; |
| 5747 | message = "truncated \\UXXXXXXXX escape"; |
| 5748 | hexescape: |
| 5749 | chr = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5750 | if (s+digits>end) { |
| 5751 | endinpos = size; |
| 5752 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5753 | errors, &errorHandler, |
| 5754 | "unicodeescape", "end of string in escape sequence", |
| 5755 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5756 | &v, &i)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5757 | goto onError; |
| 5758 | goto nextByte; |
| 5759 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5760 | for (j = 0; j < digits; ++j) { |
| 5761 | c = (unsigned char) s[j]; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 5762 | if (!Py_ISXDIGIT(c)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5763 | endinpos = (s+j+1)-starts; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5764 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5765 | errors, &errorHandler, |
| 5766 | "unicodeescape", message, |
| 5767 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5768 | &v, &i)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5769 | goto onError; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5770 | len = PyUnicode_GET_LENGTH(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5771 | goto nextByte; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5772 | } |
| 5773 | chr = (chr<<4) & ~0xF; |
| 5774 | if (c >= '0' && c <= '9') |
| 5775 | chr += c - '0'; |
| 5776 | else if (c >= 'a' && c <= 'f') |
| 5777 | chr += 10 + c - 'a'; |
| 5778 | else |
| 5779 | chr += 10 + c - 'A'; |
| 5780 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5781 | s += j; |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 5782 | if (chr == 0xffffffff && PyErr_Occurred()) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5783 | /* _decoding_error will have already written into the |
| 5784 | target buffer. */ |
| 5785 | break; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5786 | store: |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5787 | /* 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] | 5788 | if (chr <= 0x10ffff) { |
| 5789 | WRITECHAR(chr); |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5790 | } else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5791 | endinpos = s-starts; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5792 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5793 | errors, &errorHandler, |
| 5794 | "unicodeescape", "illegal Unicode character", |
| 5795 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5796 | &v, &i)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5797 | goto onError; |
| 5798 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5799 | break; |
| 5800 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5801 | /* \N{name} */ |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5802 | case 'N': |
| 5803 | message = "malformed \\N character escape"; |
| 5804 | if (ucnhash_CAPI == NULL) { |
| 5805 | /* load the unicode data module */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5806 | ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCapsule_Import( |
| 5807 | PyUnicodeData_CAPSULE_NAME, 1); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5808 | if (ucnhash_CAPI == NULL) |
| 5809 | goto ucnhashError; |
| 5810 | } |
| 5811 | if (*s == '{') { |
| 5812 | const char *start = s+1; |
| 5813 | /* look for the closing brace */ |
| 5814 | while (*s != '}' && s < end) |
| 5815 | s++; |
| 5816 | if (s > start && s < end && *s == '}') { |
| 5817 | /* found a name. look it up in the unicode database */ |
| 5818 | message = "unknown Unicode character name"; |
| 5819 | s++; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5820 | if (ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), |
Ezio Melotti | 931b8aa | 2011-10-21 21:57:36 +0300 | [diff] [blame] | 5821 | &chr, 0)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5822 | goto store; |
| 5823 | } |
| 5824 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5825 | endinpos = s-starts; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5826 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5827 | errors, &errorHandler, |
| 5828 | "unicodeescape", message, |
| 5829 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5830 | &v, &i)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5831 | goto onError; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5832 | break; |
| 5833 | |
| 5834 | default: |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5835 | if (s > end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5836 | message = "\\ at end of string"; |
| 5837 | s--; |
| 5838 | endinpos = s-starts; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5839 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5840 | errors, &errorHandler, |
| 5841 | "unicodeescape", message, |
| 5842 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5843 | &v, &i)) |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5844 | goto onError; |
| 5845 | } |
| 5846 | else { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5847 | WRITECHAR('\\'); |
| 5848 | WRITECHAR(s[-1]); |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5849 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5850 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5851 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5852 | nextByte: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5853 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5854 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5855 | #undef WRITECHAR |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5856 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5857 | if (PyUnicode_Resize(&v, i) < 0) |
| 5858 | goto onError; |
Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 5859 | Py_XDECREF(errorHandler); |
| 5860 | Py_XDECREF(exc); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 5861 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 5862 | if (_PyUnicode_READY_REPLACE(&v)) { |
| 5863 | Py_DECREF(v); |
| 5864 | return NULL; |
| 5865 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 5866 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 5867 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5868 | return v; |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5869 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5870 | ucnhashError: |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 5871 | PyErr_SetString( |
| 5872 | PyExc_UnicodeError, |
| 5873 | "\\N escapes not supported (can't load unicodedata module)" |
| 5874 | ); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 5875 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5876 | Py_XDECREF(errorHandler); |
| 5877 | Py_XDECREF(exc); |
Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 5878 | return NULL; |
| 5879 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5880 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5881 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5882 | Py_XDECREF(errorHandler); |
| 5883 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5884 | return NULL; |
| 5885 | } |
| 5886 | |
| 5887 | /* Return a Unicode-Escape string version of the Unicode object. |
| 5888 | |
| 5889 | If quotes is true, the string is enclosed in u"" or u'' quotes as |
| 5890 | appropriate. |
| 5891 | |
| 5892 | */ |
| 5893 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5894 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5895 | PyUnicode_AsUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5896 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5897 | Py_ssize_t i, len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5898 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5899 | char *p; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5900 | int kind; |
| 5901 | void *data; |
| 5902 | Py_ssize_t expandsize = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5903 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5904 | /* Initial allocation is based on the longest-possible unichr |
| 5905 | escape. |
| 5906 | |
| 5907 | In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source |
| 5908 | unichr, so in this case it's the longest unichr escape. In |
| 5909 | narrow (UTF-16) builds this is five chars per source unichr |
| 5910 | since there are two unichrs in the surrogate pair, so in narrow |
| 5911 | (UTF-16) builds it's not the longest unichr escape. |
| 5912 | |
| 5913 | In wide or narrow builds '\uxxxx' is 6 chars per source unichr, |
| 5914 | so in the narrow (UTF-16) build case it's the longest unichr |
| 5915 | escape. |
| 5916 | */ |
| 5917 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5918 | if (!PyUnicode_Check(unicode)) { |
| 5919 | PyErr_BadArgument(); |
| 5920 | return NULL; |
| 5921 | } |
| 5922 | if (PyUnicode_READY(unicode) < 0) |
| 5923 | return NULL; |
| 5924 | len = PyUnicode_GET_LENGTH(unicode); |
| 5925 | kind = PyUnicode_KIND(unicode); |
| 5926 | data = PyUnicode_DATA(unicode); |
| 5927 | switch(kind) { |
| 5928 | case PyUnicode_1BYTE_KIND: expandsize = 4; break; |
| 5929 | case PyUnicode_2BYTE_KIND: expandsize = 6; break; |
| 5930 | case PyUnicode_4BYTE_KIND: expandsize = 10; break; |
| 5931 | } |
| 5932 | |
| 5933 | if (len == 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5934 | return PyBytes_FromStringAndSize(NULL, 0); |
| 5935 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5936 | if (len > (PY_SSIZE_T_MAX - 2 - 1) / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5937 | return PyErr_NoMemory(); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5938 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5939 | repr = PyBytes_FromStringAndSize(NULL, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5940 | 2 |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5941 | + expandsize*len |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5942 | + 1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5943 | if (repr == NULL) |
| 5944 | return NULL; |
| 5945 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5946 | p = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5947 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5948 | for (i = 0; i < len; i++) { |
Victor Stinner | 3326cb6 | 2011-11-10 20:15:25 +0100 | [diff] [blame] | 5949 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5950 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5951 | /* Escape backslashes */ |
| 5952 | if (ch == '\\') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5953 | *p++ = '\\'; |
| 5954 | *p++ = (char) ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5955 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5956 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5957 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5958 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 5959 | else if (ch >= 0x10000) { |
| 5960 | *p++ = '\\'; |
| 5961 | *p++ = 'U'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 5962 | *p++ = Py_hexdigits[(ch >> 28) & 0x0000000F]; |
| 5963 | *p++ = Py_hexdigits[(ch >> 24) & 0x0000000F]; |
| 5964 | *p++ = Py_hexdigits[(ch >> 20) & 0x0000000F]; |
| 5965 | *p++ = Py_hexdigits[(ch >> 16) & 0x0000000F]; |
| 5966 | *p++ = Py_hexdigits[(ch >> 12) & 0x0000000F]; |
| 5967 | *p++ = Py_hexdigits[(ch >> 8) & 0x0000000F]; |
| 5968 | *p++ = Py_hexdigits[(ch >> 4) & 0x0000000F]; |
| 5969 | *p++ = Py_hexdigits[ch & 0x0000000F]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5970 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5971 | } |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 5972 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5973 | /* Map 16-bit characters to '\uxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 5974 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5975 | *p++ = '\\'; |
| 5976 | *p++ = 'u'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 5977 | *p++ = Py_hexdigits[(ch >> 12) & 0x000F]; |
| 5978 | *p++ = Py_hexdigits[(ch >> 8) & 0x000F]; |
| 5979 | *p++ = Py_hexdigits[(ch >> 4) & 0x000F]; |
| 5980 | *p++ = Py_hexdigits[ch & 0x000F]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5981 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5982 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 5983 | /* Map special whitespace to '\t', \n', '\r' */ |
| 5984 | else if (ch == '\t') { |
| 5985 | *p++ = '\\'; |
| 5986 | *p++ = 't'; |
| 5987 | } |
| 5988 | else if (ch == '\n') { |
| 5989 | *p++ = '\\'; |
| 5990 | *p++ = 'n'; |
| 5991 | } |
| 5992 | else if (ch == '\r') { |
| 5993 | *p++ = '\\'; |
| 5994 | *p++ = 'r'; |
| 5995 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5996 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 5997 | /* Map non-printable US ASCII to '\xhh' */ |
Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 5998 | else if (ch < ' ' || ch >= 0x7F) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5999 | *p++ = '\\'; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 6000 | *p++ = 'x'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6001 | *p++ = Py_hexdigits[(ch >> 4) & 0x000F]; |
| 6002 | *p++ = Py_hexdigits[ch & 0x000F]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6003 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 6004 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6005 | /* Copy everything else as-is */ |
| 6006 | else |
| 6007 | *p++ = (char) ch; |
| 6008 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6009 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6010 | assert(p - PyBytes_AS_STRING(repr) > 0); |
| 6011 | if (_PyBytes_Resize(&repr, p - PyBytes_AS_STRING(repr)) < 0) |
| 6012 | return NULL; |
| 6013 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6014 | } |
| 6015 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6016 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6017 | PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, |
| 6018 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6019 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6020 | PyObject *result; |
| 6021 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 6022 | if (tmp == NULL) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6023 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6024 | result = PyUnicode_AsUnicodeEscapeString(tmp); |
| 6025 | Py_DECREF(tmp); |
| 6026 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6027 | } |
| 6028 | |
| 6029 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ |
| 6030 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6031 | PyObject * |
| 6032 | PyUnicode_DecodeRawUnicodeEscape(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6033 | Py_ssize_t size, |
| 6034 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6035 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6036 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6037 | Py_ssize_t startinpos; |
| 6038 | Py_ssize_t endinpos; |
| 6039 | Py_ssize_t outpos; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6040 | PyObject *v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6041 | const char *end; |
| 6042 | const char *bs; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6043 | PyObject *errorHandler = NULL; |
| 6044 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6045 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6046 | /* Escaped strings will always be longer than the resulting |
| 6047 | 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] | 6048 | length after conversion to the true value. (But decoding error |
| 6049 | handler might have to resize the string) */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6050 | v = PyUnicode_New(size, 127); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6051 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6052 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6053 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6054 | return v; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6055 | outpos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6056 | end = s + size; |
| 6057 | while (s < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6058 | unsigned char c; |
| 6059 | Py_UCS4 x; |
| 6060 | int i; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6061 | int count; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6062 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6063 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 6064 | if (*s != '\\') { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6065 | if (unicode_putchar(&v, &outpos, (unsigned char)*s++) < 0) |
| 6066 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6067 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6068 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6069 | startinpos = s-starts; |
| 6070 | |
| 6071 | /* \u-escapes are only interpreted iff the number of leading |
| 6072 | backslashes if odd */ |
| 6073 | bs = s; |
| 6074 | for (;s < end;) { |
| 6075 | if (*s != '\\') |
| 6076 | break; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6077 | if (unicode_putchar(&v, &outpos, (unsigned char)*s++) < 0) |
| 6078 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6079 | } |
| 6080 | if (((s - bs) & 1) == 0 || |
| 6081 | s >= end || |
| 6082 | (*s != 'u' && *s != 'U')) { |
| 6083 | continue; |
| 6084 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6085 | outpos--; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6086 | count = *s=='u' ? 4 : 8; |
| 6087 | s++; |
| 6088 | |
| 6089 | /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6090 | for (x = 0, i = 0; i < count; ++i, ++s) { |
| 6091 | c = (unsigned char)*s; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 6092 | if (!Py_ISXDIGIT(c)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6093 | endinpos = s-starts; |
| 6094 | if (unicode_decode_call_errorhandler( |
| 6095 | errors, &errorHandler, |
| 6096 | "rawunicodeescape", "truncated \\uXXXX", |
| 6097 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6098 | &v, &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6099 | goto onError; |
| 6100 | goto nextByte; |
| 6101 | } |
| 6102 | x = (x<<4) & ~0xF; |
| 6103 | if (c >= '0' && c <= '9') |
| 6104 | x += c - '0'; |
| 6105 | else if (c >= 'a' && c <= 'f') |
| 6106 | x += 10 + c - 'a'; |
| 6107 | else |
| 6108 | x += 10 + c - 'A'; |
| 6109 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6110 | if (x <= 0x10ffff) { |
| 6111 | if (unicode_putchar(&v, &outpos, x) < 0) |
| 6112 | goto onError; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 6113 | } else { |
| 6114 | endinpos = s-starts; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6115 | if (unicode_decode_call_errorhandler( |
| 6116 | errors, &errorHandler, |
| 6117 | "rawunicodeescape", "\\Uxxxxxxxx out of range", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6118 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6119 | &v, &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6120 | goto onError; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6121 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6122 | nextByte: |
| 6123 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6124 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6125 | if (PyUnicode_Resize(&v, outpos) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6126 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6127 | Py_XDECREF(errorHandler); |
| 6128 | Py_XDECREF(exc); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 6129 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6130 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6131 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6132 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6133 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6134 | Py_XDECREF(errorHandler); |
| 6135 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6136 | return NULL; |
| 6137 | } |
| 6138 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6139 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6140 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6141 | PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6142 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6143 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6144 | char *p; |
| 6145 | char *q; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6146 | Py_ssize_t expandsize, pos; |
| 6147 | int kind; |
| 6148 | void *data; |
| 6149 | Py_ssize_t len; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6150 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6151 | if (!PyUnicode_Check(unicode)) { |
| 6152 | PyErr_BadArgument(); |
| 6153 | return NULL; |
| 6154 | } |
| 6155 | if (PyUnicode_READY(unicode) < 0) |
| 6156 | return NULL; |
| 6157 | kind = PyUnicode_KIND(unicode); |
| 6158 | data = PyUnicode_DATA(unicode); |
| 6159 | len = PyUnicode_GET_LENGTH(unicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6160 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6161 | switch(kind) { |
| 6162 | case PyUnicode_1BYTE_KIND: expandsize = 4; break; |
| 6163 | case PyUnicode_2BYTE_KIND: expandsize = 6; break; |
| 6164 | case PyUnicode_4BYTE_KIND: expandsize = 10; break; |
| 6165 | } |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 6166 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6167 | if (len > PY_SSIZE_T_MAX / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6168 | return PyErr_NoMemory(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6169 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6170 | repr = PyBytes_FromStringAndSize(NULL, expandsize * len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6171 | if (repr == NULL) |
| 6172 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6173 | if (len == 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6174 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6175 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6176 | p = q = PyBytes_AS_STRING(repr); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6177 | for (pos = 0; pos < len; pos++) { |
| 6178 | Py_UCS4 ch = PyUnicode_READ(kind, data, pos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6179 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 6180 | if (ch >= 0x10000) { |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6181 | *p++ = '\\'; |
| 6182 | *p++ = 'U'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6183 | *p++ = Py_hexdigits[(ch >> 28) & 0xf]; |
| 6184 | *p++ = Py_hexdigits[(ch >> 24) & 0xf]; |
| 6185 | *p++ = Py_hexdigits[(ch >> 20) & 0xf]; |
| 6186 | *p++ = Py_hexdigits[(ch >> 16) & 0xf]; |
| 6187 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 6188 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 6189 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 6190 | *p++ = Py_hexdigits[ch & 15]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6191 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6192 | /* Map 16-bit characters to '\uxxxx' */ |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6193 | else if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6194 | *p++ = '\\'; |
| 6195 | *p++ = 'u'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6196 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 6197 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 6198 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 6199 | *p++ = Py_hexdigits[ch & 15]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6200 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6201 | /* Copy everything else as-is */ |
| 6202 | else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6203 | *p++ = (char) ch; |
| 6204 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6205 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6206 | assert(p > q); |
| 6207 | if (_PyBytes_Resize(&repr, p - q) < 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6208 | return NULL; |
| 6209 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6210 | } |
| 6211 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6212 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6213 | PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
| 6214 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6215 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6216 | PyObject *result; |
| 6217 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 6218 | if (tmp == NULL) |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 6219 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6220 | result = PyUnicode_AsRawUnicodeEscapeString(tmp); |
| 6221 | Py_DECREF(tmp); |
| 6222 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6223 | } |
| 6224 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6225 | /* --- Unicode Internal Codec ------------------------------------------- */ |
| 6226 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6227 | PyObject * |
| 6228 | _PyUnicode_DecodeUnicodeInternal(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6229 | Py_ssize_t size, |
| 6230 | const char *errors) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6231 | { |
| 6232 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6233 | Py_ssize_t startinpos; |
| 6234 | Py_ssize_t endinpos; |
| 6235 | Py_ssize_t outpos; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6236 | PyObject *v; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6237 | const char *end; |
| 6238 | const char *reason; |
| 6239 | PyObject *errorHandler = NULL; |
| 6240 | PyObject *exc = NULL; |
| 6241 | |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6242 | if (PyErr_WarnEx(PyExc_DeprecationWarning, |
Ezio Melotti | 11060a4 | 2011-11-16 09:39:10 +0200 | [diff] [blame] | 6243 | "unicode_internal codec has been deprecated", |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6244 | 1)) |
| 6245 | return NULL; |
| 6246 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6247 | /* XXX overflow detection missing */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6248 | 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] | 6249 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6250 | goto onError; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6251 | if (PyUnicode_GET_LENGTH(v) == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6252 | return v; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6253 | outpos = 0; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6254 | end = s + size; |
| 6255 | |
| 6256 | while (s < end) { |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6257 | Py_UNICODE uch; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6258 | Py_UCS4 ch; |
| 6259 | /* We copy the raw representation one byte at a time because the |
| 6260 | pointer may be unaligned (see test_codeccallbacks). */ |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6261 | ((char *) &uch)[0] = s[0]; |
| 6262 | ((char *) &uch)[1] = s[1]; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6263 | #ifdef Py_UNICODE_WIDE |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6264 | ((char *) &uch)[2] = s[2]; |
| 6265 | ((char *) &uch)[3] = s[3]; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6266 | #endif |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6267 | ch = uch; |
| 6268 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6269 | /* We have to sanity check the raw data, otherwise doom looms for |
| 6270 | some malformed UCS-4 data. */ |
| 6271 | if ( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6272 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6273 | ch > 0x10ffff || |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6274 | #endif |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6275 | end-s < Py_UNICODE_SIZE |
| 6276 | ) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6277 | { |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6278 | startinpos = s - starts; |
| 6279 | if (end-s < Py_UNICODE_SIZE) { |
| 6280 | endinpos = end-starts; |
| 6281 | reason = "truncated input"; |
| 6282 | } |
| 6283 | else { |
| 6284 | endinpos = s - starts + Py_UNICODE_SIZE; |
| 6285 | reason = "illegal code point (> 0x10FFFF)"; |
| 6286 | } |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6287 | if (unicode_decode_call_errorhandler( |
| 6288 | errors, &errorHandler, |
| 6289 | "unicode_internal", reason, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 6290 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6291 | &v, &outpos)) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6292 | goto onError; |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6293 | continue; |
| 6294 | } |
| 6295 | |
| 6296 | s += Py_UNICODE_SIZE; |
| 6297 | #ifndef Py_UNICODE_WIDE |
| 6298 | if (ch >= 0xD800 && ch <= 0xDBFF && s < end) |
| 6299 | { |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6300 | Py_UNICODE uch2; |
| 6301 | ((char *) &uch2)[0] = s[0]; |
| 6302 | ((char *) &uch2)[1] = s[1]; |
| 6303 | if (uch2 >= 0xDC00 && uch2 <= 0xDFFF) |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6304 | { |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6305 | ch = (((uch & 0x3FF)<<10) | (uch2 & 0x3FF)) + 0x10000; |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6306 | s += Py_UNICODE_SIZE; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6307 | } |
| 6308 | } |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6309 | #endif |
| 6310 | |
| 6311 | if (unicode_putchar(&v, &outpos, ch) < 0) |
| 6312 | goto onError; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6313 | } |
| 6314 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6315 | if (PyUnicode_Resize(&v, outpos) < 0) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6316 | goto onError; |
| 6317 | Py_XDECREF(errorHandler); |
| 6318 | Py_XDECREF(exc); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 6319 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6320 | return v; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6321 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6322 | onError: |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6323 | Py_XDECREF(v); |
| 6324 | Py_XDECREF(errorHandler); |
| 6325 | Py_XDECREF(exc); |
| 6326 | return NULL; |
| 6327 | } |
| 6328 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6329 | /* --- Latin-1 Codec ------------------------------------------------------ */ |
| 6330 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6331 | PyObject * |
| 6332 | PyUnicode_DecodeLatin1(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6333 | Py_ssize_t size, |
| 6334 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6335 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6336 | /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 6337 | return _PyUnicode_FromUCS1((unsigned char*)s, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6338 | } |
| 6339 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6340 | /* create or adjust a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6341 | static void |
| 6342 | make_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6343 | const char *encoding, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6344 | PyObject *unicode, |
| 6345 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6346 | const char *reason) |
| 6347 | { |
| 6348 | if (*exceptionObject == NULL) { |
| 6349 | *exceptionObject = PyObject_CallFunction( |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6350 | PyExc_UnicodeEncodeError, "sOnns", |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6351 | encoding, unicode, startpos, endpos, reason); |
| 6352 | } |
| 6353 | else { |
| 6354 | if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 6355 | goto onError; |
| 6356 | if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) |
| 6357 | goto onError; |
| 6358 | if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) |
| 6359 | goto onError; |
| 6360 | return; |
| 6361 | onError: |
| 6362 | Py_DECREF(*exceptionObject); |
| 6363 | *exceptionObject = NULL; |
| 6364 | } |
| 6365 | } |
| 6366 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6367 | /* raises a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6368 | static void |
| 6369 | raise_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6370 | const char *encoding, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6371 | PyObject *unicode, |
| 6372 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6373 | const char *reason) |
| 6374 | { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6375 | make_encode_exception(exceptionObject, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6376 | encoding, unicode, startpos, endpos, reason); |
| 6377 | if (*exceptionObject != NULL) |
| 6378 | PyCodec_StrictErrors(*exceptionObject); |
| 6379 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6380 | |
| 6381 | /* error handling callback helper: |
| 6382 | build arguments, call the callback and check the arguments, |
| 6383 | put the result into newpos and return the replacement string, which |
| 6384 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6385 | static PyObject * |
| 6386 | unicode_encode_call_errorhandler(const char *errors, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6387 | PyObject **errorHandler, |
| 6388 | const char *encoding, const char *reason, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6389 | PyObject *unicode, PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6390 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6391 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6392 | { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6393 | 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] | 6394 | Py_ssize_t len; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6395 | PyObject *restuple; |
| 6396 | PyObject *resunicode; |
| 6397 | |
| 6398 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6399 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6400 | if (*errorHandler == 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 | } |
| 6403 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6404 | if (PyUnicode_READY(unicode) < 0) |
| 6405 | return NULL; |
| 6406 | len = PyUnicode_GET_LENGTH(unicode); |
| 6407 | |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6408 | make_encode_exception(exceptionObject, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6409 | encoding, unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6410 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6411 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6412 | |
| 6413 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6414 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6415 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6416 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6417 | if (!PyTuple_Check(restuple)) { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6418 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6419 | Py_DECREF(restuple); |
| 6420 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6421 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6422 | if (!PyArg_ParseTuple(restuple, argparse, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6423 | &resunicode, newpos)) { |
| 6424 | Py_DECREF(restuple); |
| 6425 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6426 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6427 | if (!PyUnicode_Check(resunicode) && !PyBytes_Check(resunicode)) { |
| 6428 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
| 6429 | Py_DECREF(restuple); |
| 6430 | return NULL; |
| 6431 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6432 | if (*newpos<0) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6433 | *newpos = len + *newpos; |
| 6434 | if (*newpos<0 || *newpos>len) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6435 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 6436 | Py_DECREF(restuple); |
| 6437 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 6438 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6439 | Py_INCREF(resunicode); |
| 6440 | Py_DECREF(restuple); |
| 6441 | return resunicode; |
| 6442 | } |
| 6443 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6444 | static PyObject * |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6445 | unicode_encode_ucs1(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6446 | const char *errors, |
Victor Stinner | fcd9653 | 2011-11-04 00:28:50 +0100 | [diff] [blame] | 6447 | unsigned int limit) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6448 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6449 | /* input state */ |
| 6450 | Py_ssize_t pos=0, size; |
| 6451 | int kind; |
| 6452 | void *data; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6453 | /* output object */ |
| 6454 | PyObject *res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6455 | /* pointer into the output */ |
| 6456 | char *str; |
| 6457 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6458 | Py_ssize_t ressize; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6459 | const char *encoding = (limit == 256) ? "latin-1" : "ascii"; |
| 6460 | 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] | 6461 | PyObject *errorHandler = NULL; |
| 6462 | PyObject *exc = NULL; |
| 6463 | /* the following variable is used for caching string comparisons |
| 6464 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 6465 | int known_errorHandler = -1; |
| 6466 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6467 | if (PyUnicode_READY(unicode) < 0) |
| 6468 | return NULL; |
| 6469 | size = PyUnicode_GET_LENGTH(unicode); |
| 6470 | kind = PyUnicode_KIND(unicode); |
| 6471 | data = PyUnicode_DATA(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6472 | /* allocate enough for a simple encoding without |
| 6473 | replacements, if we need more, we'll resize */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6474 | if (size == 0) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 6475 | return PyBytes_FromStringAndSize(NULL, 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6476 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6477 | if (res == NULL) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6478 | return NULL; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6479 | str = PyBytes_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6480 | ressize = size; |
| 6481 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6482 | while (pos < size) { |
| 6483 | Py_UCS4 c = PyUnicode_READ(kind, data, pos); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6484 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6485 | /* can we encode this? */ |
| 6486 | if (c<limit) { |
| 6487 | /* no overflow check, because we know that the space is enough */ |
| 6488 | *str++ = (char)c; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6489 | ++pos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6490 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6491 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6492 | Py_ssize_t requiredsize; |
| 6493 | PyObject *repunicode; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6494 | Py_ssize_t repsize, newpos, respos, i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6495 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6496 | Py_ssize_t collstart = pos; |
| 6497 | Py_ssize_t collend = pos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6498 | /* find all unecodable characters */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6499 | while ((collend < size) && (PyUnicode_READ(kind, data, collend)>=limit)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6500 | ++collend; |
| 6501 | /* cache callback name lookup (if not done yet, i.e. it's the first error) */ |
| 6502 | if (known_errorHandler==-1) { |
| 6503 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 6504 | known_errorHandler = 1; |
| 6505 | else if (!strcmp(errors, "replace")) |
| 6506 | known_errorHandler = 2; |
| 6507 | else if (!strcmp(errors, "ignore")) |
| 6508 | known_errorHandler = 3; |
| 6509 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 6510 | known_errorHandler = 4; |
| 6511 | else |
| 6512 | known_errorHandler = 0; |
| 6513 | } |
| 6514 | switch (known_errorHandler) { |
| 6515 | case 1: /* strict */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6516 | raise_encode_exception(&exc, encoding, unicode, collstart, collend, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6517 | goto onError; |
| 6518 | case 2: /* replace */ |
| 6519 | while (collstart++<collend) |
| 6520 | *str++ = '?'; /* fall through */ |
| 6521 | case 3: /* ignore */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6522 | pos = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6523 | break; |
| 6524 | case 4: /* xmlcharrefreplace */ |
| 6525 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6526 | /* determine replacement size */ |
| 6527 | for (i = collstart, repsize = 0; i < collend; ++i) { |
| 6528 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 6529 | if (ch < 10) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6530 | repsize += 2+1+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6531 | else if (ch < 100) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6532 | repsize += 2+2+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6533 | else if (ch < 1000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6534 | repsize += 2+3+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6535 | else if (ch < 10000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6536 | repsize += 2+4+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 6537 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6538 | else |
| 6539 | repsize += 2+5+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 6540 | #else |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6541 | else if (ch < 100000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6542 | repsize += 2+5+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6543 | else if (ch < 1000000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6544 | repsize += 2+6+1; |
| 6545 | else |
| 6546 | repsize += 2+7+1; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 6547 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6548 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6549 | requiredsize = respos+repsize+(size-collend); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6550 | if (requiredsize > ressize) { |
| 6551 | if (requiredsize<2*ressize) |
| 6552 | requiredsize = 2*ressize; |
| 6553 | if (_PyBytes_Resize(&res, requiredsize)) |
| 6554 | goto onError; |
| 6555 | str = PyBytes_AS_STRING(res) + respos; |
| 6556 | ressize = requiredsize; |
| 6557 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6558 | /* generate replacement */ |
| 6559 | for (i = collstart; i < collend; ++i) { |
| 6560 | str += sprintf(str, "&#%d;", PyUnicode_READ(kind, data, i)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6561 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6562 | pos = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6563 | break; |
| 6564 | default: |
| 6565 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6566 | encoding, reason, unicode, &exc, |
| 6567 | collstart, collend, &newpos); |
| 6568 | if (repunicode == NULL || (PyUnicode_Check(repunicode) && |
| 6569 | PyUnicode_READY(repunicode) < 0)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6570 | goto onError; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6571 | if (PyBytes_Check(repunicode)) { |
| 6572 | /* Directly copy bytes result to output. */ |
| 6573 | repsize = PyBytes_Size(repunicode); |
| 6574 | if (repsize > 1) { |
| 6575 | /* Make room for all additional bytes. */ |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 6576 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6577 | if (_PyBytes_Resize(&res, ressize+repsize-1)) { |
| 6578 | Py_DECREF(repunicode); |
| 6579 | goto onError; |
| 6580 | } |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 6581 | str = PyBytes_AS_STRING(res) + respos; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6582 | ressize += repsize-1; |
| 6583 | } |
| 6584 | memcpy(str, PyBytes_AsString(repunicode), repsize); |
| 6585 | str += repsize; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6586 | pos = newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6587 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6588 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6589 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6590 | /* need more space? (at least enough for what we |
| 6591 | have+the replacement+the rest of the string, so |
| 6592 | we won't have to check space for encodable characters) */ |
| 6593 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6594 | repsize = PyUnicode_GET_LENGTH(repunicode); |
| 6595 | requiredsize = respos+repsize+(size-collend); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6596 | if (requiredsize > ressize) { |
| 6597 | if (requiredsize<2*ressize) |
| 6598 | requiredsize = 2*ressize; |
| 6599 | if (_PyBytes_Resize(&res, requiredsize)) { |
| 6600 | Py_DECREF(repunicode); |
| 6601 | goto onError; |
| 6602 | } |
| 6603 | str = PyBytes_AS_STRING(res) + respos; |
| 6604 | ressize = requiredsize; |
| 6605 | } |
| 6606 | /* check if there is anything unencodable in the replacement |
| 6607 | and copy it to the output */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6608 | for (i = 0; repsize-->0; ++i, ++str) { |
| 6609 | c = PyUnicode_READ_CHAR(repunicode, i); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6610 | if (c >= limit) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6611 | raise_encode_exception(&exc, encoding, unicode, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6612 | pos, pos+1, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6613 | Py_DECREF(repunicode); |
| 6614 | goto onError; |
| 6615 | } |
| 6616 | *str = (char)c; |
| 6617 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6618 | pos = newpos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6619 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6620 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6621 | } |
| 6622 | } |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6623 | /* Resize if we allocated to much */ |
| 6624 | size = str - PyBytes_AS_STRING(res); |
| 6625 | if (size < ressize) { /* If this falls res will be NULL */ |
Alexandre Vassalotti | bad1b92 | 2008-12-27 09:49:09 +0000 | [diff] [blame] | 6626 | assert(size >= 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6627 | if (_PyBytes_Resize(&res, size) < 0) |
| 6628 | goto onError; |
| 6629 | } |
| 6630 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6631 | Py_XDECREF(errorHandler); |
| 6632 | Py_XDECREF(exc); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6633 | return res; |
| 6634 | |
| 6635 | onError: |
| 6636 | Py_XDECREF(res); |
| 6637 | Py_XDECREF(errorHandler); |
| 6638 | Py_XDECREF(exc); |
| 6639 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6640 | } |
| 6641 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6642 | /* Deprecated */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6643 | PyObject * |
| 6644 | PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6645 | Py_ssize_t size, |
| 6646 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6647 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6648 | PyObject *result; |
| 6649 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 6650 | if (unicode == NULL) |
| 6651 | return NULL; |
| 6652 | result = unicode_encode_ucs1(unicode, errors, 256); |
| 6653 | Py_DECREF(unicode); |
| 6654 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6655 | } |
| 6656 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6657 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6658 | _PyUnicode_AsLatin1String(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6659 | { |
| 6660 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6661 | PyErr_BadArgument(); |
| 6662 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6663 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6664 | if (PyUnicode_READY(unicode) == -1) |
| 6665 | return NULL; |
| 6666 | /* Fast path: if it is a one-byte string, construct |
| 6667 | bytes object directly. */ |
| 6668 | if (PyUnicode_KIND(unicode) == PyUnicode_1BYTE_KIND) |
| 6669 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 6670 | PyUnicode_GET_LENGTH(unicode)); |
| 6671 | /* Non-Latin-1 characters present. Defer to above function to |
| 6672 | raise the exception. */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6673 | return unicode_encode_ucs1(unicode, errors, 256); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6674 | } |
| 6675 | |
| 6676 | PyObject* |
| 6677 | PyUnicode_AsLatin1String(PyObject *unicode) |
| 6678 | { |
| 6679 | return _PyUnicode_AsLatin1String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6680 | } |
| 6681 | |
| 6682 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ |
| 6683 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6684 | PyObject * |
| 6685 | PyUnicode_DecodeASCII(const char *s, |
| 6686 | Py_ssize_t size, |
| 6687 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6688 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6689 | const char *starts = s; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6690 | PyObject *v; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6691 | int kind; |
| 6692 | void *data; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6693 | Py_ssize_t startinpos; |
| 6694 | Py_ssize_t endinpos; |
| 6695 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6696 | const char *e; |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6697 | int has_error; |
| 6698 | const unsigned char *p = (const unsigned char *)s; |
| 6699 | const unsigned char *end = p + size; |
| 6700 | 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] | 6701 | PyObject *errorHandler = NULL; |
| 6702 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6703 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6704 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6705 | if (size == 1 && (unsigned char)s[0] < 128) |
| 6706 | return get_latin1_char((unsigned char)s[0]); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6707 | |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6708 | has_error = 0; |
| 6709 | while (p < end && !has_error) { |
| 6710 | /* Fast path, see below in PyUnicode_DecodeUTF8Stateful for |
| 6711 | an explanation. */ |
| 6712 | if (!((size_t) p & LONG_PTR_MASK)) { |
| 6713 | /* Help register allocation */ |
| 6714 | register const unsigned char *_p = p; |
| 6715 | while (_p < aligned_end) { |
| 6716 | unsigned long value = *(unsigned long *) _p; |
| 6717 | if (value & ASCII_CHAR_MASK) { |
| 6718 | has_error = 1; |
| 6719 | break; |
| 6720 | } |
| 6721 | _p += SIZEOF_LONG; |
| 6722 | } |
| 6723 | if (_p == end) |
| 6724 | break; |
| 6725 | if (has_error) |
| 6726 | break; |
| 6727 | p = _p; |
| 6728 | } |
| 6729 | if (*p & 0x80) { |
| 6730 | has_error = 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6731 | break; |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6732 | } |
| 6733 | else { |
| 6734 | ++p; |
| 6735 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 6736 | } |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6737 | if (!has_error) |
| 6738 | return unicode_fromascii((const unsigned char *)s, size); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6739 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6740 | v = PyUnicode_New(size, 127); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6741 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6742 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6743 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6744 | return v; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6745 | kind = PyUnicode_KIND(v); |
| 6746 | data = PyUnicode_DATA(v); |
| 6747 | outpos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6748 | e = s + size; |
| 6749 | while (s < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6750 | register unsigned char c = (unsigned char)*s; |
| 6751 | if (c < 128) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6752 | PyUnicode_WRITE(kind, data, outpos++, c); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6753 | ++s; |
| 6754 | } |
| 6755 | else { |
| 6756 | startinpos = s-starts; |
| 6757 | endinpos = startinpos + 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6758 | if (unicode_decode_call_errorhandler( |
| 6759 | errors, &errorHandler, |
| 6760 | "ascii", "ordinal not in range(128)", |
| 6761 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6762 | &v, &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6763 | goto onError; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6764 | kind = PyUnicode_KIND(v); |
| 6765 | data = PyUnicode_DATA(v); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6766 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6767 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6768 | if (PyUnicode_Resize(&v, outpos) < 0) |
| 6769 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6770 | Py_XDECREF(errorHandler); |
| 6771 | Py_XDECREF(exc); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 6772 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6773 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6774 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6775 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6776 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6777 | Py_XDECREF(errorHandler); |
| 6778 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6779 | return NULL; |
| 6780 | } |
| 6781 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6782 | /* Deprecated */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6783 | PyObject * |
| 6784 | PyUnicode_EncodeASCII(const Py_UNICODE *p, |
| 6785 | Py_ssize_t size, |
| 6786 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6787 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6788 | PyObject *result; |
| 6789 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 6790 | if (unicode == NULL) |
| 6791 | return NULL; |
| 6792 | result = unicode_encode_ucs1(unicode, errors, 128); |
| 6793 | Py_DECREF(unicode); |
| 6794 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6795 | } |
| 6796 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6797 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6798 | _PyUnicode_AsASCIIString(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6799 | { |
| 6800 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6801 | PyErr_BadArgument(); |
| 6802 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6803 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6804 | if (PyUnicode_READY(unicode) == -1) |
| 6805 | return NULL; |
| 6806 | /* Fast path: if it is an ASCII-only string, construct bytes object |
| 6807 | directly. Else defer to above function to raise the exception. */ |
| 6808 | if (PyUnicode_MAX_CHAR_VALUE(unicode) < 128) |
| 6809 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 6810 | PyUnicode_GET_LENGTH(unicode)); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6811 | return unicode_encode_ucs1(unicode, errors, 128); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6812 | } |
| 6813 | |
| 6814 | PyObject * |
| 6815 | PyUnicode_AsASCIIString(PyObject *unicode) |
| 6816 | { |
| 6817 | return _PyUnicode_AsASCIIString(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6818 | } |
| 6819 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 6820 | #ifdef HAVE_MBCS |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6821 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6822 | /* --- MBCS codecs for Windows -------------------------------------------- */ |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6823 | |
Hirokazu Yamamoto | 3530246 | 2009-03-21 13:23:27 +0000 | [diff] [blame] | 6824 | #if SIZEOF_INT < SIZEOF_SIZE_T |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6825 | #define NEED_RETRY |
| 6826 | #endif |
| 6827 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6828 | #ifndef WC_ERR_INVALID_CHARS |
| 6829 | # define WC_ERR_INVALID_CHARS 0x0080 |
| 6830 | #endif |
| 6831 | |
| 6832 | static char* |
| 6833 | code_page_name(UINT code_page, PyObject **obj) |
| 6834 | { |
| 6835 | *obj = NULL; |
| 6836 | if (code_page == CP_ACP) |
| 6837 | return "mbcs"; |
| 6838 | if (code_page == CP_UTF7) |
| 6839 | return "CP_UTF7"; |
| 6840 | if (code_page == CP_UTF8) |
| 6841 | return "CP_UTF8"; |
| 6842 | |
| 6843 | *obj = PyBytes_FromFormat("cp%u", code_page); |
| 6844 | if (*obj == NULL) |
| 6845 | return NULL; |
| 6846 | return PyBytes_AS_STRING(*obj); |
| 6847 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6848 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6849 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6850 | is_dbcs_lead_byte(UINT code_page, const char *s, int offset) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6851 | { |
| 6852 | const char *curr = s + offset; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6853 | const char *prev; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6854 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6855 | if (!IsDBCSLeadByteEx(code_page, *curr)) |
| 6856 | return 0; |
| 6857 | |
| 6858 | prev = CharPrevExA(code_page, s, curr, 0); |
| 6859 | if (prev == curr) |
| 6860 | return 1; |
| 6861 | /* FIXME: This code is limited to "true" double-byte encodings, |
| 6862 | as it assumes an incomplete character consists of a single |
| 6863 | byte. */ |
| 6864 | if (curr - prev == 2) |
| 6865 | return 1; |
| 6866 | if (!IsDBCSLeadByteEx(code_page, *prev)) |
| 6867 | return 1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6868 | return 0; |
| 6869 | } |
| 6870 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6871 | static DWORD |
| 6872 | decode_code_page_flags(UINT code_page) |
| 6873 | { |
| 6874 | if (code_page == CP_UTF7) { |
| 6875 | /* The CP_UTF7 decoder only supports flags=0 */ |
| 6876 | return 0; |
| 6877 | } |
| 6878 | else |
| 6879 | return MB_ERR_INVALID_CHARS; |
| 6880 | } |
| 6881 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6882 | /* |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6883 | * Decode a byte string from a Windows code page into unicode object in strict |
| 6884 | * mode. |
| 6885 | * |
| 6886 | * Returns consumed size if succeed, returns -2 on decode error, or raise a |
| 6887 | * WindowsError and returns -1 on other error. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6888 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6889 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6890 | decode_code_page_strict(UINT code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6891 | PyObject **v, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6892 | const char *in, |
| 6893 | int insize) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6894 | { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6895 | const DWORD flags = decode_code_page_flags(code_page); |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 6896 | wchar_t *out; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6897 | DWORD outsize; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6898 | |
| 6899 | /* First get the size of the result */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6900 | assert(insize > 0); |
| 6901 | outsize = MultiByteToWideChar(code_page, flags, in, insize, NULL, 0); |
| 6902 | if (outsize <= 0) |
| 6903 | goto error; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6904 | |
| 6905 | if (*v == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6906 | /* Create unicode object */ |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6907 | *v = (PyObject*)_PyUnicode_New(outsize); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6908 | if (*v == NULL) |
| 6909 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6910 | out = PyUnicode_AS_UNICODE(*v); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6911 | } |
| 6912 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6913 | /* Extend unicode object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6914 | Py_ssize_t n = PyUnicode_GET_SIZE(*v); |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6915 | if (PyUnicode_Resize(v, n + outsize) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6916 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6917 | out = PyUnicode_AS_UNICODE(*v) + n; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6918 | } |
| 6919 | |
| 6920 | /* Do the conversion */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6921 | outsize = MultiByteToWideChar(code_page, flags, in, insize, out, outsize); |
| 6922 | if (outsize <= 0) |
| 6923 | goto error; |
| 6924 | return insize; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6925 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6926 | error: |
| 6927 | if (GetLastError() == ERROR_NO_UNICODE_TRANSLATION) |
| 6928 | return -2; |
| 6929 | PyErr_SetFromWindowsErr(0); |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6930 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6931 | } |
| 6932 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6933 | /* |
| 6934 | * Decode a byte string from a code page into unicode object with an error |
| 6935 | * handler. |
| 6936 | * |
| 6937 | * Returns consumed size if succeed, or raise a WindowsError or |
| 6938 | * UnicodeDecodeError exception and returns -1 on error. |
| 6939 | */ |
| 6940 | static int |
| 6941 | decode_code_page_errors(UINT code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6942 | PyObject **v, |
| 6943 | const char *in, const int size, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6944 | const char *errors) |
| 6945 | { |
| 6946 | const char *startin = in; |
| 6947 | const char *endin = in + size; |
| 6948 | const DWORD flags = decode_code_page_flags(code_page); |
| 6949 | /* Ideally, we should get reason from FormatMessage. This is the Windows |
| 6950 | 2000 English version of the message. */ |
| 6951 | const char *reason = "No mapping for the Unicode character exists " |
| 6952 | "in the target code page."; |
| 6953 | /* each step cannot decode more than 1 character, but a character can be |
| 6954 | represented as a surrogate pair */ |
| 6955 | wchar_t buffer[2], *startout, *out; |
| 6956 | int insize, outsize; |
| 6957 | PyObject *errorHandler = NULL; |
| 6958 | PyObject *exc = NULL; |
| 6959 | PyObject *encoding_obj = NULL; |
| 6960 | char *encoding; |
| 6961 | DWORD err; |
| 6962 | int ret = -1; |
| 6963 | |
| 6964 | assert(size > 0); |
| 6965 | |
| 6966 | encoding = code_page_name(code_page, &encoding_obj); |
| 6967 | if (encoding == NULL) |
| 6968 | return -1; |
| 6969 | |
| 6970 | if (errors == NULL || strcmp(errors, "strict") == 0) { |
| 6971 | /* The last error was ERROR_NO_UNICODE_TRANSLATION, then we raise a |
| 6972 | UnicodeDecodeError. */ |
| 6973 | make_decode_exception(&exc, encoding, in, size, 0, 0, reason); |
| 6974 | if (exc != NULL) { |
| 6975 | PyCodec_StrictErrors(exc); |
| 6976 | Py_CLEAR(exc); |
| 6977 | } |
| 6978 | goto error; |
| 6979 | } |
| 6980 | |
| 6981 | if (*v == NULL) { |
| 6982 | /* Create unicode object */ |
| 6983 | if (size > PY_SSIZE_T_MAX / (Py_ssize_t)Py_ARRAY_LENGTH(buffer)) { |
| 6984 | PyErr_NoMemory(); |
| 6985 | goto error; |
| 6986 | } |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6987 | *v = (PyObject*)_PyUnicode_New(size * Py_ARRAY_LENGTH(buffer)); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6988 | if (*v == NULL) |
| 6989 | goto error; |
| 6990 | startout = PyUnicode_AS_UNICODE(*v); |
| 6991 | } |
| 6992 | else { |
| 6993 | /* Extend unicode object */ |
| 6994 | Py_ssize_t n = PyUnicode_GET_SIZE(*v); |
| 6995 | if (size > (PY_SSIZE_T_MAX - n) / (Py_ssize_t)Py_ARRAY_LENGTH(buffer)) { |
| 6996 | PyErr_NoMemory(); |
| 6997 | goto error; |
| 6998 | } |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6999 | if (PyUnicode_Resize(v, n + size * Py_ARRAY_LENGTH(buffer)) < 0) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7000 | goto error; |
| 7001 | startout = PyUnicode_AS_UNICODE(*v) + n; |
| 7002 | } |
| 7003 | |
| 7004 | /* Decode the byte string character per character */ |
| 7005 | out = startout; |
| 7006 | while (in < endin) |
| 7007 | { |
| 7008 | /* Decode a character */ |
| 7009 | insize = 1; |
| 7010 | do |
| 7011 | { |
| 7012 | outsize = MultiByteToWideChar(code_page, flags, |
| 7013 | in, insize, |
| 7014 | buffer, Py_ARRAY_LENGTH(buffer)); |
| 7015 | if (outsize > 0) |
| 7016 | break; |
| 7017 | err = GetLastError(); |
| 7018 | if (err != ERROR_NO_UNICODE_TRANSLATION |
| 7019 | && err != ERROR_INSUFFICIENT_BUFFER) |
| 7020 | { |
| 7021 | PyErr_SetFromWindowsErr(0); |
| 7022 | goto error; |
| 7023 | } |
| 7024 | insize++; |
| 7025 | } |
| 7026 | /* 4=maximum length of a UTF-8 sequence */ |
| 7027 | while (insize <= 4 && (in + insize) <= endin); |
| 7028 | |
| 7029 | if (outsize <= 0) { |
| 7030 | Py_ssize_t startinpos, endinpos, outpos; |
| 7031 | |
| 7032 | startinpos = in - startin; |
| 7033 | endinpos = startinpos + 1; |
| 7034 | outpos = out - PyUnicode_AS_UNICODE(*v); |
| 7035 | if (unicode_decode_call_errorhandler( |
| 7036 | errors, &errorHandler, |
| 7037 | encoding, reason, |
| 7038 | &startin, &endin, &startinpos, &endinpos, &exc, &in, |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 7039 | v, &outpos)) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7040 | { |
| 7041 | goto error; |
| 7042 | } |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 7043 | out = PyUnicode_AS_UNICODE(*v) + outpos; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7044 | } |
| 7045 | else { |
| 7046 | in += insize; |
| 7047 | memcpy(out, buffer, outsize * sizeof(wchar_t)); |
| 7048 | out += outsize; |
| 7049 | } |
| 7050 | } |
| 7051 | |
| 7052 | /* write a NUL character at the end */ |
| 7053 | *out = 0; |
| 7054 | |
| 7055 | /* Extend unicode object */ |
| 7056 | outsize = out - startout; |
| 7057 | assert(outsize <= PyUnicode_WSTR_LENGTH(*v)); |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7058 | if (PyUnicode_Resize(v, outsize) < 0) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7059 | goto error; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7060 | ret = size; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7061 | |
| 7062 | error: |
| 7063 | Py_XDECREF(encoding_obj); |
| 7064 | Py_XDECREF(errorHandler); |
| 7065 | Py_XDECREF(exc); |
| 7066 | return ret; |
| 7067 | } |
| 7068 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7069 | static PyObject * |
| 7070 | decode_code_page_stateful(int code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7071 | const char *s, Py_ssize_t size, |
| 7072 | const char *errors, Py_ssize_t *consumed) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7073 | { |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7074 | PyObject *v = NULL; |
| 7075 | int chunk_size, final, converted, done; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7076 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7077 | if (code_page < 0) { |
| 7078 | PyErr_SetString(PyExc_ValueError, "invalid code page number"); |
| 7079 | return NULL; |
| 7080 | } |
| 7081 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7082 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7083 | *consumed = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7084 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7085 | do |
| 7086 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7087 | #ifdef NEED_RETRY |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7088 | if (size > INT_MAX) { |
| 7089 | chunk_size = INT_MAX; |
| 7090 | final = 0; |
| 7091 | done = 0; |
| 7092 | } |
| 7093 | else |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7094 | #endif |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7095 | { |
| 7096 | chunk_size = (int)size; |
| 7097 | final = (consumed == NULL); |
| 7098 | done = 1; |
| 7099 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7100 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7101 | /* Skip trailing lead-byte unless 'final' is set */ |
| 7102 | if (!final && is_dbcs_lead_byte(code_page, s, chunk_size - 1)) |
| 7103 | --chunk_size; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7104 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7105 | if (chunk_size == 0 && done) { |
| 7106 | if (v != NULL) |
| 7107 | break; |
| 7108 | Py_INCREF(unicode_empty); |
| 7109 | return unicode_empty; |
| 7110 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7111 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7112 | |
| 7113 | converted = decode_code_page_strict(code_page, &v, |
| 7114 | s, chunk_size); |
| 7115 | if (converted == -2) |
| 7116 | converted = decode_code_page_errors(code_page, &v, |
| 7117 | s, chunk_size, |
| 7118 | errors); |
| 7119 | assert(converted != 0); |
| 7120 | |
| 7121 | if (converted < 0) { |
| 7122 | Py_XDECREF(v); |
| 7123 | return NULL; |
| 7124 | } |
| 7125 | |
| 7126 | if (consumed) |
| 7127 | *consumed += converted; |
| 7128 | |
| 7129 | s += converted; |
| 7130 | size -= converted; |
| 7131 | } while (!done); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7132 | |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 7133 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 7134 | if (_PyUnicode_READY_REPLACE(&v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7135 | Py_DECREF(v); |
| 7136 | return NULL; |
| 7137 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 7138 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 7139 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7140 | return v; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7141 | } |
| 7142 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7143 | PyObject * |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7144 | PyUnicode_DecodeCodePageStateful(int code_page, |
| 7145 | const char *s, |
| 7146 | Py_ssize_t size, |
| 7147 | const char *errors, |
| 7148 | Py_ssize_t *consumed) |
| 7149 | { |
| 7150 | return decode_code_page_stateful(code_page, s, size, errors, consumed); |
| 7151 | } |
| 7152 | |
| 7153 | PyObject * |
| 7154 | PyUnicode_DecodeMBCSStateful(const char *s, |
| 7155 | Py_ssize_t size, |
| 7156 | const char *errors, |
| 7157 | Py_ssize_t *consumed) |
| 7158 | { |
| 7159 | return decode_code_page_stateful(CP_ACP, s, size, errors, consumed); |
| 7160 | } |
| 7161 | |
| 7162 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7163 | PyUnicode_DecodeMBCS(const char *s, |
| 7164 | Py_ssize_t size, |
| 7165 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7166 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7167 | return PyUnicode_DecodeMBCSStateful(s, size, errors, NULL); |
| 7168 | } |
| 7169 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7170 | static DWORD |
| 7171 | encode_code_page_flags(UINT code_page, const char *errors) |
| 7172 | { |
| 7173 | if (code_page == CP_UTF8) { |
| 7174 | if (winver.dwMajorVersion >= 6) |
| 7175 | /* CP_UTF8 supports WC_ERR_INVALID_CHARS on Windows Vista |
| 7176 | and later */ |
| 7177 | return WC_ERR_INVALID_CHARS; |
| 7178 | else |
| 7179 | /* CP_UTF8 only supports flags=0 on Windows older than Vista */ |
| 7180 | return 0; |
| 7181 | } |
| 7182 | else if (code_page == CP_UTF7) { |
| 7183 | /* CP_UTF7 only supports flags=0 */ |
| 7184 | return 0; |
| 7185 | } |
| 7186 | else { |
| 7187 | if (errors != NULL && strcmp(errors, "replace") == 0) |
| 7188 | return 0; |
| 7189 | else |
| 7190 | return WC_NO_BEST_FIT_CHARS; |
| 7191 | } |
| 7192 | } |
| 7193 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7194 | /* |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7195 | * Encode a Unicode string to a Windows code page into a byte string in strict |
| 7196 | * mode. |
| 7197 | * |
| 7198 | * Returns consumed characters if succeed, returns -2 on encode error, or raise |
| 7199 | * a WindowsError and returns -1 on other error. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7200 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7201 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7202 | encode_code_page_strict(UINT code_page, PyObject **outbytes, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7203 | PyObject *unicode, Py_ssize_t offset, int len, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7204 | const char* errors) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7205 | { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7206 | BOOL usedDefaultChar = FALSE; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7207 | BOOL *pusedDefaultChar = &usedDefaultChar; |
| 7208 | int outsize; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7209 | PyObject *exc = NULL; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 7210 | wchar_t *p; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7211 | Py_ssize_t size; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7212 | const DWORD flags = encode_code_page_flags(code_page, NULL); |
| 7213 | char *out; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7214 | /* Create a substring so that we can get the UTF-16 representation |
| 7215 | of just the slice under consideration. */ |
| 7216 | PyObject *substring; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7217 | |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7218 | assert(len > 0); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7219 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7220 | if (code_page != CP_UTF8 && code_page != CP_UTF7) |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7221 | pusedDefaultChar = &usedDefaultChar; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7222 | else |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7223 | pusedDefaultChar = NULL; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7224 | |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7225 | substring = PyUnicode_Substring(unicode, offset, offset+len); |
| 7226 | if (substring == NULL) |
| 7227 | return -1; |
| 7228 | p = PyUnicode_AsUnicodeAndSize(substring, &size); |
| 7229 | if (p == NULL) { |
| 7230 | Py_DECREF(substring); |
| 7231 | return -1; |
| 7232 | } |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7233 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7234 | /* First get the size of the result */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7235 | outsize = WideCharToMultiByte(code_page, flags, |
| 7236 | p, size, |
| 7237 | NULL, 0, |
| 7238 | NULL, pusedDefaultChar); |
| 7239 | if (outsize <= 0) |
| 7240 | goto error; |
| 7241 | /* If we used a default char, then we failed! */ |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7242 | if (pusedDefaultChar && *pusedDefaultChar) { |
| 7243 | Py_DECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7244 | return -2; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7245 | } |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7246 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7247 | if (*outbytes == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7248 | /* Create string object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7249 | *outbytes = PyBytes_FromStringAndSize(NULL, outsize); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7250 | if (*outbytes == NULL) { |
| 7251 | Py_DECREF(substring); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7252 | return -1; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7253 | } |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7254 | out = PyBytes_AS_STRING(*outbytes); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7255 | } |
| 7256 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7257 | /* Extend string object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7258 | const Py_ssize_t n = PyBytes_Size(*outbytes); |
| 7259 | if (outsize > PY_SSIZE_T_MAX - n) { |
| 7260 | PyErr_NoMemory(); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7261 | Py_DECREF(substring); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7262 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7263 | } |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7264 | if (_PyBytes_Resize(outbytes, n + outsize) < 0) { |
| 7265 | Py_DECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7266 | return -1; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7267 | } |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7268 | out = PyBytes_AS_STRING(*outbytes) + n; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7269 | } |
| 7270 | |
| 7271 | /* Do the conversion */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7272 | outsize = WideCharToMultiByte(code_page, flags, |
| 7273 | p, size, |
| 7274 | out, outsize, |
| 7275 | NULL, pusedDefaultChar); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7276 | Py_CLEAR(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7277 | if (outsize <= 0) |
| 7278 | goto error; |
| 7279 | if (pusedDefaultChar && *pusedDefaultChar) |
| 7280 | return -2; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7281 | return 0; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7282 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7283 | error: |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7284 | Py_XDECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7285 | if (GetLastError() == ERROR_NO_UNICODE_TRANSLATION) |
| 7286 | return -2; |
| 7287 | PyErr_SetFromWindowsErr(0); |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7288 | return -1; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7289 | } |
| 7290 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7291 | /* |
| 7292 | * Encode a Unicode string to a Windows code page into a byte string using a |
| 7293 | * error handler. |
| 7294 | * |
| 7295 | * Returns consumed characters if succeed, or raise a WindowsError and returns |
| 7296 | * -1 on other error. |
| 7297 | */ |
| 7298 | static int |
| 7299 | encode_code_page_errors(UINT code_page, PyObject **outbytes, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7300 | PyObject *unicode, Py_ssize_t unicode_offset, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7301 | Py_ssize_t insize, const char* errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7302 | { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7303 | const DWORD flags = encode_code_page_flags(code_page, errors); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7304 | Py_ssize_t pos = unicode_offset; |
| 7305 | Py_ssize_t endin = unicode_offset + insize; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7306 | /* Ideally, we should get reason from FormatMessage. This is the Windows |
| 7307 | 2000 English version of the message. */ |
| 7308 | const char *reason = "invalid character"; |
| 7309 | /* 4=maximum length of a UTF-8 sequence */ |
| 7310 | char buffer[4]; |
| 7311 | BOOL usedDefaultChar = FALSE, *pusedDefaultChar; |
| 7312 | Py_ssize_t outsize; |
| 7313 | char *out; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7314 | PyObject *errorHandler = NULL; |
| 7315 | PyObject *exc = NULL; |
| 7316 | PyObject *encoding_obj = NULL; |
| 7317 | char *encoding; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7318 | Py_ssize_t newpos, newoutsize; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7319 | PyObject *rep; |
| 7320 | int ret = -1; |
| 7321 | |
| 7322 | assert(insize > 0); |
| 7323 | |
| 7324 | encoding = code_page_name(code_page, &encoding_obj); |
| 7325 | if (encoding == NULL) |
| 7326 | return -1; |
| 7327 | |
| 7328 | if (errors == NULL || strcmp(errors, "strict") == 0) { |
| 7329 | /* The last error was ERROR_NO_UNICODE_TRANSLATION, |
| 7330 | then we raise a UnicodeEncodeError. */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 7331 | make_encode_exception(&exc, encoding, unicode, 0, 0, reason); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7332 | if (exc != NULL) { |
| 7333 | PyCodec_StrictErrors(exc); |
| 7334 | Py_DECREF(exc); |
| 7335 | } |
| 7336 | Py_XDECREF(encoding_obj); |
| 7337 | return -1; |
| 7338 | } |
| 7339 | |
| 7340 | if (code_page != CP_UTF8 && code_page != CP_UTF7) |
| 7341 | pusedDefaultChar = &usedDefaultChar; |
| 7342 | else |
| 7343 | pusedDefaultChar = NULL; |
| 7344 | |
| 7345 | if (Py_ARRAY_LENGTH(buffer) > PY_SSIZE_T_MAX / insize) { |
| 7346 | PyErr_NoMemory(); |
| 7347 | goto error; |
| 7348 | } |
| 7349 | outsize = insize * Py_ARRAY_LENGTH(buffer); |
| 7350 | |
| 7351 | if (*outbytes == NULL) { |
| 7352 | /* Create string object */ |
| 7353 | *outbytes = PyBytes_FromStringAndSize(NULL, outsize); |
| 7354 | if (*outbytes == NULL) |
| 7355 | goto error; |
| 7356 | out = PyBytes_AS_STRING(*outbytes); |
| 7357 | } |
| 7358 | else { |
| 7359 | /* Extend string object */ |
| 7360 | Py_ssize_t n = PyBytes_Size(*outbytes); |
| 7361 | if (n > PY_SSIZE_T_MAX - outsize) { |
| 7362 | PyErr_NoMemory(); |
| 7363 | goto error; |
| 7364 | } |
| 7365 | if (_PyBytes_Resize(outbytes, n + outsize) < 0) |
| 7366 | goto error; |
| 7367 | out = PyBytes_AS_STRING(*outbytes) + n; |
| 7368 | } |
| 7369 | |
| 7370 | /* Encode the string character per character */ |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7371 | while (pos < endin) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7372 | { |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7373 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, pos); |
| 7374 | wchar_t chars[2]; |
| 7375 | int charsize; |
| 7376 | if (ch < 0x10000) { |
| 7377 | chars[0] = (wchar_t)ch; |
| 7378 | charsize = 1; |
| 7379 | } |
| 7380 | else { |
| 7381 | ch -= 0x10000; |
| 7382 | chars[0] = 0xd800 + (ch >> 10); |
| 7383 | chars[1] = 0xdc00 + (ch & 0x3ff); |
| 7384 | charsize = 2; |
| 7385 | } |
| 7386 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7387 | outsize = WideCharToMultiByte(code_page, flags, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7388 | chars, charsize, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7389 | buffer, Py_ARRAY_LENGTH(buffer), |
| 7390 | NULL, pusedDefaultChar); |
| 7391 | if (outsize > 0) { |
| 7392 | if (pusedDefaultChar == NULL || !(*pusedDefaultChar)) |
| 7393 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7394 | pos++; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7395 | memcpy(out, buffer, outsize); |
| 7396 | out += outsize; |
| 7397 | continue; |
| 7398 | } |
| 7399 | } |
| 7400 | else if (GetLastError() != ERROR_NO_UNICODE_TRANSLATION) { |
| 7401 | PyErr_SetFromWindowsErr(0); |
| 7402 | goto error; |
| 7403 | } |
| 7404 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7405 | rep = unicode_encode_call_errorhandler( |
| 7406 | errors, &errorHandler, encoding, reason, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7407 | unicode, &exc, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7408 | pos, pos + 1, &newpos); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7409 | if (rep == NULL) |
| 7410 | goto error; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7411 | pos = newpos; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7412 | |
| 7413 | if (PyBytes_Check(rep)) { |
| 7414 | outsize = PyBytes_GET_SIZE(rep); |
| 7415 | if (outsize != 1) { |
| 7416 | Py_ssize_t offset = out - PyBytes_AS_STRING(*outbytes); |
| 7417 | newoutsize = PyBytes_GET_SIZE(*outbytes) + (outsize - 1); |
| 7418 | if (_PyBytes_Resize(outbytes, newoutsize) < 0) { |
| 7419 | Py_DECREF(rep); |
| 7420 | goto error; |
| 7421 | } |
| 7422 | out = PyBytes_AS_STRING(*outbytes) + offset; |
| 7423 | } |
| 7424 | memcpy(out, PyBytes_AS_STRING(rep), outsize); |
| 7425 | out += outsize; |
| 7426 | } |
| 7427 | else { |
| 7428 | Py_ssize_t i; |
| 7429 | enum PyUnicode_Kind kind; |
| 7430 | void *data; |
| 7431 | |
| 7432 | if (PyUnicode_READY(rep) < 0) { |
| 7433 | Py_DECREF(rep); |
| 7434 | goto error; |
| 7435 | } |
| 7436 | |
| 7437 | outsize = PyUnicode_GET_LENGTH(rep); |
| 7438 | if (outsize != 1) { |
| 7439 | Py_ssize_t offset = out - PyBytes_AS_STRING(*outbytes); |
| 7440 | newoutsize = PyBytes_GET_SIZE(*outbytes) + (outsize - 1); |
| 7441 | if (_PyBytes_Resize(outbytes, newoutsize) < 0) { |
| 7442 | Py_DECREF(rep); |
| 7443 | goto error; |
| 7444 | } |
| 7445 | out = PyBytes_AS_STRING(*outbytes) + offset; |
| 7446 | } |
| 7447 | kind = PyUnicode_KIND(rep); |
| 7448 | data = PyUnicode_DATA(rep); |
| 7449 | for (i=0; i < outsize; i++) { |
| 7450 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 7451 | if (ch > 127) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 7452 | raise_encode_exception(&exc, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7453 | encoding, unicode, |
| 7454 | pos, pos + 1, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7455 | "unable to encode error handler result to ASCII"); |
| 7456 | Py_DECREF(rep); |
| 7457 | goto error; |
| 7458 | } |
| 7459 | *out = (unsigned char)ch; |
| 7460 | out++; |
| 7461 | } |
| 7462 | } |
| 7463 | Py_DECREF(rep); |
| 7464 | } |
| 7465 | /* write a NUL byte */ |
| 7466 | *out = 0; |
| 7467 | outsize = out - PyBytes_AS_STRING(*outbytes); |
| 7468 | assert(outsize <= PyBytes_GET_SIZE(*outbytes)); |
| 7469 | if (_PyBytes_Resize(outbytes, outsize) < 0) |
| 7470 | goto error; |
| 7471 | ret = 0; |
| 7472 | |
| 7473 | error: |
| 7474 | Py_XDECREF(encoding_obj); |
| 7475 | Py_XDECREF(errorHandler); |
| 7476 | Py_XDECREF(exc); |
| 7477 | return ret; |
| 7478 | } |
| 7479 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7480 | static PyObject * |
| 7481 | encode_code_page(int code_page, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7482 | PyObject *unicode, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7483 | const char *errors) |
| 7484 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7485 | Py_ssize_t len; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7486 | PyObject *outbytes = NULL; |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7487 | Py_ssize_t offset; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7488 | int chunk_len, ret, done; |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7489 | |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7490 | if (PyUnicode_READY(unicode) < 0) |
| 7491 | return NULL; |
| 7492 | len = PyUnicode_GET_LENGTH(unicode); |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 7493 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7494 | if (code_page < 0) { |
| 7495 | PyErr_SetString(PyExc_ValueError, "invalid code page number"); |
| 7496 | return NULL; |
| 7497 | } |
| 7498 | |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7499 | if (len == 0) |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7500 | return PyBytes_FromStringAndSize(NULL, 0); |
| 7501 | |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7502 | offset = 0; |
| 7503 | do |
| 7504 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7505 | #ifdef NEED_RETRY |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7506 | /* 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] | 7507 | chunks. */ |
| 7508 | if (len > INT_MAX/2) { |
| 7509 | chunk_len = INT_MAX/2; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7510 | done = 0; |
| 7511 | } |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7512 | else |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7513 | #endif |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7514 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7515 | chunk_len = (int)len; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7516 | done = 1; |
| 7517 | } |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7518 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7519 | ret = encode_code_page_strict(code_page, &outbytes, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7520 | unicode, offset, chunk_len, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7521 | errors); |
| 7522 | if (ret == -2) |
| 7523 | ret = encode_code_page_errors(code_page, &outbytes, |
| 7524 | unicode, offset, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7525 | chunk_len, errors); |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7526 | if (ret < 0) { |
| 7527 | Py_XDECREF(outbytes); |
| 7528 | return NULL; |
| 7529 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7530 | |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7531 | offset += chunk_len; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7532 | len -= chunk_len; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7533 | } while (!done); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7534 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7535 | return outbytes; |
| 7536 | } |
| 7537 | |
| 7538 | PyObject * |
| 7539 | PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
| 7540 | Py_ssize_t size, |
| 7541 | const char *errors) |
| 7542 | { |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7543 | PyObject *unicode, *res; |
| 7544 | unicode = PyUnicode_FromUnicode(p, size); |
| 7545 | if (unicode == NULL) |
| 7546 | return NULL; |
| 7547 | res = encode_code_page(CP_ACP, unicode, errors); |
| 7548 | Py_DECREF(unicode); |
| 7549 | return res; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7550 | } |
| 7551 | |
| 7552 | PyObject * |
| 7553 | PyUnicode_EncodeCodePage(int code_page, |
| 7554 | PyObject *unicode, |
| 7555 | const char *errors) |
| 7556 | { |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7557 | return encode_code_page(code_page, unicode, errors); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7558 | } |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 7559 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7560 | PyObject * |
| 7561 | PyUnicode_AsMBCSString(PyObject *unicode) |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 7562 | { |
| 7563 | if (!PyUnicode_Check(unicode)) { |
| 7564 | PyErr_BadArgument(); |
| 7565 | return NULL; |
| 7566 | } |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7567 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, NULL); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 7568 | } |
| 7569 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7570 | #undef NEED_RETRY |
| 7571 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 7572 | #endif /* HAVE_MBCS */ |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7573 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7574 | /* --- Character Mapping Codec -------------------------------------------- */ |
| 7575 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7576 | PyObject * |
| 7577 | PyUnicode_DecodeCharmap(const char *s, |
| 7578 | Py_ssize_t size, |
| 7579 | PyObject *mapping, |
| 7580 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7581 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7582 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7583 | Py_ssize_t startinpos; |
| 7584 | Py_ssize_t endinpos; |
| 7585 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7586 | const char *e; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 7587 | PyObject *v; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7588 | Py_ssize_t extrachars = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7589 | PyObject *errorHandler = NULL; |
| 7590 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7591 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7592 | /* Default to Latin-1 */ |
| 7593 | if (mapping == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7594 | return PyUnicode_DecodeLatin1(s, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7595 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7596 | v = PyUnicode_New(size, 127); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7597 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7598 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7599 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 7600 | return v; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7601 | outpos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7602 | e = s + size; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7603 | if (PyUnicode_CheckExact(mapping)) { |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7604 | Py_ssize_t maplen; |
| 7605 | enum PyUnicode_Kind kind; |
| 7606 | void *data; |
| 7607 | Py_UCS4 x; |
| 7608 | |
| 7609 | if (PyUnicode_READY(mapping) < 0) |
| 7610 | return NULL; |
| 7611 | |
| 7612 | maplen = PyUnicode_GET_LENGTH(mapping); |
| 7613 | data = PyUnicode_DATA(mapping); |
| 7614 | kind = PyUnicode_KIND(mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7615 | while (s < e) { |
| 7616 | unsigned char ch = *s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7617 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7618 | if (ch < maplen) |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7619 | x = PyUnicode_READ(kind, data, ch); |
| 7620 | else |
| 7621 | x = 0xfffe; /* invalid value */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7622 | |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7623 | if (x == 0xfffe) |
| 7624 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7625 | /* undefined mapping */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7626 | startinpos = s-starts; |
| 7627 | endinpos = startinpos+1; |
| 7628 | if (unicode_decode_call_errorhandler( |
| 7629 | errors, &errorHandler, |
| 7630 | "charmap", "character maps to <undefined>", |
| 7631 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7632 | &v, &outpos)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7633 | goto onError; |
| 7634 | } |
| 7635 | continue; |
| 7636 | } |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7637 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7638 | if (unicode_putchar(&v, &outpos, x) < 0) |
| 7639 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7640 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7641 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7642 | } |
| 7643 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7644 | while (s < e) { |
| 7645 | unsigned char ch = *s; |
| 7646 | PyObject *w, *x; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7647 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7648 | /* Get mapping (char ordinal -> integer, Unicode char or None) */ |
| 7649 | w = PyLong_FromLong((long)ch); |
| 7650 | if (w == NULL) |
| 7651 | goto onError; |
| 7652 | x = PyObject_GetItem(mapping, w); |
| 7653 | Py_DECREF(w); |
| 7654 | if (x == NULL) { |
| 7655 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 7656 | /* No mapping found means: mapping is undefined. */ |
| 7657 | PyErr_Clear(); |
| 7658 | x = Py_None; |
| 7659 | Py_INCREF(x); |
| 7660 | } else |
| 7661 | goto onError; |
| 7662 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7663 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7664 | /* Apply mapping */ |
| 7665 | if (PyLong_Check(x)) { |
| 7666 | long value = PyLong_AS_LONG(x); |
| 7667 | if (value < 0 || value > 65535) { |
| 7668 | PyErr_SetString(PyExc_TypeError, |
| 7669 | "character mapping must be in range(65536)"); |
| 7670 | Py_DECREF(x); |
| 7671 | goto onError; |
| 7672 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7673 | if (unicode_putchar(&v, &outpos, value) < 0) |
| 7674 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7675 | } |
| 7676 | else if (x == Py_None) { |
| 7677 | /* undefined mapping */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7678 | startinpos = s-starts; |
| 7679 | endinpos = startinpos+1; |
| 7680 | if (unicode_decode_call_errorhandler( |
| 7681 | errors, &errorHandler, |
| 7682 | "charmap", "character maps to <undefined>", |
| 7683 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7684 | &v, &outpos)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7685 | Py_DECREF(x); |
| 7686 | goto onError; |
| 7687 | } |
| 7688 | Py_DECREF(x); |
| 7689 | continue; |
| 7690 | } |
| 7691 | else if (PyUnicode_Check(x)) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7692 | Py_ssize_t targetsize; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7693 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7694 | if (PyUnicode_READY(x) < 0) |
| 7695 | goto onError; |
| 7696 | targetsize = PyUnicode_GET_LENGTH(x); |
| 7697 | |
| 7698 | if (targetsize == 1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7699 | /* 1-1 mapping */ |
Victor Stinner | 62aa4d0 | 2011-11-09 00:03:45 +0100 | [diff] [blame] | 7700 | if (unicode_putchar(&v, &outpos, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7701 | PyUnicode_READ_CHAR(x, 0)) < 0) |
| 7702 | goto onError; |
| 7703 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7704 | else if (targetsize > 1) { |
| 7705 | /* 1-n mapping */ |
| 7706 | if (targetsize > extrachars) { |
| 7707 | /* resize first */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7708 | Py_ssize_t needed = (targetsize - extrachars) + \ |
| 7709 | (targetsize << 2); |
| 7710 | extrachars += needed; |
| 7711 | /* XXX overflow detection missing */ |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 7712 | if (PyUnicode_Resize(&v, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7713 | PyUnicode_GET_LENGTH(v) + needed) < 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7714 | Py_DECREF(x); |
| 7715 | goto onError; |
| 7716 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7717 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7718 | if (unicode_widen(&v, PyUnicode_MAX_CHAR_VALUE(x)) < 0) |
| 7719 | goto onError; |
| 7720 | PyUnicode_CopyCharacters(v, outpos, x, 0, targetsize); |
| 7721 | outpos += targetsize; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7722 | extrachars -= targetsize; |
| 7723 | } |
| 7724 | /* 1-0 mapping: skip the character */ |
| 7725 | } |
| 7726 | else { |
| 7727 | /* wrong return value */ |
| 7728 | PyErr_SetString(PyExc_TypeError, |
| 7729 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7730 | Py_DECREF(x); |
| 7731 | goto onError; |
| 7732 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7733 | Py_DECREF(x); |
| 7734 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7735 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7736 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7737 | if (PyUnicode_Resize(&v, outpos) < 0) |
Antoine Pitrou | a8f63c0 | 2011-11-08 18:37:16 +0100 | [diff] [blame] | 7738 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7739 | Py_XDECREF(errorHandler); |
| 7740 | Py_XDECREF(exc); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 7741 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 7742 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7743 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7744 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7745 | Py_XDECREF(errorHandler); |
| 7746 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7747 | Py_XDECREF(v); |
| 7748 | return NULL; |
| 7749 | } |
| 7750 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7751 | /* Charmap encoding: the lookup table */ |
| 7752 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7753 | struct encoding_map { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7754 | PyObject_HEAD |
| 7755 | unsigned char level1[32]; |
| 7756 | int count2, count3; |
| 7757 | unsigned char level23[1]; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7758 | }; |
| 7759 | |
| 7760 | static PyObject* |
| 7761 | encoding_map_size(PyObject *obj, PyObject* args) |
| 7762 | { |
| 7763 | struct encoding_map *map = (struct encoding_map*)obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7764 | return PyLong_FromLong(sizeof(*map) - 1 + 16*map->count2 + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7765 | 128*map->count3); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7766 | } |
| 7767 | |
| 7768 | static PyMethodDef encoding_map_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7769 | {"size", encoding_map_size, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7770 | PyDoc_STR("Return the size (in bytes) of this object") }, |
| 7771 | { 0 } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7772 | }; |
| 7773 | |
| 7774 | static void |
| 7775 | encoding_map_dealloc(PyObject* o) |
| 7776 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7777 | PyObject_FREE(o); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7778 | } |
| 7779 | |
| 7780 | static PyTypeObject EncodingMapType = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7781 | PyVarObject_HEAD_INIT(NULL, 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7782 | "EncodingMap", /*tp_name*/ |
| 7783 | sizeof(struct encoding_map), /*tp_basicsize*/ |
| 7784 | 0, /*tp_itemsize*/ |
| 7785 | /* methods */ |
| 7786 | encoding_map_dealloc, /*tp_dealloc*/ |
| 7787 | 0, /*tp_print*/ |
| 7788 | 0, /*tp_getattr*/ |
| 7789 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 7790 | 0, /*tp_reserved*/ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7791 | 0, /*tp_repr*/ |
| 7792 | 0, /*tp_as_number*/ |
| 7793 | 0, /*tp_as_sequence*/ |
| 7794 | 0, /*tp_as_mapping*/ |
| 7795 | 0, /*tp_hash*/ |
| 7796 | 0, /*tp_call*/ |
| 7797 | 0, /*tp_str*/ |
| 7798 | 0, /*tp_getattro*/ |
| 7799 | 0, /*tp_setattro*/ |
| 7800 | 0, /*tp_as_buffer*/ |
| 7801 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 7802 | 0, /*tp_doc*/ |
| 7803 | 0, /*tp_traverse*/ |
| 7804 | 0, /*tp_clear*/ |
| 7805 | 0, /*tp_richcompare*/ |
| 7806 | 0, /*tp_weaklistoffset*/ |
| 7807 | 0, /*tp_iter*/ |
| 7808 | 0, /*tp_iternext*/ |
| 7809 | encoding_map_methods, /*tp_methods*/ |
| 7810 | 0, /*tp_members*/ |
| 7811 | 0, /*tp_getset*/ |
| 7812 | 0, /*tp_base*/ |
| 7813 | 0, /*tp_dict*/ |
| 7814 | 0, /*tp_descr_get*/ |
| 7815 | 0, /*tp_descr_set*/ |
| 7816 | 0, /*tp_dictoffset*/ |
| 7817 | 0, /*tp_init*/ |
| 7818 | 0, /*tp_alloc*/ |
| 7819 | 0, /*tp_new*/ |
| 7820 | 0, /*tp_free*/ |
| 7821 | 0, /*tp_is_gc*/ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7822 | }; |
| 7823 | |
| 7824 | PyObject* |
| 7825 | PyUnicode_BuildEncodingMap(PyObject* string) |
| 7826 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7827 | PyObject *result; |
| 7828 | struct encoding_map *mresult; |
| 7829 | int i; |
| 7830 | int need_dict = 0; |
| 7831 | unsigned char level1[32]; |
| 7832 | unsigned char level2[512]; |
| 7833 | unsigned char *mlevel1, *mlevel2, *mlevel3; |
| 7834 | int count2 = 0, count3 = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7835 | int kind; |
| 7836 | void *data; |
| 7837 | Py_UCS4 ch; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7838 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7839 | if (!PyUnicode_Check(string) || PyUnicode_GET_LENGTH(string) != 256) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7840 | PyErr_BadArgument(); |
| 7841 | return NULL; |
| 7842 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7843 | kind = PyUnicode_KIND(string); |
| 7844 | data = PyUnicode_DATA(string); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7845 | memset(level1, 0xFF, sizeof level1); |
| 7846 | memset(level2, 0xFF, sizeof level2); |
| 7847 | |
| 7848 | /* If there isn't a one-to-one mapping of NULL to \0, |
| 7849 | or if there are non-BMP characters, we need to use |
| 7850 | a mapping dictionary. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7851 | if (PyUnicode_READ(kind, data, 0) != 0) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7852 | need_dict = 1; |
| 7853 | for (i = 1; i < 256; i++) { |
| 7854 | int l1, l2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7855 | ch = PyUnicode_READ(kind, data, i); |
| 7856 | if (ch == 0 || ch > 0xFFFF) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7857 | need_dict = 1; |
| 7858 | break; |
| 7859 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7860 | if (ch == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7861 | /* unmapped character */ |
| 7862 | continue; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7863 | l1 = ch >> 11; |
| 7864 | l2 = ch >> 7; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7865 | if (level1[l1] == 0xFF) |
| 7866 | level1[l1] = count2++; |
| 7867 | if (level2[l2] == 0xFF) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7868 | level2[l2] = count3++; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7869 | } |
| 7870 | |
| 7871 | if (count2 >= 0xFF || count3 >= 0xFF) |
| 7872 | need_dict = 1; |
| 7873 | |
| 7874 | if (need_dict) { |
| 7875 | PyObject *result = PyDict_New(); |
| 7876 | PyObject *key, *value; |
| 7877 | if (!result) |
| 7878 | return NULL; |
| 7879 | for (i = 0; i < 256; i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7880 | key = PyLong_FromLong(PyUnicode_READ(kind, data, i)); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7881 | value = PyLong_FromLong(i); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7882 | if (!key || !value) |
| 7883 | goto failed1; |
| 7884 | if (PyDict_SetItem(result, key, value) == -1) |
| 7885 | goto failed1; |
| 7886 | Py_DECREF(key); |
| 7887 | Py_DECREF(value); |
| 7888 | } |
| 7889 | return result; |
| 7890 | failed1: |
| 7891 | Py_XDECREF(key); |
| 7892 | Py_XDECREF(value); |
| 7893 | Py_DECREF(result); |
| 7894 | return NULL; |
| 7895 | } |
| 7896 | |
| 7897 | /* Create a three-level trie */ |
| 7898 | result = PyObject_MALLOC(sizeof(struct encoding_map) + |
| 7899 | 16*count2 + 128*count3 - 1); |
| 7900 | if (!result) |
| 7901 | return PyErr_NoMemory(); |
| 7902 | PyObject_Init(result, &EncodingMapType); |
| 7903 | mresult = (struct encoding_map*)result; |
| 7904 | mresult->count2 = count2; |
| 7905 | mresult->count3 = count3; |
| 7906 | mlevel1 = mresult->level1; |
| 7907 | mlevel2 = mresult->level23; |
| 7908 | mlevel3 = mresult->level23 + 16*count2; |
| 7909 | memcpy(mlevel1, level1, 32); |
| 7910 | memset(mlevel2, 0xFF, 16*count2); |
| 7911 | memset(mlevel3, 0, 128*count3); |
| 7912 | count3 = 0; |
| 7913 | for (i = 1; i < 256; i++) { |
| 7914 | int o1, o2, o3, i2, i3; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7915 | if (PyUnicode_READ(kind, data, i) == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7916 | /* unmapped character */ |
| 7917 | continue; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7918 | o1 = PyUnicode_READ(kind, data, i)>>11; |
| 7919 | o2 = (PyUnicode_READ(kind, data, i)>>7) & 0xF; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7920 | i2 = 16*mlevel1[o1] + o2; |
| 7921 | if (mlevel2[i2] == 0xFF) |
| 7922 | mlevel2[i2] = count3++; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7923 | o3 = PyUnicode_READ(kind, data, i) & 0x7F; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7924 | i3 = 128*mlevel2[i2] + o3; |
| 7925 | mlevel3[i3] = i; |
| 7926 | } |
| 7927 | return result; |
| 7928 | } |
| 7929 | |
| 7930 | static int |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 7931 | encoding_map_lookup(Py_UCS4 c, PyObject *mapping) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7932 | { |
| 7933 | struct encoding_map *map = (struct encoding_map*)mapping; |
| 7934 | int l1 = c>>11; |
| 7935 | int l2 = (c>>7) & 0xF; |
| 7936 | int l3 = c & 0x7F; |
| 7937 | int i; |
| 7938 | |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 7939 | if (c > 0xFFFF) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7940 | return -1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7941 | if (c == 0) |
| 7942 | return 0; |
| 7943 | /* level 1*/ |
| 7944 | i = map->level1[l1]; |
| 7945 | if (i == 0xFF) { |
| 7946 | return -1; |
| 7947 | } |
| 7948 | /* level 2*/ |
| 7949 | i = map->level23[16*i+l2]; |
| 7950 | if (i == 0xFF) { |
| 7951 | return -1; |
| 7952 | } |
| 7953 | /* level 3 */ |
| 7954 | i = map->level23[16*map->count2 + 128*i + l3]; |
| 7955 | if (i == 0) { |
| 7956 | return -1; |
| 7957 | } |
| 7958 | return i; |
| 7959 | } |
| 7960 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7961 | /* Lookup the character ch in the mapping. If the character |
| 7962 | can't be found, Py_None is returned (or NULL, if another |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 7963 | error occurred). */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7964 | static PyObject * |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 7965 | charmapencode_lookup(Py_UCS4 c, PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7966 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7967 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7968 | PyObject *x; |
| 7969 | |
| 7970 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7971 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7972 | x = PyObject_GetItem(mapping, w); |
| 7973 | Py_DECREF(w); |
| 7974 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7975 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 7976 | /* No mapping found means: mapping is undefined. */ |
| 7977 | PyErr_Clear(); |
| 7978 | x = Py_None; |
| 7979 | Py_INCREF(x); |
| 7980 | return x; |
| 7981 | } else |
| 7982 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7983 | } |
Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 7984 | else if (x == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7985 | return x; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7986 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7987 | long value = PyLong_AS_LONG(x); |
| 7988 | if (value < 0 || value > 255) { |
| 7989 | PyErr_SetString(PyExc_TypeError, |
| 7990 | "character mapping must be in range(256)"); |
| 7991 | Py_DECREF(x); |
| 7992 | return NULL; |
| 7993 | } |
| 7994 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7995 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7996 | else if (PyBytes_Check(x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7997 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7998 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7999 | /* wrong return value */ |
| 8000 | PyErr_Format(PyExc_TypeError, |
| 8001 | "character mapping must return integer, bytes or None, not %.400s", |
| 8002 | x->ob_type->tp_name); |
| 8003 | Py_DECREF(x); |
| 8004 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8005 | } |
| 8006 | } |
| 8007 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8008 | static int |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 8009 | charmapencode_resize(PyObject **outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8010 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8011 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
| 8012 | /* exponentially overallocate to minimize reallocations */ |
| 8013 | if (requiredsize < 2*outsize) |
| 8014 | requiredsize = 2*outsize; |
| 8015 | if (_PyBytes_Resize(outobj, requiredsize)) |
| 8016 | return -1; |
| 8017 | return 0; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8018 | } |
| 8019 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8020 | typedef enum charmapencode_result { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8021 | enc_SUCCESS, enc_FAILED, enc_EXCEPTION |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8022 | } charmapencode_result; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8023 | /* 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] | 8024 | various state variables. Resize the output bytes object if not enough |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8025 | space is available. Return a new reference to the object that |
| 8026 | was put in the output buffer, or Py_None, if the mapping was undefined |
| 8027 | (in which case no character was written) or NULL, if a |
Andrew M. Kuchling | 8294de5 | 2005-11-02 16:36:12 +0000 | [diff] [blame] | 8028 | reallocation error occurred. The caller must decref the result */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8029 | static charmapencode_result |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 8030 | charmapencode_output(Py_UCS4 c, PyObject *mapping, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8031 | PyObject **outobj, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8032 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8033 | PyObject *rep; |
| 8034 | char *outstart; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8035 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8036 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 8037 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8038 | int res = encoding_map_lookup(c, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8039 | Py_ssize_t requiredsize = *outpos+1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8040 | if (res == -1) |
| 8041 | return enc_FAILED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8042 | if (outsize<requiredsize) |
| 8043 | if (charmapencode_resize(outobj, outpos, requiredsize)) |
| 8044 | return enc_EXCEPTION; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8045 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8046 | outstart[(*outpos)++] = (char)res; |
| 8047 | return enc_SUCCESS; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8048 | } |
| 8049 | |
| 8050 | rep = charmapencode_lookup(c, mapping); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8051 | if (rep==NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8052 | return enc_EXCEPTION; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8053 | else if (rep==Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8054 | Py_DECREF(rep); |
| 8055 | return enc_FAILED; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8056 | } else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8057 | if (PyLong_Check(rep)) { |
| 8058 | Py_ssize_t requiredsize = *outpos+1; |
| 8059 | if (outsize<requiredsize) |
| 8060 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 8061 | Py_DECREF(rep); |
| 8062 | return enc_EXCEPTION; |
| 8063 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8064 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8065 | outstart[(*outpos)++] = (char)PyLong_AS_LONG(rep); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8066 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8067 | else { |
| 8068 | const char *repchars = PyBytes_AS_STRING(rep); |
| 8069 | Py_ssize_t repsize = PyBytes_GET_SIZE(rep); |
| 8070 | Py_ssize_t requiredsize = *outpos+repsize; |
| 8071 | if (outsize<requiredsize) |
| 8072 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 8073 | Py_DECREF(rep); |
| 8074 | return enc_EXCEPTION; |
| 8075 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8076 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8077 | memcpy(outstart + *outpos, repchars, repsize); |
| 8078 | *outpos += repsize; |
| 8079 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8080 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8081 | Py_DECREF(rep); |
| 8082 | return enc_SUCCESS; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8083 | } |
| 8084 | |
| 8085 | /* handle an error in PyUnicode_EncodeCharmap |
| 8086 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8087 | static int |
| 8088 | charmap_encoding_error( |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8089 | PyObject *unicode, Py_ssize_t *inpos, PyObject *mapping, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8090 | PyObject **exceptionObject, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 8091 | int *known_errorHandler, PyObject **errorHandler, const char *errors, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 8092 | PyObject **res, Py_ssize_t *respos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8093 | { |
| 8094 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8095 | Py_ssize_t size, repsize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8096 | Py_ssize_t newpos; |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8097 | enum PyUnicode_Kind kind; |
| 8098 | void *data; |
| 8099 | Py_ssize_t index; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8100 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8101 | Py_ssize_t collstartpos = *inpos; |
| 8102 | Py_ssize_t collendpos = *inpos+1; |
| 8103 | Py_ssize_t collpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8104 | char *encoding = "charmap"; |
| 8105 | char *reason = "character maps to <undefined>"; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8106 | charmapencode_result x; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8107 | Py_UCS4 ch; |
Brian Curtin | 2787ea4 | 2011-11-02 15:09:37 -0500 | [diff] [blame] | 8108 | int val; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8109 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8110 | if (PyUnicode_READY(unicode) < 0) |
| 8111 | return -1; |
| 8112 | size = PyUnicode_GET_LENGTH(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8113 | /* find all unencodable characters */ |
| 8114 | while (collendpos < size) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8115 | PyObject *rep; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 8116 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8117 | ch = PyUnicode_READ_CHAR(unicode, collendpos); |
Brian Curtin | 2787ea4 | 2011-11-02 15:09:37 -0500 | [diff] [blame] | 8118 | val = encoding_map_lookup(ch, mapping); |
| 8119 | if (val != -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8120 | break; |
| 8121 | ++collendpos; |
| 8122 | continue; |
| 8123 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8124 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8125 | ch = PyUnicode_READ_CHAR(unicode, collendpos); |
| 8126 | rep = charmapencode_lookup(ch, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8127 | if (rep==NULL) |
| 8128 | return -1; |
| 8129 | else if (rep!=Py_None) { |
| 8130 | Py_DECREF(rep); |
| 8131 | break; |
| 8132 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8133 | Py_DECREF(rep); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8134 | ++collendpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8135 | } |
| 8136 | /* cache callback name lookup |
| 8137 | * (if not done yet, i.e. it's the first error) */ |
| 8138 | if (*known_errorHandler==-1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8139 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 8140 | *known_errorHandler = 1; |
| 8141 | else if (!strcmp(errors, "replace")) |
| 8142 | *known_errorHandler = 2; |
| 8143 | else if (!strcmp(errors, "ignore")) |
| 8144 | *known_errorHandler = 3; |
| 8145 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 8146 | *known_errorHandler = 4; |
| 8147 | else |
| 8148 | *known_errorHandler = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8149 | } |
| 8150 | switch (*known_errorHandler) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8151 | case 1: /* strict */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8152 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8153 | return -1; |
| 8154 | case 2: /* replace */ |
| 8155 | for (collpos = collstartpos; collpos<collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8156 | x = charmapencode_output('?', mapping, res, respos); |
| 8157 | if (x==enc_EXCEPTION) { |
| 8158 | return -1; |
| 8159 | } |
| 8160 | else if (x==enc_FAILED) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8161 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8162 | return -1; |
| 8163 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8164 | } |
| 8165 | /* fall through */ |
| 8166 | case 3: /* ignore */ |
| 8167 | *inpos = collendpos; |
| 8168 | break; |
| 8169 | case 4: /* xmlcharrefreplace */ |
| 8170 | /* generate replacement (temporarily (mis)uses p) */ |
| 8171 | for (collpos = collstartpos; collpos < collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8172 | char buffer[2+29+1+1]; |
| 8173 | char *cp; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8174 | sprintf(buffer, "&#%d;", (int)PyUnicode_READ_CHAR(unicode, collpos)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8175 | for (cp = buffer; *cp; ++cp) { |
| 8176 | x = charmapencode_output(*cp, mapping, res, respos); |
| 8177 | if (x==enc_EXCEPTION) |
| 8178 | return -1; |
| 8179 | else if (x==enc_FAILED) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8180 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8181 | return -1; |
| 8182 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8183 | } |
| 8184 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8185 | *inpos = collendpos; |
| 8186 | break; |
| 8187 | default: |
| 8188 | repunicode = unicode_encode_call_errorhandler(errors, errorHandler, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8189 | encoding, reason, unicode, exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8190 | collstartpos, collendpos, &newpos); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8191 | if (repunicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8192 | return -1; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8193 | if (PyBytes_Check(repunicode)) { |
| 8194 | /* Directly copy bytes result to output. */ |
| 8195 | Py_ssize_t outsize = PyBytes_Size(*res); |
| 8196 | Py_ssize_t requiredsize; |
| 8197 | repsize = PyBytes_Size(repunicode); |
| 8198 | requiredsize = *respos + repsize; |
| 8199 | if (requiredsize > outsize) |
| 8200 | /* Make room for all additional bytes. */ |
| 8201 | if (charmapencode_resize(res, respos, requiredsize)) { |
| 8202 | Py_DECREF(repunicode); |
| 8203 | return -1; |
| 8204 | } |
| 8205 | memcpy(PyBytes_AsString(*res) + *respos, |
| 8206 | PyBytes_AsString(repunicode), repsize); |
| 8207 | *respos += repsize; |
| 8208 | *inpos = newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 8209 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8210 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 8211 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8212 | /* generate replacement */ |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8213 | if (PyUnicode_READY(repunicode) < 0) { |
| 8214 | Py_DECREF(repunicode); |
| 8215 | return -1; |
| 8216 | } |
Victor Stinner | 9e30aa5 | 2011-11-21 02:49:52 +0100 | [diff] [blame] | 8217 | repsize = PyUnicode_GET_LENGTH(repunicode); |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8218 | data = PyUnicode_DATA(repunicode); |
| 8219 | kind = PyUnicode_KIND(repunicode); |
| 8220 | for (index = 0; index < repsize; index++) { |
| 8221 | Py_UCS4 repch = PyUnicode_READ(kind, data, index); |
| 8222 | x = charmapencode_output(repch, mapping, res, respos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8223 | if (x==enc_EXCEPTION) { |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8224 | Py_DECREF(repunicode); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8225 | return -1; |
| 8226 | } |
| 8227 | else if (x==enc_FAILED) { |
| 8228 | Py_DECREF(repunicode); |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8229 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8230 | return -1; |
| 8231 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8232 | } |
| 8233 | *inpos = newpos; |
| 8234 | Py_DECREF(repunicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8235 | } |
| 8236 | return 0; |
| 8237 | } |
| 8238 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8239 | PyObject * |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8240 | _PyUnicode_EncodeCharmap(PyObject *unicode, |
| 8241 | PyObject *mapping, |
| 8242 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8243 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8244 | /* output object */ |
| 8245 | PyObject *res = NULL; |
| 8246 | /* current input position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8247 | Py_ssize_t inpos = 0; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8248 | Py_ssize_t size; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8249 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8250 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8251 | PyObject *errorHandler = NULL; |
| 8252 | PyObject *exc = NULL; |
| 8253 | /* the following variable is used for caching string comparisons |
| 8254 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 8255 | * 3=ignore, 4=xmlcharrefreplace */ |
| 8256 | int known_errorHandler = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8257 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8258 | if (PyUnicode_READY(unicode) < 0) |
| 8259 | return NULL; |
| 8260 | size = PyUnicode_GET_LENGTH(unicode); |
| 8261 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8262 | /* Default to Latin-1 */ |
| 8263 | if (mapping == NULL) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8264 | return unicode_encode_ucs1(unicode, errors, 256); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8265 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8266 | /* allocate enough for a simple encoding without |
| 8267 | replacements, if we need more, we'll resize */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8268 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8269 | if (res == NULL) |
| 8270 | goto onError; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 8271 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8272 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8273 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8274 | while (inpos<size) { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8275 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, inpos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8276 | /* try to encode it */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8277 | charmapencode_result x = charmapencode_output(ch, mapping, &res, &respos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8278 | if (x==enc_EXCEPTION) /* error */ |
| 8279 | goto onError; |
| 8280 | if (x==enc_FAILED) { /* unencodable character */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8281 | if (charmap_encoding_error(unicode, &inpos, mapping, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8282 | &exc, |
| 8283 | &known_errorHandler, &errorHandler, errors, |
| 8284 | &res, &respos)) { |
| 8285 | goto onError; |
| 8286 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8287 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8288 | else |
| 8289 | /* done with this character => adjust input position */ |
| 8290 | ++inpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8291 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8292 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8293 | /* Resize if we allocated to much */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8294 | if (respos<PyBytes_GET_SIZE(res)) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 8295 | if (_PyBytes_Resize(&res, respos) < 0) |
| 8296 | goto onError; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 8297 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8298 | Py_XDECREF(exc); |
| 8299 | Py_XDECREF(errorHandler); |
| 8300 | return res; |
| 8301 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8302 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8303 | Py_XDECREF(res); |
| 8304 | Py_XDECREF(exc); |
| 8305 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8306 | return NULL; |
| 8307 | } |
| 8308 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8309 | /* Deprecated */ |
| 8310 | PyObject * |
| 8311 | PyUnicode_EncodeCharmap(const Py_UNICODE *p, |
| 8312 | Py_ssize_t size, |
| 8313 | PyObject *mapping, |
| 8314 | const char *errors) |
| 8315 | { |
| 8316 | PyObject *result; |
| 8317 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 8318 | if (unicode == NULL) |
| 8319 | return NULL; |
| 8320 | result = _PyUnicode_EncodeCharmap(unicode, mapping, errors); |
| 8321 | Py_DECREF(unicode); |
Victor Stinner | fc026c9 | 2011-11-04 00:24:51 +0100 | [diff] [blame] | 8322 | return result; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8323 | } |
| 8324 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8325 | PyObject * |
| 8326 | PyUnicode_AsCharmapString(PyObject *unicode, |
| 8327 | PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8328 | { |
| 8329 | if (!PyUnicode_Check(unicode) || mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8330 | PyErr_BadArgument(); |
| 8331 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8332 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8333 | return _PyUnicode_EncodeCharmap(unicode, mapping, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8334 | } |
| 8335 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8336 | /* create or adjust a UnicodeTranslateError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8337 | static void |
| 8338 | make_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8339 | PyObject *unicode, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8340 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 8341 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8342 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8343 | if (*exceptionObject == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8344 | *exceptionObject = _PyUnicodeTranslateError_Create( |
| 8345 | unicode, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8346 | } |
| 8347 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8348 | if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
| 8349 | goto onError; |
| 8350 | if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) |
| 8351 | goto onError; |
| 8352 | if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) |
| 8353 | goto onError; |
| 8354 | return; |
| 8355 | onError: |
| 8356 | Py_DECREF(*exceptionObject); |
| 8357 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8358 | } |
| 8359 | } |
| 8360 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8361 | /* raises a UnicodeTranslateError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8362 | static void |
| 8363 | raise_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8364 | PyObject *unicode, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8365 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 8366 | const char *reason) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8367 | { |
| 8368 | make_translate_exception(exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8369 | unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8370 | if (*exceptionObject != NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8371 | PyCodec_StrictErrors(*exceptionObject); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8372 | } |
| 8373 | |
| 8374 | /* error handling callback helper: |
| 8375 | build arguments, call the callback and check the arguments, |
| 8376 | put the result into newpos and return the replacement string, which |
| 8377 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8378 | static PyObject * |
| 8379 | unicode_translate_call_errorhandler(const char *errors, |
| 8380 | PyObject **errorHandler, |
| 8381 | const char *reason, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8382 | PyObject *unicode, PyObject **exceptionObject, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8383 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 8384 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8385 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 8386 | 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] | 8387 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8388 | Py_ssize_t i_newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8389 | PyObject *restuple; |
| 8390 | PyObject *resunicode; |
| 8391 | |
| 8392 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8393 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8394 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8395 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8396 | } |
| 8397 | |
| 8398 | make_translate_exception(exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8399 | unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8400 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8401 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8402 | |
| 8403 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8404 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8405 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8406 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8407 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 8408 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8409 | Py_DECREF(restuple); |
| 8410 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8411 | } |
| 8412 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8413 | &resunicode, &i_newpos)) { |
| 8414 | Py_DECREF(restuple); |
| 8415 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8416 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8417 | if (i_newpos<0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8418 | *newpos = PyUnicode_GET_LENGTH(unicode)+i_newpos; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8419 | else |
| 8420 | *newpos = i_newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8421 | if (*newpos<0 || *newpos>PyUnicode_GET_LENGTH(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8422 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 8423 | Py_DECREF(restuple); |
| 8424 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 8425 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8426 | Py_INCREF(resunicode); |
| 8427 | Py_DECREF(restuple); |
| 8428 | return resunicode; |
| 8429 | } |
| 8430 | |
| 8431 | /* Lookup the character ch in the mapping and put the result in result, |
| 8432 | which must be decrefed by the caller. |
| 8433 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8434 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8435 | charmaptranslate_lookup(Py_UCS4 c, PyObject *mapping, PyObject **result) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8436 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8437 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8438 | PyObject *x; |
| 8439 | |
| 8440 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8441 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8442 | x = PyObject_GetItem(mapping, w); |
| 8443 | Py_DECREF(w); |
| 8444 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8445 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 8446 | /* No mapping found means: use 1:1 mapping. */ |
| 8447 | PyErr_Clear(); |
| 8448 | *result = NULL; |
| 8449 | return 0; |
| 8450 | } else |
| 8451 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8452 | } |
| 8453 | else if (x == Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8454 | *result = x; |
| 8455 | return 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8456 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8457 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8458 | long value = PyLong_AS_LONG(x); |
| 8459 | long max = PyUnicode_GetMax(); |
| 8460 | if (value < 0 || value > max) { |
| 8461 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5a2f7e60 | 2007-10-24 21:13:09 +0000 | [diff] [blame] | 8462 | "character mapping must be in range(0x%x)", max+1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8463 | Py_DECREF(x); |
| 8464 | return -1; |
| 8465 | } |
| 8466 | *result = x; |
| 8467 | return 0; |
| 8468 | } |
| 8469 | else if (PyUnicode_Check(x)) { |
| 8470 | *result = x; |
| 8471 | return 0; |
| 8472 | } |
| 8473 | else { |
| 8474 | /* wrong return value */ |
| 8475 | PyErr_SetString(PyExc_TypeError, |
| 8476 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8477 | Py_DECREF(x); |
| 8478 | return -1; |
| 8479 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8480 | } |
| 8481 | /* ensure that *outobj is at least requiredsize characters long, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8482 | if not reallocate and adjust various state variables. |
| 8483 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8484 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8485 | charmaptranslate_makespace(Py_UCS4 **outobj, Py_ssize_t *psize, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8486 | Py_ssize_t requiredsize) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8487 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8488 | Py_ssize_t oldsize = *psize; |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 8489 | if (requiredsize > oldsize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8490 | /* exponentially overallocate to minimize reallocations */ |
| 8491 | if (requiredsize < 2 * oldsize) |
| 8492 | requiredsize = 2 * oldsize; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8493 | *outobj = PyMem_Realloc(*outobj, requiredsize * sizeof(Py_UCS4)); |
| 8494 | if (*outobj == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8495 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8496 | *psize = requiredsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8497 | } |
| 8498 | return 0; |
| 8499 | } |
| 8500 | /* lookup the character, put the result in the output string and adjust |
| 8501 | various state variables. Return a new reference to the object that |
| 8502 | was put in the output buffer in *result, or Py_None, if the mapping was |
| 8503 | undefined (in which case no character was written). |
| 8504 | The called must decref result. |
| 8505 | Return 0 on success, -1 on error. */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8506 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8507 | charmaptranslate_output(PyObject *input, Py_ssize_t ipos, |
| 8508 | PyObject *mapping, Py_UCS4 **output, |
| 8509 | Py_ssize_t *osize, Py_ssize_t *opos, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8510 | PyObject **res) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8511 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8512 | Py_UCS4 curinp = PyUnicode_READ_CHAR(input, ipos); |
| 8513 | if (charmaptranslate_lookup(curinp, mapping, res)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8514 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8515 | if (*res==NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8516 | /* not found => default to 1:1 mapping */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8517 | (*output)[(*opos)++] = curinp; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8518 | } |
| 8519 | else if (*res==Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8520 | ; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8521 | else if (PyLong_Check(*res)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8522 | /* 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] | 8523 | (*output)[(*opos)++] = (Py_UCS4)PyLong_AS_LONG(*res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8524 | } |
| 8525 | else if (PyUnicode_Check(*res)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8526 | Py_ssize_t repsize; |
| 8527 | if (PyUnicode_READY(*res) == -1) |
| 8528 | return -1; |
| 8529 | repsize = PyUnicode_GET_LENGTH(*res); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8530 | if (repsize==1) { |
| 8531 | /* 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] | 8532 | (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8533 | } |
| 8534 | else if (repsize!=0) { |
| 8535 | /* more than one character */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8536 | Py_ssize_t requiredsize = *opos + |
| 8537 | (PyUnicode_GET_LENGTH(input) - ipos) + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8538 | repsize - 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8539 | Py_ssize_t i; |
| 8540 | if (charmaptranslate_makespace(output, osize, requiredsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8541 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8542 | for(i = 0; i < repsize; i++) |
| 8543 | (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, i); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8544 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8545 | } |
| 8546 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8547 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8548 | return 0; |
| 8549 | } |
| 8550 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8551 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8552 | _PyUnicode_TranslateCharmap(PyObject *input, |
| 8553 | PyObject *mapping, |
| 8554 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8555 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8556 | /* input object */ |
| 8557 | char *idata; |
| 8558 | Py_ssize_t size, i; |
| 8559 | int kind; |
| 8560 | /* output buffer */ |
| 8561 | Py_UCS4 *output = NULL; |
| 8562 | Py_ssize_t osize; |
| 8563 | PyObject *res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8564 | /* current output position */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8565 | Py_ssize_t opos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8566 | char *reason = "character maps to <undefined>"; |
| 8567 | PyObject *errorHandler = NULL; |
| 8568 | PyObject *exc = NULL; |
| 8569 | /* the following variable is used for caching string comparisons |
| 8570 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 8571 | * 3=ignore, 4=xmlcharrefreplace */ |
| 8572 | int known_errorHandler = -1; |
| 8573 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8574 | if (mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8575 | PyErr_BadArgument(); |
| 8576 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8577 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8578 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8579 | if (PyUnicode_READY(input) == -1) |
| 8580 | return NULL; |
| 8581 | idata = (char*)PyUnicode_DATA(input); |
| 8582 | kind = PyUnicode_KIND(input); |
| 8583 | size = PyUnicode_GET_LENGTH(input); |
| 8584 | i = 0; |
| 8585 | |
| 8586 | if (size == 0) { |
| 8587 | Py_INCREF(input); |
| 8588 | return input; |
| 8589 | } |
| 8590 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8591 | /* allocate enough for a simple 1:1 translation without |
| 8592 | replacements, if we need more, we'll resize */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8593 | osize = size; |
| 8594 | output = PyMem_Malloc(osize * sizeof(Py_UCS4)); |
| 8595 | opos = 0; |
| 8596 | if (output == NULL) { |
| 8597 | PyErr_NoMemory(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8598 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8599 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8600 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8601 | while (i<size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8602 | /* try to encode it */ |
| 8603 | PyObject *x = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8604 | if (charmaptranslate_output(input, i, mapping, |
| 8605 | &output, &osize, &opos, &x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8606 | Py_XDECREF(x); |
| 8607 | goto onError; |
| 8608 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8609 | Py_XDECREF(x); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8610 | if (x!=Py_None) /* it worked => adjust input pointer */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8611 | ++i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8612 | else { /* untranslatable character */ |
| 8613 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
| 8614 | Py_ssize_t repsize; |
| 8615 | Py_ssize_t newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8616 | Py_ssize_t uni2; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8617 | /* startpos for collecting untranslatable chars */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8618 | Py_ssize_t collstart = i; |
| 8619 | Py_ssize_t collend = i+1; |
| 8620 | Py_ssize_t coll; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8621 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8622 | /* find all untranslatable characters */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8623 | while (collend < size) { |
| 8624 | if (charmaptranslate_lookup(PyUnicode_READ(kind,idata, collend), mapping, &x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8625 | goto onError; |
| 8626 | Py_XDECREF(x); |
| 8627 | if (x!=Py_None) |
| 8628 | break; |
| 8629 | ++collend; |
| 8630 | } |
| 8631 | /* cache callback name lookup |
| 8632 | * (if not done yet, i.e. it's the first error) */ |
| 8633 | if (known_errorHandler==-1) { |
| 8634 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 8635 | known_errorHandler = 1; |
| 8636 | else if (!strcmp(errors, "replace")) |
| 8637 | known_errorHandler = 2; |
| 8638 | else if (!strcmp(errors, "ignore")) |
| 8639 | known_errorHandler = 3; |
| 8640 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 8641 | known_errorHandler = 4; |
| 8642 | else |
| 8643 | known_errorHandler = 0; |
| 8644 | } |
| 8645 | switch (known_errorHandler) { |
| 8646 | case 1: /* strict */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8647 | raise_translate_exception(&exc, input, collstart, |
| 8648 | collend, reason); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8649 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8650 | case 2: /* replace */ |
| 8651 | /* 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] | 8652 | for (coll = collstart; coll<collend; coll++) |
| 8653 | output[opos++] = '?'; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8654 | /* fall through */ |
| 8655 | case 3: /* ignore */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8656 | i = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8657 | break; |
| 8658 | case 4: /* xmlcharrefreplace */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8659 | /* generate replacement (temporarily (mis)uses i) */ |
| 8660 | for (i = collstart; i < collend; ++i) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8661 | char buffer[2+29+1+1]; |
| 8662 | char *cp; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8663 | sprintf(buffer, "&#%d;", PyUnicode_READ(kind, idata, i)); |
| 8664 | if (charmaptranslate_makespace(&output, &osize, |
| 8665 | opos+strlen(buffer)+(size-collend))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8666 | goto onError; |
| 8667 | for (cp = buffer; *cp; ++cp) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8668 | output[opos++] = *cp; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8669 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8670 | i = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8671 | break; |
| 8672 | default: |
| 8673 | repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8674 | reason, input, &exc, |
| 8675 | collstart, collend, &newpos); |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 8676 | if (repunicode == NULL || _PyUnicode_READY_REPLACE(&repunicode)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8677 | goto onError; |
| 8678 | /* generate replacement */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8679 | repsize = PyUnicode_GET_LENGTH(repunicode); |
| 8680 | if (charmaptranslate_makespace(&output, &osize, |
| 8681 | opos+repsize+(size-collend))) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8682 | Py_DECREF(repunicode); |
| 8683 | goto onError; |
| 8684 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8685 | for (uni2 = 0; repsize-->0; ++uni2) |
| 8686 | output[opos++] = PyUnicode_READ_CHAR(repunicode, uni2); |
| 8687 | i = newpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8688 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8689 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8690 | } |
| 8691 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8692 | res = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, output, opos); |
| 8693 | if (!res) |
| 8694 | goto onError; |
| 8695 | PyMem_Free(output); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8696 | Py_XDECREF(exc); |
| 8697 | Py_XDECREF(errorHandler); |
| 8698 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8699 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8700 | onError: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8701 | PyMem_Free(output); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8702 | Py_XDECREF(exc); |
| 8703 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8704 | return NULL; |
| 8705 | } |
| 8706 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8707 | /* Deprecated. Use PyUnicode_Translate instead. */ |
| 8708 | PyObject * |
| 8709 | PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
| 8710 | Py_ssize_t size, |
| 8711 | PyObject *mapping, |
| 8712 | const char *errors) |
| 8713 | { |
| 8714 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 8715 | if (!unicode) |
| 8716 | return NULL; |
| 8717 | return _PyUnicode_TranslateCharmap(unicode, mapping, errors); |
| 8718 | } |
| 8719 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8720 | PyObject * |
| 8721 | PyUnicode_Translate(PyObject *str, |
| 8722 | PyObject *mapping, |
| 8723 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8724 | { |
| 8725 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8726 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8727 | str = PyUnicode_FromObject(str); |
| 8728 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8729 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8730 | result = _PyUnicode_TranslateCharmap(str, mapping, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8731 | Py_DECREF(str); |
| 8732 | return result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8733 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8734 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8735 | Py_XDECREF(str); |
| 8736 | return NULL; |
| 8737 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8738 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8739 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8740 | fix_decimal_and_space_to_ascii(PyObject *self) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8741 | { |
| 8742 | /* No need to call PyUnicode_READY(self) because this function is only |
| 8743 | called as a callback from fixup() which does it already. */ |
| 8744 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 8745 | const int kind = PyUnicode_KIND(self); |
| 8746 | void *data = PyUnicode_DATA(self); |
| 8747 | Py_UCS4 maxchar = 0, ch, fixed; |
| 8748 | Py_ssize_t i; |
| 8749 | |
| 8750 | for (i = 0; i < len; ++i) { |
| 8751 | ch = PyUnicode_READ(kind, data, i); |
| 8752 | fixed = 0; |
| 8753 | if (ch > 127) { |
| 8754 | if (Py_UNICODE_ISSPACE(ch)) |
| 8755 | fixed = ' '; |
| 8756 | else { |
| 8757 | const int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8758 | if (decimal >= 0) |
| 8759 | fixed = '0' + decimal; |
| 8760 | } |
| 8761 | if (fixed != 0) { |
| 8762 | if (fixed > maxchar) |
| 8763 | maxchar = fixed; |
| 8764 | PyUnicode_WRITE(kind, data, i, fixed); |
| 8765 | } |
| 8766 | else if (ch > maxchar) |
| 8767 | maxchar = ch; |
| 8768 | } |
| 8769 | else if (ch > maxchar) |
| 8770 | maxchar = ch; |
| 8771 | } |
| 8772 | |
| 8773 | return maxchar; |
| 8774 | } |
| 8775 | |
| 8776 | PyObject * |
| 8777 | _PyUnicode_TransformDecimalAndSpaceToASCII(PyObject *unicode) |
| 8778 | { |
| 8779 | if (!PyUnicode_Check(unicode)) { |
| 8780 | PyErr_BadInternalCall(); |
| 8781 | return NULL; |
| 8782 | } |
| 8783 | if (PyUnicode_READY(unicode) == -1) |
| 8784 | return NULL; |
| 8785 | if (PyUnicode_MAX_CHAR_VALUE(unicode) <= 127) { |
| 8786 | /* If the string is already ASCII, just return the same string */ |
| 8787 | Py_INCREF(unicode); |
| 8788 | return unicode; |
| 8789 | } |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8790 | return fixup(unicode, fix_decimal_and_space_to_ascii); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8791 | } |
| 8792 | |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8793 | PyObject * |
| 8794 | PyUnicode_TransformDecimalToASCII(Py_UNICODE *s, |
| 8795 | Py_ssize_t length) |
| 8796 | { |
| 8797 | PyObject *result; |
| 8798 | Py_UNICODE *p; /* write pointer into result */ |
| 8799 | Py_ssize_t i; |
| 8800 | /* Copy to a new string */ |
| 8801 | result = (PyObject *)_PyUnicode_New(length); |
| 8802 | Py_UNICODE_COPY(PyUnicode_AS_UNICODE(result), s, length); |
| 8803 | if (result == NULL) |
| 8804 | return result; |
| 8805 | p = PyUnicode_AS_UNICODE(result); |
| 8806 | /* Iterate over code points */ |
| 8807 | for (i = 0; i < length; i++) { |
| 8808 | Py_UNICODE ch =s[i]; |
| 8809 | if (ch > 127) { |
| 8810 | int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8811 | if (decimal >= 0) |
| 8812 | p[i] = '0' + decimal; |
| 8813 | } |
| 8814 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 8815 | #ifndef DONT_MAKE_RESULT_READY |
| 8816 | if (_PyUnicode_READY_REPLACE(&result)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8817 | Py_DECREF(result); |
| 8818 | return NULL; |
| 8819 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 8820 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 8821 | assert(_PyUnicode_CheckConsistency(result, 1)); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8822 | return result; |
| 8823 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8824 | /* --- Decimal Encoder ---------------------------------------------------- */ |
| 8825 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8826 | int |
| 8827 | PyUnicode_EncodeDecimal(Py_UNICODE *s, |
| 8828 | Py_ssize_t length, |
| 8829 | char *output, |
| 8830 | const char *errors) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8831 | { |
| 8832 | Py_UNICODE *p, *end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8833 | PyObject *errorHandler = NULL; |
| 8834 | PyObject *exc = NULL; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8835 | PyObject *unicode; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8836 | const char *encoding = "decimal"; |
| 8837 | const char *reason = "invalid decimal Unicode string"; |
| 8838 | /* the following variable is used for caching string comparisons |
| 8839 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 8840 | int known_errorHandler = -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8841 | |
| 8842 | if (output == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8843 | PyErr_BadArgument(); |
| 8844 | return -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8845 | } |
| 8846 | |
| 8847 | p = s; |
| 8848 | end = s + length; |
| 8849 | while (p < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8850 | register Py_UNICODE ch = *p; |
| 8851 | int decimal; |
| 8852 | PyObject *repunicode; |
| 8853 | Py_ssize_t repsize; |
| 8854 | Py_ssize_t newpos; |
| 8855 | Py_UNICODE *uni2; |
| 8856 | Py_UNICODE *collstart; |
| 8857 | Py_UNICODE *collend; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8858 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8859 | if (Py_UNICODE_ISSPACE(ch)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8860 | *output++ = ' '; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8861 | ++p; |
| 8862 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8863 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8864 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 8865 | if (decimal >= 0) { |
| 8866 | *output++ = '0' + decimal; |
| 8867 | ++p; |
| 8868 | continue; |
| 8869 | } |
| 8870 | if (0 < ch && ch < 256) { |
| 8871 | *output++ = (char)ch; |
| 8872 | ++p; |
| 8873 | continue; |
| 8874 | } |
| 8875 | /* All other characters are considered unencodable */ |
| 8876 | collstart = p; |
| 8877 | collend = p+1; |
| 8878 | while (collend < end) { |
| 8879 | if ((0 < *collend && *collend < 256) || |
| 8880 | !Py_UNICODE_ISSPACE(*collend) || |
| 8881 | Py_UNICODE_TODECIMAL(*collend)) |
| 8882 | break; |
| 8883 | } |
| 8884 | /* cache callback name lookup |
| 8885 | * (if not done yet, i.e. it's the first error) */ |
| 8886 | if (known_errorHandler==-1) { |
| 8887 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 8888 | known_errorHandler = 1; |
| 8889 | else if (!strcmp(errors, "replace")) |
| 8890 | known_errorHandler = 2; |
| 8891 | else if (!strcmp(errors, "ignore")) |
| 8892 | known_errorHandler = 3; |
| 8893 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 8894 | known_errorHandler = 4; |
| 8895 | else |
| 8896 | known_errorHandler = 0; |
| 8897 | } |
| 8898 | switch (known_errorHandler) { |
| 8899 | case 1: /* strict */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8900 | unicode = PyUnicode_FromUnicode(s, length); |
| 8901 | if (unicode == NULL) |
| 8902 | goto onError; |
| 8903 | raise_encode_exception(&exc, encoding, unicode, collstart-s, collend-s, reason); |
| 8904 | Py_DECREF(unicode); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8905 | goto onError; |
| 8906 | case 2: /* replace */ |
| 8907 | for (p = collstart; p < collend; ++p) |
| 8908 | *output++ = '?'; |
| 8909 | /* fall through */ |
| 8910 | case 3: /* ignore */ |
| 8911 | p = collend; |
| 8912 | break; |
| 8913 | case 4: /* xmlcharrefreplace */ |
| 8914 | /* generate replacement (temporarily (mis)uses p) */ |
| 8915 | for (p = collstart; p < collend; ++p) |
| 8916 | output += sprintf(output, "&#%d;", (int)*p); |
| 8917 | p = collend; |
| 8918 | break; |
| 8919 | default: |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8920 | unicode = PyUnicode_FromUnicode(s, length); |
| 8921 | if (unicode == NULL) |
| 8922 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8923 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8924 | encoding, reason, unicode, &exc, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8925 | collstart-s, collend-s, &newpos); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8926 | Py_DECREF(unicode); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8927 | if (repunicode == NULL) |
| 8928 | goto onError; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 8929 | if (!PyUnicode_Check(repunicode)) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8930 | /* Byte results not supported, since they have no decimal property. */ |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 8931 | PyErr_SetString(PyExc_TypeError, "error handler should return unicode"); |
| 8932 | Py_DECREF(repunicode); |
| 8933 | goto onError; |
| 8934 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8935 | /* generate replacement */ |
| 8936 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 8937 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
| 8938 | Py_UNICODE ch = *uni2; |
| 8939 | if (Py_UNICODE_ISSPACE(ch)) |
| 8940 | *output++ = ' '; |
| 8941 | else { |
| 8942 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 8943 | if (decimal >= 0) |
| 8944 | *output++ = '0' + decimal; |
| 8945 | else if (0 < ch && ch < 256) |
| 8946 | *output++ = (char)ch; |
| 8947 | else { |
| 8948 | Py_DECREF(repunicode); |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8949 | unicode = PyUnicode_FromUnicode(s, length); |
| 8950 | if (unicode == NULL) |
| 8951 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8952 | raise_encode_exception(&exc, encoding, |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8953 | unicode, collstart-s, collend-s, reason); |
| 8954 | Py_DECREF(unicode); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8955 | goto onError; |
| 8956 | } |
| 8957 | } |
| 8958 | } |
| 8959 | p = s + newpos; |
| 8960 | Py_DECREF(repunicode); |
| 8961 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8962 | } |
| 8963 | /* 0-terminate the output string */ |
| 8964 | *output++ = '\0'; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8965 | Py_XDECREF(exc); |
| 8966 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8967 | return 0; |
| 8968 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8969 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8970 | Py_XDECREF(exc); |
| 8971 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8972 | return -1; |
| 8973 | } |
| 8974 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8975 | /* --- Helpers ------------------------------------------------------------ */ |
| 8976 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8977 | static Py_ssize_t |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 8978 | any_find_slice(int direction, PyObject* s1, PyObject* s2, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8979 | Py_ssize_t start, |
| 8980 | Py_ssize_t end) |
| 8981 | { |
| 8982 | int kind1, kind2, kind; |
| 8983 | void *buf1, *buf2; |
| 8984 | Py_ssize_t len1, len2, result; |
| 8985 | |
| 8986 | kind1 = PyUnicode_KIND(s1); |
| 8987 | kind2 = PyUnicode_KIND(s2); |
| 8988 | kind = kind1 > kind2 ? kind1 : kind2; |
| 8989 | buf1 = PyUnicode_DATA(s1); |
| 8990 | buf2 = PyUnicode_DATA(s2); |
| 8991 | if (kind1 != kind) |
| 8992 | buf1 = _PyUnicode_AsKind(s1, kind); |
| 8993 | if (!buf1) |
| 8994 | return -2; |
| 8995 | if (kind2 != kind) |
| 8996 | buf2 = _PyUnicode_AsKind(s2, kind); |
| 8997 | if (!buf2) { |
| 8998 | if (kind1 != kind) PyMem_Free(buf1); |
| 8999 | return -2; |
| 9000 | } |
| 9001 | len1 = PyUnicode_GET_LENGTH(s1); |
| 9002 | len2 = PyUnicode_GET_LENGTH(s2); |
| 9003 | |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 9004 | if (direction > 0) { |
| 9005 | switch(kind) { |
| 9006 | case PyUnicode_1BYTE_KIND: |
| 9007 | if (PyUnicode_IS_ASCII(s1) && PyUnicode_IS_ASCII(s2)) |
| 9008 | result = asciilib_find_slice(buf1, len1, buf2, len2, start, end); |
| 9009 | else |
| 9010 | result = ucs1lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 9011 | break; |
| 9012 | case PyUnicode_2BYTE_KIND: |
| 9013 | result = ucs2lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 9014 | break; |
| 9015 | case PyUnicode_4BYTE_KIND: |
| 9016 | result = ucs4lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 9017 | break; |
| 9018 | default: |
| 9019 | assert(0); result = -2; |
| 9020 | } |
| 9021 | } |
| 9022 | else { |
| 9023 | switch(kind) { |
| 9024 | case PyUnicode_1BYTE_KIND: |
| 9025 | if (PyUnicode_IS_ASCII(s1) && PyUnicode_IS_ASCII(s2)) |
| 9026 | result = asciilib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 9027 | else |
| 9028 | result = ucs1lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 9029 | break; |
| 9030 | case PyUnicode_2BYTE_KIND: |
| 9031 | result = ucs2lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 9032 | break; |
| 9033 | case PyUnicode_4BYTE_KIND: |
| 9034 | result = ucs4lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 9035 | break; |
| 9036 | default: |
| 9037 | assert(0); result = -2; |
| 9038 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9039 | } |
| 9040 | |
| 9041 | if (kind1 != kind) |
| 9042 | PyMem_Free(buf1); |
| 9043 | if (kind2 != kind) |
| 9044 | PyMem_Free(buf2); |
| 9045 | |
| 9046 | return result; |
| 9047 | } |
| 9048 | |
| 9049 | Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9050 | _PyUnicode_InsertThousandsGrouping(PyObject *unicode, int kind, void *data, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9051 | Py_ssize_t n_buffer, |
| 9052 | void *digits, Py_ssize_t n_digits, |
| 9053 | Py_ssize_t min_width, |
| 9054 | const char *grouping, |
| 9055 | const char *thousands_sep) |
| 9056 | { |
| 9057 | switch(kind) { |
| 9058 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9059 | if (unicode != NULL && PyUnicode_IS_ASCII(unicode)) |
| 9060 | return _PyUnicode_ascii_InsertThousandsGrouping( |
| 9061 | (Py_UCS1*)data, n_buffer, (Py_UCS1*)digits, n_digits, |
| 9062 | min_width, grouping, thousands_sep); |
| 9063 | else |
| 9064 | return _PyUnicode_ucs1_InsertThousandsGrouping( |
| 9065 | (Py_UCS1*)data, n_buffer, (Py_UCS1*)digits, n_digits, |
| 9066 | min_width, grouping, thousands_sep); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9067 | case PyUnicode_2BYTE_KIND: |
| 9068 | return _PyUnicode_ucs2_InsertThousandsGrouping( |
| 9069 | (Py_UCS2*)data, n_buffer, (Py_UCS2*)digits, n_digits, |
| 9070 | min_width, grouping, thousands_sep); |
| 9071 | case PyUnicode_4BYTE_KIND: |
| 9072 | return _PyUnicode_ucs4_InsertThousandsGrouping( |
| 9073 | (Py_UCS4*)data, n_buffer, (Py_UCS4*)digits, n_digits, |
| 9074 | min_width, grouping, thousands_sep); |
| 9075 | } |
| 9076 | assert(0); |
| 9077 | return -1; |
| 9078 | } |
| 9079 | |
| 9080 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9081 | /* helper macro to fixup start/end slice values */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9082 | #define ADJUST_INDICES(start, end, len) \ |
| 9083 | if (end > len) \ |
| 9084 | end = len; \ |
| 9085 | else if (end < 0) { \ |
| 9086 | end += len; \ |
| 9087 | if (end < 0) \ |
| 9088 | end = 0; \ |
| 9089 | } \ |
| 9090 | if (start < 0) { \ |
| 9091 | start += len; \ |
| 9092 | if (start < 0) \ |
| 9093 | start = 0; \ |
| 9094 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9095 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9096 | Py_ssize_t |
| 9097 | PyUnicode_Count(PyObject *str, |
| 9098 | PyObject *substr, |
| 9099 | Py_ssize_t start, |
| 9100 | Py_ssize_t end) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9101 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9102 | Py_ssize_t result; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9103 | PyObject* str_obj; |
| 9104 | PyObject* sub_obj; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9105 | int kind1, kind2, kind; |
| 9106 | void *buf1 = NULL, *buf2 = NULL; |
| 9107 | Py_ssize_t len1, len2; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9108 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9109 | str_obj = PyUnicode_FromObject(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9110 | if (!str_obj || PyUnicode_READY(str_obj) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9111 | return -1; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9112 | sub_obj = PyUnicode_FromObject(substr); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 9113 | if (!sub_obj || PyUnicode_READY(sub_obj) == -1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9114 | Py_DECREF(str_obj); |
| 9115 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9116 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9117 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9118 | kind1 = PyUnicode_KIND(str_obj); |
| 9119 | kind2 = PyUnicode_KIND(sub_obj); |
| 9120 | kind = kind1 > kind2 ? kind1 : kind2; |
| 9121 | buf1 = PyUnicode_DATA(str_obj); |
| 9122 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9123 | buf1 = _PyUnicode_AsKind(str_obj, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9124 | if (!buf1) |
| 9125 | goto onError; |
| 9126 | buf2 = PyUnicode_DATA(sub_obj); |
| 9127 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9128 | buf2 = _PyUnicode_AsKind(sub_obj, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9129 | if (!buf2) |
| 9130 | goto onError; |
| 9131 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 9132 | len2 = PyUnicode_GET_LENGTH(sub_obj); |
| 9133 | |
| 9134 | ADJUST_INDICES(start, end, len1); |
| 9135 | switch(kind) { |
| 9136 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9137 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sub_obj)) |
| 9138 | result = asciilib_count( |
| 9139 | ((Py_UCS1*)buf1) + start, end - start, |
| 9140 | buf2, len2, PY_SSIZE_T_MAX |
| 9141 | ); |
| 9142 | else |
| 9143 | result = ucs1lib_count( |
| 9144 | ((Py_UCS1*)buf1) + start, end - start, |
| 9145 | buf2, len2, PY_SSIZE_T_MAX |
| 9146 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9147 | break; |
| 9148 | case PyUnicode_2BYTE_KIND: |
| 9149 | result = ucs2lib_count( |
| 9150 | ((Py_UCS2*)buf1) + start, end - start, |
| 9151 | buf2, len2, PY_SSIZE_T_MAX |
| 9152 | ); |
| 9153 | break; |
| 9154 | case PyUnicode_4BYTE_KIND: |
| 9155 | result = ucs4lib_count( |
| 9156 | ((Py_UCS4*)buf1) + start, end - start, |
| 9157 | buf2, len2, PY_SSIZE_T_MAX |
| 9158 | ); |
| 9159 | break; |
| 9160 | default: |
| 9161 | assert(0); result = 0; |
| 9162 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9163 | |
| 9164 | Py_DECREF(sub_obj); |
| 9165 | Py_DECREF(str_obj); |
| 9166 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9167 | if (kind1 != kind) |
| 9168 | PyMem_Free(buf1); |
| 9169 | if (kind2 != kind) |
| 9170 | PyMem_Free(buf2); |
| 9171 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9172 | return result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9173 | onError: |
| 9174 | Py_DECREF(sub_obj); |
| 9175 | Py_DECREF(str_obj); |
| 9176 | if (kind1 != kind && buf1) |
| 9177 | PyMem_Free(buf1); |
| 9178 | if (kind2 != kind && buf2) |
| 9179 | PyMem_Free(buf2); |
| 9180 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9181 | } |
| 9182 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9183 | Py_ssize_t |
| 9184 | PyUnicode_Find(PyObject *str, |
| 9185 | PyObject *sub, |
| 9186 | Py_ssize_t start, |
| 9187 | Py_ssize_t end, |
| 9188 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9189 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9190 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9191 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9192 | str = PyUnicode_FromObject(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9193 | if (!str || PyUnicode_READY(str) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9194 | return -2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9195 | sub = PyUnicode_FromObject(sub); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9196 | if (!sub || PyUnicode_READY(sub) == -1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9197 | Py_DECREF(str); |
| 9198 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9199 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9200 | |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 9201 | result = any_find_slice(direction, |
| 9202 | str, sub, start, end |
| 9203 | ); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9204 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9205 | Py_DECREF(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9206 | Py_DECREF(sub); |
| 9207 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9208 | return result; |
| 9209 | } |
| 9210 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9211 | Py_ssize_t |
| 9212 | PyUnicode_FindChar(PyObject *str, Py_UCS4 ch, |
| 9213 | Py_ssize_t start, Py_ssize_t end, |
| 9214 | int direction) |
| 9215 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9216 | int kind; |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 9217 | Py_ssize_t result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9218 | if (PyUnicode_READY(str) == -1) |
| 9219 | return -2; |
Victor Stinner | 267aa24 | 2011-10-02 01:08:37 +0200 | [diff] [blame] | 9220 | if (start < 0 || end < 0) { |
| 9221 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 9222 | return -2; |
| 9223 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9224 | if (end > PyUnicode_GET_LENGTH(str)) |
| 9225 | end = PyUnicode_GET_LENGTH(str); |
| 9226 | kind = PyUnicode_KIND(str); |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 9227 | result = findchar(PyUnicode_1BYTE_DATA(str) + kind*start, |
| 9228 | kind, end-start, ch, direction); |
| 9229 | if (result == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9230 | return -1; |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 9231 | else |
| 9232 | return start + result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9233 | } |
| 9234 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9235 | static int |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9236 | tailmatch(PyObject *self, |
| 9237 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9238 | Py_ssize_t start, |
| 9239 | Py_ssize_t end, |
| 9240 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9241 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9242 | int kind_self; |
| 9243 | int kind_sub; |
| 9244 | void *data_self; |
| 9245 | void *data_sub; |
| 9246 | Py_ssize_t offset; |
| 9247 | Py_ssize_t i; |
| 9248 | Py_ssize_t end_sub; |
| 9249 | |
| 9250 | if (PyUnicode_READY(self) == -1 || |
| 9251 | PyUnicode_READY(substring) == -1) |
| 9252 | return 0; |
| 9253 | |
| 9254 | if (PyUnicode_GET_LENGTH(substring) == 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9255 | return 1; |
| 9256 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9257 | ADJUST_INDICES(start, end, PyUnicode_GET_LENGTH(self)); |
| 9258 | end -= PyUnicode_GET_LENGTH(substring); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9259 | if (end < start) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9260 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9261 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9262 | kind_self = PyUnicode_KIND(self); |
| 9263 | data_self = PyUnicode_DATA(self); |
| 9264 | kind_sub = PyUnicode_KIND(substring); |
| 9265 | data_sub = PyUnicode_DATA(substring); |
| 9266 | end_sub = PyUnicode_GET_LENGTH(substring) - 1; |
| 9267 | |
| 9268 | if (direction > 0) |
| 9269 | offset = end; |
| 9270 | else |
| 9271 | offset = start; |
| 9272 | |
| 9273 | if (PyUnicode_READ(kind_self, data_self, offset) == |
| 9274 | PyUnicode_READ(kind_sub, data_sub, 0) && |
| 9275 | PyUnicode_READ(kind_self, data_self, offset + end_sub) == |
| 9276 | PyUnicode_READ(kind_sub, data_sub, end_sub)) { |
| 9277 | /* If both are of the same kind, memcmp is sufficient */ |
| 9278 | if (kind_self == kind_sub) { |
| 9279 | return ! memcmp((char *)data_self + |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9280 | (offset * PyUnicode_KIND(substring)), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9281 | data_sub, |
| 9282 | PyUnicode_GET_LENGTH(substring) * |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9283 | PyUnicode_KIND(substring)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9284 | } |
| 9285 | /* otherwise we have to compare each character by first accesing it */ |
| 9286 | else { |
| 9287 | /* We do not need to compare 0 and len(substring)-1 because |
| 9288 | the if statement above ensured already that they are equal |
| 9289 | when we end up here. */ |
| 9290 | // TODO: honor direction and do a forward or backwards search |
| 9291 | for (i = 1; i < end_sub; ++i) { |
| 9292 | if (PyUnicode_READ(kind_self, data_self, offset + i) != |
| 9293 | PyUnicode_READ(kind_sub, data_sub, i)) |
| 9294 | return 0; |
| 9295 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9296 | return 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9297 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9298 | } |
| 9299 | |
| 9300 | return 0; |
| 9301 | } |
| 9302 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9303 | Py_ssize_t |
| 9304 | PyUnicode_Tailmatch(PyObject *str, |
| 9305 | PyObject *substr, |
| 9306 | Py_ssize_t start, |
| 9307 | Py_ssize_t end, |
| 9308 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9309 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9310 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9311 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9312 | str = PyUnicode_FromObject(str); |
| 9313 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9314 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9315 | substr = PyUnicode_FromObject(substr); |
| 9316 | if (substr == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9317 | Py_DECREF(str); |
| 9318 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9319 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9320 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9321 | result = tailmatch(str, substr, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9322 | start, end, direction); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9323 | Py_DECREF(str); |
| 9324 | Py_DECREF(substr); |
| 9325 | return result; |
| 9326 | } |
| 9327 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9328 | /* Apply fixfct filter to the Unicode object self and return a |
| 9329 | reference to the modified object */ |
| 9330 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9331 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9332 | fixup(PyObject *self, |
| 9333 | Py_UCS4 (*fixfct)(PyObject *s)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9334 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9335 | PyObject *u; |
| 9336 | Py_UCS4 maxchar_old, maxchar_new = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9337 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9338 | if (PyUnicode_READY(self) == -1) |
| 9339 | return NULL; |
| 9340 | maxchar_old = PyUnicode_MAX_CHAR_VALUE(self); |
| 9341 | u = PyUnicode_New(PyUnicode_GET_LENGTH(self), |
| 9342 | maxchar_old); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9343 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9344 | return NULL; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9345 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9346 | Py_MEMCPY(PyUnicode_1BYTE_DATA(u), PyUnicode_1BYTE_DATA(self), |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9347 | PyUnicode_GET_LENGTH(u) * PyUnicode_KIND(u)); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9348 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9349 | /* fix functions return the new maximum character in a string, |
| 9350 | if the kind of the resulting unicode object does not change, |
| 9351 | everything is fine. Otherwise we need to change the string kind |
| 9352 | and re-run the fix function. */ |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9353 | maxchar_new = fixfct(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9354 | if (maxchar_new == 0) |
| 9355 | /* do nothing, keep maxchar_new at 0 which means no changes. */; |
| 9356 | else if (maxchar_new <= 127) |
| 9357 | maxchar_new = 127; |
| 9358 | else if (maxchar_new <= 255) |
| 9359 | maxchar_new = 255; |
| 9360 | else if (maxchar_new <= 65535) |
| 9361 | maxchar_new = 65535; |
| 9362 | else |
| 9363 | maxchar_new = 1114111; /* 0x10ffff */ |
| 9364 | |
| 9365 | if (!maxchar_new && PyUnicode_CheckExact(self)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9366 | /* fixfct should return TRUE if it modified the buffer. If |
| 9367 | FALSE, return a reference to the original buffer instead |
| 9368 | (to save space, not time) */ |
| 9369 | Py_INCREF(self); |
| 9370 | Py_DECREF(u); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9371 | return self; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9372 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9373 | else if (maxchar_new == maxchar_old) { |
| 9374 | return u; |
| 9375 | } |
| 9376 | else { |
| 9377 | /* In case the maximum character changed, we need to |
| 9378 | convert the string to the new category. */ |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9379 | PyObject *v = PyUnicode_New(PyUnicode_GET_LENGTH(self), maxchar_new); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9380 | if (v == NULL) { |
| 9381 | Py_DECREF(u); |
| 9382 | return NULL; |
| 9383 | } |
| 9384 | if (maxchar_new > maxchar_old) { |
| 9385 | /* If the maxchar increased so that the kind changed, not all |
| 9386 | characters are representable anymore and we need to fix the |
| 9387 | string again. This only happens in very few cases. */ |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9388 | copy_characters(v, 0, self, 0, PyUnicode_GET_LENGTH(self)); |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9389 | maxchar_old = fixfct(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9390 | assert(maxchar_old > 0 && maxchar_old <= maxchar_new); |
| 9391 | } |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9392 | else { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9393 | copy_characters(v, 0, u, 0, PyUnicode_GET_LENGTH(self)); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9394 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9395 | |
| 9396 | Py_DECREF(u); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 9397 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9398 | return v; |
| 9399 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9400 | } |
| 9401 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9402 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9403 | fixupper(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9404 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9405 | /* No need to call PyUnicode_READY(self) because this function is only |
| 9406 | called as a callback from fixup() which does it already. */ |
| 9407 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 9408 | const int kind = PyUnicode_KIND(self); |
| 9409 | void *data = PyUnicode_DATA(self); |
| 9410 | int touched = 0; |
| 9411 | Py_UCS4 maxchar = 0; |
| 9412 | Py_ssize_t i; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9413 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9414 | for (i = 0; i < len; ++i) { |
| 9415 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 9416 | const Py_UCS4 up = Py_UNICODE_TOUPPER(ch); |
| 9417 | if (up != ch) { |
| 9418 | if (up > maxchar) |
| 9419 | maxchar = up; |
| 9420 | PyUnicode_WRITE(kind, data, i, up); |
| 9421 | touched = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9422 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9423 | else if (ch > maxchar) |
| 9424 | maxchar = ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9425 | } |
| 9426 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9427 | if (touched) |
| 9428 | return maxchar; |
| 9429 | else |
| 9430 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9431 | } |
| 9432 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9433 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9434 | fixlower(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9435 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9436 | /* No need to call PyUnicode_READY(self) because fixup() which does it. */ |
| 9437 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 9438 | const int kind = PyUnicode_KIND(self); |
| 9439 | void *data = PyUnicode_DATA(self); |
| 9440 | int touched = 0; |
| 9441 | Py_UCS4 maxchar = 0; |
| 9442 | Py_ssize_t i; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9443 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9444 | for(i = 0; i < len; ++i) { |
| 9445 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 9446 | const Py_UCS4 lo = Py_UNICODE_TOLOWER(ch); |
| 9447 | if (lo != ch) { |
| 9448 | if (lo > maxchar) |
| 9449 | maxchar = lo; |
| 9450 | PyUnicode_WRITE(kind, data, i, lo); |
| 9451 | touched = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9452 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9453 | else if (ch > maxchar) |
| 9454 | maxchar = ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9455 | } |
| 9456 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9457 | if (touched) |
| 9458 | return maxchar; |
| 9459 | else |
| 9460 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9461 | } |
| 9462 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9463 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9464 | fixswapcase(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9465 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9466 | /* No need to call PyUnicode_READY(self) because fixup() which does it. */ |
| 9467 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 9468 | const int kind = PyUnicode_KIND(self); |
| 9469 | void *data = PyUnicode_DATA(self); |
| 9470 | int touched = 0; |
| 9471 | Py_UCS4 maxchar = 0; |
| 9472 | Py_ssize_t i; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9473 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9474 | for(i = 0; i < len; ++i) { |
| 9475 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 9476 | Py_UCS4 nu = 0; |
| 9477 | |
| 9478 | if (Py_UNICODE_ISUPPER(ch)) |
| 9479 | nu = Py_UNICODE_TOLOWER(ch); |
| 9480 | else if (Py_UNICODE_ISLOWER(ch)) |
| 9481 | nu = Py_UNICODE_TOUPPER(ch); |
| 9482 | |
| 9483 | if (nu != 0) { |
| 9484 | if (nu > maxchar) |
| 9485 | maxchar = nu; |
| 9486 | PyUnicode_WRITE(kind, data, i, nu); |
| 9487 | touched = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9488 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9489 | else if (ch > maxchar) |
| 9490 | maxchar = ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9491 | } |
| 9492 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9493 | if (touched) |
| 9494 | return maxchar; |
| 9495 | else |
| 9496 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9497 | } |
| 9498 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9499 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9500 | fixcapitalize(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9501 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9502 | /* No need to call PyUnicode_READY(self) because fixup() which does it. */ |
| 9503 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 9504 | const int kind = PyUnicode_KIND(self); |
| 9505 | void *data = PyUnicode_DATA(self); |
| 9506 | int touched = 0; |
| 9507 | Py_UCS4 maxchar = 0; |
| 9508 | Py_ssize_t i = 0; |
| 9509 | Py_UCS4 ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9510 | |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9511 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9512 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9513 | |
| 9514 | ch = PyUnicode_READ(kind, data, i); |
| 9515 | if (!Py_UNICODE_ISUPPER(ch)) { |
| 9516 | maxchar = Py_UNICODE_TOUPPER(ch); |
| 9517 | PyUnicode_WRITE(kind, data, i, maxchar); |
| 9518 | touched = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9519 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9520 | ++i; |
| 9521 | for(; i < len; ++i) { |
| 9522 | ch = PyUnicode_READ(kind, data, i); |
| 9523 | if (!Py_UNICODE_ISLOWER(ch)) { |
| 9524 | const Py_UCS4 lo = Py_UNICODE_TOLOWER(ch); |
| 9525 | if (lo > maxchar) |
| 9526 | maxchar = lo; |
| 9527 | PyUnicode_WRITE(kind, data, i, lo); |
| 9528 | touched = 1; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9529 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9530 | else if (ch > maxchar) |
| 9531 | maxchar = ch; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9532 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9533 | |
| 9534 | if (touched) |
| 9535 | return maxchar; |
| 9536 | else |
| 9537 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9538 | } |
| 9539 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9540 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9541 | fixtitle(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9542 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9543 | /* No need to call PyUnicode_READY(self) because fixup() which does it. */ |
| 9544 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 9545 | const int kind = PyUnicode_KIND(self); |
| 9546 | void *data = PyUnicode_DATA(self); |
| 9547 | Py_UCS4 maxchar = 0; |
| 9548 | Py_ssize_t i = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9549 | int previous_is_cased; |
| 9550 | |
| 9551 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9552 | if (len == 1) { |
| 9553 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 9554 | const Py_UCS4 ti = Py_UNICODE_TOTITLE(ch); |
| 9555 | if (ti != ch) { |
| 9556 | PyUnicode_WRITE(kind, data, i, ti); |
| 9557 | return ti; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9558 | } |
| 9559 | else |
| 9560 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9561 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9562 | previous_is_cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9563 | for(; i < len; ++i) { |
| 9564 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 9565 | Py_UCS4 nu; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9566 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9567 | if (previous_is_cased) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9568 | nu = Py_UNICODE_TOLOWER(ch); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9569 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9570 | nu = Py_UNICODE_TOTITLE(ch); |
| 9571 | |
| 9572 | if (nu > maxchar) |
| 9573 | maxchar = nu; |
| 9574 | PyUnicode_WRITE(kind, data, i, nu); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9575 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9576 | if (Py_UNICODE_ISLOWER(ch) || |
| 9577 | Py_UNICODE_ISUPPER(ch) || |
| 9578 | Py_UNICODE_ISTITLE(ch)) |
| 9579 | previous_is_cased = 1; |
| 9580 | else |
| 9581 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9582 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9583 | return maxchar; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9584 | } |
| 9585 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9586 | PyObject * |
| 9587 | PyUnicode_Join(PyObject *separator, PyObject *seq) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9588 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9589 | PyObject *sep = NULL; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9590 | Py_ssize_t seplen; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9591 | PyObject *res = NULL; /* the result */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9592 | PyObject *fseq; /* PySequence_Fast(seq) */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9593 | Py_ssize_t seqlen; /* len(fseq) -- number of items in sequence */ |
| 9594 | PyObject **items; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9595 | PyObject *item; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9596 | Py_ssize_t sz, i, res_offset; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9597 | Py_UCS4 maxchar; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9598 | Py_UCS4 item_maxchar; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9599 | int use_memcpy; |
| 9600 | unsigned char *res_data = NULL, *sep_data = NULL; |
| 9601 | PyObject *last_obj; |
| 9602 | unsigned int kind = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9603 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9604 | fseq = PySequence_Fast(seq, ""); |
| 9605 | if (fseq == NULL) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9606 | return NULL; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9607 | } |
| 9608 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9609 | /* NOTE: the following code can't call back into Python code, |
| 9610 | * so we are sure that fseq won't be mutated. |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 9611 | */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9612 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9613 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 9614 | /* If empty sequence, return u"". */ |
| 9615 | if (seqlen == 0) { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9616 | Py_DECREF(fseq); |
| 9617 | Py_INCREF(unicode_empty); |
| 9618 | res = unicode_empty; |
| 9619 | return res; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9620 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9621 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9622 | /* If singleton sequence with an exact Unicode, return that. */ |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9623 | last_obj = NULL; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9624 | items = PySequence_Fast_ITEMS(fseq); |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9625 | if (seqlen == 1) { |
| 9626 | if (PyUnicode_CheckExact(items[0])) { |
| 9627 | res = items[0]; |
| 9628 | Py_INCREF(res); |
| 9629 | Py_DECREF(fseq); |
| 9630 | return res; |
| 9631 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9632 | seplen = 0; |
Victor Stinner | c6f0df7 | 2011-10-06 15:58:54 +0200 | [diff] [blame] | 9633 | maxchar = 0; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9634 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9635 | else { |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9636 | /* Set up sep and seplen */ |
| 9637 | if (separator == NULL) { |
| 9638 | /* fall back to a blank space separator */ |
| 9639 | sep = PyUnicode_FromOrdinal(' '); |
| 9640 | if (!sep) |
| 9641 | goto onError; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9642 | seplen = 1; |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9643 | maxchar = 32; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9644 | } |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9645 | else { |
| 9646 | if (!PyUnicode_Check(separator)) { |
| 9647 | PyErr_Format(PyExc_TypeError, |
| 9648 | "separator: expected str instance," |
| 9649 | " %.80s found", |
| 9650 | Py_TYPE(separator)->tp_name); |
| 9651 | goto onError; |
| 9652 | } |
| 9653 | if (PyUnicode_READY(separator)) |
| 9654 | goto onError; |
| 9655 | sep = separator; |
| 9656 | seplen = PyUnicode_GET_LENGTH(separator); |
| 9657 | maxchar = PyUnicode_MAX_CHAR_VALUE(separator); |
| 9658 | /* inc refcount to keep this code path symmetric with the |
| 9659 | above case of a blank separator */ |
| 9660 | Py_INCREF(sep); |
| 9661 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9662 | last_obj = sep; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9663 | } |
| 9664 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9665 | /* There are at least two things to join, or else we have a subclass |
| 9666 | * of str in the sequence. |
| 9667 | * Do a pre-pass to figure out the total amount of space we'll |
| 9668 | * need (sz), and see whether all argument are strings. |
| 9669 | */ |
| 9670 | sz = 0; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9671 | #ifdef Py_DEBUG |
| 9672 | use_memcpy = 0; |
| 9673 | #else |
| 9674 | use_memcpy = 1; |
| 9675 | #endif |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9676 | for (i = 0; i < seqlen; i++) { |
| 9677 | const Py_ssize_t old_sz = sz; |
| 9678 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9679 | if (!PyUnicode_Check(item)) { |
| 9680 | PyErr_Format(PyExc_TypeError, |
| 9681 | "sequence item %zd: expected str instance," |
| 9682 | " %.80s found", |
| 9683 | i, Py_TYPE(item)->tp_name); |
| 9684 | goto onError; |
| 9685 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9686 | if (PyUnicode_READY(item) == -1) |
| 9687 | goto onError; |
| 9688 | sz += PyUnicode_GET_LENGTH(item); |
| 9689 | item_maxchar = PyUnicode_MAX_CHAR_VALUE(item); |
Victor Stinner | c6f0df7 | 2011-10-06 15:58:54 +0200 | [diff] [blame] | 9690 | maxchar = Py_MAX(maxchar, item_maxchar); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9691 | if (i != 0) |
| 9692 | sz += seplen; |
| 9693 | if (sz < old_sz || sz > PY_SSIZE_T_MAX) { |
| 9694 | PyErr_SetString(PyExc_OverflowError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9695 | "join() result is too long for a Python string"); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9696 | goto onError; |
| 9697 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9698 | if (use_memcpy && last_obj != NULL) { |
| 9699 | if (PyUnicode_KIND(last_obj) != PyUnicode_KIND(item)) |
| 9700 | use_memcpy = 0; |
| 9701 | } |
| 9702 | last_obj = item; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9703 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9704 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9705 | res = PyUnicode_New(sz, maxchar); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9706 | if (res == NULL) |
| 9707 | goto onError; |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 9708 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9709 | /* Catenate everything. */ |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9710 | #ifdef Py_DEBUG |
| 9711 | use_memcpy = 0; |
| 9712 | #else |
| 9713 | if (use_memcpy) { |
| 9714 | res_data = PyUnicode_1BYTE_DATA(res); |
| 9715 | kind = PyUnicode_KIND(res); |
| 9716 | if (seplen != 0) |
| 9717 | sep_data = PyUnicode_1BYTE_DATA(sep); |
| 9718 | } |
| 9719 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9720 | for (i = 0, res_offset = 0; i < seqlen; ++i) { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9721 | Py_ssize_t itemlen; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9722 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9723 | /* Copy item, and maybe the separator. */ |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 9724 | if (i && seplen != 0) { |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9725 | if (use_memcpy) { |
| 9726 | Py_MEMCPY(res_data, |
| 9727 | sep_data, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9728 | kind * seplen); |
| 9729 | res_data += kind * seplen; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9730 | } |
| 9731 | else { |
| 9732 | copy_characters(res, res_offset, sep, 0, seplen); |
| 9733 | res_offset += seplen; |
| 9734 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9735 | } |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 9736 | itemlen = PyUnicode_GET_LENGTH(item); |
| 9737 | if (itemlen != 0) { |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9738 | if (use_memcpy) { |
| 9739 | Py_MEMCPY(res_data, |
| 9740 | PyUnicode_DATA(item), |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9741 | kind * itemlen); |
| 9742 | res_data += kind * itemlen; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9743 | } |
| 9744 | else { |
| 9745 | copy_characters(res, res_offset, item, 0, itemlen); |
| 9746 | res_offset += itemlen; |
| 9747 | } |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 9748 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9749 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9750 | if (use_memcpy) |
| 9751 | assert(res_data == PyUnicode_1BYTE_DATA(res) |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9752 | + kind * PyUnicode_GET_LENGTH(res)); |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9753 | else |
| 9754 | assert(res_offset == PyUnicode_GET_LENGTH(res)); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9755 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9756 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9757 | Py_XDECREF(sep); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 9758 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9759 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9760 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9761 | onError: |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9762 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9763 | Py_XDECREF(sep); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9764 | Py_XDECREF(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9765 | return NULL; |
| 9766 | } |
| 9767 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9768 | #define FILL(kind, data, value, start, length) \ |
| 9769 | do { \ |
| 9770 | Py_ssize_t i_ = 0; \ |
| 9771 | assert(kind != PyUnicode_WCHAR_KIND); \ |
| 9772 | switch ((kind)) { \ |
| 9773 | case PyUnicode_1BYTE_KIND: { \ |
| 9774 | unsigned char * to_ = (unsigned char *)((data)) + (start); \ |
| 9775 | memset(to_, (unsigned char)value, length); \ |
| 9776 | break; \ |
| 9777 | } \ |
| 9778 | case PyUnicode_2BYTE_KIND: { \ |
| 9779 | Py_UCS2 * to_ = (Py_UCS2 *)((data)) + (start); \ |
| 9780 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 9781 | break; \ |
| 9782 | } \ |
| 9783 | default: { \ |
| 9784 | Py_UCS4 * to_ = (Py_UCS4 *)((data)) + (start); \ |
| 9785 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 9786 | break; \ |
| 9787 | } \ |
| 9788 | } \ |
| 9789 | } while (0) |
| 9790 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9791 | static PyObject * |
| 9792 | pad(PyObject *self, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9793 | Py_ssize_t left, |
| 9794 | Py_ssize_t right, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9795 | Py_UCS4 fill) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9796 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9797 | PyObject *u; |
| 9798 | Py_UCS4 maxchar; |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9799 | int kind; |
| 9800 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9801 | |
| 9802 | if (left < 0) |
| 9803 | left = 0; |
| 9804 | if (right < 0) |
| 9805 | right = 0; |
| 9806 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 9807 | if (left == 0 && right == 0 && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9808 | Py_INCREF(self); |
| 9809 | return self; |
| 9810 | } |
| 9811 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9812 | if (left > PY_SSIZE_T_MAX - _PyUnicode_LENGTH(self) || |
| 9813 | right > PY_SSIZE_T_MAX - (left + _PyUnicode_LENGTH(self))) { |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 9814 | PyErr_SetString(PyExc_OverflowError, "padded string is too long"); |
| 9815 | return NULL; |
| 9816 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9817 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
| 9818 | if (fill > maxchar) |
| 9819 | maxchar = fill; |
| 9820 | u = PyUnicode_New(left + _PyUnicode_LENGTH(self) + right, maxchar); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9821 | if (!u) |
| 9822 | return NULL; |
| 9823 | |
| 9824 | kind = PyUnicode_KIND(u); |
| 9825 | data = PyUnicode_DATA(u); |
| 9826 | if (left) |
| 9827 | FILL(kind, data, fill, 0, left); |
| 9828 | if (right) |
| 9829 | FILL(kind, data, fill, left + _PyUnicode_LENGTH(self), right); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9830 | copy_characters(u, left, self, 0, _PyUnicode_LENGTH(self)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 9831 | assert(_PyUnicode_CheckConsistency(u, 1)); |
| 9832 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9833 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9834 | #undef FILL |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9835 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9836 | PyObject * |
| 9837 | PyUnicode_Splitlines(PyObject *string, int keepends) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9838 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9839 | PyObject *list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9840 | |
| 9841 | string = PyUnicode_FromObject(string); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9842 | if (string == NULL || PyUnicode_READY(string) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9843 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9844 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9845 | switch(PyUnicode_KIND(string)) { |
| 9846 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9847 | if (PyUnicode_IS_ASCII(string)) |
| 9848 | list = asciilib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9849 | string, PyUnicode_1BYTE_DATA(string), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9850 | PyUnicode_GET_LENGTH(string), keepends); |
| 9851 | else |
| 9852 | list = ucs1lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9853 | string, PyUnicode_1BYTE_DATA(string), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9854 | PyUnicode_GET_LENGTH(string), keepends); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9855 | break; |
| 9856 | case PyUnicode_2BYTE_KIND: |
| 9857 | list = ucs2lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9858 | string, PyUnicode_2BYTE_DATA(string), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9859 | PyUnicode_GET_LENGTH(string), keepends); |
| 9860 | break; |
| 9861 | case PyUnicode_4BYTE_KIND: |
| 9862 | list = ucs4lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9863 | string, PyUnicode_4BYTE_DATA(string), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9864 | PyUnicode_GET_LENGTH(string), keepends); |
| 9865 | break; |
| 9866 | default: |
| 9867 | assert(0); |
| 9868 | list = 0; |
| 9869 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9870 | Py_DECREF(string); |
| 9871 | return list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9872 | } |
| 9873 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9874 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9875 | split(PyObject *self, |
| 9876 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9877 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9878 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9879 | int kind1, kind2, kind; |
| 9880 | void *buf1, *buf2; |
| 9881 | Py_ssize_t len1, len2; |
| 9882 | PyObject* out; |
| 9883 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9884 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9885 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9886 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9887 | if (PyUnicode_READY(self) == -1) |
| 9888 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9889 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9890 | if (substring == NULL) |
| 9891 | switch(PyUnicode_KIND(self)) { |
| 9892 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9893 | if (PyUnicode_IS_ASCII(self)) |
| 9894 | return asciilib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9895 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9896 | PyUnicode_GET_LENGTH(self), maxcount |
| 9897 | ); |
| 9898 | else |
| 9899 | return ucs1lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9900 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9901 | PyUnicode_GET_LENGTH(self), maxcount |
| 9902 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9903 | case PyUnicode_2BYTE_KIND: |
| 9904 | return ucs2lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9905 | self, PyUnicode_2BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9906 | PyUnicode_GET_LENGTH(self), maxcount |
| 9907 | ); |
| 9908 | case PyUnicode_4BYTE_KIND: |
| 9909 | return ucs4lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9910 | self, PyUnicode_4BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9911 | PyUnicode_GET_LENGTH(self), maxcount |
| 9912 | ); |
| 9913 | default: |
| 9914 | assert(0); |
| 9915 | return NULL; |
| 9916 | } |
| 9917 | |
| 9918 | if (PyUnicode_READY(substring) == -1) |
| 9919 | return NULL; |
| 9920 | |
| 9921 | kind1 = PyUnicode_KIND(self); |
| 9922 | kind2 = PyUnicode_KIND(substring); |
| 9923 | kind = kind1 > kind2 ? kind1 : kind2; |
| 9924 | buf1 = PyUnicode_DATA(self); |
| 9925 | buf2 = PyUnicode_DATA(substring); |
| 9926 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9927 | buf1 = _PyUnicode_AsKind(self, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9928 | if (!buf1) |
| 9929 | return NULL; |
| 9930 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9931 | buf2 = _PyUnicode_AsKind(substring, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9932 | if (!buf2) { |
| 9933 | if (kind1 != kind) PyMem_Free(buf1); |
| 9934 | return NULL; |
| 9935 | } |
| 9936 | len1 = PyUnicode_GET_LENGTH(self); |
| 9937 | len2 = PyUnicode_GET_LENGTH(substring); |
| 9938 | |
| 9939 | switch(kind) { |
| 9940 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9941 | if (PyUnicode_IS_ASCII(self) && PyUnicode_IS_ASCII(substring)) |
| 9942 | out = asciilib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9943 | self, buf1, len1, buf2, len2, maxcount); |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9944 | else |
| 9945 | out = ucs1lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9946 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9947 | break; |
| 9948 | case PyUnicode_2BYTE_KIND: |
| 9949 | out = ucs2lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9950 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9951 | break; |
| 9952 | case PyUnicode_4BYTE_KIND: |
| 9953 | out = ucs4lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9954 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9955 | break; |
| 9956 | default: |
| 9957 | out = NULL; |
| 9958 | } |
| 9959 | if (kind1 != kind) |
| 9960 | PyMem_Free(buf1); |
| 9961 | if (kind2 != kind) |
| 9962 | PyMem_Free(buf2); |
| 9963 | return out; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9964 | } |
| 9965 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9966 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9967 | rsplit(PyObject *self, |
| 9968 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9969 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9970 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9971 | int kind1, kind2, kind; |
| 9972 | void *buf1, *buf2; |
| 9973 | Py_ssize_t len1, len2; |
| 9974 | PyObject* out; |
| 9975 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9976 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9977 | maxcount = PY_SSIZE_T_MAX; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9978 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9979 | if (PyUnicode_READY(self) == -1) |
| 9980 | return NULL; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9981 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9982 | if (substring == NULL) |
| 9983 | switch(PyUnicode_KIND(self)) { |
| 9984 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9985 | if (PyUnicode_IS_ASCII(self)) |
| 9986 | return asciilib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9987 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9988 | PyUnicode_GET_LENGTH(self), maxcount |
| 9989 | ); |
| 9990 | else |
| 9991 | return ucs1lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9992 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9993 | PyUnicode_GET_LENGTH(self), maxcount |
| 9994 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9995 | case PyUnicode_2BYTE_KIND: |
| 9996 | return ucs2lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9997 | self, PyUnicode_2BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9998 | PyUnicode_GET_LENGTH(self), maxcount |
| 9999 | ); |
| 10000 | case PyUnicode_4BYTE_KIND: |
| 10001 | return ucs4lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10002 | self, PyUnicode_4BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10003 | PyUnicode_GET_LENGTH(self), maxcount |
| 10004 | ); |
| 10005 | default: |
| 10006 | assert(0); |
| 10007 | return NULL; |
| 10008 | } |
| 10009 | |
| 10010 | if (PyUnicode_READY(substring) == -1) |
| 10011 | return NULL; |
| 10012 | |
| 10013 | kind1 = PyUnicode_KIND(self); |
| 10014 | kind2 = PyUnicode_KIND(substring); |
| 10015 | kind = kind1 > kind2 ? kind1 : kind2; |
| 10016 | buf1 = PyUnicode_DATA(self); |
| 10017 | buf2 = PyUnicode_DATA(substring); |
| 10018 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10019 | buf1 = _PyUnicode_AsKind(self, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10020 | if (!buf1) |
| 10021 | return NULL; |
| 10022 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10023 | buf2 = _PyUnicode_AsKind(substring, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10024 | if (!buf2) { |
| 10025 | if (kind1 != kind) PyMem_Free(buf1); |
| 10026 | return NULL; |
| 10027 | } |
| 10028 | len1 = PyUnicode_GET_LENGTH(self); |
| 10029 | len2 = PyUnicode_GET_LENGTH(substring); |
| 10030 | |
| 10031 | switch(kind) { |
| 10032 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10033 | if (PyUnicode_IS_ASCII(self) && PyUnicode_IS_ASCII(substring)) |
| 10034 | out = asciilib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10035 | self, buf1, len1, buf2, len2, maxcount); |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10036 | else |
| 10037 | out = ucs1lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10038 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10039 | break; |
| 10040 | case PyUnicode_2BYTE_KIND: |
| 10041 | out = ucs2lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10042 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10043 | break; |
| 10044 | case PyUnicode_4BYTE_KIND: |
| 10045 | out = ucs4lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10046 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10047 | break; |
| 10048 | default: |
| 10049 | out = NULL; |
| 10050 | } |
| 10051 | if (kind1 != kind) |
| 10052 | PyMem_Free(buf1); |
| 10053 | if (kind2 != kind) |
| 10054 | PyMem_Free(buf2); |
| 10055 | return out; |
| 10056 | } |
| 10057 | |
| 10058 | static Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10059 | anylib_find(int kind, PyObject *str1, void *buf1, Py_ssize_t len1, |
| 10060 | 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] | 10061 | { |
| 10062 | switch(kind) { |
| 10063 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10064 | if (PyUnicode_IS_ASCII(str1) && PyUnicode_IS_ASCII(str2)) |
| 10065 | return asciilib_find(buf1, len1, buf2, len2, offset); |
| 10066 | else |
| 10067 | return ucs1lib_find(buf1, len1, buf2, len2, offset); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10068 | case PyUnicode_2BYTE_KIND: |
| 10069 | return ucs2lib_find(buf1, len1, buf2, len2, offset); |
| 10070 | case PyUnicode_4BYTE_KIND: |
| 10071 | return ucs4lib_find(buf1, len1, buf2, len2, offset); |
| 10072 | } |
| 10073 | assert(0); |
| 10074 | return -1; |
| 10075 | } |
| 10076 | |
| 10077 | static Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10078 | anylib_count(int kind, PyObject *sstr, void* sbuf, Py_ssize_t slen, |
| 10079 | 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] | 10080 | { |
| 10081 | switch(kind) { |
| 10082 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10083 | if (PyUnicode_IS_ASCII(sstr) && PyUnicode_IS_ASCII(str1)) |
| 10084 | return asciilib_count(sbuf, slen, buf1, len1, maxcount); |
| 10085 | else |
| 10086 | return ucs1lib_count(sbuf, slen, buf1, len1, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10087 | case PyUnicode_2BYTE_KIND: |
| 10088 | return ucs2lib_count(sbuf, slen, buf1, len1, maxcount); |
| 10089 | case PyUnicode_4BYTE_KIND: |
| 10090 | return ucs4lib_count(sbuf, slen, buf1, len1, maxcount); |
| 10091 | } |
| 10092 | assert(0); |
| 10093 | return 0; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10094 | } |
| 10095 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10096 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10097 | replace(PyObject *self, PyObject *str1, |
| 10098 | PyObject *str2, Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10099 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10100 | PyObject *u; |
| 10101 | char *sbuf = PyUnicode_DATA(self); |
| 10102 | char *buf1 = PyUnicode_DATA(str1); |
| 10103 | char *buf2 = PyUnicode_DATA(str2); |
| 10104 | int srelease = 0, release1 = 0, release2 = 0; |
| 10105 | int skind = PyUnicode_KIND(self); |
| 10106 | int kind1 = PyUnicode_KIND(str1); |
| 10107 | int kind2 = PyUnicode_KIND(str2); |
| 10108 | Py_ssize_t slen = PyUnicode_GET_LENGTH(self); |
| 10109 | Py_ssize_t len1 = PyUnicode_GET_LENGTH(str1); |
| 10110 | Py_ssize_t len2 = PyUnicode_GET_LENGTH(str2); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10111 | int mayshrink; |
| 10112 | Py_UCS4 maxchar, maxchar_str2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10113 | |
| 10114 | if (maxcount < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10115 | maxcount = PY_SSIZE_T_MAX; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10116 | else if (maxcount == 0 || slen == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10117 | goto nothing; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10118 | |
Victor Stinner | 59de0ee | 2011-10-07 10:01:28 +0200 | [diff] [blame] | 10119 | if (str1 == str2) |
| 10120 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10121 | if (skind < kind1) |
| 10122 | /* substring too wide to be present */ |
| 10123 | goto nothing; |
| 10124 | |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10125 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
| 10126 | maxchar_str2 = PyUnicode_MAX_CHAR_VALUE(str2); |
| 10127 | /* Replacing str1 with str2 may cause a maxchar reduction in the |
| 10128 | result string. */ |
| 10129 | mayshrink = (maxchar_str2 < maxchar); |
| 10130 | maxchar = Py_MAX(maxchar, maxchar_str2); |
| 10131 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10132 | if (len1 == len2) { |
Antoine Pitrou | cbfdee3 | 2010-01-13 08:58:08 +0000 | [diff] [blame] | 10133 | Py_ssize_t i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10134 | /* same length */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10135 | if (len1 == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10136 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10137 | if (len1 == 1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10138 | /* replace characters */ |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10139 | Py_UCS4 u1, u2; |
| 10140 | int rkind; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10141 | u1 = PyUnicode_READ_CHAR(str1, 0); |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 10142 | if (findchar(sbuf, PyUnicode_KIND(self), |
| 10143 | slen, u1, 1) < 0) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10144 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10145 | u2 = PyUnicode_READ_CHAR(str2, 0); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10146 | u = PyUnicode_New(slen, maxchar); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10147 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10148 | goto error; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 10149 | copy_characters(u, 0, self, 0, slen); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10150 | rkind = PyUnicode_KIND(u); |
| 10151 | for (i = 0; i < PyUnicode_GET_LENGTH(u); i++) |
| 10152 | if (PyUnicode_READ(rkind, PyUnicode_DATA(u), i) == u1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10153 | if (--maxcount < 0) |
| 10154 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10155 | PyUnicode_WRITE(rkind, PyUnicode_DATA(u), i, u2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10156 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10157 | } |
| 10158 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10159 | int rkind = skind; |
| 10160 | char *res; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10161 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10162 | if (kind1 < rkind) { |
| 10163 | /* widen substring */ |
| 10164 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10165 | if (!buf1) goto error; |
| 10166 | release1 = 1; |
| 10167 | } |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10168 | i = anylib_find(rkind, self, sbuf, slen, str1, buf1, len1, 0); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10169 | if (i < 0) |
| 10170 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10171 | if (rkind > kind2) { |
| 10172 | /* widen replacement */ |
| 10173 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 10174 | if (!buf2) goto error; |
| 10175 | release2 = 1; |
| 10176 | } |
| 10177 | else if (rkind < kind2) { |
| 10178 | /* widen self and buf1 */ |
| 10179 | rkind = kind2; |
| 10180 | if (release1) PyMem_Free(buf1); |
| 10181 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 10182 | if (!sbuf) goto error; |
| 10183 | srelease = 1; |
| 10184 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10185 | if (!buf1) goto error; |
| 10186 | release1 = 1; |
| 10187 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10188 | u = PyUnicode_New(slen, maxchar); |
| 10189 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10190 | goto error; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10191 | assert(PyUnicode_KIND(u) == rkind); |
| 10192 | res = PyUnicode_DATA(u); |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10193 | |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10194 | memcpy(res, sbuf, rkind * slen); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10195 | /* change everything in-place, starting with this one */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10196 | memcpy(res + rkind * i, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10197 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10198 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10199 | i += len1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10200 | |
| 10201 | while ( --maxcount > 0) { |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10202 | i = anylib_find(rkind, self, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10203 | sbuf+rkind*i, slen-i, |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10204 | str1, buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10205 | if (i == -1) |
| 10206 | break; |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10207 | memcpy(res + rkind * i, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10208 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10209 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10210 | i += len1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10211 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10212 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10213 | } |
| 10214 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10215 | Py_ssize_t n, i, j, ires; |
| 10216 | Py_ssize_t product, new_size; |
| 10217 | int rkind = skind; |
| 10218 | char *res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10219 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10220 | if (kind1 < rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10221 | /* widen substring */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10222 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10223 | if (!buf1) goto error; |
| 10224 | release1 = 1; |
| 10225 | } |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10226 | n = anylib_count(rkind, self, sbuf, slen, str1, buf1, len1, maxcount); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10227 | if (n == 0) |
| 10228 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10229 | if (kind2 < rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10230 | /* widen replacement */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10231 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 10232 | if (!buf2) goto error; |
| 10233 | release2 = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10234 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10235 | else if (kind2 > rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10236 | /* widen self and buf1 */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10237 | rkind = kind2; |
| 10238 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 10239 | if (!sbuf) goto error; |
| 10240 | srelease = 1; |
| 10241 | if (release1) PyMem_Free(buf1); |
| 10242 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10243 | if (!buf1) goto error; |
| 10244 | release1 = 1; |
| 10245 | } |
| 10246 | /* new_size = PyUnicode_GET_LENGTH(self) + n * (PyUnicode_GET_LENGTH(str2) - |
| 10247 | PyUnicode_GET_LENGTH(str1))); */ |
| 10248 | product = n * (len2-len1); |
| 10249 | if ((product / (len2-len1)) != n) { |
| 10250 | PyErr_SetString(PyExc_OverflowError, |
| 10251 | "replace string is too long"); |
| 10252 | goto error; |
| 10253 | } |
| 10254 | new_size = slen + product; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10255 | if (new_size == 0) { |
| 10256 | Py_INCREF(unicode_empty); |
| 10257 | u = unicode_empty; |
| 10258 | goto done; |
| 10259 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10260 | if (new_size < 0 || new_size > (PY_SSIZE_T_MAX >> (rkind-1))) { |
| 10261 | PyErr_SetString(PyExc_OverflowError, |
| 10262 | "replace string is too long"); |
| 10263 | goto error; |
| 10264 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10265 | u = PyUnicode_New(new_size, maxchar); |
| 10266 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10267 | goto error; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10268 | assert(PyUnicode_KIND(u) == rkind); |
| 10269 | res = PyUnicode_DATA(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10270 | ires = i = 0; |
| 10271 | if (len1 > 0) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10272 | while (n-- > 0) { |
| 10273 | /* look for next match */ |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10274 | j = anylib_find(rkind, self, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10275 | sbuf + rkind * i, slen-i, |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10276 | str1, buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10277 | if (j == -1) |
| 10278 | break; |
| 10279 | else if (j > i) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10280 | /* copy unchanged part [i:j] */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10281 | memcpy(res + rkind * ires, |
| 10282 | sbuf + rkind * i, |
| 10283 | rkind * (j-i)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10284 | ires += j - i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10285 | } |
| 10286 | /* copy substitution string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10287 | if (len2 > 0) { |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10288 | memcpy(res + rkind * ires, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10289 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10290 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10291 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10292 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10293 | i = j + len1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10294 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10295 | if (i < slen) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10296 | /* copy tail [i:] */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10297 | memcpy(res + rkind * ires, |
| 10298 | sbuf + rkind * i, |
| 10299 | rkind * (slen-i)); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10300 | } |
| 10301 | else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10302 | /* interleave */ |
| 10303 | while (n > 0) { |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10304 | memcpy(res + rkind * ires, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10305 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10306 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10307 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10308 | if (--n <= 0) |
| 10309 | break; |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10310 | memcpy(res + rkind * ires, |
| 10311 | sbuf + rkind * i, |
| 10312 | rkind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10313 | ires++; |
| 10314 | i++; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10315 | } |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10316 | memcpy(res + rkind * ires, |
| 10317 | sbuf + rkind * i, |
| 10318 | rkind * (slen-i)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10319 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10320 | } |
| 10321 | |
| 10322 | if (mayshrink) { |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10323 | unicode_adjust_maxchar(&u); |
| 10324 | if (u == NULL) |
| 10325 | goto error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10326 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10327 | |
| 10328 | done: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10329 | if (srelease) |
| 10330 | PyMem_FREE(sbuf); |
| 10331 | if (release1) |
| 10332 | PyMem_FREE(buf1); |
| 10333 | if (release2) |
| 10334 | PyMem_FREE(buf2); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10335 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10336 | return u; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10337 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10338 | nothing: |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10339 | /* nothing to replace; return original string (when possible) */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10340 | if (srelease) |
| 10341 | PyMem_FREE(sbuf); |
| 10342 | if (release1) |
| 10343 | PyMem_FREE(buf1); |
| 10344 | if (release2) |
| 10345 | PyMem_FREE(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10346 | if (PyUnicode_CheckExact(self)) { |
| 10347 | Py_INCREF(self); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10348 | return self; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10349 | } |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 10350 | return PyUnicode_Copy(self); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10351 | error: |
| 10352 | if (srelease && sbuf) |
| 10353 | PyMem_FREE(sbuf); |
| 10354 | if (release1 && buf1) |
| 10355 | PyMem_FREE(buf1); |
| 10356 | if (release2 && buf2) |
| 10357 | PyMem_FREE(buf2); |
| 10358 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10359 | } |
| 10360 | |
| 10361 | /* --- Unicode Object Methods --------------------------------------------- */ |
| 10362 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10363 | PyDoc_STRVAR(title__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10364 | "S.title() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10365 | \n\ |
| 10366 | 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] | 10367 | characters, all remaining cased characters have 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_title(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, fixtitle); |
| 10373 | } |
| 10374 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10375 | PyDoc_STRVAR(capitalize__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10376 | "S.capitalize() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10377 | \n\ |
| 10378 | 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] | 10379 | have upper case and the rest lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10380 | |
| 10381 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10382 | unicode_capitalize(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10383 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10384 | return fixup(self, fixcapitalize); |
| 10385 | } |
| 10386 | |
| 10387 | #if 0 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10388 | PyDoc_STRVAR(capwords__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10389 | "S.capwords() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10390 | \n\ |
| 10391 | 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] | 10392 | normalized whitespace (all whitespace strings are replaced by ' ')."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10393 | |
| 10394 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10395 | unicode_capwords(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10396 | { |
| 10397 | PyObject *list; |
| 10398 | PyObject *item; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10399 | Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10400 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10401 | /* Split into words */ |
| 10402 | list = split(self, NULL, -1); |
| 10403 | if (!list) |
| 10404 | return NULL; |
| 10405 | |
| 10406 | /* Capitalize each word */ |
| 10407 | for (i = 0; i < PyList_GET_SIZE(list); i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10408 | item = fixup(PyList_GET_ITEM(list, i), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10409 | fixcapitalize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10410 | if (item == NULL) |
| 10411 | goto onError; |
| 10412 | Py_DECREF(PyList_GET_ITEM(list, i)); |
| 10413 | PyList_SET_ITEM(list, i, item); |
| 10414 | } |
| 10415 | |
| 10416 | /* Join the words to form a new string */ |
| 10417 | item = PyUnicode_Join(NULL, list); |
| 10418 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10419 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10420 | Py_DECREF(list); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10421 | return item; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10422 | } |
| 10423 | #endif |
| 10424 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10425 | /* Argument converter. Coerces to a single unicode character */ |
| 10426 | |
| 10427 | static int |
| 10428 | convert_uc(PyObject *obj, void *addr) |
| 10429 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10430 | Py_UCS4 *fillcharloc = (Py_UCS4 *)addr; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10431 | PyObject *uniobj; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10432 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10433 | uniobj = PyUnicode_FromObject(obj); |
| 10434 | if (uniobj == NULL) { |
| 10435 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10436 | "The fill character cannot be converted to Unicode"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10437 | return 0; |
| 10438 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10439 | if (PyUnicode_GET_LENGTH(uniobj) != 1) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10440 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10441 | "The fill character must be exactly one character long"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10442 | Py_DECREF(uniobj); |
| 10443 | return 0; |
| 10444 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10445 | *fillcharloc = PyUnicode_READ_CHAR(uniobj, 0); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10446 | Py_DECREF(uniobj); |
| 10447 | return 1; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10448 | } |
| 10449 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10450 | PyDoc_STRVAR(center__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10451 | "S.center(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10452 | \n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 10453 | Return S centered in a string of length width. Padding is\n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10454 | done using the specified fill character (default is a space)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10455 | |
| 10456 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10457 | unicode_center(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10458 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10459 | Py_ssize_t marg, left; |
| 10460 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10461 | Py_UCS4 fillchar = ' '; |
| 10462 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 10463 | 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] | 10464 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10465 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 10466 | if (PyUnicode_READY(self) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10467 | return NULL; |
| 10468 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10469 | if (_PyUnicode_LENGTH(self) >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10470 | Py_INCREF(self); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10471 | return self; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10472 | } |
| 10473 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10474 | marg = width - _PyUnicode_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10475 | left = marg / 2 + (marg & width & 1); |
| 10476 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10477 | return pad(self, left, marg - left, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10478 | } |
| 10479 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10480 | /* This function assumes that str1 and str2 are readied by the caller. */ |
| 10481 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10482 | static int |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10483 | unicode_compare(PyObject *str1, PyObject *str2) |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10484 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10485 | int kind1, kind2; |
| 10486 | void *data1, *data2; |
| 10487 | Py_ssize_t len1, len2, i; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10488 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10489 | kind1 = PyUnicode_KIND(str1); |
| 10490 | kind2 = PyUnicode_KIND(str2); |
| 10491 | data1 = PyUnicode_DATA(str1); |
| 10492 | data2 = PyUnicode_DATA(str2); |
| 10493 | len1 = PyUnicode_GET_LENGTH(str1); |
| 10494 | len2 = PyUnicode_GET_LENGTH(str2); |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10495 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10496 | for (i = 0; i < len1 && i < len2; ++i) { |
| 10497 | Py_UCS4 c1, c2; |
| 10498 | c1 = PyUnicode_READ(kind1, data1, i); |
| 10499 | c2 = PyUnicode_READ(kind2, data2, i); |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 10500 | |
| 10501 | if (c1 != c2) |
| 10502 | return (c1 < c2) ? -1 : 1; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10503 | } |
| 10504 | |
| 10505 | return (len1 < len2) ? -1 : (len1 != len2); |
| 10506 | } |
| 10507 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10508 | int |
| 10509 | PyUnicode_Compare(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10510 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10511 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 10512 | if (PyUnicode_READY(left) == -1 || |
| 10513 | PyUnicode_READY(right) == -1) |
| 10514 | return -1; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10515 | return unicode_compare(left, right); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10516 | } |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 10517 | PyErr_Format(PyExc_TypeError, |
| 10518 | "Can't compare %.100s and %.100s", |
| 10519 | left->ob_type->tp_name, |
| 10520 | right->ob_type->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10521 | return -1; |
| 10522 | } |
| 10523 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10524 | int |
| 10525 | PyUnicode_CompareWithASCIIString(PyObject* uni, const char* str) |
| 10526 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10527 | Py_ssize_t i; |
| 10528 | int kind; |
| 10529 | void *data; |
| 10530 | Py_UCS4 chr; |
| 10531 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 10532 | assert(_PyUnicode_CHECK(uni)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10533 | if (PyUnicode_READY(uni) == -1) |
| 10534 | return -1; |
| 10535 | kind = PyUnicode_KIND(uni); |
| 10536 | data = PyUnicode_DATA(uni); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10537 | /* Compare Unicode string and source character set string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10538 | for (i = 0; (chr = PyUnicode_READ(kind, data, i)) && str[i]; i++) |
| 10539 | if (chr != str[i]) |
| 10540 | return (chr < (unsigned char)(str[i])) ? -1 : 1; |
Benjamin Peterson | 8667a9b | 2010-01-09 21:45:28 +0000 | [diff] [blame] | 10541 | /* This check keeps Python strings that end in '\0' from comparing equal |
| 10542 | to C strings identical up to that point. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10543 | if (PyUnicode_GET_LENGTH(uni) != i || chr) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10544 | return 1; /* uni is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10545 | if (str[i]) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10546 | return -1; /* str is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10547 | return 0; |
| 10548 | } |
| 10549 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10550 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10551 | #define TEST_COND(cond) \ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10552 | ((cond) ? Py_True : Py_False) |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10553 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10554 | PyObject * |
| 10555 | PyUnicode_RichCompare(PyObject *left, PyObject *right, int op) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10556 | { |
| 10557 | int result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10558 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10559 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 10560 | PyObject *v; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10561 | if (PyUnicode_READY(left) == -1 || |
| 10562 | PyUnicode_READY(right) == -1) |
| 10563 | return NULL; |
| 10564 | if (PyUnicode_GET_LENGTH(left) != PyUnicode_GET_LENGTH(right) || |
| 10565 | PyUnicode_KIND(left) != PyUnicode_KIND(right)) { |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10566 | if (op == Py_EQ) { |
| 10567 | Py_INCREF(Py_False); |
| 10568 | return Py_False; |
| 10569 | } |
| 10570 | if (op == Py_NE) { |
| 10571 | Py_INCREF(Py_True); |
| 10572 | return Py_True; |
| 10573 | } |
| 10574 | } |
| 10575 | if (left == right) |
| 10576 | result = 0; |
| 10577 | else |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10578 | result = unicode_compare(left, right); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10579 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10580 | /* Convert the return value to a Boolean */ |
| 10581 | switch (op) { |
| 10582 | case Py_EQ: |
| 10583 | v = TEST_COND(result == 0); |
| 10584 | break; |
| 10585 | case Py_NE: |
| 10586 | v = TEST_COND(result != 0); |
| 10587 | break; |
| 10588 | case Py_LE: |
| 10589 | v = TEST_COND(result <= 0); |
| 10590 | break; |
| 10591 | case Py_GE: |
| 10592 | v = TEST_COND(result >= 0); |
| 10593 | break; |
| 10594 | case Py_LT: |
| 10595 | v = TEST_COND(result == -1); |
| 10596 | break; |
| 10597 | case Py_GT: |
| 10598 | v = TEST_COND(result == 1); |
| 10599 | break; |
| 10600 | default: |
| 10601 | PyErr_BadArgument(); |
| 10602 | return NULL; |
| 10603 | } |
| 10604 | Py_INCREF(v); |
| 10605 | return v; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10606 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10607 | |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 10608 | Py_RETURN_NOTIMPLEMENTED; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10609 | } |
| 10610 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10611 | int |
| 10612 | PyUnicode_Contains(PyObject *container, PyObject *element) |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10613 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10614 | PyObject *str, *sub; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10615 | int kind1, kind2, kind; |
| 10616 | void *buf1, *buf2; |
| 10617 | Py_ssize_t len1, len2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10618 | int result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10619 | |
| 10620 | /* Coerce the two arguments */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10621 | sub = PyUnicode_FromObject(element); |
| 10622 | if (!sub) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10623 | PyErr_Format(PyExc_TypeError, |
| 10624 | "'in <string>' requires string as left operand, not %s", |
| 10625 | element->ob_type->tp_name); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10626 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10627 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10628 | if (PyUnicode_READY(sub) == -1) |
| 10629 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10630 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10631 | str = PyUnicode_FromObject(container); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 10632 | if (!str || PyUnicode_READY(str) == -1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10633 | Py_DECREF(sub); |
| 10634 | return -1; |
| 10635 | } |
| 10636 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10637 | kind1 = PyUnicode_KIND(str); |
| 10638 | kind2 = PyUnicode_KIND(sub); |
| 10639 | kind = kind1 > kind2 ? kind1 : kind2; |
| 10640 | buf1 = PyUnicode_DATA(str); |
| 10641 | buf2 = PyUnicode_DATA(sub); |
| 10642 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10643 | buf1 = _PyUnicode_AsKind(str, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10644 | if (!buf1) { |
| 10645 | Py_DECREF(sub); |
| 10646 | return -1; |
| 10647 | } |
| 10648 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10649 | buf2 = _PyUnicode_AsKind(sub, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10650 | if (!buf2) { |
| 10651 | Py_DECREF(sub); |
| 10652 | if (kind1 != kind) PyMem_Free(buf1); |
| 10653 | return -1; |
| 10654 | } |
| 10655 | len1 = PyUnicode_GET_LENGTH(str); |
| 10656 | len2 = PyUnicode_GET_LENGTH(sub); |
| 10657 | |
| 10658 | switch(kind) { |
| 10659 | case PyUnicode_1BYTE_KIND: |
| 10660 | result = ucs1lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10661 | break; |
| 10662 | case PyUnicode_2BYTE_KIND: |
| 10663 | result = ucs2lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10664 | break; |
| 10665 | case PyUnicode_4BYTE_KIND: |
| 10666 | result = ucs4lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10667 | break; |
| 10668 | default: |
| 10669 | result = -1; |
| 10670 | assert(0); |
| 10671 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10672 | |
| 10673 | Py_DECREF(str); |
| 10674 | Py_DECREF(sub); |
| 10675 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10676 | if (kind1 != kind) |
| 10677 | PyMem_Free(buf1); |
| 10678 | if (kind2 != kind) |
| 10679 | PyMem_Free(buf2); |
| 10680 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10681 | return result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10682 | } |
| 10683 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10684 | /* Concat to string or Unicode object giving a new Unicode object. */ |
| 10685 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10686 | PyObject * |
| 10687 | PyUnicode_Concat(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10688 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10689 | PyObject *u = NULL, *v = NULL, *w; |
Victor Stinner | 127226b | 2011-10-13 01:12:34 +0200 | [diff] [blame] | 10690 | Py_UCS4 maxchar, maxchar2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10691 | |
| 10692 | /* Coerce the two arguments */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10693 | u = PyUnicode_FromObject(left); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10694 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10695 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10696 | v = PyUnicode_FromObject(right); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10697 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10698 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10699 | |
| 10700 | /* Shortcuts */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 10701 | if (v == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10702 | Py_DECREF(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10703 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10704 | } |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 10705 | if (u == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10706 | Py_DECREF(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10707 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10708 | } |
| 10709 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10710 | maxchar = PyUnicode_MAX_CHAR_VALUE(u); |
Victor Stinner | 127226b | 2011-10-13 01:12:34 +0200 | [diff] [blame] | 10711 | maxchar2 = PyUnicode_MAX_CHAR_VALUE(v); |
| 10712 | maxchar = Py_MAX(maxchar, maxchar2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10713 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10714 | /* Concat the two Unicode strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10715 | w = PyUnicode_New( |
| 10716 | PyUnicode_GET_LENGTH(u) + PyUnicode_GET_LENGTH(v), |
| 10717 | maxchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10718 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10719 | goto onError; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 10720 | copy_characters(w, 0, u, 0, PyUnicode_GET_LENGTH(u)); |
| 10721 | 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] | 10722 | Py_DECREF(u); |
| 10723 | Py_DECREF(v); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10724 | assert(_PyUnicode_CheckConsistency(w, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10725 | return w; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10726 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10727 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10728 | Py_XDECREF(u); |
| 10729 | Py_XDECREF(v); |
| 10730 | return NULL; |
| 10731 | } |
| 10732 | |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10733 | static void |
| 10734 | unicode_append_inplace(PyObject **p_left, PyObject *right) |
| 10735 | { |
| 10736 | Py_ssize_t left_len, right_len, new_len; |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10737 | |
| 10738 | assert(PyUnicode_IS_READY(*p_left)); |
| 10739 | assert(PyUnicode_IS_READY(right)); |
| 10740 | |
| 10741 | left_len = PyUnicode_GET_LENGTH(*p_left); |
| 10742 | right_len = PyUnicode_GET_LENGTH(right); |
| 10743 | if (left_len > PY_SSIZE_T_MAX - right_len) { |
| 10744 | PyErr_SetString(PyExc_OverflowError, |
| 10745 | "strings are too large to concat"); |
| 10746 | goto error; |
| 10747 | } |
| 10748 | new_len = left_len + right_len; |
| 10749 | |
| 10750 | /* Now we own the last reference to 'left', so we can resize it |
| 10751 | * in-place. |
| 10752 | */ |
| 10753 | if (unicode_resize(p_left, new_len) != 0) { |
| 10754 | /* XXX if _PyUnicode_Resize() fails, 'left' has been |
| 10755 | * deallocated so it cannot be put back into |
| 10756 | * 'variable'. The MemoryError is raised when there |
| 10757 | * is no value in 'variable', which might (very |
| 10758 | * remotely) be a cause of incompatibilities. |
| 10759 | */ |
| 10760 | goto error; |
| 10761 | } |
| 10762 | /* copy 'right' into the newly allocated area of 'left' */ |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 10763 | copy_characters(*p_left, left_len, right, 0, right_len); |
| 10764 | _PyUnicode_DIRTY(*p_left); |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10765 | return; |
| 10766 | |
| 10767 | error: |
| 10768 | Py_DECREF(*p_left); |
| 10769 | *p_left = NULL; |
| 10770 | } |
| 10771 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10772 | void |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10773 | PyUnicode_Append(PyObject **p_left, PyObject *right) |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10774 | { |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10775 | PyObject *left, *res; |
| 10776 | |
| 10777 | if (p_left == NULL) { |
| 10778 | if (!PyErr_Occurred()) |
| 10779 | PyErr_BadInternalCall(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10780 | return; |
| 10781 | } |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10782 | left = *p_left; |
| 10783 | if (right == NULL || !PyUnicode_Check(left)) { |
| 10784 | if (!PyErr_Occurred()) |
| 10785 | PyErr_BadInternalCall(); |
| 10786 | goto error; |
| 10787 | } |
| 10788 | |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 10789 | if (PyUnicode_READY(left)) |
| 10790 | goto error; |
| 10791 | if (PyUnicode_READY(right)) |
| 10792 | goto error; |
| 10793 | |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10794 | if (PyUnicode_CheckExact(left) && left != unicode_empty |
| 10795 | && PyUnicode_CheckExact(right) && right != unicode_empty |
| 10796 | && unicode_resizable(left) |
| 10797 | && (_PyUnicode_KIND(right) <= _PyUnicode_KIND(left) |
| 10798 | || _PyUnicode_WSTR(left) != NULL)) |
| 10799 | { |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10800 | /* Don't resize for ascii += latin1. Convert ascii to latin1 requires |
| 10801 | to change the structure size, but characters are stored just after |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 10802 | the structure, and so it requires to move all characters which is |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10803 | not so different than duplicating the string. */ |
| 10804 | if (!(PyUnicode_IS_ASCII(left) && !PyUnicode_IS_ASCII(right))) |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10805 | { |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10806 | unicode_append_inplace(p_left, right); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10807 | if (p_left != NULL) |
| 10808 | assert(_PyUnicode_CheckConsistency(*p_left, 1)); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10809 | return; |
| 10810 | } |
| 10811 | } |
| 10812 | |
| 10813 | res = PyUnicode_Concat(left, right); |
| 10814 | if (res == NULL) |
| 10815 | goto error; |
| 10816 | Py_DECREF(left); |
| 10817 | *p_left = res; |
| 10818 | return; |
| 10819 | |
| 10820 | error: |
| 10821 | Py_DECREF(*p_left); |
| 10822 | *p_left = NULL; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10823 | } |
| 10824 | |
| 10825 | void |
| 10826 | PyUnicode_AppendAndDel(PyObject **pleft, PyObject *right) |
| 10827 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10828 | PyUnicode_Append(pleft, right); |
| 10829 | Py_XDECREF(right); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10830 | } |
| 10831 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10832 | PyDoc_STRVAR(count__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10833 | "S.count(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10834 | \n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10835 | Return the number of non-overlapping occurrences of substring sub in\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 10836 | 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] | 10837 | interpreted as in slice notation."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10838 | |
| 10839 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10840 | unicode_count(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10841 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10842 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10843 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10844 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10845 | PyObject *result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10846 | int kind1, kind2, kind; |
| 10847 | void *buf1, *buf2; |
| 10848 | Py_ssize_t len1, len2, iresult; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10849 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10850 | if (!stringlib_parse_args_finds_unicode("count", args, &substring, |
| 10851 | &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10852 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 10853 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10854 | kind1 = PyUnicode_KIND(self); |
| 10855 | kind2 = PyUnicode_KIND(substring); |
| 10856 | kind = kind1 > kind2 ? kind1 : kind2; |
| 10857 | buf1 = PyUnicode_DATA(self); |
| 10858 | buf2 = PyUnicode_DATA(substring); |
| 10859 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10860 | buf1 = _PyUnicode_AsKind(self, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10861 | if (!buf1) { |
| 10862 | Py_DECREF(substring); |
| 10863 | return NULL; |
| 10864 | } |
| 10865 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10866 | buf2 = _PyUnicode_AsKind(substring, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10867 | if (!buf2) { |
| 10868 | Py_DECREF(substring); |
| 10869 | if (kind1 != kind) PyMem_Free(buf1); |
| 10870 | return NULL; |
| 10871 | } |
| 10872 | len1 = PyUnicode_GET_LENGTH(self); |
| 10873 | len2 = PyUnicode_GET_LENGTH(substring); |
| 10874 | |
| 10875 | ADJUST_INDICES(start, end, len1); |
| 10876 | switch(kind) { |
| 10877 | case PyUnicode_1BYTE_KIND: |
| 10878 | iresult = ucs1lib_count( |
| 10879 | ((Py_UCS1*)buf1) + start, end - start, |
| 10880 | buf2, len2, PY_SSIZE_T_MAX |
| 10881 | ); |
| 10882 | break; |
| 10883 | case PyUnicode_2BYTE_KIND: |
| 10884 | iresult = ucs2lib_count( |
| 10885 | ((Py_UCS2*)buf1) + start, end - start, |
| 10886 | buf2, len2, PY_SSIZE_T_MAX |
| 10887 | ); |
| 10888 | break; |
| 10889 | case PyUnicode_4BYTE_KIND: |
| 10890 | iresult = ucs4lib_count( |
| 10891 | ((Py_UCS4*)buf1) + start, end - start, |
| 10892 | buf2, len2, PY_SSIZE_T_MAX |
| 10893 | ); |
| 10894 | break; |
| 10895 | default: |
| 10896 | assert(0); iresult = 0; |
| 10897 | } |
| 10898 | |
| 10899 | result = PyLong_FromSsize_t(iresult); |
| 10900 | |
| 10901 | if (kind1 != kind) |
| 10902 | PyMem_Free(buf1); |
| 10903 | if (kind2 != kind) |
| 10904 | PyMem_Free(buf2); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10905 | |
| 10906 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10907 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10908 | return result; |
| 10909 | } |
| 10910 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10911 | PyDoc_STRVAR(encode__doc__, |
Victor Stinner | c911bbf | 2010-11-07 19:04:46 +0000 | [diff] [blame] | 10912 | "S.encode(encoding='utf-8', errors='strict') -> bytes\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10913 | \n\ |
Victor Stinner | e14e212 | 2010-11-07 18:41:46 +0000 | [diff] [blame] | 10914 | Encode S using the codec registered for encoding. Default encoding\n\ |
| 10915 | 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] | 10916 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 10917 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 10918 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 10919 | codecs.register_error that can handle UnicodeEncodeErrors."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10920 | |
| 10921 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10922 | unicode_encode(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10923 | { |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 10924 | static char *kwlist[] = {"encoding", "errors", 0}; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10925 | char *encoding = NULL; |
| 10926 | char *errors = NULL; |
Guido van Rossum | 35d9428 | 2007-08-27 18:20:11 +0000 | [diff] [blame] | 10927 | |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 10928 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:encode", |
| 10929 | kwlist, &encoding, &errors)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10930 | return NULL; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10931 | return PyUnicode_AsEncodedString(self, encoding, errors); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 10932 | } |
| 10933 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10934 | PyDoc_STRVAR(expandtabs__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10935 | "S.expandtabs([tabsize]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10936 | \n\ |
| 10937 | 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] | 10938 | 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] | 10939 | |
| 10940 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10941 | unicode_expandtabs(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10942 | { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10943 | Py_ssize_t i, j, line_pos, src_len, incr; |
| 10944 | Py_UCS4 ch; |
| 10945 | PyObject *u; |
| 10946 | void *src_data, *dest_data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10947 | int tabsize = 8; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10948 | int kind; |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 10949 | int found; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10950 | |
| 10951 | if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10952 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10953 | |
Antoine Pitrou | 2242522 | 2011-10-04 19:10:51 +0200 | [diff] [blame] | 10954 | if (PyUnicode_READY(self) == -1) |
| 10955 | return NULL; |
| 10956 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 10957 | /* First pass: determine size of output string */ |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10958 | src_len = PyUnicode_GET_LENGTH(self); |
| 10959 | i = j = line_pos = 0; |
| 10960 | kind = PyUnicode_KIND(self); |
| 10961 | src_data = PyUnicode_DATA(self); |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 10962 | found = 0; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10963 | for (; i < src_len; i++) { |
| 10964 | ch = PyUnicode_READ(kind, src_data, i); |
| 10965 | if (ch == '\t') { |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 10966 | found = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10967 | if (tabsize > 0) { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10968 | incr = tabsize - (line_pos % tabsize); /* cannot overflow */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10969 | if (j > PY_SSIZE_T_MAX - incr) |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10970 | goto overflow; |
| 10971 | line_pos += incr; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10972 | j += incr; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10973 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10974 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10975 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10976 | if (j > PY_SSIZE_T_MAX - 1) |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10977 | goto overflow; |
| 10978 | line_pos++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10979 | j++; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10980 | if (ch == '\n' || ch == '\r') |
| 10981 | line_pos = 0; |
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 | } |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 10984 | if (!found && PyUnicode_CheckExact(self)) { |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10985 | Py_INCREF(self); |
| 10986 | return self; |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 10987 | } |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 10988 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10989 | /* Second pass: create output string and fill it */ |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10990 | u = PyUnicode_New(j, PyUnicode_MAX_CHAR_VALUE(self)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10991 | if (!u) |
| 10992 | return NULL; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10993 | dest_data = PyUnicode_DATA(u); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10994 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10995 | i = j = line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10996 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10997 | for (; i < src_len; i++) { |
| 10998 | ch = PyUnicode_READ(kind, src_data, i); |
| 10999 | if (ch == '\t') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11000 | if (tabsize > 0) { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11001 | incr = tabsize - (line_pos % tabsize); |
| 11002 | line_pos += incr; |
| 11003 | while (incr--) { |
| 11004 | PyUnicode_WRITE(kind, dest_data, j, ' '); |
| 11005 | j++; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11006 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11007 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11008 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11009 | else { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11010 | line_pos++; |
| 11011 | PyUnicode_WRITE(kind, dest_data, j, ch); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11012 | j++; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11013 | if (ch == '\n' || ch == '\r') |
| 11014 | line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11015 | } |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11016 | } |
| 11017 | assert (j == PyUnicode_GET_LENGTH(u)); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 11018 | #ifndef DONT_MAKE_RESULT_READY |
| 11019 | if (_PyUnicode_READY_REPLACE(&u)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11020 | Py_DECREF(u); |
| 11021 | return NULL; |
| 11022 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 11023 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 11024 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11025 | return u; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11026 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11027 | overflow: |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11028 | PyErr_SetString(PyExc_OverflowError, "new string is too long"); |
| 11029 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11030 | } |
| 11031 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11032 | PyDoc_STRVAR(find__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11033 | "S.find(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11034 | \n\ |
| 11035 | Return the lowest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 11036 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11037 | arguments start and end are interpreted as in slice notation.\n\ |
| 11038 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11039 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11040 | |
| 11041 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11042 | unicode_find(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11043 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11044 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 11045 | Py_ssize_t start; |
| 11046 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11047 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11048 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11049 | if (!stringlib_parse_args_finds_unicode("find", args, &substring, |
| 11050 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11051 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11052 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11053 | if (PyUnicode_READY(self) == -1) |
| 11054 | return NULL; |
| 11055 | if (PyUnicode_READY(substring) == -1) |
| 11056 | return NULL; |
| 11057 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11058 | result = any_find_slice(1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11059 | |
| 11060 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11061 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11062 | if (result == -2) |
| 11063 | return NULL; |
| 11064 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 11065 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11066 | } |
| 11067 | |
| 11068 | static PyObject * |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 11069 | unicode_getitem(PyObject *self, Py_ssize_t index) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11070 | { |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 11071 | Py_UCS4 ch = PyUnicode_ReadChar(self, index); |
| 11072 | if (ch == (Py_UCS4)-1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11073 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11074 | return PyUnicode_FromOrdinal(ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11075 | } |
| 11076 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11077 | /* Believe it or not, this produces the same value for ASCII strings |
Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 11078 | as bytes_hash(). */ |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 11079 | static Py_hash_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11080 | unicode_hash(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11081 | { |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11082 | Py_ssize_t len; |
Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 11083 | Py_uhash_t x; |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11084 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11085 | if (_PyUnicode_HASH(self) != -1) |
| 11086 | return _PyUnicode_HASH(self); |
| 11087 | if (PyUnicode_READY(self) == -1) |
| 11088 | return -1; |
| 11089 | len = PyUnicode_GET_LENGTH(self); |
| 11090 | |
| 11091 | /* The hash function as a macro, gets expanded three times below. */ |
| 11092 | #define HASH(P) \ |
| 11093 | x = (Py_uhash_t)*P << 7; \ |
| 11094 | while (--len >= 0) \ |
| 11095 | x = (1000003*x) ^ (Py_uhash_t)*P++; |
| 11096 | |
| 11097 | switch (PyUnicode_KIND(self)) { |
| 11098 | case PyUnicode_1BYTE_KIND: { |
| 11099 | const unsigned char *c = PyUnicode_1BYTE_DATA(self); |
| 11100 | HASH(c); |
| 11101 | break; |
| 11102 | } |
| 11103 | case PyUnicode_2BYTE_KIND: { |
| 11104 | const Py_UCS2 *s = PyUnicode_2BYTE_DATA(self); |
| 11105 | HASH(s); |
| 11106 | break; |
| 11107 | } |
| 11108 | default: { |
| 11109 | Py_UCS4 *l; |
| 11110 | assert(PyUnicode_KIND(self) == PyUnicode_4BYTE_KIND && |
| 11111 | "Impossible switch case in unicode_hash"); |
| 11112 | l = PyUnicode_4BYTE_DATA(self); |
| 11113 | HASH(l); |
| 11114 | break; |
| 11115 | } |
| 11116 | } |
| 11117 | x ^= (Py_uhash_t)PyUnicode_GET_LENGTH(self); |
| 11118 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11119 | if (x == -1) |
| 11120 | x = -2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11121 | _PyUnicode_HASH(self) = x; |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11122 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11123 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11124 | #undef HASH |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11125 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11126 | PyDoc_STRVAR(index__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11127 | "S.index(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11128 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11129 | 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] | 11130 | |
| 11131 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11132 | unicode_index(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11133 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11134 | Py_ssize_t result; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11135 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 11136 | Py_ssize_t start; |
| 11137 | Py_ssize_t end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11138 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11139 | if (!stringlib_parse_args_finds_unicode("index", args, &substring, |
| 11140 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11141 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11142 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11143 | if (PyUnicode_READY(self) == -1) |
| 11144 | return NULL; |
| 11145 | if (PyUnicode_READY(substring) == -1) |
| 11146 | return NULL; |
| 11147 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11148 | result = any_find_slice(1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11149 | |
| 11150 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11151 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11152 | if (result == -2) |
| 11153 | return NULL; |
| 11154 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11155 | if (result < 0) { |
| 11156 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 11157 | return NULL; |
| 11158 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11159 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 11160 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11161 | } |
| 11162 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11163 | PyDoc_STRVAR(islower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11164 | "S.islower() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11165 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11166 | 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] | 11167 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11168 | |
| 11169 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11170 | unicode_islower(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11171 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11172 | Py_ssize_t i, length; |
| 11173 | int kind; |
| 11174 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11175 | int cased; |
| 11176 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11177 | if (PyUnicode_READY(self) == -1) |
| 11178 | return NULL; |
| 11179 | length = PyUnicode_GET_LENGTH(self); |
| 11180 | kind = PyUnicode_KIND(self); |
| 11181 | data = PyUnicode_DATA(self); |
| 11182 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11183 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11184 | if (length == 1) |
| 11185 | return PyBool_FromLong( |
| 11186 | Py_UNICODE_ISLOWER(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11187 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11188 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11189 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11190 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11191 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11192 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11193 | for (i = 0; i < length; i++) { |
| 11194 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11195 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11196 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 11197 | return PyBool_FromLong(0); |
| 11198 | else if (!cased && Py_UNICODE_ISLOWER(ch)) |
| 11199 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11200 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11201 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11202 | } |
| 11203 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11204 | PyDoc_STRVAR(isupper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11205 | "S.isupper() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11206 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11207 | 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] | 11208 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11209 | |
| 11210 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11211 | unicode_isupper(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11212 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11213 | Py_ssize_t i, length; |
| 11214 | int kind; |
| 11215 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11216 | int cased; |
| 11217 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11218 | if (PyUnicode_READY(self) == -1) |
| 11219 | return NULL; |
| 11220 | length = PyUnicode_GET_LENGTH(self); |
| 11221 | kind = PyUnicode_KIND(self); |
| 11222 | data = PyUnicode_DATA(self); |
| 11223 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11224 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11225 | if (length == 1) |
| 11226 | return PyBool_FromLong( |
| 11227 | Py_UNICODE_ISUPPER(PyUnicode_READ(kind, data, 0)) != 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11228 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11229 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11230 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11231 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11232 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11233 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11234 | for (i = 0; i < length; i++) { |
| 11235 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11236 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11237 | if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 11238 | return PyBool_FromLong(0); |
| 11239 | else if (!cased && Py_UNICODE_ISUPPER(ch)) |
| 11240 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11241 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11242 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11243 | } |
| 11244 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11245 | PyDoc_STRVAR(istitle__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11246 | "S.istitle() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11247 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11248 | Return True if S is a titlecased string and there is at least one\n\ |
| 11249 | character in S, i.e. upper- and titlecase characters may only\n\ |
| 11250 | follow uncased characters and lowercase characters only cased ones.\n\ |
| 11251 | Return False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11252 | |
| 11253 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11254 | unicode_istitle(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11255 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11256 | Py_ssize_t i, length; |
| 11257 | int kind; |
| 11258 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11259 | int cased, previous_is_cased; |
| 11260 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11261 | if (PyUnicode_READY(self) == -1) |
| 11262 | return NULL; |
| 11263 | length = PyUnicode_GET_LENGTH(self); |
| 11264 | kind = PyUnicode_KIND(self); |
| 11265 | data = PyUnicode_DATA(self); |
| 11266 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11267 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11268 | if (length == 1) { |
| 11269 | Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11270 | return PyBool_FromLong((Py_UNICODE_ISTITLE(ch) != 0) || |
| 11271 | (Py_UNICODE_ISUPPER(ch) != 0)); |
| 11272 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11273 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11274 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11275 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11276 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11277 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11278 | cased = 0; |
| 11279 | previous_is_cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11280 | for (i = 0; i < length; i++) { |
| 11281 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11282 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11283 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { |
| 11284 | if (previous_is_cased) |
| 11285 | return PyBool_FromLong(0); |
| 11286 | previous_is_cased = 1; |
| 11287 | cased = 1; |
| 11288 | } |
| 11289 | else if (Py_UNICODE_ISLOWER(ch)) { |
| 11290 | if (!previous_is_cased) |
| 11291 | return PyBool_FromLong(0); |
| 11292 | previous_is_cased = 1; |
| 11293 | cased = 1; |
| 11294 | } |
| 11295 | else |
| 11296 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11297 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11298 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11299 | } |
| 11300 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11301 | PyDoc_STRVAR(isspace__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11302 | "S.isspace() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11303 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11304 | Return True if all characters in S are whitespace\n\ |
| 11305 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11306 | |
| 11307 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11308 | unicode_isspace(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11309 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11310 | Py_ssize_t i, length; |
| 11311 | int kind; |
| 11312 | void *data; |
| 11313 | |
| 11314 | if (PyUnicode_READY(self) == -1) |
| 11315 | return NULL; |
| 11316 | length = PyUnicode_GET_LENGTH(self); |
| 11317 | kind = PyUnicode_KIND(self); |
| 11318 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11319 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11320 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11321 | if (length == 1) |
| 11322 | return PyBool_FromLong( |
| 11323 | Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11324 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11325 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11326 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11327 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11328 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11329 | for (i = 0; i < length; i++) { |
| 11330 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11331 | if (!Py_UNICODE_ISSPACE(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11332 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11333 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11334 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11335 | } |
| 11336 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11337 | PyDoc_STRVAR(isalpha__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11338 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11339 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11340 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11341 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11342 | |
| 11343 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11344 | unicode_isalpha(PyObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11345 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11346 | Py_ssize_t i, length; |
| 11347 | int kind; |
| 11348 | void *data; |
| 11349 | |
| 11350 | if (PyUnicode_READY(self) == -1) |
| 11351 | return NULL; |
| 11352 | length = PyUnicode_GET_LENGTH(self); |
| 11353 | kind = PyUnicode_KIND(self); |
| 11354 | data = PyUnicode_DATA(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11355 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11356 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11357 | if (length == 1) |
| 11358 | return PyBool_FromLong( |
| 11359 | Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, 0))); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11360 | |
| 11361 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11362 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11363 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11364 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11365 | for (i = 0; i < length; i++) { |
| 11366 | if (!Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11367 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11368 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11369 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11370 | } |
| 11371 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11372 | PyDoc_STRVAR(isalnum__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11373 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11374 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11375 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11376 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11377 | |
| 11378 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11379 | unicode_isalnum(PyObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11380 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11381 | int kind; |
| 11382 | void *data; |
| 11383 | Py_ssize_t len, i; |
| 11384 | |
| 11385 | if (PyUnicode_READY(self) == -1) |
| 11386 | return NULL; |
| 11387 | |
| 11388 | kind = PyUnicode_KIND(self); |
| 11389 | data = PyUnicode_DATA(self); |
| 11390 | len = PyUnicode_GET_LENGTH(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11391 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11392 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11393 | if (len == 1) { |
| 11394 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11395 | return PyBool_FromLong(Py_UNICODE_ISALNUM(ch)); |
| 11396 | } |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11397 | |
| 11398 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11399 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11400 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11401 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11402 | for (i = 0; i < len; i++) { |
| 11403 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11404 | if (!Py_UNICODE_ISALNUM(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11405 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11406 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11407 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11408 | } |
| 11409 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11410 | PyDoc_STRVAR(isdecimal__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11411 | "S.isdecimal() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11412 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11413 | 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] | 11414 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11415 | |
| 11416 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11417 | unicode_isdecimal(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11418 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11419 | Py_ssize_t i, length; |
| 11420 | int kind; |
| 11421 | void *data; |
| 11422 | |
| 11423 | if (PyUnicode_READY(self) == -1) |
| 11424 | return NULL; |
| 11425 | length = PyUnicode_GET_LENGTH(self); |
| 11426 | kind = PyUnicode_KIND(self); |
| 11427 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11428 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11429 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11430 | if (length == 1) |
| 11431 | return PyBool_FromLong( |
| 11432 | Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11433 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11434 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11435 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11436 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11437 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11438 | for (i = 0; i < length; i++) { |
| 11439 | if (!Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11440 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11441 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11442 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11443 | } |
| 11444 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11445 | PyDoc_STRVAR(isdigit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11446 | "S.isdigit() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11447 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11448 | Return True if all characters in S are digits\n\ |
| 11449 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11450 | |
| 11451 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11452 | unicode_isdigit(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11453 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11454 | Py_ssize_t i, length; |
| 11455 | int kind; |
| 11456 | void *data; |
| 11457 | |
| 11458 | if (PyUnicode_READY(self) == -1) |
| 11459 | return NULL; |
| 11460 | length = PyUnicode_GET_LENGTH(self); |
| 11461 | kind = PyUnicode_KIND(self); |
| 11462 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11463 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11464 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11465 | if (length == 1) { |
| 11466 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11467 | return PyBool_FromLong(Py_UNICODE_ISDIGIT(ch)); |
| 11468 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11469 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11470 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11471 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11472 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11473 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11474 | for (i = 0; i < length; i++) { |
| 11475 | if (!Py_UNICODE_ISDIGIT(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11476 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11477 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11478 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11479 | } |
| 11480 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11481 | PyDoc_STRVAR(isnumeric__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11482 | "S.isnumeric() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11483 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11484 | 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] | 11485 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11486 | |
| 11487 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11488 | unicode_isnumeric(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11489 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11490 | Py_ssize_t i, length; |
| 11491 | int kind; |
| 11492 | void *data; |
| 11493 | |
| 11494 | if (PyUnicode_READY(self) == -1) |
| 11495 | return NULL; |
| 11496 | length = PyUnicode_GET_LENGTH(self); |
| 11497 | kind = PyUnicode_KIND(self); |
| 11498 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11499 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11500 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11501 | if (length == 1) |
| 11502 | return PyBool_FromLong( |
| 11503 | Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11504 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11505 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11506 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11507 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11508 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11509 | for (i = 0; i < length; i++) { |
| 11510 | if (!Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11511 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11512 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11513 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11514 | } |
| 11515 | |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11516 | int |
| 11517 | PyUnicode_IsIdentifier(PyObject *self) |
| 11518 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11519 | int kind; |
| 11520 | void *data; |
| 11521 | Py_ssize_t i; |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11522 | Py_UCS4 first; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11523 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11524 | if (PyUnicode_READY(self) == -1) { |
| 11525 | Py_FatalError("identifier not ready"); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11526 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11527 | } |
| 11528 | |
| 11529 | /* Special case for empty strings */ |
| 11530 | if (PyUnicode_GET_LENGTH(self) == 0) |
| 11531 | return 0; |
| 11532 | kind = PyUnicode_KIND(self); |
| 11533 | data = PyUnicode_DATA(self); |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11534 | |
| 11535 | /* PEP 3131 says that the first character must be in |
| 11536 | XID_Start and subsequent characters in XID_Continue, |
| 11537 | and for the ASCII range, the 2.x rules apply (i.e |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11538 | start with letters and underscore, continue with |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11539 | letters, digits, underscore). However, given the current |
| 11540 | definition of XID_Start and XID_Continue, it is sufficient |
| 11541 | to check just for these, except that _ must be allowed |
| 11542 | as starting an identifier. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11543 | first = PyUnicode_READ(kind, data, 0); |
Benjamin Peterson | f413b80 | 2011-08-12 22:17:18 -0500 | [diff] [blame] | 11544 | if (!_PyUnicode_IsXidStart(first) && first != 0x5F /* LOW LINE */) |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11545 | return 0; |
| 11546 | |
Benjamin Peterson | 9c6e6a0 | 2011-09-28 08:09:05 -0400 | [diff] [blame] | 11547 | for (i = 1; i < PyUnicode_GET_LENGTH(self); i++) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11548 | if (!_PyUnicode_IsXidContinue(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11549 | return 0; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11550 | return 1; |
| 11551 | } |
| 11552 | |
| 11553 | PyDoc_STRVAR(isidentifier__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11554 | "S.isidentifier() -> bool\n\ |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11555 | \n\ |
| 11556 | Return True if S is a valid identifier according\n\ |
| 11557 | to the language definition."); |
| 11558 | |
| 11559 | static PyObject* |
| 11560 | unicode_isidentifier(PyObject *self) |
| 11561 | { |
| 11562 | return PyBool_FromLong(PyUnicode_IsIdentifier(self)); |
| 11563 | } |
| 11564 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11565 | PyDoc_STRVAR(isprintable__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11566 | "S.isprintable() -> bool\n\ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11567 | \n\ |
| 11568 | Return True if all characters in S are considered\n\ |
| 11569 | printable in repr() or S is empty, False otherwise."); |
| 11570 | |
| 11571 | static PyObject* |
| 11572 | unicode_isprintable(PyObject *self) |
| 11573 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11574 | Py_ssize_t i, length; |
| 11575 | int kind; |
| 11576 | void *data; |
| 11577 | |
| 11578 | if (PyUnicode_READY(self) == -1) |
| 11579 | return NULL; |
| 11580 | length = PyUnicode_GET_LENGTH(self); |
| 11581 | kind = PyUnicode_KIND(self); |
| 11582 | data = PyUnicode_DATA(self); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11583 | |
| 11584 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11585 | if (length == 1) |
| 11586 | return PyBool_FromLong( |
| 11587 | Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, 0))); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11588 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11589 | for (i = 0; i < length; i++) { |
| 11590 | if (!Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, i))) { |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11591 | Py_RETURN_FALSE; |
| 11592 | } |
| 11593 | } |
| 11594 | Py_RETURN_TRUE; |
| 11595 | } |
| 11596 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11597 | PyDoc_STRVAR(join__doc__, |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 11598 | "S.join(iterable) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11599 | \n\ |
| 11600 | 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] | 11601 | iterable. The separator between elements is S."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11602 | |
| 11603 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11604 | unicode_join(PyObject *self, PyObject *data) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11605 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11606 | return PyUnicode_Join(self, data); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11607 | } |
| 11608 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11609 | static Py_ssize_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11610 | unicode_length(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11611 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11612 | if (PyUnicode_READY(self) == -1) |
| 11613 | return -1; |
| 11614 | return PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11615 | } |
| 11616 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11617 | PyDoc_STRVAR(ljust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11618 | "S.ljust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11619 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 11620 | 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] | 11621 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11622 | |
| 11623 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11624 | unicode_ljust(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11625 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11626 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11627 | Py_UCS4 fillchar = ' '; |
| 11628 | |
| 11629 | if (PyUnicode_READY(self) == -1) |
| 11630 | return NULL; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 11631 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11632 | if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11633 | return NULL; |
| 11634 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11635 | if (_PyUnicode_LENGTH(self) >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11636 | Py_INCREF(self); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11637 | return self; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11638 | } |
| 11639 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11640 | return pad(self, 0, width - _PyUnicode_LENGTH(self), fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11641 | } |
| 11642 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11643 | PyDoc_STRVAR(lower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11644 | "S.lower() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11645 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11646 | Return a copy of the string S converted to lowercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11647 | |
| 11648 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11649 | unicode_lower(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11650 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11651 | return fixup(self, fixlower); |
| 11652 | } |
| 11653 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11654 | #define LEFTSTRIP 0 |
| 11655 | #define RIGHTSTRIP 1 |
| 11656 | #define BOTHSTRIP 2 |
| 11657 | |
| 11658 | /* Arrays indexed by above */ |
| 11659 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 11660 | |
| 11661 | #define STRIPNAME(i) (stripformat[i]+3) |
| 11662 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11663 | /* externally visible for str.strip(unicode) */ |
| 11664 | PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11665 | _PyUnicode_XStrip(PyObject *self, int striptype, PyObject *sepobj) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11666 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11667 | void *data; |
| 11668 | int kind; |
| 11669 | Py_ssize_t i, j, len; |
| 11670 | BLOOM_MASK sepmask; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11671 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11672 | if (PyUnicode_READY(self) == -1 || PyUnicode_READY(sepobj) == -1) |
| 11673 | return NULL; |
| 11674 | |
| 11675 | kind = PyUnicode_KIND(self); |
| 11676 | data = PyUnicode_DATA(self); |
| 11677 | len = PyUnicode_GET_LENGTH(self); |
| 11678 | sepmask = make_bloom_mask(PyUnicode_KIND(sepobj), |
| 11679 | PyUnicode_DATA(sepobj), |
| 11680 | PyUnicode_GET_LENGTH(sepobj)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11681 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11682 | i = 0; |
| 11683 | if (striptype != RIGHTSTRIP) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11684 | while (i < len && |
| 11685 | BLOOM_MEMBER(sepmask, PyUnicode_READ(kind, data, i), sepobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11686 | i++; |
| 11687 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11688 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11689 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11690 | j = len; |
| 11691 | if (striptype != LEFTSTRIP) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11692 | do { |
| 11693 | j--; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11694 | } while (j >= i && |
| 11695 | BLOOM_MEMBER(sepmask, PyUnicode_READ(kind, data, j), sepobj)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11696 | j++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11697 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11698 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11699 | return PyUnicode_Substring(self, i, j); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11700 | } |
| 11701 | |
| 11702 | PyObject* |
| 11703 | PyUnicode_Substring(PyObject *self, Py_ssize_t start, Py_ssize_t end) |
| 11704 | { |
| 11705 | unsigned char *data; |
| 11706 | int kind; |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11707 | Py_ssize_t length; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11708 | |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 11709 | if (PyUnicode_READY(self) == -1) |
| 11710 | return NULL; |
| 11711 | |
| 11712 | end = Py_MIN(end, PyUnicode_GET_LENGTH(self)); |
| 11713 | |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11714 | if (start == 0 && end == PyUnicode_GET_LENGTH(self)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11715 | { |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11716 | if (PyUnicode_CheckExact(self)) { |
| 11717 | Py_INCREF(self); |
| 11718 | return self; |
| 11719 | } |
| 11720 | else |
| 11721 | return PyUnicode_Copy(self); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11722 | } |
| 11723 | |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11724 | length = end - start; |
| 11725 | if (length == 1) |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 11726 | return unicode_getitem(self, start); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11727 | |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 11728 | if (start < 0 || end < 0) { |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11729 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 11730 | return NULL; |
| 11731 | } |
| 11732 | |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 11733 | if (PyUnicode_IS_ASCII(self)) { |
| 11734 | kind = PyUnicode_KIND(self); |
| 11735 | data = PyUnicode_1BYTE_DATA(self); |
| 11736 | return unicode_fromascii(data + start, length); |
| 11737 | } |
| 11738 | else { |
| 11739 | kind = PyUnicode_KIND(self); |
| 11740 | data = PyUnicode_1BYTE_DATA(self); |
| 11741 | return PyUnicode_FromKindAndData(kind, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 11742 | data + kind * start, |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 11743 | length); |
| 11744 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11745 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11746 | |
| 11747 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11748 | do_strip(PyObject *self, int striptype) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11749 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11750 | int kind; |
| 11751 | void *data; |
| 11752 | Py_ssize_t len, i, j; |
| 11753 | |
| 11754 | if (PyUnicode_READY(self) == -1) |
| 11755 | return NULL; |
| 11756 | |
| 11757 | kind = PyUnicode_KIND(self); |
| 11758 | data = PyUnicode_DATA(self); |
| 11759 | len = PyUnicode_GET_LENGTH(self); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11760 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11761 | i = 0; |
| 11762 | if (striptype != RIGHTSTRIP) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11763 | while (i < len && Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, i))) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11764 | i++; |
| 11765 | } |
| 11766 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11767 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11768 | j = len; |
| 11769 | if (striptype != LEFTSTRIP) { |
| 11770 | do { |
| 11771 | j--; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11772 | } while (j >= i && Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, j))); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11773 | j++; |
| 11774 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11775 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11776 | return PyUnicode_Substring(self, i, j); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11777 | } |
| 11778 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11779 | |
| 11780 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11781 | do_argstrip(PyObject *self, int striptype, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11782 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11783 | PyObject *sep = NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11784 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11785 | if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) |
| 11786 | return NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11787 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11788 | if (sep != NULL && sep != Py_None) { |
| 11789 | if (PyUnicode_Check(sep)) |
| 11790 | return _PyUnicode_XStrip(self, striptype, sep); |
| 11791 | else { |
| 11792 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11793 | "%s arg must be None or str", |
| 11794 | STRIPNAME(striptype)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11795 | return NULL; |
| 11796 | } |
| 11797 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11798 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11799 | return do_strip(self, striptype); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11800 | } |
| 11801 | |
| 11802 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11803 | PyDoc_STRVAR(strip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11804 | "S.strip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11805 | \n\ |
| 11806 | Return a copy of the string S with leading and trailing\n\ |
| 11807 | whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11808 | 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] | 11809 | |
| 11810 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11811 | unicode_strip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11812 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11813 | if (PyTuple_GET_SIZE(args) == 0) |
| 11814 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 11815 | else |
| 11816 | return do_argstrip(self, BOTHSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11817 | } |
| 11818 | |
| 11819 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11820 | PyDoc_STRVAR(lstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11821 | "S.lstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11822 | \n\ |
| 11823 | Return a copy of the string S with leading whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11824 | 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] | 11825 | |
| 11826 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11827 | unicode_lstrip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11828 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11829 | if (PyTuple_GET_SIZE(args) == 0) |
| 11830 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 11831 | else |
| 11832 | return do_argstrip(self, LEFTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11833 | } |
| 11834 | |
| 11835 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11836 | PyDoc_STRVAR(rstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11837 | "S.rstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11838 | \n\ |
| 11839 | Return a copy of the string S with trailing whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11840 | 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] | 11841 | |
| 11842 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11843 | unicode_rstrip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11844 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11845 | if (PyTuple_GET_SIZE(args) == 0) |
| 11846 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 11847 | else |
| 11848 | return do_argstrip(self, RIGHTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11849 | } |
| 11850 | |
| 11851 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11852 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11853 | unicode_repeat(PyObject *str, Py_ssize_t len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11854 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11855 | PyObject *u; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11856 | Py_ssize_t nchars, n; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11857 | |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 11858 | if (len < 1) { |
| 11859 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 11860 | return unicode_empty; |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 11861 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11862 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 11863 | if (len == 1 && PyUnicode_CheckExact(str)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11864 | /* no repeat, return original string */ |
| 11865 | Py_INCREF(str); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11866 | return str; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11867 | } |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 11868 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11869 | if (PyUnicode_READY(str) == -1) |
| 11870 | return NULL; |
| 11871 | |
Victor Stinner | c759f3e | 2011-10-01 03:09:58 +0200 | [diff] [blame] | 11872 | if (PyUnicode_GET_LENGTH(str) > PY_SSIZE_T_MAX / len) { |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11873 | PyErr_SetString(PyExc_OverflowError, |
| 11874 | "repeated string is too long"); |
| 11875 | return NULL; |
| 11876 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11877 | nchars = len * PyUnicode_GET_LENGTH(str); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11878 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11879 | u = PyUnicode_New(nchars, PyUnicode_MAX_CHAR_VALUE(str)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11880 | if (!u) |
| 11881 | return NULL; |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11882 | assert(PyUnicode_KIND(u) == PyUnicode_KIND(str)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11883 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11884 | if (PyUnicode_GET_LENGTH(str) == 1) { |
| 11885 | const int kind = PyUnicode_KIND(str); |
| 11886 | const Py_UCS4 fill_char = PyUnicode_READ(kind, PyUnicode_DATA(str), 0); |
| 11887 | void *to = PyUnicode_DATA(u); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11888 | if (kind == PyUnicode_1BYTE_KIND) |
| 11889 | memset(to, (unsigned char)fill_char, len); |
| 11890 | else { |
| 11891 | for (n = 0; n < len; ++n) |
| 11892 | PyUnicode_WRITE(kind, to, n, fill_char); |
| 11893 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11894 | } |
| 11895 | else { |
| 11896 | /* number of characters copied this far */ |
| 11897 | Py_ssize_t done = PyUnicode_GET_LENGTH(str); |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 11898 | const Py_ssize_t char_size = PyUnicode_KIND(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11899 | char *to = (char *) PyUnicode_DATA(u); |
| 11900 | Py_MEMCPY(to, PyUnicode_DATA(str), |
| 11901 | PyUnicode_GET_LENGTH(str) * char_size); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11902 | while (done < nchars) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11903 | n = (done <= nchars-done) ? done : nchars-done; |
| 11904 | Py_MEMCPY(to + (done * char_size), to, n * char_size); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11905 | done += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11906 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11907 | } |
| 11908 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 11909 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11910 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11911 | } |
| 11912 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 11913 | PyObject * |
| 11914 | PyUnicode_Replace(PyObject *obj, |
| 11915 | PyObject *subobj, |
| 11916 | PyObject *replobj, |
| 11917 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11918 | { |
| 11919 | PyObject *self; |
| 11920 | PyObject *str1; |
| 11921 | PyObject *str2; |
| 11922 | PyObject *result; |
| 11923 | |
| 11924 | self = PyUnicode_FromObject(obj); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11925 | if (self == NULL || PyUnicode_READY(self) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11926 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11927 | str1 = PyUnicode_FromObject(subobj); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11928 | if (str1 == NULL || PyUnicode_READY(str1) == -1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11929 | Py_DECREF(self); |
| 11930 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11931 | } |
| 11932 | str2 = PyUnicode_FromObject(replobj); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11933 | if (str2 == NULL || PyUnicode_READY(str2)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11934 | Py_DECREF(self); |
| 11935 | Py_DECREF(str1); |
| 11936 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11937 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11938 | result = replace(self, str1, str2, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11939 | Py_DECREF(self); |
| 11940 | Py_DECREF(str1); |
| 11941 | Py_DECREF(str2); |
| 11942 | return result; |
| 11943 | } |
| 11944 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11945 | PyDoc_STRVAR(replace__doc__, |
Ezio Melotti | c1897e7 | 2010-06-26 18:50:39 +0000 | [diff] [blame] | 11946 | "S.replace(old, new[, count]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11947 | \n\ |
| 11948 | Return a copy of S with all occurrences of substring\n\ |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 11949 | old replaced by new. If the optional argument count is\n\ |
| 11950 | given, only the first count occurrences are replaced."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11951 | |
| 11952 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11953 | unicode_replace(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11954 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11955 | PyObject *str1; |
| 11956 | PyObject *str2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11957 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11958 | PyObject *result; |
| 11959 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11960 | if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11961 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11962 | if (!PyUnicode_READY(self) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11963 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11964 | str1 = PyUnicode_FromObject(str1); |
| 11965 | if (str1 == NULL || PyUnicode_READY(str1) == -1) |
| 11966 | return NULL; |
| 11967 | str2 = PyUnicode_FromObject(str2); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11968 | if (str2 == NULL || PyUnicode_READY(str2) == -1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11969 | Py_DECREF(str1); |
| 11970 | return NULL; |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 11971 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11972 | |
| 11973 | result = replace(self, str1, str2, maxcount); |
| 11974 | |
| 11975 | Py_DECREF(str1); |
| 11976 | Py_DECREF(str2); |
| 11977 | return result; |
| 11978 | } |
| 11979 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 11980 | static PyObject * |
| 11981 | unicode_repr(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11982 | { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11983 | PyObject *repr; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11984 | Py_ssize_t isize; |
| 11985 | Py_ssize_t osize, squote, dquote, i, o; |
| 11986 | Py_UCS4 max, quote; |
| 11987 | int ikind, okind; |
| 11988 | void *idata, *odata; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11989 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11990 | if (PyUnicode_READY(unicode) == -1) |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11991 | return NULL; |
| 11992 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11993 | isize = PyUnicode_GET_LENGTH(unicode); |
| 11994 | idata = PyUnicode_DATA(unicode); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11995 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11996 | /* Compute length of output, quote characters, and |
| 11997 | maximum character */ |
| 11998 | osize = 2; /* quotes */ |
| 11999 | max = 127; |
| 12000 | squote = dquote = 0; |
| 12001 | ikind = PyUnicode_KIND(unicode); |
| 12002 | for (i = 0; i < isize; i++) { |
| 12003 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
| 12004 | switch (ch) { |
| 12005 | case '\'': squote++; osize++; break; |
| 12006 | case '"': dquote++; osize++; break; |
| 12007 | case '\\': case '\t': case '\r': case '\n': |
| 12008 | osize += 2; break; |
| 12009 | default: |
| 12010 | /* Fast-path ASCII */ |
| 12011 | if (ch < ' ' || ch == 0x7f) |
| 12012 | osize += 4; /* \xHH */ |
| 12013 | else if (ch < 0x7f) |
| 12014 | osize++; |
| 12015 | else if (Py_UNICODE_ISPRINTABLE(ch)) { |
| 12016 | osize++; |
| 12017 | max = ch > max ? ch : max; |
| 12018 | } |
| 12019 | else if (ch < 0x100) |
| 12020 | osize += 4; /* \xHH */ |
| 12021 | else if (ch < 0x10000) |
| 12022 | osize += 6; /* \uHHHH */ |
| 12023 | else |
| 12024 | osize += 10; /* \uHHHHHHHH */ |
| 12025 | } |
| 12026 | } |
| 12027 | |
| 12028 | quote = '\''; |
| 12029 | if (squote) { |
| 12030 | if (dquote) |
| 12031 | /* Both squote and dquote present. Use squote, |
| 12032 | and escape them */ |
| 12033 | osize += squote; |
| 12034 | else |
| 12035 | quote = '"'; |
| 12036 | } |
| 12037 | |
| 12038 | repr = PyUnicode_New(osize, max); |
| 12039 | if (repr == NULL) |
| 12040 | return NULL; |
| 12041 | okind = PyUnicode_KIND(repr); |
| 12042 | odata = PyUnicode_DATA(repr); |
| 12043 | |
| 12044 | PyUnicode_WRITE(okind, odata, 0, quote); |
| 12045 | PyUnicode_WRITE(okind, odata, osize-1, quote); |
| 12046 | |
| 12047 | for (i = 0, o = 1; i < isize; i++) { |
| 12048 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12049 | |
| 12050 | /* Escape quotes and backslashes */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12051 | if ((ch == quote) || (ch == '\\')) { |
| 12052 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12053 | PyUnicode_WRITE(okind, odata, o++, ch); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12054 | continue; |
| 12055 | } |
| 12056 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12057 | /* Map special whitespace to '\t', \n', '\r' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12058 | if (ch == '\t') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12059 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12060 | PyUnicode_WRITE(okind, odata, o++, 't'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12061 | } |
| 12062 | else if (ch == '\n') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12063 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12064 | PyUnicode_WRITE(okind, odata, o++, 'n'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12065 | } |
| 12066 | else if (ch == '\r') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12067 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12068 | PyUnicode_WRITE(okind, odata, o++, 'r'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12069 | } |
| 12070 | |
| 12071 | /* Map non-printable US ASCII to '\xhh' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12072 | else if (ch < ' ' || ch == 0x7F) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12073 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12074 | PyUnicode_WRITE(okind, odata, o++, 'x'); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 12075 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0x000F]); |
| 12076 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0x000F]); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12077 | } |
| 12078 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12079 | /* Copy ASCII characters as-is */ |
| 12080 | else if (ch < 0x7F) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12081 | PyUnicode_WRITE(okind, odata, o++, ch); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12082 | } |
| 12083 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12084 | /* Non-ASCII characters */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12085 | else { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12086 | /* Map Unicode whitespace and control characters |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12087 | (categories Z* and C* except ASCII space) |
| 12088 | */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12089 | if (!Py_UNICODE_ISPRINTABLE(ch)) { |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12090 | /* Map 8-bit characters to '\xhh' */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12091 | if (ch <= 0xff) { |
| 12092 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12093 | PyUnicode_WRITE(okind, odata, o++, 'x'); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 12094 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0x000F]); |
| 12095 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0x000F]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12096 | } |
| 12097 | /* Map 21-bit characters to '\U00xxxxxx' */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12098 | else if (ch >= 0x10000) { |
| 12099 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12100 | PyUnicode_WRITE(okind, odata, o++, 'U'); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 12101 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 28) & 0xF]); |
| 12102 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 24) & 0xF]); |
| 12103 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 20) & 0xF]); |
| 12104 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 16) & 0xF]); |
| 12105 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 12) & 0xF]); |
| 12106 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 8) & 0xF]); |
| 12107 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0xF]); |
| 12108 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0xF]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12109 | } |
| 12110 | /* Map 16-bit characters to '\uxxxx' */ |
| 12111 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12112 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12113 | PyUnicode_WRITE(okind, odata, o++, 'u'); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 12114 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 12) & 0xF]); |
| 12115 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 8) & 0xF]); |
| 12116 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0xF]); |
| 12117 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0xF]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12118 | } |
| 12119 | } |
| 12120 | /* Copy characters as-is */ |
| 12121 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12122 | PyUnicode_WRITE(okind, odata, o++, ch); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12123 | } |
| 12124 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12125 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12126 | /* Closing quote already added at the beginning */ |
Victor Stinner | 05d1189 | 2011-10-06 01:13:58 +0200 | [diff] [blame] | 12127 | assert(_PyUnicode_CheckConsistency(repr, 1)); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12128 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12129 | } |
| 12130 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12131 | PyDoc_STRVAR(rfind__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12132 | "S.rfind(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12133 | \n\ |
| 12134 | Return the highest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 12135 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12136 | arguments start and end are interpreted as in slice notation.\n\ |
| 12137 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12138 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12139 | |
| 12140 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12141 | unicode_rfind(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12142 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12143 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 12144 | Py_ssize_t start; |
| 12145 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12146 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12147 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12148 | if (!stringlib_parse_args_finds_unicode("rfind", args, &substring, |
| 12149 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12150 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12151 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12152 | if (PyUnicode_READY(self) == -1) |
| 12153 | return NULL; |
| 12154 | if (PyUnicode_READY(substring) == -1) |
| 12155 | return NULL; |
| 12156 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12157 | result = any_find_slice(-1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12158 | |
| 12159 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12160 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12161 | if (result == -2) |
| 12162 | return NULL; |
| 12163 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12164 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12165 | } |
| 12166 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12167 | PyDoc_STRVAR(rindex__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12168 | "S.rindex(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12169 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12170 | 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] | 12171 | |
| 12172 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12173 | unicode_rindex(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12174 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12175 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 12176 | Py_ssize_t start; |
| 12177 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12178 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12179 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12180 | if (!stringlib_parse_args_finds_unicode("rindex", args, &substring, |
| 12181 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12182 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12183 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12184 | if (PyUnicode_READY(self) == -1) |
| 12185 | return NULL; |
| 12186 | if (PyUnicode_READY(substring) == -1) |
| 12187 | return NULL; |
| 12188 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12189 | result = any_find_slice(-1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12190 | |
| 12191 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12192 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12193 | if (result == -2) |
| 12194 | return NULL; |
| 12195 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12196 | if (result < 0) { |
| 12197 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 12198 | return NULL; |
| 12199 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12200 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12201 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12202 | } |
| 12203 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12204 | PyDoc_STRVAR(rjust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12205 | "S.rjust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12206 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 12207 | 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] | 12208 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12209 | |
| 12210 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12211 | unicode_rjust(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12212 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12213 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12214 | Py_UCS4 fillchar = ' '; |
| 12215 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 12216 | 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] | 12217 | return NULL; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 12218 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 12219 | if (PyUnicode_READY(self) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12220 | return NULL; |
| 12221 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12222 | if (_PyUnicode_LENGTH(self) >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12223 | Py_INCREF(self); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12224 | return self; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12225 | } |
| 12226 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12227 | return pad(self, width - _PyUnicode_LENGTH(self), 0, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12228 | } |
| 12229 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12230 | PyObject * |
| 12231 | PyUnicode_Split(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12232 | { |
| 12233 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 12234 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12235 | s = PyUnicode_FromObject(s); |
| 12236 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12237 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12238 | if (sep != NULL) { |
| 12239 | sep = PyUnicode_FromObject(sep); |
| 12240 | if (sep == NULL) { |
| 12241 | Py_DECREF(s); |
| 12242 | return NULL; |
| 12243 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12244 | } |
| 12245 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12246 | result = split(s, sep, maxsplit); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12247 | |
| 12248 | Py_DECREF(s); |
| 12249 | Py_XDECREF(sep); |
| 12250 | return result; |
| 12251 | } |
| 12252 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12253 | PyDoc_STRVAR(split__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12254 | "S.split([sep[, maxsplit]]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12255 | \n\ |
| 12256 | Return a list of the words in S, using sep as the\n\ |
| 12257 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 12258 | 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] | 12259 | whitespace string is a separator and empty strings are\n\ |
| 12260 | removed from the result."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12261 | |
| 12262 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12263 | unicode_split(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12264 | { |
| 12265 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12266 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12267 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12268 | if (!PyArg_ParseTuple(args, "|On:split", &substring, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12269 | return NULL; |
| 12270 | |
| 12271 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12272 | return split(self, NULL, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12273 | else if (PyUnicode_Check(substring)) |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12274 | return split(self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12275 | else |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12276 | return PyUnicode_Split(self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12277 | } |
| 12278 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12279 | PyObject * |
| 12280 | PyUnicode_Partition(PyObject *str_in, PyObject *sep_in) |
| 12281 | { |
| 12282 | PyObject* str_obj; |
| 12283 | PyObject* sep_obj; |
| 12284 | PyObject* out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12285 | int kind1, kind2, kind; |
| 12286 | void *buf1 = NULL, *buf2 = NULL; |
| 12287 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12288 | |
| 12289 | str_obj = PyUnicode_FromObject(str_in); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 12290 | if (!str_obj || PyUnicode_READY(str_obj) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12291 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12292 | sep_obj = PyUnicode_FromObject(sep_in); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12293 | if (!sep_obj || PyUnicode_READY(sep_obj) == -1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12294 | Py_DECREF(str_obj); |
| 12295 | return NULL; |
| 12296 | } |
| 12297 | |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12298 | kind1 = PyUnicode_KIND(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12299 | kind2 = PyUnicode_KIND(sep_obj); |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12300 | kind = Py_MAX(kind1, kind2); |
| 12301 | buf1 = PyUnicode_DATA(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12302 | if (kind1 != kind) |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12303 | buf1 = _PyUnicode_AsKind(str_obj, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12304 | if (!buf1) |
| 12305 | goto onError; |
| 12306 | buf2 = PyUnicode_DATA(sep_obj); |
| 12307 | if (kind2 != kind) |
| 12308 | buf2 = _PyUnicode_AsKind(sep_obj, kind); |
| 12309 | if (!buf2) |
| 12310 | goto onError; |
| 12311 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 12312 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
| 12313 | |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12314 | switch(PyUnicode_KIND(str_obj)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12315 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 12316 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sep_obj)) |
| 12317 | out = asciilib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12318 | else |
| 12319 | 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] | 12320 | break; |
| 12321 | case PyUnicode_2BYTE_KIND: |
| 12322 | out = ucs2lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12323 | break; |
| 12324 | case PyUnicode_4BYTE_KIND: |
| 12325 | out = ucs4lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12326 | break; |
| 12327 | default: |
| 12328 | assert(0); |
| 12329 | out = 0; |
| 12330 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12331 | |
| 12332 | Py_DECREF(sep_obj); |
| 12333 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12334 | if (kind1 != kind) |
| 12335 | PyMem_Free(buf1); |
| 12336 | if (kind2 != kind) |
| 12337 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12338 | |
| 12339 | return out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12340 | onError: |
| 12341 | Py_DECREF(sep_obj); |
| 12342 | Py_DECREF(str_obj); |
| 12343 | if (kind1 != kind && buf1) |
| 12344 | PyMem_Free(buf1); |
| 12345 | if (kind2 != kind && buf2) |
| 12346 | PyMem_Free(buf2); |
| 12347 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12348 | } |
| 12349 | |
| 12350 | |
| 12351 | PyObject * |
| 12352 | PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in) |
| 12353 | { |
| 12354 | PyObject* str_obj; |
| 12355 | PyObject* sep_obj; |
| 12356 | PyObject* out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12357 | int kind1, kind2, kind; |
| 12358 | void *buf1 = NULL, *buf2 = NULL; |
| 12359 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12360 | |
| 12361 | str_obj = PyUnicode_FromObject(str_in); |
| 12362 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12363 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12364 | sep_obj = PyUnicode_FromObject(sep_in); |
| 12365 | if (!sep_obj) { |
| 12366 | Py_DECREF(str_obj); |
| 12367 | return NULL; |
| 12368 | } |
| 12369 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12370 | kind1 = PyUnicode_KIND(str_in); |
| 12371 | kind2 = PyUnicode_KIND(sep_obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 12372 | kind = Py_MAX(kind1, kind2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12373 | buf1 = PyUnicode_DATA(str_in); |
| 12374 | if (kind1 != kind) |
| 12375 | buf1 = _PyUnicode_AsKind(str_in, kind); |
| 12376 | if (!buf1) |
| 12377 | goto onError; |
| 12378 | buf2 = PyUnicode_DATA(sep_obj); |
| 12379 | if (kind2 != kind) |
| 12380 | buf2 = _PyUnicode_AsKind(sep_obj, kind); |
| 12381 | if (!buf2) |
| 12382 | goto onError; |
| 12383 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 12384 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
| 12385 | |
| 12386 | switch(PyUnicode_KIND(str_in)) { |
| 12387 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 12388 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sep_obj)) |
| 12389 | out = asciilib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12390 | else |
| 12391 | 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] | 12392 | break; |
| 12393 | case PyUnicode_2BYTE_KIND: |
| 12394 | out = ucs2lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12395 | break; |
| 12396 | case PyUnicode_4BYTE_KIND: |
| 12397 | out = ucs4lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12398 | break; |
| 12399 | default: |
| 12400 | assert(0); |
| 12401 | out = 0; |
| 12402 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12403 | |
| 12404 | Py_DECREF(sep_obj); |
| 12405 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12406 | if (kind1 != kind) |
| 12407 | PyMem_Free(buf1); |
| 12408 | if (kind2 != kind) |
| 12409 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12410 | |
| 12411 | return out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12412 | onError: |
| 12413 | Py_DECREF(sep_obj); |
| 12414 | Py_DECREF(str_obj); |
| 12415 | if (kind1 != kind && buf1) |
| 12416 | PyMem_Free(buf1); |
| 12417 | if (kind2 != kind && buf2) |
| 12418 | PyMem_Free(buf2); |
| 12419 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12420 | } |
| 12421 | |
| 12422 | PyDoc_STRVAR(partition__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12423 | "S.partition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12424 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 12425 | 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] | 12426 | 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] | 12427 | found, return S and two empty strings."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12428 | |
| 12429 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12430 | unicode_partition(PyObject *self, PyObject *separator) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12431 | { |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12432 | return PyUnicode_Partition(self, separator); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12433 | } |
| 12434 | |
| 12435 | PyDoc_STRVAR(rpartition__doc__, |
Ezio Melotti | 5b2b242 | 2010-01-25 11:58:28 +0000 | [diff] [blame] | 12436 | "S.rpartition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12437 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 12438 | 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] | 12439 | 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] | 12440 | separator is not found, return two empty strings and S."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12441 | |
| 12442 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12443 | unicode_rpartition(PyObject *self, PyObject *separator) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12444 | { |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12445 | return PyUnicode_RPartition(self, separator); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12446 | } |
| 12447 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12448 | PyObject * |
| 12449 | PyUnicode_RSplit(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12450 | { |
| 12451 | PyObject *result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12452 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12453 | s = PyUnicode_FromObject(s); |
| 12454 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12455 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12456 | if (sep != NULL) { |
| 12457 | sep = PyUnicode_FromObject(sep); |
| 12458 | if (sep == NULL) { |
| 12459 | Py_DECREF(s); |
| 12460 | return NULL; |
| 12461 | } |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12462 | } |
| 12463 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12464 | result = rsplit(s, sep, maxsplit); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12465 | |
| 12466 | Py_DECREF(s); |
| 12467 | Py_XDECREF(sep); |
| 12468 | return result; |
| 12469 | } |
| 12470 | |
| 12471 | PyDoc_STRVAR(rsplit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12472 | "S.rsplit([sep[, maxsplit]]) -> list of strings\n\ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12473 | \n\ |
| 12474 | Return a list of the words in S, using sep as the\n\ |
| 12475 | delimiter string, starting at the end of the string and\n\ |
| 12476 | working to the front. If maxsplit is given, at most maxsplit\n\ |
| 12477 | splits are done. If sep is not specified, any whitespace string\n\ |
| 12478 | is a separator."); |
| 12479 | |
| 12480 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12481 | unicode_rsplit(PyObject *self, PyObject *args) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12482 | { |
| 12483 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12484 | Py_ssize_t maxcount = -1; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12485 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12486 | if (!PyArg_ParseTuple(args, "|On:rsplit", &substring, &maxcount)) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12487 | return NULL; |
| 12488 | |
| 12489 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12490 | return rsplit(self, NULL, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12491 | else if (PyUnicode_Check(substring)) |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12492 | return rsplit(self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12493 | else |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12494 | return PyUnicode_RSplit(self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12495 | } |
| 12496 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12497 | PyDoc_STRVAR(splitlines__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12498 | "S.splitlines([keepends]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12499 | \n\ |
| 12500 | 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] | 12501 | 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] | 12502 | is given and true."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12503 | |
| 12504 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12505 | unicode_splitlines(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12506 | { |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12507 | static char *kwlist[] = {"keepends", 0}; |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 12508 | int keepends = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12509 | |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12510 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|i:splitlines", |
| 12511 | kwlist, &keepends)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12512 | return NULL; |
| 12513 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12514 | return PyUnicode_Splitlines(self, keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12515 | } |
| 12516 | |
| 12517 | static |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 12518 | PyObject *unicode_str(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12519 | { |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 12520 | if (PyUnicode_CheckExact(self)) { |
| 12521 | Py_INCREF(self); |
| 12522 | return self; |
| 12523 | } else |
| 12524 | /* Subtype -- return genuine unicode string with the same value. */ |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 12525 | return PyUnicode_Copy(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12526 | } |
| 12527 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12528 | PyDoc_STRVAR(swapcase__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12529 | "S.swapcase() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12530 | \n\ |
| 12531 | 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] | 12532 | and vice versa."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12533 | |
| 12534 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12535 | unicode_swapcase(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12536 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12537 | return fixup(self, fixswapcase); |
| 12538 | } |
| 12539 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12540 | PyDoc_STRVAR(maketrans__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12541 | "str.maketrans(x[, y[, z]]) -> dict (static method)\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12542 | \n\ |
| 12543 | Return a translation table usable for str.translate().\n\ |
| 12544 | If there is only one argument, it must be a dictionary mapping Unicode\n\ |
| 12545 | ordinals (integers) or characters to Unicode ordinals, strings or None.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12546 | Character keys will be then converted to ordinals.\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12547 | If there are two arguments, they must be strings of equal length, and\n\ |
| 12548 | in the resulting dictionary, each character in x will be mapped to the\n\ |
| 12549 | character at the same position in y. If there is a third argument, it\n\ |
| 12550 | must be a string, whose characters will be mapped to None in the result."); |
| 12551 | |
| 12552 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12553 | unicode_maketrans(PyObject *null, PyObject *args) |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12554 | { |
| 12555 | PyObject *x, *y = NULL, *z = NULL; |
| 12556 | PyObject *new = NULL, *key, *value; |
| 12557 | Py_ssize_t i = 0; |
| 12558 | int res; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12559 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12560 | if (!PyArg_ParseTuple(args, "O|UU:maketrans", &x, &y, &z)) |
| 12561 | return NULL; |
| 12562 | new = PyDict_New(); |
| 12563 | if (!new) |
| 12564 | return NULL; |
| 12565 | if (y != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12566 | int x_kind, y_kind, z_kind; |
| 12567 | void *x_data, *y_data, *z_data; |
| 12568 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12569 | /* x must be a string too, of equal length */ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12570 | if (!PyUnicode_Check(x)) { |
| 12571 | PyErr_SetString(PyExc_TypeError, "first maketrans argument must " |
| 12572 | "be a string if there is a second argument"); |
| 12573 | goto err; |
| 12574 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12575 | if (PyUnicode_GET_LENGTH(x) != PyUnicode_GET_LENGTH(y)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12576 | PyErr_SetString(PyExc_ValueError, "the first two maketrans " |
| 12577 | "arguments must have equal length"); |
| 12578 | goto err; |
| 12579 | } |
| 12580 | /* 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] | 12581 | x_kind = PyUnicode_KIND(x); |
| 12582 | y_kind = PyUnicode_KIND(y); |
| 12583 | x_data = PyUnicode_DATA(x); |
| 12584 | y_data = PyUnicode_DATA(y); |
| 12585 | for (i = 0; i < PyUnicode_GET_LENGTH(x); i++) { |
| 12586 | key = PyLong_FromLong(PyUnicode_READ(x_kind, x_data, i)); |
| 12587 | value = PyLong_FromLong(PyUnicode_READ(y_kind, y_data, i)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12588 | if (!key || !value) |
| 12589 | goto err; |
| 12590 | res = PyDict_SetItem(new, key, value); |
| 12591 | Py_DECREF(key); |
| 12592 | Py_DECREF(value); |
| 12593 | if (res < 0) |
| 12594 | goto err; |
| 12595 | } |
| 12596 | /* create entries for deleting chars in z */ |
| 12597 | if (z != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12598 | z_kind = PyUnicode_KIND(z); |
| 12599 | z_data = PyUnicode_DATA(z); |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 12600 | for (i = 0; i < PyUnicode_GET_LENGTH(z); i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12601 | key = PyLong_FromLong(PyUnicode_READ(z_kind, z_data, i)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12602 | if (!key) |
| 12603 | goto err; |
| 12604 | res = PyDict_SetItem(new, key, Py_None); |
| 12605 | Py_DECREF(key); |
| 12606 | if (res < 0) |
| 12607 | goto err; |
| 12608 | } |
| 12609 | } |
| 12610 | } else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12611 | int kind; |
| 12612 | void *data; |
| 12613 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12614 | /* x must be a dict */ |
Raymond Hettinger | 3ad0576 | 2009-05-29 22:11:22 +0000 | [diff] [blame] | 12615 | if (!PyDict_CheckExact(x)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12616 | PyErr_SetString(PyExc_TypeError, "if you give only one argument " |
| 12617 | "to maketrans it must be a dict"); |
| 12618 | goto err; |
| 12619 | } |
| 12620 | /* copy entries into the new dict, converting string keys to int keys */ |
| 12621 | while (PyDict_Next(x, &i, &key, &value)) { |
| 12622 | if (PyUnicode_Check(key)) { |
| 12623 | /* convert string keys to integer keys */ |
| 12624 | PyObject *newkey; |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 12625 | if (PyUnicode_GET_LENGTH(key) != 1) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12626 | PyErr_SetString(PyExc_ValueError, "string keys in translate " |
| 12627 | "table must be of length 1"); |
| 12628 | goto err; |
| 12629 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12630 | kind = PyUnicode_KIND(key); |
| 12631 | data = PyUnicode_DATA(key); |
| 12632 | newkey = PyLong_FromLong(PyUnicode_READ(kind, data, 0)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12633 | if (!newkey) |
| 12634 | goto err; |
| 12635 | res = PyDict_SetItem(new, newkey, value); |
| 12636 | Py_DECREF(newkey); |
| 12637 | if (res < 0) |
| 12638 | goto err; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12639 | } else if (PyLong_Check(key)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12640 | /* just keep integer keys */ |
| 12641 | if (PyDict_SetItem(new, key, value) < 0) |
| 12642 | goto err; |
| 12643 | } else { |
| 12644 | PyErr_SetString(PyExc_TypeError, "keys in translate table must " |
| 12645 | "be strings or integers"); |
| 12646 | goto err; |
| 12647 | } |
| 12648 | } |
| 12649 | } |
| 12650 | return new; |
| 12651 | err: |
| 12652 | Py_DECREF(new); |
| 12653 | return NULL; |
| 12654 | } |
| 12655 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12656 | PyDoc_STRVAR(translate__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12657 | "S.translate(table) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12658 | \n\ |
| 12659 | Return a copy of the string S, where all characters have been mapped\n\ |
| 12660 | through the given translation table, which must be a mapping of\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12661 | Unicode ordinals to Unicode ordinals, strings, or None.\n\ |
Walter Dörwald | 5c1ee17 | 2002-09-04 20:31:32 +0000 | [diff] [blame] | 12662 | Unmapped characters are left untouched. Characters mapped to None\n\ |
| 12663 | are deleted."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12664 | |
| 12665 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12666 | unicode_translate(PyObject *self, PyObject *table) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12667 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12668 | return _PyUnicode_TranslateCharmap(self, table, "ignore"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12669 | } |
| 12670 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12671 | PyDoc_STRVAR(upper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12672 | "S.upper() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12673 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12674 | Return a copy of S converted to uppercase."); |
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_upper(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12678 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12679 | return fixup(self, fixupper); |
| 12680 | } |
| 12681 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12682 | PyDoc_STRVAR(zfill__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12683 | "S.zfill(width) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12684 | \n\ |
Benjamin Peterson | 9aa4299 | 2008-09-10 21:57:34 +0000 | [diff] [blame] | 12685 | Pad a numeric string S with zeros on the left, to fill a field\n\ |
| 12686 | of the specified width. The string S is never truncated."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12687 | |
| 12688 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12689 | unicode_zfill(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12690 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12691 | Py_ssize_t fill; |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12692 | PyObject *u; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12693 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12694 | int kind; |
| 12695 | void *data; |
| 12696 | Py_UCS4 chr; |
| 12697 | |
| 12698 | if (PyUnicode_READY(self) == -1) |
| 12699 | return NULL; |
| 12700 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12701 | if (!PyArg_ParseTuple(args, "n:zfill", &width)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12702 | return NULL; |
| 12703 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12704 | if (PyUnicode_GET_LENGTH(self) >= width) { |
Walter Dörwald | 0fe940c | 2002-04-15 18:42:15 +0000 | [diff] [blame] | 12705 | if (PyUnicode_CheckExact(self)) { |
| 12706 | Py_INCREF(self); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12707 | return self; |
Walter Dörwald | 0fe940c | 2002-04-15 18:42:15 +0000 | [diff] [blame] | 12708 | } |
| 12709 | else |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12710 | return PyUnicode_Copy(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12711 | } |
| 12712 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12713 | fill = width - _PyUnicode_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12714 | |
| 12715 | u = pad(self, fill, 0, '0'); |
| 12716 | |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 12717 | if (u == NULL) |
| 12718 | return NULL; |
| 12719 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12720 | kind = PyUnicode_KIND(u); |
| 12721 | data = PyUnicode_DATA(u); |
| 12722 | chr = PyUnicode_READ(kind, data, fill); |
| 12723 | |
| 12724 | if (chr == '+' || chr == '-') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12725 | /* move sign to beginning of string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12726 | PyUnicode_WRITE(kind, data, 0, chr); |
| 12727 | PyUnicode_WRITE(kind, data, fill, '0'); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12728 | } |
| 12729 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12730 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12731 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12732 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12733 | |
| 12734 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12735 | static PyObject * |
| 12736 | unicode__decimal2ascii(PyObject *self) |
| 12737 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12738 | return PyUnicode_TransformDecimalAndSpaceToASCII(self); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12739 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12740 | #endif |
| 12741 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12742 | PyDoc_STRVAR(startswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12743 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12744 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 12745 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 12746 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12747 | With optional end, stop comparing S at that position.\n\ |
| 12748 | prefix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12749 | |
| 12750 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12751 | unicode_startswith(PyObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12752 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12753 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12754 | PyObject *subobj; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12755 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12756 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12757 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12758 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12759 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12760 | if (!stringlib_parse_args_finds("startswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12761 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12762 | if (PyTuple_Check(subobj)) { |
| 12763 | Py_ssize_t i; |
| 12764 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12765 | substring = PyUnicode_FromObject(PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12766 | if (substring == NULL) |
| 12767 | return NULL; |
| 12768 | result = tailmatch(self, substring, start, end, -1); |
| 12769 | Py_DECREF(substring); |
| 12770 | if (result) { |
| 12771 | Py_RETURN_TRUE; |
| 12772 | } |
| 12773 | } |
| 12774 | /* nothing matched */ |
| 12775 | Py_RETURN_FALSE; |
| 12776 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12777 | substring = PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12778 | if (substring == NULL) { |
| 12779 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 12780 | PyErr_Format(PyExc_TypeError, "startswith first arg must be str or " |
| 12781 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12782 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12783 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12784 | result = tailmatch(self, substring, start, end, -1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12785 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12786 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12787 | } |
| 12788 | |
| 12789 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12790 | PyDoc_STRVAR(endswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12791 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12792 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 12793 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 12794 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12795 | With optional end, stop comparing S at that position.\n\ |
| 12796 | suffix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12797 | |
| 12798 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12799 | unicode_endswith(PyObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12800 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12801 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12802 | PyObject *subobj; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12803 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12804 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12805 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12806 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12807 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12808 | if (!stringlib_parse_args_finds("endswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12809 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12810 | if (PyTuple_Check(subobj)) { |
| 12811 | Py_ssize_t i; |
| 12812 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12813 | substring = PyUnicode_FromObject( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12814 | PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12815 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12816 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12817 | result = tailmatch(self, substring, start, end, +1); |
| 12818 | Py_DECREF(substring); |
| 12819 | if (result) { |
| 12820 | Py_RETURN_TRUE; |
| 12821 | } |
| 12822 | } |
| 12823 | Py_RETURN_FALSE; |
| 12824 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12825 | substring = PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12826 | if (substring == NULL) { |
| 12827 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 12828 | PyErr_Format(PyExc_TypeError, "endswith first arg must be str or " |
| 12829 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12830 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12831 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12832 | result = tailmatch(self, substring, start, end, +1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12833 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12834 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12835 | } |
| 12836 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12837 | #include "stringlib/unicode_format.h" |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12838 | |
| 12839 | PyDoc_STRVAR(format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12840 | "S.format(*args, **kwargs) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12841 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 12842 | Return a formatted version of S, using substitutions from args and kwargs.\n\ |
| 12843 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12844 | |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 12845 | PyDoc_STRVAR(format_map__doc__, |
| 12846 | "S.format_map(mapping) -> str\n\ |
| 12847 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 12848 | Return a formatted version of S, using substitutions from mapping.\n\ |
| 12849 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 12850 | |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 12851 | static PyObject * |
| 12852 | unicode__format__(PyObject* self, PyObject* args) |
| 12853 | { |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12854 | PyObject *format_spec, *out; |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 12855 | |
| 12856 | if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) |
| 12857 | return NULL; |
| 12858 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12859 | out = _PyUnicode_FormatAdvanced(self, format_spec, 0, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12860 | PyUnicode_GET_LENGTH(format_spec)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12861 | return out; |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 12862 | } |
| 12863 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12864 | PyDoc_STRVAR(p_format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12865 | "S.__format__(format_spec) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12866 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 12867 | Return a formatted version of S as described by format_spec."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12868 | |
| 12869 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12870 | unicode__sizeof__(PyObject *v) |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 12871 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12872 | Py_ssize_t size; |
| 12873 | |
| 12874 | /* If it's a compact object, account for base structure + |
| 12875 | character data. */ |
| 12876 | if (PyUnicode_IS_COMPACT_ASCII(v)) |
| 12877 | size = sizeof(PyASCIIObject) + PyUnicode_GET_LENGTH(v) + 1; |
| 12878 | else if (PyUnicode_IS_COMPACT(v)) |
| 12879 | size = sizeof(PyCompactUnicodeObject) + |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 12880 | (PyUnicode_GET_LENGTH(v) + 1) * PyUnicode_KIND(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12881 | else { |
| 12882 | /* If it is a two-block object, account for base object, and |
| 12883 | for character block if present. */ |
| 12884 | size = sizeof(PyUnicodeObject); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 12885 | if (_PyUnicode_DATA_ANY(v)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12886 | size += (PyUnicode_GET_LENGTH(v) + 1) * |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 12887 | PyUnicode_KIND(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12888 | } |
| 12889 | /* 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] | 12890 | with the data pointer. Check if the data is not shared. */ |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 12891 | if (_PyUnicode_HAS_WSTR_MEMORY(v)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12892 | size += (PyUnicode_WSTR_LENGTH(v) + 1) * sizeof(wchar_t); |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 12893 | if (_PyUnicode_HAS_UTF8_MEMORY(v)) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 12894 | size += PyUnicode_UTF8_LENGTH(v) + 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12895 | |
| 12896 | return PyLong_FromSsize_t(size); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 12897 | } |
| 12898 | |
| 12899 | PyDoc_STRVAR(sizeof__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12900 | "S.__sizeof__() -> size of S in memory, in bytes"); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 12901 | |
| 12902 | static PyObject * |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 12903 | unicode_getnewargs(PyObject *v) |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 12904 | { |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 12905 | PyObject *copy = PyUnicode_Copy(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12906 | if (!copy) |
| 12907 | return NULL; |
| 12908 | return Py_BuildValue("(N)", copy); |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 12909 | } |
| 12910 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12911 | static PyMethodDef unicode_methods[] = { |
| 12912 | |
| 12913 | /* Order is according to common usage: often used methods should |
| 12914 | appear first, since lookup is done sequentially. */ |
| 12915 | |
Benjamin Peterson | 28a4dce | 2010-12-12 01:33:04 +0000 | [diff] [blame] | 12916 | {"encode", (PyCFunction) unicode_encode, METH_VARARGS | METH_KEYWORDS, encode__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12917 | {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, |
| 12918 | {"split", (PyCFunction) unicode_split, METH_VARARGS, split__doc__}, |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12919 | {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS, rsplit__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12920 | {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, |
| 12921 | {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, |
| 12922 | {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, |
| 12923 | {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, |
| 12924 | {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, |
| 12925 | {"expandtabs", (PyCFunction) unicode_expandtabs, METH_VARARGS, expandtabs__doc__}, |
| 12926 | {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12927 | {"partition", (PyCFunction) unicode_partition, METH_O, partition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12928 | {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, |
| 12929 | {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, |
| 12930 | {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12931 | {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12932 | {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, |
| 12933 | {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, |
| 12934 | {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12935 | {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12936 | {"rpartition", (PyCFunction) unicode_rpartition, METH_O, rpartition__doc__}, |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12937 | {"splitlines", (PyCFunction) unicode_splitlines, METH_VARARGS | METH_KEYWORDS, splitlines__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12938 | {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12939 | {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, |
| 12940 | {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, |
| 12941 | {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, |
| 12942 | {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, |
| 12943 | {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, |
| 12944 | {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, |
| 12945 | {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, |
| 12946 | {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, |
| 12947 | {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, |
| 12948 | {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, |
| 12949 | {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, |
| 12950 | {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, |
| 12951 | {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, |
| 12952 | {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 12953 | {"isidentifier", (PyCFunction) unicode_isidentifier, METH_NOARGS, isidentifier__doc__}, |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12954 | {"isprintable", (PyCFunction) unicode_isprintable, METH_NOARGS, isprintable__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12955 | {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, |
Eric Smith | 9cd1e09 | 2007-08-31 18:39:38 +0000 | [diff] [blame] | 12956 | {"format", (PyCFunction) do_string_format, METH_VARARGS | METH_KEYWORDS, format__doc__}, |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 12957 | {"format_map", (PyCFunction) do_string_format_map, METH_O, format_map__doc__}, |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 12958 | {"__format__", (PyCFunction) unicode__format__, METH_VARARGS, p_format__doc__}, |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12959 | {"maketrans", (PyCFunction) unicode_maketrans, |
| 12960 | METH_VARARGS | METH_STATIC, maketrans__doc__}, |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 12961 | {"__sizeof__", (PyCFunction) unicode__sizeof__, METH_NOARGS, sizeof__doc__}, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 12962 | #if 0 |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12963 | {"capwords", (PyCFunction) unicode_capwords, METH_NOARGS, capwords__doc__}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12964 | #endif |
| 12965 | |
| 12966 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12967 | /* These methods are just used for debugging the implementation. */ |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12968 | {"_decimal2ascii", (PyCFunction) unicode__decimal2ascii, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12969 | #endif |
| 12970 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12971 | {"__getnewargs__", (PyCFunction)unicode_getnewargs, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12972 | {NULL, NULL} |
| 12973 | }; |
| 12974 | |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 12975 | static PyObject * |
| 12976 | unicode_mod(PyObject *v, PyObject *w) |
| 12977 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 12978 | if (!PyUnicode_Check(v)) |
| 12979 | Py_RETURN_NOTIMPLEMENTED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12980 | return PyUnicode_Format(v, w); |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 12981 | } |
| 12982 | |
| 12983 | static PyNumberMethods unicode_as_number = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12984 | 0, /*nb_add*/ |
| 12985 | 0, /*nb_subtract*/ |
| 12986 | 0, /*nb_multiply*/ |
| 12987 | unicode_mod, /*nb_remainder*/ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 12988 | }; |
| 12989 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12990 | static PySequenceMethods unicode_as_sequence = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12991 | (lenfunc) unicode_length, /* sq_length */ |
| 12992 | PyUnicode_Concat, /* sq_concat */ |
| 12993 | (ssizeargfunc) unicode_repeat, /* sq_repeat */ |
| 12994 | (ssizeargfunc) unicode_getitem, /* sq_item */ |
| 12995 | 0, /* sq_slice */ |
| 12996 | 0, /* sq_ass_item */ |
| 12997 | 0, /* sq_ass_slice */ |
| 12998 | PyUnicode_Contains, /* sq_contains */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12999 | }; |
| 13000 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13001 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13002 | unicode_subscript(PyObject* self, PyObject* item) |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13003 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13004 | if (PyUnicode_READY(self) == -1) |
| 13005 | return NULL; |
| 13006 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 13007 | if (PyIndex_Check(item)) { |
| 13008 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13009 | if (i == -1 && PyErr_Occurred()) |
| 13010 | return NULL; |
| 13011 | if (i < 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13012 | i += PyUnicode_GET_LENGTH(self); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13013 | return unicode_getitem(self, i); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13014 | } else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13015 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13016 | PyObject *result; |
| 13017 | void *src_data, *dest_data; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13018 | int src_kind, dest_kind; |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 13019 | Py_UCS4 ch, max_char, kind_limit; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13020 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13021 | if (PySlice_GetIndicesEx(item, PyUnicode_GET_LENGTH(self), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13022 | &start, &stop, &step, &slicelength) < 0) { |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13023 | return NULL; |
| 13024 | } |
| 13025 | |
| 13026 | if (slicelength <= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13027 | return PyUnicode_New(0, 0); |
| 13028 | } else if (start == 0 && step == 1 && |
| 13029 | slicelength == PyUnicode_GET_LENGTH(self) && |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 13030 | PyUnicode_CheckExact(self)) { |
| 13031 | Py_INCREF(self); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13032 | return self; |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 13033 | } else if (step == 1) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13034 | return PyUnicode_Substring(self, |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 13035 | start, start + slicelength); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13036 | } |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13037 | /* General case */ |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13038 | src_kind = PyUnicode_KIND(self); |
| 13039 | src_data = PyUnicode_DATA(self); |
Victor Stinner | 55c9911 | 2011-10-13 01:17:06 +0200 | [diff] [blame] | 13040 | if (!PyUnicode_IS_ASCII(self)) { |
| 13041 | kind_limit = kind_maxchar_limit(src_kind); |
| 13042 | max_char = 0; |
| 13043 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
| 13044 | ch = PyUnicode_READ(src_kind, src_data, cur); |
| 13045 | if (ch > max_char) { |
| 13046 | max_char = ch; |
| 13047 | if (max_char >= kind_limit) |
| 13048 | break; |
| 13049 | } |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 13050 | } |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13051 | } |
Victor Stinner | 55c9911 | 2011-10-13 01:17:06 +0200 | [diff] [blame] | 13052 | else |
| 13053 | max_char = 127; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13054 | result = PyUnicode_New(slicelength, max_char); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13055 | if (result == NULL) |
| 13056 | return NULL; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13057 | dest_kind = PyUnicode_KIND(result); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13058 | dest_data = PyUnicode_DATA(result); |
| 13059 | |
| 13060 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13061 | Py_UCS4 ch = PyUnicode_READ(src_kind, src_data, cur); |
| 13062 | PyUnicode_WRITE(dest_kind, dest_data, i, ch); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13063 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13064 | assert(_PyUnicode_CheckConsistency(result, 1)); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13065 | return result; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13066 | } else { |
| 13067 | PyErr_SetString(PyExc_TypeError, "string indices must be integers"); |
| 13068 | return NULL; |
| 13069 | } |
| 13070 | } |
| 13071 | |
| 13072 | static PyMappingMethods unicode_as_mapping = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13073 | (lenfunc)unicode_length, /* mp_length */ |
| 13074 | (binaryfunc)unicode_subscript, /* mp_subscript */ |
| 13075 | (objobjargproc)0, /* mp_ass_subscript */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13076 | }; |
| 13077 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13078 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13079 | /* Helpers for PyUnicode_Format() */ |
| 13080 | |
| 13081 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13082 | 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] | 13083 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13084 | Py_ssize_t argidx = *p_argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13085 | if (argidx < arglen) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13086 | (*p_argidx)++; |
| 13087 | if (arglen < 0) |
| 13088 | return args; |
| 13089 | else |
| 13090 | return PyTuple_GetItem(args, argidx); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13091 | } |
| 13092 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13093 | "not enough arguments for format string"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13094 | return NULL; |
| 13095 | } |
| 13096 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13097 | /* 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] | 13098 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13099 | static PyObject * |
| 13100 | formatfloat(PyObject *v, int flags, int prec, int type) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13101 | { |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13102 | char *p; |
| 13103 | PyObject *result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13104 | double x; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 13105 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13106 | x = PyFloat_AsDouble(v); |
| 13107 | if (x == -1.0 && PyErr_Occurred()) |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13108 | return NULL; |
| 13109 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13110 | if (prec < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13111 | prec = 6; |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 13112 | |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 13113 | p = PyOS_double_to_string(x, type, prec, |
| 13114 | (flags & F_ALT) ? Py_DTSF_ALT : 0, NULL); |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13115 | if (p == NULL) |
| 13116 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13117 | result = PyUnicode_DecodeASCII(p, strlen(p), NULL); |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 13118 | PyMem_Free(p); |
| 13119 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13120 | } |
| 13121 | |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13122 | static PyObject* |
| 13123 | formatlong(PyObject *val, int flags, int prec, int type) |
| 13124 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13125 | char *buf; |
| 13126 | int len; |
| 13127 | PyObject *str; /* temporary string object. */ |
| 13128 | PyObject *result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13129 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13130 | str = _PyBytes_FormatLong(val, flags, prec, type, &buf, &len); |
| 13131 | if (!str) |
| 13132 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13133 | result = PyUnicode_DecodeASCII(buf, len, NULL); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13134 | Py_DECREF(str); |
| 13135 | return result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13136 | } |
| 13137 | |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13138 | static Py_UCS4 |
| 13139 | formatchar(PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13140 | { |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 13141 | /* presume that the buffer is at least 3 characters long */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 13142 | if (PyUnicode_Check(v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13143 | if (PyUnicode_GET_LENGTH(v) == 1) { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13144 | return PyUnicode_READ_CHAR(v, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13145 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13146 | goto onError; |
| 13147 | } |
| 13148 | else { |
| 13149 | /* Integer input truncated to a character */ |
| 13150 | long x; |
| 13151 | x = PyLong_AsLong(v); |
| 13152 | if (x == -1 && PyErr_Occurred()) |
| 13153 | goto onError; |
| 13154 | |
| 13155 | if (x < 0 || x > 0x10ffff) { |
| 13156 | PyErr_SetString(PyExc_OverflowError, |
| 13157 | "%c arg not in range(0x110000)"); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13158 | return (Py_UCS4) -1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13159 | } |
| 13160 | |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13161 | return (Py_UCS4) x; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13162 | } |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 13163 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13164 | onError: |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 13165 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13166 | "%c requires int or char"); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13167 | return (Py_UCS4) -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13168 | } |
| 13169 | |
Antoine Pitrou | 978b9d2 | 2011-10-07 12:35:48 +0200 | [diff] [blame] | 13170 | static int |
| 13171 | repeat_accumulate(_PyAccu *acc, PyObject *obj, Py_ssize_t count) |
| 13172 | { |
| 13173 | int r; |
| 13174 | assert(count > 0); |
| 13175 | assert(PyUnicode_Check(obj)); |
| 13176 | if (count > 5) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13177 | PyObject *repeated = unicode_repeat(obj, count); |
Antoine Pitrou | 978b9d2 | 2011-10-07 12:35:48 +0200 | [diff] [blame] | 13178 | if (repeated == NULL) |
| 13179 | return -1; |
| 13180 | r = _PyAccu_Accumulate(acc, repeated); |
| 13181 | Py_DECREF(repeated); |
| 13182 | return r; |
| 13183 | } |
| 13184 | else { |
| 13185 | do { |
| 13186 | if (_PyAccu_Accumulate(acc, obj)) |
| 13187 | return -1; |
| 13188 | } while (--count); |
| 13189 | return 0; |
| 13190 | } |
| 13191 | } |
| 13192 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 13193 | PyObject * |
| 13194 | PyUnicode_Format(PyObject *format, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13195 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13196 | void *fmt; |
| 13197 | int fmtkind; |
| 13198 | PyObject *result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13199 | int kind; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13200 | int r; |
| 13201 | Py_ssize_t fmtcnt, fmtpos, arglen, argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13202 | int args_owned = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13203 | PyObject *dict = NULL; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13204 | PyObject *temp = NULL; |
| 13205 | PyObject *second = NULL; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13206 | PyObject *uformat; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13207 | _PyAccu acc; |
| 13208 | static PyObject *plus, *minus, *blank, *zero, *percent; |
| 13209 | |
| 13210 | if (!plus && !(plus = get_latin1_char('+'))) |
| 13211 | return NULL; |
| 13212 | if (!minus && !(minus = get_latin1_char('-'))) |
| 13213 | return NULL; |
| 13214 | if (!blank && !(blank = get_latin1_char(' '))) |
| 13215 | return NULL; |
| 13216 | if (!zero && !(zero = get_latin1_char('0'))) |
| 13217 | return NULL; |
| 13218 | if (!percent && !(percent = get_latin1_char('%'))) |
| 13219 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 13220 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13221 | if (format == NULL || args == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13222 | PyErr_BadInternalCall(); |
| 13223 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13224 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13225 | uformat = PyUnicode_FromObject(format); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13226 | if (uformat == NULL || PyUnicode_READY(uformat) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13227 | return NULL; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13228 | if (_PyAccu_Init(&acc)) |
| 13229 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13230 | fmt = PyUnicode_DATA(uformat); |
| 13231 | fmtkind = PyUnicode_KIND(uformat); |
| 13232 | fmtcnt = PyUnicode_GET_LENGTH(uformat); |
| 13233 | fmtpos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13234 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13235 | if (PyTuple_Check(args)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13236 | arglen = PyTuple_Size(args); |
| 13237 | argidx = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13238 | } |
| 13239 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13240 | arglen = -1; |
| 13241 | argidx = -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13242 | } |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 13243 | if (Py_TYPE(args)->tp_as_mapping && !PyTuple_Check(args) && |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 13244 | !PyUnicode_Check(args)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13245 | dict = args; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13246 | |
| 13247 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13248 | if (PyUnicode_READ(fmtkind, fmt, fmtpos) != '%') { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13249 | PyObject *nonfmt; |
| 13250 | Py_ssize_t nonfmtpos; |
| 13251 | nonfmtpos = fmtpos++; |
| 13252 | while (fmtcnt >= 0 && |
| 13253 | PyUnicode_READ(fmtkind, fmt, fmtpos) != '%') { |
| 13254 | fmtpos++; |
| 13255 | fmtcnt--; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13256 | } |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 13257 | nonfmt = PyUnicode_Substring(uformat, nonfmtpos, fmtpos); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13258 | if (nonfmt == NULL) |
| 13259 | goto onError; |
| 13260 | r = _PyAccu_Accumulate(&acc, nonfmt); |
| 13261 | Py_DECREF(nonfmt); |
| 13262 | if (r) |
| 13263 | goto onError; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13264 | } |
| 13265 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13266 | /* Got a format specifier */ |
| 13267 | int flags = 0; |
| 13268 | Py_ssize_t width = -1; |
| 13269 | int prec = -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13270 | Py_UCS4 c = '\0'; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13271 | Py_UCS4 fill, sign; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13272 | int isnumok; |
| 13273 | PyObject *v = NULL; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13274 | void *pbuf = NULL; |
| 13275 | Py_ssize_t pindex, len; |
| 13276 | PyObject *signobj = NULL, *fillobj = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13277 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13278 | fmtpos++; |
| 13279 | if (PyUnicode_READ(fmtkind, fmt, fmtpos) == '(') { |
| 13280 | Py_ssize_t keystart; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13281 | Py_ssize_t keylen; |
| 13282 | PyObject *key; |
| 13283 | int pcount = 1; |
Christian Heimes | a612dc0 | 2008-02-24 13:08:18 +0000 | [diff] [blame] | 13284 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13285 | if (dict == NULL) { |
| 13286 | PyErr_SetString(PyExc_TypeError, |
| 13287 | "format requires a mapping"); |
| 13288 | goto onError; |
| 13289 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13290 | ++fmtpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13291 | --fmtcnt; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13292 | keystart = fmtpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13293 | /* Skip over balanced parentheses */ |
| 13294 | while (pcount > 0 && --fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13295 | if (PyUnicode_READ(fmtkind, fmt, fmtpos) == ')') |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13296 | --pcount; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13297 | else if (PyUnicode_READ(fmtkind, fmt, fmtpos) == '(') |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13298 | ++pcount; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13299 | fmtpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13300 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13301 | keylen = fmtpos - keystart - 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13302 | if (fmtcnt < 0 || pcount > 0) { |
| 13303 | PyErr_SetString(PyExc_ValueError, |
| 13304 | "incomplete format key"); |
| 13305 | goto onError; |
| 13306 | } |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 13307 | key = PyUnicode_Substring(uformat, |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 13308 | keystart, keystart + keylen); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13309 | if (key == NULL) |
| 13310 | goto onError; |
| 13311 | if (args_owned) { |
| 13312 | Py_DECREF(args); |
| 13313 | args_owned = 0; |
| 13314 | } |
| 13315 | args = PyObject_GetItem(dict, key); |
| 13316 | Py_DECREF(key); |
| 13317 | if (args == NULL) { |
| 13318 | goto onError; |
| 13319 | } |
| 13320 | args_owned = 1; |
| 13321 | arglen = -1; |
| 13322 | argidx = -2; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13323 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13324 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13325 | switch (c = PyUnicode_READ(fmtkind, fmt, fmtpos++)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13326 | case '-': flags |= F_LJUST; continue; |
| 13327 | case '+': flags |= F_SIGN; continue; |
| 13328 | case ' ': flags |= F_BLANK; continue; |
| 13329 | case '#': flags |= F_ALT; continue; |
| 13330 | case '0': flags |= F_ZERO; continue; |
| 13331 | } |
| 13332 | break; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13333 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13334 | if (c == '*') { |
| 13335 | v = getnextarg(args, arglen, &argidx); |
| 13336 | if (v == NULL) |
| 13337 | goto onError; |
| 13338 | if (!PyLong_Check(v)) { |
| 13339 | PyErr_SetString(PyExc_TypeError, |
| 13340 | "* wants int"); |
| 13341 | goto onError; |
| 13342 | } |
| 13343 | width = PyLong_AsLong(v); |
| 13344 | if (width == -1 && PyErr_Occurred()) |
| 13345 | goto onError; |
| 13346 | if (width < 0) { |
| 13347 | flags |= F_LJUST; |
| 13348 | width = -width; |
| 13349 | } |
| 13350 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13351 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13352 | } |
| 13353 | else if (c >= '0' && c <= '9') { |
| 13354 | width = c - '0'; |
| 13355 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13356 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13357 | if (c < '0' || c > '9') |
| 13358 | break; |
| 13359 | if ((width*10) / 10 != width) { |
| 13360 | PyErr_SetString(PyExc_ValueError, |
| 13361 | "width too big"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13362 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13363 | } |
| 13364 | width = width*10 + (c - '0'); |
| 13365 | } |
| 13366 | } |
| 13367 | if (c == '.') { |
| 13368 | prec = 0; |
| 13369 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13370 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13371 | if (c == '*') { |
| 13372 | v = getnextarg(args, arglen, &argidx); |
| 13373 | if (v == NULL) |
| 13374 | goto onError; |
| 13375 | if (!PyLong_Check(v)) { |
| 13376 | PyErr_SetString(PyExc_TypeError, |
| 13377 | "* wants int"); |
| 13378 | goto onError; |
| 13379 | } |
| 13380 | prec = PyLong_AsLong(v); |
| 13381 | if (prec == -1 && PyErr_Occurred()) |
| 13382 | goto onError; |
| 13383 | if (prec < 0) |
| 13384 | prec = 0; |
| 13385 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13386 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13387 | } |
| 13388 | else if (c >= '0' && c <= '9') { |
| 13389 | prec = c - '0'; |
| 13390 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13391 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13392 | if (c < '0' || c > '9') |
| 13393 | break; |
| 13394 | if ((prec*10) / 10 != prec) { |
| 13395 | PyErr_SetString(PyExc_ValueError, |
| 13396 | "prec too big"); |
| 13397 | goto onError; |
| 13398 | } |
| 13399 | prec = prec*10 + (c - '0'); |
| 13400 | } |
| 13401 | } |
| 13402 | } /* prec */ |
| 13403 | if (fmtcnt >= 0) { |
| 13404 | if (c == 'h' || c == 'l' || c == 'L') { |
| 13405 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13406 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13407 | } |
| 13408 | } |
| 13409 | if (fmtcnt < 0) { |
| 13410 | PyErr_SetString(PyExc_ValueError, |
| 13411 | "incomplete format"); |
| 13412 | goto onError; |
| 13413 | } |
| 13414 | if (c != '%') { |
| 13415 | v = getnextarg(args, arglen, &argidx); |
| 13416 | if (v == NULL) |
| 13417 | goto onError; |
| 13418 | } |
| 13419 | sign = 0; |
| 13420 | fill = ' '; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13421 | fillobj = blank; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13422 | switch (c) { |
| 13423 | |
| 13424 | case '%': |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13425 | _PyAccu_Accumulate(&acc, percent); |
| 13426 | continue; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13427 | |
| 13428 | case 's': |
| 13429 | case 'r': |
| 13430 | case 'a': |
Victor Stinner | 808fc0a | 2010-03-22 12:50:40 +0000 | [diff] [blame] | 13431 | if (PyUnicode_CheckExact(v) && c == 's') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13432 | temp = v; |
| 13433 | Py_INCREF(temp); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13434 | } |
| 13435 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13436 | if (c == 's') |
| 13437 | temp = PyObject_Str(v); |
| 13438 | else if (c == 'r') |
| 13439 | temp = PyObject_Repr(v); |
| 13440 | else |
| 13441 | temp = PyObject_ASCII(v); |
| 13442 | if (temp == NULL) |
| 13443 | goto onError; |
| 13444 | if (PyUnicode_Check(temp)) |
| 13445 | /* nothing to do */; |
| 13446 | else { |
| 13447 | Py_DECREF(temp); |
| 13448 | PyErr_SetString(PyExc_TypeError, |
| 13449 | "%s argument has non-string str()"); |
| 13450 | goto onError; |
| 13451 | } |
| 13452 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13453 | if (PyUnicode_READY(temp) == -1) { |
| 13454 | Py_CLEAR(temp); |
| 13455 | goto onError; |
| 13456 | } |
| 13457 | pbuf = PyUnicode_DATA(temp); |
| 13458 | kind = PyUnicode_KIND(temp); |
| 13459 | len = PyUnicode_GET_LENGTH(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13460 | if (prec >= 0 && len > prec) |
| 13461 | len = prec; |
| 13462 | break; |
| 13463 | |
| 13464 | case 'i': |
| 13465 | case 'd': |
| 13466 | case 'u': |
| 13467 | case 'o': |
| 13468 | case 'x': |
| 13469 | case 'X': |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13470 | isnumok = 0; |
| 13471 | if (PyNumber_Check(v)) { |
| 13472 | PyObject *iobj=NULL; |
| 13473 | |
| 13474 | if (PyLong_Check(v)) { |
| 13475 | iobj = v; |
| 13476 | Py_INCREF(iobj); |
| 13477 | } |
| 13478 | else { |
| 13479 | iobj = PyNumber_Long(v); |
| 13480 | } |
| 13481 | if (iobj!=NULL) { |
| 13482 | if (PyLong_Check(iobj)) { |
| 13483 | isnumok = 1; |
Senthil Kumaran | 9ebe08d | 2011-07-03 21:03:16 -0700 | [diff] [blame] | 13484 | temp = formatlong(iobj, flags, prec, (c == 'i'? 'd': c)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13485 | Py_DECREF(iobj); |
| 13486 | if (!temp) |
| 13487 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13488 | if (PyUnicode_READY(temp) == -1) { |
| 13489 | Py_CLEAR(temp); |
| 13490 | goto onError; |
| 13491 | } |
| 13492 | pbuf = PyUnicode_DATA(temp); |
| 13493 | kind = PyUnicode_KIND(temp); |
| 13494 | len = PyUnicode_GET_LENGTH(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13495 | sign = 1; |
| 13496 | } |
| 13497 | else { |
| 13498 | Py_DECREF(iobj); |
| 13499 | } |
| 13500 | } |
| 13501 | } |
| 13502 | if (!isnumok) { |
| 13503 | PyErr_Format(PyExc_TypeError, |
| 13504 | "%%%c format: a number is required, " |
| 13505 | "not %.200s", (char)c, Py_TYPE(v)->tp_name); |
| 13506 | goto onError; |
| 13507 | } |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13508 | if (flags & F_ZERO) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13509 | fill = '0'; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13510 | fillobj = zero; |
| 13511 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13512 | break; |
| 13513 | |
| 13514 | case 'e': |
| 13515 | case 'E': |
| 13516 | case 'f': |
| 13517 | case 'F': |
| 13518 | case 'g': |
| 13519 | case 'G': |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13520 | temp = formatfloat(v, flags, prec, c); |
| 13521 | if (!temp) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13522 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13523 | if (PyUnicode_READY(temp) == -1) { |
| 13524 | Py_CLEAR(temp); |
| 13525 | goto onError; |
| 13526 | } |
| 13527 | pbuf = PyUnicode_DATA(temp); |
| 13528 | kind = PyUnicode_KIND(temp); |
| 13529 | len = PyUnicode_GET_LENGTH(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13530 | sign = 1; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13531 | if (flags & F_ZERO) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13532 | fill = '0'; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13533 | fillobj = zero; |
| 13534 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13535 | break; |
| 13536 | |
| 13537 | case 'c': |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13538 | { |
| 13539 | Py_UCS4 ch = formatchar(v); |
| 13540 | if (ch == (Py_UCS4) -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13541 | goto onError; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13542 | temp = _PyUnicode_FromUCS4(&ch, 1); |
| 13543 | if (temp == NULL) |
| 13544 | goto onError; |
| 13545 | pbuf = PyUnicode_DATA(temp); |
| 13546 | kind = PyUnicode_KIND(temp); |
| 13547 | len = PyUnicode_GET_LENGTH(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13548 | break; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13549 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13550 | |
| 13551 | default: |
| 13552 | PyErr_Format(PyExc_ValueError, |
| 13553 | "unsupported format character '%c' (0x%x) " |
| 13554 | "at index %zd", |
| 13555 | (31<=c && c<=126) ? (char)c : '?', |
| 13556 | (int)c, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13557 | fmtpos - 1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13558 | goto onError; |
| 13559 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13560 | /* pbuf is initialized here. */ |
| 13561 | pindex = 0; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13562 | if (sign) { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13563 | if (PyUnicode_READ(kind, pbuf, pindex) == '-') { |
| 13564 | signobj = minus; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13565 | len--; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13566 | pindex++; |
| 13567 | } |
| 13568 | else if (PyUnicode_READ(kind, pbuf, pindex) == '+') { |
| 13569 | signobj = plus; |
| 13570 | len--; |
| 13571 | pindex++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13572 | } |
| 13573 | else if (flags & F_SIGN) |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13574 | signobj = plus; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13575 | else if (flags & F_BLANK) |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13576 | signobj = blank; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13577 | else |
| 13578 | sign = 0; |
| 13579 | } |
| 13580 | if (width < len) |
| 13581 | width = len; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13582 | if (sign) { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13583 | if (fill != ' ') { |
| 13584 | assert(signobj != NULL); |
| 13585 | if (_PyAccu_Accumulate(&acc, signobj)) |
| 13586 | goto onError; |
| 13587 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13588 | if (width > len) |
| 13589 | width--; |
| 13590 | } |
| 13591 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13592 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13593 | assert(PyUnicode_READ(kind, pbuf, pindex + 1) == c); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13594 | if (fill != ' ') { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13595 | second = get_latin1_char( |
| 13596 | PyUnicode_READ(kind, pbuf, pindex + 1)); |
| 13597 | pindex += 2; |
| 13598 | if (second == NULL || |
| 13599 | _PyAccu_Accumulate(&acc, zero) || |
| 13600 | _PyAccu_Accumulate(&acc, second)) |
| 13601 | goto onError; |
| 13602 | Py_CLEAR(second); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13603 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13604 | width -= 2; |
| 13605 | if (width < 0) |
| 13606 | width = 0; |
| 13607 | len -= 2; |
| 13608 | } |
| 13609 | if (width > len && !(flags & F_LJUST)) { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13610 | assert(fillobj != NULL); |
Antoine Pitrou | 978b9d2 | 2011-10-07 12:35:48 +0200 | [diff] [blame] | 13611 | if (repeat_accumulate(&acc, fillobj, width - len)) |
| 13612 | goto onError; |
| 13613 | width = len; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13614 | } |
| 13615 | if (fill == ' ') { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13616 | if (sign) { |
| 13617 | assert(signobj != NULL); |
| 13618 | if (_PyAccu_Accumulate(&acc, signobj)) |
| 13619 | goto onError; |
| 13620 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13621 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13622 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
| 13623 | assert(PyUnicode_READ(kind, pbuf, pindex+1) == c); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13624 | second = get_latin1_char( |
| 13625 | PyUnicode_READ(kind, pbuf, pindex + 1)); |
| 13626 | pindex += 2; |
| 13627 | if (second == NULL || |
| 13628 | _PyAccu_Accumulate(&acc, zero) || |
| 13629 | _PyAccu_Accumulate(&acc, second)) |
| 13630 | goto onError; |
| 13631 | Py_CLEAR(second); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13632 | } |
| 13633 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13634 | /* Copy all characters, preserving len */ |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13635 | if (temp != NULL) { |
| 13636 | assert(pbuf == PyUnicode_DATA(temp)); |
| 13637 | v = PyUnicode_Substring(temp, pindex, pindex + len); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13638 | } |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13639 | else { |
| 13640 | const char *p = (const char *) pbuf; |
| 13641 | assert(pbuf != NULL); |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 13642 | p += kind * pindex; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13643 | v = PyUnicode_FromKindAndData(kind, p, len); |
| 13644 | } |
| 13645 | if (v == NULL) |
| 13646 | goto onError; |
| 13647 | r = _PyAccu_Accumulate(&acc, v); |
| 13648 | Py_DECREF(v); |
| 13649 | if (r) |
| 13650 | goto onError; |
Antoine Pitrou | 978b9d2 | 2011-10-07 12:35:48 +0200 | [diff] [blame] | 13651 | if (width > len && repeat_accumulate(&acc, blank, width - len)) |
| 13652 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13653 | if (dict && (argidx < arglen) && c != '%') { |
| 13654 | PyErr_SetString(PyExc_TypeError, |
| 13655 | "not all arguments converted during string formatting"); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13656 | goto onError; |
| 13657 | } |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13658 | Py_CLEAR(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13659 | } /* '%' */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13660 | } /* until end */ |
| 13661 | if (argidx < arglen && !dict) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13662 | PyErr_SetString(PyExc_TypeError, |
| 13663 | "not all arguments converted during string formatting"); |
| 13664 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13665 | } |
| 13666 | |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13667 | result = _PyAccu_Finish(&acc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13668 | if (args_owned) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13669 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13670 | } |
| 13671 | Py_DECREF(uformat); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13672 | Py_XDECREF(temp); |
| 13673 | Py_XDECREF(second); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 13674 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13675 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13676 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13677 | Py_DECREF(uformat); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13678 | Py_XDECREF(temp); |
| 13679 | Py_XDECREF(second); |
| 13680 | _PyAccu_Destroy(&acc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13681 | if (args_owned) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13682 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13683 | } |
| 13684 | return NULL; |
| 13685 | } |
| 13686 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 13687 | static PyObject * |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 13688 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 13689 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13690 | static PyObject * |
| 13691 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 13692 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13693 | PyObject *x = NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13694 | static char *kwlist[] = {"object", "encoding", "errors", 0}; |
| 13695 | char *encoding = NULL; |
| 13696 | char *errors = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13697 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13698 | if (type != &PyUnicode_Type) |
| 13699 | return unicode_subtype_new(type, args, kwds); |
| 13700 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:str", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13701 | kwlist, &x, &encoding, &errors)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13702 | return NULL; |
| 13703 | if (x == NULL) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 13704 | return PyUnicode_New(0, 0); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13705 | if (encoding == NULL && errors == NULL) |
| 13706 | return PyObject_Str(x); |
| 13707 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13708 | return PyUnicode_FromEncodedObject(x, encoding, errors); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13709 | } |
| 13710 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 13711 | static PyObject * |
| 13712 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 13713 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13714 | PyObject *unicode, *self; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13715 | Py_ssize_t length, char_size; |
| 13716 | int share_wstr, share_utf8; |
| 13717 | unsigned int kind; |
| 13718 | void *data; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 13719 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13720 | assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13721 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13722 | unicode = unicode_new(&PyUnicode_Type, args, kwds); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13723 | if (unicode == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13724 | return NULL; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 13725 | assert(_PyUnicode_CHECK(unicode)); |
Victor Stinner | e06e145 | 2011-10-04 20:52:31 +0200 | [diff] [blame] | 13726 | if (PyUnicode_READY(unicode)) |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13727 | return NULL; |
| 13728 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13729 | self = type->tp_alloc(type, 0); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13730 | if (self == NULL) { |
| 13731 | Py_DECREF(unicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13732 | return NULL; |
| 13733 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13734 | kind = PyUnicode_KIND(unicode); |
| 13735 | length = PyUnicode_GET_LENGTH(unicode); |
| 13736 | |
| 13737 | _PyUnicode_LENGTH(self) = length; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 13738 | #ifdef Py_DEBUG |
| 13739 | _PyUnicode_HASH(self) = -1; |
| 13740 | #else |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13741 | _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 13742 | #endif |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13743 | _PyUnicode_STATE(self).interned = 0; |
| 13744 | _PyUnicode_STATE(self).kind = kind; |
| 13745 | _PyUnicode_STATE(self).compact = 0; |
Victor Stinner | 3cf4637 | 2011-10-03 14:42:15 +0200 | [diff] [blame] | 13746 | _PyUnicode_STATE(self).ascii = _PyUnicode_STATE(unicode).ascii; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13747 | _PyUnicode_STATE(self).ready = 1; |
| 13748 | _PyUnicode_WSTR(self) = NULL; |
| 13749 | _PyUnicode_UTF8_LENGTH(self) = 0; |
| 13750 | _PyUnicode_UTF8(self) = NULL; |
| 13751 | _PyUnicode_WSTR_LENGTH(self) = 0; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 13752 | _PyUnicode_DATA_ANY(self) = NULL; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13753 | |
| 13754 | share_utf8 = 0; |
| 13755 | share_wstr = 0; |
| 13756 | if (kind == PyUnicode_1BYTE_KIND) { |
| 13757 | char_size = 1; |
| 13758 | if (PyUnicode_MAX_CHAR_VALUE(unicode) < 128) |
| 13759 | share_utf8 = 1; |
| 13760 | } |
| 13761 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 13762 | char_size = 2; |
| 13763 | if (sizeof(wchar_t) == 2) |
| 13764 | share_wstr = 1; |
| 13765 | } |
| 13766 | else { |
| 13767 | assert(kind == PyUnicode_4BYTE_KIND); |
| 13768 | char_size = 4; |
| 13769 | if (sizeof(wchar_t) == 4) |
| 13770 | share_wstr = 1; |
| 13771 | } |
| 13772 | |
| 13773 | /* Ensure we won't overflow the length. */ |
| 13774 | if (length > (PY_SSIZE_T_MAX / char_size - 1)) { |
| 13775 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13776 | goto onError; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13777 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13778 | data = PyObject_MALLOC((length + 1) * char_size); |
| 13779 | if (data == NULL) { |
| 13780 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13781 | goto onError; |
| 13782 | } |
| 13783 | |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 13784 | _PyUnicode_DATA_ANY(self) = data; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13785 | if (share_utf8) { |
| 13786 | _PyUnicode_UTF8_LENGTH(self) = length; |
| 13787 | _PyUnicode_UTF8(self) = data; |
| 13788 | } |
| 13789 | if (share_wstr) { |
| 13790 | _PyUnicode_WSTR_LENGTH(self) = length; |
| 13791 | _PyUnicode_WSTR(self) = (wchar_t *)data; |
| 13792 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13793 | |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13794 | Py_MEMCPY(data, PyUnicode_DATA(unicode), |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 13795 | kind * (length + 1)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13796 | assert(_PyUnicode_CheckConsistency(self, 1)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 13797 | #ifdef Py_DEBUG |
| 13798 | _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode); |
| 13799 | #endif |
Victor Stinner | dd18d3a | 2011-10-22 11:08:10 +0200 | [diff] [blame] | 13800 | Py_DECREF(unicode); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 13801 | return self; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13802 | |
| 13803 | onError: |
| 13804 | Py_DECREF(unicode); |
| 13805 | Py_DECREF(self); |
| 13806 | return NULL; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 13807 | } |
| 13808 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 13809 | PyDoc_STRVAR(unicode_doc, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13810 | "str(string[, encoding[, errors]]) -> str\n\ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13811 | \n\ |
Collin Winter | d474ce8 | 2007-08-07 19:42:11 +0000 | [diff] [blame] | 13812 | Create a new string object from the given encoded string.\n\ |
Skip Montanaro | 35b37a5 | 2002-07-26 16:22:46 +0000 | [diff] [blame] | 13813 | encoding defaults to the current default string encoding.\n\ |
| 13814 | errors can be 'strict', 'replace' or 'ignore' and defaults to 'strict'."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13815 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13816 | static PyObject *unicode_iter(PyObject *seq); |
| 13817 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13818 | PyTypeObject PyUnicode_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 13819 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13820 | "str", /* tp_name */ |
| 13821 | sizeof(PyUnicodeObject), /* tp_size */ |
| 13822 | 0, /* tp_itemsize */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13823 | /* Slots */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13824 | (destructor)unicode_dealloc, /* tp_dealloc */ |
| 13825 | 0, /* tp_print */ |
| 13826 | 0, /* tp_getattr */ |
| 13827 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 13828 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13829 | unicode_repr, /* tp_repr */ |
| 13830 | &unicode_as_number, /* tp_as_number */ |
| 13831 | &unicode_as_sequence, /* tp_as_sequence */ |
| 13832 | &unicode_as_mapping, /* tp_as_mapping */ |
| 13833 | (hashfunc) unicode_hash, /* tp_hash*/ |
| 13834 | 0, /* tp_call*/ |
| 13835 | (reprfunc) unicode_str, /* tp_str */ |
| 13836 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 13837 | 0, /* tp_setattro */ |
| 13838 | 0, /* tp_as_buffer */ |
| 13839 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13840 | Py_TPFLAGS_UNICODE_SUBCLASS, /* tp_flags */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13841 | unicode_doc, /* tp_doc */ |
| 13842 | 0, /* tp_traverse */ |
| 13843 | 0, /* tp_clear */ |
| 13844 | PyUnicode_RichCompare, /* tp_richcompare */ |
| 13845 | 0, /* tp_weaklistoffset */ |
| 13846 | unicode_iter, /* tp_iter */ |
| 13847 | 0, /* tp_iternext */ |
| 13848 | unicode_methods, /* tp_methods */ |
| 13849 | 0, /* tp_members */ |
| 13850 | 0, /* tp_getset */ |
| 13851 | &PyBaseObject_Type, /* tp_base */ |
| 13852 | 0, /* tp_dict */ |
| 13853 | 0, /* tp_descr_get */ |
| 13854 | 0, /* tp_descr_set */ |
| 13855 | 0, /* tp_dictoffset */ |
| 13856 | 0, /* tp_init */ |
| 13857 | 0, /* tp_alloc */ |
| 13858 | unicode_new, /* tp_new */ |
| 13859 | PyObject_Del, /* tp_free */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13860 | }; |
| 13861 | |
| 13862 | /* Initialize the Unicode implementation */ |
| 13863 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 13864 | int _PyUnicode_Init(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13865 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 13866 | int i; |
| 13867 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13868 | /* XXX - move this array to unicodectype.c ? */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13869 | Py_UCS2 linebreak[] = { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13870 | 0x000A, /* LINE FEED */ |
| 13871 | 0x000D, /* CARRIAGE RETURN */ |
| 13872 | 0x001C, /* FILE SEPARATOR */ |
| 13873 | 0x001D, /* GROUP SEPARATOR */ |
| 13874 | 0x001E, /* RECORD SEPARATOR */ |
| 13875 | 0x0085, /* NEXT LINE */ |
| 13876 | 0x2028, /* LINE SEPARATOR */ |
| 13877 | 0x2029, /* PARAGRAPH SEPARATOR */ |
| 13878 | }; |
| 13879 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 13880 | /* Init the implementation */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 13881 | unicode_empty = PyUnicode_New(0, 0); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13882 | assert(_PyUnicode_CheckConsistency(unicode_empty, 1)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13883 | if (!unicode_empty) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13884 | Py_FatalError("Can't create empty string"); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13885 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 13886 | for (i = 0; i < 256; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13887 | unicode_latin1[i] = NULL; |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 13888 | if (PyType_Ready(&PyUnicode_Type) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13889 | Py_FatalError("Can't initialize 'unicode'"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13890 | |
| 13891 | /* initialize the linebreak bloom filter */ |
| 13892 | bloom_linebreak = make_bloom_mask( |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13893 | PyUnicode_2BYTE_KIND, linebreak, |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 13894 | Py_ARRAY_LENGTH(linebreak)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13895 | |
| 13896 | PyType_Ready(&EncodingMapType); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 13897 | |
| 13898 | #ifdef HAVE_MBCS |
| 13899 | winver.dwOSVersionInfoSize = sizeof(winver); |
| 13900 | if (!GetVersionEx((OSVERSIONINFO*)&winver)) { |
| 13901 | PyErr_SetFromWindowsErr(0); |
| 13902 | return -1; |
| 13903 | } |
| 13904 | #endif |
| 13905 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13906 | } |
| 13907 | |
| 13908 | /* Finalize the Unicode implementation */ |
| 13909 | |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 13910 | int |
| 13911 | PyUnicode_ClearFreeList(void) |
| 13912 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13913 | return 0; |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 13914 | } |
| 13915 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13916 | void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 13917 | _PyUnicode_Fini(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13918 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 13919 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13920 | |
Guido van Rossum | 4ae8ef8 | 2000-10-03 18:09:04 +0000 | [diff] [blame] | 13921 | Py_XDECREF(unicode_empty); |
| 13922 | unicode_empty = NULL; |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 13923 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 13924 | for (i = 0; i < 256; i++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13925 | if (unicode_latin1[i]) { |
| 13926 | Py_DECREF(unicode_latin1[i]); |
| 13927 | unicode_latin1[i] = NULL; |
| 13928 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 13929 | } |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 13930 | _PyUnicode_ClearStaticStrings(); |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 13931 | (void)PyUnicode_ClearFreeList(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13932 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 13933 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13934 | void |
| 13935 | PyUnicode_InternInPlace(PyObject **p) |
| 13936 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13937 | register PyObject *s = *p; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13938 | PyObject *t; |
Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 13939 | #ifdef Py_DEBUG |
| 13940 | assert(s != NULL); |
| 13941 | assert(_PyUnicode_CHECK(s)); |
| 13942 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13943 | if (s == NULL || !PyUnicode_Check(s)) |
Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 13944 | return; |
| 13945 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13946 | /* If it's a subclass, we don't really know what putting |
| 13947 | it in the interned dict might do. */ |
| 13948 | if (!PyUnicode_CheckExact(s)) |
| 13949 | return; |
| 13950 | if (PyUnicode_CHECK_INTERNED(s)) |
| 13951 | return; |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 13952 | if (_PyUnicode_READY_REPLACE(p)) { |
Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 13953 | assert(0 && "_PyUnicode_READY_REPLACE fail in PyUnicode_InternInPlace"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13954 | return; |
| 13955 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13956 | s = *p; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13957 | if (interned == NULL) { |
| 13958 | interned = PyDict_New(); |
| 13959 | if (interned == NULL) { |
| 13960 | PyErr_Clear(); /* Don't leave an exception */ |
| 13961 | return; |
| 13962 | } |
| 13963 | } |
| 13964 | /* It might be that the GetItem call fails even |
| 13965 | though the key is present in the dictionary, |
| 13966 | namely when this happens during a stack overflow. */ |
| 13967 | Py_ALLOW_RECURSION |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 13968 | t = PyDict_GetItem(interned, s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13969 | Py_END_ALLOW_RECURSION |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 13970 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13971 | if (t) { |
| 13972 | Py_INCREF(t); |
| 13973 | Py_DECREF(*p); |
| 13974 | *p = t; |
| 13975 | return; |
| 13976 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13977 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13978 | PyThreadState_GET()->recursion_critical = 1; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 13979 | if (PyDict_SetItem(interned, s, s) < 0) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13980 | PyErr_Clear(); |
| 13981 | PyThreadState_GET()->recursion_critical = 0; |
| 13982 | return; |
| 13983 | } |
| 13984 | PyThreadState_GET()->recursion_critical = 0; |
| 13985 | /* The two references in interned are not counted by refcnt. |
| 13986 | The deallocator will take care of this */ |
| 13987 | Py_REFCNT(s) -= 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13988 | _PyUnicode_STATE(s).interned = SSTATE_INTERNED_MORTAL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13989 | } |
| 13990 | |
| 13991 | void |
| 13992 | PyUnicode_InternImmortal(PyObject **p) |
| 13993 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13994 | PyUnicode_InternInPlace(p); |
| 13995 | if (PyUnicode_CHECK_INTERNED(*p) != SSTATE_INTERNED_IMMORTAL) { |
Victor Stinner | af9e4b8 | 2011-10-23 20:07:00 +0200 | [diff] [blame] | 13996 | _PyUnicode_STATE(*p).interned = SSTATE_INTERNED_IMMORTAL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13997 | Py_INCREF(*p); |
| 13998 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13999 | } |
| 14000 | |
| 14001 | PyObject * |
| 14002 | PyUnicode_InternFromString(const char *cp) |
| 14003 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14004 | PyObject *s = PyUnicode_FromString(cp); |
| 14005 | if (s == NULL) |
| 14006 | return NULL; |
| 14007 | PyUnicode_InternInPlace(&s); |
| 14008 | return s; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14009 | } |
| 14010 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 14011 | void |
| 14012 | _Py_ReleaseInternedUnicodeStrings(void) |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14013 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14014 | PyObject *keys; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14015 | PyObject *s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14016 | Py_ssize_t i, n; |
| 14017 | Py_ssize_t immortal_size = 0, mortal_size = 0; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14018 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14019 | if (interned == NULL || !PyDict_Check(interned)) |
| 14020 | return; |
| 14021 | keys = PyDict_Keys(interned); |
| 14022 | if (keys == NULL || !PyList_Check(keys)) { |
| 14023 | PyErr_Clear(); |
| 14024 | return; |
| 14025 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14026 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14027 | /* Since _Py_ReleaseInternedUnicodeStrings() is intended to help a leak |
| 14028 | detector, interned unicode strings are not forcibly deallocated; |
| 14029 | rather, we give them their stolen references back, and then clear |
| 14030 | and DECREF the interned dict. */ |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14031 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14032 | n = PyList_GET_SIZE(keys); |
| 14033 | fprintf(stderr, "releasing %" PY_FORMAT_SIZE_T "d interned strings\n", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14034 | n); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14035 | for (i = 0; i < n; i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14036 | s = PyList_GET_ITEM(keys, i); |
Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 14037 | if (PyUnicode_READY(s) == -1) { |
| 14038 | assert(0 && "could not ready string"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14039 | fprintf(stderr, "could not ready string\n"); |
Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 14040 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14041 | switch (PyUnicode_CHECK_INTERNED(s)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14042 | case SSTATE_NOT_INTERNED: |
| 14043 | /* XXX Shouldn't happen */ |
| 14044 | break; |
| 14045 | case SSTATE_INTERNED_IMMORTAL: |
| 14046 | Py_REFCNT(s) += 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14047 | immortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14048 | break; |
| 14049 | case SSTATE_INTERNED_MORTAL: |
| 14050 | Py_REFCNT(s) += 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14051 | mortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14052 | break; |
| 14053 | default: |
| 14054 | Py_FatalError("Inconsistent interned string state."); |
| 14055 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14056 | _PyUnicode_STATE(s).interned = SSTATE_NOT_INTERNED; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14057 | } |
| 14058 | fprintf(stderr, "total size of all interned strings: " |
| 14059 | "%" PY_FORMAT_SIZE_T "d/%" PY_FORMAT_SIZE_T "d " |
| 14060 | "mortal/immortal\n", mortal_size, immortal_size); |
| 14061 | Py_DECREF(keys); |
| 14062 | PyDict_Clear(interned); |
| 14063 | Py_DECREF(interned); |
| 14064 | interned = NULL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14065 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14066 | |
| 14067 | |
| 14068 | /********************* Unicode Iterator **************************/ |
| 14069 | |
| 14070 | typedef struct { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14071 | PyObject_HEAD |
| 14072 | Py_ssize_t it_index; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14073 | PyObject *it_seq; /* Set to NULL when iterator is exhausted */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14074 | } unicodeiterobject; |
| 14075 | |
| 14076 | static void |
| 14077 | unicodeiter_dealloc(unicodeiterobject *it) |
| 14078 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14079 | _PyObject_GC_UNTRACK(it); |
| 14080 | Py_XDECREF(it->it_seq); |
| 14081 | PyObject_GC_Del(it); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14082 | } |
| 14083 | |
| 14084 | static int |
| 14085 | unicodeiter_traverse(unicodeiterobject *it, visitproc visit, void *arg) |
| 14086 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14087 | Py_VISIT(it->it_seq); |
| 14088 | return 0; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14089 | } |
| 14090 | |
| 14091 | static PyObject * |
| 14092 | unicodeiter_next(unicodeiterobject *it) |
| 14093 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14094 | PyObject *seq, *item; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14095 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14096 | assert(it != NULL); |
| 14097 | seq = it->it_seq; |
| 14098 | if (seq == NULL) |
| 14099 | return NULL; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 14100 | assert(_PyUnicode_CHECK(seq)); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14101 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14102 | if (it->it_index < PyUnicode_GET_LENGTH(seq)) { |
| 14103 | int kind = PyUnicode_KIND(seq); |
| 14104 | void *data = PyUnicode_DATA(seq); |
| 14105 | Py_UCS4 chr = PyUnicode_READ(kind, data, it->it_index); |
| 14106 | item = PyUnicode_FromOrdinal(chr); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14107 | if (item != NULL) |
| 14108 | ++it->it_index; |
| 14109 | return item; |
| 14110 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14111 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14112 | Py_DECREF(seq); |
| 14113 | it->it_seq = NULL; |
| 14114 | return NULL; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14115 | } |
| 14116 | |
| 14117 | static PyObject * |
| 14118 | unicodeiter_len(unicodeiterobject *it) |
| 14119 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14120 | Py_ssize_t len = 0; |
| 14121 | if (it->it_seq) |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 14122 | len = PyUnicode_GET_LENGTH(it->it_seq) - it->it_index; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14123 | return PyLong_FromSsize_t(len); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14124 | } |
| 14125 | |
| 14126 | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
| 14127 | |
| 14128 | static PyMethodDef unicodeiter_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14129 | {"__length_hint__", (PyCFunction)unicodeiter_len, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14130 | length_hint_doc}, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14131 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14132 | }; |
| 14133 | |
| 14134 | PyTypeObject PyUnicodeIter_Type = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14135 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 14136 | "str_iterator", /* tp_name */ |
| 14137 | sizeof(unicodeiterobject), /* tp_basicsize */ |
| 14138 | 0, /* tp_itemsize */ |
| 14139 | /* methods */ |
| 14140 | (destructor)unicodeiter_dealloc, /* tp_dealloc */ |
| 14141 | 0, /* tp_print */ |
| 14142 | 0, /* tp_getattr */ |
| 14143 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 14144 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14145 | 0, /* tp_repr */ |
| 14146 | 0, /* tp_as_number */ |
| 14147 | 0, /* tp_as_sequence */ |
| 14148 | 0, /* tp_as_mapping */ |
| 14149 | 0, /* tp_hash */ |
| 14150 | 0, /* tp_call */ |
| 14151 | 0, /* tp_str */ |
| 14152 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 14153 | 0, /* tp_setattro */ |
| 14154 | 0, /* tp_as_buffer */ |
| 14155 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 14156 | 0, /* tp_doc */ |
| 14157 | (traverseproc)unicodeiter_traverse, /* tp_traverse */ |
| 14158 | 0, /* tp_clear */ |
| 14159 | 0, /* tp_richcompare */ |
| 14160 | 0, /* tp_weaklistoffset */ |
| 14161 | PyObject_SelfIter, /* tp_iter */ |
| 14162 | (iternextfunc)unicodeiter_next, /* tp_iternext */ |
| 14163 | unicodeiter_methods, /* tp_methods */ |
| 14164 | 0, |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14165 | }; |
| 14166 | |
| 14167 | static PyObject * |
| 14168 | unicode_iter(PyObject *seq) |
| 14169 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14170 | unicodeiterobject *it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14171 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14172 | if (!PyUnicode_Check(seq)) { |
| 14173 | PyErr_BadInternalCall(); |
| 14174 | return NULL; |
| 14175 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14176 | if (PyUnicode_READY(seq) == -1) |
| 14177 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14178 | it = PyObject_GC_New(unicodeiterobject, &PyUnicodeIter_Type); |
| 14179 | if (it == NULL) |
| 14180 | return NULL; |
| 14181 | it->it_index = 0; |
| 14182 | Py_INCREF(seq); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14183 | it->it_seq = seq; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14184 | _PyObject_GC_TRACK(it); |
| 14185 | return (PyObject *)it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14186 | } |
| 14187 | |
Martin v. Löwis | 0d3072e | 2011-10-31 08:40:56 +0100 | [diff] [blame] | 14188 | |
| 14189 | size_t |
| 14190 | Py_UNICODE_strlen(const Py_UNICODE *u) |
| 14191 | { |
| 14192 | int res = 0; |
| 14193 | while(*u++) |
| 14194 | res++; |
| 14195 | return res; |
| 14196 | } |
| 14197 | |
| 14198 | Py_UNICODE* |
| 14199 | Py_UNICODE_strcpy(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 14200 | { |
| 14201 | Py_UNICODE *u = s1; |
| 14202 | while ((*u++ = *s2++)); |
| 14203 | return s1; |
| 14204 | } |
| 14205 | |
| 14206 | Py_UNICODE* |
| 14207 | Py_UNICODE_strncpy(Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 14208 | { |
| 14209 | Py_UNICODE *u = s1; |
| 14210 | while ((*u++ = *s2++)) |
| 14211 | if (n-- == 0) |
| 14212 | break; |
| 14213 | return s1; |
| 14214 | } |
| 14215 | |
| 14216 | Py_UNICODE* |
| 14217 | Py_UNICODE_strcat(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 14218 | { |
| 14219 | Py_UNICODE *u1 = s1; |
| 14220 | u1 += Py_UNICODE_strlen(u1); |
| 14221 | Py_UNICODE_strcpy(u1, s2); |
| 14222 | return s1; |
| 14223 | } |
| 14224 | |
| 14225 | int |
| 14226 | Py_UNICODE_strcmp(const Py_UNICODE *s1, const Py_UNICODE *s2) |
| 14227 | { |
| 14228 | while (*s1 && *s2 && *s1 == *s2) |
| 14229 | s1++, s2++; |
| 14230 | if (*s1 && *s2) |
| 14231 | return (*s1 < *s2) ? -1 : +1; |
| 14232 | if (*s1) |
| 14233 | return 1; |
| 14234 | if (*s2) |
| 14235 | return -1; |
| 14236 | return 0; |
| 14237 | } |
| 14238 | |
| 14239 | int |
| 14240 | Py_UNICODE_strncmp(const Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 14241 | { |
| 14242 | register Py_UNICODE u1, u2; |
| 14243 | for (; n != 0; n--) { |
| 14244 | u1 = *s1; |
| 14245 | u2 = *s2; |
| 14246 | if (u1 != u2) |
| 14247 | return (u1 < u2) ? -1 : +1; |
| 14248 | if (u1 == '\0') |
| 14249 | return 0; |
| 14250 | s1++; |
| 14251 | s2++; |
| 14252 | } |
| 14253 | return 0; |
| 14254 | } |
| 14255 | |
| 14256 | Py_UNICODE* |
| 14257 | Py_UNICODE_strchr(const Py_UNICODE *s, Py_UNICODE c) |
| 14258 | { |
| 14259 | const Py_UNICODE *p; |
| 14260 | for (p = s; *p; p++) |
| 14261 | if (*p == c) |
| 14262 | return (Py_UNICODE*)p; |
| 14263 | return NULL; |
| 14264 | } |
| 14265 | |
| 14266 | Py_UNICODE* |
| 14267 | Py_UNICODE_strrchr(const Py_UNICODE *s, Py_UNICODE c) |
| 14268 | { |
| 14269 | const Py_UNICODE *p; |
| 14270 | p = s + Py_UNICODE_strlen(s); |
| 14271 | while (p != s) { |
| 14272 | p--; |
| 14273 | if (*p == c) |
| 14274 | return (Py_UNICODE*)p; |
| 14275 | } |
| 14276 | return NULL; |
| 14277 | } |
Victor Stinner | 331ea92 | 2010-08-10 16:37:20 +0000 | [diff] [blame] | 14278 | |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14279 | Py_UNICODE* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14280 | PyUnicode_AsUnicodeCopy(PyObject *unicode) |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14281 | { |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 14282 | Py_UNICODE *u, *copy; |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14283 | Py_ssize_t len, size; |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14284 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14285 | if (!PyUnicode_Check(unicode)) { |
| 14286 | PyErr_BadArgument(); |
| 14287 | return NULL; |
| 14288 | } |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14289 | u = PyUnicode_AsUnicodeAndSize(unicode, &len); |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 14290 | if (u == NULL) |
| 14291 | return NULL; |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14292 | /* Ensure we won't overflow the size. */ |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14293 | if (len > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14294 | PyErr_NoMemory(); |
| 14295 | return NULL; |
| 14296 | } |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14297 | size = len + 1; /* copy the null character */ |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14298 | size *= sizeof(Py_UNICODE); |
| 14299 | copy = PyMem_Malloc(size); |
| 14300 | if (copy == NULL) { |
| 14301 | PyErr_NoMemory(); |
| 14302 | return NULL; |
| 14303 | } |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 14304 | memcpy(copy, u, size); |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14305 | return copy; |
| 14306 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 14307 | |
Georg Brandl | 66c221e | 2010-10-14 07:04:07 +0000 | [diff] [blame] | 14308 | /* A _string module, to export formatter_parser and formatter_field_name_split |
| 14309 | to the string.Formatter class implemented in Python. */ |
| 14310 | |
| 14311 | static PyMethodDef _string_methods[] = { |
| 14312 | {"formatter_field_name_split", (PyCFunction) formatter_field_name_split, |
| 14313 | METH_O, PyDoc_STR("split the argument as a field name")}, |
| 14314 | {"formatter_parser", (PyCFunction) formatter_parser, |
| 14315 | METH_O, PyDoc_STR("parse the argument as a format string")}, |
| 14316 | {NULL, NULL} |
| 14317 | }; |
| 14318 | |
| 14319 | static struct PyModuleDef _string_module = { |
| 14320 | PyModuleDef_HEAD_INIT, |
| 14321 | "_string", |
| 14322 | PyDoc_STR("string helper module"), |
| 14323 | 0, |
| 14324 | _string_methods, |
| 14325 | NULL, |
| 14326 | NULL, |
| 14327 | NULL, |
| 14328 | NULL |
| 14329 | }; |
| 14330 | |
| 14331 | PyMODINIT_FUNC |
| 14332 | PyInit__string(void) |
| 14333 | { |
| 14334 | return PyModule_Create(&_string_module); |
| 14335 | } |
| 14336 | |
| 14337 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 14338 | #ifdef __cplusplus |
| 14339 | } |
| 14340 | #endif |