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, |
| 257 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 258 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 259 | const char *reason); |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 260 | static void |
| 261 | raise_encode_exception_obj(PyObject **exceptionObject, |
| 262 | const char *encoding, |
| 263 | PyObject *unicode, |
| 264 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 265 | const char *reason); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 266 | |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 267 | /* Same for linebreaks */ |
| 268 | static unsigned char ascii_linebreak[] = { |
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 | /* 0x000A, * LINE FEED */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 271 | /* 0x000B, * LINE TABULATION */ |
| 272 | /* 0x000C, * FORM FEED */ |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 273 | /* 0x000D, * CARRIAGE RETURN */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 274 | 0, 0, 1, 1, 1, 1, 0, 0, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 275 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 276 | /* 0x001C, * FILE SEPARATOR */ |
| 277 | /* 0x001D, * GROUP SEPARATOR */ |
| 278 | /* 0x001E, * RECORD SEPARATOR */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 279 | 0, 0, 0, 0, 1, 1, 1, 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, |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 284 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 285 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 286 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 287 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 288 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 289 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 290 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 291 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 292 | 0, 0, 0, 0, 0, 0, 0, 0 |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 293 | }; |
| 294 | |
Ezio Melotti | 48a2f8f | 2011-09-29 00:18:19 +0300 | [diff] [blame] | 295 | /* The max unicode value is always 0x10FFFF while using the PEP-393 API. |
| 296 | 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] | 297 | Py_UNICODE |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 298 | PyUnicode_GetMax(void) |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 299 | { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 300 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 301 | return 0x10FFFF; |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 302 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 303 | /* This is actually an illegal character, so it should |
| 304 | not be passed to unichr. */ |
| 305 | return 0xFFFF; |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 306 | #endif |
| 307 | } |
| 308 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 309 | #ifdef Py_DEBUG |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 310 | int |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 311 | _PyUnicode_CheckConsistency(PyObject *op, int check_content) |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 312 | { |
| 313 | PyASCIIObject *ascii; |
| 314 | unsigned int kind; |
| 315 | |
| 316 | assert(PyUnicode_Check(op)); |
| 317 | |
| 318 | ascii = (PyASCIIObject *)op; |
| 319 | kind = ascii->state.kind; |
| 320 | |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 321 | if (ascii->state.ascii == 1 && ascii->state.compact == 1) { |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 322 | assert(kind == PyUnicode_1BYTE_KIND); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 323 | assert(ascii->state.ready == 1); |
| 324 | } |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 325 | else { |
Victor Stinner | 85041a5 | 2011-10-03 14:42:39 +0200 | [diff] [blame] | 326 | PyCompactUnicodeObject *compact = (PyCompactUnicodeObject *)op; |
Victor Stinner | 7f11ad4 | 2011-10-04 00:00:20 +0200 | [diff] [blame] | 327 | void *data; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 328 | |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 329 | if (ascii->state.compact == 1) { |
| 330 | data = compact + 1; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 331 | assert(kind == PyUnicode_1BYTE_KIND |
| 332 | || kind == PyUnicode_2BYTE_KIND |
| 333 | || kind == PyUnicode_4BYTE_KIND); |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 334 | assert(ascii->state.ascii == 0); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 335 | assert(ascii->state.ready == 1); |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 336 | assert (compact->utf8 != data); |
| 337 | } else { |
| 338 | PyUnicodeObject *unicode = (PyUnicodeObject *)op; |
| 339 | |
| 340 | data = unicode->data.any; |
| 341 | if (kind == PyUnicode_WCHAR_KIND) { |
| 342 | assert(ascii->state.compact == 0); |
| 343 | assert(ascii->state.ascii == 0); |
| 344 | assert(ascii->state.ready == 0); |
| 345 | assert(ascii->wstr != NULL); |
| 346 | assert(data == NULL); |
| 347 | assert(compact->utf8 == NULL); |
| 348 | assert(ascii->state.interned == SSTATE_NOT_INTERNED); |
| 349 | } |
| 350 | else { |
| 351 | assert(kind == PyUnicode_1BYTE_KIND |
| 352 | || kind == PyUnicode_2BYTE_KIND |
| 353 | || kind == PyUnicode_4BYTE_KIND); |
| 354 | assert(ascii->state.compact == 0); |
| 355 | assert(ascii->state.ready == 1); |
| 356 | assert(data != NULL); |
| 357 | if (ascii->state.ascii) { |
| 358 | assert (compact->utf8 == data); |
| 359 | assert (compact->utf8_length == ascii->length); |
| 360 | } |
| 361 | else |
| 362 | assert (compact->utf8 != data); |
| 363 | } |
| 364 | } |
| 365 | if (kind != PyUnicode_WCHAR_KIND) { |
Victor Stinner | 7f11ad4 | 2011-10-04 00:00:20 +0200 | [diff] [blame] | 366 | if ( |
| 367 | #if SIZEOF_WCHAR_T == 2 |
| 368 | kind == PyUnicode_2BYTE_KIND |
| 369 | #else |
| 370 | kind == PyUnicode_4BYTE_KIND |
| 371 | #endif |
| 372 | ) |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 373 | { |
| 374 | assert(ascii->wstr == data); |
| 375 | assert(compact->wstr_length == ascii->length); |
| 376 | } else |
| 377 | assert(ascii->wstr != data); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 378 | } |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 379 | |
| 380 | if (compact->utf8 == NULL) |
| 381 | assert(compact->utf8_length == 0); |
| 382 | if (ascii->wstr == NULL) |
| 383 | assert(compact->wstr_length == 0); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 384 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 385 | /* check that the best kind is used */ |
| 386 | if (check_content && kind != PyUnicode_WCHAR_KIND) |
| 387 | { |
| 388 | Py_ssize_t i; |
| 389 | Py_UCS4 maxchar = 0; |
| 390 | void *data = PyUnicode_DATA(ascii); |
| 391 | for (i=0; i < ascii->length; i++) |
| 392 | { |
| 393 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 394 | if (ch > maxchar) |
| 395 | maxchar = ch; |
| 396 | } |
| 397 | if (kind == PyUnicode_1BYTE_KIND) { |
| 398 | if (ascii->state.ascii == 0) |
| 399 | assert(maxchar >= 128); |
| 400 | else |
| 401 | assert(maxchar < 128); |
| 402 | } |
| 403 | else if (kind == PyUnicode_2BYTE_KIND) |
| 404 | assert(maxchar >= 0x100); |
| 405 | else |
| 406 | assert(maxchar >= 0x10000); |
| 407 | } |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 408 | if (check_content && !unicode_is_singleton(op)) |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 409 | assert(ascii->hash == -1); |
Benjamin Peterson | ccc51c1 | 2011-10-03 19:34:12 -0400 | [diff] [blame] | 410 | return 1; |
| 411 | } |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 412 | #endif |
| 413 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 414 | #ifdef HAVE_MBCS |
| 415 | static OSVERSIONINFOEX winver; |
| 416 | #endif |
| 417 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 418 | /* --- Bloom Filters ----------------------------------------------------- */ |
| 419 | |
| 420 | /* stuff to implement simple "bloom filters" for Unicode characters. |
| 421 | to keep things simple, we use a single bitmask, using the least 5 |
| 422 | bits from each unicode characters as the bit index. */ |
| 423 | |
| 424 | /* the linebreak mask is set up by Unicode_Init below */ |
| 425 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 426 | #if LONG_BIT >= 128 |
| 427 | #define BLOOM_WIDTH 128 |
| 428 | #elif LONG_BIT >= 64 |
| 429 | #define BLOOM_WIDTH 64 |
| 430 | #elif LONG_BIT >= 32 |
| 431 | #define BLOOM_WIDTH 32 |
| 432 | #else |
| 433 | #error "LONG_BIT is smaller than 32" |
| 434 | #endif |
| 435 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 436 | #define BLOOM_MASK unsigned long |
| 437 | |
| 438 | static BLOOM_MASK bloom_linebreak; |
| 439 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 440 | #define BLOOM_ADD(mask, ch) ((mask |= (1UL << ((ch) & (BLOOM_WIDTH - 1))))) |
| 441 | #define BLOOM(mask, ch) ((mask & (1UL << ((ch) & (BLOOM_WIDTH - 1))))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 442 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 443 | #define BLOOM_LINEBREAK(ch) \ |
| 444 | ((ch) < 128U ? ascii_linebreak[(ch)] : \ |
| 445 | (BLOOM(bloom_linebreak, (ch)) && Py_UNICODE_ISLINEBREAK(ch))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 446 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 447 | Py_LOCAL_INLINE(BLOOM_MASK) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 448 | make_bloom_mask(int kind, void* ptr, Py_ssize_t len) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 449 | { |
| 450 | /* calculate simple bloom-style bitmask for a given unicode string */ |
| 451 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 452 | BLOOM_MASK mask; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 453 | Py_ssize_t i; |
| 454 | |
| 455 | mask = 0; |
| 456 | for (i = 0; i < len; i++) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 457 | BLOOM_ADD(mask, PyUnicode_READ(kind, ptr, i)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 458 | |
| 459 | return mask; |
| 460 | } |
| 461 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 462 | #define BLOOM_MEMBER(mask, chr, str) \ |
| 463 | (BLOOM(mask, chr) \ |
| 464 | && (PyUnicode_FindChar(str, chr, 0, PyUnicode_GET_LENGTH(str), 1) >= 0)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 465 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 466 | /* Compilation of templated routines */ |
| 467 | |
| 468 | #include "stringlib/asciilib.h" |
| 469 | #include "stringlib/fastsearch.h" |
| 470 | #include "stringlib/partition.h" |
| 471 | #include "stringlib/split.h" |
| 472 | #include "stringlib/count.h" |
| 473 | #include "stringlib/find.h" |
| 474 | #include "stringlib/find_max_char.h" |
| 475 | #include "stringlib/localeutil.h" |
| 476 | #include "stringlib/undef.h" |
| 477 | |
| 478 | #include "stringlib/ucs1lib.h" |
| 479 | #include "stringlib/fastsearch.h" |
| 480 | #include "stringlib/partition.h" |
| 481 | #include "stringlib/split.h" |
| 482 | #include "stringlib/count.h" |
| 483 | #include "stringlib/find.h" |
| 484 | #include "stringlib/find_max_char.h" |
| 485 | #include "stringlib/localeutil.h" |
| 486 | #include "stringlib/undef.h" |
| 487 | |
| 488 | #include "stringlib/ucs2lib.h" |
| 489 | #include "stringlib/fastsearch.h" |
| 490 | #include "stringlib/partition.h" |
| 491 | #include "stringlib/split.h" |
| 492 | #include "stringlib/count.h" |
| 493 | #include "stringlib/find.h" |
| 494 | #include "stringlib/find_max_char.h" |
| 495 | #include "stringlib/localeutil.h" |
| 496 | #include "stringlib/undef.h" |
| 497 | |
| 498 | #include "stringlib/ucs4lib.h" |
| 499 | #include "stringlib/fastsearch.h" |
| 500 | #include "stringlib/partition.h" |
| 501 | #include "stringlib/split.h" |
| 502 | #include "stringlib/count.h" |
| 503 | #include "stringlib/find.h" |
| 504 | #include "stringlib/find_max_char.h" |
| 505 | #include "stringlib/localeutil.h" |
| 506 | #include "stringlib/undef.h" |
| 507 | |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 508 | #include "stringlib/unicodedefs.h" |
| 509 | #include "stringlib/fastsearch.h" |
| 510 | #include "stringlib/count.h" |
| 511 | #include "stringlib/find.h" |
| 512 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 513 | /* --- Unicode Object ----------------------------------------------------- */ |
| 514 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 515 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 516 | fixup(PyObject *self, Py_UCS4 (*fixfct)(PyObject *s)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 517 | |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 518 | Py_LOCAL_INLINE(Py_ssize_t) findchar(void *s, int kind, |
| 519 | Py_ssize_t size, Py_UCS4 ch, |
| 520 | int direction) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 521 | { |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 522 | int mode = (direction == 1) ? FAST_SEARCH : FAST_RSEARCH; |
| 523 | |
| 524 | switch (kind) { |
| 525 | case PyUnicode_1BYTE_KIND: |
| 526 | { |
| 527 | Py_UCS1 ch1 = (Py_UCS1) ch; |
| 528 | if (ch1 == ch) |
| 529 | return ucs1lib_fastsearch((Py_UCS1 *) s, size, &ch1, 1, 0, mode); |
| 530 | else |
| 531 | return -1; |
| 532 | } |
| 533 | case PyUnicode_2BYTE_KIND: |
| 534 | { |
| 535 | Py_UCS2 ch2 = (Py_UCS2) ch; |
| 536 | if (ch2 == ch) |
| 537 | return ucs2lib_fastsearch((Py_UCS2 *) s, size, &ch2, 1, 0, mode); |
| 538 | else |
| 539 | return -1; |
| 540 | } |
| 541 | case PyUnicode_4BYTE_KIND: |
| 542 | return ucs4lib_fastsearch((Py_UCS4 *) s, size, &ch, 1, 0, mode); |
| 543 | default: |
| 544 | assert(0); |
| 545 | return -1; |
Victor Stinner | 9e7a1bc | 2011-10-13 00:18:12 +0200 | [diff] [blame] | 546 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 547 | } |
| 548 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 549 | static PyObject* |
| 550 | resize_compact(PyObject *unicode, Py_ssize_t length) |
| 551 | { |
| 552 | Py_ssize_t char_size; |
| 553 | Py_ssize_t struct_size; |
| 554 | Py_ssize_t new_size; |
| 555 | int share_wstr; |
| 556 | |
| 557 | assert(PyUnicode_IS_READY(unicode)); |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 558 | char_size = PyUnicode_KIND(unicode); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 559 | if (PyUnicode_IS_COMPACT_ASCII(unicode)) |
| 560 | struct_size = sizeof(PyASCIIObject); |
| 561 | else |
| 562 | struct_size = sizeof(PyCompactUnicodeObject); |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 563 | share_wstr = _PyUnicode_SHARE_WSTR(unicode); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 564 | |
| 565 | _Py_DEC_REFTOTAL; |
| 566 | _Py_ForgetReference(unicode); |
| 567 | |
| 568 | if (length > ((PY_SSIZE_T_MAX - struct_size) / char_size - 1)) { |
| 569 | PyErr_NoMemory(); |
| 570 | return NULL; |
| 571 | } |
| 572 | new_size = (struct_size + (length + 1) * char_size); |
| 573 | |
| 574 | unicode = (PyObject *)PyObject_REALLOC((char *)unicode, new_size); |
| 575 | if (unicode == NULL) { |
| 576 | PyObject_Del(unicode); |
| 577 | PyErr_NoMemory(); |
| 578 | return NULL; |
| 579 | } |
| 580 | _Py_NewReference(unicode); |
| 581 | _PyUnicode_LENGTH(unicode) = length; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 582 | if (share_wstr) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 583 | _PyUnicode_WSTR(unicode) = PyUnicode_DATA(unicode); |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 584 | if (!PyUnicode_IS_COMPACT_ASCII(unicode)) |
| 585 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 586 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 587 | PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode), |
| 588 | length, 0); |
| 589 | return unicode; |
| 590 | } |
| 591 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 592 | static int |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 593 | resize_inplace(PyObject *unicode, Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 594 | { |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 595 | wchar_t *wstr; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 596 | assert(!PyUnicode_IS_COMPACT(unicode)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 597 | assert(Py_REFCNT(unicode) == 1); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 598 | |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 599 | _PyUnicode_DIRTY(unicode); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 600 | |
| 601 | if (PyUnicode_IS_READY(unicode)) { |
| 602 | Py_ssize_t char_size; |
| 603 | Py_ssize_t new_size; |
Victor Stinner | 1c8d0c7 | 2011-10-03 12:11:00 +0200 | [diff] [blame] | 604 | int share_wstr, share_utf8; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 605 | void *data; |
| 606 | |
| 607 | data = _PyUnicode_DATA_ANY(unicode); |
| 608 | assert(data != NULL); |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 609 | char_size = PyUnicode_KIND(unicode); |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 610 | share_wstr = _PyUnicode_SHARE_WSTR(unicode); |
| 611 | share_utf8 = _PyUnicode_SHARE_UTF8(unicode); |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 612 | if (!share_utf8 && _PyUnicode_HAS_UTF8_MEMORY(unicode)) |
| 613 | { |
| 614 | PyObject_DEL(_PyUnicode_UTF8(unicode)); |
| 615 | _PyUnicode_UTF8(unicode) = NULL; |
| 616 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
| 617 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 618 | |
| 619 | if (length > (PY_SSIZE_T_MAX / char_size - 1)) { |
| 620 | PyErr_NoMemory(); |
| 621 | return -1; |
| 622 | } |
| 623 | new_size = (length + 1) * char_size; |
| 624 | |
| 625 | data = (PyObject *)PyObject_REALLOC(data, new_size); |
| 626 | if (data == NULL) { |
| 627 | PyErr_NoMemory(); |
| 628 | return -1; |
| 629 | } |
| 630 | _PyUnicode_DATA_ANY(unicode) = data; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 631 | if (share_wstr) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 632 | _PyUnicode_WSTR(unicode) = data; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 633 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 634 | } |
| 635 | if (share_utf8) { |
Victor Stinner | 1c8d0c7 | 2011-10-03 12:11:00 +0200 | [diff] [blame] | 636 | _PyUnicode_UTF8(unicode) = data; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 637 | _PyUnicode_UTF8_LENGTH(unicode) = length; |
| 638 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 639 | _PyUnicode_LENGTH(unicode) = length; |
| 640 | PyUnicode_WRITE(PyUnicode_KIND(unicode), data, length, 0); |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 641 | if (share_wstr || _PyUnicode_WSTR(unicode) == NULL) { |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 642 | assert(_PyUnicode_CheckConsistency(unicode, 0)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 643 | return 0; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 644 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 645 | } |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 646 | assert(_PyUnicode_WSTR(unicode) != NULL); |
| 647 | |
| 648 | /* check for integer overflow */ |
| 649 | if (length > PY_SSIZE_T_MAX / sizeof(wchar_t) - 1) { |
| 650 | PyErr_NoMemory(); |
| 651 | return -1; |
| 652 | } |
| 653 | wstr = _PyUnicode_WSTR(unicode); |
| 654 | wstr = PyObject_REALLOC(wstr, sizeof(wchar_t) * (length + 1)); |
| 655 | if (!wstr) { |
| 656 | PyErr_NoMemory(); |
| 657 | return -1; |
| 658 | } |
| 659 | _PyUnicode_WSTR(unicode) = wstr; |
| 660 | _PyUnicode_WSTR(unicode)[length] = 0; |
| 661 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 662 | assert(_PyUnicode_CheckConsistency(unicode, 0)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 663 | return 0; |
| 664 | } |
| 665 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 666 | static PyObject* |
| 667 | resize_copy(PyObject *unicode, Py_ssize_t length) |
| 668 | { |
| 669 | Py_ssize_t copy_length; |
| 670 | if (PyUnicode_IS_COMPACT(unicode)) { |
| 671 | PyObject *copy; |
| 672 | assert(PyUnicode_IS_READY(unicode)); |
| 673 | |
| 674 | copy = PyUnicode_New(length, PyUnicode_MAX_CHAR_VALUE(unicode)); |
| 675 | if (copy == NULL) |
| 676 | return NULL; |
| 677 | |
| 678 | copy_length = Py_MIN(length, PyUnicode_GET_LENGTH(unicode)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 679 | copy_characters(copy, 0, unicode, 0, copy_length); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 680 | return copy; |
Victor Stinner | 8cfcbed | 2011-10-03 23:19:21 +0200 | [diff] [blame] | 681 | } |
| 682 | else { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 683 | PyObject *w; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 684 | assert(_PyUnicode_WSTR(unicode) != NULL); |
| 685 | assert(_PyUnicode_DATA_ANY(unicode) == NULL); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 686 | w = (PyObject*)_PyUnicode_New(length); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 687 | if (w == NULL) |
| 688 | return NULL; |
| 689 | copy_length = _PyUnicode_WSTR_LENGTH(unicode); |
| 690 | copy_length = Py_MIN(copy_length, length); |
| 691 | Py_UNICODE_COPY(_PyUnicode_WSTR(w), _PyUnicode_WSTR(unicode), |
| 692 | copy_length); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 693 | return w; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 694 | } |
| 695 | } |
| 696 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 697 | /* 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] | 698 | Ux0000 terminated; some code (e.g. new_identifier) |
| 699 | relies on that. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 700 | |
| 701 | XXX This allocator could further be enhanced by assuring that the |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 702 | free list never reduces its size below 1. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 703 | |
| 704 | */ |
| 705 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 706 | #ifdef Py_DEBUG |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 707 | static int unicode_old_new_calls = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 708 | #endif |
| 709 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 710 | static PyUnicodeObject * |
| 711 | _PyUnicode_New(Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 712 | { |
| 713 | register PyUnicodeObject *unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 714 | size_t new_size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 715 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 716 | /* Optimization for empty strings */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 717 | if (length == 0 && unicode_empty != NULL) { |
| 718 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 719 | return (PyUnicodeObject*)unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 720 | } |
| 721 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 722 | /* Ensure we won't overflow the size. */ |
| 723 | if (length > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
| 724 | return (PyUnicodeObject *)PyErr_NoMemory(); |
| 725 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 726 | if (length < 0) { |
| 727 | PyErr_SetString(PyExc_SystemError, |
| 728 | "Negative size passed to _PyUnicode_New"); |
| 729 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 730 | } |
| 731 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 732 | #ifdef Py_DEBUG |
| 733 | ++unicode_old_new_calls; |
| 734 | #endif |
| 735 | |
| 736 | unicode = PyObject_New(PyUnicodeObject, &PyUnicode_Type); |
| 737 | if (unicode == NULL) |
| 738 | return NULL; |
| 739 | new_size = sizeof(Py_UNICODE) * ((size_t)length + 1); |
| 740 | _PyUnicode_WSTR(unicode) = (Py_UNICODE*) PyObject_MALLOC(new_size); |
| 741 | if (!_PyUnicode_WSTR(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 742 | PyErr_NoMemory(); |
| 743 | goto onError; |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 744 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 745 | |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 746 | /* Initialize the first element to guard against cases where |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 747 | * the caller fails before initializing str -- unicode_resize() |
| 748 | * reads str[0], and the Keep-Alive optimization can keep memory |
| 749 | * allocated for str alive across a call to unicode_dealloc(unicode). |
| 750 | * We don't want unicode_resize to read uninitialized memory in |
| 751 | * that case. |
| 752 | */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 753 | _PyUnicode_WSTR(unicode)[0] = 0; |
| 754 | _PyUnicode_WSTR(unicode)[length] = 0; |
| 755 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 756 | _PyUnicode_HASH(unicode) = -1; |
| 757 | _PyUnicode_STATE(unicode).interned = 0; |
| 758 | _PyUnicode_STATE(unicode).kind = 0; |
| 759 | _PyUnicode_STATE(unicode).compact = 0; |
| 760 | _PyUnicode_STATE(unicode).ready = 0; |
| 761 | _PyUnicode_STATE(unicode).ascii = 0; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 762 | _PyUnicode_DATA_ANY(unicode) = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 763 | _PyUnicode_LENGTH(unicode) = 0; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 764 | _PyUnicode_UTF8(unicode) = NULL; |
| 765 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 766 | assert(_PyUnicode_CheckConsistency((PyObject *)unicode, 0)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 767 | return unicode; |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 768 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 769 | onError: |
Amaury Forgeot d'Arc | 7888d08 | 2008-08-01 01:06:32 +0000 | [diff] [blame] | 770 | /* XXX UNREF/NEWREF interface should be more symmetrical */ |
| 771 | _Py_DEC_REFTOTAL; |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 772 | _Py_ForgetReference((PyObject *)unicode); |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 773 | PyObject_Del(unicode); |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 774 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 775 | } |
| 776 | |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 777 | static const char* |
| 778 | unicode_kind_name(PyObject *unicode) |
| 779 | { |
Victor Stinner | 42dfd71 | 2011-10-03 14:41:45 +0200 | [diff] [blame] | 780 | /* don't check consistency: unicode_kind_name() is called from |
| 781 | _PyUnicode_Dump() */ |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 782 | if (!PyUnicode_IS_COMPACT(unicode)) |
| 783 | { |
| 784 | if (!PyUnicode_IS_READY(unicode)) |
| 785 | return "wstr"; |
| 786 | switch(PyUnicode_KIND(unicode)) |
| 787 | { |
| 788 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 789 | if (PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 790 | return "legacy ascii"; |
| 791 | else |
| 792 | return "legacy latin1"; |
| 793 | case PyUnicode_2BYTE_KIND: |
| 794 | return "legacy UCS2"; |
| 795 | case PyUnicode_4BYTE_KIND: |
| 796 | return "legacy UCS4"; |
| 797 | default: |
| 798 | return "<legacy invalid kind>"; |
| 799 | } |
| 800 | } |
| 801 | assert(PyUnicode_IS_READY(unicode)); |
| 802 | switch(PyUnicode_KIND(unicode)) |
| 803 | { |
| 804 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 805 | if (PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 806 | return "ascii"; |
| 807 | else |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 808 | return "latin1"; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 809 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 810 | return "UCS2"; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 811 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 812 | return "UCS4"; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 813 | default: |
| 814 | return "<invalid compact kind>"; |
| 815 | } |
| 816 | } |
| 817 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 818 | #ifdef Py_DEBUG |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 819 | static int unicode_new_new_calls = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 820 | |
| 821 | /* Functions wrapping macros for use in debugger */ |
| 822 | char *_PyUnicode_utf8(void *unicode){ |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 823 | return PyUnicode_UTF8(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 824 | } |
| 825 | |
| 826 | void *_PyUnicode_compact_data(void *unicode) { |
| 827 | return _PyUnicode_COMPACT_DATA(unicode); |
| 828 | } |
| 829 | void *_PyUnicode_data(void *unicode){ |
| 830 | printf("obj %p\n", unicode); |
| 831 | printf("compact %d\n", PyUnicode_IS_COMPACT(unicode)); |
| 832 | printf("compact ascii %d\n", PyUnicode_IS_COMPACT_ASCII(unicode)); |
| 833 | printf("ascii op %p\n", ((void*)((PyASCIIObject*)(unicode) + 1))); |
| 834 | printf("compact op %p\n", ((void*)((PyCompactUnicodeObject*)(unicode) + 1))); |
| 835 | printf("compact data %p\n", _PyUnicode_COMPACT_DATA(unicode)); |
| 836 | return PyUnicode_DATA(unicode); |
| 837 | } |
Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 838 | |
| 839 | void |
| 840 | _PyUnicode_Dump(PyObject *op) |
| 841 | { |
| 842 | PyASCIIObject *ascii = (PyASCIIObject *)op; |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 843 | PyCompactUnicodeObject *compact = (PyCompactUnicodeObject *)op; |
| 844 | PyUnicodeObject *unicode = (PyUnicodeObject *)op; |
| 845 | void *data; |
Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 846 | |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 847 | if (ascii->state.compact) |
Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 848 | { |
| 849 | if (ascii->state.ascii) |
| 850 | data = (ascii + 1); |
| 851 | else |
| 852 | data = (compact + 1); |
| 853 | } |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 854 | else |
| 855 | data = unicode->data.any; |
Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 856 | printf("%s: len=%zu, ",unicode_kind_name(op), ascii->length); |
| 857 | |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 858 | if (ascii->wstr == data) |
| 859 | printf("shared "); |
| 860 | printf("wstr=%p", ascii->wstr); |
Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 861 | |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 862 | if (!(ascii->state.ascii == 1 && ascii->state.compact == 1)) { |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 863 | printf(" (%zu), ", compact->wstr_length); |
| 864 | if (!ascii->state.compact && compact->utf8 == unicode->data.any) |
| 865 | printf("shared "); |
| 866 | printf("utf8=%p (%zu)", compact->utf8, compact->utf8_length); |
Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 867 | } |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 868 | printf(", data=%p\n", data); |
Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 869 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 870 | #endif |
| 871 | |
| 872 | PyObject * |
| 873 | PyUnicode_New(Py_ssize_t size, Py_UCS4 maxchar) |
| 874 | { |
| 875 | PyObject *obj; |
| 876 | PyCompactUnicodeObject *unicode; |
| 877 | void *data; |
| 878 | int kind_state; |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 879 | int is_sharing, is_ascii; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 880 | Py_ssize_t char_size; |
| 881 | Py_ssize_t struct_size; |
| 882 | |
| 883 | /* Optimization for empty strings */ |
| 884 | if (size == 0 && unicode_empty != NULL) { |
| 885 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 886 | return unicode_empty; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 887 | } |
| 888 | |
| 889 | #ifdef Py_DEBUG |
| 890 | ++unicode_new_new_calls; |
| 891 | #endif |
| 892 | |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 893 | is_ascii = 0; |
| 894 | is_sharing = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 895 | struct_size = sizeof(PyCompactUnicodeObject); |
| 896 | if (maxchar < 128) { |
| 897 | kind_state = PyUnicode_1BYTE_KIND; |
| 898 | char_size = 1; |
| 899 | is_ascii = 1; |
| 900 | struct_size = sizeof(PyASCIIObject); |
| 901 | } |
| 902 | else if (maxchar < 256) { |
| 903 | kind_state = PyUnicode_1BYTE_KIND; |
| 904 | char_size = 1; |
| 905 | } |
| 906 | else if (maxchar < 65536) { |
| 907 | kind_state = PyUnicode_2BYTE_KIND; |
| 908 | char_size = 2; |
| 909 | if (sizeof(wchar_t) == 2) |
| 910 | is_sharing = 1; |
| 911 | } |
| 912 | else { |
| 913 | kind_state = PyUnicode_4BYTE_KIND; |
| 914 | char_size = 4; |
| 915 | if (sizeof(wchar_t) == 4) |
| 916 | is_sharing = 1; |
| 917 | } |
| 918 | |
| 919 | /* Ensure we won't overflow the size. */ |
| 920 | if (size < 0) { |
| 921 | PyErr_SetString(PyExc_SystemError, |
| 922 | "Negative size passed to PyUnicode_New"); |
| 923 | return NULL; |
| 924 | } |
| 925 | if (size > ((PY_SSIZE_T_MAX - struct_size) / char_size - 1)) |
| 926 | return PyErr_NoMemory(); |
| 927 | |
| 928 | /* Duplicated allocation code from _PyObject_New() instead of a call to |
| 929 | * PyObject_New() so we are able to allocate space for the object and |
| 930 | * it's data buffer. |
| 931 | */ |
| 932 | obj = (PyObject *) PyObject_MALLOC(struct_size + (size + 1) * char_size); |
| 933 | if (obj == NULL) |
| 934 | return PyErr_NoMemory(); |
| 935 | obj = PyObject_INIT(obj, &PyUnicode_Type); |
| 936 | if (obj == NULL) |
| 937 | return NULL; |
| 938 | |
| 939 | unicode = (PyCompactUnicodeObject *)obj; |
| 940 | if (is_ascii) |
| 941 | data = ((PyASCIIObject*)obj) + 1; |
| 942 | else |
| 943 | data = unicode + 1; |
| 944 | _PyUnicode_LENGTH(unicode) = size; |
| 945 | _PyUnicode_HASH(unicode) = -1; |
| 946 | _PyUnicode_STATE(unicode).interned = 0; |
| 947 | _PyUnicode_STATE(unicode).kind = kind_state; |
| 948 | _PyUnicode_STATE(unicode).compact = 1; |
| 949 | _PyUnicode_STATE(unicode).ready = 1; |
| 950 | _PyUnicode_STATE(unicode).ascii = is_ascii; |
| 951 | if (is_ascii) { |
| 952 | ((char*)data)[size] = 0; |
| 953 | _PyUnicode_WSTR(unicode) = NULL; |
| 954 | } |
| 955 | else if (kind_state == PyUnicode_1BYTE_KIND) { |
| 956 | ((char*)data)[size] = 0; |
| 957 | _PyUnicode_WSTR(unicode) = NULL; |
| 958 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 959 | unicode->utf8 = NULL; |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 960 | unicode->utf8_length = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 961 | } |
| 962 | else { |
| 963 | unicode->utf8 = NULL; |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 964 | unicode->utf8_length = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 965 | if (kind_state == PyUnicode_2BYTE_KIND) |
| 966 | ((Py_UCS2*)data)[size] = 0; |
| 967 | else /* kind_state == PyUnicode_4BYTE_KIND */ |
| 968 | ((Py_UCS4*)data)[size] = 0; |
| 969 | if (is_sharing) { |
| 970 | _PyUnicode_WSTR_LENGTH(unicode) = size; |
| 971 | _PyUnicode_WSTR(unicode) = (wchar_t *)data; |
| 972 | } |
| 973 | else { |
| 974 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 975 | _PyUnicode_WSTR(unicode) = NULL; |
| 976 | } |
| 977 | } |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 978 | assert(_PyUnicode_CheckConsistency((PyObject*)unicode, 0)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 979 | return obj; |
| 980 | } |
| 981 | |
| 982 | #if SIZEOF_WCHAR_T == 2 |
| 983 | /* Helper function to convert a 16-bits wchar_t representation to UCS4, this |
| 984 | will decode surrogate pairs, the other conversions are implemented as macros |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 985 | for efficiency. |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 986 | |
| 987 | This function assumes that unicode can hold one more code point than wstr |
| 988 | characters for a terminating null character. */ |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 989 | static void |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 990 | 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] | 991 | PyObject *unicode) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 992 | { |
| 993 | const wchar_t *iter; |
| 994 | Py_UCS4 *ucs4_out; |
| 995 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 996 | assert(unicode != NULL); |
| 997 | assert(_PyUnicode_CHECK(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 998 | assert(_PyUnicode_KIND(unicode) == PyUnicode_4BYTE_KIND); |
| 999 | ucs4_out = PyUnicode_4BYTE_DATA(unicode); |
| 1000 | |
| 1001 | for (iter = begin; iter < end; ) { |
| 1002 | assert(ucs4_out < (PyUnicode_4BYTE_DATA(unicode) + |
| 1003 | _PyUnicode_GET_LENGTH(unicode))); |
| 1004 | if (*iter >= 0xD800 && *iter <= 0xDBFF |
| 1005 | && (iter+1) < end && iter[1] >= 0xDC00 && iter[1] <= 0xDFFF) |
| 1006 | { |
| 1007 | *ucs4_out++ = (((iter[0] & 0x3FF)<<10) | (iter[1] & 0x3FF)) + 0x10000; |
| 1008 | iter += 2; |
| 1009 | } |
| 1010 | else { |
| 1011 | *ucs4_out++ = *iter; |
| 1012 | iter++; |
| 1013 | } |
| 1014 | } |
| 1015 | assert(ucs4_out == (PyUnicode_4BYTE_DATA(unicode) + |
| 1016 | _PyUnicode_GET_LENGTH(unicode))); |
| 1017 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1018 | } |
| 1019 | #endif |
| 1020 | |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1021 | static int |
| 1022 | _PyUnicode_Dirty(PyObject *unicode) |
| 1023 | { |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1024 | assert(_PyUnicode_CHECK(unicode)); |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1025 | if (Py_REFCNT(unicode) != 1) { |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 1026 | PyErr_SetString(PyExc_SystemError, |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1027 | "Cannot modify a string having more than 1 reference"); |
| 1028 | return -1; |
| 1029 | } |
| 1030 | _PyUnicode_DIRTY(unicode); |
| 1031 | return 0; |
| 1032 | } |
| 1033 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1034 | static int |
| 1035 | _copy_characters(PyObject *to, Py_ssize_t to_start, |
| 1036 | PyObject *from, Py_ssize_t from_start, |
| 1037 | Py_ssize_t how_many, int check_maxchar) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1038 | { |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1039 | unsigned int from_kind, to_kind; |
| 1040 | void *from_data, *to_data; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1041 | int fast; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1042 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1043 | assert(PyUnicode_Check(from)); |
| 1044 | assert(PyUnicode_Check(to)); |
| 1045 | assert(PyUnicode_IS_READY(from)); |
| 1046 | assert(PyUnicode_IS_READY(to)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1047 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1048 | assert(PyUnicode_GET_LENGTH(from) >= how_many); |
| 1049 | assert(to_start + how_many <= PyUnicode_GET_LENGTH(to)); |
| 1050 | assert(0 <= how_many); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1051 | |
Victor Stinner | f5ca1a2 | 2011-09-28 23:54:59 +0200 | [diff] [blame] | 1052 | if (how_many == 0) |
| 1053 | return 0; |
| 1054 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1055 | from_kind = PyUnicode_KIND(from); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1056 | from_data = PyUnicode_DATA(from); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1057 | to_kind = PyUnicode_KIND(to); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1058 | to_data = PyUnicode_DATA(to); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1059 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1060 | #ifdef Py_DEBUG |
| 1061 | if (!check_maxchar |
| 1062 | && (from_kind > to_kind |
| 1063 | || (!PyUnicode_IS_ASCII(from) && PyUnicode_IS_ASCII(to)))) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 1064 | { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1065 | const Py_UCS4 to_maxchar = PyUnicode_MAX_CHAR_VALUE(to); |
| 1066 | Py_UCS4 ch; |
| 1067 | Py_ssize_t i; |
| 1068 | for (i=0; i < how_many; i++) { |
| 1069 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
| 1070 | assert(ch <= to_maxchar); |
| 1071 | } |
| 1072 | } |
| 1073 | #endif |
| 1074 | fast = (from_kind == to_kind); |
| 1075 | if (check_maxchar |
| 1076 | && (!PyUnicode_IS_ASCII(from) && PyUnicode_IS_ASCII(to))) |
| 1077 | { |
| 1078 | /* deny latin1 => ascii */ |
| 1079 | fast = 0; |
| 1080 | } |
| 1081 | |
| 1082 | if (fast) { |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 1083 | Py_MEMCPY((char*)to_data + to_kind * to_start, |
| 1084 | (char*)from_data + from_kind * from_start, |
| 1085 | to_kind * how_many); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1086 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1087 | else if (from_kind == PyUnicode_1BYTE_KIND |
| 1088 | && to_kind == PyUnicode_2BYTE_KIND) |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1089 | { |
| 1090 | _PyUnicode_CONVERT_BYTES( |
| 1091 | Py_UCS1, Py_UCS2, |
| 1092 | PyUnicode_1BYTE_DATA(from) + from_start, |
| 1093 | PyUnicode_1BYTE_DATA(from) + from_start + how_many, |
| 1094 | PyUnicode_2BYTE_DATA(to) + to_start |
| 1095 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1096 | } |
Victor Stinner | 157f83f | 2011-09-28 21:41:31 +0200 | [diff] [blame] | 1097 | else if (from_kind == PyUnicode_1BYTE_KIND |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1098 | && to_kind == PyUnicode_4BYTE_KIND) |
| 1099 | { |
| 1100 | _PyUnicode_CONVERT_BYTES( |
| 1101 | Py_UCS1, Py_UCS4, |
| 1102 | PyUnicode_1BYTE_DATA(from) + from_start, |
| 1103 | PyUnicode_1BYTE_DATA(from) + from_start + how_many, |
| 1104 | PyUnicode_4BYTE_DATA(to) + to_start |
| 1105 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1106 | } |
| 1107 | else if (from_kind == PyUnicode_2BYTE_KIND |
| 1108 | && to_kind == PyUnicode_4BYTE_KIND) |
| 1109 | { |
| 1110 | _PyUnicode_CONVERT_BYTES( |
| 1111 | Py_UCS2, Py_UCS4, |
| 1112 | PyUnicode_2BYTE_DATA(from) + from_start, |
| 1113 | PyUnicode_2BYTE_DATA(from) + from_start + how_many, |
| 1114 | PyUnicode_4BYTE_DATA(to) + to_start |
| 1115 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1116 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1117 | else { |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 1118 | /* check if max_char(from substring) <= max_char(to) */ |
| 1119 | if (from_kind > to_kind |
| 1120 | /* latin1 => ascii */ |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1121 | || (!PyUnicode_IS_ASCII(from) && PyUnicode_IS_ASCII(to))) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 1122 | { |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1123 | /* slow path to check for character overflow */ |
| 1124 | const Py_UCS4 to_maxchar = PyUnicode_MAX_CHAR_VALUE(to); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1125 | Py_UCS4 ch; |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1126 | Py_ssize_t i; |
| 1127 | |
Victor Stinner | 56c161a | 2011-10-06 02:47:11 +0200 | [diff] [blame] | 1128 | #ifdef Py_DEBUG |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1129 | for (i=0; i < how_many; i++) { |
| 1130 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
Victor Stinner | 56c161a | 2011-10-06 02:47:11 +0200 | [diff] [blame] | 1131 | assert(ch <= to_maxchar); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1132 | PyUnicode_WRITE(to_kind, to_data, to_start + i, ch); |
| 1133 | } |
Victor Stinner | 56c161a | 2011-10-06 02:47:11 +0200 | [diff] [blame] | 1134 | #else |
| 1135 | if (!check_maxchar) { |
| 1136 | for (i=0; i < how_many; i++) { |
| 1137 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
| 1138 | PyUnicode_WRITE(to_kind, to_data, to_start + i, ch); |
| 1139 | } |
| 1140 | } |
| 1141 | else { |
| 1142 | for (i=0; i < how_many; i++) { |
| 1143 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
| 1144 | if (ch > to_maxchar) |
| 1145 | return 1; |
| 1146 | PyUnicode_WRITE(to_kind, to_data, to_start + i, ch); |
| 1147 | } |
| 1148 | } |
| 1149 | #endif |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1150 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1151 | else { |
Victor Stinner | 56c161a | 2011-10-06 02:47:11 +0200 | [diff] [blame] | 1152 | assert(0 && "inconsistent state"); |
| 1153 | return 1; |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1154 | } |
| 1155 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1156 | return 0; |
| 1157 | } |
| 1158 | |
| 1159 | static void |
| 1160 | copy_characters(PyObject *to, Py_ssize_t to_start, |
| 1161 | PyObject *from, Py_ssize_t from_start, |
| 1162 | Py_ssize_t how_many) |
| 1163 | { |
| 1164 | (void)_copy_characters(to, to_start, from, from_start, how_many, 0); |
| 1165 | } |
| 1166 | |
| 1167 | Py_ssize_t |
| 1168 | PyUnicode_CopyCharacters(PyObject *to, Py_ssize_t to_start, |
| 1169 | PyObject *from, Py_ssize_t from_start, |
| 1170 | Py_ssize_t how_many) |
| 1171 | { |
| 1172 | int err; |
| 1173 | |
| 1174 | if (!PyUnicode_Check(from) || !PyUnicode_Check(to)) { |
| 1175 | PyErr_BadInternalCall(); |
| 1176 | return -1; |
| 1177 | } |
| 1178 | |
| 1179 | if (PyUnicode_READY(from)) |
| 1180 | return -1; |
| 1181 | if (PyUnicode_READY(to)) |
| 1182 | return -1; |
| 1183 | |
| 1184 | how_many = Py_MIN(PyUnicode_GET_LENGTH(from), how_many); |
| 1185 | if (to_start + how_many > PyUnicode_GET_LENGTH(to)) { |
| 1186 | PyErr_Format(PyExc_SystemError, |
| 1187 | "Cannot write %zi characters at %zi " |
| 1188 | "in a string of %zi characters", |
| 1189 | how_many, to_start, PyUnicode_GET_LENGTH(to)); |
| 1190 | return -1; |
| 1191 | } |
| 1192 | |
| 1193 | if (how_many == 0) |
| 1194 | return 0; |
| 1195 | |
| 1196 | if (_PyUnicode_Dirty(to)) |
| 1197 | return -1; |
| 1198 | |
| 1199 | err = _copy_characters(to, to_start, from, from_start, how_many, 1); |
| 1200 | if (err) { |
| 1201 | PyErr_Format(PyExc_SystemError, |
| 1202 | "Cannot copy %s characters " |
| 1203 | "into a string of %s characters", |
| 1204 | unicode_kind_name(from), |
| 1205 | unicode_kind_name(to)); |
| 1206 | return -1; |
| 1207 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1208 | return how_many; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1209 | } |
| 1210 | |
Victor Stinner | 1722216 | 2011-09-28 22:15:37 +0200 | [diff] [blame] | 1211 | /* Find the maximum code point and count the number of surrogate pairs so a |
| 1212 | correct string length can be computed before converting a string to UCS4. |
| 1213 | This function counts single surrogates as a character and not as a pair. |
| 1214 | |
| 1215 | Return 0 on success, or -1 on error. */ |
| 1216 | static int |
| 1217 | find_maxchar_surrogates(const wchar_t *begin, const wchar_t *end, |
| 1218 | Py_UCS4 *maxchar, Py_ssize_t *num_surrogates) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1219 | { |
| 1220 | const wchar_t *iter; |
| 1221 | |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1222 | assert(num_surrogates != NULL && maxchar != NULL); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1223 | *num_surrogates = 0; |
| 1224 | *maxchar = 0; |
| 1225 | |
| 1226 | for (iter = begin; iter < end; ) { |
Victor Stinner | ae86485 | 2011-10-05 14:02:44 +0200 | [diff] [blame] | 1227 | if (*iter > *maxchar) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1228 | *maxchar = *iter; |
Victor Stinner | ae86485 | 2011-10-05 14:02:44 +0200 | [diff] [blame] | 1229 | #if SIZEOF_WCHAR_T != 2 |
| 1230 | if (*maxchar >= 0x10000) |
| 1231 | return 0; |
| 1232 | #endif |
| 1233 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1234 | #if SIZEOF_WCHAR_T == 2 |
| 1235 | if (*iter >= 0xD800 && *iter <= 0xDBFF |
| 1236 | && (iter+1) < end && iter[1] >= 0xDC00 && iter[1] <= 0xDFFF) |
| 1237 | { |
| 1238 | Py_UCS4 surrogate_val; |
| 1239 | surrogate_val = (((iter[0] & 0x3FF)<<10) |
| 1240 | | (iter[1] & 0x3FF)) + 0x10000; |
| 1241 | ++(*num_surrogates); |
| 1242 | if (surrogate_val > *maxchar) |
| 1243 | *maxchar = surrogate_val; |
| 1244 | iter += 2; |
| 1245 | } |
| 1246 | else |
| 1247 | iter++; |
| 1248 | #else |
| 1249 | iter++; |
| 1250 | #endif |
| 1251 | } |
| 1252 | return 0; |
| 1253 | } |
| 1254 | |
| 1255 | #ifdef Py_DEBUG |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 1256 | static int unicode_ready_calls = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1257 | #endif |
| 1258 | |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 1259 | static int |
| 1260 | unicode_ready(PyObject **p_obj, int replace) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1261 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1262 | PyObject *unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1263 | wchar_t *end; |
| 1264 | Py_UCS4 maxchar = 0; |
| 1265 | Py_ssize_t num_surrogates; |
| 1266 | #if SIZEOF_WCHAR_T == 2 |
| 1267 | Py_ssize_t length_wo_surrogates; |
| 1268 | #endif |
| 1269 | |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 1270 | assert(p_obj != NULL); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1271 | unicode = *p_obj; |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 1272 | |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 1273 | /* _PyUnicode_Ready() is only intended for old-style API usage where |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1274 | strings were created using _PyObject_New() and where no canonical |
| 1275 | representation (the str field) has been set yet aka strings |
| 1276 | which are not yet ready. */ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1277 | assert(_PyUnicode_CHECK(unicode)); |
| 1278 | assert(_PyUnicode_KIND(unicode) == PyUnicode_WCHAR_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1279 | assert(_PyUnicode_WSTR(unicode) != NULL); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1280 | assert(_PyUnicode_DATA_ANY(unicode) == NULL); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1281 | assert(_PyUnicode_UTF8(unicode) == NULL); |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1282 | /* Actually, it should neither be interned nor be anything else: */ |
| 1283 | assert(_PyUnicode_STATE(unicode).interned == SSTATE_NOT_INTERNED); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1284 | |
| 1285 | #ifdef Py_DEBUG |
| 1286 | ++unicode_ready_calls; |
| 1287 | #endif |
| 1288 | |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 1289 | #ifdef Py_DEBUG |
| 1290 | assert(!replace || Py_REFCNT(unicode) == 1); |
| 1291 | #else |
| 1292 | if (replace && Py_REFCNT(unicode) != 1) |
| 1293 | replace = 0; |
| 1294 | #endif |
| 1295 | if (replace) { |
| 1296 | Py_ssize_t len = _PyUnicode_WSTR_LENGTH(unicode); |
| 1297 | wchar_t *wstr = _PyUnicode_WSTR(unicode); |
| 1298 | /* Optimization for empty strings */ |
| 1299 | if (len == 0) { |
| 1300 | Py_INCREF(unicode_empty); |
| 1301 | Py_DECREF(*p_obj); |
| 1302 | *p_obj = unicode_empty; |
| 1303 | return 0; |
| 1304 | } |
| 1305 | if (len == 1 && wstr[0] < 256) { |
| 1306 | PyObject *latin1_char = get_latin1_char((unsigned char)wstr[0]); |
| 1307 | if (latin1_char == NULL) |
| 1308 | return -1; |
| 1309 | Py_DECREF(*p_obj); |
| 1310 | *p_obj = latin1_char; |
| 1311 | return 0; |
| 1312 | } |
| 1313 | } |
| 1314 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1315 | end = _PyUnicode_WSTR(unicode) + _PyUnicode_WSTR_LENGTH(unicode); |
Victor Stinner | 1722216 | 2011-09-28 22:15:37 +0200 | [diff] [blame] | 1316 | if (find_maxchar_surrogates(_PyUnicode_WSTR(unicode), end, |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1317 | &maxchar, &num_surrogates) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1318 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1319 | |
| 1320 | if (maxchar < 256) { |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1321 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC(_PyUnicode_WSTR_LENGTH(unicode) + 1); |
| 1322 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1323 | PyErr_NoMemory(); |
| 1324 | return -1; |
| 1325 | } |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1326 | _PyUnicode_CONVERT_BYTES(wchar_t, unsigned char, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1327 | _PyUnicode_WSTR(unicode), end, |
| 1328 | PyUnicode_1BYTE_DATA(unicode)); |
| 1329 | PyUnicode_1BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1330 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1331 | _PyUnicode_STATE(unicode).kind = PyUnicode_1BYTE_KIND; |
| 1332 | if (maxchar < 128) { |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1333 | _PyUnicode_STATE(unicode).ascii = 1; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1334 | _PyUnicode_UTF8(unicode) = _PyUnicode_DATA_ANY(unicode); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1335 | _PyUnicode_UTF8_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1336 | } |
| 1337 | else { |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1338 | _PyUnicode_STATE(unicode).ascii = 0; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1339 | _PyUnicode_UTF8(unicode) = NULL; |
| 1340 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1341 | } |
| 1342 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1343 | _PyUnicode_WSTR(unicode) = NULL; |
| 1344 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1345 | } |
| 1346 | /* In this case we might have to convert down from 4-byte native |
| 1347 | wchar_t to 2-byte unicode. */ |
| 1348 | else if (maxchar < 65536) { |
| 1349 | assert(num_surrogates == 0 && |
| 1350 | "FindMaxCharAndNumSurrogatePairs() messed up"); |
| 1351 | |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1352 | #if SIZEOF_WCHAR_T == 2 |
| 1353 | /* We can share representations and are done. */ |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1354 | _PyUnicode_DATA_ANY(unicode) = _PyUnicode_WSTR(unicode); |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1355 | PyUnicode_2BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1356 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1357 | _PyUnicode_STATE(unicode).kind = PyUnicode_2BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1358 | _PyUnicode_UTF8(unicode) = NULL; |
| 1359 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1360 | #else |
| 1361 | /* sizeof(wchar_t) == 4 */ |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1362 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC( |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1363 | 2 * (_PyUnicode_WSTR_LENGTH(unicode) + 1)); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1364 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1365 | PyErr_NoMemory(); |
| 1366 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1367 | } |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1368 | _PyUnicode_CONVERT_BYTES(wchar_t, Py_UCS2, |
| 1369 | _PyUnicode_WSTR(unicode), end, |
| 1370 | PyUnicode_2BYTE_DATA(unicode)); |
| 1371 | PyUnicode_2BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1372 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1373 | _PyUnicode_STATE(unicode).kind = PyUnicode_2BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1374 | _PyUnicode_UTF8(unicode) = NULL; |
| 1375 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1376 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1377 | _PyUnicode_WSTR(unicode) = NULL; |
| 1378 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1379 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1380 | } |
| 1381 | /* maxchar exeeds 16 bit, wee need 4 bytes for unicode characters */ |
| 1382 | else { |
| 1383 | #if SIZEOF_WCHAR_T == 2 |
| 1384 | /* in case the native representation is 2-bytes, we need to allocate a |
| 1385 | new normalized 4-byte version. */ |
| 1386 | length_wo_surrogates = _PyUnicode_WSTR_LENGTH(unicode) - num_surrogates; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1387 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC(4 * (length_wo_surrogates + 1)); |
| 1388 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1389 | PyErr_NoMemory(); |
| 1390 | return -1; |
| 1391 | } |
| 1392 | _PyUnicode_LENGTH(unicode) = length_wo_surrogates; |
| 1393 | _PyUnicode_STATE(unicode).kind = PyUnicode_4BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1394 | _PyUnicode_UTF8(unicode) = NULL; |
| 1395 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 126c559 | 2011-10-03 04:17:10 +0200 | [diff] [blame] | 1396 | /* unicode_convert_wchar_to_ucs4() requires a ready string */ |
| 1397 | _PyUnicode_STATE(unicode).ready = 1; |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1398 | unicode_convert_wchar_to_ucs4(_PyUnicode_WSTR(unicode), end, unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1399 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1400 | _PyUnicode_WSTR(unicode) = NULL; |
| 1401 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1402 | #else |
| 1403 | assert(num_surrogates == 0); |
| 1404 | |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1405 | _PyUnicode_DATA_ANY(unicode) = _PyUnicode_WSTR(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1406 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1407 | _PyUnicode_UTF8(unicode) = NULL; |
| 1408 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1409 | _PyUnicode_STATE(unicode).kind = PyUnicode_4BYTE_KIND; |
| 1410 | #endif |
| 1411 | PyUnicode_4BYTE_DATA(unicode)[_PyUnicode_LENGTH(unicode)] = '\0'; |
| 1412 | } |
| 1413 | _PyUnicode_STATE(unicode).ready = 1; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1414 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1415 | return 0; |
| 1416 | } |
| 1417 | |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 1418 | int |
| 1419 | _PyUnicode_ReadyReplace(PyObject **op) |
| 1420 | { |
| 1421 | return unicode_ready(op, 1); |
| 1422 | } |
| 1423 | |
| 1424 | int |
| 1425 | _PyUnicode_Ready(PyObject *op) |
| 1426 | { |
| 1427 | return unicode_ready(&op, 0); |
| 1428 | } |
| 1429 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1430 | static void |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1431 | unicode_dealloc(register PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1432 | { |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1433 | switch (PyUnicode_CHECK_INTERNED(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1434 | case SSTATE_NOT_INTERNED: |
| 1435 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1436 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1437 | case SSTATE_INTERNED_MORTAL: |
| 1438 | /* revive dead object temporarily for DelItem */ |
| 1439 | Py_REFCNT(unicode) = 3; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 1440 | if (PyDict_DelItem(interned, unicode) != 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1441 | Py_FatalError( |
| 1442 | "deletion of interned string failed"); |
| 1443 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1444 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1445 | case SSTATE_INTERNED_IMMORTAL: |
| 1446 | Py_FatalError("Immortal interned string died."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1447 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1448 | default: |
| 1449 | Py_FatalError("Inconsistent interned string state."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1450 | } |
| 1451 | |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 1452 | if (_PyUnicode_HAS_WSTR_MEMORY(unicode)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1453 | PyObject_DEL(_PyUnicode_WSTR(unicode)); |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 1454 | if (_PyUnicode_HAS_UTF8_MEMORY(unicode)) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1455 | PyObject_DEL(_PyUnicode_UTF8(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1456 | |
| 1457 | if (PyUnicode_IS_COMPACT(unicode)) { |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 1458 | Py_TYPE(unicode)->tp_free(unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1459 | } |
| 1460 | else { |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1461 | if (_PyUnicode_DATA_ANY(unicode)) |
| 1462 | PyObject_DEL(_PyUnicode_DATA_ANY(unicode)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 1463 | Py_TYPE(unicode)->tp_free(unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1464 | } |
| 1465 | } |
| 1466 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1467 | #ifdef Py_DEBUG |
| 1468 | static int |
| 1469 | unicode_is_singleton(PyObject *unicode) |
| 1470 | { |
| 1471 | PyASCIIObject *ascii = (PyASCIIObject *)unicode; |
| 1472 | if (unicode == unicode_empty) |
| 1473 | return 1; |
| 1474 | if (ascii->state.kind != PyUnicode_WCHAR_KIND && ascii->length == 1) |
| 1475 | { |
| 1476 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, 0); |
| 1477 | if (ch < 256 && unicode_latin1[ch] == unicode) |
| 1478 | return 1; |
| 1479 | } |
| 1480 | return 0; |
| 1481 | } |
| 1482 | #endif |
| 1483 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1484 | static int |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1485 | unicode_resizable(PyObject *unicode) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1486 | { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1487 | if (Py_REFCNT(unicode) != 1) |
| 1488 | return 0; |
| 1489 | if (PyUnicode_CHECK_INTERNED(unicode)) |
| 1490 | return 0; |
Victor Stinner | 77bb47b | 2011-10-03 20:06:05 +0200 | [diff] [blame] | 1491 | #ifdef Py_DEBUG |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1492 | /* singleton refcount is greater than 1 */ |
| 1493 | assert(!unicode_is_singleton(unicode)); |
Victor Stinner | 77bb47b | 2011-10-03 20:06:05 +0200 | [diff] [blame] | 1494 | #endif |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1495 | return 1; |
| 1496 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1497 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1498 | static int |
| 1499 | unicode_resize(PyObject **p_unicode, Py_ssize_t length) |
| 1500 | { |
| 1501 | PyObject *unicode; |
| 1502 | Py_ssize_t old_length; |
| 1503 | |
| 1504 | assert(p_unicode != NULL); |
| 1505 | unicode = *p_unicode; |
| 1506 | |
| 1507 | assert(unicode != NULL); |
| 1508 | assert(PyUnicode_Check(unicode)); |
| 1509 | assert(0 <= length); |
| 1510 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1511 | if (_PyUnicode_KIND(unicode) == PyUnicode_WCHAR_KIND) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1512 | old_length = PyUnicode_WSTR_LENGTH(unicode); |
| 1513 | else |
| 1514 | old_length = PyUnicode_GET_LENGTH(unicode); |
| 1515 | if (old_length == length) |
| 1516 | return 0; |
| 1517 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1518 | if (!unicode_resizable(unicode)) { |
| 1519 | PyObject *copy = resize_copy(unicode, length); |
| 1520 | if (copy == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1521 | return -1; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1522 | Py_DECREF(*p_unicode); |
| 1523 | *p_unicode = copy; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1524 | return 0; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1525 | } |
| 1526 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1527 | if (PyUnicode_IS_COMPACT(unicode)) { |
| 1528 | *p_unicode = resize_compact(unicode, length); |
| 1529 | if (*p_unicode == NULL) |
| 1530 | return -1; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1531 | assert(_PyUnicode_CheckConsistency(*p_unicode, 0)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1532 | return 0; |
Benjamin Peterson | 4bfce8f | 2011-10-03 19:35:07 -0400 | [diff] [blame] | 1533 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1534 | return resize_inplace(unicode, length); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1535 | } |
| 1536 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1537 | int |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1538 | PyUnicode_Resize(PyObject **p_unicode, Py_ssize_t length) |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 1539 | { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1540 | PyObject *unicode; |
| 1541 | if (p_unicode == NULL) { |
| 1542 | PyErr_BadInternalCall(); |
| 1543 | return -1; |
| 1544 | } |
| 1545 | unicode = *p_unicode; |
| 1546 | if (unicode == NULL || !PyUnicode_Check(unicode) || length < 0 |
| 1547 | || _PyUnicode_KIND(unicode) != PyUnicode_WCHAR_KIND) |
| 1548 | { |
| 1549 | PyErr_BadInternalCall(); |
| 1550 | return -1; |
| 1551 | } |
| 1552 | return unicode_resize(p_unicode, length); |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 1553 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1554 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1555 | static PyObject* |
| 1556 | get_latin1_char(unsigned char ch) |
| 1557 | { |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1558 | PyObject *unicode = unicode_latin1[ch]; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1559 | if (!unicode) { |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1560 | unicode = PyUnicode_New(1, ch); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1561 | if (!unicode) |
| 1562 | return NULL; |
| 1563 | PyUnicode_1BYTE_DATA(unicode)[0] = ch; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1564 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1565 | unicode_latin1[ch] = unicode; |
| 1566 | } |
| 1567 | Py_INCREF(unicode); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1568 | return unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1569 | } |
| 1570 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1571 | PyObject * |
| 1572 | PyUnicode_FromUnicode(const Py_UNICODE *u, Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1573 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1574 | PyObject *unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1575 | Py_UCS4 maxchar = 0; |
| 1576 | Py_ssize_t num_surrogates; |
| 1577 | |
| 1578 | if (u == NULL) |
| 1579 | return (PyObject*)_PyUnicode_New(size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1580 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1581 | /* If the Unicode data is known at construction time, we can apply |
| 1582 | some optimizations which share commonly used objects. */ |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1583 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1584 | /* Optimization for empty strings */ |
| 1585 | if (size == 0 && unicode_empty != NULL) { |
| 1586 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1587 | return unicode_empty; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1588 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1589 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1590 | /* Single character Unicode objects in the Latin-1 range are |
| 1591 | shared when using this constructor */ |
| 1592 | if (size == 1 && *u < 256) |
| 1593 | return get_latin1_char((unsigned char)*u); |
| 1594 | |
| 1595 | /* If not empty and not single character, copy the Unicode data |
| 1596 | into the new object */ |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1597 | if (find_maxchar_surrogates(u, u + size, |
| 1598 | &maxchar, &num_surrogates) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1599 | return NULL; |
| 1600 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1601 | unicode = PyUnicode_New(size - num_surrogates, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1602 | maxchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1603 | if (!unicode) |
| 1604 | return NULL; |
| 1605 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1606 | switch (PyUnicode_KIND(unicode)) { |
| 1607 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1608 | _PyUnicode_CONVERT_BYTES(Py_UNICODE, unsigned char, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1609 | u, u + size, PyUnicode_1BYTE_DATA(unicode)); |
| 1610 | break; |
| 1611 | case PyUnicode_2BYTE_KIND: |
| 1612 | #if Py_UNICODE_SIZE == 2 |
| 1613 | Py_MEMCPY(PyUnicode_2BYTE_DATA(unicode), u, size * 2); |
| 1614 | #else |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1615 | _PyUnicode_CONVERT_BYTES(Py_UNICODE, Py_UCS2, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1616 | u, u + size, PyUnicode_2BYTE_DATA(unicode)); |
| 1617 | #endif |
| 1618 | break; |
| 1619 | case PyUnicode_4BYTE_KIND: |
| 1620 | #if SIZEOF_WCHAR_T == 2 |
| 1621 | /* This is the only case which has to process surrogates, thus |
| 1622 | a simple copy loop is not enough and we need a function. */ |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1623 | unicode_convert_wchar_to_ucs4(u, u + size, unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1624 | #else |
| 1625 | assert(num_surrogates == 0); |
| 1626 | Py_MEMCPY(PyUnicode_4BYTE_DATA(unicode), u, size * 4); |
| 1627 | #endif |
| 1628 | break; |
| 1629 | default: |
| 1630 | assert(0 && "Impossible state"); |
| 1631 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1632 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1633 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1634 | return unicode; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1635 | } |
| 1636 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1637 | PyObject * |
| 1638 | PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size) |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1639 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1640 | if (size < 0) { |
| 1641 | PyErr_SetString(PyExc_SystemError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1642 | "Negative size passed to PyUnicode_FromStringAndSize"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1643 | return NULL; |
| 1644 | } |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1645 | |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1646 | /* 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] | 1647 | some optimizations which share commonly used objects. |
| 1648 | Also, this means the input must be UTF-8, so fall back to the |
| 1649 | UTF-8 decoder at the end. */ |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1650 | if (u != NULL) { |
| 1651 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1652 | /* Optimization for empty strings */ |
| 1653 | if (size == 0 && unicode_empty != NULL) { |
| 1654 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1655 | return unicode_empty; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1656 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1657 | |
| 1658 | /* Single characters are shared when using this constructor. |
| 1659 | Restrict to ASCII, since the input must be UTF-8. */ |
Victor Stinner | 9faa384 | 2011-10-23 20:06:00 +0200 | [diff] [blame] | 1660 | if (size == 1 && (unsigned char)*u < 128) |
| 1661 | return get_latin1_char((unsigned char)*u); |
Martin v. Löwis | 9c12106 | 2007-08-05 20:26:11 +0000 | [diff] [blame] | 1662 | |
| 1663 | return PyUnicode_DecodeUTF8(u, size, NULL); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1664 | } |
| 1665 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1666 | return (PyObject *)_PyUnicode_New(size); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1667 | } |
| 1668 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1669 | PyObject * |
| 1670 | PyUnicode_FromString(const char *u) |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1671 | { |
| 1672 | size_t size = strlen(u); |
| 1673 | if (size > PY_SSIZE_T_MAX) { |
| 1674 | PyErr_SetString(PyExc_OverflowError, "input too long"); |
| 1675 | return NULL; |
| 1676 | } |
| 1677 | |
| 1678 | return PyUnicode_FromStringAndSize(u, size); |
| 1679 | } |
| 1680 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1681 | PyObject * |
| 1682 | _PyUnicode_FromId(_Py_Identifier *id) |
| 1683 | { |
| 1684 | if (!id->object) { |
| 1685 | id->object = PyUnicode_FromString(id->string); |
| 1686 | if (!id->object) |
| 1687 | return NULL; |
| 1688 | PyUnicode_InternInPlace(&id->object); |
| 1689 | assert(!id->next); |
| 1690 | id->next = static_strings; |
| 1691 | static_strings = id; |
| 1692 | } |
| 1693 | Py_INCREF(id->object); |
| 1694 | return id->object; |
| 1695 | } |
| 1696 | |
| 1697 | void |
| 1698 | _PyUnicode_ClearStaticStrings() |
| 1699 | { |
| 1700 | _Py_Identifier *i; |
| 1701 | for (i = static_strings; i; i = i->next) { |
| 1702 | Py_DECREF(i->object); |
| 1703 | i->object = NULL; |
| 1704 | i->next = NULL; |
| 1705 | } |
| 1706 | } |
| 1707 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1708 | static PyObject* |
Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 1709 | unicode_fromascii(const unsigned char* s, Py_ssize_t size) |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1710 | { |
Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 1711 | PyObject *res; |
| 1712 | #ifdef Py_DEBUG |
| 1713 | const unsigned char *p; |
| 1714 | const unsigned char *end = s + size; |
| 1715 | for (p=s; p < end; p++) { |
| 1716 | assert(*p < 128); |
| 1717 | } |
| 1718 | #endif |
Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 1719 | if (size == 1) |
| 1720 | return get_latin1_char(s[0]); |
Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 1721 | res = PyUnicode_New(size, 127); |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1722 | if (!res) |
| 1723 | return NULL; |
Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 1724 | memcpy(PyUnicode_1BYTE_DATA(res), s, size); |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1725 | return res; |
| 1726 | } |
| 1727 | |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 1728 | static Py_UCS4 |
| 1729 | kind_maxchar_limit(unsigned int kind) |
| 1730 | { |
| 1731 | switch(kind) { |
| 1732 | case PyUnicode_1BYTE_KIND: |
| 1733 | return 0x80; |
| 1734 | case PyUnicode_2BYTE_KIND: |
| 1735 | return 0x100; |
| 1736 | case PyUnicode_4BYTE_KIND: |
| 1737 | return 0x10000; |
| 1738 | default: |
| 1739 | assert(0 && "invalid kind"); |
| 1740 | return 0x10ffff; |
| 1741 | } |
| 1742 | } |
| 1743 | |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1744 | static PyObject* |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1745 | _PyUnicode_FromUCS1(const unsigned char* u, Py_ssize_t size) |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1746 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1747 | PyObject *res; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1748 | unsigned char max_char = 127; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1749 | |
| 1750 | assert(size >= 0); |
Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 1751 | if (size == 1) |
| 1752 | return get_latin1_char(u[0]); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1753 | max_char = ucs1lib_find_max_char(u, u + size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1754 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1755 | if (!res) |
| 1756 | return NULL; |
| 1757 | memcpy(PyUnicode_1BYTE_DATA(res), u, size); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1758 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1759 | return res; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1760 | } |
| 1761 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1762 | static PyObject* |
| 1763 | _PyUnicode_FromUCS2(const Py_UCS2 *u, Py_ssize_t size) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1764 | { |
| 1765 | PyObject *res; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1766 | Py_UCS2 max_char = 0; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1767 | |
| 1768 | assert(size >= 0); |
Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 1769 | if (size == 1 && u[0] < 256) |
Victor Stinner | 4e10100 | 2011-10-11 23:27:52 +0200 | [diff] [blame] | 1770 | return get_latin1_char((unsigned char)u[0]); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1771 | max_char = ucs2lib_find_max_char(u, u + size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1772 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1773 | if (!res) |
| 1774 | return NULL; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1775 | if (max_char >= 256) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1776 | memcpy(PyUnicode_2BYTE_DATA(res), u, sizeof(Py_UCS2)*size); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1777 | else { |
| 1778 | _PyUnicode_CONVERT_BYTES( |
| 1779 | Py_UCS2, Py_UCS1, u, u + size, PyUnicode_1BYTE_DATA(res)); |
| 1780 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1781 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1782 | return res; |
| 1783 | } |
| 1784 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1785 | static PyObject* |
| 1786 | _PyUnicode_FromUCS4(const Py_UCS4 *u, Py_ssize_t size) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1787 | { |
| 1788 | PyObject *res; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1789 | Py_UCS4 max_char = 0; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1790 | |
| 1791 | assert(size >= 0); |
Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 1792 | if (size == 1 && u[0] < 256) |
| 1793 | return get_latin1_char(u[0]); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1794 | max_char = ucs4lib_find_max_char(u, u + size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1795 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1796 | if (!res) |
| 1797 | return NULL; |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 1798 | if (max_char < 256) |
| 1799 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS1, u, u + size, |
| 1800 | PyUnicode_1BYTE_DATA(res)); |
| 1801 | else if (max_char < 0x10000) |
| 1802 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS2, u, u + size, |
| 1803 | PyUnicode_2BYTE_DATA(res)); |
| 1804 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1805 | memcpy(PyUnicode_4BYTE_DATA(res), u, sizeof(Py_UCS4)*size); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1806 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1807 | return res; |
| 1808 | } |
| 1809 | |
| 1810 | PyObject* |
| 1811 | PyUnicode_FromKindAndData(int kind, const void *buffer, Py_ssize_t size) |
| 1812 | { |
| 1813 | switch(kind) { |
| 1814 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1815 | return _PyUnicode_FromUCS1(buffer, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1816 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1817 | return _PyUnicode_FromUCS2(buffer, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1818 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1819 | return _PyUnicode_FromUCS4(buffer, size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1820 | default: |
| 1821 | assert(0 && "invalid kind"); |
| 1822 | PyErr_SetString(PyExc_SystemError, "invalid kind"); |
| 1823 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1824 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1825 | } |
| 1826 | |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1827 | /* Ensure that a string uses the most efficient storage, if it is not the |
| 1828 | case: create a new string with of the right kind. Write NULL into *p_unicode |
| 1829 | on error. */ |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 1830 | static void |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1831 | unicode_adjust_maxchar(PyObject **p_unicode) |
| 1832 | { |
| 1833 | PyObject *unicode, *copy; |
| 1834 | Py_UCS4 max_char; |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1835 | Py_ssize_t len; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1836 | unsigned int kind; |
| 1837 | |
| 1838 | assert(p_unicode != NULL); |
| 1839 | unicode = *p_unicode; |
| 1840 | assert(PyUnicode_IS_READY(unicode)); |
| 1841 | if (PyUnicode_IS_ASCII(unicode)) |
| 1842 | return; |
| 1843 | |
| 1844 | len = PyUnicode_GET_LENGTH(unicode); |
| 1845 | kind = PyUnicode_KIND(unicode); |
| 1846 | if (kind == PyUnicode_1BYTE_KIND) { |
| 1847 | const Py_UCS1 *u = PyUnicode_1BYTE_DATA(unicode); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1848 | max_char = ucs1lib_find_max_char(u, u + len); |
| 1849 | if (max_char >= 128) |
| 1850 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1851 | } |
| 1852 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 1853 | const Py_UCS2 *u = PyUnicode_2BYTE_DATA(unicode); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1854 | max_char = ucs2lib_find_max_char(u, u + len); |
| 1855 | if (max_char >= 256) |
| 1856 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1857 | } |
| 1858 | else { |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1859 | const Py_UCS4 *u = PyUnicode_4BYTE_DATA(unicode); |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1860 | assert(kind == PyUnicode_4BYTE_KIND); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1861 | max_char = ucs4lib_find_max_char(u, u + len); |
| 1862 | if (max_char >= 0x10000) |
| 1863 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1864 | } |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1865 | copy = PyUnicode_New(len, max_char); |
| 1866 | copy_characters(copy, 0, unicode, 0, len); |
| 1867 | Py_DECREF(unicode); |
| 1868 | *p_unicode = copy; |
| 1869 | } |
| 1870 | |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 1871 | PyObject* |
| 1872 | PyUnicode_Copy(PyObject *unicode) |
| 1873 | { |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 1874 | Py_ssize_t size; |
| 1875 | PyObject *copy; |
| 1876 | void *data; |
| 1877 | |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 1878 | if (!PyUnicode_Check(unicode)) { |
| 1879 | PyErr_BadInternalCall(); |
| 1880 | return NULL; |
| 1881 | } |
| 1882 | if (PyUnicode_READY(unicode)) |
| 1883 | return NULL; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 1884 | |
| 1885 | size = PyUnicode_GET_LENGTH(unicode); |
| 1886 | copy = PyUnicode_New(size, PyUnicode_MAX_CHAR_VALUE(unicode)); |
| 1887 | if (!copy) |
| 1888 | return NULL; |
| 1889 | assert(PyUnicode_KIND(copy) == PyUnicode_KIND(unicode)); |
| 1890 | |
| 1891 | data = PyUnicode_DATA(unicode); |
| 1892 | switch (PyUnicode_KIND(unicode)) |
| 1893 | { |
| 1894 | case PyUnicode_1BYTE_KIND: |
| 1895 | memcpy(PyUnicode_1BYTE_DATA(copy), data, size); |
| 1896 | break; |
| 1897 | case PyUnicode_2BYTE_KIND: |
| 1898 | memcpy(PyUnicode_2BYTE_DATA(copy), data, sizeof(Py_UCS2) * size); |
| 1899 | break; |
| 1900 | case PyUnicode_4BYTE_KIND: |
| 1901 | memcpy(PyUnicode_4BYTE_DATA(copy), data, sizeof(Py_UCS4) * size); |
| 1902 | break; |
| 1903 | default: |
| 1904 | assert(0); |
| 1905 | break; |
| 1906 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1907 | assert(_PyUnicode_CheckConsistency(copy, 1)); |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 1908 | return copy; |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 1909 | } |
| 1910 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1911 | |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1912 | /* Widen Unicode objects to larger buffers. Don't write terminating null |
| 1913 | character. Return NULL on error. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1914 | |
| 1915 | void* |
| 1916 | _PyUnicode_AsKind(PyObject *s, unsigned int kind) |
| 1917 | { |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1918 | Py_ssize_t len; |
| 1919 | void *result; |
| 1920 | unsigned int skind; |
| 1921 | |
| 1922 | if (PyUnicode_READY(s)) |
| 1923 | return NULL; |
| 1924 | |
| 1925 | len = PyUnicode_GET_LENGTH(s); |
| 1926 | skind = PyUnicode_KIND(s); |
| 1927 | if (skind >= kind) { |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 1928 | PyErr_SetString(PyExc_SystemError, "invalid widening attempt"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1929 | return NULL; |
| 1930 | } |
| 1931 | switch(kind) { |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1932 | case PyUnicode_2BYTE_KIND: |
| 1933 | result = PyMem_Malloc(len * sizeof(Py_UCS2)); |
| 1934 | if (!result) |
| 1935 | return PyErr_NoMemory(); |
| 1936 | assert(skind == PyUnicode_1BYTE_KIND); |
| 1937 | _PyUnicode_CONVERT_BYTES( |
| 1938 | Py_UCS1, Py_UCS2, |
| 1939 | PyUnicode_1BYTE_DATA(s), |
| 1940 | PyUnicode_1BYTE_DATA(s) + len, |
| 1941 | result); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1942 | return result; |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1943 | case PyUnicode_4BYTE_KIND: |
| 1944 | result = PyMem_Malloc(len * sizeof(Py_UCS4)); |
| 1945 | if (!result) |
| 1946 | return PyErr_NoMemory(); |
| 1947 | if (skind == PyUnicode_2BYTE_KIND) { |
| 1948 | _PyUnicode_CONVERT_BYTES( |
| 1949 | Py_UCS2, Py_UCS4, |
| 1950 | PyUnicode_2BYTE_DATA(s), |
| 1951 | PyUnicode_2BYTE_DATA(s) + len, |
| 1952 | result); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1953 | } |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1954 | else { |
| 1955 | assert(skind == PyUnicode_1BYTE_KIND); |
| 1956 | _PyUnicode_CONVERT_BYTES( |
| 1957 | Py_UCS1, Py_UCS4, |
| 1958 | PyUnicode_1BYTE_DATA(s), |
| 1959 | PyUnicode_1BYTE_DATA(s) + len, |
| 1960 | result); |
| 1961 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1962 | return result; |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1963 | default: |
| 1964 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1965 | } |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 1966 | PyErr_SetString(PyExc_SystemError, "invalid kind"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1967 | return NULL; |
| 1968 | } |
| 1969 | |
| 1970 | static Py_UCS4* |
| 1971 | as_ucs4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, |
| 1972 | int copy_null) |
| 1973 | { |
| 1974 | int kind; |
| 1975 | void *data; |
| 1976 | Py_ssize_t len, targetlen; |
| 1977 | if (PyUnicode_READY(string) == -1) |
| 1978 | return NULL; |
| 1979 | kind = PyUnicode_KIND(string); |
| 1980 | data = PyUnicode_DATA(string); |
| 1981 | len = PyUnicode_GET_LENGTH(string); |
| 1982 | targetlen = len; |
| 1983 | if (copy_null) |
| 1984 | targetlen++; |
| 1985 | if (!target) { |
| 1986 | if (PY_SSIZE_T_MAX / sizeof(Py_UCS4) < targetlen) { |
| 1987 | PyErr_NoMemory(); |
| 1988 | return NULL; |
| 1989 | } |
| 1990 | target = PyMem_Malloc(targetlen * sizeof(Py_UCS4)); |
| 1991 | if (!target) { |
| 1992 | PyErr_NoMemory(); |
| 1993 | return NULL; |
| 1994 | } |
| 1995 | } |
| 1996 | else { |
| 1997 | if (targetsize < targetlen) { |
| 1998 | PyErr_Format(PyExc_SystemError, |
| 1999 | "string is longer than the buffer"); |
| 2000 | if (copy_null && 0 < targetsize) |
| 2001 | target[0] = 0; |
| 2002 | return NULL; |
| 2003 | } |
| 2004 | } |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2005 | if (kind == PyUnicode_1BYTE_KIND) { |
| 2006 | Py_UCS1 *start = (Py_UCS1 *) data; |
| 2007 | _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] | 2008 | } |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2009 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 2010 | Py_UCS2 *start = (Py_UCS2 *) data; |
| 2011 | _PyUnicode_CONVERT_BYTES(Py_UCS2, Py_UCS4, start, start + len, target); |
| 2012 | } |
| 2013 | else { |
| 2014 | assert(kind == PyUnicode_4BYTE_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2015 | Py_MEMCPY(target, data, len * sizeof(Py_UCS4)); |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2016 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2017 | if (copy_null) |
| 2018 | target[len] = 0; |
| 2019 | return target; |
| 2020 | } |
| 2021 | |
| 2022 | Py_UCS4* |
| 2023 | PyUnicode_AsUCS4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, |
| 2024 | int copy_null) |
| 2025 | { |
| 2026 | if (target == NULL || targetsize < 1) { |
| 2027 | PyErr_BadInternalCall(); |
| 2028 | return NULL; |
| 2029 | } |
| 2030 | return as_ucs4(string, target, targetsize, copy_null); |
| 2031 | } |
| 2032 | |
| 2033 | Py_UCS4* |
| 2034 | PyUnicode_AsUCS4Copy(PyObject *string) |
| 2035 | { |
| 2036 | return as_ucs4(string, NULL, 0, 1); |
| 2037 | } |
| 2038 | |
| 2039 | #ifdef HAVE_WCHAR_H |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 2040 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2041 | PyObject * |
| 2042 | PyUnicode_FromWideChar(register const wchar_t *w, Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2043 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2044 | if (w == NULL) { |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 2045 | if (size == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2046 | return PyUnicode_New(0, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2047 | PyErr_BadInternalCall(); |
| 2048 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2049 | } |
| 2050 | |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 2051 | if (size == -1) { |
| 2052 | size = wcslen(w); |
| 2053 | } |
| 2054 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2055 | return PyUnicode_FromUnicode(w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2056 | } |
| 2057 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2058 | #endif /* HAVE_WCHAR_H */ |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 2059 | |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2060 | static void |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2061 | makefmt(char *fmt, int longflag, int longlongflag, int size_tflag, |
| 2062 | int zeropad, int width, int precision, char c) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2063 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2064 | *fmt++ = '%'; |
| 2065 | if (width) { |
| 2066 | if (zeropad) |
| 2067 | *fmt++ = '0'; |
| 2068 | fmt += sprintf(fmt, "%d", width); |
| 2069 | } |
| 2070 | if (precision) |
| 2071 | fmt += sprintf(fmt, ".%d", precision); |
| 2072 | if (longflag) |
| 2073 | *fmt++ = 'l'; |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2074 | else if (longlongflag) { |
| 2075 | /* longlongflag should only ever be nonzero on machines with |
| 2076 | HAVE_LONG_LONG defined */ |
| 2077 | #ifdef HAVE_LONG_LONG |
| 2078 | char *f = PY_FORMAT_LONG_LONG; |
| 2079 | while (*f) |
| 2080 | *fmt++ = *f++; |
| 2081 | #else |
| 2082 | /* we shouldn't ever get here */ |
| 2083 | assert(0); |
| 2084 | *fmt++ = 'l'; |
| 2085 | #endif |
| 2086 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2087 | else if (size_tflag) { |
| 2088 | char *f = PY_FORMAT_SIZE_T; |
| 2089 | while (*f) |
| 2090 | *fmt++ = *f++; |
| 2091 | } |
| 2092 | *fmt++ = c; |
| 2093 | *fmt = '\0'; |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2094 | } |
| 2095 | |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2096 | /* helper for PyUnicode_FromFormatV() */ |
| 2097 | |
| 2098 | static const char* |
| 2099 | parse_format_flags(const char *f, |
| 2100 | int *p_width, int *p_precision, |
| 2101 | int *p_longflag, int *p_longlongflag, int *p_size_tflag) |
| 2102 | { |
| 2103 | int width, precision, longflag, longlongflag, size_tflag; |
| 2104 | |
| 2105 | /* parse the width.precision part, e.g. "%2.5s" => width=2, precision=5 */ |
| 2106 | f++; |
| 2107 | width = 0; |
| 2108 | while (Py_ISDIGIT((unsigned)*f)) |
| 2109 | width = (width*10) + *f++ - '0'; |
| 2110 | precision = 0; |
| 2111 | if (*f == '.') { |
| 2112 | f++; |
| 2113 | while (Py_ISDIGIT((unsigned)*f)) |
| 2114 | precision = (precision*10) + *f++ - '0'; |
| 2115 | if (*f == '%') { |
| 2116 | /* "%.3%s" => f points to "3" */ |
| 2117 | f--; |
| 2118 | } |
| 2119 | } |
| 2120 | if (*f == '\0') { |
| 2121 | /* bogus format "%.1" => go backward, f points to "1" */ |
| 2122 | f--; |
| 2123 | } |
| 2124 | if (p_width != NULL) |
| 2125 | *p_width = width; |
| 2126 | if (p_precision != NULL) |
| 2127 | *p_precision = precision; |
| 2128 | |
| 2129 | /* Handle %ld, %lu, %lld and %llu. */ |
| 2130 | longflag = 0; |
| 2131 | longlongflag = 0; |
Victor Stinner | e7faec1 | 2011-03-02 00:01:53 +0000 | [diff] [blame] | 2132 | size_tflag = 0; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2133 | |
| 2134 | if (*f == 'l') { |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2135 | if (f[1] == 'd' || f[1] == 'u' || f[1] == 'i') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2136 | longflag = 1; |
| 2137 | ++f; |
| 2138 | } |
| 2139 | #ifdef HAVE_LONG_LONG |
| 2140 | else if (f[1] == 'l' && |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2141 | (f[2] == 'd' || f[2] == 'u' || f[2] == 'i')) { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2142 | longlongflag = 1; |
| 2143 | f += 2; |
| 2144 | } |
| 2145 | #endif |
| 2146 | } |
| 2147 | /* handle the size_t flag. */ |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2148 | 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] | 2149 | size_tflag = 1; |
| 2150 | ++f; |
| 2151 | } |
| 2152 | if (p_longflag != NULL) |
| 2153 | *p_longflag = longflag; |
| 2154 | if (p_longlongflag != NULL) |
| 2155 | *p_longlongflag = longlongflag; |
| 2156 | if (p_size_tflag != NULL) |
| 2157 | *p_size_tflag = size_tflag; |
| 2158 | return f; |
| 2159 | } |
| 2160 | |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2161 | /* maximum number of characters required for output of %ld. 21 characters |
| 2162 | allows for 64-bit integers (in decimal) and an optional sign. */ |
| 2163 | #define MAX_LONG_CHARS 21 |
| 2164 | /* maximum number of characters required for output of %lld. |
| 2165 | We need at most ceil(log10(256)*SIZEOF_LONG_LONG) digits, |
| 2166 | plus 1 for the sign. 53/22 is an upper bound for log10(256). */ |
| 2167 | #define MAX_LONG_LONG_CHARS (2 + (SIZEOF_LONG_LONG*53-1) / 22) |
| 2168 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2169 | PyObject * |
| 2170 | PyUnicode_FromFormatV(const char *format, va_list vargs) |
| 2171 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2172 | va_list count; |
| 2173 | Py_ssize_t callcount = 0; |
| 2174 | PyObject **callresults = NULL; |
| 2175 | PyObject **callresult = NULL; |
| 2176 | Py_ssize_t n = 0; |
| 2177 | int width = 0; |
| 2178 | int precision = 0; |
| 2179 | int zeropad; |
| 2180 | const char* f; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2181 | PyObject *string; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2182 | /* used by sprintf */ |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2183 | char fmt[61]; /* should be enough for %0width.precisionlld */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2184 | Py_UCS4 maxchar = 127; /* result is ASCII by default */ |
| 2185 | Py_UCS4 argmaxchar; |
| 2186 | Py_ssize_t numbersize = 0; |
| 2187 | char *numberresults = NULL; |
| 2188 | char *numberresult = NULL; |
| 2189 | Py_ssize_t i; |
| 2190 | int kind; |
| 2191 | void *data; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2192 | |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 2193 | Py_VA_COPY(count, vargs); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2194 | /* step 1: count the number of %S/%R/%A/%s format specifications |
| 2195 | * (we call PyObject_Str()/PyObject_Repr()/PyObject_ASCII()/ |
| 2196 | * 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] | 2197 | * result in an array) |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 2198 | * also estimate a upper bound for all the number formats in the string, |
| 2199 | * 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] | 2200 | * buffer before putting everything together. */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2201 | for (f = format; *f; f++) { |
| 2202 | if (*f == '%') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2203 | int longlongflag; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2204 | /* skip width or width.precision (eg. "1.2" of "%1.2f") */ |
| 2205 | f = parse_format_flags(f, &width, NULL, NULL, &longlongflag, NULL); |
| 2206 | if (*f == 's' || *f=='S' || *f=='R' || *f=='A' || *f=='V') |
| 2207 | ++callcount; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2208 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2209 | else if (*f == 'd' || *f=='u' || *f=='i' || *f=='x' || *f=='p') { |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2210 | #ifdef HAVE_LONG_LONG |
| 2211 | if (longlongflag) { |
| 2212 | if (width < MAX_LONG_LONG_CHARS) |
| 2213 | width = MAX_LONG_LONG_CHARS; |
| 2214 | } |
| 2215 | else |
| 2216 | #endif |
| 2217 | /* MAX_LONG_CHARS is enough to hold a 64-bit integer, |
| 2218 | including sign. Decimal takes the most space. This |
| 2219 | isn't enough for octal. If a width is specified we |
| 2220 | need more (which we allocate later). */ |
| 2221 | if (width < MAX_LONG_CHARS) |
| 2222 | width = MAX_LONG_CHARS; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2223 | |
| 2224 | /* account for the size + '\0' to separate numbers |
| 2225 | inside of the numberresults buffer */ |
| 2226 | numbersize += (width + 1); |
| 2227 | } |
| 2228 | } |
| 2229 | else if ((unsigned char)*f > 127) { |
| 2230 | PyErr_Format(PyExc_ValueError, |
| 2231 | "PyUnicode_FromFormatV() expects an ASCII-encoded format " |
| 2232 | "string, got a non-ASCII byte: 0x%02x", |
| 2233 | (unsigned char)*f); |
| 2234 | return NULL; |
| 2235 | } |
| 2236 | } |
| 2237 | /* step 2: allocate memory for the results of |
| 2238 | * PyObject_Str()/PyObject_Repr()/PyUnicode_DecodeUTF8() calls */ |
| 2239 | if (callcount) { |
| 2240 | callresults = PyObject_Malloc(sizeof(PyObject *) * callcount); |
| 2241 | if (!callresults) { |
| 2242 | PyErr_NoMemory(); |
| 2243 | return NULL; |
| 2244 | } |
| 2245 | callresult = callresults; |
| 2246 | } |
| 2247 | /* step 2.5: allocate memory for the results of formating numbers */ |
| 2248 | if (numbersize) { |
| 2249 | numberresults = PyObject_Malloc(numbersize); |
| 2250 | if (!numberresults) { |
| 2251 | PyErr_NoMemory(); |
| 2252 | goto fail; |
| 2253 | } |
| 2254 | numberresult = numberresults; |
| 2255 | } |
| 2256 | |
| 2257 | /* step 3: format numbers and figure out how large a buffer we need */ |
| 2258 | for (f = format; *f; f++) { |
| 2259 | if (*f == '%') { |
| 2260 | const char* p; |
| 2261 | int longflag; |
| 2262 | int longlongflag; |
| 2263 | int size_tflag; |
| 2264 | int numprinted; |
| 2265 | |
| 2266 | p = f; |
| 2267 | zeropad = (f[1] == '0'); |
| 2268 | f = parse_format_flags(f, &width, &precision, |
| 2269 | &longflag, &longlongflag, &size_tflag); |
| 2270 | switch (*f) { |
| 2271 | case 'c': |
| 2272 | { |
| 2273 | Py_UCS4 ordinal = va_arg(count, int); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2274 | maxchar = Py_MAX(maxchar, ordinal); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2275 | n++; |
| 2276 | break; |
| 2277 | } |
| 2278 | case '%': |
| 2279 | n++; |
| 2280 | break; |
| 2281 | case 'i': |
| 2282 | case 'd': |
| 2283 | makefmt(fmt, longflag, longlongflag, size_tflag, zeropad, |
| 2284 | width, precision, *f); |
| 2285 | if (longflag) |
| 2286 | numprinted = sprintf(numberresult, fmt, |
| 2287 | va_arg(count, long)); |
| 2288 | #ifdef HAVE_LONG_LONG |
| 2289 | else if (longlongflag) |
| 2290 | numprinted = sprintf(numberresult, fmt, |
| 2291 | va_arg(count, PY_LONG_LONG)); |
| 2292 | #endif |
| 2293 | else if (size_tflag) |
| 2294 | numprinted = sprintf(numberresult, fmt, |
| 2295 | va_arg(count, Py_ssize_t)); |
| 2296 | else |
| 2297 | numprinted = sprintf(numberresult, fmt, |
| 2298 | va_arg(count, int)); |
| 2299 | n += numprinted; |
| 2300 | /* advance by +1 to skip over the '\0' */ |
| 2301 | numberresult += (numprinted + 1); |
| 2302 | assert(*(numberresult - 1) == '\0'); |
| 2303 | assert(*(numberresult - 2) != '\0'); |
| 2304 | assert(numprinted >= 0); |
| 2305 | assert(numberresult <= numberresults + numbersize); |
| 2306 | break; |
| 2307 | case 'u': |
| 2308 | makefmt(fmt, longflag, longlongflag, size_tflag, zeropad, |
| 2309 | width, precision, 'u'); |
| 2310 | if (longflag) |
| 2311 | numprinted = sprintf(numberresult, fmt, |
| 2312 | va_arg(count, unsigned long)); |
| 2313 | #ifdef HAVE_LONG_LONG |
| 2314 | else if (longlongflag) |
| 2315 | numprinted = sprintf(numberresult, fmt, |
| 2316 | va_arg(count, unsigned PY_LONG_LONG)); |
| 2317 | #endif |
| 2318 | else if (size_tflag) |
| 2319 | numprinted = sprintf(numberresult, fmt, |
| 2320 | va_arg(count, size_t)); |
| 2321 | else |
| 2322 | numprinted = sprintf(numberresult, fmt, |
| 2323 | va_arg(count, unsigned int)); |
| 2324 | n += numprinted; |
| 2325 | numberresult += (numprinted + 1); |
| 2326 | assert(*(numberresult - 1) == '\0'); |
| 2327 | assert(*(numberresult - 2) != '\0'); |
| 2328 | assert(numprinted >= 0); |
| 2329 | assert(numberresult <= numberresults + numbersize); |
| 2330 | break; |
| 2331 | case 'x': |
| 2332 | makefmt(fmt, 0, 0, 0, zeropad, width, precision, 'x'); |
| 2333 | numprinted = sprintf(numberresult, fmt, va_arg(count, int)); |
| 2334 | n += numprinted; |
| 2335 | numberresult += (numprinted + 1); |
| 2336 | assert(*(numberresult - 1) == '\0'); |
| 2337 | assert(*(numberresult - 2) != '\0'); |
| 2338 | assert(numprinted >= 0); |
| 2339 | assert(numberresult <= numberresults + numbersize); |
| 2340 | break; |
| 2341 | case 'p': |
| 2342 | numprinted = sprintf(numberresult, "%p", va_arg(count, void*)); |
| 2343 | /* %p is ill-defined: ensure leading 0x. */ |
| 2344 | if (numberresult[1] == 'X') |
| 2345 | numberresult[1] = 'x'; |
| 2346 | else if (numberresult[1] != 'x') { |
| 2347 | memmove(numberresult + 2, numberresult, |
| 2348 | strlen(numberresult) + 1); |
| 2349 | numberresult[0] = '0'; |
| 2350 | numberresult[1] = 'x'; |
| 2351 | numprinted += 2; |
| 2352 | } |
| 2353 | n += numprinted; |
| 2354 | numberresult += (numprinted + 1); |
| 2355 | assert(*(numberresult - 1) == '\0'); |
| 2356 | assert(*(numberresult - 2) != '\0'); |
| 2357 | assert(numprinted >= 0); |
| 2358 | assert(numberresult <= numberresults + numbersize); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2359 | break; |
| 2360 | case 's': |
| 2361 | { |
| 2362 | /* UTF-8 */ |
Georg Brandl | 780b2a6 | 2009-05-05 09:19:59 +0000 | [diff] [blame] | 2363 | const char *s = va_arg(count, const char*); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2364 | PyObject *str = PyUnicode_DecodeUTF8(s, strlen(s), "replace"); |
| 2365 | if (!str) |
| 2366 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2367 | /* since PyUnicode_DecodeUTF8 returns already flexible |
| 2368 | unicode objects, there is no need to call ready on them */ |
| 2369 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2370 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2371 | n += PyUnicode_GET_LENGTH(str); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2372 | /* Remember the str and switch to the next slot */ |
| 2373 | *callresult++ = str; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2374 | break; |
| 2375 | } |
| 2376 | case 'U': |
| 2377 | { |
| 2378 | PyObject *obj = va_arg(count, PyObject *); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 2379 | assert(obj && _PyUnicode_CHECK(obj)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2380 | if (PyUnicode_READY(obj) == -1) |
| 2381 | goto fail; |
| 2382 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2383 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2384 | n += PyUnicode_GET_LENGTH(obj); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2385 | break; |
| 2386 | } |
| 2387 | case 'V': |
| 2388 | { |
| 2389 | PyObject *obj = va_arg(count, PyObject *); |
| 2390 | const char *str = va_arg(count, const char *); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2391 | PyObject *str_obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2392 | assert(obj || str); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 2393 | assert(!obj || _PyUnicode_CHECK(obj)); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2394 | if (obj) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2395 | if (PyUnicode_READY(obj) == -1) |
| 2396 | goto fail; |
| 2397 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2398 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2399 | n += PyUnicode_GET_LENGTH(obj); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2400 | *callresult++ = NULL; |
| 2401 | } |
| 2402 | else { |
| 2403 | str_obj = PyUnicode_DecodeUTF8(str, strlen(str), "replace"); |
| 2404 | if (!str_obj) |
| 2405 | goto fail; |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 2406 | if (PyUnicode_READY(str_obj)) { |
| 2407 | Py_DECREF(str_obj); |
| 2408 | goto fail; |
| 2409 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2410 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str_obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2411 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2412 | n += PyUnicode_GET_LENGTH(str_obj); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2413 | *callresult++ = str_obj; |
| 2414 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2415 | break; |
| 2416 | } |
| 2417 | case 'S': |
| 2418 | { |
| 2419 | PyObject *obj = va_arg(count, PyObject *); |
| 2420 | PyObject *str; |
| 2421 | assert(obj); |
| 2422 | str = PyObject_Str(obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2423 | if (!str || PyUnicode_READY(str) == -1) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2424 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2425 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2426 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2427 | n += PyUnicode_GET_LENGTH(str); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2428 | /* Remember the str and switch to the next slot */ |
| 2429 | *callresult++ = str; |
| 2430 | break; |
| 2431 | } |
| 2432 | case 'R': |
| 2433 | { |
| 2434 | PyObject *obj = va_arg(count, PyObject *); |
| 2435 | PyObject *repr; |
| 2436 | assert(obj); |
| 2437 | repr = PyObject_Repr(obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2438 | if (!repr || PyUnicode_READY(repr) == -1) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2439 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2440 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(repr); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2441 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2442 | n += PyUnicode_GET_LENGTH(repr); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2443 | /* Remember the repr and switch to the next slot */ |
| 2444 | *callresult++ = repr; |
| 2445 | break; |
| 2446 | } |
| 2447 | case 'A': |
| 2448 | { |
| 2449 | PyObject *obj = va_arg(count, PyObject *); |
| 2450 | PyObject *ascii; |
| 2451 | assert(obj); |
| 2452 | ascii = PyObject_ASCII(obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2453 | if (!ascii || PyUnicode_READY(ascii) == -1) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2454 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2455 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(ascii); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2456 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2457 | n += PyUnicode_GET_LENGTH(ascii); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2458 | /* Remember the repr and switch to the next slot */ |
| 2459 | *callresult++ = ascii; |
| 2460 | break; |
| 2461 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2462 | default: |
| 2463 | /* if we stumble upon an unknown |
| 2464 | formatting code, copy the rest of |
| 2465 | the format string to the output |
| 2466 | string. (we cannot just skip the |
| 2467 | code, since there's no way to know |
| 2468 | what's in the argument list) */ |
| 2469 | n += strlen(p); |
| 2470 | goto expand; |
| 2471 | } |
| 2472 | } else |
| 2473 | n++; |
| 2474 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2475 | expand: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2476 | /* step 4: fill the buffer */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2477 | /* Since we've analyzed how much space we need, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2478 | we don't have to resize the string. |
| 2479 | There can be no errors beyond this point. */ |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2480 | string = PyUnicode_New(n, maxchar); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2481 | if (!string) |
| 2482 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2483 | kind = PyUnicode_KIND(string); |
| 2484 | data = PyUnicode_DATA(string); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2485 | callresult = callresults; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2486 | numberresult = numberresults; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2487 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2488 | for (i = 0, f = format; *f; f++) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2489 | if (*f == '%') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2490 | const char* p; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2491 | |
| 2492 | p = f; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2493 | f = parse_format_flags(f, NULL, NULL, NULL, NULL, NULL); |
| 2494 | /* checking for == because the last argument could be a empty |
| 2495 | string, which causes i to point to end, the assert at the end of |
| 2496 | the loop */ |
| 2497 | assert(i <= PyUnicode_GET_LENGTH(string)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2498 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2499 | switch (*f) { |
| 2500 | case 'c': |
Victor Stinner | 5ed8b2c | 2011-02-21 21:13:44 +0000 | [diff] [blame] | 2501 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2502 | const int ordinal = va_arg(vargs, int); |
| 2503 | PyUnicode_WRITE(kind, data, i++, ordinal); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2504 | break; |
Victor Stinner | 5ed8b2c | 2011-02-21 21:13:44 +0000 | [diff] [blame] | 2505 | } |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2506 | case 'i': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2507 | case 'd': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2508 | case 'u': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2509 | case 'x': |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2510 | case 'p': |
| 2511 | /* unused, since we already have the result */ |
| 2512 | if (*f == 'p') |
| 2513 | (void) va_arg(vargs, void *); |
| 2514 | else |
| 2515 | (void) va_arg(vargs, int); |
| 2516 | /* extract the result from numberresults and append. */ |
| 2517 | for (; *numberresult; ++i, ++numberresult) |
| 2518 | PyUnicode_WRITE(kind, data, i, *numberresult); |
| 2519 | /* skip over the separating '\0' */ |
| 2520 | assert(*numberresult == '\0'); |
| 2521 | numberresult++; |
| 2522 | assert(numberresult <= numberresults + numbersize); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2523 | break; |
| 2524 | case 's': |
| 2525 | { |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2526 | /* unused, since we already have the result */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2527 | Py_ssize_t size; |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2528 | (void) va_arg(vargs, char *); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2529 | size = PyUnicode_GET_LENGTH(*callresult); |
| 2530 | assert(PyUnicode_KIND(*callresult) <= PyUnicode_KIND(string)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2531 | copy_characters(string, i, *callresult, 0, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2532 | i += size; |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2533 | /* We're done with the unicode()/repr() => forget it */ |
| 2534 | Py_DECREF(*callresult); |
| 2535 | /* switch to next unicode()/repr() result */ |
| 2536 | ++callresult; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2537 | break; |
| 2538 | } |
| 2539 | case 'U': |
| 2540 | { |
| 2541 | PyObject *obj = va_arg(vargs, PyObject *); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2542 | Py_ssize_t size; |
| 2543 | assert(PyUnicode_KIND(obj) <= PyUnicode_KIND(string)); |
| 2544 | size = PyUnicode_GET_LENGTH(obj); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2545 | copy_characters(string, i, obj, 0, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2546 | i += size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2547 | break; |
| 2548 | } |
| 2549 | case 'V': |
| 2550 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2551 | Py_ssize_t size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2552 | PyObject *obj = va_arg(vargs, PyObject *); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2553 | va_arg(vargs, const char *); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2554 | if (obj) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2555 | size = PyUnicode_GET_LENGTH(obj); |
| 2556 | assert(PyUnicode_KIND(obj) <= PyUnicode_KIND(string)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2557 | copy_characters(string, i, obj, 0, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2558 | i += size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2559 | } else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2560 | size = PyUnicode_GET_LENGTH(*callresult); |
| 2561 | assert(PyUnicode_KIND(*callresult) <= |
| 2562 | PyUnicode_KIND(string)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2563 | copy_characters(string, i, *callresult, 0, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2564 | i += size; |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2565 | Py_DECREF(*callresult); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2566 | } |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2567 | ++callresult; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2568 | break; |
| 2569 | } |
| 2570 | case 'S': |
| 2571 | case 'R': |
Victor Stinner | 9a90900 | 2010-10-18 20:59:24 +0000 | [diff] [blame] | 2572 | case 'A': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2573 | { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2574 | Py_ssize_t size = PyUnicode_GET_LENGTH(*callresult); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2575 | /* unused, since we already have the result */ |
| 2576 | (void) va_arg(vargs, PyObject *); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2577 | assert(PyUnicode_KIND(*callresult) <= PyUnicode_KIND(string)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2578 | copy_characters(string, i, *callresult, 0, size); |
| 2579 | i += size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2580 | /* We're done with the unicode()/repr() => forget it */ |
| 2581 | Py_DECREF(*callresult); |
| 2582 | /* switch to next unicode()/repr() result */ |
| 2583 | ++callresult; |
| 2584 | break; |
| 2585 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2586 | case '%': |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2587 | PyUnicode_WRITE(kind, data, i++, '%'); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2588 | break; |
| 2589 | default: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2590 | for (; *p; ++p, ++i) |
| 2591 | PyUnicode_WRITE(kind, data, i, *p); |
| 2592 | assert(i == PyUnicode_GET_LENGTH(string)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2593 | goto end; |
| 2594 | } |
Victor Stinner | 1205f27 | 2010-09-11 00:54:47 +0000 | [diff] [blame] | 2595 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2596 | else { |
| 2597 | assert(i < PyUnicode_GET_LENGTH(string)); |
| 2598 | PyUnicode_WRITE(kind, data, i++, *f); |
| 2599 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2600 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2601 | assert(i == PyUnicode_GET_LENGTH(string)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2602 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2603 | end: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2604 | if (callresults) |
| 2605 | PyObject_Free(callresults); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2606 | if (numberresults) |
| 2607 | PyObject_Free(numberresults); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2608 | assert(_PyUnicode_CheckConsistency(string, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 2609 | return string; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2610 | fail: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2611 | if (callresults) { |
| 2612 | PyObject **callresult2 = callresults; |
| 2613 | while (callresult2 < callresult) { |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2614 | Py_XDECREF(*callresult2); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2615 | ++callresult2; |
| 2616 | } |
| 2617 | PyObject_Free(callresults); |
| 2618 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2619 | if (numberresults) |
| 2620 | PyObject_Free(numberresults); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2621 | return NULL; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2622 | } |
| 2623 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2624 | PyObject * |
| 2625 | PyUnicode_FromFormat(const char *format, ...) |
| 2626 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2627 | PyObject* ret; |
| 2628 | va_list vargs; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2629 | |
| 2630 | #ifdef HAVE_STDARG_PROTOTYPES |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2631 | va_start(vargs, format); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2632 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2633 | va_start(vargs); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2634 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2635 | ret = PyUnicode_FromFormatV(format, vargs); |
| 2636 | va_end(vargs); |
| 2637 | return ret; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2638 | } |
| 2639 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2640 | #ifdef HAVE_WCHAR_H |
| 2641 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2642 | /* Helper function for PyUnicode_AsWideChar() and PyUnicode_AsWideCharString(): |
| 2643 | convert a Unicode object to a wide character string. |
| 2644 | |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2645 | - 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] | 2646 | character) required to convert the unicode object. Ignore size argument. |
| 2647 | |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2648 | - Otherwise: return the number of wide characters (excluding the null |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2649 | character) written into w. Write at most size wide characters (including |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2650 | the null character). */ |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2651 | static Py_ssize_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2652 | unicode_aswidechar(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2653 | wchar_t *w, |
| 2654 | Py_ssize_t size) |
| 2655 | { |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2656 | Py_ssize_t res; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2657 | const wchar_t *wstr; |
| 2658 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2659 | wstr = PyUnicode_AsUnicodeAndSize(unicode, &res); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2660 | if (wstr == NULL) |
| 2661 | return -1; |
| 2662 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2663 | if (w != NULL) { |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2664 | if (size > res) |
| 2665 | size = res + 1; |
| 2666 | else |
| 2667 | res = size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2668 | Py_MEMCPY(w, wstr, size * sizeof(wchar_t)); |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2669 | return res; |
| 2670 | } |
| 2671 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2672 | return res + 1; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2673 | } |
| 2674 | |
| 2675 | Py_ssize_t |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2676 | PyUnicode_AsWideChar(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2677 | wchar_t *w, |
| 2678 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2679 | { |
| 2680 | if (unicode == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2681 | PyErr_BadInternalCall(); |
| 2682 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2683 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2684 | return unicode_aswidechar(unicode, w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2685 | } |
| 2686 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2687 | wchar_t* |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 2688 | PyUnicode_AsWideCharString(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2689 | Py_ssize_t *size) |
| 2690 | { |
| 2691 | wchar_t* buffer; |
| 2692 | Py_ssize_t buflen; |
| 2693 | |
| 2694 | if (unicode == NULL) { |
| 2695 | PyErr_BadInternalCall(); |
| 2696 | return NULL; |
| 2697 | } |
| 2698 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2699 | buflen = unicode_aswidechar(unicode, NULL, 0); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2700 | if (buflen == -1) |
| 2701 | return NULL; |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2702 | if (PY_SSIZE_T_MAX / sizeof(wchar_t) < buflen) { |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2703 | PyErr_NoMemory(); |
| 2704 | return NULL; |
| 2705 | } |
| 2706 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2707 | buffer = PyMem_MALLOC(buflen * sizeof(wchar_t)); |
| 2708 | if (buffer == NULL) { |
| 2709 | PyErr_NoMemory(); |
| 2710 | return NULL; |
| 2711 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2712 | buflen = unicode_aswidechar(unicode, buffer, buflen); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2713 | if (buflen == -1) |
| 2714 | return NULL; |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2715 | if (size != NULL) |
| 2716 | *size = buflen; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2717 | return buffer; |
| 2718 | } |
| 2719 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2720 | #endif /* HAVE_WCHAR_H */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2721 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2722 | PyObject * |
| 2723 | PyUnicode_FromOrdinal(int ordinal) |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2724 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2725 | PyObject *v; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2726 | if (ordinal < 0 || ordinal > 0x10ffff) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2727 | PyErr_SetString(PyExc_ValueError, |
| 2728 | "chr() arg not in range(0x110000)"); |
| 2729 | return NULL; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2730 | } |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 2731 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2732 | if (ordinal < 256) |
| 2733 | return get_latin1_char(ordinal); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2734 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2735 | v = PyUnicode_New(1, ordinal); |
| 2736 | if (v == NULL) |
| 2737 | return NULL; |
| 2738 | PyUnicode_WRITE(PyUnicode_KIND(v), PyUnicode_DATA(v), 0, ordinal); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2739 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2740 | return v; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2741 | } |
| 2742 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2743 | PyObject * |
| 2744 | PyUnicode_FromObject(register PyObject *obj) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2745 | { |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2746 | /* XXX Perhaps we should make this API an alias of |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2747 | PyObject_Str() instead ?! */ |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2748 | if (PyUnicode_CheckExact(obj)) { |
Victor Stinner | d3a83d5 | 2011-10-01 03:09:33 +0200 | [diff] [blame] | 2749 | if (PyUnicode_READY(obj)) |
| 2750 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2751 | Py_INCREF(obj); |
| 2752 | return obj; |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2753 | } |
| 2754 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2755 | /* For a Unicode subtype that's not a Unicode object, |
| 2756 | return a true Unicode object with the same data. */ |
Victor Stinner | 2219e0a | 2011-10-01 01:16:59 +0200 | [diff] [blame] | 2757 | return PyUnicode_Copy(obj); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2758 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2759 | PyErr_Format(PyExc_TypeError, |
| 2760 | "Can't convert '%.100s' object to str implicitly", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2761 | Py_TYPE(obj)->tp_name); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2762 | return NULL; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2763 | } |
| 2764 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2765 | PyObject * |
| 2766 | PyUnicode_FromEncodedObject(register PyObject *obj, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2767 | const char *encoding, |
| 2768 | const char *errors) |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2769 | { |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2770 | Py_buffer buffer; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2771 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2772 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2773 | if (obj == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2774 | PyErr_BadInternalCall(); |
| 2775 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2776 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2777 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2778 | /* Decoding bytes objects is the most common case and should be fast */ |
| 2779 | if (PyBytes_Check(obj)) { |
| 2780 | if (PyBytes_GET_SIZE(obj) == 0) { |
| 2781 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 2782 | v = unicode_empty; |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2783 | } |
| 2784 | else { |
| 2785 | v = PyUnicode_Decode( |
| 2786 | PyBytes_AS_STRING(obj), PyBytes_GET_SIZE(obj), |
| 2787 | encoding, errors); |
| 2788 | } |
| 2789 | return v; |
| 2790 | } |
| 2791 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2792 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2793 | PyErr_SetString(PyExc_TypeError, |
| 2794 | "decoding str is not supported"); |
| 2795 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2796 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2797 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2798 | /* Retrieve a bytes buffer view through the PEP 3118 buffer interface */ |
| 2799 | if (PyObject_GetBuffer(obj, &buffer, PyBUF_SIMPLE) < 0) { |
| 2800 | PyErr_Format(PyExc_TypeError, |
| 2801 | "coercing to str: need bytes, bytearray " |
| 2802 | "or buffer-like object, %.80s found", |
| 2803 | Py_TYPE(obj)->tp_name); |
| 2804 | return NULL; |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 2805 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2806 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2807 | if (buffer.len == 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2808 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 2809 | v = unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2810 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2811 | else |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2812 | v = PyUnicode_Decode((char*) buffer.buf, buffer.len, encoding, errors); |
Marc-André Lemburg | ad7c98e | 2001-01-17 17:09:53 +0000 | [diff] [blame] | 2813 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2814 | PyBuffer_Release(&buffer); |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2815 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2816 | } |
| 2817 | |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2818 | /* Convert encoding to lower case and replace '_' with '-' in order to |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2819 | catch e.g. UTF_8. Return 0 on error (encoding is longer than lower_len-1), |
| 2820 | 1 on success. */ |
| 2821 | static int |
| 2822 | normalize_encoding(const char *encoding, |
| 2823 | char *lower, |
| 2824 | size_t lower_len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2825 | { |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2826 | const char *e; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2827 | char *l; |
| 2828 | char *l_end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2829 | |
Benjamin Peterson | 7a6debe | 2011-10-15 09:25:28 -0400 | [diff] [blame] | 2830 | if (encoding == NULL) { |
| 2831 | strcpy(lower, "utf-8"); |
| 2832 | return 1; |
| 2833 | } |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2834 | e = encoding; |
| 2835 | l = lower; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2836 | l_end = &lower[lower_len - 1]; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2837 | while (*e) { |
| 2838 | if (l == l_end) |
| 2839 | return 0; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 2840 | if (Py_ISUPPER(*e)) { |
| 2841 | *l++ = Py_TOLOWER(*e++); |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2842 | } |
| 2843 | else if (*e == '_') { |
| 2844 | *l++ = '-'; |
| 2845 | e++; |
| 2846 | } |
| 2847 | else { |
| 2848 | *l++ = *e++; |
| 2849 | } |
| 2850 | } |
| 2851 | *l = '\0'; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2852 | return 1; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2853 | } |
| 2854 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2855 | PyObject * |
| 2856 | PyUnicode_Decode(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2857 | Py_ssize_t size, |
| 2858 | const char *encoding, |
| 2859 | const char *errors) |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2860 | { |
| 2861 | PyObject *buffer = NULL, *unicode; |
| 2862 | Py_buffer info; |
| 2863 | char lower[11]; /* Enough for any encoding shortcut */ |
| 2864 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 2865 | /* Shortcuts for common default encodings */ |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2866 | if (normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2867 | if ((strcmp(lower, "utf-8") == 0) || |
| 2868 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2869 | return PyUnicode_DecodeUTF8(s, size, errors); |
| 2870 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2871 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2872 | (strcmp(lower, "iso-8859-1") == 0)) |
| 2873 | return PyUnicode_DecodeLatin1(s, size, errors); |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 2874 | #ifdef HAVE_MBCS |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2875 | else if (strcmp(lower, "mbcs") == 0) |
| 2876 | return PyUnicode_DecodeMBCS(s, size, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 2877 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2878 | else if (strcmp(lower, "ascii") == 0) |
| 2879 | return PyUnicode_DecodeASCII(s, size, errors); |
| 2880 | else if (strcmp(lower, "utf-16") == 0) |
| 2881 | return PyUnicode_DecodeUTF16(s, size, errors, 0); |
| 2882 | else if (strcmp(lower, "utf-32") == 0) |
| 2883 | return PyUnicode_DecodeUTF32(s, size, errors, 0); |
| 2884 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2885 | |
| 2886 | /* Decode via the codec registry */ |
Guido van Rossum | be801ac | 2007-10-08 03:32:34 +0000 | [diff] [blame] | 2887 | buffer = NULL; |
Antoine Pitrou | c3b3924 | 2009-01-03 16:59:18 +0000 | [diff] [blame] | 2888 | 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] | 2889 | goto onError; |
Antoine Pitrou | ee58fa4 | 2008-08-19 18:22:14 +0000 | [diff] [blame] | 2890 | buffer = PyMemoryView_FromBuffer(&info); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2891 | if (buffer == NULL) |
| 2892 | goto onError; |
| 2893 | unicode = PyCodec_Decode(buffer, encoding, errors); |
| 2894 | if (unicode == NULL) |
| 2895 | goto onError; |
| 2896 | if (!PyUnicode_Check(unicode)) { |
| 2897 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 2898 | "decoder did not return a str object (type=%.400s)", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2899 | Py_TYPE(unicode)->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2900 | Py_DECREF(unicode); |
| 2901 | goto onError; |
| 2902 | } |
| 2903 | Py_DECREF(buffer); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 2904 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 2905 | if (_PyUnicode_READY_REPLACE(&unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2906 | Py_DECREF(unicode); |
| 2907 | return NULL; |
| 2908 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 2909 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2910 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2911 | return unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2912 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2913 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2914 | Py_XDECREF(buffer); |
| 2915 | return NULL; |
| 2916 | } |
| 2917 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2918 | PyObject * |
| 2919 | PyUnicode_AsDecodedObject(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2920 | const char *encoding, |
| 2921 | const char *errors) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2922 | { |
| 2923 | PyObject *v; |
| 2924 | |
| 2925 | if (!PyUnicode_Check(unicode)) { |
| 2926 | PyErr_BadArgument(); |
| 2927 | goto onError; |
| 2928 | } |
| 2929 | |
| 2930 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2931 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2932 | |
| 2933 | /* Decode via the codec registry */ |
| 2934 | v = PyCodec_Decode(unicode, encoding, errors); |
| 2935 | if (v == NULL) |
| 2936 | goto onError; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2937 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2938 | return v; |
| 2939 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2940 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2941 | return NULL; |
| 2942 | } |
| 2943 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2944 | PyObject * |
| 2945 | PyUnicode_AsDecodedUnicode(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2946 | const char *encoding, |
| 2947 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2948 | { |
| 2949 | PyObject *v; |
| 2950 | |
| 2951 | if (!PyUnicode_Check(unicode)) { |
| 2952 | PyErr_BadArgument(); |
| 2953 | goto onError; |
| 2954 | } |
| 2955 | |
| 2956 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2957 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2958 | |
| 2959 | /* Decode via the codec registry */ |
| 2960 | v = PyCodec_Decode(unicode, encoding, errors); |
| 2961 | if (v == NULL) |
| 2962 | goto onError; |
| 2963 | if (!PyUnicode_Check(v)) { |
| 2964 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 2965 | "decoder did not return a str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2966 | Py_TYPE(v)->tp_name); |
| 2967 | Py_DECREF(v); |
| 2968 | goto onError; |
| 2969 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2970 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2971 | return v; |
| 2972 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2973 | onError: |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2974 | return NULL; |
| 2975 | } |
| 2976 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2977 | PyObject * |
| 2978 | PyUnicode_Encode(const Py_UNICODE *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2979 | Py_ssize_t size, |
| 2980 | const char *encoding, |
| 2981 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2982 | { |
| 2983 | PyObject *v, *unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2984 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2985 | unicode = PyUnicode_FromUnicode(s, size); |
| 2986 | if (unicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2987 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2988 | v = PyUnicode_AsEncodedString(unicode, encoding, errors); |
| 2989 | Py_DECREF(unicode); |
| 2990 | return v; |
| 2991 | } |
| 2992 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2993 | PyObject * |
| 2994 | PyUnicode_AsEncodedObject(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2995 | const char *encoding, |
| 2996 | const char *errors) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2997 | { |
| 2998 | PyObject *v; |
| 2999 | |
| 3000 | if (!PyUnicode_Check(unicode)) { |
| 3001 | PyErr_BadArgument(); |
| 3002 | goto onError; |
| 3003 | } |
| 3004 | |
| 3005 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3006 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3007 | |
| 3008 | /* Encode via the codec registry */ |
| 3009 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3010 | if (v == NULL) |
| 3011 | goto onError; |
| 3012 | return v; |
| 3013 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3014 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3015 | return NULL; |
| 3016 | } |
| 3017 | |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3018 | PyObject * |
| 3019 | PyUnicode_EncodeFSDefault(PyObject *unicode) |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3020 | { |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3021 | #ifdef HAVE_MBCS |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3022 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
| 3023 | PyUnicode_GET_SIZE(unicode), |
| 3024 | NULL); |
| 3025 | #elif defined(__APPLE__) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3026 | return _PyUnicode_AsUTF8String(unicode, "surrogateescape"); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3027 | #else |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 3028 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 3029 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 3030 | cannot use it to encode and decode filenames before it is loaded. Load |
| 3031 | the Python codec requires to encode at least its own filename. Use the C |
| 3032 | version of the locale codec until the codec registry is initialized and |
| 3033 | the Python codec is loaded. |
| 3034 | |
| 3035 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 3036 | cannot only rely on it: check also interp->fscodec_initialized for |
| 3037 | subinterpreters. */ |
| 3038 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3039 | return PyUnicode_AsEncodedString(unicode, |
| 3040 | Py_FileSystemDefaultEncoding, |
| 3041 | "surrogateescape"); |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 3042 | } |
| 3043 | else { |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3044 | /* locale encoding with surrogateescape */ |
| 3045 | wchar_t *wchar; |
| 3046 | char *bytes; |
| 3047 | PyObject *bytes_obj; |
Victor Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 3048 | size_t error_pos; |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3049 | |
| 3050 | wchar = PyUnicode_AsWideCharString(unicode, NULL); |
| 3051 | if (wchar == NULL) |
| 3052 | return NULL; |
Victor Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 3053 | bytes = _Py_wchar2char(wchar, &error_pos); |
| 3054 | if (bytes == NULL) { |
| 3055 | if (error_pos != (size_t)-1) { |
| 3056 | char *errmsg = strerror(errno); |
| 3057 | PyObject *exc = NULL; |
| 3058 | if (errmsg == NULL) |
| 3059 | errmsg = "Py_wchar2char() failed"; |
| 3060 | raise_encode_exception(&exc, |
| 3061 | "filesystemencoding", |
| 3062 | PyUnicode_AS_UNICODE(unicode), PyUnicode_GET_SIZE(unicode), |
| 3063 | error_pos, error_pos+1, |
| 3064 | errmsg); |
| 3065 | Py_XDECREF(exc); |
| 3066 | } |
| 3067 | else |
| 3068 | PyErr_NoMemory(); |
| 3069 | PyMem_Free(wchar); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3070 | return NULL; |
Victor Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 3071 | } |
| 3072 | PyMem_Free(wchar); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3073 | |
| 3074 | bytes_obj = PyBytes_FromString(bytes); |
| 3075 | PyMem_Free(bytes); |
| 3076 | return bytes_obj; |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 3077 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3078 | #endif |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3079 | } |
| 3080 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3081 | PyObject * |
| 3082 | PyUnicode_AsEncodedString(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3083 | const char *encoding, |
| 3084 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3085 | { |
| 3086 | PyObject *v; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3087 | char lower[11]; /* Enough for any encoding shortcut */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3088 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3089 | if (!PyUnicode_Check(unicode)) { |
| 3090 | PyErr_BadArgument(); |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3091 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3092 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3093 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3094 | /* Shortcuts for common default encodings */ |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3095 | if (normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3096 | if ((strcmp(lower, "utf-8") == 0) || |
| 3097 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 3098 | { |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3099 | if (errors == NULL || strcmp(errors, "strict") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3100 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3101 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3102 | return _PyUnicode_AsUTF8String(unicode, errors); |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 3103 | } |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3104 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3105 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3106 | (strcmp(lower, "iso-8859-1") == 0)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3107 | return _PyUnicode_AsLatin1String(unicode, errors); |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3108 | #ifdef HAVE_MBCS |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3109 | else if (strcmp(lower, "mbcs") == 0) |
| 3110 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
| 3111 | PyUnicode_GET_SIZE(unicode), |
| 3112 | errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 3113 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3114 | else if (strcmp(lower, "ascii") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3115 | return _PyUnicode_AsASCIIString(unicode, errors); |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3116 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3117 | |
| 3118 | /* Encode via the codec registry */ |
| 3119 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3120 | if (v == NULL) |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3121 | return NULL; |
| 3122 | |
| 3123 | /* The normal path */ |
| 3124 | if (PyBytes_Check(v)) |
| 3125 | return v; |
| 3126 | |
| 3127 | /* 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] | 3128 | if (PyByteArray_Check(v)) { |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 3129 | int error; |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3130 | PyObject *b; |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 3131 | |
| 3132 | error = PyErr_WarnFormat(PyExc_RuntimeWarning, 1, |
| 3133 | "encoder %s returned bytearray instead of bytes", |
| 3134 | encoding); |
| 3135 | if (error) { |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3136 | Py_DECREF(v); |
| 3137 | return NULL; |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3138 | } |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3139 | |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3140 | b = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(v), Py_SIZE(v)); |
| 3141 | Py_DECREF(v); |
| 3142 | return b; |
| 3143 | } |
| 3144 | |
| 3145 | PyErr_Format(PyExc_TypeError, |
| 3146 | "encoder did not return a bytes object (type=%.400s)", |
| 3147 | Py_TYPE(v)->tp_name); |
| 3148 | Py_DECREF(v); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3149 | return NULL; |
| 3150 | } |
| 3151 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3152 | PyObject * |
| 3153 | PyUnicode_AsEncodedUnicode(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3154 | const char *encoding, |
| 3155 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3156 | { |
| 3157 | PyObject *v; |
| 3158 | |
| 3159 | if (!PyUnicode_Check(unicode)) { |
| 3160 | PyErr_BadArgument(); |
| 3161 | goto onError; |
| 3162 | } |
| 3163 | |
| 3164 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3165 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3166 | |
| 3167 | /* Encode via the codec registry */ |
| 3168 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3169 | if (v == NULL) |
| 3170 | goto onError; |
| 3171 | if (!PyUnicode_Check(v)) { |
| 3172 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3173 | "encoder did not return an str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3174 | Py_TYPE(v)->tp_name); |
| 3175 | Py_DECREF(v); |
| 3176 | goto onError; |
| 3177 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3178 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3179 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3180 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3181 | return NULL; |
| 3182 | } |
| 3183 | |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3184 | PyObject* |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3185 | PyUnicode_DecodeFSDefault(const char *s) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3186 | Py_ssize_t size = (Py_ssize_t)strlen(s); |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3187 | return PyUnicode_DecodeFSDefaultAndSize(s, size); |
| 3188 | } |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3189 | |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3190 | PyObject* |
| 3191 | PyUnicode_DecodeFSDefaultAndSize(const char *s, Py_ssize_t size) |
| 3192 | { |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3193 | #ifdef HAVE_MBCS |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3194 | return PyUnicode_DecodeMBCS(s, size, NULL); |
| 3195 | #elif defined(__APPLE__) |
| 3196 | return PyUnicode_DecodeUTF8(s, size, "surrogateescape"); |
| 3197 | #else |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 3198 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 3199 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 3200 | cannot use it to encode and decode filenames before it is loaded. Load |
| 3201 | the Python codec requires to encode at least its own filename. Use the C |
| 3202 | version of the locale codec until the codec registry is initialized and |
| 3203 | the Python codec is loaded. |
| 3204 | |
| 3205 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 3206 | cannot only rely on it: check also interp->fscodec_initialized for |
| 3207 | subinterpreters. */ |
| 3208 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3209 | return PyUnicode_Decode(s, size, |
| 3210 | Py_FileSystemDefaultEncoding, |
Victor Stinner | b9a20ad | 2010-04-30 16:37:52 +0000 | [diff] [blame] | 3211 | "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3212 | } |
| 3213 | else { |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3214 | /* locale encoding with surrogateescape */ |
| 3215 | wchar_t *wchar; |
| 3216 | PyObject *unicode; |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 3217 | size_t len; |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3218 | |
| 3219 | if (s[size] != '\0' || size != strlen(s)) { |
| 3220 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
| 3221 | return NULL; |
| 3222 | } |
| 3223 | |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 3224 | wchar = _Py_char2wchar(s, &len); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3225 | if (wchar == NULL) |
Victor Stinner | d5af0a5 | 2010-11-08 23:34:29 +0000 | [diff] [blame] | 3226 | return PyErr_NoMemory(); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3227 | |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 3228 | unicode = PyUnicode_FromWideChar(wchar, len); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3229 | PyMem_Free(wchar); |
| 3230 | return unicode; |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3231 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3232 | #endif |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3233 | } |
| 3234 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3235 | |
| 3236 | int |
| 3237 | PyUnicode_FSConverter(PyObject* arg, void* addr) |
| 3238 | { |
| 3239 | PyObject *output = NULL; |
| 3240 | Py_ssize_t size; |
| 3241 | void *data; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3242 | if (arg == NULL) { |
| 3243 | Py_DECREF(*(PyObject**)addr); |
| 3244 | return 1; |
| 3245 | } |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 3246 | if (PyBytes_Check(arg)) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3247 | output = arg; |
| 3248 | Py_INCREF(output); |
| 3249 | } |
| 3250 | else { |
| 3251 | arg = PyUnicode_FromObject(arg); |
| 3252 | if (!arg) |
| 3253 | return 0; |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3254 | output = PyUnicode_EncodeFSDefault(arg); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3255 | Py_DECREF(arg); |
| 3256 | if (!output) |
| 3257 | return 0; |
| 3258 | if (!PyBytes_Check(output)) { |
| 3259 | Py_DECREF(output); |
| 3260 | PyErr_SetString(PyExc_TypeError, "encoder failed to return bytes"); |
| 3261 | return 0; |
| 3262 | } |
| 3263 | } |
Victor Stinner | 0ea2a46 | 2010-04-30 00:22:08 +0000 | [diff] [blame] | 3264 | size = PyBytes_GET_SIZE(output); |
| 3265 | data = PyBytes_AS_STRING(output); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3266 | if (size != strlen(data)) { |
Benjamin Peterson | 7a6b44a | 2011-08-18 13:51:47 -0500 | [diff] [blame] | 3267 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3268 | Py_DECREF(output); |
| 3269 | return 0; |
| 3270 | } |
| 3271 | *(PyObject**)addr = output; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3272 | return Py_CLEANUP_SUPPORTED; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3273 | } |
| 3274 | |
| 3275 | |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3276 | int |
| 3277 | PyUnicode_FSDecoder(PyObject* arg, void* addr) |
| 3278 | { |
| 3279 | PyObject *output = NULL; |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3280 | if (arg == NULL) { |
| 3281 | Py_DECREF(*(PyObject**)addr); |
| 3282 | return 1; |
| 3283 | } |
| 3284 | if (PyUnicode_Check(arg)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3285 | if (PyUnicode_READY(arg)) |
| 3286 | return 0; |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3287 | output = arg; |
| 3288 | Py_INCREF(output); |
| 3289 | } |
| 3290 | else { |
| 3291 | arg = PyBytes_FromObject(arg); |
| 3292 | if (!arg) |
| 3293 | return 0; |
| 3294 | output = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AS_STRING(arg), |
| 3295 | PyBytes_GET_SIZE(arg)); |
| 3296 | Py_DECREF(arg); |
| 3297 | if (!output) |
| 3298 | return 0; |
| 3299 | if (!PyUnicode_Check(output)) { |
| 3300 | Py_DECREF(output); |
| 3301 | PyErr_SetString(PyExc_TypeError, "decoder failed to return unicode"); |
| 3302 | return 0; |
| 3303 | } |
| 3304 | } |
Victor Stinner | 065836e | 2011-10-27 01:56:33 +0200 | [diff] [blame] | 3305 | if (PyUnicode_READY(output) < 0) { |
| 3306 | Py_DECREF(output); |
| 3307 | return 0; |
| 3308 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3309 | if (findchar(PyUnicode_DATA(output), PyUnicode_KIND(output), |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 3310 | PyUnicode_GET_LENGTH(output), 0, 1) >= 0) { |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3311 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
| 3312 | Py_DECREF(output); |
| 3313 | return 0; |
| 3314 | } |
| 3315 | *(PyObject**)addr = output; |
| 3316 | return Py_CLEANUP_SUPPORTED; |
| 3317 | } |
| 3318 | |
| 3319 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3320 | char* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3321 | PyUnicode_AsUTF8AndSize(PyObject *unicode, Py_ssize_t *psize) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3322 | { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 3323 | PyObject *bytes; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3324 | |
Neal Norwitz | e0a0a6e | 2007-08-25 01:04:21 +0000 | [diff] [blame] | 3325 | if (!PyUnicode_Check(unicode)) { |
| 3326 | PyErr_BadArgument(); |
| 3327 | return NULL; |
| 3328 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3329 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3330 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3331 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3332 | if (PyUnicode_UTF8(unicode) == NULL) { |
| 3333 | assert(!PyUnicode_IS_COMPACT_ASCII(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3334 | bytes = _PyUnicode_AsUTF8String(unicode, "strict"); |
| 3335 | if (bytes == NULL) |
| 3336 | return NULL; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3337 | _PyUnicode_UTF8(unicode) = PyObject_MALLOC(PyBytes_GET_SIZE(bytes) + 1); |
| 3338 | if (_PyUnicode_UTF8(unicode) == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3339 | Py_DECREF(bytes); |
| 3340 | return NULL; |
| 3341 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3342 | _PyUnicode_UTF8_LENGTH(unicode) = PyBytes_GET_SIZE(bytes); |
| 3343 | Py_MEMCPY(_PyUnicode_UTF8(unicode), |
| 3344 | PyBytes_AS_STRING(bytes), |
| 3345 | _PyUnicode_UTF8_LENGTH(unicode) + 1); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3346 | Py_DECREF(bytes); |
| 3347 | } |
| 3348 | |
| 3349 | if (psize) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3350 | *psize = PyUnicode_UTF8_LENGTH(unicode); |
| 3351 | return PyUnicode_UTF8(unicode); |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 3352 | } |
| 3353 | |
| 3354 | char* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3355 | PyUnicode_AsUTF8(PyObject *unicode) |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 3356 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3357 | return PyUnicode_AsUTF8AndSize(unicode, NULL); |
| 3358 | } |
| 3359 | |
| 3360 | #ifdef Py_DEBUG |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 3361 | static int unicode_as_unicode_calls = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3362 | #endif |
| 3363 | |
| 3364 | |
| 3365 | Py_UNICODE * |
| 3366 | PyUnicode_AsUnicodeAndSize(PyObject *unicode, Py_ssize_t *size) |
| 3367 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3368 | const unsigned char *one_byte; |
| 3369 | #if SIZEOF_WCHAR_T == 4 |
| 3370 | const Py_UCS2 *two_bytes; |
| 3371 | #else |
| 3372 | const Py_UCS4 *four_bytes; |
| 3373 | const Py_UCS4 *ucs4_end; |
| 3374 | Py_ssize_t num_surrogates; |
| 3375 | #endif |
| 3376 | wchar_t *w; |
| 3377 | wchar_t *wchar_end; |
| 3378 | |
| 3379 | if (!PyUnicode_Check(unicode)) { |
| 3380 | PyErr_BadArgument(); |
| 3381 | return NULL; |
| 3382 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3383 | if (_PyUnicode_WSTR(unicode) == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3384 | /* Non-ASCII compact unicode object */ |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3385 | assert(_PyUnicode_KIND(unicode) != 0); |
| 3386 | assert(PyUnicode_IS_READY(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3387 | |
| 3388 | #ifdef Py_DEBUG |
| 3389 | ++unicode_as_unicode_calls; |
| 3390 | #endif |
| 3391 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3392 | if (PyUnicode_KIND(unicode) == PyUnicode_4BYTE_KIND) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3393 | #if SIZEOF_WCHAR_T == 2 |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3394 | four_bytes = PyUnicode_4BYTE_DATA(unicode); |
| 3395 | ucs4_end = four_bytes + _PyUnicode_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3396 | num_surrogates = 0; |
| 3397 | |
| 3398 | for (; four_bytes < ucs4_end; ++four_bytes) { |
| 3399 | if (*four_bytes > 0xFFFF) |
| 3400 | ++num_surrogates; |
| 3401 | } |
| 3402 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3403 | _PyUnicode_WSTR(unicode) = (wchar_t *) PyObject_MALLOC( |
| 3404 | sizeof(wchar_t) * (_PyUnicode_LENGTH(unicode) + 1 + num_surrogates)); |
| 3405 | if (!_PyUnicode_WSTR(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3406 | PyErr_NoMemory(); |
| 3407 | return NULL; |
| 3408 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3409 | _PyUnicode_WSTR_LENGTH(unicode) = _PyUnicode_LENGTH(unicode) + num_surrogates; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3410 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3411 | w = _PyUnicode_WSTR(unicode); |
| 3412 | wchar_end = w + _PyUnicode_WSTR_LENGTH(unicode); |
| 3413 | four_bytes = PyUnicode_4BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3414 | for (; four_bytes < ucs4_end; ++four_bytes, ++w) { |
| 3415 | if (*four_bytes > 0xFFFF) { |
| 3416 | /* encode surrogate pair in this case */ |
| 3417 | *w++ = 0xD800 | ((*four_bytes - 0x10000) >> 10); |
| 3418 | *w = 0xDC00 | ((*four_bytes - 0x10000) & 0x3FF); |
| 3419 | } |
| 3420 | else |
| 3421 | *w = *four_bytes; |
| 3422 | |
| 3423 | if (w > wchar_end) { |
| 3424 | assert(0 && "Miscalculated string end"); |
| 3425 | } |
| 3426 | } |
| 3427 | *w = 0; |
| 3428 | #else |
| 3429 | /* sizeof(wchar_t) == 4 */ |
| 3430 | Py_FatalError("Impossible unicode object state, wstr and str " |
| 3431 | "should share memory already."); |
| 3432 | return NULL; |
| 3433 | #endif |
| 3434 | } |
| 3435 | else { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3436 | _PyUnicode_WSTR(unicode) = (wchar_t *) PyObject_MALLOC(sizeof(wchar_t) * |
| 3437 | (_PyUnicode_LENGTH(unicode) + 1)); |
| 3438 | if (!_PyUnicode_WSTR(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3439 | PyErr_NoMemory(); |
| 3440 | return NULL; |
| 3441 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3442 | if (!PyUnicode_IS_COMPACT_ASCII(unicode)) |
| 3443 | _PyUnicode_WSTR_LENGTH(unicode) = _PyUnicode_LENGTH(unicode); |
| 3444 | w = _PyUnicode_WSTR(unicode); |
| 3445 | wchar_end = w + _PyUnicode_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3446 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3447 | if (PyUnicode_KIND(unicode) == PyUnicode_1BYTE_KIND) { |
| 3448 | one_byte = PyUnicode_1BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3449 | for (; w < wchar_end; ++one_byte, ++w) |
| 3450 | *w = *one_byte; |
| 3451 | /* null-terminate the wstr */ |
| 3452 | *w = 0; |
| 3453 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3454 | else if (PyUnicode_KIND(unicode) == PyUnicode_2BYTE_KIND) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3455 | #if SIZEOF_WCHAR_T == 4 |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3456 | two_bytes = PyUnicode_2BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3457 | for (; w < wchar_end; ++two_bytes, ++w) |
| 3458 | *w = *two_bytes; |
| 3459 | /* null-terminate the wstr */ |
| 3460 | *w = 0; |
| 3461 | #else |
| 3462 | /* sizeof(wchar_t) == 2 */ |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3463 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 3464 | _PyUnicode_WSTR(unicode) = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3465 | Py_FatalError("Impossible unicode object state, wstr " |
| 3466 | "and str should share memory already."); |
| 3467 | return NULL; |
| 3468 | #endif |
| 3469 | } |
| 3470 | else { |
| 3471 | assert(0 && "This should never happen."); |
| 3472 | } |
| 3473 | } |
| 3474 | } |
| 3475 | if (size != NULL) |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3476 | *size = PyUnicode_WSTR_LENGTH(unicode); |
| 3477 | return _PyUnicode_WSTR(unicode); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3478 | } |
| 3479 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3480 | Py_UNICODE * |
| 3481 | PyUnicode_AsUnicode(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3482 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3483 | return PyUnicode_AsUnicodeAndSize(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3484 | } |
| 3485 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3486 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3487 | Py_ssize_t |
| 3488 | PyUnicode_GetSize(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3489 | { |
| 3490 | if (!PyUnicode_Check(unicode)) { |
| 3491 | PyErr_BadArgument(); |
| 3492 | goto onError; |
| 3493 | } |
| 3494 | return PyUnicode_GET_SIZE(unicode); |
| 3495 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3496 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3497 | return -1; |
| 3498 | } |
| 3499 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3500 | Py_ssize_t |
| 3501 | PyUnicode_GetLength(PyObject *unicode) |
| 3502 | { |
Victor Stinner | 5a706cf | 2011-10-02 00:36:53 +0200 | [diff] [blame] | 3503 | if (!PyUnicode_Check(unicode) || PyUnicode_READY(unicode) == -1) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3504 | PyErr_BadArgument(); |
| 3505 | return -1; |
| 3506 | } |
| 3507 | |
| 3508 | return PyUnicode_GET_LENGTH(unicode); |
| 3509 | } |
| 3510 | |
| 3511 | Py_UCS4 |
| 3512 | PyUnicode_ReadChar(PyObject *unicode, Py_ssize_t index) |
| 3513 | { |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 3514 | if (!PyUnicode_Check(unicode) || PyUnicode_READY(unicode) == -1) { |
| 3515 | PyErr_BadArgument(); |
| 3516 | return (Py_UCS4)-1; |
| 3517 | } |
| 3518 | if (index < 0 || index >= _PyUnicode_LENGTH(unicode)) { |
| 3519 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3520 | return (Py_UCS4)-1; |
| 3521 | } |
| 3522 | return PyUnicode_READ_CHAR(unicode, index); |
| 3523 | } |
| 3524 | |
| 3525 | int |
| 3526 | PyUnicode_WriteChar(PyObject *unicode, Py_ssize_t index, Py_UCS4 ch) |
| 3527 | { |
| 3528 | if (!PyUnicode_Check(unicode) || !PyUnicode_IS_COMPACT(unicode)) { |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 3529 | PyErr_BadArgument(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3530 | return -1; |
| 3531 | } |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 3532 | if (index < 0 || index >= _PyUnicode_LENGTH(unicode)) { |
| 3533 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 3534 | return -1; |
| 3535 | } |
| 3536 | if (_PyUnicode_Dirty(unicode)) |
| 3537 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3538 | PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode), |
| 3539 | index, ch); |
| 3540 | return 0; |
| 3541 | } |
| 3542 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3543 | const char * |
| 3544 | PyUnicode_GetDefaultEncoding(void) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3545 | { |
Victor Stinner | 42cb462 | 2010-09-01 19:39:01 +0000 | [diff] [blame] | 3546 | return "utf-8"; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3547 | } |
| 3548 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 3549 | /* create or adjust a UnicodeDecodeError */ |
| 3550 | static void |
| 3551 | make_decode_exception(PyObject **exceptionObject, |
| 3552 | const char *encoding, |
| 3553 | const char *input, Py_ssize_t length, |
| 3554 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 3555 | const char *reason) |
| 3556 | { |
| 3557 | if (*exceptionObject == NULL) { |
| 3558 | *exceptionObject = PyUnicodeDecodeError_Create( |
| 3559 | encoding, input, length, startpos, endpos, reason); |
| 3560 | } |
| 3561 | else { |
| 3562 | if (PyUnicodeDecodeError_SetStart(*exceptionObject, startpos)) |
| 3563 | goto onError; |
| 3564 | if (PyUnicodeDecodeError_SetEnd(*exceptionObject, endpos)) |
| 3565 | goto onError; |
| 3566 | if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) |
| 3567 | goto onError; |
| 3568 | } |
| 3569 | return; |
| 3570 | |
| 3571 | onError: |
| 3572 | Py_DECREF(*exceptionObject); |
| 3573 | *exceptionObject = NULL; |
| 3574 | } |
| 3575 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3576 | /* error handling callback helper: |
| 3577 | build arguments, call the callback and check the arguments, |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 3578 | if no exception occurred, copy the replacement to the output |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3579 | and adjust various state variables. |
| 3580 | return 0 on success, -1 on error |
| 3581 | */ |
| 3582 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3583 | static int |
| 3584 | unicode_decode_call_errorhandler(const char *errors, PyObject **errorHandler, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3585 | const char *encoding, const char *reason, |
| 3586 | const char **input, const char **inend, Py_ssize_t *startinpos, |
| 3587 | Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 3588 | PyObject **output, Py_ssize_t *outpos, Py_UNICODE **outptr) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3589 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3590 | 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] | 3591 | |
| 3592 | PyObject *restuple = NULL; |
| 3593 | PyObject *repunicode = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3594 | Py_ssize_t outsize = PyUnicode_GET_SIZE(*output); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3595 | Py_ssize_t insize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3596 | Py_ssize_t requiredsize; |
| 3597 | Py_ssize_t newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3598 | const Py_UNICODE *repptr; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3599 | PyObject *inputobj = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3600 | Py_ssize_t repsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3601 | int res = -1; |
| 3602 | |
| 3603 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3604 | *errorHandler = PyCodec_LookupError(errors); |
| 3605 | if (*errorHandler == NULL) |
| 3606 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3607 | } |
| 3608 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 3609 | make_decode_exception(exceptionObject, |
| 3610 | encoding, |
| 3611 | *input, *inend - *input, |
| 3612 | *startinpos, *endinpos, |
| 3613 | reason); |
| 3614 | if (*exceptionObject == NULL) |
| 3615 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3616 | |
| 3617 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 3618 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3619 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3620 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 3621 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3622 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3623 | } |
| 3624 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3625 | goto onError; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3626 | |
| 3627 | /* Copy back the bytes variables, which might have been modified by the |
| 3628 | callback */ |
| 3629 | inputobj = PyUnicodeDecodeError_GetObject(*exceptionObject); |
| 3630 | if (!inputobj) |
| 3631 | goto onError; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3632 | if (!PyBytes_Check(inputobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3633 | PyErr_Format(PyExc_TypeError, "exception attribute object must be bytes"); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3634 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3635 | *input = PyBytes_AS_STRING(inputobj); |
| 3636 | insize = PyBytes_GET_SIZE(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3637 | *inend = *input + insize; |
Walter Dörwald | 36f938f | 2007-08-10 10:11:43 +0000 | [diff] [blame] | 3638 | /* we can DECREF safely, as the exception has another reference, |
| 3639 | so the object won't go away. */ |
| 3640 | Py_DECREF(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3641 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3642 | if (newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3643 | newpos = insize+newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 3644 | if (newpos<0 || newpos>insize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3645 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", newpos); |
| 3646 | goto onError; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 3647 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3648 | |
| 3649 | /* need more space? (at least enough for what we |
| 3650 | have+the replacement+the rest of the string (starting |
| 3651 | at the new input position), so we won't have to check space |
| 3652 | when there are no errors in the rest of the string) */ |
| 3653 | repptr = PyUnicode_AS_UNICODE(repunicode); |
| 3654 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 3655 | requiredsize = *outpos + repsize + insize-newpos; |
| 3656 | if (requiredsize > outsize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3657 | if (requiredsize<2*outsize) |
| 3658 | requiredsize = 2*outsize; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 3659 | if (PyUnicode_Resize(output, requiredsize) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3660 | goto onError; |
| 3661 | *outptr = PyUnicode_AS_UNICODE(*output) + *outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3662 | } |
| 3663 | *endinpos = newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3664 | *inptr = *input + newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3665 | Py_UNICODE_COPY(*outptr, repptr, repsize); |
| 3666 | *outptr += repsize; |
| 3667 | *outpos += repsize; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3668 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3669 | /* we made it! */ |
| 3670 | res = 0; |
| 3671 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3672 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3673 | Py_XDECREF(restuple); |
| 3674 | return res; |
| 3675 | } |
| 3676 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3677 | /* --- UTF-7 Codec -------------------------------------------------------- */ |
| 3678 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3679 | /* See RFC2152 for details. We encode conservatively and decode liberally. */ |
| 3680 | |
| 3681 | /* Three simple macros defining base-64. */ |
| 3682 | |
| 3683 | /* Is c a base-64 character? */ |
| 3684 | |
| 3685 | #define IS_BASE64(c) \ |
| 3686 | (((c) >= 'A' && (c) <= 'Z') || \ |
| 3687 | ((c) >= 'a' && (c) <= 'z') || \ |
| 3688 | ((c) >= '0' && (c) <= '9') || \ |
| 3689 | (c) == '+' || (c) == '/') |
| 3690 | |
| 3691 | /* given that c is a base-64 character, what is its base-64 value? */ |
| 3692 | |
| 3693 | #define FROM_BASE64(c) \ |
| 3694 | (((c) >= 'A' && (c) <= 'Z') ? (c) - 'A' : \ |
| 3695 | ((c) >= 'a' && (c) <= 'z') ? (c) - 'a' + 26 : \ |
| 3696 | ((c) >= '0' && (c) <= '9') ? (c) - '0' + 52 : \ |
| 3697 | (c) == '+' ? 62 : 63) |
| 3698 | |
| 3699 | /* What is the base-64 character of the bottom 6 bits of n? */ |
| 3700 | |
| 3701 | #define TO_BASE64(n) \ |
| 3702 | ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) |
| 3703 | |
| 3704 | /* DECODE_DIRECT: this byte encountered in a UTF-7 string should be |
| 3705 | * decoded as itself. We are permissive on decoding; the only ASCII |
| 3706 | * byte not decoding to itself is the + which begins a base64 |
| 3707 | * string. */ |
| 3708 | |
| 3709 | #define DECODE_DIRECT(c) \ |
| 3710 | ((c) <= 127 && (c) != '+') |
| 3711 | |
| 3712 | /* The UTF-7 encoder treats ASCII characters differently according to |
| 3713 | * whether they are Set D, Set O, Whitespace, or special (i.e. none of |
| 3714 | * the above). See RFC2152. This array identifies these different |
| 3715 | * sets: |
| 3716 | * 0 : "Set D" |
| 3717 | * alphanumeric and '(),-./:? |
| 3718 | * 1 : "Set O" |
| 3719 | * !"#$%&*;<=>@[]^_`{|} |
| 3720 | * 2 : "whitespace" |
| 3721 | * ht nl cr sp |
| 3722 | * 3 : special (must be base64 encoded) |
| 3723 | * everything else (i.e. +\~ and non-printing codes 0-8 11-12 14-31 127) |
| 3724 | */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3725 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3726 | static |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3727 | char utf7_category[128] = { |
| 3728 | /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */ |
| 3729 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 3, 3, 2, 3, 3, |
| 3730 | /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */ |
| 3731 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 3732 | /* sp ! " # $ % & ' ( ) * + , - . / */ |
| 3733 | 2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 3, 0, 0, 0, 0, |
| 3734 | /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */ |
| 3735 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, |
| 3736 | /* @ A B C D E F G H I J K L M N O */ |
| 3737 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 3738 | /* P Q R S T U V W X Y Z [ \ ] ^ _ */ |
| 3739 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 1, 1, 1, |
| 3740 | /* ` a b c d e f g h i j k l m n o */ |
| 3741 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 3742 | /* p q r s t u v w x y z { | } ~ del */ |
| 3743 | 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] | 3744 | }; |
| 3745 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3746 | /* ENCODE_DIRECT: this character should be encoded as itself. The |
| 3747 | * answer depends on whether we are encoding set O as itself, and also |
| 3748 | * on whether we are encoding whitespace as itself. RFC2152 makes it |
| 3749 | * clear that the answers to these questions vary between |
| 3750 | * applications, so this code needs to be flexible. */ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 3751 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3752 | #define ENCODE_DIRECT(c, directO, directWS) \ |
| 3753 | ((c) < 128 && (c) > 0 && \ |
| 3754 | ((utf7_category[(c)] == 0) || \ |
| 3755 | (directWS && (utf7_category[(c)] == 2)) || \ |
| 3756 | (directO && (utf7_category[(c)] == 1)))) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3757 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3758 | PyObject * |
| 3759 | PyUnicode_DecodeUTF7(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3760 | Py_ssize_t size, |
| 3761 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3762 | { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3763 | return PyUnicode_DecodeUTF7Stateful(s, size, errors, NULL); |
| 3764 | } |
| 3765 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3766 | /* The decoder. The only state we preserve is our read position, |
| 3767 | * i.e. how many characters we have consumed. So if we end in the |
| 3768 | * middle of a shift sequence we have to back off the read position |
| 3769 | * and the output to the beginning of the sequence, otherwise we lose |
| 3770 | * all the shift state (seen bits, number of bits seen, high |
| 3771 | * surrogate). */ |
| 3772 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3773 | PyObject * |
| 3774 | PyUnicode_DecodeUTF7Stateful(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3775 | Py_ssize_t size, |
| 3776 | const char *errors, |
| 3777 | Py_ssize_t *consumed) |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3778 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3779 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3780 | Py_ssize_t startinpos; |
| 3781 | Py_ssize_t endinpos; |
| 3782 | Py_ssize_t outpos; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3783 | const char *e; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 3784 | PyObject *unicode; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3785 | Py_UNICODE *p; |
| 3786 | const char *errmsg = ""; |
| 3787 | int inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3788 | Py_UNICODE *shiftOutStart; |
| 3789 | unsigned int base64bits = 0; |
| 3790 | unsigned long base64buffer = 0; |
| 3791 | Py_UNICODE surrogate = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3792 | PyObject *errorHandler = NULL; |
| 3793 | PyObject *exc = NULL; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3794 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 3795 | unicode = (PyObject*)_PyUnicode_New(size); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3796 | if (!unicode) |
| 3797 | return NULL; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3798 | if (size == 0) { |
| 3799 | if (consumed) |
| 3800 | *consumed = 0; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 3801 | return unicode; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3802 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3803 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3804 | p = PyUnicode_AS_UNICODE(unicode); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3805 | shiftOutStart = p; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3806 | e = s + size; |
| 3807 | |
| 3808 | while (s < e) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3809 | Py_UNICODE ch; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3810 | restart: |
Antoine Pitrou | 5ffd9e9 | 2008-07-25 18:05:24 +0000 | [diff] [blame] | 3811 | ch = (unsigned char) *s; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3812 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3813 | if (inShift) { /* in a base-64 section */ |
| 3814 | if (IS_BASE64(ch)) { /* consume a base-64 character */ |
| 3815 | base64buffer = (base64buffer << 6) | FROM_BASE64(ch); |
| 3816 | base64bits += 6; |
| 3817 | s++; |
| 3818 | if (base64bits >= 16) { |
| 3819 | /* we have enough bits for a UTF-16 value */ |
| 3820 | Py_UNICODE outCh = (Py_UNICODE) |
| 3821 | (base64buffer >> (base64bits-16)); |
| 3822 | base64bits -= 16; |
| 3823 | base64buffer &= (1 << base64bits) - 1; /* clear high bits */ |
| 3824 | if (surrogate) { |
| 3825 | /* expecting a second surrogate */ |
| 3826 | if (outCh >= 0xDC00 && outCh <= 0xDFFF) { |
| 3827 | #ifdef Py_UNICODE_WIDE |
| 3828 | *p++ = (((surrogate & 0x3FF)<<10) |
| 3829 | | (outCh & 0x3FF)) + 0x10000; |
| 3830 | #else |
| 3831 | *p++ = surrogate; |
| 3832 | *p++ = outCh; |
| 3833 | #endif |
| 3834 | surrogate = 0; |
| 3835 | } |
| 3836 | else { |
| 3837 | surrogate = 0; |
| 3838 | errmsg = "second surrogate missing"; |
| 3839 | goto utf7Error; |
| 3840 | } |
| 3841 | } |
| 3842 | else if (outCh >= 0xD800 && outCh <= 0xDBFF) { |
| 3843 | /* first surrogate */ |
| 3844 | surrogate = outCh; |
| 3845 | } |
| 3846 | else if (outCh >= 0xDC00 && outCh <= 0xDFFF) { |
| 3847 | errmsg = "unexpected second surrogate"; |
| 3848 | goto utf7Error; |
| 3849 | } |
| 3850 | else { |
| 3851 | *p++ = outCh; |
| 3852 | } |
| 3853 | } |
| 3854 | } |
| 3855 | else { /* now leaving a base-64 section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3856 | inShift = 0; |
| 3857 | s++; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3858 | if (surrogate) { |
| 3859 | errmsg = "second surrogate missing at end of shift sequence"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3860 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3861 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3862 | if (base64bits > 0) { /* left-over bits */ |
| 3863 | if (base64bits >= 6) { |
| 3864 | /* We've seen at least one base-64 character */ |
| 3865 | errmsg = "partial character in shift sequence"; |
| 3866 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3867 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3868 | else { |
| 3869 | /* Some bits remain; they should be zero */ |
| 3870 | if (base64buffer != 0) { |
| 3871 | errmsg = "non-zero padding bits in shift sequence"; |
| 3872 | goto utf7Error; |
| 3873 | } |
| 3874 | } |
| 3875 | } |
| 3876 | if (ch != '-') { |
| 3877 | /* '-' is absorbed; other terminating |
| 3878 | characters are preserved */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3879 | *p++ = ch; |
| 3880 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3881 | } |
| 3882 | } |
| 3883 | else if ( ch == '+' ) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3884 | startinpos = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3885 | s++; /* consume '+' */ |
| 3886 | if (s < e && *s == '-') { /* '+-' encodes '+' */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3887 | s++; |
| 3888 | *p++ = '+'; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3889 | } |
| 3890 | else { /* begin base64-encoded section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3891 | inShift = 1; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3892 | shiftOutStart = p; |
| 3893 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3894 | } |
| 3895 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3896 | else if (DECODE_DIRECT(ch)) { /* character decodes as itself */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3897 | *p++ = ch; |
| 3898 | s++; |
| 3899 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3900 | else { |
| 3901 | startinpos = s-starts; |
| 3902 | s++; |
| 3903 | errmsg = "unexpected special character"; |
| 3904 | goto utf7Error; |
| 3905 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3906 | continue; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3907 | utf7Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3908 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 3909 | endinpos = s-starts; |
| 3910 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3911 | errors, &errorHandler, |
| 3912 | "utf7", errmsg, |
| 3913 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 3914 | &unicode, &outpos, &p)) |
| 3915 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3916 | } |
| 3917 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3918 | /* end of string */ |
| 3919 | |
| 3920 | if (inShift && !consumed) { /* in shift sequence, no more to follow */ |
| 3921 | /* if we're in an inconsistent state, that's an error */ |
| 3922 | if (surrogate || |
| 3923 | (base64bits >= 6) || |
| 3924 | (base64bits > 0 && base64buffer != 0)) { |
| 3925 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 3926 | endinpos = size; |
| 3927 | if (unicode_decode_call_errorhandler( |
| 3928 | errors, &errorHandler, |
| 3929 | "utf7", "unterminated shift sequence", |
| 3930 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 3931 | &unicode, &outpos, &p)) |
| 3932 | goto onError; |
| 3933 | if (s < e) |
| 3934 | goto restart; |
| 3935 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3936 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3937 | |
| 3938 | /* return state */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3939 | if (consumed) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3940 | if (inShift) { |
| 3941 | p = shiftOutStart; /* back off output */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3942 | *consumed = startinpos; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3943 | } |
| 3944 | else { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3945 | *consumed = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3946 | } |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3947 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3948 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 3949 | if (PyUnicode_Resize(&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3950 | goto onError; |
| 3951 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3952 | Py_XDECREF(errorHandler); |
| 3953 | Py_XDECREF(exc); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 3954 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 3955 | if (_PyUnicode_READY_REPLACE(&unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3956 | Py_DECREF(unicode); |
| 3957 | return NULL; |
| 3958 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 3959 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 3960 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 3961 | return unicode; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3962 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3963 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3964 | Py_XDECREF(errorHandler); |
| 3965 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3966 | Py_DECREF(unicode); |
| 3967 | return NULL; |
| 3968 | } |
| 3969 | |
| 3970 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3971 | PyObject * |
| 3972 | PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3973 | Py_ssize_t size, |
| 3974 | int base64SetO, |
| 3975 | int base64WhiteSpace, |
| 3976 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3977 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3978 | PyObject *v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3979 | /* It might be possible to tighten this worst case */ |
Alexandre Vassalotti | e85bd98 | 2009-07-21 00:39:03 +0000 | [diff] [blame] | 3980 | Py_ssize_t allocated = 8 * size; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3981 | int inShift = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3982 | Py_ssize_t i = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3983 | unsigned int base64bits = 0; |
| 3984 | unsigned long base64buffer = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3985 | char * out; |
| 3986 | char * start; |
| 3987 | |
| 3988 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3989 | return PyBytes_FromStringAndSize(NULL, 0); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3990 | |
Alexandre Vassalotti | e85bd98 | 2009-07-21 00:39:03 +0000 | [diff] [blame] | 3991 | if (allocated / 8 != size) |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3992 | return PyErr_NoMemory(); |
| 3993 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3994 | v = PyBytes_FromStringAndSize(NULL, allocated); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3995 | if (v == NULL) |
| 3996 | return NULL; |
| 3997 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3998 | start = out = PyBytes_AS_STRING(v); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3999 | for (;i < size; ++i) { |
| 4000 | Py_UNICODE ch = s[i]; |
| 4001 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4002 | if (inShift) { |
| 4003 | if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 4004 | /* shifting out */ |
| 4005 | if (base64bits) { /* output remaining bits */ |
| 4006 | *out++ = TO_BASE64(base64buffer << (6-base64bits)); |
| 4007 | base64buffer = 0; |
| 4008 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4009 | } |
| 4010 | inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4011 | /* Characters not in the BASE64 set implicitly unshift the sequence |
| 4012 | so no '-' is required, except if the character is itself a '-' */ |
| 4013 | if (IS_BASE64(ch) || ch == '-') { |
| 4014 | *out++ = '-'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4015 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4016 | *out++ = (char) ch; |
| 4017 | } |
| 4018 | else { |
| 4019 | goto encode_char; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4020 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4021 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4022 | else { /* not in a shift sequence */ |
| 4023 | if (ch == '+') { |
| 4024 | *out++ = '+'; |
| 4025 | *out++ = '-'; |
| 4026 | } |
| 4027 | else if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 4028 | *out++ = (char) ch; |
| 4029 | } |
| 4030 | else { |
| 4031 | *out++ = '+'; |
| 4032 | inShift = 1; |
| 4033 | goto encode_char; |
| 4034 | } |
| 4035 | } |
| 4036 | continue; |
| 4037 | encode_char: |
| 4038 | #ifdef Py_UNICODE_WIDE |
| 4039 | if (ch >= 0x10000) { |
| 4040 | /* code first surrogate */ |
| 4041 | base64bits += 16; |
| 4042 | base64buffer = (base64buffer << 16) | 0xd800 | ((ch-0x10000) >> 10); |
| 4043 | while (base64bits >= 6) { |
| 4044 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 4045 | base64bits -= 6; |
| 4046 | } |
| 4047 | /* prepare second surrogate */ |
| 4048 | ch = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 4049 | } |
| 4050 | #endif |
| 4051 | base64bits += 16; |
| 4052 | base64buffer = (base64buffer << 16) | ch; |
| 4053 | while (base64bits >= 6) { |
| 4054 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 4055 | base64bits -= 6; |
| 4056 | } |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 4057 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4058 | if (base64bits) |
| 4059 | *out++= TO_BASE64(base64buffer << (6-base64bits) ); |
| 4060 | if (inShift) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4061 | *out++ = '-'; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4062 | if (_PyBytes_Resize(&v, out - start) < 0) |
| 4063 | return NULL; |
| 4064 | return v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4065 | } |
| 4066 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4067 | #undef IS_BASE64 |
| 4068 | #undef FROM_BASE64 |
| 4069 | #undef TO_BASE64 |
| 4070 | #undef DECODE_DIRECT |
| 4071 | #undef ENCODE_DIRECT |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4072 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4073 | /* --- UTF-8 Codec -------------------------------------------------------- */ |
| 4074 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4075 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4076 | char utf8_code_length[256] = { |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4077 | /* Map UTF-8 encoded prefix byte to sequence length. Zero means |
| 4078 | illegal prefix. See RFC 3629 for details */ |
| 4079 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 00-0F */ |
| 4080 | 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] | 4081 | 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] | 4082 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 4083 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 4084 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 4085 | 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] | 4086 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 70-7F */ |
| 4087 | 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] | 4088 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4089 | 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] | 4090 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* B0-BF */ |
| 4091 | 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* C0-C1 + C2-CF */ |
| 4092 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* D0-DF */ |
| 4093 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* E0-EF */ |
| 4094 | 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] | 4095 | }; |
| 4096 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4097 | PyObject * |
| 4098 | PyUnicode_DecodeUTF8(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4099 | Py_ssize_t size, |
| 4100 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4101 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4102 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
| 4103 | } |
| 4104 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4105 | /* Mask to check or force alignment of a pointer to C 'long' boundaries */ |
| 4106 | #define LONG_PTR_MASK (size_t) (SIZEOF_LONG - 1) |
| 4107 | |
| 4108 | /* Mask to quickly check whether a C 'long' contains a |
| 4109 | non-ASCII, UTF8-encoded char. */ |
| 4110 | #if (SIZEOF_LONG == 8) |
| 4111 | # define ASCII_CHAR_MASK 0x8080808080808080L |
| 4112 | #elif (SIZEOF_LONG == 4) |
| 4113 | # define ASCII_CHAR_MASK 0x80808080L |
| 4114 | #else |
| 4115 | # error C 'long' size should be either 4 or 8! |
| 4116 | #endif |
| 4117 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4118 | /* Scans a UTF-8 string and returns the maximum character to be expected, |
| 4119 | the size of the decoded unicode string and if any major errors were |
| 4120 | encountered. |
| 4121 | |
| 4122 | This function does check basic UTF-8 sanity, it does however NOT CHECK |
| 4123 | if the string contains surrogates, and if all continuation bytes are |
| 4124 | within the correct ranges, these checks are performed in |
| 4125 | PyUnicode_DecodeUTF8Stateful. |
| 4126 | |
| 4127 | If it sets has_errors to 1, it means the value of unicode_size and max_char |
| 4128 | will be bogus and you should not rely on useful information in them. |
| 4129 | */ |
| 4130 | static Py_UCS4 |
| 4131 | utf8_max_char_size_and_has_errors(const char *s, Py_ssize_t string_size, |
| 4132 | Py_ssize_t *unicode_size, Py_ssize_t* consumed, |
| 4133 | int *has_errors) |
| 4134 | { |
| 4135 | Py_ssize_t n; |
| 4136 | Py_ssize_t char_count = 0; |
| 4137 | Py_UCS4 max_char = 127, new_max; |
| 4138 | Py_UCS4 upper_bound; |
| 4139 | const unsigned char *p = (const unsigned char *)s; |
| 4140 | const unsigned char *end = p + string_size; |
| 4141 | const unsigned char *aligned_end = (const unsigned char *) ((size_t) end & ~LONG_PTR_MASK); |
| 4142 | int err = 0; |
| 4143 | |
| 4144 | for (; p < end && !err; ++p, ++char_count) { |
| 4145 | /* Only check value if it's not a ASCII char... */ |
| 4146 | if (*p < 0x80) { |
| 4147 | /* Fast path, see below in PyUnicode_DecodeUTF8Stateful for |
| 4148 | an explanation. */ |
| 4149 | if (!((size_t) p & LONG_PTR_MASK)) { |
| 4150 | /* Help register allocation */ |
| 4151 | register const unsigned char *_p = p; |
| 4152 | while (_p < aligned_end) { |
| 4153 | unsigned long value = *(unsigned long *) _p; |
| 4154 | if (value & ASCII_CHAR_MASK) |
| 4155 | break; |
| 4156 | _p += SIZEOF_LONG; |
| 4157 | char_count += SIZEOF_LONG; |
| 4158 | } |
| 4159 | p = _p; |
| 4160 | if (p == end) |
| 4161 | break; |
| 4162 | } |
| 4163 | } |
| 4164 | if (*p >= 0x80) { |
| 4165 | n = utf8_code_length[*p]; |
| 4166 | new_max = max_char; |
| 4167 | switch (n) { |
| 4168 | /* invalid start byte */ |
| 4169 | case 0: |
| 4170 | err = 1; |
| 4171 | break; |
| 4172 | case 2: |
| 4173 | /* Code points between 0x00FF and 0x07FF inclusive. |
| 4174 | Approximate the upper bound of the code point, |
| 4175 | if this flips over 255 we can be sure it will be more |
| 4176 | than 255 and the string will need 2 bytes per code coint, |
| 4177 | if it stays under or equal to 255, we can be sure 1 byte |
| 4178 | is enough. |
| 4179 | ((*p & 0b00011111) << 6) | 0b00111111 */ |
| 4180 | upper_bound = ((*p & 0x1F) << 6) | 0x3F; |
| 4181 | if (max_char < upper_bound) |
| 4182 | new_max = upper_bound; |
| 4183 | /* Ensure we track at least that we left ASCII space. */ |
| 4184 | if (new_max < 128) |
| 4185 | new_max = 128; |
| 4186 | break; |
| 4187 | case 3: |
| 4188 | /* Between 0x0FFF and 0xFFFF inclusive, so values are |
| 4189 | always > 255 and <= 65535 and will always need 2 bytes. */ |
| 4190 | if (max_char < 65535) |
| 4191 | new_max = 65535; |
| 4192 | break; |
| 4193 | case 4: |
| 4194 | /* Code point will be above 0xFFFF for sure in this case. */ |
| 4195 | new_max = 65537; |
| 4196 | break; |
| 4197 | /* Internal error, this should be caught by the first if */ |
| 4198 | case 1: |
| 4199 | default: |
| 4200 | assert(0 && "Impossible case in utf8_max_char_and_size"); |
| 4201 | err = 1; |
| 4202 | } |
| 4203 | /* Instead of number of overall bytes for this code point, |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 4204 | n contains the number of following bytes: */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4205 | --n; |
| 4206 | /* Check if the follow up chars are all valid continuation bytes */ |
| 4207 | if (n >= 1) { |
| 4208 | const unsigned char *cont; |
| 4209 | if ((p + n) >= end) { |
| 4210 | if (consumed == 0) |
| 4211 | /* incomplete data, non-incremental decoding */ |
| 4212 | err = 1; |
| 4213 | break; |
| 4214 | } |
| 4215 | for (cont = p + 1; cont < (p + n); ++cont) { |
| 4216 | if ((*cont & 0xc0) != 0x80) { |
| 4217 | err = 1; |
| 4218 | break; |
| 4219 | } |
| 4220 | } |
| 4221 | p += n; |
| 4222 | } |
| 4223 | else |
| 4224 | err = 1; |
| 4225 | max_char = new_max; |
| 4226 | } |
| 4227 | } |
| 4228 | |
| 4229 | if (unicode_size) |
| 4230 | *unicode_size = char_count; |
| 4231 | if (has_errors) |
| 4232 | *has_errors = err; |
| 4233 | return max_char; |
| 4234 | } |
| 4235 | |
| 4236 | /* Similar to PyUnicode_WRITE but can also write into wstr field |
| 4237 | of the legacy unicode representation */ |
| 4238 | #define WRITE_FLEXIBLE_OR_WSTR(kind, buf, index, value) \ |
| 4239 | do { \ |
| 4240 | const int k_ = (kind); \ |
| 4241 | if (k_ == PyUnicode_WCHAR_KIND) \ |
| 4242 | ((Py_UNICODE *)(buf))[(index)] = (Py_UNICODE)(value); \ |
| 4243 | else if (k_ == PyUnicode_1BYTE_KIND) \ |
| 4244 | ((unsigned char *)(buf))[(index)] = (unsigned char)(value); \ |
| 4245 | else if (k_ == PyUnicode_2BYTE_KIND) \ |
| 4246 | ((Py_UCS2 *)(buf))[(index)] = (Py_UCS2)(value); \ |
| 4247 | else \ |
| 4248 | ((Py_UCS4 *)(buf))[(index)] = (Py_UCS4)(value); \ |
| 4249 | } while (0) |
| 4250 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4251 | PyObject * |
| 4252 | PyUnicode_DecodeUTF8Stateful(const char *s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4253 | Py_ssize_t size, |
| 4254 | const char *errors, |
| 4255 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4256 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4257 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4258 | int n; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4259 | int k; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4260 | Py_ssize_t startinpos; |
| 4261 | Py_ssize_t endinpos; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4262 | const char *e, *aligned_end; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4263 | PyObject *unicode; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4264 | const char *errmsg = ""; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4265 | PyObject *errorHandler = NULL; |
| 4266 | PyObject *exc = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4267 | Py_UCS4 maxchar = 0; |
| 4268 | Py_ssize_t unicode_size; |
| 4269 | Py_ssize_t i; |
| 4270 | int kind; |
| 4271 | void *data; |
| 4272 | int has_errors; |
| 4273 | Py_UNICODE *error_outptr; |
| 4274 | #if SIZEOF_WCHAR_T == 2 |
| 4275 | Py_ssize_t wchar_offset = 0; |
| 4276 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4277 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4278 | if (size == 0) { |
| 4279 | if (consumed) |
| 4280 | *consumed = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4281 | return (PyObject *)PyUnicode_New(0, 0); |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4282 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4283 | maxchar = utf8_max_char_size_and_has_errors(s, size, &unicode_size, |
| 4284 | consumed, &has_errors); |
| 4285 | if (has_errors) { |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4286 | unicode = (PyObject*)_PyUnicode_New(size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4287 | if (!unicode) |
| 4288 | return NULL; |
| 4289 | kind = PyUnicode_WCHAR_KIND; |
| 4290 | data = PyUnicode_AS_UNICODE(unicode); |
| 4291 | assert(data != NULL); |
| 4292 | } |
| 4293 | else { |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4294 | unicode = PyUnicode_New(unicode_size, maxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4295 | if (!unicode) |
| 4296 | return NULL; |
| 4297 | /* When the string is ASCII only, just use memcpy and return. |
| 4298 | unicode_size may be != size if there is an incomplete UTF-8 |
| 4299 | sequence at the end of the ASCII block. */ |
| 4300 | if (maxchar < 128 && size == unicode_size) { |
| 4301 | Py_MEMCPY(PyUnicode_1BYTE_DATA(unicode), s, unicode_size); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4302 | return unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4303 | } |
| 4304 | kind = PyUnicode_KIND(unicode); |
| 4305 | data = PyUnicode_DATA(unicode); |
| 4306 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4307 | /* Unpack UTF-8 encoded data */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4308 | i = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4309 | e = s + size; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4310 | aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4311 | |
| 4312 | while (s < e) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 4313 | Py_UCS4 ch = (unsigned char)*s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4314 | |
| 4315 | if (ch < 0x80) { |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4316 | /* Fast path for runs of ASCII characters. Given that common UTF-8 |
| 4317 | input will consist of an overwhelming majority of ASCII |
| 4318 | characters, we try to optimize for this case by checking |
| 4319 | as many characters as a C 'long' can contain. |
| 4320 | First, check if we can do an aligned read, as most CPUs have |
| 4321 | a penalty for unaligned reads. |
| 4322 | */ |
| 4323 | if (!((size_t) s & LONG_PTR_MASK)) { |
| 4324 | /* Help register allocation */ |
| 4325 | register const char *_s = s; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4326 | register Py_ssize_t _i = i; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4327 | while (_s < aligned_end) { |
| 4328 | /* Read a whole long at a time (either 4 or 8 bytes), |
| 4329 | and do a fast unrolled copy if it only contains ASCII |
| 4330 | characters. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4331 | unsigned long value = *(unsigned long *) _s; |
| 4332 | if (value & ASCII_CHAR_MASK) |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4333 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4334 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+0, _s[0]); |
| 4335 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+1, _s[1]); |
| 4336 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+2, _s[2]); |
| 4337 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+3, _s[3]); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4338 | #if (SIZEOF_LONG == 8) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4339 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+4, _s[4]); |
| 4340 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+5, _s[5]); |
| 4341 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+6, _s[6]); |
| 4342 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+7, _s[7]); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4343 | #endif |
| 4344 | _s += SIZEOF_LONG; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4345 | _i += SIZEOF_LONG; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4346 | } |
| 4347 | s = _s; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4348 | i = _i; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4349 | if (s == e) |
| 4350 | break; |
| 4351 | ch = (unsigned char)*s; |
| 4352 | } |
| 4353 | } |
| 4354 | |
| 4355 | if (ch < 0x80) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4356 | WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4357 | s++; |
| 4358 | continue; |
| 4359 | } |
| 4360 | |
| 4361 | n = utf8_code_length[ch]; |
| 4362 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4363 | if (s + n > e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4364 | if (consumed) |
| 4365 | break; |
| 4366 | else { |
| 4367 | errmsg = "unexpected end of data"; |
| 4368 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4369 | endinpos = startinpos+1; |
| 4370 | for (k=1; (k < size-startinpos) && ((s[k]&0xC0) == 0x80); k++) |
| 4371 | endinpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4372 | goto utf8Error; |
| 4373 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4374 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4375 | |
| 4376 | switch (n) { |
| 4377 | |
| 4378 | case 0: |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4379 | errmsg = "invalid start byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4380 | startinpos = s-starts; |
| 4381 | endinpos = startinpos+1; |
| 4382 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4383 | |
| 4384 | case 1: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4385 | errmsg = "internal error"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4386 | startinpos = s-starts; |
| 4387 | endinpos = startinpos+1; |
| 4388 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4389 | |
| 4390 | case 2: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4391 | if ((s[1] & 0xc0) != 0x80) { |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4392 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4393 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4394 | endinpos = startinpos + 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4395 | goto utf8Error; |
| 4396 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4397 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4398 | assert ((ch > 0x007F) && (ch <= 0x07FF)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4399 | WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4400 | break; |
| 4401 | |
| 4402 | case 3: |
Ezio Melotti | 9bf2b3a | 2010-07-03 04:52:19 +0000 | [diff] [blame] | 4403 | /* Decoding UTF-8 sequences in range \xed\xa0\x80-\xed\xbf\xbf |
| 4404 | will result in surrogates in range d800-dfff. Surrogates are |
| 4405 | not valid UTF-8 so they are rejected. |
| 4406 | See http://www.unicode.org/versions/Unicode5.2.0/ch03.pdf |
| 4407 | (table 3-7) and http://www.rfc-editor.org/rfc/rfc3629.txt */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4408 | if ((s[1] & 0xc0) != 0x80 || |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4409 | (s[2] & 0xc0) != 0x80 || |
| 4410 | ((unsigned char)s[0] == 0xE0 && |
| 4411 | (unsigned char)s[1] < 0xA0) || |
| 4412 | ((unsigned char)s[0] == 0xED && |
| 4413 | (unsigned char)s[1] > 0x9F)) { |
| 4414 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4415 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4416 | endinpos = startinpos + 1; |
| 4417 | |
| 4418 | /* if s[1] first two bits are 1 and 0, then the invalid |
| 4419 | continuation byte is s[2], so increment endinpos by 1, |
| 4420 | if not, s[1] is invalid and endinpos doesn't need to |
| 4421 | be incremented. */ |
| 4422 | if ((s[1] & 0xC0) == 0x80) |
| 4423 | endinpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4424 | goto utf8Error; |
| 4425 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4426 | ch = ((s[0] & 0x0f) << 12) + ((s[1] & 0x3f) << 6) + (s[2] & 0x3f); |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4427 | assert ((ch > 0x07FF) && (ch <= 0xFFFF)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4428 | WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, ch); |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 4429 | break; |
| 4430 | |
| 4431 | case 4: |
| 4432 | if ((s[1] & 0xc0) != 0x80 || |
| 4433 | (s[2] & 0xc0) != 0x80 || |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4434 | (s[3] & 0xc0) != 0x80 || |
| 4435 | ((unsigned char)s[0] == 0xF0 && |
| 4436 | (unsigned char)s[1] < 0x90) || |
| 4437 | ((unsigned char)s[0] == 0xF4 && |
| 4438 | (unsigned char)s[1] > 0x8F)) { |
| 4439 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4440 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4441 | endinpos = startinpos + 1; |
| 4442 | if ((s[1] & 0xC0) == 0x80) { |
| 4443 | endinpos++; |
| 4444 | if ((s[2] & 0xC0) == 0x80) |
| 4445 | endinpos++; |
| 4446 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4447 | goto utf8Error; |
| 4448 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 4449 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4450 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
| 4451 | assert ((ch > 0xFFFF) && (ch <= 0x10ffff)); |
| 4452 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4453 | /* If the string is flexible or we have native UCS-4, write |
| 4454 | directly.. */ |
| 4455 | if (sizeof(Py_UNICODE) > 2 || kind != PyUnicode_WCHAR_KIND) |
| 4456 | WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, ch); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4457 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4458 | else { |
| 4459 | /* compute and append the two surrogates: */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4460 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4461 | /* translate from 10000..10FFFF to 0..FFFF */ |
| 4462 | ch -= 0x10000; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4463 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4464 | /* high surrogate = top 10 bits added to D800 */ |
| 4465 | WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, |
| 4466 | (Py_UNICODE)(0xD800 + (ch >> 10))); |
| 4467 | |
| 4468 | /* low surrogate = bottom 10 bits added to DC00 */ |
| 4469 | WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, |
| 4470 | (Py_UNICODE)(0xDC00 + (ch & 0x03FF))); |
| 4471 | } |
| 4472 | #if SIZEOF_WCHAR_T == 2 |
| 4473 | wchar_offset++; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 4474 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4475 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4476 | } |
| 4477 | s += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4478 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4479 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4480 | utf8Error: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4481 | /* If this is not yet a resizable string, make it one.. */ |
| 4482 | if (kind != PyUnicode_WCHAR_KIND) { |
| 4483 | const Py_UNICODE *u; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4484 | PyObject *new_unicode = (PyObject*)_PyUnicode_New(size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4485 | if (!new_unicode) |
| 4486 | goto onError; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4487 | u = PyUnicode_AsUnicode(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4488 | if (!u) |
| 4489 | goto onError; |
| 4490 | #if SIZEOF_WCHAR_T == 2 |
| 4491 | i += wchar_offset; |
| 4492 | #endif |
| 4493 | Py_UNICODE_COPY(PyUnicode_AS_UNICODE(new_unicode), u, i); |
| 4494 | Py_DECREF(unicode); |
| 4495 | unicode = new_unicode; |
| 4496 | kind = 0; |
| 4497 | data = PyUnicode_AS_UNICODE(new_unicode); |
| 4498 | assert(data != NULL); |
| 4499 | } |
| 4500 | error_outptr = PyUnicode_AS_UNICODE(unicode) + i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4501 | if (unicode_decode_call_errorhandler( |
| 4502 | errors, &errorHandler, |
| 4503 | "utf8", errmsg, |
| 4504 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4505 | &unicode, &i, &error_outptr)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4506 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4507 | /* Update data because unicode_decode_call_errorhandler might have |
| 4508 | re-created or resized the unicode object. */ |
| 4509 | data = PyUnicode_AS_UNICODE(unicode); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4510 | aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4511 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4512 | /* Ensure the unicode_size calculation above was correct: */ |
| 4513 | assert(kind == PyUnicode_WCHAR_KIND || i == unicode_size); |
| 4514 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4515 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4516 | *consumed = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4517 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4518 | /* Adjust length and ready string when it contained errors and |
| 4519 | is of the old resizable kind. */ |
| 4520 | if (kind == PyUnicode_WCHAR_KIND) { |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4521 | if (PyUnicode_Resize(&unicode, i) < 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4522 | goto onError; |
| 4523 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4524 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4525 | Py_XDECREF(errorHandler); |
| 4526 | Py_XDECREF(exc); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 4527 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 4528 | if (_PyUnicode_READY_REPLACE(&unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4529 | Py_DECREF(unicode); |
| 4530 | return NULL; |
| 4531 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 4532 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 4533 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4534 | return unicode; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4535 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4536 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4537 | Py_XDECREF(errorHandler); |
| 4538 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4539 | Py_DECREF(unicode); |
| 4540 | return NULL; |
| 4541 | } |
| 4542 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4543 | #undef WRITE_FLEXIBLE_OR_WSTR |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4544 | |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4545 | #ifdef __APPLE__ |
| 4546 | |
| 4547 | /* Simplified UTF-8 decoder using surrogateescape error handler, |
| 4548 | used to decode the command line arguments on Mac OS X. */ |
| 4549 | |
| 4550 | wchar_t* |
| 4551 | _Py_DecodeUTF8_surrogateescape(const char *s, Py_ssize_t size) |
| 4552 | { |
| 4553 | int n; |
| 4554 | const char *e; |
| 4555 | wchar_t *unicode, *p; |
| 4556 | |
| 4557 | /* Note: size will always be longer than the resulting Unicode |
| 4558 | character count */ |
| 4559 | if (PY_SSIZE_T_MAX / sizeof(wchar_t) < (size + 1)) { |
| 4560 | PyErr_NoMemory(); |
| 4561 | return NULL; |
| 4562 | } |
| 4563 | unicode = PyMem_Malloc((size + 1) * sizeof(wchar_t)); |
| 4564 | if (!unicode) |
| 4565 | return NULL; |
| 4566 | |
| 4567 | /* Unpack UTF-8 encoded data */ |
| 4568 | p = unicode; |
| 4569 | e = s + size; |
| 4570 | while (s < e) { |
| 4571 | Py_UCS4 ch = (unsigned char)*s; |
| 4572 | |
| 4573 | if (ch < 0x80) { |
| 4574 | *p++ = (wchar_t)ch; |
| 4575 | s++; |
| 4576 | continue; |
| 4577 | } |
| 4578 | |
| 4579 | n = utf8_code_length[ch]; |
| 4580 | if (s + n > e) { |
| 4581 | goto surrogateescape; |
| 4582 | } |
| 4583 | |
| 4584 | switch (n) { |
| 4585 | case 0: |
| 4586 | case 1: |
| 4587 | goto surrogateescape; |
| 4588 | |
| 4589 | case 2: |
| 4590 | if ((s[1] & 0xc0) != 0x80) |
| 4591 | goto surrogateescape; |
| 4592 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
| 4593 | assert ((ch > 0x007F) && (ch <= 0x07FF)); |
| 4594 | *p++ = (wchar_t)ch; |
| 4595 | break; |
| 4596 | |
| 4597 | case 3: |
| 4598 | /* Decoding UTF-8 sequences in range \xed\xa0\x80-\xed\xbf\xbf |
| 4599 | will result in surrogates in range d800-dfff. Surrogates are |
| 4600 | not valid UTF-8 so they are rejected. |
| 4601 | See http://www.unicode.org/versions/Unicode5.2.0/ch03.pdf |
| 4602 | (table 3-7) and http://www.rfc-editor.org/rfc/rfc3629.txt */ |
| 4603 | if ((s[1] & 0xc0) != 0x80 || |
| 4604 | (s[2] & 0xc0) != 0x80 || |
| 4605 | ((unsigned char)s[0] == 0xE0 && |
| 4606 | (unsigned char)s[1] < 0xA0) || |
| 4607 | ((unsigned char)s[0] == 0xED && |
| 4608 | (unsigned char)s[1] > 0x9F)) { |
| 4609 | |
| 4610 | goto surrogateescape; |
| 4611 | } |
| 4612 | ch = ((s[0] & 0x0f) << 12) + ((s[1] & 0x3f) << 6) + (s[2] & 0x3f); |
| 4613 | assert ((ch > 0x07FF) && (ch <= 0xFFFF)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4614 | *p++ = (wchar_t)ch; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4615 | break; |
| 4616 | |
| 4617 | case 4: |
| 4618 | if ((s[1] & 0xc0) != 0x80 || |
| 4619 | (s[2] & 0xc0) != 0x80 || |
| 4620 | (s[3] & 0xc0) != 0x80 || |
| 4621 | ((unsigned char)s[0] == 0xF0 && |
| 4622 | (unsigned char)s[1] < 0x90) || |
| 4623 | ((unsigned char)s[0] == 0xF4 && |
| 4624 | (unsigned char)s[1] > 0x8F)) { |
| 4625 | goto surrogateescape; |
| 4626 | } |
| 4627 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
| 4628 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
| 4629 | assert ((ch > 0xFFFF) && (ch <= 0x10ffff)); |
| 4630 | |
| 4631 | #if SIZEOF_WCHAR_T == 4 |
| 4632 | *p++ = (wchar_t)ch; |
| 4633 | #else |
| 4634 | /* compute and append the two surrogates: */ |
| 4635 | |
| 4636 | /* translate from 10000..10FFFF to 0..FFFF */ |
| 4637 | ch -= 0x10000; |
| 4638 | |
| 4639 | /* high surrogate = top 10 bits added to D800 */ |
| 4640 | *p++ = (wchar_t)(0xD800 + (ch >> 10)); |
| 4641 | |
| 4642 | /* low surrogate = bottom 10 bits added to DC00 */ |
| 4643 | *p++ = (wchar_t)(0xDC00 + (ch & 0x03FF)); |
| 4644 | #endif |
| 4645 | break; |
| 4646 | } |
| 4647 | s += n; |
| 4648 | continue; |
| 4649 | |
| 4650 | surrogateescape: |
| 4651 | *p++ = 0xDC00 + ch; |
| 4652 | s++; |
| 4653 | } |
| 4654 | *p = L'\0'; |
| 4655 | return unicode; |
| 4656 | } |
| 4657 | |
| 4658 | #endif /* __APPLE__ */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4659 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4660 | /* Primary internal function which creates utf8 encoded bytes objects. |
| 4661 | |
| 4662 | Allocation strategy: if the string is short, convert into a stack buffer |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4663 | and allocate exactly as much space needed at the end. Else allocate the |
| 4664 | maximum possible needed (4 result bytes per Unicode character), and return |
| 4665 | the excess memory at the end. |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4666 | */ |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 4667 | PyObject * |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4668 | _PyUnicode_AsUTF8String(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4669 | { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4670 | #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] | 4671 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4672 | Py_ssize_t i; /* index into s of next input byte */ |
| 4673 | PyObject *result; /* result string object */ |
| 4674 | char *p; /* next free byte in output buffer */ |
| 4675 | Py_ssize_t nallocated; /* number of result bytes allocated */ |
| 4676 | Py_ssize_t nneeded; /* number of result bytes needed */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4677 | char stackbuf[MAX_SHORT_UNICHARS * 4]; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4678 | PyObject *errorHandler = NULL; |
| 4679 | PyObject *exc = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4680 | int kind; |
| 4681 | void *data; |
| 4682 | Py_ssize_t size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4683 | #if SIZEOF_WCHAR_T == 2 |
| 4684 | Py_ssize_t wchar_offset = 0; |
| 4685 | #endif |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 4686 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4687 | if (!PyUnicode_Check(unicode)) { |
| 4688 | PyErr_BadArgument(); |
| 4689 | return NULL; |
| 4690 | } |
| 4691 | |
| 4692 | if (PyUnicode_READY(unicode) == -1) |
| 4693 | return NULL; |
| 4694 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 4695 | if (PyUnicode_UTF8(unicode)) |
| 4696 | return PyBytes_FromStringAndSize(PyUnicode_UTF8(unicode), |
| 4697 | PyUnicode_UTF8_LENGTH(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4698 | |
| 4699 | kind = PyUnicode_KIND(unicode); |
| 4700 | data = PyUnicode_DATA(unicode); |
| 4701 | size = PyUnicode_GET_LENGTH(unicode); |
| 4702 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4703 | assert(size >= 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4704 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4705 | if (size <= MAX_SHORT_UNICHARS) { |
| 4706 | /* Write into the stack buffer; nallocated can't overflow. |
| 4707 | * At the end, we'll allocate exactly as much heap space as it |
| 4708 | * turns out we need. |
| 4709 | */ |
| 4710 | nallocated = Py_SAFE_DOWNCAST(sizeof(stackbuf), size_t, int); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4711 | result = NULL; /* will allocate after we're done */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4712 | p = stackbuf; |
| 4713 | } |
| 4714 | else { |
| 4715 | /* Overallocate on the heap, and give the excess back at the end. */ |
| 4716 | nallocated = size * 4; |
| 4717 | if (nallocated / 4 != size) /* overflow! */ |
| 4718 | return PyErr_NoMemory(); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4719 | result = PyBytes_FromStringAndSize(NULL, nallocated); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4720 | if (result == NULL) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4721 | return NULL; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4722 | p = PyBytes_AS_STRING(result); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4723 | } |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4724 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4725 | for (i = 0; i < size;) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4726 | Py_UCS4 ch = PyUnicode_READ(kind, data, i++); |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 4727 | |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4728 | if (ch < 0x80) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4729 | /* Encode ASCII */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4730 | *p++ = (char) ch; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 4731 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4732 | else if (ch < 0x0800) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4733 | /* Encode Latin-1 */ |
Marc-André Lemburg | dc724d6 | 2002-02-06 18:20:19 +0000 | [diff] [blame] | 4734 | *p++ = (char)(0xc0 | (ch >> 6)); |
| 4735 | *p++ = (char)(0x80 | (ch & 0x3f)); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4736 | } else if (0xD800 <= ch && ch <= 0xDFFF) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4737 | Py_ssize_t newpos; |
| 4738 | PyObject *rep; |
| 4739 | Py_ssize_t repsize, k, startpos; |
| 4740 | startpos = i-1; |
| 4741 | #if SIZEOF_WCHAR_T == 2 |
| 4742 | startpos += wchar_offset; |
Victor Stinner | 445a623 | 2010-04-22 20:01:57 +0000 | [diff] [blame] | 4743 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4744 | rep = unicode_encode_call_errorhandler( |
| 4745 | errors, &errorHandler, "utf-8", "surrogates not allowed", |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4746 | unicode, &exc, startpos, startpos+1, &newpos); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4747 | if (!rep) |
| 4748 | goto error; |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4749 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4750 | if (PyBytes_Check(rep)) |
| 4751 | repsize = PyBytes_GET_SIZE(rep); |
| 4752 | else |
| 4753 | repsize = PyUnicode_GET_SIZE(rep); |
| 4754 | |
| 4755 | if (repsize > 4) { |
| 4756 | Py_ssize_t offset; |
| 4757 | |
| 4758 | if (result == NULL) |
| 4759 | offset = p - stackbuf; |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4760 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4761 | offset = p - PyBytes_AS_STRING(result); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4762 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4763 | if (nallocated > PY_SSIZE_T_MAX - repsize + 4) { |
| 4764 | /* integer overflow */ |
| 4765 | PyErr_NoMemory(); |
| 4766 | goto error; |
| 4767 | } |
| 4768 | nallocated += repsize - 4; |
| 4769 | if (result != NULL) { |
| 4770 | if (_PyBytes_Resize(&result, nallocated) < 0) |
| 4771 | goto error; |
| 4772 | } else { |
| 4773 | result = PyBytes_FromStringAndSize(NULL, nallocated); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4774 | if (result == NULL) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4775 | goto error; |
| 4776 | Py_MEMCPY(PyBytes_AS_STRING(result), stackbuf, offset); |
| 4777 | } |
| 4778 | p = PyBytes_AS_STRING(result) + offset; |
| 4779 | } |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4780 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4781 | if (PyBytes_Check(rep)) { |
| 4782 | char *prep = PyBytes_AS_STRING(rep); |
| 4783 | for(k = repsize; k > 0; k--) |
| 4784 | *p++ = *prep++; |
| 4785 | } else /* rep is unicode */ { |
| 4786 | const Py_UNICODE *prep = PyUnicode_AS_UNICODE(rep); |
| 4787 | Py_UNICODE c; |
| 4788 | |
| 4789 | for(k=0; k<repsize; k++) { |
| 4790 | c = prep[k]; |
| 4791 | if (0x80 <= c) { |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 4792 | raise_encode_exception_obj(&exc, "utf-8", |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4793 | unicode, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 4794 | i-1, i, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4795 | "surrogates not allowed"); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4796 | goto error; |
| 4797 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4798 | *p++ = (char)prep[k]; |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4799 | } |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4800 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4801 | Py_DECREF(rep); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4802 | } else if (ch < 0x10000) { |
| 4803 | *p++ = (char)(0xe0 | (ch >> 12)); |
| 4804 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 4805 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 4806 | } else /* ch >= 0x10000 */ { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4807 | /* Encode UCS4 Unicode ordinals */ |
| 4808 | *p++ = (char)(0xf0 | (ch >> 18)); |
| 4809 | *p++ = (char)(0x80 | ((ch >> 12) & 0x3f)); |
| 4810 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 4811 | *p++ = (char)(0x80 | (ch & 0x3f)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4812 | #if SIZEOF_WCHAR_T == 2 |
| 4813 | wchar_offset++; |
| 4814 | #endif |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4815 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4816 | } |
Tim Peters | 0eca65c | 2002-04-21 17:28:06 +0000 | [diff] [blame] | 4817 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4818 | if (result == NULL) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4819 | /* This was stack allocated. */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4820 | nneeded = p - stackbuf; |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4821 | assert(nneeded <= nallocated); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4822 | result = PyBytes_FromStringAndSize(stackbuf, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4823 | } |
| 4824 | else { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 4825 | /* Cut back to size actually needed. */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4826 | nneeded = p - PyBytes_AS_STRING(result); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4827 | assert(nneeded <= nallocated); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4828 | _PyBytes_Resize(&result, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4829 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4830 | |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4831 | Py_XDECREF(errorHandler); |
| 4832 | Py_XDECREF(exc); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4833 | return result; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4834 | error: |
| 4835 | Py_XDECREF(errorHandler); |
| 4836 | Py_XDECREF(exc); |
| 4837 | Py_XDECREF(result); |
| 4838 | return NULL; |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4839 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4840 | #undef MAX_SHORT_UNICHARS |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4841 | } |
| 4842 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4843 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4844 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
| 4845 | Py_ssize_t size, |
| 4846 | const char *errors) |
| 4847 | { |
| 4848 | PyObject *v, *unicode; |
| 4849 | |
| 4850 | unicode = PyUnicode_FromUnicode(s, size); |
| 4851 | if (unicode == NULL) |
| 4852 | return NULL; |
| 4853 | v = _PyUnicode_AsUTF8String(unicode, errors); |
| 4854 | Py_DECREF(unicode); |
| 4855 | return v; |
| 4856 | } |
| 4857 | |
| 4858 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4859 | PyUnicode_AsUTF8String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4860 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4861 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4862 | } |
| 4863 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4864 | /* --- UTF-32 Codec ------------------------------------------------------- */ |
| 4865 | |
| 4866 | PyObject * |
| 4867 | PyUnicode_DecodeUTF32(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4868 | Py_ssize_t size, |
| 4869 | const char *errors, |
| 4870 | int *byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4871 | { |
| 4872 | return PyUnicode_DecodeUTF32Stateful(s, size, errors, byteorder, NULL); |
| 4873 | } |
| 4874 | |
| 4875 | PyObject * |
| 4876 | PyUnicode_DecodeUTF32Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4877 | Py_ssize_t size, |
| 4878 | const char *errors, |
| 4879 | int *byteorder, |
| 4880 | Py_ssize_t *consumed) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4881 | { |
| 4882 | const char *starts = s; |
| 4883 | Py_ssize_t startinpos; |
| 4884 | Py_ssize_t endinpos; |
| 4885 | Py_ssize_t outpos; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4886 | PyObject *unicode; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4887 | Py_UNICODE *p; |
| 4888 | #ifndef Py_UNICODE_WIDE |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 4889 | int pairs = 0; |
Mark Dickinson | 7db923c | 2010-06-12 09:10:14 +0000 | [diff] [blame] | 4890 | const unsigned char *qq; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4891 | #else |
| 4892 | const int pairs = 0; |
| 4893 | #endif |
Mark Dickinson | 7db923c | 2010-06-12 09:10:14 +0000 | [diff] [blame] | 4894 | const unsigned char *q, *e; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4895 | int bo = 0; /* assume native ordering by default */ |
| 4896 | const char *errmsg = ""; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4897 | /* Offsets from q for retrieving bytes in the right order. */ |
| 4898 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 4899 | int iorder[] = {0, 1, 2, 3}; |
| 4900 | #else |
| 4901 | int iorder[] = {3, 2, 1, 0}; |
| 4902 | #endif |
| 4903 | PyObject *errorHandler = NULL; |
| 4904 | PyObject *exc = NULL; |
Victor Stinner | 313a120 | 2010-06-11 23:56:51 +0000 | [diff] [blame] | 4905 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4906 | q = (unsigned char *)s; |
| 4907 | e = q + size; |
| 4908 | |
| 4909 | if (byteorder) |
| 4910 | bo = *byteorder; |
| 4911 | |
| 4912 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 4913 | byte order setting accordingly. In native mode, the leading BOM |
| 4914 | mark is skipped, in all other modes, it is copied to the output |
| 4915 | stream as-is (giving a ZWNBSP character). */ |
| 4916 | if (bo == 0) { |
| 4917 | if (size >= 4) { |
| 4918 | const Py_UCS4 bom = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4919 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4920 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4921 | if (bom == 0x0000FEFF) { |
| 4922 | q += 4; |
| 4923 | bo = -1; |
| 4924 | } |
| 4925 | else if (bom == 0xFFFE0000) { |
| 4926 | q += 4; |
| 4927 | bo = 1; |
| 4928 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4929 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4930 | if (bom == 0x0000FEFF) { |
| 4931 | q += 4; |
| 4932 | bo = 1; |
| 4933 | } |
| 4934 | else if (bom == 0xFFFE0000) { |
| 4935 | q += 4; |
| 4936 | bo = -1; |
| 4937 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4938 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4939 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4940 | } |
| 4941 | |
| 4942 | if (bo == -1) { |
| 4943 | /* force LE */ |
| 4944 | iorder[0] = 0; |
| 4945 | iorder[1] = 1; |
| 4946 | iorder[2] = 2; |
| 4947 | iorder[3] = 3; |
| 4948 | } |
| 4949 | else if (bo == 1) { |
| 4950 | /* force BE */ |
| 4951 | iorder[0] = 3; |
| 4952 | iorder[1] = 2; |
| 4953 | iorder[2] = 1; |
| 4954 | iorder[3] = 0; |
| 4955 | } |
| 4956 | |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 4957 | /* On narrow builds we split characters outside the BMP into two |
| 4958 | codepoints => count how much extra space we need. */ |
| 4959 | #ifndef Py_UNICODE_WIDE |
| 4960 | for (qq = q; qq < e; qq += 4) |
| 4961 | if (qq[iorder[2]] != 0 || qq[iorder[3]] != 0) |
| 4962 | pairs++; |
| 4963 | #endif |
| 4964 | |
| 4965 | /* This might be one to much, because of a BOM */ |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4966 | unicode = (PyObject*)_PyUnicode_New((size+3)/4+pairs); |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 4967 | if (!unicode) |
| 4968 | return NULL; |
| 4969 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4970 | return unicode; |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 4971 | |
| 4972 | /* Unpack UTF-32 encoded data */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4973 | p = PyUnicode_AS_UNICODE(unicode); |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 4974 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4975 | while (q < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4976 | Py_UCS4 ch; |
| 4977 | /* remaining bytes at the end? (size should be divisible by 4) */ |
| 4978 | if (e-q<4) { |
| 4979 | if (consumed) |
| 4980 | break; |
| 4981 | errmsg = "truncated data"; |
| 4982 | startinpos = ((const char *)q)-starts; |
| 4983 | endinpos = ((const char *)e)-starts; |
| 4984 | goto utf32Error; |
| 4985 | /* The remaining input chars are ignored if the callback |
| 4986 | chooses to skip the input */ |
| 4987 | } |
| 4988 | ch = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
| 4989 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4990 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4991 | if (ch >= 0x110000) |
| 4992 | { |
| 4993 | errmsg = "codepoint not in range(0x110000)"; |
| 4994 | startinpos = ((const char *)q)-starts; |
| 4995 | endinpos = startinpos+4; |
| 4996 | goto utf32Error; |
| 4997 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4998 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4999 | if (ch >= 0x10000) |
| 5000 | { |
| 5001 | *p++ = 0xD800 | ((ch-0x10000) >> 10); |
| 5002 | *p++ = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 5003 | } |
| 5004 | else |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5005 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5006 | *p++ = ch; |
| 5007 | q += 4; |
| 5008 | continue; |
| 5009 | utf32Error: |
| 5010 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 5011 | if (unicode_decode_call_errorhandler( |
| 5012 | errors, &errorHandler, |
| 5013 | "utf32", errmsg, |
| 5014 | &starts, (const char **)&e, &startinpos, &endinpos, &exc, (const char **)&q, |
| 5015 | &unicode, &outpos, &p)) |
| 5016 | goto onError; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5017 | } |
| 5018 | |
| 5019 | if (byteorder) |
| 5020 | *byteorder = bo; |
| 5021 | |
| 5022 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5023 | *consumed = (const char *)q-starts; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5024 | |
| 5025 | /* Adjust length */ |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5026 | if (PyUnicode_Resize(&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5027 | goto onError; |
| 5028 | |
| 5029 | Py_XDECREF(errorHandler); |
| 5030 | Py_XDECREF(exc); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 5031 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 5032 | if (_PyUnicode_READY_REPLACE(&unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5033 | Py_DECREF(unicode); |
| 5034 | return NULL; |
| 5035 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 5036 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 5037 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5038 | return unicode; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5039 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5040 | onError: |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5041 | Py_DECREF(unicode); |
| 5042 | Py_XDECREF(errorHandler); |
| 5043 | Py_XDECREF(exc); |
| 5044 | return NULL; |
| 5045 | } |
| 5046 | |
| 5047 | PyObject * |
| 5048 | PyUnicode_EncodeUTF32(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5049 | Py_ssize_t size, |
| 5050 | const char *errors, |
| 5051 | int byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5052 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5053 | PyObject *v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5054 | unsigned char *p; |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5055 | Py_ssize_t nsize, bytesize; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5056 | #ifndef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5057 | Py_ssize_t i, pairs; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5058 | #else |
| 5059 | const int pairs = 0; |
| 5060 | #endif |
| 5061 | /* Offsets from p for storing byte pairs in the right order. */ |
| 5062 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5063 | int iorder[] = {0, 1, 2, 3}; |
| 5064 | #else |
| 5065 | int iorder[] = {3, 2, 1, 0}; |
| 5066 | #endif |
| 5067 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5068 | #define STORECHAR(CH) \ |
| 5069 | do { \ |
| 5070 | p[iorder[3]] = ((CH) >> 24) & 0xff; \ |
| 5071 | p[iorder[2]] = ((CH) >> 16) & 0xff; \ |
| 5072 | p[iorder[1]] = ((CH) >> 8) & 0xff; \ |
| 5073 | p[iorder[0]] = (CH) & 0xff; \ |
| 5074 | p += 4; \ |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5075 | } while(0) |
| 5076 | |
| 5077 | /* In narrow builds we can output surrogate pairs as one codepoint, |
| 5078 | so we need less space. */ |
| 5079 | #ifndef Py_UNICODE_WIDE |
| 5080 | for (i = pairs = 0; i < size-1; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5081 | if (0xD800 <= s[i] && s[i] <= 0xDBFF && |
| 5082 | 0xDC00 <= s[i+1] && s[i+1] <= 0xDFFF) |
| 5083 | pairs++; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5084 | #endif |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5085 | nsize = (size - pairs + (byteorder == 0)); |
| 5086 | bytesize = nsize * 4; |
| 5087 | if (bytesize / 4 != nsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5088 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5089 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5090 | if (v == NULL) |
| 5091 | return NULL; |
| 5092 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5093 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5094 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5095 | STORECHAR(0xFEFF); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5096 | if (size == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5097 | goto done; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5098 | |
| 5099 | if (byteorder == -1) { |
| 5100 | /* force LE */ |
| 5101 | iorder[0] = 0; |
| 5102 | iorder[1] = 1; |
| 5103 | iorder[2] = 2; |
| 5104 | iorder[3] = 3; |
| 5105 | } |
| 5106 | else if (byteorder == 1) { |
| 5107 | /* force BE */ |
| 5108 | iorder[0] = 3; |
| 5109 | iorder[1] = 2; |
| 5110 | iorder[2] = 1; |
| 5111 | iorder[3] = 0; |
| 5112 | } |
| 5113 | |
| 5114 | while (size-- > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5115 | Py_UCS4 ch = *s++; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5116 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5117 | if (0xD800 <= ch && ch <= 0xDBFF && size > 0) { |
| 5118 | Py_UCS4 ch2 = *s; |
| 5119 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
| 5120 | ch = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
| 5121 | s++; |
| 5122 | size--; |
| 5123 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5124 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5125 | #endif |
| 5126 | STORECHAR(ch); |
| 5127 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5128 | |
| 5129 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5130 | return v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5131 | #undef STORECHAR |
| 5132 | } |
| 5133 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5134 | PyObject * |
| 5135 | PyUnicode_AsUTF32String(PyObject *unicode) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5136 | { |
| 5137 | if (!PyUnicode_Check(unicode)) { |
| 5138 | PyErr_BadArgument(); |
| 5139 | return NULL; |
| 5140 | } |
| 5141 | return PyUnicode_EncodeUTF32(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5142 | PyUnicode_GET_SIZE(unicode), |
| 5143 | NULL, |
| 5144 | 0); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5145 | } |
| 5146 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5147 | /* --- UTF-16 Codec ------------------------------------------------------- */ |
| 5148 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5149 | PyObject * |
| 5150 | PyUnicode_DecodeUTF16(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5151 | Py_ssize_t size, |
| 5152 | const char *errors, |
| 5153 | int *byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5154 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5155 | return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL); |
| 5156 | } |
| 5157 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5158 | /* Two masks for fast checking of whether a C 'long' may contain |
| 5159 | UTF16-encoded surrogate characters. This is an efficient heuristic, |
| 5160 | assuming that non-surrogate characters with a code point >= 0x8000 are |
| 5161 | rare in most input. |
| 5162 | FAST_CHAR_MASK is used when the input is in native byte ordering, |
| 5163 | SWAPPED_FAST_CHAR_MASK when the input is in byteswapped ordering. |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5164 | */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5165 | #if (SIZEOF_LONG == 8) |
| 5166 | # define FAST_CHAR_MASK 0x8000800080008000L |
| 5167 | # define SWAPPED_FAST_CHAR_MASK 0x0080008000800080L |
| 5168 | #elif (SIZEOF_LONG == 4) |
| 5169 | # define FAST_CHAR_MASK 0x80008000L |
| 5170 | # define SWAPPED_FAST_CHAR_MASK 0x00800080L |
| 5171 | #else |
| 5172 | # error C 'long' size should be either 4 or 8! |
| 5173 | #endif |
| 5174 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5175 | PyObject * |
| 5176 | PyUnicode_DecodeUTF16Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5177 | Py_ssize_t size, |
| 5178 | const char *errors, |
| 5179 | int *byteorder, |
| 5180 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5181 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5182 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5183 | Py_ssize_t startinpos; |
| 5184 | Py_ssize_t endinpos; |
| 5185 | Py_ssize_t outpos; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5186 | PyObject *unicode; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5187 | Py_UNICODE *p; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5188 | const unsigned char *q, *e, *aligned_end; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5189 | int bo = 0; /* assume native ordering by default */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5190 | int native_ordering = 0; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 5191 | const char *errmsg = ""; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5192 | /* Offsets from q for retrieving byte pairs in the right order. */ |
| 5193 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5194 | int ihi = 1, ilo = 0; |
| 5195 | #else |
| 5196 | int ihi = 0, ilo = 1; |
| 5197 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5198 | PyObject *errorHandler = NULL; |
| 5199 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5200 | |
| 5201 | /* Note: size will always be longer than the resulting Unicode |
| 5202 | character count */ |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5203 | unicode = (PyObject*)_PyUnicode_New(size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5204 | if (!unicode) |
| 5205 | return NULL; |
| 5206 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5207 | return unicode; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5208 | |
| 5209 | /* Unpack UTF-16 encoded data */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5210 | p = PyUnicode_AS_UNICODE(unicode); |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5211 | q = (unsigned char *)s; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5212 | e = q + size - 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5213 | |
| 5214 | if (byteorder) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5215 | bo = *byteorder; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5216 | |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5217 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 5218 | byte order setting accordingly. In native mode, the leading BOM |
| 5219 | mark is skipped, in all other modes, it is copied to the output |
| 5220 | stream as-is (giving a ZWNBSP character). */ |
| 5221 | if (bo == 0) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5222 | if (size >= 2) { |
| 5223 | const Py_UNICODE bom = (q[ihi] << 8) | q[ilo]; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5224 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5225 | if (bom == 0xFEFF) { |
| 5226 | q += 2; |
| 5227 | bo = -1; |
| 5228 | } |
| 5229 | else if (bom == 0xFFFE) { |
| 5230 | q += 2; |
| 5231 | bo = 1; |
| 5232 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5233 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5234 | if (bom == 0xFEFF) { |
| 5235 | q += 2; |
| 5236 | bo = 1; |
| 5237 | } |
| 5238 | else if (bom == 0xFFFE) { |
| 5239 | q += 2; |
| 5240 | bo = -1; |
| 5241 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5242 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5243 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5244 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5245 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5246 | if (bo == -1) { |
| 5247 | /* force LE */ |
| 5248 | ihi = 1; |
| 5249 | ilo = 0; |
| 5250 | } |
| 5251 | else if (bo == 1) { |
| 5252 | /* force BE */ |
| 5253 | ihi = 0; |
| 5254 | ilo = 1; |
| 5255 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5256 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5257 | native_ordering = ilo < ihi; |
| 5258 | #else |
| 5259 | native_ordering = ilo > ihi; |
| 5260 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5261 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5262 | aligned_end = (const unsigned char *) ((size_t) e & ~LONG_PTR_MASK); |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5263 | while (q < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5264 | Py_UNICODE ch; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5265 | /* First check for possible aligned read of a C 'long'. Unaligned |
| 5266 | reads are more expensive, better to defer to another iteration. */ |
| 5267 | if (!((size_t) q & LONG_PTR_MASK)) { |
| 5268 | /* Fast path for runs of non-surrogate chars. */ |
| 5269 | register const unsigned char *_q = q; |
| 5270 | Py_UNICODE *_p = p; |
| 5271 | if (native_ordering) { |
| 5272 | /* Native ordering is simple: as long as the input cannot |
| 5273 | possibly contain a surrogate char, do an unrolled copy |
| 5274 | of several 16-bit code points to the target object. |
| 5275 | The non-surrogate check is done on several input bytes |
| 5276 | at a time (as many as a C 'long' can contain). */ |
| 5277 | while (_q < aligned_end) { |
| 5278 | unsigned long data = * (unsigned long *) _q; |
| 5279 | if (data & FAST_CHAR_MASK) |
| 5280 | break; |
| 5281 | _p[0] = ((unsigned short *) _q)[0]; |
| 5282 | _p[1] = ((unsigned short *) _q)[1]; |
| 5283 | #if (SIZEOF_LONG == 8) |
| 5284 | _p[2] = ((unsigned short *) _q)[2]; |
| 5285 | _p[3] = ((unsigned short *) _q)[3]; |
| 5286 | #endif |
| 5287 | _q += SIZEOF_LONG; |
| 5288 | _p += SIZEOF_LONG / 2; |
| 5289 | } |
| 5290 | } |
| 5291 | else { |
| 5292 | /* Byteswapped ordering is similar, but we must decompose |
| 5293 | the copy bytewise, and take care of zero'ing out the |
| 5294 | upper bytes if the target object is in 32-bit units |
| 5295 | (that is, in UCS-4 builds). */ |
| 5296 | while (_q < aligned_end) { |
| 5297 | unsigned long data = * (unsigned long *) _q; |
| 5298 | if (data & SWAPPED_FAST_CHAR_MASK) |
| 5299 | break; |
| 5300 | /* Zero upper bytes in UCS-4 builds */ |
| 5301 | #if (Py_UNICODE_SIZE > 2) |
| 5302 | _p[0] = 0; |
| 5303 | _p[1] = 0; |
| 5304 | #if (SIZEOF_LONG == 8) |
| 5305 | _p[2] = 0; |
| 5306 | _p[3] = 0; |
| 5307 | #endif |
| 5308 | #endif |
Antoine Pitrou | d6e8de1 | 2009-01-11 23:56:55 +0000 | [diff] [blame] | 5309 | /* Issue #4916; UCS-4 builds on big endian machines must |
| 5310 | fill the two last bytes of each 4-byte unit. */ |
| 5311 | #if (!defined(BYTEORDER_IS_LITTLE_ENDIAN) && Py_UNICODE_SIZE > 2) |
| 5312 | # define OFF 2 |
| 5313 | #else |
| 5314 | # define OFF 0 |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5315 | #endif |
Antoine Pitrou | d6e8de1 | 2009-01-11 23:56:55 +0000 | [diff] [blame] | 5316 | ((unsigned char *) _p)[OFF + 1] = _q[0]; |
| 5317 | ((unsigned char *) _p)[OFF + 0] = _q[1]; |
| 5318 | ((unsigned char *) _p)[OFF + 1 + Py_UNICODE_SIZE] = _q[2]; |
| 5319 | ((unsigned char *) _p)[OFF + 0 + Py_UNICODE_SIZE] = _q[3]; |
| 5320 | #if (SIZEOF_LONG == 8) |
| 5321 | ((unsigned char *) _p)[OFF + 1 + 2 * Py_UNICODE_SIZE] = _q[4]; |
| 5322 | ((unsigned char *) _p)[OFF + 0 + 2 * Py_UNICODE_SIZE] = _q[5]; |
| 5323 | ((unsigned char *) _p)[OFF + 1 + 3 * Py_UNICODE_SIZE] = _q[6]; |
| 5324 | ((unsigned char *) _p)[OFF + 0 + 3 * Py_UNICODE_SIZE] = _q[7]; |
| 5325 | #endif |
| 5326 | #undef OFF |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5327 | _q += SIZEOF_LONG; |
| 5328 | _p += SIZEOF_LONG / 2; |
| 5329 | } |
| 5330 | } |
| 5331 | p = _p; |
| 5332 | q = _q; |
| 5333 | if (q >= e) |
| 5334 | break; |
| 5335 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5336 | ch = (q[ihi] << 8) | q[ilo]; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5337 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5338 | q += 2; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5339 | |
| 5340 | if (ch < 0xD800 || ch > 0xDFFF) { |
| 5341 | *p++ = ch; |
| 5342 | continue; |
| 5343 | } |
| 5344 | |
| 5345 | /* UTF-16 code pair: */ |
| 5346 | if (q > e) { |
| 5347 | errmsg = "unexpected end of data"; |
| 5348 | startinpos = (((const char *)q) - 2) - starts; |
| 5349 | endinpos = ((const char *)e) + 1 - starts; |
| 5350 | goto utf16Error; |
| 5351 | } |
| 5352 | if (0xD800 <= ch && ch <= 0xDBFF) { |
| 5353 | Py_UNICODE ch2 = (q[ihi] << 8) | q[ilo]; |
| 5354 | q += 2; |
| 5355 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 5356 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5357 | *p++ = ch; |
| 5358 | *p++ = ch2; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5359 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5360 | *p++ = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5361 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5362 | continue; |
| 5363 | } |
| 5364 | else { |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5365 | errmsg = "illegal UTF-16 surrogate"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5366 | startinpos = (((const char *)q)-4)-starts; |
| 5367 | endinpos = startinpos+2; |
| 5368 | goto utf16Error; |
| 5369 | } |
| 5370 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5371 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5372 | errmsg = "illegal encoding"; |
| 5373 | startinpos = (((const char *)q)-2)-starts; |
| 5374 | endinpos = startinpos+2; |
| 5375 | /* Fall through to report the error */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5376 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5377 | utf16Error: |
| 5378 | outpos = p - PyUnicode_AS_UNICODE(unicode); |
| 5379 | if (unicode_decode_call_errorhandler( |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5380 | errors, |
| 5381 | &errorHandler, |
| 5382 | "utf16", errmsg, |
| 5383 | &starts, |
| 5384 | (const char **)&e, |
| 5385 | &startinpos, |
| 5386 | &endinpos, |
| 5387 | &exc, |
| 5388 | (const char **)&q, |
| 5389 | &unicode, |
| 5390 | &outpos, |
| 5391 | &p)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5392 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5393 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5394 | /* remaining byte at the end? (size should be even) */ |
| 5395 | if (e == q) { |
| 5396 | if (!consumed) { |
| 5397 | errmsg = "truncated data"; |
| 5398 | startinpos = ((const char *)q) - starts; |
| 5399 | endinpos = ((const char *)e) + 1 - starts; |
| 5400 | outpos = p - PyUnicode_AS_UNICODE(unicode); |
| 5401 | if (unicode_decode_call_errorhandler( |
| 5402 | errors, |
| 5403 | &errorHandler, |
| 5404 | "utf16", errmsg, |
| 5405 | &starts, |
| 5406 | (const char **)&e, |
| 5407 | &startinpos, |
| 5408 | &endinpos, |
| 5409 | &exc, |
| 5410 | (const char **)&q, |
| 5411 | &unicode, |
| 5412 | &outpos, |
| 5413 | &p)) |
| 5414 | goto onError; |
| 5415 | /* The remaining input chars are ignored if the callback |
| 5416 | chooses to skip the input */ |
| 5417 | } |
| 5418 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5419 | |
| 5420 | if (byteorder) |
| 5421 | *byteorder = bo; |
| 5422 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5423 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5424 | *consumed = (const char *)q-starts; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5425 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5426 | /* Adjust length */ |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5427 | if (PyUnicode_Resize(&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5428 | goto onError; |
| 5429 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5430 | Py_XDECREF(errorHandler); |
| 5431 | Py_XDECREF(exc); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 5432 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 5433 | if (_PyUnicode_READY_REPLACE(&unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5434 | Py_DECREF(unicode); |
| 5435 | return NULL; |
| 5436 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 5437 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 5438 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5439 | return unicode; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5440 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5441 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5442 | Py_DECREF(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5443 | Py_XDECREF(errorHandler); |
| 5444 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5445 | return NULL; |
| 5446 | } |
| 5447 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5448 | #undef FAST_CHAR_MASK |
| 5449 | #undef SWAPPED_FAST_CHAR_MASK |
| 5450 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5451 | PyObject * |
| 5452 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5453 | Py_ssize_t size, |
| 5454 | const char *errors, |
| 5455 | int byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5456 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5457 | PyObject *v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5458 | unsigned char *p; |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5459 | Py_ssize_t nsize, bytesize; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 5460 | #ifdef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5461 | Py_ssize_t i, pairs; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 5462 | #else |
| 5463 | const int pairs = 0; |
| 5464 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5465 | /* Offsets from p for storing byte pairs in the right order. */ |
| 5466 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5467 | int ihi = 1, ilo = 0; |
| 5468 | #else |
| 5469 | int ihi = 0, ilo = 1; |
| 5470 | #endif |
| 5471 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5472 | #define STORECHAR(CH) \ |
| 5473 | do { \ |
| 5474 | p[ihi] = ((CH) >> 8) & 0xff; \ |
| 5475 | p[ilo] = (CH) & 0xff; \ |
| 5476 | p += 2; \ |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5477 | } while(0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5478 | |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 5479 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5480 | for (i = pairs = 0; i < size; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5481 | if (s[i] >= 0x10000) |
| 5482 | pairs++; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 5483 | #endif |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5484 | /* 2 * (size + pairs + (byteorder == 0)) */ |
| 5485 | if (size > PY_SSIZE_T_MAX || |
| 5486 | size > PY_SSIZE_T_MAX - pairs - (byteorder == 0)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5487 | return PyErr_NoMemory(); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5488 | nsize = size + pairs + (byteorder == 0); |
| 5489 | bytesize = nsize * 2; |
| 5490 | if (bytesize / 2 != nsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5491 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5492 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5493 | if (v == NULL) |
| 5494 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5495 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5496 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5497 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5498 | STORECHAR(0xFEFF); |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 5499 | if (size == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5500 | goto done; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5501 | |
| 5502 | if (byteorder == -1) { |
| 5503 | /* force LE */ |
| 5504 | ihi = 1; |
| 5505 | ilo = 0; |
| 5506 | } |
| 5507 | else if (byteorder == 1) { |
| 5508 | /* force BE */ |
| 5509 | ihi = 0; |
| 5510 | ilo = 1; |
| 5511 | } |
| 5512 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5513 | while (size-- > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5514 | Py_UNICODE ch = *s++; |
| 5515 | Py_UNICODE ch2 = 0; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 5516 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5517 | if (ch >= 0x10000) { |
| 5518 | ch2 = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 5519 | ch = 0xD800 | ((ch-0x10000) >> 10); |
| 5520 | } |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 5521 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5522 | STORECHAR(ch); |
| 5523 | if (ch2) |
| 5524 | STORECHAR(ch2); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5525 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5526 | |
| 5527 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5528 | return v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5529 | #undef STORECHAR |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5530 | } |
| 5531 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5532 | PyObject * |
| 5533 | PyUnicode_AsUTF16String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5534 | { |
| 5535 | if (!PyUnicode_Check(unicode)) { |
| 5536 | PyErr_BadArgument(); |
| 5537 | return NULL; |
| 5538 | } |
| 5539 | return PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5540 | PyUnicode_GET_SIZE(unicode), |
| 5541 | NULL, |
| 5542 | 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5543 | } |
| 5544 | |
| 5545 | /* --- Unicode Escape Codec ----------------------------------------------- */ |
| 5546 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5547 | /* Helper function for PyUnicode_DecodeUnicodeEscape, determines |
| 5548 | if all the escapes in the string make it still a valid ASCII string. |
| 5549 | Returns -1 if any escapes were found which cause the string to |
| 5550 | pop out of ASCII range. Otherwise returns the length of the |
| 5551 | required buffer to hold the string. |
| 5552 | */ |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 5553 | static Py_ssize_t |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5554 | length_of_escaped_ascii_string(const char *s, Py_ssize_t size) |
| 5555 | { |
| 5556 | const unsigned char *p = (const unsigned char *)s; |
| 5557 | const unsigned char *end = p + size; |
| 5558 | Py_ssize_t length = 0; |
| 5559 | |
| 5560 | if (size < 0) |
| 5561 | return -1; |
| 5562 | |
| 5563 | for (; p < end; ++p) { |
| 5564 | if (*p > 127) { |
| 5565 | /* Non-ASCII */ |
| 5566 | return -1; |
| 5567 | } |
| 5568 | else if (*p != '\\') { |
| 5569 | /* Normal character */ |
| 5570 | ++length; |
| 5571 | } |
| 5572 | else { |
| 5573 | /* Backslash-escape, check next char */ |
| 5574 | ++p; |
| 5575 | /* Escape sequence reaches till end of string or |
| 5576 | non-ASCII follow-up. */ |
| 5577 | if (p >= end || *p > 127) |
| 5578 | return -1; |
| 5579 | switch (*p) { |
| 5580 | case '\n': |
| 5581 | /* backslash + \n result in zero characters */ |
| 5582 | break; |
| 5583 | case '\\': case '\'': case '\"': |
| 5584 | case 'b': case 'f': case 't': |
| 5585 | case 'n': case 'r': case 'v': case 'a': |
| 5586 | ++length; |
| 5587 | break; |
| 5588 | case '0': case '1': case '2': case '3': |
| 5589 | case '4': case '5': case '6': case '7': |
| 5590 | case 'x': case 'u': case 'U': case 'N': |
| 5591 | /* these do not guarantee ASCII characters */ |
| 5592 | return -1; |
| 5593 | default: |
| 5594 | /* count the backslash + the other character */ |
| 5595 | length += 2; |
| 5596 | } |
| 5597 | } |
| 5598 | } |
| 5599 | return length; |
| 5600 | } |
| 5601 | |
| 5602 | /* Similar to PyUnicode_WRITE but either write into wstr field |
| 5603 | or treat string as ASCII. */ |
| 5604 | #define WRITE_ASCII_OR_WSTR(kind, buf, index, value) \ |
| 5605 | do { \ |
| 5606 | if ((kind) != PyUnicode_WCHAR_KIND) \ |
| 5607 | ((unsigned char *)(buf))[(index)] = (unsigned char)(value); \ |
| 5608 | else \ |
| 5609 | ((Py_UNICODE *)(buf))[(index)] = (Py_UNICODE)(value); \ |
| 5610 | } while (0) |
| 5611 | |
| 5612 | #define WRITE_WSTR(buf, index, value) \ |
| 5613 | assert(kind == PyUnicode_WCHAR_KIND), \ |
| 5614 | ((Py_UNICODE *)(buf))[(index)] = (Py_UNICODE)(value) |
| 5615 | |
| 5616 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 5617 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; |
Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 5618 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5619 | PyObject * |
| 5620 | PyUnicode_DecodeUnicodeEscape(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5621 | Py_ssize_t size, |
Victor Stinner | c17f540 | 2011-09-29 00:16:58 +0200 | [diff] [blame] | 5622 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5623 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5624 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5625 | Py_ssize_t startinpos; |
| 5626 | Py_ssize_t endinpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5627 | int j; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5628 | PyObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5629 | Py_UNICODE *p; |
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 | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5635 | Py_ssize_t ascii_length; |
| 5636 | Py_ssize_t i; |
| 5637 | int kind; |
| 5638 | void *data; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5639 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5640 | ascii_length = length_of_escaped_ascii_string(s, size); |
| 5641 | |
| 5642 | /* After length_of_escaped_ascii_string() there are two alternatives, |
| 5643 | either the string is pure ASCII with named escapes like \n, etc. |
| 5644 | and we determined it's exact size (common case) |
| 5645 | or it contains \x, \u, ... escape sequences. then we create a |
| 5646 | legacy wchar string and resize it at the end of this function. */ |
| 5647 | if (ascii_length >= 0) { |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5648 | v = PyUnicode_New(ascii_length, 127); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5649 | if (!v) |
| 5650 | goto onError; |
| 5651 | assert(PyUnicode_KIND(v) == PyUnicode_1BYTE_KIND); |
| 5652 | kind = PyUnicode_1BYTE_KIND; |
| 5653 | data = PyUnicode_DATA(v); |
| 5654 | } |
| 5655 | else { |
| 5656 | /* Escaped strings will always be longer than the resulting |
| 5657 | Unicode string, so we start with size here and then reduce the |
| 5658 | length after conversion to the true value. |
| 5659 | (but if the error callback returns a long replacement string |
| 5660 | we'll have to allocate more space) */ |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5661 | v = (PyObject*)_PyUnicode_New(size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5662 | if (!v) |
| 5663 | goto onError; |
| 5664 | kind = PyUnicode_WCHAR_KIND; |
| 5665 | data = PyUnicode_AS_UNICODE(v); |
| 5666 | } |
| 5667 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5668 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5669 | return v; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5670 | i = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5671 | end = s + size; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5672 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5673 | while (s < end) { |
| 5674 | unsigned char c; |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 5675 | Py_UNICODE x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5676 | int digits; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5677 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5678 | if (kind == PyUnicode_WCHAR_KIND) { |
| 5679 | assert(i < _PyUnicode_WSTR_LENGTH(v)); |
| 5680 | } |
| 5681 | else { |
| 5682 | /* The only case in which i == ascii_length is a backslash |
| 5683 | followed by a newline. */ |
| 5684 | assert(i <= ascii_length); |
| 5685 | } |
| 5686 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5687 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 5688 | if (*s != '\\') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5689 | WRITE_ASCII_OR_WSTR(kind, data, i++, (unsigned char) *s++); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5690 | continue; |
| 5691 | } |
| 5692 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5693 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5694 | /* \ - Escapes */ |
| 5695 | s++; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5696 | c = *s++; |
| 5697 | if (s > end) |
| 5698 | c = '\0'; /* Invalid after \ */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5699 | |
| 5700 | if (kind == PyUnicode_WCHAR_KIND) { |
| 5701 | assert(i < _PyUnicode_WSTR_LENGTH(v)); |
| 5702 | } |
| 5703 | else { |
| 5704 | /* The only case in which i == ascii_length is a backslash |
| 5705 | followed by a newline. */ |
| 5706 | assert(i < ascii_length || (i == ascii_length && c == '\n')); |
| 5707 | } |
| 5708 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5709 | switch (c) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5710 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5711 | /* \x escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5712 | case '\n': break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5713 | case '\\': WRITE_ASCII_OR_WSTR(kind, data, i++, '\\'); break; |
| 5714 | case '\'': WRITE_ASCII_OR_WSTR(kind, data, i++, '\''); break; |
| 5715 | case '\"': WRITE_ASCII_OR_WSTR(kind, data, i++, '\"'); break; |
| 5716 | case 'b': WRITE_ASCII_OR_WSTR(kind, data, i++, '\b'); break; |
| 5717 | /* FF */ |
| 5718 | case 'f': WRITE_ASCII_OR_WSTR(kind, data, i++, '\014'); break; |
| 5719 | case 't': WRITE_ASCII_OR_WSTR(kind, data, i++, '\t'); break; |
| 5720 | case 'n': WRITE_ASCII_OR_WSTR(kind, data, i++, '\n'); break; |
| 5721 | case 'r': WRITE_ASCII_OR_WSTR(kind, data, i++, '\r'); break; |
| 5722 | /* VT */ |
| 5723 | case 'v': WRITE_ASCII_OR_WSTR(kind, data, i++, '\013'); break; |
| 5724 | /* BEL, not classic C */ |
| 5725 | case 'a': WRITE_ASCII_OR_WSTR(kind, data, i++, '\007'); break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5726 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5727 | /* \OOO (octal) escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5728 | case '0': case '1': case '2': case '3': |
| 5729 | case '4': case '5': case '6': case '7': |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5730 | x = s[-1] - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5731 | if (s < end && '0' <= *s && *s <= '7') { |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5732 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5733 | if (s < end && '0' <= *s && *s <= '7') |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5734 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5735 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5736 | WRITE_WSTR(data, i++, x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5737 | break; |
| 5738 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5739 | /* hex escapes */ |
| 5740 | /* \xXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5741 | case 'x': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5742 | digits = 2; |
| 5743 | message = "truncated \\xXX escape"; |
| 5744 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5745 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5746 | /* \uXXXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5747 | case 'u': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5748 | digits = 4; |
| 5749 | message = "truncated \\uXXXX escape"; |
| 5750 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5751 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5752 | /* \UXXXXXXXX */ |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5753 | case 'U': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5754 | digits = 8; |
| 5755 | message = "truncated \\UXXXXXXXX escape"; |
| 5756 | hexescape: |
| 5757 | chr = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5758 | p = PyUnicode_AS_UNICODE(v) + i; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5759 | if (s+digits>end) { |
| 5760 | endinpos = size; |
| 5761 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5762 | errors, &errorHandler, |
| 5763 | "unicodeescape", "end of string in escape sequence", |
| 5764 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5765 | &v, &i, &p)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5766 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5767 | data = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5768 | goto nextByte; |
| 5769 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5770 | for (j = 0; j < digits; ++j) { |
| 5771 | c = (unsigned char) s[j]; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 5772 | if (!Py_ISXDIGIT(c)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5773 | endinpos = (s+j+1)-starts; |
| 5774 | p = PyUnicode_AS_UNICODE(v) + i; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5775 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5776 | errors, &errorHandler, |
| 5777 | "unicodeescape", message, |
| 5778 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5779 | &v, &i, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5780 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5781 | data = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5782 | goto nextByte; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5783 | } |
| 5784 | chr = (chr<<4) & ~0xF; |
| 5785 | if (c >= '0' && c <= '9') |
| 5786 | chr += c - '0'; |
| 5787 | else if (c >= 'a' && c <= 'f') |
| 5788 | chr += 10 + c - 'a'; |
| 5789 | else |
| 5790 | chr += 10 + c - 'A'; |
| 5791 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5792 | s += j; |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 5793 | if (chr == 0xffffffff && PyErr_Occurred()) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5794 | /* _decoding_error will have already written into the |
| 5795 | target buffer. */ |
| 5796 | break; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5797 | store: |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5798 | /* when we get here, chr is a 32-bit unicode character */ |
| 5799 | if (chr <= 0xffff) |
| 5800 | /* UCS-2 character */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5801 | WRITE_WSTR(data, i++, chr); |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5802 | else if (chr <= 0x10ffff) { |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 5803 | /* UCS-4 character. Either store directly, or as |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5804 | surrogate pair. */ |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 5805 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5806 | WRITE_WSTR(data, i++, chr); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5807 | #else |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5808 | chr -= 0x10000L; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5809 | WRITE_WSTR(data, i++, 0xD800 + (Py_UNICODE) (chr >> 10)); |
| 5810 | WRITE_WSTR(data, i++, 0xDC00 + (Py_UNICODE) (chr & 0x03FF)); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5811 | #endif |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5812 | } else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5813 | endinpos = s-starts; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5814 | p = PyUnicode_AS_UNICODE(v) + i; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5815 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5816 | errors, &errorHandler, |
| 5817 | "unicodeescape", "illegal Unicode character", |
| 5818 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5819 | &v, &i, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5820 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5821 | data = PyUnicode_AS_UNICODE(v); |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5822 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5823 | break; |
| 5824 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5825 | /* \N{name} */ |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5826 | case 'N': |
| 5827 | message = "malformed \\N character escape"; |
| 5828 | if (ucnhash_CAPI == NULL) { |
| 5829 | /* load the unicode data module */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5830 | ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCapsule_Import( |
| 5831 | PyUnicodeData_CAPSULE_NAME, 1); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5832 | if (ucnhash_CAPI == NULL) |
| 5833 | goto ucnhashError; |
| 5834 | } |
| 5835 | if (*s == '{') { |
| 5836 | const char *start = s+1; |
| 5837 | /* look for the closing brace */ |
| 5838 | while (*s != '}' && s < end) |
| 5839 | s++; |
| 5840 | if (s > start && s < end && *s == '}') { |
| 5841 | /* found a name. look it up in the unicode database */ |
| 5842 | message = "unknown Unicode character name"; |
| 5843 | s++; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5844 | if (ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), |
Ezio Melotti | 931b8aa | 2011-10-21 21:57:36 +0300 | [diff] [blame] | 5845 | &chr, 0)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5846 | goto store; |
| 5847 | } |
| 5848 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5849 | endinpos = s-starts; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5850 | p = PyUnicode_AS_UNICODE(v) + i; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5851 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5852 | errors, &errorHandler, |
| 5853 | "unicodeescape", message, |
| 5854 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5855 | &v, &i, &p)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5856 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5857 | data = PyUnicode_AS_UNICODE(v); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5858 | break; |
| 5859 | |
| 5860 | default: |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5861 | if (s > end) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5862 | assert(kind == PyUnicode_WCHAR_KIND); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5863 | message = "\\ at end of string"; |
| 5864 | s--; |
| 5865 | endinpos = s-starts; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5866 | p = PyUnicode_AS_UNICODE(v) + i; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5867 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5868 | errors, &errorHandler, |
| 5869 | "unicodeescape", message, |
| 5870 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5871 | &v, &i, &p)) |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5872 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5873 | data = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5874 | } |
| 5875 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5876 | WRITE_ASCII_OR_WSTR(kind, data, i++, '\\'); |
| 5877 | WRITE_ASCII_OR_WSTR(kind, data, i++, (unsigned char)s[-1]); |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5878 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5879 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5880 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5881 | nextByte: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5882 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5883 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5884 | /* Ensure the length prediction worked in case of ASCII strings */ |
| 5885 | assert(kind == PyUnicode_WCHAR_KIND || i == ascii_length); |
| 5886 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 5887 | if (kind == PyUnicode_WCHAR_KIND) |
| 5888 | { |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5889 | if (PyUnicode_Resize(&v, i) < 0) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 5890 | goto onError; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 5891 | } |
Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 5892 | Py_XDECREF(errorHandler); |
| 5893 | Py_XDECREF(exc); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 5894 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 5895 | if (_PyUnicode_READY_REPLACE(&v)) { |
| 5896 | Py_DECREF(v); |
| 5897 | return NULL; |
| 5898 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 5899 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 5900 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5901 | return v; |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5902 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5903 | ucnhashError: |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 5904 | PyErr_SetString( |
| 5905 | PyExc_UnicodeError, |
| 5906 | "\\N escapes not supported (can't load unicodedata module)" |
| 5907 | ); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 5908 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5909 | Py_XDECREF(errorHandler); |
| 5910 | Py_XDECREF(exc); |
Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 5911 | return NULL; |
| 5912 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5913 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5914 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5915 | Py_XDECREF(errorHandler); |
| 5916 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5917 | return NULL; |
| 5918 | } |
| 5919 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5920 | #undef WRITE_ASCII_OR_WSTR |
| 5921 | #undef WRITE_WSTR |
| 5922 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5923 | /* Return a Unicode-Escape string version of the Unicode object. |
| 5924 | |
| 5925 | If quotes is true, the string is enclosed in u"" or u'' quotes as |
| 5926 | appropriate. |
| 5927 | |
| 5928 | */ |
| 5929 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5930 | PyObject * |
| 5931 | PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5932 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5933 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5934 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5935 | char *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5936 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5937 | #ifdef Py_UNICODE_WIDE |
| 5938 | const Py_ssize_t expandsize = 10; |
| 5939 | #else |
| 5940 | const Py_ssize_t expandsize = 6; |
| 5941 | #endif |
| 5942 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5943 | /* XXX(nnorwitz): rather than over-allocating, it would be |
| 5944 | better to choose a different scheme. Perhaps scan the |
| 5945 | first N-chars of the string and allocate based on that size. |
| 5946 | */ |
| 5947 | /* Initial allocation is based on the longest-possible unichr |
| 5948 | escape. |
| 5949 | |
| 5950 | In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source |
| 5951 | unichr, so in this case it's the longest unichr escape. In |
| 5952 | narrow (UTF-16) builds this is five chars per source unichr |
| 5953 | since there are two unichrs in the surrogate pair, so in narrow |
| 5954 | (UTF-16) builds it's not the longest unichr escape. |
| 5955 | |
| 5956 | In wide or narrow builds '\uxxxx' is 6 chars per source unichr, |
| 5957 | so in the narrow (UTF-16) build case it's the longest unichr |
| 5958 | escape. |
| 5959 | */ |
| 5960 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5961 | if (size == 0) |
| 5962 | return PyBytes_FromStringAndSize(NULL, 0); |
| 5963 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5964 | if (size > (PY_SSIZE_T_MAX - 2 - 1) / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5965 | return PyErr_NoMemory(); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5966 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5967 | repr = PyBytes_FromStringAndSize(NULL, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5968 | 2 |
| 5969 | + expandsize*size |
| 5970 | + 1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5971 | if (repr == NULL) |
| 5972 | return NULL; |
| 5973 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5974 | p = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5975 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5976 | while (size-- > 0) { |
| 5977 | Py_UNICODE ch = *s++; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5978 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5979 | /* Escape backslashes */ |
| 5980 | if (ch == '\\') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5981 | *p++ = '\\'; |
| 5982 | *p++ = (char) ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5983 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5984 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5985 | |
Guido van Rossum | 0d42e0c | 2001-07-20 16:36:21 +0000 | [diff] [blame] | 5986 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5987 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 5988 | else if (ch >= 0x10000) { |
| 5989 | *p++ = '\\'; |
| 5990 | *p++ = 'U'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 5991 | *p++ = Py_hexdigits[(ch >> 28) & 0x0000000F]; |
| 5992 | *p++ = Py_hexdigits[(ch >> 24) & 0x0000000F]; |
| 5993 | *p++ = Py_hexdigits[(ch >> 20) & 0x0000000F]; |
| 5994 | *p++ = Py_hexdigits[(ch >> 16) & 0x0000000F]; |
| 5995 | *p++ = Py_hexdigits[(ch >> 12) & 0x0000000F]; |
| 5996 | *p++ = Py_hexdigits[(ch >> 8) & 0x0000000F]; |
| 5997 | *p++ = Py_hexdigits[(ch >> 4) & 0x0000000F]; |
| 5998 | *p++ = Py_hexdigits[ch & 0x0000000F]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5999 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 6000 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6001 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6002 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
| 6003 | else if (ch >= 0xD800 && ch < 0xDC00) { |
| 6004 | Py_UNICODE ch2; |
| 6005 | Py_UCS4 ucs; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6006 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6007 | ch2 = *s++; |
| 6008 | size--; |
Georg Brandl | 78eef3de | 2010-08-01 20:51:02 +0000 | [diff] [blame] | 6009 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6010 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 6011 | *p++ = '\\'; |
| 6012 | *p++ = 'U'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6013 | *p++ = Py_hexdigits[(ucs >> 28) & 0x0000000F]; |
| 6014 | *p++ = Py_hexdigits[(ucs >> 24) & 0x0000000F]; |
| 6015 | *p++ = Py_hexdigits[(ucs >> 20) & 0x0000000F]; |
| 6016 | *p++ = Py_hexdigits[(ucs >> 16) & 0x0000000F]; |
| 6017 | *p++ = Py_hexdigits[(ucs >> 12) & 0x0000000F]; |
| 6018 | *p++ = Py_hexdigits[(ucs >> 8) & 0x0000000F]; |
| 6019 | *p++ = Py_hexdigits[(ucs >> 4) & 0x0000000F]; |
| 6020 | *p++ = Py_hexdigits[ucs & 0x0000000F]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6021 | continue; |
| 6022 | } |
| 6023 | /* Fall through: isolated surrogates are copied as-is */ |
| 6024 | s--; |
| 6025 | size++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6026 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6027 | #endif |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 6028 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6029 | /* Map 16-bit characters to '\uxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 6030 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6031 | *p++ = '\\'; |
| 6032 | *p++ = 'u'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6033 | *p++ = Py_hexdigits[(ch >> 12) & 0x000F]; |
| 6034 | *p++ = Py_hexdigits[(ch >> 8) & 0x000F]; |
| 6035 | *p++ = Py_hexdigits[(ch >> 4) & 0x000F]; |
| 6036 | *p++ = Py_hexdigits[ch & 0x000F]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6037 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 6038 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 6039 | /* Map special whitespace to '\t', \n', '\r' */ |
| 6040 | else if (ch == '\t') { |
| 6041 | *p++ = '\\'; |
| 6042 | *p++ = 't'; |
| 6043 | } |
| 6044 | else if (ch == '\n') { |
| 6045 | *p++ = '\\'; |
| 6046 | *p++ = 'n'; |
| 6047 | } |
| 6048 | else if (ch == '\r') { |
| 6049 | *p++ = '\\'; |
| 6050 | *p++ = 'r'; |
| 6051 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 6052 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 6053 | /* Map non-printable US ASCII to '\xhh' */ |
Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 6054 | else if (ch < ' ' || ch >= 0x7F) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6055 | *p++ = '\\'; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 6056 | *p++ = 'x'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6057 | *p++ = Py_hexdigits[(ch >> 4) & 0x000F]; |
| 6058 | *p++ = Py_hexdigits[ch & 0x000F]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6059 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 6060 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6061 | /* Copy everything else as-is */ |
| 6062 | else |
| 6063 | *p++ = (char) ch; |
| 6064 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6065 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6066 | assert(p - PyBytes_AS_STRING(repr) > 0); |
| 6067 | if (_PyBytes_Resize(&repr, p - PyBytes_AS_STRING(repr)) < 0) |
| 6068 | return NULL; |
| 6069 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6070 | } |
| 6071 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6072 | PyObject * |
| 6073 | PyUnicode_AsUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6074 | { |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 6075 | PyObject *s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6076 | if (!PyUnicode_Check(unicode)) { |
| 6077 | PyErr_BadArgument(); |
| 6078 | return NULL; |
| 6079 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 6080 | s = PyUnicode_EncodeUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 6081 | PyUnicode_GET_SIZE(unicode)); |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 6082 | return s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6083 | } |
| 6084 | |
| 6085 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ |
| 6086 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6087 | PyObject * |
| 6088 | PyUnicode_DecodeRawUnicodeEscape(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6089 | Py_ssize_t size, |
| 6090 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6091 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6092 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6093 | Py_ssize_t startinpos; |
| 6094 | Py_ssize_t endinpos; |
| 6095 | Py_ssize_t outpos; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6096 | PyObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6097 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6098 | const char *end; |
| 6099 | const char *bs; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6100 | PyObject *errorHandler = NULL; |
| 6101 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6102 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6103 | /* Escaped strings will always be longer than the resulting |
| 6104 | 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] | 6105 | length after conversion to the true value. (But decoding error |
| 6106 | handler might have to resize the string) */ |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6107 | v = (PyObject*)_PyUnicode_New(size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6108 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6109 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6110 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6111 | return v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6112 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6113 | end = s + size; |
| 6114 | while (s < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6115 | unsigned char c; |
| 6116 | Py_UCS4 x; |
| 6117 | int i; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6118 | int count; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6119 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6120 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 6121 | if (*s != '\\') { |
| 6122 | *p++ = (unsigned char)*s++; |
| 6123 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6124 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6125 | startinpos = s-starts; |
| 6126 | |
| 6127 | /* \u-escapes are only interpreted iff the number of leading |
| 6128 | backslashes if odd */ |
| 6129 | bs = s; |
| 6130 | for (;s < end;) { |
| 6131 | if (*s != '\\') |
| 6132 | break; |
| 6133 | *p++ = (unsigned char)*s++; |
| 6134 | } |
| 6135 | if (((s - bs) & 1) == 0 || |
| 6136 | s >= end || |
| 6137 | (*s != 'u' && *s != 'U')) { |
| 6138 | continue; |
| 6139 | } |
| 6140 | p--; |
| 6141 | count = *s=='u' ? 4 : 8; |
| 6142 | s++; |
| 6143 | |
| 6144 | /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ |
| 6145 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 6146 | for (x = 0, i = 0; i < count; ++i, ++s) { |
| 6147 | c = (unsigned char)*s; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 6148 | if (!Py_ISXDIGIT(c)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6149 | endinpos = s-starts; |
| 6150 | if (unicode_decode_call_errorhandler( |
| 6151 | errors, &errorHandler, |
| 6152 | "rawunicodeescape", "truncated \\uXXXX", |
| 6153 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 6154 | &v, &outpos, &p)) |
| 6155 | goto onError; |
| 6156 | goto nextByte; |
| 6157 | } |
| 6158 | x = (x<<4) & ~0xF; |
| 6159 | if (c >= '0' && c <= '9') |
| 6160 | x += c - '0'; |
| 6161 | else if (c >= 'a' && c <= 'f') |
| 6162 | x += 10 + c - 'a'; |
| 6163 | else |
| 6164 | x += 10 + c - 'A'; |
| 6165 | } |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 6166 | if (x <= 0xffff) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6167 | /* UCS-2 character */ |
| 6168 | *p++ = (Py_UNICODE) x; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 6169 | else if (x <= 0x10ffff) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6170 | /* UCS-4 character. Either store directly, or as |
| 6171 | surrogate pair. */ |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 6172 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6173 | *p++ = (Py_UNICODE) x; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 6174 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6175 | x -= 0x10000L; |
| 6176 | *p++ = 0xD800 + (Py_UNICODE) (x >> 10); |
| 6177 | *p++ = 0xDC00 + (Py_UNICODE) (x & 0x03FF); |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 6178 | #endif |
| 6179 | } else { |
| 6180 | endinpos = s-starts; |
| 6181 | outpos = p-PyUnicode_AS_UNICODE(v); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6182 | if (unicode_decode_call_errorhandler( |
| 6183 | errors, &errorHandler, |
| 6184 | "rawunicodeescape", "\\Uxxxxxxxx out of range", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6185 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 6186 | &v, &outpos, &p)) |
| 6187 | goto onError; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6188 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6189 | nextByte: |
| 6190 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6191 | } |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6192 | if (PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6193 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6194 | Py_XDECREF(errorHandler); |
| 6195 | Py_XDECREF(exc); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 6196 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 6197 | if (_PyUnicode_READY_REPLACE(&v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6198 | Py_DECREF(v); |
| 6199 | return NULL; |
| 6200 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 6201 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 6202 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6203 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6204 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6205 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6206 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6207 | Py_XDECREF(errorHandler); |
| 6208 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6209 | return NULL; |
| 6210 | } |
| 6211 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6212 | PyObject * |
| 6213 | PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6214 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6215 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6216 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6217 | char *p; |
| 6218 | char *q; |
| 6219 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6220 | #ifdef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 6221 | const Py_ssize_t expandsize = 10; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6222 | #else |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 6223 | const Py_ssize_t expandsize = 6; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6224 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6225 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 6226 | if (size > PY_SSIZE_T_MAX / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6227 | return PyErr_NoMemory(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6228 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6229 | repr = PyBytes_FromStringAndSize(NULL, expandsize * size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6230 | if (repr == NULL) |
| 6231 | return NULL; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 6232 | if (size == 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6233 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6234 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6235 | p = q = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6236 | while (size-- > 0) { |
| 6237 | Py_UNICODE ch = *s++; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6238 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6239 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 6240 | if (ch >= 0x10000) { |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6241 | *p++ = '\\'; |
| 6242 | *p++ = 'U'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6243 | *p++ = Py_hexdigits[(ch >> 28) & 0xf]; |
| 6244 | *p++ = Py_hexdigits[(ch >> 24) & 0xf]; |
| 6245 | *p++ = Py_hexdigits[(ch >> 20) & 0xf]; |
| 6246 | *p++ = Py_hexdigits[(ch >> 16) & 0xf]; |
| 6247 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 6248 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 6249 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 6250 | *p++ = Py_hexdigits[ch & 15]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6251 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6252 | else |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 6253 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6254 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
| 6255 | if (ch >= 0xD800 && ch < 0xDC00) { |
| 6256 | Py_UNICODE ch2; |
| 6257 | Py_UCS4 ucs; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 6258 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6259 | ch2 = *s++; |
| 6260 | size--; |
Georg Brandl | 78eef3de | 2010-08-01 20:51:02 +0000 | [diff] [blame] | 6261 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6262 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 6263 | *p++ = '\\'; |
| 6264 | *p++ = 'U'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6265 | *p++ = Py_hexdigits[(ucs >> 28) & 0xf]; |
| 6266 | *p++ = Py_hexdigits[(ucs >> 24) & 0xf]; |
| 6267 | *p++ = Py_hexdigits[(ucs >> 20) & 0xf]; |
| 6268 | *p++ = Py_hexdigits[(ucs >> 16) & 0xf]; |
| 6269 | *p++ = Py_hexdigits[(ucs >> 12) & 0xf]; |
| 6270 | *p++ = Py_hexdigits[(ucs >> 8) & 0xf]; |
| 6271 | *p++ = Py_hexdigits[(ucs >> 4) & 0xf]; |
| 6272 | *p++ = Py_hexdigits[ucs & 0xf]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6273 | continue; |
| 6274 | } |
| 6275 | /* Fall through: isolated surrogates are copied as-is */ |
| 6276 | s--; |
| 6277 | size++; |
| 6278 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6279 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6280 | /* Map 16-bit characters to '\uxxxx' */ |
| 6281 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6282 | *p++ = '\\'; |
| 6283 | *p++ = 'u'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6284 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 6285 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 6286 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 6287 | *p++ = Py_hexdigits[ch & 15]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6288 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6289 | /* Copy everything else as-is */ |
| 6290 | else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6291 | *p++ = (char) ch; |
| 6292 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6293 | size = p - q; |
| 6294 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6295 | assert(size > 0); |
| 6296 | if (_PyBytes_Resize(&repr, size) < 0) |
| 6297 | return NULL; |
| 6298 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6299 | } |
| 6300 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6301 | PyObject * |
| 6302 | PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6303 | { |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 6304 | PyObject *s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6305 | if (!PyUnicode_Check(unicode)) { |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 6306 | PyErr_BadArgument(); |
| 6307 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6308 | } |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 6309 | s = PyUnicode_EncodeRawUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 6310 | PyUnicode_GET_SIZE(unicode)); |
| 6311 | |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 6312 | return s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6313 | } |
| 6314 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6315 | /* --- Unicode Internal Codec ------------------------------------------- */ |
| 6316 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6317 | PyObject * |
| 6318 | _PyUnicode_DecodeUnicodeInternal(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6319 | Py_ssize_t size, |
| 6320 | const char *errors) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6321 | { |
| 6322 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6323 | Py_ssize_t startinpos; |
| 6324 | Py_ssize_t endinpos; |
| 6325 | Py_ssize_t outpos; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6326 | PyObject *v; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6327 | Py_UNICODE *p; |
| 6328 | const char *end; |
| 6329 | const char *reason; |
| 6330 | PyObject *errorHandler = NULL; |
| 6331 | PyObject *exc = NULL; |
| 6332 | |
Neal Norwitz | d43069c | 2006-01-08 01:12:10 +0000 | [diff] [blame] | 6333 | #ifdef Py_UNICODE_WIDE |
| 6334 | Py_UNICODE unimax = PyUnicode_GetMax(); |
| 6335 | #endif |
| 6336 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6337 | /* XXX overflow detection missing */ |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6338 | v = (PyObject*)_PyUnicode_New((size+Py_UNICODE_SIZE-1)/ Py_UNICODE_SIZE); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6339 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6340 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6341 | /* Intentionally PyUnicode_GET_SIZE instead of PyUnicode_GET_LENGTH |
| 6342 | as string was created with the old API. */ |
| 6343 | if (PyUnicode_GET_SIZE(v) == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6344 | return v; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6345 | p = PyUnicode_AS_UNICODE(v); |
| 6346 | end = s + size; |
| 6347 | |
| 6348 | while (s < end) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6349 | memcpy(p, s, sizeof(Py_UNICODE)); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6350 | /* We have to sanity check the raw data, otherwise doom looms for |
| 6351 | some malformed UCS-4 data. */ |
| 6352 | if ( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6353 | #ifdef Py_UNICODE_WIDE |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6354 | *p > unimax || *p < 0 || |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6355 | #endif |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6356 | end-s < Py_UNICODE_SIZE |
| 6357 | ) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6358 | { |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6359 | startinpos = s - starts; |
| 6360 | if (end-s < Py_UNICODE_SIZE) { |
| 6361 | endinpos = end-starts; |
| 6362 | reason = "truncated input"; |
| 6363 | } |
| 6364 | else { |
| 6365 | endinpos = s - starts + Py_UNICODE_SIZE; |
| 6366 | reason = "illegal code point (> 0x10FFFF)"; |
| 6367 | } |
| 6368 | outpos = p - PyUnicode_AS_UNICODE(v); |
| 6369 | if (unicode_decode_call_errorhandler( |
| 6370 | errors, &errorHandler, |
| 6371 | "unicode_internal", reason, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 6372 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 6373 | &v, &outpos, &p)) { |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6374 | goto onError; |
| 6375 | } |
| 6376 | } |
| 6377 | else { |
| 6378 | p++; |
| 6379 | s += Py_UNICODE_SIZE; |
| 6380 | } |
| 6381 | } |
| 6382 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6383 | if (PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6384 | goto onError; |
| 6385 | Py_XDECREF(errorHandler); |
| 6386 | Py_XDECREF(exc); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 6387 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 6388 | if (_PyUnicode_READY_REPLACE(&v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6389 | Py_DECREF(v); |
| 6390 | return NULL; |
| 6391 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 6392 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 6393 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6394 | return v; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6395 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6396 | onError: |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6397 | Py_XDECREF(v); |
| 6398 | Py_XDECREF(errorHandler); |
| 6399 | Py_XDECREF(exc); |
| 6400 | return NULL; |
| 6401 | } |
| 6402 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6403 | /* --- Latin-1 Codec ------------------------------------------------------ */ |
| 6404 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6405 | PyObject * |
| 6406 | PyUnicode_DecodeLatin1(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6407 | Py_ssize_t size, |
| 6408 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6409 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6410 | /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 6411 | return _PyUnicode_FromUCS1((unsigned char*)s, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6412 | } |
| 6413 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6414 | /* create or adjust a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6415 | static void |
| 6416 | make_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6417 | const char *encoding, |
| 6418 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 6419 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6420 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6421 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6422 | if (*exceptionObject == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6423 | *exceptionObject = PyUnicodeEncodeError_Create( |
| 6424 | encoding, unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6425 | } |
| 6426 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6427 | if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 6428 | goto onError; |
| 6429 | if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) |
| 6430 | goto onError; |
| 6431 | if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) |
| 6432 | goto onError; |
| 6433 | return; |
| 6434 | onError: |
| 6435 | Py_DECREF(*exceptionObject); |
| 6436 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6437 | } |
| 6438 | } |
| 6439 | |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6440 | /* This is ultimately going t replace above function. */ |
| 6441 | static void |
| 6442 | make_encode_exception_obj(PyObject **exceptionObject, |
| 6443 | const char *encoding, |
| 6444 | PyObject *unicode, |
| 6445 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6446 | const char *reason) |
| 6447 | { |
| 6448 | if (*exceptionObject == NULL) { |
| 6449 | *exceptionObject = PyObject_CallFunction( |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6450 | PyExc_UnicodeEncodeError, "sOnns", |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6451 | encoding, unicode, startpos, endpos, reason); |
| 6452 | } |
| 6453 | else { |
| 6454 | if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 6455 | goto onError; |
| 6456 | if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) |
| 6457 | goto onError; |
| 6458 | if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) |
| 6459 | goto onError; |
| 6460 | return; |
| 6461 | onError: |
| 6462 | Py_DECREF(*exceptionObject); |
| 6463 | *exceptionObject = NULL; |
| 6464 | } |
| 6465 | } |
| 6466 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6467 | /* raises a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6468 | static void |
| 6469 | raise_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6470 | const char *encoding, |
| 6471 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 6472 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6473 | const char *reason) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6474 | { |
| 6475 | make_encode_exception(exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6476 | encoding, unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6477 | if (*exceptionObject != NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6478 | PyCodec_StrictErrors(*exceptionObject); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6479 | } |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6480 | /* This is ultimately going to replace above function. */ |
| 6481 | static void |
| 6482 | raise_encode_exception_obj(PyObject **exceptionObject, |
| 6483 | const char *encoding, |
| 6484 | PyObject *unicode, |
| 6485 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6486 | const char *reason) |
| 6487 | { |
| 6488 | make_encode_exception_obj(exceptionObject, |
| 6489 | encoding, unicode, startpos, endpos, reason); |
| 6490 | if (*exceptionObject != NULL) |
| 6491 | PyCodec_StrictErrors(*exceptionObject); |
| 6492 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6493 | |
| 6494 | /* error handling callback helper: |
| 6495 | build arguments, call the callback and check the arguments, |
| 6496 | put the result into newpos and return the replacement string, which |
| 6497 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6498 | static PyObject * |
| 6499 | unicode_encode_call_errorhandler(const char *errors, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6500 | PyObject **errorHandler, |
| 6501 | const char *encoding, const char *reason, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6502 | PyObject *unicode, PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6503 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6504 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6505 | { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6506 | 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] | 6507 | Py_ssize_t len; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6508 | PyObject *restuple; |
| 6509 | PyObject *resunicode; |
| 6510 | |
| 6511 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6512 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6513 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6514 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6515 | } |
| 6516 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6517 | if (PyUnicode_READY(unicode) < 0) |
| 6518 | return NULL; |
| 6519 | len = PyUnicode_GET_LENGTH(unicode); |
| 6520 | |
| 6521 | make_encode_exception_obj(exceptionObject, |
| 6522 | encoding, unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6523 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6524 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6525 | |
| 6526 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6527 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6528 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6529 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6530 | if (!PyTuple_Check(restuple)) { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6531 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6532 | Py_DECREF(restuple); |
| 6533 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6534 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6535 | if (!PyArg_ParseTuple(restuple, argparse, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6536 | &resunicode, newpos)) { |
| 6537 | Py_DECREF(restuple); |
| 6538 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6539 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6540 | if (!PyUnicode_Check(resunicode) && !PyBytes_Check(resunicode)) { |
| 6541 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
| 6542 | Py_DECREF(restuple); |
| 6543 | return NULL; |
| 6544 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6545 | if (*newpos<0) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6546 | *newpos = len + *newpos; |
| 6547 | if (*newpos<0 || *newpos>len) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6548 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 6549 | Py_DECREF(restuple); |
| 6550 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 6551 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6552 | Py_INCREF(resunicode); |
| 6553 | Py_DECREF(restuple); |
| 6554 | return resunicode; |
| 6555 | } |
| 6556 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6557 | static PyObject * |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6558 | unicode_encode_ucs1(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6559 | const char *errors, |
Victor Stinner | fcd9653 | 2011-11-04 00:28:50 +0100 | [diff] [blame^] | 6560 | unsigned int limit) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6561 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6562 | /* input state */ |
| 6563 | Py_ssize_t pos=0, size; |
| 6564 | int kind; |
| 6565 | void *data; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6566 | /* output object */ |
| 6567 | PyObject *res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6568 | /* pointer into the output */ |
| 6569 | char *str; |
| 6570 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6571 | Py_ssize_t ressize; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6572 | const char *encoding = (limit == 256) ? "latin-1" : "ascii"; |
| 6573 | 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] | 6574 | PyObject *errorHandler = NULL; |
| 6575 | PyObject *exc = NULL; |
| 6576 | /* the following variable is used for caching string comparisons |
| 6577 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 6578 | int known_errorHandler = -1; |
| 6579 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6580 | if (PyUnicode_READY(unicode) < 0) |
| 6581 | return NULL; |
| 6582 | size = PyUnicode_GET_LENGTH(unicode); |
| 6583 | kind = PyUnicode_KIND(unicode); |
| 6584 | data = PyUnicode_DATA(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6585 | /* allocate enough for a simple encoding without |
| 6586 | replacements, if we need more, we'll resize */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6587 | if (size == 0) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 6588 | return PyBytes_FromStringAndSize(NULL, 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6589 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6590 | if (res == NULL) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6591 | return NULL; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6592 | str = PyBytes_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6593 | ressize = size; |
| 6594 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6595 | while (pos < size) { |
| 6596 | Py_UCS4 c = PyUnicode_READ(kind, data, pos); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6597 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6598 | /* can we encode this? */ |
| 6599 | if (c<limit) { |
| 6600 | /* no overflow check, because we know that the space is enough */ |
| 6601 | *str++ = (char)c; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6602 | ++pos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6603 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6604 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6605 | Py_ssize_t requiredsize; |
| 6606 | PyObject *repunicode; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6607 | Py_ssize_t repsize, newpos, respos, i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6608 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6609 | Py_ssize_t collstart = pos; |
| 6610 | Py_ssize_t collend = pos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6611 | /* find all unecodable characters */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6612 | while ((collend < size) && (PyUnicode_READ(kind, data, collend)>=limit)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6613 | ++collend; |
| 6614 | /* cache callback name lookup (if not done yet, i.e. it's the first error) */ |
| 6615 | if (known_errorHandler==-1) { |
| 6616 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 6617 | known_errorHandler = 1; |
| 6618 | else if (!strcmp(errors, "replace")) |
| 6619 | known_errorHandler = 2; |
| 6620 | else if (!strcmp(errors, "ignore")) |
| 6621 | known_errorHandler = 3; |
| 6622 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 6623 | known_errorHandler = 4; |
| 6624 | else |
| 6625 | known_errorHandler = 0; |
| 6626 | } |
| 6627 | switch (known_errorHandler) { |
| 6628 | case 1: /* strict */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6629 | raise_encode_exception_obj(&exc, encoding, unicode, collstart, collend, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6630 | goto onError; |
| 6631 | case 2: /* replace */ |
| 6632 | while (collstart++<collend) |
| 6633 | *str++ = '?'; /* fall through */ |
| 6634 | case 3: /* ignore */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6635 | pos = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6636 | break; |
| 6637 | case 4: /* xmlcharrefreplace */ |
| 6638 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6639 | /* determine replacement size */ |
| 6640 | for (i = collstart, repsize = 0; i < collend; ++i) { |
| 6641 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 6642 | if (ch < 10) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6643 | repsize += 2+1+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6644 | else if (ch < 100) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6645 | repsize += 2+2+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6646 | else if (ch < 1000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6647 | repsize += 2+3+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6648 | else if (ch < 10000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6649 | repsize += 2+4+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 6650 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6651 | else |
| 6652 | repsize += 2+5+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 6653 | #else |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6654 | else if (ch < 100000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6655 | repsize += 2+5+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6656 | else if (ch < 1000000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6657 | repsize += 2+6+1; |
| 6658 | else |
| 6659 | repsize += 2+7+1; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 6660 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6661 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6662 | requiredsize = respos+repsize+(size-collend); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6663 | if (requiredsize > ressize) { |
| 6664 | if (requiredsize<2*ressize) |
| 6665 | requiredsize = 2*ressize; |
| 6666 | if (_PyBytes_Resize(&res, requiredsize)) |
| 6667 | goto onError; |
| 6668 | str = PyBytes_AS_STRING(res) + respos; |
| 6669 | ressize = requiredsize; |
| 6670 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6671 | /* generate replacement */ |
| 6672 | for (i = collstart; i < collend; ++i) { |
| 6673 | str += sprintf(str, "&#%d;", PyUnicode_READ(kind, data, i)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6674 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6675 | pos = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6676 | break; |
| 6677 | default: |
| 6678 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6679 | encoding, reason, unicode, &exc, |
| 6680 | collstart, collend, &newpos); |
| 6681 | if (repunicode == NULL || (PyUnicode_Check(repunicode) && |
| 6682 | PyUnicode_READY(repunicode) < 0)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6683 | goto onError; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6684 | if (PyBytes_Check(repunicode)) { |
| 6685 | /* Directly copy bytes result to output. */ |
| 6686 | repsize = PyBytes_Size(repunicode); |
| 6687 | if (repsize > 1) { |
| 6688 | /* Make room for all additional bytes. */ |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 6689 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6690 | if (_PyBytes_Resize(&res, ressize+repsize-1)) { |
| 6691 | Py_DECREF(repunicode); |
| 6692 | goto onError; |
| 6693 | } |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 6694 | str = PyBytes_AS_STRING(res) + respos; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6695 | ressize += repsize-1; |
| 6696 | } |
| 6697 | memcpy(str, PyBytes_AsString(repunicode), repsize); |
| 6698 | str += repsize; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6699 | pos = newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6700 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6701 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6702 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6703 | /* need more space? (at least enough for what we |
| 6704 | have+the replacement+the rest of the string, so |
| 6705 | we won't have to check space for encodable characters) */ |
| 6706 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6707 | repsize = PyUnicode_GET_LENGTH(repunicode); |
| 6708 | requiredsize = respos+repsize+(size-collend); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6709 | if (requiredsize > ressize) { |
| 6710 | if (requiredsize<2*ressize) |
| 6711 | requiredsize = 2*ressize; |
| 6712 | if (_PyBytes_Resize(&res, requiredsize)) { |
| 6713 | Py_DECREF(repunicode); |
| 6714 | goto onError; |
| 6715 | } |
| 6716 | str = PyBytes_AS_STRING(res) + respos; |
| 6717 | ressize = requiredsize; |
| 6718 | } |
| 6719 | /* check if there is anything unencodable in the replacement |
| 6720 | and copy it to the output */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6721 | for (i = 0; repsize-->0; ++i, ++str) { |
| 6722 | c = PyUnicode_READ_CHAR(repunicode, i); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6723 | if (c >= limit) { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6724 | raise_encode_exception_obj(&exc, encoding, unicode, |
| 6725 | pos, pos+1, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6726 | Py_DECREF(repunicode); |
| 6727 | goto onError; |
| 6728 | } |
| 6729 | *str = (char)c; |
| 6730 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6731 | pos = newpos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6732 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6733 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6734 | } |
| 6735 | } |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6736 | /* Resize if we allocated to much */ |
| 6737 | size = str - PyBytes_AS_STRING(res); |
| 6738 | if (size < ressize) { /* If this falls res will be NULL */ |
Alexandre Vassalotti | bad1b92 | 2008-12-27 09:49:09 +0000 | [diff] [blame] | 6739 | assert(size >= 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6740 | if (_PyBytes_Resize(&res, size) < 0) |
| 6741 | goto onError; |
| 6742 | } |
| 6743 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6744 | Py_XDECREF(errorHandler); |
| 6745 | Py_XDECREF(exc); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6746 | return res; |
| 6747 | |
| 6748 | onError: |
| 6749 | Py_XDECREF(res); |
| 6750 | Py_XDECREF(errorHandler); |
| 6751 | Py_XDECREF(exc); |
| 6752 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6753 | } |
| 6754 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6755 | /* Deprecated */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6756 | PyObject * |
| 6757 | PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6758 | Py_ssize_t size, |
| 6759 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6760 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6761 | PyObject *result; |
| 6762 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 6763 | if (unicode == NULL) |
| 6764 | return NULL; |
| 6765 | result = unicode_encode_ucs1(unicode, errors, 256); |
| 6766 | Py_DECREF(unicode); |
| 6767 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6768 | } |
| 6769 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6770 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6771 | _PyUnicode_AsLatin1String(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6772 | { |
| 6773 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6774 | PyErr_BadArgument(); |
| 6775 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6776 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6777 | if (PyUnicode_READY(unicode) == -1) |
| 6778 | return NULL; |
| 6779 | /* Fast path: if it is a one-byte string, construct |
| 6780 | bytes object directly. */ |
| 6781 | if (PyUnicode_KIND(unicode) == PyUnicode_1BYTE_KIND) |
| 6782 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 6783 | PyUnicode_GET_LENGTH(unicode)); |
| 6784 | /* Non-Latin-1 characters present. Defer to above function to |
| 6785 | raise the exception. */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6786 | return unicode_encode_ucs1(unicode, errors, 256); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6787 | } |
| 6788 | |
| 6789 | PyObject* |
| 6790 | PyUnicode_AsLatin1String(PyObject *unicode) |
| 6791 | { |
| 6792 | return _PyUnicode_AsLatin1String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6793 | } |
| 6794 | |
| 6795 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ |
| 6796 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6797 | PyObject * |
| 6798 | PyUnicode_DecodeASCII(const char *s, |
| 6799 | Py_ssize_t size, |
| 6800 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6801 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6802 | const char *starts = s; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6803 | PyObject *v; |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6804 | Py_UNICODE *u; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6805 | Py_ssize_t startinpos; |
| 6806 | Py_ssize_t endinpos; |
| 6807 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6808 | const char *e; |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6809 | int has_error; |
| 6810 | const unsigned char *p = (const unsigned char *)s; |
| 6811 | const unsigned char *end = p + size; |
| 6812 | 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] | 6813 | PyObject *errorHandler = NULL; |
| 6814 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6815 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6816 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6817 | if (size == 1 && (unsigned char)s[0] < 128) |
| 6818 | return get_latin1_char((unsigned char)s[0]); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6819 | |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6820 | has_error = 0; |
| 6821 | while (p < end && !has_error) { |
| 6822 | /* Fast path, see below in PyUnicode_DecodeUTF8Stateful for |
| 6823 | an explanation. */ |
| 6824 | if (!((size_t) p & LONG_PTR_MASK)) { |
| 6825 | /* Help register allocation */ |
| 6826 | register const unsigned char *_p = p; |
| 6827 | while (_p < aligned_end) { |
| 6828 | unsigned long value = *(unsigned long *) _p; |
| 6829 | if (value & ASCII_CHAR_MASK) { |
| 6830 | has_error = 1; |
| 6831 | break; |
| 6832 | } |
| 6833 | _p += SIZEOF_LONG; |
| 6834 | } |
| 6835 | if (_p == end) |
| 6836 | break; |
| 6837 | if (has_error) |
| 6838 | break; |
| 6839 | p = _p; |
| 6840 | } |
| 6841 | if (*p & 0x80) { |
| 6842 | has_error = 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6843 | break; |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6844 | } |
| 6845 | else { |
| 6846 | ++p; |
| 6847 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 6848 | } |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6849 | if (!has_error) |
| 6850 | return unicode_fromascii((const unsigned char *)s, size); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6851 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6852 | v = (PyObject*)_PyUnicode_New(size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6853 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6854 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6855 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6856 | return v; |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6857 | u = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6858 | e = s + size; |
| 6859 | while (s < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6860 | register unsigned char c = (unsigned char)*s; |
| 6861 | if (c < 128) { |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6862 | *u++ = c; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6863 | ++s; |
| 6864 | } |
| 6865 | else { |
| 6866 | startinpos = s-starts; |
| 6867 | endinpos = startinpos + 1; |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6868 | outpos = u - (Py_UNICODE *)PyUnicode_AS_UNICODE(v); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6869 | if (unicode_decode_call_errorhandler( |
| 6870 | errors, &errorHandler, |
| 6871 | "ascii", "ordinal not in range(128)", |
| 6872 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6873 | &v, &outpos, &u)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6874 | goto onError; |
| 6875 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6876 | } |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6877 | if (u - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6878 | if (PyUnicode_Resize(&v, u - PyUnicode_AS_UNICODE(v)) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6879 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6880 | Py_XDECREF(errorHandler); |
| 6881 | Py_XDECREF(exc); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 6882 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 6883 | if (_PyUnicode_READY_REPLACE(&v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6884 | Py_DECREF(v); |
| 6885 | return NULL; |
| 6886 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 6887 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 6888 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6889 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6890 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6891 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6892 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6893 | Py_XDECREF(errorHandler); |
| 6894 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6895 | return NULL; |
| 6896 | } |
| 6897 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6898 | /* Deprecated */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6899 | PyObject * |
| 6900 | PyUnicode_EncodeASCII(const Py_UNICODE *p, |
| 6901 | Py_ssize_t size, |
| 6902 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6903 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6904 | PyObject *result; |
| 6905 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 6906 | if (unicode == NULL) |
| 6907 | return NULL; |
| 6908 | result = unicode_encode_ucs1(unicode, errors, 128); |
| 6909 | Py_DECREF(unicode); |
| 6910 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6911 | } |
| 6912 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6913 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6914 | _PyUnicode_AsASCIIString(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6915 | { |
| 6916 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6917 | PyErr_BadArgument(); |
| 6918 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6919 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6920 | if (PyUnicode_READY(unicode) == -1) |
| 6921 | return NULL; |
| 6922 | /* Fast path: if it is an ASCII-only string, construct bytes object |
| 6923 | directly. Else defer to above function to raise the exception. */ |
| 6924 | if (PyUnicode_MAX_CHAR_VALUE(unicode) < 128) |
| 6925 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 6926 | PyUnicode_GET_LENGTH(unicode)); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6927 | return unicode_encode_ucs1(unicode, errors, 128); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6928 | } |
| 6929 | |
| 6930 | PyObject * |
| 6931 | PyUnicode_AsASCIIString(PyObject *unicode) |
| 6932 | { |
| 6933 | return _PyUnicode_AsASCIIString(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6934 | } |
| 6935 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 6936 | #ifdef HAVE_MBCS |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6937 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6938 | /* --- MBCS codecs for Windows -------------------------------------------- */ |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6939 | |
Hirokazu Yamamoto | 3530246 | 2009-03-21 13:23:27 +0000 | [diff] [blame] | 6940 | #if SIZEOF_INT < SIZEOF_SIZE_T |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6941 | #define NEED_RETRY |
| 6942 | #endif |
| 6943 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6944 | #ifndef WC_ERR_INVALID_CHARS |
| 6945 | # define WC_ERR_INVALID_CHARS 0x0080 |
| 6946 | #endif |
| 6947 | |
| 6948 | static char* |
| 6949 | code_page_name(UINT code_page, PyObject **obj) |
| 6950 | { |
| 6951 | *obj = NULL; |
| 6952 | if (code_page == CP_ACP) |
| 6953 | return "mbcs"; |
| 6954 | if (code_page == CP_UTF7) |
| 6955 | return "CP_UTF7"; |
| 6956 | if (code_page == CP_UTF8) |
| 6957 | return "CP_UTF8"; |
| 6958 | |
| 6959 | *obj = PyBytes_FromFormat("cp%u", code_page); |
| 6960 | if (*obj == NULL) |
| 6961 | return NULL; |
| 6962 | return PyBytes_AS_STRING(*obj); |
| 6963 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6964 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6965 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6966 | is_dbcs_lead_byte(UINT code_page, const char *s, int offset) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6967 | { |
| 6968 | const char *curr = s + offset; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6969 | const char *prev; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6970 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6971 | if (!IsDBCSLeadByteEx(code_page, *curr)) |
| 6972 | return 0; |
| 6973 | |
| 6974 | prev = CharPrevExA(code_page, s, curr, 0); |
| 6975 | if (prev == curr) |
| 6976 | return 1; |
| 6977 | /* FIXME: This code is limited to "true" double-byte encodings, |
| 6978 | as it assumes an incomplete character consists of a single |
| 6979 | byte. */ |
| 6980 | if (curr - prev == 2) |
| 6981 | return 1; |
| 6982 | if (!IsDBCSLeadByteEx(code_page, *prev)) |
| 6983 | return 1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6984 | return 0; |
| 6985 | } |
| 6986 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6987 | static DWORD |
| 6988 | decode_code_page_flags(UINT code_page) |
| 6989 | { |
| 6990 | if (code_page == CP_UTF7) { |
| 6991 | /* The CP_UTF7 decoder only supports flags=0 */ |
| 6992 | return 0; |
| 6993 | } |
| 6994 | else |
| 6995 | return MB_ERR_INVALID_CHARS; |
| 6996 | } |
| 6997 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6998 | /* |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6999 | * Decode a byte string from a Windows code page into unicode object in strict |
| 7000 | * mode. |
| 7001 | * |
| 7002 | * Returns consumed size if succeed, returns -2 on decode error, or raise a |
| 7003 | * WindowsError and returns -1 on other error. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7004 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7005 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7006 | decode_code_page_strict(UINT code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7007 | PyObject **v, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7008 | const char *in, |
| 7009 | int insize) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7010 | { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7011 | const DWORD flags = decode_code_page_flags(code_page); |
| 7012 | Py_UNICODE *out; |
| 7013 | DWORD outsize; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7014 | |
| 7015 | /* First get the size of the result */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7016 | assert(insize > 0); |
| 7017 | outsize = MultiByteToWideChar(code_page, flags, in, insize, NULL, 0); |
| 7018 | if (outsize <= 0) |
| 7019 | goto error; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7020 | |
| 7021 | if (*v == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7022 | /* Create unicode object */ |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7023 | *v = (PyObject*)_PyUnicode_New(outsize); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7024 | if (*v == NULL) |
| 7025 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7026 | out = PyUnicode_AS_UNICODE(*v); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7027 | } |
| 7028 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7029 | /* Extend unicode object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7030 | Py_ssize_t n = PyUnicode_GET_SIZE(*v); |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7031 | if (PyUnicode_Resize(v, n + outsize) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7032 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7033 | out = PyUnicode_AS_UNICODE(*v) + n; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7034 | } |
| 7035 | |
| 7036 | /* Do the conversion */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7037 | outsize = MultiByteToWideChar(code_page, flags, in, insize, out, outsize); |
| 7038 | if (outsize <= 0) |
| 7039 | goto error; |
| 7040 | return insize; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7041 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7042 | error: |
| 7043 | if (GetLastError() == ERROR_NO_UNICODE_TRANSLATION) |
| 7044 | return -2; |
| 7045 | PyErr_SetFromWindowsErr(0); |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7046 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7047 | } |
| 7048 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7049 | /* |
| 7050 | * Decode a byte string from a code page into unicode object with an error |
| 7051 | * handler. |
| 7052 | * |
| 7053 | * Returns consumed size if succeed, or raise a WindowsError or |
| 7054 | * UnicodeDecodeError exception and returns -1 on error. |
| 7055 | */ |
| 7056 | static int |
| 7057 | decode_code_page_errors(UINT code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7058 | PyObject **v, |
| 7059 | const char *in, const int size, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7060 | const char *errors) |
| 7061 | { |
| 7062 | const char *startin = in; |
| 7063 | const char *endin = in + size; |
| 7064 | const DWORD flags = decode_code_page_flags(code_page); |
| 7065 | /* Ideally, we should get reason from FormatMessage. This is the Windows |
| 7066 | 2000 English version of the message. */ |
| 7067 | const char *reason = "No mapping for the Unicode character exists " |
| 7068 | "in the target code page."; |
| 7069 | /* each step cannot decode more than 1 character, but a character can be |
| 7070 | represented as a surrogate pair */ |
| 7071 | wchar_t buffer[2], *startout, *out; |
| 7072 | int insize, outsize; |
| 7073 | PyObject *errorHandler = NULL; |
| 7074 | PyObject *exc = NULL; |
| 7075 | PyObject *encoding_obj = NULL; |
| 7076 | char *encoding; |
| 7077 | DWORD err; |
| 7078 | int ret = -1; |
| 7079 | |
| 7080 | assert(size > 0); |
| 7081 | |
| 7082 | encoding = code_page_name(code_page, &encoding_obj); |
| 7083 | if (encoding == NULL) |
| 7084 | return -1; |
| 7085 | |
| 7086 | if (errors == NULL || strcmp(errors, "strict") == 0) { |
| 7087 | /* The last error was ERROR_NO_UNICODE_TRANSLATION, then we raise a |
| 7088 | UnicodeDecodeError. */ |
| 7089 | make_decode_exception(&exc, encoding, in, size, 0, 0, reason); |
| 7090 | if (exc != NULL) { |
| 7091 | PyCodec_StrictErrors(exc); |
| 7092 | Py_CLEAR(exc); |
| 7093 | } |
| 7094 | goto error; |
| 7095 | } |
| 7096 | |
| 7097 | if (*v == NULL) { |
| 7098 | /* Create unicode object */ |
| 7099 | if (size > PY_SSIZE_T_MAX / (Py_ssize_t)Py_ARRAY_LENGTH(buffer)) { |
| 7100 | PyErr_NoMemory(); |
| 7101 | goto error; |
| 7102 | } |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7103 | *v = (PyObject*)_PyUnicode_New(size * Py_ARRAY_LENGTH(buffer)); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7104 | if (*v == NULL) |
| 7105 | goto error; |
| 7106 | startout = PyUnicode_AS_UNICODE(*v); |
| 7107 | } |
| 7108 | else { |
| 7109 | /* Extend unicode object */ |
| 7110 | Py_ssize_t n = PyUnicode_GET_SIZE(*v); |
| 7111 | if (size > (PY_SSIZE_T_MAX - n) / (Py_ssize_t)Py_ARRAY_LENGTH(buffer)) { |
| 7112 | PyErr_NoMemory(); |
| 7113 | goto error; |
| 7114 | } |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7115 | if (PyUnicode_Resize(v, n + size * Py_ARRAY_LENGTH(buffer)) < 0) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7116 | goto error; |
| 7117 | startout = PyUnicode_AS_UNICODE(*v) + n; |
| 7118 | } |
| 7119 | |
| 7120 | /* Decode the byte string character per character */ |
| 7121 | out = startout; |
| 7122 | while (in < endin) |
| 7123 | { |
| 7124 | /* Decode a character */ |
| 7125 | insize = 1; |
| 7126 | do |
| 7127 | { |
| 7128 | outsize = MultiByteToWideChar(code_page, flags, |
| 7129 | in, insize, |
| 7130 | buffer, Py_ARRAY_LENGTH(buffer)); |
| 7131 | if (outsize > 0) |
| 7132 | break; |
| 7133 | err = GetLastError(); |
| 7134 | if (err != ERROR_NO_UNICODE_TRANSLATION |
| 7135 | && err != ERROR_INSUFFICIENT_BUFFER) |
| 7136 | { |
| 7137 | PyErr_SetFromWindowsErr(0); |
| 7138 | goto error; |
| 7139 | } |
| 7140 | insize++; |
| 7141 | } |
| 7142 | /* 4=maximum length of a UTF-8 sequence */ |
| 7143 | while (insize <= 4 && (in + insize) <= endin); |
| 7144 | |
| 7145 | if (outsize <= 0) { |
| 7146 | Py_ssize_t startinpos, endinpos, outpos; |
| 7147 | |
| 7148 | startinpos = in - startin; |
| 7149 | endinpos = startinpos + 1; |
| 7150 | outpos = out - PyUnicode_AS_UNICODE(*v); |
| 7151 | if (unicode_decode_call_errorhandler( |
| 7152 | errors, &errorHandler, |
| 7153 | encoding, reason, |
| 7154 | &startin, &endin, &startinpos, &endinpos, &exc, &in, |
| 7155 | v, &outpos, &out)) |
| 7156 | { |
| 7157 | goto error; |
| 7158 | } |
| 7159 | } |
| 7160 | else { |
| 7161 | in += insize; |
| 7162 | memcpy(out, buffer, outsize * sizeof(wchar_t)); |
| 7163 | out += outsize; |
| 7164 | } |
| 7165 | } |
| 7166 | |
| 7167 | /* write a NUL character at the end */ |
| 7168 | *out = 0; |
| 7169 | |
| 7170 | /* Extend unicode object */ |
| 7171 | outsize = out - startout; |
| 7172 | assert(outsize <= PyUnicode_WSTR_LENGTH(*v)); |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7173 | if (PyUnicode_Resize(v, outsize) < 0) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7174 | goto error; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7175 | ret = size; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7176 | |
| 7177 | error: |
| 7178 | Py_XDECREF(encoding_obj); |
| 7179 | Py_XDECREF(errorHandler); |
| 7180 | Py_XDECREF(exc); |
| 7181 | return ret; |
| 7182 | } |
| 7183 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7184 | static PyObject * |
| 7185 | decode_code_page_stateful(int code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7186 | const char *s, Py_ssize_t size, |
| 7187 | const char *errors, Py_ssize_t *consumed) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7188 | { |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7189 | PyObject *v = NULL; |
| 7190 | int chunk_size, final, converted, done; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7191 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7192 | if (code_page < 0) { |
| 7193 | PyErr_SetString(PyExc_ValueError, "invalid code page number"); |
| 7194 | return NULL; |
| 7195 | } |
| 7196 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7197 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7198 | *consumed = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7199 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7200 | do |
| 7201 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7202 | #ifdef NEED_RETRY |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7203 | if (size > INT_MAX) { |
| 7204 | chunk_size = INT_MAX; |
| 7205 | final = 0; |
| 7206 | done = 0; |
| 7207 | } |
| 7208 | else |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7209 | #endif |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7210 | { |
| 7211 | chunk_size = (int)size; |
| 7212 | final = (consumed == NULL); |
| 7213 | done = 1; |
| 7214 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7215 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7216 | /* Skip trailing lead-byte unless 'final' is set */ |
| 7217 | if (!final && is_dbcs_lead_byte(code_page, s, chunk_size - 1)) |
| 7218 | --chunk_size; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7219 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7220 | if (chunk_size == 0 && done) { |
| 7221 | if (v != NULL) |
| 7222 | break; |
| 7223 | Py_INCREF(unicode_empty); |
| 7224 | return unicode_empty; |
| 7225 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7226 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7227 | |
| 7228 | converted = decode_code_page_strict(code_page, &v, |
| 7229 | s, chunk_size); |
| 7230 | if (converted == -2) |
| 7231 | converted = decode_code_page_errors(code_page, &v, |
| 7232 | s, chunk_size, |
| 7233 | errors); |
| 7234 | assert(converted != 0); |
| 7235 | |
| 7236 | if (converted < 0) { |
| 7237 | Py_XDECREF(v); |
| 7238 | return NULL; |
| 7239 | } |
| 7240 | |
| 7241 | if (consumed) |
| 7242 | *consumed += converted; |
| 7243 | |
| 7244 | s += converted; |
| 7245 | size -= converted; |
| 7246 | } while (!done); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7247 | |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 7248 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 7249 | if (_PyUnicode_READY_REPLACE(&v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7250 | Py_DECREF(v); |
| 7251 | return NULL; |
| 7252 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 7253 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 7254 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7255 | return v; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7256 | } |
| 7257 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7258 | PyObject * |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7259 | PyUnicode_DecodeCodePageStateful(int code_page, |
| 7260 | const char *s, |
| 7261 | Py_ssize_t size, |
| 7262 | const char *errors, |
| 7263 | Py_ssize_t *consumed) |
| 7264 | { |
| 7265 | return decode_code_page_stateful(code_page, s, size, errors, consumed); |
| 7266 | } |
| 7267 | |
| 7268 | PyObject * |
| 7269 | PyUnicode_DecodeMBCSStateful(const char *s, |
| 7270 | Py_ssize_t size, |
| 7271 | const char *errors, |
| 7272 | Py_ssize_t *consumed) |
| 7273 | { |
| 7274 | return decode_code_page_stateful(CP_ACP, s, size, errors, consumed); |
| 7275 | } |
| 7276 | |
| 7277 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7278 | PyUnicode_DecodeMBCS(const char *s, |
| 7279 | Py_ssize_t size, |
| 7280 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7281 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7282 | return PyUnicode_DecodeMBCSStateful(s, size, errors, NULL); |
| 7283 | } |
| 7284 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7285 | static DWORD |
| 7286 | encode_code_page_flags(UINT code_page, const char *errors) |
| 7287 | { |
| 7288 | if (code_page == CP_UTF8) { |
| 7289 | if (winver.dwMajorVersion >= 6) |
| 7290 | /* CP_UTF8 supports WC_ERR_INVALID_CHARS on Windows Vista |
| 7291 | and later */ |
| 7292 | return WC_ERR_INVALID_CHARS; |
| 7293 | else |
| 7294 | /* CP_UTF8 only supports flags=0 on Windows older than Vista */ |
| 7295 | return 0; |
| 7296 | } |
| 7297 | else if (code_page == CP_UTF7) { |
| 7298 | /* CP_UTF7 only supports flags=0 */ |
| 7299 | return 0; |
| 7300 | } |
| 7301 | else { |
| 7302 | if (errors != NULL && strcmp(errors, "replace") == 0) |
| 7303 | return 0; |
| 7304 | else |
| 7305 | return WC_NO_BEST_FIT_CHARS; |
| 7306 | } |
| 7307 | } |
| 7308 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7309 | /* |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7310 | * Encode a Unicode string to a Windows code page into a byte string in strict |
| 7311 | * mode. |
| 7312 | * |
| 7313 | * Returns consumed characters if succeed, returns -2 on encode error, or raise |
| 7314 | * a WindowsError and returns -1 on other error. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7315 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7316 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7317 | encode_code_page_strict(UINT code_page, PyObject **outbytes, |
| 7318 | const Py_UNICODE *p, const int size, |
| 7319 | const char* errors) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7320 | { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7321 | BOOL usedDefaultChar = FALSE; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7322 | BOOL *pusedDefaultChar = &usedDefaultChar; |
| 7323 | int outsize; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7324 | PyObject *exc = NULL; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7325 | const DWORD flags = encode_code_page_flags(code_page, NULL); |
| 7326 | char *out; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7327 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7328 | assert(size > 0); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7329 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7330 | if (code_page != CP_UTF8 && code_page != CP_UTF7) |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7331 | pusedDefaultChar = &usedDefaultChar; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7332 | else |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7333 | pusedDefaultChar = NULL; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7334 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7335 | /* First get the size of the result */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7336 | outsize = WideCharToMultiByte(code_page, flags, |
| 7337 | p, size, |
| 7338 | NULL, 0, |
| 7339 | NULL, pusedDefaultChar); |
| 7340 | if (outsize <= 0) |
| 7341 | goto error; |
| 7342 | /* If we used a default char, then we failed! */ |
| 7343 | if (pusedDefaultChar && *pusedDefaultChar) |
| 7344 | return -2; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7345 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7346 | if (*outbytes == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7347 | /* Create string object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7348 | *outbytes = PyBytes_FromStringAndSize(NULL, outsize); |
| 7349 | if (*outbytes == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7350 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7351 | out = PyBytes_AS_STRING(*outbytes); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7352 | } |
| 7353 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7354 | /* Extend string object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7355 | const Py_ssize_t n = PyBytes_Size(*outbytes); |
| 7356 | if (outsize > PY_SSIZE_T_MAX - n) { |
| 7357 | PyErr_NoMemory(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7358 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7359 | } |
| 7360 | if (_PyBytes_Resize(outbytes, n + outsize) < 0) |
| 7361 | return -1; |
| 7362 | out = PyBytes_AS_STRING(*outbytes) + n; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7363 | } |
| 7364 | |
| 7365 | /* Do the conversion */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7366 | outsize = WideCharToMultiByte(code_page, flags, |
| 7367 | p, size, |
| 7368 | out, outsize, |
| 7369 | NULL, pusedDefaultChar); |
| 7370 | if (outsize <= 0) |
| 7371 | goto error; |
| 7372 | if (pusedDefaultChar && *pusedDefaultChar) |
| 7373 | return -2; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7374 | return 0; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7375 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7376 | error: |
| 7377 | if (GetLastError() == ERROR_NO_UNICODE_TRANSLATION) |
| 7378 | return -2; |
| 7379 | PyErr_SetFromWindowsErr(0); |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7380 | return -1; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7381 | } |
| 7382 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7383 | /* |
| 7384 | * Encode a Unicode string to a Windows code page into a byte string using a |
| 7385 | * error handler. |
| 7386 | * |
| 7387 | * Returns consumed characters if succeed, or raise a WindowsError and returns |
| 7388 | * -1 on other error. |
| 7389 | */ |
| 7390 | static int |
| 7391 | encode_code_page_errors(UINT code_page, PyObject **outbytes, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7392 | PyObject *unicode, Py_ssize_t unicode_offset, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7393 | const Py_UNICODE *in, const int insize, |
| 7394 | const char* errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7395 | { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7396 | const DWORD flags = encode_code_page_flags(code_page, errors); |
| 7397 | const Py_UNICODE *startin = in; |
| 7398 | const Py_UNICODE *endin = in + insize; |
| 7399 | /* Ideally, we should get reason from FormatMessage. This is the Windows |
| 7400 | 2000 English version of the message. */ |
| 7401 | const char *reason = "invalid character"; |
| 7402 | /* 4=maximum length of a UTF-8 sequence */ |
| 7403 | char buffer[4]; |
| 7404 | BOOL usedDefaultChar = FALSE, *pusedDefaultChar; |
| 7405 | Py_ssize_t outsize; |
| 7406 | char *out; |
| 7407 | int charsize; |
| 7408 | PyObject *errorHandler = NULL; |
| 7409 | PyObject *exc = NULL; |
| 7410 | PyObject *encoding_obj = NULL; |
| 7411 | char *encoding; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7412 | Py_ssize_t startpos, newpos, newoutsize; |
| 7413 | PyObject *rep; |
| 7414 | int ret = -1; |
| 7415 | |
| 7416 | assert(insize > 0); |
| 7417 | |
| 7418 | encoding = code_page_name(code_page, &encoding_obj); |
| 7419 | if (encoding == NULL) |
| 7420 | return -1; |
| 7421 | |
| 7422 | if (errors == NULL || strcmp(errors, "strict") == 0) { |
| 7423 | /* The last error was ERROR_NO_UNICODE_TRANSLATION, |
| 7424 | then we raise a UnicodeEncodeError. */ |
| 7425 | make_encode_exception(&exc, encoding, in, insize, 0, 0, reason); |
| 7426 | if (exc != NULL) { |
| 7427 | PyCodec_StrictErrors(exc); |
| 7428 | Py_DECREF(exc); |
| 7429 | } |
| 7430 | Py_XDECREF(encoding_obj); |
| 7431 | return -1; |
| 7432 | } |
| 7433 | |
| 7434 | if (code_page != CP_UTF8 && code_page != CP_UTF7) |
| 7435 | pusedDefaultChar = &usedDefaultChar; |
| 7436 | else |
| 7437 | pusedDefaultChar = NULL; |
| 7438 | |
| 7439 | if (Py_ARRAY_LENGTH(buffer) > PY_SSIZE_T_MAX / insize) { |
| 7440 | PyErr_NoMemory(); |
| 7441 | goto error; |
| 7442 | } |
| 7443 | outsize = insize * Py_ARRAY_LENGTH(buffer); |
| 7444 | |
| 7445 | if (*outbytes == NULL) { |
| 7446 | /* Create string object */ |
| 7447 | *outbytes = PyBytes_FromStringAndSize(NULL, outsize); |
| 7448 | if (*outbytes == NULL) |
| 7449 | goto error; |
| 7450 | out = PyBytes_AS_STRING(*outbytes); |
| 7451 | } |
| 7452 | else { |
| 7453 | /* Extend string object */ |
| 7454 | Py_ssize_t n = PyBytes_Size(*outbytes); |
| 7455 | if (n > PY_SSIZE_T_MAX - outsize) { |
| 7456 | PyErr_NoMemory(); |
| 7457 | goto error; |
| 7458 | } |
| 7459 | if (_PyBytes_Resize(outbytes, n + outsize) < 0) |
| 7460 | goto error; |
| 7461 | out = PyBytes_AS_STRING(*outbytes) + n; |
| 7462 | } |
| 7463 | |
| 7464 | /* Encode the string character per character */ |
| 7465 | while (in < endin) |
| 7466 | { |
| 7467 | if ((in + 2) <= endin |
| 7468 | && 0xD800 <= in[0] && in[0] <= 0xDBFF |
| 7469 | && 0xDC00 <= in[1] && in[1] <= 0xDFFF) |
| 7470 | charsize = 2; |
| 7471 | else |
| 7472 | charsize = 1; |
| 7473 | |
| 7474 | outsize = WideCharToMultiByte(code_page, flags, |
| 7475 | in, charsize, |
| 7476 | buffer, Py_ARRAY_LENGTH(buffer), |
| 7477 | NULL, pusedDefaultChar); |
| 7478 | if (outsize > 0) { |
| 7479 | if (pusedDefaultChar == NULL || !(*pusedDefaultChar)) |
| 7480 | { |
| 7481 | in += charsize; |
| 7482 | memcpy(out, buffer, outsize); |
| 7483 | out += outsize; |
| 7484 | continue; |
| 7485 | } |
| 7486 | } |
| 7487 | else if (GetLastError() != ERROR_NO_UNICODE_TRANSLATION) { |
| 7488 | PyErr_SetFromWindowsErr(0); |
| 7489 | goto error; |
| 7490 | } |
| 7491 | |
| 7492 | charsize = Py_MAX(charsize - 1, 1); |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7493 | startpos = unicode_offset + in - startin; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7494 | rep = unicode_encode_call_errorhandler( |
| 7495 | errors, &errorHandler, encoding, reason, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7496 | unicode, &exc, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7497 | startpos, startpos + charsize, &newpos); |
| 7498 | if (rep == NULL) |
| 7499 | goto error; |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7500 | in += (newpos - startpos); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7501 | |
| 7502 | if (PyBytes_Check(rep)) { |
| 7503 | outsize = PyBytes_GET_SIZE(rep); |
| 7504 | if (outsize != 1) { |
| 7505 | Py_ssize_t offset = out - PyBytes_AS_STRING(*outbytes); |
| 7506 | newoutsize = PyBytes_GET_SIZE(*outbytes) + (outsize - 1); |
| 7507 | if (_PyBytes_Resize(outbytes, newoutsize) < 0) { |
| 7508 | Py_DECREF(rep); |
| 7509 | goto error; |
| 7510 | } |
| 7511 | out = PyBytes_AS_STRING(*outbytes) + offset; |
| 7512 | } |
| 7513 | memcpy(out, PyBytes_AS_STRING(rep), outsize); |
| 7514 | out += outsize; |
| 7515 | } |
| 7516 | else { |
| 7517 | Py_ssize_t i; |
| 7518 | enum PyUnicode_Kind kind; |
| 7519 | void *data; |
| 7520 | |
| 7521 | if (PyUnicode_READY(rep) < 0) { |
| 7522 | Py_DECREF(rep); |
| 7523 | goto error; |
| 7524 | } |
| 7525 | |
| 7526 | outsize = PyUnicode_GET_LENGTH(rep); |
| 7527 | if (outsize != 1) { |
| 7528 | Py_ssize_t offset = out - PyBytes_AS_STRING(*outbytes); |
| 7529 | newoutsize = PyBytes_GET_SIZE(*outbytes) + (outsize - 1); |
| 7530 | if (_PyBytes_Resize(outbytes, newoutsize) < 0) { |
| 7531 | Py_DECREF(rep); |
| 7532 | goto error; |
| 7533 | } |
| 7534 | out = PyBytes_AS_STRING(*outbytes) + offset; |
| 7535 | } |
| 7536 | kind = PyUnicode_KIND(rep); |
| 7537 | data = PyUnicode_DATA(rep); |
| 7538 | for (i=0; i < outsize; i++) { |
| 7539 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 7540 | if (ch > 127) { |
| 7541 | raise_encode_exception(&exc, |
| 7542 | encoding, |
| 7543 | startin, insize, |
| 7544 | startpos, startpos + charsize, |
| 7545 | "unable to encode error handler result to ASCII"); |
| 7546 | Py_DECREF(rep); |
| 7547 | goto error; |
| 7548 | } |
| 7549 | *out = (unsigned char)ch; |
| 7550 | out++; |
| 7551 | } |
| 7552 | } |
| 7553 | Py_DECREF(rep); |
| 7554 | } |
| 7555 | /* write a NUL byte */ |
| 7556 | *out = 0; |
| 7557 | outsize = out - PyBytes_AS_STRING(*outbytes); |
| 7558 | assert(outsize <= PyBytes_GET_SIZE(*outbytes)); |
| 7559 | if (_PyBytes_Resize(outbytes, outsize) < 0) |
| 7560 | goto error; |
| 7561 | ret = 0; |
| 7562 | |
| 7563 | error: |
| 7564 | Py_XDECREF(encoding_obj); |
| 7565 | Py_XDECREF(errorHandler); |
| 7566 | Py_XDECREF(exc); |
| 7567 | return ret; |
| 7568 | } |
| 7569 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7570 | static PyObject * |
| 7571 | encode_code_page(int code_page, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7572 | PyObject *unicode, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7573 | const char *errors) |
| 7574 | { |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7575 | const Py_UNICODE *p; |
| 7576 | Py_ssize_t size; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7577 | PyObject *outbytes = NULL; |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7578 | Py_ssize_t offset; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7579 | int chunk_len, ret, done; |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7580 | |
| 7581 | p = PyUnicode_AsUnicodeAndSize(unicode, &size); |
| 7582 | if (p == NULL) |
| 7583 | return NULL; |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 7584 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7585 | if (code_page < 0) { |
| 7586 | PyErr_SetString(PyExc_ValueError, "invalid code page number"); |
| 7587 | return NULL; |
| 7588 | } |
| 7589 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7590 | if (size == 0) |
| 7591 | return PyBytes_FromStringAndSize(NULL, 0); |
| 7592 | |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7593 | offset = 0; |
| 7594 | do |
| 7595 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7596 | #ifdef NEED_RETRY |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7597 | if (size > INT_MAX) { |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7598 | chunk_len = INT_MAX; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7599 | done = 0; |
| 7600 | } |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7601 | else |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7602 | #endif |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7603 | { |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7604 | chunk_len = (int)size; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7605 | done = 1; |
| 7606 | } |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7607 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7608 | ret = encode_code_page_strict(code_page, &outbytes, |
| 7609 | p, chunk_len, |
| 7610 | errors); |
| 7611 | if (ret == -2) |
| 7612 | ret = encode_code_page_errors(code_page, &outbytes, |
| 7613 | unicode, offset, |
| 7614 | p, chunk_len, |
| 7615 | errors); |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7616 | if (ret < 0) { |
| 7617 | Py_XDECREF(outbytes); |
| 7618 | return NULL; |
| 7619 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7620 | |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7621 | p += chunk_len; |
| 7622 | offset += chunk_len; |
| 7623 | size -= chunk_len; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7624 | } while (!done); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7625 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7626 | return outbytes; |
| 7627 | } |
| 7628 | |
| 7629 | PyObject * |
| 7630 | PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
| 7631 | Py_ssize_t size, |
| 7632 | const char *errors) |
| 7633 | { |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7634 | PyObject *unicode, *res; |
| 7635 | unicode = PyUnicode_FromUnicode(p, size); |
| 7636 | if (unicode == NULL) |
| 7637 | return NULL; |
| 7638 | res = encode_code_page(CP_ACP, unicode, errors); |
| 7639 | Py_DECREF(unicode); |
| 7640 | return res; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7641 | } |
| 7642 | |
| 7643 | PyObject * |
| 7644 | PyUnicode_EncodeCodePage(int code_page, |
| 7645 | PyObject *unicode, |
| 7646 | const char *errors) |
| 7647 | { |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7648 | return encode_code_page(code_page, unicode, errors); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7649 | } |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 7650 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7651 | PyObject * |
| 7652 | PyUnicode_AsMBCSString(PyObject *unicode) |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 7653 | { |
| 7654 | if (!PyUnicode_Check(unicode)) { |
| 7655 | PyErr_BadArgument(); |
| 7656 | return NULL; |
| 7657 | } |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7658 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, NULL); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 7659 | } |
| 7660 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7661 | #undef NEED_RETRY |
| 7662 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 7663 | #endif /* HAVE_MBCS */ |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7664 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7665 | /* --- Character Mapping Codec -------------------------------------------- */ |
| 7666 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7667 | PyObject * |
| 7668 | PyUnicode_DecodeCharmap(const char *s, |
| 7669 | Py_ssize_t size, |
| 7670 | PyObject *mapping, |
| 7671 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7672 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7673 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7674 | Py_ssize_t startinpos; |
| 7675 | Py_ssize_t endinpos; |
| 7676 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7677 | const char *e; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 7678 | PyObject *v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7679 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7680 | Py_ssize_t extrachars = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7681 | PyObject *errorHandler = NULL; |
| 7682 | PyObject *exc = NULL; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7683 | Py_UNICODE *mapstring = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7684 | Py_ssize_t maplen = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7685 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7686 | /* Default to Latin-1 */ |
| 7687 | if (mapping == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7688 | return PyUnicode_DecodeLatin1(s, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7689 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 7690 | v = (PyObject*)_PyUnicode_New(size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7691 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7692 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7693 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 7694 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7695 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7696 | e = s + size; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7697 | if (PyUnicode_CheckExact(mapping)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7698 | mapstring = PyUnicode_AS_UNICODE(mapping); |
| 7699 | maplen = PyUnicode_GET_SIZE(mapping); |
| 7700 | while (s < e) { |
| 7701 | unsigned char ch = *s; |
| 7702 | Py_UNICODE x = 0xfffe; /* illegal value */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7703 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7704 | if (ch < maplen) |
| 7705 | x = mapstring[ch]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7706 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7707 | if (x == 0xfffe) { |
| 7708 | /* undefined mapping */ |
| 7709 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 7710 | startinpos = s-starts; |
| 7711 | endinpos = startinpos+1; |
| 7712 | if (unicode_decode_call_errorhandler( |
| 7713 | errors, &errorHandler, |
| 7714 | "charmap", "character maps to <undefined>", |
| 7715 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 7716 | &v, &outpos, &p)) { |
| 7717 | goto onError; |
| 7718 | } |
| 7719 | continue; |
| 7720 | } |
| 7721 | *p++ = x; |
| 7722 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7723 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7724 | } |
| 7725 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7726 | while (s < e) { |
| 7727 | unsigned char ch = *s; |
| 7728 | PyObject *w, *x; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7729 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7730 | /* Get mapping (char ordinal -> integer, Unicode char or None) */ |
| 7731 | w = PyLong_FromLong((long)ch); |
| 7732 | if (w == NULL) |
| 7733 | goto onError; |
| 7734 | x = PyObject_GetItem(mapping, w); |
| 7735 | Py_DECREF(w); |
| 7736 | if (x == NULL) { |
| 7737 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 7738 | /* No mapping found means: mapping is undefined. */ |
| 7739 | PyErr_Clear(); |
| 7740 | x = Py_None; |
| 7741 | Py_INCREF(x); |
| 7742 | } else |
| 7743 | goto onError; |
| 7744 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7745 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7746 | /* Apply mapping */ |
| 7747 | if (PyLong_Check(x)) { |
| 7748 | long value = PyLong_AS_LONG(x); |
| 7749 | if (value < 0 || value > 65535) { |
| 7750 | PyErr_SetString(PyExc_TypeError, |
| 7751 | "character mapping must be in range(65536)"); |
| 7752 | Py_DECREF(x); |
| 7753 | goto onError; |
| 7754 | } |
| 7755 | *p++ = (Py_UNICODE)value; |
| 7756 | } |
| 7757 | else if (x == Py_None) { |
| 7758 | /* undefined mapping */ |
| 7759 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 7760 | startinpos = s-starts; |
| 7761 | endinpos = startinpos+1; |
| 7762 | if (unicode_decode_call_errorhandler( |
| 7763 | errors, &errorHandler, |
| 7764 | "charmap", "character maps to <undefined>", |
| 7765 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 7766 | &v, &outpos, &p)) { |
| 7767 | Py_DECREF(x); |
| 7768 | goto onError; |
| 7769 | } |
| 7770 | Py_DECREF(x); |
| 7771 | continue; |
| 7772 | } |
| 7773 | else if (PyUnicode_Check(x)) { |
| 7774 | Py_ssize_t targetsize = PyUnicode_GET_SIZE(x); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7775 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7776 | if (targetsize == 1) |
| 7777 | /* 1-1 mapping */ |
| 7778 | *p++ = *PyUnicode_AS_UNICODE(x); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7779 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7780 | else if (targetsize > 1) { |
| 7781 | /* 1-n mapping */ |
| 7782 | if (targetsize > extrachars) { |
| 7783 | /* resize first */ |
| 7784 | Py_ssize_t oldpos = p - PyUnicode_AS_UNICODE(v); |
| 7785 | Py_ssize_t needed = (targetsize - extrachars) + \ |
| 7786 | (targetsize << 2); |
| 7787 | extrachars += needed; |
| 7788 | /* XXX overflow detection missing */ |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 7789 | if (PyUnicode_Resize(&v, |
| 7790 | PyUnicode_GET_SIZE(v) + needed) < 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7791 | Py_DECREF(x); |
| 7792 | goto onError; |
| 7793 | } |
| 7794 | p = PyUnicode_AS_UNICODE(v) + oldpos; |
| 7795 | } |
| 7796 | Py_UNICODE_COPY(p, |
| 7797 | PyUnicode_AS_UNICODE(x), |
| 7798 | targetsize); |
| 7799 | p += targetsize; |
| 7800 | extrachars -= targetsize; |
| 7801 | } |
| 7802 | /* 1-0 mapping: skip the character */ |
| 7803 | } |
| 7804 | else { |
| 7805 | /* wrong return value */ |
| 7806 | PyErr_SetString(PyExc_TypeError, |
| 7807 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7808 | Py_DECREF(x); |
| 7809 | goto onError; |
| 7810 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7811 | Py_DECREF(x); |
| 7812 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7813 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7814 | } |
| 7815 | if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 7816 | if (PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7817 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7818 | Py_XDECREF(errorHandler); |
| 7819 | Py_XDECREF(exc); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 7820 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 7821 | if (_PyUnicode_READY_REPLACE(&v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7822 | Py_DECREF(v); |
| 7823 | return NULL; |
| 7824 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 7825 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 7826 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 7827 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7828 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7829 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7830 | Py_XDECREF(errorHandler); |
| 7831 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7832 | Py_XDECREF(v); |
| 7833 | return NULL; |
| 7834 | } |
| 7835 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7836 | /* Charmap encoding: the lookup table */ |
| 7837 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7838 | struct encoding_map { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7839 | PyObject_HEAD |
| 7840 | unsigned char level1[32]; |
| 7841 | int count2, count3; |
| 7842 | unsigned char level23[1]; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7843 | }; |
| 7844 | |
| 7845 | static PyObject* |
| 7846 | encoding_map_size(PyObject *obj, PyObject* args) |
| 7847 | { |
| 7848 | struct encoding_map *map = (struct encoding_map*)obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7849 | return PyLong_FromLong(sizeof(*map) - 1 + 16*map->count2 + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7850 | 128*map->count3); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7851 | } |
| 7852 | |
| 7853 | static PyMethodDef encoding_map_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7854 | {"size", encoding_map_size, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7855 | PyDoc_STR("Return the size (in bytes) of this object") }, |
| 7856 | { 0 } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7857 | }; |
| 7858 | |
| 7859 | static void |
| 7860 | encoding_map_dealloc(PyObject* o) |
| 7861 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7862 | PyObject_FREE(o); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7863 | } |
| 7864 | |
| 7865 | static PyTypeObject EncodingMapType = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7866 | PyVarObject_HEAD_INIT(NULL, 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7867 | "EncodingMap", /*tp_name*/ |
| 7868 | sizeof(struct encoding_map), /*tp_basicsize*/ |
| 7869 | 0, /*tp_itemsize*/ |
| 7870 | /* methods */ |
| 7871 | encoding_map_dealloc, /*tp_dealloc*/ |
| 7872 | 0, /*tp_print*/ |
| 7873 | 0, /*tp_getattr*/ |
| 7874 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 7875 | 0, /*tp_reserved*/ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7876 | 0, /*tp_repr*/ |
| 7877 | 0, /*tp_as_number*/ |
| 7878 | 0, /*tp_as_sequence*/ |
| 7879 | 0, /*tp_as_mapping*/ |
| 7880 | 0, /*tp_hash*/ |
| 7881 | 0, /*tp_call*/ |
| 7882 | 0, /*tp_str*/ |
| 7883 | 0, /*tp_getattro*/ |
| 7884 | 0, /*tp_setattro*/ |
| 7885 | 0, /*tp_as_buffer*/ |
| 7886 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 7887 | 0, /*tp_doc*/ |
| 7888 | 0, /*tp_traverse*/ |
| 7889 | 0, /*tp_clear*/ |
| 7890 | 0, /*tp_richcompare*/ |
| 7891 | 0, /*tp_weaklistoffset*/ |
| 7892 | 0, /*tp_iter*/ |
| 7893 | 0, /*tp_iternext*/ |
| 7894 | encoding_map_methods, /*tp_methods*/ |
| 7895 | 0, /*tp_members*/ |
| 7896 | 0, /*tp_getset*/ |
| 7897 | 0, /*tp_base*/ |
| 7898 | 0, /*tp_dict*/ |
| 7899 | 0, /*tp_descr_get*/ |
| 7900 | 0, /*tp_descr_set*/ |
| 7901 | 0, /*tp_dictoffset*/ |
| 7902 | 0, /*tp_init*/ |
| 7903 | 0, /*tp_alloc*/ |
| 7904 | 0, /*tp_new*/ |
| 7905 | 0, /*tp_free*/ |
| 7906 | 0, /*tp_is_gc*/ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7907 | }; |
| 7908 | |
| 7909 | PyObject* |
| 7910 | PyUnicode_BuildEncodingMap(PyObject* string) |
| 7911 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7912 | PyObject *result; |
| 7913 | struct encoding_map *mresult; |
| 7914 | int i; |
| 7915 | int need_dict = 0; |
| 7916 | unsigned char level1[32]; |
| 7917 | unsigned char level2[512]; |
| 7918 | unsigned char *mlevel1, *mlevel2, *mlevel3; |
| 7919 | int count2 = 0, count3 = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7920 | int kind; |
| 7921 | void *data; |
| 7922 | Py_UCS4 ch; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7923 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7924 | if (!PyUnicode_Check(string) || PyUnicode_GET_LENGTH(string) != 256) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7925 | PyErr_BadArgument(); |
| 7926 | return NULL; |
| 7927 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7928 | kind = PyUnicode_KIND(string); |
| 7929 | data = PyUnicode_DATA(string); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7930 | memset(level1, 0xFF, sizeof level1); |
| 7931 | memset(level2, 0xFF, sizeof level2); |
| 7932 | |
| 7933 | /* If there isn't a one-to-one mapping of NULL to \0, |
| 7934 | or if there are non-BMP characters, we need to use |
| 7935 | a mapping dictionary. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7936 | if (PyUnicode_READ(kind, data, 0) != 0) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7937 | need_dict = 1; |
| 7938 | for (i = 1; i < 256; i++) { |
| 7939 | int l1, l2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7940 | ch = PyUnicode_READ(kind, data, i); |
| 7941 | if (ch == 0 || ch > 0xFFFF) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7942 | need_dict = 1; |
| 7943 | break; |
| 7944 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7945 | if (ch == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7946 | /* unmapped character */ |
| 7947 | continue; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7948 | l1 = ch >> 11; |
| 7949 | l2 = ch >> 7; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7950 | if (level1[l1] == 0xFF) |
| 7951 | level1[l1] = count2++; |
| 7952 | if (level2[l2] == 0xFF) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7953 | level2[l2] = count3++; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7954 | } |
| 7955 | |
| 7956 | if (count2 >= 0xFF || count3 >= 0xFF) |
| 7957 | need_dict = 1; |
| 7958 | |
| 7959 | if (need_dict) { |
| 7960 | PyObject *result = PyDict_New(); |
| 7961 | PyObject *key, *value; |
| 7962 | if (!result) |
| 7963 | return NULL; |
| 7964 | for (i = 0; i < 256; i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7965 | key = PyLong_FromLong(PyUnicode_READ(kind, data, i)); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7966 | value = PyLong_FromLong(i); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7967 | if (!key || !value) |
| 7968 | goto failed1; |
| 7969 | if (PyDict_SetItem(result, key, value) == -1) |
| 7970 | goto failed1; |
| 7971 | Py_DECREF(key); |
| 7972 | Py_DECREF(value); |
| 7973 | } |
| 7974 | return result; |
| 7975 | failed1: |
| 7976 | Py_XDECREF(key); |
| 7977 | Py_XDECREF(value); |
| 7978 | Py_DECREF(result); |
| 7979 | return NULL; |
| 7980 | } |
| 7981 | |
| 7982 | /* Create a three-level trie */ |
| 7983 | result = PyObject_MALLOC(sizeof(struct encoding_map) + |
| 7984 | 16*count2 + 128*count3 - 1); |
| 7985 | if (!result) |
| 7986 | return PyErr_NoMemory(); |
| 7987 | PyObject_Init(result, &EncodingMapType); |
| 7988 | mresult = (struct encoding_map*)result; |
| 7989 | mresult->count2 = count2; |
| 7990 | mresult->count3 = count3; |
| 7991 | mlevel1 = mresult->level1; |
| 7992 | mlevel2 = mresult->level23; |
| 7993 | mlevel3 = mresult->level23 + 16*count2; |
| 7994 | memcpy(mlevel1, level1, 32); |
| 7995 | memset(mlevel2, 0xFF, 16*count2); |
| 7996 | memset(mlevel3, 0, 128*count3); |
| 7997 | count3 = 0; |
| 7998 | for (i = 1; i < 256; i++) { |
| 7999 | int o1, o2, o3, i2, i3; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8000 | if (PyUnicode_READ(kind, data, i) == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8001 | /* unmapped character */ |
| 8002 | continue; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8003 | o1 = PyUnicode_READ(kind, data, i)>>11; |
| 8004 | o2 = (PyUnicode_READ(kind, data, i)>>7) & 0xF; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8005 | i2 = 16*mlevel1[o1] + o2; |
| 8006 | if (mlevel2[i2] == 0xFF) |
| 8007 | mlevel2[i2] = count3++; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8008 | o3 = PyUnicode_READ(kind, data, i) & 0x7F; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8009 | i3 = 128*mlevel2[i2] + o3; |
| 8010 | mlevel3[i3] = i; |
| 8011 | } |
| 8012 | return result; |
| 8013 | } |
| 8014 | |
| 8015 | static int |
| 8016 | encoding_map_lookup(Py_UNICODE c, PyObject *mapping) |
| 8017 | { |
| 8018 | struct encoding_map *map = (struct encoding_map*)mapping; |
| 8019 | int l1 = c>>11; |
| 8020 | int l2 = (c>>7) & 0xF; |
| 8021 | int l3 = c & 0x7F; |
| 8022 | int i; |
| 8023 | |
| 8024 | #ifdef Py_UNICODE_WIDE |
| 8025 | if (c > 0xFFFF) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8026 | return -1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8027 | } |
| 8028 | #endif |
| 8029 | if (c == 0) |
| 8030 | return 0; |
| 8031 | /* level 1*/ |
| 8032 | i = map->level1[l1]; |
| 8033 | if (i == 0xFF) { |
| 8034 | return -1; |
| 8035 | } |
| 8036 | /* level 2*/ |
| 8037 | i = map->level23[16*i+l2]; |
| 8038 | if (i == 0xFF) { |
| 8039 | return -1; |
| 8040 | } |
| 8041 | /* level 3 */ |
| 8042 | i = map->level23[16*map->count2 + 128*i + l3]; |
| 8043 | if (i == 0) { |
| 8044 | return -1; |
| 8045 | } |
| 8046 | return i; |
| 8047 | } |
| 8048 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8049 | /* Lookup the character ch in the mapping. If the character |
| 8050 | can't be found, Py_None is returned (or NULL, if another |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 8051 | error occurred). */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8052 | static PyObject * |
| 8053 | charmapencode_lookup(Py_UNICODE c, PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8054 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8055 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8056 | PyObject *x; |
| 8057 | |
| 8058 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8059 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8060 | x = PyObject_GetItem(mapping, w); |
| 8061 | Py_DECREF(w); |
| 8062 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8063 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 8064 | /* No mapping found means: mapping is undefined. */ |
| 8065 | PyErr_Clear(); |
| 8066 | x = Py_None; |
| 8067 | Py_INCREF(x); |
| 8068 | return x; |
| 8069 | } else |
| 8070 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8071 | } |
Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 8072 | else if (x == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8073 | return x; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8074 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8075 | long value = PyLong_AS_LONG(x); |
| 8076 | if (value < 0 || value > 255) { |
| 8077 | PyErr_SetString(PyExc_TypeError, |
| 8078 | "character mapping must be in range(256)"); |
| 8079 | Py_DECREF(x); |
| 8080 | return NULL; |
| 8081 | } |
| 8082 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8083 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8084 | else if (PyBytes_Check(x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8085 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8086 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8087 | /* wrong return value */ |
| 8088 | PyErr_Format(PyExc_TypeError, |
| 8089 | "character mapping must return integer, bytes or None, not %.400s", |
| 8090 | x->ob_type->tp_name); |
| 8091 | Py_DECREF(x); |
| 8092 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8093 | } |
| 8094 | } |
| 8095 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8096 | static int |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 8097 | charmapencode_resize(PyObject **outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8098 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8099 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
| 8100 | /* exponentially overallocate to minimize reallocations */ |
| 8101 | if (requiredsize < 2*outsize) |
| 8102 | requiredsize = 2*outsize; |
| 8103 | if (_PyBytes_Resize(outobj, requiredsize)) |
| 8104 | return -1; |
| 8105 | return 0; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8106 | } |
| 8107 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8108 | typedef enum charmapencode_result { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8109 | enc_SUCCESS, enc_FAILED, enc_EXCEPTION |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8110 | } charmapencode_result; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8111 | /* 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] | 8112 | various state variables. Resize the output bytes object if not enough |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8113 | space is available. Return a new reference to the object that |
| 8114 | was put in the output buffer, or Py_None, if the mapping was undefined |
| 8115 | (in which case no character was written) or NULL, if a |
Andrew M. Kuchling | 8294de5 | 2005-11-02 16:36:12 +0000 | [diff] [blame] | 8116 | reallocation error occurred. The caller must decref the result */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8117 | static charmapencode_result |
| 8118 | charmapencode_output(Py_UNICODE c, PyObject *mapping, |
| 8119 | PyObject **outobj, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8120 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8121 | PyObject *rep; |
| 8122 | char *outstart; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8123 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8124 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 8125 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8126 | int res = encoding_map_lookup(c, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8127 | Py_ssize_t requiredsize = *outpos+1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8128 | if (res == -1) |
| 8129 | return enc_FAILED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8130 | if (outsize<requiredsize) |
| 8131 | if (charmapencode_resize(outobj, outpos, requiredsize)) |
| 8132 | return enc_EXCEPTION; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8133 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8134 | outstart[(*outpos)++] = (char)res; |
| 8135 | return enc_SUCCESS; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8136 | } |
| 8137 | |
| 8138 | rep = charmapencode_lookup(c, mapping); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8139 | if (rep==NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8140 | return enc_EXCEPTION; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8141 | else if (rep==Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8142 | Py_DECREF(rep); |
| 8143 | return enc_FAILED; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8144 | } else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8145 | if (PyLong_Check(rep)) { |
| 8146 | Py_ssize_t requiredsize = *outpos+1; |
| 8147 | if (outsize<requiredsize) |
| 8148 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 8149 | Py_DECREF(rep); |
| 8150 | return enc_EXCEPTION; |
| 8151 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8152 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8153 | outstart[(*outpos)++] = (char)PyLong_AS_LONG(rep); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8154 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8155 | else { |
| 8156 | const char *repchars = PyBytes_AS_STRING(rep); |
| 8157 | Py_ssize_t repsize = PyBytes_GET_SIZE(rep); |
| 8158 | Py_ssize_t requiredsize = *outpos+repsize; |
| 8159 | if (outsize<requiredsize) |
| 8160 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 8161 | Py_DECREF(rep); |
| 8162 | return enc_EXCEPTION; |
| 8163 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8164 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8165 | memcpy(outstart + *outpos, repchars, repsize); |
| 8166 | *outpos += repsize; |
| 8167 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8168 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8169 | Py_DECREF(rep); |
| 8170 | return enc_SUCCESS; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8171 | } |
| 8172 | |
| 8173 | /* handle an error in PyUnicode_EncodeCharmap |
| 8174 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8175 | static int |
| 8176 | charmap_encoding_error( |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8177 | PyObject *unicode, Py_ssize_t *inpos, PyObject *mapping, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8178 | PyObject **exceptionObject, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 8179 | int *known_errorHandler, PyObject **errorHandler, const char *errors, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 8180 | PyObject **res, Py_ssize_t *respos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8181 | { |
| 8182 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8183 | Py_ssize_t size, repsize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8184 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8185 | Py_UNICODE *uni2; |
| 8186 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8187 | Py_ssize_t collstartpos = *inpos; |
| 8188 | Py_ssize_t collendpos = *inpos+1; |
| 8189 | Py_ssize_t collpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8190 | char *encoding = "charmap"; |
| 8191 | char *reason = "character maps to <undefined>"; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8192 | charmapencode_result x; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8193 | Py_UCS4 ch; |
Brian Curtin | 2787ea4 | 2011-11-02 15:09:37 -0500 | [diff] [blame] | 8194 | int val; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8195 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8196 | if (PyUnicode_READY(unicode) < 0) |
| 8197 | return -1; |
| 8198 | size = PyUnicode_GET_LENGTH(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8199 | /* find all unencodable characters */ |
| 8200 | while (collendpos < size) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8201 | PyObject *rep; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 8202 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8203 | ch = PyUnicode_READ_CHAR(unicode, collendpos); |
Brian Curtin | 2787ea4 | 2011-11-02 15:09:37 -0500 | [diff] [blame] | 8204 | val = encoding_map_lookup(ch, mapping); |
| 8205 | if (val != -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8206 | break; |
| 8207 | ++collendpos; |
| 8208 | continue; |
| 8209 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8210 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8211 | ch = PyUnicode_READ_CHAR(unicode, collendpos); |
| 8212 | rep = charmapencode_lookup(ch, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8213 | if (rep==NULL) |
| 8214 | return -1; |
| 8215 | else if (rep!=Py_None) { |
| 8216 | Py_DECREF(rep); |
| 8217 | break; |
| 8218 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8219 | Py_DECREF(rep); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8220 | ++collendpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8221 | } |
| 8222 | /* cache callback name lookup |
| 8223 | * (if not done yet, i.e. it's the first error) */ |
| 8224 | if (*known_errorHandler==-1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8225 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 8226 | *known_errorHandler = 1; |
| 8227 | else if (!strcmp(errors, "replace")) |
| 8228 | *known_errorHandler = 2; |
| 8229 | else if (!strcmp(errors, "ignore")) |
| 8230 | *known_errorHandler = 3; |
| 8231 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 8232 | *known_errorHandler = 4; |
| 8233 | else |
| 8234 | *known_errorHandler = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8235 | } |
| 8236 | switch (*known_errorHandler) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8237 | case 1: /* strict */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8238 | raise_encode_exception_obj(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8239 | return -1; |
| 8240 | case 2: /* replace */ |
| 8241 | for (collpos = collstartpos; collpos<collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8242 | x = charmapencode_output('?', mapping, res, respos); |
| 8243 | if (x==enc_EXCEPTION) { |
| 8244 | return -1; |
| 8245 | } |
| 8246 | else if (x==enc_FAILED) { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8247 | raise_encode_exception_obj(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8248 | return -1; |
| 8249 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8250 | } |
| 8251 | /* fall through */ |
| 8252 | case 3: /* ignore */ |
| 8253 | *inpos = collendpos; |
| 8254 | break; |
| 8255 | case 4: /* xmlcharrefreplace */ |
| 8256 | /* generate replacement (temporarily (mis)uses p) */ |
| 8257 | for (collpos = collstartpos; collpos < collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8258 | char buffer[2+29+1+1]; |
| 8259 | char *cp; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8260 | sprintf(buffer, "&#%d;", (int)PyUnicode_READ_CHAR(unicode, collpos)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8261 | for (cp = buffer; *cp; ++cp) { |
| 8262 | x = charmapencode_output(*cp, mapping, res, respos); |
| 8263 | if (x==enc_EXCEPTION) |
| 8264 | return -1; |
| 8265 | else if (x==enc_FAILED) { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8266 | raise_encode_exception_obj(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8267 | return -1; |
| 8268 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8269 | } |
| 8270 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8271 | *inpos = collendpos; |
| 8272 | break; |
| 8273 | default: |
| 8274 | repunicode = unicode_encode_call_errorhandler(errors, errorHandler, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8275 | encoding, reason, unicode, exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8276 | collstartpos, collendpos, &newpos); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8277 | if (repunicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8278 | return -1; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8279 | if (PyBytes_Check(repunicode)) { |
| 8280 | /* Directly copy bytes result to output. */ |
| 8281 | Py_ssize_t outsize = PyBytes_Size(*res); |
| 8282 | Py_ssize_t requiredsize; |
| 8283 | repsize = PyBytes_Size(repunicode); |
| 8284 | requiredsize = *respos + repsize; |
| 8285 | if (requiredsize > outsize) |
| 8286 | /* Make room for all additional bytes. */ |
| 8287 | if (charmapencode_resize(res, respos, requiredsize)) { |
| 8288 | Py_DECREF(repunicode); |
| 8289 | return -1; |
| 8290 | } |
| 8291 | memcpy(PyBytes_AsString(*res) + *respos, |
| 8292 | PyBytes_AsString(repunicode), repsize); |
| 8293 | *respos += repsize; |
| 8294 | *inpos = newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 8295 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8296 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 8297 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8298 | /* generate replacement */ |
| 8299 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 8300 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8301 | x = charmapencode_output(*uni2, mapping, res, respos); |
| 8302 | if (x==enc_EXCEPTION) { |
| 8303 | return -1; |
| 8304 | } |
| 8305 | else if (x==enc_FAILED) { |
| 8306 | Py_DECREF(repunicode); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8307 | raise_encode_exception_obj(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8308 | return -1; |
| 8309 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8310 | } |
| 8311 | *inpos = newpos; |
| 8312 | Py_DECREF(repunicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8313 | } |
| 8314 | return 0; |
| 8315 | } |
| 8316 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8317 | PyObject * |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8318 | _PyUnicode_EncodeCharmap(PyObject *unicode, |
| 8319 | PyObject *mapping, |
| 8320 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8321 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8322 | /* output object */ |
| 8323 | PyObject *res = NULL; |
| 8324 | /* current input position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8325 | Py_ssize_t inpos = 0; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8326 | Py_ssize_t size; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8327 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8328 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8329 | PyObject *errorHandler = NULL; |
| 8330 | PyObject *exc = NULL; |
| 8331 | /* the following variable is used for caching string comparisons |
| 8332 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 8333 | * 3=ignore, 4=xmlcharrefreplace */ |
| 8334 | int known_errorHandler = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8335 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8336 | if (PyUnicode_READY(unicode) < 0) |
| 8337 | return NULL; |
| 8338 | size = PyUnicode_GET_LENGTH(unicode); |
| 8339 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8340 | /* Default to Latin-1 */ |
| 8341 | if (mapping == NULL) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8342 | return unicode_encode_ucs1(unicode, errors, 256); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8343 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8344 | /* allocate enough for a simple encoding without |
| 8345 | replacements, if we need more, we'll resize */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8346 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8347 | if (res == NULL) |
| 8348 | goto onError; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 8349 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8350 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8351 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8352 | while (inpos<size) { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8353 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, inpos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8354 | /* try to encode it */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8355 | charmapencode_result x = charmapencode_output(ch, mapping, &res, &respos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8356 | if (x==enc_EXCEPTION) /* error */ |
| 8357 | goto onError; |
| 8358 | if (x==enc_FAILED) { /* unencodable character */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8359 | if (charmap_encoding_error(unicode, &inpos, mapping, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8360 | &exc, |
| 8361 | &known_errorHandler, &errorHandler, errors, |
| 8362 | &res, &respos)) { |
| 8363 | goto onError; |
| 8364 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8365 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8366 | else |
| 8367 | /* done with this character => adjust input position */ |
| 8368 | ++inpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8369 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8370 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8371 | /* Resize if we allocated to much */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8372 | if (respos<PyBytes_GET_SIZE(res)) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 8373 | if (_PyBytes_Resize(&res, respos) < 0) |
| 8374 | goto onError; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 8375 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8376 | Py_XDECREF(exc); |
| 8377 | Py_XDECREF(errorHandler); |
| 8378 | return res; |
| 8379 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8380 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8381 | Py_XDECREF(res); |
| 8382 | Py_XDECREF(exc); |
| 8383 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8384 | return NULL; |
| 8385 | } |
| 8386 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8387 | /* Deprecated */ |
| 8388 | PyObject * |
| 8389 | PyUnicode_EncodeCharmap(const Py_UNICODE *p, |
| 8390 | Py_ssize_t size, |
| 8391 | PyObject *mapping, |
| 8392 | const char *errors) |
| 8393 | { |
| 8394 | PyObject *result; |
| 8395 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 8396 | if (unicode == NULL) |
| 8397 | return NULL; |
| 8398 | result = _PyUnicode_EncodeCharmap(unicode, mapping, errors); |
| 8399 | Py_DECREF(unicode); |
Victor Stinner | fc026c9 | 2011-11-04 00:24:51 +0100 | [diff] [blame] | 8400 | return result; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8401 | } |
| 8402 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8403 | PyObject * |
| 8404 | PyUnicode_AsCharmapString(PyObject *unicode, |
| 8405 | PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8406 | { |
| 8407 | if (!PyUnicode_Check(unicode) || mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8408 | PyErr_BadArgument(); |
| 8409 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8410 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8411 | return _PyUnicode_EncodeCharmap(unicode, mapping, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8412 | } |
| 8413 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8414 | /* create or adjust a UnicodeTranslateError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8415 | static void |
| 8416 | make_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8417 | PyObject *unicode, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8418 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 8419 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8420 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8421 | if (*exceptionObject == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8422 | *exceptionObject = _PyUnicodeTranslateError_Create( |
| 8423 | unicode, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8424 | } |
| 8425 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8426 | if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
| 8427 | goto onError; |
| 8428 | if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) |
| 8429 | goto onError; |
| 8430 | if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) |
| 8431 | goto onError; |
| 8432 | return; |
| 8433 | onError: |
| 8434 | Py_DECREF(*exceptionObject); |
| 8435 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8436 | } |
| 8437 | } |
| 8438 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8439 | /* raises a UnicodeTranslateError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8440 | static void |
| 8441 | raise_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8442 | PyObject *unicode, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8443 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 8444 | const char *reason) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8445 | { |
| 8446 | make_translate_exception(exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8447 | unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8448 | if (*exceptionObject != NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8449 | PyCodec_StrictErrors(*exceptionObject); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8450 | } |
| 8451 | |
| 8452 | /* error handling callback helper: |
| 8453 | build arguments, call the callback and check the arguments, |
| 8454 | put the result into newpos and return the replacement string, which |
| 8455 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8456 | static PyObject * |
| 8457 | unicode_translate_call_errorhandler(const char *errors, |
| 8458 | PyObject **errorHandler, |
| 8459 | const char *reason, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8460 | PyObject *unicode, PyObject **exceptionObject, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8461 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 8462 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8463 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 8464 | 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] | 8465 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8466 | Py_ssize_t i_newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8467 | PyObject *restuple; |
| 8468 | PyObject *resunicode; |
| 8469 | |
| 8470 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8471 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8472 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8473 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8474 | } |
| 8475 | |
| 8476 | make_translate_exception(exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8477 | unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8478 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8479 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8480 | |
| 8481 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8482 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8483 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8484 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8485 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 8486 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8487 | Py_DECREF(restuple); |
| 8488 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8489 | } |
| 8490 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8491 | &resunicode, &i_newpos)) { |
| 8492 | Py_DECREF(restuple); |
| 8493 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8494 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8495 | if (i_newpos<0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8496 | *newpos = PyUnicode_GET_LENGTH(unicode)+i_newpos; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8497 | else |
| 8498 | *newpos = i_newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8499 | if (*newpos<0 || *newpos>PyUnicode_GET_LENGTH(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8500 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 8501 | Py_DECREF(restuple); |
| 8502 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 8503 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8504 | Py_INCREF(resunicode); |
| 8505 | Py_DECREF(restuple); |
| 8506 | return resunicode; |
| 8507 | } |
| 8508 | |
| 8509 | /* Lookup the character ch in the mapping and put the result in result, |
| 8510 | which must be decrefed by the caller. |
| 8511 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8512 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8513 | charmaptranslate_lookup(Py_UCS4 c, PyObject *mapping, PyObject **result) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8514 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8515 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8516 | PyObject *x; |
| 8517 | |
| 8518 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8519 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8520 | x = PyObject_GetItem(mapping, w); |
| 8521 | Py_DECREF(w); |
| 8522 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8523 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 8524 | /* No mapping found means: use 1:1 mapping. */ |
| 8525 | PyErr_Clear(); |
| 8526 | *result = NULL; |
| 8527 | return 0; |
| 8528 | } else |
| 8529 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8530 | } |
| 8531 | else if (x == Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8532 | *result = x; |
| 8533 | return 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8534 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8535 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8536 | long value = PyLong_AS_LONG(x); |
| 8537 | long max = PyUnicode_GetMax(); |
| 8538 | if (value < 0 || value > max) { |
| 8539 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5a2f7e60 | 2007-10-24 21:13:09 +0000 | [diff] [blame] | 8540 | "character mapping must be in range(0x%x)", max+1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8541 | Py_DECREF(x); |
| 8542 | return -1; |
| 8543 | } |
| 8544 | *result = x; |
| 8545 | return 0; |
| 8546 | } |
| 8547 | else if (PyUnicode_Check(x)) { |
| 8548 | *result = x; |
| 8549 | return 0; |
| 8550 | } |
| 8551 | else { |
| 8552 | /* wrong return value */ |
| 8553 | PyErr_SetString(PyExc_TypeError, |
| 8554 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8555 | Py_DECREF(x); |
| 8556 | return -1; |
| 8557 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8558 | } |
| 8559 | /* ensure that *outobj is at least requiredsize characters long, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8560 | if not reallocate and adjust various state variables. |
| 8561 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8562 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8563 | charmaptranslate_makespace(Py_UCS4 **outobj, Py_ssize_t *psize, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8564 | Py_ssize_t requiredsize) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8565 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8566 | Py_ssize_t oldsize = *psize; |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 8567 | if (requiredsize > oldsize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8568 | /* exponentially overallocate to minimize reallocations */ |
| 8569 | if (requiredsize < 2 * oldsize) |
| 8570 | requiredsize = 2 * oldsize; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8571 | *outobj = PyMem_Realloc(*outobj, requiredsize * sizeof(Py_UCS4)); |
| 8572 | if (*outobj == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8573 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8574 | *psize = requiredsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8575 | } |
| 8576 | return 0; |
| 8577 | } |
| 8578 | /* lookup the character, put the result in the output string and adjust |
| 8579 | various state variables. Return a new reference to the object that |
| 8580 | was put in the output buffer in *result, or Py_None, if the mapping was |
| 8581 | undefined (in which case no character was written). |
| 8582 | The called must decref result. |
| 8583 | Return 0 on success, -1 on error. */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8584 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8585 | charmaptranslate_output(PyObject *input, Py_ssize_t ipos, |
| 8586 | PyObject *mapping, Py_UCS4 **output, |
| 8587 | Py_ssize_t *osize, Py_ssize_t *opos, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8588 | PyObject **res) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8589 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8590 | Py_UCS4 curinp = PyUnicode_READ_CHAR(input, ipos); |
| 8591 | if (charmaptranslate_lookup(curinp, mapping, res)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8592 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8593 | if (*res==NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8594 | /* not found => default to 1:1 mapping */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8595 | (*output)[(*opos)++] = curinp; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8596 | } |
| 8597 | else if (*res==Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8598 | ; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8599 | else if (PyLong_Check(*res)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8600 | /* 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] | 8601 | (*output)[(*opos)++] = (Py_UCS4)PyLong_AS_LONG(*res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8602 | } |
| 8603 | else if (PyUnicode_Check(*res)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8604 | Py_ssize_t repsize; |
| 8605 | if (PyUnicode_READY(*res) == -1) |
| 8606 | return -1; |
| 8607 | repsize = PyUnicode_GET_LENGTH(*res); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8608 | if (repsize==1) { |
| 8609 | /* 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] | 8610 | (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8611 | } |
| 8612 | else if (repsize!=0) { |
| 8613 | /* more than one character */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8614 | Py_ssize_t requiredsize = *opos + |
| 8615 | (PyUnicode_GET_LENGTH(input) - ipos) + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8616 | repsize - 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8617 | Py_ssize_t i; |
| 8618 | if (charmaptranslate_makespace(output, osize, requiredsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8619 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8620 | for(i = 0; i < repsize; i++) |
| 8621 | (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, i); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8622 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8623 | } |
| 8624 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8625 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8626 | return 0; |
| 8627 | } |
| 8628 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8629 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8630 | _PyUnicode_TranslateCharmap(PyObject *input, |
| 8631 | PyObject *mapping, |
| 8632 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8633 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8634 | /* input object */ |
| 8635 | char *idata; |
| 8636 | Py_ssize_t size, i; |
| 8637 | int kind; |
| 8638 | /* output buffer */ |
| 8639 | Py_UCS4 *output = NULL; |
| 8640 | Py_ssize_t osize; |
| 8641 | PyObject *res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8642 | /* current output position */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8643 | Py_ssize_t opos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8644 | char *reason = "character maps to <undefined>"; |
| 8645 | PyObject *errorHandler = NULL; |
| 8646 | PyObject *exc = NULL; |
| 8647 | /* the following variable is used for caching string comparisons |
| 8648 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 8649 | * 3=ignore, 4=xmlcharrefreplace */ |
| 8650 | int known_errorHandler = -1; |
| 8651 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8652 | if (mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8653 | PyErr_BadArgument(); |
| 8654 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8655 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8656 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8657 | if (PyUnicode_READY(input) == -1) |
| 8658 | return NULL; |
| 8659 | idata = (char*)PyUnicode_DATA(input); |
| 8660 | kind = PyUnicode_KIND(input); |
| 8661 | size = PyUnicode_GET_LENGTH(input); |
| 8662 | i = 0; |
| 8663 | |
| 8664 | if (size == 0) { |
| 8665 | Py_INCREF(input); |
| 8666 | return input; |
| 8667 | } |
| 8668 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8669 | /* allocate enough for a simple 1:1 translation without |
| 8670 | replacements, if we need more, we'll resize */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8671 | osize = size; |
| 8672 | output = PyMem_Malloc(osize * sizeof(Py_UCS4)); |
| 8673 | opos = 0; |
| 8674 | if (output == NULL) { |
| 8675 | PyErr_NoMemory(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8676 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8677 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8678 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8679 | while (i<size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8680 | /* try to encode it */ |
| 8681 | PyObject *x = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8682 | if (charmaptranslate_output(input, i, mapping, |
| 8683 | &output, &osize, &opos, &x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8684 | Py_XDECREF(x); |
| 8685 | goto onError; |
| 8686 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8687 | Py_XDECREF(x); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8688 | if (x!=Py_None) /* it worked => adjust input pointer */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8689 | ++i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8690 | else { /* untranslatable character */ |
| 8691 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
| 8692 | Py_ssize_t repsize; |
| 8693 | Py_ssize_t newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8694 | Py_ssize_t uni2; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8695 | /* startpos for collecting untranslatable chars */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8696 | Py_ssize_t collstart = i; |
| 8697 | Py_ssize_t collend = i+1; |
| 8698 | Py_ssize_t coll; |
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 | /* find all untranslatable characters */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8701 | while (collend < size) { |
| 8702 | if (charmaptranslate_lookup(PyUnicode_READ(kind,idata, collend), mapping, &x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8703 | goto onError; |
| 8704 | Py_XDECREF(x); |
| 8705 | if (x!=Py_None) |
| 8706 | break; |
| 8707 | ++collend; |
| 8708 | } |
| 8709 | /* cache callback name lookup |
| 8710 | * (if not done yet, i.e. it's the first error) */ |
| 8711 | if (known_errorHandler==-1) { |
| 8712 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 8713 | known_errorHandler = 1; |
| 8714 | else if (!strcmp(errors, "replace")) |
| 8715 | known_errorHandler = 2; |
| 8716 | else if (!strcmp(errors, "ignore")) |
| 8717 | known_errorHandler = 3; |
| 8718 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 8719 | known_errorHandler = 4; |
| 8720 | else |
| 8721 | known_errorHandler = 0; |
| 8722 | } |
| 8723 | switch (known_errorHandler) { |
| 8724 | case 1: /* strict */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8725 | raise_translate_exception(&exc, input, collstart, |
| 8726 | collend, reason); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8727 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8728 | case 2: /* replace */ |
| 8729 | /* 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] | 8730 | for (coll = collstart; coll<collend; coll++) |
| 8731 | output[opos++] = '?'; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8732 | /* fall through */ |
| 8733 | case 3: /* ignore */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8734 | i = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8735 | break; |
| 8736 | case 4: /* xmlcharrefreplace */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8737 | /* generate replacement (temporarily (mis)uses i) */ |
| 8738 | for (i = collstart; i < collend; ++i) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8739 | char buffer[2+29+1+1]; |
| 8740 | char *cp; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8741 | sprintf(buffer, "&#%d;", PyUnicode_READ(kind, idata, i)); |
| 8742 | if (charmaptranslate_makespace(&output, &osize, |
| 8743 | opos+strlen(buffer)+(size-collend))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8744 | goto onError; |
| 8745 | for (cp = buffer; *cp; ++cp) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8746 | output[opos++] = *cp; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8747 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8748 | i = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8749 | break; |
| 8750 | default: |
| 8751 | repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8752 | reason, input, &exc, |
| 8753 | collstart, collend, &newpos); |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 8754 | if (repunicode == NULL || _PyUnicode_READY_REPLACE(&repunicode)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8755 | goto onError; |
| 8756 | /* generate replacement */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8757 | repsize = PyUnicode_GET_LENGTH(repunicode); |
| 8758 | if (charmaptranslate_makespace(&output, &osize, |
| 8759 | opos+repsize+(size-collend))) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8760 | Py_DECREF(repunicode); |
| 8761 | goto onError; |
| 8762 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8763 | for (uni2 = 0; repsize-->0; ++uni2) |
| 8764 | output[opos++] = PyUnicode_READ_CHAR(repunicode, uni2); |
| 8765 | i = newpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8766 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8767 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8768 | } |
| 8769 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8770 | res = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, output, opos); |
| 8771 | if (!res) |
| 8772 | goto onError; |
| 8773 | PyMem_Free(output); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8774 | Py_XDECREF(exc); |
| 8775 | Py_XDECREF(errorHandler); |
| 8776 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8777 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8778 | onError: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8779 | PyMem_Free(output); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8780 | Py_XDECREF(exc); |
| 8781 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8782 | return NULL; |
| 8783 | } |
| 8784 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8785 | /* Deprecated. Use PyUnicode_Translate instead. */ |
| 8786 | PyObject * |
| 8787 | PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
| 8788 | Py_ssize_t size, |
| 8789 | PyObject *mapping, |
| 8790 | const char *errors) |
| 8791 | { |
| 8792 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 8793 | if (!unicode) |
| 8794 | return NULL; |
| 8795 | return _PyUnicode_TranslateCharmap(unicode, mapping, errors); |
| 8796 | } |
| 8797 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8798 | PyObject * |
| 8799 | PyUnicode_Translate(PyObject *str, |
| 8800 | PyObject *mapping, |
| 8801 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8802 | { |
| 8803 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8804 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8805 | str = PyUnicode_FromObject(str); |
| 8806 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8807 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8808 | result = _PyUnicode_TranslateCharmap(str, mapping, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8809 | Py_DECREF(str); |
| 8810 | return result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8811 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8812 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8813 | Py_XDECREF(str); |
| 8814 | return NULL; |
| 8815 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8816 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8817 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8818 | fix_decimal_and_space_to_ascii(PyObject *self) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8819 | { |
| 8820 | /* No need to call PyUnicode_READY(self) because this function is only |
| 8821 | called as a callback from fixup() which does it already. */ |
| 8822 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 8823 | const int kind = PyUnicode_KIND(self); |
| 8824 | void *data = PyUnicode_DATA(self); |
| 8825 | Py_UCS4 maxchar = 0, ch, fixed; |
| 8826 | Py_ssize_t i; |
| 8827 | |
| 8828 | for (i = 0; i < len; ++i) { |
| 8829 | ch = PyUnicode_READ(kind, data, i); |
| 8830 | fixed = 0; |
| 8831 | if (ch > 127) { |
| 8832 | if (Py_UNICODE_ISSPACE(ch)) |
| 8833 | fixed = ' '; |
| 8834 | else { |
| 8835 | const int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8836 | if (decimal >= 0) |
| 8837 | fixed = '0' + decimal; |
| 8838 | } |
| 8839 | if (fixed != 0) { |
| 8840 | if (fixed > maxchar) |
| 8841 | maxchar = fixed; |
| 8842 | PyUnicode_WRITE(kind, data, i, fixed); |
| 8843 | } |
| 8844 | else if (ch > maxchar) |
| 8845 | maxchar = ch; |
| 8846 | } |
| 8847 | else if (ch > maxchar) |
| 8848 | maxchar = ch; |
| 8849 | } |
| 8850 | |
| 8851 | return maxchar; |
| 8852 | } |
| 8853 | |
| 8854 | PyObject * |
| 8855 | _PyUnicode_TransformDecimalAndSpaceToASCII(PyObject *unicode) |
| 8856 | { |
| 8857 | if (!PyUnicode_Check(unicode)) { |
| 8858 | PyErr_BadInternalCall(); |
| 8859 | return NULL; |
| 8860 | } |
| 8861 | if (PyUnicode_READY(unicode) == -1) |
| 8862 | return NULL; |
| 8863 | if (PyUnicode_MAX_CHAR_VALUE(unicode) <= 127) { |
| 8864 | /* If the string is already ASCII, just return the same string */ |
| 8865 | Py_INCREF(unicode); |
| 8866 | return unicode; |
| 8867 | } |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8868 | return fixup(unicode, fix_decimal_and_space_to_ascii); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8869 | } |
| 8870 | |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8871 | PyObject * |
| 8872 | PyUnicode_TransformDecimalToASCII(Py_UNICODE *s, |
| 8873 | Py_ssize_t length) |
| 8874 | { |
| 8875 | PyObject *result; |
| 8876 | Py_UNICODE *p; /* write pointer into result */ |
| 8877 | Py_ssize_t i; |
| 8878 | /* Copy to a new string */ |
| 8879 | result = (PyObject *)_PyUnicode_New(length); |
| 8880 | Py_UNICODE_COPY(PyUnicode_AS_UNICODE(result), s, length); |
| 8881 | if (result == NULL) |
| 8882 | return result; |
| 8883 | p = PyUnicode_AS_UNICODE(result); |
| 8884 | /* Iterate over code points */ |
| 8885 | for (i = 0; i < length; i++) { |
| 8886 | Py_UNICODE ch =s[i]; |
| 8887 | if (ch > 127) { |
| 8888 | int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8889 | if (decimal >= 0) |
| 8890 | p[i] = '0' + decimal; |
| 8891 | } |
| 8892 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 8893 | #ifndef DONT_MAKE_RESULT_READY |
| 8894 | if (_PyUnicode_READY_REPLACE(&result)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8895 | Py_DECREF(result); |
| 8896 | return NULL; |
| 8897 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 8898 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 8899 | assert(_PyUnicode_CheckConsistency(result, 1)); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8900 | return result; |
| 8901 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8902 | /* --- Decimal Encoder ---------------------------------------------------- */ |
| 8903 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8904 | int |
| 8905 | PyUnicode_EncodeDecimal(Py_UNICODE *s, |
| 8906 | Py_ssize_t length, |
| 8907 | char *output, |
| 8908 | const char *errors) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8909 | { |
| 8910 | Py_UNICODE *p, *end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8911 | PyObject *errorHandler = NULL; |
| 8912 | PyObject *exc = NULL; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8913 | PyObject *unicode; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8914 | const char *encoding = "decimal"; |
| 8915 | const char *reason = "invalid decimal Unicode string"; |
| 8916 | /* the following variable is used for caching string comparisons |
| 8917 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 8918 | int known_errorHandler = -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8919 | |
| 8920 | if (output == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8921 | PyErr_BadArgument(); |
| 8922 | return -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8923 | } |
| 8924 | |
| 8925 | p = s; |
| 8926 | end = s + length; |
| 8927 | while (p < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8928 | register Py_UNICODE ch = *p; |
| 8929 | int decimal; |
| 8930 | PyObject *repunicode; |
| 8931 | Py_ssize_t repsize; |
| 8932 | Py_ssize_t newpos; |
| 8933 | Py_UNICODE *uni2; |
| 8934 | Py_UNICODE *collstart; |
| 8935 | Py_UNICODE *collend; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8936 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8937 | if (Py_UNICODE_ISSPACE(ch)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8938 | *output++ = ' '; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8939 | ++p; |
| 8940 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8941 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8942 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 8943 | if (decimal >= 0) { |
| 8944 | *output++ = '0' + decimal; |
| 8945 | ++p; |
| 8946 | continue; |
| 8947 | } |
| 8948 | if (0 < ch && ch < 256) { |
| 8949 | *output++ = (char)ch; |
| 8950 | ++p; |
| 8951 | continue; |
| 8952 | } |
| 8953 | /* All other characters are considered unencodable */ |
| 8954 | collstart = p; |
| 8955 | collend = p+1; |
| 8956 | while (collend < end) { |
| 8957 | if ((0 < *collend && *collend < 256) || |
| 8958 | !Py_UNICODE_ISSPACE(*collend) || |
| 8959 | Py_UNICODE_TODECIMAL(*collend)) |
| 8960 | break; |
| 8961 | } |
| 8962 | /* cache callback name lookup |
| 8963 | * (if not done yet, i.e. it's the first error) */ |
| 8964 | if (known_errorHandler==-1) { |
| 8965 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 8966 | known_errorHandler = 1; |
| 8967 | else if (!strcmp(errors, "replace")) |
| 8968 | known_errorHandler = 2; |
| 8969 | else if (!strcmp(errors, "ignore")) |
| 8970 | known_errorHandler = 3; |
| 8971 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 8972 | known_errorHandler = 4; |
| 8973 | else |
| 8974 | known_errorHandler = 0; |
| 8975 | } |
| 8976 | switch (known_errorHandler) { |
| 8977 | case 1: /* strict */ |
| 8978 | raise_encode_exception(&exc, encoding, s, length, collstart-s, collend-s, reason); |
| 8979 | goto onError; |
| 8980 | case 2: /* replace */ |
| 8981 | for (p = collstart; p < collend; ++p) |
| 8982 | *output++ = '?'; |
| 8983 | /* fall through */ |
| 8984 | case 3: /* ignore */ |
| 8985 | p = collend; |
| 8986 | break; |
| 8987 | case 4: /* xmlcharrefreplace */ |
| 8988 | /* generate replacement (temporarily (mis)uses p) */ |
| 8989 | for (p = collstart; p < collend; ++p) |
| 8990 | output += sprintf(output, "&#%d;", (int)*p); |
| 8991 | p = collend; |
| 8992 | break; |
| 8993 | default: |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8994 | unicode = PyUnicode_FromUnicode(s, length); |
| 8995 | if (unicode == NULL) |
| 8996 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8997 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8998 | encoding, reason, unicode, &exc, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8999 | collstart-s, collend-s, &newpos); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 9000 | Py_DECREF(unicode); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9001 | if (repunicode == NULL) |
| 9002 | goto onError; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 9003 | if (!PyUnicode_Check(repunicode)) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 9004 | /* Byte results not supported, since they have no decimal property. */ |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 9005 | PyErr_SetString(PyExc_TypeError, "error handler should return unicode"); |
| 9006 | Py_DECREF(repunicode); |
| 9007 | goto onError; |
| 9008 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9009 | /* generate replacement */ |
| 9010 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 9011 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
| 9012 | Py_UNICODE ch = *uni2; |
| 9013 | if (Py_UNICODE_ISSPACE(ch)) |
| 9014 | *output++ = ' '; |
| 9015 | else { |
| 9016 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 9017 | if (decimal >= 0) |
| 9018 | *output++ = '0' + decimal; |
| 9019 | else if (0 < ch && ch < 256) |
| 9020 | *output++ = (char)ch; |
| 9021 | else { |
| 9022 | Py_DECREF(repunicode); |
| 9023 | raise_encode_exception(&exc, encoding, |
| 9024 | s, length, collstart-s, collend-s, reason); |
| 9025 | goto onError; |
| 9026 | } |
| 9027 | } |
| 9028 | } |
| 9029 | p = s + newpos; |
| 9030 | Py_DECREF(repunicode); |
| 9031 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 9032 | } |
| 9033 | /* 0-terminate the output string */ |
| 9034 | *output++ = '\0'; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 9035 | Py_XDECREF(exc); |
| 9036 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 9037 | return 0; |
| 9038 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9039 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 9040 | Py_XDECREF(exc); |
| 9041 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 9042 | return -1; |
| 9043 | } |
| 9044 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9045 | /* --- Helpers ------------------------------------------------------------ */ |
| 9046 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9047 | static Py_ssize_t |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 9048 | any_find_slice(int direction, PyObject* s1, PyObject* s2, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9049 | Py_ssize_t start, |
| 9050 | Py_ssize_t end) |
| 9051 | { |
| 9052 | int kind1, kind2, kind; |
| 9053 | void *buf1, *buf2; |
| 9054 | Py_ssize_t len1, len2, result; |
| 9055 | |
| 9056 | kind1 = PyUnicode_KIND(s1); |
| 9057 | kind2 = PyUnicode_KIND(s2); |
| 9058 | kind = kind1 > kind2 ? kind1 : kind2; |
| 9059 | buf1 = PyUnicode_DATA(s1); |
| 9060 | buf2 = PyUnicode_DATA(s2); |
| 9061 | if (kind1 != kind) |
| 9062 | buf1 = _PyUnicode_AsKind(s1, kind); |
| 9063 | if (!buf1) |
| 9064 | return -2; |
| 9065 | if (kind2 != kind) |
| 9066 | buf2 = _PyUnicode_AsKind(s2, kind); |
| 9067 | if (!buf2) { |
| 9068 | if (kind1 != kind) PyMem_Free(buf1); |
| 9069 | return -2; |
| 9070 | } |
| 9071 | len1 = PyUnicode_GET_LENGTH(s1); |
| 9072 | len2 = PyUnicode_GET_LENGTH(s2); |
| 9073 | |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 9074 | if (direction > 0) { |
| 9075 | switch(kind) { |
| 9076 | case PyUnicode_1BYTE_KIND: |
| 9077 | if (PyUnicode_IS_ASCII(s1) && PyUnicode_IS_ASCII(s2)) |
| 9078 | result = asciilib_find_slice(buf1, len1, buf2, len2, start, end); |
| 9079 | else |
| 9080 | result = ucs1lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 9081 | break; |
| 9082 | case PyUnicode_2BYTE_KIND: |
| 9083 | result = ucs2lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 9084 | break; |
| 9085 | case PyUnicode_4BYTE_KIND: |
| 9086 | result = ucs4lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 9087 | break; |
| 9088 | default: |
| 9089 | assert(0); result = -2; |
| 9090 | } |
| 9091 | } |
| 9092 | else { |
| 9093 | switch(kind) { |
| 9094 | case PyUnicode_1BYTE_KIND: |
| 9095 | if (PyUnicode_IS_ASCII(s1) && PyUnicode_IS_ASCII(s2)) |
| 9096 | result = asciilib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 9097 | else |
| 9098 | result = ucs1lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 9099 | break; |
| 9100 | case PyUnicode_2BYTE_KIND: |
| 9101 | result = ucs2lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 9102 | break; |
| 9103 | case PyUnicode_4BYTE_KIND: |
| 9104 | result = ucs4lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 9105 | break; |
| 9106 | default: |
| 9107 | assert(0); result = -2; |
| 9108 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9109 | } |
| 9110 | |
| 9111 | if (kind1 != kind) |
| 9112 | PyMem_Free(buf1); |
| 9113 | if (kind2 != kind) |
| 9114 | PyMem_Free(buf2); |
| 9115 | |
| 9116 | return result; |
| 9117 | } |
| 9118 | |
| 9119 | Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9120 | _PyUnicode_InsertThousandsGrouping(PyObject *unicode, int kind, void *data, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9121 | Py_ssize_t n_buffer, |
| 9122 | void *digits, Py_ssize_t n_digits, |
| 9123 | Py_ssize_t min_width, |
| 9124 | const char *grouping, |
| 9125 | const char *thousands_sep) |
| 9126 | { |
| 9127 | switch(kind) { |
| 9128 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9129 | if (unicode != NULL && PyUnicode_IS_ASCII(unicode)) |
| 9130 | return _PyUnicode_ascii_InsertThousandsGrouping( |
| 9131 | (Py_UCS1*)data, n_buffer, (Py_UCS1*)digits, n_digits, |
| 9132 | min_width, grouping, thousands_sep); |
| 9133 | else |
| 9134 | return _PyUnicode_ucs1_InsertThousandsGrouping( |
| 9135 | (Py_UCS1*)data, n_buffer, (Py_UCS1*)digits, n_digits, |
| 9136 | min_width, grouping, thousands_sep); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9137 | case PyUnicode_2BYTE_KIND: |
| 9138 | return _PyUnicode_ucs2_InsertThousandsGrouping( |
| 9139 | (Py_UCS2*)data, n_buffer, (Py_UCS2*)digits, n_digits, |
| 9140 | min_width, grouping, thousands_sep); |
| 9141 | case PyUnicode_4BYTE_KIND: |
| 9142 | return _PyUnicode_ucs4_InsertThousandsGrouping( |
| 9143 | (Py_UCS4*)data, n_buffer, (Py_UCS4*)digits, n_digits, |
| 9144 | min_width, grouping, thousands_sep); |
| 9145 | } |
| 9146 | assert(0); |
| 9147 | return -1; |
| 9148 | } |
| 9149 | |
| 9150 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9151 | /* helper macro to fixup start/end slice values */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9152 | #define ADJUST_INDICES(start, end, len) \ |
| 9153 | if (end > len) \ |
| 9154 | end = len; \ |
| 9155 | else if (end < 0) { \ |
| 9156 | end += len; \ |
| 9157 | if (end < 0) \ |
| 9158 | end = 0; \ |
| 9159 | } \ |
| 9160 | if (start < 0) { \ |
| 9161 | start += len; \ |
| 9162 | if (start < 0) \ |
| 9163 | start = 0; \ |
| 9164 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9165 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9166 | Py_ssize_t |
| 9167 | PyUnicode_Count(PyObject *str, |
| 9168 | PyObject *substr, |
| 9169 | Py_ssize_t start, |
| 9170 | Py_ssize_t end) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9171 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9172 | Py_ssize_t result; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9173 | PyObject* str_obj; |
| 9174 | PyObject* sub_obj; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9175 | int kind1, kind2, kind; |
| 9176 | void *buf1 = NULL, *buf2 = NULL; |
| 9177 | Py_ssize_t len1, len2; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9178 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9179 | str_obj = PyUnicode_FromObject(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9180 | if (!str_obj || PyUnicode_READY(str_obj) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9181 | return -1; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9182 | sub_obj = PyUnicode_FromObject(substr); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 9183 | if (!sub_obj || PyUnicode_READY(sub_obj) == -1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9184 | Py_DECREF(str_obj); |
| 9185 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9186 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9187 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9188 | kind1 = PyUnicode_KIND(str_obj); |
| 9189 | kind2 = PyUnicode_KIND(sub_obj); |
| 9190 | kind = kind1 > kind2 ? kind1 : kind2; |
| 9191 | buf1 = PyUnicode_DATA(str_obj); |
| 9192 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9193 | buf1 = _PyUnicode_AsKind(str_obj, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9194 | if (!buf1) |
| 9195 | goto onError; |
| 9196 | buf2 = PyUnicode_DATA(sub_obj); |
| 9197 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9198 | buf2 = _PyUnicode_AsKind(sub_obj, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9199 | if (!buf2) |
| 9200 | goto onError; |
| 9201 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 9202 | len2 = PyUnicode_GET_LENGTH(sub_obj); |
| 9203 | |
| 9204 | ADJUST_INDICES(start, end, len1); |
| 9205 | switch(kind) { |
| 9206 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9207 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sub_obj)) |
| 9208 | result = asciilib_count( |
| 9209 | ((Py_UCS1*)buf1) + start, end - start, |
| 9210 | buf2, len2, PY_SSIZE_T_MAX |
| 9211 | ); |
| 9212 | else |
| 9213 | result = ucs1lib_count( |
| 9214 | ((Py_UCS1*)buf1) + start, end - start, |
| 9215 | buf2, len2, PY_SSIZE_T_MAX |
| 9216 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9217 | break; |
| 9218 | case PyUnicode_2BYTE_KIND: |
| 9219 | result = ucs2lib_count( |
| 9220 | ((Py_UCS2*)buf1) + start, end - start, |
| 9221 | buf2, len2, PY_SSIZE_T_MAX |
| 9222 | ); |
| 9223 | break; |
| 9224 | case PyUnicode_4BYTE_KIND: |
| 9225 | result = ucs4lib_count( |
| 9226 | ((Py_UCS4*)buf1) + start, end - start, |
| 9227 | buf2, len2, PY_SSIZE_T_MAX |
| 9228 | ); |
| 9229 | break; |
| 9230 | default: |
| 9231 | assert(0); result = 0; |
| 9232 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9233 | |
| 9234 | Py_DECREF(sub_obj); |
| 9235 | Py_DECREF(str_obj); |
| 9236 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9237 | if (kind1 != kind) |
| 9238 | PyMem_Free(buf1); |
| 9239 | if (kind2 != kind) |
| 9240 | PyMem_Free(buf2); |
| 9241 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9242 | return result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9243 | onError: |
| 9244 | Py_DECREF(sub_obj); |
| 9245 | Py_DECREF(str_obj); |
| 9246 | if (kind1 != kind && buf1) |
| 9247 | PyMem_Free(buf1); |
| 9248 | if (kind2 != kind && buf2) |
| 9249 | PyMem_Free(buf2); |
| 9250 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9251 | } |
| 9252 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9253 | Py_ssize_t |
| 9254 | PyUnicode_Find(PyObject *str, |
| 9255 | PyObject *sub, |
| 9256 | Py_ssize_t start, |
| 9257 | Py_ssize_t end, |
| 9258 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9259 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9260 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9261 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9262 | str = PyUnicode_FromObject(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9263 | if (!str || PyUnicode_READY(str) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9264 | return -2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9265 | sub = PyUnicode_FromObject(sub); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9266 | if (!sub || PyUnicode_READY(sub) == -1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9267 | Py_DECREF(str); |
| 9268 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9269 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9270 | |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 9271 | result = any_find_slice(direction, |
| 9272 | str, sub, start, end |
| 9273 | ); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9274 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9275 | Py_DECREF(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9276 | Py_DECREF(sub); |
| 9277 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9278 | return result; |
| 9279 | } |
| 9280 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9281 | Py_ssize_t |
| 9282 | PyUnicode_FindChar(PyObject *str, Py_UCS4 ch, |
| 9283 | Py_ssize_t start, Py_ssize_t end, |
| 9284 | int direction) |
| 9285 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9286 | int kind; |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 9287 | Py_ssize_t result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9288 | if (PyUnicode_READY(str) == -1) |
| 9289 | return -2; |
Victor Stinner | 267aa24 | 2011-10-02 01:08:37 +0200 | [diff] [blame] | 9290 | if (start < 0 || end < 0) { |
| 9291 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 9292 | return -2; |
| 9293 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9294 | if (end > PyUnicode_GET_LENGTH(str)) |
| 9295 | end = PyUnicode_GET_LENGTH(str); |
| 9296 | kind = PyUnicode_KIND(str); |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 9297 | result = findchar(PyUnicode_1BYTE_DATA(str) + kind*start, |
| 9298 | kind, end-start, ch, direction); |
| 9299 | if (result == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9300 | return -1; |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 9301 | else |
| 9302 | return start + result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9303 | } |
| 9304 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9305 | static int |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9306 | tailmatch(PyObject *self, |
| 9307 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9308 | Py_ssize_t start, |
| 9309 | Py_ssize_t end, |
| 9310 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9311 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9312 | int kind_self; |
| 9313 | int kind_sub; |
| 9314 | void *data_self; |
| 9315 | void *data_sub; |
| 9316 | Py_ssize_t offset; |
| 9317 | Py_ssize_t i; |
| 9318 | Py_ssize_t end_sub; |
| 9319 | |
| 9320 | if (PyUnicode_READY(self) == -1 || |
| 9321 | PyUnicode_READY(substring) == -1) |
| 9322 | return 0; |
| 9323 | |
| 9324 | if (PyUnicode_GET_LENGTH(substring) == 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9325 | return 1; |
| 9326 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9327 | ADJUST_INDICES(start, end, PyUnicode_GET_LENGTH(self)); |
| 9328 | end -= PyUnicode_GET_LENGTH(substring); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9329 | if (end < start) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9330 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9331 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9332 | kind_self = PyUnicode_KIND(self); |
| 9333 | data_self = PyUnicode_DATA(self); |
| 9334 | kind_sub = PyUnicode_KIND(substring); |
| 9335 | data_sub = PyUnicode_DATA(substring); |
| 9336 | end_sub = PyUnicode_GET_LENGTH(substring) - 1; |
| 9337 | |
| 9338 | if (direction > 0) |
| 9339 | offset = end; |
| 9340 | else |
| 9341 | offset = start; |
| 9342 | |
| 9343 | if (PyUnicode_READ(kind_self, data_self, offset) == |
| 9344 | PyUnicode_READ(kind_sub, data_sub, 0) && |
| 9345 | PyUnicode_READ(kind_self, data_self, offset + end_sub) == |
| 9346 | PyUnicode_READ(kind_sub, data_sub, end_sub)) { |
| 9347 | /* If both are of the same kind, memcmp is sufficient */ |
| 9348 | if (kind_self == kind_sub) { |
| 9349 | return ! memcmp((char *)data_self + |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9350 | (offset * PyUnicode_KIND(substring)), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9351 | data_sub, |
| 9352 | PyUnicode_GET_LENGTH(substring) * |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9353 | PyUnicode_KIND(substring)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9354 | } |
| 9355 | /* otherwise we have to compare each character by first accesing it */ |
| 9356 | else { |
| 9357 | /* We do not need to compare 0 and len(substring)-1 because |
| 9358 | the if statement above ensured already that they are equal |
| 9359 | when we end up here. */ |
| 9360 | // TODO: honor direction and do a forward or backwards search |
| 9361 | for (i = 1; i < end_sub; ++i) { |
| 9362 | if (PyUnicode_READ(kind_self, data_self, offset + i) != |
| 9363 | PyUnicode_READ(kind_sub, data_sub, i)) |
| 9364 | return 0; |
| 9365 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9366 | return 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9367 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9368 | } |
| 9369 | |
| 9370 | return 0; |
| 9371 | } |
| 9372 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9373 | Py_ssize_t |
| 9374 | PyUnicode_Tailmatch(PyObject *str, |
| 9375 | PyObject *substr, |
| 9376 | Py_ssize_t start, |
| 9377 | Py_ssize_t end, |
| 9378 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9379 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9380 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9381 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9382 | str = PyUnicode_FromObject(str); |
| 9383 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9384 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9385 | substr = PyUnicode_FromObject(substr); |
| 9386 | if (substr == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9387 | Py_DECREF(str); |
| 9388 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9389 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9390 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9391 | result = tailmatch(str, substr, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9392 | start, end, direction); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9393 | Py_DECREF(str); |
| 9394 | Py_DECREF(substr); |
| 9395 | return result; |
| 9396 | } |
| 9397 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9398 | /* Apply fixfct filter to the Unicode object self and return a |
| 9399 | reference to the modified object */ |
| 9400 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9401 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9402 | fixup(PyObject *self, |
| 9403 | Py_UCS4 (*fixfct)(PyObject *s)) |
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 | PyObject *u; |
| 9406 | Py_UCS4 maxchar_old, maxchar_new = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9407 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9408 | if (PyUnicode_READY(self) == -1) |
| 9409 | return NULL; |
| 9410 | maxchar_old = PyUnicode_MAX_CHAR_VALUE(self); |
| 9411 | u = PyUnicode_New(PyUnicode_GET_LENGTH(self), |
| 9412 | maxchar_old); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9413 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9414 | return NULL; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9415 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9416 | Py_MEMCPY(PyUnicode_1BYTE_DATA(u), PyUnicode_1BYTE_DATA(self), |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9417 | PyUnicode_GET_LENGTH(u) * PyUnicode_KIND(u)); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9418 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9419 | /* fix functions return the new maximum character in a string, |
| 9420 | if the kind of the resulting unicode object does not change, |
| 9421 | everything is fine. Otherwise we need to change the string kind |
| 9422 | and re-run the fix function. */ |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9423 | maxchar_new = fixfct(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9424 | if (maxchar_new == 0) |
| 9425 | /* do nothing, keep maxchar_new at 0 which means no changes. */; |
| 9426 | else if (maxchar_new <= 127) |
| 9427 | maxchar_new = 127; |
| 9428 | else if (maxchar_new <= 255) |
| 9429 | maxchar_new = 255; |
| 9430 | else if (maxchar_new <= 65535) |
| 9431 | maxchar_new = 65535; |
| 9432 | else |
| 9433 | maxchar_new = 1114111; /* 0x10ffff */ |
| 9434 | |
| 9435 | if (!maxchar_new && PyUnicode_CheckExact(self)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9436 | /* fixfct should return TRUE if it modified the buffer. If |
| 9437 | FALSE, return a reference to the original buffer instead |
| 9438 | (to save space, not time) */ |
| 9439 | Py_INCREF(self); |
| 9440 | Py_DECREF(u); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9441 | return self; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9442 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9443 | else if (maxchar_new == maxchar_old) { |
| 9444 | return u; |
| 9445 | } |
| 9446 | else { |
| 9447 | /* In case the maximum character changed, we need to |
| 9448 | convert the string to the new category. */ |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9449 | PyObject *v = PyUnicode_New(PyUnicode_GET_LENGTH(self), maxchar_new); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9450 | if (v == NULL) { |
| 9451 | Py_DECREF(u); |
| 9452 | return NULL; |
| 9453 | } |
| 9454 | if (maxchar_new > maxchar_old) { |
| 9455 | /* If the maxchar increased so that the kind changed, not all |
| 9456 | characters are representable anymore and we need to fix the |
| 9457 | string again. This only happens in very few cases. */ |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9458 | copy_characters(v, 0, self, 0, PyUnicode_GET_LENGTH(self)); |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9459 | maxchar_old = fixfct(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9460 | assert(maxchar_old > 0 && maxchar_old <= maxchar_new); |
| 9461 | } |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9462 | else { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9463 | copy_characters(v, 0, u, 0, PyUnicode_GET_LENGTH(self)); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9464 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9465 | |
| 9466 | Py_DECREF(u); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 9467 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9468 | return v; |
| 9469 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9470 | } |
| 9471 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9472 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9473 | fixupper(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9474 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9475 | /* No need to call PyUnicode_READY(self) because this function is only |
| 9476 | called as a callback from fixup() which does it already. */ |
| 9477 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 9478 | const int kind = PyUnicode_KIND(self); |
| 9479 | void *data = PyUnicode_DATA(self); |
| 9480 | int touched = 0; |
| 9481 | Py_UCS4 maxchar = 0; |
| 9482 | Py_ssize_t i; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9483 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9484 | for (i = 0; i < len; ++i) { |
| 9485 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 9486 | const Py_UCS4 up = Py_UNICODE_TOUPPER(ch); |
| 9487 | if (up != ch) { |
| 9488 | if (up > maxchar) |
| 9489 | maxchar = up; |
| 9490 | PyUnicode_WRITE(kind, data, i, up); |
| 9491 | touched = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9492 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9493 | else if (ch > maxchar) |
| 9494 | maxchar = ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9495 | } |
| 9496 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9497 | if (touched) |
| 9498 | return maxchar; |
| 9499 | else |
| 9500 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9501 | } |
| 9502 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9503 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9504 | fixlower(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9505 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9506 | /* No need to call PyUnicode_READY(self) because fixup() which does it. */ |
| 9507 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 9508 | const int kind = PyUnicode_KIND(self); |
| 9509 | void *data = PyUnicode_DATA(self); |
| 9510 | int touched = 0; |
| 9511 | Py_UCS4 maxchar = 0; |
| 9512 | Py_ssize_t i; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9513 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9514 | for(i = 0; i < len; ++i) { |
| 9515 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 9516 | const Py_UCS4 lo = Py_UNICODE_TOLOWER(ch); |
| 9517 | if (lo != ch) { |
| 9518 | if (lo > maxchar) |
| 9519 | maxchar = lo; |
| 9520 | PyUnicode_WRITE(kind, data, i, lo); |
| 9521 | touched = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9522 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9523 | else if (ch > maxchar) |
| 9524 | maxchar = ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9525 | } |
| 9526 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9527 | if (touched) |
| 9528 | return maxchar; |
| 9529 | else |
| 9530 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9531 | } |
| 9532 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9533 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9534 | fixswapcase(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9535 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9536 | /* No need to call PyUnicode_READY(self) because fixup() which does it. */ |
| 9537 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 9538 | const int kind = PyUnicode_KIND(self); |
| 9539 | void *data = PyUnicode_DATA(self); |
| 9540 | int touched = 0; |
| 9541 | Py_UCS4 maxchar = 0; |
| 9542 | Py_ssize_t i; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9543 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9544 | for(i = 0; i < len; ++i) { |
| 9545 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 9546 | Py_UCS4 nu = 0; |
| 9547 | |
| 9548 | if (Py_UNICODE_ISUPPER(ch)) |
| 9549 | nu = Py_UNICODE_TOLOWER(ch); |
| 9550 | else if (Py_UNICODE_ISLOWER(ch)) |
| 9551 | nu = Py_UNICODE_TOUPPER(ch); |
| 9552 | |
| 9553 | if (nu != 0) { |
| 9554 | if (nu > maxchar) |
| 9555 | maxchar = nu; |
| 9556 | PyUnicode_WRITE(kind, data, i, nu); |
| 9557 | touched = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9558 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9559 | else if (ch > maxchar) |
| 9560 | maxchar = ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9561 | } |
| 9562 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9563 | if (touched) |
| 9564 | return maxchar; |
| 9565 | else |
| 9566 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9567 | } |
| 9568 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9569 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9570 | fixcapitalize(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9571 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9572 | /* No need to call PyUnicode_READY(self) because fixup() which does it. */ |
| 9573 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 9574 | const int kind = PyUnicode_KIND(self); |
| 9575 | void *data = PyUnicode_DATA(self); |
| 9576 | int touched = 0; |
| 9577 | Py_UCS4 maxchar = 0; |
| 9578 | Py_ssize_t i = 0; |
| 9579 | Py_UCS4 ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9580 | |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9581 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9582 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9583 | |
| 9584 | ch = PyUnicode_READ(kind, data, i); |
| 9585 | if (!Py_UNICODE_ISUPPER(ch)) { |
| 9586 | maxchar = Py_UNICODE_TOUPPER(ch); |
| 9587 | PyUnicode_WRITE(kind, data, i, maxchar); |
| 9588 | touched = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9589 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9590 | ++i; |
| 9591 | for(; i < len; ++i) { |
| 9592 | ch = PyUnicode_READ(kind, data, i); |
| 9593 | if (!Py_UNICODE_ISLOWER(ch)) { |
| 9594 | const Py_UCS4 lo = Py_UNICODE_TOLOWER(ch); |
| 9595 | if (lo > maxchar) |
| 9596 | maxchar = lo; |
| 9597 | PyUnicode_WRITE(kind, data, i, lo); |
| 9598 | touched = 1; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9599 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9600 | else if (ch > maxchar) |
| 9601 | maxchar = ch; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9602 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9603 | |
| 9604 | if (touched) |
| 9605 | return maxchar; |
| 9606 | else |
| 9607 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9608 | } |
| 9609 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9610 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9611 | fixtitle(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9612 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9613 | /* No need to call PyUnicode_READY(self) because fixup() which does it. */ |
| 9614 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 9615 | const int kind = PyUnicode_KIND(self); |
| 9616 | void *data = PyUnicode_DATA(self); |
| 9617 | Py_UCS4 maxchar = 0; |
| 9618 | Py_ssize_t i = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9619 | int previous_is_cased; |
| 9620 | |
| 9621 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9622 | if (len == 1) { |
| 9623 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 9624 | const Py_UCS4 ti = Py_UNICODE_TOTITLE(ch); |
| 9625 | if (ti != ch) { |
| 9626 | PyUnicode_WRITE(kind, data, i, ti); |
| 9627 | return ti; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9628 | } |
| 9629 | else |
| 9630 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9631 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9632 | previous_is_cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9633 | for(; i < len; ++i) { |
| 9634 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 9635 | Py_UCS4 nu; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9636 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9637 | if (previous_is_cased) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9638 | nu = Py_UNICODE_TOLOWER(ch); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9639 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9640 | nu = Py_UNICODE_TOTITLE(ch); |
| 9641 | |
| 9642 | if (nu > maxchar) |
| 9643 | maxchar = nu; |
| 9644 | PyUnicode_WRITE(kind, data, i, nu); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9645 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9646 | if (Py_UNICODE_ISLOWER(ch) || |
| 9647 | Py_UNICODE_ISUPPER(ch) || |
| 9648 | Py_UNICODE_ISTITLE(ch)) |
| 9649 | previous_is_cased = 1; |
| 9650 | else |
| 9651 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9652 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9653 | return maxchar; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9654 | } |
| 9655 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9656 | PyObject * |
| 9657 | PyUnicode_Join(PyObject *separator, PyObject *seq) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9658 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9659 | PyObject *sep = NULL; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9660 | Py_ssize_t seplen; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9661 | PyObject *res = NULL; /* the result */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9662 | PyObject *fseq; /* PySequence_Fast(seq) */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9663 | Py_ssize_t seqlen; /* len(fseq) -- number of items in sequence */ |
| 9664 | PyObject **items; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9665 | PyObject *item; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9666 | Py_ssize_t sz, i, res_offset; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9667 | Py_UCS4 maxchar; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9668 | Py_UCS4 item_maxchar; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9669 | int use_memcpy; |
| 9670 | unsigned char *res_data = NULL, *sep_data = NULL; |
| 9671 | PyObject *last_obj; |
| 9672 | unsigned int kind = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9673 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9674 | fseq = PySequence_Fast(seq, ""); |
| 9675 | if (fseq == NULL) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9676 | return NULL; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9677 | } |
| 9678 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9679 | /* NOTE: the following code can't call back into Python code, |
| 9680 | * so we are sure that fseq won't be mutated. |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 9681 | */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9682 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9683 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 9684 | /* If empty sequence, return u"". */ |
| 9685 | if (seqlen == 0) { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9686 | Py_DECREF(fseq); |
| 9687 | Py_INCREF(unicode_empty); |
| 9688 | res = unicode_empty; |
| 9689 | return res; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9690 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9691 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9692 | /* If singleton sequence with an exact Unicode, return that. */ |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9693 | last_obj = NULL; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9694 | items = PySequence_Fast_ITEMS(fseq); |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9695 | if (seqlen == 1) { |
| 9696 | if (PyUnicode_CheckExact(items[0])) { |
| 9697 | res = items[0]; |
| 9698 | Py_INCREF(res); |
| 9699 | Py_DECREF(fseq); |
| 9700 | return res; |
| 9701 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9702 | seplen = 0; |
Victor Stinner | c6f0df7 | 2011-10-06 15:58:54 +0200 | [diff] [blame] | 9703 | maxchar = 0; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9704 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9705 | else { |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9706 | /* Set up sep and seplen */ |
| 9707 | if (separator == NULL) { |
| 9708 | /* fall back to a blank space separator */ |
| 9709 | sep = PyUnicode_FromOrdinal(' '); |
| 9710 | if (!sep) |
| 9711 | goto onError; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9712 | seplen = 1; |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9713 | maxchar = 32; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9714 | } |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9715 | else { |
| 9716 | if (!PyUnicode_Check(separator)) { |
| 9717 | PyErr_Format(PyExc_TypeError, |
| 9718 | "separator: expected str instance," |
| 9719 | " %.80s found", |
| 9720 | Py_TYPE(separator)->tp_name); |
| 9721 | goto onError; |
| 9722 | } |
| 9723 | if (PyUnicode_READY(separator)) |
| 9724 | goto onError; |
| 9725 | sep = separator; |
| 9726 | seplen = PyUnicode_GET_LENGTH(separator); |
| 9727 | maxchar = PyUnicode_MAX_CHAR_VALUE(separator); |
| 9728 | /* inc refcount to keep this code path symmetric with the |
| 9729 | above case of a blank separator */ |
| 9730 | Py_INCREF(sep); |
| 9731 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9732 | last_obj = sep; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9733 | } |
| 9734 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9735 | /* There are at least two things to join, or else we have a subclass |
| 9736 | * of str in the sequence. |
| 9737 | * Do a pre-pass to figure out the total amount of space we'll |
| 9738 | * need (sz), and see whether all argument are strings. |
| 9739 | */ |
| 9740 | sz = 0; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9741 | #ifdef Py_DEBUG |
| 9742 | use_memcpy = 0; |
| 9743 | #else |
| 9744 | use_memcpy = 1; |
| 9745 | #endif |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9746 | for (i = 0; i < seqlen; i++) { |
| 9747 | const Py_ssize_t old_sz = sz; |
| 9748 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9749 | if (!PyUnicode_Check(item)) { |
| 9750 | PyErr_Format(PyExc_TypeError, |
| 9751 | "sequence item %zd: expected str instance," |
| 9752 | " %.80s found", |
| 9753 | i, Py_TYPE(item)->tp_name); |
| 9754 | goto onError; |
| 9755 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9756 | if (PyUnicode_READY(item) == -1) |
| 9757 | goto onError; |
| 9758 | sz += PyUnicode_GET_LENGTH(item); |
| 9759 | item_maxchar = PyUnicode_MAX_CHAR_VALUE(item); |
Victor Stinner | c6f0df7 | 2011-10-06 15:58:54 +0200 | [diff] [blame] | 9760 | maxchar = Py_MAX(maxchar, item_maxchar); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9761 | if (i != 0) |
| 9762 | sz += seplen; |
| 9763 | if (sz < old_sz || sz > PY_SSIZE_T_MAX) { |
| 9764 | PyErr_SetString(PyExc_OverflowError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9765 | "join() result is too long for a Python string"); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9766 | goto onError; |
| 9767 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9768 | if (use_memcpy && last_obj != NULL) { |
| 9769 | if (PyUnicode_KIND(last_obj) != PyUnicode_KIND(item)) |
| 9770 | use_memcpy = 0; |
| 9771 | } |
| 9772 | last_obj = item; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9773 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9774 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9775 | res = PyUnicode_New(sz, maxchar); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9776 | if (res == NULL) |
| 9777 | goto onError; |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 9778 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9779 | /* Catenate everything. */ |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9780 | #ifdef Py_DEBUG |
| 9781 | use_memcpy = 0; |
| 9782 | #else |
| 9783 | if (use_memcpy) { |
| 9784 | res_data = PyUnicode_1BYTE_DATA(res); |
| 9785 | kind = PyUnicode_KIND(res); |
| 9786 | if (seplen != 0) |
| 9787 | sep_data = PyUnicode_1BYTE_DATA(sep); |
| 9788 | } |
| 9789 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9790 | for (i = 0, res_offset = 0; i < seqlen; ++i) { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9791 | Py_ssize_t itemlen; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9792 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9793 | /* Copy item, and maybe the separator. */ |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 9794 | if (i && seplen != 0) { |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9795 | if (use_memcpy) { |
| 9796 | Py_MEMCPY(res_data, |
| 9797 | sep_data, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9798 | kind * seplen); |
| 9799 | res_data += kind * seplen; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9800 | } |
| 9801 | else { |
| 9802 | copy_characters(res, res_offset, sep, 0, seplen); |
| 9803 | res_offset += seplen; |
| 9804 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9805 | } |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 9806 | itemlen = PyUnicode_GET_LENGTH(item); |
| 9807 | if (itemlen != 0) { |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9808 | if (use_memcpy) { |
| 9809 | Py_MEMCPY(res_data, |
| 9810 | PyUnicode_DATA(item), |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9811 | kind * itemlen); |
| 9812 | res_data += kind * itemlen; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9813 | } |
| 9814 | else { |
| 9815 | copy_characters(res, res_offset, item, 0, itemlen); |
| 9816 | res_offset += itemlen; |
| 9817 | } |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 9818 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9819 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9820 | if (use_memcpy) |
| 9821 | assert(res_data == PyUnicode_1BYTE_DATA(res) |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9822 | + kind * PyUnicode_GET_LENGTH(res)); |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9823 | else |
| 9824 | assert(res_offset == PyUnicode_GET_LENGTH(res)); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9825 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9826 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9827 | Py_XDECREF(sep); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 9828 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9829 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9830 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9831 | onError: |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9832 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9833 | Py_XDECREF(sep); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9834 | Py_XDECREF(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9835 | return NULL; |
| 9836 | } |
| 9837 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9838 | #define FILL(kind, data, value, start, length) \ |
| 9839 | do { \ |
| 9840 | Py_ssize_t i_ = 0; \ |
| 9841 | assert(kind != PyUnicode_WCHAR_KIND); \ |
| 9842 | switch ((kind)) { \ |
| 9843 | case PyUnicode_1BYTE_KIND: { \ |
| 9844 | unsigned char * to_ = (unsigned char *)((data)) + (start); \ |
| 9845 | memset(to_, (unsigned char)value, length); \ |
| 9846 | break; \ |
| 9847 | } \ |
| 9848 | case PyUnicode_2BYTE_KIND: { \ |
| 9849 | Py_UCS2 * to_ = (Py_UCS2 *)((data)) + (start); \ |
| 9850 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 9851 | break; \ |
| 9852 | } \ |
| 9853 | default: { \ |
| 9854 | Py_UCS4 * to_ = (Py_UCS4 *)((data)) + (start); \ |
| 9855 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 9856 | break; \ |
| 9857 | } \ |
| 9858 | } \ |
| 9859 | } while (0) |
| 9860 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9861 | static PyObject * |
| 9862 | pad(PyObject *self, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9863 | Py_ssize_t left, |
| 9864 | Py_ssize_t right, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9865 | Py_UCS4 fill) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9866 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9867 | PyObject *u; |
| 9868 | Py_UCS4 maxchar; |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9869 | int kind; |
| 9870 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9871 | |
| 9872 | if (left < 0) |
| 9873 | left = 0; |
| 9874 | if (right < 0) |
| 9875 | right = 0; |
| 9876 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 9877 | if (left == 0 && right == 0 && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9878 | Py_INCREF(self); |
| 9879 | return self; |
| 9880 | } |
| 9881 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9882 | if (left > PY_SSIZE_T_MAX - _PyUnicode_LENGTH(self) || |
| 9883 | right > PY_SSIZE_T_MAX - (left + _PyUnicode_LENGTH(self))) { |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 9884 | PyErr_SetString(PyExc_OverflowError, "padded string is too long"); |
| 9885 | return NULL; |
| 9886 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9887 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
| 9888 | if (fill > maxchar) |
| 9889 | maxchar = fill; |
| 9890 | u = PyUnicode_New(left + _PyUnicode_LENGTH(self) + right, maxchar); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9891 | if (!u) |
| 9892 | return NULL; |
| 9893 | |
| 9894 | kind = PyUnicode_KIND(u); |
| 9895 | data = PyUnicode_DATA(u); |
| 9896 | if (left) |
| 9897 | FILL(kind, data, fill, 0, left); |
| 9898 | if (right) |
| 9899 | FILL(kind, data, fill, left + _PyUnicode_LENGTH(self), right); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9900 | copy_characters(u, left, self, 0, _PyUnicode_LENGTH(self)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 9901 | assert(_PyUnicode_CheckConsistency(u, 1)); |
| 9902 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9903 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9904 | #undef FILL |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9905 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9906 | PyObject * |
| 9907 | PyUnicode_Splitlines(PyObject *string, int keepends) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9908 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9909 | PyObject *list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9910 | |
| 9911 | string = PyUnicode_FromObject(string); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9912 | if (string == NULL || PyUnicode_READY(string) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9913 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9914 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9915 | switch(PyUnicode_KIND(string)) { |
| 9916 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9917 | if (PyUnicode_IS_ASCII(string)) |
| 9918 | list = asciilib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9919 | string, PyUnicode_1BYTE_DATA(string), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9920 | PyUnicode_GET_LENGTH(string), keepends); |
| 9921 | else |
| 9922 | list = ucs1lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9923 | string, PyUnicode_1BYTE_DATA(string), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9924 | PyUnicode_GET_LENGTH(string), keepends); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9925 | break; |
| 9926 | case PyUnicode_2BYTE_KIND: |
| 9927 | list = ucs2lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9928 | string, PyUnicode_2BYTE_DATA(string), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9929 | PyUnicode_GET_LENGTH(string), keepends); |
| 9930 | break; |
| 9931 | case PyUnicode_4BYTE_KIND: |
| 9932 | list = ucs4lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9933 | string, PyUnicode_4BYTE_DATA(string), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9934 | PyUnicode_GET_LENGTH(string), keepends); |
| 9935 | break; |
| 9936 | default: |
| 9937 | assert(0); |
| 9938 | list = 0; |
| 9939 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9940 | Py_DECREF(string); |
| 9941 | return list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9942 | } |
| 9943 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9944 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9945 | split(PyObject *self, |
| 9946 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9947 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9948 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9949 | int kind1, kind2, kind; |
| 9950 | void *buf1, *buf2; |
| 9951 | Py_ssize_t len1, len2; |
| 9952 | PyObject* out; |
| 9953 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9954 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9955 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9956 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9957 | if (PyUnicode_READY(self) == -1) |
| 9958 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9959 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9960 | if (substring == NULL) |
| 9961 | switch(PyUnicode_KIND(self)) { |
| 9962 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9963 | if (PyUnicode_IS_ASCII(self)) |
| 9964 | return asciilib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9965 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9966 | PyUnicode_GET_LENGTH(self), maxcount |
| 9967 | ); |
| 9968 | else |
| 9969 | return ucs1lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9970 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9971 | PyUnicode_GET_LENGTH(self), maxcount |
| 9972 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9973 | case PyUnicode_2BYTE_KIND: |
| 9974 | return ucs2lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9975 | self, PyUnicode_2BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9976 | PyUnicode_GET_LENGTH(self), maxcount |
| 9977 | ); |
| 9978 | case PyUnicode_4BYTE_KIND: |
| 9979 | return ucs4lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9980 | self, PyUnicode_4BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9981 | PyUnicode_GET_LENGTH(self), maxcount |
| 9982 | ); |
| 9983 | default: |
| 9984 | assert(0); |
| 9985 | return NULL; |
| 9986 | } |
| 9987 | |
| 9988 | if (PyUnicode_READY(substring) == -1) |
| 9989 | return NULL; |
| 9990 | |
| 9991 | kind1 = PyUnicode_KIND(self); |
| 9992 | kind2 = PyUnicode_KIND(substring); |
| 9993 | kind = kind1 > kind2 ? kind1 : kind2; |
| 9994 | buf1 = PyUnicode_DATA(self); |
| 9995 | buf2 = PyUnicode_DATA(substring); |
| 9996 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9997 | buf1 = _PyUnicode_AsKind(self, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9998 | if (!buf1) |
| 9999 | return NULL; |
| 10000 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10001 | buf2 = _PyUnicode_AsKind(substring, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10002 | if (!buf2) { |
| 10003 | if (kind1 != kind) PyMem_Free(buf1); |
| 10004 | return NULL; |
| 10005 | } |
| 10006 | len1 = PyUnicode_GET_LENGTH(self); |
| 10007 | len2 = PyUnicode_GET_LENGTH(substring); |
| 10008 | |
| 10009 | switch(kind) { |
| 10010 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10011 | if (PyUnicode_IS_ASCII(self) && PyUnicode_IS_ASCII(substring)) |
| 10012 | out = asciilib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10013 | self, buf1, len1, buf2, len2, maxcount); |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10014 | else |
| 10015 | out = ucs1lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10016 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10017 | break; |
| 10018 | case PyUnicode_2BYTE_KIND: |
| 10019 | out = ucs2lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10020 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10021 | break; |
| 10022 | case PyUnicode_4BYTE_KIND: |
| 10023 | out = ucs4lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10024 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10025 | break; |
| 10026 | default: |
| 10027 | out = NULL; |
| 10028 | } |
| 10029 | if (kind1 != kind) |
| 10030 | PyMem_Free(buf1); |
| 10031 | if (kind2 != kind) |
| 10032 | PyMem_Free(buf2); |
| 10033 | return out; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10034 | } |
| 10035 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10036 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10037 | rsplit(PyObject *self, |
| 10038 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10039 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10040 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10041 | int kind1, kind2, kind; |
| 10042 | void *buf1, *buf2; |
| 10043 | Py_ssize_t len1, len2; |
| 10044 | PyObject* out; |
| 10045 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10046 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10047 | maxcount = PY_SSIZE_T_MAX; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10048 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10049 | if (PyUnicode_READY(self) == -1) |
| 10050 | return NULL; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10051 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10052 | if (substring == NULL) |
| 10053 | switch(PyUnicode_KIND(self)) { |
| 10054 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10055 | if (PyUnicode_IS_ASCII(self)) |
| 10056 | return asciilib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10057 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10058 | PyUnicode_GET_LENGTH(self), maxcount |
| 10059 | ); |
| 10060 | else |
| 10061 | return ucs1lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10062 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10063 | PyUnicode_GET_LENGTH(self), maxcount |
| 10064 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10065 | case PyUnicode_2BYTE_KIND: |
| 10066 | return ucs2lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10067 | self, PyUnicode_2BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10068 | PyUnicode_GET_LENGTH(self), maxcount |
| 10069 | ); |
| 10070 | case PyUnicode_4BYTE_KIND: |
| 10071 | return ucs4lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10072 | self, PyUnicode_4BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10073 | PyUnicode_GET_LENGTH(self), maxcount |
| 10074 | ); |
| 10075 | default: |
| 10076 | assert(0); |
| 10077 | return NULL; |
| 10078 | } |
| 10079 | |
| 10080 | if (PyUnicode_READY(substring) == -1) |
| 10081 | return NULL; |
| 10082 | |
| 10083 | kind1 = PyUnicode_KIND(self); |
| 10084 | kind2 = PyUnicode_KIND(substring); |
| 10085 | kind = kind1 > kind2 ? kind1 : kind2; |
| 10086 | buf1 = PyUnicode_DATA(self); |
| 10087 | buf2 = PyUnicode_DATA(substring); |
| 10088 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10089 | buf1 = _PyUnicode_AsKind(self, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10090 | if (!buf1) |
| 10091 | return NULL; |
| 10092 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10093 | buf2 = _PyUnicode_AsKind(substring, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10094 | if (!buf2) { |
| 10095 | if (kind1 != kind) PyMem_Free(buf1); |
| 10096 | return NULL; |
| 10097 | } |
| 10098 | len1 = PyUnicode_GET_LENGTH(self); |
| 10099 | len2 = PyUnicode_GET_LENGTH(substring); |
| 10100 | |
| 10101 | switch(kind) { |
| 10102 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10103 | if (PyUnicode_IS_ASCII(self) && PyUnicode_IS_ASCII(substring)) |
| 10104 | out = asciilib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10105 | self, buf1, len1, buf2, len2, maxcount); |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10106 | else |
| 10107 | out = ucs1lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10108 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10109 | break; |
| 10110 | case PyUnicode_2BYTE_KIND: |
| 10111 | out = ucs2lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10112 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10113 | break; |
| 10114 | case PyUnicode_4BYTE_KIND: |
| 10115 | out = ucs4lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10116 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10117 | break; |
| 10118 | default: |
| 10119 | out = NULL; |
| 10120 | } |
| 10121 | if (kind1 != kind) |
| 10122 | PyMem_Free(buf1); |
| 10123 | if (kind2 != kind) |
| 10124 | PyMem_Free(buf2); |
| 10125 | return out; |
| 10126 | } |
| 10127 | |
| 10128 | static Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10129 | anylib_find(int kind, PyObject *str1, void *buf1, Py_ssize_t len1, |
| 10130 | 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] | 10131 | { |
| 10132 | switch(kind) { |
| 10133 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10134 | if (PyUnicode_IS_ASCII(str1) && PyUnicode_IS_ASCII(str2)) |
| 10135 | return asciilib_find(buf1, len1, buf2, len2, offset); |
| 10136 | else |
| 10137 | return ucs1lib_find(buf1, len1, buf2, len2, offset); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10138 | case PyUnicode_2BYTE_KIND: |
| 10139 | return ucs2lib_find(buf1, len1, buf2, len2, offset); |
| 10140 | case PyUnicode_4BYTE_KIND: |
| 10141 | return ucs4lib_find(buf1, len1, buf2, len2, offset); |
| 10142 | } |
| 10143 | assert(0); |
| 10144 | return -1; |
| 10145 | } |
| 10146 | |
| 10147 | static Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10148 | anylib_count(int kind, PyObject *sstr, void* sbuf, Py_ssize_t slen, |
| 10149 | 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] | 10150 | { |
| 10151 | switch(kind) { |
| 10152 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10153 | if (PyUnicode_IS_ASCII(sstr) && PyUnicode_IS_ASCII(str1)) |
| 10154 | return asciilib_count(sbuf, slen, buf1, len1, maxcount); |
| 10155 | else |
| 10156 | return ucs1lib_count(sbuf, slen, buf1, len1, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10157 | case PyUnicode_2BYTE_KIND: |
| 10158 | return ucs2lib_count(sbuf, slen, buf1, len1, maxcount); |
| 10159 | case PyUnicode_4BYTE_KIND: |
| 10160 | return ucs4lib_count(sbuf, slen, buf1, len1, maxcount); |
| 10161 | } |
| 10162 | assert(0); |
| 10163 | return 0; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10164 | } |
| 10165 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10166 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10167 | replace(PyObject *self, PyObject *str1, |
| 10168 | PyObject *str2, Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10169 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10170 | PyObject *u; |
| 10171 | char *sbuf = PyUnicode_DATA(self); |
| 10172 | char *buf1 = PyUnicode_DATA(str1); |
| 10173 | char *buf2 = PyUnicode_DATA(str2); |
| 10174 | int srelease = 0, release1 = 0, release2 = 0; |
| 10175 | int skind = PyUnicode_KIND(self); |
| 10176 | int kind1 = PyUnicode_KIND(str1); |
| 10177 | int kind2 = PyUnicode_KIND(str2); |
| 10178 | Py_ssize_t slen = PyUnicode_GET_LENGTH(self); |
| 10179 | Py_ssize_t len1 = PyUnicode_GET_LENGTH(str1); |
| 10180 | Py_ssize_t len2 = PyUnicode_GET_LENGTH(str2); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10181 | int mayshrink; |
| 10182 | Py_UCS4 maxchar, maxchar_str2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10183 | |
| 10184 | if (maxcount < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10185 | maxcount = PY_SSIZE_T_MAX; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10186 | else if (maxcount == 0 || slen == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10187 | goto nothing; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10188 | |
Victor Stinner | 59de0ee | 2011-10-07 10:01:28 +0200 | [diff] [blame] | 10189 | if (str1 == str2) |
| 10190 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10191 | if (skind < kind1) |
| 10192 | /* substring too wide to be present */ |
| 10193 | goto nothing; |
| 10194 | |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10195 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
| 10196 | maxchar_str2 = PyUnicode_MAX_CHAR_VALUE(str2); |
| 10197 | /* Replacing str1 with str2 may cause a maxchar reduction in the |
| 10198 | result string. */ |
| 10199 | mayshrink = (maxchar_str2 < maxchar); |
| 10200 | maxchar = Py_MAX(maxchar, maxchar_str2); |
| 10201 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10202 | if (len1 == len2) { |
Antoine Pitrou | cbfdee3 | 2010-01-13 08:58:08 +0000 | [diff] [blame] | 10203 | Py_ssize_t i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10204 | /* same length */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10205 | if (len1 == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10206 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10207 | if (len1 == 1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10208 | /* replace characters */ |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10209 | Py_UCS4 u1, u2; |
| 10210 | int rkind; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10211 | u1 = PyUnicode_READ_CHAR(str1, 0); |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 10212 | if (findchar(sbuf, PyUnicode_KIND(self), |
| 10213 | slen, u1, 1) < 0) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10214 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10215 | u2 = PyUnicode_READ_CHAR(str2, 0); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10216 | u = PyUnicode_New(slen, maxchar); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10217 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10218 | goto error; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 10219 | copy_characters(u, 0, self, 0, slen); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10220 | rkind = PyUnicode_KIND(u); |
| 10221 | for (i = 0; i < PyUnicode_GET_LENGTH(u); i++) |
| 10222 | if (PyUnicode_READ(rkind, PyUnicode_DATA(u), i) == u1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10223 | if (--maxcount < 0) |
| 10224 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10225 | PyUnicode_WRITE(rkind, PyUnicode_DATA(u), i, u2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10226 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10227 | } |
| 10228 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10229 | int rkind = skind; |
| 10230 | char *res; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10231 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10232 | if (kind1 < rkind) { |
| 10233 | /* widen substring */ |
| 10234 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10235 | if (!buf1) goto error; |
| 10236 | release1 = 1; |
| 10237 | } |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10238 | i = anylib_find(rkind, self, sbuf, slen, str1, buf1, len1, 0); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10239 | if (i < 0) |
| 10240 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10241 | if (rkind > kind2) { |
| 10242 | /* widen replacement */ |
| 10243 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 10244 | if (!buf2) goto error; |
| 10245 | release2 = 1; |
| 10246 | } |
| 10247 | else if (rkind < kind2) { |
| 10248 | /* widen self and buf1 */ |
| 10249 | rkind = kind2; |
| 10250 | if (release1) PyMem_Free(buf1); |
| 10251 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 10252 | if (!sbuf) goto error; |
| 10253 | srelease = 1; |
| 10254 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10255 | if (!buf1) goto error; |
| 10256 | release1 = 1; |
| 10257 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10258 | u = PyUnicode_New(slen, maxchar); |
| 10259 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10260 | goto error; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10261 | assert(PyUnicode_KIND(u) == rkind); |
| 10262 | res = PyUnicode_DATA(u); |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10263 | |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10264 | memcpy(res, sbuf, rkind * slen); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10265 | /* change everything in-place, starting with this one */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10266 | memcpy(res + rkind * i, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10267 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10268 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10269 | i += len1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10270 | |
| 10271 | while ( --maxcount > 0) { |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10272 | i = anylib_find(rkind, self, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10273 | sbuf+rkind*i, slen-i, |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10274 | str1, buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10275 | if (i == -1) |
| 10276 | break; |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10277 | memcpy(res + rkind * i, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10278 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10279 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10280 | i += len1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10281 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10282 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10283 | } |
| 10284 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10285 | Py_ssize_t n, i, j, ires; |
| 10286 | Py_ssize_t product, new_size; |
| 10287 | int rkind = skind; |
| 10288 | char *res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10289 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10290 | if (kind1 < rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10291 | /* widen substring */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10292 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10293 | if (!buf1) goto error; |
| 10294 | release1 = 1; |
| 10295 | } |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10296 | n = anylib_count(rkind, self, sbuf, slen, str1, buf1, len1, maxcount); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10297 | if (n == 0) |
| 10298 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10299 | if (kind2 < rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10300 | /* widen replacement */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10301 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 10302 | if (!buf2) goto error; |
| 10303 | release2 = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10304 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10305 | else if (kind2 > rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10306 | /* widen self and buf1 */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10307 | rkind = kind2; |
| 10308 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 10309 | if (!sbuf) goto error; |
| 10310 | srelease = 1; |
| 10311 | if (release1) PyMem_Free(buf1); |
| 10312 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10313 | if (!buf1) goto error; |
| 10314 | release1 = 1; |
| 10315 | } |
| 10316 | /* new_size = PyUnicode_GET_LENGTH(self) + n * (PyUnicode_GET_LENGTH(str2) - |
| 10317 | PyUnicode_GET_LENGTH(str1))); */ |
| 10318 | product = n * (len2-len1); |
| 10319 | if ((product / (len2-len1)) != n) { |
| 10320 | PyErr_SetString(PyExc_OverflowError, |
| 10321 | "replace string is too long"); |
| 10322 | goto error; |
| 10323 | } |
| 10324 | new_size = slen + product; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10325 | if (new_size == 0) { |
| 10326 | Py_INCREF(unicode_empty); |
| 10327 | u = unicode_empty; |
| 10328 | goto done; |
| 10329 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10330 | if (new_size < 0 || new_size > (PY_SSIZE_T_MAX >> (rkind-1))) { |
| 10331 | PyErr_SetString(PyExc_OverflowError, |
| 10332 | "replace string is too long"); |
| 10333 | goto error; |
| 10334 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10335 | u = PyUnicode_New(new_size, maxchar); |
| 10336 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10337 | goto error; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10338 | assert(PyUnicode_KIND(u) == rkind); |
| 10339 | res = PyUnicode_DATA(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10340 | ires = i = 0; |
| 10341 | if (len1 > 0) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10342 | while (n-- > 0) { |
| 10343 | /* look for next match */ |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10344 | j = anylib_find(rkind, self, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10345 | sbuf + rkind * i, slen-i, |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10346 | str1, buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10347 | if (j == -1) |
| 10348 | break; |
| 10349 | else if (j > i) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10350 | /* copy unchanged part [i:j] */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10351 | memcpy(res + rkind * ires, |
| 10352 | sbuf + rkind * i, |
| 10353 | rkind * (j-i)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10354 | ires += j - i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10355 | } |
| 10356 | /* copy substitution string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10357 | if (len2 > 0) { |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10358 | memcpy(res + rkind * ires, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10359 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10360 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10361 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10362 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10363 | i = j + len1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10364 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10365 | if (i < slen) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10366 | /* copy tail [i:] */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10367 | memcpy(res + rkind * ires, |
| 10368 | sbuf + rkind * i, |
| 10369 | rkind * (slen-i)); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10370 | } |
| 10371 | else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10372 | /* interleave */ |
| 10373 | while (n > 0) { |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10374 | memcpy(res + rkind * ires, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10375 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10376 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10377 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10378 | if (--n <= 0) |
| 10379 | break; |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10380 | memcpy(res + rkind * ires, |
| 10381 | sbuf + rkind * i, |
| 10382 | rkind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10383 | ires++; |
| 10384 | i++; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10385 | } |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10386 | memcpy(res + rkind * ires, |
| 10387 | sbuf + rkind * i, |
| 10388 | rkind * (slen-i)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10389 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10390 | } |
| 10391 | |
| 10392 | if (mayshrink) { |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10393 | unicode_adjust_maxchar(&u); |
| 10394 | if (u == NULL) |
| 10395 | goto error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10396 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10397 | |
| 10398 | done: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10399 | if (srelease) |
| 10400 | PyMem_FREE(sbuf); |
| 10401 | if (release1) |
| 10402 | PyMem_FREE(buf1); |
| 10403 | if (release2) |
| 10404 | PyMem_FREE(buf2); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10405 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10406 | return u; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10407 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10408 | nothing: |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10409 | /* nothing to replace; return original string (when possible) */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10410 | if (srelease) |
| 10411 | PyMem_FREE(sbuf); |
| 10412 | if (release1) |
| 10413 | PyMem_FREE(buf1); |
| 10414 | if (release2) |
| 10415 | PyMem_FREE(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10416 | if (PyUnicode_CheckExact(self)) { |
| 10417 | Py_INCREF(self); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10418 | return self; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10419 | } |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 10420 | return PyUnicode_Copy(self); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10421 | error: |
| 10422 | if (srelease && sbuf) |
| 10423 | PyMem_FREE(sbuf); |
| 10424 | if (release1 && buf1) |
| 10425 | PyMem_FREE(buf1); |
| 10426 | if (release2 && buf2) |
| 10427 | PyMem_FREE(buf2); |
| 10428 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10429 | } |
| 10430 | |
| 10431 | /* --- Unicode Object Methods --------------------------------------------- */ |
| 10432 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10433 | PyDoc_STRVAR(title__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10434 | "S.title() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10435 | \n\ |
| 10436 | 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] | 10437 | characters, all remaining cased characters have lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10438 | |
| 10439 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10440 | unicode_title(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10441 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10442 | return fixup(self, fixtitle); |
| 10443 | } |
| 10444 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10445 | PyDoc_STRVAR(capitalize__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10446 | "S.capitalize() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10447 | \n\ |
| 10448 | 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] | 10449 | have upper case and the rest lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10450 | |
| 10451 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10452 | unicode_capitalize(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10453 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10454 | return fixup(self, fixcapitalize); |
| 10455 | } |
| 10456 | |
| 10457 | #if 0 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10458 | PyDoc_STRVAR(capwords__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10459 | "S.capwords() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10460 | \n\ |
| 10461 | 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] | 10462 | normalized whitespace (all whitespace strings are replaced by ' ')."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10463 | |
| 10464 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10465 | unicode_capwords(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10466 | { |
| 10467 | PyObject *list; |
| 10468 | PyObject *item; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10469 | Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10470 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10471 | /* Split into words */ |
| 10472 | list = split(self, NULL, -1); |
| 10473 | if (!list) |
| 10474 | return NULL; |
| 10475 | |
| 10476 | /* Capitalize each word */ |
| 10477 | for (i = 0; i < PyList_GET_SIZE(list); i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10478 | item = fixup(PyList_GET_ITEM(list, i), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10479 | fixcapitalize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10480 | if (item == NULL) |
| 10481 | goto onError; |
| 10482 | Py_DECREF(PyList_GET_ITEM(list, i)); |
| 10483 | PyList_SET_ITEM(list, i, item); |
| 10484 | } |
| 10485 | |
| 10486 | /* Join the words to form a new string */ |
| 10487 | item = PyUnicode_Join(NULL, list); |
| 10488 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10489 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10490 | Py_DECREF(list); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10491 | return item; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10492 | } |
| 10493 | #endif |
| 10494 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10495 | /* Argument converter. Coerces to a single unicode character */ |
| 10496 | |
| 10497 | static int |
| 10498 | convert_uc(PyObject *obj, void *addr) |
| 10499 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10500 | Py_UCS4 *fillcharloc = (Py_UCS4 *)addr; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10501 | PyObject *uniobj; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10502 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10503 | uniobj = PyUnicode_FromObject(obj); |
| 10504 | if (uniobj == NULL) { |
| 10505 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10506 | "The fill character cannot be converted to Unicode"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10507 | return 0; |
| 10508 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10509 | if (PyUnicode_GET_LENGTH(uniobj) != 1) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10510 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10511 | "The fill character must be exactly one character long"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10512 | Py_DECREF(uniobj); |
| 10513 | return 0; |
| 10514 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10515 | *fillcharloc = PyUnicode_READ_CHAR(uniobj, 0); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10516 | Py_DECREF(uniobj); |
| 10517 | return 1; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10518 | } |
| 10519 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10520 | PyDoc_STRVAR(center__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10521 | "S.center(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10522 | \n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 10523 | Return S centered in a string of length width. Padding is\n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10524 | done using the specified fill character (default is a space)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10525 | |
| 10526 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10527 | unicode_center(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10528 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10529 | Py_ssize_t marg, left; |
| 10530 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10531 | Py_UCS4 fillchar = ' '; |
| 10532 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 10533 | 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] | 10534 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10535 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 10536 | if (PyUnicode_READY(self) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10537 | return NULL; |
| 10538 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10539 | if (_PyUnicode_LENGTH(self) >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10540 | Py_INCREF(self); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10541 | return self; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10542 | } |
| 10543 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10544 | marg = width - _PyUnicode_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10545 | left = marg / 2 + (marg & width & 1); |
| 10546 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10547 | return pad(self, left, marg - left, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10548 | } |
| 10549 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10550 | /* This function assumes that str1 and str2 are readied by the caller. */ |
| 10551 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10552 | static int |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10553 | unicode_compare(PyObject *str1, PyObject *str2) |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10554 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10555 | int kind1, kind2; |
| 10556 | void *data1, *data2; |
| 10557 | Py_ssize_t len1, len2, i; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10558 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10559 | kind1 = PyUnicode_KIND(str1); |
| 10560 | kind2 = PyUnicode_KIND(str2); |
| 10561 | data1 = PyUnicode_DATA(str1); |
| 10562 | data2 = PyUnicode_DATA(str2); |
| 10563 | len1 = PyUnicode_GET_LENGTH(str1); |
| 10564 | len2 = PyUnicode_GET_LENGTH(str2); |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10565 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10566 | for (i = 0; i < len1 && i < len2; ++i) { |
| 10567 | Py_UCS4 c1, c2; |
| 10568 | c1 = PyUnicode_READ(kind1, data1, i); |
| 10569 | c2 = PyUnicode_READ(kind2, data2, i); |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 10570 | |
| 10571 | if (c1 != c2) |
| 10572 | return (c1 < c2) ? -1 : 1; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10573 | } |
| 10574 | |
| 10575 | return (len1 < len2) ? -1 : (len1 != len2); |
| 10576 | } |
| 10577 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10578 | int |
| 10579 | PyUnicode_Compare(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10580 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10581 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 10582 | if (PyUnicode_READY(left) == -1 || |
| 10583 | PyUnicode_READY(right) == -1) |
| 10584 | return -1; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10585 | return unicode_compare(left, right); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10586 | } |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 10587 | PyErr_Format(PyExc_TypeError, |
| 10588 | "Can't compare %.100s and %.100s", |
| 10589 | left->ob_type->tp_name, |
| 10590 | right->ob_type->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10591 | return -1; |
| 10592 | } |
| 10593 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10594 | int |
| 10595 | PyUnicode_CompareWithASCIIString(PyObject* uni, const char* str) |
| 10596 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10597 | Py_ssize_t i; |
| 10598 | int kind; |
| 10599 | void *data; |
| 10600 | Py_UCS4 chr; |
| 10601 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 10602 | assert(_PyUnicode_CHECK(uni)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10603 | if (PyUnicode_READY(uni) == -1) |
| 10604 | return -1; |
| 10605 | kind = PyUnicode_KIND(uni); |
| 10606 | data = PyUnicode_DATA(uni); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10607 | /* Compare Unicode string and source character set string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10608 | for (i = 0; (chr = PyUnicode_READ(kind, data, i)) && str[i]; i++) |
| 10609 | if (chr != str[i]) |
| 10610 | return (chr < (unsigned char)(str[i])) ? -1 : 1; |
Benjamin Peterson | 8667a9b | 2010-01-09 21:45:28 +0000 | [diff] [blame] | 10611 | /* This check keeps Python strings that end in '\0' from comparing equal |
| 10612 | to C strings identical up to that point. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10613 | if (PyUnicode_GET_LENGTH(uni) != i || chr) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10614 | return 1; /* uni is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10615 | if (str[i]) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10616 | return -1; /* str is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10617 | return 0; |
| 10618 | } |
| 10619 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10620 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10621 | #define TEST_COND(cond) \ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10622 | ((cond) ? Py_True : Py_False) |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10623 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10624 | PyObject * |
| 10625 | PyUnicode_RichCompare(PyObject *left, PyObject *right, int op) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10626 | { |
| 10627 | int result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10628 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10629 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 10630 | PyObject *v; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10631 | if (PyUnicode_READY(left) == -1 || |
| 10632 | PyUnicode_READY(right) == -1) |
| 10633 | return NULL; |
| 10634 | if (PyUnicode_GET_LENGTH(left) != PyUnicode_GET_LENGTH(right) || |
| 10635 | PyUnicode_KIND(left) != PyUnicode_KIND(right)) { |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10636 | if (op == Py_EQ) { |
| 10637 | Py_INCREF(Py_False); |
| 10638 | return Py_False; |
| 10639 | } |
| 10640 | if (op == Py_NE) { |
| 10641 | Py_INCREF(Py_True); |
| 10642 | return Py_True; |
| 10643 | } |
| 10644 | } |
| 10645 | if (left == right) |
| 10646 | result = 0; |
| 10647 | else |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10648 | result = unicode_compare(left, right); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10649 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10650 | /* Convert the return value to a Boolean */ |
| 10651 | switch (op) { |
| 10652 | case Py_EQ: |
| 10653 | v = TEST_COND(result == 0); |
| 10654 | break; |
| 10655 | case Py_NE: |
| 10656 | v = TEST_COND(result != 0); |
| 10657 | break; |
| 10658 | case Py_LE: |
| 10659 | v = TEST_COND(result <= 0); |
| 10660 | break; |
| 10661 | case Py_GE: |
| 10662 | v = TEST_COND(result >= 0); |
| 10663 | break; |
| 10664 | case Py_LT: |
| 10665 | v = TEST_COND(result == -1); |
| 10666 | break; |
| 10667 | case Py_GT: |
| 10668 | v = TEST_COND(result == 1); |
| 10669 | break; |
| 10670 | default: |
| 10671 | PyErr_BadArgument(); |
| 10672 | return NULL; |
| 10673 | } |
| 10674 | Py_INCREF(v); |
| 10675 | return v; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10676 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10677 | |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 10678 | Py_RETURN_NOTIMPLEMENTED; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10679 | } |
| 10680 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10681 | int |
| 10682 | PyUnicode_Contains(PyObject *container, PyObject *element) |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10683 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10684 | PyObject *str, *sub; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10685 | int kind1, kind2, kind; |
| 10686 | void *buf1, *buf2; |
| 10687 | Py_ssize_t len1, len2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10688 | int result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10689 | |
| 10690 | /* Coerce the two arguments */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10691 | sub = PyUnicode_FromObject(element); |
| 10692 | if (!sub) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10693 | PyErr_Format(PyExc_TypeError, |
| 10694 | "'in <string>' requires string as left operand, not %s", |
| 10695 | element->ob_type->tp_name); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10696 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10697 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10698 | if (PyUnicode_READY(sub) == -1) |
| 10699 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10700 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10701 | str = PyUnicode_FromObject(container); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 10702 | if (!str || PyUnicode_READY(str) == -1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10703 | Py_DECREF(sub); |
| 10704 | return -1; |
| 10705 | } |
| 10706 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10707 | kind1 = PyUnicode_KIND(str); |
| 10708 | kind2 = PyUnicode_KIND(sub); |
| 10709 | kind = kind1 > kind2 ? kind1 : kind2; |
| 10710 | buf1 = PyUnicode_DATA(str); |
| 10711 | buf2 = PyUnicode_DATA(sub); |
| 10712 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10713 | buf1 = _PyUnicode_AsKind(str, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10714 | if (!buf1) { |
| 10715 | Py_DECREF(sub); |
| 10716 | return -1; |
| 10717 | } |
| 10718 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10719 | buf2 = _PyUnicode_AsKind(sub, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10720 | if (!buf2) { |
| 10721 | Py_DECREF(sub); |
| 10722 | if (kind1 != kind) PyMem_Free(buf1); |
| 10723 | return -1; |
| 10724 | } |
| 10725 | len1 = PyUnicode_GET_LENGTH(str); |
| 10726 | len2 = PyUnicode_GET_LENGTH(sub); |
| 10727 | |
| 10728 | switch(kind) { |
| 10729 | case PyUnicode_1BYTE_KIND: |
| 10730 | result = ucs1lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10731 | break; |
| 10732 | case PyUnicode_2BYTE_KIND: |
| 10733 | result = ucs2lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10734 | break; |
| 10735 | case PyUnicode_4BYTE_KIND: |
| 10736 | result = ucs4lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10737 | break; |
| 10738 | default: |
| 10739 | result = -1; |
| 10740 | assert(0); |
| 10741 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10742 | |
| 10743 | Py_DECREF(str); |
| 10744 | Py_DECREF(sub); |
| 10745 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10746 | if (kind1 != kind) |
| 10747 | PyMem_Free(buf1); |
| 10748 | if (kind2 != kind) |
| 10749 | PyMem_Free(buf2); |
| 10750 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10751 | return result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10752 | } |
| 10753 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10754 | /* Concat to string or Unicode object giving a new Unicode object. */ |
| 10755 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10756 | PyObject * |
| 10757 | PyUnicode_Concat(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10758 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10759 | PyObject *u = NULL, *v = NULL, *w; |
Victor Stinner | 127226b | 2011-10-13 01:12:34 +0200 | [diff] [blame] | 10760 | Py_UCS4 maxchar, maxchar2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10761 | |
| 10762 | /* Coerce the two arguments */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10763 | u = PyUnicode_FromObject(left); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10764 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10765 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10766 | v = PyUnicode_FromObject(right); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10767 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10768 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10769 | |
| 10770 | /* Shortcuts */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 10771 | if (v == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10772 | Py_DECREF(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10773 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10774 | } |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 10775 | if (u == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10776 | Py_DECREF(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10777 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10778 | } |
| 10779 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10780 | maxchar = PyUnicode_MAX_CHAR_VALUE(u); |
Victor Stinner | 127226b | 2011-10-13 01:12:34 +0200 | [diff] [blame] | 10781 | maxchar2 = PyUnicode_MAX_CHAR_VALUE(v); |
| 10782 | maxchar = Py_MAX(maxchar, maxchar2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10783 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10784 | /* Concat the two Unicode strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10785 | w = PyUnicode_New( |
| 10786 | PyUnicode_GET_LENGTH(u) + PyUnicode_GET_LENGTH(v), |
| 10787 | maxchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10788 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10789 | goto onError; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 10790 | copy_characters(w, 0, u, 0, PyUnicode_GET_LENGTH(u)); |
| 10791 | 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] | 10792 | Py_DECREF(u); |
| 10793 | Py_DECREF(v); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10794 | assert(_PyUnicode_CheckConsistency(w, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10795 | return w; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10796 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10797 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10798 | Py_XDECREF(u); |
| 10799 | Py_XDECREF(v); |
| 10800 | return NULL; |
| 10801 | } |
| 10802 | |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10803 | static void |
| 10804 | unicode_append_inplace(PyObject **p_left, PyObject *right) |
| 10805 | { |
| 10806 | Py_ssize_t left_len, right_len, new_len; |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10807 | |
| 10808 | assert(PyUnicode_IS_READY(*p_left)); |
| 10809 | assert(PyUnicode_IS_READY(right)); |
| 10810 | |
| 10811 | left_len = PyUnicode_GET_LENGTH(*p_left); |
| 10812 | right_len = PyUnicode_GET_LENGTH(right); |
| 10813 | if (left_len > PY_SSIZE_T_MAX - right_len) { |
| 10814 | PyErr_SetString(PyExc_OverflowError, |
| 10815 | "strings are too large to concat"); |
| 10816 | goto error; |
| 10817 | } |
| 10818 | new_len = left_len + right_len; |
| 10819 | |
| 10820 | /* Now we own the last reference to 'left', so we can resize it |
| 10821 | * in-place. |
| 10822 | */ |
| 10823 | if (unicode_resize(p_left, new_len) != 0) { |
| 10824 | /* XXX if _PyUnicode_Resize() fails, 'left' has been |
| 10825 | * deallocated so it cannot be put back into |
| 10826 | * 'variable'. The MemoryError is raised when there |
| 10827 | * is no value in 'variable', which might (very |
| 10828 | * remotely) be a cause of incompatibilities. |
| 10829 | */ |
| 10830 | goto error; |
| 10831 | } |
| 10832 | /* copy 'right' into the newly allocated area of 'left' */ |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 10833 | copy_characters(*p_left, left_len, right, 0, right_len); |
| 10834 | _PyUnicode_DIRTY(*p_left); |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10835 | return; |
| 10836 | |
| 10837 | error: |
| 10838 | Py_DECREF(*p_left); |
| 10839 | *p_left = NULL; |
| 10840 | } |
| 10841 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10842 | void |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10843 | PyUnicode_Append(PyObject **p_left, PyObject *right) |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10844 | { |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10845 | PyObject *left, *res; |
| 10846 | |
| 10847 | if (p_left == NULL) { |
| 10848 | if (!PyErr_Occurred()) |
| 10849 | PyErr_BadInternalCall(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10850 | return; |
| 10851 | } |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10852 | left = *p_left; |
| 10853 | if (right == NULL || !PyUnicode_Check(left)) { |
| 10854 | if (!PyErr_Occurred()) |
| 10855 | PyErr_BadInternalCall(); |
| 10856 | goto error; |
| 10857 | } |
| 10858 | |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 10859 | if (PyUnicode_READY(left)) |
| 10860 | goto error; |
| 10861 | if (PyUnicode_READY(right)) |
| 10862 | goto error; |
| 10863 | |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10864 | if (PyUnicode_CheckExact(left) && left != unicode_empty |
| 10865 | && PyUnicode_CheckExact(right) && right != unicode_empty |
| 10866 | && unicode_resizable(left) |
| 10867 | && (_PyUnicode_KIND(right) <= _PyUnicode_KIND(left) |
| 10868 | || _PyUnicode_WSTR(left) != NULL)) |
| 10869 | { |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10870 | /* Don't resize for ascii += latin1. Convert ascii to latin1 requires |
| 10871 | to change the structure size, but characters are stored just after |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 10872 | the structure, and so it requires to move all characters which is |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10873 | not so different than duplicating the string. */ |
| 10874 | if (!(PyUnicode_IS_ASCII(left) && !PyUnicode_IS_ASCII(right))) |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10875 | { |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10876 | unicode_append_inplace(p_left, right); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10877 | if (p_left != NULL) |
| 10878 | assert(_PyUnicode_CheckConsistency(*p_left, 1)); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10879 | return; |
| 10880 | } |
| 10881 | } |
| 10882 | |
| 10883 | res = PyUnicode_Concat(left, right); |
| 10884 | if (res == NULL) |
| 10885 | goto error; |
| 10886 | Py_DECREF(left); |
| 10887 | *p_left = res; |
| 10888 | return; |
| 10889 | |
| 10890 | error: |
| 10891 | Py_DECREF(*p_left); |
| 10892 | *p_left = NULL; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10893 | } |
| 10894 | |
| 10895 | void |
| 10896 | PyUnicode_AppendAndDel(PyObject **pleft, PyObject *right) |
| 10897 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10898 | PyUnicode_Append(pleft, right); |
| 10899 | Py_XDECREF(right); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10900 | } |
| 10901 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10902 | PyDoc_STRVAR(count__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10903 | "S.count(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10904 | \n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10905 | Return the number of non-overlapping occurrences of substring sub in\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 10906 | 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] | 10907 | interpreted as in slice notation."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10908 | |
| 10909 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10910 | unicode_count(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10911 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10912 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10913 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10914 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10915 | PyObject *result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10916 | int kind1, kind2, kind; |
| 10917 | void *buf1, *buf2; |
| 10918 | Py_ssize_t len1, len2, iresult; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10919 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10920 | if (!stringlib_parse_args_finds_unicode("count", args, &substring, |
| 10921 | &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10922 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 10923 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10924 | kind1 = PyUnicode_KIND(self); |
| 10925 | kind2 = PyUnicode_KIND(substring); |
| 10926 | kind = kind1 > kind2 ? kind1 : kind2; |
| 10927 | buf1 = PyUnicode_DATA(self); |
| 10928 | buf2 = PyUnicode_DATA(substring); |
| 10929 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10930 | buf1 = _PyUnicode_AsKind(self, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10931 | if (!buf1) { |
| 10932 | Py_DECREF(substring); |
| 10933 | return NULL; |
| 10934 | } |
| 10935 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10936 | buf2 = _PyUnicode_AsKind(substring, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10937 | if (!buf2) { |
| 10938 | Py_DECREF(substring); |
| 10939 | if (kind1 != kind) PyMem_Free(buf1); |
| 10940 | return NULL; |
| 10941 | } |
| 10942 | len1 = PyUnicode_GET_LENGTH(self); |
| 10943 | len2 = PyUnicode_GET_LENGTH(substring); |
| 10944 | |
| 10945 | ADJUST_INDICES(start, end, len1); |
| 10946 | switch(kind) { |
| 10947 | case PyUnicode_1BYTE_KIND: |
| 10948 | iresult = ucs1lib_count( |
| 10949 | ((Py_UCS1*)buf1) + start, end - start, |
| 10950 | buf2, len2, PY_SSIZE_T_MAX |
| 10951 | ); |
| 10952 | break; |
| 10953 | case PyUnicode_2BYTE_KIND: |
| 10954 | iresult = ucs2lib_count( |
| 10955 | ((Py_UCS2*)buf1) + start, end - start, |
| 10956 | buf2, len2, PY_SSIZE_T_MAX |
| 10957 | ); |
| 10958 | break; |
| 10959 | case PyUnicode_4BYTE_KIND: |
| 10960 | iresult = ucs4lib_count( |
| 10961 | ((Py_UCS4*)buf1) + start, end - start, |
| 10962 | buf2, len2, PY_SSIZE_T_MAX |
| 10963 | ); |
| 10964 | break; |
| 10965 | default: |
| 10966 | assert(0); iresult = 0; |
| 10967 | } |
| 10968 | |
| 10969 | result = PyLong_FromSsize_t(iresult); |
| 10970 | |
| 10971 | if (kind1 != kind) |
| 10972 | PyMem_Free(buf1); |
| 10973 | if (kind2 != kind) |
| 10974 | PyMem_Free(buf2); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10975 | |
| 10976 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10977 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10978 | return result; |
| 10979 | } |
| 10980 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10981 | PyDoc_STRVAR(encode__doc__, |
Victor Stinner | c911bbf | 2010-11-07 19:04:46 +0000 | [diff] [blame] | 10982 | "S.encode(encoding='utf-8', errors='strict') -> bytes\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10983 | \n\ |
Victor Stinner | e14e212 | 2010-11-07 18:41:46 +0000 | [diff] [blame] | 10984 | Encode S using the codec registered for encoding. Default encoding\n\ |
| 10985 | 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] | 10986 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 10987 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 10988 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 10989 | codecs.register_error that can handle UnicodeEncodeErrors."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10990 | |
| 10991 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10992 | unicode_encode(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10993 | { |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 10994 | static char *kwlist[] = {"encoding", "errors", 0}; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10995 | char *encoding = NULL; |
| 10996 | char *errors = NULL; |
Guido van Rossum | 35d9428 | 2007-08-27 18:20:11 +0000 | [diff] [blame] | 10997 | |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 10998 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:encode", |
| 10999 | kwlist, &encoding, &errors)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11000 | return NULL; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11001 | return PyUnicode_AsEncodedString(self, encoding, errors); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 11002 | } |
| 11003 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11004 | PyDoc_STRVAR(expandtabs__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11005 | "S.expandtabs([tabsize]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11006 | \n\ |
| 11007 | 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] | 11008 | 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] | 11009 | |
| 11010 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11011 | unicode_expandtabs(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11012 | { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11013 | Py_ssize_t i, j, line_pos, src_len, incr; |
| 11014 | Py_UCS4 ch; |
| 11015 | PyObject *u; |
| 11016 | void *src_data, *dest_data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11017 | int tabsize = 8; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11018 | int kind; |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 11019 | int found; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11020 | |
| 11021 | if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11022 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11023 | |
Antoine Pitrou | 2242522 | 2011-10-04 19:10:51 +0200 | [diff] [blame] | 11024 | if (PyUnicode_READY(self) == -1) |
| 11025 | return NULL; |
| 11026 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 11027 | /* First pass: determine size of output string */ |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11028 | src_len = PyUnicode_GET_LENGTH(self); |
| 11029 | i = j = line_pos = 0; |
| 11030 | kind = PyUnicode_KIND(self); |
| 11031 | src_data = PyUnicode_DATA(self); |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 11032 | found = 0; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11033 | for (; i < src_len; i++) { |
| 11034 | ch = PyUnicode_READ(kind, src_data, i); |
| 11035 | if (ch == '\t') { |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 11036 | found = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11037 | if (tabsize > 0) { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11038 | incr = tabsize - (line_pos % tabsize); /* cannot overflow */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11039 | if (j > PY_SSIZE_T_MAX - incr) |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11040 | goto overflow; |
| 11041 | line_pos += incr; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11042 | j += incr; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11043 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11044 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11045 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11046 | if (j > PY_SSIZE_T_MAX - 1) |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11047 | goto overflow; |
| 11048 | line_pos++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11049 | j++; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11050 | if (ch == '\n' || ch == '\r') |
| 11051 | line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11052 | } |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11053 | } |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 11054 | if (!found && PyUnicode_CheckExact(self)) { |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11055 | Py_INCREF(self); |
| 11056 | return self; |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 11057 | } |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 11058 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11059 | /* Second pass: create output string and fill it */ |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11060 | u = PyUnicode_New(j, PyUnicode_MAX_CHAR_VALUE(self)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11061 | if (!u) |
| 11062 | return NULL; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11063 | dest_data = PyUnicode_DATA(u); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11064 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11065 | i = j = line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11066 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11067 | for (; i < src_len; i++) { |
| 11068 | ch = PyUnicode_READ(kind, src_data, i); |
| 11069 | if (ch == '\t') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11070 | if (tabsize > 0) { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11071 | incr = tabsize - (line_pos % tabsize); |
| 11072 | line_pos += incr; |
| 11073 | while (incr--) { |
| 11074 | PyUnicode_WRITE(kind, dest_data, j, ' '); |
| 11075 | j++; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11076 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11077 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11078 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11079 | else { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11080 | line_pos++; |
| 11081 | PyUnicode_WRITE(kind, dest_data, j, ch); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11082 | j++; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11083 | if (ch == '\n' || ch == '\r') |
| 11084 | line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11085 | } |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11086 | } |
| 11087 | assert (j == PyUnicode_GET_LENGTH(u)); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 11088 | #ifndef DONT_MAKE_RESULT_READY |
| 11089 | if (_PyUnicode_READY_REPLACE(&u)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11090 | Py_DECREF(u); |
| 11091 | return NULL; |
| 11092 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 11093 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 11094 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11095 | return u; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11096 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11097 | overflow: |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11098 | PyErr_SetString(PyExc_OverflowError, "new string is too long"); |
| 11099 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11100 | } |
| 11101 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11102 | PyDoc_STRVAR(find__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11103 | "S.find(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11104 | \n\ |
| 11105 | Return the lowest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 11106 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11107 | arguments start and end are interpreted as in slice notation.\n\ |
| 11108 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11109 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11110 | |
| 11111 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11112 | unicode_find(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11113 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11114 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 11115 | Py_ssize_t start; |
| 11116 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11117 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11118 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11119 | if (!stringlib_parse_args_finds_unicode("find", args, &substring, |
| 11120 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11121 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11122 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11123 | if (PyUnicode_READY(self) == -1) |
| 11124 | return NULL; |
| 11125 | if (PyUnicode_READY(substring) == -1) |
| 11126 | return NULL; |
| 11127 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11128 | result = any_find_slice(1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11129 | |
| 11130 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11131 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11132 | if (result == -2) |
| 11133 | return NULL; |
| 11134 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 11135 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11136 | } |
| 11137 | |
| 11138 | static PyObject * |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 11139 | unicode_getitem(PyObject *self, Py_ssize_t index) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11140 | { |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 11141 | Py_UCS4 ch = PyUnicode_ReadChar(self, index); |
| 11142 | if (ch == (Py_UCS4)-1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11143 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11144 | return PyUnicode_FromOrdinal(ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11145 | } |
| 11146 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11147 | /* Believe it or not, this produces the same value for ASCII strings |
Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 11148 | as bytes_hash(). */ |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 11149 | static Py_hash_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11150 | unicode_hash(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11151 | { |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11152 | Py_ssize_t len; |
Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 11153 | Py_uhash_t x; |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11154 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11155 | if (_PyUnicode_HASH(self) != -1) |
| 11156 | return _PyUnicode_HASH(self); |
| 11157 | if (PyUnicode_READY(self) == -1) |
| 11158 | return -1; |
| 11159 | len = PyUnicode_GET_LENGTH(self); |
| 11160 | |
| 11161 | /* The hash function as a macro, gets expanded three times below. */ |
| 11162 | #define HASH(P) \ |
| 11163 | x = (Py_uhash_t)*P << 7; \ |
| 11164 | while (--len >= 0) \ |
| 11165 | x = (1000003*x) ^ (Py_uhash_t)*P++; |
| 11166 | |
| 11167 | switch (PyUnicode_KIND(self)) { |
| 11168 | case PyUnicode_1BYTE_KIND: { |
| 11169 | const unsigned char *c = PyUnicode_1BYTE_DATA(self); |
| 11170 | HASH(c); |
| 11171 | break; |
| 11172 | } |
| 11173 | case PyUnicode_2BYTE_KIND: { |
| 11174 | const Py_UCS2 *s = PyUnicode_2BYTE_DATA(self); |
| 11175 | HASH(s); |
| 11176 | break; |
| 11177 | } |
| 11178 | default: { |
| 11179 | Py_UCS4 *l; |
| 11180 | assert(PyUnicode_KIND(self) == PyUnicode_4BYTE_KIND && |
| 11181 | "Impossible switch case in unicode_hash"); |
| 11182 | l = PyUnicode_4BYTE_DATA(self); |
| 11183 | HASH(l); |
| 11184 | break; |
| 11185 | } |
| 11186 | } |
| 11187 | x ^= (Py_uhash_t)PyUnicode_GET_LENGTH(self); |
| 11188 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11189 | if (x == -1) |
| 11190 | x = -2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11191 | _PyUnicode_HASH(self) = x; |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11192 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11193 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11194 | #undef HASH |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11195 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11196 | PyDoc_STRVAR(index__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11197 | "S.index(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11198 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11199 | 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] | 11200 | |
| 11201 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11202 | unicode_index(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11203 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11204 | Py_ssize_t result; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11205 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 11206 | Py_ssize_t start; |
| 11207 | Py_ssize_t end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11208 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11209 | if (!stringlib_parse_args_finds_unicode("index", args, &substring, |
| 11210 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11211 | return NULL; |
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 | if (PyUnicode_READY(self) == -1) |
| 11214 | return NULL; |
| 11215 | if (PyUnicode_READY(substring) == -1) |
| 11216 | return NULL; |
| 11217 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11218 | result = any_find_slice(1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11219 | |
| 11220 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11221 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11222 | if (result == -2) |
| 11223 | return NULL; |
| 11224 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11225 | if (result < 0) { |
| 11226 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 11227 | return NULL; |
| 11228 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11229 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 11230 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11231 | } |
| 11232 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11233 | PyDoc_STRVAR(islower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11234 | "S.islower() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11235 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11236 | 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] | 11237 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11238 | |
| 11239 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11240 | unicode_islower(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11241 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11242 | Py_ssize_t i, length; |
| 11243 | int kind; |
| 11244 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11245 | int cased; |
| 11246 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11247 | if (PyUnicode_READY(self) == -1) |
| 11248 | return NULL; |
| 11249 | length = PyUnicode_GET_LENGTH(self); |
| 11250 | kind = PyUnicode_KIND(self); |
| 11251 | data = PyUnicode_DATA(self); |
| 11252 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11253 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11254 | if (length == 1) |
| 11255 | return PyBool_FromLong( |
| 11256 | Py_UNICODE_ISLOWER(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11257 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11258 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11259 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11260 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11261 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11262 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11263 | for (i = 0; i < length; i++) { |
| 11264 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11265 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11266 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 11267 | return PyBool_FromLong(0); |
| 11268 | else if (!cased && Py_UNICODE_ISLOWER(ch)) |
| 11269 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11270 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11271 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11272 | } |
| 11273 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11274 | PyDoc_STRVAR(isupper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11275 | "S.isupper() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11276 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11277 | 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] | 11278 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11279 | |
| 11280 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11281 | unicode_isupper(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11282 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11283 | Py_ssize_t i, length; |
| 11284 | int kind; |
| 11285 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11286 | int cased; |
| 11287 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11288 | if (PyUnicode_READY(self) == -1) |
| 11289 | return NULL; |
| 11290 | length = PyUnicode_GET_LENGTH(self); |
| 11291 | kind = PyUnicode_KIND(self); |
| 11292 | data = PyUnicode_DATA(self); |
| 11293 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11294 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11295 | if (length == 1) |
| 11296 | return PyBool_FromLong( |
| 11297 | Py_UNICODE_ISUPPER(PyUnicode_READ(kind, data, 0)) != 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11298 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11299 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11300 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11301 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11302 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11303 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11304 | for (i = 0; i < length; i++) { |
| 11305 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11306 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11307 | if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 11308 | return PyBool_FromLong(0); |
| 11309 | else if (!cased && Py_UNICODE_ISUPPER(ch)) |
| 11310 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11311 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11312 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11313 | } |
| 11314 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11315 | PyDoc_STRVAR(istitle__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11316 | "S.istitle() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11317 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11318 | Return True if S is a titlecased string and there is at least one\n\ |
| 11319 | character in S, i.e. upper- and titlecase characters may only\n\ |
| 11320 | follow uncased characters and lowercase characters only cased ones.\n\ |
| 11321 | Return False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11322 | |
| 11323 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11324 | unicode_istitle(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11325 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11326 | Py_ssize_t i, length; |
| 11327 | int kind; |
| 11328 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11329 | int cased, previous_is_cased; |
| 11330 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11331 | if (PyUnicode_READY(self) == -1) |
| 11332 | return NULL; |
| 11333 | length = PyUnicode_GET_LENGTH(self); |
| 11334 | kind = PyUnicode_KIND(self); |
| 11335 | data = PyUnicode_DATA(self); |
| 11336 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11337 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11338 | if (length == 1) { |
| 11339 | Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11340 | return PyBool_FromLong((Py_UNICODE_ISTITLE(ch) != 0) || |
| 11341 | (Py_UNICODE_ISUPPER(ch) != 0)); |
| 11342 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11343 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11344 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11345 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11346 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11347 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11348 | cased = 0; |
| 11349 | previous_is_cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11350 | for (i = 0; i < length; i++) { |
| 11351 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11352 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11353 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { |
| 11354 | if (previous_is_cased) |
| 11355 | return PyBool_FromLong(0); |
| 11356 | previous_is_cased = 1; |
| 11357 | cased = 1; |
| 11358 | } |
| 11359 | else if (Py_UNICODE_ISLOWER(ch)) { |
| 11360 | if (!previous_is_cased) |
| 11361 | return PyBool_FromLong(0); |
| 11362 | previous_is_cased = 1; |
| 11363 | cased = 1; |
| 11364 | } |
| 11365 | else |
| 11366 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11367 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11368 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11369 | } |
| 11370 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11371 | PyDoc_STRVAR(isspace__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11372 | "S.isspace() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11373 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11374 | Return True if all characters in S are whitespace\n\ |
| 11375 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11376 | |
| 11377 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11378 | unicode_isspace(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11379 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11380 | Py_ssize_t i, length; |
| 11381 | int kind; |
| 11382 | void *data; |
| 11383 | |
| 11384 | if (PyUnicode_READY(self) == -1) |
| 11385 | return NULL; |
| 11386 | length = PyUnicode_GET_LENGTH(self); |
| 11387 | kind = PyUnicode_KIND(self); |
| 11388 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11389 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11390 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11391 | if (length == 1) |
| 11392 | return PyBool_FromLong( |
| 11393 | Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11394 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11395 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11396 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11397 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11398 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11399 | for (i = 0; i < length; i++) { |
| 11400 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11401 | if (!Py_UNICODE_ISSPACE(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11402 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11403 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11404 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11405 | } |
| 11406 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11407 | PyDoc_STRVAR(isalpha__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11408 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11409 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11410 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11411 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11412 | |
| 11413 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11414 | unicode_isalpha(PyObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11415 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11416 | Py_ssize_t i, length; |
| 11417 | int kind; |
| 11418 | void *data; |
| 11419 | |
| 11420 | if (PyUnicode_READY(self) == -1) |
| 11421 | return NULL; |
| 11422 | length = PyUnicode_GET_LENGTH(self); |
| 11423 | kind = PyUnicode_KIND(self); |
| 11424 | data = PyUnicode_DATA(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11425 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11426 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11427 | if (length == 1) |
| 11428 | return PyBool_FromLong( |
| 11429 | Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, 0))); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11430 | |
| 11431 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11432 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11433 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11434 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11435 | for (i = 0; i < length; i++) { |
| 11436 | if (!Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11437 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11438 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11439 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11440 | } |
| 11441 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11442 | PyDoc_STRVAR(isalnum__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11443 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11444 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11445 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11446 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11447 | |
| 11448 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11449 | unicode_isalnum(PyObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11450 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11451 | int kind; |
| 11452 | void *data; |
| 11453 | Py_ssize_t len, i; |
| 11454 | |
| 11455 | if (PyUnicode_READY(self) == -1) |
| 11456 | return NULL; |
| 11457 | |
| 11458 | kind = PyUnicode_KIND(self); |
| 11459 | data = PyUnicode_DATA(self); |
| 11460 | len = PyUnicode_GET_LENGTH(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11461 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11462 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11463 | if (len == 1) { |
| 11464 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11465 | return PyBool_FromLong(Py_UNICODE_ISALNUM(ch)); |
| 11466 | } |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11467 | |
| 11468 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11469 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11470 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11471 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11472 | for (i = 0; i < len; i++) { |
| 11473 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11474 | if (!Py_UNICODE_ISALNUM(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11475 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11476 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11477 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11478 | } |
| 11479 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11480 | PyDoc_STRVAR(isdecimal__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11481 | "S.isdecimal() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11482 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11483 | 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] | 11484 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11485 | |
| 11486 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11487 | unicode_isdecimal(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11488 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11489 | Py_ssize_t i, length; |
| 11490 | int kind; |
| 11491 | void *data; |
| 11492 | |
| 11493 | if (PyUnicode_READY(self) == -1) |
| 11494 | return NULL; |
| 11495 | length = PyUnicode_GET_LENGTH(self); |
| 11496 | kind = PyUnicode_KIND(self); |
| 11497 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11498 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11499 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11500 | if (length == 1) |
| 11501 | return PyBool_FromLong( |
| 11502 | Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11503 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11504 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11505 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11506 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11507 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11508 | for (i = 0; i < length; i++) { |
| 11509 | if (!Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11510 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11511 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11512 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11513 | } |
| 11514 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11515 | PyDoc_STRVAR(isdigit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11516 | "S.isdigit() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11517 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11518 | Return True if all characters in S are digits\n\ |
| 11519 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11520 | |
| 11521 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11522 | unicode_isdigit(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11523 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11524 | Py_ssize_t i, length; |
| 11525 | int kind; |
| 11526 | void *data; |
| 11527 | |
| 11528 | if (PyUnicode_READY(self) == -1) |
| 11529 | return NULL; |
| 11530 | length = PyUnicode_GET_LENGTH(self); |
| 11531 | kind = PyUnicode_KIND(self); |
| 11532 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11533 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11534 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11535 | if (length == 1) { |
| 11536 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11537 | return PyBool_FromLong(Py_UNICODE_ISDIGIT(ch)); |
| 11538 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11539 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11540 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11541 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11542 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11543 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11544 | for (i = 0; i < length; i++) { |
| 11545 | if (!Py_UNICODE_ISDIGIT(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11546 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11547 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11548 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11549 | } |
| 11550 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11551 | PyDoc_STRVAR(isnumeric__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11552 | "S.isnumeric() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11553 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11554 | 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] | 11555 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11556 | |
| 11557 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11558 | unicode_isnumeric(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11559 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11560 | Py_ssize_t i, length; |
| 11561 | int kind; |
| 11562 | void *data; |
| 11563 | |
| 11564 | if (PyUnicode_READY(self) == -1) |
| 11565 | return NULL; |
| 11566 | length = PyUnicode_GET_LENGTH(self); |
| 11567 | kind = PyUnicode_KIND(self); |
| 11568 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11569 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11570 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11571 | if (length == 1) |
| 11572 | return PyBool_FromLong( |
| 11573 | Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11574 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11575 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11576 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11577 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11578 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11579 | for (i = 0; i < length; i++) { |
| 11580 | if (!Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11581 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11582 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11583 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11584 | } |
| 11585 | |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11586 | int |
| 11587 | PyUnicode_IsIdentifier(PyObject *self) |
| 11588 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11589 | int kind; |
| 11590 | void *data; |
| 11591 | Py_ssize_t i; |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11592 | Py_UCS4 first; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11593 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11594 | if (PyUnicode_READY(self) == -1) { |
| 11595 | Py_FatalError("identifier not ready"); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11596 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11597 | } |
| 11598 | |
| 11599 | /* Special case for empty strings */ |
| 11600 | if (PyUnicode_GET_LENGTH(self) == 0) |
| 11601 | return 0; |
| 11602 | kind = PyUnicode_KIND(self); |
| 11603 | data = PyUnicode_DATA(self); |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11604 | |
| 11605 | /* PEP 3131 says that the first character must be in |
| 11606 | XID_Start and subsequent characters in XID_Continue, |
| 11607 | and for the ASCII range, the 2.x rules apply (i.e |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11608 | start with letters and underscore, continue with |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11609 | letters, digits, underscore). However, given the current |
| 11610 | definition of XID_Start and XID_Continue, it is sufficient |
| 11611 | to check just for these, except that _ must be allowed |
| 11612 | as starting an identifier. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11613 | first = PyUnicode_READ(kind, data, 0); |
Benjamin Peterson | f413b80 | 2011-08-12 22:17:18 -0500 | [diff] [blame] | 11614 | if (!_PyUnicode_IsXidStart(first) && first != 0x5F /* LOW LINE */) |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11615 | return 0; |
| 11616 | |
Benjamin Peterson | 9c6e6a0 | 2011-09-28 08:09:05 -0400 | [diff] [blame] | 11617 | for (i = 1; i < PyUnicode_GET_LENGTH(self); i++) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11618 | if (!_PyUnicode_IsXidContinue(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11619 | return 0; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11620 | return 1; |
| 11621 | } |
| 11622 | |
| 11623 | PyDoc_STRVAR(isidentifier__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11624 | "S.isidentifier() -> bool\n\ |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11625 | \n\ |
| 11626 | Return True if S is a valid identifier according\n\ |
| 11627 | to the language definition."); |
| 11628 | |
| 11629 | static PyObject* |
| 11630 | unicode_isidentifier(PyObject *self) |
| 11631 | { |
| 11632 | return PyBool_FromLong(PyUnicode_IsIdentifier(self)); |
| 11633 | } |
| 11634 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11635 | PyDoc_STRVAR(isprintable__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11636 | "S.isprintable() -> bool\n\ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11637 | \n\ |
| 11638 | Return True if all characters in S are considered\n\ |
| 11639 | printable in repr() or S is empty, False otherwise."); |
| 11640 | |
| 11641 | static PyObject* |
| 11642 | unicode_isprintable(PyObject *self) |
| 11643 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11644 | Py_ssize_t i, length; |
| 11645 | int kind; |
| 11646 | void *data; |
| 11647 | |
| 11648 | if (PyUnicode_READY(self) == -1) |
| 11649 | return NULL; |
| 11650 | length = PyUnicode_GET_LENGTH(self); |
| 11651 | kind = PyUnicode_KIND(self); |
| 11652 | data = PyUnicode_DATA(self); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11653 | |
| 11654 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11655 | if (length == 1) |
| 11656 | return PyBool_FromLong( |
| 11657 | Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, 0))); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11658 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11659 | for (i = 0; i < length; i++) { |
| 11660 | if (!Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, i))) { |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11661 | Py_RETURN_FALSE; |
| 11662 | } |
| 11663 | } |
| 11664 | Py_RETURN_TRUE; |
| 11665 | } |
| 11666 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11667 | PyDoc_STRVAR(join__doc__, |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 11668 | "S.join(iterable) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11669 | \n\ |
| 11670 | 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] | 11671 | iterable. The separator between elements is S."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11672 | |
| 11673 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11674 | unicode_join(PyObject *self, PyObject *data) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11675 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11676 | return PyUnicode_Join(self, data); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11677 | } |
| 11678 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11679 | static Py_ssize_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11680 | unicode_length(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11681 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11682 | if (PyUnicode_READY(self) == -1) |
| 11683 | return -1; |
| 11684 | return PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11685 | } |
| 11686 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11687 | PyDoc_STRVAR(ljust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11688 | "S.ljust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11689 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 11690 | 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] | 11691 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11692 | |
| 11693 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11694 | unicode_ljust(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11695 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11696 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11697 | Py_UCS4 fillchar = ' '; |
| 11698 | |
| 11699 | if (PyUnicode_READY(self) == -1) |
| 11700 | return NULL; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 11701 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11702 | if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11703 | return NULL; |
| 11704 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11705 | if (_PyUnicode_LENGTH(self) >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11706 | Py_INCREF(self); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11707 | return self; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11708 | } |
| 11709 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11710 | return pad(self, 0, width - _PyUnicode_LENGTH(self), fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11711 | } |
| 11712 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11713 | PyDoc_STRVAR(lower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11714 | "S.lower() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11715 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11716 | Return a copy of the string S converted to lowercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11717 | |
| 11718 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11719 | unicode_lower(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11720 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11721 | return fixup(self, fixlower); |
| 11722 | } |
| 11723 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11724 | #define LEFTSTRIP 0 |
| 11725 | #define RIGHTSTRIP 1 |
| 11726 | #define BOTHSTRIP 2 |
| 11727 | |
| 11728 | /* Arrays indexed by above */ |
| 11729 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 11730 | |
| 11731 | #define STRIPNAME(i) (stripformat[i]+3) |
| 11732 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11733 | /* externally visible for str.strip(unicode) */ |
| 11734 | PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11735 | _PyUnicode_XStrip(PyObject *self, int striptype, PyObject *sepobj) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11736 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11737 | void *data; |
| 11738 | int kind; |
| 11739 | Py_ssize_t i, j, len; |
| 11740 | BLOOM_MASK sepmask; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11741 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11742 | if (PyUnicode_READY(self) == -1 || PyUnicode_READY(sepobj) == -1) |
| 11743 | return NULL; |
| 11744 | |
| 11745 | kind = PyUnicode_KIND(self); |
| 11746 | data = PyUnicode_DATA(self); |
| 11747 | len = PyUnicode_GET_LENGTH(self); |
| 11748 | sepmask = make_bloom_mask(PyUnicode_KIND(sepobj), |
| 11749 | PyUnicode_DATA(sepobj), |
| 11750 | PyUnicode_GET_LENGTH(sepobj)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11751 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11752 | i = 0; |
| 11753 | if (striptype != RIGHTSTRIP) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11754 | while (i < len && |
| 11755 | BLOOM_MEMBER(sepmask, PyUnicode_READ(kind, data, i), sepobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11756 | i++; |
| 11757 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11758 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11759 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11760 | j = len; |
| 11761 | if (striptype != LEFTSTRIP) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11762 | do { |
| 11763 | j--; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11764 | } while (j >= i && |
| 11765 | BLOOM_MEMBER(sepmask, PyUnicode_READ(kind, data, j), sepobj)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11766 | j++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11767 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11768 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11769 | return PyUnicode_Substring(self, i, j); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11770 | } |
| 11771 | |
| 11772 | PyObject* |
| 11773 | PyUnicode_Substring(PyObject *self, Py_ssize_t start, Py_ssize_t end) |
| 11774 | { |
| 11775 | unsigned char *data; |
| 11776 | int kind; |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11777 | Py_ssize_t length; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11778 | |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 11779 | if (PyUnicode_READY(self) == -1) |
| 11780 | return NULL; |
| 11781 | |
| 11782 | end = Py_MIN(end, PyUnicode_GET_LENGTH(self)); |
| 11783 | |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11784 | if (start == 0 && end == PyUnicode_GET_LENGTH(self)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11785 | { |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11786 | if (PyUnicode_CheckExact(self)) { |
| 11787 | Py_INCREF(self); |
| 11788 | return self; |
| 11789 | } |
| 11790 | else |
| 11791 | return PyUnicode_Copy(self); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11792 | } |
| 11793 | |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11794 | length = end - start; |
| 11795 | if (length == 1) |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 11796 | return unicode_getitem(self, start); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11797 | |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 11798 | if (start < 0 || end < 0) { |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11799 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 11800 | return NULL; |
| 11801 | } |
| 11802 | |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 11803 | if (PyUnicode_IS_ASCII(self)) { |
| 11804 | kind = PyUnicode_KIND(self); |
| 11805 | data = PyUnicode_1BYTE_DATA(self); |
| 11806 | return unicode_fromascii(data + start, length); |
| 11807 | } |
| 11808 | else { |
| 11809 | kind = PyUnicode_KIND(self); |
| 11810 | data = PyUnicode_1BYTE_DATA(self); |
| 11811 | return PyUnicode_FromKindAndData(kind, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 11812 | data + kind * start, |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 11813 | length); |
| 11814 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11815 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11816 | |
| 11817 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11818 | do_strip(PyObject *self, int striptype) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11819 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11820 | int kind; |
| 11821 | void *data; |
| 11822 | Py_ssize_t len, i, j; |
| 11823 | |
| 11824 | if (PyUnicode_READY(self) == -1) |
| 11825 | return NULL; |
| 11826 | |
| 11827 | kind = PyUnicode_KIND(self); |
| 11828 | data = PyUnicode_DATA(self); |
| 11829 | len = PyUnicode_GET_LENGTH(self); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11830 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11831 | i = 0; |
| 11832 | if (striptype != RIGHTSTRIP) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11833 | while (i < len && Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, i))) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11834 | i++; |
| 11835 | } |
| 11836 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11837 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11838 | j = len; |
| 11839 | if (striptype != LEFTSTRIP) { |
| 11840 | do { |
| 11841 | j--; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11842 | } while (j >= i && Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, j))); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11843 | j++; |
| 11844 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11845 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11846 | return PyUnicode_Substring(self, i, j); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11847 | } |
| 11848 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11849 | |
| 11850 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11851 | do_argstrip(PyObject *self, int striptype, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11852 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11853 | PyObject *sep = NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11854 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11855 | if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) |
| 11856 | return NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11857 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11858 | if (sep != NULL && sep != Py_None) { |
| 11859 | if (PyUnicode_Check(sep)) |
| 11860 | return _PyUnicode_XStrip(self, striptype, sep); |
| 11861 | else { |
| 11862 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11863 | "%s arg must be None or str", |
| 11864 | STRIPNAME(striptype)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11865 | return NULL; |
| 11866 | } |
| 11867 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11868 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11869 | return do_strip(self, striptype); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11870 | } |
| 11871 | |
| 11872 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11873 | PyDoc_STRVAR(strip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11874 | "S.strip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11875 | \n\ |
| 11876 | Return a copy of the string S with leading and trailing\n\ |
| 11877 | whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11878 | 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] | 11879 | |
| 11880 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11881 | unicode_strip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11882 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11883 | if (PyTuple_GET_SIZE(args) == 0) |
| 11884 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 11885 | else |
| 11886 | return do_argstrip(self, BOTHSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11887 | } |
| 11888 | |
| 11889 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11890 | PyDoc_STRVAR(lstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11891 | "S.lstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11892 | \n\ |
| 11893 | Return a copy of the string S with leading whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11894 | 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] | 11895 | |
| 11896 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11897 | unicode_lstrip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11898 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11899 | if (PyTuple_GET_SIZE(args) == 0) |
| 11900 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 11901 | else |
| 11902 | return do_argstrip(self, LEFTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11903 | } |
| 11904 | |
| 11905 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11906 | PyDoc_STRVAR(rstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11907 | "S.rstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11908 | \n\ |
| 11909 | Return a copy of the string S with trailing whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11910 | 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] | 11911 | |
| 11912 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11913 | unicode_rstrip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11914 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11915 | if (PyTuple_GET_SIZE(args) == 0) |
| 11916 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 11917 | else |
| 11918 | return do_argstrip(self, RIGHTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11919 | } |
| 11920 | |
| 11921 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11922 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11923 | unicode_repeat(PyObject *str, Py_ssize_t len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11924 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11925 | PyObject *u; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11926 | Py_ssize_t nchars, n; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11927 | |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 11928 | if (len < 1) { |
| 11929 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 11930 | return unicode_empty; |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 11931 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11932 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 11933 | if (len == 1 && PyUnicode_CheckExact(str)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11934 | /* no repeat, return original string */ |
| 11935 | Py_INCREF(str); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11936 | return str; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11937 | } |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 11938 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11939 | if (PyUnicode_READY(str) == -1) |
| 11940 | return NULL; |
| 11941 | |
Victor Stinner | c759f3e | 2011-10-01 03:09:58 +0200 | [diff] [blame] | 11942 | if (PyUnicode_GET_LENGTH(str) > PY_SSIZE_T_MAX / len) { |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11943 | PyErr_SetString(PyExc_OverflowError, |
| 11944 | "repeated string is too long"); |
| 11945 | return NULL; |
| 11946 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11947 | nchars = len * PyUnicode_GET_LENGTH(str); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11948 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11949 | u = PyUnicode_New(nchars, PyUnicode_MAX_CHAR_VALUE(str)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11950 | if (!u) |
| 11951 | return NULL; |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11952 | assert(PyUnicode_KIND(u) == PyUnicode_KIND(str)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11953 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11954 | if (PyUnicode_GET_LENGTH(str) == 1) { |
| 11955 | const int kind = PyUnicode_KIND(str); |
| 11956 | const Py_UCS4 fill_char = PyUnicode_READ(kind, PyUnicode_DATA(str), 0); |
| 11957 | void *to = PyUnicode_DATA(u); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11958 | if (kind == PyUnicode_1BYTE_KIND) |
| 11959 | memset(to, (unsigned char)fill_char, len); |
| 11960 | else { |
| 11961 | for (n = 0; n < len; ++n) |
| 11962 | PyUnicode_WRITE(kind, to, n, fill_char); |
| 11963 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11964 | } |
| 11965 | else { |
| 11966 | /* number of characters copied this far */ |
| 11967 | Py_ssize_t done = PyUnicode_GET_LENGTH(str); |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 11968 | const Py_ssize_t char_size = PyUnicode_KIND(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11969 | char *to = (char *) PyUnicode_DATA(u); |
| 11970 | Py_MEMCPY(to, PyUnicode_DATA(str), |
| 11971 | PyUnicode_GET_LENGTH(str) * char_size); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11972 | while (done < nchars) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11973 | n = (done <= nchars-done) ? done : nchars-done; |
| 11974 | Py_MEMCPY(to + (done * char_size), to, n * char_size); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11975 | done += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11976 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11977 | } |
| 11978 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 11979 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11980 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11981 | } |
| 11982 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 11983 | PyObject * |
| 11984 | PyUnicode_Replace(PyObject *obj, |
| 11985 | PyObject *subobj, |
| 11986 | PyObject *replobj, |
| 11987 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11988 | { |
| 11989 | PyObject *self; |
| 11990 | PyObject *str1; |
| 11991 | PyObject *str2; |
| 11992 | PyObject *result; |
| 11993 | |
| 11994 | self = PyUnicode_FromObject(obj); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11995 | if (self == NULL || PyUnicode_READY(self) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11996 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11997 | str1 = PyUnicode_FromObject(subobj); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11998 | if (str1 == NULL || PyUnicode_READY(str1) == -1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11999 | Py_DECREF(self); |
| 12000 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12001 | } |
| 12002 | str2 = PyUnicode_FromObject(replobj); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 12003 | if (str2 == NULL || PyUnicode_READY(str2)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12004 | Py_DECREF(self); |
| 12005 | Py_DECREF(str1); |
| 12006 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12007 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12008 | result = replace(self, str1, str2, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12009 | Py_DECREF(self); |
| 12010 | Py_DECREF(str1); |
| 12011 | Py_DECREF(str2); |
| 12012 | return result; |
| 12013 | } |
| 12014 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12015 | PyDoc_STRVAR(replace__doc__, |
Ezio Melotti | c1897e7 | 2010-06-26 18:50:39 +0000 | [diff] [blame] | 12016 | "S.replace(old, new[, count]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12017 | \n\ |
| 12018 | Return a copy of S with all occurrences of substring\n\ |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 12019 | old replaced by new. If the optional argument count is\n\ |
| 12020 | given, only the first count occurrences are replaced."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12021 | |
| 12022 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12023 | unicode_replace(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12024 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12025 | PyObject *str1; |
| 12026 | PyObject *str2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12027 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12028 | PyObject *result; |
| 12029 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12030 | if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12031 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12032 | if (!PyUnicode_READY(self) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12033 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12034 | str1 = PyUnicode_FromObject(str1); |
| 12035 | if (str1 == NULL || PyUnicode_READY(str1) == -1) |
| 12036 | return NULL; |
| 12037 | str2 = PyUnicode_FromObject(str2); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 12038 | if (str2 == NULL || PyUnicode_READY(str2) == -1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12039 | Py_DECREF(str1); |
| 12040 | return NULL; |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 12041 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12042 | |
| 12043 | result = replace(self, str1, str2, maxcount); |
| 12044 | |
| 12045 | Py_DECREF(str1); |
| 12046 | Py_DECREF(str2); |
| 12047 | return result; |
| 12048 | } |
| 12049 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12050 | static PyObject * |
| 12051 | unicode_repr(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12052 | { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12053 | PyObject *repr; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12054 | Py_ssize_t isize; |
| 12055 | Py_ssize_t osize, squote, dquote, i, o; |
| 12056 | Py_UCS4 max, quote; |
| 12057 | int ikind, okind; |
| 12058 | void *idata, *odata; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12059 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12060 | if (PyUnicode_READY(unicode) == -1) |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12061 | return NULL; |
| 12062 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12063 | isize = PyUnicode_GET_LENGTH(unicode); |
| 12064 | idata = PyUnicode_DATA(unicode); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12065 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12066 | /* Compute length of output, quote characters, and |
| 12067 | maximum character */ |
| 12068 | osize = 2; /* quotes */ |
| 12069 | max = 127; |
| 12070 | squote = dquote = 0; |
| 12071 | ikind = PyUnicode_KIND(unicode); |
| 12072 | for (i = 0; i < isize; i++) { |
| 12073 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
| 12074 | switch (ch) { |
| 12075 | case '\'': squote++; osize++; break; |
| 12076 | case '"': dquote++; osize++; break; |
| 12077 | case '\\': case '\t': case '\r': case '\n': |
| 12078 | osize += 2; break; |
| 12079 | default: |
| 12080 | /* Fast-path ASCII */ |
| 12081 | if (ch < ' ' || ch == 0x7f) |
| 12082 | osize += 4; /* \xHH */ |
| 12083 | else if (ch < 0x7f) |
| 12084 | osize++; |
| 12085 | else if (Py_UNICODE_ISPRINTABLE(ch)) { |
| 12086 | osize++; |
| 12087 | max = ch > max ? ch : max; |
| 12088 | } |
| 12089 | else if (ch < 0x100) |
| 12090 | osize += 4; /* \xHH */ |
| 12091 | else if (ch < 0x10000) |
| 12092 | osize += 6; /* \uHHHH */ |
| 12093 | else |
| 12094 | osize += 10; /* \uHHHHHHHH */ |
| 12095 | } |
| 12096 | } |
| 12097 | |
| 12098 | quote = '\''; |
| 12099 | if (squote) { |
| 12100 | if (dquote) |
| 12101 | /* Both squote and dquote present. Use squote, |
| 12102 | and escape them */ |
| 12103 | osize += squote; |
| 12104 | else |
| 12105 | quote = '"'; |
| 12106 | } |
| 12107 | |
| 12108 | repr = PyUnicode_New(osize, max); |
| 12109 | if (repr == NULL) |
| 12110 | return NULL; |
| 12111 | okind = PyUnicode_KIND(repr); |
| 12112 | odata = PyUnicode_DATA(repr); |
| 12113 | |
| 12114 | PyUnicode_WRITE(okind, odata, 0, quote); |
| 12115 | PyUnicode_WRITE(okind, odata, osize-1, quote); |
| 12116 | |
| 12117 | for (i = 0, o = 1; i < isize; i++) { |
| 12118 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12119 | |
| 12120 | /* Escape quotes and backslashes */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12121 | if ((ch == quote) || (ch == '\\')) { |
| 12122 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12123 | PyUnicode_WRITE(okind, odata, o++, ch); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12124 | continue; |
| 12125 | } |
| 12126 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12127 | /* Map special whitespace to '\t', \n', '\r' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12128 | if (ch == '\t') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12129 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12130 | PyUnicode_WRITE(okind, odata, o++, 't'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12131 | } |
| 12132 | else if (ch == '\n') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12133 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12134 | PyUnicode_WRITE(okind, odata, o++, 'n'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12135 | } |
| 12136 | else if (ch == '\r') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12137 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12138 | PyUnicode_WRITE(okind, odata, o++, 'r'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12139 | } |
| 12140 | |
| 12141 | /* Map non-printable US ASCII to '\xhh' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12142 | else if (ch < ' ' || ch == 0x7F) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12143 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12144 | PyUnicode_WRITE(okind, odata, o++, 'x'); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 12145 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0x000F]); |
| 12146 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0x000F]); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12147 | } |
| 12148 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12149 | /* Copy ASCII characters as-is */ |
| 12150 | else if (ch < 0x7F) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12151 | PyUnicode_WRITE(okind, odata, o++, ch); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12152 | } |
| 12153 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12154 | /* Non-ASCII characters */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12155 | else { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12156 | /* Map Unicode whitespace and control characters |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12157 | (categories Z* and C* except ASCII space) |
| 12158 | */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12159 | if (!Py_UNICODE_ISPRINTABLE(ch)) { |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12160 | /* Map 8-bit characters to '\xhh' */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12161 | if (ch <= 0xff) { |
| 12162 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12163 | PyUnicode_WRITE(okind, odata, o++, 'x'); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 12164 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0x000F]); |
| 12165 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0x000F]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12166 | } |
| 12167 | /* Map 21-bit characters to '\U00xxxxxx' */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12168 | else if (ch >= 0x10000) { |
| 12169 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12170 | PyUnicode_WRITE(okind, odata, o++, 'U'); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 12171 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 28) & 0xF]); |
| 12172 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 24) & 0xF]); |
| 12173 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 20) & 0xF]); |
| 12174 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 16) & 0xF]); |
| 12175 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 12) & 0xF]); |
| 12176 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 8) & 0xF]); |
| 12177 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0xF]); |
| 12178 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0xF]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12179 | } |
| 12180 | /* Map 16-bit characters to '\uxxxx' */ |
| 12181 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12182 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12183 | PyUnicode_WRITE(okind, odata, o++, 'u'); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 12184 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 12) & 0xF]); |
| 12185 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 8) & 0xF]); |
| 12186 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0xF]); |
| 12187 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0xF]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12188 | } |
| 12189 | } |
| 12190 | /* Copy characters as-is */ |
| 12191 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12192 | PyUnicode_WRITE(okind, odata, o++, ch); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12193 | } |
| 12194 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12195 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12196 | /* Closing quote already added at the beginning */ |
Victor Stinner | 05d1189 | 2011-10-06 01:13:58 +0200 | [diff] [blame] | 12197 | assert(_PyUnicode_CheckConsistency(repr, 1)); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12198 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12199 | } |
| 12200 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12201 | PyDoc_STRVAR(rfind__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12202 | "S.rfind(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12203 | \n\ |
| 12204 | Return the highest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 12205 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12206 | arguments start and end are interpreted as in slice notation.\n\ |
| 12207 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12208 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12209 | |
| 12210 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12211 | unicode_rfind(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12212 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12213 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 12214 | Py_ssize_t start; |
| 12215 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12216 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12217 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12218 | if (!stringlib_parse_args_finds_unicode("rfind", args, &substring, |
| 12219 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12220 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12221 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12222 | if (PyUnicode_READY(self) == -1) |
| 12223 | return NULL; |
| 12224 | if (PyUnicode_READY(substring) == -1) |
| 12225 | return NULL; |
| 12226 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12227 | result = any_find_slice(-1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12228 | |
| 12229 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12230 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12231 | if (result == -2) |
| 12232 | return NULL; |
| 12233 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12234 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12235 | } |
| 12236 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12237 | PyDoc_STRVAR(rindex__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12238 | "S.rindex(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12239 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12240 | 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] | 12241 | |
| 12242 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12243 | unicode_rindex(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12244 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12245 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 12246 | Py_ssize_t start; |
| 12247 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12248 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12249 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12250 | if (!stringlib_parse_args_finds_unicode("rindex", args, &substring, |
| 12251 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12252 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12253 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12254 | if (PyUnicode_READY(self) == -1) |
| 12255 | return NULL; |
| 12256 | if (PyUnicode_READY(substring) == -1) |
| 12257 | return NULL; |
| 12258 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12259 | result = any_find_slice(-1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12260 | |
| 12261 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12262 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12263 | if (result == -2) |
| 12264 | return NULL; |
| 12265 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12266 | if (result < 0) { |
| 12267 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 12268 | return NULL; |
| 12269 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12270 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12271 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12272 | } |
| 12273 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12274 | PyDoc_STRVAR(rjust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12275 | "S.rjust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12276 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 12277 | 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] | 12278 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12279 | |
| 12280 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12281 | unicode_rjust(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12282 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12283 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12284 | Py_UCS4 fillchar = ' '; |
| 12285 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 12286 | 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] | 12287 | return NULL; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 12288 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 12289 | if (PyUnicode_READY(self) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12290 | return NULL; |
| 12291 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12292 | if (_PyUnicode_LENGTH(self) >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12293 | Py_INCREF(self); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12294 | return self; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12295 | } |
| 12296 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12297 | return pad(self, width - _PyUnicode_LENGTH(self), 0, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12298 | } |
| 12299 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12300 | PyObject * |
| 12301 | PyUnicode_Split(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12302 | { |
| 12303 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 12304 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12305 | s = PyUnicode_FromObject(s); |
| 12306 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12307 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12308 | if (sep != NULL) { |
| 12309 | sep = PyUnicode_FromObject(sep); |
| 12310 | if (sep == NULL) { |
| 12311 | Py_DECREF(s); |
| 12312 | return NULL; |
| 12313 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12314 | } |
| 12315 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12316 | result = split(s, sep, maxsplit); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12317 | |
| 12318 | Py_DECREF(s); |
| 12319 | Py_XDECREF(sep); |
| 12320 | return result; |
| 12321 | } |
| 12322 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12323 | PyDoc_STRVAR(split__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12324 | "S.split([sep[, maxsplit]]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12325 | \n\ |
| 12326 | Return a list of the words in S, using sep as the\n\ |
| 12327 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 12328 | 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] | 12329 | whitespace string is a separator and empty strings are\n\ |
| 12330 | removed from the result."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12331 | |
| 12332 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12333 | unicode_split(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12334 | { |
| 12335 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12336 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12337 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12338 | if (!PyArg_ParseTuple(args, "|On:split", &substring, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12339 | return NULL; |
| 12340 | |
| 12341 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12342 | return split(self, NULL, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12343 | else if (PyUnicode_Check(substring)) |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12344 | return split(self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12345 | else |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12346 | return PyUnicode_Split(self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12347 | } |
| 12348 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12349 | PyObject * |
| 12350 | PyUnicode_Partition(PyObject *str_in, PyObject *sep_in) |
| 12351 | { |
| 12352 | PyObject* str_obj; |
| 12353 | PyObject* sep_obj; |
| 12354 | PyObject* out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12355 | int kind1, kind2, kind; |
| 12356 | void *buf1 = NULL, *buf2 = NULL; |
| 12357 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12358 | |
| 12359 | str_obj = PyUnicode_FromObject(str_in); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 12360 | if (!str_obj || PyUnicode_READY(str_obj) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12361 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12362 | sep_obj = PyUnicode_FromObject(sep_in); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12363 | if (!sep_obj || PyUnicode_READY(sep_obj) == -1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12364 | Py_DECREF(str_obj); |
| 12365 | return NULL; |
| 12366 | } |
| 12367 | |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12368 | kind1 = PyUnicode_KIND(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12369 | kind2 = PyUnicode_KIND(sep_obj); |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12370 | kind = Py_MAX(kind1, kind2); |
| 12371 | buf1 = PyUnicode_DATA(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12372 | if (kind1 != kind) |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12373 | buf1 = _PyUnicode_AsKind(str_obj, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12374 | if (!buf1) |
| 12375 | goto onError; |
| 12376 | buf2 = PyUnicode_DATA(sep_obj); |
| 12377 | if (kind2 != kind) |
| 12378 | buf2 = _PyUnicode_AsKind(sep_obj, kind); |
| 12379 | if (!buf2) |
| 12380 | goto onError; |
| 12381 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 12382 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
| 12383 | |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12384 | switch(PyUnicode_KIND(str_obj)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12385 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 12386 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sep_obj)) |
| 12387 | out = asciilib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12388 | else |
| 12389 | 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] | 12390 | break; |
| 12391 | case PyUnicode_2BYTE_KIND: |
| 12392 | out = ucs2lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12393 | break; |
| 12394 | case PyUnicode_4BYTE_KIND: |
| 12395 | out = ucs4lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12396 | break; |
| 12397 | default: |
| 12398 | assert(0); |
| 12399 | out = 0; |
| 12400 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12401 | |
| 12402 | Py_DECREF(sep_obj); |
| 12403 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12404 | if (kind1 != kind) |
| 12405 | PyMem_Free(buf1); |
| 12406 | if (kind2 != kind) |
| 12407 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12408 | |
| 12409 | return out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12410 | onError: |
| 12411 | Py_DECREF(sep_obj); |
| 12412 | Py_DECREF(str_obj); |
| 12413 | if (kind1 != kind && buf1) |
| 12414 | PyMem_Free(buf1); |
| 12415 | if (kind2 != kind && buf2) |
| 12416 | PyMem_Free(buf2); |
| 12417 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12418 | } |
| 12419 | |
| 12420 | |
| 12421 | PyObject * |
| 12422 | PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in) |
| 12423 | { |
| 12424 | PyObject* str_obj; |
| 12425 | PyObject* sep_obj; |
| 12426 | PyObject* out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12427 | int kind1, kind2, kind; |
| 12428 | void *buf1 = NULL, *buf2 = NULL; |
| 12429 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12430 | |
| 12431 | str_obj = PyUnicode_FromObject(str_in); |
| 12432 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12433 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12434 | sep_obj = PyUnicode_FromObject(sep_in); |
| 12435 | if (!sep_obj) { |
| 12436 | Py_DECREF(str_obj); |
| 12437 | return NULL; |
| 12438 | } |
| 12439 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12440 | kind1 = PyUnicode_KIND(str_in); |
| 12441 | kind2 = PyUnicode_KIND(sep_obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 12442 | kind = Py_MAX(kind1, kind2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12443 | buf1 = PyUnicode_DATA(str_in); |
| 12444 | if (kind1 != kind) |
| 12445 | buf1 = _PyUnicode_AsKind(str_in, kind); |
| 12446 | if (!buf1) |
| 12447 | goto onError; |
| 12448 | buf2 = PyUnicode_DATA(sep_obj); |
| 12449 | if (kind2 != kind) |
| 12450 | buf2 = _PyUnicode_AsKind(sep_obj, kind); |
| 12451 | if (!buf2) |
| 12452 | goto onError; |
| 12453 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 12454 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
| 12455 | |
| 12456 | switch(PyUnicode_KIND(str_in)) { |
| 12457 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 12458 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sep_obj)) |
| 12459 | out = asciilib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12460 | else |
| 12461 | 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] | 12462 | break; |
| 12463 | case PyUnicode_2BYTE_KIND: |
| 12464 | out = ucs2lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12465 | break; |
| 12466 | case PyUnicode_4BYTE_KIND: |
| 12467 | out = ucs4lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12468 | break; |
| 12469 | default: |
| 12470 | assert(0); |
| 12471 | out = 0; |
| 12472 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12473 | |
| 12474 | Py_DECREF(sep_obj); |
| 12475 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12476 | if (kind1 != kind) |
| 12477 | PyMem_Free(buf1); |
| 12478 | if (kind2 != kind) |
| 12479 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12480 | |
| 12481 | return out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12482 | onError: |
| 12483 | Py_DECREF(sep_obj); |
| 12484 | Py_DECREF(str_obj); |
| 12485 | if (kind1 != kind && buf1) |
| 12486 | PyMem_Free(buf1); |
| 12487 | if (kind2 != kind && buf2) |
| 12488 | PyMem_Free(buf2); |
| 12489 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12490 | } |
| 12491 | |
| 12492 | PyDoc_STRVAR(partition__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12493 | "S.partition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12494 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 12495 | 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] | 12496 | 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] | 12497 | found, return S and two empty strings."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12498 | |
| 12499 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12500 | unicode_partition(PyObject *self, PyObject *separator) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12501 | { |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12502 | return PyUnicode_Partition(self, separator); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12503 | } |
| 12504 | |
| 12505 | PyDoc_STRVAR(rpartition__doc__, |
Ezio Melotti | 5b2b242 | 2010-01-25 11:58:28 +0000 | [diff] [blame] | 12506 | "S.rpartition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12507 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 12508 | 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] | 12509 | 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] | 12510 | separator is not found, return two empty strings and S."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12511 | |
| 12512 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12513 | unicode_rpartition(PyObject *self, PyObject *separator) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12514 | { |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12515 | return PyUnicode_RPartition(self, separator); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12516 | } |
| 12517 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12518 | PyObject * |
| 12519 | PyUnicode_RSplit(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12520 | { |
| 12521 | PyObject *result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12522 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12523 | s = PyUnicode_FromObject(s); |
| 12524 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12525 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12526 | if (sep != NULL) { |
| 12527 | sep = PyUnicode_FromObject(sep); |
| 12528 | if (sep == NULL) { |
| 12529 | Py_DECREF(s); |
| 12530 | return NULL; |
| 12531 | } |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12532 | } |
| 12533 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12534 | result = rsplit(s, sep, maxsplit); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12535 | |
| 12536 | Py_DECREF(s); |
| 12537 | Py_XDECREF(sep); |
| 12538 | return result; |
| 12539 | } |
| 12540 | |
| 12541 | PyDoc_STRVAR(rsplit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12542 | "S.rsplit([sep[, maxsplit]]) -> list of strings\n\ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12543 | \n\ |
| 12544 | Return a list of the words in S, using sep as the\n\ |
| 12545 | delimiter string, starting at the end of the string and\n\ |
| 12546 | working to the front. If maxsplit is given, at most maxsplit\n\ |
| 12547 | splits are done. If sep is not specified, any whitespace string\n\ |
| 12548 | is a separator."); |
| 12549 | |
| 12550 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12551 | unicode_rsplit(PyObject *self, PyObject *args) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12552 | { |
| 12553 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12554 | Py_ssize_t maxcount = -1; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12555 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12556 | if (!PyArg_ParseTuple(args, "|On:rsplit", &substring, &maxcount)) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12557 | return NULL; |
| 12558 | |
| 12559 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12560 | return rsplit(self, NULL, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12561 | else if (PyUnicode_Check(substring)) |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12562 | return rsplit(self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12563 | else |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12564 | return PyUnicode_RSplit(self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12565 | } |
| 12566 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12567 | PyDoc_STRVAR(splitlines__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12568 | "S.splitlines([keepends]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12569 | \n\ |
| 12570 | 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] | 12571 | 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] | 12572 | is given and true."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12573 | |
| 12574 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12575 | unicode_splitlines(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12576 | { |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12577 | static char *kwlist[] = {"keepends", 0}; |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 12578 | int keepends = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12579 | |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12580 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|i:splitlines", |
| 12581 | kwlist, &keepends)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12582 | return NULL; |
| 12583 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12584 | return PyUnicode_Splitlines(self, keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12585 | } |
| 12586 | |
| 12587 | static |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 12588 | PyObject *unicode_str(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12589 | { |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 12590 | if (PyUnicode_CheckExact(self)) { |
| 12591 | Py_INCREF(self); |
| 12592 | return self; |
| 12593 | } else |
| 12594 | /* Subtype -- return genuine unicode string with the same value. */ |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 12595 | return PyUnicode_Copy(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12596 | } |
| 12597 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12598 | PyDoc_STRVAR(swapcase__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12599 | "S.swapcase() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12600 | \n\ |
| 12601 | 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] | 12602 | and vice versa."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12603 | |
| 12604 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12605 | unicode_swapcase(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12606 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12607 | return fixup(self, fixswapcase); |
| 12608 | } |
| 12609 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12610 | PyDoc_STRVAR(maketrans__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12611 | "str.maketrans(x[, y[, z]]) -> dict (static method)\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12612 | \n\ |
| 12613 | Return a translation table usable for str.translate().\n\ |
| 12614 | If there is only one argument, it must be a dictionary mapping Unicode\n\ |
| 12615 | ordinals (integers) or characters to Unicode ordinals, strings or None.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12616 | Character keys will be then converted to ordinals.\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12617 | If there are two arguments, they must be strings of equal length, and\n\ |
| 12618 | in the resulting dictionary, each character in x will be mapped to the\n\ |
| 12619 | character at the same position in y. If there is a third argument, it\n\ |
| 12620 | must be a string, whose characters will be mapped to None in the result."); |
| 12621 | |
| 12622 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12623 | unicode_maketrans(PyObject *null, PyObject *args) |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12624 | { |
| 12625 | PyObject *x, *y = NULL, *z = NULL; |
| 12626 | PyObject *new = NULL, *key, *value; |
| 12627 | Py_ssize_t i = 0; |
| 12628 | int res; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12629 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12630 | if (!PyArg_ParseTuple(args, "O|UU:maketrans", &x, &y, &z)) |
| 12631 | return NULL; |
| 12632 | new = PyDict_New(); |
| 12633 | if (!new) |
| 12634 | return NULL; |
| 12635 | if (y != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12636 | int x_kind, y_kind, z_kind; |
| 12637 | void *x_data, *y_data, *z_data; |
| 12638 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12639 | /* x must be a string too, of equal length */ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12640 | if (!PyUnicode_Check(x)) { |
| 12641 | PyErr_SetString(PyExc_TypeError, "first maketrans argument must " |
| 12642 | "be a string if there is a second argument"); |
| 12643 | goto err; |
| 12644 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12645 | if (PyUnicode_GET_LENGTH(x) != PyUnicode_GET_LENGTH(y)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12646 | PyErr_SetString(PyExc_ValueError, "the first two maketrans " |
| 12647 | "arguments must have equal length"); |
| 12648 | goto err; |
| 12649 | } |
| 12650 | /* 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] | 12651 | x_kind = PyUnicode_KIND(x); |
| 12652 | y_kind = PyUnicode_KIND(y); |
| 12653 | x_data = PyUnicode_DATA(x); |
| 12654 | y_data = PyUnicode_DATA(y); |
| 12655 | for (i = 0; i < PyUnicode_GET_LENGTH(x); i++) { |
| 12656 | key = PyLong_FromLong(PyUnicode_READ(x_kind, x_data, i)); |
| 12657 | value = PyLong_FromLong(PyUnicode_READ(y_kind, y_data, i)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12658 | if (!key || !value) |
| 12659 | goto err; |
| 12660 | res = PyDict_SetItem(new, key, value); |
| 12661 | Py_DECREF(key); |
| 12662 | Py_DECREF(value); |
| 12663 | if (res < 0) |
| 12664 | goto err; |
| 12665 | } |
| 12666 | /* create entries for deleting chars in z */ |
| 12667 | if (z != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12668 | z_kind = PyUnicode_KIND(z); |
| 12669 | z_data = PyUnicode_DATA(z); |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 12670 | for (i = 0; i < PyUnicode_GET_LENGTH(z); i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12671 | key = PyLong_FromLong(PyUnicode_READ(z_kind, z_data, i)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12672 | if (!key) |
| 12673 | goto err; |
| 12674 | res = PyDict_SetItem(new, key, Py_None); |
| 12675 | Py_DECREF(key); |
| 12676 | if (res < 0) |
| 12677 | goto err; |
| 12678 | } |
| 12679 | } |
| 12680 | } else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12681 | int kind; |
| 12682 | void *data; |
| 12683 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12684 | /* x must be a dict */ |
Raymond Hettinger | 3ad0576 | 2009-05-29 22:11:22 +0000 | [diff] [blame] | 12685 | if (!PyDict_CheckExact(x)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12686 | PyErr_SetString(PyExc_TypeError, "if you give only one argument " |
| 12687 | "to maketrans it must be a dict"); |
| 12688 | goto err; |
| 12689 | } |
| 12690 | /* copy entries into the new dict, converting string keys to int keys */ |
| 12691 | while (PyDict_Next(x, &i, &key, &value)) { |
| 12692 | if (PyUnicode_Check(key)) { |
| 12693 | /* convert string keys to integer keys */ |
| 12694 | PyObject *newkey; |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 12695 | if (PyUnicode_GET_LENGTH(key) != 1) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12696 | PyErr_SetString(PyExc_ValueError, "string keys in translate " |
| 12697 | "table must be of length 1"); |
| 12698 | goto err; |
| 12699 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12700 | kind = PyUnicode_KIND(key); |
| 12701 | data = PyUnicode_DATA(key); |
| 12702 | newkey = PyLong_FromLong(PyUnicode_READ(kind, data, 0)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12703 | if (!newkey) |
| 12704 | goto err; |
| 12705 | res = PyDict_SetItem(new, newkey, value); |
| 12706 | Py_DECREF(newkey); |
| 12707 | if (res < 0) |
| 12708 | goto err; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12709 | } else if (PyLong_Check(key)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12710 | /* just keep integer keys */ |
| 12711 | if (PyDict_SetItem(new, key, value) < 0) |
| 12712 | goto err; |
| 12713 | } else { |
| 12714 | PyErr_SetString(PyExc_TypeError, "keys in translate table must " |
| 12715 | "be strings or integers"); |
| 12716 | goto err; |
| 12717 | } |
| 12718 | } |
| 12719 | } |
| 12720 | return new; |
| 12721 | err: |
| 12722 | Py_DECREF(new); |
| 12723 | return NULL; |
| 12724 | } |
| 12725 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12726 | PyDoc_STRVAR(translate__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12727 | "S.translate(table) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12728 | \n\ |
| 12729 | Return a copy of the string S, where all characters have been mapped\n\ |
| 12730 | through the given translation table, which must be a mapping of\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12731 | Unicode ordinals to Unicode ordinals, strings, or None.\n\ |
Walter Dörwald | 5c1ee17 | 2002-09-04 20:31:32 +0000 | [diff] [blame] | 12732 | Unmapped characters are left untouched. Characters mapped to None\n\ |
| 12733 | are deleted."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12734 | |
| 12735 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12736 | unicode_translate(PyObject *self, PyObject *table) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12737 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12738 | return _PyUnicode_TranslateCharmap(self, table, "ignore"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12739 | } |
| 12740 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12741 | PyDoc_STRVAR(upper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12742 | "S.upper() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12743 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12744 | Return a copy of S converted to uppercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12745 | |
| 12746 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12747 | unicode_upper(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12748 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12749 | return fixup(self, fixupper); |
| 12750 | } |
| 12751 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12752 | PyDoc_STRVAR(zfill__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12753 | "S.zfill(width) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12754 | \n\ |
Benjamin Peterson | 9aa4299 | 2008-09-10 21:57:34 +0000 | [diff] [blame] | 12755 | Pad a numeric string S with zeros on the left, to fill a field\n\ |
| 12756 | of the specified width. The string S is never truncated."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12757 | |
| 12758 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12759 | unicode_zfill(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12760 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12761 | Py_ssize_t fill; |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12762 | PyObject *u; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12763 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12764 | int kind; |
| 12765 | void *data; |
| 12766 | Py_UCS4 chr; |
| 12767 | |
| 12768 | if (PyUnicode_READY(self) == -1) |
| 12769 | return NULL; |
| 12770 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12771 | if (!PyArg_ParseTuple(args, "n:zfill", &width)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12772 | return NULL; |
| 12773 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12774 | if (PyUnicode_GET_LENGTH(self) >= width) { |
Walter Dörwald | 0fe940c | 2002-04-15 18:42:15 +0000 | [diff] [blame] | 12775 | if (PyUnicode_CheckExact(self)) { |
| 12776 | Py_INCREF(self); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12777 | return self; |
Walter Dörwald | 0fe940c | 2002-04-15 18:42:15 +0000 | [diff] [blame] | 12778 | } |
| 12779 | else |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12780 | return PyUnicode_Copy(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12781 | } |
| 12782 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12783 | fill = width - _PyUnicode_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12784 | |
| 12785 | u = pad(self, fill, 0, '0'); |
| 12786 | |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 12787 | if (u == NULL) |
| 12788 | return NULL; |
| 12789 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12790 | kind = PyUnicode_KIND(u); |
| 12791 | data = PyUnicode_DATA(u); |
| 12792 | chr = PyUnicode_READ(kind, data, fill); |
| 12793 | |
| 12794 | if (chr == '+' || chr == '-') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12795 | /* move sign to beginning of string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12796 | PyUnicode_WRITE(kind, data, 0, chr); |
| 12797 | PyUnicode_WRITE(kind, data, fill, '0'); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12798 | } |
| 12799 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12800 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12801 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12802 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12803 | |
| 12804 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12805 | static PyObject * |
| 12806 | unicode__decimal2ascii(PyObject *self) |
| 12807 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12808 | return PyUnicode_TransformDecimalAndSpaceToASCII(self); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12809 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12810 | #endif |
| 12811 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12812 | PyDoc_STRVAR(startswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12813 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12814 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 12815 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 12816 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12817 | With optional end, stop comparing S at that position.\n\ |
| 12818 | prefix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12819 | |
| 12820 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12821 | unicode_startswith(PyObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12822 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12823 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12824 | PyObject *subobj; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12825 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12826 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12827 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12828 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12829 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12830 | if (!stringlib_parse_args_finds("startswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12831 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12832 | if (PyTuple_Check(subobj)) { |
| 12833 | Py_ssize_t i; |
| 12834 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12835 | substring = PyUnicode_FromObject(PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12836 | if (substring == NULL) |
| 12837 | return NULL; |
| 12838 | result = tailmatch(self, substring, start, end, -1); |
| 12839 | Py_DECREF(substring); |
| 12840 | if (result) { |
| 12841 | Py_RETURN_TRUE; |
| 12842 | } |
| 12843 | } |
| 12844 | /* nothing matched */ |
| 12845 | Py_RETURN_FALSE; |
| 12846 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12847 | substring = PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12848 | if (substring == NULL) { |
| 12849 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 12850 | PyErr_Format(PyExc_TypeError, "startswith first arg must be str or " |
| 12851 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12852 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12853 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12854 | result = tailmatch(self, substring, start, end, -1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12855 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12856 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12857 | } |
| 12858 | |
| 12859 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12860 | PyDoc_STRVAR(endswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12861 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12862 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 12863 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 12864 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12865 | With optional end, stop comparing S at that position.\n\ |
| 12866 | suffix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12867 | |
| 12868 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12869 | unicode_endswith(PyObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12870 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12871 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12872 | PyObject *subobj; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12873 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12874 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12875 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12876 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12877 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12878 | if (!stringlib_parse_args_finds("endswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12879 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12880 | if (PyTuple_Check(subobj)) { |
| 12881 | Py_ssize_t i; |
| 12882 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12883 | substring = PyUnicode_FromObject( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12884 | PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12885 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12886 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12887 | result = tailmatch(self, substring, start, end, +1); |
| 12888 | Py_DECREF(substring); |
| 12889 | if (result) { |
| 12890 | Py_RETURN_TRUE; |
| 12891 | } |
| 12892 | } |
| 12893 | Py_RETURN_FALSE; |
| 12894 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12895 | substring = PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12896 | if (substring == NULL) { |
| 12897 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 12898 | PyErr_Format(PyExc_TypeError, "endswith first arg must be str or " |
| 12899 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12900 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12901 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12902 | result = tailmatch(self, substring, start, end, +1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12903 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12904 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12905 | } |
| 12906 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12907 | #include "stringlib/unicode_format.h" |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12908 | |
| 12909 | PyDoc_STRVAR(format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12910 | "S.format(*args, **kwargs) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12911 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 12912 | Return a formatted version of S, using substitutions from args and kwargs.\n\ |
| 12913 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12914 | |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 12915 | PyDoc_STRVAR(format_map__doc__, |
| 12916 | "S.format_map(mapping) -> str\n\ |
| 12917 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 12918 | Return a formatted version of S, using substitutions from mapping.\n\ |
| 12919 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 12920 | |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 12921 | static PyObject * |
| 12922 | unicode__format__(PyObject* self, PyObject* args) |
| 12923 | { |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12924 | PyObject *format_spec, *out; |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 12925 | |
| 12926 | if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) |
| 12927 | return NULL; |
| 12928 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12929 | out = _PyUnicode_FormatAdvanced(self, format_spec, 0, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12930 | PyUnicode_GET_LENGTH(format_spec)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12931 | return out; |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 12932 | } |
| 12933 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12934 | PyDoc_STRVAR(p_format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12935 | "S.__format__(format_spec) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12936 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 12937 | Return a formatted version of S as described by format_spec."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12938 | |
| 12939 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12940 | unicode__sizeof__(PyObject *v) |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 12941 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12942 | Py_ssize_t size; |
| 12943 | |
| 12944 | /* If it's a compact object, account for base structure + |
| 12945 | character data. */ |
| 12946 | if (PyUnicode_IS_COMPACT_ASCII(v)) |
| 12947 | size = sizeof(PyASCIIObject) + PyUnicode_GET_LENGTH(v) + 1; |
| 12948 | else if (PyUnicode_IS_COMPACT(v)) |
| 12949 | size = sizeof(PyCompactUnicodeObject) + |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 12950 | (PyUnicode_GET_LENGTH(v) + 1) * PyUnicode_KIND(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12951 | else { |
| 12952 | /* If it is a two-block object, account for base object, and |
| 12953 | for character block if present. */ |
| 12954 | size = sizeof(PyUnicodeObject); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 12955 | if (_PyUnicode_DATA_ANY(v)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12956 | size += (PyUnicode_GET_LENGTH(v) + 1) * |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 12957 | PyUnicode_KIND(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12958 | } |
| 12959 | /* 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] | 12960 | with the data pointer. Check if the data is not shared. */ |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 12961 | if (_PyUnicode_HAS_WSTR_MEMORY(v)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12962 | size += (PyUnicode_WSTR_LENGTH(v) + 1) * sizeof(wchar_t); |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 12963 | if (_PyUnicode_HAS_UTF8_MEMORY(v)) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 12964 | size += PyUnicode_UTF8_LENGTH(v) + 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12965 | |
| 12966 | return PyLong_FromSsize_t(size); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 12967 | } |
| 12968 | |
| 12969 | PyDoc_STRVAR(sizeof__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12970 | "S.__sizeof__() -> size of S in memory, in bytes"); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 12971 | |
| 12972 | static PyObject * |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 12973 | unicode_getnewargs(PyObject *v) |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 12974 | { |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 12975 | PyObject *copy = PyUnicode_Copy(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12976 | if (!copy) |
| 12977 | return NULL; |
| 12978 | return Py_BuildValue("(N)", copy); |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 12979 | } |
| 12980 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12981 | static PyMethodDef unicode_methods[] = { |
| 12982 | |
| 12983 | /* Order is according to common usage: often used methods should |
| 12984 | appear first, since lookup is done sequentially. */ |
| 12985 | |
Benjamin Peterson | 28a4dce | 2010-12-12 01:33:04 +0000 | [diff] [blame] | 12986 | {"encode", (PyCFunction) unicode_encode, METH_VARARGS | METH_KEYWORDS, encode__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12987 | {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, |
| 12988 | {"split", (PyCFunction) unicode_split, METH_VARARGS, split__doc__}, |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12989 | {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS, rsplit__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12990 | {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, |
| 12991 | {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, |
| 12992 | {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, |
| 12993 | {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, |
| 12994 | {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, |
| 12995 | {"expandtabs", (PyCFunction) unicode_expandtabs, METH_VARARGS, expandtabs__doc__}, |
| 12996 | {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12997 | {"partition", (PyCFunction) unicode_partition, METH_O, partition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12998 | {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, |
| 12999 | {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, |
| 13000 | {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 13001 | {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13002 | {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, |
| 13003 | {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, |
| 13004 | {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 13005 | {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13006 | {"rpartition", (PyCFunction) unicode_rpartition, METH_O, rpartition__doc__}, |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 13007 | {"splitlines", (PyCFunction) unicode_splitlines, METH_VARARGS | METH_KEYWORDS, splitlines__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 13008 | {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13009 | {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, |
| 13010 | {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, |
| 13011 | {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, |
| 13012 | {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, |
| 13013 | {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, |
| 13014 | {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, |
| 13015 | {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, |
| 13016 | {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, |
| 13017 | {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, |
| 13018 | {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, |
| 13019 | {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, |
| 13020 | {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, |
| 13021 | {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, |
| 13022 | {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 13023 | {"isidentifier", (PyCFunction) unicode_isidentifier, METH_NOARGS, isidentifier__doc__}, |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 13024 | {"isprintable", (PyCFunction) unicode_isprintable, METH_NOARGS, isprintable__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13025 | {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, |
Eric Smith | 9cd1e09 | 2007-08-31 18:39:38 +0000 | [diff] [blame] | 13026 | {"format", (PyCFunction) do_string_format, METH_VARARGS | METH_KEYWORDS, format__doc__}, |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 13027 | {"format_map", (PyCFunction) do_string_format_map, METH_O, format_map__doc__}, |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 13028 | {"__format__", (PyCFunction) unicode__format__, METH_VARARGS, p_format__doc__}, |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 13029 | {"maketrans", (PyCFunction) unicode_maketrans, |
| 13030 | METH_VARARGS | METH_STATIC, maketrans__doc__}, |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13031 | {"__sizeof__", (PyCFunction) unicode__sizeof__, METH_NOARGS, sizeof__doc__}, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 13032 | #if 0 |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13033 | {"capwords", (PyCFunction) unicode_capwords, METH_NOARGS, capwords__doc__}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13034 | #endif |
| 13035 | |
| 13036 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 13037 | /* These methods are just used for debugging the implementation. */ |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 13038 | {"_decimal2ascii", (PyCFunction) unicode__decimal2ascii, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13039 | #endif |
| 13040 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13041 | {"__getnewargs__", (PyCFunction)unicode_getnewargs, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13042 | {NULL, NULL} |
| 13043 | }; |
| 13044 | |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 13045 | static PyObject * |
| 13046 | unicode_mod(PyObject *v, PyObject *w) |
| 13047 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 13048 | if (!PyUnicode_Check(v)) |
| 13049 | Py_RETURN_NOTIMPLEMENTED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13050 | return PyUnicode_Format(v, w); |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 13051 | } |
| 13052 | |
| 13053 | static PyNumberMethods unicode_as_number = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13054 | 0, /*nb_add*/ |
| 13055 | 0, /*nb_subtract*/ |
| 13056 | 0, /*nb_multiply*/ |
| 13057 | unicode_mod, /*nb_remainder*/ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 13058 | }; |
| 13059 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13060 | static PySequenceMethods unicode_as_sequence = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13061 | (lenfunc) unicode_length, /* sq_length */ |
| 13062 | PyUnicode_Concat, /* sq_concat */ |
| 13063 | (ssizeargfunc) unicode_repeat, /* sq_repeat */ |
| 13064 | (ssizeargfunc) unicode_getitem, /* sq_item */ |
| 13065 | 0, /* sq_slice */ |
| 13066 | 0, /* sq_ass_item */ |
| 13067 | 0, /* sq_ass_slice */ |
| 13068 | PyUnicode_Contains, /* sq_contains */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13069 | }; |
| 13070 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13071 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13072 | unicode_subscript(PyObject* self, PyObject* item) |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13073 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13074 | if (PyUnicode_READY(self) == -1) |
| 13075 | return NULL; |
| 13076 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 13077 | if (PyIndex_Check(item)) { |
| 13078 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13079 | if (i == -1 && PyErr_Occurred()) |
| 13080 | return NULL; |
| 13081 | if (i < 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13082 | i += PyUnicode_GET_LENGTH(self); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13083 | return unicode_getitem(self, i); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13084 | } else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13085 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13086 | PyObject *result; |
| 13087 | void *src_data, *dest_data; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13088 | int src_kind, dest_kind; |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 13089 | Py_UCS4 ch, max_char, kind_limit; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13090 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13091 | if (PySlice_GetIndicesEx(item, PyUnicode_GET_LENGTH(self), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13092 | &start, &stop, &step, &slicelength) < 0) { |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13093 | return NULL; |
| 13094 | } |
| 13095 | |
| 13096 | if (slicelength <= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13097 | return PyUnicode_New(0, 0); |
| 13098 | } else if (start == 0 && step == 1 && |
| 13099 | slicelength == PyUnicode_GET_LENGTH(self) && |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 13100 | PyUnicode_CheckExact(self)) { |
| 13101 | Py_INCREF(self); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13102 | return self; |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 13103 | } else if (step == 1) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13104 | return PyUnicode_Substring(self, |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 13105 | start, start + slicelength); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13106 | } |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13107 | /* General case */ |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13108 | src_kind = PyUnicode_KIND(self); |
| 13109 | src_data = PyUnicode_DATA(self); |
Victor Stinner | 55c9911 | 2011-10-13 01:17:06 +0200 | [diff] [blame] | 13110 | if (!PyUnicode_IS_ASCII(self)) { |
| 13111 | kind_limit = kind_maxchar_limit(src_kind); |
| 13112 | max_char = 0; |
| 13113 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
| 13114 | ch = PyUnicode_READ(src_kind, src_data, cur); |
| 13115 | if (ch > max_char) { |
| 13116 | max_char = ch; |
| 13117 | if (max_char >= kind_limit) |
| 13118 | break; |
| 13119 | } |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 13120 | } |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13121 | } |
Victor Stinner | 55c9911 | 2011-10-13 01:17:06 +0200 | [diff] [blame] | 13122 | else |
| 13123 | max_char = 127; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13124 | result = PyUnicode_New(slicelength, max_char); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13125 | if (result == NULL) |
| 13126 | return NULL; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13127 | dest_kind = PyUnicode_KIND(result); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13128 | dest_data = PyUnicode_DATA(result); |
| 13129 | |
| 13130 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13131 | Py_UCS4 ch = PyUnicode_READ(src_kind, src_data, cur); |
| 13132 | PyUnicode_WRITE(dest_kind, dest_data, i, ch); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13133 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13134 | assert(_PyUnicode_CheckConsistency(result, 1)); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13135 | return result; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13136 | } else { |
| 13137 | PyErr_SetString(PyExc_TypeError, "string indices must be integers"); |
| 13138 | return NULL; |
| 13139 | } |
| 13140 | } |
| 13141 | |
| 13142 | static PyMappingMethods unicode_as_mapping = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13143 | (lenfunc)unicode_length, /* mp_length */ |
| 13144 | (binaryfunc)unicode_subscript, /* mp_subscript */ |
| 13145 | (objobjargproc)0, /* mp_ass_subscript */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13146 | }; |
| 13147 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13148 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13149 | /* Helpers for PyUnicode_Format() */ |
| 13150 | |
| 13151 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13152 | 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] | 13153 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13154 | Py_ssize_t argidx = *p_argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13155 | if (argidx < arglen) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13156 | (*p_argidx)++; |
| 13157 | if (arglen < 0) |
| 13158 | return args; |
| 13159 | else |
| 13160 | return PyTuple_GetItem(args, argidx); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13161 | } |
| 13162 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13163 | "not enough arguments for format string"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13164 | return NULL; |
| 13165 | } |
| 13166 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13167 | /* 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] | 13168 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13169 | static PyObject * |
| 13170 | formatfloat(PyObject *v, int flags, int prec, int type) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13171 | { |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13172 | char *p; |
| 13173 | PyObject *result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13174 | double x; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 13175 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13176 | x = PyFloat_AsDouble(v); |
| 13177 | if (x == -1.0 && PyErr_Occurred()) |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13178 | return NULL; |
| 13179 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13180 | if (prec < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13181 | prec = 6; |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 13182 | |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 13183 | p = PyOS_double_to_string(x, type, prec, |
| 13184 | (flags & F_ALT) ? Py_DTSF_ALT : 0, NULL); |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13185 | if (p == NULL) |
| 13186 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13187 | result = PyUnicode_DecodeASCII(p, strlen(p), NULL); |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 13188 | PyMem_Free(p); |
| 13189 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13190 | } |
| 13191 | |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13192 | static PyObject* |
| 13193 | formatlong(PyObject *val, int flags, int prec, int type) |
| 13194 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13195 | char *buf; |
| 13196 | int len; |
| 13197 | PyObject *str; /* temporary string object. */ |
| 13198 | PyObject *result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13199 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13200 | str = _PyBytes_FormatLong(val, flags, prec, type, &buf, &len); |
| 13201 | if (!str) |
| 13202 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13203 | result = PyUnicode_DecodeASCII(buf, len, NULL); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13204 | Py_DECREF(str); |
| 13205 | return result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13206 | } |
| 13207 | |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13208 | static Py_UCS4 |
| 13209 | formatchar(PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13210 | { |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 13211 | /* presume that the buffer is at least 3 characters long */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 13212 | if (PyUnicode_Check(v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13213 | if (PyUnicode_GET_LENGTH(v) == 1) { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13214 | return PyUnicode_READ_CHAR(v, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13215 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13216 | goto onError; |
| 13217 | } |
| 13218 | else { |
| 13219 | /* Integer input truncated to a character */ |
| 13220 | long x; |
| 13221 | x = PyLong_AsLong(v); |
| 13222 | if (x == -1 && PyErr_Occurred()) |
| 13223 | goto onError; |
| 13224 | |
| 13225 | if (x < 0 || x > 0x10ffff) { |
| 13226 | PyErr_SetString(PyExc_OverflowError, |
| 13227 | "%c arg not in range(0x110000)"); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13228 | return (Py_UCS4) -1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13229 | } |
| 13230 | |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13231 | return (Py_UCS4) x; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13232 | } |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 13233 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13234 | onError: |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 13235 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13236 | "%c requires int or char"); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13237 | return (Py_UCS4) -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13238 | } |
| 13239 | |
Antoine Pitrou | 978b9d2 | 2011-10-07 12:35:48 +0200 | [diff] [blame] | 13240 | static int |
| 13241 | repeat_accumulate(_PyAccu *acc, PyObject *obj, Py_ssize_t count) |
| 13242 | { |
| 13243 | int r; |
| 13244 | assert(count > 0); |
| 13245 | assert(PyUnicode_Check(obj)); |
| 13246 | if (count > 5) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13247 | PyObject *repeated = unicode_repeat(obj, count); |
Antoine Pitrou | 978b9d2 | 2011-10-07 12:35:48 +0200 | [diff] [blame] | 13248 | if (repeated == NULL) |
| 13249 | return -1; |
| 13250 | r = _PyAccu_Accumulate(acc, repeated); |
| 13251 | Py_DECREF(repeated); |
| 13252 | return r; |
| 13253 | } |
| 13254 | else { |
| 13255 | do { |
| 13256 | if (_PyAccu_Accumulate(acc, obj)) |
| 13257 | return -1; |
| 13258 | } while (--count); |
| 13259 | return 0; |
| 13260 | } |
| 13261 | } |
| 13262 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 13263 | PyObject * |
| 13264 | PyUnicode_Format(PyObject *format, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13265 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13266 | void *fmt; |
| 13267 | int fmtkind; |
| 13268 | PyObject *result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13269 | int kind; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13270 | int r; |
| 13271 | Py_ssize_t fmtcnt, fmtpos, arglen, argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13272 | int args_owned = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13273 | PyObject *dict = NULL; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13274 | PyObject *temp = NULL; |
| 13275 | PyObject *second = NULL; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13276 | PyObject *uformat; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13277 | _PyAccu acc; |
| 13278 | static PyObject *plus, *minus, *blank, *zero, *percent; |
| 13279 | |
| 13280 | if (!plus && !(plus = get_latin1_char('+'))) |
| 13281 | return NULL; |
| 13282 | if (!minus && !(minus = get_latin1_char('-'))) |
| 13283 | return NULL; |
| 13284 | if (!blank && !(blank = get_latin1_char(' '))) |
| 13285 | return NULL; |
| 13286 | if (!zero && !(zero = get_latin1_char('0'))) |
| 13287 | return NULL; |
| 13288 | if (!percent && !(percent = get_latin1_char('%'))) |
| 13289 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 13290 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13291 | if (format == NULL || args == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13292 | PyErr_BadInternalCall(); |
| 13293 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13294 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13295 | uformat = PyUnicode_FromObject(format); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13296 | if (uformat == NULL || PyUnicode_READY(uformat) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13297 | return NULL; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13298 | if (_PyAccu_Init(&acc)) |
| 13299 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13300 | fmt = PyUnicode_DATA(uformat); |
| 13301 | fmtkind = PyUnicode_KIND(uformat); |
| 13302 | fmtcnt = PyUnicode_GET_LENGTH(uformat); |
| 13303 | fmtpos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13304 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13305 | if (PyTuple_Check(args)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13306 | arglen = PyTuple_Size(args); |
| 13307 | argidx = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13308 | } |
| 13309 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13310 | arglen = -1; |
| 13311 | argidx = -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13312 | } |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 13313 | if (Py_TYPE(args)->tp_as_mapping && !PyTuple_Check(args) && |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 13314 | !PyUnicode_Check(args)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13315 | dict = args; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13316 | |
| 13317 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13318 | if (PyUnicode_READ(fmtkind, fmt, fmtpos) != '%') { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13319 | PyObject *nonfmt; |
| 13320 | Py_ssize_t nonfmtpos; |
| 13321 | nonfmtpos = fmtpos++; |
| 13322 | while (fmtcnt >= 0 && |
| 13323 | PyUnicode_READ(fmtkind, fmt, fmtpos) != '%') { |
| 13324 | fmtpos++; |
| 13325 | fmtcnt--; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13326 | } |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 13327 | nonfmt = PyUnicode_Substring(uformat, nonfmtpos, fmtpos); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13328 | if (nonfmt == NULL) |
| 13329 | goto onError; |
| 13330 | r = _PyAccu_Accumulate(&acc, nonfmt); |
| 13331 | Py_DECREF(nonfmt); |
| 13332 | if (r) |
| 13333 | goto onError; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13334 | } |
| 13335 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13336 | /* Got a format specifier */ |
| 13337 | int flags = 0; |
| 13338 | Py_ssize_t width = -1; |
| 13339 | int prec = -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13340 | Py_UCS4 c = '\0'; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13341 | Py_UCS4 fill, sign; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13342 | int isnumok; |
| 13343 | PyObject *v = NULL; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13344 | void *pbuf = NULL; |
| 13345 | Py_ssize_t pindex, len; |
| 13346 | PyObject *signobj = NULL, *fillobj = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13347 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13348 | fmtpos++; |
| 13349 | if (PyUnicode_READ(fmtkind, fmt, fmtpos) == '(') { |
| 13350 | Py_ssize_t keystart; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13351 | Py_ssize_t keylen; |
| 13352 | PyObject *key; |
| 13353 | int pcount = 1; |
Christian Heimes | a612dc0 | 2008-02-24 13:08:18 +0000 | [diff] [blame] | 13354 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13355 | if (dict == NULL) { |
| 13356 | PyErr_SetString(PyExc_TypeError, |
| 13357 | "format requires a mapping"); |
| 13358 | goto onError; |
| 13359 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13360 | ++fmtpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13361 | --fmtcnt; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13362 | keystart = fmtpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13363 | /* Skip over balanced parentheses */ |
| 13364 | while (pcount > 0 && --fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13365 | if (PyUnicode_READ(fmtkind, fmt, fmtpos) == ')') |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13366 | --pcount; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13367 | else if (PyUnicode_READ(fmtkind, fmt, fmtpos) == '(') |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13368 | ++pcount; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13369 | fmtpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13370 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13371 | keylen = fmtpos - keystart - 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13372 | if (fmtcnt < 0 || pcount > 0) { |
| 13373 | PyErr_SetString(PyExc_ValueError, |
| 13374 | "incomplete format key"); |
| 13375 | goto onError; |
| 13376 | } |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 13377 | key = PyUnicode_Substring(uformat, |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 13378 | keystart, keystart + keylen); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13379 | if (key == NULL) |
| 13380 | goto onError; |
| 13381 | if (args_owned) { |
| 13382 | Py_DECREF(args); |
| 13383 | args_owned = 0; |
| 13384 | } |
| 13385 | args = PyObject_GetItem(dict, key); |
| 13386 | Py_DECREF(key); |
| 13387 | if (args == NULL) { |
| 13388 | goto onError; |
| 13389 | } |
| 13390 | args_owned = 1; |
| 13391 | arglen = -1; |
| 13392 | argidx = -2; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13393 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13394 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13395 | switch (c = PyUnicode_READ(fmtkind, fmt, fmtpos++)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13396 | case '-': flags |= F_LJUST; continue; |
| 13397 | case '+': flags |= F_SIGN; continue; |
| 13398 | case ' ': flags |= F_BLANK; continue; |
| 13399 | case '#': flags |= F_ALT; continue; |
| 13400 | case '0': flags |= F_ZERO; continue; |
| 13401 | } |
| 13402 | break; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13403 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13404 | if (c == '*') { |
| 13405 | v = getnextarg(args, arglen, &argidx); |
| 13406 | if (v == NULL) |
| 13407 | goto onError; |
| 13408 | if (!PyLong_Check(v)) { |
| 13409 | PyErr_SetString(PyExc_TypeError, |
| 13410 | "* wants int"); |
| 13411 | goto onError; |
| 13412 | } |
| 13413 | width = PyLong_AsLong(v); |
| 13414 | if (width == -1 && PyErr_Occurred()) |
| 13415 | goto onError; |
| 13416 | if (width < 0) { |
| 13417 | flags |= F_LJUST; |
| 13418 | width = -width; |
| 13419 | } |
| 13420 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13421 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13422 | } |
| 13423 | else if (c >= '0' && c <= '9') { |
| 13424 | width = c - '0'; |
| 13425 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13426 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13427 | if (c < '0' || c > '9') |
| 13428 | break; |
| 13429 | if ((width*10) / 10 != width) { |
| 13430 | PyErr_SetString(PyExc_ValueError, |
| 13431 | "width too big"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13432 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13433 | } |
| 13434 | width = width*10 + (c - '0'); |
| 13435 | } |
| 13436 | } |
| 13437 | if (c == '.') { |
| 13438 | prec = 0; |
| 13439 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13440 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13441 | if (c == '*') { |
| 13442 | v = getnextarg(args, arglen, &argidx); |
| 13443 | if (v == NULL) |
| 13444 | goto onError; |
| 13445 | if (!PyLong_Check(v)) { |
| 13446 | PyErr_SetString(PyExc_TypeError, |
| 13447 | "* wants int"); |
| 13448 | goto onError; |
| 13449 | } |
| 13450 | prec = PyLong_AsLong(v); |
| 13451 | if (prec == -1 && PyErr_Occurred()) |
| 13452 | goto onError; |
| 13453 | if (prec < 0) |
| 13454 | prec = 0; |
| 13455 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13456 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13457 | } |
| 13458 | else if (c >= '0' && c <= '9') { |
| 13459 | prec = c - '0'; |
| 13460 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13461 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13462 | if (c < '0' || c > '9') |
| 13463 | break; |
| 13464 | if ((prec*10) / 10 != prec) { |
| 13465 | PyErr_SetString(PyExc_ValueError, |
| 13466 | "prec too big"); |
| 13467 | goto onError; |
| 13468 | } |
| 13469 | prec = prec*10 + (c - '0'); |
| 13470 | } |
| 13471 | } |
| 13472 | } /* prec */ |
| 13473 | if (fmtcnt >= 0) { |
| 13474 | if (c == 'h' || c == 'l' || c == 'L') { |
| 13475 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13476 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13477 | } |
| 13478 | } |
| 13479 | if (fmtcnt < 0) { |
| 13480 | PyErr_SetString(PyExc_ValueError, |
| 13481 | "incomplete format"); |
| 13482 | goto onError; |
| 13483 | } |
| 13484 | if (c != '%') { |
| 13485 | v = getnextarg(args, arglen, &argidx); |
| 13486 | if (v == NULL) |
| 13487 | goto onError; |
| 13488 | } |
| 13489 | sign = 0; |
| 13490 | fill = ' '; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13491 | fillobj = blank; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13492 | switch (c) { |
| 13493 | |
| 13494 | case '%': |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13495 | _PyAccu_Accumulate(&acc, percent); |
| 13496 | continue; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13497 | |
| 13498 | case 's': |
| 13499 | case 'r': |
| 13500 | case 'a': |
Victor Stinner | 808fc0a | 2010-03-22 12:50:40 +0000 | [diff] [blame] | 13501 | if (PyUnicode_CheckExact(v) && c == 's') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13502 | temp = v; |
| 13503 | Py_INCREF(temp); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13504 | } |
| 13505 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13506 | if (c == 's') |
| 13507 | temp = PyObject_Str(v); |
| 13508 | else if (c == 'r') |
| 13509 | temp = PyObject_Repr(v); |
| 13510 | else |
| 13511 | temp = PyObject_ASCII(v); |
| 13512 | if (temp == NULL) |
| 13513 | goto onError; |
| 13514 | if (PyUnicode_Check(temp)) |
| 13515 | /* nothing to do */; |
| 13516 | else { |
| 13517 | Py_DECREF(temp); |
| 13518 | PyErr_SetString(PyExc_TypeError, |
| 13519 | "%s argument has non-string str()"); |
| 13520 | goto onError; |
| 13521 | } |
| 13522 | } |
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 | if (prec >= 0 && len > prec) |
| 13531 | len = prec; |
| 13532 | break; |
| 13533 | |
| 13534 | case 'i': |
| 13535 | case 'd': |
| 13536 | case 'u': |
| 13537 | case 'o': |
| 13538 | case 'x': |
| 13539 | case 'X': |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13540 | isnumok = 0; |
| 13541 | if (PyNumber_Check(v)) { |
| 13542 | PyObject *iobj=NULL; |
| 13543 | |
| 13544 | if (PyLong_Check(v)) { |
| 13545 | iobj = v; |
| 13546 | Py_INCREF(iobj); |
| 13547 | } |
| 13548 | else { |
| 13549 | iobj = PyNumber_Long(v); |
| 13550 | } |
| 13551 | if (iobj!=NULL) { |
| 13552 | if (PyLong_Check(iobj)) { |
| 13553 | isnumok = 1; |
Senthil Kumaran | 9ebe08d | 2011-07-03 21:03:16 -0700 | [diff] [blame] | 13554 | temp = formatlong(iobj, flags, prec, (c == 'i'? 'd': c)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13555 | Py_DECREF(iobj); |
| 13556 | if (!temp) |
| 13557 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13558 | if (PyUnicode_READY(temp) == -1) { |
| 13559 | Py_CLEAR(temp); |
| 13560 | goto onError; |
| 13561 | } |
| 13562 | pbuf = PyUnicode_DATA(temp); |
| 13563 | kind = PyUnicode_KIND(temp); |
| 13564 | len = PyUnicode_GET_LENGTH(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13565 | sign = 1; |
| 13566 | } |
| 13567 | else { |
| 13568 | Py_DECREF(iobj); |
| 13569 | } |
| 13570 | } |
| 13571 | } |
| 13572 | if (!isnumok) { |
| 13573 | PyErr_Format(PyExc_TypeError, |
| 13574 | "%%%c format: a number is required, " |
| 13575 | "not %.200s", (char)c, Py_TYPE(v)->tp_name); |
| 13576 | goto onError; |
| 13577 | } |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13578 | if (flags & F_ZERO) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13579 | fill = '0'; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13580 | fillobj = zero; |
| 13581 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13582 | break; |
| 13583 | |
| 13584 | case 'e': |
| 13585 | case 'E': |
| 13586 | case 'f': |
| 13587 | case 'F': |
| 13588 | case 'g': |
| 13589 | case 'G': |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13590 | temp = formatfloat(v, flags, prec, c); |
| 13591 | if (!temp) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13592 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13593 | if (PyUnicode_READY(temp) == -1) { |
| 13594 | Py_CLEAR(temp); |
| 13595 | goto onError; |
| 13596 | } |
| 13597 | pbuf = PyUnicode_DATA(temp); |
| 13598 | kind = PyUnicode_KIND(temp); |
| 13599 | len = PyUnicode_GET_LENGTH(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13600 | sign = 1; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13601 | if (flags & F_ZERO) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13602 | fill = '0'; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13603 | fillobj = zero; |
| 13604 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13605 | break; |
| 13606 | |
| 13607 | case 'c': |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13608 | { |
| 13609 | Py_UCS4 ch = formatchar(v); |
| 13610 | if (ch == (Py_UCS4) -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13611 | goto onError; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13612 | temp = _PyUnicode_FromUCS4(&ch, 1); |
| 13613 | if (temp == NULL) |
| 13614 | goto onError; |
| 13615 | pbuf = PyUnicode_DATA(temp); |
| 13616 | kind = PyUnicode_KIND(temp); |
| 13617 | len = PyUnicode_GET_LENGTH(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13618 | break; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13619 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13620 | |
| 13621 | default: |
| 13622 | PyErr_Format(PyExc_ValueError, |
| 13623 | "unsupported format character '%c' (0x%x) " |
| 13624 | "at index %zd", |
| 13625 | (31<=c && c<=126) ? (char)c : '?', |
| 13626 | (int)c, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13627 | fmtpos - 1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13628 | goto onError; |
| 13629 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13630 | /* pbuf is initialized here. */ |
| 13631 | pindex = 0; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13632 | if (sign) { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13633 | if (PyUnicode_READ(kind, pbuf, pindex) == '-') { |
| 13634 | signobj = minus; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13635 | len--; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13636 | pindex++; |
| 13637 | } |
| 13638 | else if (PyUnicode_READ(kind, pbuf, pindex) == '+') { |
| 13639 | signobj = plus; |
| 13640 | len--; |
| 13641 | pindex++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13642 | } |
| 13643 | else if (flags & F_SIGN) |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13644 | signobj = plus; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13645 | else if (flags & F_BLANK) |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13646 | signobj = blank; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13647 | else |
| 13648 | sign = 0; |
| 13649 | } |
| 13650 | if (width < len) |
| 13651 | width = len; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13652 | if (sign) { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13653 | if (fill != ' ') { |
| 13654 | assert(signobj != NULL); |
| 13655 | if (_PyAccu_Accumulate(&acc, signobj)) |
| 13656 | goto onError; |
| 13657 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13658 | if (width > len) |
| 13659 | width--; |
| 13660 | } |
| 13661 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13662 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13663 | assert(PyUnicode_READ(kind, pbuf, pindex + 1) == c); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13664 | if (fill != ' ') { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13665 | second = get_latin1_char( |
| 13666 | PyUnicode_READ(kind, pbuf, pindex + 1)); |
| 13667 | pindex += 2; |
| 13668 | if (second == NULL || |
| 13669 | _PyAccu_Accumulate(&acc, zero) || |
| 13670 | _PyAccu_Accumulate(&acc, second)) |
| 13671 | goto onError; |
| 13672 | Py_CLEAR(second); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13673 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13674 | width -= 2; |
| 13675 | if (width < 0) |
| 13676 | width = 0; |
| 13677 | len -= 2; |
| 13678 | } |
| 13679 | if (width > len && !(flags & F_LJUST)) { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13680 | assert(fillobj != NULL); |
Antoine Pitrou | 978b9d2 | 2011-10-07 12:35:48 +0200 | [diff] [blame] | 13681 | if (repeat_accumulate(&acc, fillobj, width - len)) |
| 13682 | goto onError; |
| 13683 | width = len; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13684 | } |
| 13685 | if (fill == ' ') { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13686 | if (sign) { |
| 13687 | assert(signobj != NULL); |
| 13688 | if (_PyAccu_Accumulate(&acc, signobj)) |
| 13689 | goto onError; |
| 13690 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13691 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13692 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
| 13693 | assert(PyUnicode_READ(kind, pbuf, pindex+1) == c); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13694 | second = get_latin1_char( |
| 13695 | PyUnicode_READ(kind, pbuf, pindex + 1)); |
| 13696 | pindex += 2; |
| 13697 | if (second == NULL || |
| 13698 | _PyAccu_Accumulate(&acc, zero) || |
| 13699 | _PyAccu_Accumulate(&acc, second)) |
| 13700 | goto onError; |
| 13701 | Py_CLEAR(second); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13702 | } |
| 13703 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13704 | /* Copy all characters, preserving len */ |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13705 | if (temp != NULL) { |
| 13706 | assert(pbuf == PyUnicode_DATA(temp)); |
| 13707 | v = PyUnicode_Substring(temp, pindex, pindex + len); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13708 | } |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13709 | else { |
| 13710 | const char *p = (const char *) pbuf; |
| 13711 | assert(pbuf != NULL); |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 13712 | p += kind * pindex; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13713 | v = PyUnicode_FromKindAndData(kind, p, len); |
| 13714 | } |
| 13715 | if (v == NULL) |
| 13716 | goto onError; |
| 13717 | r = _PyAccu_Accumulate(&acc, v); |
| 13718 | Py_DECREF(v); |
| 13719 | if (r) |
| 13720 | goto onError; |
Antoine Pitrou | 978b9d2 | 2011-10-07 12:35:48 +0200 | [diff] [blame] | 13721 | if (width > len && repeat_accumulate(&acc, blank, width - len)) |
| 13722 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13723 | if (dict && (argidx < arglen) && c != '%') { |
| 13724 | PyErr_SetString(PyExc_TypeError, |
| 13725 | "not all arguments converted during string formatting"); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13726 | goto onError; |
| 13727 | } |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13728 | Py_CLEAR(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13729 | } /* '%' */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13730 | } /* until end */ |
| 13731 | if (argidx < arglen && !dict) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13732 | PyErr_SetString(PyExc_TypeError, |
| 13733 | "not all arguments converted during string formatting"); |
| 13734 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13735 | } |
| 13736 | |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13737 | result = _PyAccu_Finish(&acc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13738 | if (args_owned) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13739 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13740 | } |
| 13741 | Py_DECREF(uformat); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13742 | Py_XDECREF(temp); |
| 13743 | Py_XDECREF(second); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 13744 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13745 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13746 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13747 | Py_DECREF(uformat); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13748 | Py_XDECREF(temp); |
| 13749 | Py_XDECREF(second); |
| 13750 | _PyAccu_Destroy(&acc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13751 | if (args_owned) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13752 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13753 | } |
| 13754 | return NULL; |
| 13755 | } |
| 13756 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 13757 | static PyObject * |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 13758 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 13759 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13760 | static PyObject * |
| 13761 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 13762 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13763 | PyObject *x = NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13764 | static char *kwlist[] = {"object", "encoding", "errors", 0}; |
| 13765 | char *encoding = NULL; |
| 13766 | char *errors = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13767 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13768 | if (type != &PyUnicode_Type) |
| 13769 | return unicode_subtype_new(type, args, kwds); |
| 13770 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:str", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13771 | kwlist, &x, &encoding, &errors)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13772 | return NULL; |
| 13773 | if (x == NULL) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 13774 | return PyUnicode_New(0, 0); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13775 | if (encoding == NULL && errors == NULL) |
| 13776 | return PyObject_Str(x); |
| 13777 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13778 | return PyUnicode_FromEncodedObject(x, encoding, errors); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13779 | } |
| 13780 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 13781 | static PyObject * |
| 13782 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 13783 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13784 | PyObject *unicode, *self; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13785 | Py_ssize_t length, char_size; |
| 13786 | int share_wstr, share_utf8; |
| 13787 | unsigned int kind; |
| 13788 | void *data; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 13789 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13790 | assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13791 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13792 | unicode = unicode_new(&PyUnicode_Type, args, kwds); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13793 | if (unicode == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13794 | return NULL; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 13795 | assert(_PyUnicode_CHECK(unicode)); |
Victor Stinner | e06e145 | 2011-10-04 20:52:31 +0200 | [diff] [blame] | 13796 | if (PyUnicode_READY(unicode)) |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13797 | return NULL; |
| 13798 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13799 | self = type->tp_alloc(type, 0); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13800 | if (self == NULL) { |
| 13801 | Py_DECREF(unicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13802 | return NULL; |
| 13803 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13804 | kind = PyUnicode_KIND(unicode); |
| 13805 | length = PyUnicode_GET_LENGTH(unicode); |
| 13806 | |
| 13807 | _PyUnicode_LENGTH(self) = length; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 13808 | #ifdef Py_DEBUG |
| 13809 | _PyUnicode_HASH(self) = -1; |
| 13810 | #else |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13811 | _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 13812 | #endif |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13813 | _PyUnicode_STATE(self).interned = 0; |
| 13814 | _PyUnicode_STATE(self).kind = kind; |
| 13815 | _PyUnicode_STATE(self).compact = 0; |
Victor Stinner | 3cf4637 | 2011-10-03 14:42:15 +0200 | [diff] [blame] | 13816 | _PyUnicode_STATE(self).ascii = _PyUnicode_STATE(unicode).ascii; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13817 | _PyUnicode_STATE(self).ready = 1; |
| 13818 | _PyUnicode_WSTR(self) = NULL; |
| 13819 | _PyUnicode_UTF8_LENGTH(self) = 0; |
| 13820 | _PyUnicode_UTF8(self) = NULL; |
| 13821 | _PyUnicode_WSTR_LENGTH(self) = 0; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 13822 | _PyUnicode_DATA_ANY(self) = NULL; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13823 | |
| 13824 | share_utf8 = 0; |
| 13825 | share_wstr = 0; |
| 13826 | if (kind == PyUnicode_1BYTE_KIND) { |
| 13827 | char_size = 1; |
| 13828 | if (PyUnicode_MAX_CHAR_VALUE(unicode) < 128) |
| 13829 | share_utf8 = 1; |
| 13830 | } |
| 13831 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 13832 | char_size = 2; |
| 13833 | if (sizeof(wchar_t) == 2) |
| 13834 | share_wstr = 1; |
| 13835 | } |
| 13836 | else { |
| 13837 | assert(kind == PyUnicode_4BYTE_KIND); |
| 13838 | char_size = 4; |
| 13839 | if (sizeof(wchar_t) == 4) |
| 13840 | share_wstr = 1; |
| 13841 | } |
| 13842 | |
| 13843 | /* Ensure we won't overflow the length. */ |
| 13844 | if (length > (PY_SSIZE_T_MAX / char_size - 1)) { |
| 13845 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13846 | goto onError; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13847 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13848 | data = PyObject_MALLOC((length + 1) * char_size); |
| 13849 | if (data == NULL) { |
| 13850 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13851 | goto onError; |
| 13852 | } |
| 13853 | |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 13854 | _PyUnicode_DATA_ANY(self) = data; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13855 | if (share_utf8) { |
| 13856 | _PyUnicode_UTF8_LENGTH(self) = length; |
| 13857 | _PyUnicode_UTF8(self) = data; |
| 13858 | } |
| 13859 | if (share_wstr) { |
| 13860 | _PyUnicode_WSTR_LENGTH(self) = length; |
| 13861 | _PyUnicode_WSTR(self) = (wchar_t *)data; |
| 13862 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13863 | |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13864 | Py_MEMCPY(data, PyUnicode_DATA(unicode), |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 13865 | kind * (length + 1)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13866 | assert(_PyUnicode_CheckConsistency(self, 1)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 13867 | #ifdef Py_DEBUG |
| 13868 | _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode); |
| 13869 | #endif |
Victor Stinner | dd18d3a | 2011-10-22 11:08:10 +0200 | [diff] [blame] | 13870 | Py_DECREF(unicode); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 13871 | return self; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13872 | |
| 13873 | onError: |
| 13874 | Py_DECREF(unicode); |
| 13875 | Py_DECREF(self); |
| 13876 | return NULL; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 13877 | } |
| 13878 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 13879 | PyDoc_STRVAR(unicode_doc, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13880 | "str(string[, encoding[, errors]]) -> str\n\ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13881 | \n\ |
Collin Winter | d474ce8 | 2007-08-07 19:42:11 +0000 | [diff] [blame] | 13882 | Create a new string object from the given encoded string.\n\ |
Skip Montanaro | 35b37a5 | 2002-07-26 16:22:46 +0000 | [diff] [blame] | 13883 | encoding defaults to the current default string encoding.\n\ |
| 13884 | errors can be 'strict', 'replace' or 'ignore' and defaults to 'strict'."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13885 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13886 | static PyObject *unicode_iter(PyObject *seq); |
| 13887 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13888 | PyTypeObject PyUnicode_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 13889 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13890 | "str", /* tp_name */ |
| 13891 | sizeof(PyUnicodeObject), /* tp_size */ |
| 13892 | 0, /* tp_itemsize */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13893 | /* Slots */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13894 | (destructor)unicode_dealloc, /* tp_dealloc */ |
| 13895 | 0, /* tp_print */ |
| 13896 | 0, /* tp_getattr */ |
| 13897 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 13898 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13899 | unicode_repr, /* tp_repr */ |
| 13900 | &unicode_as_number, /* tp_as_number */ |
| 13901 | &unicode_as_sequence, /* tp_as_sequence */ |
| 13902 | &unicode_as_mapping, /* tp_as_mapping */ |
| 13903 | (hashfunc) unicode_hash, /* tp_hash*/ |
| 13904 | 0, /* tp_call*/ |
| 13905 | (reprfunc) unicode_str, /* tp_str */ |
| 13906 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 13907 | 0, /* tp_setattro */ |
| 13908 | 0, /* tp_as_buffer */ |
| 13909 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13910 | Py_TPFLAGS_UNICODE_SUBCLASS, /* tp_flags */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13911 | unicode_doc, /* tp_doc */ |
| 13912 | 0, /* tp_traverse */ |
| 13913 | 0, /* tp_clear */ |
| 13914 | PyUnicode_RichCompare, /* tp_richcompare */ |
| 13915 | 0, /* tp_weaklistoffset */ |
| 13916 | unicode_iter, /* tp_iter */ |
| 13917 | 0, /* tp_iternext */ |
| 13918 | unicode_methods, /* tp_methods */ |
| 13919 | 0, /* tp_members */ |
| 13920 | 0, /* tp_getset */ |
| 13921 | &PyBaseObject_Type, /* tp_base */ |
| 13922 | 0, /* tp_dict */ |
| 13923 | 0, /* tp_descr_get */ |
| 13924 | 0, /* tp_descr_set */ |
| 13925 | 0, /* tp_dictoffset */ |
| 13926 | 0, /* tp_init */ |
| 13927 | 0, /* tp_alloc */ |
| 13928 | unicode_new, /* tp_new */ |
| 13929 | PyObject_Del, /* tp_free */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13930 | }; |
| 13931 | |
| 13932 | /* Initialize the Unicode implementation */ |
| 13933 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 13934 | int _PyUnicode_Init(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13935 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 13936 | int i; |
| 13937 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13938 | /* XXX - move this array to unicodectype.c ? */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13939 | Py_UCS2 linebreak[] = { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13940 | 0x000A, /* LINE FEED */ |
| 13941 | 0x000D, /* CARRIAGE RETURN */ |
| 13942 | 0x001C, /* FILE SEPARATOR */ |
| 13943 | 0x001D, /* GROUP SEPARATOR */ |
| 13944 | 0x001E, /* RECORD SEPARATOR */ |
| 13945 | 0x0085, /* NEXT LINE */ |
| 13946 | 0x2028, /* LINE SEPARATOR */ |
| 13947 | 0x2029, /* PARAGRAPH SEPARATOR */ |
| 13948 | }; |
| 13949 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 13950 | /* Init the implementation */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 13951 | unicode_empty = PyUnicode_New(0, 0); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13952 | assert(_PyUnicode_CheckConsistency(unicode_empty, 1)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13953 | if (!unicode_empty) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13954 | Py_FatalError("Can't create empty string"); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13955 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 13956 | for (i = 0; i < 256; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13957 | unicode_latin1[i] = NULL; |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 13958 | if (PyType_Ready(&PyUnicode_Type) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13959 | Py_FatalError("Can't initialize 'unicode'"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13960 | |
| 13961 | /* initialize the linebreak bloom filter */ |
| 13962 | bloom_linebreak = make_bloom_mask( |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13963 | PyUnicode_2BYTE_KIND, linebreak, |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 13964 | Py_ARRAY_LENGTH(linebreak)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13965 | |
| 13966 | PyType_Ready(&EncodingMapType); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 13967 | |
| 13968 | #ifdef HAVE_MBCS |
| 13969 | winver.dwOSVersionInfoSize = sizeof(winver); |
| 13970 | if (!GetVersionEx((OSVERSIONINFO*)&winver)) { |
| 13971 | PyErr_SetFromWindowsErr(0); |
| 13972 | return -1; |
| 13973 | } |
| 13974 | #endif |
| 13975 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13976 | } |
| 13977 | |
| 13978 | /* Finalize the Unicode implementation */ |
| 13979 | |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 13980 | int |
| 13981 | PyUnicode_ClearFreeList(void) |
| 13982 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13983 | return 0; |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 13984 | } |
| 13985 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13986 | void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 13987 | _PyUnicode_Fini(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13988 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 13989 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13990 | |
Guido van Rossum | 4ae8ef8 | 2000-10-03 18:09:04 +0000 | [diff] [blame] | 13991 | Py_XDECREF(unicode_empty); |
| 13992 | unicode_empty = NULL; |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 13993 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 13994 | for (i = 0; i < 256; i++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13995 | if (unicode_latin1[i]) { |
| 13996 | Py_DECREF(unicode_latin1[i]); |
| 13997 | unicode_latin1[i] = NULL; |
| 13998 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 13999 | } |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 14000 | _PyUnicode_ClearStaticStrings(); |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 14001 | (void)PyUnicode_ClearFreeList(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14002 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 14003 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14004 | void |
| 14005 | PyUnicode_InternInPlace(PyObject **p) |
| 14006 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14007 | register PyObject *s = *p; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14008 | PyObject *t; |
Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 14009 | #ifdef Py_DEBUG |
| 14010 | assert(s != NULL); |
| 14011 | assert(_PyUnicode_CHECK(s)); |
| 14012 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14013 | if (s == NULL || !PyUnicode_Check(s)) |
Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 14014 | return; |
| 14015 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14016 | /* If it's a subclass, we don't really know what putting |
| 14017 | it in the interned dict might do. */ |
| 14018 | if (!PyUnicode_CheckExact(s)) |
| 14019 | return; |
| 14020 | if (PyUnicode_CHECK_INTERNED(s)) |
| 14021 | return; |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 14022 | if (_PyUnicode_READY_REPLACE(p)) { |
Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 14023 | assert(0 && "_PyUnicode_READY_REPLACE fail in PyUnicode_InternInPlace"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14024 | return; |
| 14025 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14026 | s = *p; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14027 | if (interned == NULL) { |
| 14028 | interned = PyDict_New(); |
| 14029 | if (interned == NULL) { |
| 14030 | PyErr_Clear(); /* Don't leave an exception */ |
| 14031 | return; |
| 14032 | } |
| 14033 | } |
| 14034 | /* It might be that the GetItem call fails even |
| 14035 | though the key is present in the dictionary, |
| 14036 | namely when this happens during a stack overflow. */ |
| 14037 | Py_ALLOW_RECURSION |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 14038 | t = PyDict_GetItem(interned, s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14039 | Py_END_ALLOW_RECURSION |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 14040 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14041 | if (t) { |
| 14042 | Py_INCREF(t); |
| 14043 | Py_DECREF(*p); |
| 14044 | *p = t; |
| 14045 | return; |
| 14046 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14047 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14048 | PyThreadState_GET()->recursion_critical = 1; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 14049 | if (PyDict_SetItem(interned, s, s) < 0) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14050 | PyErr_Clear(); |
| 14051 | PyThreadState_GET()->recursion_critical = 0; |
| 14052 | return; |
| 14053 | } |
| 14054 | PyThreadState_GET()->recursion_critical = 0; |
| 14055 | /* The two references in interned are not counted by refcnt. |
| 14056 | The deallocator will take care of this */ |
| 14057 | Py_REFCNT(s) -= 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14058 | _PyUnicode_STATE(s).interned = SSTATE_INTERNED_MORTAL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14059 | } |
| 14060 | |
| 14061 | void |
| 14062 | PyUnicode_InternImmortal(PyObject **p) |
| 14063 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14064 | PyUnicode_InternInPlace(p); |
| 14065 | if (PyUnicode_CHECK_INTERNED(*p) != SSTATE_INTERNED_IMMORTAL) { |
Victor Stinner | af9e4b8 | 2011-10-23 20:07:00 +0200 | [diff] [blame] | 14066 | _PyUnicode_STATE(*p).interned = SSTATE_INTERNED_IMMORTAL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14067 | Py_INCREF(*p); |
| 14068 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14069 | } |
| 14070 | |
| 14071 | PyObject * |
| 14072 | PyUnicode_InternFromString(const char *cp) |
| 14073 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14074 | PyObject *s = PyUnicode_FromString(cp); |
| 14075 | if (s == NULL) |
| 14076 | return NULL; |
| 14077 | PyUnicode_InternInPlace(&s); |
| 14078 | return s; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14079 | } |
| 14080 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 14081 | void |
| 14082 | _Py_ReleaseInternedUnicodeStrings(void) |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14083 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14084 | PyObject *keys; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14085 | PyObject *s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14086 | Py_ssize_t i, n; |
| 14087 | Py_ssize_t immortal_size = 0, mortal_size = 0; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14088 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14089 | if (interned == NULL || !PyDict_Check(interned)) |
| 14090 | return; |
| 14091 | keys = PyDict_Keys(interned); |
| 14092 | if (keys == NULL || !PyList_Check(keys)) { |
| 14093 | PyErr_Clear(); |
| 14094 | return; |
| 14095 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14096 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14097 | /* Since _Py_ReleaseInternedUnicodeStrings() is intended to help a leak |
| 14098 | detector, interned unicode strings are not forcibly deallocated; |
| 14099 | rather, we give them their stolen references back, and then clear |
| 14100 | and DECREF the interned dict. */ |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14101 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14102 | n = PyList_GET_SIZE(keys); |
| 14103 | fprintf(stderr, "releasing %" PY_FORMAT_SIZE_T "d interned strings\n", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14104 | n); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14105 | for (i = 0; i < n; i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14106 | s = PyList_GET_ITEM(keys, i); |
Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 14107 | if (PyUnicode_READY(s) == -1) { |
| 14108 | assert(0 && "could not ready string"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14109 | fprintf(stderr, "could not ready string\n"); |
Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 14110 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14111 | switch (PyUnicode_CHECK_INTERNED(s)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14112 | case SSTATE_NOT_INTERNED: |
| 14113 | /* XXX Shouldn't happen */ |
| 14114 | break; |
| 14115 | case SSTATE_INTERNED_IMMORTAL: |
| 14116 | Py_REFCNT(s) += 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14117 | immortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14118 | break; |
| 14119 | case SSTATE_INTERNED_MORTAL: |
| 14120 | Py_REFCNT(s) += 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14121 | mortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14122 | break; |
| 14123 | default: |
| 14124 | Py_FatalError("Inconsistent interned string state."); |
| 14125 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14126 | _PyUnicode_STATE(s).interned = SSTATE_NOT_INTERNED; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14127 | } |
| 14128 | fprintf(stderr, "total size of all interned strings: " |
| 14129 | "%" PY_FORMAT_SIZE_T "d/%" PY_FORMAT_SIZE_T "d " |
| 14130 | "mortal/immortal\n", mortal_size, immortal_size); |
| 14131 | Py_DECREF(keys); |
| 14132 | PyDict_Clear(interned); |
| 14133 | Py_DECREF(interned); |
| 14134 | interned = NULL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14135 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14136 | |
| 14137 | |
| 14138 | /********************* Unicode Iterator **************************/ |
| 14139 | |
| 14140 | typedef struct { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14141 | PyObject_HEAD |
| 14142 | Py_ssize_t it_index; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14143 | PyObject *it_seq; /* Set to NULL when iterator is exhausted */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14144 | } unicodeiterobject; |
| 14145 | |
| 14146 | static void |
| 14147 | unicodeiter_dealloc(unicodeiterobject *it) |
| 14148 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14149 | _PyObject_GC_UNTRACK(it); |
| 14150 | Py_XDECREF(it->it_seq); |
| 14151 | PyObject_GC_Del(it); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14152 | } |
| 14153 | |
| 14154 | static int |
| 14155 | unicodeiter_traverse(unicodeiterobject *it, visitproc visit, void *arg) |
| 14156 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14157 | Py_VISIT(it->it_seq); |
| 14158 | return 0; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14159 | } |
| 14160 | |
| 14161 | static PyObject * |
| 14162 | unicodeiter_next(unicodeiterobject *it) |
| 14163 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14164 | PyObject *seq, *item; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14165 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14166 | assert(it != NULL); |
| 14167 | seq = it->it_seq; |
| 14168 | if (seq == NULL) |
| 14169 | return NULL; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 14170 | assert(_PyUnicode_CHECK(seq)); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14171 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14172 | if (it->it_index < PyUnicode_GET_LENGTH(seq)) { |
| 14173 | int kind = PyUnicode_KIND(seq); |
| 14174 | void *data = PyUnicode_DATA(seq); |
| 14175 | Py_UCS4 chr = PyUnicode_READ(kind, data, it->it_index); |
| 14176 | item = PyUnicode_FromOrdinal(chr); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14177 | if (item != NULL) |
| 14178 | ++it->it_index; |
| 14179 | return item; |
| 14180 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14181 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14182 | Py_DECREF(seq); |
| 14183 | it->it_seq = NULL; |
| 14184 | return NULL; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14185 | } |
| 14186 | |
| 14187 | static PyObject * |
| 14188 | unicodeiter_len(unicodeiterobject *it) |
| 14189 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14190 | Py_ssize_t len = 0; |
| 14191 | if (it->it_seq) |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 14192 | len = PyUnicode_GET_LENGTH(it->it_seq) - it->it_index; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14193 | return PyLong_FromSsize_t(len); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14194 | } |
| 14195 | |
| 14196 | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
| 14197 | |
| 14198 | static PyMethodDef unicodeiter_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14199 | {"__length_hint__", (PyCFunction)unicodeiter_len, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14200 | length_hint_doc}, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14201 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14202 | }; |
| 14203 | |
| 14204 | PyTypeObject PyUnicodeIter_Type = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14205 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 14206 | "str_iterator", /* tp_name */ |
| 14207 | sizeof(unicodeiterobject), /* tp_basicsize */ |
| 14208 | 0, /* tp_itemsize */ |
| 14209 | /* methods */ |
| 14210 | (destructor)unicodeiter_dealloc, /* tp_dealloc */ |
| 14211 | 0, /* tp_print */ |
| 14212 | 0, /* tp_getattr */ |
| 14213 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 14214 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14215 | 0, /* tp_repr */ |
| 14216 | 0, /* tp_as_number */ |
| 14217 | 0, /* tp_as_sequence */ |
| 14218 | 0, /* tp_as_mapping */ |
| 14219 | 0, /* tp_hash */ |
| 14220 | 0, /* tp_call */ |
| 14221 | 0, /* tp_str */ |
| 14222 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 14223 | 0, /* tp_setattro */ |
| 14224 | 0, /* tp_as_buffer */ |
| 14225 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 14226 | 0, /* tp_doc */ |
| 14227 | (traverseproc)unicodeiter_traverse, /* tp_traverse */ |
| 14228 | 0, /* tp_clear */ |
| 14229 | 0, /* tp_richcompare */ |
| 14230 | 0, /* tp_weaklistoffset */ |
| 14231 | PyObject_SelfIter, /* tp_iter */ |
| 14232 | (iternextfunc)unicodeiter_next, /* tp_iternext */ |
| 14233 | unicodeiter_methods, /* tp_methods */ |
| 14234 | 0, |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14235 | }; |
| 14236 | |
| 14237 | static PyObject * |
| 14238 | unicode_iter(PyObject *seq) |
| 14239 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14240 | unicodeiterobject *it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14241 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14242 | if (!PyUnicode_Check(seq)) { |
| 14243 | PyErr_BadInternalCall(); |
| 14244 | return NULL; |
| 14245 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14246 | if (PyUnicode_READY(seq) == -1) |
| 14247 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14248 | it = PyObject_GC_New(unicodeiterobject, &PyUnicodeIter_Type); |
| 14249 | if (it == NULL) |
| 14250 | return NULL; |
| 14251 | it->it_index = 0; |
| 14252 | Py_INCREF(seq); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14253 | it->it_seq = seq; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14254 | _PyObject_GC_TRACK(it); |
| 14255 | return (PyObject *)it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14256 | } |
| 14257 | |
Martin v. Löwis | 0d3072e | 2011-10-31 08:40:56 +0100 | [diff] [blame] | 14258 | |
| 14259 | size_t |
| 14260 | Py_UNICODE_strlen(const Py_UNICODE *u) |
| 14261 | { |
| 14262 | int res = 0; |
| 14263 | while(*u++) |
| 14264 | res++; |
| 14265 | return res; |
| 14266 | } |
| 14267 | |
| 14268 | Py_UNICODE* |
| 14269 | Py_UNICODE_strcpy(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 14270 | { |
| 14271 | Py_UNICODE *u = s1; |
| 14272 | while ((*u++ = *s2++)); |
| 14273 | return s1; |
| 14274 | } |
| 14275 | |
| 14276 | Py_UNICODE* |
| 14277 | Py_UNICODE_strncpy(Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 14278 | { |
| 14279 | Py_UNICODE *u = s1; |
| 14280 | while ((*u++ = *s2++)) |
| 14281 | if (n-- == 0) |
| 14282 | break; |
| 14283 | return s1; |
| 14284 | } |
| 14285 | |
| 14286 | Py_UNICODE* |
| 14287 | Py_UNICODE_strcat(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 14288 | { |
| 14289 | Py_UNICODE *u1 = s1; |
| 14290 | u1 += Py_UNICODE_strlen(u1); |
| 14291 | Py_UNICODE_strcpy(u1, s2); |
| 14292 | return s1; |
| 14293 | } |
| 14294 | |
| 14295 | int |
| 14296 | Py_UNICODE_strcmp(const Py_UNICODE *s1, const Py_UNICODE *s2) |
| 14297 | { |
| 14298 | while (*s1 && *s2 && *s1 == *s2) |
| 14299 | s1++, s2++; |
| 14300 | if (*s1 && *s2) |
| 14301 | return (*s1 < *s2) ? -1 : +1; |
| 14302 | if (*s1) |
| 14303 | return 1; |
| 14304 | if (*s2) |
| 14305 | return -1; |
| 14306 | return 0; |
| 14307 | } |
| 14308 | |
| 14309 | int |
| 14310 | Py_UNICODE_strncmp(const Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 14311 | { |
| 14312 | register Py_UNICODE u1, u2; |
| 14313 | for (; n != 0; n--) { |
| 14314 | u1 = *s1; |
| 14315 | u2 = *s2; |
| 14316 | if (u1 != u2) |
| 14317 | return (u1 < u2) ? -1 : +1; |
| 14318 | if (u1 == '\0') |
| 14319 | return 0; |
| 14320 | s1++; |
| 14321 | s2++; |
| 14322 | } |
| 14323 | return 0; |
| 14324 | } |
| 14325 | |
| 14326 | Py_UNICODE* |
| 14327 | Py_UNICODE_strchr(const Py_UNICODE *s, Py_UNICODE c) |
| 14328 | { |
| 14329 | const Py_UNICODE *p; |
| 14330 | for (p = s; *p; p++) |
| 14331 | if (*p == c) |
| 14332 | return (Py_UNICODE*)p; |
| 14333 | return NULL; |
| 14334 | } |
| 14335 | |
| 14336 | Py_UNICODE* |
| 14337 | Py_UNICODE_strrchr(const Py_UNICODE *s, Py_UNICODE c) |
| 14338 | { |
| 14339 | const Py_UNICODE *p; |
| 14340 | p = s + Py_UNICODE_strlen(s); |
| 14341 | while (p != s) { |
| 14342 | p--; |
| 14343 | if (*p == c) |
| 14344 | return (Py_UNICODE*)p; |
| 14345 | } |
| 14346 | return NULL; |
| 14347 | } |
Victor Stinner | 331ea92 | 2010-08-10 16:37:20 +0000 | [diff] [blame] | 14348 | |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14349 | Py_UNICODE* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14350 | PyUnicode_AsUnicodeCopy(PyObject *unicode) |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14351 | { |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 14352 | Py_UNICODE *u, *copy; |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14353 | Py_ssize_t len, size; |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14354 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14355 | if (!PyUnicode_Check(unicode)) { |
| 14356 | PyErr_BadArgument(); |
| 14357 | return NULL; |
| 14358 | } |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14359 | u = PyUnicode_AsUnicodeAndSize(unicode, &len); |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 14360 | if (u == NULL) |
| 14361 | return NULL; |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14362 | /* Ensure we won't overflow the size. */ |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14363 | if (len > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14364 | PyErr_NoMemory(); |
| 14365 | return NULL; |
| 14366 | } |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14367 | size = len + 1; /* copy the null character */ |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14368 | size *= sizeof(Py_UNICODE); |
| 14369 | copy = PyMem_Malloc(size); |
| 14370 | if (copy == NULL) { |
| 14371 | PyErr_NoMemory(); |
| 14372 | return NULL; |
| 14373 | } |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 14374 | memcpy(copy, u, size); |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14375 | return copy; |
| 14376 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 14377 | |
Georg Brandl | 66c221e | 2010-10-14 07:04:07 +0000 | [diff] [blame] | 14378 | /* A _string module, to export formatter_parser and formatter_field_name_split |
| 14379 | to the string.Formatter class implemented in Python. */ |
| 14380 | |
| 14381 | static PyMethodDef _string_methods[] = { |
| 14382 | {"formatter_field_name_split", (PyCFunction) formatter_field_name_split, |
| 14383 | METH_O, PyDoc_STR("split the argument as a field name")}, |
| 14384 | {"formatter_parser", (PyCFunction) formatter_parser, |
| 14385 | METH_O, PyDoc_STR("parse the argument as a format string")}, |
| 14386 | {NULL, NULL} |
| 14387 | }; |
| 14388 | |
| 14389 | static struct PyModuleDef _string_module = { |
| 14390 | PyModuleDef_HEAD_INIT, |
| 14391 | "_string", |
| 14392 | PyDoc_STR("string helper module"), |
| 14393 | 0, |
| 14394 | _string_methods, |
| 14395 | NULL, |
| 14396 | NULL, |
| 14397 | NULL, |
| 14398 | NULL |
| 14399 | }; |
| 14400 | |
| 14401 | PyMODINIT_FUNC |
| 14402 | PyInit__string(void) |
| 14403 | { |
| 14404 | return PyModule_Create(&_string_module); |
| 14405 | } |
| 14406 | |
| 14407 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 14408 | #ifdef __cplusplus |
| 14409 | } |
| 14410 | #endif |