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 : \ |
| 119 | _PyUnicode_Ready((PyObject *)(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, |
| 251 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 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); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 260 | |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 261 | /* Same for linebreaks */ |
| 262 | static unsigned char ascii_linebreak[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 263 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 264 | /* 0x000A, * LINE FEED */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 265 | /* 0x000B, * LINE TABULATION */ |
| 266 | /* 0x000C, * FORM FEED */ |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 267 | /* 0x000D, * CARRIAGE RETURN */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 268 | 0, 0, 1, 1, 1, 1, 0, 0, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 269 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 270 | /* 0x001C, * FILE SEPARATOR */ |
| 271 | /* 0x001D, * GROUP SEPARATOR */ |
| 272 | /* 0x001E, * RECORD SEPARATOR */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 273 | 0, 0, 0, 0, 1, 1, 1, 0, |
| 274 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 275 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 276 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 277 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 278 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 279 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 280 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 281 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 282 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 283 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 284 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 285 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 286 | 0, 0, 0, 0, 0, 0, 0, 0 |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 287 | }; |
| 288 | |
Ezio Melotti | 48a2f8f | 2011-09-29 00:18:19 +0300 | [diff] [blame] | 289 | /* The max unicode value is always 0x10FFFF while using the PEP-393 API. |
| 290 | This function is kept for backward compatibility with the old API. */ |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 291 | Py_UNICODE |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 292 | PyUnicode_GetMax(void) |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 293 | { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 294 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 295 | return 0x10FFFF; |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 296 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 297 | /* This is actually an illegal character, so it should |
| 298 | not be passed to unichr. */ |
| 299 | return 0xFFFF; |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 300 | #endif |
| 301 | } |
| 302 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 303 | #ifdef Py_DEBUG |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 304 | int |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 305 | /* FIXME: use PyObject* type for op */ |
| 306 | _PyUnicode_CheckConsistency(void *op, int check_content) |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 307 | { |
| 308 | PyASCIIObject *ascii; |
| 309 | unsigned int kind; |
| 310 | |
| 311 | assert(PyUnicode_Check(op)); |
| 312 | |
| 313 | ascii = (PyASCIIObject *)op; |
| 314 | kind = ascii->state.kind; |
| 315 | |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 316 | if (ascii->state.ascii == 1 && ascii->state.compact == 1) { |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 317 | assert(kind == PyUnicode_1BYTE_KIND); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 318 | assert(ascii->state.ready == 1); |
| 319 | } |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 320 | else { |
Victor Stinner | 85041a5 | 2011-10-03 14:42:39 +0200 | [diff] [blame] | 321 | PyCompactUnicodeObject *compact = (PyCompactUnicodeObject *)op; |
Victor Stinner | 7f11ad4 | 2011-10-04 00:00:20 +0200 | [diff] [blame] | 322 | void *data; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 323 | |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 324 | if (ascii->state.compact == 1) { |
| 325 | data = compact + 1; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 326 | assert(kind == PyUnicode_1BYTE_KIND |
| 327 | || kind == PyUnicode_2BYTE_KIND |
| 328 | || kind == PyUnicode_4BYTE_KIND); |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 329 | assert(ascii->state.ascii == 0); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 330 | assert(ascii->state.ready == 1); |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 331 | assert (compact->utf8 != data); |
| 332 | } else { |
| 333 | PyUnicodeObject *unicode = (PyUnicodeObject *)op; |
| 334 | |
| 335 | data = unicode->data.any; |
| 336 | if (kind == PyUnicode_WCHAR_KIND) { |
| 337 | assert(ascii->state.compact == 0); |
| 338 | assert(ascii->state.ascii == 0); |
| 339 | assert(ascii->state.ready == 0); |
| 340 | assert(ascii->wstr != NULL); |
| 341 | assert(data == NULL); |
| 342 | assert(compact->utf8 == NULL); |
| 343 | assert(ascii->state.interned == SSTATE_NOT_INTERNED); |
| 344 | } |
| 345 | else { |
| 346 | assert(kind == PyUnicode_1BYTE_KIND |
| 347 | || kind == PyUnicode_2BYTE_KIND |
| 348 | || kind == PyUnicode_4BYTE_KIND); |
| 349 | assert(ascii->state.compact == 0); |
| 350 | assert(ascii->state.ready == 1); |
| 351 | assert(data != NULL); |
| 352 | if (ascii->state.ascii) { |
| 353 | assert (compact->utf8 == data); |
| 354 | assert (compact->utf8_length == ascii->length); |
| 355 | } |
| 356 | else |
| 357 | assert (compact->utf8 != data); |
| 358 | } |
| 359 | } |
| 360 | if (kind != PyUnicode_WCHAR_KIND) { |
Victor Stinner | 7f11ad4 | 2011-10-04 00:00:20 +0200 | [diff] [blame] | 361 | if ( |
| 362 | #if SIZEOF_WCHAR_T == 2 |
| 363 | kind == PyUnicode_2BYTE_KIND |
| 364 | #else |
| 365 | kind == PyUnicode_4BYTE_KIND |
| 366 | #endif |
| 367 | ) |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 368 | { |
| 369 | assert(ascii->wstr == data); |
| 370 | assert(compact->wstr_length == ascii->length); |
| 371 | } else |
| 372 | assert(ascii->wstr != data); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 373 | } |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 374 | |
| 375 | if (compact->utf8 == NULL) |
| 376 | assert(compact->utf8_length == 0); |
| 377 | if (ascii->wstr == NULL) |
| 378 | assert(compact->wstr_length == 0); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 379 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 380 | /* check that the best kind is used */ |
| 381 | if (check_content && kind != PyUnicode_WCHAR_KIND) |
| 382 | { |
| 383 | Py_ssize_t i; |
| 384 | Py_UCS4 maxchar = 0; |
| 385 | void *data = PyUnicode_DATA(ascii); |
| 386 | for (i=0; i < ascii->length; i++) |
| 387 | { |
| 388 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 389 | if (ch > maxchar) |
| 390 | maxchar = ch; |
| 391 | } |
| 392 | if (kind == PyUnicode_1BYTE_KIND) { |
| 393 | if (ascii->state.ascii == 0) |
| 394 | assert(maxchar >= 128); |
| 395 | else |
| 396 | assert(maxchar < 128); |
| 397 | } |
| 398 | else if (kind == PyUnicode_2BYTE_KIND) |
| 399 | assert(maxchar >= 0x100); |
| 400 | else |
| 401 | assert(maxchar >= 0x10000); |
| 402 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 403 | if (check_content && !unicode_is_singleton((PyObject*)ascii)) |
| 404 | assert(ascii->hash == -1); |
Benjamin Peterson | ccc51c1 | 2011-10-03 19:34:12 -0400 | [diff] [blame] | 405 | return 1; |
| 406 | } |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 407 | #endif |
| 408 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 409 | #ifdef HAVE_MBCS |
| 410 | static OSVERSIONINFOEX winver; |
| 411 | #endif |
| 412 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 413 | /* --- Bloom Filters ----------------------------------------------------- */ |
| 414 | |
| 415 | /* stuff to implement simple "bloom filters" for Unicode characters. |
| 416 | to keep things simple, we use a single bitmask, using the least 5 |
| 417 | bits from each unicode characters as the bit index. */ |
| 418 | |
| 419 | /* the linebreak mask is set up by Unicode_Init below */ |
| 420 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 421 | #if LONG_BIT >= 128 |
| 422 | #define BLOOM_WIDTH 128 |
| 423 | #elif LONG_BIT >= 64 |
| 424 | #define BLOOM_WIDTH 64 |
| 425 | #elif LONG_BIT >= 32 |
| 426 | #define BLOOM_WIDTH 32 |
| 427 | #else |
| 428 | #error "LONG_BIT is smaller than 32" |
| 429 | #endif |
| 430 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 431 | #define BLOOM_MASK unsigned long |
| 432 | |
| 433 | static BLOOM_MASK bloom_linebreak; |
| 434 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 435 | #define BLOOM_ADD(mask, ch) ((mask |= (1UL << ((ch) & (BLOOM_WIDTH - 1))))) |
| 436 | #define BLOOM(mask, ch) ((mask & (1UL << ((ch) & (BLOOM_WIDTH - 1))))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 437 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 438 | #define BLOOM_LINEBREAK(ch) \ |
| 439 | ((ch) < 128U ? ascii_linebreak[(ch)] : \ |
| 440 | (BLOOM(bloom_linebreak, (ch)) && Py_UNICODE_ISLINEBREAK(ch))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 441 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 442 | Py_LOCAL_INLINE(BLOOM_MASK) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 443 | make_bloom_mask(int kind, void* ptr, Py_ssize_t len) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 444 | { |
| 445 | /* calculate simple bloom-style bitmask for a given unicode string */ |
| 446 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 447 | BLOOM_MASK mask; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 448 | Py_ssize_t i; |
| 449 | |
| 450 | mask = 0; |
| 451 | for (i = 0; i < len; i++) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 452 | BLOOM_ADD(mask, PyUnicode_READ(kind, ptr, i)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 453 | |
| 454 | return mask; |
| 455 | } |
| 456 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 457 | #define BLOOM_MEMBER(mask, chr, str) \ |
| 458 | (BLOOM(mask, chr) \ |
| 459 | && (PyUnicode_FindChar(str, chr, 0, PyUnicode_GET_LENGTH(str), 1) >= 0)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 460 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 461 | /* Compilation of templated routines */ |
| 462 | |
| 463 | #include "stringlib/asciilib.h" |
| 464 | #include "stringlib/fastsearch.h" |
| 465 | #include "stringlib/partition.h" |
| 466 | #include "stringlib/split.h" |
| 467 | #include "stringlib/count.h" |
| 468 | #include "stringlib/find.h" |
| 469 | #include "stringlib/find_max_char.h" |
| 470 | #include "stringlib/localeutil.h" |
| 471 | #include "stringlib/undef.h" |
| 472 | |
| 473 | #include "stringlib/ucs1lib.h" |
| 474 | #include "stringlib/fastsearch.h" |
| 475 | #include "stringlib/partition.h" |
| 476 | #include "stringlib/split.h" |
| 477 | #include "stringlib/count.h" |
| 478 | #include "stringlib/find.h" |
| 479 | #include "stringlib/find_max_char.h" |
| 480 | #include "stringlib/localeutil.h" |
| 481 | #include "stringlib/undef.h" |
| 482 | |
| 483 | #include "stringlib/ucs2lib.h" |
| 484 | #include "stringlib/fastsearch.h" |
| 485 | #include "stringlib/partition.h" |
| 486 | #include "stringlib/split.h" |
| 487 | #include "stringlib/count.h" |
| 488 | #include "stringlib/find.h" |
| 489 | #include "stringlib/find_max_char.h" |
| 490 | #include "stringlib/localeutil.h" |
| 491 | #include "stringlib/undef.h" |
| 492 | |
| 493 | #include "stringlib/ucs4lib.h" |
| 494 | #include "stringlib/fastsearch.h" |
| 495 | #include "stringlib/partition.h" |
| 496 | #include "stringlib/split.h" |
| 497 | #include "stringlib/count.h" |
| 498 | #include "stringlib/find.h" |
| 499 | #include "stringlib/find_max_char.h" |
| 500 | #include "stringlib/localeutil.h" |
| 501 | #include "stringlib/undef.h" |
| 502 | |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 503 | #include "stringlib/unicodedefs.h" |
| 504 | #include "stringlib/fastsearch.h" |
| 505 | #include "stringlib/count.h" |
| 506 | #include "stringlib/find.h" |
| 507 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 508 | /* --- Unicode Object ----------------------------------------------------- */ |
| 509 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 510 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 511 | fixup(PyObject *self, Py_UCS4 (*fixfct)(PyObject *s)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 512 | |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 513 | Py_LOCAL_INLINE(Py_ssize_t) findchar(void *s, int kind, |
| 514 | Py_ssize_t size, Py_UCS4 ch, |
| 515 | int direction) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 516 | { |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 517 | int mode = (direction == 1) ? FAST_SEARCH : FAST_RSEARCH; |
| 518 | |
| 519 | switch (kind) { |
| 520 | case PyUnicode_1BYTE_KIND: |
| 521 | { |
| 522 | Py_UCS1 ch1 = (Py_UCS1) ch; |
| 523 | if (ch1 == ch) |
| 524 | return ucs1lib_fastsearch((Py_UCS1 *) s, size, &ch1, 1, 0, mode); |
| 525 | else |
| 526 | return -1; |
| 527 | } |
| 528 | case PyUnicode_2BYTE_KIND: |
| 529 | { |
| 530 | Py_UCS2 ch2 = (Py_UCS2) ch; |
| 531 | if (ch2 == ch) |
| 532 | return ucs2lib_fastsearch((Py_UCS2 *) s, size, &ch2, 1, 0, mode); |
| 533 | else |
| 534 | return -1; |
| 535 | } |
| 536 | case PyUnicode_4BYTE_KIND: |
| 537 | return ucs4lib_fastsearch((Py_UCS4 *) s, size, &ch, 1, 0, mode); |
| 538 | default: |
| 539 | assert(0); |
| 540 | return -1; |
Victor Stinner | 9e7a1bc | 2011-10-13 00:18:12 +0200 | [diff] [blame] | 541 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 542 | } |
| 543 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 544 | static PyObject* |
| 545 | resize_compact(PyObject *unicode, Py_ssize_t length) |
| 546 | { |
| 547 | Py_ssize_t char_size; |
| 548 | Py_ssize_t struct_size; |
| 549 | Py_ssize_t new_size; |
| 550 | int share_wstr; |
| 551 | |
| 552 | assert(PyUnicode_IS_READY(unicode)); |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 553 | char_size = PyUnicode_KIND(unicode); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 554 | if (PyUnicode_IS_COMPACT_ASCII(unicode)) |
| 555 | struct_size = sizeof(PyASCIIObject); |
| 556 | else |
| 557 | struct_size = sizeof(PyCompactUnicodeObject); |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 558 | share_wstr = _PyUnicode_SHARE_WSTR(unicode); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 559 | |
| 560 | _Py_DEC_REFTOTAL; |
| 561 | _Py_ForgetReference(unicode); |
| 562 | |
| 563 | if (length > ((PY_SSIZE_T_MAX - struct_size) / char_size - 1)) { |
| 564 | PyErr_NoMemory(); |
| 565 | return NULL; |
| 566 | } |
| 567 | new_size = (struct_size + (length + 1) * char_size); |
| 568 | |
| 569 | unicode = (PyObject *)PyObject_REALLOC((char *)unicode, new_size); |
| 570 | if (unicode == NULL) { |
| 571 | PyObject_Del(unicode); |
| 572 | PyErr_NoMemory(); |
| 573 | return NULL; |
| 574 | } |
| 575 | _Py_NewReference(unicode); |
| 576 | _PyUnicode_LENGTH(unicode) = length; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 577 | if (share_wstr) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 578 | _PyUnicode_WSTR(unicode) = PyUnicode_DATA(unicode); |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 579 | if (!PyUnicode_IS_COMPACT_ASCII(unicode)) |
| 580 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 581 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 582 | PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode), |
| 583 | length, 0); |
| 584 | return unicode; |
| 585 | } |
| 586 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 587 | static int |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 588 | resize_inplace(PyObject *unicode, Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 589 | { |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 590 | wchar_t *wstr; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 591 | assert(!PyUnicode_IS_COMPACT(unicode)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 592 | assert(Py_REFCNT(unicode) == 1); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 593 | |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 594 | _PyUnicode_DIRTY(unicode); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 595 | |
| 596 | if (PyUnicode_IS_READY(unicode)) { |
| 597 | Py_ssize_t char_size; |
| 598 | Py_ssize_t new_size; |
Victor Stinner | 1c8d0c7 | 2011-10-03 12:11:00 +0200 | [diff] [blame] | 599 | int share_wstr, share_utf8; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 600 | void *data; |
| 601 | |
| 602 | data = _PyUnicode_DATA_ANY(unicode); |
| 603 | assert(data != NULL); |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 604 | char_size = PyUnicode_KIND(unicode); |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 605 | share_wstr = _PyUnicode_SHARE_WSTR(unicode); |
| 606 | share_utf8 = _PyUnicode_SHARE_UTF8(unicode); |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 607 | if (!share_utf8 && _PyUnicode_HAS_UTF8_MEMORY(unicode)) |
| 608 | { |
| 609 | PyObject_DEL(_PyUnicode_UTF8(unicode)); |
| 610 | _PyUnicode_UTF8(unicode) = NULL; |
| 611 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
| 612 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 613 | |
| 614 | if (length > (PY_SSIZE_T_MAX / char_size - 1)) { |
| 615 | PyErr_NoMemory(); |
| 616 | return -1; |
| 617 | } |
| 618 | new_size = (length + 1) * char_size; |
| 619 | |
| 620 | data = (PyObject *)PyObject_REALLOC(data, new_size); |
| 621 | if (data == NULL) { |
| 622 | PyErr_NoMemory(); |
| 623 | return -1; |
| 624 | } |
| 625 | _PyUnicode_DATA_ANY(unicode) = data; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 626 | if (share_wstr) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 627 | _PyUnicode_WSTR(unicode) = data; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 628 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 629 | } |
| 630 | if (share_utf8) { |
Victor Stinner | 1c8d0c7 | 2011-10-03 12:11:00 +0200 | [diff] [blame] | 631 | _PyUnicode_UTF8(unicode) = data; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 632 | _PyUnicode_UTF8_LENGTH(unicode) = length; |
| 633 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 634 | _PyUnicode_LENGTH(unicode) = length; |
| 635 | PyUnicode_WRITE(PyUnicode_KIND(unicode), data, length, 0); |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 636 | if (share_wstr || _PyUnicode_WSTR(unicode) == NULL) { |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 637 | assert(_PyUnicode_CheckConsistency(unicode, 0)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 638 | return 0; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 639 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 640 | } |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 641 | assert(_PyUnicode_WSTR(unicode) != NULL); |
| 642 | |
| 643 | /* check for integer overflow */ |
| 644 | if (length > PY_SSIZE_T_MAX / sizeof(wchar_t) - 1) { |
| 645 | PyErr_NoMemory(); |
| 646 | return -1; |
| 647 | } |
| 648 | wstr = _PyUnicode_WSTR(unicode); |
| 649 | wstr = PyObject_REALLOC(wstr, sizeof(wchar_t) * (length + 1)); |
| 650 | if (!wstr) { |
| 651 | PyErr_NoMemory(); |
| 652 | return -1; |
| 653 | } |
| 654 | _PyUnicode_WSTR(unicode) = wstr; |
| 655 | _PyUnicode_WSTR(unicode)[length] = 0; |
| 656 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 657 | assert(_PyUnicode_CheckConsistency(unicode, 0)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 658 | return 0; |
| 659 | } |
| 660 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 661 | static PyObject* |
| 662 | resize_copy(PyObject *unicode, Py_ssize_t length) |
| 663 | { |
| 664 | Py_ssize_t copy_length; |
| 665 | if (PyUnicode_IS_COMPACT(unicode)) { |
| 666 | PyObject *copy; |
| 667 | assert(PyUnicode_IS_READY(unicode)); |
| 668 | |
| 669 | copy = PyUnicode_New(length, PyUnicode_MAX_CHAR_VALUE(unicode)); |
| 670 | if (copy == NULL) |
| 671 | return NULL; |
| 672 | |
| 673 | copy_length = Py_MIN(length, PyUnicode_GET_LENGTH(unicode)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 674 | copy_characters(copy, 0, unicode, 0, copy_length); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 675 | return copy; |
Victor Stinner | 8cfcbed | 2011-10-03 23:19:21 +0200 | [diff] [blame] | 676 | } |
| 677 | else { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 678 | PyObject *w; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 679 | assert(_PyUnicode_WSTR(unicode) != NULL); |
| 680 | assert(_PyUnicode_DATA_ANY(unicode) == NULL); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 681 | w = (PyObject*)_PyUnicode_New(length); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 682 | if (w == NULL) |
| 683 | return NULL; |
| 684 | copy_length = _PyUnicode_WSTR_LENGTH(unicode); |
| 685 | copy_length = Py_MIN(copy_length, length); |
| 686 | Py_UNICODE_COPY(_PyUnicode_WSTR(w), _PyUnicode_WSTR(unicode), |
| 687 | copy_length); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 688 | return w; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 689 | } |
| 690 | } |
| 691 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 692 | /* 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] | 693 | Ux0000 terminated; some code (e.g. new_identifier) |
| 694 | relies on that. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 695 | |
| 696 | XXX This allocator could further be enhanced by assuring that the |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 697 | free list never reduces its size below 1. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 698 | |
| 699 | */ |
| 700 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 701 | #ifdef Py_DEBUG |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 702 | static int unicode_old_new_calls = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 703 | #endif |
| 704 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 705 | static PyUnicodeObject * |
| 706 | _PyUnicode_New(Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 707 | { |
| 708 | register PyUnicodeObject *unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 709 | size_t new_size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 710 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 711 | /* Optimization for empty strings */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 712 | if (length == 0 && unicode_empty != NULL) { |
| 713 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 714 | return (PyUnicodeObject*)unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 715 | } |
| 716 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 717 | /* Ensure we won't overflow the size. */ |
| 718 | if (length > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
| 719 | return (PyUnicodeObject *)PyErr_NoMemory(); |
| 720 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 721 | if (length < 0) { |
| 722 | PyErr_SetString(PyExc_SystemError, |
| 723 | "Negative size passed to _PyUnicode_New"); |
| 724 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 725 | } |
| 726 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 727 | #ifdef Py_DEBUG |
| 728 | ++unicode_old_new_calls; |
| 729 | #endif |
| 730 | |
| 731 | unicode = PyObject_New(PyUnicodeObject, &PyUnicode_Type); |
| 732 | if (unicode == NULL) |
| 733 | return NULL; |
| 734 | new_size = sizeof(Py_UNICODE) * ((size_t)length + 1); |
| 735 | _PyUnicode_WSTR(unicode) = (Py_UNICODE*) PyObject_MALLOC(new_size); |
| 736 | if (!_PyUnicode_WSTR(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 737 | PyErr_NoMemory(); |
| 738 | goto onError; |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 739 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 740 | |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 741 | /* Initialize the first element to guard against cases where |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 742 | * the caller fails before initializing str -- unicode_resize() |
| 743 | * reads str[0], and the Keep-Alive optimization can keep memory |
| 744 | * allocated for str alive across a call to unicode_dealloc(unicode). |
| 745 | * We don't want unicode_resize to read uninitialized memory in |
| 746 | * that case. |
| 747 | */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 748 | _PyUnicode_WSTR(unicode)[0] = 0; |
| 749 | _PyUnicode_WSTR(unicode)[length] = 0; |
| 750 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 751 | _PyUnicode_HASH(unicode) = -1; |
| 752 | _PyUnicode_STATE(unicode).interned = 0; |
| 753 | _PyUnicode_STATE(unicode).kind = 0; |
| 754 | _PyUnicode_STATE(unicode).compact = 0; |
| 755 | _PyUnicode_STATE(unicode).ready = 0; |
| 756 | _PyUnicode_STATE(unicode).ascii = 0; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 757 | _PyUnicode_DATA_ANY(unicode) = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 758 | _PyUnicode_LENGTH(unicode) = 0; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 759 | _PyUnicode_UTF8(unicode) = NULL; |
| 760 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 6707293 | 2011-10-18 22:10:14 +0200 | [diff] [blame] | 761 | assert(_PyUnicode_CheckConsistency(unicode, 0)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 762 | return unicode; |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 763 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 764 | onError: |
Amaury Forgeot d'Arc | 7888d08 | 2008-08-01 01:06:32 +0000 | [diff] [blame] | 765 | /* XXX UNREF/NEWREF interface should be more symmetrical */ |
| 766 | _Py_DEC_REFTOTAL; |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 767 | _Py_ForgetReference((PyObject *)unicode); |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 768 | PyObject_Del(unicode); |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 769 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 770 | } |
| 771 | |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 772 | static const char* |
| 773 | unicode_kind_name(PyObject *unicode) |
| 774 | { |
Victor Stinner | 42dfd71 | 2011-10-03 14:41:45 +0200 | [diff] [blame] | 775 | /* don't check consistency: unicode_kind_name() is called from |
| 776 | _PyUnicode_Dump() */ |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 777 | if (!PyUnicode_IS_COMPACT(unicode)) |
| 778 | { |
| 779 | if (!PyUnicode_IS_READY(unicode)) |
| 780 | return "wstr"; |
| 781 | switch(PyUnicode_KIND(unicode)) |
| 782 | { |
| 783 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 784 | if (PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 785 | return "legacy ascii"; |
| 786 | else |
| 787 | return "legacy latin1"; |
| 788 | case PyUnicode_2BYTE_KIND: |
| 789 | return "legacy UCS2"; |
| 790 | case PyUnicode_4BYTE_KIND: |
| 791 | return "legacy UCS4"; |
| 792 | default: |
| 793 | return "<legacy invalid kind>"; |
| 794 | } |
| 795 | } |
| 796 | assert(PyUnicode_IS_READY(unicode)); |
| 797 | switch(PyUnicode_KIND(unicode)) |
| 798 | { |
| 799 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 800 | if (PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 801 | return "ascii"; |
| 802 | else |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 803 | return "latin1"; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 804 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 805 | return "UCS2"; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 806 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 807 | return "UCS4"; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 808 | default: |
| 809 | return "<invalid compact kind>"; |
| 810 | } |
| 811 | } |
| 812 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 813 | #ifdef Py_DEBUG |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 814 | static int unicode_new_new_calls = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 815 | |
| 816 | /* Functions wrapping macros for use in debugger */ |
| 817 | char *_PyUnicode_utf8(void *unicode){ |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 818 | return PyUnicode_UTF8(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 819 | } |
| 820 | |
| 821 | void *_PyUnicode_compact_data(void *unicode) { |
| 822 | return _PyUnicode_COMPACT_DATA(unicode); |
| 823 | } |
| 824 | void *_PyUnicode_data(void *unicode){ |
| 825 | printf("obj %p\n", unicode); |
| 826 | printf("compact %d\n", PyUnicode_IS_COMPACT(unicode)); |
| 827 | printf("compact ascii %d\n", PyUnicode_IS_COMPACT_ASCII(unicode)); |
| 828 | printf("ascii op %p\n", ((void*)((PyASCIIObject*)(unicode) + 1))); |
| 829 | printf("compact op %p\n", ((void*)((PyCompactUnicodeObject*)(unicode) + 1))); |
| 830 | printf("compact data %p\n", _PyUnicode_COMPACT_DATA(unicode)); |
| 831 | return PyUnicode_DATA(unicode); |
| 832 | } |
Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 833 | |
| 834 | void |
| 835 | _PyUnicode_Dump(PyObject *op) |
| 836 | { |
| 837 | PyASCIIObject *ascii = (PyASCIIObject *)op; |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 838 | PyCompactUnicodeObject *compact = (PyCompactUnicodeObject *)op; |
| 839 | PyUnicodeObject *unicode = (PyUnicodeObject *)op; |
| 840 | void *data; |
Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 841 | |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 842 | if (ascii->state.compact) |
Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 843 | { |
| 844 | if (ascii->state.ascii) |
| 845 | data = (ascii + 1); |
| 846 | else |
| 847 | data = (compact + 1); |
| 848 | } |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 849 | else |
| 850 | data = unicode->data.any; |
Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 851 | printf("%s: len=%zu, ",unicode_kind_name(op), ascii->length); |
| 852 | |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 853 | if (ascii->wstr == data) |
| 854 | printf("shared "); |
| 855 | printf("wstr=%p", ascii->wstr); |
Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 856 | |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 857 | if (!(ascii->state.ascii == 1 && ascii->state.compact == 1)) { |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 858 | printf(" (%zu), ", compact->wstr_length); |
| 859 | if (!ascii->state.compact && compact->utf8 == unicode->data.any) |
| 860 | printf("shared "); |
| 861 | printf("utf8=%p (%zu)", compact->utf8, compact->utf8_length); |
Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 862 | } |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 863 | printf(", data=%p\n", data); |
Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 864 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 865 | #endif |
| 866 | |
| 867 | PyObject * |
| 868 | PyUnicode_New(Py_ssize_t size, Py_UCS4 maxchar) |
| 869 | { |
| 870 | PyObject *obj; |
| 871 | PyCompactUnicodeObject *unicode; |
| 872 | void *data; |
| 873 | int kind_state; |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 874 | int is_sharing, is_ascii; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 875 | Py_ssize_t char_size; |
| 876 | Py_ssize_t struct_size; |
| 877 | |
| 878 | /* Optimization for empty strings */ |
| 879 | if (size == 0 && unicode_empty != NULL) { |
| 880 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 881 | return unicode_empty; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 882 | } |
| 883 | |
| 884 | #ifdef Py_DEBUG |
| 885 | ++unicode_new_new_calls; |
| 886 | #endif |
| 887 | |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 888 | is_ascii = 0; |
| 889 | is_sharing = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 890 | struct_size = sizeof(PyCompactUnicodeObject); |
| 891 | if (maxchar < 128) { |
| 892 | kind_state = PyUnicode_1BYTE_KIND; |
| 893 | char_size = 1; |
| 894 | is_ascii = 1; |
| 895 | struct_size = sizeof(PyASCIIObject); |
| 896 | } |
| 897 | else if (maxchar < 256) { |
| 898 | kind_state = PyUnicode_1BYTE_KIND; |
| 899 | char_size = 1; |
| 900 | } |
| 901 | else if (maxchar < 65536) { |
| 902 | kind_state = PyUnicode_2BYTE_KIND; |
| 903 | char_size = 2; |
| 904 | if (sizeof(wchar_t) == 2) |
| 905 | is_sharing = 1; |
| 906 | } |
| 907 | else { |
| 908 | kind_state = PyUnicode_4BYTE_KIND; |
| 909 | char_size = 4; |
| 910 | if (sizeof(wchar_t) == 4) |
| 911 | is_sharing = 1; |
| 912 | } |
| 913 | |
| 914 | /* Ensure we won't overflow the size. */ |
| 915 | if (size < 0) { |
| 916 | PyErr_SetString(PyExc_SystemError, |
| 917 | "Negative size passed to PyUnicode_New"); |
| 918 | return NULL; |
| 919 | } |
| 920 | if (size > ((PY_SSIZE_T_MAX - struct_size) / char_size - 1)) |
| 921 | return PyErr_NoMemory(); |
| 922 | |
| 923 | /* Duplicated allocation code from _PyObject_New() instead of a call to |
| 924 | * PyObject_New() so we are able to allocate space for the object and |
| 925 | * it's data buffer. |
| 926 | */ |
| 927 | obj = (PyObject *) PyObject_MALLOC(struct_size + (size + 1) * char_size); |
| 928 | if (obj == NULL) |
| 929 | return PyErr_NoMemory(); |
| 930 | obj = PyObject_INIT(obj, &PyUnicode_Type); |
| 931 | if (obj == NULL) |
| 932 | return NULL; |
| 933 | |
| 934 | unicode = (PyCompactUnicodeObject *)obj; |
| 935 | if (is_ascii) |
| 936 | data = ((PyASCIIObject*)obj) + 1; |
| 937 | else |
| 938 | data = unicode + 1; |
| 939 | _PyUnicode_LENGTH(unicode) = size; |
| 940 | _PyUnicode_HASH(unicode) = -1; |
| 941 | _PyUnicode_STATE(unicode).interned = 0; |
| 942 | _PyUnicode_STATE(unicode).kind = kind_state; |
| 943 | _PyUnicode_STATE(unicode).compact = 1; |
| 944 | _PyUnicode_STATE(unicode).ready = 1; |
| 945 | _PyUnicode_STATE(unicode).ascii = is_ascii; |
| 946 | if (is_ascii) { |
| 947 | ((char*)data)[size] = 0; |
| 948 | _PyUnicode_WSTR(unicode) = NULL; |
| 949 | } |
| 950 | else if (kind_state == PyUnicode_1BYTE_KIND) { |
| 951 | ((char*)data)[size] = 0; |
| 952 | _PyUnicode_WSTR(unicode) = NULL; |
| 953 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 954 | unicode->utf8 = NULL; |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 955 | unicode->utf8_length = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 956 | } |
| 957 | else { |
| 958 | unicode->utf8 = NULL; |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 959 | unicode->utf8_length = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 960 | if (kind_state == PyUnicode_2BYTE_KIND) |
| 961 | ((Py_UCS2*)data)[size] = 0; |
| 962 | else /* kind_state == PyUnicode_4BYTE_KIND */ |
| 963 | ((Py_UCS4*)data)[size] = 0; |
| 964 | if (is_sharing) { |
| 965 | _PyUnicode_WSTR_LENGTH(unicode) = size; |
| 966 | _PyUnicode_WSTR(unicode) = (wchar_t *)data; |
| 967 | } |
| 968 | else { |
| 969 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 970 | _PyUnicode_WSTR(unicode) = NULL; |
| 971 | } |
| 972 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 973 | assert(_PyUnicode_CheckConsistency(unicode, 0)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 974 | return obj; |
| 975 | } |
| 976 | |
| 977 | #if SIZEOF_WCHAR_T == 2 |
| 978 | /* Helper function to convert a 16-bits wchar_t representation to UCS4, this |
| 979 | will decode surrogate pairs, the other conversions are implemented as macros |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 980 | for efficiency. |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 981 | |
| 982 | This function assumes that unicode can hold one more code point than wstr |
| 983 | characters for a terminating null character. */ |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 984 | static void |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 985 | 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] | 986 | PyObject *unicode) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 987 | { |
| 988 | const wchar_t *iter; |
| 989 | Py_UCS4 *ucs4_out; |
| 990 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 991 | assert(unicode != NULL); |
| 992 | assert(_PyUnicode_CHECK(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 993 | assert(_PyUnicode_KIND(unicode) == PyUnicode_4BYTE_KIND); |
| 994 | ucs4_out = PyUnicode_4BYTE_DATA(unicode); |
| 995 | |
| 996 | for (iter = begin; iter < end; ) { |
| 997 | assert(ucs4_out < (PyUnicode_4BYTE_DATA(unicode) + |
| 998 | _PyUnicode_GET_LENGTH(unicode))); |
| 999 | if (*iter >= 0xD800 && *iter <= 0xDBFF |
| 1000 | && (iter+1) < end && iter[1] >= 0xDC00 && iter[1] <= 0xDFFF) |
| 1001 | { |
| 1002 | *ucs4_out++ = (((iter[0] & 0x3FF)<<10) | (iter[1] & 0x3FF)) + 0x10000; |
| 1003 | iter += 2; |
| 1004 | } |
| 1005 | else { |
| 1006 | *ucs4_out++ = *iter; |
| 1007 | iter++; |
| 1008 | } |
| 1009 | } |
| 1010 | assert(ucs4_out == (PyUnicode_4BYTE_DATA(unicode) + |
| 1011 | _PyUnicode_GET_LENGTH(unicode))); |
| 1012 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1013 | } |
| 1014 | #endif |
| 1015 | |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1016 | static int |
| 1017 | _PyUnicode_Dirty(PyObject *unicode) |
| 1018 | { |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1019 | assert(_PyUnicode_CHECK(unicode)); |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1020 | if (Py_REFCNT(unicode) != 1) { |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 1021 | PyErr_SetString(PyExc_SystemError, |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1022 | "Cannot modify a string having more than 1 reference"); |
| 1023 | return -1; |
| 1024 | } |
| 1025 | _PyUnicode_DIRTY(unicode); |
| 1026 | return 0; |
| 1027 | } |
| 1028 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1029 | static int |
| 1030 | _copy_characters(PyObject *to, Py_ssize_t to_start, |
| 1031 | PyObject *from, Py_ssize_t from_start, |
| 1032 | Py_ssize_t how_many, int check_maxchar) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1033 | { |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1034 | unsigned int from_kind, to_kind; |
| 1035 | void *from_data, *to_data; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1036 | int fast; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1037 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1038 | assert(PyUnicode_Check(from)); |
| 1039 | assert(PyUnicode_Check(to)); |
| 1040 | assert(PyUnicode_IS_READY(from)); |
| 1041 | assert(PyUnicode_IS_READY(to)); |
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_GET_LENGTH(from) >= how_many); |
| 1044 | assert(to_start + how_many <= PyUnicode_GET_LENGTH(to)); |
| 1045 | assert(0 <= how_many); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1046 | |
Victor Stinner | f5ca1a2 | 2011-09-28 23:54:59 +0200 | [diff] [blame] | 1047 | if (how_many == 0) |
| 1048 | return 0; |
| 1049 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1050 | from_kind = PyUnicode_KIND(from); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1051 | from_data = PyUnicode_DATA(from); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1052 | to_kind = PyUnicode_KIND(to); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1053 | to_data = PyUnicode_DATA(to); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1054 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1055 | #ifdef Py_DEBUG |
| 1056 | if (!check_maxchar |
| 1057 | && (from_kind > to_kind |
| 1058 | || (!PyUnicode_IS_ASCII(from) && PyUnicode_IS_ASCII(to)))) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 1059 | { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1060 | const Py_UCS4 to_maxchar = PyUnicode_MAX_CHAR_VALUE(to); |
| 1061 | Py_UCS4 ch; |
| 1062 | Py_ssize_t i; |
| 1063 | for (i=0; i < how_many; i++) { |
| 1064 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
| 1065 | assert(ch <= to_maxchar); |
| 1066 | } |
| 1067 | } |
| 1068 | #endif |
| 1069 | fast = (from_kind == to_kind); |
| 1070 | if (check_maxchar |
| 1071 | && (!PyUnicode_IS_ASCII(from) && PyUnicode_IS_ASCII(to))) |
| 1072 | { |
| 1073 | /* deny latin1 => ascii */ |
| 1074 | fast = 0; |
| 1075 | } |
| 1076 | |
| 1077 | if (fast) { |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 1078 | Py_MEMCPY((char*)to_data + to_kind * to_start, |
| 1079 | (char*)from_data + from_kind * from_start, |
| 1080 | to_kind * how_many); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1081 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1082 | else if (from_kind == PyUnicode_1BYTE_KIND |
| 1083 | && to_kind == PyUnicode_2BYTE_KIND) |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1084 | { |
| 1085 | _PyUnicode_CONVERT_BYTES( |
| 1086 | Py_UCS1, Py_UCS2, |
| 1087 | PyUnicode_1BYTE_DATA(from) + from_start, |
| 1088 | PyUnicode_1BYTE_DATA(from) + from_start + how_many, |
| 1089 | PyUnicode_2BYTE_DATA(to) + to_start |
| 1090 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1091 | } |
Victor Stinner | 157f83f | 2011-09-28 21:41:31 +0200 | [diff] [blame] | 1092 | else if (from_kind == PyUnicode_1BYTE_KIND |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1093 | && to_kind == PyUnicode_4BYTE_KIND) |
| 1094 | { |
| 1095 | _PyUnicode_CONVERT_BYTES( |
| 1096 | Py_UCS1, Py_UCS4, |
| 1097 | PyUnicode_1BYTE_DATA(from) + from_start, |
| 1098 | PyUnicode_1BYTE_DATA(from) + from_start + how_many, |
| 1099 | PyUnicode_4BYTE_DATA(to) + to_start |
| 1100 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1101 | } |
| 1102 | else if (from_kind == PyUnicode_2BYTE_KIND |
| 1103 | && to_kind == PyUnicode_4BYTE_KIND) |
| 1104 | { |
| 1105 | _PyUnicode_CONVERT_BYTES( |
| 1106 | Py_UCS2, Py_UCS4, |
| 1107 | PyUnicode_2BYTE_DATA(from) + from_start, |
| 1108 | PyUnicode_2BYTE_DATA(from) + from_start + how_many, |
| 1109 | PyUnicode_4BYTE_DATA(to) + to_start |
| 1110 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1111 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1112 | else { |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 1113 | /* check if max_char(from substring) <= max_char(to) */ |
| 1114 | if (from_kind > to_kind |
| 1115 | /* latin1 => ascii */ |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1116 | || (!PyUnicode_IS_ASCII(from) && PyUnicode_IS_ASCII(to))) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 1117 | { |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1118 | /* slow path to check for character overflow */ |
| 1119 | const Py_UCS4 to_maxchar = PyUnicode_MAX_CHAR_VALUE(to); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1120 | Py_UCS4 ch; |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1121 | Py_ssize_t i; |
| 1122 | |
Victor Stinner | 56c161a | 2011-10-06 02:47:11 +0200 | [diff] [blame] | 1123 | #ifdef Py_DEBUG |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1124 | for (i=0; i < how_many; i++) { |
| 1125 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
Victor Stinner | 56c161a | 2011-10-06 02:47:11 +0200 | [diff] [blame] | 1126 | assert(ch <= to_maxchar); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1127 | PyUnicode_WRITE(to_kind, to_data, to_start + i, ch); |
| 1128 | } |
Victor Stinner | 56c161a | 2011-10-06 02:47:11 +0200 | [diff] [blame] | 1129 | #else |
| 1130 | if (!check_maxchar) { |
| 1131 | for (i=0; i < how_many; i++) { |
| 1132 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
| 1133 | PyUnicode_WRITE(to_kind, to_data, to_start + i, ch); |
| 1134 | } |
| 1135 | } |
| 1136 | else { |
| 1137 | for (i=0; i < how_many; i++) { |
| 1138 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
| 1139 | if (ch > to_maxchar) |
| 1140 | return 1; |
| 1141 | PyUnicode_WRITE(to_kind, to_data, to_start + i, ch); |
| 1142 | } |
| 1143 | } |
| 1144 | #endif |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1145 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1146 | else { |
Victor Stinner | 56c161a | 2011-10-06 02:47:11 +0200 | [diff] [blame] | 1147 | assert(0 && "inconsistent state"); |
| 1148 | return 1; |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1149 | } |
| 1150 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1151 | return 0; |
| 1152 | } |
| 1153 | |
| 1154 | static void |
| 1155 | copy_characters(PyObject *to, Py_ssize_t to_start, |
| 1156 | PyObject *from, Py_ssize_t from_start, |
| 1157 | Py_ssize_t how_many) |
| 1158 | { |
| 1159 | (void)_copy_characters(to, to_start, from, from_start, how_many, 0); |
| 1160 | } |
| 1161 | |
| 1162 | Py_ssize_t |
| 1163 | PyUnicode_CopyCharacters(PyObject *to, Py_ssize_t to_start, |
| 1164 | PyObject *from, Py_ssize_t from_start, |
| 1165 | Py_ssize_t how_many) |
| 1166 | { |
| 1167 | int err; |
| 1168 | |
| 1169 | if (!PyUnicode_Check(from) || !PyUnicode_Check(to)) { |
| 1170 | PyErr_BadInternalCall(); |
| 1171 | return -1; |
| 1172 | } |
| 1173 | |
| 1174 | if (PyUnicode_READY(from)) |
| 1175 | return -1; |
| 1176 | if (PyUnicode_READY(to)) |
| 1177 | return -1; |
| 1178 | |
| 1179 | how_many = Py_MIN(PyUnicode_GET_LENGTH(from), how_many); |
| 1180 | if (to_start + how_many > PyUnicode_GET_LENGTH(to)) { |
| 1181 | PyErr_Format(PyExc_SystemError, |
| 1182 | "Cannot write %zi characters at %zi " |
| 1183 | "in a string of %zi characters", |
| 1184 | how_many, to_start, PyUnicode_GET_LENGTH(to)); |
| 1185 | return -1; |
| 1186 | } |
| 1187 | |
| 1188 | if (how_many == 0) |
| 1189 | return 0; |
| 1190 | |
| 1191 | if (_PyUnicode_Dirty(to)) |
| 1192 | return -1; |
| 1193 | |
| 1194 | err = _copy_characters(to, to_start, from, from_start, how_many, 1); |
| 1195 | if (err) { |
| 1196 | PyErr_Format(PyExc_SystemError, |
| 1197 | "Cannot copy %s characters " |
| 1198 | "into a string of %s characters", |
| 1199 | unicode_kind_name(from), |
| 1200 | unicode_kind_name(to)); |
| 1201 | return -1; |
| 1202 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1203 | return how_many; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1204 | } |
| 1205 | |
Victor Stinner | 1722216 | 2011-09-28 22:15:37 +0200 | [diff] [blame] | 1206 | /* Find the maximum code point and count the number of surrogate pairs so a |
| 1207 | correct string length can be computed before converting a string to UCS4. |
| 1208 | This function counts single surrogates as a character and not as a pair. |
| 1209 | |
| 1210 | Return 0 on success, or -1 on error. */ |
| 1211 | static int |
| 1212 | find_maxchar_surrogates(const wchar_t *begin, const wchar_t *end, |
| 1213 | Py_UCS4 *maxchar, Py_ssize_t *num_surrogates) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1214 | { |
| 1215 | const wchar_t *iter; |
| 1216 | |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1217 | assert(num_surrogates != NULL && maxchar != NULL); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1218 | *num_surrogates = 0; |
| 1219 | *maxchar = 0; |
| 1220 | |
| 1221 | for (iter = begin; iter < end; ) { |
Victor Stinner | ae86485 | 2011-10-05 14:02:44 +0200 | [diff] [blame] | 1222 | if (*iter > *maxchar) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1223 | *maxchar = *iter; |
Victor Stinner | ae86485 | 2011-10-05 14:02:44 +0200 | [diff] [blame] | 1224 | #if SIZEOF_WCHAR_T != 2 |
| 1225 | if (*maxchar >= 0x10000) |
| 1226 | return 0; |
| 1227 | #endif |
| 1228 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1229 | #if SIZEOF_WCHAR_T == 2 |
| 1230 | if (*iter >= 0xD800 && *iter <= 0xDBFF |
| 1231 | && (iter+1) < end && iter[1] >= 0xDC00 && iter[1] <= 0xDFFF) |
| 1232 | { |
| 1233 | Py_UCS4 surrogate_val; |
| 1234 | surrogate_val = (((iter[0] & 0x3FF)<<10) |
| 1235 | | (iter[1] & 0x3FF)) + 0x10000; |
| 1236 | ++(*num_surrogates); |
| 1237 | if (surrogate_val > *maxchar) |
| 1238 | *maxchar = surrogate_val; |
| 1239 | iter += 2; |
| 1240 | } |
| 1241 | else |
| 1242 | iter++; |
| 1243 | #else |
| 1244 | iter++; |
| 1245 | #endif |
| 1246 | } |
| 1247 | return 0; |
| 1248 | } |
| 1249 | |
| 1250 | #ifdef Py_DEBUG |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 1251 | static int unicode_ready_calls = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1252 | #endif |
| 1253 | |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 1254 | static int |
| 1255 | unicode_ready(PyObject **p_obj, int replace) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1256 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1257 | PyObject *unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1258 | wchar_t *end; |
| 1259 | Py_UCS4 maxchar = 0; |
| 1260 | Py_ssize_t num_surrogates; |
| 1261 | #if SIZEOF_WCHAR_T == 2 |
| 1262 | Py_ssize_t length_wo_surrogates; |
| 1263 | #endif |
| 1264 | |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 1265 | assert(p_obj != NULL); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1266 | unicode = *p_obj; |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 1267 | |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 1268 | /* _PyUnicode_Ready() is only intended for old-style API usage where |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1269 | strings were created using _PyObject_New() and where no canonical |
| 1270 | representation (the str field) has been set yet aka strings |
| 1271 | which are not yet ready. */ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1272 | assert(_PyUnicode_CHECK(unicode)); |
| 1273 | assert(_PyUnicode_KIND(unicode) == PyUnicode_WCHAR_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1274 | assert(_PyUnicode_WSTR(unicode) != NULL); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1275 | assert(_PyUnicode_DATA_ANY(unicode) == NULL); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1276 | assert(_PyUnicode_UTF8(unicode) == NULL); |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1277 | /* Actually, it should neither be interned nor be anything else: */ |
| 1278 | assert(_PyUnicode_STATE(unicode).interned == SSTATE_NOT_INTERNED); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1279 | |
| 1280 | #ifdef Py_DEBUG |
| 1281 | ++unicode_ready_calls; |
| 1282 | #endif |
| 1283 | |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 1284 | #ifdef Py_DEBUG |
| 1285 | assert(!replace || Py_REFCNT(unicode) == 1); |
| 1286 | #else |
| 1287 | if (replace && Py_REFCNT(unicode) != 1) |
| 1288 | replace = 0; |
| 1289 | #endif |
| 1290 | if (replace) { |
| 1291 | Py_ssize_t len = _PyUnicode_WSTR_LENGTH(unicode); |
| 1292 | wchar_t *wstr = _PyUnicode_WSTR(unicode); |
| 1293 | /* Optimization for empty strings */ |
| 1294 | if (len == 0) { |
| 1295 | Py_INCREF(unicode_empty); |
| 1296 | Py_DECREF(*p_obj); |
| 1297 | *p_obj = unicode_empty; |
| 1298 | return 0; |
| 1299 | } |
| 1300 | if (len == 1 && wstr[0] < 256) { |
| 1301 | PyObject *latin1_char = get_latin1_char((unsigned char)wstr[0]); |
| 1302 | if (latin1_char == NULL) |
| 1303 | return -1; |
| 1304 | Py_DECREF(*p_obj); |
| 1305 | *p_obj = latin1_char; |
| 1306 | return 0; |
| 1307 | } |
| 1308 | } |
| 1309 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1310 | end = _PyUnicode_WSTR(unicode) + _PyUnicode_WSTR_LENGTH(unicode); |
Victor Stinner | 1722216 | 2011-09-28 22:15:37 +0200 | [diff] [blame] | 1311 | if (find_maxchar_surrogates(_PyUnicode_WSTR(unicode), end, |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1312 | &maxchar, &num_surrogates) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1313 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1314 | |
| 1315 | if (maxchar < 256) { |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1316 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC(_PyUnicode_WSTR_LENGTH(unicode) + 1); |
| 1317 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1318 | PyErr_NoMemory(); |
| 1319 | return -1; |
| 1320 | } |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1321 | _PyUnicode_CONVERT_BYTES(wchar_t, unsigned char, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1322 | _PyUnicode_WSTR(unicode), end, |
| 1323 | PyUnicode_1BYTE_DATA(unicode)); |
| 1324 | PyUnicode_1BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1325 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1326 | _PyUnicode_STATE(unicode).kind = PyUnicode_1BYTE_KIND; |
| 1327 | if (maxchar < 128) { |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1328 | _PyUnicode_STATE(unicode).ascii = 1; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1329 | _PyUnicode_UTF8(unicode) = _PyUnicode_DATA_ANY(unicode); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1330 | _PyUnicode_UTF8_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1331 | } |
| 1332 | else { |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1333 | _PyUnicode_STATE(unicode).ascii = 0; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1334 | _PyUnicode_UTF8(unicode) = NULL; |
| 1335 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1336 | } |
| 1337 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1338 | _PyUnicode_WSTR(unicode) = NULL; |
| 1339 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1340 | } |
| 1341 | /* In this case we might have to convert down from 4-byte native |
| 1342 | wchar_t to 2-byte unicode. */ |
| 1343 | else if (maxchar < 65536) { |
| 1344 | assert(num_surrogates == 0 && |
| 1345 | "FindMaxCharAndNumSurrogatePairs() messed up"); |
| 1346 | |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1347 | #if SIZEOF_WCHAR_T == 2 |
| 1348 | /* We can share representations and are done. */ |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1349 | _PyUnicode_DATA_ANY(unicode) = _PyUnicode_WSTR(unicode); |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1350 | PyUnicode_2BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1351 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1352 | _PyUnicode_STATE(unicode).kind = PyUnicode_2BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1353 | _PyUnicode_UTF8(unicode) = NULL; |
| 1354 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1355 | #else |
| 1356 | /* sizeof(wchar_t) == 4 */ |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1357 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC( |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1358 | 2 * (_PyUnicode_WSTR_LENGTH(unicode) + 1)); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1359 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1360 | PyErr_NoMemory(); |
| 1361 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1362 | } |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1363 | _PyUnicode_CONVERT_BYTES(wchar_t, Py_UCS2, |
| 1364 | _PyUnicode_WSTR(unicode), end, |
| 1365 | PyUnicode_2BYTE_DATA(unicode)); |
| 1366 | PyUnicode_2BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1367 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1368 | _PyUnicode_STATE(unicode).kind = PyUnicode_2BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1369 | _PyUnicode_UTF8(unicode) = NULL; |
| 1370 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1371 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1372 | _PyUnicode_WSTR(unicode) = NULL; |
| 1373 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1374 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1375 | } |
| 1376 | /* maxchar exeeds 16 bit, wee need 4 bytes for unicode characters */ |
| 1377 | else { |
| 1378 | #if SIZEOF_WCHAR_T == 2 |
| 1379 | /* in case the native representation is 2-bytes, we need to allocate a |
| 1380 | new normalized 4-byte version. */ |
| 1381 | length_wo_surrogates = _PyUnicode_WSTR_LENGTH(unicode) - num_surrogates; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1382 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC(4 * (length_wo_surrogates + 1)); |
| 1383 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1384 | PyErr_NoMemory(); |
| 1385 | return -1; |
| 1386 | } |
| 1387 | _PyUnicode_LENGTH(unicode) = length_wo_surrogates; |
| 1388 | _PyUnicode_STATE(unicode).kind = PyUnicode_4BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1389 | _PyUnicode_UTF8(unicode) = NULL; |
| 1390 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 126c559 | 2011-10-03 04:17:10 +0200 | [diff] [blame] | 1391 | /* unicode_convert_wchar_to_ucs4() requires a ready string */ |
| 1392 | _PyUnicode_STATE(unicode).ready = 1; |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1393 | unicode_convert_wchar_to_ucs4(_PyUnicode_WSTR(unicode), end, unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1394 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1395 | _PyUnicode_WSTR(unicode) = NULL; |
| 1396 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1397 | #else |
| 1398 | assert(num_surrogates == 0); |
| 1399 | |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1400 | _PyUnicode_DATA_ANY(unicode) = _PyUnicode_WSTR(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1401 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1402 | _PyUnicode_UTF8(unicode) = NULL; |
| 1403 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1404 | _PyUnicode_STATE(unicode).kind = PyUnicode_4BYTE_KIND; |
| 1405 | #endif |
| 1406 | PyUnicode_4BYTE_DATA(unicode)[_PyUnicode_LENGTH(unicode)] = '\0'; |
| 1407 | } |
| 1408 | _PyUnicode_STATE(unicode).ready = 1; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1409 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1410 | return 0; |
| 1411 | } |
| 1412 | |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 1413 | int |
| 1414 | _PyUnicode_ReadyReplace(PyObject **op) |
| 1415 | { |
| 1416 | return unicode_ready(op, 1); |
| 1417 | } |
| 1418 | |
| 1419 | int |
| 1420 | _PyUnicode_Ready(PyObject *op) |
| 1421 | { |
| 1422 | return unicode_ready(&op, 0); |
| 1423 | } |
| 1424 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1425 | static void |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1426 | unicode_dealloc(register PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1427 | { |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1428 | switch (PyUnicode_CHECK_INTERNED(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1429 | case SSTATE_NOT_INTERNED: |
| 1430 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1431 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1432 | case SSTATE_INTERNED_MORTAL: |
| 1433 | /* revive dead object temporarily for DelItem */ |
| 1434 | Py_REFCNT(unicode) = 3; |
| 1435 | if (PyDict_DelItem(interned, (PyObject *)unicode) != 0) |
| 1436 | Py_FatalError( |
| 1437 | "deletion of interned string failed"); |
| 1438 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1439 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1440 | case SSTATE_INTERNED_IMMORTAL: |
| 1441 | Py_FatalError("Immortal interned string died."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1442 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1443 | default: |
| 1444 | Py_FatalError("Inconsistent interned string state."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1445 | } |
| 1446 | |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 1447 | if (_PyUnicode_HAS_WSTR_MEMORY(unicode)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1448 | PyObject_DEL(_PyUnicode_WSTR(unicode)); |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 1449 | if (_PyUnicode_HAS_UTF8_MEMORY(unicode)) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1450 | PyObject_DEL(_PyUnicode_UTF8(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1451 | |
| 1452 | if (PyUnicode_IS_COMPACT(unicode)) { |
| 1453 | Py_TYPE(unicode)->tp_free((PyObject *)unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1454 | } |
| 1455 | else { |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1456 | if (_PyUnicode_DATA_ANY(unicode)) |
| 1457 | PyObject_DEL(_PyUnicode_DATA_ANY(unicode)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1458 | Py_TYPE(unicode)->tp_free((PyObject *)unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1459 | } |
| 1460 | } |
| 1461 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1462 | #ifdef Py_DEBUG |
| 1463 | static int |
| 1464 | unicode_is_singleton(PyObject *unicode) |
| 1465 | { |
| 1466 | PyASCIIObject *ascii = (PyASCIIObject *)unicode; |
| 1467 | if (unicode == unicode_empty) |
| 1468 | return 1; |
| 1469 | if (ascii->state.kind != PyUnicode_WCHAR_KIND && ascii->length == 1) |
| 1470 | { |
| 1471 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, 0); |
| 1472 | if (ch < 256 && unicode_latin1[ch] == unicode) |
| 1473 | return 1; |
| 1474 | } |
| 1475 | return 0; |
| 1476 | } |
| 1477 | #endif |
| 1478 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1479 | static int |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1480 | unicode_resizable(PyObject *unicode) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1481 | { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1482 | if (Py_REFCNT(unicode) != 1) |
| 1483 | return 0; |
| 1484 | if (PyUnicode_CHECK_INTERNED(unicode)) |
| 1485 | return 0; |
Victor Stinner | 77bb47b | 2011-10-03 20:06:05 +0200 | [diff] [blame] | 1486 | #ifdef Py_DEBUG |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1487 | /* singleton refcount is greater than 1 */ |
| 1488 | assert(!unicode_is_singleton(unicode)); |
Victor Stinner | 77bb47b | 2011-10-03 20:06:05 +0200 | [diff] [blame] | 1489 | #endif |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1490 | return 1; |
| 1491 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1492 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1493 | static int |
| 1494 | unicode_resize(PyObject **p_unicode, Py_ssize_t length) |
| 1495 | { |
| 1496 | PyObject *unicode; |
| 1497 | Py_ssize_t old_length; |
| 1498 | |
| 1499 | assert(p_unicode != NULL); |
| 1500 | unicode = *p_unicode; |
| 1501 | |
| 1502 | assert(unicode != NULL); |
| 1503 | assert(PyUnicode_Check(unicode)); |
| 1504 | assert(0 <= length); |
| 1505 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1506 | if (_PyUnicode_KIND(unicode) == PyUnicode_WCHAR_KIND) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1507 | old_length = PyUnicode_WSTR_LENGTH(unicode); |
| 1508 | else |
| 1509 | old_length = PyUnicode_GET_LENGTH(unicode); |
| 1510 | if (old_length == length) |
| 1511 | return 0; |
| 1512 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1513 | if (!unicode_resizable(unicode)) { |
| 1514 | PyObject *copy = resize_copy(unicode, length); |
| 1515 | if (copy == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1516 | return -1; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1517 | Py_DECREF(*p_unicode); |
| 1518 | *p_unicode = copy; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1519 | return 0; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1520 | } |
| 1521 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1522 | if (PyUnicode_IS_COMPACT(unicode)) { |
| 1523 | *p_unicode = resize_compact(unicode, length); |
| 1524 | if (*p_unicode == NULL) |
| 1525 | return -1; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1526 | assert(_PyUnicode_CheckConsistency(*p_unicode, 0)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1527 | return 0; |
Benjamin Peterson | 4bfce8f | 2011-10-03 19:35:07 -0400 | [diff] [blame] | 1528 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1529 | return resize_inplace(unicode, length); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1530 | } |
| 1531 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1532 | int |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1533 | PyUnicode_Resize(PyObject **p_unicode, Py_ssize_t length) |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 1534 | { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1535 | PyObject *unicode; |
| 1536 | if (p_unicode == NULL) { |
| 1537 | PyErr_BadInternalCall(); |
| 1538 | return -1; |
| 1539 | } |
| 1540 | unicode = *p_unicode; |
| 1541 | if (unicode == NULL || !PyUnicode_Check(unicode) || length < 0 |
| 1542 | || _PyUnicode_KIND(unicode) != PyUnicode_WCHAR_KIND) |
| 1543 | { |
| 1544 | PyErr_BadInternalCall(); |
| 1545 | return -1; |
| 1546 | } |
| 1547 | return unicode_resize(p_unicode, length); |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 1548 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1549 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1550 | static PyObject* |
| 1551 | get_latin1_char(unsigned char ch) |
| 1552 | { |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1553 | PyObject *unicode = unicode_latin1[ch]; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1554 | if (!unicode) { |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1555 | unicode = PyUnicode_New(1, ch); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1556 | if (!unicode) |
| 1557 | return NULL; |
| 1558 | PyUnicode_1BYTE_DATA(unicode)[0] = ch; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1559 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1560 | unicode_latin1[ch] = unicode; |
| 1561 | } |
| 1562 | Py_INCREF(unicode); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1563 | return unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1564 | } |
| 1565 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1566 | PyObject * |
| 1567 | PyUnicode_FromUnicode(const Py_UNICODE *u, Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1568 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1569 | PyObject *unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1570 | Py_UCS4 maxchar = 0; |
| 1571 | Py_ssize_t num_surrogates; |
| 1572 | |
| 1573 | if (u == NULL) |
| 1574 | return (PyObject*)_PyUnicode_New(size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1575 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1576 | /* If the Unicode data is known at construction time, we can apply |
| 1577 | some optimizations which share commonly used objects. */ |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1578 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1579 | /* Optimization for empty strings */ |
| 1580 | if (size == 0 && unicode_empty != NULL) { |
| 1581 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1582 | return unicode_empty; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1583 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1584 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1585 | /* Single character Unicode objects in the Latin-1 range are |
| 1586 | shared when using this constructor */ |
| 1587 | if (size == 1 && *u < 256) |
| 1588 | return get_latin1_char((unsigned char)*u); |
| 1589 | |
| 1590 | /* If not empty and not single character, copy the Unicode data |
| 1591 | into the new object */ |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1592 | if (find_maxchar_surrogates(u, u + size, |
| 1593 | &maxchar, &num_surrogates) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1594 | return NULL; |
| 1595 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1596 | unicode = PyUnicode_New(size - num_surrogates, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1597 | maxchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1598 | if (!unicode) |
| 1599 | return NULL; |
| 1600 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1601 | switch (PyUnicode_KIND(unicode)) { |
| 1602 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1603 | _PyUnicode_CONVERT_BYTES(Py_UNICODE, unsigned char, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1604 | u, u + size, PyUnicode_1BYTE_DATA(unicode)); |
| 1605 | break; |
| 1606 | case PyUnicode_2BYTE_KIND: |
| 1607 | #if Py_UNICODE_SIZE == 2 |
| 1608 | Py_MEMCPY(PyUnicode_2BYTE_DATA(unicode), u, size * 2); |
| 1609 | #else |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1610 | _PyUnicode_CONVERT_BYTES(Py_UNICODE, Py_UCS2, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1611 | u, u + size, PyUnicode_2BYTE_DATA(unicode)); |
| 1612 | #endif |
| 1613 | break; |
| 1614 | case PyUnicode_4BYTE_KIND: |
| 1615 | #if SIZEOF_WCHAR_T == 2 |
| 1616 | /* This is the only case which has to process surrogates, thus |
| 1617 | a simple copy loop is not enough and we need a function. */ |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1618 | unicode_convert_wchar_to_ucs4(u, u + size, unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1619 | #else |
| 1620 | assert(num_surrogates == 0); |
| 1621 | Py_MEMCPY(PyUnicode_4BYTE_DATA(unicode), u, size * 4); |
| 1622 | #endif |
| 1623 | break; |
| 1624 | default: |
| 1625 | assert(0 && "Impossible state"); |
| 1626 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1627 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1628 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1629 | return unicode; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1630 | } |
| 1631 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1632 | PyObject * |
| 1633 | PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size) |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1634 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1635 | if (size < 0) { |
| 1636 | PyErr_SetString(PyExc_SystemError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1637 | "Negative size passed to PyUnicode_FromStringAndSize"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1638 | return NULL; |
| 1639 | } |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1640 | |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1641 | /* 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] | 1642 | some optimizations which share commonly used objects. |
| 1643 | Also, this means the input must be UTF-8, so fall back to the |
| 1644 | UTF-8 decoder at the end. */ |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1645 | if (u != NULL) { |
| 1646 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1647 | /* Optimization for empty strings */ |
| 1648 | if (size == 0 && unicode_empty != NULL) { |
| 1649 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1650 | return unicode_empty; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1651 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1652 | |
| 1653 | /* Single characters are shared when using this constructor. |
| 1654 | Restrict to ASCII, since the input must be UTF-8. */ |
Victor Stinner | 9faa384 | 2011-10-23 20:06:00 +0200 | [diff] [blame^] | 1655 | if (size == 1 && (unsigned char)*u < 128) |
| 1656 | return get_latin1_char((unsigned char)*u); |
Martin v. Löwis | 9c12106 | 2007-08-05 20:26:11 +0000 | [diff] [blame] | 1657 | |
| 1658 | return PyUnicode_DecodeUTF8(u, size, NULL); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1659 | } |
| 1660 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1661 | return (PyObject *)_PyUnicode_New(size); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1662 | } |
| 1663 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1664 | PyObject * |
| 1665 | PyUnicode_FromString(const char *u) |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1666 | { |
| 1667 | size_t size = strlen(u); |
| 1668 | if (size > PY_SSIZE_T_MAX) { |
| 1669 | PyErr_SetString(PyExc_OverflowError, "input too long"); |
| 1670 | return NULL; |
| 1671 | } |
| 1672 | |
| 1673 | return PyUnicode_FromStringAndSize(u, size); |
| 1674 | } |
| 1675 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1676 | PyObject * |
| 1677 | _PyUnicode_FromId(_Py_Identifier *id) |
| 1678 | { |
| 1679 | if (!id->object) { |
| 1680 | id->object = PyUnicode_FromString(id->string); |
| 1681 | if (!id->object) |
| 1682 | return NULL; |
| 1683 | PyUnicode_InternInPlace(&id->object); |
| 1684 | assert(!id->next); |
| 1685 | id->next = static_strings; |
| 1686 | static_strings = id; |
| 1687 | } |
| 1688 | Py_INCREF(id->object); |
| 1689 | return id->object; |
| 1690 | } |
| 1691 | |
| 1692 | void |
| 1693 | _PyUnicode_ClearStaticStrings() |
| 1694 | { |
| 1695 | _Py_Identifier *i; |
| 1696 | for (i = static_strings; i; i = i->next) { |
| 1697 | Py_DECREF(i->object); |
| 1698 | i->object = NULL; |
| 1699 | i->next = NULL; |
| 1700 | } |
| 1701 | } |
| 1702 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1703 | static PyObject* |
Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 1704 | unicode_fromascii(const unsigned char* s, Py_ssize_t size) |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1705 | { |
Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 1706 | PyObject *res; |
| 1707 | #ifdef Py_DEBUG |
| 1708 | const unsigned char *p; |
| 1709 | const unsigned char *end = s + size; |
| 1710 | for (p=s; p < end; p++) { |
| 1711 | assert(*p < 128); |
| 1712 | } |
| 1713 | #endif |
Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 1714 | if (size == 1) |
| 1715 | return get_latin1_char(s[0]); |
Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 1716 | res = PyUnicode_New(size, 127); |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1717 | if (!res) |
| 1718 | return NULL; |
Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 1719 | memcpy(PyUnicode_1BYTE_DATA(res), s, size); |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1720 | return res; |
| 1721 | } |
| 1722 | |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 1723 | static Py_UCS4 |
| 1724 | kind_maxchar_limit(unsigned int kind) |
| 1725 | { |
| 1726 | switch(kind) { |
| 1727 | case PyUnicode_1BYTE_KIND: |
| 1728 | return 0x80; |
| 1729 | case PyUnicode_2BYTE_KIND: |
| 1730 | return 0x100; |
| 1731 | case PyUnicode_4BYTE_KIND: |
| 1732 | return 0x10000; |
| 1733 | default: |
| 1734 | assert(0 && "invalid kind"); |
| 1735 | return 0x10ffff; |
| 1736 | } |
| 1737 | } |
| 1738 | |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1739 | static PyObject* |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1740 | _PyUnicode_FromUCS1(const unsigned char* u, Py_ssize_t size) |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1741 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1742 | PyObject *res; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1743 | unsigned char max_char = 127; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1744 | |
| 1745 | assert(size >= 0); |
Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 1746 | if (size == 1) |
| 1747 | return get_latin1_char(u[0]); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1748 | max_char = ucs1lib_find_max_char(u, u + size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1749 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1750 | if (!res) |
| 1751 | return NULL; |
| 1752 | memcpy(PyUnicode_1BYTE_DATA(res), u, size); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1753 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1754 | return res; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1755 | } |
| 1756 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1757 | static PyObject* |
| 1758 | _PyUnicode_FromUCS2(const Py_UCS2 *u, Py_ssize_t size) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1759 | { |
| 1760 | PyObject *res; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1761 | Py_UCS2 max_char = 0; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1762 | |
| 1763 | assert(size >= 0); |
Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 1764 | if (size == 1 && u[0] < 256) |
Victor Stinner | 4e10100 | 2011-10-11 23:27:52 +0200 | [diff] [blame] | 1765 | return get_latin1_char((unsigned char)u[0]); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1766 | max_char = ucs2lib_find_max_char(u, u + size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1767 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1768 | if (!res) |
| 1769 | return NULL; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1770 | if (max_char >= 256) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1771 | memcpy(PyUnicode_2BYTE_DATA(res), u, sizeof(Py_UCS2)*size); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1772 | else { |
| 1773 | _PyUnicode_CONVERT_BYTES( |
| 1774 | Py_UCS2, Py_UCS1, u, u + size, PyUnicode_1BYTE_DATA(res)); |
| 1775 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1776 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1777 | return res; |
| 1778 | } |
| 1779 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1780 | static PyObject* |
| 1781 | _PyUnicode_FromUCS4(const Py_UCS4 *u, Py_ssize_t size) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1782 | { |
| 1783 | PyObject *res; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1784 | Py_UCS4 max_char = 0; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1785 | |
| 1786 | assert(size >= 0); |
Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 1787 | if (size == 1 && u[0] < 256) |
| 1788 | return get_latin1_char(u[0]); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1789 | max_char = ucs4lib_find_max_char(u, u + size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1790 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1791 | if (!res) |
| 1792 | return NULL; |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 1793 | if (max_char < 256) |
| 1794 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS1, u, u + size, |
| 1795 | PyUnicode_1BYTE_DATA(res)); |
| 1796 | else if (max_char < 0x10000) |
| 1797 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS2, u, u + size, |
| 1798 | PyUnicode_2BYTE_DATA(res)); |
| 1799 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1800 | memcpy(PyUnicode_4BYTE_DATA(res), u, sizeof(Py_UCS4)*size); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1801 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1802 | return res; |
| 1803 | } |
| 1804 | |
| 1805 | PyObject* |
| 1806 | PyUnicode_FromKindAndData(int kind, const void *buffer, Py_ssize_t size) |
| 1807 | { |
| 1808 | switch(kind) { |
| 1809 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1810 | return _PyUnicode_FromUCS1(buffer, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1811 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1812 | return _PyUnicode_FromUCS2(buffer, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1813 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1814 | return _PyUnicode_FromUCS4(buffer, size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1815 | default: |
| 1816 | assert(0 && "invalid kind"); |
| 1817 | PyErr_SetString(PyExc_SystemError, "invalid kind"); |
| 1818 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1819 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1820 | } |
| 1821 | |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1822 | /* Ensure that a string uses the most efficient storage, if it is not the |
| 1823 | case: create a new string with of the right kind. Write NULL into *p_unicode |
| 1824 | on error. */ |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 1825 | static void |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1826 | unicode_adjust_maxchar(PyObject **p_unicode) |
| 1827 | { |
| 1828 | PyObject *unicode, *copy; |
| 1829 | Py_UCS4 max_char; |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1830 | Py_ssize_t len; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1831 | unsigned int kind; |
| 1832 | |
| 1833 | assert(p_unicode != NULL); |
| 1834 | unicode = *p_unicode; |
| 1835 | assert(PyUnicode_IS_READY(unicode)); |
| 1836 | if (PyUnicode_IS_ASCII(unicode)) |
| 1837 | return; |
| 1838 | |
| 1839 | len = PyUnicode_GET_LENGTH(unicode); |
| 1840 | kind = PyUnicode_KIND(unicode); |
| 1841 | if (kind == PyUnicode_1BYTE_KIND) { |
| 1842 | const Py_UCS1 *u = PyUnicode_1BYTE_DATA(unicode); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1843 | max_char = ucs1lib_find_max_char(u, u + len); |
| 1844 | if (max_char >= 128) |
| 1845 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1846 | } |
| 1847 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 1848 | const Py_UCS2 *u = PyUnicode_2BYTE_DATA(unicode); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1849 | max_char = ucs2lib_find_max_char(u, u + len); |
| 1850 | if (max_char >= 256) |
| 1851 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1852 | } |
| 1853 | else { |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1854 | const Py_UCS4 *u = PyUnicode_4BYTE_DATA(unicode); |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1855 | assert(kind == PyUnicode_4BYTE_KIND); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1856 | max_char = ucs4lib_find_max_char(u, u + len); |
| 1857 | if (max_char >= 0x10000) |
| 1858 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1859 | } |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1860 | copy = PyUnicode_New(len, max_char); |
| 1861 | copy_characters(copy, 0, unicode, 0, len); |
| 1862 | Py_DECREF(unicode); |
| 1863 | *p_unicode = copy; |
| 1864 | } |
| 1865 | |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 1866 | PyObject* |
| 1867 | PyUnicode_Copy(PyObject *unicode) |
| 1868 | { |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 1869 | Py_ssize_t size; |
| 1870 | PyObject *copy; |
| 1871 | void *data; |
| 1872 | |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 1873 | if (!PyUnicode_Check(unicode)) { |
| 1874 | PyErr_BadInternalCall(); |
| 1875 | return NULL; |
| 1876 | } |
| 1877 | if (PyUnicode_READY(unicode)) |
| 1878 | return NULL; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 1879 | |
| 1880 | size = PyUnicode_GET_LENGTH(unicode); |
| 1881 | copy = PyUnicode_New(size, PyUnicode_MAX_CHAR_VALUE(unicode)); |
| 1882 | if (!copy) |
| 1883 | return NULL; |
| 1884 | assert(PyUnicode_KIND(copy) == PyUnicode_KIND(unicode)); |
| 1885 | |
| 1886 | data = PyUnicode_DATA(unicode); |
| 1887 | switch (PyUnicode_KIND(unicode)) |
| 1888 | { |
| 1889 | case PyUnicode_1BYTE_KIND: |
| 1890 | memcpy(PyUnicode_1BYTE_DATA(copy), data, size); |
| 1891 | break; |
| 1892 | case PyUnicode_2BYTE_KIND: |
| 1893 | memcpy(PyUnicode_2BYTE_DATA(copy), data, sizeof(Py_UCS2) * size); |
| 1894 | break; |
| 1895 | case PyUnicode_4BYTE_KIND: |
| 1896 | memcpy(PyUnicode_4BYTE_DATA(copy), data, sizeof(Py_UCS4) * size); |
| 1897 | break; |
| 1898 | default: |
| 1899 | assert(0); |
| 1900 | break; |
| 1901 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1902 | assert(_PyUnicode_CheckConsistency(copy, 1)); |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 1903 | return copy; |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 1904 | } |
| 1905 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1906 | |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1907 | /* Widen Unicode objects to larger buffers. Don't write terminating null |
| 1908 | character. Return NULL on error. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1909 | |
| 1910 | void* |
| 1911 | _PyUnicode_AsKind(PyObject *s, unsigned int kind) |
| 1912 | { |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1913 | Py_ssize_t len; |
| 1914 | void *result; |
| 1915 | unsigned int skind; |
| 1916 | |
| 1917 | if (PyUnicode_READY(s)) |
| 1918 | return NULL; |
| 1919 | |
| 1920 | len = PyUnicode_GET_LENGTH(s); |
| 1921 | skind = PyUnicode_KIND(s); |
| 1922 | if (skind >= kind) { |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 1923 | PyErr_SetString(PyExc_SystemError, "invalid widening attempt"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1924 | return NULL; |
| 1925 | } |
| 1926 | switch(kind) { |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1927 | case PyUnicode_2BYTE_KIND: |
| 1928 | result = PyMem_Malloc(len * sizeof(Py_UCS2)); |
| 1929 | if (!result) |
| 1930 | return PyErr_NoMemory(); |
| 1931 | assert(skind == PyUnicode_1BYTE_KIND); |
| 1932 | _PyUnicode_CONVERT_BYTES( |
| 1933 | Py_UCS1, Py_UCS2, |
| 1934 | PyUnicode_1BYTE_DATA(s), |
| 1935 | PyUnicode_1BYTE_DATA(s) + len, |
| 1936 | result); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1937 | return result; |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1938 | case PyUnicode_4BYTE_KIND: |
| 1939 | result = PyMem_Malloc(len * sizeof(Py_UCS4)); |
| 1940 | if (!result) |
| 1941 | return PyErr_NoMemory(); |
| 1942 | if (skind == PyUnicode_2BYTE_KIND) { |
| 1943 | _PyUnicode_CONVERT_BYTES( |
| 1944 | Py_UCS2, Py_UCS4, |
| 1945 | PyUnicode_2BYTE_DATA(s), |
| 1946 | PyUnicode_2BYTE_DATA(s) + len, |
| 1947 | result); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1948 | } |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1949 | else { |
| 1950 | assert(skind == PyUnicode_1BYTE_KIND); |
| 1951 | _PyUnicode_CONVERT_BYTES( |
| 1952 | Py_UCS1, Py_UCS4, |
| 1953 | PyUnicode_1BYTE_DATA(s), |
| 1954 | PyUnicode_1BYTE_DATA(s) + len, |
| 1955 | result); |
| 1956 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1957 | return result; |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1958 | default: |
| 1959 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1960 | } |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 1961 | PyErr_SetString(PyExc_SystemError, "invalid kind"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1962 | return NULL; |
| 1963 | } |
| 1964 | |
| 1965 | static Py_UCS4* |
| 1966 | as_ucs4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, |
| 1967 | int copy_null) |
| 1968 | { |
| 1969 | int kind; |
| 1970 | void *data; |
| 1971 | Py_ssize_t len, targetlen; |
| 1972 | if (PyUnicode_READY(string) == -1) |
| 1973 | return NULL; |
| 1974 | kind = PyUnicode_KIND(string); |
| 1975 | data = PyUnicode_DATA(string); |
| 1976 | len = PyUnicode_GET_LENGTH(string); |
| 1977 | targetlen = len; |
| 1978 | if (copy_null) |
| 1979 | targetlen++; |
| 1980 | if (!target) { |
| 1981 | if (PY_SSIZE_T_MAX / sizeof(Py_UCS4) < targetlen) { |
| 1982 | PyErr_NoMemory(); |
| 1983 | return NULL; |
| 1984 | } |
| 1985 | target = PyMem_Malloc(targetlen * sizeof(Py_UCS4)); |
| 1986 | if (!target) { |
| 1987 | PyErr_NoMemory(); |
| 1988 | return NULL; |
| 1989 | } |
| 1990 | } |
| 1991 | else { |
| 1992 | if (targetsize < targetlen) { |
| 1993 | PyErr_Format(PyExc_SystemError, |
| 1994 | "string is longer than the buffer"); |
| 1995 | if (copy_null && 0 < targetsize) |
| 1996 | target[0] = 0; |
| 1997 | return NULL; |
| 1998 | } |
| 1999 | } |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2000 | if (kind == PyUnicode_1BYTE_KIND) { |
| 2001 | Py_UCS1 *start = (Py_UCS1 *) data; |
| 2002 | _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] | 2003 | } |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2004 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 2005 | Py_UCS2 *start = (Py_UCS2 *) data; |
| 2006 | _PyUnicode_CONVERT_BYTES(Py_UCS2, Py_UCS4, start, start + len, target); |
| 2007 | } |
| 2008 | else { |
| 2009 | assert(kind == PyUnicode_4BYTE_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2010 | Py_MEMCPY(target, data, len * sizeof(Py_UCS4)); |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2011 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2012 | if (copy_null) |
| 2013 | target[len] = 0; |
| 2014 | return target; |
| 2015 | } |
| 2016 | |
| 2017 | Py_UCS4* |
| 2018 | PyUnicode_AsUCS4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, |
| 2019 | int copy_null) |
| 2020 | { |
| 2021 | if (target == NULL || targetsize < 1) { |
| 2022 | PyErr_BadInternalCall(); |
| 2023 | return NULL; |
| 2024 | } |
| 2025 | return as_ucs4(string, target, targetsize, copy_null); |
| 2026 | } |
| 2027 | |
| 2028 | Py_UCS4* |
| 2029 | PyUnicode_AsUCS4Copy(PyObject *string) |
| 2030 | { |
| 2031 | return as_ucs4(string, NULL, 0, 1); |
| 2032 | } |
| 2033 | |
| 2034 | #ifdef HAVE_WCHAR_H |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 2035 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2036 | PyObject * |
| 2037 | PyUnicode_FromWideChar(register const wchar_t *w, Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2038 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2039 | if (w == NULL) { |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 2040 | if (size == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2041 | return PyUnicode_New(0, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2042 | PyErr_BadInternalCall(); |
| 2043 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2044 | } |
| 2045 | |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 2046 | if (size == -1) { |
| 2047 | size = wcslen(w); |
| 2048 | } |
| 2049 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2050 | return PyUnicode_FromUnicode(w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2051 | } |
| 2052 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2053 | #endif /* HAVE_WCHAR_H */ |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 2054 | |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2055 | static void |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2056 | makefmt(char *fmt, int longflag, int longlongflag, int size_tflag, |
| 2057 | int zeropad, int width, int precision, char c) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2058 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2059 | *fmt++ = '%'; |
| 2060 | if (width) { |
| 2061 | if (zeropad) |
| 2062 | *fmt++ = '0'; |
| 2063 | fmt += sprintf(fmt, "%d", width); |
| 2064 | } |
| 2065 | if (precision) |
| 2066 | fmt += sprintf(fmt, ".%d", precision); |
| 2067 | if (longflag) |
| 2068 | *fmt++ = 'l'; |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2069 | else if (longlongflag) { |
| 2070 | /* longlongflag should only ever be nonzero on machines with |
| 2071 | HAVE_LONG_LONG defined */ |
| 2072 | #ifdef HAVE_LONG_LONG |
| 2073 | char *f = PY_FORMAT_LONG_LONG; |
| 2074 | while (*f) |
| 2075 | *fmt++ = *f++; |
| 2076 | #else |
| 2077 | /* we shouldn't ever get here */ |
| 2078 | assert(0); |
| 2079 | *fmt++ = 'l'; |
| 2080 | #endif |
| 2081 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2082 | else if (size_tflag) { |
| 2083 | char *f = PY_FORMAT_SIZE_T; |
| 2084 | while (*f) |
| 2085 | *fmt++ = *f++; |
| 2086 | } |
| 2087 | *fmt++ = c; |
| 2088 | *fmt = '\0'; |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2089 | } |
| 2090 | |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2091 | /* helper for PyUnicode_FromFormatV() */ |
| 2092 | |
| 2093 | static const char* |
| 2094 | parse_format_flags(const char *f, |
| 2095 | int *p_width, int *p_precision, |
| 2096 | int *p_longflag, int *p_longlongflag, int *p_size_tflag) |
| 2097 | { |
| 2098 | int width, precision, longflag, longlongflag, size_tflag; |
| 2099 | |
| 2100 | /* parse the width.precision part, e.g. "%2.5s" => width=2, precision=5 */ |
| 2101 | f++; |
| 2102 | width = 0; |
| 2103 | while (Py_ISDIGIT((unsigned)*f)) |
| 2104 | width = (width*10) + *f++ - '0'; |
| 2105 | precision = 0; |
| 2106 | if (*f == '.') { |
| 2107 | f++; |
| 2108 | while (Py_ISDIGIT((unsigned)*f)) |
| 2109 | precision = (precision*10) + *f++ - '0'; |
| 2110 | if (*f == '%') { |
| 2111 | /* "%.3%s" => f points to "3" */ |
| 2112 | f--; |
| 2113 | } |
| 2114 | } |
| 2115 | if (*f == '\0') { |
| 2116 | /* bogus format "%.1" => go backward, f points to "1" */ |
| 2117 | f--; |
| 2118 | } |
| 2119 | if (p_width != NULL) |
| 2120 | *p_width = width; |
| 2121 | if (p_precision != NULL) |
| 2122 | *p_precision = precision; |
| 2123 | |
| 2124 | /* Handle %ld, %lu, %lld and %llu. */ |
| 2125 | longflag = 0; |
| 2126 | longlongflag = 0; |
Victor Stinner | e7faec1 | 2011-03-02 00:01:53 +0000 | [diff] [blame] | 2127 | size_tflag = 0; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2128 | |
| 2129 | if (*f == 'l') { |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2130 | if (f[1] == 'd' || f[1] == 'u' || f[1] == 'i') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2131 | longflag = 1; |
| 2132 | ++f; |
| 2133 | } |
| 2134 | #ifdef HAVE_LONG_LONG |
| 2135 | else if (f[1] == 'l' && |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2136 | (f[2] == 'd' || f[2] == 'u' || f[2] == 'i')) { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2137 | longlongflag = 1; |
| 2138 | f += 2; |
| 2139 | } |
| 2140 | #endif |
| 2141 | } |
| 2142 | /* handle the size_t flag. */ |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2143 | 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] | 2144 | size_tflag = 1; |
| 2145 | ++f; |
| 2146 | } |
| 2147 | if (p_longflag != NULL) |
| 2148 | *p_longflag = longflag; |
| 2149 | if (p_longlongflag != NULL) |
| 2150 | *p_longlongflag = longlongflag; |
| 2151 | if (p_size_tflag != NULL) |
| 2152 | *p_size_tflag = size_tflag; |
| 2153 | return f; |
| 2154 | } |
| 2155 | |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2156 | /* maximum number of characters required for output of %ld. 21 characters |
| 2157 | allows for 64-bit integers (in decimal) and an optional sign. */ |
| 2158 | #define MAX_LONG_CHARS 21 |
| 2159 | /* maximum number of characters required for output of %lld. |
| 2160 | We need at most ceil(log10(256)*SIZEOF_LONG_LONG) digits, |
| 2161 | plus 1 for the sign. 53/22 is an upper bound for log10(256). */ |
| 2162 | #define MAX_LONG_LONG_CHARS (2 + (SIZEOF_LONG_LONG*53-1) / 22) |
| 2163 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2164 | PyObject * |
| 2165 | PyUnicode_FromFormatV(const char *format, va_list vargs) |
| 2166 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2167 | va_list count; |
| 2168 | Py_ssize_t callcount = 0; |
| 2169 | PyObject **callresults = NULL; |
| 2170 | PyObject **callresult = NULL; |
| 2171 | Py_ssize_t n = 0; |
| 2172 | int width = 0; |
| 2173 | int precision = 0; |
| 2174 | int zeropad; |
| 2175 | const char* f; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2176 | PyObject *string; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2177 | /* used by sprintf */ |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2178 | char fmt[61]; /* should be enough for %0width.precisionlld */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2179 | Py_UCS4 maxchar = 127; /* result is ASCII by default */ |
| 2180 | Py_UCS4 argmaxchar; |
| 2181 | Py_ssize_t numbersize = 0; |
| 2182 | char *numberresults = NULL; |
| 2183 | char *numberresult = NULL; |
| 2184 | Py_ssize_t i; |
| 2185 | int kind; |
| 2186 | void *data; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2187 | |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 2188 | Py_VA_COPY(count, vargs); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2189 | /* step 1: count the number of %S/%R/%A/%s format specifications |
| 2190 | * (we call PyObject_Str()/PyObject_Repr()/PyObject_ASCII()/ |
| 2191 | * 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] | 2192 | * result in an array) |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 2193 | * also estimate a upper bound for all the number formats in the string, |
| 2194 | * 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] | 2195 | * buffer before putting everything together. */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2196 | for (f = format; *f; f++) { |
| 2197 | if (*f == '%') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2198 | int longlongflag; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2199 | /* skip width or width.precision (eg. "1.2" of "%1.2f") */ |
| 2200 | f = parse_format_flags(f, &width, NULL, NULL, &longlongflag, NULL); |
| 2201 | if (*f == 's' || *f=='S' || *f=='R' || *f=='A' || *f=='V') |
| 2202 | ++callcount; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2203 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2204 | else if (*f == 'd' || *f=='u' || *f=='i' || *f=='x' || *f=='p') { |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2205 | #ifdef HAVE_LONG_LONG |
| 2206 | if (longlongflag) { |
| 2207 | if (width < MAX_LONG_LONG_CHARS) |
| 2208 | width = MAX_LONG_LONG_CHARS; |
| 2209 | } |
| 2210 | else |
| 2211 | #endif |
| 2212 | /* MAX_LONG_CHARS is enough to hold a 64-bit integer, |
| 2213 | including sign. Decimal takes the most space. This |
| 2214 | isn't enough for octal. If a width is specified we |
| 2215 | need more (which we allocate later). */ |
| 2216 | if (width < MAX_LONG_CHARS) |
| 2217 | width = MAX_LONG_CHARS; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2218 | |
| 2219 | /* account for the size + '\0' to separate numbers |
| 2220 | inside of the numberresults buffer */ |
| 2221 | numbersize += (width + 1); |
| 2222 | } |
| 2223 | } |
| 2224 | else if ((unsigned char)*f > 127) { |
| 2225 | PyErr_Format(PyExc_ValueError, |
| 2226 | "PyUnicode_FromFormatV() expects an ASCII-encoded format " |
| 2227 | "string, got a non-ASCII byte: 0x%02x", |
| 2228 | (unsigned char)*f); |
| 2229 | return NULL; |
| 2230 | } |
| 2231 | } |
| 2232 | /* step 2: allocate memory for the results of |
| 2233 | * PyObject_Str()/PyObject_Repr()/PyUnicode_DecodeUTF8() calls */ |
| 2234 | if (callcount) { |
| 2235 | callresults = PyObject_Malloc(sizeof(PyObject *) * callcount); |
| 2236 | if (!callresults) { |
| 2237 | PyErr_NoMemory(); |
| 2238 | return NULL; |
| 2239 | } |
| 2240 | callresult = callresults; |
| 2241 | } |
| 2242 | /* step 2.5: allocate memory for the results of formating numbers */ |
| 2243 | if (numbersize) { |
| 2244 | numberresults = PyObject_Malloc(numbersize); |
| 2245 | if (!numberresults) { |
| 2246 | PyErr_NoMemory(); |
| 2247 | goto fail; |
| 2248 | } |
| 2249 | numberresult = numberresults; |
| 2250 | } |
| 2251 | |
| 2252 | /* step 3: format numbers and figure out how large a buffer we need */ |
| 2253 | for (f = format; *f; f++) { |
| 2254 | if (*f == '%') { |
| 2255 | const char* p; |
| 2256 | int longflag; |
| 2257 | int longlongflag; |
| 2258 | int size_tflag; |
| 2259 | int numprinted; |
| 2260 | |
| 2261 | p = f; |
| 2262 | zeropad = (f[1] == '0'); |
| 2263 | f = parse_format_flags(f, &width, &precision, |
| 2264 | &longflag, &longlongflag, &size_tflag); |
| 2265 | switch (*f) { |
| 2266 | case 'c': |
| 2267 | { |
| 2268 | Py_UCS4 ordinal = va_arg(count, int); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2269 | maxchar = Py_MAX(maxchar, ordinal); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2270 | n++; |
| 2271 | break; |
| 2272 | } |
| 2273 | case '%': |
| 2274 | n++; |
| 2275 | break; |
| 2276 | case 'i': |
| 2277 | case 'd': |
| 2278 | makefmt(fmt, longflag, longlongflag, size_tflag, zeropad, |
| 2279 | width, precision, *f); |
| 2280 | if (longflag) |
| 2281 | numprinted = sprintf(numberresult, fmt, |
| 2282 | va_arg(count, long)); |
| 2283 | #ifdef HAVE_LONG_LONG |
| 2284 | else if (longlongflag) |
| 2285 | numprinted = sprintf(numberresult, fmt, |
| 2286 | va_arg(count, PY_LONG_LONG)); |
| 2287 | #endif |
| 2288 | else if (size_tflag) |
| 2289 | numprinted = sprintf(numberresult, fmt, |
| 2290 | va_arg(count, Py_ssize_t)); |
| 2291 | else |
| 2292 | numprinted = sprintf(numberresult, fmt, |
| 2293 | va_arg(count, int)); |
| 2294 | n += numprinted; |
| 2295 | /* advance by +1 to skip over the '\0' */ |
| 2296 | numberresult += (numprinted + 1); |
| 2297 | assert(*(numberresult - 1) == '\0'); |
| 2298 | assert(*(numberresult - 2) != '\0'); |
| 2299 | assert(numprinted >= 0); |
| 2300 | assert(numberresult <= numberresults + numbersize); |
| 2301 | break; |
| 2302 | case 'u': |
| 2303 | makefmt(fmt, longflag, longlongflag, size_tflag, zeropad, |
| 2304 | width, precision, 'u'); |
| 2305 | if (longflag) |
| 2306 | numprinted = sprintf(numberresult, fmt, |
| 2307 | va_arg(count, unsigned long)); |
| 2308 | #ifdef HAVE_LONG_LONG |
| 2309 | else if (longlongflag) |
| 2310 | numprinted = sprintf(numberresult, fmt, |
| 2311 | va_arg(count, unsigned PY_LONG_LONG)); |
| 2312 | #endif |
| 2313 | else if (size_tflag) |
| 2314 | numprinted = sprintf(numberresult, fmt, |
| 2315 | va_arg(count, size_t)); |
| 2316 | else |
| 2317 | numprinted = sprintf(numberresult, fmt, |
| 2318 | va_arg(count, unsigned int)); |
| 2319 | n += numprinted; |
| 2320 | numberresult += (numprinted + 1); |
| 2321 | assert(*(numberresult - 1) == '\0'); |
| 2322 | assert(*(numberresult - 2) != '\0'); |
| 2323 | assert(numprinted >= 0); |
| 2324 | assert(numberresult <= numberresults + numbersize); |
| 2325 | break; |
| 2326 | case 'x': |
| 2327 | makefmt(fmt, 0, 0, 0, zeropad, width, precision, 'x'); |
| 2328 | numprinted = sprintf(numberresult, fmt, va_arg(count, int)); |
| 2329 | n += numprinted; |
| 2330 | numberresult += (numprinted + 1); |
| 2331 | assert(*(numberresult - 1) == '\0'); |
| 2332 | assert(*(numberresult - 2) != '\0'); |
| 2333 | assert(numprinted >= 0); |
| 2334 | assert(numberresult <= numberresults + numbersize); |
| 2335 | break; |
| 2336 | case 'p': |
| 2337 | numprinted = sprintf(numberresult, "%p", va_arg(count, void*)); |
| 2338 | /* %p is ill-defined: ensure leading 0x. */ |
| 2339 | if (numberresult[1] == 'X') |
| 2340 | numberresult[1] = 'x'; |
| 2341 | else if (numberresult[1] != 'x') { |
| 2342 | memmove(numberresult + 2, numberresult, |
| 2343 | strlen(numberresult) + 1); |
| 2344 | numberresult[0] = '0'; |
| 2345 | numberresult[1] = 'x'; |
| 2346 | numprinted += 2; |
| 2347 | } |
| 2348 | n += numprinted; |
| 2349 | numberresult += (numprinted + 1); |
| 2350 | assert(*(numberresult - 1) == '\0'); |
| 2351 | assert(*(numberresult - 2) != '\0'); |
| 2352 | assert(numprinted >= 0); |
| 2353 | assert(numberresult <= numberresults + numbersize); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2354 | break; |
| 2355 | case 's': |
| 2356 | { |
| 2357 | /* UTF-8 */ |
Georg Brandl | 780b2a6 | 2009-05-05 09:19:59 +0000 | [diff] [blame] | 2358 | const char *s = va_arg(count, const char*); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2359 | PyObject *str = PyUnicode_DecodeUTF8(s, strlen(s), "replace"); |
| 2360 | if (!str) |
| 2361 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2362 | /* since PyUnicode_DecodeUTF8 returns already flexible |
| 2363 | unicode objects, there is no need to call ready on them */ |
| 2364 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2365 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2366 | n += PyUnicode_GET_LENGTH(str); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2367 | /* Remember the str and switch to the next slot */ |
| 2368 | *callresult++ = str; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2369 | break; |
| 2370 | } |
| 2371 | case 'U': |
| 2372 | { |
| 2373 | PyObject *obj = va_arg(count, PyObject *); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 2374 | assert(obj && _PyUnicode_CHECK(obj)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2375 | if (PyUnicode_READY(obj) == -1) |
| 2376 | goto fail; |
| 2377 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2378 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2379 | n += PyUnicode_GET_LENGTH(obj); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2380 | break; |
| 2381 | } |
| 2382 | case 'V': |
| 2383 | { |
| 2384 | PyObject *obj = va_arg(count, PyObject *); |
| 2385 | const char *str = va_arg(count, const char *); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2386 | PyObject *str_obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2387 | assert(obj || str); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 2388 | assert(!obj || _PyUnicode_CHECK(obj)); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2389 | if (obj) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2390 | if (PyUnicode_READY(obj) == -1) |
| 2391 | goto fail; |
| 2392 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2393 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2394 | n += PyUnicode_GET_LENGTH(obj); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2395 | *callresult++ = NULL; |
| 2396 | } |
| 2397 | else { |
| 2398 | str_obj = PyUnicode_DecodeUTF8(str, strlen(str), "replace"); |
| 2399 | if (!str_obj) |
| 2400 | goto fail; |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 2401 | if (PyUnicode_READY(str_obj)) { |
| 2402 | Py_DECREF(str_obj); |
| 2403 | goto fail; |
| 2404 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2405 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str_obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2406 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2407 | n += PyUnicode_GET_LENGTH(str_obj); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2408 | *callresult++ = str_obj; |
| 2409 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2410 | break; |
| 2411 | } |
| 2412 | case 'S': |
| 2413 | { |
| 2414 | PyObject *obj = va_arg(count, PyObject *); |
| 2415 | PyObject *str; |
| 2416 | assert(obj); |
| 2417 | str = PyObject_Str(obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2418 | if (!str || PyUnicode_READY(str) == -1) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2419 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2420 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2421 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2422 | n += PyUnicode_GET_LENGTH(str); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2423 | /* Remember the str and switch to the next slot */ |
| 2424 | *callresult++ = str; |
| 2425 | break; |
| 2426 | } |
| 2427 | case 'R': |
| 2428 | { |
| 2429 | PyObject *obj = va_arg(count, PyObject *); |
| 2430 | PyObject *repr; |
| 2431 | assert(obj); |
| 2432 | repr = PyObject_Repr(obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2433 | if (!repr || PyUnicode_READY(repr) == -1) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2434 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2435 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(repr); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2436 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2437 | n += PyUnicode_GET_LENGTH(repr); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2438 | /* Remember the repr and switch to the next slot */ |
| 2439 | *callresult++ = repr; |
| 2440 | break; |
| 2441 | } |
| 2442 | case 'A': |
| 2443 | { |
| 2444 | PyObject *obj = va_arg(count, PyObject *); |
| 2445 | PyObject *ascii; |
| 2446 | assert(obj); |
| 2447 | ascii = PyObject_ASCII(obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2448 | if (!ascii || PyUnicode_READY(ascii) == -1) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2449 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2450 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(ascii); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2451 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2452 | n += PyUnicode_GET_LENGTH(ascii); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2453 | /* Remember the repr and switch to the next slot */ |
| 2454 | *callresult++ = ascii; |
| 2455 | break; |
| 2456 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2457 | default: |
| 2458 | /* if we stumble upon an unknown |
| 2459 | formatting code, copy the rest of |
| 2460 | the format string to the output |
| 2461 | string. (we cannot just skip the |
| 2462 | code, since there's no way to know |
| 2463 | what's in the argument list) */ |
| 2464 | n += strlen(p); |
| 2465 | goto expand; |
| 2466 | } |
| 2467 | } else |
| 2468 | n++; |
| 2469 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2470 | expand: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2471 | /* step 4: fill the buffer */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2472 | /* Since we've analyzed how much space we need, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2473 | we don't have to resize the string. |
| 2474 | There can be no errors beyond this point. */ |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2475 | string = PyUnicode_New(n, maxchar); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2476 | if (!string) |
| 2477 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2478 | kind = PyUnicode_KIND(string); |
| 2479 | data = PyUnicode_DATA(string); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2480 | callresult = callresults; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2481 | numberresult = numberresults; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2482 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2483 | for (i = 0, f = format; *f; f++) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2484 | if (*f == '%') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2485 | const char* p; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2486 | |
| 2487 | p = f; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2488 | f = parse_format_flags(f, NULL, NULL, NULL, NULL, NULL); |
| 2489 | /* checking for == because the last argument could be a empty |
| 2490 | string, which causes i to point to end, the assert at the end of |
| 2491 | the loop */ |
| 2492 | assert(i <= PyUnicode_GET_LENGTH(string)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2493 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2494 | switch (*f) { |
| 2495 | case 'c': |
Victor Stinner | 5ed8b2c | 2011-02-21 21:13:44 +0000 | [diff] [blame] | 2496 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2497 | const int ordinal = va_arg(vargs, int); |
| 2498 | PyUnicode_WRITE(kind, data, i++, ordinal); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2499 | break; |
Victor Stinner | 5ed8b2c | 2011-02-21 21:13:44 +0000 | [diff] [blame] | 2500 | } |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2501 | case 'i': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2502 | case 'd': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2503 | case 'u': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2504 | case 'x': |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2505 | case 'p': |
| 2506 | /* unused, since we already have the result */ |
| 2507 | if (*f == 'p') |
| 2508 | (void) va_arg(vargs, void *); |
| 2509 | else |
| 2510 | (void) va_arg(vargs, int); |
| 2511 | /* extract the result from numberresults and append. */ |
| 2512 | for (; *numberresult; ++i, ++numberresult) |
| 2513 | PyUnicode_WRITE(kind, data, i, *numberresult); |
| 2514 | /* skip over the separating '\0' */ |
| 2515 | assert(*numberresult == '\0'); |
| 2516 | numberresult++; |
| 2517 | assert(numberresult <= numberresults + numbersize); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2518 | break; |
| 2519 | case 's': |
| 2520 | { |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2521 | /* unused, since we already have the result */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2522 | Py_ssize_t size; |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2523 | (void) va_arg(vargs, char *); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2524 | size = PyUnicode_GET_LENGTH(*callresult); |
| 2525 | assert(PyUnicode_KIND(*callresult) <= PyUnicode_KIND(string)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2526 | copy_characters(string, i, *callresult, 0, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2527 | i += size; |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2528 | /* We're done with the unicode()/repr() => forget it */ |
| 2529 | Py_DECREF(*callresult); |
| 2530 | /* switch to next unicode()/repr() result */ |
| 2531 | ++callresult; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2532 | break; |
| 2533 | } |
| 2534 | case 'U': |
| 2535 | { |
| 2536 | PyObject *obj = va_arg(vargs, PyObject *); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2537 | Py_ssize_t size; |
| 2538 | assert(PyUnicode_KIND(obj) <= PyUnicode_KIND(string)); |
| 2539 | size = PyUnicode_GET_LENGTH(obj); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2540 | copy_characters(string, i, obj, 0, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2541 | i += size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2542 | break; |
| 2543 | } |
| 2544 | case 'V': |
| 2545 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2546 | Py_ssize_t size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2547 | PyObject *obj = va_arg(vargs, PyObject *); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2548 | va_arg(vargs, const char *); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2549 | if (obj) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2550 | size = PyUnicode_GET_LENGTH(obj); |
| 2551 | assert(PyUnicode_KIND(obj) <= PyUnicode_KIND(string)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2552 | copy_characters(string, i, obj, 0, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2553 | i += size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2554 | } else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2555 | size = PyUnicode_GET_LENGTH(*callresult); |
| 2556 | assert(PyUnicode_KIND(*callresult) <= |
| 2557 | PyUnicode_KIND(string)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2558 | copy_characters(string, i, *callresult, 0, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2559 | i += size; |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2560 | Py_DECREF(*callresult); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2561 | } |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2562 | ++callresult; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2563 | break; |
| 2564 | } |
| 2565 | case 'S': |
| 2566 | case 'R': |
Victor Stinner | 9a90900 | 2010-10-18 20:59:24 +0000 | [diff] [blame] | 2567 | case 'A': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2568 | { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2569 | Py_ssize_t size = PyUnicode_GET_LENGTH(*callresult); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2570 | /* unused, since we already have the result */ |
| 2571 | (void) va_arg(vargs, PyObject *); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2572 | assert(PyUnicode_KIND(*callresult) <= PyUnicode_KIND(string)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2573 | copy_characters(string, i, *callresult, 0, size); |
| 2574 | i += size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2575 | /* We're done with the unicode()/repr() => forget it */ |
| 2576 | Py_DECREF(*callresult); |
| 2577 | /* switch to next unicode()/repr() result */ |
| 2578 | ++callresult; |
| 2579 | break; |
| 2580 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2581 | case '%': |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2582 | PyUnicode_WRITE(kind, data, i++, '%'); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2583 | break; |
| 2584 | default: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2585 | for (; *p; ++p, ++i) |
| 2586 | PyUnicode_WRITE(kind, data, i, *p); |
| 2587 | assert(i == PyUnicode_GET_LENGTH(string)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2588 | goto end; |
| 2589 | } |
Victor Stinner | 1205f27 | 2010-09-11 00:54:47 +0000 | [diff] [blame] | 2590 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2591 | else { |
| 2592 | assert(i < PyUnicode_GET_LENGTH(string)); |
| 2593 | PyUnicode_WRITE(kind, data, i++, *f); |
| 2594 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2595 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2596 | assert(i == PyUnicode_GET_LENGTH(string)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2597 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2598 | end: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2599 | if (callresults) |
| 2600 | PyObject_Free(callresults); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2601 | if (numberresults) |
| 2602 | PyObject_Free(numberresults); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2603 | assert(_PyUnicode_CheckConsistency(string, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2604 | return (PyObject *)string; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2605 | fail: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2606 | if (callresults) { |
| 2607 | PyObject **callresult2 = callresults; |
| 2608 | while (callresult2 < callresult) { |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2609 | Py_XDECREF(*callresult2); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2610 | ++callresult2; |
| 2611 | } |
| 2612 | PyObject_Free(callresults); |
| 2613 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2614 | if (numberresults) |
| 2615 | PyObject_Free(numberresults); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2616 | return NULL; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2617 | } |
| 2618 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2619 | PyObject * |
| 2620 | PyUnicode_FromFormat(const char *format, ...) |
| 2621 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2622 | PyObject* ret; |
| 2623 | va_list vargs; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2624 | |
| 2625 | #ifdef HAVE_STDARG_PROTOTYPES |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2626 | va_start(vargs, format); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2627 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2628 | va_start(vargs); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2629 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2630 | ret = PyUnicode_FromFormatV(format, vargs); |
| 2631 | va_end(vargs); |
| 2632 | return ret; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2633 | } |
| 2634 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2635 | #ifdef HAVE_WCHAR_H |
| 2636 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2637 | /* Helper function for PyUnicode_AsWideChar() and PyUnicode_AsWideCharString(): |
| 2638 | convert a Unicode object to a wide character string. |
| 2639 | |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2640 | - 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] | 2641 | character) required to convert the unicode object. Ignore size argument. |
| 2642 | |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2643 | - Otherwise: return the number of wide characters (excluding the null |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2644 | character) written into w. Write at most size wide characters (including |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2645 | the null character). */ |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2646 | static Py_ssize_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2647 | unicode_aswidechar(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2648 | wchar_t *w, |
| 2649 | Py_ssize_t size) |
| 2650 | { |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2651 | Py_ssize_t res; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2652 | const wchar_t *wstr; |
| 2653 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2654 | wstr = PyUnicode_AsUnicodeAndSize(unicode, &res); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2655 | if (wstr == NULL) |
| 2656 | return -1; |
| 2657 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2658 | if (w != NULL) { |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2659 | if (size > res) |
| 2660 | size = res + 1; |
| 2661 | else |
| 2662 | res = size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2663 | Py_MEMCPY(w, wstr, size * sizeof(wchar_t)); |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2664 | return res; |
| 2665 | } |
| 2666 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2667 | return res + 1; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2668 | } |
| 2669 | |
| 2670 | Py_ssize_t |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2671 | PyUnicode_AsWideChar(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2672 | wchar_t *w, |
| 2673 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2674 | { |
| 2675 | if (unicode == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2676 | PyErr_BadInternalCall(); |
| 2677 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2678 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2679 | return unicode_aswidechar(unicode, w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2680 | } |
| 2681 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2682 | wchar_t* |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 2683 | PyUnicode_AsWideCharString(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2684 | Py_ssize_t *size) |
| 2685 | { |
| 2686 | wchar_t* buffer; |
| 2687 | Py_ssize_t buflen; |
| 2688 | |
| 2689 | if (unicode == NULL) { |
| 2690 | PyErr_BadInternalCall(); |
| 2691 | return NULL; |
| 2692 | } |
| 2693 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2694 | buflen = unicode_aswidechar(unicode, NULL, 0); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2695 | if (buflen == -1) |
| 2696 | return NULL; |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2697 | if (PY_SSIZE_T_MAX / sizeof(wchar_t) < buflen) { |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2698 | PyErr_NoMemory(); |
| 2699 | return NULL; |
| 2700 | } |
| 2701 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2702 | buffer = PyMem_MALLOC(buflen * sizeof(wchar_t)); |
| 2703 | if (buffer == NULL) { |
| 2704 | PyErr_NoMemory(); |
| 2705 | return NULL; |
| 2706 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2707 | buflen = unicode_aswidechar(unicode, buffer, buflen); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2708 | if (buflen == -1) |
| 2709 | return NULL; |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2710 | if (size != NULL) |
| 2711 | *size = buflen; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2712 | return buffer; |
| 2713 | } |
| 2714 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2715 | #endif /* HAVE_WCHAR_H */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2716 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2717 | PyObject * |
| 2718 | PyUnicode_FromOrdinal(int ordinal) |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2719 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2720 | PyObject *v; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2721 | if (ordinal < 0 || ordinal > 0x10ffff) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2722 | PyErr_SetString(PyExc_ValueError, |
| 2723 | "chr() arg not in range(0x110000)"); |
| 2724 | return NULL; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2725 | } |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 2726 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2727 | if (ordinal < 256) |
| 2728 | return get_latin1_char(ordinal); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2729 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2730 | v = PyUnicode_New(1, ordinal); |
| 2731 | if (v == NULL) |
| 2732 | return NULL; |
| 2733 | PyUnicode_WRITE(PyUnicode_KIND(v), PyUnicode_DATA(v), 0, ordinal); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2734 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2735 | return v; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2736 | } |
| 2737 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2738 | PyObject * |
| 2739 | PyUnicode_FromObject(register PyObject *obj) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2740 | { |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2741 | /* XXX Perhaps we should make this API an alias of |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2742 | PyObject_Str() instead ?! */ |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2743 | if (PyUnicode_CheckExact(obj)) { |
Victor Stinner | d3a83d5 | 2011-10-01 03:09:33 +0200 | [diff] [blame] | 2744 | if (PyUnicode_READY(obj)) |
| 2745 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2746 | Py_INCREF(obj); |
| 2747 | return obj; |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2748 | } |
| 2749 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2750 | /* For a Unicode subtype that's not a Unicode object, |
| 2751 | return a true Unicode object with the same data. */ |
Victor Stinner | 2219e0a | 2011-10-01 01:16:59 +0200 | [diff] [blame] | 2752 | return PyUnicode_Copy(obj); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2753 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2754 | PyErr_Format(PyExc_TypeError, |
| 2755 | "Can't convert '%.100s' object to str implicitly", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2756 | Py_TYPE(obj)->tp_name); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2757 | return NULL; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2758 | } |
| 2759 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2760 | PyObject * |
| 2761 | PyUnicode_FromEncodedObject(register PyObject *obj, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2762 | const char *encoding, |
| 2763 | const char *errors) |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2764 | { |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2765 | Py_buffer buffer; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2766 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2767 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2768 | if (obj == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2769 | PyErr_BadInternalCall(); |
| 2770 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2771 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2772 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2773 | /* Decoding bytes objects is the most common case and should be fast */ |
| 2774 | if (PyBytes_Check(obj)) { |
| 2775 | if (PyBytes_GET_SIZE(obj) == 0) { |
| 2776 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 2777 | v = unicode_empty; |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2778 | } |
| 2779 | else { |
| 2780 | v = PyUnicode_Decode( |
| 2781 | PyBytes_AS_STRING(obj), PyBytes_GET_SIZE(obj), |
| 2782 | encoding, errors); |
| 2783 | } |
| 2784 | return v; |
| 2785 | } |
| 2786 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2787 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2788 | PyErr_SetString(PyExc_TypeError, |
| 2789 | "decoding str is not supported"); |
| 2790 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2791 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2792 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2793 | /* Retrieve a bytes buffer view through the PEP 3118 buffer interface */ |
| 2794 | if (PyObject_GetBuffer(obj, &buffer, PyBUF_SIMPLE) < 0) { |
| 2795 | PyErr_Format(PyExc_TypeError, |
| 2796 | "coercing to str: need bytes, bytearray " |
| 2797 | "or buffer-like object, %.80s found", |
| 2798 | Py_TYPE(obj)->tp_name); |
| 2799 | return NULL; |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 2800 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2801 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2802 | if (buffer.len == 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2803 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 2804 | v = unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2805 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2806 | else |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2807 | v = PyUnicode_Decode((char*) buffer.buf, buffer.len, encoding, errors); |
Marc-André Lemburg | ad7c98e | 2001-01-17 17:09:53 +0000 | [diff] [blame] | 2808 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2809 | PyBuffer_Release(&buffer); |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2810 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2811 | } |
| 2812 | |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2813 | /* Convert encoding to lower case and replace '_' with '-' in order to |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2814 | catch e.g. UTF_8. Return 0 on error (encoding is longer than lower_len-1), |
| 2815 | 1 on success. */ |
| 2816 | static int |
| 2817 | normalize_encoding(const char *encoding, |
| 2818 | char *lower, |
| 2819 | size_t lower_len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2820 | { |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2821 | const char *e; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2822 | char *l; |
| 2823 | char *l_end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2824 | |
Benjamin Peterson | 7a6debe | 2011-10-15 09:25:28 -0400 | [diff] [blame] | 2825 | if (encoding == NULL) { |
| 2826 | strcpy(lower, "utf-8"); |
| 2827 | return 1; |
| 2828 | } |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2829 | e = encoding; |
| 2830 | l = lower; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2831 | l_end = &lower[lower_len - 1]; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2832 | while (*e) { |
| 2833 | if (l == l_end) |
| 2834 | return 0; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 2835 | if (Py_ISUPPER(*e)) { |
| 2836 | *l++ = Py_TOLOWER(*e++); |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2837 | } |
| 2838 | else if (*e == '_') { |
| 2839 | *l++ = '-'; |
| 2840 | e++; |
| 2841 | } |
| 2842 | else { |
| 2843 | *l++ = *e++; |
| 2844 | } |
| 2845 | } |
| 2846 | *l = '\0'; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2847 | return 1; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2848 | } |
| 2849 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2850 | PyObject * |
| 2851 | PyUnicode_Decode(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2852 | Py_ssize_t size, |
| 2853 | const char *encoding, |
| 2854 | const char *errors) |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2855 | { |
| 2856 | PyObject *buffer = NULL, *unicode; |
| 2857 | Py_buffer info; |
| 2858 | char lower[11]; /* Enough for any encoding shortcut */ |
| 2859 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 2860 | /* Shortcuts for common default encodings */ |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2861 | if (normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2862 | if ((strcmp(lower, "utf-8") == 0) || |
| 2863 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2864 | return PyUnicode_DecodeUTF8(s, size, errors); |
| 2865 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2866 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2867 | (strcmp(lower, "iso-8859-1") == 0)) |
| 2868 | return PyUnicode_DecodeLatin1(s, size, errors); |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 2869 | #ifdef HAVE_MBCS |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2870 | else if (strcmp(lower, "mbcs") == 0) |
| 2871 | return PyUnicode_DecodeMBCS(s, size, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 2872 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2873 | else if (strcmp(lower, "ascii") == 0) |
| 2874 | return PyUnicode_DecodeASCII(s, size, errors); |
| 2875 | else if (strcmp(lower, "utf-16") == 0) |
| 2876 | return PyUnicode_DecodeUTF16(s, size, errors, 0); |
| 2877 | else if (strcmp(lower, "utf-32") == 0) |
| 2878 | return PyUnicode_DecodeUTF32(s, size, errors, 0); |
| 2879 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2880 | |
| 2881 | /* Decode via the codec registry */ |
Guido van Rossum | be801ac | 2007-10-08 03:32:34 +0000 | [diff] [blame] | 2882 | buffer = NULL; |
Antoine Pitrou | c3b3924 | 2009-01-03 16:59:18 +0000 | [diff] [blame] | 2883 | 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] | 2884 | goto onError; |
Antoine Pitrou | ee58fa4 | 2008-08-19 18:22:14 +0000 | [diff] [blame] | 2885 | buffer = PyMemoryView_FromBuffer(&info); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2886 | if (buffer == NULL) |
| 2887 | goto onError; |
| 2888 | unicode = PyCodec_Decode(buffer, encoding, errors); |
| 2889 | if (unicode == NULL) |
| 2890 | goto onError; |
| 2891 | if (!PyUnicode_Check(unicode)) { |
| 2892 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 2893 | "decoder did not return a str object (type=%.400s)", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2894 | Py_TYPE(unicode)->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2895 | Py_DECREF(unicode); |
| 2896 | goto onError; |
| 2897 | } |
| 2898 | Py_DECREF(buffer); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 2899 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 2900 | if (_PyUnicode_READY_REPLACE(&unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2901 | Py_DECREF(unicode); |
| 2902 | return NULL; |
| 2903 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 2904 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2905 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2906 | return unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2907 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2908 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2909 | Py_XDECREF(buffer); |
| 2910 | return NULL; |
| 2911 | } |
| 2912 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2913 | PyObject * |
| 2914 | PyUnicode_AsDecodedObject(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2915 | const char *encoding, |
| 2916 | const char *errors) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2917 | { |
| 2918 | PyObject *v; |
| 2919 | |
| 2920 | if (!PyUnicode_Check(unicode)) { |
| 2921 | PyErr_BadArgument(); |
| 2922 | goto onError; |
| 2923 | } |
| 2924 | |
| 2925 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2926 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2927 | |
| 2928 | /* Decode via the codec registry */ |
| 2929 | v = PyCodec_Decode(unicode, encoding, errors); |
| 2930 | if (v == NULL) |
| 2931 | goto onError; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2932 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2933 | return v; |
| 2934 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2935 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2936 | return NULL; |
| 2937 | } |
| 2938 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2939 | PyObject * |
| 2940 | PyUnicode_AsDecodedUnicode(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2941 | const char *encoding, |
| 2942 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2943 | { |
| 2944 | PyObject *v; |
| 2945 | |
| 2946 | if (!PyUnicode_Check(unicode)) { |
| 2947 | PyErr_BadArgument(); |
| 2948 | goto onError; |
| 2949 | } |
| 2950 | |
| 2951 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2952 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2953 | |
| 2954 | /* Decode via the codec registry */ |
| 2955 | v = PyCodec_Decode(unicode, encoding, errors); |
| 2956 | if (v == NULL) |
| 2957 | goto onError; |
| 2958 | if (!PyUnicode_Check(v)) { |
| 2959 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 2960 | "decoder did not return a str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2961 | Py_TYPE(v)->tp_name); |
| 2962 | Py_DECREF(v); |
| 2963 | goto onError; |
| 2964 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2965 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2966 | return v; |
| 2967 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2968 | onError: |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2969 | return NULL; |
| 2970 | } |
| 2971 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2972 | PyObject * |
| 2973 | PyUnicode_Encode(const Py_UNICODE *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2974 | Py_ssize_t size, |
| 2975 | const char *encoding, |
| 2976 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2977 | { |
| 2978 | PyObject *v, *unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2979 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2980 | unicode = PyUnicode_FromUnicode(s, size); |
| 2981 | if (unicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2982 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2983 | v = PyUnicode_AsEncodedString(unicode, encoding, errors); |
| 2984 | Py_DECREF(unicode); |
| 2985 | return v; |
| 2986 | } |
| 2987 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2988 | PyObject * |
| 2989 | PyUnicode_AsEncodedObject(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2990 | const char *encoding, |
| 2991 | const char *errors) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2992 | { |
| 2993 | PyObject *v; |
| 2994 | |
| 2995 | if (!PyUnicode_Check(unicode)) { |
| 2996 | PyErr_BadArgument(); |
| 2997 | goto onError; |
| 2998 | } |
| 2999 | |
| 3000 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3001 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3002 | |
| 3003 | /* Encode via the codec registry */ |
| 3004 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3005 | if (v == NULL) |
| 3006 | goto onError; |
| 3007 | return v; |
| 3008 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3009 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3010 | return NULL; |
| 3011 | } |
| 3012 | |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3013 | PyObject * |
| 3014 | PyUnicode_EncodeFSDefault(PyObject *unicode) |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3015 | { |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3016 | #ifdef HAVE_MBCS |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3017 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
| 3018 | PyUnicode_GET_SIZE(unicode), |
| 3019 | NULL); |
| 3020 | #elif defined(__APPLE__) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3021 | return _PyUnicode_AsUTF8String(unicode, "surrogateescape"); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3022 | #else |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 3023 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 3024 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 3025 | cannot use it to encode and decode filenames before it is loaded. Load |
| 3026 | the Python codec requires to encode at least its own filename. Use the C |
| 3027 | version of the locale codec until the codec registry is initialized and |
| 3028 | the Python codec is loaded. |
| 3029 | |
| 3030 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 3031 | cannot only rely on it: check also interp->fscodec_initialized for |
| 3032 | subinterpreters. */ |
| 3033 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3034 | return PyUnicode_AsEncodedString(unicode, |
| 3035 | Py_FileSystemDefaultEncoding, |
| 3036 | "surrogateescape"); |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 3037 | } |
| 3038 | else { |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3039 | /* locale encoding with surrogateescape */ |
| 3040 | wchar_t *wchar; |
| 3041 | char *bytes; |
| 3042 | PyObject *bytes_obj; |
Victor Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 3043 | size_t error_pos; |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3044 | |
| 3045 | wchar = PyUnicode_AsWideCharString(unicode, NULL); |
| 3046 | if (wchar == NULL) |
| 3047 | return NULL; |
Victor Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 3048 | bytes = _Py_wchar2char(wchar, &error_pos); |
| 3049 | if (bytes == NULL) { |
| 3050 | if (error_pos != (size_t)-1) { |
| 3051 | char *errmsg = strerror(errno); |
| 3052 | PyObject *exc = NULL; |
| 3053 | if (errmsg == NULL) |
| 3054 | errmsg = "Py_wchar2char() failed"; |
| 3055 | raise_encode_exception(&exc, |
| 3056 | "filesystemencoding", |
| 3057 | PyUnicode_AS_UNICODE(unicode), PyUnicode_GET_SIZE(unicode), |
| 3058 | error_pos, error_pos+1, |
| 3059 | errmsg); |
| 3060 | Py_XDECREF(exc); |
| 3061 | } |
| 3062 | else |
| 3063 | PyErr_NoMemory(); |
| 3064 | PyMem_Free(wchar); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3065 | return NULL; |
Victor Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 3066 | } |
| 3067 | PyMem_Free(wchar); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3068 | |
| 3069 | bytes_obj = PyBytes_FromString(bytes); |
| 3070 | PyMem_Free(bytes); |
| 3071 | return bytes_obj; |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 3072 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3073 | #endif |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3074 | } |
| 3075 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3076 | PyObject * |
| 3077 | PyUnicode_AsEncodedString(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3078 | const char *encoding, |
| 3079 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3080 | { |
| 3081 | PyObject *v; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3082 | char lower[11]; /* Enough for any encoding shortcut */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3083 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3084 | if (!PyUnicode_Check(unicode)) { |
| 3085 | PyErr_BadArgument(); |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3086 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3087 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3088 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3089 | /* Shortcuts for common default encodings */ |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3090 | if (normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3091 | if ((strcmp(lower, "utf-8") == 0) || |
| 3092 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 3093 | { |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3094 | if (errors == NULL || strcmp(errors, "strict") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3095 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3096 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3097 | return _PyUnicode_AsUTF8String(unicode, errors); |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 3098 | } |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3099 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3100 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3101 | (strcmp(lower, "iso-8859-1") == 0)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3102 | return _PyUnicode_AsLatin1String(unicode, errors); |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3103 | #ifdef HAVE_MBCS |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3104 | else if (strcmp(lower, "mbcs") == 0) |
| 3105 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
| 3106 | PyUnicode_GET_SIZE(unicode), |
| 3107 | errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 3108 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3109 | else if (strcmp(lower, "ascii") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3110 | return _PyUnicode_AsASCIIString(unicode, errors); |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3111 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3112 | |
| 3113 | /* Encode via the codec registry */ |
| 3114 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3115 | if (v == NULL) |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3116 | return NULL; |
| 3117 | |
| 3118 | /* The normal path */ |
| 3119 | if (PyBytes_Check(v)) |
| 3120 | return v; |
| 3121 | |
| 3122 | /* 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] | 3123 | if (PyByteArray_Check(v)) { |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 3124 | int error; |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3125 | PyObject *b; |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 3126 | |
| 3127 | error = PyErr_WarnFormat(PyExc_RuntimeWarning, 1, |
| 3128 | "encoder %s returned bytearray instead of bytes", |
| 3129 | encoding); |
| 3130 | if (error) { |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3131 | Py_DECREF(v); |
| 3132 | return NULL; |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3133 | } |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3134 | |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3135 | b = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(v), Py_SIZE(v)); |
| 3136 | Py_DECREF(v); |
| 3137 | return b; |
| 3138 | } |
| 3139 | |
| 3140 | PyErr_Format(PyExc_TypeError, |
| 3141 | "encoder did not return a bytes object (type=%.400s)", |
| 3142 | Py_TYPE(v)->tp_name); |
| 3143 | Py_DECREF(v); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3144 | return NULL; |
| 3145 | } |
| 3146 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3147 | PyObject * |
| 3148 | PyUnicode_AsEncodedUnicode(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3149 | const char *encoding, |
| 3150 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3151 | { |
| 3152 | PyObject *v; |
| 3153 | |
| 3154 | if (!PyUnicode_Check(unicode)) { |
| 3155 | PyErr_BadArgument(); |
| 3156 | goto onError; |
| 3157 | } |
| 3158 | |
| 3159 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3160 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3161 | |
| 3162 | /* Encode via the codec registry */ |
| 3163 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3164 | if (v == NULL) |
| 3165 | goto onError; |
| 3166 | if (!PyUnicode_Check(v)) { |
| 3167 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3168 | "encoder did not return an str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3169 | Py_TYPE(v)->tp_name); |
| 3170 | Py_DECREF(v); |
| 3171 | goto onError; |
| 3172 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3173 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3174 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3175 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3176 | return NULL; |
| 3177 | } |
| 3178 | |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3179 | PyObject* |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3180 | PyUnicode_DecodeFSDefault(const char *s) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3181 | Py_ssize_t size = (Py_ssize_t)strlen(s); |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3182 | return PyUnicode_DecodeFSDefaultAndSize(s, size); |
| 3183 | } |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3184 | |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3185 | PyObject* |
| 3186 | PyUnicode_DecodeFSDefaultAndSize(const char *s, Py_ssize_t size) |
| 3187 | { |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3188 | #ifdef HAVE_MBCS |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3189 | return PyUnicode_DecodeMBCS(s, size, NULL); |
| 3190 | #elif defined(__APPLE__) |
| 3191 | return PyUnicode_DecodeUTF8(s, size, "surrogateescape"); |
| 3192 | #else |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 3193 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 3194 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 3195 | cannot use it to encode and decode filenames before it is loaded. Load |
| 3196 | the Python codec requires to encode at least its own filename. Use the C |
| 3197 | version of the locale codec until the codec registry is initialized and |
| 3198 | the Python codec is loaded. |
| 3199 | |
| 3200 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 3201 | cannot only rely on it: check also interp->fscodec_initialized for |
| 3202 | subinterpreters. */ |
| 3203 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3204 | return PyUnicode_Decode(s, size, |
| 3205 | Py_FileSystemDefaultEncoding, |
Victor Stinner | b9a20ad | 2010-04-30 16:37:52 +0000 | [diff] [blame] | 3206 | "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3207 | } |
| 3208 | else { |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3209 | /* locale encoding with surrogateescape */ |
| 3210 | wchar_t *wchar; |
| 3211 | PyObject *unicode; |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 3212 | size_t len; |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3213 | |
| 3214 | if (s[size] != '\0' || size != strlen(s)) { |
| 3215 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
| 3216 | return NULL; |
| 3217 | } |
| 3218 | |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 3219 | wchar = _Py_char2wchar(s, &len); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3220 | if (wchar == NULL) |
Victor Stinner | d5af0a5 | 2010-11-08 23:34:29 +0000 | [diff] [blame] | 3221 | return PyErr_NoMemory(); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3222 | |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 3223 | unicode = PyUnicode_FromWideChar(wchar, len); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3224 | PyMem_Free(wchar); |
| 3225 | return unicode; |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3226 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3227 | #endif |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3228 | } |
| 3229 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3230 | |
| 3231 | int |
| 3232 | PyUnicode_FSConverter(PyObject* arg, void* addr) |
| 3233 | { |
| 3234 | PyObject *output = NULL; |
| 3235 | Py_ssize_t size; |
| 3236 | void *data; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3237 | if (arg == NULL) { |
| 3238 | Py_DECREF(*(PyObject**)addr); |
| 3239 | return 1; |
| 3240 | } |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 3241 | if (PyBytes_Check(arg)) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3242 | output = arg; |
| 3243 | Py_INCREF(output); |
| 3244 | } |
| 3245 | else { |
| 3246 | arg = PyUnicode_FromObject(arg); |
| 3247 | if (!arg) |
| 3248 | return 0; |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3249 | output = PyUnicode_EncodeFSDefault(arg); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3250 | Py_DECREF(arg); |
| 3251 | if (!output) |
| 3252 | return 0; |
| 3253 | if (!PyBytes_Check(output)) { |
| 3254 | Py_DECREF(output); |
| 3255 | PyErr_SetString(PyExc_TypeError, "encoder failed to return bytes"); |
| 3256 | return 0; |
| 3257 | } |
| 3258 | } |
Victor Stinner | 0ea2a46 | 2010-04-30 00:22:08 +0000 | [diff] [blame] | 3259 | size = PyBytes_GET_SIZE(output); |
| 3260 | data = PyBytes_AS_STRING(output); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3261 | if (size != strlen(data)) { |
Benjamin Peterson | 7a6b44a | 2011-08-18 13:51:47 -0500 | [diff] [blame] | 3262 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3263 | Py_DECREF(output); |
| 3264 | return 0; |
| 3265 | } |
| 3266 | *(PyObject**)addr = output; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3267 | return Py_CLEANUP_SUPPORTED; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3268 | } |
| 3269 | |
| 3270 | |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3271 | int |
| 3272 | PyUnicode_FSDecoder(PyObject* arg, void* addr) |
| 3273 | { |
| 3274 | PyObject *output = NULL; |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3275 | if (arg == NULL) { |
| 3276 | Py_DECREF(*(PyObject**)addr); |
| 3277 | return 1; |
| 3278 | } |
| 3279 | if (PyUnicode_Check(arg)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3280 | if (PyUnicode_READY(arg)) |
| 3281 | return 0; |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3282 | output = arg; |
| 3283 | Py_INCREF(output); |
| 3284 | } |
| 3285 | else { |
| 3286 | arg = PyBytes_FromObject(arg); |
| 3287 | if (!arg) |
| 3288 | return 0; |
| 3289 | output = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AS_STRING(arg), |
| 3290 | PyBytes_GET_SIZE(arg)); |
| 3291 | Py_DECREF(arg); |
| 3292 | if (!output) |
| 3293 | return 0; |
| 3294 | if (!PyUnicode_Check(output)) { |
| 3295 | Py_DECREF(output); |
| 3296 | PyErr_SetString(PyExc_TypeError, "decoder failed to return unicode"); |
| 3297 | return 0; |
| 3298 | } |
| 3299 | } |
Victor Stinner | 065836e | 2011-10-27 01:56:33 +0200 | [diff] [blame] | 3300 | if (PyUnicode_READY(output) < 0) { |
| 3301 | Py_DECREF(output); |
| 3302 | return 0; |
| 3303 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3304 | if (findchar(PyUnicode_DATA(output), PyUnicode_KIND(output), |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 3305 | PyUnicode_GET_LENGTH(output), 0, 1) >= 0) { |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3306 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
| 3307 | Py_DECREF(output); |
| 3308 | return 0; |
| 3309 | } |
| 3310 | *(PyObject**)addr = output; |
| 3311 | return Py_CLEANUP_SUPPORTED; |
| 3312 | } |
| 3313 | |
| 3314 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3315 | char* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3316 | PyUnicode_AsUTF8AndSize(PyObject *unicode, Py_ssize_t *psize) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3317 | { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 3318 | PyObject *bytes; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3319 | |
Neal Norwitz | e0a0a6e | 2007-08-25 01:04:21 +0000 | [diff] [blame] | 3320 | if (!PyUnicode_Check(unicode)) { |
| 3321 | PyErr_BadArgument(); |
| 3322 | return NULL; |
| 3323 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3324 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3325 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3326 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3327 | if (PyUnicode_UTF8(unicode) == NULL) { |
| 3328 | assert(!PyUnicode_IS_COMPACT_ASCII(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3329 | bytes = _PyUnicode_AsUTF8String(unicode, "strict"); |
| 3330 | if (bytes == NULL) |
| 3331 | return NULL; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3332 | _PyUnicode_UTF8(unicode) = PyObject_MALLOC(PyBytes_GET_SIZE(bytes) + 1); |
| 3333 | if (_PyUnicode_UTF8(unicode) == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3334 | Py_DECREF(bytes); |
| 3335 | return NULL; |
| 3336 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3337 | _PyUnicode_UTF8_LENGTH(unicode) = PyBytes_GET_SIZE(bytes); |
| 3338 | Py_MEMCPY(_PyUnicode_UTF8(unicode), |
| 3339 | PyBytes_AS_STRING(bytes), |
| 3340 | _PyUnicode_UTF8_LENGTH(unicode) + 1); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3341 | Py_DECREF(bytes); |
| 3342 | } |
| 3343 | |
| 3344 | if (psize) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3345 | *psize = PyUnicode_UTF8_LENGTH(unicode); |
| 3346 | return PyUnicode_UTF8(unicode); |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 3347 | } |
| 3348 | |
| 3349 | char* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3350 | PyUnicode_AsUTF8(PyObject *unicode) |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 3351 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3352 | return PyUnicode_AsUTF8AndSize(unicode, NULL); |
| 3353 | } |
| 3354 | |
| 3355 | #ifdef Py_DEBUG |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 3356 | static int unicode_as_unicode_calls = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3357 | #endif |
| 3358 | |
| 3359 | |
| 3360 | Py_UNICODE * |
| 3361 | PyUnicode_AsUnicodeAndSize(PyObject *unicode, Py_ssize_t *size) |
| 3362 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3363 | const unsigned char *one_byte; |
| 3364 | #if SIZEOF_WCHAR_T == 4 |
| 3365 | const Py_UCS2 *two_bytes; |
| 3366 | #else |
| 3367 | const Py_UCS4 *four_bytes; |
| 3368 | const Py_UCS4 *ucs4_end; |
| 3369 | Py_ssize_t num_surrogates; |
| 3370 | #endif |
| 3371 | wchar_t *w; |
| 3372 | wchar_t *wchar_end; |
| 3373 | |
| 3374 | if (!PyUnicode_Check(unicode)) { |
| 3375 | PyErr_BadArgument(); |
| 3376 | return NULL; |
| 3377 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3378 | if (_PyUnicode_WSTR(unicode) == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3379 | /* Non-ASCII compact unicode object */ |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3380 | assert(_PyUnicode_KIND(unicode) != 0); |
| 3381 | assert(PyUnicode_IS_READY(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3382 | |
| 3383 | #ifdef Py_DEBUG |
| 3384 | ++unicode_as_unicode_calls; |
| 3385 | #endif |
| 3386 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3387 | if (PyUnicode_KIND(unicode) == PyUnicode_4BYTE_KIND) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3388 | #if SIZEOF_WCHAR_T == 2 |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3389 | four_bytes = PyUnicode_4BYTE_DATA(unicode); |
| 3390 | ucs4_end = four_bytes + _PyUnicode_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3391 | num_surrogates = 0; |
| 3392 | |
| 3393 | for (; four_bytes < ucs4_end; ++four_bytes) { |
| 3394 | if (*four_bytes > 0xFFFF) |
| 3395 | ++num_surrogates; |
| 3396 | } |
| 3397 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3398 | _PyUnicode_WSTR(unicode) = (wchar_t *) PyObject_MALLOC( |
| 3399 | sizeof(wchar_t) * (_PyUnicode_LENGTH(unicode) + 1 + num_surrogates)); |
| 3400 | if (!_PyUnicode_WSTR(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3401 | PyErr_NoMemory(); |
| 3402 | return NULL; |
| 3403 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3404 | _PyUnicode_WSTR_LENGTH(unicode) = _PyUnicode_LENGTH(unicode) + num_surrogates; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3405 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3406 | w = _PyUnicode_WSTR(unicode); |
| 3407 | wchar_end = w + _PyUnicode_WSTR_LENGTH(unicode); |
| 3408 | four_bytes = PyUnicode_4BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3409 | for (; four_bytes < ucs4_end; ++four_bytes, ++w) { |
| 3410 | if (*four_bytes > 0xFFFF) { |
| 3411 | /* encode surrogate pair in this case */ |
| 3412 | *w++ = 0xD800 | ((*four_bytes - 0x10000) >> 10); |
| 3413 | *w = 0xDC00 | ((*four_bytes - 0x10000) & 0x3FF); |
| 3414 | } |
| 3415 | else |
| 3416 | *w = *four_bytes; |
| 3417 | |
| 3418 | if (w > wchar_end) { |
| 3419 | assert(0 && "Miscalculated string end"); |
| 3420 | } |
| 3421 | } |
| 3422 | *w = 0; |
| 3423 | #else |
| 3424 | /* sizeof(wchar_t) == 4 */ |
| 3425 | Py_FatalError("Impossible unicode object state, wstr and str " |
| 3426 | "should share memory already."); |
| 3427 | return NULL; |
| 3428 | #endif |
| 3429 | } |
| 3430 | else { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3431 | _PyUnicode_WSTR(unicode) = (wchar_t *) PyObject_MALLOC(sizeof(wchar_t) * |
| 3432 | (_PyUnicode_LENGTH(unicode) + 1)); |
| 3433 | if (!_PyUnicode_WSTR(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3434 | PyErr_NoMemory(); |
| 3435 | return NULL; |
| 3436 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3437 | if (!PyUnicode_IS_COMPACT_ASCII(unicode)) |
| 3438 | _PyUnicode_WSTR_LENGTH(unicode) = _PyUnicode_LENGTH(unicode); |
| 3439 | w = _PyUnicode_WSTR(unicode); |
| 3440 | wchar_end = w + _PyUnicode_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3441 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3442 | if (PyUnicode_KIND(unicode) == PyUnicode_1BYTE_KIND) { |
| 3443 | one_byte = PyUnicode_1BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3444 | for (; w < wchar_end; ++one_byte, ++w) |
| 3445 | *w = *one_byte; |
| 3446 | /* null-terminate the wstr */ |
| 3447 | *w = 0; |
| 3448 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3449 | else if (PyUnicode_KIND(unicode) == PyUnicode_2BYTE_KIND) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3450 | #if SIZEOF_WCHAR_T == 4 |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3451 | two_bytes = PyUnicode_2BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3452 | for (; w < wchar_end; ++two_bytes, ++w) |
| 3453 | *w = *two_bytes; |
| 3454 | /* null-terminate the wstr */ |
| 3455 | *w = 0; |
| 3456 | #else |
| 3457 | /* sizeof(wchar_t) == 2 */ |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3458 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 3459 | _PyUnicode_WSTR(unicode) = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3460 | Py_FatalError("Impossible unicode object state, wstr " |
| 3461 | "and str should share memory already."); |
| 3462 | return NULL; |
| 3463 | #endif |
| 3464 | } |
| 3465 | else { |
| 3466 | assert(0 && "This should never happen."); |
| 3467 | } |
| 3468 | } |
| 3469 | } |
| 3470 | if (size != NULL) |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3471 | *size = PyUnicode_WSTR_LENGTH(unicode); |
| 3472 | return _PyUnicode_WSTR(unicode); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3473 | } |
| 3474 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3475 | Py_UNICODE * |
| 3476 | PyUnicode_AsUnicode(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3477 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3478 | return PyUnicode_AsUnicodeAndSize(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3479 | } |
| 3480 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3481 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3482 | Py_ssize_t |
| 3483 | PyUnicode_GetSize(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3484 | { |
| 3485 | if (!PyUnicode_Check(unicode)) { |
| 3486 | PyErr_BadArgument(); |
| 3487 | goto onError; |
| 3488 | } |
| 3489 | return PyUnicode_GET_SIZE(unicode); |
| 3490 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3491 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3492 | return -1; |
| 3493 | } |
| 3494 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3495 | Py_ssize_t |
| 3496 | PyUnicode_GetLength(PyObject *unicode) |
| 3497 | { |
Victor Stinner | 5a706cf | 2011-10-02 00:36:53 +0200 | [diff] [blame] | 3498 | if (!PyUnicode_Check(unicode) || PyUnicode_READY(unicode) == -1) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3499 | PyErr_BadArgument(); |
| 3500 | return -1; |
| 3501 | } |
| 3502 | |
| 3503 | return PyUnicode_GET_LENGTH(unicode); |
| 3504 | } |
| 3505 | |
| 3506 | Py_UCS4 |
| 3507 | PyUnicode_ReadChar(PyObject *unicode, Py_ssize_t index) |
| 3508 | { |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 3509 | if (!PyUnicode_Check(unicode) || PyUnicode_READY(unicode) == -1) { |
| 3510 | PyErr_BadArgument(); |
| 3511 | return (Py_UCS4)-1; |
| 3512 | } |
| 3513 | if (index < 0 || index >= _PyUnicode_LENGTH(unicode)) { |
| 3514 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3515 | return (Py_UCS4)-1; |
| 3516 | } |
| 3517 | return PyUnicode_READ_CHAR(unicode, index); |
| 3518 | } |
| 3519 | |
| 3520 | int |
| 3521 | PyUnicode_WriteChar(PyObject *unicode, Py_ssize_t index, Py_UCS4 ch) |
| 3522 | { |
| 3523 | if (!PyUnicode_Check(unicode) || !PyUnicode_IS_COMPACT(unicode)) { |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 3524 | PyErr_BadArgument(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3525 | return -1; |
| 3526 | } |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 3527 | if (index < 0 || index >= _PyUnicode_LENGTH(unicode)) { |
| 3528 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 3529 | return -1; |
| 3530 | } |
| 3531 | if (_PyUnicode_Dirty(unicode)) |
| 3532 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3533 | PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode), |
| 3534 | index, ch); |
| 3535 | return 0; |
| 3536 | } |
| 3537 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3538 | const char * |
| 3539 | PyUnicode_GetDefaultEncoding(void) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3540 | { |
Victor Stinner | 42cb462 | 2010-09-01 19:39:01 +0000 | [diff] [blame] | 3541 | return "utf-8"; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3542 | } |
| 3543 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 3544 | /* create or adjust a UnicodeDecodeError */ |
| 3545 | static void |
| 3546 | make_decode_exception(PyObject **exceptionObject, |
| 3547 | const char *encoding, |
| 3548 | const char *input, Py_ssize_t length, |
| 3549 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 3550 | const char *reason) |
| 3551 | { |
| 3552 | if (*exceptionObject == NULL) { |
| 3553 | *exceptionObject = PyUnicodeDecodeError_Create( |
| 3554 | encoding, input, length, startpos, endpos, reason); |
| 3555 | } |
| 3556 | else { |
| 3557 | if (PyUnicodeDecodeError_SetStart(*exceptionObject, startpos)) |
| 3558 | goto onError; |
| 3559 | if (PyUnicodeDecodeError_SetEnd(*exceptionObject, endpos)) |
| 3560 | goto onError; |
| 3561 | if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) |
| 3562 | goto onError; |
| 3563 | } |
| 3564 | return; |
| 3565 | |
| 3566 | onError: |
| 3567 | Py_DECREF(*exceptionObject); |
| 3568 | *exceptionObject = NULL; |
| 3569 | } |
| 3570 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3571 | /* error handling callback helper: |
| 3572 | build arguments, call the callback and check the arguments, |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 3573 | if no exception occurred, copy the replacement to the output |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3574 | and adjust various state variables. |
| 3575 | return 0 on success, -1 on error |
| 3576 | */ |
| 3577 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3578 | static int |
| 3579 | unicode_decode_call_errorhandler(const char *errors, PyObject **errorHandler, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3580 | const char *encoding, const char *reason, |
| 3581 | const char **input, const char **inend, Py_ssize_t *startinpos, |
| 3582 | Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
| 3583 | PyUnicodeObject **output, Py_ssize_t *outpos, Py_UNICODE **outptr) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3584 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3585 | 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] | 3586 | |
| 3587 | PyObject *restuple = NULL; |
| 3588 | PyObject *repunicode = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3589 | Py_ssize_t outsize = PyUnicode_GET_SIZE(*output); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3590 | Py_ssize_t insize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3591 | Py_ssize_t requiredsize; |
| 3592 | Py_ssize_t newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3593 | const Py_UNICODE *repptr; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3594 | PyObject *inputobj = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3595 | Py_ssize_t repsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3596 | int res = -1; |
| 3597 | |
| 3598 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3599 | *errorHandler = PyCodec_LookupError(errors); |
| 3600 | if (*errorHandler == NULL) |
| 3601 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3602 | } |
| 3603 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 3604 | make_decode_exception(exceptionObject, |
| 3605 | encoding, |
| 3606 | *input, *inend - *input, |
| 3607 | *startinpos, *endinpos, |
| 3608 | reason); |
| 3609 | if (*exceptionObject == NULL) |
| 3610 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3611 | |
| 3612 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 3613 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3614 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3615 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 3616 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3617 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3618 | } |
| 3619 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3620 | goto onError; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3621 | |
| 3622 | /* Copy back the bytes variables, which might have been modified by the |
| 3623 | callback */ |
| 3624 | inputobj = PyUnicodeDecodeError_GetObject(*exceptionObject); |
| 3625 | if (!inputobj) |
| 3626 | goto onError; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3627 | if (!PyBytes_Check(inputobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3628 | PyErr_Format(PyExc_TypeError, "exception attribute object must be bytes"); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3629 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3630 | *input = PyBytes_AS_STRING(inputobj); |
| 3631 | insize = PyBytes_GET_SIZE(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3632 | *inend = *input + insize; |
Walter Dörwald | 36f938f | 2007-08-10 10:11:43 +0000 | [diff] [blame] | 3633 | /* we can DECREF safely, as the exception has another reference, |
| 3634 | so the object won't go away. */ |
| 3635 | Py_DECREF(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3636 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3637 | if (newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3638 | newpos = insize+newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 3639 | if (newpos<0 || newpos>insize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3640 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", newpos); |
| 3641 | goto onError; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 3642 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3643 | |
| 3644 | /* need more space? (at least enough for what we |
| 3645 | have+the replacement+the rest of the string (starting |
| 3646 | at the new input position), so we won't have to check space |
| 3647 | when there are no errors in the rest of the string) */ |
| 3648 | repptr = PyUnicode_AS_UNICODE(repunicode); |
| 3649 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 3650 | requiredsize = *outpos + repsize + insize-newpos; |
| 3651 | if (requiredsize > outsize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3652 | if (requiredsize<2*outsize) |
| 3653 | requiredsize = 2*outsize; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 3654 | if (PyUnicode_Resize((PyObject**)output, requiredsize) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3655 | goto onError; |
| 3656 | *outptr = PyUnicode_AS_UNICODE(*output) + *outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3657 | } |
| 3658 | *endinpos = newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3659 | *inptr = *input + newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3660 | Py_UNICODE_COPY(*outptr, repptr, repsize); |
| 3661 | *outptr += repsize; |
| 3662 | *outpos += repsize; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3663 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3664 | /* we made it! */ |
| 3665 | res = 0; |
| 3666 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3667 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3668 | Py_XDECREF(restuple); |
| 3669 | return res; |
| 3670 | } |
| 3671 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3672 | /* --- UTF-7 Codec -------------------------------------------------------- */ |
| 3673 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3674 | /* See RFC2152 for details. We encode conservatively and decode liberally. */ |
| 3675 | |
| 3676 | /* Three simple macros defining base-64. */ |
| 3677 | |
| 3678 | /* Is c a base-64 character? */ |
| 3679 | |
| 3680 | #define IS_BASE64(c) \ |
| 3681 | (((c) >= 'A' && (c) <= 'Z') || \ |
| 3682 | ((c) >= 'a' && (c) <= 'z') || \ |
| 3683 | ((c) >= '0' && (c) <= '9') || \ |
| 3684 | (c) == '+' || (c) == '/') |
| 3685 | |
| 3686 | /* given that c is a base-64 character, what is its base-64 value? */ |
| 3687 | |
| 3688 | #define FROM_BASE64(c) \ |
| 3689 | (((c) >= 'A' && (c) <= 'Z') ? (c) - 'A' : \ |
| 3690 | ((c) >= 'a' && (c) <= 'z') ? (c) - 'a' + 26 : \ |
| 3691 | ((c) >= '0' && (c) <= '9') ? (c) - '0' + 52 : \ |
| 3692 | (c) == '+' ? 62 : 63) |
| 3693 | |
| 3694 | /* What is the base-64 character of the bottom 6 bits of n? */ |
| 3695 | |
| 3696 | #define TO_BASE64(n) \ |
| 3697 | ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) |
| 3698 | |
| 3699 | /* DECODE_DIRECT: this byte encountered in a UTF-7 string should be |
| 3700 | * decoded as itself. We are permissive on decoding; the only ASCII |
| 3701 | * byte not decoding to itself is the + which begins a base64 |
| 3702 | * string. */ |
| 3703 | |
| 3704 | #define DECODE_DIRECT(c) \ |
| 3705 | ((c) <= 127 && (c) != '+') |
| 3706 | |
| 3707 | /* The UTF-7 encoder treats ASCII characters differently according to |
| 3708 | * whether they are Set D, Set O, Whitespace, or special (i.e. none of |
| 3709 | * the above). See RFC2152. This array identifies these different |
| 3710 | * sets: |
| 3711 | * 0 : "Set D" |
| 3712 | * alphanumeric and '(),-./:? |
| 3713 | * 1 : "Set O" |
| 3714 | * !"#$%&*;<=>@[]^_`{|} |
| 3715 | * 2 : "whitespace" |
| 3716 | * ht nl cr sp |
| 3717 | * 3 : special (must be base64 encoded) |
| 3718 | * everything else (i.e. +\~ and non-printing codes 0-8 11-12 14-31 127) |
| 3719 | */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3720 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3721 | static |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3722 | char utf7_category[128] = { |
| 3723 | /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */ |
| 3724 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 3, 3, 2, 3, 3, |
| 3725 | /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */ |
| 3726 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 3727 | /* sp ! " # $ % & ' ( ) * + , - . / */ |
| 3728 | 2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 3, 0, 0, 0, 0, |
| 3729 | /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */ |
| 3730 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, |
| 3731 | /* @ A B C D E F G H I J K L M N O */ |
| 3732 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 3733 | /* P Q R S T U V W X Y Z [ \ ] ^ _ */ |
| 3734 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 1, 1, 1, |
| 3735 | /* ` a b c d e f g h i j k l m n o */ |
| 3736 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 3737 | /* p q r s t u v w x y z { | } ~ del */ |
| 3738 | 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] | 3739 | }; |
| 3740 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3741 | /* ENCODE_DIRECT: this character should be encoded as itself. The |
| 3742 | * answer depends on whether we are encoding set O as itself, and also |
| 3743 | * on whether we are encoding whitespace as itself. RFC2152 makes it |
| 3744 | * clear that the answers to these questions vary between |
| 3745 | * applications, so this code needs to be flexible. */ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 3746 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3747 | #define ENCODE_DIRECT(c, directO, directWS) \ |
| 3748 | ((c) < 128 && (c) > 0 && \ |
| 3749 | ((utf7_category[(c)] == 0) || \ |
| 3750 | (directWS && (utf7_category[(c)] == 2)) || \ |
| 3751 | (directO && (utf7_category[(c)] == 1)))) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3752 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3753 | PyObject * |
| 3754 | PyUnicode_DecodeUTF7(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3755 | Py_ssize_t size, |
| 3756 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3757 | { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3758 | return PyUnicode_DecodeUTF7Stateful(s, size, errors, NULL); |
| 3759 | } |
| 3760 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3761 | /* The decoder. The only state we preserve is our read position, |
| 3762 | * i.e. how many characters we have consumed. So if we end in the |
| 3763 | * middle of a shift sequence we have to back off the read position |
| 3764 | * and the output to the beginning of the sequence, otherwise we lose |
| 3765 | * all the shift state (seen bits, number of bits seen, high |
| 3766 | * surrogate). */ |
| 3767 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3768 | PyObject * |
| 3769 | PyUnicode_DecodeUTF7Stateful(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3770 | Py_ssize_t size, |
| 3771 | const char *errors, |
| 3772 | Py_ssize_t *consumed) |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3773 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3774 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3775 | Py_ssize_t startinpos; |
| 3776 | Py_ssize_t endinpos; |
| 3777 | Py_ssize_t outpos; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3778 | const char *e; |
| 3779 | PyUnicodeObject *unicode; |
| 3780 | Py_UNICODE *p; |
| 3781 | const char *errmsg = ""; |
| 3782 | int inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3783 | Py_UNICODE *shiftOutStart; |
| 3784 | unsigned int base64bits = 0; |
| 3785 | unsigned long base64buffer = 0; |
| 3786 | Py_UNICODE surrogate = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3787 | PyObject *errorHandler = NULL; |
| 3788 | PyObject *exc = NULL; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3789 | |
| 3790 | unicode = _PyUnicode_New(size); |
| 3791 | if (!unicode) |
| 3792 | return NULL; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3793 | if (size == 0) { |
| 3794 | if (consumed) |
| 3795 | *consumed = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3796 | return (PyObject *)unicode; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3797 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3798 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3799 | p = PyUnicode_AS_UNICODE(unicode); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3800 | shiftOutStart = p; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3801 | e = s + size; |
| 3802 | |
| 3803 | while (s < e) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3804 | Py_UNICODE ch; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3805 | restart: |
Antoine Pitrou | 5ffd9e9 | 2008-07-25 18:05:24 +0000 | [diff] [blame] | 3806 | ch = (unsigned char) *s; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3807 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3808 | if (inShift) { /* in a base-64 section */ |
| 3809 | if (IS_BASE64(ch)) { /* consume a base-64 character */ |
| 3810 | base64buffer = (base64buffer << 6) | FROM_BASE64(ch); |
| 3811 | base64bits += 6; |
| 3812 | s++; |
| 3813 | if (base64bits >= 16) { |
| 3814 | /* we have enough bits for a UTF-16 value */ |
| 3815 | Py_UNICODE outCh = (Py_UNICODE) |
| 3816 | (base64buffer >> (base64bits-16)); |
| 3817 | base64bits -= 16; |
| 3818 | base64buffer &= (1 << base64bits) - 1; /* clear high bits */ |
| 3819 | if (surrogate) { |
| 3820 | /* expecting a second surrogate */ |
| 3821 | if (outCh >= 0xDC00 && outCh <= 0xDFFF) { |
| 3822 | #ifdef Py_UNICODE_WIDE |
| 3823 | *p++ = (((surrogate & 0x3FF)<<10) |
| 3824 | | (outCh & 0x3FF)) + 0x10000; |
| 3825 | #else |
| 3826 | *p++ = surrogate; |
| 3827 | *p++ = outCh; |
| 3828 | #endif |
| 3829 | surrogate = 0; |
| 3830 | } |
| 3831 | else { |
| 3832 | surrogate = 0; |
| 3833 | errmsg = "second surrogate missing"; |
| 3834 | goto utf7Error; |
| 3835 | } |
| 3836 | } |
| 3837 | else if (outCh >= 0xD800 && outCh <= 0xDBFF) { |
| 3838 | /* first surrogate */ |
| 3839 | surrogate = outCh; |
| 3840 | } |
| 3841 | else if (outCh >= 0xDC00 && outCh <= 0xDFFF) { |
| 3842 | errmsg = "unexpected second surrogate"; |
| 3843 | goto utf7Error; |
| 3844 | } |
| 3845 | else { |
| 3846 | *p++ = outCh; |
| 3847 | } |
| 3848 | } |
| 3849 | } |
| 3850 | else { /* now leaving a base-64 section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3851 | inShift = 0; |
| 3852 | s++; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3853 | if (surrogate) { |
| 3854 | errmsg = "second surrogate missing at end of shift sequence"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3855 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3856 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3857 | if (base64bits > 0) { /* left-over bits */ |
| 3858 | if (base64bits >= 6) { |
| 3859 | /* We've seen at least one base-64 character */ |
| 3860 | errmsg = "partial character in shift sequence"; |
| 3861 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3862 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3863 | else { |
| 3864 | /* Some bits remain; they should be zero */ |
| 3865 | if (base64buffer != 0) { |
| 3866 | errmsg = "non-zero padding bits in shift sequence"; |
| 3867 | goto utf7Error; |
| 3868 | } |
| 3869 | } |
| 3870 | } |
| 3871 | if (ch != '-') { |
| 3872 | /* '-' is absorbed; other terminating |
| 3873 | characters are preserved */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3874 | *p++ = ch; |
| 3875 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3876 | } |
| 3877 | } |
| 3878 | else if ( ch == '+' ) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3879 | startinpos = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3880 | s++; /* consume '+' */ |
| 3881 | if (s < e && *s == '-') { /* '+-' encodes '+' */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3882 | s++; |
| 3883 | *p++ = '+'; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3884 | } |
| 3885 | else { /* begin base64-encoded section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3886 | inShift = 1; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3887 | shiftOutStart = p; |
| 3888 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3889 | } |
| 3890 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3891 | else if (DECODE_DIRECT(ch)) { /* character decodes as itself */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3892 | *p++ = ch; |
| 3893 | s++; |
| 3894 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3895 | else { |
| 3896 | startinpos = s-starts; |
| 3897 | s++; |
| 3898 | errmsg = "unexpected special character"; |
| 3899 | goto utf7Error; |
| 3900 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3901 | continue; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3902 | utf7Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3903 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 3904 | endinpos = s-starts; |
| 3905 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3906 | errors, &errorHandler, |
| 3907 | "utf7", errmsg, |
| 3908 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 3909 | &unicode, &outpos, &p)) |
| 3910 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3911 | } |
| 3912 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3913 | /* end of string */ |
| 3914 | |
| 3915 | if (inShift && !consumed) { /* in shift sequence, no more to follow */ |
| 3916 | /* if we're in an inconsistent state, that's an error */ |
| 3917 | if (surrogate || |
| 3918 | (base64bits >= 6) || |
| 3919 | (base64bits > 0 && base64buffer != 0)) { |
| 3920 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 3921 | endinpos = size; |
| 3922 | if (unicode_decode_call_errorhandler( |
| 3923 | errors, &errorHandler, |
| 3924 | "utf7", "unterminated shift sequence", |
| 3925 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 3926 | &unicode, &outpos, &p)) |
| 3927 | goto onError; |
| 3928 | if (s < e) |
| 3929 | goto restart; |
| 3930 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3931 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3932 | |
| 3933 | /* return state */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3934 | if (consumed) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3935 | if (inShift) { |
| 3936 | p = shiftOutStart; /* back off output */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3937 | *consumed = startinpos; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3938 | } |
| 3939 | else { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3940 | *consumed = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3941 | } |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3942 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3943 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 3944 | if (PyUnicode_Resize((PyObject**)&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3945 | goto onError; |
| 3946 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3947 | Py_XDECREF(errorHandler); |
| 3948 | Py_XDECREF(exc); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 3949 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 3950 | if (_PyUnicode_READY_REPLACE(&unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3951 | Py_DECREF(unicode); |
| 3952 | return NULL; |
| 3953 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 3954 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 3955 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3956 | return (PyObject *)unicode; |
| 3957 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3958 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3959 | Py_XDECREF(errorHandler); |
| 3960 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3961 | Py_DECREF(unicode); |
| 3962 | return NULL; |
| 3963 | } |
| 3964 | |
| 3965 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3966 | PyObject * |
| 3967 | PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3968 | Py_ssize_t size, |
| 3969 | int base64SetO, |
| 3970 | int base64WhiteSpace, |
| 3971 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3972 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3973 | PyObject *v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3974 | /* It might be possible to tighten this worst case */ |
Alexandre Vassalotti | e85bd98 | 2009-07-21 00:39:03 +0000 | [diff] [blame] | 3975 | Py_ssize_t allocated = 8 * size; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3976 | int inShift = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3977 | Py_ssize_t i = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3978 | unsigned int base64bits = 0; |
| 3979 | unsigned long base64buffer = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3980 | char * out; |
| 3981 | char * start; |
| 3982 | |
| 3983 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3984 | return PyBytes_FromStringAndSize(NULL, 0); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3985 | |
Alexandre Vassalotti | e85bd98 | 2009-07-21 00:39:03 +0000 | [diff] [blame] | 3986 | if (allocated / 8 != size) |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3987 | return PyErr_NoMemory(); |
| 3988 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3989 | v = PyBytes_FromStringAndSize(NULL, allocated); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3990 | if (v == NULL) |
| 3991 | return NULL; |
| 3992 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3993 | start = out = PyBytes_AS_STRING(v); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3994 | for (;i < size; ++i) { |
| 3995 | Py_UNICODE ch = s[i]; |
| 3996 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3997 | if (inShift) { |
| 3998 | if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 3999 | /* shifting out */ |
| 4000 | if (base64bits) { /* output remaining bits */ |
| 4001 | *out++ = TO_BASE64(base64buffer << (6-base64bits)); |
| 4002 | base64buffer = 0; |
| 4003 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4004 | } |
| 4005 | inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4006 | /* Characters not in the BASE64 set implicitly unshift the sequence |
| 4007 | so no '-' is required, except if the character is itself a '-' */ |
| 4008 | if (IS_BASE64(ch) || ch == '-') { |
| 4009 | *out++ = '-'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4010 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4011 | *out++ = (char) ch; |
| 4012 | } |
| 4013 | else { |
| 4014 | goto encode_char; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4015 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4016 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4017 | else { /* not in a shift sequence */ |
| 4018 | if (ch == '+') { |
| 4019 | *out++ = '+'; |
| 4020 | *out++ = '-'; |
| 4021 | } |
| 4022 | else if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 4023 | *out++ = (char) ch; |
| 4024 | } |
| 4025 | else { |
| 4026 | *out++ = '+'; |
| 4027 | inShift = 1; |
| 4028 | goto encode_char; |
| 4029 | } |
| 4030 | } |
| 4031 | continue; |
| 4032 | encode_char: |
| 4033 | #ifdef Py_UNICODE_WIDE |
| 4034 | if (ch >= 0x10000) { |
| 4035 | /* code first surrogate */ |
| 4036 | base64bits += 16; |
| 4037 | base64buffer = (base64buffer << 16) | 0xd800 | ((ch-0x10000) >> 10); |
| 4038 | while (base64bits >= 6) { |
| 4039 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 4040 | base64bits -= 6; |
| 4041 | } |
| 4042 | /* prepare second surrogate */ |
| 4043 | ch = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 4044 | } |
| 4045 | #endif |
| 4046 | base64bits += 16; |
| 4047 | base64buffer = (base64buffer << 16) | ch; |
| 4048 | while (base64bits >= 6) { |
| 4049 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 4050 | base64bits -= 6; |
| 4051 | } |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 4052 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4053 | if (base64bits) |
| 4054 | *out++= TO_BASE64(base64buffer << (6-base64bits) ); |
| 4055 | if (inShift) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4056 | *out++ = '-'; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4057 | if (_PyBytes_Resize(&v, out - start) < 0) |
| 4058 | return NULL; |
| 4059 | return v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4060 | } |
| 4061 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4062 | #undef IS_BASE64 |
| 4063 | #undef FROM_BASE64 |
| 4064 | #undef TO_BASE64 |
| 4065 | #undef DECODE_DIRECT |
| 4066 | #undef ENCODE_DIRECT |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4067 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4068 | /* --- UTF-8 Codec -------------------------------------------------------- */ |
| 4069 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4070 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4071 | char utf8_code_length[256] = { |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4072 | /* Map UTF-8 encoded prefix byte to sequence length. Zero means |
| 4073 | illegal prefix. See RFC 3629 for details */ |
| 4074 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 00-0F */ |
| 4075 | 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] | 4076 | 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] | 4077 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 4078 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 4079 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 4080 | 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] | 4081 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 70-7F */ |
| 4082 | 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] | 4083 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4084 | 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] | 4085 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* B0-BF */ |
| 4086 | 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* C0-C1 + C2-CF */ |
| 4087 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* D0-DF */ |
| 4088 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* E0-EF */ |
| 4089 | 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] | 4090 | }; |
| 4091 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4092 | PyObject * |
| 4093 | PyUnicode_DecodeUTF8(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4094 | Py_ssize_t size, |
| 4095 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4096 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4097 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
| 4098 | } |
| 4099 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4100 | /* Mask to check or force alignment of a pointer to C 'long' boundaries */ |
| 4101 | #define LONG_PTR_MASK (size_t) (SIZEOF_LONG - 1) |
| 4102 | |
| 4103 | /* Mask to quickly check whether a C 'long' contains a |
| 4104 | non-ASCII, UTF8-encoded char. */ |
| 4105 | #if (SIZEOF_LONG == 8) |
| 4106 | # define ASCII_CHAR_MASK 0x8080808080808080L |
| 4107 | #elif (SIZEOF_LONG == 4) |
| 4108 | # define ASCII_CHAR_MASK 0x80808080L |
| 4109 | #else |
| 4110 | # error C 'long' size should be either 4 or 8! |
| 4111 | #endif |
| 4112 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4113 | /* Scans a UTF-8 string and returns the maximum character to be expected, |
| 4114 | the size of the decoded unicode string and if any major errors were |
| 4115 | encountered. |
| 4116 | |
| 4117 | This function does check basic UTF-8 sanity, it does however NOT CHECK |
| 4118 | if the string contains surrogates, and if all continuation bytes are |
| 4119 | within the correct ranges, these checks are performed in |
| 4120 | PyUnicode_DecodeUTF8Stateful. |
| 4121 | |
| 4122 | If it sets has_errors to 1, it means the value of unicode_size and max_char |
| 4123 | will be bogus and you should not rely on useful information in them. |
| 4124 | */ |
| 4125 | static Py_UCS4 |
| 4126 | utf8_max_char_size_and_has_errors(const char *s, Py_ssize_t string_size, |
| 4127 | Py_ssize_t *unicode_size, Py_ssize_t* consumed, |
| 4128 | int *has_errors) |
| 4129 | { |
| 4130 | Py_ssize_t n; |
| 4131 | Py_ssize_t char_count = 0; |
| 4132 | Py_UCS4 max_char = 127, new_max; |
| 4133 | Py_UCS4 upper_bound; |
| 4134 | const unsigned char *p = (const unsigned char *)s; |
| 4135 | const unsigned char *end = p + string_size; |
| 4136 | const unsigned char *aligned_end = (const unsigned char *) ((size_t) end & ~LONG_PTR_MASK); |
| 4137 | int err = 0; |
| 4138 | |
| 4139 | for (; p < end && !err; ++p, ++char_count) { |
| 4140 | /* Only check value if it's not a ASCII char... */ |
| 4141 | if (*p < 0x80) { |
| 4142 | /* Fast path, see below in PyUnicode_DecodeUTF8Stateful for |
| 4143 | an explanation. */ |
| 4144 | if (!((size_t) p & LONG_PTR_MASK)) { |
| 4145 | /* Help register allocation */ |
| 4146 | register const unsigned char *_p = p; |
| 4147 | while (_p < aligned_end) { |
| 4148 | unsigned long value = *(unsigned long *) _p; |
| 4149 | if (value & ASCII_CHAR_MASK) |
| 4150 | break; |
| 4151 | _p += SIZEOF_LONG; |
| 4152 | char_count += SIZEOF_LONG; |
| 4153 | } |
| 4154 | p = _p; |
| 4155 | if (p == end) |
| 4156 | break; |
| 4157 | } |
| 4158 | } |
| 4159 | if (*p >= 0x80) { |
| 4160 | n = utf8_code_length[*p]; |
| 4161 | new_max = max_char; |
| 4162 | switch (n) { |
| 4163 | /* invalid start byte */ |
| 4164 | case 0: |
| 4165 | err = 1; |
| 4166 | break; |
| 4167 | case 2: |
| 4168 | /* Code points between 0x00FF and 0x07FF inclusive. |
| 4169 | Approximate the upper bound of the code point, |
| 4170 | if this flips over 255 we can be sure it will be more |
| 4171 | than 255 and the string will need 2 bytes per code coint, |
| 4172 | if it stays under or equal to 255, we can be sure 1 byte |
| 4173 | is enough. |
| 4174 | ((*p & 0b00011111) << 6) | 0b00111111 */ |
| 4175 | upper_bound = ((*p & 0x1F) << 6) | 0x3F; |
| 4176 | if (max_char < upper_bound) |
| 4177 | new_max = upper_bound; |
| 4178 | /* Ensure we track at least that we left ASCII space. */ |
| 4179 | if (new_max < 128) |
| 4180 | new_max = 128; |
| 4181 | break; |
| 4182 | case 3: |
| 4183 | /* Between 0x0FFF and 0xFFFF inclusive, so values are |
| 4184 | always > 255 and <= 65535 and will always need 2 bytes. */ |
| 4185 | if (max_char < 65535) |
| 4186 | new_max = 65535; |
| 4187 | break; |
| 4188 | case 4: |
| 4189 | /* Code point will be above 0xFFFF for sure in this case. */ |
| 4190 | new_max = 65537; |
| 4191 | break; |
| 4192 | /* Internal error, this should be caught by the first if */ |
| 4193 | case 1: |
| 4194 | default: |
| 4195 | assert(0 && "Impossible case in utf8_max_char_and_size"); |
| 4196 | err = 1; |
| 4197 | } |
| 4198 | /* Instead of number of overall bytes for this code point, |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 4199 | n contains the number of following bytes: */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4200 | --n; |
| 4201 | /* Check if the follow up chars are all valid continuation bytes */ |
| 4202 | if (n >= 1) { |
| 4203 | const unsigned char *cont; |
| 4204 | if ((p + n) >= end) { |
| 4205 | if (consumed == 0) |
| 4206 | /* incomplete data, non-incremental decoding */ |
| 4207 | err = 1; |
| 4208 | break; |
| 4209 | } |
| 4210 | for (cont = p + 1; cont < (p + n); ++cont) { |
| 4211 | if ((*cont & 0xc0) != 0x80) { |
| 4212 | err = 1; |
| 4213 | break; |
| 4214 | } |
| 4215 | } |
| 4216 | p += n; |
| 4217 | } |
| 4218 | else |
| 4219 | err = 1; |
| 4220 | max_char = new_max; |
| 4221 | } |
| 4222 | } |
| 4223 | |
| 4224 | if (unicode_size) |
| 4225 | *unicode_size = char_count; |
| 4226 | if (has_errors) |
| 4227 | *has_errors = err; |
| 4228 | return max_char; |
| 4229 | } |
| 4230 | |
| 4231 | /* Similar to PyUnicode_WRITE but can also write into wstr field |
| 4232 | of the legacy unicode representation */ |
| 4233 | #define WRITE_FLEXIBLE_OR_WSTR(kind, buf, index, value) \ |
| 4234 | do { \ |
| 4235 | const int k_ = (kind); \ |
| 4236 | if (k_ == PyUnicode_WCHAR_KIND) \ |
| 4237 | ((Py_UNICODE *)(buf))[(index)] = (Py_UNICODE)(value); \ |
| 4238 | else if (k_ == PyUnicode_1BYTE_KIND) \ |
| 4239 | ((unsigned char *)(buf))[(index)] = (unsigned char)(value); \ |
| 4240 | else if (k_ == PyUnicode_2BYTE_KIND) \ |
| 4241 | ((Py_UCS2 *)(buf))[(index)] = (Py_UCS2)(value); \ |
| 4242 | else \ |
| 4243 | ((Py_UCS4 *)(buf))[(index)] = (Py_UCS4)(value); \ |
| 4244 | } while (0) |
| 4245 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4246 | PyObject * |
| 4247 | PyUnicode_DecodeUTF8Stateful(const char *s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4248 | Py_ssize_t size, |
| 4249 | const char *errors, |
| 4250 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4251 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4252 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4253 | int n; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4254 | int k; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4255 | Py_ssize_t startinpos; |
| 4256 | Py_ssize_t endinpos; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4257 | const char *e, *aligned_end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4258 | PyUnicodeObject *unicode; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4259 | const char *errmsg = ""; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4260 | PyObject *errorHandler = NULL; |
| 4261 | PyObject *exc = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4262 | Py_UCS4 maxchar = 0; |
| 4263 | Py_ssize_t unicode_size; |
| 4264 | Py_ssize_t i; |
| 4265 | int kind; |
| 4266 | void *data; |
| 4267 | int has_errors; |
| 4268 | Py_UNICODE *error_outptr; |
| 4269 | #if SIZEOF_WCHAR_T == 2 |
| 4270 | Py_ssize_t wchar_offset = 0; |
| 4271 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4272 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4273 | if (size == 0) { |
| 4274 | if (consumed) |
| 4275 | *consumed = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4276 | return (PyObject *)PyUnicode_New(0, 0); |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4277 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4278 | maxchar = utf8_max_char_size_and_has_errors(s, size, &unicode_size, |
| 4279 | consumed, &has_errors); |
| 4280 | if (has_errors) { |
| 4281 | unicode = _PyUnicode_New(size); |
| 4282 | if (!unicode) |
| 4283 | return NULL; |
| 4284 | kind = PyUnicode_WCHAR_KIND; |
| 4285 | data = PyUnicode_AS_UNICODE(unicode); |
| 4286 | assert(data != NULL); |
| 4287 | } |
| 4288 | else { |
| 4289 | unicode = (PyUnicodeObject *)PyUnicode_New(unicode_size, maxchar); |
| 4290 | if (!unicode) |
| 4291 | return NULL; |
| 4292 | /* When the string is ASCII only, just use memcpy and return. |
| 4293 | unicode_size may be != size if there is an incomplete UTF-8 |
| 4294 | sequence at the end of the ASCII block. */ |
| 4295 | if (maxchar < 128 && size == unicode_size) { |
| 4296 | Py_MEMCPY(PyUnicode_1BYTE_DATA(unicode), s, unicode_size); |
| 4297 | return (PyObject *)unicode; |
| 4298 | } |
| 4299 | kind = PyUnicode_KIND(unicode); |
| 4300 | data = PyUnicode_DATA(unicode); |
| 4301 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4302 | /* Unpack UTF-8 encoded data */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4303 | i = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4304 | e = s + size; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4305 | aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4306 | |
| 4307 | while (s < e) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 4308 | Py_UCS4 ch = (unsigned char)*s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4309 | |
| 4310 | if (ch < 0x80) { |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4311 | /* Fast path for runs of ASCII characters. Given that common UTF-8 |
| 4312 | input will consist of an overwhelming majority of ASCII |
| 4313 | characters, we try to optimize for this case by checking |
| 4314 | as many characters as a C 'long' can contain. |
| 4315 | First, check if we can do an aligned read, as most CPUs have |
| 4316 | a penalty for unaligned reads. |
| 4317 | */ |
| 4318 | if (!((size_t) s & LONG_PTR_MASK)) { |
| 4319 | /* Help register allocation */ |
| 4320 | register const char *_s = s; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4321 | register Py_ssize_t _i = i; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4322 | while (_s < aligned_end) { |
| 4323 | /* Read a whole long at a time (either 4 or 8 bytes), |
| 4324 | and do a fast unrolled copy if it only contains ASCII |
| 4325 | characters. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4326 | unsigned long value = *(unsigned long *) _s; |
| 4327 | if (value & ASCII_CHAR_MASK) |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4328 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4329 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+0, _s[0]); |
| 4330 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+1, _s[1]); |
| 4331 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+2, _s[2]); |
| 4332 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+3, _s[3]); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4333 | #if (SIZEOF_LONG == 8) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4334 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+4, _s[4]); |
| 4335 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+5, _s[5]); |
| 4336 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+6, _s[6]); |
| 4337 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+7, _s[7]); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4338 | #endif |
| 4339 | _s += SIZEOF_LONG; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4340 | _i += SIZEOF_LONG; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4341 | } |
| 4342 | s = _s; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4343 | i = _i; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4344 | if (s == e) |
| 4345 | break; |
| 4346 | ch = (unsigned char)*s; |
| 4347 | } |
| 4348 | } |
| 4349 | |
| 4350 | if (ch < 0x80) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4351 | WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4352 | s++; |
| 4353 | continue; |
| 4354 | } |
| 4355 | |
| 4356 | n = utf8_code_length[ch]; |
| 4357 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4358 | if (s + n > e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4359 | if (consumed) |
| 4360 | break; |
| 4361 | else { |
| 4362 | errmsg = "unexpected end of data"; |
| 4363 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4364 | endinpos = startinpos+1; |
| 4365 | for (k=1; (k < size-startinpos) && ((s[k]&0xC0) == 0x80); k++) |
| 4366 | endinpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4367 | goto utf8Error; |
| 4368 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4369 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4370 | |
| 4371 | switch (n) { |
| 4372 | |
| 4373 | case 0: |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4374 | errmsg = "invalid start byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4375 | startinpos = s-starts; |
| 4376 | endinpos = startinpos+1; |
| 4377 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4378 | |
| 4379 | case 1: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4380 | errmsg = "internal error"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4381 | startinpos = s-starts; |
| 4382 | endinpos = startinpos+1; |
| 4383 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4384 | |
| 4385 | case 2: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4386 | if ((s[1] & 0xc0) != 0x80) { |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4387 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4388 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4389 | endinpos = startinpos + 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4390 | goto utf8Error; |
| 4391 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4392 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4393 | assert ((ch > 0x007F) && (ch <= 0x07FF)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4394 | WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4395 | break; |
| 4396 | |
| 4397 | case 3: |
Ezio Melotti | 9bf2b3a | 2010-07-03 04:52:19 +0000 | [diff] [blame] | 4398 | /* Decoding UTF-8 sequences in range \xed\xa0\x80-\xed\xbf\xbf |
| 4399 | will result in surrogates in range d800-dfff. Surrogates are |
| 4400 | not valid UTF-8 so they are rejected. |
| 4401 | See http://www.unicode.org/versions/Unicode5.2.0/ch03.pdf |
| 4402 | (table 3-7) and http://www.rfc-editor.org/rfc/rfc3629.txt */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4403 | if ((s[1] & 0xc0) != 0x80 || |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4404 | (s[2] & 0xc0) != 0x80 || |
| 4405 | ((unsigned char)s[0] == 0xE0 && |
| 4406 | (unsigned char)s[1] < 0xA0) || |
| 4407 | ((unsigned char)s[0] == 0xED && |
| 4408 | (unsigned char)s[1] > 0x9F)) { |
| 4409 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4410 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4411 | endinpos = startinpos + 1; |
| 4412 | |
| 4413 | /* if s[1] first two bits are 1 and 0, then the invalid |
| 4414 | continuation byte is s[2], so increment endinpos by 1, |
| 4415 | if not, s[1] is invalid and endinpos doesn't need to |
| 4416 | be incremented. */ |
| 4417 | if ((s[1] & 0xC0) == 0x80) |
| 4418 | endinpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4419 | goto utf8Error; |
| 4420 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4421 | ch = ((s[0] & 0x0f) << 12) + ((s[1] & 0x3f) << 6) + (s[2] & 0x3f); |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4422 | assert ((ch > 0x07FF) && (ch <= 0xFFFF)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4423 | WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, ch); |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 4424 | break; |
| 4425 | |
| 4426 | case 4: |
| 4427 | if ((s[1] & 0xc0) != 0x80 || |
| 4428 | (s[2] & 0xc0) != 0x80 || |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4429 | (s[3] & 0xc0) != 0x80 || |
| 4430 | ((unsigned char)s[0] == 0xF0 && |
| 4431 | (unsigned char)s[1] < 0x90) || |
| 4432 | ((unsigned char)s[0] == 0xF4 && |
| 4433 | (unsigned char)s[1] > 0x8F)) { |
| 4434 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4435 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4436 | endinpos = startinpos + 1; |
| 4437 | if ((s[1] & 0xC0) == 0x80) { |
| 4438 | endinpos++; |
| 4439 | if ((s[2] & 0xC0) == 0x80) |
| 4440 | endinpos++; |
| 4441 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4442 | goto utf8Error; |
| 4443 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 4444 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4445 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
| 4446 | assert ((ch > 0xFFFF) && (ch <= 0x10ffff)); |
| 4447 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4448 | /* If the string is flexible or we have native UCS-4, write |
| 4449 | directly.. */ |
| 4450 | if (sizeof(Py_UNICODE) > 2 || kind != PyUnicode_WCHAR_KIND) |
| 4451 | WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, ch); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4452 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4453 | else { |
| 4454 | /* compute and append the two surrogates: */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4455 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4456 | /* translate from 10000..10FFFF to 0..FFFF */ |
| 4457 | ch -= 0x10000; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4458 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4459 | /* high surrogate = top 10 bits added to D800 */ |
| 4460 | WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, |
| 4461 | (Py_UNICODE)(0xD800 + (ch >> 10))); |
| 4462 | |
| 4463 | /* low surrogate = bottom 10 bits added to DC00 */ |
| 4464 | WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, |
| 4465 | (Py_UNICODE)(0xDC00 + (ch & 0x03FF))); |
| 4466 | } |
| 4467 | #if SIZEOF_WCHAR_T == 2 |
| 4468 | wchar_offset++; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 4469 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4470 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4471 | } |
| 4472 | s += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4473 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4474 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4475 | utf8Error: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4476 | /* If this is not yet a resizable string, make it one.. */ |
| 4477 | if (kind != PyUnicode_WCHAR_KIND) { |
| 4478 | const Py_UNICODE *u; |
| 4479 | PyUnicodeObject *new_unicode = _PyUnicode_New(size); |
| 4480 | if (!new_unicode) |
| 4481 | goto onError; |
| 4482 | u = PyUnicode_AsUnicode((PyObject *)unicode); |
| 4483 | if (!u) |
| 4484 | goto onError; |
| 4485 | #if SIZEOF_WCHAR_T == 2 |
| 4486 | i += wchar_offset; |
| 4487 | #endif |
| 4488 | Py_UNICODE_COPY(PyUnicode_AS_UNICODE(new_unicode), u, i); |
| 4489 | Py_DECREF(unicode); |
| 4490 | unicode = new_unicode; |
| 4491 | kind = 0; |
| 4492 | data = PyUnicode_AS_UNICODE(new_unicode); |
| 4493 | assert(data != NULL); |
| 4494 | } |
| 4495 | error_outptr = PyUnicode_AS_UNICODE(unicode) + i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4496 | if (unicode_decode_call_errorhandler( |
| 4497 | errors, &errorHandler, |
| 4498 | "utf8", errmsg, |
| 4499 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4500 | &unicode, &i, &error_outptr)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4501 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4502 | /* Update data because unicode_decode_call_errorhandler might have |
| 4503 | re-created or resized the unicode object. */ |
| 4504 | data = PyUnicode_AS_UNICODE(unicode); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4505 | aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4506 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4507 | /* Ensure the unicode_size calculation above was correct: */ |
| 4508 | assert(kind == PyUnicode_WCHAR_KIND || i == unicode_size); |
| 4509 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4510 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4511 | *consumed = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4512 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4513 | /* Adjust length and ready string when it contained errors and |
| 4514 | is of the old resizable kind. */ |
| 4515 | if (kind == PyUnicode_WCHAR_KIND) { |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 4516 | if (PyUnicode_Resize((PyObject**)&unicode, i) < 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4517 | goto onError; |
| 4518 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4519 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4520 | Py_XDECREF(errorHandler); |
| 4521 | Py_XDECREF(exc); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 4522 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 4523 | if (_PyUnicode_READY_REPLACE(&unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4524 | Py_DECREF(unicode); |
| 4525 | return NULL; |
| 4526 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 4527 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 4528 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4529 | return (PyObject *)unicode; |
| 4530 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4531 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4532 | Py_XDECREF(errorHandler); |
| 4533 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4534 | Py_DECREF(unicode); |
| 4535 | return NULL; |
| 4536 | } |
| 4537 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4538 | #undef WRITE_FLEXIBLE_OR_WSTR |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4539 | |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4540 | #ifdef __APPLE__ |
| 4541 | |
| 4542 | /* Simplified UTF-8 decoder using surrogateescape error handler, |
| 4543 | used to decode the command line arguments on Mac OS X. */ |
| 4544 | |
| 4545 | wchar_t* |
| 4546 | _Py_DecodeUTF8_surrogateescape(const char *s, Py_ssize_t size) |
| 4547 | { |
| 4548 | int n; |
| 4549 | const char *e; |
| 4550 | wchar_t *unicode, *p; |
| 4551 | |
| 4552 | /* Note: size will always be longer than the resulting Unicode |
| 4553 | character count */ |
| 4554 | if (PY_SSIZE_T_MAX / sizeof(wchar_t) < (size + 1)) { |
| 4555 | PyErr_NoMemory(); |
| 4556 | return NULL; |
| 4557 | } |
| 4558 | unicode = PyMem_Malloc((size + 1) * sizeof(wchar_t)); |
| 4559 | if (!unicode) |
| 4560 | return NULL; |
| 4561 | |
| 4562 | /* Unpack UTF-8 encoded data */ |
| 4563 | p = unicode; |
| 4564 | e = s + size; |
| 4565 | while (s < e) { |
| 4566 | Py_UCS4 ch = (unsigned char)*s; |
| 4567 | |
| 4568 | if (ch < 0x80) { |
| 4569 | *p++ = (wchar_t)ch; |
| 4570 | s++; |
| 4571 | continue; |
| 4572 | } |
| 4573 | |
| 4574 | n = utf8_code_length[ch]; |
| 4575 | if (s + n > e) { |
| 4576 | goto surrogateescape; |
| 4577 | } |
| 4578 | |
| 4579 | switch (n) { |
| 4580 | case 0: |
| 4581 | case 1: |
| 4582 | goto surrogateescape; |
| 4583 | |
| 4584 | case 2: |
| 4585 | if ((s[1] & 0xc0) != 0x80) |
| 4586 | goto surrogateescape; |
| 4587 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
| 4588 | assert ((ch > 0x007F) && (ch <= 0x07FF)); |
| 4589 | *p++ = (wchar_t)ch; |
| 4590 | break; |
| 4591 | |
| 4592 | case 3: |
| 4593 | /* Decoding UTF-8 sequences in range \xed\xa0\x80-\xed\xbf\xbf |
| 4594 | will result in surrogates in range d800-dfff. Surrogates are |
| 4595 | not valid UTF-8 so they are rejected. |
| 4596 | See http://www.unicode.org/versions/Unicode5.2.0/ch03.pdf |
| 4597 | (table 3-7) and http://www.rfc-editor.org/rfc/rfc3629.txt */ |
| 4598 | if ((s[1] & 0xc0) != 0x80 || |
| 4599 | (s[2] & 0xc0) != 0x80 || |
| 4600 | ((unsigned char)s[0] == 0xE0 && |
| 4601 | (unsigned char)s[1] < 0xA0) || |
| 4602 | ((unsigned char)s[0] == 0xED && |
| 4603 | (unsigned char)s[1] > 0x9F)) { |
| 4604 | |
| 4605 | goto surrogateescape; |
| 4606 | } |
| 4607 | ch = ((s[0] & 0x0f) << 12) + ((s[1] & 0x3f) << 6) + (s[2] & 0x3f); |
| 4608 | assert ((ch > 0x07FF) && (ch <= 0xFFFF)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4609 | *p++ = (wchar_t)ch; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4610 | break; |
| 4611 | |
| 4612 | case 4: |
| 4613 | if ((s[1] & 0xc0) != 0x80 || |
| 4614 | (s[2] & 0xc0) != 0x80 || |
| 4615 | (s[3] & 0xc0) != 0x80 || |
| 4616 | ((unsigned char)s[0] == 0xF0 && |
| 4617 | (unsigned char)s[1] < 0x90) || |
| 4618 | ((unsigned char)s[0] == 0xF4 && |
| 4619 | (unsigned char)s[1] > 0x8F)) { |
| 4620 | goto surrogateescape; |
| 4621 | } |
| 4622 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
| 4623 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
| 4624 | assert ((ch > 0xFFFF) && (ch <= 0x10ffff)); |
| 4625 | |
| 4626 | #if SIZEOF_WCHAR_T == 4 |
| 4627 | *p++ = (wchar_t)ch; |
| 4628 | #else |
| 4629 | /* compute and append the two surrogates: */ |
| 4630 | |
| 4631 | /* translate from 10000..10FFFF to 0..FFFF */ |
| 4632 | ch -= 0x10000; |
| 4633 | |
| 4634 | /* high surrogate = top 10 bits added to D800 */ |
| 4635 | *p++ = (wchar_t)(0xD800 + (ch >> 10)); |
| 4636 | |
| 4637 | /* low surrogate = bottom 10 bits added to DC00 */ |
| 4638 | *p++ = (wchar_t)(0xDC00 + (ch & 0x03FF)); |
| 4639 | #endif |
| 4640 | break; |
| 4641 | } |
| 4642 | s += n; |
| 4643 | continue; |
| 4644 | |
| 4645 | surrogateescape: |
| 4646 | *p++ = 0xDC00 + ch; |
| 4647 | s++; |
| 4648 | } |
| 4649 | *p = L'\0'; |
| 4650 | return unicode; |
| 4651 | } |
| 4652 | |
| 4653 | #endif /* __APPLE__ */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4654 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4655 | /* Primary internal function which creates utf8 encoded bytes objects. |
| 4656 | |
| 4657 | Allocation strategy: if the string is short, convert into a stack buffer |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4658 | and allocate exactly as much space needed at the end. Else allocate the |
| 4659 | maximum possible needed (4 result bytes per Unicode character), and return |
| 4660 | the excess memory at the end. |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4661 | */ |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 4662 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4663 | _PyUnicode_AsUTF8String(PyObject *obj, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4664 | { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4665 | #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] | 4666 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4667 | Py_ssize_t i; /* index into s of next input byte */ |
| 4668 | PyObject *result; /* result string object */ |
| 4669 | char *p; /* next free byte in output buffer */ |
| 4670 | Py_ssize_t nallocated; /* number of result bytes allocated */ |
| 4671 | Py_ssize_t nneeded; /* number of result bytes needed */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4672 | char stackbuf[MAX_SHORT_UNICHARS * 4]; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4673 | PyObject *errorHandler = NULL; |
| 4674 | PyObject *exc = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4675 | int kind; |
| 4676 | void *data; |
| 4677 | Py_ssize_t size; |
| 4678 | PyUnicodeObject *unicode = (PyUnicodeObject *)obj; |
| 4679 | #if SIZEOF_WCHAR_T == 2 |
| 4680 | Py_ssize_t wchar_offset = 0; |
| 4681 | #endif |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 4682 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4683 | if (!PyUnicode_Check(unicode)) { |
| 4684 | PyErr_BadArgument(); |
| 4685 | return NULL; |
| 4686 | } |
| 4687 | |
| 4688 | if (PyUnicode_READY(unicode) == -1) |
| 4689 | return NULL; |
| 4690 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 4691 | if (PyUnicode_UTF8(unicode)) |
| 4692 | return PyBytes_FromStringAndSize(PyUnicode_UTF8(unicode), |
| 4693 | PyUnicode_UTF8_LENGTH(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4694 | |
| 4695 | kind = PyUnicode_KIND(unicode); |
| 4696 | data = PyUnicode_DATA(unicode); |
| 4697 | size = PyUnicode_GET_LENGTH(unicode); |
| 4698 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4699 | assert(size >= 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4700 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4701 | if (size <= MAX_SHORT_UNICHARS) { |
| 4702 | /* Write into the stack buffer; nallocated can't overflow. |
| 4703 | * At the end, we'll allocate exactly as much heap space as it |
| 4704 | * turns out we need. |
| 4705 | */ |
| 4706 | nallocated = Py_SAFE_DOWNCAST(sizeof(stackbuf), size_t, int); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4707 | result = NULL; /* will allocate after we're done */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4708 | p = stackbuf; |
| 4709 | } |
| 4710 | else { |
| 4711 | /* Overallocate on the heap, and give the excess back at the end. */ |
| 4712 | nallocated = size * 4; |
| 4713 | if (nallocated / 4 != size) /* overflow! */ |
| 4714 | return PyErr_NoMemory(); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4715 | result = PyBytes_FromStringAndSize(NULL, nallocated); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4716 | if (result == NULL) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4717 | return NULL; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4718 | p = PyBytes_AS_STRING(result); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4719 | } |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4720 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4721 | for (i = 0; i < size;) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4722 | Py_UCS4 ch = PyUnicode_READ(kind, data, i++); |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 4723 | |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4724 | if (ch < 0x80) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4725 | /* Encode ASCII */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4726 | *p++ = (char) ch; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 4727 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4728 | else if (ch < 0x0800) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4729 | /* Encode Latin-1 */ |
Marc-André Lemburg | dc724d6 | 2002-02-06 18:20:19 +0000 | [diff] [blame] | 4730 | *p++ = (char)(0xc0 | (ch >> 6)); |
| 4731 | *p++ = (char)(0x80 | (ch & 0x3f)); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4732 | } else if (0xD800 <= ch && ch <= 0xDFFF) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4733 | Py_ssize_t newpos; |
| 4734 | PyObject *rep; |
| 4735 | Py_ssize_t repsize, k, startpos; |
| 4736 | startpos = i-1; |
| 4737 | #if SIZEOF_WCHAR_T == 2 |
| 4738 | startpos += wchar_offset; |
Victor Stinner | 445a623 | 2010-04-22 20:01:57 +0000 | [diff] [blame] | 4739 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4740 | rep = unicode_encode_call_errorhandler( |
| 4741 | errors, &errorHandler, "utf-8", "surrogates not allowed", |
| 4742 | PyUnicode_AS_UNICODE(unicode), PyUnicode_GET_SIZE(unicode), |
| 4743 | &exc, startpos, startpos+1, &newpos); |
| 4744 | if (!rep) |
| 4745 | goto error; |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4746 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4747 | if (PyBytes_Check(rep)) |
| 4748 | repsize = PyBytes_GET_SIZE(rep); |
| 4749 | else |
| 4750 | repsize = PyUnicode_GET_SIZE(rep); |
| 4751 | |
| 4752 | if (repsize > 4) { |
| 4753 | Py_ssize_t offset; |
| 4754 | |
| 4755 | if (result == NULL) |
| 4756 | offset = p - stackbuf; |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4757 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4758 | offset = p - PyBytes_AS_STRING(result); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4759 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4760 | if (nallocated > PY_SSIZE_T_MAX - repsize + 4) { |
| 4761 | /* integer overflow */ |
| 4762 | PyErr_NoMemory(); |
| 4763 | goto error; |
| 4764 | } |
| 4765 | nallocated += repsize - 4; |
| 4766 | if (result != NULL) { |
| 4767 | if (_PyBytes_Resize(&result, nallocated) < 0) |
| 4768 | goto error; |
| 4769 | } else { |
| 4770 | result = PyBytes_FromStringAndSize(NULL, nallocated); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4771 | if (result == NULL) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4772 | goto error; |
| 4773 | Py_MEMCPY(PyBytes_AS_STRING(result), stackbuf, offset); |
| 4774 | } |
| 4775 | p = PyBytes_AS_STRING(result) + offset; |
| 4776 | } |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4777 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4778 | if (PyBytes_Check(rep)) { |
| 4779 | char *prep = PyBytes_AS_STRING(rep); |
| 4780 | for(k = repsize; k > 0; k--) |
| 4781 | *p++ = *prep++; |
| 4782 | } else /* rep is unicode */ { |
| 4783 | const Py_UNICODE *prep = PyUnicode_AS_UNICODE(rep); |
| 4784 | Py_UNICODE c; |
| 4785 | |
| 4786 | for(k=0; k<repsize; k++) { |
| 4787 | c = prep[k]; |
| 4788 | if (0x80 <= c) { |
| 4789 | raise_encode_exception(&exc, "utf-8", |
| 4790 | PyUnicode_AS_UNICODE(unicode), |
| 4791 | size, i-1, i, |
| 4792 | "surrogates not allowed"); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4793 | goto error; |
| 4794 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4795 | *p++ = (char)prep[k]; |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4796 | } |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4797 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4798 | Py_DECREF(rep); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4799 | } else if (ch < 0x10000) { |
| 4800 | *p++ = (char)(0xe0 | (ch >> 12)); |
| 4801 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 4802 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 4803 | } else /* ch >= 0x10000 */ { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4804 | /* Encode UCS4 Unicode ordinals */ |
| 4805 | *p++ = (char)(0xf0 | (ch >> 18)); |
| 4806 | *p++ = (char)(0x80 | ((ch >> 12) & 0x3f)); |
| 4807 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 4808 | *p++ = (char)(0x80 | (ch & 0x3f)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4809 | #if SIZEOF_WCHAR_T == 2 |
| 4810 | wchar_offset++; |
| 4811 | #endif |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4812 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4813 | } |
Tim Peters | 0eca65c | 2002-04-21 17:28:06 +0000 | [diff] [blame] | 4814 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4815 | if (result == NULL) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4816 | /* This was stack allocated. */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4817 | nneeded = p - stackbuf; |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4818 | assert(nneeded <= nallocated); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4819 | result = PyBytes_FromStringAndSize(stackbuf, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4820 | } |
| 4821 | else { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 4822 | /* Cut back to size actually needed. */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4823 | nneeded = p - PyBytes_AS_STRING(result); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4824 | assert(nneeded <= nallocated); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4825 | _PyBytes_Resize(&result, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4826 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4827 | |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4828 | Py_XDECREF(errorHandler); |
| 4829 | Py_XDECREF(exc); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4830 | return result; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4831 | error: |
| 4832 | Py_XDECREF(errorHandler); |
| 4833 | Py_XDECREF(exc); |
| 4834 | Py_XDECREF(result); |
| 4835 | return NULL; |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4836 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4837 | #undef MAX_SHORT_UNICHARS |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4838 | } |
| 4839 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4840 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4841 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
| 4842 | Py_ssize_t size, |
| 4843 | const char *errors) |
| 4844 | { |
| 4845 | PyObject *v, *unicode; |
| 4846 | |
| 4847 | unicode = PyUnicode_FromUnicode(s, size); |
| 4848 | if (unicode == NULL) |
| 4849 | return NULL; |
| 4850 | v = _PyUnicode_AsUTF8String(unicode, errors); |
| 4851 | Py_DECREF(unicode); |
| 4852 | return v; |
| 4853 | } |
| 4854 | |
| 4855 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4856 | PyUnicode_AsUTF8String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4857 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4858 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4859 | } |
| 4860 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4861 | /* --- UTF-32 Codec ------------------------------------------------------- */ |
| 4862 | |
| 4863 | PyObject * |
| 4864 | PyUnicode_DecodeUTF32(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4865 | Py_ssize_t size, |
| 4866 | const char *errors, |
| 4867 | int *byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4868 | { |
| 4869 | return PyUnicode_DecodeUTF32Stateful(s, size, errors, byteorder, NULL); |
| 4870 | } |
| 4871 | |
| 4872 | PyObject * |
| 4873 | PyUnicode_DecodeUTF32Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4874 | Py_ssize_t size, |
| 4875 | const char *errors, |
| 4876 | int *byteorder, |
| 4877 | Py_ssize_t *consumed) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4878 | { |
| 4879 | const char *starts = s; |
| 4880 | Py_ssize_t startinpos; |
| 4881 | Py_ssize_t endinpos; |
| 4882 | Py_ssize_t outpos; |
| 4883 | PyUnicodeObject *unicode; |
| 4884 | Py_UNICODE *p; |
| 4885 | #ifndef Py_UNICODE_WIDE |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 4886 | int pairs = 0; |
Mark Dickinson | 7db923c | 2010-06-12 09:10:14 +0000 | [diff] [blame] | 4887 | const unsigned char *qq; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4888 | #else |
| 4889 | const int pairs = 0; |
| 4890 | #endif |
Mark Dickinson | 7db923c | 2010-06-12 09:10:14 +0000 | [diff] [blame] | 4891 | const unsigned char *q, *e; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4892 | int bo = 0; /* assume native ordering by default */ |
| 4893 | const char *errmsg = ""; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4894 | /* Offsets from q for retrieving bytes in the right order. */ |
| 4895 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 4896 | int iorder[] = {0, 1, 2, 3}; |
| 4897 | #else |
| 4898 | int iorder[] = {3, 2, 1, 0}; |
| 4899 | #endif |
| 4900 | PyObject *errorHandler = NULL; |
| 4901 | PyObject *exc = NULL; |
Victor Stinner | 313a120 | 2010-06-11 23:56:51 +0000 | [diff] [blame] | 4902 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4903 | q = (unsigned char *)s; |
| 4904 | e = q + size; |
| 4905 | |
| 4906 | if (byteorder) |
| 4907 | bo = *byteorder; |
| 4908 | |
| 4909 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 4910 | byte order setting accordingly. In native mode, the leading BOM |
| 4911 | mark is skipped, in all other modes, it is copied to the output |
| 4912 | stream as-is (giving a ZWNBSP character). */ |
| 4913 | if (bo == 0) { |
| 4914 | if (size >= 4) { |
| 4915 | const Py_UCS4 bom = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4916 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4917 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4918 | if (bom == 0x0000FEFF) { |
| 4919 | q += 4; |
| 4920 | bo = -1; |
| 4921 | } |
| 4922 | else if (bom == 0xFFFE0000) { |
| 4923 | q += 4; |
| 4924 | bo = 1; |
| 4925 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4926 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4927 | if (bom == 0x0000FEFF) { |
| 4928 | q += 4; |
| 4929 | bo = 1; |
| 4930 | } |
| 4931 | else if (bom == 0xFFFE0000) { |
| 4932 | q += 4; |
| 4933 | bo = -1; |
| 4934 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4935 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4936 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4937 | } |
| 4938 | |
| 4939 | if (bo == -1) { |
| 4940 | /* force LE */ |
| 4941 | iorder[0] = 0; |
| 4942 | iorder[1] = 1; |
| 4943 | iorder[2] = 2; |
| 4944 | iorder[3] = 3; |
| 4945 | } |
| 4946 | else if (bo == 1) { |
| 4947 | /* force BE */ |
| 4948 | iorder[0] = 3; |
| 4949 | iorder[1] = 2; |
| 4950 | iorder[2] = 1; |
| 4951 | iorder[3] = 0; |
| 4952 | } |
| 4953 | |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 4954 | /* On narrow builds we split characters outside the BMP into two |
| 4955 | codepoints => count how much extra space we need. */ |
| 4956 | #ifndef Py_UNICODE_WIDE |
| 4957 | for (qq = q; qq < e; qq += 4) |
| 4958 | if (qq[iorder[2]] != 0 || qq[iorder[3]] != 0) |
| 4959 | pairs++; |
| 4960 | #endif |
| 4961 | |
| 4962 | /* This might be one to much, because of a BOM */ |
| 4963 | unicode = _PyUnicode_New((size+3)/4+pairs); |
| 4964 | if (!unicode) |
| 4965 | return NULL; |
| 4966 | if (size == 0) |
| 4967 | return (PyObject *)unicode; |
| 4968 | |
| 4969 | /* Unpack UTF-32 encoded data */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4970 | p = PyUnicode_AS_UNICODE(unicode); |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 4971 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4972 | while (q < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4973 | Py_UCS4 ch; |
| 4974 | /* remaining bytes at the end? (size should be divisible by 4) */ |
| 4975 | if (e-q<4) { |
| 4976 | if (consumed) |
| 4977 | break; |
| 4978 | errmsg = "truncated data"; |
| 4979 | startinpos = ((const char *)q)-starts; |
| 4980 | endinpos = ((const char *)e)-starts; |
| 4981 | goto utf32Error; |
| 4982 | /* The remaining input chars are ignored if the callback |
| 4983 | chooses to skip the input */ |
| 4984 | } |
| 4985 | ch = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
| 4986 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4987 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4988 | if (ch >= 0x110000) |
| 4989 | { |
| 4990 | errmsg = "codepoint not in range(0x110000)"; |
| 4991 | startinpos = ((const char *)q)-starts; |
| 4992 | endinpos = startinpos+4; |
| 4993 | goto utf32Error; |
| 4994 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4995 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4996 | if (ch >= 0x10000) |
| 4997 | { |
| 4998 | *p++ = 0xD800 | ((ch-0x10000) >> 10); |
| 4999 | *p++ = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 5000 | } |
| 5001 | else |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5002 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5003 | *p++ = ch; |
| 5004 | q += 4; |
| 5005 | continue; |
| 5006 | utf32Error: |
| 5007 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 5008 | if (unicode_decode_call_errorhandler( |
| 5009 | errors, &errorHandler, |
| 5010 | "utf32", errmsg, |
| 5011 | &starts, (const char **)&e, &startinpos, &endinpos, &exc, (const char **)&q, |
| 5012 | &unicode, &outpos, &p)) |
| 5013 | goto onError; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5014 | } |
| 5015 | |
| 5016 | if (byteorder) |
| 5017 | *byteorder = bo; |
| 5018 | |
| 5019 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5020 | *consumed = (const char *)q-starts; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5021 | |
| 5022 | /* Adjust length */ |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 5023 | if (PyUnicode_Resize((PyObject**)&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5024 | goto onError; |
| 5025 | |
| 5026 | Py_XDECREF(errorHandler); |
| 5027 | Py_XDECREF(exc); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 5028 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 5029 | if (_PyUnicode_READY_REPLACE(&unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5030 | Py_DECREF(unicode); |
| 5031 | return NULL; |
| 5032 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 5033 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 5034 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5035 | return (PyObject *)unicode; |
| 5036 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5037 | onError: |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5038 | Py_DECREF(unicode); |
| 5039 | Py_XDECREF(errorHandler); |
| 5040 | Py_XDECREF(exc); |
| 5041 | return NULL; |
| 5042 | } |
| 5043 | |
| 5044 | PyObject * |
| 5045 | PyUnicode_EncodeUTF32(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5046 | Py_ssize_t size, |
| 5047 | const char *errors, |
| 5048 | int byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5049 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5050 | PyObject *v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5051 | unsigned char *p; |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5052 | Py_ssize_t nsize, bytesize; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5053 | #ifndef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5054 | Py_ssize_t i, pairs; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5055 | #else |
| 5056 | const int pairs = 0; |
| 5057 | #endif |
| 5058 | /* Offsets from p for storing byte pairs in the right order. */ |
| 5059 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5060 | int iorder[] = {0, 1, 2, 3}; |
| 5061 | #else |
| 5062 | int iorder[] = {3, 2, 1, 0}; |
| 5063 | #endif |
| 5064 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5065 | #define STORECHAR(CH) \ |
| 5066 | do { \ |
| 5067 | p[iorder[3]] = ((CH) >> 24) & 0xff; \ |
| 5068 | p[iorder[2]] = ((CH) >> 16) & 0xff; \ |
| 5069 | p[iorder[1]] = ((CH) >> 8) & 0xff; \ |
| 5070 | p[iorder[0]] = (CH) & 0xff; \ |
| 5071 | p += 4; \ |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5072 | } while(0) |
| 5073 | |
| 5074 | /* In narrow builds we can output surrogate pairs as one codepoint, |
| 5075 | so we need less space. */ |
| 5076 | #ifndef Py_UNICODE_WIDE |
| 5077 | for (i = pairs = 0; i < size-1; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5078 | if (0xD800 <= s[i] && s[i] <= 0xDBFF && |
| 5079 | 0xDC00 <= s[i+1] && s[i+1] <= 0xDFFF) |
| 5080 | pairs++; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5081 | #endif |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5082 | nsize = (size - pairs + (byteorder == 0)); |
| 5083 | bytesize = nsize * 4; |
| 5084 | if (bytesize / 4 != nsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5085 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5086 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5087 | if (v == NULL) |
| 5088 | return NULL; |
| 5089 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5090 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5091 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5092 | STORECHAR(0xFEFF); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5093 | if (size == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5094 | goto done; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5095 | |
| 5096 | if (byteorder == -1) { |
| 5097 | /* force LE */ |
| 5098 | iorder[0] = 0; |
| 5099 | iorder[1] = 1; |
| 5100 | iorder[2] = 2; |
| 5101 | iorder[3] = 3; |
| 5102 | } |
| 5103 | else if (byteorder == 1) { |
| 5104 | /* force BE */ |
| 5105 | iorder[0] = 3; |
| 5106 | iorder[1] = 2; |
| 5107 | iorder[2] = 1; |
| 5108 | iorder[3] = 0; |
| 5109 | } |
| 5110 | |
| 5111 | while (size-- > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5112 | Py_UCS4 ch = *s++; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5113 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5114 | if (0xD800 <= ch && ch <= 0xDBFF && size > 0) { |
| 5115 | Py_UCS4 ch2 = *s; |
| 5116 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
| 5117 | ch = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
| 5118 | s++; |
| 5119 | size--; |
| 5120 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5121 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5122 | #endif |
| 5123 | STORECHAR(ch); |
| 5124 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5125 | |
| 5126 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5127 | return v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5128 | #undef STORECHAR |
| 5129 | } |
| 5130 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5131 | PyObject * |
| 5132 | PyUnicode_AsUTF32String(PyObject *unicode) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5133 | { |
| 5134 | if (!PyUnicode_Check(unicode)) { |
| 5135 | PyErr_BadArgument(); |
| 5136 | return NULL; |
| 5137 | } |
| 5138 | return PyUnicode_EncodeUTF32(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5139 | PyUnicode_GET_SIZE(unicode), |
| 5140 | NULL, |
| 5141 | 0); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5142 | } |
| 5143 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5144 | /* --- UTF-16 Codec ------------------------------------------------------- */ |
| 5145 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5146 | PyObject * |
| 5147 | PyUnicode_DecodeUTF16(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5148 | Py_ssize_t size, |
| 5149 | const char *errors, |
| 5150 | int *byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5151 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5152 | return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL); |
| 5153 | } |
| 5154 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5155 | /* Two masks for fast checking of whether a C 'long' may contain |
| 5156 | UTF16-encoded surrogate characters. This is an efficient heuristic, |
| 5157 | assuming that non-surrogate characters with a code point >= 0x8000 are |
| 5158 | rare in most input. |
| 5159 | FAST_CHAR_MASK is used when the input is in native byte ordering, |
| 5160 | SWAPPED_FAST_CHAR_MASK when the input is in byteswapped ordering. |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5161 | */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5162 | #if (SIZEOF_LONG == 8) |
| 5163 | # define FAST_CHAR_MASK 0x8000800080008000L |
| 5164 | # define SWAPPED_FAST_CHAR_MASK 0x0080008000800080L |
| 5165 | #elif (SIZEOF_LONG == 4) |
| 5166 | # define FAST_CHAR_MASK 0x80008000L |
| 5167 | # define SWAPPED_FAST_CHAR_MASK 0x00800080L |
| 5168 | #else |
| 5169 | # error C 'long' size should be either 4 or 8! |
| 5170 | #endif |
| 5171 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5172 | PyObject * |
| 5173 | PyUnicode_DecodeUTF16Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5174 | Py_ssize_t size, |
| 5175 | const char *errors, |
| 5176 | int *byteorder, |
| 5177 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5178 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5179 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5180 | Py_ssize_t startinpos; |
| 5181 | Py_ssize_t endinpos; |
| 5182 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5183 | PyUnicodeObject *unicode; |
| 5184 | Py_UNICODE *p; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5185 | const unsigned char *q, *e, *aligned_end; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5186 | int bo = 0; /* assume native ordering by default */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5187 | int native_ordering = 0; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 5188 | const char *errmsg = ""; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5189 | /* Offsets from q for retrieving byte pairs in the right order. */ |
| 5190 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5191 | int ihi = 1, ilo = 0; |
| 5192 | #else |
| 5193 | int ihi = 0, ilo = 1; |
| 5194 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5195 | PyObject *errorHandler = NULL; |
| 5196 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5197 | |
| 5198 | /* Note: size will always be longer than the resulting Unicode |
| 5199 | character count */ |
| 5200 | unicode = _PyUnicode_New(size); |
| 5201 | if (!unicode) |
| 5202 | return NULL; |
| 5203 | if (size == 0) |
| 5204 | return (PyObject *)unicode; |
| 5205 | |
| 5206 | /* Unpack UTF-16 encoded data */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5207 | p = PyUnicode_AS_UNICODE(unicode); |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5208 | q = (unsigned char *)s; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5209 | e = q + size - 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5210 | |
| 5211 | if (byteorder) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5212 | bo = *byteorder; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5213 | |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5214 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 5215 | byte order setting accordingly. In native mode, the leading BOM |
| 5216 | mark is skipped, in all other modes, it is copied to the output |
| 5217 | stream as-is (giving a ZWNBSP character). */ |
| 5218 | if (bo == 0) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5219 | if (size >= 2) { |
| 5220 | const Py_UNICODE bom = (q[ihi] << 8) | q[ilo]; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5221 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5222 | if (bom == 0xFEFF) { |
| 5223 | q += 2; |
| 5224 | bo = -1; |
| 5225 | } |
| 5226 | else if (bom == 0xFFFE) { |
| 5227 | q += 2; |
| 5228 | bo = 1; |
| 5229 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5230 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5231 | if (bom == 0xFEFF) { |
| 5232 | q += 2; |
| 5233 | bo = 1; |
| 5234 | } |
| 5235 | else if (bom == 0xFFFE) { |
| 5236 | q += 2; |
| 5237 | bo = -1; |
| 5238 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5239 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5240 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5241 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5242 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5243 | if (bo == -1) { |
| 5244 | /* force LE */ |
| 5245 | ihi = 1; |
| 5246 | ilo = 0; |
| 5247 | } |
| 5248 | else if (bo == 1) { |
| 5249 | /* force BE */ |
| 5250 | ihi = 0; |
| 5251 | ilo = 1; |
| 5252 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5253 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5254 | native_ordering = ilo < ihi; |
| 5255 | #else |
| 5256 | native_ordering = ilo > ihi; |
| 5257 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5258 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5259 | aligned_end = (const unsigned char *) ((size_t) e & ~LONG_PTR_MASK); |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5260 | while (q < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5261 | Py_UNICODE ch; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5262 | /* First check for possible aligned read of a C 'long'. Unaligned |
| 5263 | reads are more expensive, better to defer to another iteration. */ |
| 5264 | if (!((size_t) q & LONG_PTR_MASK)) { |
| 5265 | /* Fast path for runs of non-surrogate chars. */ |
| 5266 | register const unsigned char *_q = q; |
| 5267 | Py_UNICODE *_p = p; |
| 5268 | if (native_ordering) { |
| 5269 | /* Native ordering is simple: as long as the input cannot |
| 5270 | possibly contain a surrogate char, do an unrolled copy |
| 5271 | of several 16-bit code points to the target object. |
| 5272 | The non-surrogate check is done on several input bytes |
| 5273 | at a time (as many as a C 'long' can contain). */ |
| 5274 | while (_q < aligned_end) { |
| 5275 | unsigned long data = * (unsigned long *) _q; |
| 5276 | if (data & FAST_CHAR_MASK) |
| 5277 | break; |
| 5278 | _p[0] = ((unsigned short *) _q)[0]; |
| 5279 | _p[1] = ((unsigned short *) _q)[1]; |
| 5280 | #if (SIZEOF_LONG == 8) |
| 5281 | _p[2] = ((unsigned short *) _q)[2]; |
| 5282 | _p[3] = ((unsigned short *) _q)[3]; |
| 5283 | #endif |
| 5284 | _q += SIZEOF_LONG; |
| 5285 | _p += SIZEOF_LONG / 2; |
| 5286 | } |
| 5287 | } |
| 5288 | else { |
| 5289 | /* Byteswapped ordering is similar, but we must decompose |
| 5290 | the copy bytewise, and take care of zero'ing out the |
| 5291 | upper bytes if the target object is in 32-bit units |
| 5292 | (that is, in UCS-4 builds). */ |
| 5293 | while (_q < aligned_end) { |
| 5294 | unsigned long data = * (unsigned long *) _q; |
| 5295 | if (data & SWAPPED_FAST_CHAR_MASK) |
| 5296 | break; |
| 5297 | /* Zero upper bytes in UCS-4 builds */ |
| 5298 | #if (Py_UNICODE_SIZE > 2) |
| 5299 | _p[0] = 0; |
| 5300 | _p[1] = 0; |
| 5301 | #if (SIZEOF_LONG == 8) |
| 5302 | _p[2] = 0; |
| 5303 | _p[3] = 0; |
| 5304 | #endif |
| 5305 | #endif |
Antoine Pitrou | d6e8de1 | 2009-01-11 23:56:55 +0000 | [diff] [blame] | 5306 | /* Issue #4916; UCS-4 builds on big endian machines must |
| 5307 | fill the two last bytes of each 4-byte unit. */ |
| 5308 | #if (!defined(BYTEORDER_IS_LITTLE_ENDIAN) && Py_UNICODE_SIZE > 2) |
| 5309 | # define OFF 2 |
| 5310 | #else |
| 5311 | # define OFF 0 |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5312 | #endif |
Antoine Pitrou | d6e8de1 | 2009-01-11 23:56:55 +0000 | [diff] [blame] | 5313 | ((unsigned char *) _p)[OFF + 1] = _q[0]; |
| 5314 | ((unsigned char *) _p)[OFF + 0] = _q[1]; |
| 5315 | ((unsigned char *) _p)[OFF + 1 + Py_UNICODE_SIZE] = _q[2]; |
| 5316 | ((unsigned char *) _p)[OFF + 0 + Py_UNICODE_SIZE] = _q[3]; |
| 5317 | #if (SIZEOF_LONG == 8) |
| 5318 | ((unsigned char *) _p)[OFF + 1 + 2 * Py_UNICODE_SIZE] = _q[4]; |
| 5319 | ((unsigned char *) _p)[OFF + 0 + 2 * Py_UNICODE_SIZE] = _q[5]; |
| 5320 | ((unsigned char *) _p)[OFF + 1 + 3 * Py_UNICODE_SIZE] = _q[6]; |
| 5321 | ((unsigned char *) _p)[OFF + 0 + 3 * Py_UNICODE_SIZE] = _q[7]; |
| 5322 | #endif |
| 5323 | #undef OFF |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5324 | _q += SIZEOF_LONG; |
| 5325 | _p += SIZEOF_LONG / 2; |
| 5326 | } |
| 5327 | } |
| 5328 | p = _p; |
| 5329 | q = _q; |
| 5330 | if (q >= e) |
| 5331 | break; |
| 5332 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5333 | ch = (q[ihi] << 8) | q[ilo]; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5334 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5335 | q += 2; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5336 | |
| 5337 | if (ch < 0xD800 || ch > 0xDFFF) { |
| 5338 | *p++ = ch; |
| 5339 | continue; |
| 5340 | } |
| 5341 | |
| 5342 | /* UTF-16 code pair: */ |
| 5343 | if (q > e) { |
| 5344 | errmsg = "unexpected end of data"; |
| 5345 | startinpos = (((const char *)q) - 2) - starts; |
| 5346 | endinpos = ((const char *)e) + 1 - starts; |
| 5347 | goto utf16Error; |
| 5348 | } |
| 5349 | if (0xD800 <= ch && ch <= 0xDBFF) { |
| 5350 | Py_UNICODE ch2 = (q[ihi] << 8) | q[ilo]; |
| 5351 | q += 2; |
| 5352 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 5353 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5354 | *p++ = ch; |
| 5355 | *p++ = ch2; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5356 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5357 | *p++ = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5358 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5359 | continue; |
| 5360 | } |
| 5361 | else { |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5362 | errmsg = "illegal UTF-16 surrogate"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5363 | startinpos = (((const char *)q)-4)-starts; |
| 5364 | endinpos = startinpos+2; |
| 5365 | goto utf16Error; |
| 5366 | } |
| 5367 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5368 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5369 | errmsg = "illegal encoding"; |
| 5370 | startinpos = (((const char *)q)-2)-starts; |
| 5371 | endinpos = startinpos+2; |
| 5372 | /* Fall through to report the error */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5373 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5374 | utf16Error: |
| 5375 | outpos = p - PyUnicode_AS_UNICODE(unicode); |
| 5376 | if (unicode_decode_call_errorhandler( |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5377 | errors, |
| 5378 | &errorHandler, |
| 5379 | "utf16", errmsg, |
| 5380 | &starts, |
| 5381 | (const char **)&e, |
| 5382 | &startinpos, |
| 5383 | &endinpos, |
| 5384 | &exc, |
| 5385 | (const char **)&q, |
| 5386 | &unicode, |
| 5387 | &outpos, |
| 5388 | &p)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5389 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5390 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5391 | /* remaining byte at the end? (size should be even) */ |
| 5392 | if (e == q) { |
| 5393 | if (!consumed) { |
| 5394 | errmsg = "truncated data"; |
| 5395 | startinpos = ((const char *)q) - starts; |
| 5396 | endinpos = ((const char *)e) + 1 - starts; |
| 5397 | outpos = p - PyUnicode_AS_UNICODE(unicode); |
| 5398 | if (unicode_decode_call_errorhandler( |
| 5399 | errors, |
| 5400 | &errorHandler, |
| 5401 | "utf16", errmsg, |
| 5402 | &starts, |
| 5403 | (const char **)&e, |
| 5404 | &startinpos, |
| 5405 | &endinpos, |
| 5406 | &exc, |
| 5407 | (const char **)&q, |
| 5408 | &unicode, |
| 5409 | &outpos, |
| 5410 | &p)) |
| 5411 | goto onError; |
| 5412 | /* The remaining input chars are ignored if the callback |
| 5413 | chooses to skip the input */ |
| 5414 | } |
| 5415 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5416 | |
| 5417 | if (byteorder) |
| 5418 | *byteorder = bo; |
| 5419 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5420 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5421 | *consumed = (const char *)q-starts; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5422 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5423 | /* Adjust length */ |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 5424 | if (PyUnicode_Resize((PyObject**)&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5425 | goto onError; |
| 5426 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5427 | Py_XDECREF(errorHandler); |
| 5428 | Py_XDECREF(exc); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 5429 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 5430 | if (_PyUnicode_READY_REPLACE(&unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5431 | Py_DECREF(unicode); |
| 5432 | return NULL; |
| 5433 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 5434 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 5435 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5436 | return (PyObject *)unicode; |
| 5437 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5438 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5439 | Py_DECREF(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5440 | Py_XDECREF(errorHandler); |
| 5441 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5442 | return NULL; |
| 5443 | } |
| 5444 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5445 | #undef FAST_CHAR_MASK |
| 5446 | #undef SWAPPED_FAST_CHAR_MASK |
| 5447 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5448 | PyObject * |
| 5449 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5450 | Py_ssize_t size, |
| 5451 | const char *errors, |
| 5452 | int byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5453 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5454 | PyObject *v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5455 | unsigned char *p; |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5456 | Py_ssize_t nsize, bytesize; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 5457 | #ifdef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5458 | Py_ssize_t i, pairs; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 5459 | #else |
| 5460 | const int pairs = 0; |
| 5461 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5462 | /* Offsets from p for storing byte pairs in the right order. */ |
| 5463 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5464 | int ihi = 1, ilo = 0; |
| 5465 | #else |
| 5466 | int ihi = 0, ilo = 1; |
| 5467 | #endif |
| 5468 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5469 | #define STORECHAR(CH) \ |
| 5470 | do { \ |
| 5471 | p[ihi] = ((CH) >> 8) & 0xff; \ |
| 5472 | p[ilo] = (CH) & 0xff; \ |
| 5473 | p += 2; \ |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5474 | } while(0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5475 | |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 5476 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5477 | for (i = pairs = 0; i < size; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5478 | if (s[i] >= 0x10000) |
| 5479 | pairs++; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 5480 | #endif |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5481 | /* 2 * (size + pairs + (byteorder == 0)) */ |
| 5482 | if (size > PY_SSIZE_T_MAX || |
| 5483 | size > PY_SSIZE_T_MAX - pairs - (byteorder == 0)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5484 | return PyErr_NoMemory(); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5485 | nsize = size + pairs + (byteorder == 0); |
| 5486 | bytesize = nsize * 2; |
| 5487 | if (bytesize / 2 != nsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5488 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5489 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5490 | if (v == NULL) |
| 5491 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5492 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5493 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5494 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5495 | STORECHAR(0xFEFF); |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 5496 | if (size == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5497 | goto done; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5498 | |
| 5499 | if (byteorder == -1) { |
| 5500 | /* force LE */ |
| 5501 | ihi = 1; |
| 5502 | ilo = 0; |
| 5503 | } |
| 5504 | else if (byteorder == 1) { |
| 5505 | /* force BE */ |
| 5506 | ihi = 0; |
| 5507 | ilo = 1; |
| 5508 | } |
| 5509 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5510 | while (size-- > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5511 | Py_UNICODE ch = *s++; |
| 5512 | Py_UNICODE ch2 = 0; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 5513 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5514 | if (ch >= 0x10000) { |
| 5515 | ch2 = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 5516 | ch = 0xD800 | ((ch-0x10000) >> 10); |
| 5517 | } |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 5518 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5519 | STORECHAR(ch); |
| 5520 | if (ch2) |
| 5521 | STORECHAR(ch2); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5522 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5523 | |
| 5524 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5525 | return v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5526 | #undef STORECHAR |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5527 | } |
| 5528 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5529 | PyObject * |
| 5530 | PyUnicode_AsUTF16String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5531 | { |
| 5532 | if (!PyUnicode_Check(unicode)) { |
| 5533 | PyErr_BadArgument(); |
| 5534 | return NULL; |
| 5535 | } |
| 5536 | return PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5537 | PyUnicode_GET_SIZE(unicode), |
| 5538 | NULL, |
| 5539 | 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5540 | } |
| 5541 | |
| 5542 | /* --- Unicode Escape Codec ----------------------------------------------- */ |
| 5543 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5544 | /* Helper function for PyUnicode_DecodeUnicodeEscape, determines |
| 5545 | if all the escapes in the string make it still a valid ASCII string. |
| 5546 | Returns -1 if any escapes were found which cause the string to |
| 5547 | pop out of ASCII range. Otherwise returns the length of the |
| 5548 | required buffer to hold the string. |
| 5549 | */ |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 5550 | static Py_ssize_t |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5551 | length_of_escaped_ascii_string(const char *s, Py_ssize_t size) |
| 5552 | { |
| 5553 | const unsigned char *p = (const unsigned char *)s; |
| 5554 | const unsigned char *end = p + size; |
| 5555 | Py_ssize_t length = 0; |
| 5556 | |
| 5557 | if (size < 0) |
| 5558 | return -1; |
| 5559 | |
| 5560 | for (; p < end; ++p) { |
| 5561 | if (*p > 127) { |
| 5562 | /* Non-ASCII */ |
| 5563 | return -1; |
| 5564 | } |
| 5565 | else if (*p != '\\') { |
| 5566 | /* Normal character */ |
| 5567 | ++length; |
| 5568 | } |
| 5569 | else { |
| 5570 | /* Backslash-escape, check next char */ |
| 5571 | ++p; |
| 5572 | /* Escape sequence reaches till end of string or |
| 5573 | non-ASCII follow-up. */ |
| 5574 | if (p >= end || *p > 127) |
| 5575 | return -1; |
| 5576 | switch (*p) { |
| 5577 | case '\n': |
| 5578 | /* backslash + \n result in zero characters */ |
| 5579 | break; |
| 5580 | case '\\': case '\'': case '\"': |
| 5581 | case 'b': case 'f': case 't': |
| 5582 | case 'n': case 'r': case 'v': case 'a': |
| 5583 | ++length; |
| 5584 | break; |
| 5585 | case '0': case '1': case '2': case '3': |
| 5586 | case '4': case '5': case '6': case '7': |
| 5587 | case 'x': case 'u': case 'U': case 'N': |
| 5588 | /* these do not guarantee ASCII characters */ |
| 5589 | return -1; |
| 5590 | default: |
| 5591 | /* count the backslash + the other character */ |
| 5592 | length += 2; |
| 5593 | } |
| 5594 | } |
| 5595 | } |
| 5596 | return length; |
| 5597 | } |
| 5598 | |
| 5599 | /* Similar to PyUnicode_WRITE but either write into wstr field |
| 5600 | or treat string as ASCII. */ |
| 5601 | #define WRITE_ASCII_OR_WSTR(kind, buf, index, value) \ |
| 5602 | do { \ |
| 5603 | if ((kind) != PyUnicode_WCHAR_KIND) \ |
| 5604 | ((unsigned char *)(buf))[(index)] = (unsigned char)(value); \ |
| 5605 | else \ |
| 5606 | ((Py_UNICODE *)(buf))[(index)] = (Py_UNICODE)(value); \ |
| 5607 | } while (0) |
| 5608 | |
| 5609 | #define WRITE_WSTR(buf, index, value) \ |
| 5610 | assert(kind == PyUnicode_WCHAR_KIND), \ |
| 5611 | ((Py_UNICODE *)(buf))[(index)] = (Py_UNICODE)(value) |
| 5612 | |
| 5613 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 5614 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; |
Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 5615 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5616 | PyObject * |
| 5617 | PyUnicode_DecodeUnicodeEscape(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5618 | Py_ssize_t size, |
Victor Stinner | c17f540 | 2011-09-29 00:16:58 +0200 | [diff] [blame] | 5619 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5620 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5621 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5622 | Py_ssize_t startinpos; |
| 5623 | Py_ssize_t endinpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5624 | int j; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5625 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5626 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5627 | const char *end; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5628 | char* message; |
| 5629 | Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5630 | PyObject *errorHandler = NULL; |
| 5631 | PyObject *exc = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5632 | Py_ssize_t ascii_length; |
| 5633 | Py_ssize_t i; |
| 5634 | int kind; |
| 5635 | void *data; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5636 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5637 | ascii_length = length_of_escaped_ascii_string(s, size); |
| 5638 | |
| 5639 | /* After length_of_escaped_ascii_string() there are two alternatives, |
| 5640 | either the string is pure ASCII with named escapes like \n, etc. |
| 5641 | and we determined it's exact size (common case) |
| 5642 | or it contains \x, \u, ... escape sequences. then we create a |
| 5643 | legacy wchar string and resize it at the end of this function. */ |
| 5644 | if (ascii_length >= 0) { |
| 5645 | v = (PyUnicodeObject *)PyUnicode_New(ascii_length, 127); |
| 5646 | if (!v) |
| 5647 | goto onError; |
| 5648 | assert(PyUnicode_KIND(v) == PyUnicode_1BYTE_KIND); |
| 5649 | kind = PyUnicode_1BYTE_KIND; |
| 5650 | data = PyUnicode_DATA(v); |
| 5651 | } |
| 5652 | else { |
| 5653 | /* Escaped strings will always be longer than the resulting |
| 5654 | Unicode string, so we start with size here and then reduce the |
| 5655 | length after conversion to the true value. |
| 5656 | (but if the error callback returns a long replacement string |
| 5657 | we'll have to allocate more space) */ |
| 5658 | v = _PyUnicode_New(size); |
| 5659 | if (!v) |
| 5660 | goto onError; |
| 5661 | kind = PyUnicode_WCHAR_KIND; |
| 5662 | data = PyUnicode_AS_UNICODE(v); |
| 5663 | } |
| 5664 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5665 | if (size == 0) |
| 5666 | return (PyObject *)v; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5667 | i = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5668 | end = s + size; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5669 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5670 | while (s < end) { |
| 5671 | unsigned char c; |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 5672 | Py_UNICODE x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5673 | int digits; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5674 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5675 | if (kind == PyUnicode_WCHAR_KIND) { |
| 5676 | assert(i < _PyUnicode_WSTR_LENGTH(v)); |
| 5677 | } |
| 5678 | else { |
| 5679 | /* The only case in which i == ascii_length is a backslash |
| 5680 | followed by a newline. */ |
| 5681 | assert(i <= ascii_length); |
| 5682 | } |
| 5683 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5684 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 5685 | if (*s != '\\') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5686 | WRITE_ASCII_OR_WSTR(kind, data, i++, (unsigned char) *s++); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5687 | continue; |
| 5688 | } |
| 5689 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5690 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5691 | /* \ - Escapes */ |
| 5692 | s++; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5693 | c = *s++; |
| 5694 | if (s > end) |
| 5695 | c = '\0'; /* Invalid after \ */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5696 | |
| 5697 | if (kind == PyUnicode_WCHAR_KIND) { |
| 5698 | assert(i < _PyUnicode_WSTR_LENGTH(v)); |
| 5699 | } |
| 5700 | else { |
| 5701 | /* The only case in which i == ascii_length is a backslash |
| 5702 | followed by a newline. */ |
| 5703 | assert(i < ascii_length || (i == ascii_length && c == '\n')); |
| 5704 | } |
| 5705 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5706 | switch (c) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5707 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5708 | /* \x escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5709 | case '\n': break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5710 | case '\\': WRITE_ASCII_OR_WSTR(kind, data, i++, '\\'); break; |
| 5711 | case '\'': WRITE_ASCII_OR_WSTR(kind, data, i++, '\''); break; |
| 5712 | case '\"': WRITE_ASCII_OR_WSTR(kind, data, i++, '\"'); break; |
| 5713 | case 'b': WRITE_ASCII_OR_WSTR(kind, data, i++, '\b'); break; |
| 5714 | /* FF */ |
| 5715 | case 'f': WRITE_ASCII_OR_WSTR(kind, data, i++, '\014'); break; |
| 5716 | case 't': WRITE_ASCII_OR_WSTR(kind, data, i++, '\t'); break; |
| 5717 | case 'n': WRITE_ASCII_OR_WSTR(kind, data, i++, '\n'); break; |
| 5718 | case 'r': WRITE_ASCII_OR_WSTR(kind, data, i++, '\r'); break; |
| 5719 | /* VT */ |
| 5720 | case 'v': WRITE_ASCII_OR_WSTR(kind, data, i++, '\013'); break; |
| 5721 | /* BEL, not classic C */ |
| 5722 | case 'a': WRITE_ASCII_OR_WSTR(kind, data, i++, '\007'); break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5723 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5724 | /* \OOO (octal) escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5725 | case '0': case '1': case '2': case '3': |
| 5726 | case '4': case '5': case '6': case '7': |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5727 | x = s[-1] - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5728 | if (s < end && '0' <= *s && *s <= '7') { |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5729 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5730 | if (s < end && '0' <= *s && *s <= '7') |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5731 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5732 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5733 | WRITE_WSTR(data, i++, x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5734 | break; |
| 5735 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5736 | /* hex escapes */ |
| 5737 | /* \xXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5738 | case 'x': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5739 | digits = 2; |
| 5740 | message = "truncated \\xXX escape"; |
| 5741 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5742 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5743 | /* \uXXXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5744 | case 'u': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5745 | digits = 4; |
| 5746 | message = "truncated \\uXXXX escape"; |
| 5747 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5748 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5749 | /* \UXXXXXXXX */ |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5750 | case 'U': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5751 | digits = 8; |
| 5752 | message = "truncated \\UXXXXXXXX escape"; |
| 5753 | hexescape: |
| 5754 | chr = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5755 | p = PyUnicode_AS_UNICODE(v) + i; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5756 | if (s+digits>end) { |
| 5757 | endinpos = size; |
| 5758 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5759 | errors, &errorHandler, |
| 5760 | "unicodeescape", "end of string in escape sequence", |
| 5761 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5762 | &v, &i, &p)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5763 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5764 | data = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5765 | goto nextByte; |
| 5766 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5767 | for (j = 0; j < digits; ++j) { |
| 5768 | c = (unsigned char) s[j]; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 5769 | if (!Py_ISXDIGIT(c)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5770 | endinpos = (s+j+1)-starts; |
| 5771 | p = PyUnicode_AS_UNICODE(v) + i; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5772 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5773 | errors, &errorHandler, |
| 5774 | "unicodeescape", message, |
| 5775 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5776 | &v, &i, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5777 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5778 | data = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5779 | goto nextByte; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5780 | } |
| 5781 | chr = (chr<<4) & ~0xF; |
| 5782 | if (c >= '0' && c <= '9') |
| 5783 | chr += c - '0'; |
| 5784 | else if (c >= 'a' && c <= 'f') |
| 5785 | chr += 10 + c - 'a'; |
| 5786 | else |
| 5787 | chr += 10 + c - 'A'; |
| 5788 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5789 | s += j; |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 5790 | if (chr == 0xffffffff && PyErr_Occurred()) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5791 | /* _decoding_error will have already written into the |
| 5792 | target buffer. */ |
| 5793 | break; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5794 | store: |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5795 | /* when we get here, chr is a 32-bit unicode character */ |
| 5796 | if (chr <= 0xffff) |
| 5797 | /* UCS-2 character */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5798 | WRITE_WSTR(data, i++, chr); |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5799 | else if (chr <= 0x10ffff) { |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 5800 | /* UCS-4 character. Either store directly, or as |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5801 | surrogate pair. */ |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 5802 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5803 | WRITE_WSTR(data, i++, chr); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5804 | #else |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5805 | chr -= 0x10000L; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5806 | WRITE_WSTR(data, i++, 0xD800 + (Py_UNICODE) (chr >> 10)); |
| 5807 | WRITE_WSTR(data, i++, 0xDC00 + (Py_UNICODE) (chr & 0x03FF)); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5808 | #endif |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5809 | } else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5810 | endinpos = s-starts; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5811 | p = PyUnicode_AS_UNICODE(v) + i; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5812 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5813 | errors, &errorHandler, |
| 5814 | "unicodeescape", "illegal Unicode character", |
| 5815 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5816 | &v, &i, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5817 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5818 | data = PyUnicode_AS_UNICODE(v); |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5819 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5820 | break; |
| 5821 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5822 | /* \N{name} */ |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5823 | case 'N': |
| 5824 | message = "malformed \\N character escape"; |
| 5825 | if (ucnhash_CAPI == NULL) { |
| 5826 | /* load the unicode data module */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5827 | ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCapsule_Import( |
| 5828 | PyUnicodeData_CAPSULE_NAME, 1); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5829 | if (ucnhash_CAPI == NULL) |
| 5830 | goto ucnhashError; |
| 5831 | } |
| 5832 | if (*s == '{') { |
| 5833 | const char *start = s+1; |
| 5834 | /* look for the closing brace */ |
| 5835 | while (*s != '}' && s < end) |
| 5836 | s++; |
| 5837 | if (s > start && s < end && *s == '}') { |
| 5838 | /* found a name. look it up in the unicode database */ |
| 5839 | message = "unknown Unicode character name"; |
| 5840 | s++; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5841 | if (ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), |
Ezio Melotti | 931b8aa | 2011-10-21 21:57:36 +0300 | [diff] [blame] | 5842 | &chr, 0)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5843 | goto store; |
| 5844 | } |
| 5845 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5846 | endinpos = s-starts; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5847 | p = PyUnicode_AS_UNICODE(v) + i; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5848 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5849 | errors, &errorHandler, |
| 5850 | "unicodeescape", message, |
| 5851 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5852 | &v, &i, &p)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5853 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5854 | data = PyUnicode_AS_UNICODE(v); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5855 | break; |
| 5856 | |
| 5857 | default: |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5858 | if (s > end) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5859 | assert(kind == PyUnicode_WCHAR_KIND); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5860 | message = "\\ at end of string"; |
| 5861 | s--; |
| 5862 | endinpos = s-starts; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5863 | p = PyUnicode_AS_UNICODE(v) + i; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5864 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5865 | errors, &errorHandler, |
| 5866 | "unicodeescape", message, |
| 5867 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5868 | &v, &i, &p)) |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5869 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5870 | data = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5871 | } |
| 5872 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5873 | WRITE_ASCII_OR_WSTR(kind, data, i++, '\\'); |
| 5874 | WRITE_ASCII_OR_WSTR(kind, data, i++, (unsigned char)s[-1]); |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5875 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5876 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5877 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5878 | nextByte: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5879 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5880 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5881 | /* Ensure the length prediction worked in case of ASCII strings */ |
| 5882 | assert(kind == PyUnicode_WCHAR_KIND || i == ascii_length); |
| 5883 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 5884 | if (kind == PyUnicode_WCHAR_KIND) |
| 5885 | { |
| 5886 | if (PyUnicode_Resize((PyObject**)&v, i) < 0) |
| 5887 | goto onError; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 5888 | } |
Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 5889 | Py_XDECREF(errorHandler); |
| 5890 | Py_XDECREF(exc); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 5891 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 5892 | if (_PyUnicode_READY_REPLACE(&v)) { |
| 5893 | Py_DECREF(v); |
| 5894 | return NULL; |
| 5895 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 5896 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 5897 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5898 | return (PyObject *)v; |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5899 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5900 | ucnhashError: |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 5901 | PyErr_SetString( |
| 5902 | PyExc_UnicodeError, |
| 5903 | "\\N escapes not supported (can't load unicodedata module)" |
| 5904 | ); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 5905 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5906 | Py_XDECREF(errorHandler); |
| 5907 | Py_XDECREF(exc); |
Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 5908 | return NULL; |
| 5909 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5910 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5911 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5912 | Py_XDECREF(errorHandler); |
| 5913 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5914 | return NULL; |
| 5915 | } |
| 5916 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5917 | #undef WRITE_ASCII_OR_WSTR |
| 5918 | #undef WRITE_WSTR |
| 5919 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5920 | /* Return a Unicode-Escape string version of the Unicode object. |
| 5921 | |
| 5922 | If quotes is true, the string is enclosed in u"" or u'' quotes as |
| 5923 | appropriate. |
| 5924 | |
| 5925 | */ |
| 5926 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5927 | PyObject * |
| 5928 | PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5929 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5930 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5931 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5932 | char *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5933 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5934 | #ifdef Py_UNICODE_WIDE |
| 5935 | const Py_ssize_t expandsize = 10; |
| 5936 | #else |
| 5937 | const Py_ssize_t expandsize = 6; |
| 5938 | #endif |
| 5939 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5940 | /* XXX(nnorwitz): rather than over-allocating, it would be |
| 5941 | better to choose a different scheme. Perhaps scan the |
| 5942 | first N-chars of the string and allocate based on that size. |
| 5943 | */ |
| 5944 | /* Initial allocation is based on the longest-possible unichr |
| 5945 | escape. |
| 5946 | |
| 5947 | In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source |
| 5948 | unichr, so in this case it's the longest unichr escape. In |
| 5949 | narrow (UTF-16) builds this is five chars per source unichr |
| 5950 | since there are two unichrs in the surrogate pair, so in narrow |
| 5951 | (UTF-16) builds it's not the longest unichr escape. |
| 5952 | |
| 5953 | In wide or narrow builds '\uxxxx' is 6 chars per source unichr, |
| 5954 | so in the narrow (UTF-16) build case it's the longest unichr |
| 5955 | escape. |
| 5956 | */ |
| 5957 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5958 | if (size == 0) |
| 5959 | return PyBytes_FromStringAndSize(NULL, 0); |
| 5960 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5961 | if (size > (PY_SSIZE_T_MAX - 2 - 1) / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5962 | return PyErr_NoMemory(); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5963 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5964 | repr = PyBytes_FromStringAndSize(NULL, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5965 | 2 |
| 5966 | + expandsize*size |
| 5967 | + 1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5968 | if (repr == NULL) |
| 5969 | return NULL; |
| 5970 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5971 | p = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5972 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5973 | while (size-- > 0) { |
| 5974 | Py_UNICODE ch = *s++; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5975 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5976 | /* Escape backslashes */ |
| 5977 | if (ch == '\\') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5978 | *p++ = '\\'; |
| 5979 | *p++ = (char) ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5980 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5981 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5982 | |
Guido van Rossum | 0d42e0c | 2001-07-20 16:36:21 +0000 | [diff] [blame] | 5983 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5984 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 5985 | else if (ch >= 0x10000) { |
| 5986 | *p++ = '\\'; |
| 5987 | *p++ = 'U'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 5988 | *p++ = Py_hexdigits[(ch >> 28) & 0x0000000F]; |
| 5989 | *p++ = Py_hexdigits[(ch >> 24) & 0x0000000F]; |
| 5990 | *p++ = Py_hexdigits[(ch >> 20) & 0x0000000F]; |
| 5991 | *p++ = Py_hexdigits[(ch >> 16) & 0x0000000F]; |
| 5992 | *p++ = Py_hexdigits[(ch >> 12) & 0x0000000F]; |
| 5993 | *p++ = Py_hexdigits[(ch >> 8) & 0x0000000F]; |
| 5994 | *p++ = Py_hexdigits[(ch >> 4) & 0x0000000F]; |
| 5995 | *p++ = Py_hexdigits[ch & 0x0000000F]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5996 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5997 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5998 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5999 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
| 6000 | else if (ch >= 0xD800 && ch < 0xDC00) { |
| 6001 | Py_UNICODE ch2; |
| 6002 | Py_UCS4 ucs; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6003 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6004 | ch2 = *s++; |
| 6005 | size--; |
Georg Brandl | 78eef3de | 2010-08-01 20:51:02 +0000 | [diff] [blame] | 6006 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6007 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 6008 | *p++ = '\\'; |
| 6009 | *p++ = 'U'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6010 | *p++ = Py_hexdigits[(ucs >> 28) & 0x0000000F]; |
| 6011 | *p++ = Py_hexdigits[(ucs >> 24) & 0x0000000F]; |
| 6012 | *p++ = Py_hexdigits[(ucs >> 20) & 0x0000000F]; |
| 6013 | *p++ = Py_hexdigits[(ucs >> 16) & 0x0000000F]; |
| 6014 | *p++ = Py_hexdigits[(ucs >> 12) & 0x0000000F]; |
| 6015 | *p++ = Py_hexdigits[(ucs >> 8) & 0x0000000F]; |
| 6016 | *p++ = Py_hexdigits[(ucs >> 4) & 0x0000000F]; |
| 6017 | *p++ = Py_hexdigits[ucs & 0x0000000F]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6018 | continue; |
| 6019 | } |
| 6020 | /* Fall through: isolated surrogates are copied as-is */ |
| 6021 | s--; |
| 6022 | size++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6023 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6024 | #endif |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 6025 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6026 | /* Map 16-bit characters to '\uxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 6027 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6028 | *p++ = '\\'; |
| 6029 | *p++ = 'u'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6030 | *p++ = Py_hexdigits[(ch >> 12) & 0x000F]; |
| 6031 | *p++ = Py_hexdigits[(ch >> 8) & 0x000F]; |
| 6032 | *p++ = Py_hexdigits[(ch >> 4) & 0x000F]; |
| 6033 | *p++ = Py_hexdigits[ch & 0x000F]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6034 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 6035 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 6036 | /* Map special whitespace to '\t', \n', '\r' */ |
| 6037 | else if (ch == '\t') { |
| 6038 | *p++ = '\\'; |
| 6039 | *p++ = 't'; |
| 6040 | } |
| 6041 | else if (ch == '\n') { |
| 6042 | *p++ = '\\'; |
| 6043 | *p++ = 'n'; |
| 6044 | } |
| 6045 | else if (ch == '\r') { |
| 6046 | *p++ = '\\'; |
| 6047 | *p++ = 'r'; |
| 6048 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 6049 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 6050 | /* Map non-printable US ASCII to '\xhh' */ |
Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 6051 | else if (ch < ' ' || ch >= 0x7F) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6052 | *p++ = '\\'; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 6053 | *p++ = 'x'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6054 | *p++ = Py_hexdigits[(ch >> 4) & 0x000F]; |
| 6055 | *p++ = Py_hexdigits[ch & 0x000F]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6056 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 6057 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6058 | /* Copy everything else as-is */ |
| 6059 | else |
| 6060 | *p++ = (char) ch; |
| 6061 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6062 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6063 | assert(p - PyBytes_AS_STRING(repr) > 0); |
| 6064 | if (_PyBytes_Resize(&repr, p - PyBytes_AS_STRING(repr)) < 0) |
| 6065 | return NULL; |
| 6066 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6067 | } |
| 6068 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6069 | PyObject * |
| 6070 | PyUnicode_AsUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6071 | { |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 6072 | PyObject *s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6073 | if (!PyUnicode_Check(unicode)) { |
| 6074 | PyErr_BadArgument(); |
| 6075 | return NULL; |
| 6076 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 6077 | s = PyUnicode_EncodeUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 6078 | PyUnicode_GET_SIZE(unicode)); |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 6079 | return s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6080 | } |
| 6081 | |
| 6082 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ |
| 6083 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6084 | PyObject * |
| 6085 | PyUnicode_DecodeRawUnicodeEscape(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6086 | Py_ssize_t size, |
| 6087 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6088 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6089 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6090 | Py_ssize_t startinpos; |
| 6091 | Py_ssize_t endinpos; |
| 6092 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6093 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6094 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6095 | const char *end; |
| 6096 | const char *bs; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6097 | PyObject *errorHandler = NULL; |
| 6098 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6099 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6100 | /* Escaped strings will always be longer than the resulting |
| 6101 | 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] | 6102 | length after conversion to the true value. (But decoding error |
| 6103 | handler might have to resize the string) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6104 | v = _PyUnicode_New(size); |
| 6105 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6106 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6107 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6108 | return (PyObject *)v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6109 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6110 | end = s + size; |
| 6111 | while (s < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6112 | unsigned char c; |
| 6113 | Py_UCS4 x; |
| 6114 | int i; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6115 | int count; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6116 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6117 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 6118 | if (*s != '\\') { |
| 6119 | *p++ = (unsigned char)*s++; |
| 6120 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6121 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6122 | startinpos = s-starts; |
| 6123 | |
| 6124 | /* \u-escapes are only interpreted iff the number of leading |
| 6125 | backslashes if odd */ |
| 6126 | bs = s; |
| 6127 | for (;s < end;) { |
| 6128 | if (*s != '\\') |
| 6129 | break; |
| 6130 | *p++ = (unsigned char)*s++; |
| 6131 | } |
| 6132 | if (((s - bs) & 1) == 0 || |
| 6133 | s >= end || |
| 6134 | (*s != 'u' && *s != 'U')) { |
| 6135 | continue; |
| 6136 | } |
| 6137 | p--; |
| 6138 | count = *s=='u' ? 4 : 8; |
| 6139 | s++; |
| 6140 | |
| 6141 | /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ |
| 6142 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 6143 | for (x = 0, i = 0; i < count; ++i, ++s) { |
| 6144 | c = (unsigned char)*s; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 6145 | if (!Py_ISXDIGIT(c)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6146 | endinpos = s-starts; |
| 6147 | if (unicode_decode_call_errorhandler( |
| 6148 | errors, &errorHandler, |
| 6149 | "rawunicodeescape", "truncated \\uXXXX", |
| 6150 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 6151 | &v, &outpos, &p)) |
| 6152 | goto onError; |
| 6153 | goto nextByte; |
| 6154 | } |
| 6155 | x = (x<<4) & ~0xF; |
| 6156 | if (c >= '0' && c <= '9') |
| 6157 | x += c - '0'; |
| 6158 | else if (c >= 'a' && c <= 'f') |
| 6159 | x += 10 + c - 'a'; |
| 6160 | else |
| 6161 | x += 10 + c - 'A'; |
| 6162 | } |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 6163 | if (x <= 0xffff) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6164 | /* UCS-2 character */ |
| 6165 | *p++ = (Py_UNICODE) x; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 6166 | else if (x <= 0x10ffff) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6167 | /* UCS-4 character. Either store directly, or as |
| 6168 | surrogate pair. */ |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 6169 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6170 | *p++ = (Py_UNICODE) x; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 6171 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6172 | x -= 0x10000L; |
| 6173 | *p++ = 0xD800 + (Py_UNICODE) (x >> 10); |
| 6174 | *p++ = 0xDC00 + (Py_UNICODE) (x & 0x03FF); |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 6175 | #endif |
| 6176 | } else { |
| 6177 | endinpos = s-starts; |
| 6178 | outpos = p-PyUnicode_AS_UNICODE(v); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6179 | if (unicode_decode_call_errorhandler( |
| 6180 | errors, &errorHandler, |
| 6181 | "rawunicodeescape", "\\Uxxxxxxxx out of range", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6182 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 6183 | &v, &outpos, &p)) |
| 6184 | goto onError; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6185 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6186 | nextByte: |
| 6187 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6188 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 6189 | if (PyUnicode_Resize((PyObject**)&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6190 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6191 | Py_XDECREF(errorHandler); |
| 6192 | Py_XDECREF(exc); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 6193 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 6194 | if (_PyUnicode_READY_REPLACE(&v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6195 | Py_DECREF(v); |
| 6196 | return NULL; |
| 6197 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 6198 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 6199 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6200 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6201 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6202 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6203 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6204 | Py_XDECREF(errorHandler); |
| 6205 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6206 | return NULL; |
| 6207 | } |
| 6208 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6209 | PyObject * |
| 6210 | PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6211 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6212 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6213 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6214 | char *p; |
| 6215 | char *q; |
| 6216 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6217 | #ifdef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 6218 | const Py_ssize_t expandsize = 10; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6219 | #else |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 6220 | const Py_ssize_t expandsize = 6; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6221 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6222 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 6223 | if (size > PY_SSIZE_T_MAX / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6224 | return PyErr_NoMemory(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6225 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6226 | repr = PyBytes_FromStringAndSize(NULL, expandsize * size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6227 | if (repr == NULL) |
| 6228 | return NULL; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 6229 | if (size == 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6230 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6231 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6232 | p = q = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6233 | while (size-- > 0) { |
| 6234 | Py_UNICODE ch = *s++; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6235 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6236 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 6237 | if (ch >= 0x10000) { |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6238 | *p++ = '\\'; |
| 6239 | *p++ = 'U'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6240 | *p++ = Py_hexdigits[(ch >> 28) & 0xf]; |
| 6241 | *p++ = Py_hexdigits[(ch >> 24) & 0xf]; |
| 6242 | *p++ = Py_hexdigits[(ch >> 20) & 0xf]; |
| 6243 | *p++ = Py_hexdigits[(ch >> 16) & 0xf]; |
| 6244 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 6245 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 6246 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 6247 | *p++ = Py_hexdigits[ch & 15]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6248 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6249 | else |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 6250 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6251 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
| 6252 | if (ch >= 0xD800 && ch < 0xDC00) { |
| 6253 | Py_UNICODE ch2; |
| 6254 | Py_UCS4 ucs; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 6255 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6256 | ch2 = *s++; |
| 6257 | size--; |
Georg Brandl | 78eef3de | 2010-08-01 20:51:02 +0000 | [diff] [blame] | 6258 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6259 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 6260 | *p++ = '\\'; |
| 6261 | *p++ = 'U'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6262 | *p++ = Py_hexdigits[(ucs >> 28) & 0xf]; |
| 6263 | *p++ = Py_hexdigits[(ucs >> 24) & 0xf]; |
| 6264 | *p++ = Py_hexdigits[(ucs >> 20) & 0xf]; |
| 6265 | *p++ = Py_hexdigits[(ucs >> 16) & 0xf]; |
| 6266 | *p++ = Py_hexdigits[(ucs >> 12) & 0xf]; |
| 6267 | *p++ = Py_hexdigits[(ucs >> 8) & 0xf]; |
| 6268 | *p++ = Py_hexdigits[(ucs >> 4) & 0xf]; |
| 6269 | *p++ = Py_hexdigits[ucs & 0xf]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6270 | continue; |
| 6271 | } |
| 6272 | /* Fall through: isolated surrogates are copied as-is */ |
| 6273 | s--; |
| 6274 | size++; |
| 6275 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6276 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6277 | /* Map 16-bit characters to '\uxxxx' */ |
| 6278 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6279 | *p++ = '\\'; |
| 6280 | *p++ = 'u'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6281 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 6282 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 6283 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 6284 | *p++ = Py_hexdigits[ch & 15]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6285 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6286 | /* Copy everything else as-is */ |
| 6287 | else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6288 | *p++ = (char) ch; |
| 6289 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6290 | size = p - q; |
| 6291 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6292 | assert(size > 0); |
| 6293 | if (_PyBytes_Resize(&repr, size) < 0) |
| 6294 | return NULL; |
| 6295 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6296 | } |
| 6297 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6298 | PyObject * |
| 6299 | PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6300 | { |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 6301 | PyObject *s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6302 | if (!PyUnicode_Check(unicode)) { |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 6303 | PyErr_BadArgument(); |
| 6304 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6305 | } |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 6306 | s = PyUnicode_EncodeRawUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 6307 | PyUnicode_GET_SIZE(unicode)); |
| 6308 | |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 6309 | return s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6310 | } |
| 6311 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6312 | /* --- Unicode Internal Codec ------------------------------------------- */ |
| 6313 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6314 | PyObject * |
| 6315 | _PyUnicode_DecodeUnicodeInternal(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6316 | Py_ssize_t size, |
| 6317 | const char *errors) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6318 | { |
| 6319 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6320 | Py_ssize_t startinpos; |
| 6321 | Py_ssize_t endinpos; |
| 6322 | Py_ssize_t outpos; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6323 | PyUnicodeObject *v; |
| 6324 | Py_UNICODE *p; |
| 6325 | const char *end; |
| 6326 | const char *reason; |
| 6327 | PyObject *errorHandler = NULL; |
| 6328 | PyObject *exc = NULL; |
| 6329 | |
Neal Norwitz | d43069c | 2006-01-08 01:12:10 +0000 | [diff] [blame] | 6330 | #ifdef Py_UNICODE_WIDE |
| 6331 | Py_UNICODE unimax = PyUnicode_GetMax(); |
| 6332 | #endif |
| 6333 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6334 | /* XXX overflow detection missing */ |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6335 | v = _PyUnicode_New((size+Py_UNICODE_SIZE-1)/ Py_UNICODE_SIZE); |
| 6336 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6337 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6338 | /* Intentionally PyUnicode_GET_SIZE instead of PyUnicode_GET_LENGTH |
| 6339 | as string was created with the old API. */ |
| 6340 | if (PyUnicode_GET_SIZE(v) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6341 | return (PyObject *)v; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6342 | p = PyUnicode_AS_UNICODE(v); |
| 6343 | end = s + size; |
| 6344 | |
| 6345 | while (s < end) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6346 | memcpy(p, s, sizeof(Py_UNICODE)); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6347 | /* We have to sanity check the raw data, otherwise doom looms for |
| 6348 | some malformed UCS-4 data. */ |
| 6349 | if ( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6350 | #ifdef Py_UNICODE_WIDE |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6351 | *p > unimax || *p < 0 || |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6352 | #endif |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6353 | end-s < Py_UNICODE_SIZE |
| 6354 | ) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6355 | { |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6356 | startinpos = s - starts; |
| 6357 | if (end-s < Py_UNICODE_SIZE) { |
| 6358 | endinpos = end-starts; |
| 6359 | reason = "truncated input"; |
| 6360 | } |
| 6361 | else { |
| 6362 | endinpos = s - starts + Py_UNICODE_SIZE; |
| 6363 | reason = "illegal code point (> 0x10FFFF)"; |
| 6364 | } |
| 6365 | outpos = p - PyUnicode_AS_UNICODE(v); |
| 6366 | if (unicode_decode_call_errorhandler( |
| 6367 | errors, &errorHandler, |
| 6368 | "unicode_internal", reason, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 6369 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 6370 | &v, &outpos, &p)) { |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6371 | goto onError; |
| 6372 | } |
| 6373 | } |
| 6374 | else { |
| 6375 | p++; |
| 6376 | s += Py_UNICODE_SIZE; |
| 6377 | } |
| 6378 | } |
| 6379 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 6380 | if (PyUnicode_Resize((PyObject**)&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6381 | goto onError; |
| 6382 | Py_XDECREF(errorHandler); |
| 6383 | Py_XDECREF(exc); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 6384 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 6385 | if (_PyUnicode_READY_REPLACE(&v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6386 | Py_DECREF(v); |
| 6387 | return NULL; |
| 6388 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 6389 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 6390 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6391 | return (PyObject *)v; |
| 6392 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6393 | onError: |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6394 | Py_XDECREF(v); |
| 6395 | Py_XDECREF(errorHandler); |
| 6396 | Py_XDECREF(exc); |
| 6397 | return NULL; |
| 6398 | } |
| 6399 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6400 | /* --- Latin-1 Codec ------------------------------------------------------ */ |
| 6401 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6402 | PyObject * |
| 6403 | PyUnicode_DecodeLatin1(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6404 | Py_ssize_t size, |
| 6405 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6406 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6407 | /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 6408 | return _PyUnicode_FromUCS1((unsigned char*)s, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6409 | } |
| 6410 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6411 | /* create or adjust a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6412 | static void |
| 6413 | make_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6414 | const char *encoding, |
| 6415 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 6416 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6417 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6418 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6419 | if (*exceptionObject == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6420 | *exceptionObject = PyUnicodeEncodeError_Create( |
| 6421 | encoding, unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6422 | } |
| 6423 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6424 | if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 6425 | goto onError; |
| 6426 | if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) |
| 6427 | goto onError; |
| 6428 | if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) |
| 6429 | goto onError; |
| 6430 | return; |
| 6431 | onError: |
| 6432 | Py_DECREF(*exceptionObject); |
| 6433 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6434 | } |
| 6435 | } |
| 6436 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6437 | /* raises a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6438 | static void |
| 6439 | raise_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6440 | const char *encoding, |
| 6441 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 6442 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6443 | const char *reason) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6444 | { |
| 6445 | make_encode_exception(exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6446 | encoding, unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6447 | if (*exceptionObject != NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6448 | PyCodec_StrictErrors(*exceptionObject); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6449 | } |
| 6450 | |
| 6451 | /* error handling callback helper: |
| 6452 | build arguments, call the callback and check the arguments, |
| 6453 | put the result into newpos and return the replacement string, which |
| 6454 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6455 | static PyObject * |
| 6456 | unicode_encode_call_errorhandler(const char *errors, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6457 | PyObject **errorHandler, |
| 6458 | const char *encoding, const char *reason, |
| 6459 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 6460 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6461 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6462 | { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6463 | static char *argparse = "On;encoding error handler must return (str/bytes, int) tuple"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6464 | |
| 6465 | PyObject *restuple; |
| 6466 | PyObject *resunicode; |
| 6467 | |
| 6468 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6469 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6470 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6471 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6472 | } |
| 6473 | |
| 6474 | make_encode_exception(exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6475 | encoding, unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6476 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6477 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6478 | |
| 6479 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6480 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6481 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6482 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6483 | if (!PyTuple_Check(restuple)) { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6484 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6485 | Py_DECREF(restuple); |
| 6486 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6487 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6488 | if (!PyArg_ParseTuple(restuple, argparse, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6489 | &resunicode, newpos)) { |
| 6490 | Py_DECREF(restuple); |
| 6491 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6492 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6493 | if (!PyUnicode_Check(resunicode) && !PyBytes_Check(resunicode)) { |
| 6494 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
| 6495 | Py_DECREF(restuple); |
| 6496 | return NULL; |
| 6497 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6498 | if (*newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6499 | *newpos = size+*newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 6500 | if (*newpos<0 || *newpos>size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6501 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 6502 | Py_DECREF(restuple); |
| 6503 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 6504 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6505 | Py_INCREF(resunicode); |
| 6506 | Py_DECREF(restuple); |
| 6507 | return resunicode; |
| 6508 | } |
| 6509 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6510 | static PyObject * |
| 6511 | unicode_encode_ucs1(const Py_UNICODE *p, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6512 | Py_ssize_t size, |
| 6513 | const char *errors, |
| 6514 | int limit) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6515 | { |
| 6516 | /* output object */ |
| 6517 | PyObject *res; |
| 6518 | /* pointers to the beginning and end+1 of input */ |
| 6519 | const Py_UNICODE *startp = p; |
| 6520 | const Py_UNICODE *endp = p + size; |
| 6521 | /* pointer to the beginning of the unencodable characters */ |
| 6522 | /* const Py_UNICODE *badp = NULL; */ |
| 6523 | /* pointer into the output */ |
| 6524 | char *str; |
| 6525 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6526 | Py_ssize_t ressize; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6527 | const char *encoding = (limit == 256) ? "latin-1" : "ascii"; |
| 6528 | 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] | 6529 | PyObject *errorHandler = NULL; |
| 6530 | PyObject *exc = NULL; |
| 6531 | /* the following variable is used for caching string comparisons |
| 6532 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 6533 | int known_errorHandler = -1; |
| 6534 | |
| 6535 | /* allocate enough for a simple encoding without |
| 6536 | replacements, if we need more, we'll resize */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6537 | if (size == 0) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 6538 | return PyBytes_FromStringAndSize(NULL, 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6539 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6540 | if (res == NULL) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6541 | return NULL; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6542 | str = PyBytes_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6543 | ressize = size; |
| 6544 | |
| 6545 | while (p<endp) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6546 | Py_UNICODE c = *p; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6547 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6548 | /* can we encode this? */ |
| 6549 | if (c<limit) { |
| 6550 | /* no overflow check, because we know that the space is enough */ |
| 6551 | *str++ = (char)c; |
| 6552 | ++p; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6553 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6554 | else { |
| 6555 | Py_ssize_t unicodepos = p-startp; |
| 6556 | Py_ssize_t requiredsize; |
| 6557 | PyObject *repunicode; |
| 6558 | Py_ssize_t repsize; |
| 6559 | Py_ssize_t newpos; |
| 6560 | Py_ssize_t respos; |
| 6561 | Py_UNICODE *uni2; |
| 6562 | /* startpos for collecting unencodable chars */ |
| 6563 | const Py_UNICODE *collstart = p; |
| 6564 | const Py_UNICODE *collend = p; |
| 6565 | /* find all unecodable characters */ |
| 6566 | while ((collend < endp) && ((*collend)>=limit)) |
| 6567 | ++collend; |
| 6568 | /* cache callback name lookup (if not done yet, i.e. it's the first error) */ |
| 6569 | if (known_errorHandler==-1) { |
| 6570 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 6571 | known_errorHandler = 1; |
| 6572 | else if (!strcmp(errors, "replace")) |
| 6573 | known_errorHandler = 2; |
| 6574 | else if (!strcmp(errors, "ignore")) |
| 6575 | known_errorHandler = 3; |
| 6576 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 6577 | known_errorHandler = 4; |
| 6578 | else |
| 6579 | known_errorHandler = 0; |
| 6580 | } |
| 6581 | switch (known_errorHandler) { |
| 6582 | case 1: /* strict */ |
| 6583 | raise_encode_exception(&exc, encoding, startp, size, collstart-startp, collend-startp, reason); |
| 6584 | goto onError; |
| 6585 | case 2: /* replace */ |
| 6586 | while (collstart++<collend) |
| 6587 | *str++ = '?'; /* fall through */ |
| 6588 | case 3: /* ignore */ |
| 6589 | p = collend; |
| 6590 | break; |
| 6591 | case 4: /* xmlcharrefreplace */ |
| 6592 | respos = str - PyBytes_AS_STRING(res); |
| 6593 | /* determine replacement size (temporarily (mis)uses p) */ |
| 6594 | for (p = collstart, repsize = 0; p < collend; ++p) { |
| 6595 | if (*p<10) |
| 6596 | repsize += 2+1+1; |
| 6597 | else if (*p<100) |
| 6598 | repsize += 2+2+1; |
| 6599 | else if (*p<1000) |
| 6600 | repsize += 2+3+1; |
| 6601 | else if (*p<10000) |
| 6602 | repsize += 2+4+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 6603 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6604 | else |
| 6605 | repsize += 2+5+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 6606 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6607 | else if (*p<100000) |
| 6608 | repsize += 2+5+1; |
| 6609 | else if (*p<1000000) |
| 6610 | repsize += 2+6+1; |
| 6611 | else |
| 6612 | repsize += 2+7+1; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 6613 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6614 | } |
| 6615 | requiredsize = respos+repsize+(endp-collend); |
| 6616 | if (requiredsize > ressize) { |
| 6617 | if (requiredsize<2*ressize) |
| 6618 | requiredsize = 2*ressize; |
| 6619 | if (_PyBytes_Resize(&res, requiredsize)) |
| 6620 | goto onError; |
| 6621 | str = PyBytes_AS_STRING(res) + respos; |
| 6622 | ressize = requiredsize; |
| 6623 | } |
| 6624 | /* generate replacement (temporarily (mis)uses p) */ |
| 6625 | for (p = collstart; p < collend; ++p) { |
| 6626 | str += sprintf(str, "&#%d;", (int)*p); |
| 6627 | } |
| 6628 | p = collend; |
| 6629 | break; |
| 6630 | default: |
| 6631 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 6632 | encoding, reason, startp, size, &exc, |
| 6633 | collstart-startp, collend-startp, &newpos); |
| 6634 | if (repunicode == NULL) |
| 6635 | goto onError; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6636 | if (PyBytes_Check(repunicode)) { |
| 6637 | /* Directly copy bytes result to output. */ |
| 6638 | repsize = PyBytes_Size(repunicode); |
| 6639 | if (repsize > 1) { |
| 6640 | /* Make room for all additional bytes. */ |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 6641 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6642 | if (_PyBytes_Resize(&res, ressize+repsize-1)) { |
| 6643 | Py_DECREF(repunicode); |
| 6644 | goto onError; |
| 6645 | } |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 6646 | str = PyBytes_AS_STRING(res) + respos; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6647 | ressize += repsize-1; |
| 6648 | } |
| 6649 | memcpy(str, PyBytes_AsString(repunicode), repsize); |
| 6650 | str += repsize; |
| 6651 | p = startp + newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6652 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6653 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6654 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6655 | /* need more space? (at least enough for what we |
| 6656 | have+the replacement+the rest of the string, so |
| 6657 | we won't have to check space for encodable characters) */ |
| 6658 | respos = str - PyBytes_AS_STRING(res); |
| 6659 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 6660 | requiredsize = respos+repsize+(endp-collend); |
| 6661 | if (requiredsize > ressize) { |
| 6662 | if (requiredsize<2*ressize) |
| 6663 | requiredsize = 2*ressize; |
| 6664 | if (_PyBytes_Resize(&res, requiredsize)) { |
| 6665 | Py_DECREF(repunicode); |
| 6666 | goto onError; |
| 6667 | } |
| 6668 | str = PyBytes_AS_STRING(res) + respos; |
| 6669 | ressize = requiredsize; |
| 6670 | } |
| 6671 | /* check if there is anything unencodable in the replacement |
| 6672 | and copy it to the output */ |
| 6673 | for (uni2 = PyUnicode_AS_UNICODE(repunicode);repsize-->0; ++uni2, ++str) { |
| 6674 | c = *uni2; |
| 6675 | if (c >= limit) { |
| 6676 | raise_encode_exception(&exc, encoding, startp, size, |
| 6677 | unicodepos, unicodepos+1, reason); |
| 6678 | Py_DECREF(repunicode); |
| 6679 | goto onError; |
| 6680 | } |
| 6681 | *str = (char)c; |
| 6682 | } |
| 6683 | p = startp + newpos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6684 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6685 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6686 | } |
| 6687 | } |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6688 | /* Resize if we allocated to much */ |
| 6689 | size = str - PyBytes_AS_STRING(res); |
| 6690 | if (size < ressize) { /* If this falls res will be NULL */ |
Alexandre Vassalotti | bad1b92 | 2008-12-27 09:49:09 +0000 | [diff] [blame] | 6691 | assert(size >= 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6692 | if (_PyBytes_Resize(&res, size) < 0) |
| 6693 | goto onError; |
| 6694 | } |
| 6695 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6696 | Py_XDECREF(errorHandler); |
| 6697 | Py_XDECREF(exc); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6698 | return res; |
| 6699 | |
| 6700 | onError: |
| 6701 | Py_XDECREF(res); |
| 6702 | Py_XDECREF(errorHandler); |
| 6703 | Py_XDECREF(exc); |
| 6704 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6705 | } |
| 6706 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6707 | PyObject * |
| 6708 | PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6709 | Py_ssize_t size, |
| 6710 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6711 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6712 | return unicode_encode_ucs1(p, size, errors, 256); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6713 | } |
| 6714 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6715 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6716 | _PyUnicode_AsLatin1String(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6717 | { |
| 6718 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6719 | PyErr_BadArgument(); |
| 6720 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6721 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6722 | if (PyUnicode_READY(unicode) == -1) |
| 6723 | return NULL; |
| 6724 | /* Fast path: if it is a one-byte string, construct |
| 6725 | bytes object directly. */ |
| 6726 | if (PyUnicode_KIND(unicode) == PyUnicode_1BYTE_KIND) |
| 6727 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 6728 | PyUnicode_GET_LENGTH(unicode)); |
| 6729 | /* Non-Latin-1 characters present. Defer to above function to |
| 6730 | raise the exception. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6731 | return PyUnicode_EncodeLatin1(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6732 | PyUnicode_GET_SIZE(unicode), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6733 | errors); |
| 6734 | } |
| 6735 | |
| 6736 | PyObject* |
| 6737 | PyUnicode_AsLatin1String(PyObject *unicode) |
| 6738 | { |
| 6739 | return _PyUnicode_AsLatin1String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6740 | } |
| 6741 | |
| 6742 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ |
| 6743 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6744 | PyObject * |
| 6745 | PyUnicode_DecodeASCII(const char *s, |
| 6746 | Py_ssize_t size, |
| 6747 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6748 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6749 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6750 | PyUnicodeObject *v; |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6751 | Py_UNICODE *u; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6752 | Py_ssize_t startinpos; |
| 6753 | Py_ssize_t endinpos; |
| 6754 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6755 | const char *e; |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6756 | int has_error; |
| 6757 | const unsigned char *p = (const unsigned char *)s; |
| 6758 | const unsigned char *end = p + size; |
| 6759 | 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] | 6760 | PyObject *errorHandler = NULL; |
| 6761 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6762 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6763 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6764 | if (size == 1 && (unsigned char)s[0] < 128) |
| 6765 | return get_latin1_char((unsigned char)s[0]); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6766 | |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6767 | has_error = 0; |
| 6768 | while (p < end && !has_error) { |
| 6769 | /* Fast path, see below in PyUnicode_DecodeUTF8Stateful for |
| 6770 | an explanation. */ |
| 6771 | if (!((size_t) p & LONG_PTR_MASK)) { |
| 6772 | /* Help register allocation */ |
| 6773 | register const unsigned char *_p = p; |
| 6774 | while (_p < aligned_end) { |
| 6775 | unsigned long value = *(unsigned long *) _p; |
| 6776 | if (value & ASCII_CHAR_MASK) { |
| 6777 | has_error = 1; |
| 6778 | break; |
| 6779 | } |
| 6780 | _p += SIZEOF_LONG; |
| 6781 | } |
| 6782 | if (_p == end) |
| 6783 | break; |
| 6784 | if (has_error) |
| 6785 | break; |
| 6786 | p = _p; |
| 6787 | } |
| 6788 | if (*p & 0x80) { |
| 6789 | has_error = 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6790 | break; |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6791 | } |
| 6792 | else { |
| 6793 | ++p; |
| 6794 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 6795 | } |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6796 | if (!has_error) |
| 6797 | return unicode_fromascii((const unsigned char *)s, size); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6798 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6799 | v = _PyUnicode_New(size); |
| 6800 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6801 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6802 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6803 | return (PyObject *)v; |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6804 | u = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6805 | e = s + size; |
| 6806 | while (s < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6807 | register unsigned char c = (unsigned char)*s; |
| 6808 | if (c < 128) { |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6809 | *u++ = c; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6810 | ++s; |
| 6811 | } |
| 6812 | else { |
| 6813 | startinpos = s-starts; |
| 6814 | endinpos = startinpos + 1; |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6815 | outpos = u - (Py_UNICODE *)PyUnicode_AS_UNICODE(v); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6816 | if (unicode_decode_call_errorhandler( |
| 6817 | errors, &errorHandler, |
| 6818 | "ascii", "ordinal not in range(128)", |
| 6819 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6820 | &v, &outpos, &u)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6821 | goto onError; |
| 6822 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6823 | } |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6824 | if (u - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
| 6825 | if (PyUnicode_Resize((PyObject**)&v, u - PyUnicode_AS_UNICODE(v)) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6826 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6827 | Py_XDECREF(errorHandler); |
| 6828 | Py_XDECREF(exc); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 6829 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 6830 | if (_PyUnicode_READY_REPLACE(&v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6831 | Py_DECREF(v); |
| 6832 | return NULL; |
| 6833 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 6834 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 6835 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6836 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6837 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6838 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6839 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6840 | Py_XDECREF(errorHandler); |
| 6841 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6842 | return NULL; |
| 6843 | } |
| 6844 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6845 | PyObject * |
| 6846 | PyUnicode_EncodeASCII(const Py_UNICODE *p, |
| 6847 | Py_ssize_t size, |
| 6848 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6849 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6850 | return unicode_encode_ucs1(p, size, errors, 128); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6851 | } |
| 6852 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6853 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6854 | _PyUnicode_AsASCIIString(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6855 | { |
| 6856 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6857 | PyErr_BadArgument(); |
| 6858 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6859 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6860 | if (PyUnicode_READY(unicode) == -1) |
| 6861 | return NULL; |
| 6862 | /* Fast path: if it is an ASCII-only string, construct bytes object |
| 6863 | directly. Else defer to above function to raise the exception. */ |
| 6864 | if (PyUnicode_MAX_CHAR_VALUE(unicode) < 128) |
| 6865 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 6866 | PyUnicode_GET_LENGTH(unicode)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6867 | return PyUnicode_EncodeASCII(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6868 | PyUnicode_GET_SIZE(unicode), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6869 | errors); |
| 6870 | } |
| 6871 | |
| 6872 | PyObject * |
| 6873 | PyUnicode_AsASCIIString(PyObject *unicode) |
| 6874 | { |
| 6875 | return _PyUnicode_AsASCIIString(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6876 | } |
| 6877 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 6878 | #ifdef HAVE_MBCS |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6879 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6880 | /* --- MBCS codecs for Windows -------------------------------------------- */ |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6881 | |
Hirokazu Yamamoto | 3530246 | 2009-03-21 13:23:27 +0000 | [diff] [blame] | 6882 | #if SIZEOF_INT < SIZEOF_SIZE_T |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6883 | #define NEED_RETRY |
| 6884 | #endif |
| 6885 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6886 | #ifndef WC_ERR_INVALID_CHARS |
| 6887 | # define WC_ERR_INVALID_CHARS 0x0080 |
| 6888 | #endif |
| 6889 | |
| 6890 | static char* |
| 6891 | code_page_name(UINT code_page, PyObject **obj) |
| 6892 | { |
| 6893 | *obj = NULL; |
| 6894 | if (code_page == CP_ACP) |
| 6895 | return "mbcs"; |
| 6896 | if (code_page == CP_UTF7) |
| 6897 | return "CP_UTF7"; |
| 6898 | if (code_page == CP_UTF8) |
| 6899 | return "CP_UTF8"; |
| 6900 | |
| 6901 | *obj = PyBytes_FromFormat("cp%u", code_page); |
| 6902 | if (*obj == NULL) |
| 6903 | return NULL; |
| 6904 | return PyBytes_AS_STRING(*obj); |
| 6905 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6906 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6907 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6908 | is_dbcs_lead_byte(UINT code_page, const char *s, int offset) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6909 | { |
| 6910 | const char *curr = s + offset; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6911 | const char *prev; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6912 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6913 | if (!IsDBCSLeadByteEx(code_page, *curr)) |
| 6914 | return 0; |
| 6915 | |
| 6916 | prev = CharPrevExA(code_page, s, curr, 0); |
| 6917 | if (prev == curr) |
| 6918 | return 1; |
| 6919 | /* FIXME: This code is limited to "true" double-byte encodings, |
| 6920 | as it assumes an incomplete character consists of a single |
| 6921 | byte. */ |
| 6922 | if (curr - prev == 2) |
| 6923 | return 1; |
| 6924 | if (!IsDBCSLeadByteEx(code_page, *prev)) |
| 6925 | return 1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6926 | return 0; |
| 6927 | } |
| 6928 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6929 | static DWORD |
| 6930 | decode_code_page_flags(UINT code_page) |
| 6931 | { |
| 6932 | if (code_page == CP_UTF7) { |
| 6933 | /* The CP_UTF7 decoder only supports flags=0 */ |
| 6934 | return 0; |
| 6935 | } |
| 6936 | else |
| 6937 | return MB_ERR_INVALID_CHARS; |
| 6938 | } |
| 6939 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6940 | /* |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6941 | * Decode a byte string from a Windows code page into unicode object in strict |
| 6942 | * mode. |
| 6943 | * |
| 6944 | * Returns consumed size if succeed, returns -2 on decode error, or raise a |
| 6945 | * WindowsError and returns -1 on other error. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6946 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6947 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6948 | decode_code_page_strict(UINT code_page, |
| 6949 | PyUnicodeObject **v, |
| 6950 | const char *in, |
| 6951 | int insize) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6952 | { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6953 | const DWORD flags = decode_code_page_flags(code_page); |
| 6954 | Py_UNICODE *out; |
| 6955 | DWORD outsize; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6956 | |
| 6957 | /* First get the size of the result */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6958 | assert(insize > 0); |
| 6959 | outsize = MultiByteToWideChar(code_page, flags, in, insize, NULL, 0); |
| 6960 | if (outsize <= 0) |
| 6961 | goto error; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6962 | |
| 6963 | if (*v == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6964 | /* Create unicode object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6965 | *v = _PyUnicode_New(outsize); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6966 | if (*v == NULL) |
| 6967 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6968 | out = PyUnicode_AS_UNICODE(*v); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6969 | } |
| 6970 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6971 | /* Extend unicode object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6972 | Py_ssize_t n = PyUnicode_GET_SIZE(*v); |
| 6973 | if (PyUnicode_Resize((PyObject**)v, n + outsize) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6974 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6975 | out = PyUnicode_AS_UNICODE(*v) + n; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6976 | } |
| 6977 | |
| 6978 | /* Do the conversion */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6979 | outsize = MultiByteToWideChar(code_page, flags, in, insize, out, outsize); |
| 6980 | if (outsize <= 0) |
| 6981 | goto error; |
| 6982 | return insize; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6983 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6984 | error: |
| 6985 | if (GetLastError() == ERROR_NO_UNICODE_TRANSLATION) |
| 6986 | return -2; |
| 6987 | PyErr_SetFromWindowsErr(0); |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6988 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6989 | } |
| 6990 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6991 | /* |
| 6992 | * Decode a byte string from a code page into unicode object with an error |
| 6993 | * handler. |
| 6994 | * |
| 6995 | * Returns consumed size if succeed, or raise a WindowsError or |
| 6996 | * UnicodeDecodeError exception and returns -1 on error. |
| 6997 | */ |
| 6998 | static int |
| 6999 | decode_code_page_errors(UINT code_page, |
| 7000 | PyUnicodeObject **v, |
| 7001 | const char *in, |
| 7002 | int size, |
| 7003 | const char *errors) |
| 7004 | { |
| 7005 | const char *startin = in; |
| 7006 | const char *endin = in + size; |
| 7007 | const DWORD flags = decode_code_page_flags(code_page); |
| 7008 | /* Ideally, we should get reason from FormatMessage. This is the Windows |
| 7009 | 2000 English version of the message. */ |
| 7010 | const char *reason = "No mapping for the Unicode character exists " |
| 7011 | "in the target code page."; |
| 7012 | /* each step cannot decode more than 1 character, but a character can be |
| 7013 | represented as a surrogate pair */ |
| 7014 | wchar_t buffer[2], *startout, *out; |
| 7015 | int insize, outsize; |
| 7016 | PyObject *errorHandler = NULL; |
| 7017 | PyObject *exc = NULL; |
| 7018 | PyObject *encoding_obj = NULL; |
| 7019 | char *encoding; |
| 7020 | DWORD err; |
| 7021 | int ret = -1; |
| 7022 | |
| 7023 | assert(size > 0); |
| 7024 | |
| 7025 | encoding = code_page_name(code_page, &encoding_obj); |
| 7026 | if (encoding == NULL) |
| 7027 | return -1; |
| 7028 | |
| 7029 | if (errors == NULL || strcmp(errors, "strict") == 0) { |
| 7030 | /* The last error was ERROR_NO_UNICODE_TRANSLATION, then we raise a |
| 7031 | UnicodeDecodeError. */ |
| 7032 | make_decode_exception(&exc, encoding, in, size, 0, 0, reason); |
| 7033 | if (exc != NULL) { |
| 7034 | PyCodec_StrictErrors(exc); |
| 7035 | Py_CLEAR(exc); |
| 7036 | } |
| 7037 | goto error; |
| 7038 | } |
| 7039 | |
| 7040 | if (*v == NULL) { |
| 7041 | /* Create unicode object */ |
| 7042 | if (size > PY_SSIZE_T_MAX / (Py_ssize_t)Py_ARRAY_LENGTH(buffer)) { |
| 7043 | PyErr_NoMemory(); |
| 7044 | goto error; |
| 7045 | } |
| 7046 | *v = _PyUnicode_New(size * Py_ARRAY_LENGTH(buffer)); |
| 7047 | if (*v == NULL) |
| 7048 | goto error; |
| 7049 | startout = PyUnicode_AS_UNICODE(*v); |
| 7050 | } |
| 7051 | else { |
| 7052 | /* Extend unicode object */ |
| 7053 | Py_ssize_t n = PyUnicode_GET_SIZE(*v); |
| 7054 | if (size > (PY_SSIZE_T_MAX - n) / (Py_ssize_t)Py_ARRAY_LENGTH(buffer)) { |
| 7055 | PyErr_NoMemory(); |
| 7056 | goto error; |
| 7057 | } |
| 7058 | if (PyUnicode_Resize((PyObject**)v, n + size * Py_ARRAY_LENGTH(buffer)) < 0) |
| 7059 | goto error; |
| 7060 | startout = PyUnicode_AS_UNICODE(*v) + n; |
| 7061 | } |
| 7062 | |
| 7063 | /* Decode the byte string character per character */ |
| 7064 | out = startout; |
| 7065 | while (in < endin) |
| 7066 | { |
| 7067 | /* Decode a character */ |
| 7068 | insize = 1; |
| 7069 | do |
| 7070 | { |
| 7071 | outsize = MultiByteToWideChar(code_page, flags, |
| 7072 | in, insize, |
| 7073 | buffer, Py_ARRAY_LENGTH(buffer)); |
| 7074 | if (outsize > 0) |
| 7075 | break; |
| 7076 | err = GetLastError(); |
| 7077 | if (err != ERROR_NO_UNICODE_TRANSLATION |
| 7078 | && err != ERROR_INSUFFICIENT_BUFFER) |
| 7079 | { |
| 7080 | PyErr_SetFromWindowsErr(0); |
| 7081 | goto error; |
| 7082 | } |
| 7083 | insize++; |
| 7084 | } |
| 7085 | /* 4=maximum length of a UTF-8 sequence */ |
| 7086 | while (insize <= 4 && (in + insize) <= endin); |
| 7087 | |
| 7088 | if (outsize <= 0) { |
| 7089 | Py_ssize_t startinpos, endinpos, outpos; |
| 7090 | |
| 7091 | startinpos = in - startin; |
| 7092 | endinpos = startinpos + 1; |
| 7093 | outpos = out - PyUnicode_AS_UNICODE(*v); |
| 7094 | if (unicode_decode_call_errorhandler( |
| 7095 | errors, &errorHandler, |
| 7096 | encoding, reason, |
| 7097 | &startin, &endin, &startinpos, &endinpos, &exc, &in, |
| 7098 | v, &outpos, &out)) |
| 7099 | { |
| 7100 | goto error; |
| 7101 | } |
| 7102 | } |
| 7103 | else { |
| 7104 | in += insize; |
| 7105 | memcpy(out, buffer, outsize * sizeof(wchar_t)); |
| 7106 | out += outsize; |
| 7107 | } |
| 7108 | } |
| 7109 | |
| 7110 | /* write a NUL character at the end */ |
| 7111 | *out = 0; |
| 7112 | |
| 7113 | /* Extend unicode object */ |
| 7114 | outsize = out - startout; |
| 7115 | assert(outsize <= PyUnicode_WSTR_LENGTH(*v)); |
| 7116 | if (PyUnicode_Resize((PyObject**)v, outsize) < 0) |
| 7117 | goto error; |
| 7118 | ret = 0; |
| 7119 | |
| 7120 | error: |
| 7121 | Py_XDECREF(encoding_obj); |
| 7122 | Py_XDECREF(errorHandler); |
| 7123 | Py_XDECREF(exc); |
| 7124 | return ret; |
| 7125 | } |
| 7126 | |
| 7127 | /* |
| 7128 | * Decode a byte string from a Windows code page into unicode object. If |
| 7129 | * 'final' is set, converts trailing lead-byte too. |
| 7130 | * |
| 7131 | * Returns consumed size if succeed, or raise a WindowsError or |
| 7132 | * UnicodeDecodeError exception and returns -1 on error. |
| 7133 | */ |
| 7134 | static int |
| 7135 | decode_code_page(UINT code_page, |
| 7136 | PyUnicodeObject **v, |
| 7137 | const char *s, int size, |
| 7138 | int final, const char *errors) |
| 7139 | { |
| 7140 | int done; |
| 7141 | |
| 7142 | /* Skip trailing lead-byte unless 'final' is set */ |
| 7143 | if (size == 0) { |
| 7144 | if (*v == NULL) { |
| 7145 | Py_INCREF(unicode_empty); |
| 7146 | *v = (PyUnicodeObject*)unicode_empty; |
| 7147 | if (*v == NULL) |
| 7148 | return -1; |
| 7149 | } |
| 7150 | return 0; |
| 7151 | } |
| 7152 | |
| 7153 | if (!final && is_dbcs_lead_byte(code_page, s, size - 1)) |
| 7154 | --size; |
| 7155 | |
| 7156 | done = decode_code_page_strict(code_page, v, s, size); |
| 7157 | if (done == -2) |
| 7158 | done = decode_code_page_errors(code_page, v, s, size, errors); |
| 7159 | return done; |
| 7160 | } |
| 7161 | |
| 7162 | static PyObject * |
| 7163 | decode_code_page_stateful(int code_page, |
| 7164 | const char *s, |
| 7165 | Py_ssize_t size, |
| 7166 | const char *errors, |
| 7167 | Py_ssize_t *consumed) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7168 | { |
| 7169 | PyUnicodeObject *v = NULL; |
| 7170 | int done; |
| 7171 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7172 | if (code_page < 0) { |
| 7173 | PyErr_SetString(PyExc_ValueError, "invalid code page number"); |
| 7174 | return NULL; |
| 7175 | } |
| 7176 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7177 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7178 | *consumed = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7179 | |
| 7180 | #ifdef NEED_RETRY |
| 7181 | retry: |
| 7182 | if (size > INT_MAX) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7183 | done = decode_code_page(code_page, &v, s, INT_MAX, 0, errors); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7184 | else |
| 7185 | #endif |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7186 | done = decode_code_page(code_page, &v, s, (int)size, !consumed, errors); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7187 | |
| 7188 | if (done < 0) { |
| 7189 | Py_XDECREF(v); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7190 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7191 | } |
| 7192 | |
| 7193 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7194 | *consumed += done; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7195 | |
| 7196 | #ifdef NEED_RETRY |
| 7197 | if (size > INT_MAX) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7198 | s += done; |
| 7199 | size -= done; |
| 7200 | goto retry; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7201 | } |
| 7202 | #endif |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7203 | |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 7204 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 7205 | if (_PyUnicode_READY_REPLACE(&v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7206 | Py_DECREF(v); |
| 7207 | return NULL; |
| 7208 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 7209 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 7210 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7211 | return (PyObject *)v; |
| 7212 | } |
| 7213 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7214 | PyObject * |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7215 | PyUnicode_DecodeCodePageStateful(int code_page, |
| 7216 | const char *s, |
| 7217 | Py_ssize_t size, |
| 7218 | const char *errors, |
| 7219 | Py_ssize_t *consumed) |
| 7220 | { |
| 7221 | return decode_code_page_stateful(code_page, s, size, errors, consumed); |
| 7222 | } |
| 7223 | |
| 7224 | PyObject * |
| 7225 | PyUnicode_DecodeMBCSStateful(const char *s, |
| 7226 | Py_ssize_t size, |
| 7227 | const char *errors, |
| 7228 | Py_ssize_t *consumed) |
| 7229 | { |
| 7230 | return decode_code_page_stateful(CP_ACP, s, size, errors, consumed); |
| 7231 | } |
| 7232 | |
| 7233 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7234 | PyUnicode_DecodeMBCS(const char *s, |
| 7235 | Py_ssize_t size, |
| 7236 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7237 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7238 | return PyUnicode_DecodeMBCSStateful(s, size, errors, NULL); |
| 7239 | } |
| 7240 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7241 | static DWORD |
| 7242 | encode_code_page_flags(UINT code_page, const char *errors) |
| 7243 | { |
| 7244 | if (code_page == CP_UTF8) { |
| 7245 | if (winver.dwMajorVersion >= 6) |
| 7246 | /* CP_UTF8 supports WC_ERR_INVALID_CHARS on Windows Vista |
| 7247 | and later */ |
| 7248 | return WC_ERR_INVALID_CHARS; |
| 7249 | else |
| 7250 | /* CP_UTF8 only supports flags=0 on Windows older than Vista */ |
| 7251 | return 0; |
| 7252 | } |
| 7253 | else if (code_page == CP_UTF7) { |
| 7254 | /* CP_UTF7 only supports flags=0 */ |
| 7255 | return 0; |
| 7256 | } |
| 7257 | else { |
| 7258 | if (errors != NULL && strcmp(errors, "replace") == 0) |
| 7259 | return 0; |
| 7260 | else |
| 7261 | return WC_NO_BEST_FIT_CHARS; |
| 7262 | } |
| 7263 | } |
| 7264 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7265 | /* |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7266 | * Encode a Unicode string to a Windows code page into a byte string in strict |
| 7267 | * mode. |
| 7268 | * |
| 7269 | * Returns consumed characters if succeed, returns -2 on encode error, or raise |
| 7270 | * a WindowsError and returns -1 on other error. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7271 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7272 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7273 | encode_code_page_strict(UINT code_page, PyObject **outbytes, |
| 7274 | const Py_UNICODE *p, const int size, |
| 7275 | const char* errors) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7276 | { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7277 | BOOL usedDefaultChar = FALSE; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7278 | BOOL *pusedDefaultChar = &usedDefaultChar; |
| 7279 | int outsize; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7280 | PyObject *exc = NULL; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7281 | const DWORD flags = encode_code_page_flags(code_page, NULL); |
| 7282 | char *out; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7283 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7284 | assert(size > 0); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7285 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7286 | if (code_page != CP_UTF8 && code_page != CP_UTF7) |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7287 | pusedDefaultChar = &usedDefaultChar; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7288 | else |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7289 | pusedDefaultChar = NULL; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7290 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7291 | /* First get the size of the result */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7292 | outsize = WideCharToMultiByte(code_page, flags, |
| 7293 | p, size, |
| 7294 | NULL, 0, |
| 7295 | NULL, pusedDefaultChar); |
| 7296 | if (outsize <= 0) |
| 7297 | goto error; |
| 7298 | /* If we used a default char, then we failed! */ |
| 7299 | if (pusedDefaultChar && *pusedDefaultChar) |
| 7300 | return -2; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7301 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7302 | if (*outbytes == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7303 | /* Create string object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7304 | *outbytes = PyBytes_FromStringAndSize(NULL, outsize); |
| 7305 | if (*outbytes == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7306 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7307 | out = PyBytes_AS_STRING(*outbytes); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7308 | } |
| 7309 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7310 | /* Extend string object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7311 | const Py_ssize_t n = PyBytes_Size(*outbytes); |
| 7312 | if (outsize > PY_SSIZE_T_MAX - n) { |
| 7313 | PyErr_NoMemory(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7314 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7315 | } |
| 7316 | if (_PyBytes_Resize(outbytes, n + outsize) < 0) |
| 7317 | return -1; |
| 7318 | out = PyBytes_AS_STRING(*outbytes) + n; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7319 | } |
| 7320 | |
| 7321 | /* Do the conversion */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7322 | outsize = WideCharToMultiByte(code_page, flags, |
| 7323 | p, size, |
| 7324 | out, outsize, |
| 7325 | NULL, pusedDefaultChar); |
| 7326 | if (outsize <= 0) |
| 7327 | goto error; |
| 7328 | if (pusedDefaultChar && *pusedDefaultChar) |
| 7329 | return -2; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7330 | return 0; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7331 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7332 | error: |
| 7333 | if (GetLastError() == ERROR_NO_UNICODE_TRANSLATION) |
| 7334 | return -2; |
| 7335 | PyErr_SetFromWindowsErr(0); |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7336 | return -1; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7337 | } |
| 7338 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7339 | /* |
| 7340 | * Encode a Unicode string to a Windows code page into a byte string using a |
| 7341 | * error handler. |
| 7342 | * |
| 7343 | * Returns consumed characters if succeed, or raise a WindowsError and returns |
| 7344 | * -1 on other error. |
| 7345 | */ |
| 7346 | static int |
| 7347 | encode_code_page_errors(UINT code_page, PyObject **outbytes, |
| 7348 | const Py_UNICODE *in, const int insize, |
| 7349 | const char* errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7350 | { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7351 | const DWORD flags = encode_code_page_flags(code_page, errors); |
| 7352 | const Py_UNICODE *startin = in; |
| 7353 | const Py_UNICODE *endin = in + insize; |
| 7354 | /* Ideally, we should get reason from FormatMessage. This is the Windows |
| 7355 | 2000 English version of the message. */ |
| 7356 | const char *reason = "invalid character"; |
| 7357 | /* 4=maximum length of a UTF-8 sequence */ |
| 7358 | char buffer[4]; |
| 7359 | BOOL usedDefaultChar = FALSE, *pusedDefaultChar; |
| 7360 | Py_ssize_t outsize; |
| 7361 | char *out; |
| 7362 | int charsize; |
| 7363 | PyObject *errorHandler = NULL; |
| 7364 | PyObject *exc = NULL; |
| 7365 | PyObject *encoding_obj = NULL; |
| 7366 | char *encoding; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7367 | Py_ssize_t startpos, newpos, newoutsize; |
| 7368 | PyObject *rep; |
| 7369 | int ret = -1; |
| 7370 | |
| 7371 | assert(insize > 0); |
| 7372 | |
| 7373 | encoding = code_page_name(code_page, &encoding_obj); |
| 7374 | if (encoding == NULL) |
| 7375 | return -1; |
| 7376 | |
| 7377 | if (errors == NULL || strcmp(errors, "strict") == 0) { |
| 7378 | /* The last error was ERROR_NO_UNICODE_TRANSLATION, |
| 7379 | then we raise a UnicodeEncodeError. */ |
| 7380 | make_encode_exception(&exc, encoding, in, insize, 0, 0, reason); |
| 7381 | if (exc != NULL) { |
| 7382 | PyCodec_StrictErrors(exc); |
| 7383 | Py_DECREF(exc); |
| 7384 | } |
| 7385 | Py_XDECREF(encoding_obj); |
| 7386 | return -1; |
| 7387 | } |
| 7388 | |
| 7389 | if (code_page != CP_UTF8 && code_page != CP_UTF7) |
| 7390 | pusedDefaultChar = &usedDefaultChar; |
| 7391 | else |
| 7392 | pusedDefaultChar = NULL; |
| 7393 | |
| 7394 | if (Py_ARRAY_LENGTH(buffer) > PY_SSIZE_T_MAX / insize) { |
| 7395 | PyErr_NoMemory(); |
| 7396 | goto error; |
| 7397 | } |
| 7398 | outsize = insize * Py_ARRAY_LENGTH(buffer); |
| 7399 | |
| 7400 | if (*outbytes == NULL) { |
| 7401 | /* Create string object */ |
| 7402 | *outbytes = PyBytes_FromStringAndSize(NULL, outsize); |
| 7403 | if (*outbytes == NULL) |
| 7404 | goto error; |
| 7405 | out = PyBytes_AS_STRING(*outbytes); |
| 7406 | } |
| 7407 | else { |
| 7408 | /* Extend string object */ |
| 7409 | Py_ssize_t n = PyBytes_Size(*outbytes); |
| 7410 | if (n > PY_SSIZE_T_MAX - outsize) { |
| 7411 | PyErr_NoMemory(); |
| 7412 | goto error; |
| 7413 | } |
| 7414 | if (_PyBytes_Resize(outbytes, n + outsize) < 0) |
| 7415 | goto error; |
| 7416 | out = PyBytes_AS_STRING(*outbytes) + n; |
| 7417 | } |
| 7418 | |
| 7419 | /* Encode the string character per character */ |
| 7420 | while (in < endin) |
| 7421 | { |
| 7422 | if ((in + 2) <= endin |
| 7423 | && 0xD800 <= in[0] && in[0] <= 0xDBFF |
| 7424 | && 0xDC00 <= in[1] && in[1] <= 0xDFFF) |
| 7425 | charsize = 2; |
| 7426 | else |
| 7427 | charsize = 1; |
| 7428 | |
| 7429 | outsize = WideCharToMultiByte(code_page, flags, |
| 7430 | in, charsize, |
| 7431 | buffer, Py_ARRAY_LENGTH(buffer), |
| 7432 | NULL, pusedDefaultChar); |
| 7433 | if (outsize > 0) { |
| 7434 | if (pusedDefaultChar == NULL || !(*pusedDefaultChar)) |
| 7435 | { |
| 7436 | in += charsize; |
| 7437 | memcpy(out, buffer, outsize); |
| 7438 | out += outsize; |
| 7439 | continue; |
| 7440 | } |
| 7441 | } |
| 7442 | else if (GetLastError() != ERROR_NO_UNICODE_TRANSLATION) { |
| 7443 | PyErr_SetFromWindowsErr(0); |
| 7444 | goto error; |
| 7445 | } |
| 7446 | |
| 7447 | charsize = Py_MAX(charsize - 1, 1); |
| 7448 | startpos = in - startin; |
| 7449 | rep = unicode_encode_call_errorhandler( |
| 7450 | errors, &errorHandler, encoding, reason, |
| 7451 | startin, insize, &exc, |
| 7452 | startpos, startpos + charsize, &newpos); |
| 7453 | if (rep == NULL) |
| 7454 | goto error; |
| 7455 | in = startin + newpos; |
| 7456 | |
| 7457 | if (PyBytes_Check(rep)) { |
| 7458 | outsize = PyBytes_GET_SIZE(rep); |
| 7459 | if (outsize != 1) { |
| 7460 | Py_ssize_t offset = out - PyBytes_AS_STRING(*outbytes); |
| 7461 | newoutsize = PyBytes_GET_SIZE(*outbytes) + (outsize - 1); |
| 7462 | if (_PyBytes_Resize(outbytes, newoutsize) < 0) { |
| 7463 | Py_DECREF(rep); |
| 7464 | goto error; |
| 7465 | } |
| 7466 | out = PyBytes_AS_STRING(*outbytes) + offset; |
| 7467 | } |
| 7468 | memcpy(out, PyBytes_AS_STRING(rep), outsize); |
| 7469 | out += outsize; |
| 7470 | } |
| 7471 | else { |
| 7472 | Py_ssize_t i; |
| 7473 | enum PyUnicode_Kind kind; |
| 7474 | void *data; |
| 7475 | |
| 7476 | if (PyUnicode_READY(rep) < 0) { |
| 7477 | Py_DECREF(rep); |
| 7478 | goto error; |
| 7479 | } |
| 7480 | |
| 7481 | outsize = PyUnicode_GET_LENGTH(rep); |
| 7482 | if (outsize != 1) { |
| 7483 | Py_ssize_t offset = out - PyBytes_AS_STRING(*outbytes); |
| 7484 | newoutsize = PyBytes_GET_SIZE(*outbytes) + (outsize - 1); |
| 7485 | if (_PyBytes_Resize(outbytes, newoutsize) < 0) { |
| 7486 | Py_DECREF(rep); |
| 7487 | goto error; |
| 7488 | } |
| 7489 | out = PyBytes_AS_STRING(*outbytes) + offset; |
| 7490 | } |
| 7491 | kind = PyUnicode_KIND(rep); |
| 7492 | data = PyUnicode_DATA(rep); |
| 7493 | for (i=0; i < outsize; i++) { |
| 7494 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 7495 | if (ch > 127) { |
| 7496 | raise_encode_exception(&exc, |
| 7497 | encoding, |
| 7498 | startin, insize, |
| 7499 | startpos, startpos + charsize, |
| 7500 | "unable to encode error handler result to ASCII"); |
| 7501 | Py_DECREF(rep); |
| 7502 | goto error; |
| 7503 | } |
| 7504 | *out = (unsigned char)ch; |
| 7505 | out++; |
| 7506 | } |
| 7507 | } |
| 7508 | Py_DECREF(rep); |
| 7509 | } |
| 7510 | /* write a NUL byte */ |
| 7511 | *out = 0; |
| 7512 | outsize = out - PyBytes_AS_STRING(*outbytes); |
| 7513 | assert(outsize <= PyBytes_GET_SIZE(*outbytes)); |
| 7514 | if (_PyBytes_Resize(outbytes, outsize) < 0) |
| 7515 | goto error; |
| 7516 | ret = 0; |
| 7517 | |
| 7518 | error: |
| 7519 | Py_XDECREF(encoding_obj); |
| 7520 | Py_XDECREF(errorHandler); |
| 7521 | Py_XDECREF(exc); |
| 7522 | return ret; |
| 7523 | } |
| 7524 | |
| 7525 | /* |
| 7526 | * Encode a Unicode string to a Windows code page into a byte string. |
| 7527 | * |
| 7528 | * Returns consumed characters if succeed, or raise a WindowsError and returns |
| 7529 | * -1 on other error. |
| 7530 | */ |
| 7531 | static int |
| 7532 | encode_code_page_chunk(UINT code_page, PyObject **outbytes, |
| 7533 | const Py_UNICODE *p, int size, |
| 7534 | const char* errors) |
| 7535 | { |
| 7536 | int done; |
| 7537 | |
| 7538 | if (size == 0) { |
| 7539 | if (*outbytes == NULL) { |
| 7540 | *outbytes = PyBytes_FromStringAndSize(NULL, 0); |
| 7541 | if (*outbytes == NULL) |
| 7542 | return -1; |
| 7543 | } |
| 7544 | return 0; |
| 7545 | } |
| 7546 | |
| 7547 | done = encode_code_page_strict(code_page, outbytes, p, size, errors); |
| 7548 | if (done == -2) |
| 7549 | done = encode_code_page_errors(code_page, outbytes, p, size, errors); |
| 7550 | return done; |
| 7551 | } |
| 7552 | |
| 7553 | static PyObject * |
| 7554 | encode_code_page(int code_page, |
| 7555 | const Py_UNICODE *p, Py_ssize_t size, |
| 7556 | const char *errors) |
| 7557 | { |
| 7558 | PyObject *outbytes = NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7559 | int ret; |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 7560 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7561 | if (code_page < 0) { |
| 7562 | PyErr_SetString(PyExc_ValueError, "invalid code page number"); |
| 7563 | return NULL; |
| 7564 | } |
| 7565 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7566 | #ifdef NEED_RETRY |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7567 | retry: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7568 | if (size > INT_MAX) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7569 | ret = encode_code_page_chunk(code_page, &outbytes, p, INT_MAX, errors); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7570 | else |
| 7571 | #endif |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7572 | ret = encode_code_page_chunk(code_page, &outbytes, p, (int)size, errors); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7573 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7574 | if (ret < 0) { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7575 | Py_XDECREF(outbytes); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7576 | return NULL; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7577 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7578 | |
| 7579 | #ifdef NEED_RETRY |
| 7580 | if (size > INT_MAX) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7581 | p += INT_MAX; |
| 7582 | size -= INT_MAX; |
| 7583 | goto retry; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7584 | } |
| 7585 | #endif |
| 7586 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7587 | return outbytes; |
| 7588 | } |
| 7589 | |
| 7590 | PyObject * |
| 7591 | PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
| 7592 | Py_ssize_t size, |
| 7593 | const char *errors) |
| 7594 | { |
| 7595 | return encode_code_page(CP_ACP, p, size, errors); |
| 7596 | } |
| 7597 | |
| 7598 | PyObject * |
| 7599 | PyUnicode_EncodeCodePage(int code_page, |
| 7600 | PyObject *unicode, |
| 7601 | const char *errors) |
| 7602 | { |
| 7603 | const Py_UNICODE *p; |
| 7604 | Py_ssize_t size; |
| 7605 | p = PyUnicode_AsUnicodeAndSize(unicode, &size); |
| 7606 | if (p == NULL) |
| 7607 | return NULL; |
| 7608 | return encode_code_page(code_page, p, size, errors); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7609 | } |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 7610 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7611 | PyObject * |
| 7612 | PyUnicode_AsMBCSString(PyObject *unicode) |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 7613 | { |
| 7614 | if (!PyUnicode_Check(unicode)) { |
| 7615 | PyErr_BadArgument(); |
| 7616 | return NULL; |
| 7617 | } |
| 7618 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7619 | PyUnicode_GET_SIZE(unicode), |
| 7620 | NULL); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 7621 | } |
| 7622 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7623 | #undef NEED_RETRY |
| 7624 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 7625 | #endif /* HAVE_MBCS */ |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7626 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7627 | /* --- Character Mapping Codec -------------------------------------------- */ |
| 7628 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7629 | PyObject * |
| 7630 | PyUnicode_DecodeCharmap(const char *s, |
| 7631 | Py_ssize_t size, |
| 7632 | PyObject *mapping, |
| 7633 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7634 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7635 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7636 | Py_ssize_t startinpos; |
| 7637 | Py_ssize_t endinpos; |
| 7638 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7639 | const char *e; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7640 | PyUnicodeObject *v; |
| 7641 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7642 | Py_ssize_t extrachars = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7643 | PyObject *errorHandler = NULL; |
| 7644 | PyObject *exc = NULL; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7645 | Py_UNICODE *mapstring = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7646 | Py_ssize_t maplen = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7647 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7648 | /* Default to Latin-1 */ |
| 7649 | if (mapping == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7650 | return PyUnicode_DecodeLatin1(s, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7651 | |
| 7652 | v = _PyUnicode_New(size); |
| 7653 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7654 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7655 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7656 | return (PyObject *)v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7657 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7658 | e = s + size; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7659 | if (PyUnicode_CheckExact(mapping)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7660 | mapstring = PyUnicode_AS_UNICODE(mapping); |
| 7661 | maplen = PyUnicode_GET_SIZE(mapping); |
| 7662 | while (s < e) { |
| 7663 | unsigned char ch = *s; |
| 7664 | Py_UNICODE x = 0xfffe; /* illegal value */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7665 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7666 | if (ch < maplen) |
| 7667 | x = mapstring[ch]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7668 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7669 | if (x == 0xfffe) { |
| 7670 | /* undefined mapping */ |
| 7671 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 7672 | startinpos = s-starts; |
| 7673 | endinpos = startinpos+1; |
| 7674 | if (unicode_decode_call_errorhandler( |
| 7675 | errors, &errorHandler, |
| 7676 | "charmap", "character maps to <undefined>", |
| 7677 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 7678 | &v, &outpos, &p)) { |
| 7679 | goto onError; |
| 7680 | } |
| 7681 | continue; |
| 7682 | } |
| 7683 | *p++ = x; |
| 7684 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7685 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7686 | } |
| 7687 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7688 | while (s < e) { |
| 7689 | unsigned char ch = *s; |
| 7690 | PyObject *w, *x; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7691 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7692 | /* Get mapping (char ordinal -> integer, Unicode char or None) */ |
| 7693 | w = PyLong_FromLong((long)ch); |
| 7694 | if (w == NULL) |
| 7695 | goto onError; |
| 7696 | x = PyObject_GetItem(mapping, w); |
| 7697 | Py_DECREF(w); |
| 7698 | if (x == NULL) { |
| 7699 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 7700 | /* No mapping found means: mapping is undefined. */ |
| 7701 | PyErr_Clear(); |
| 7702 | x = Py_None; |
| 7703 | Py_INCREF(x); |
| 7704 | } else |
| 7705 | goto onError; |
| 7706 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7707 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7708 | /* Apply mapping */ |
| 7709 | if (PyLong_Check(x)) { |
| 7710 | long value = PyLong_AS_LONG(x); |
| 7711 | if (value < 0 || value > 65535) { |
| 7712 | PyErr_SetString(PyExc_TypeError, |
| 7713 | "character mapping must be in range(65536)"); |
| 7714 | Py_DECREF(x); |
| 7715 | goto onError; |
| 7716 | } |
| 7717 | *p++ = (Py_UNICODE)value; |
| 7718 | } |
| 7719 | else if (x == Py_None) { |
| 7720 | /* undefined mapping */ |
| 7721 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 7722 | startinpos = s-starts; |
| 7723 | endinpos = startinpos+1; |
| 7724 | if (unicode_decode_call_errorhandler( |
| 7725 | errors, &errorHandler, |
| 7726 | "charmap", "character maps to <undefined>", |
| 7727 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 7728 | &v, &outpos, &p)) { |
| 7729 | Py_DECREF(x); |
| 7730 | goto onError; |
| 7731 | } |
| 7732 | Py_DECREF(x); |
| 7733 | continue; |
| 7734 | } |
| 7735 | else if (PyUnicode_Check(x)) { |
| 7736 | Py_ssize_t targetsize = PyUnicode_GET_SIZE(x); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7737 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7738 | if (targetsize == 1) |
| 7739 | /* 1-1 mapping */ |
| 7740 | *p++ = *PyUnicode_AS_UNICODE(x); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7741 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7742 | else if (targetsize > 1) { |
| 7743 | /* 1-n mapping */ |
| 7744 | if (targetsize > extrachars) { |
| 7745 | /* resize first */ |
| 7746 | Py_ssize_t oldpos = p - PyUnicode_AS_UNICODE(v); |
| 7747 | Py_ssize_t needed = (targetsize - extrachars) + \ |
| 7748 | (targetsize << 2); |
| 7749 | extrachars += needed; |
| 7750 | /* XXX overflow detection missing */ |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 7751 | if (PyUnicode_Resize((PyObject**)&v, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7752 | PyUnicode_GET_SIZE(v) + needed) < 0) { |
| 7753 | Py_DECREF(x); |
| 7754 | goto onError; |
| 7755 | } |
| 7756 | p = PyUnicode_AS_UNICODE(v) + oldpos; |
| 7757 | } |
| 7758 | Py_UNICODE_COPY(p, |
| 7759 | PyUnicode_AS_UNICODE(x), |
| 7760 | targetsize); |
| 7761 | p += targetsize; |
| 7762 | extrachars -= targetsize; |
| 7763 | } |
| 7764 | /* 1-0 mapping: skip the character */ |
| 7765 | } |
| 7766 | else { |
| 7767 | /* wrong return value */ |
| 7768 | PyErr_SetString(PyExc_TypeError, |
| 7769 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7770 | Py_DECREF(x); |
| 7771 | goto onError; |
| 7772 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7773 | Py_DECREF(x); |
| 7774 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7775 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7776 | } |
| 7777 | if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 7778 | if (PyUnicode_Resize((PyObject**)&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7779 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7780 | Py_XDECREF(errorHandler); |
| 7781 | Py_XDECREF(exc); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 7782 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 7783 | if (_PyUnicode_READY_REPLACE(&v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7784 | Py_DECREF(v); |
| 7785 | return NULL; |
| 7786 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 7787 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 7788 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7789 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7790 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7791 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7792 | Py_XDECREF(errorHandler); |
| 7793 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7794 | Py_XDECREF(v); |
| 7795 | return NULL; |
| 7796 | } |
| 7797 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7798 | /* Charmap encoding: the lookup table */ |
| 7799 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7800 | struct encoding_map { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7801 | PyObject_HEAD |
| 7802 | unsigned char level1[32]; |
| 7803 | int count2, count3; |
| 7804 | unsigned char level23[1]; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7805 | }; |
| 7806 | |
| 7807 | static PyObject* |
| 7808 | encoding_map_size(PyObject *obj, PyObject* args) |
| 7809 | { |
| 7810 | struct encoding_map *map = (struct encoding_map*)obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7811 | return PyLong_FromLong(sizeof(*map) - 1 + 16*map->count2 + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7812 | 128*map->count3); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7813 | } |
| 7814 | |
| 7815 | static PyMethodDef encoding_map_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7816 | {"size", encoding_map_size, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7817 | PyDoc_STR("Return the size (in bytes) of this object") }, |
| 7818 | { 0 } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7819 | }; |
| 7820 | |
| 7821 | static void |
| 7822 | encoding_map_dealloc(PyObject* o) |
| 7823 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7824 | PyObject_FREE(o); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7825 | } |
| 7826 | |
| 7827 | static PyTypeObject EncodingMapType = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7828 | PyVarObject_HEAD_INIT(NULL, 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7829 | "EncodingMap", /*tp_name*/ |
| 7830 | sizeof(struct encoding_map), /*tp_basicsize*/ |
| 7831 | 0, /*tp_itemsize*/ |
| 7832 | /* methods */ |
| 7833 | encoding_map_dealloc, /*tp_dealloc*/ |
| 7834 | 0, /*tp_print*/ |
| 7835 | 0, /*tp_getattr*/ |
| 7836 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 7837 | 0, /*tp_reserved*/ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7838 | 0, /*tp_repr*/ |
| 7839 | 0, /*tp_as_number*/ |
| 7840 | 0, /*tp_as_sequence*/ |
| 7841 | 0, /*tp_as_mapping*/ |
| 7842 | 0, /*tp_hash*/ |
| 7843 | 0, /*tp_call*/ |
| 7844 | 0, /*tp_str*/ |
| 7845 | 0, /*tp_getattro*/ |
| 7846 | 0, /*tp_setattro*/ |
| 7847 | 0, /*tp_as_buffer*/ |
| 7848 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 7849 | 0, /*tp_doc*/ |
| 7850 | 0, /*tp_traverse*/ |
| 7851 | 0, /*tp_clear*/ |
| 7852 | 0, /*tp_richcompare*/ |
| 7853 | 0, /*tp_weaklistoffset*/ |
| 7854 | 0, /*tp_iter*/ |
| 7855 | 0, /*tp_iternext*/ |
| 7856 | encoding_map_methods, /*tp_methods*/ |
| 7857 | 0, /*tp_members*/ |
| 7858 | 0, /*tp_getset*/ |
| 7859 | 0, /*tp_base*/ |
| 7860 | 0, /*tp_dict*/ |
| 7861 | 0, /*tp_descr_get*/ |
| 7862 | 0, /*tp_descr_set*/ |
| 7863 | 0, /*tp_dictoffset*/ |
| 7864 | 0, /*tp_init*/ |
| 7865 | 0, /*tp_alloc*/ |
| 7866 | 0, /*tp_new*/ |
| 7867 | 0, /*tp_free*/ |
| 7868 | 0, /*tp_is_gc*/ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7869 | }; |
| 7870 | |
| 7871 | PyObject* |
| 7872 | PyUnicode_BuildEncodingMap(PyObject* string) |
| 7873 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7874 | PyObject *result; |
| 7875 | struct encoding_map *mresult; |
| 7876 | int i; |
| 7877 | int need_dict = 0; |
| 7878 | unsigned char level1[32]; |
| 7879 | unsigned char level2[512]; |
| 7880 | unsigned char *mlevel1, *mlevel2, *mlevel3; |
| 7881 | int count2 = 0, count3 = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7882 | int kind; |
| 7883 | void *data; |
| 7884 | Py_UCS4 ch; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7885 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7886 | if (!PyUnicode_Check(string) || PyUnicode_GET_LENGTH(string) != 256) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7887 | PyErr_BadArgument(); |
| 7888 | return NULL; |
| 7889 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7890 | kind = PyUnicode_KIND(string); |
| 7891 | data = PyUnicode_DATA(string); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7892 | memset(level1, 0xFF, sizeof level1); |
| 7893 | memset(level2, 0xFF, sizeof level2); |
| 7894 | |
| 7895 | /* If there isn't a one-to-one mapping of NULL to \0, |
| 7896 | or if there are non-BMP characters, we need to use |
| 7897 | a mapping dictionary. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7898 | if (PyUnicode_READ(kind, data, 0) != 0) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7899 | need_dict = 1; |
| 7900 | for (i = 1; i < 256; i++) { |
| 7901 | int l1, l2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7902 | ch = PyUnicode_READ(kind, data, i); |
| 7903 | if (ch == 0 || ch > 0xFFFF) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7904 | need_dict = 1; |
| 7905 | break; |
| 7906 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7907 | if (ch == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7908 | /* unmapped character */ |
| 7909 | continue; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7910 | l1 = ch >> 11; |
| 7911 | l2 = ch >> 7; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7912 | if (level1[l1] == 0xFF) |
| 7913 | level1[l1] = count2++; |
| 7914 | if (level2[l2] == 0xFF) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7915 | level2[l2] = count3++; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7916 | } |
| 7917 | |
| 7918 | if (count2 >= 0xFF || count3 >= 0xFF) |
| 7919 | need_dict = 1; |
| 7920 | |
| 7921 | if (need_dict) { |
| 7922 | PyObject *result = PyDict_New(); |
| 7923 | PyObject *key, *value; |
| 7924 | if (!result) |
| 7925 | return NULL; |
| 7926 | for (i = 0; i < 256; i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7927 | key = PyLong_FromLong(PyUnicode_READ(kind, data, i)); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7928 | value = PyLong_FromLong(i); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7929 | if (!key || !value) |
| 7930 | goto failed1; |
| 7931 | if (PyDict_SetItem(result, key, value) == -1) |
| 7932 | goto failed1; |
| 7933 | Py_DECREF(key); |
| 7934 | Py_DECREF(value); |
| 7935 | } |
| 7936 | return result; |
| 7937 | failed1: |
| 7938 | Py_XDECREF(key); |
| 7939 | Py_XDECREF(value); |
| 7940 | Py_DECREF(result); |
| 7941 | return NULL; |
| 7942 | } |
| 7943 | |
| 7944 | /* Create a three-level trie */ |
| 7945 | result = PyObject_MALLOC(sizeof(struct encoding_map) + |
| 7946 | 16*count2 + 128*count3 - 1); |
| 7947 | if (!result) |
| 7948 | return PyErr_NoMemory(); |
| 7949 | PyObject_Init(result, &EncodingMapType); |
| 7950 | mresult = (struct encoding_map*)result; |
| 7951 | mresult->count2 = count2; |
| 7952 | mresult->count3 = count3; |
| 7953 | mlevel1 = mresult->level1; |
| 7954 | mlevel2 = mresult->level23; |
| 7955 | mlevel3 = mresult->level23 + 16*count2; |
| 7956 | memcpy(mlevel1, level1, 32); |
| 7957 | memset(mlevel2, 0xFF, 16*count2); |
| 7958 | memset(mlevel3, 0, 128*count3); |
| 7959 | count3 = 0; |
| 7960 | for (i = 1; i < 256; i++) { |
| 7961 | int o1, o2, o3, i2, i3; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7962 | if (PyUnicode_READ(kind, data, i) == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7963 | /* unmapped character */ |
| 7964 | continue; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7965 | o1 = PyUnicode_READ(kind, data, i)>>11; |
| 7966 | o2 = (PyUnicode_READ(kind, data, i)>>7) & 0xF; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7967 | i2 = 16*mlevel1[o1] + o2; |
| 7968 | if (mlevel2[i2] == 0xFF) |
| 7969 | mlevel2[i2] = count3++; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7970 | o3 = PyUnicode_READ(kind, data, i) & 0x7F; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7971 | i3 = 128*mlevel2[i2] + o3; |
| 7972 | mlevel3[i3] = i; |
| 7973 | } |
| 7974 | return result; |
| 7975 | } |
| 7976 | |
| 7977 | static int |
| 7978 | encoding_map_lookup(Py_UNICODE c, PyObject *mapping) |
| 7979 | { |
| 7980 | struct encoding_map *map = (struct encoding_map*)mapping; |
| 7981 | int l1 = c>>11; |
| 7982 | int l2 = (c>>7) & 0xF; |
| 7983 | int l3 = c & 0x7F; |
| 7984 | int i; |
| 7985 | |
| 7986 | #ifdef Py_UNICODE_WIDE |
| 7987 | if (c > 0xFFFF) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7988 | return -1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7989 | } |
| 7990 | #endif |
| 7991 | if (c == 0) |
| 7992 | return 0; |
| 7993 | /* level 1*/ |
| 7994 | i = map->level1[l1]; |
| 7995 | if (i == 0xFF) { |
| 7996 | return -1; |
| 7997 | } |
| 7998 | /* level 2*/ |
| 7999 | i = map->level23[16*i+l2]; |
| 8000 | if (i == 0xFF) { |
| 8001 | return -1; |
| 8002 | } |
| 8003 | /* level 3 */ |
| 8004 | i = map->level23[16*map->count2 + 128*i + l3]; |
| 8005 | if (i == 0) { |
| 8006 | return -1; |
| 8007 | } |
| 8008 | return i; |
| 8009 | } |
| 8010 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8011 | /* Lookup the character ch in the mapping. If the character |
| 8012 | can't be found, Py_None is returned (or NULL, if another |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 8013 | error occurred). */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8014 | static PyObject * |
| 8015 | charmapencode_lookup(Py_UNICODE c, PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8016 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8017 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8018 | PyObject *x; |
| 8019 | |
| 8020 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8021 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8022 | x = PyObject_GetItem(mapping, w); |
| 8023 | Py_DECREF(w); |
| 8024 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8025 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 8026 | /* No mapping found means: mapping is undefined. */ |
| 8027 | PyErr_Clear(); |
| 8028 | x = Py_None; |
| 8029 | Py_INCREF(x); |
| 8030 | return x; |
| 8031 | } else |
| 8032 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8033 | } |
Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 8034 | else if (x == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8035 | return x; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8036 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8037 | long value = PyLong_AS_LONG(x); |
| 8038 | if (value < 0 || value > 255) { |
| 8039 | PyErr_SetString(PyExc_TypeError, |
| 8040 | "character mapping must be in range(256)"); |
| 8041 | Py_DECREF(x); |
| 8042 | return NULL; |
| 8043 | } |
| 8044 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8045 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8046 | else if (PyBytes_Check(x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8047 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8048 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8049 | /* wrong return value */ |
| 8050 | PyErr_Format(PyExc_TypeError, |
| 8051 | "character mapping must return integer, bytes or None, not %.400s", |
| 8052 | x->ob_type->tp_name); |
| 8053 | Py_DECREF(x); |
| 8054 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8055 | } |
| 8056 | } |
| 8057 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8058 | static int |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 8059 | charmapencode_resize(PyObject **outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8060 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8061 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
| 8062 | /* exponentially overallocate to minimize reallocations */ |
| 8063 | if (requiredsize < 2*outsize) |
| 8064 | requiredsize = 2*outsize; |
| 8065 | if (_PyBytes_Resize(outobj, requiredsize)) |
| 8066 | return -1; |
| 8067 | return 0; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8068 | } |
| 8069 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8070 | typedef enum charmapencode_result { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8071 | enc_SUCCESS, enc_FAILED, enc_EXCEPTION |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8072 | } charmapencode_result; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8073 | /* 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] | 8074 | various state variables. Resize the output bytes object if not enough |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8075 | space is available. Return a new reference to the object that |
| 8076 | was put in the output buffer, or Py_None, if the mapping was undefined |
| 8077 | (in which case no character was written) or NULL, if a |
Andrew M. Kuchling | 8294de5 | 2005-11-02 16:36:12 +0000 | [diff] [blame] | 8078 | reallocation error occurred. The caller must decref the result */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8079 | static charmapencode_result |
| 8080 | charmapencode_output(Py_UNICODE c, PyObject *mapping, |
| 8081 | PyObject **outobj, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8082 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8083 | PyObject *rep; |
| 8084 | char *outstart; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8085 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8086 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 8087 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8088 | int res = encoding_map_lookup(c, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8089 | Py_ssize_t requiredsize = *outpos+1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8090 | if (res == -1) |
| 8091 | return enc_FAILED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8092 | if (outsize<requiredsize) |
| 8093 | if (charmapencode_resize(outobj, outpos, requiredsize)) |
| 8094 | return enc_EXCEPTION; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8095 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8096 | outstart[(*outpos)++] = (char)res; |
| 8097 | return enc_SUCCESS; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8098 | } |
| 8099 | |
| 8100 | rep = charmapencode_lookup(c, mapping); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8101 | if (rep==NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8102 | return enc_EXCEPTION; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8103 | else if (rep==Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8104 | Py_DECREF(rep); |
| 8105 | return enc_FAILED; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8106 | } else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8107 | if (PyLong_Check(rep)) { |
| 8108 | Py_ssize_t requiredsize = *outpos+1; |
| 8109 | if (outsize<requiredsize) |
| 8110 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 8111 | Py_DECREF(rep); |
| 8112 | return enc_EXCEPTION; |
| 8113 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8114 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8115 | outstart[(*outpos)++] = (char)PyLong_AS_LONG(rep); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8116 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8117 | else { |
| 8118 | const char *repchars = PyBytes_AS_STRING(rep); |
| 8119 | Py_ssize_t repsize = PyBytes_GET_SIZE(rep); |
| 8120 | Py_ssize_t requiredsize = *outpos+repsize; |
| 8121 | if (outsize<requiredsize) |
| 8122 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 8123 | Py_DECREF(rep); |
| 8124 | return enc_EXCEPTION; |
| 8125 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8126 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8127 | memcpy(outstart + *outpos, repchars, repsize); |
| 8128 | *outpos += repsize; |
| 8129 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8130 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8131 | Py_DECREF(rep); |
| 8132 | return enc_SUCCESS; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8133 | } |
| 8134 | |
| 8135 | /* handle an error in PyUnicode_EncodeCharmap |
| 8136 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8137 | static int |
| 8138 | charmap_encoding_error( |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8139 | const Py_UNICODE *p, Py_ssize_t size, Py_ssize_t *inpos, PyObject *mapping, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8140 | PyObject **exceptionObject, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 8141 | int *known_errorHandler, PyObject **errorHandler, const char *errors, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 8142 | PyObject **res, Py_ssize_t *respos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8143 | { |
| 8144 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8145 | Py_ssize_t repsize; |
| 8146 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8147 | Py_UNICODE *uni2; |
| 8148 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8149 | Py_ssize_t collstartpos = *inpos; |
| 8150 | Py_ssize_t collendpos = *inpos+1; |
| 8151 | Py_ssize_t collpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8152 | char *encoding = "charmap"; |
| 8153 | char *reason = "character maps to <undefined>"; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8154 | charmapencode_result x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8155 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8156 | /* find all unencodable characters */ |
| 8157 | while (collendpos < size) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8158 | PyObject *rep; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 8159 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8160 | int res = encoding_map_lookup(p[collendpos], mapping); |
| 8161 | if (res != -1) |
| 8162 | break; |
| 8163 | ++collendpos; |
| 8164 | continue; |
| 8165 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8166 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8167 | rep = charmapencode_lookup(p[collendpos], mapping); |
| 8168 | if (rep==NULL) |
| 8169 | return -1; |
| 8170 | else if (rep!=Py_None) { |
| 8171 | Py_DECREF(rep); |
| 8172 | break; |
| 8173 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8174 | Py_DECREF(rep); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8175 | ++collendpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8176 | } |
| 8177 | /* cache callback name lookup |
| 8178 | * (if not done yet, i.e. it's the first error) */ |
| 8179 | if (*known_errorHandler==-1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8180 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 8181 | *known_errorHandler = 1; |
| 8182 | else if (!strcmp(errors, "replace")) |
| 8183 | *known_errorHandler = 2; |
| 8184 | else if (!strcmp(errors, "ignore")) |
| 8185 | *known_errorHandler = 3; |
| 8186 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 8187 | *known_errorHandler = 4; |
| 8188 | else |
| 8189 | *known_errorHandler = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8190 | } |
| 8191 | switch (*known_errorHandler) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8192 | case 1: /* strict */ |
| 8193 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 8194 | return -1; |
| 8195 | case 2: /* replace */ |
| 8196 | for (collpos = collstartpos; collpos<collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8197 | x = charmapencode_output('?', mapping, res, respos); |
| 8198 | if (x==enc_EXCEPTION) { |
| 8199 | return -1; |
| 8200 | } |
| 8201 | else if (x==enc_FAILED) { |
| 8202 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 8203 | return -1; |
| 8204 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8205 | } |
| 8206 | /* fall through */ |
| 8207 | case 3: /* ignore */ |
| 8208 | *inpos = collendpos; |
| 8209 | break; |
| 8210 | case 4: /* xmlcharrefreplace */ |
| 8211 | /* generate replacement (temporarily (mis)uses p) */ |
| 8212 | for (collpos = collstartpos; collpos < collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8213 | char buffer[2+29+1+1]; |
| 8214 | char *cp; |
| 8215 | sprintf(buffer, "&#%d;", (int)p[collpos]); |
| 8216 | for (cp = buffer; *cp; ++cp) { |
| 8217 | x = charmapencode_output(*cp, mapping, res, respos); |
| 8218 | if (x==enc_EXCEPTION) |
| 8219 | return -1; |
| 8220 | else if (x==enc_FAILED) { |
| 8221 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 8222 | return -1; |
| 8223 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8224 | } |
| 8225 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8226 | *inpos = collendpos; |
| 8227 | break; |
| 8228 | default: |
| 8229 | repunicode = unicode_encode_call_errorhandler(errors, errorHandler, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8230 | encoding, reason, p, size, exceptionObject, |
| 8231 | collstartpos, collendpos, &newpos); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8232 | if (repunicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8233 | return -1; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8234 | if (PyBytes_Check(repunicode)) { |
| 8235 | /* Directly copy bytes result to output. */ |
| 8236 | Py_ssize_t outsize = PyBytes_Size(*res); |
| 8237 | Py_ssize_t requiredsize; |
| 8238 | repsize = PyBytes_Size(repunicode); |
| 8239 | requiredsize = *respos + repsize; |
| 8240 | if (requiredsize > outsize) |
| 8241 | /* Make room for all additional bytes. */ |
| 8242 | if (charmapencode_resize(res, respos, requiredsize)) { |
| 8243 | Py_DECREF(repunicode); |
| 8244 | return -1; |
| 8245 | } |
| 8246 | memcpy(PyBytes_AsString(*res) + *respos, |
| 8247 | PyBytes_AsString(repunicode), repsize); |
| 8248 | *respos += repsize; |
| 8249 | *inpos = newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 8250 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8251 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 8252 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8253 | /* generate replacement */ |
| 8254 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 8255 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8256 | x = charmapencode_output(*uni2, mapping, res, respos); |
| 8257 | if (x==enc_EXCEPTION) { |
| 8258 | return -1; |
| 8259 | } |
| 8260 | else if (x==enc_FAILED) { |
| 8261 | Py_DECREF(repunicode); |
| 8262 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 8263 | return -1; |
| 8264 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8265 | } |
| 8266 | *inpos = newpos; |
| 8267 | Py_DECREF(repunicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8268 | } |
| 8269 | return 0; |
| 8270 | } |
| 8271 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8272 | PyObject * |
| 8273 | PyUnicode_EncodeCharmap(const Py_UNICODE *p, |
| 8274 | Py_ssize_t size, |
| 8275 | PyObject *mapping, |
| 8276 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8277 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8278 | /* output object */ |
| 8279 | PyObject *res = NULL; |
| 8280 | /* current input position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8281 | Py_ssize_t inpos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8282 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8283 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8284 | PyObject *errorHandler = NULL; |
| 8285 | PyObject *exc = NULL; |
| 8286 | /* the following variable is used for caching string comparisons |
| 8287 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 8288 | * 3=ignore, 4=xmlcharrefreplace */ |
| 8289 | int known_errorHandler = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8290 | |
| 8291 | /* Default to Latin-1 */ |
| 8292 | if (mapping == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8293 | return PyUnicode_EncodeLatin1(p, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8294 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8295 | /* allocate enough for a simple encoding without |
| 8296 | replacements, if we need more, we'll resize */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8297 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8298 | if (res == NULL) |
| 8299 | goto onError; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 8300 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8301 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8302 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8303 | while (inpos<size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8304 | /* try to encode it */ |
| 8305 | charmapencode_result x = charmapencode_output(p[inpos], mapping, &res, &respos); |
| 8306 | if (x==enc_EXCEPTION) /* error */ |
| 8307 | goto onError; |
| 8308 | if (x==enc_FAILED) { /* unencodable character */ |
| 8309 | if (charmap_encoding_error(p, size, &inpos, mapping, |
| 8310 | &exc, |
| 8311 | &known_errorHandler, &errorHandler, errors, |
| 8312 | &res, &respos)) { |
| 8313 | goto onError; |
| 8314 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8315 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8316 | else |
| 8317 | /* done with this character => adjust input position */ |
| 8318 | ++inpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8319 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8320 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8321 | /* Resize if we allocated to much */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8322 | if (respos<PyBytes_GET_SIZE(res)) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 8323 | if (_PyBytes_Resize(&res, respos) < 0) |
| 8324 | goto onError; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 8325 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8326 | Py_XDECREF(exc); |
| 8327 | Py_XDECREF(errorHandler); |
| 8328 | return res; |
| 8329 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8330 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8331 | Py_XDECREF(res); |
| 8332 | Py_XDECREF(exc); |
| 8333 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8334 | return NULL; |
| 8335 | } |
| 8336 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8337 | PyObject * |
| 8338 | PyUnicode_AsCharmapString(PyObject *unicode, |
| 8339 | PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8340 | { |
| 8341 | if (!PyUnicode_Check(unicode) || mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8342 | PyErr_BadArgument(); |
| 8343 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8344 | } |
| 8345 | return PyUnicode_EncodeCharmap(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8346 | PyUnicode_GET_SIZE(unicode), |
| 8347 | mapping, |
| 8348 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8349 | } |
| 8350 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8351 | /* create or adjust a UnicodeTranslateError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8352 | static void |
| 8353 | make_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8354 | PyObject *unicode, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8355 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 8356 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8357 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8358 | if (*exceptionObject == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8359 | *exceptionObject = _PyUnicodeTranslateError_Create( |
| 8360 | unicode, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8361 | } |
| 8362 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8363 | if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
| 8364 | goto onError; |
| 8365 | if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) |
| 8366 | goto onError; |
| 8367 | if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) |
| 8368 | goto onError; |
| 8369 | return; |
| 8370 | onError: |
| 8371 | Py_DECREF(*exceptionObject); |
| 8372 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8373 | } |
| 8374 | } |
| 8375 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8376 | /* raises a UnicodeTranslateError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8377 | static void |
| 8378 | raise_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8379 | PyObject *unicode, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8380 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 8381 | const char *reason) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8382 | { |
| 8383 | make_translate_exception(exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8384 | unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8385 | if (*exceptionObject != NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8386 | PyCodec_StrictErrors(*exceptionObject); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8387 | } |
| 8388 | |
| 8389 | /* error handling callback helper: |
| 8390 | build arguments, call the callback and check the arguments, |
| 8391 | put the result into newpos and return the replacement string, which |
| 8392 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8393 | static PyObject * |
| 8394 | unicode_translate_call_errorhandler(const char *errors, |
| 8395 | PyObject **errorHandler, |
| 8396 | const char *reason, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8397 | PyObject *unicode, PyObject **exceptionObject, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8398 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 8399 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8400 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 8401 | 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] | 8402 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8403 | Py_ssize_t i_newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8404 | PyObject *restuple; |
| 8405 | PyObject *resunicode; |
| 8406 | |
| 8407 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8408 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8409 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8410 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8411 | } |
| 8412 | |
| 8413 | make_translate_exception(exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8414 | unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8415 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8416 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8417 | |
| 8418 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8419 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8420 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8421 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8422 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 8423 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8424 | Py_DECREF(restuple); |
| 8425 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8426 | } |
| 8427 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8428 | &resunicode, &i_newpos)) { |
| 8429 | Py_DECREF(restuple); |
| 8430 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8431 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8432 | if (i_newpos<0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8433 | *newpos = PyUnicode_GET_LENGTH(unicode)+i_newpos; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8434 | else |
| 8435 | *newpos = i_newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8436 | if (*newpos<0 || *newpos>PyUnicode_GET_LENGTH(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8437 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 8438 | Py_DECREF(restuple); |
| 8439 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 8440 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8441 | Py_INCREF(resunicode); |
| 8442 | Py_DECREF(restuple); |
| 8443 | return resunicode; |
| 8444 | } |
| 8445 | |
| 8446 | /* Lookup the character ch in the mapping and put the result in result, |
| 8447 | which must be decrefed by the caller. |
| 8448 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8449 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8450 | charmaptranslate_lookup(Py_UCS4 c, PyObject *mapping, PyObject **result) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8451 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8452 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8453 | PyObject *x; |
| 8454 | |
| 8455 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8456 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8457 | x = PyObject_GetItem(mapping, w); |
| 8458 | Py_DECREF(w); |
| 8459 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8460 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 8461 | /* No mapping found means: use 1:1 mapping. */ |
| 8462 | PyErr_Clear(); |
| 8463 | *result = NULL; |
| 8464 | return 0; |
| 8465 | } else |
| 8466 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8467 | } |
| 8468 | else if (x == Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8469 | *result = x; |
| 8470 | return 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8471 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8472 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8473 | long value = PyLong_AS_LONG(x); |
| 8474 | long max = PyUnicode_GetMax(); |
| 8475 | if (value < 0 || value > max) { |
| 8476 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5a2f7e60 | 2007-10-24 21:13:09 +0000 | [diff] [blame] | 8477 | "character mapping must be in range(0x%x)", max+1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8478 | Py_DECREF(x); |
| 8479 | return -1; |
| 8480 | } |
| 8481 | *result = x; |
| 8482 | return 0; |
| 8483 | } |
| 8484 | else if (PyUnicode_Check(x)) { |
| 8485 | *result = x; |
| 8486 | return 0; |
| 8487 | } |
| 8488 | else { |
| 8489 | /* wrong return value */ |
| 8490 | PyErr_SetString(PyExc_TypeError, |
| 8491 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8492 | Py_DECREF(x); |
| 8493 | return -1; |
| 8494 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8495 | } |
| 8496 | /* ensure that *outobj is at least requiredsize characters long, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8497 | if not reallocate and adjust various state variables. |
| 8498 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8499 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8500 | charmaptranslate_makespace(Py_UCS4 **outobj, Py_ssize_t *psize, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8501 | Py_ssize_t requiredsize) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8502 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8503 | Py_ssize_t oldsize = *psize; |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 8504 | if (requiredsize > oldsize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8505 | /* exponentially overallocate to minimize reallocations */ |
| 8506 | if (requiredsize < 2 * oldsize) |
| 8507 | requiredsize = 2 * oldsize; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8508 | *outobj = PyMem_Realloc(*outobj, requiredsize * sizeof(Py_UCS4)); |
| 8509 | if (*outobj == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8510 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8511 | *psize = requiredsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8512 | } |
| 8513 | return 0; |
| 8514 | } |
| 8515 | /* lookup the character, put the result in the output string and adjust |
| 8516 | various state variables. Return a new reference to the object that |
| 8517 | was put in the output buffer in *result, or Py_None, if the mapping was |
| 8518 | undefined (in which case no character was written). |
| 8519 | The called must decref result. |
| 8520 | Return 0 on success, -1 on error. */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8521 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8522 | charmaptranslate_output(PyObject *input, Py_ssize_t ipos, |
| 8523 | PyObject *mapping, Py_UCS4 **output, |
| 8524 | Py_ssize_t *osize, Py_ssize_t *opos, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8525 | PyObject **res) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8526 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8527 | Py_UCS4 curinp = PyUnicode_READ_CHAR(input, ipos); |
| 8528 | if (charmaptranslate_lookup(curinp, mapping, res)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8529 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8530 | if (*res==NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8531 | /* not found => default to 1:1 mapping */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8532 | (*output)[(*opos)++] = curinp; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8533 | } |
| 8534 | else if (*res==Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8535 | ; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8536 | else if (PyLong_Check(*res)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8537 | /* 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] | 8538 | (*output)[(*opos)++] = (Py_UCS4)PyLong_AS_LONG(*res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8539 | } |
| 8540 | else if (PyUnicode_Check(*res)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8541 | Py_ssize_t repsize; |
| 8542 | if (PyUnicode_READY(*res) == -1) |
| 8543 | return -1; |
| 8544 | repsize = PyUnicode_GET_LENGTH(*res); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8545 | if (repsize==1) { |
| 8546 | /* 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] | 8547 | (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8548 | } |
| 8549 | else if (repsize!=0) { |
| 8550 | /* more than one character */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8551 | Py_ssize_t requiredsize = *opos + |
| 8552 | (PyUnicode_GET_LENGTH(input) - ipos) + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8553 | repsize - 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8554 | Py_ssize_t i; |
| 8555 | if (charmaptranslate_makespace(output, osize, requiredsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8556 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8557 | for(i = 0; i < repsize; i++) |
| 8558 | (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, i); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8559 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8560 | } |
| 8561 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8562 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8563 | return 0; |
| 8564 | } |
| 8565 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8566 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8567 | _PyUnicode_TranslateCharmap(PyObject *input, |
| 8568 | PyObject *mapping, |
| 8569 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8570 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8571 | /* input object */ |
| 8572 | char *idata; |
| 8573 | Py_ssize_t size, i; |
| 8574 | int kind; |
| 8575 | /* output buffer */ |
| 8576 | Py_UCS4 *output = NULL; |
| 8577 | Py_ssize_t osize; |
| 8578 | PyObject *res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8579 | /* current output position */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8580 | Py_ssize_t opos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8581 | char *reason = "character maps to <undefined>"; |
| 8582 | PyObject *errorHandler = NULL; |
| 8583 | PyObject *exc = NULL; |
| 8584 | /* the following variable is used for caching string comparisons |
| 8585 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 8586 | * 3=ignore, 4=xmlcharrefreplace */ |
| 8587 | int known_errorHandler = -1; |
| 8588 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8589 | if (mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8590 | PyErr_BadArgument(); |
| 8591 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8592 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8593 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8594 | if (PyUnicode_READY(input) == -1) |
| 8595 | return NULL; |
| 8596 | idata = (char*)PyUnicode_DATA(input); |
| 8597 | kind = PyUnicode_KIND(input); |
| 8598 | size = PyUnicode_GET_LENGTH(input); |
| 8599 | i = 0; |
| 8600 | |
| 8601 | if (size == 0) { |
| 8602 | Py_INCREF(input); |
| 8603 | return input; |
| 8604 | } |
| 8605 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8606 | /* allocate enough for a simple 1:1 translation without |
| 8607 | replacements, if we need more, we'll resize */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8608 | osize = size; |
| 8609 | output = PyMem_Malloc(osize * sizeof(Py_UCS4)); |
| 8610 | opos = 0; |
| 8611 | if (output == NULL) { |
| 8612 | PyErr_NoMemory(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8613 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8614 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8615 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8616 | while (i<size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8617 | /* try to encode it */ |
| 8618 | PyObject *x = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8619 | if (charmaptranslate_output(input, i, mapping, |
| 8620 | &output, &osize, &opos, &x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8621 | Py_XDECREF(x); |
| 8622 | goto onError; |
| 8623 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8624 | Py_XDECREF(x); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8625 | if (x!=Py_None) /* it worked => adjust input pointer */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8626 | ++i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8627 | else { /* untranslatable character */ |
| 8628 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
| 8629 | Py_ssize_t repsize; |
| 8630 | Py_ssize_t newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8631 | Py_ssize_t uni2; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8632 | /* startpos for collecting untranslatable chars */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8633 | Py_ssize_t collstart = i; |
| 8634 | Py_ssize_t collend = i+1; |
| 8635 | Py_ssize_t coll; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8636 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8637 | /* find all untranslatable characters */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8638 | while (collend < size) { |
| 8639 | if (charmaptranslate_lookup(PyUnicode_READ(kind,idata, collend), mapping, &x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8640 | goto onError; |
| 8641 | Py_XDECREF(x); |
| 8642 | if (x!=Py_None) |
| 8643 | break; |
| 8644 | ++collend; |
| 8645 | } |
| 8646 | /* cache callback name lookup |
| 8647 | * (if not done yet, i.e. it's the first error) */ |
| 8648 | if (known_errorHandler==-1) { |
| 8649 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 8650 | known_errorHandler = 1; |
| 8651 | else if (!strcmp(errors, "replace")) |
| 8652 | known_errorHandler = 2; |
| 8653 | else if (!strcmp(errors, "ignore")) |
| 8654 | known_errorHandler = 3; |
| 8655 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 8656 | known_errorHandler = 4; |
| 8657 | else |
| 8658 | known_errorHandler = 0; |
| 8659 | } |
| 8660 | switch (known_errorHandler) { |
| 8661 | case 1: /* strict */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8662 | raise_translate_exception(&exc, input, collstart, |
| 8663 | collend, reason); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8664 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8665 | case 2: /* replace */ |
| 8666 | /* 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] | 8667 | for (coll = collstart; coll<collend; coll++) |
| 8668 | output[opos++] = '?'; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8669 | /* fall through */ |
| 8670 | case 3: /* ignore */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8671 | i = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8672 | break; |
| 8673 | case 4: /* xmlcharrefreplace */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8674 | /* generate replacement (temporarily (mis)uses i) */ |
| 8675 | for (i = collstart; i < collend; ++i) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8676 | char buffer[2+29+1+1]; |
| 8677 | char *cp; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8678 | sprintf(buffer, "&#%d;", PyUnicode_READ(kind, idata, i)); |
| 8679 | if (charmaptranslate_makespace(&output, &osize, |
| 8680 | opos+strlen(buffer)+(size-collend))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8681 | goto onError; |
| 8682 | for (cp = buffer; *cp; ++cp) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8683 | output[opos++] = *cp; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8684 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8685 | i = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8686 | break; |
| 8687 | default: |
| 8688 | repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8689 | reason, input, &exc, |
| 8690 | collstart, collend, &newpos); |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 8691 | if (repunicode == NULL || _PyUnicode_READY_REPLACE(&repunicode)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8692 | goto onError; |
| 8693 | /* generate replacement */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8694 | repsize = PyUnicode_GET_LENGTH(repunicode); |
| 8695 | if (charmaptranslate_makespace(&output, &osize, |
| 8696 | opos+repsize+(size-collend))) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8697 | Py_DECREF(repunicode); |
| 8698 | goto onError; |
| 8699 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8700 | for (uni2 = 0; repsize-->0; ++uni2) |
| 8701 | output[opos++] = PyUnicode_READ_CHAR(repunicode, uni2); |
| 8702 | i = newpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8703 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8704 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8705 | } |
| 8706 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8707 | res = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, output, opos); |
| 8708 | if (!res) |
| 8709 | goto onError; |
| 8710 | PyMem_Free(output); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8711 | Py_XDECREF(exc); |
| 8712 | Py_XDECREF(errorHandler); |
| 8713 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8714 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8715 | onError: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8716 | PyMem_Free(output); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8717 | Py_XDECREF(exc); |
| 8718 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8719 | return NULL; |
| 8720 | } |
| 8721 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8722 | /* Deprecated. Use PyUnicode_Translate instead. */ |
| 8723 | PyObject * |
| 8724 | PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
| 8725 | Py_ssize_t size, |
| 8726 | PyObject *mapping, |
| 8727 | const char *errors) |
| 8728 | { |
| 8729 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 8730 | if (!unicode) |
| 8731 | return NULL; |
| 8732 | return _PyUnicode_TranslateCharmap(unicode, mapping, errors); |
| 8733 | } |
| 8734 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8735 | PyObject * |
| 8736 | PyUnicode_Translate(PyObject *str, |
| 8737 | PyObject *mapping, |
| 8738 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8739 | { |
| 8740 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8741 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8742 | str = PyUnicode_FromObject(str); |
| 8743 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8744 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8745 | result = _PyUnicode_TranslateCharmap(str, mapping, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8746 | Py_DECREF(str); |
| 8747 | return result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8748 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8749 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8750 | Py_XDECREF(str); |
| 8751 | return NULL; |
| 8752 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8753 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8754 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8755 | fix_decimal_and_space_to_ascii(PyObject *self) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8756 | { |
| 8757 | /* No need to call PyUnicode_READY(self) because this function is only |
| 8758 | called as a callback from fixup() which does it already. */ |
| 8759 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 8760 | const int kind = PyUnicode_KIND(self); |
| 8761 | void *data = PyUnicode_DATA(self); |
| 8762 | Py_UCS4 maxchar = 0, ch, fixed; |
| 8763 | Py_ssize_t i; |
| 8764 | |
| 8765 | for (i = 0; i < len; ++i) { |
| 8766 | ch = PyUnicode_READ(kind, data, i); |
| 8767 | fixed = 0; |
| 8768 | if (ch > 127) { |
| 8769 | if (Py_UNICODE_ISSPACE(ch)) |
| 8770 | fixed = ' '; |
| 8771 | else { |
| 8772 | const int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8773 | if (decimal >= 0) |
| 8774 | fixed = '0' + decimal; |
| 8775 | } |
| 8776 | if (fixed != 0) { |
| 8777 | if (fixed > maxchar) |
| 8778 | maxchar = fixed; |
| 8779 | PyUnicode_WRITE(kind, data, i, fixed); |
| 8780 | } |
| 8781 | else if (ch > maxchar) |
| 8782 | maxchar = ch; |
| 8783 | } |
| 8784 | else if (ch > maxchar) |
| 8785 | maxchar = ch; |
| 8786 | } |
| 8787 | |
| 8788 | return maxchar; |
| 8789 | } |
| 8790 | |
| 8791 | PyObject * |
| 8792 | _PyUnicode_TransformDecimalAndSpaceToASCII(PyObject *unicode) |
| 8793 | { |
| 8794 | if (!PyUnicode_Check(unicode)) { |
| 8795 | PyErr_BadInternalCall(); |
| 8796 | return NULL; |
| 8797 | } |
| 8798 | if (PyUnicode_READY(unicode) == -1) |
| 8799 | return NULL; |
| 8800 | if (PyUnicode_MAX_CHAR_VALUE(unicode) <= 127) { |
| 8801 | /* If the string is already ASCII, just return the same string */ |
| 8802 | Py_INCREF(unicode); |
| 8803 | return unicode; |
| 8804 | } |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8805 | return fixup(unicode, fix_decimal_and_space_to_ascii); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8806 | } |
| 8807 | |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8808 | PyObject * |
| 8809 | PyUnicode_TransformDecimalToASCII(Py_UNICODE *s, |
| 8810 | Py_ssize_t length) |
| 8811 | { |
| 8812 | PyObject *result; |
| 8813 | Py_UNICODE *p; /* write pointer into result */ |
| 8814 | Py_ssize_t i; |
| 8815 | /* Copy to a new string */ |
| 8816 | result = (PyObject *)_PyUnicode_New(length); |
| 8817 | Py_UNICODE_COPY(PyUnicode_AS_UNICODE(result), s, length); |
| 8818 | if (result == NULL) |
| 8819 | return result; |
| 8820 | p = PyUnicode_AS_UNICODE(result); |
| 8821 | /* Iterate over code points */ |
| 8822 | for (i = 0; i < length; i++) { |
| 8823 | Py_UNICODE ch =s[i]; |
| 8824 | if (ch > 127) { |
| 8825 | int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8826 | if (decimal >= 0) |
| 8827 | p[i] = '0' + decimal; |
| 8828 | } |
| 8829 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 8830 | #ifndef DONT_MAKE_RESULT_READY |
| 8831 | if (_PyUnicode_READY_REPLACE(&result)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8832 | Py_DECREF(result); |
| 8833 | return NULL; |
| 8834 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 8835 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 8836 | assert(_PyUnicode_CheckConsistency(result, 1)); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8837 | return result; |
| 8838 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8839 | /* --- Decimal Encoder ---------------------------------------------------- */ |
| 8840 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8841 | int |
| 8842 | PyUnicode_EncodeDecimal(Py_UNICODE *s, |
| 8843 | Py_ssize_t length, |
| 8844 | char *output, |
| 8845 | const char *errors) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8846 | { |
| 8847 | Py_UNICODE *p, *end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8848 | PyObject *errorHandler = NULL; |
| 8849 | PyObject *exc = NULL; |
| 8850 | const char *encoding = "decimal"; |
| 8851 | const char *reason = "invalid decimal Unicode string"; |
| 8852 | /* the following variable is used for caching string comparisons |
| 8853 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 8854 | int known_errorHandler = -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8855 | |
| 8856 | if (output == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8857 | PyErr_BadArgument(); |
| 8858 | return -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8859 | } |
| 8860 | |
| 8861 | p = s; |
| 8862 | end = s + length; |
| 8863 | while (p < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8864 | register Py_UNICODE ch = *p; |
| 8865 | int decimal; |
| 8866 | PyObject *repunicode; |
| 8867 | Py_ssize_t repsize; |
| 8868 | Py_ssize_t newpos; |
| 8869 | Py_UNICODE *uni2; |
| 8870 | Py_UNICODE *collstart; |
| 8871 | Py_UNICODE *collend; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8872 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8873 | if (Py_UNICODE_ISSPACE(ch)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8874 | *output++ = ' '; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8875 | ++p; |
| 8876 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8877 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8878 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 8879 | if (decimal >= 0) { |
| 8880 | *output++ = '0' + decimal; |
| 8881 | ++p; |
| 8882 | continue; |
| 8883 | } |
| 8884 | if (0 < ch && ch < 256) { |
| 8885 | *output++ = (char)ch; |
| 8886 | ++p; |
| 8887 | continue; |
| 8888 | } |
| 8889 | /* All other characters are considered unencodable */ |
| 8890 | collstart = p; |
| 8891 | collend = p+1; |
| 8892 | while (collend < end) { |
| 8893 | if ((0 < *collend && *collend < 256) || |
| 8894 | !Py_UNICODE_ISSPACE(*collend) || |
| 8895 | Py_UNICODE_TODECIMAL(*collend)) |
| 8896 | break; |
| 8897 | } |
| 8898 | /* cache callback name lookup |
| 8899 | * (if not done yet, i.e. it's the first error) */ |
| 8900 | if (known_errorHandler==-1) { |
| 8901 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 8902 | known_errorHandler = 1; |
| 8903 | else if (!strcmp(errors, "replace")) |
| 8904 | known_errorHandler = 2; |
| 8905 | else if (!strcmp(errors, "ignore")) |
| 8906 | known_errorHandler = 3; |
| 8907 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 8908 | known_errorHandler = 4; |
| 8909 | else |
| 8910 | known_errorHandler = 0; |
| 8911 | } |
| 8912 | switch (known_errorHandler) { |
| 8913 | case 1: /* strict */ |
| 8914 | raise_encode_exception(&exc, encoding, s, length, collstart-s, collend-s, reason); |
| 8915 | goto onError; |
| 8916 | case 2: /* replace */ |
| 8917 | for (p = collstart; p < collend; ++p) |
| 8918 | *output++ = '?'; |
| 8919 | /* fall through */ |
| 8920 | case 3: /* ignore */ |
| 8921 | p = collend; |
| 8922 | break; |
| 8923 | case 4: /* xmlcharrefreplace */ |
| 8924 | /* generate replacement (temporarily (mis)uses p) */ |
| 8925 | for (p = collstart; p < collend; ++p) |
| 8926 | output += sprintf(output, "&#%d;", (int)*p); |
| 8927 | p = collend; |
| 8928 | break; |
| 8929 | default: |
| 8930 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 8931 | encoding, reason, s, length, &exc, |
| 8932 | collstart-s, collend-s, &newpos); |
| 8933 | if (repunicode == NULL) |
| 8934 | goto onError; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 8935 | if (!PyUnicode_Check(repunicode)) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8936 | /* Byte results not supported, since they have no decimal property. */ |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 8937 | PyErr_SetString(PyExc_TypeError, "error handler should return unicode"); |
| 8938 | Py_DECREF(repunicode); |
| 8939 | goto onError; |
| 8940 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8941 | /* generate replacement */ |
| 8942 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 8943 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
| 8944 | Py_UNICODE ch = *uni2; |
| 8945 | if (Py_UNICODE_ISSPACE(ch)) |
| 8946 | *output++ = ' '; |
| 8947 | else { |
| 8948 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 8949 | if (decimal >= 0) |
| 8950 | *output++ = '0' + decimal; |
| 8951 | else if (0 < ch && ch < 256) |
| 8952 | *output++ = (char)ch; |
| 8953 | else { |
| 8954 | Py_DECREF(repunicode); |
| 8955 | raise_encode_exception(&exc, encoding, |
| 8956 | s, length, collstart-s, collend-s, reason); |
| 8957 | goto onError; |
| 8958 | } |
| 8959 | } |
| 8960 | } |
| 8961 | p = s + newpos; |
| 8962 | Py_DECREF(repunicode); |
| 8963 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8964 | } |
| 8965 | /* 0-terminate the output string */ |
| 8966 | *output++ = '\0'; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8967 | Py_XDECREF(exc); |
| 8968 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8969 | return 0; |
| 8970 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8971 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8972 | Py_XDECREF(exc); |
| 8973 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8974 | return -1; |
| 8975 | } |
| 8976 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8977 | /* --- Helpers ------------------------------------------------------------ */ |
| 8978 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8979 | static Py_ssize_t |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 8980 | any_find_slice(int direction, PyObject* s1, PyObject* s2, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8981 | Py_ssize_t start, |
| 8982 | Py_ssize_t end) |
| 8983 | { |
| 8984 | int kind1, kind2, kind; |
| 8985 | void *buf1, *buf2; |
| 8986 | Py_ssize_t len1, len2, result; |
| 8987 | |
| 8988 | kind1 = PyUnicode_KIND(s1); |
| 8989 | kind2 = PyUnicode_KIND(s2); |
| 8990 | kind = kind1 > kind2 ? kind1 : kind2; |
| 8991 | buf1 = PyUnicode_DATA(s1); |
| 8992 | buf2 = PyUnicode_DATA(s2); |
| 8993 | if (kind1 != kind) |
| 8994 | buf1 = _PyUnicode_AsKind(s1, kind); |
| 8995 | if (!buf1) |
| 8996 | return -2; |
| 8997 | if (kind2 != kind) |
| 8998 | buf2 = _PyUnicode_AsKind(s2, kind); |
| 8999 | if (!buf2) { |
| 9000 | if (kind1 != kind) PyMem_Free(buf1); |
| 9001 | return -2; |
| 9002 | } |
| 9003 | len1 = PyUnicode_GET_LENGTH(s1); |
| 9004 | len2 = PyUnicode_GET_LENGTH(s2); |
| 9005 | |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 9006 | if (direction > 0) { |
| 9007 | switch(kind) { |
| 9008 | case PyUnicode_1BYTE_KIND: |
| 9009 | if (PyUnicode_IS_ASCII(s1) && PyUnicode_IS_ASCII(s2)) |
| 9010 | result = asciilib_find_slice(buf1, len1, buf2, len2, start, end); |
| 9011 | else |
| 9012 | result = ucs1lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 9013 | break; |
| 9014 | case PyUnicode_2BYTE_KIND: |
| 9015 | result = ucs2lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 9016 | break; |
| 9017 | case PyUnicode_4BYTE_KIND: |
| 9018 | result = ucs4lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 9019 | break; |
| 9020 | default: |
| 9021 | assert(0); result = -2; |
| 9022 | } |
| 9023 | } |
| 9024 | else { |
| 9025 | switch(kind) { |
| 9026 | case PyUnicode_1BYTE_KIND: |
| 9027 | if (PyUnicode_IS_ASCII(s1) && PyUnicode_IS_ASCII(s2)) |
| 9028 | result = asciilib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 9029 | else |
| 9030 | result = ucs1lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 9031 | break; |
| 9032 | case PyUnicode_2BYTE_KIND: |
| 9033 | result = ucs2lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 9034 | break; |
| 9035 | case PyUnicode_4BYTE_KIND: |
| 9036 | result = ucs4lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 9037 | break; |
| 9038 | default: |
| 9039 | assert(0); result = -2; |
| 9040 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9041 | } |
| 9042 | |
| 9043 | if (kind1 != kind) |
| 9044 | PyMem_Free(buf1); |
| 9045 | if (kind2 != kind) |
| 9046 | PyMem_Free(buf2); |
| 9047 | |
| 9048 | return result; |
| 9049 | } |
| 9050 | |
| 9051 | Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9052 | _PyUnicode_InsertThousandsGrouping(PyObject *unicode, int kind, void *data, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9053 | Py_ssize_t n_buffer, |
| 9054 | void *digits, Py_ssize_t n_digits, |
| 9055 | Py_ssize_t min_width, |
| 9056 | const char *grouping, |
| 9057 | const char *thousands_sep) |
| 9058 | { |
| 9059 | switch(kind) { |
| 9060 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9061 | if (unicode != NULL && PyUnicode_IS_ASCII(unicode)) |
| 9062 | return _PyUnicode_ascii_InsertThousandsGrouping( |
| 9063 | (Py_UCS1*)data, n_buffer, (Py_UCS1*)digits, n_digits, |
| 9064 | min_width, grouping, thousands_sep); |
| 9065 | else |
| 9066 | return _PyUnicode_ucs1_InsertThousandsGrouping( |
| 9067 | (Py_UCS1*)data, n_buffer, (Py_UCS1*)digits, n_digits, |
| 9068 | min_width, grouping, thousands_sep); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9069 | case PyUnicode_2BYTE_KIND: |
| 9070 | return _PyUnicode_ucs2_InsertThousandsGrouping( |
| 9071 | (Py_UCS2*)data, n_buffer, (Py_UCS2*)digits, n_digits, |
| 9072 | min_width, grouping, thousands_sep); |
| 9073 | case PyUnicode_4BYTE_KIND: |
| 9074 | return _PyUnicode_ucs4_InsertThousandsGrouping( |
| 9075 | (Py_UCS4*)data, n_buffer, (Py_UCS4*)digits, n_digits, |
| 9076 | min_width, grouping, thousands_sep); |
| 9077 | } |
| 9078 | assert(0); |
| 9079 | return -1; |
| 9080 | } |
| 9081 | |
| 9082 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9083 | /* helper macro to fixup start/end slice values */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9084 | #define ADJUST_INDICES(start, end, len) \ |
| 9085 | if (end > len) \ |
| 9086 | end = len; \ |
| 9087 | else if (end < 0) { \ |
| 9088 | end += len; \ |
| 9089 | if (end < 0) \ |
| 9090 | end = 0; \ |
| 9091 | } \ |
| 9092 | if (start < 0) { \ |
| 9093 | start += len; \ |
| 9094 | if (start < 0) \ |
| 9095 | start = 0; \ |
| 9096 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9097 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9098 | Py_ssize_t |
| 9099 | PyUnicode_Count(PyObject *str, |
| 9100 | PyObject *substr, |
| 9101 | Py_ssize_t start, |
| 9102 | Py_ssize_t end) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9103 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9104 | Py_ssize_t result; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9105 | PyObject* str_obj; |
| 9106 | PyObject* sub_obj; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9107 | int kind1, kind2, kind; |
| 9108 | void *buf1 = NULL, *buf2 = NULL; |
| 9109 | Py_ssize_t len1, len2; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9110 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9111 | str_obj = PyUnicode_FromObject(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9112 | if (!str_obj || PyUnicode_READY(str_obj) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9113 | return -1; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9114 | sub_obj = PyUnicode_FromObject(substr); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 9115 | if (!sub_obj || PyUnicode_READY(sub_obj) == -1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9116 | Py_DECREF(str_obj); |
| 9117 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9118 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9119 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9120 | kind1 = PyUnicode_KIND(str_obj); |
| 9121 | kind2 = PyUnicode_KIND(sub_obj); |
| 9122 | kind = kind1 > kind2 ? kind1 : kind2; |
| 9123 | buf1 = PyUnicode_DATA(str_obj); |
| 9124 | if (kind1 != kind) |
| 9125 | buf1 = _PyUnicode_AsKind((PyObject*)str_obj, kind); |
| 9126 | if (!buf1) |
| 9127 | goto onError; |
| 9128 | buf2 = PyUnicode_DATA(sub_obj); |
| 9129 | if (kind2 != kind) |
| 9130 | buf2 = _PyUnicode_AsKind((PyObject*)sub_obj, kind); |
| 9131 | if (!buf2) |
| 9132 | goto onError; |
| 9133 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 9134 | len2 = PyUnicode_GET_LENGTH(sub_obj); |
| 9135 | |
| 9136 | ADJUST_INDICES(start, end, len1); |
| 9137 | switch(kind) { |
| 9138 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9139 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sub_obj)) |
| 9140 | result = asciilib_count( |
| 9141 | ((Py_UCS1*)buf1) + start, end - start, |
| 9142 | buf2, len2, PY_SSIZE_T_MAX |
| 9143 | ); |
| 9144 | else |
| 9145 | result = ucs1lib_count( |
| 9146 | ((Py_UCS1*)buf1) + start, end - start, |
| 9147 | buf2, len2, PY_SSIZE_T_MAX |
| 9148 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9149 | break; |
| 9150 | case PyUnicode_2BYTE_KIND: |
| 9151 | result = ucs2lib_count( |
| 9152 | ((Py_UCS2*)buf1) + start, end - start, |
| 9153 | buf2, len2, PY_SSIZE_T_MAX |
| 9154 | ); |
| 9155 | break; |
| 9156 | case PyUnicode_4BYTE_KIND: |
| 9157 | result = ucs4lib_count( |
| 9158 | ((Py_UCS4*)buf1) + start, end - start, |
| 9159 | buf2, len2, PY_SSIZE_T_MAX |
| 9160 | ); |
| 9161 | break; |
| 9162 | default: |
| 9163 | assert(0); result = 0; |
| 9164 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9165 | |
| 9166 | Py_DECREF(sub_obj); |
| 9167 | Py_DECREF(str_obj); |
| 9168 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9169 | if (kind1 != kind) |
| 9170 | PyMem_Free(buf1); |
| 9171 | if (kind2 != kind) |
| 9172 | PyMem_Free(buf2); |
| 9173 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9174 | return result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9175 | onError: |
| 9176 | Py_DECREF(sub_obj); |
| 9177 | Py_DECREF(str_obj); |
| 9178 | if (kind1 != kind && buf1) |
| 9179 | PyMem_Free(buf1); |
| 9180 | if (kind2 != kind && buf2) |
| 9181 | PyMem_Free(buf2); |
| 9182 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9183 | } |
| 9184 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9185 | Py_ssize_t |
| 9186 | PyUnicode_Find(PyObject *str, |
| 9187 | PyObject *sub, |
| 9188 | Py_ssize_t start, |
| 9189 | Py_ssize_t end, |
| 9190 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9191 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9192 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9193 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9194 | str = PyUnicode_FromObject(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9195 | if (!str || PyUnicode_READY(str) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9196 | return -2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9197 | sub = PyUnicode_FromObject(sub); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9198 | if (!sub || PyUnicode_READY(sub) == -1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9199 | Py_DECREF(str); |
| 9200 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9201 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9202 | |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 9203 | result = any_find_slice(direction, |
| 9204 | str, sub, start, end |
| 9205 | ); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9206 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9207 | Py_DECREF(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9208 | Py_DECREF(sub); |
| 9209 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9210 | return result; |
| 9211 | } |
| 9212 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9213 | Py_ssize_t |
| 9214 | PyUnicode_FindChar(PyObject *str, Py_UCS4 ch, |
| 9215 | Py_ssize_t start, Py_ssize_t end, |
| 9216 | int direction) |
| 9217 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9218 | int kind; |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 9219 | Py_ssize_t result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9220 | if (PyUnicode_READY(str) == -1) |
| 9221 | return -2; |
Victor Stinner | 267aa24 | 2011-10-02 01:08:37 +0200 | [diff] [blame] | 9222 | if (start < 0 || end < 0) { |
| 9223 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 9224 | return -2; |
| 9225 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9226 | if (end > PyUnicode_GET_LENGTH(str)) |
| 9227 | end = PyUnicode_GET_LENGTH(str); |
| 9228 | kind = PyUnicode_KIND(str); |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 9229 | result = findchar(PyUnicode_1BYTE_DATA(str) + kind*start, |
| 9230 | kind, end-start, ch, direction); |
| 9231 | if (result == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9232 | return -1; |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 9233 | else |
| 9234 | return start + result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9235 | } |
| 9236 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9237 | static int |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9238 | tailmatch(PyObject *self, |
| 9239 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9240 | Py_ssize_t start, |
| 9241 | Py_ssize_t end, |
| 9242 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9243 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9244 | int kind_self; |
| 9245 | int kind_sub; |
| 9246 | void *data_self; |
| 9247 | void *data_sub; |
| 9248 | Py_ssize_t offset; |
| 9249 | Py_ssize_t i; |
| 9250 | Py_ssize_t end_sub; |
| 9251 | |
| 9252 | if (PyUnicode_READY(self) == -1 || |
| 9253 | PyUnicode_READY(substring) == -1) |
| 9254 | return 0; |
| 9255 | |
| 9256 | if (PyUnicode_GET_LENGTH(substring) == 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9257 | return 1; |
| 9258 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9259 | ADJUST_INDICES(start, end, PyUnicode_GET_LENGTH(self)); |
| 9260 | end -= PyUnicode_GET_LENGTH(substring); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9261 | if (end < start) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9262 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9263 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9264 | kind_self = PyUnicode_KIND(self); |
| 9265 | data_self = PyUnicode_DATA(self); |
| 9266 | kind_sub = PyUnicode_KIND(substring); |
| 9267 | data_sub = PyUnicode_DATA(substring); |
| 9268 | end_sub = PyUnicode_GET_LENGTH(substring) - 1; |
| 9269 | |
| 9270 | if (direction > 0) |
| 9271 | offset = end; |
| 9272 | else |
| 9273 | offset = start; |
| 9274 | |
| 9275 | if (PyUnicode_READ(kind_self, data_self, offset) == |
| 9276 | PyUnicode_READ(kind_sub, data_sub, 0) && |
| 9277 | PyUnicode_READ(kind_self, data_self, offset + end_sub) == |
| 9278 | PyUnicode_READ(kind_sub, data_sub, end_sub)) { |
| 9279 | /* If both are of the same kind, memcmp is sufficient */ |
| 9280 | if (kind_self == kind_sub) { |
| 9281 | return ! memcmp((char *)data_self + |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9282 | (offset * PyUnicode_KIND(substring)), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9283 | data_sub, |
| 9284 | PyUnicode_GET_LENGTH(substring) * |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9285 | PyUnicode_KIND(substring)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9286 | } |
| 9287 | /* otherwise we have to compare each character by first accesing it */ |
| 9288 | else { |
| 9289 | /* We do not need to compare 0 and len(substring)-1 because |
| 9290 | the if statement above ensured already that they are equal |
| 9291 | when we end up here. */ |
| 9292 | // TODO: honor direction and do a forward or backwards search |
| 9293 | for (i = 1; i < end_sub; ++i) { |
| 9294 | if (PyUnicode_READ(kind_self, data_self, offset + i) != |
| 9295 | PyUnicode_READ(kind_sub, data_sub, i)) |
| 9296 | return 0; |
| 9297 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9298 | return 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9299 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9300 | } |
| 9301 | |
| 9302 | return 0; |
| 9303 | } |
| 9304 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9305 | Py_ssize_t |
| 9306 | PyUnicode_Tailmatch(PyObject *str, |
| 9307 | PyObject *substr, |
| 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 | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9312 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9313 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9314 | str = PyUnicode_FromObject(str); |
| 9315 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9316 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9317 | substr = PyUnicode_FromObject(substr); |
| 9318 | if (substr == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9319 | Py_DECREF(str); |
| 9320 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9321 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9322 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9323 | result = tailmatch(str, substr, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9324 | start, end, direction); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9325 | Py_DECREF(str); |
| 9326 | Py_DECREF(substr); |
| 9327 | return result; |
| 9328 | } |
| 9329 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9330 | /* Apply fixfct filter to the Unicode object self and return a |
| 9331 | reference to the modified object */ |
| 9332 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9333 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9334 | fixup(PyObject *self, |
| 9335 | Py_UCS4 (*fixfct)(PyObject *s)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9336 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9337 | PyObject *u; |
| 9338 | Py_UCS4 maxchar_old, maxchar_new = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9339 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9340 | if (PyUnicode_READY(self) == -1) |
| 9341 | return NULL; |
| 9342 | maxchar_old = PyUnicode_MAX_CHAR_VALUE(self); |
| 9343 | u = PyUnicode_New(PyUnicode_GET_LENGTH(self), |
| 9344 | maxchar_old); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9345 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9346 | return NULL; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9347 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9348 | Py_MEMCPY(PyUnicode_1BYTE_DATA(u), PyUnicode_1BYTE_DATA(self), |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9349 | PyUnicode_GET_LENGTH(u) * PyUnicode_KIND(u)); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9350 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9351 | /* fix functions return the new maximum character in a string, |
| 9352 | if the kind of the resulting unicode object does not change, |
| 9353 | everything is fine. Otherwise we need to change the string kind |
| 9354 | and re-run the fix function. */ |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9355 | maxchar_new = fixfct(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9356 | if (maxchar_new == 0) |
| 9357 | /* do nothing, keep maxchar_new at 0 which means no changes. */; |
| 9358 | else if (maxchar_new <= 127) |
| 9359 | maxchar_new = 127; |
| 9360 | else if (maxchar_new <= 255) |
| 9361 | maxchar_new = 255; |
| 9362 | else if (maxchar_new <= 65535) |
| 9363 | maxchar_new = 65535; |
| 9364 | else |
| 9365 | maxchar_new = 1114111; /* 0x10ffff */ |
| 9366 | |
| 9367 | if (!maxchar_new && PyUnicode_CheckExact(self)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9368 | /* fixfct should return TRUE if it modified the buffer. If |
| 9369 | FALSE, return a reference to the original buffer instead |
| 9370 | (to save space, not time) */ |
| 9371 | Py_INCREF(self); |
| 9372 | Py_DECREF(u); |
| 9373 | return (PyObject*) self; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9374 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9375 | else if (maxchar_new == maxchar_old) { |
| 9376 | return u; |
| 9377 | } |
| 9378 | else { |
| 9379 | /* In case the maximum character changed, we need to |
| 9380 | convert the string to the new category. */ |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9381 | PyObject *v = PyUnicode_New(PyUnicode_GET_LENGTH(self), maxchar_new); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9382 | if (v == NULL) { |
| 9383 | Py_DECREF(u); |
| 9384 | return NULL; |
| 9385 | } |
| 9386 | if (maxchar_new > maxchar_old) { |
| 9387 | /* If the maxchar increased so that the kind changed, not all |
| 9388 | characters are representable anymore and we need to fix the |
| 9389 | string again. This only happens in very few cases. */ |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9390 | copy_characters(v, 0, self, 0, PyUnicode_GET_LENGTH(self)); |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9391 | maxchar_old = fixfct(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9392 | assert(maxchar_old > 0 && maxchar_old <= maxchar_new); |
| 9393 | } |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9394 | else { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9395 | copy_characters(v, 0, u, 0, PyUnicode_GET_LENGTH(self)); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9396 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9397 | |
| 9398 | Py_DECREF(u); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 9399 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9400 | return v; |
| 9401 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9402 | } |
| 9403 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9404 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9405 | fixupper(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9406 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9407 | /* No need to call PyUnicode_READY(self) because this function is only |
| 9408 | called as a callback from fixup() which does it already. */ |
| 9409 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 9410 | const int kind = PyUnicode_KIND(self); |
| 9411 | void *data = PyUnicode_DATA(self); |
| 9412 | int touched = 0; |
| 9413 | Py_UCS4 maxchar = 0; |
| 9414 | Py_ssize_t i; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9415 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9416 | for (i = 0; i < len; ++i) { |
| 9417 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 9418 | const Py_UCS4 up = Py_UNICODE_TOUPPER(ch); |
| 9419 | if (up != ch) { |
| 9420 | if (up > maxchar) |
| 9421 | maxchar = up; |
| 9422 | PyUnicode_WRITE(kind, data, i, up); |
| 9423 | touched = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9424 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9425 | else if (ch > maxchar) |
| 9426 | maxchar = ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9427 | } |
| 9428 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9429 | if (touched) |
| 9430 | return maxchar; |
| 9431 | else |
| 9432 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9433 | } |
| 9434 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9435 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9436 | fixlower(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9437 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9438 | /* No need to call PyUnicode_READY(self) because fixup() which does it. */ |
| 9439 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 9440 | const int kind = PyUnicode_KIND(self); |
| 9441 | void *data = PyUnicode_DATA(self); |
| 9442 | int touched = 0; |
| 9443 | Py_UCS4 maxchar = 0; |
| 9444 | Py_ssize_t i; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9445 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9446 | for(i = 0; i < len; ++i) { |
| 9447 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 9448 | const Py_UCS4 lo = Py_UNICODE_TOLOWER(ch); |
| 9449 | if (lo != ch) { |
| 9450 | if (lo > maxchar) |
| 9451 | maxchar = lo; |
| 9452 | PyUnicode_WRITE(kind, data, i, lo); |
| 9453 | touched = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9454 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9455 | else if (ch > maxchar) |
| 9456 | maxchar = ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9457 | } |
| 9458 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9459 | if (touched) |
| 9460 | return maxchar; |
| 9461 | else |
| 9462 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9463 | } |
| 9464 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9465 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9466 | fixswapcase(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9467 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9468 | /* No need to call PyUnicode_READY(self) because fixup() which does it. */ |
| 9469 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 9470 | const int kind = PyUnicode_KIND(self); |
| 9471 | void *data = PyUnicode_DATA(self); |
| 9472 | int touched = 0; |
| 9473 | Py_UCS4 maxchar = 0; |
| 9474 | Py_ssize_t i; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9475 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9476 | for(i = 0; i < len; ++i) { |
| 9477 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 9478 | Py_UCS4 nu = 0; |
| 9479 | |
| 9480 | if (Py_UNICODE_ISUPPER(ch)) |
| 9481 | nu = Py_UNICODE_TOLOWER(ch); |
| 9482 | else if (Py_UNICODE_ISLOWER(ch)) |
| 9483 | nu = Py_UNICODE_TOUPPER(ch); |
| 9484 | |
| 9485 | if (nu != 0) { |
| 9486 | if (nu > maxchar) |
| 9487 | maxchar = nu; |
| 9488 | PyUnicode_WRITE(kind, data, i, nu); |
| 9489 | touched = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9490 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9491 | else if (ch > maxchar) |
| 9492 | maxchar = ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9493 | } |
| 9494 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9495 | if (touched) |
| 9496 | return maxchar; |
| 9497 | else |
| 9498 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9499 | } |
| 9500 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9501 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9502 | fixcapitalize(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9503 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9504 | /* No need to call PyUnicode_READY(self) because fixup() which does it. */ |
| 9505 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 9506 | const int kind = PyUnicode_KIND(self); |
| 9507 | void *data = PyUnicode_DATA(self); |
| 9508 | int touched = 0; |
| 9509 | Py_UCS4 maxchar = 0; |
| 9510 | Py_ssize_t i = 0; |
| 9511 | Py_UCS4 ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9512 | |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9513 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9514 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9515 | |
| 9516 | ch = PyUnicode_READ(kind, data, i); |
| 9517 | if (!Py_UNICODE_ISUPPER(ch)) { |
| 9518 | maxchar = Py_UNICODE_TOUPPER(ch); |
| 9519 | PyUnicode_WRITE(kind, data, i, maxchar); |
| 9520 | touched = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9521 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9522 | ++i; |
| 9523 | for(; i < len; ++i) { |
| 9524 | ch = PyUnicode_READ(kind, data, i); |
| 9525 | if (!Py_UNICODE_ISLOWER(ch)) { |
| 9526 | const Py_UCS4 lo = Py_UNICODE_TOLOWER(ch); |
| 9527 | if (lo > maxchar) |
| 9528 | maxchar = lo; |
| 9529 | PyUnicode_WRITE(kind, data, i, lo); |
| 9530 | touched = 1; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9531 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9532 | else if (ch > maxchar) |
| 9533 | maxchar = ch; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9534 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9535 | |
| 9536 | if (touched) |
| 9537 | return maxchar; |
| 9538 | else |
| 9539 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9540 | } |
| 9541 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9542 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9543 | fixtitle(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9544 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9545 | /* No need to call PyUnicode_READY(self) because fixup() which does it. */ |
| 9546 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 9547 | const int kind = PyUnicode_KIND(self); |
| 9548 | void *data = PyUnicode_DATA(self); |
| 9549 | Py_UCS4 maxchar = 0; |
| 9550 | Py_ssize_t i = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9551 | int previous_is_cased; |
| 9552 | |
| 9553 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9554 | if (len == 1) { |
| 9555 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 9556 | const Py_UCS4 ti = Py_UNICODE_TOTITLE(ch); |
| 9557 | if (ti != ch) { |
| 9558 | PyUnicode_WRITE(kind, data, i, ti); |
| 9559 | return ti; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9560 | } |
| 9561 | else |
| 9562 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9563 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9564 | previous_is_cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9565 | for(; i < len; ++i) { |
| 9566 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 9567 | Py_UCS4 nu; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9568 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9569 | if (previous_is_cased) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9570 | nu = Py_UNICODE_TOLOWER(ch); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9571 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9572 | nu = Py_UNICODE_TOTITLE(ch); |
| 9573 | |
| 9574 | if (nu > maxchar) |
| 9575 | maxchar = nu; |
| 9576 | PyUnicode_WRITE(kind, data, i, nu); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9577 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9578 | if (Py_UNICODE_ISLOWER(ch) || |
| 9579 | Py_UNICODE_ISUPPER(ch) || |
| 9580 | Py_UNICODE_ISTITLE(ch)) |
| 9581 | previous_is_cased = 1; |
| 9582 | else |
| 9583 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9584 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9585 | return maxchar; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9586 | } |
| 9587 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9588 | PyObject * |
| 9589 | PyUnicode_Join(PyObject *separator, PyObject *seq) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9590 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9591 | PyObject *sep = NULL; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9592 | Py_ssize_t seplen; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9593 | PyObject *res = NULL; /* the result */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9594 | PyObject *fseq; /* PySequence_Fast(seq) */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9595 | Py_ssize_t seqlen; /* len(fseq) -- number of items in sequence */ |
| 9596 | PyObject **items; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9597 | PyObject *item; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9598 | Py_ssize_t sz, i, res_offset; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9599 | Py_UCS4 maxchar; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9600 | Py_UCS4 item_maxchar; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9601 | int use_memcpy; |
| 9602 | unsigned char *res_data = NULL, *sep_data = NULL; |
| 9603 | PyObject *last_obj; |
| 9604 | unsigned int kind = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9605 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9606 | fseq = PySequence_Fast(seq, ""); |
| 9607 | if (fseq == NULL) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9608 | return NULL; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9609 | } |
| 9610 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9611 | /* NOTE: the following code can't call back into Python code, |
| 9612 | * so we are sure that fseq won't be mutated. |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 9613 | */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9614 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9615 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 9616 | /* If empty sequence, return u"". */ |
| 9617 | if (seqlen == 0) { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9618 | Py_DECREF(fseq); |
| 9619 | Py_INCREF(unicode_empty); |
| 9620 | res = unicode_empty; |
| 9621 | return res; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9622 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9623 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9624 | /* If singleton sequence with an exact Unicode, return that. */ |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9625 | last_obj = NULL; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9626 | items = PySequence_Fast_ITEMS(fseq); |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9627 | if (seqlen == 1) { |
| 9628 | if (PyUnicode_CheckExact(items[0])) { |
| 9629 | res = items[0]; |
| 9630 | Py_INCREF(res); |
| 9631 | Py_DECREF(fseq); |
| 9632 | return res; |
| 9633 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9634 | seplen = 0; |
Victor Stinner | c6f0df7 | 2011-10-06 15:58:54 +0200 | [diff] [blame] | 9635 | maxchar = 0; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9636 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9637 | else { |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9638 | /* Set up sep and seplen */ |
| 9639 | if (separator == NULL) { |
| 9640 | /* fall back to a blank space separator */ |
| 9641 | sep = PyUnicode_FromOrdinal(' '); |
| 9642 | if (!sep) |
| 9643 | goto onError; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9644 | seplen = 1; |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9645 | maxchar = 32; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9646 | } |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9647 | else { |
| 9648 | if (!PyUnicode_Check(separator)) { |
| 9649 | PyErr_Format(PyExc_TypeError, |
| 9650 | "separator: expected str instance," |
| 9651 | " %.80s found", |
| 9652 | Py_TYPE(separator)->tp_name); |
| 9653 | goto onError; |
| 9654 | } |
| 9655 | if (PyUnicode_READY(separator)) |
| 9656 | goto onError; |
| 9657 | sep = separator; |
| 9658 | seplen = PyUnicode_GET_LENGTH(separator); |
| 9659 | maxchar = PyUnicode_MAX_CHAR_VALUE(separator); |
| 9660 | /* inc refcount to keep this code path symmetric with the |
| 9661 | above case of a blank separator */ |
| 9662 | Py_INCREF(sep); |
| 9663 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9664 | last_obj = sep; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9665 | } |
| 9666 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9667 | /* There are at least two things to join, or else we have a subclass |
| 9668 | * of str in the sequence. |
| 9669 | * Do a pre-pass to figure out the total amount of space we'll |
| 9670 | * need (sz), and see whether all argument are strings. |
| 9671 | */ |
| 9672 | sz = 0; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9673 | #ifdef Py_DEBUG |
| 9674 | use_memcpy = 0; |
| 9675 | #else |
| 9676 | use_memcpy = 1; |
| 9677 | #endif |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9678 | for (i = 0; i < seqlen; i++) { |
| 9679 | const Py_ssize_t old_sz = sz; |
| 9680 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9681 | if (!PyUnicode_Check(item)) { |
| 9682 | PyErr_Format(PyExc_TypeError, |
| 9683 | "sequence item %zd: expected str instance," |
| 9684 | " %.80s found", |
| 9685 | i, Py_TYPE(item)->tp_name); |
| 9686 | goto onError; |
| 9687 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9688 | if (PyUnicode_READY(item) == -1) |
| 9689 | goto onError; |
| 9690 | sz += PyUnicode_GET_LENGTH(item); |
| 9691 | item_maxchar = PyUnicode_MAX_CHAR_VALUE(item); |
Victor Stinner | c6f0df7 | 2011-10-06 15:58:54 +0200 | [diff] [blame] | 9692 | maxchar = Py_MAX(maxchar, item_maxchar); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9693 | if (i != 0) |
| 9694 | sz += seplen; |
| 9695 | if (sz < old_sz || sz > PY_SSIZE_T_MAX) { |
| 9696 | PyErr_SetString(PyExc_OverflowError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9697 | "join() result is too long for a Python string"); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9698 | goto onError; |
| 9699 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9700 | if (use_memcpy && last_obj != NULL) { |
| 9701 | if (PyUnicode_KIND(last_obj) != PyUnicode_KIND(item)) |
| 9702 | use_memcpy = 0; |
| 9703 | } |
| 9704 | last_obj = item; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9705 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9706 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9707 | res = PyUnicode_New(sz, maxchar); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9708 | if (res == NULL) |
| 9709 | goto onError; |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 9710 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9711 | /* Catenate everything. */ |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9712 | #ifdef Py_DEBUG |
| 9713 | use_memcpy = 0; |
| 9714 | #else |
| 9715 | if (use_memcpy) { |
| 9716 | res_data = PyUnicode_1BYTE_DATA(res); |
| 9717 | kind = PyUnicode_KIND(res); |
| 9718 | if (seplen != 0) |
| 9719 | sep_data = PyUnicode_1BYTE_DATA(sep); |
| 9720 | } |
| 9721 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9722 | for (i = 0, res_offset = 0; i < seqlen; ++i) { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9723 | Py_ssize_t itemlen; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9724 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9725 | /* Copy item, and maybe the separator. */ |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 9726 | if (i && seplen != 0) { |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9727 | if (use_memcpy) { |
| 9728 | Py_MEMCPY(res_data, |
| 9729 | sep_data, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9730 | kind * seplen); |
| 9731 | res_data += kind * seplen; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9732 | } |
| 9733 | else { |
| 9734 | copy_characters(res, res_offset, sep, 0, seplen); |
| 9735 | res_offset += seplen; |
| 9736 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9737 | } |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 9738 | itemlen = PyUnicode_GET_LENGTH(item); |
| 9739 | if (itemlen != 0) { |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9740 | if (use_memcpy) { |
| 9741 | Py_MEMCPY(res_data, |
| 9742 | PyUnicode_DATA(item), |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9743 | kind * itemlen); |
| 9744 | res_data += kind * itemlen; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9745 | } |
| 9746 | else { |
| 9747 | copy_characters(res, res_offset, item, 0, itemlen); |
| 9748 | res_offset += itemlen; |
| 9749 | } |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 9750 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9751 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9752 | if (use_memcpy) |
| 9753 | assert(res_data == PyUnicode_1BYTE_DATA(res) |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9754 | + kind * PyUnicode_GET_LENGTH(res)); |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9755 | else |
| 9756 | assert(res_offset == PyUnicode_GET_LENGTH(res)); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9757 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9758 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9759 | Py_XDECREF(sep); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 9760 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9761 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9762 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9763 | onError: |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9764 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9765 | Py_XDECREF(sep); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9766 | Py_XDECREF(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9767 | return NULL; |
| 9768 | } |
| 9769 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9770 | #define FILL(kind, data, value, start, length) \ |
| 9771 | do { \ |
| 9772 | Py_ssize_t i_ = 0; \ |
| 9773 | assert(kind != PyUnicode_WCHAR_KIND); \ |
| 9774 | switch ((kind)) { \ |
| 9775 | case PyUnicode_1BYTE_KIND: { \ |
| 9776 | unsigned char * to_ = (unsigned char *)((data)) + (start); \ |
| 9777 | memset(to_, (unsigned char)value, length); \ |
| 9778 | break; \ |
| 9779 | } \ |
| 9780 | case PyUnicode_2BYTE_KIND: { \ |
| 9781 | Py_UCS2 * to_ = (Py_UCS2 *)((data)) + (start); \ |
| 9782 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 9783 | break; \ |
| 9784 | } \ |
| 9785 | default: { \ |
| 9786 | Py_UCS4 * to_ = (Py_UCS4 *)((data)) + (start); \ |
| 9787 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 9788 | break; \ |
| 9789 | } \ |
| 9790 | } \ |
| 9791 | } while (0) |
| 9792 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9793 | static PyObject * |
| 9794 | pad(PyObject *self, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9795 | Py_ssize_t left, |
| 9796 | Py_ssize_t right, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9797 | Py_UCS4 fill) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9798 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9799 | PyObject *u; |
| 9800 | Py_UCS4 maxchar; |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9801 | int kind; |
| 9802 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9803 | |
| 9804 | if (left < 0) |
| 9805 | left = 0; |
| 9806 | if (right < 0) |
| 9807 | right = 0; |
| 9808 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 9809 | if (left == 0 && right == 0 && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9810 | Py_INCREF(self); |
| 9811 | return self; |
| 9812 | } |
| 9813 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9814 | if (left > PY_SSIZE_T_MAX - _PyUnicode_LENGTH(self) || |
| 9815 | right > PY_SSIZE_T_MAX - (left + _PyUnicode_LENGTH(self))) { |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 9816 | PyErr_SetString(PyExc_OverflowError, "padded string is too long"); |
| 9817 | return NULL; |
| 9818 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9819 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
| 9820 | if (fill > maxchar) |
| 9821 | maxchar = fill; |
| 9822 | u = PyUnicode_New(left + _PyUnicode_LENGTH(self) + right, maxchar); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9823 | if (!u) |
| 9824 | return NULL; |
| 9825 | |
| 9826 | kind = PyUnicode_KIND(u); |
| 9827 | data = PyUnicode_DATA(u); |
| 9828 | if (left) |
| 9829 | FILL(kind, data, fill, 0, left); |
| 9830 | if (right) |
| 9831 | FILL(kind, data, fill, left + _PyUnicode_LENGTH(self), right); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9832 | copy_characters(u, left, self, 0, _PyUnicode_LENGTH(self)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 9833 | assert(_PyUnicode_CheckConsistency(u, 1)); |
| 9834 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9835 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9836 | #undef FILL |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9837 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9838 | PyObject * |
| 9839 | PyUnicode_Splitlines(PyObject *string, int keepends) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9840 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9841 | PyObject *list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9842 | |
| 9843 | string = PyUnicode_FromObject(string); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9844 | if (string == NULL || PyUnicode_READY(string) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9845 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9846 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9847 | switch(PyUnicode_KIND(string)) { |
| 9848 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9849 | if (PyUnicode_IS_ASCII(string)) |
| 9850 | list = asciilib_splitlines( |
| 9851 | (PyObject*) string, PyUnicode_1BYTE_DATA(string), |
| 9852 | PyUnicode_GET_LENGTH(string), keepends); |
| 9853 | else |
| 9854 | list = ucs1lib_splitlines( |
| 9855 | (PyObject*) string, PyUnicode_1BYTE_DATA(string), |
| 9856 | PyUnicode_GET_LENGTH(string), keepends); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9857 | break; |
| 9858 | case PyUnicode_2BYTE_KIND: |
| 9859 | list = ucs2lib_splitlines( |
| 9860 | (PyObject*) string, PyUnicode_2BYTE_DATA(string), |
| 9861 | PyUnicode_GET_LENGTH(string), keepends); |
| 9862 | break; |
| 9863 | case PyUnicode_4BYTE_KIND: |
| 9864 | list = ucs4lib_splitlines( |
| 9865 | (PyObject*) string, PyUnicode_4BYTE_DATA(string), |
| 9866 | PyUnicode_GET_LENGTH(string), keepends); |
| 9867 | break; |
| 9868 | default: |
| 9869 | assert(0); |
| 9870 | list = 0; |
| 9871 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9872 | Py_DECREF(string); |
| 9873 | return list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9874 | } |
| 9875 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9876 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9877 | split(PyObject *self, |
| 9878 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9879 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9880 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9881 | int kind1, kind2, kind; |
| 9882 | void *buf1, *buf2; |
| 9883 | Py_ssize_t len1, len2; |
| 9884 | PyObject* out; |
| 9885 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9886 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9887 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9888 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9889 | if (PyUnicode_READY(self) == -1) |
| 9890 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9891 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9892 | if (substring == NULL) |
| 9893 | switch(PyUnicode_KIND(self)) { |
| 9894 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9895 | if (PyUnicode_IS_ASCII(self)) |
| 9896 | return asciilib_split_whitespace( |
| 9897 | (PyObject*) self, PyUnicode_1BYTE_DATA(self), |
| 9898 | PyUnicode_GET_LENGTH(self), maxcount |
| 9899 | ); |
| 9900 | else |
| 9901 | return ucs1lib_split_whitespace( |
| 9902 | (PyObject*) self, PyUnicode_1BYTE_DATA(self), |
| 9903 | PyUnicode_GET_LENGTH(self), maxcount |
| 9904 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9905 | case PyUnicode_2BYTE_KIND: |
| 9906 | return ucs2lib_split_whitespace( |
| 9907 | (PyObject*) self, PyUnicode_2BYTE_DATA(self), |
| 9908 | PyUnicode_GET_LENGTH(self), maxcount |
| 9909 | ); |
| 9910 | case PyUnicode_4BYTE_KIND: |
| 9911 | return ucs4lib_split_whitespace( |
| 9912 | (PyObject*) self, PyUnicode_4BYTE_DATA(self), |
| 9913 | PyUnicode_GET_LENGTH(self), maxcount |
| 9914 | ); |
| 9915 | default: |
| 9916 | assert(0); |
| 9917 | return NULL; |
| 9918 | } |
| 9919 | |
| 9920 | if (PyUnicode_READY(substring) == -1) |
| 9921 | return NULL; |
| 9922 | |
| 9923 | kind1 = PyUnicode_KIND(self); |
| 9924 | kind2 = PyUnicode_KIND(substring); |
| 9925 | kind = kind1 > kind2 ? kind1 : kind2; |
| 9926 | buf1 = PyUnicode_DATA(self); |
| 9927 | buf2 = PyUnicode_DATA(substring); |
| 9928 | if (kind1 != kind) |
| 9929 | buf1 = _PyUnicode_AsKind((PyObject*)self, kind); |
| 9930 | if (!buf1) |
| 9931 | return NULL; |
| 9932 | if (kind2 != kind) |
| 9933 | buf2 = _PyUnicode_AsKind((PyObject*)substring, kind); |
| 9934 | if (!buf2) { |
| 9935 | if (kind1 != kind) PyMem_Free(buf1); |
| 9936 | return NULL; |
| 9937 | } |
| 9938 | len1 = PyUnicode_GET_LENGTH(self); |
| 9939 | len2 = PyUnicode_GET_LENGTH(substring); |
| 9940 | |
| 9941 | switch(kind) { |
| 9942 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9943 | if (PyUnicode_IS_ASCII(self) && PyUnicode_IS_ASCII(substring)) |
| 9944 | out = asciilib_split( |
| 9945 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
| 9946 | else |
| 9947 | out = ucs1lib_split( |
| 9948 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9949 | break; |
| 9950 | case PyUnicode_2BYTE_KIND: |
| 9951 | out = ucs2lib_split( |
| 9952 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
| 9953 | break; |
| 9954 | case PyUnicode_4BYTE_KIND: |
| 9955 | out = ucs4lib_split( |
| 9956 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
| 9957 | break; |
| 9958 | default: |
| 9959 | out = NULL; |
| 9960 | } |
| 9961 | if (kind1 != kind) |
| 9962 | PyMem_Free(buf1); |
| 9963 | if (kind2 != kind) |
| 9964 | PyMem_Free(buf2); |
| 9965 | return out; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9966 | } |
| 9967 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9968 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9969 | rsplit(PyObject *self, |
| 9970 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9971 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9972 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9973 | int kind1, kind2, kind; |
| 9974 | void *buf1, *buf2; |
| 9975 | Py_ssize_t len1, len2; |
| 9976 | PyObject* out; |
| 9977 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9978 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9979 | maxcount = PY_SSIZE_T_MAX; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9980 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9981 | if (PyUnicode_READY(self) == -1) |
| 9982 | return NULL; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9983 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9984 | if (substring == NULL) |
| 9985 | switch(PyUnicode_KIND(self)) { |
| 9986 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9987 | if (PyUnicode_IS_ASCII(self)) |
| 9988 | return asciilib_rsplit_whitespace( |
| 9989 | (PyObject*) self, PyUnicode_1BYTE_DATA(self), |
| 9990 | PyUnicode_GET_LENGTH(self), maxcount |
| 9991 | ); |
| 9992 | else |
| 9993 | return ucs1lib_rsplit_whitespace( |
| 9994 | (PyObject*) self, PyUnicode_1BYTE_DATA(self), |
| 9995 | PyUnicode_GET_LENGTH(self), maxcount |
| 9996 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9997 | case PyUnicode_2BYTE_KIND: |
| 9998 | return ucs2lib_rsplit_whitespace( |
| 9999 | (PyObject*) self, PyUnicode_2BYTE_DATA(self), |
| 10000 | PyUnicode_GET_LENGTH(self), maxcount |
| 10001 | ); |
| 10002 | case PyUnicode_4BYTE_KIND: |
| 10003 | return ucs4lib_rsplit_whitespace( |
| 10004 | (PyObject*) self, PyUnicode_4BYTE_DATA(self), |
| 10005 | PyUnicode_GET_LENGTH(self), maxcount |
| 10006 | ); |
| 10007 | default: |
| 10008 | assert(0); |
| 10009 | return NULL; |
| 10010 | } |
| 10011 | |
| 10012 | if (PyUnicode_READY(substring) == -1) |
| 10013 | return NULL; |
| 10014 | |
| 10015 | kind1 = PyUnicode_KIND(self); |
| 10016 | kind2 = PyUnicode_KIND(substring); |
| 10017 | kind = kind1 > kind2 ? kind1 : kind2; |
| 10018 | buf1 = PyUnicode_DATA(self); |
| 10019 | buf2 = PyUnicode_DATA(substring); |
| 10020 | if (kind1 != kind) |
| 10021 | buf1 = _PyUnicode_AsKind((PyObject*)self, kind); |
| 10022 | if (!buf1) |
| 10023 | return NULL; |
| 10024 | if (kind2 != kind) |
| 10025 | buf2 = _PyUnicode_AsKind((PyObject*)substring, kind); |
| 10026 | if (!buf2) { |
| 10027 | if (kind1 != kind) PyMem_Free(buf1); |
| 10028 | return NULL; |
| 10029 | } |
| 10030 | len1 = PyUnicode_GET_LENGTH(self); |
| 10031 | len2 = PyUnicode_GET_LENGTH(substring); |
| 10032 | |
| 10033 | switch(kind) { |
| 10034 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10035 | if (PyUnicode_IS_ASCII(self) && PyUnicode_IS_ASCII(substring)) |
| 10036 | out = asciilib_rsplit( |
| 10037 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
| 10038 | else |
| 10039 | out = ucs1lib_rsplit( |
| 10040 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10041 | break; |
| 10042 | case PyUnicode_2BYTE_KIND: |
| 10043 | out = ucs2lib_rsplit( |
| 10044 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
| 10045 | break; |
| 10046 | case PyUnicode_4BYTE_KIND: |
| 10047 | out = ucs4lib_rsplit( |
| 10048 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
| 10049 | break; |
| 10050 | default: |
| 10051 | out = NULL; |
| 10052 | } |
| 10053 | if (kind1 != kind) |
| 10054 | PyMem_Free(buf1); |
| 10055 | if (kind2 != kind) |
| 10056 | PyMem_Free(buf2); |
| 10057 | return out; |
| 10058 | } |
| 10059 | |
| 10060 | static Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10061 | anylib_find(int kind, PyObject *str1, void *buf1, Py_ssize_t len1, |
| 10062 | 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] | 10063 | { |
| 10064 | switch(kind) { |
| 10065 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10066 | if (PyUnicode_IS_ASCII(str1) && PyUnicode_IS_ASCII(str2)) |
| 10067 | return asciilib_find(buf1, len1, buf2, len2, offset); |
| 10068 | else |
| 10069 | return ucs1lib_find(buf1, len1, buf2, len2, offset); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10070 | case PyUnicode_2BYTE_KIND: |
| 10071 | return ucs2lib_find(buf1, len1, buf2, len2, offset); |
| 10072 | case PyUnicode_4BYTE_KIND: |
| 10073 | return ucs4lib_find(buf1, len1, buf2, len2, offset); |
| 10074 | } |
| 10075 | assert(0); |
| 10076 | return -1; |
| 10077 | } |
| 10078 | |
| 10079 | static Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10080 | anylib_count(int kind, PyObject *sstr, void* sbuf, Py_ssize_t slen, |
| 10081 | 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] | 10082 | { |
| 10083 | switch(kind) { |
| 10084 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10085 | if (PyUnicode_IS_ASCII(sstr) && PyUnicode_IS_ASCII(str1)) |
| 10086 | return asciilib_count(sbuf, slen, buf1, len1, maxcount); |
| 10087 | else |
| 10088 | return ucs1lib_count(sbuf, slen, buf1, len1, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10089 | case PyUnicode_2BYTE_KIND: |
| 10090 | return ucs2lib_count(sbuf, slen, buf1, len1, maxcount); |
| 10091 | case PyUnicode_4BYTE_KIND: |
| 10092 | return ucs4lib_count(sbuf, slen, buf1, len1, maxcount); |
| 10093 | } |
| 10094 | assert(0); |
| 10095 | return 0; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10096 | } |
| 10097 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10098 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10099 | replace(PyObject *self, PyObject *str1, |
| 10100 | PyObject *str2, Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10101 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10102 | PyObject *u; |
| 10103 | char *sbuf = PyUnicode_DATA(self); |
| 10104 | char *buf1 = PyUnicode_DATA(str1); |
| 10105 | char *buf2 = PyUnicode_DATA(str2); |
| 10106 | int srelease = 0, release1 = 0, release2 = 0; |
| 10107 | int skind = PyUnicode_KIND(self); |
| 10108 | int kind1 = PyUnicode_KIND(str1); |
| 10109 | int kind2 = PyUnicode_KIND(str2); |
| 10110 | Py_ssize_t slen = PyUnicode_GET_LENGTH(self); |
| 10111 | Py_ssize_t len1 = PyUnicode_GET_LENGTH(str1); |
| 10112 | Py_ssize_t len2 = PyUnicode_GET_LENGTH(str2); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10113 | int mayshrink; |
| 10114 | Py_UCS4 maxchar, maxchar_str2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10115 | |
| 10116 | if (maxcount < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10117 | maxcount = PY_SSIZE_T_MAX; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10118 | else if (maxcount == 0 || slen == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10119 | goto nothing; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10120 | |
Victor Stinner | 59de0ee | 2011-10-07 10:01:28 +0200 | [diff] [blame] | 10121 | if (str1 == str2) |
| 10122 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10123 | if (skind < kind1) |
| 10124 | /* substring too wide to be present */ |
| 10125 | goto nothing; |
| 10126 | |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10127 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
| 10128 | maxchar_str2 = PyUnicode_MAX_CHAR_VALUE(str2); |
| 10129 | /* Replacing str1 with str2 may cause a maxchar reduction in the |
| 10130 | result string. */ |
| 10131 | mayshrink = (maxchar_str2 < maxchar); |
| 10132 | maxchar = Py_MAX(maxchar, maxchar_str2); |
| 10133 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10134 | if (len1 == len2) { |
Antoine Pitrou | cbfdee3 | 2010-01-13 08:58:08 +0000 | [diff] [blame] | 10135 | Py_ssize_t i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10136 | /* same length */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10137 | if (len1 == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10138 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10139 | if (len1 == 1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10140 | /* replace characters */ |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10141 | Py_UCS4 u1, u2; |
| 10142 | int rkind; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10143 | u1 = PyUnicode_READ_CHAR(str1, 0); |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 10144 | if (findchar(sbuf, PyUnicode_KIND(self), |
| 10145 | slen, u1, 1) < 0) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10146 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10147 | u2 = PyUnicode_READ_CHAR(str2, 0); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10148 | u = PyUnicode_New(slen, maxchar); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10149 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10150 | goto error; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 10151 | copy_characters(u, 0, self, 0, slen); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10152 | rkind = PyUnicode_KIND(u); |
| 10153 | for (i = 0; i < PyUnicode_GET_LENGTH(u); i++) |
| 10154 | if (PyUnicode_READ(rkind, PyUnicode_DATA(u), i) == u1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10155 | if (--maxcount < 0) |
| 10156 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10157 | PyUnicode_WRITE(rkind, PyUnicode_DATA(u), i, u2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10158 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10159 | } |
| 10160 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10161 | int rkind = skind; |
| 10162 | char *res; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10163 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10164 | if (kind1 < rkind) { |
| 10165 | /* widen substring */ |
| 10166 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10167 | if (!buf1) goto error; |
| 10168 | release1 = 1; |
| 10169 | } |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10170 | i = anylib_find(rkind, self, sbuf, slen, str1, buf1, len1, 0); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10171 | if (i < 0) |
| 10172 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10173 | if (rkind > kind2) { |
| 10174 | /* widen replacement */ |
| 10175 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 10176 | if (!buf2) goto error; |
| 10177 | release2 = 1; |
| 10178 | } |
| 10179 | else if (rkind < kind2) { |
| 10180 | /* widen self and buf1 */ |
| 10181 | rkind = kind2; |
| 10182 | if (release1) PyMem_Free(buf1); |
| 10183 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 10184 | if (!sbuf) goto error; |
| 10185 | srelease = 1; |
| 10186 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10187 | if (!buf1) goto error; |
| 10188 | release1 = 1; |
| 10189 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10190 | u = PyUnicode_New(slen, maxchar); |
| 10191 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10192 | goto error; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10193 | assert(PyUnicode_KIND(u) == rkind); |
| 10194 | res = PyUnicode_DATA(u); |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10195 | |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10196 | memcpy(res, sbuf, rkind * slen); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10197 | /* change everything in-place, starting with this one */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10198 | memcpy(res + rkind * i, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10199 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10200 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10201 | i += len1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10202 | |
| 10203 | while ( --maxcount > 0) { |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10204 | i = anylib_find(rkind, self, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10205 | sbuf+rkind*i, slen-i, |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10206 | str1, buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10207 | if (i == -1) |
| 10208 | break; |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10209 | memcpy(res + rkind * i, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10210 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10211 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10212 | i += len1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10213 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10214 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10215 | } |
| 10216 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10217 | Py_ssize_t n, i, j, ires; |
| 10218 | Py_ssize_t product, new_size; |
| 10219 | int rkind = skind; |
| 10220 | char *res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10221 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10222 | if (kind1 < rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10223 | /* widen substring */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10224 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10225 | if (!buf1) goto error; |
| 10226 | release1 = 1; |
| 10227 | } |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10228 | n = anylib_count(rkind, self, sbuf, slen, str1, buf1, len1, maxcount); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10229 | if (n == 0) |
| 10230 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10231 | if (kind2 < rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10232 | /* widen replacement */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10233 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 10234 | if (!buf2) goto error; |
| 10235 | release2 = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10236 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10237 | else if (kind2 > rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10238 | /* widen self and buf1 */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10239 | rkind = kind2; |
| 10240 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 10241 | if (!sbuf) goto error; |
| 10242 | srelease = 1; |
| 10243 | if (release1) PyMem_Free(buf1); |
| 10244 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10245 | if (!buf1) goto error; |
| 10246 | release1 = 1; |
| 10247 | } |
| 10248 | /* new_size = PyUnicode_GET_LENGTH(self) + n * (PyUnicode_GET_LENGTH(str2) - |
| 10249 | PyUnicode_GET_LENGTH(str1))); */ |
| 10250 | product = n * (len2-len1); |
| 10251 | if ((product / (len2-len1)) != n) { |
| 10252 | PyErr_SetString(PyExc_OverflowError, |
| 10253 | "replace string is too long"); |
| 10254 | goto error; |
| 10255 | } |
| 10256 | new_size = slen + product; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10257 | if (new_size == 0) { |
| 10258 | Py_INCREF(unicode_empty); |
| 10259 | u = unicode_empty; |
| 10260 | goto done; |
| 10261 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10262 | if (new_size < 0 || new_size > (PY_SSIZE_T_MAX >> (rkind-1))) { |
| 10263 | PyErr_SetString(PyExc_OverflowError, |
| 10264 | "replace string is too long"); |
| 10265 | goto error; |
| 10266 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10267 | u = PyUnicode_New(new_size, maxchar); |
| 10268 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10269 | goto error; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10270 | assert(PyUnicode_KIND(u) == rkind); |
| 10271 | res = PyUnicode_DATA(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10272 | ires = i = 0; |
| 10273 | if (len1 > 0) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10274 | while (n-- > 0) { |
| 10275 | /* look for next match */ |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10276 | j = anylib_find(rkind, self, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10277 | sbuf + rkind * i, slen-i, |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10278 | str1, buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10279 | if (j == -1) |
| 10280 | break; |
| 10281 | else if (j > i) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10282 | /* copy unchanged part [i:j] */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10283 | memcpy(res + rkind * ires, |
| 10284 | sbuf + rkind * i, |
| 10285 | rkind * (j-i)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10286 | ires += j - i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10287 | } |
| 10288 | /* copy substitution string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10289 | if (len2 > 0) { |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10290 | memcpy(res + rkind * ires, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10291 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10292 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10293 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10294 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10295 | i = j + len1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10296 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10297 | if (i < slen) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10298 | /* copy tail [i:] */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10299 | memcpy(res + rkind * ires, |
| 10300 | sbuf + rkind * i, |
| 10301 | rkind * (slen-i)); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10302 | } |
| 10303 | else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10304 | /* interleave */ |
| 10305 | while (n > 0) { |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10306 | memcpy(res + rkind * ires, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10307 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10308 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10309 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10310 | if (--n <= 0) |
| 10311 | break; |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10312 | memcpy(res + rkind * ires, |
| 10313 | sbuf + rkind * i, |
| 10314 | rkind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10315 | ires++; |
| 10316 | i++; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10317 | } |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10318 | memcpy(res + rkind * ires, |
| 10319 | sbuf + rkind * i, |
| 10320 | rkind * (slen-i)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10321 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10322 | } |
| 10323 | |
| 10324 | if (mayshrink) { |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10325 | unicode_adjust_maxchar(&u); |
| 10326 | if (u == NULL) |
| 10327 | goto error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10328 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10329 | |
| 10330 | done: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10331 | if (srelease) |
| 10332 | PyMem_FREE(sbuf); |
| 10333 | if (release1) |
| 10334 | PyMem_FREE(buf1); |
| 10335 | if (release2) |
| 10336 | PyMem_FREE(buf2); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10337 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10338 | return u; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10339 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10340 | nothing: |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10341 | /* nothing to replace; return original string (when possible) */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10342 | if (srelease) |
| 10343 | PyMem_FREE(sbuf); |
| 10344 | if (release1) |
| 10345 | PyMem_FREE(buf1); |
| 10346 | if (release2) |
| 10347 | PyMem_FREE(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10348 | if (PyUnicode_CheckExact(self)) { |
| 10349 | Py_INCREF(self); |
| 10350 | return (PyObject *) self; |
| 10351 | } |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 10352 | return PyUnicode_Copy(self); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10353 | error: |
| 10354 | if (srelease && sbuf) |
| 10355 | PyMem_FREE(sbuf); |
| 10356 | if (release1 && buf1) |
| 10357 | PyMem_FREE(buf1); |
| 10358 | if (release2 && buf2) |
| 10359 | PyMem_FREE(buf2); |
| 10360 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10361 | } |
| 10362 | |
| 10363 | /* --- Unicode Object Methods --------------------------------------------- */ |
| 10364 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10365 | PyDoc_STRVAR(title__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10366 | "S.title() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10367 | \n\ |
| 10368 | 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] | 10369 | characters, all remaining cased characters have lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10370 | |
| 10371 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10372 | unicode_title(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10373 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10374 | return fixup(self, fixtitle); |
| 10375 | } |
| 10376 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10377 | PyDoc_STRVAR(capitalize__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10378 | "S.capitalize() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10379 | \n\ |
| 10380 | 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] | 10381 | have upper case and the rest lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10382 | |
| 10383 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10384 | unicode_capitalize(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10385 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10386 | return fixup(self, fixcapitalize); |
| 10387 | } |
| 10388 | |
| 10389 | #if 0 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10390 | PyDoc_STRVAR(capwords__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10391 | "S.capwords() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10392 | \n\ |
| 10393 | 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] | 10394 | normalized whitespace (all whitespace strings are replaced by ' ')."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10395 | |
| 10396 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10397 | unicode_capwords(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10398 | { |
| 10399 | PyObject *list; |
| 10400 | PyObject *item; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10401 | Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10402 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10403 | /* Split into words */ |
| 10404 | list = split(self, NULL, -1); |
| 10405 | if (!list) |
| 10406 | return NULL; |
| 10407 | |
| 10408 | /* Capitalize each word */ |
| 10409 | for (i = 0; i < PyList_GET_SIZE(list); i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10410 | item = fixup(PyList_GET_ITEM(list, i), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10411 | fixcapitalize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10412 | if (item == NULL) |
| 10413 | goto onError; |
| 10414 | Py_DECREF(PyList_GET_ITEM(list, i)); |
| 10415 | PyList_SET_ITEM(list, i, item); |
| 10416 | } |
| 10417 | |
| 10418 | /* Join the words to form a new string */ |
| 10419 | item = PyUnicode_Join(NULL, list); |
| 10420 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10421 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10422 | Py_DECREF(list); |
| 10423 | return (PyObject *)item; |
| 10424 | } |
| 10425 | #endif |
| 10426 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10427 | /* Argument converter. Coerces to a single unicode character */ |
| 10428 | |
| 10429 | static int |
| 10430 | convert_uc(PyObject *obj, void *addr) |
| 10431 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10432 | Py_UCS4 *fillcharloc = (Py_UCS4 *)addr; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10433 | PyObject *uniobj; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10434 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10435 | uniobj = PyUnicode_FromObject(obj); |
| 10436 | if (uniobj == NULL) { |
| 10437 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10438 | "The fill character cannot be converted to Unicode"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10439 | return 0; |
| 10440 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10441 | if (PyUnicode_GET_LENGTH(uniobj) != 1) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10442 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10443 | "The fill character must be exactly one character long"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10444 | Py_DECREF(uniobj); |
| 10445 | return 0; |
| 10446 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10447 | *fillcharloc = PyUnicode_READ_CHAR(uniobj, 0); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10448 | Py_DECREF(uniobj); |
| 10449 | return 1; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10450 | } |
| 10451 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10452 | PyDoc_STRVAR(center__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10453 | "S.center(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10454 | \n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 10455 | Return S centered in a string of length width. Padding is\n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10456 | done using the specified fill character (default is a space)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10457 | |
| 10458 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10459 | unicode_center(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10460 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10461 | Py_ssize_t marg, left; |
| 10462 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10463 | Py_UCS4 fillchar = ' '; |
| 10464 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 10465 | 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] | 10466 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10467 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 10468 | if (PyUnicode_READY(self) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10469 | return NULL; |
| 10470 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10471 | if (_PyUnicode_LENGTH(self) >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10472 | Py_INCREF(self); |
| 10473 | return (PyObject*) self; |
| 10474 | } |
| 10475 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10476 | marg = width - _PyUnicode_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10477 | left = marg / 2 + (marg & width & 1); |
| 10478 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10479 | return pad(self, left, marg - left, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10480 | } |
| 10481 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10482 | /* This function assumes that str1 and str2 are readied by the caller. */ |
| 10483 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10484 | static int |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10485 | unicode_compare(PyObject *str1, PyObject *str2) |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10486 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10487 | int kind1, kind2; |
| 10488 | void *data1, *data2; |
| 10489 | Py_ssize_t len1, len2, i; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10490 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10491 | kind1 = PyUnicode_KIND(str1); |
| 10492 | kind2 = PyUnicode_KIND(str2); |
| 10493 | data1 = PyUnicode_DATA(str1); |
| 10494 | data2 = PyUnicode_DATA(str2); |
| 10495 | len1 = PyUnicode_GET_LENGTH(str1); |
| 10496 | len2 = PyUnicode_GET_LENGTH(str2); |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10497 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10498 | for (i = 0; i < len1 && i < len2; ++i) { |
| 10499 | Py_UCS4 c1, c2; |
| 10500 | c1 = PyUnicode_READ(kind1, data1, i); |
| 10501 | c2 = PyUnicode_READ(kind2, data2, i); |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 10502 | |
| 10503 | if (c1 != c2) |
| 10504 | return (c1 < c2) ? -1 : 1; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10505 | } |
| 10506 | |
| 10507 | return (len1 < len2) ? -1 : (len1 != len2); |
| 10508 | } |
| 10509 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10510 | int |
| 10511 | PyUnicode_Compare(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10512 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10513 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 10514 | if (PyUnicode_READY(left) == -1 || |
| 10515 | PyUnicode_READY(right) == -1) |
| 10516 | return -1; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10517 | return unicode_compare(left, right); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10518 | } |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 10519 | PyErr_Format(PyExc_TypeError, |
| 10520 | "Can't compare %.100s and %.100s", |
| 10521 | left->ob_type->tp_name, |
| 10522 | right->ob_type->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10523 | return -1; |
| 10524 | } |
| 10525 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10526 | int |
| 10527 | PyUnicode_CompareWithASCIIString(PyObject* uni, const char* str) |
| 10528 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10529 | Py_ssize_t i; |
| 10530 | int kind; |
| 10531 | void *data; |
| 10532 | Py_UCS4 chr; |
| 10533 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 10534 | assert(_PyUnicode_CHECK(uni)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10535 | if (PyUnicode_READY(uni) == -1) |
| 10536 | return -1; |
| 10537 | kind = PyUnicode_KIND(uni); |
| 10538 | data = PyUnicode_DATA(uni); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10539 | /* Compare Unicode string and source character set string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10540 | for (i = 0; (chr = PyUnicode_READ(kind, data, i)) && str[i]; i++) |
| 10541 | if (chr != str[i]) |
| 10542 | return (chr < (unsigned char)(str[i])) ? -1 : 1; |
Benjamin Peterson | 8667a9b | 2010-01-09 21:45:28 +0000 | [diff] [blame] | 10543 | /* This check keeps Python strings that end in '\0' from comparing equal |
| 10544 | to C strings identical up to that point. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10545 | if (PyUnicode_GET_LENGTH(uni) != i || chr) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10546 | return 1; /* uni is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10547 | if (str[i]) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10548 | return -1; /* str is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10549 | return 0; |
| 10550 | } |
| 10551 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10552 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10553 | #define TEST_COND(cond) \ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10554 | ((cond) ? Py_True : Py_False) |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10555 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10556 | PyObject * |
| 10557 | PyUnicode_RichCompare(PyObject *left, PyObject *right, int op) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10558 | { |
| 10559 | int result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10560 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10561 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 10562 | PyObject *v; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10563 | if (PyUnicode_READY(left) == -1 || |
| 10564 | PyUnicode_READY(right) == -1) |
| 10565 | return NULL; |
| 10566 | if (PyUnicode_GET_LENGTH(left) != PyUnicode_GET_LENGTH(right) || |
| 10567 | PyUnicode_KIND(left) != PyUnicode_KIND(right)) { |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10568 | if (op == Py_EQ) { |
| 10569 | Py_INCREF(Py_False); |
| 10570 | return Py_False; |
| 10571 | } |
| 10572 | if (op == Py_NE) { |
| 10573 | Py_INCREF(Py_True); |
| 10574 | return Py_True; |
| 10575 | } |
| 10576 | } |
| 10577 | if (left == right) |
| 10578 | result = 0; |
| 10579 | else |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10580 | result = unicode_compare(left, right); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10581 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10582 | /* Convert the return value to a Boolean */ |
| 10583 | switch (op) { |
| 10584 | case Py_EQ: |
| 10585 | v = TEST_COND(result == 0); |
| 10586 | break; |
| 10587 | case Py_NE: |
| 10588 | v = TEST_COND(result != 0); |
| 10589 | break; |
| 10590 | case Py_LE: |
| 10591 | v = TEST_COND(result <= 0); |
| 10592 | break; |
| 10593 | case Py_GE: |
| 10594 | v = TEST_COND(result >= 0); |
| 10595 | break; |
| 10596 | case Py_LT: |
| 10597 | v = TEST_COND(result == -1); |
| 10598 | break; |
| 10599 | case Py_GT: |
| 10600 | v = TEST_COND(result == 1); |
| 10601 | break; |
| 10602 | default: |
| 10603 | PyErr_BadArgument(); |
| 10604 | return NULL; |
| 10605 | } |
| 10606 | Py_INCREF(v); |
| 10607 | return v; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10608 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10609 | |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 10610 | Py_RETURN_NOTIMPLEMENTED; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10611 | } |
| 10612 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10613 | int |
| 10614 | PyUnicode_Contains(PyObject *container, PyObject *element) |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10615 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10616 | PyObject *str, *sub; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10617 | int kind1, kind2, kind; |
| 10618 | void *buf1, *buf2; |
| 10619 | Py_ssize_t len1, len2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10620 | int result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10621 | |
| 10622 | /* Coerce the two arguments */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10623 | sub = PyUnicode_FromObject(element); |
| 10624 | if (!sub) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10625 | PyErr_Format(PyExc_TypeError, |
| 10626 | "'in <string>' requires string as left operand, not %s", |
| 10627 | element->ob_type->tp_name); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10628 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10629 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10630 | if (PyUnicode_READY(sub) == -1) |
| 10631 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10632 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10633 | str = PyUnicode_FromObject(container); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 10634 | if (!str || PyUnicode_READY(str) == -1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10635 | Py_DECREF(sub); |
| 10636 | return -1; |
| 10637 | } |
| 10638 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10639 | kind1 = PyUnicode_KIND(str); |
| 10640 | kind2 = PyUnicode_KIND(sub); |
| 10641 | kind = kind1 > kind2 ? kind1 : kind2; |
| 10642 | buf1 = PyUnicode_DATA(str); |
| 10643 | buf2 = PyUnicode_DATA(sub); |
| 10644 | if (kind1 != kind) |
| 10645 | buf1 = _PyUnicode_AsKind((PyObject*)str, kind); |
| 10646 | if (!buf1) { |
| 10647 | Py_DECREF(sub); |
| 10648 | return -1; |
| 10649 | } |
| 10650 | if (kind2 != kind) |
| 10651 | buf2 = _PyUnicode_AsKind((PyObject*)sub, kind); |
| 10652 | if (!buf2) { |
| 10653 | Py_DECREF(sub); |
| 10654 | if (kind1 != kind) PyMem_Free(buf1); |
| 10655 | return -1; |
| 10656 | } |
| 10657 | len1 = PyUnicode_GET_LENGTH(str); |
| 10658 | len2 = PyUnicode_GET_LENGTH(sub); |
| 10659 | |
| 10660 | switch(kind) { |
| 10661 | case PyUnicode_1BYTE_KIND: |
| 10662 | result = ucs1lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10663 | break; |
| 10664 | case PyUnicode_2BYTE_KIND: |
| 10665 | result = ucs2lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10666 | break; |
| 10667 | case PyUnicode_4BYTE_KIND: |
| 10668 | result = ucs4lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10669 | break; |
| 10670 | default: |
| 10671 | result = -1; |
| 10672 | assert(0); |
| 10673 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10674 | |
| 10675 | Py_DECREF(str); |
| 10676 | Py_DECREF(sub); |
| 10677 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10678 | if (kind1 != kind) |
| 10679 | PyMem_Free(buf1); |
| 10680 | if (kind2 != kind) |
| 10681 | PyMem_Free(buf2); |
| 10682 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10683 | return result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10684 | } |
| 10685 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10686 | /* Concat to string or Unicode object giving a new Unicode object. */ |
| 10687 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10688 | PyObject * |
| 10689 | PyUnicode_Concat(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10690 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10691 | PyObject *u = NULL, *v = NULL, *w; |
Victor Stinner | 127226b | 2011-10-13 01:12:34 +0200 | [diff] [blame] | 10692 | Py_UCS4 maxchar, maxchar2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10693 | |
| 10694 | /* Coerce the two arguments */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10695 | u = PyUnicode_FromObject(left); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10696 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10697 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10698 | v = PyUnicode_FromObject(right); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10699 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10700 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10701 | |
| 10702 | /* Shortcuts */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 10703 | if (v == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10704 | Py_DECREF(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10705 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10706 | } |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 10707 | if (u == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10708 | Py_DECREF(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10709 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10710 | } |
| 10711 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10712 | maxchar = PyUnicode_MAX_CHAR_VALUE(u); |
Victor Stinner | 127226b | 2011-10-13 01:12:34 +0200 | [diff] [blame] | 10713 | maxchar2 = PyUnicode_MAX_CHAR_VALUE(v); |
| 10714 | maxchar = Py_MAX(maxchar, maxchar2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10715 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10716 | /* Concat the two Unicode strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10717 | w = PyUnicode_New( |
| 10718 | PyUnicode_GET_LENGTH(u) + PyUnicode_GET_LENGTH(v), |
| 10719 | maxchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10720 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10721 | goto onError; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 10722 | copy_characters(w, 0, u, 0, PyUnicode_GET_LENGTH(u)); |
| 10723 | 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] | 10724 | Py_DECREF(u); |
| 10725 | Py_DECREF(v); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10726 | assert(_PyUnicode_CheckConsistency(w, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10727 | return w; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10728 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10729 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10730 | Py_XDECREF(u); |
| 10731 | Py_XDECREF(v); |
| 10732 | return NULL; |
| 10733 | } |
| 10734 | |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10735 | static void |
| 10736 | unicode_append_inplace(PyObject **p_left, PyObject *right) |
| 10737 | { |
| 10738 | Py_ssize_t left_len, right_len, new_len; |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10739 | |
| 10740 | assert(PyUnicode_IS_READY(*p_left)); |
| 10741 | assert(PyUnicode_IS_READY(right)); |
| 10742 | |
| 10743 | left_len = PyUnicode_GET_LENGTH(*p_left); |
| 10744 | right_len = PyUnicode_GET_LENGTH(right); |
| 10745 | if (left_len > PY_SSIZE_T_MAX - right_len) { |
| 10746 | PyErr_SetString(PyExc_OverflowError, |
| 10747 | "strings are too large to concat"); |
| 10748 | goto error; |
| 10749 | } |
| 10750 | new_len = left_len + right_len; |
| 10751 | |
| 10752 | /* Now we own the last reference to 'left', so we can resize it |
| 10753 | * in-place. |
| 10754 | */ |
| 10755 | if (unicode_resize(p_left, new_len) != 0) { |
| 10756 | /* XXX if _PyUnicode_Resize() fails, 'left' has been |
| 10757 | * deallocated so it cannot be put back into |
| 10758 | * 'variable'. The MemoryError is raised when there |
| 10759 | * is no value in 'variable', which might (very |
| 10760 | * remotely) be a cause of incompatibilities. |
| 10761 | */ |
| 10762 | goto error; |
| 10763 | } |
| 10764 | /* copy 'right' into the newly allocated area of 'left' */ |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 10765 | copy_characters(*p_left, left_len, right, 0, right_len); |
| 10766 | _PyUnicode_DIRTY(*p_left); |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10767 | return; |
| 10768 | |
| 10769 | error: |
| 10770 | Py_DECREF(*p_left); |
| 10771 | *p_left = NULL; |
| 10772 | } |
| 10773 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10774 | void |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10775 | PyUnicode_Append(PyObject **p_left, PyObject *right) |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10776 | { |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10777 | PyObject *left, *res; |
| 10778 | |
| 10779 | if (p_left == NULL) { |
| 10780 | if (!PyErr_Occurred()) |
| 10781 | PyErr_BadInternalCall(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10782 | return; |
| 10783 | } |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10784 | left = *p_left; |
| 10785 | if (right == NULL || !PyUnicode_Check(left)) { |
| 10786 | if (!PyErr_Occurred()) |
| 10787 | PyErr_BadInternalCall(); |
| 10788 | goto error; |
| 10789 | } |
| 10790 | |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 10791 | if (PyUnicode_READY(left)) |
| 10792 | goto error; |
| 10793 | if (PyUnicode_READY(right)) |
| 10794 | goto error; |
| 10795 | |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10796 | if (PyUnicode_CheckExact(left) && left != unicode_empty |
| 10797 | && PyUnicode_CheckExact(right) && right != unicode_empty |
| 10798 | && unicode_resizable(left) |
| 10799 | && (_PyUnicode_KIND(right) <= _PyUnicode_KIND(left) |
| 10800 | || _PyUnicode_WSTR(left) != NULL)) |
| 10801 | { |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10802 | /* Don't resize for ascii += latin1. Convert ascii to latin1 requires |
| 10803 | to change the structure size, but characters are stored just after |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 10804 | the structure, and so it requires to move all characters which is |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10805 | not so different than duplicating the string. */ |
| 10806 | if (!(PyUnicode_IS_ASCII(left) && !PyUnicode_IS_ASCII(right))) |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10807 | { |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10808 | unicode_append_inplace(p_left, right); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10809 | if (p_left != NULL) |
| 10810 | assert(_PyUnicode_CheckConsistency(*p_left, 1)); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10811 | return; |
| 10812 | } |
| 10813 | } |
| 10814 | |
| 10815 | res = PyUnicode_Concat(left, right); |
| 10816 | if (res == NULL) |
| 10817 | goto error; |
| 10818 | Py_DECREF(left); |
| 10819 | *p_left = res; |
| 10820 | return; |
| 10821 | |
| 10822 | error: |
| 10823 | Py_DECREF(*p_left); |
| 10824 | *p_left = NULL; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10825 | } |
| 10826 | |
| 10827 | void |
| 10828 | PyUnicode_AppendAndDel(PyObject **pleft, PyObject *right) |
| 10829 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10830 | PyUnicode_Append(pleft, right); |
| 10831 | Py_XDECREF(right); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10832 | } |
| 10833 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10834 | PyDoc_STRVAR(count__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10835 | "S.count(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10836 | \n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10837 | Return the number of non-overlapping occurrences of substring sub in\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 10838 | 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] | 10839 | interpreted as in slice notation."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10840 | |
| 10841 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10842 | unicode_count(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10843 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10844 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10845 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10846 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10847 | PyObject *result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10848 | int kind1, kind2, kind; |
| 10849 | void *buf1, *buf2; |
| 10850 | Py_ssize_t len1, len2, iresult; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10851 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10852 | if (!stringlib_parse_args_finds_unicode("count", args, &substring, |
| 10853 | &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10854 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 10855 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10856 | kind1 = PyUnicode_KIND(self); |
| 10857 | kind2 = PyUnicode_KIND(substring); |
| 10858 | kind = kind1 > kind2 ? kind1 : kind2; |
| 10859 | buf1 = PyUnicode_DATA(self); |
| 10860 | buf2 = PyUnicode_DATA(substring); |
| 10861 | if (kind1 != kind) |
| 10862 | buf1 = _PyUnicode_AsKind((PyObject*)self, kind); |
| 10863 | if (!buf1) { |
| 10864 | Py_DECREF(substring); |
| 10865 | return NULL; |
| 10866 | } |
| 10867 | if (kind2 != kind) |
| 10868 | buf2 = _PyUnicode_AsKind((PyObject*)substring, kind); |
| 10869 | if (!buf2) { |
| 10870 | Py_DECREF(substring); |
| 10871 | if (kind1 != kind) PyMem_Free(buf1); |
| 10872 | return NULL; |
| 10873 | } |
| 10874 | len1 = PyUnicode_GET_LENGTH(self); |
| 10875 | len2 = PyUnicode_GET_LENGTH(substring); |
| 10876 | |
| 10877 | ADJUST_INDICES(start, end, len1); |
| 10878 | switch(kind) { |
| 10879 | case PyUnicode_1BYTE_KIND: |
| 10880 | iresult = ucs1lib_count( |
| 10881 | ((Py_UCS1*)buf1) + start, end - start, |
| 10882 | buf2, len2, PY_SSIZE_T_MAX |
| 10883 | ); |
| 10884 | break; |
| 10885 | case PyUnicode_2BYTE_KIND: |
| 10886 | iresult = ucs2lib_count( |
| 10887 | ((Py_UCS2*)buf1) + start, end - start, |
| 10888 | buf2, len2, PY_SSIZE_T_MAX |
| 10889 | ); |
| 10890 | break; |
| 10891 | case PyUnicode_4BYTE_KIND: |
| 10892 | iresult = ucs4lib_count( |
| 10893 | ((Py_UCS4*)buf1) + start, end - start, |
| 10894 | buf2, len2, PY_SSIZE_T_MAX |
| 10895 | ); |
| 10896 | break; |
| 10897 | default: |
| 10898 | assert(0); iresult = 0; |
| 10899 | } |
| 10900 | |
| 10901 | result = PyLong_FromSsize_t(iresult); |
| 10902 | |
| 10903 | if (kind1 != kind) |
| 10904 | PyMem_Free(buf1); |
| 10905 | if (kind2 != kind) |
| 10906 | PyMem_Free(buf2); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10907 | |
| 10908 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10909 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10910 | return result; |
| 10911 | } |
| 10912 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10913 | PyDoc_STRVAR(encode__doc__, |
Victor Stinner | c911bbf | 2010-11-07 19:04:46 +0000 | [diff] [blame] | 10914 | "S.encode(encoding='utf-8', errors='strict') -> bytes\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10915 | \n\ |
Victor Stinner | e14e212 | 2010-11-07 18:41:46 +0000 | [diff] [blame] | 10916 | Encode S using the codec registered for encoding. Default encoding\n\ |
| 10917 | 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] | 10918 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 10919 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 10920 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 10921 | codecs.register_error that can handle UnicodeEncodeErrors."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10922 | |
| 10923 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10924 | unicode_encode(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10925 | { |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 10926 | static char *kwlist[] = {"encoding", "errors", 0}; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10927 | char *encoding = NULL; |
| 10928 | char *errors = NULL; |
Guido van Rossum | 35d9428 | 2007-08-27 18:20:11 +0000 | [diff] [blame] | 10929 | |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 10930 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:encode", |
| 10931 | kwlist, &encoding, &errors)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10932 | return NULL; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10933 | return PyUnicode_AsEncodedString(self, encoding, errors); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 10934 | } |
| 10935 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10936 | PyDoc_STRVAR(expandtabs__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10937 | "S.expandtabs([tabsize]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10938 | \n\ |
| 10939 | 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] | 10940 | 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] | 10941 | |
| 10942 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10943 | unicode_expandtabs(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10944 | { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10945 | Py_ssize_t i, j, line_pos, src_len, incr; |
| 10946 | Py_UCS4 ch; |
| 10947 | PyObject *u; |
| 10948 | void *src_data, *dest_data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10949 | int tabsize = 8; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10950 | int kind; |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 10951 | int found; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10952 | |
| 10953 | if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10954 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10955 | |
Antoine Pitrou | 2242522 | 2011-10-04 19:10:51 +0200 | [diff] [blame] | 10956 | if (PyUnicode_READY(self) == -1) |
| 10957 | return NULL; |
| 10958 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 10959 | /* First pass: determine size of output string */ |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10960 | src_len = PyUnicode_GET_LENGTH(self); |
| 10961 | i = j = line_pos = 0; |
| 10962 | kind = PyUnicode_KIND(self); |
| 10963 | src_data = PyUnicode_DATA(self); |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 10964 | found = 0; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10965 | for (; i < src_len; i++) { |
| 10966 | ch = PyUnicode_READ(kind, src_data, i); |
| 10967 | if (ch == '\t') { |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 10968 | found = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10969 | if (tabsize > 0) { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10970 | incr = tabsize - (line_pos % tabsize); /* cannot overflow */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10971 | if (j > PY_SSIZE_T_MAX - incr) |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10972 | goto overflow; |
| 10973 | line_pos += incr; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10974 | j += incr; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10975 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10976 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10977 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10978 | if (j > PY_SSIZE_T_MAX - 1) |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10979 | goto overflow; |
| 10980 | line_pos++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10981 | j++; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10982 | if (ch == '\n' || ch == '\r') |
| 10983 | line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10984 | } |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10985 | } |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 10986 | if (!found && PyUnicode_CheckExact(self)) { |
| 10987 | Py_INCREF((PyObject *) self); |
| 10988 | return (PyObject *) self; |
| 10989 | } |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 10990 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10991 | /* Second pass: create output string and fill it */ |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10992 | u = PyUnicode_New(j, PyUnicode_MAX_CHAR_VALUE(self)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10993 | if (!u) |
| 10994 | return NULL; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10995 | dest_data = PyUnicode_DATA(u); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10996 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10997 | i = j = line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10998 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10999 | for (; i < src_len; i++) { |
| 11000 | ch = PyUnicode_READ(kind, src_data, i); |
| 11001 | if (ch == '\t') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11002 | if (tabsize > 0) { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11003 | incr = tabsize - (line_pos % tabsize); |
| 11004 | line_pos += incr; |
| 11005 | while (incr--) { |
| 11006 | PyUnicode_WRITE(kind, dest_data, j, ' '); |
| 11007 | j++; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11008 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11009 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11010 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11011 | else { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11012 | line_pos++; |
| 11013 | PyUnicode_WRITE(kind, dest_data, j, ch); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11014 | j++; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11015 | if (ch == '\n' || ch == '\r') |
| 11016 | line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11017 | } |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11018 | } |
| 11019 | assert (j == PyUnicode_GET_LENGTH(u)); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 11020 | #ifndef DONT_MAKE_RESULT_READY |
| 11021 | if (_PyUnicode_READY_REPLACE(&u)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11022 | Py_DECREF(u); |
| 11023 | return NULL; |
| 11024 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 11025 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 11026 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11027 | return (PyObject*) u; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11028 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11029 | overflow: |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11030 | PyErr_SetString(PyExc_OverflowError, "new string is too long"); |
| 11031 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11032 | } |
| 11033 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11034 | PyDoc_STRVAR(find__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11035 | "S.find(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11036 | \n\ |
| 11037 | Return the lowest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 11038 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11039 | arguments start and end are interpreted as in slice notation.\n\ |
| 11040 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11041 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11042 | |
| 11043 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11044 | unicode_find(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11045 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11046 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 11047 | Py_ssize_t start; |
| 11048 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11049 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11050 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11051 | if (!stringlib_parse_args_finds_unicode("find", args, &substring, |
| 11052 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11053 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11054 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11055 | if (PyUnicode_READY(self) == -1) |
| 11056 | return NULL; |
| 11057 | if (PyUnicode_READY(substring) == -1) |
| 11058 | return NULL; |
| 11059 | |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 11060 | result = any_find_slice(1, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11061 | self, (PyObject*)substring, start, end |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11062 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11063 | |
| 11064 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11065 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11066 | if (result == -2) |
| 11067 | return NULL; |
| 11068 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 11069 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11070 | } |
| 11071 | |
| 11072 | static PyObject * |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 11073 | unicode_getitem(PyObject *self, Py_ssize_t index) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11074 | { |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 11075 | Py_UCS4 ch = PyUnicode_ReadChar(self, index); |
| 11076 | if (ch == (Py_UCS4)-1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11077 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11078 | return PyUnicode_FromOrdinal(ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11079 | } |
| 11080 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11081 | /* Believe it or not, this produces the same value for ASCII strings |
Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 11082 | as bytes_hash(). */ |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 11083 | static Py_hash_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11084 | unicode_hash(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11085 | { |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11086 | Py_ssize_t len; |
Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 11087 | Py_uhash_t x; |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11088 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11089 | if (_PyUnicode_HASH(self) != -1) |
| 11090 | return _PyUnicode_HASH(self); |
| 11091 | if (PyUnicode_READY(self) == -1) |
| 11092 | return -1; |
| 11093 | len = PyUnicode_GET_LENGTH(self); |
| 11094 | |
| 11095 | /* The hash function as a macro, gets expanded three times below. */ |
| 11096 | #define HASH(P) \ |
| 11097 | x = (Py_uhash_t)*P << 7; \ |
| 11098 | while (--len >= 0) \ |
| 11099 | x = (1000003*x) ^ (Py_uhash_t)*P++; |
| 11100 | |
| 11101 | switch (PyUnicode_KIND(self)) { |
| 11102 | case PyUnicode_1BYTE_KIND: { |
| 11103 | const unsigned char *c = PyUnicode_1BYTE_DATA(self); |
| 11104 | HASH(c); |
| 11105 | break; |
| 11106 | } |
| 11107 | case PyUnicode_2BYTE_KIND: { |
| 11108 | const Py_UCS2 *s = PyUnicode_2BYTE_DATA(self); |
| 11109 | HASH(s); |
| 11110 | break; |
| 11111 | } |
| 11112 | default: { |
| 11113 | Py_UCS4 *l; |
| 11114 | assert(PyUnicode_KIND(self) == PyUnicode_4BYTE_KIND && |
| 11115 | "Impossible switch case in unicode_hash"); |
| 11116 | l = PyUnicode_4BYTE_DATA(self); |
| 11117 | HASH(l); |
| 11118 | break; |
| 11119 | } |
| 11120 | } |
| 11121 | x ^= (Py_uhash_t)PyUnicode_GET_LENGTH(self); |
| 11122 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11123 | if (x == -1) |
| 11124 | x = -2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11125 | _PyUnicode_HASH(self) = x; |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11126 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11127 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11128 | #undef HASH |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11129 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11130 | PyDoc_STRVAR(index__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11131 | "S.index(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11132 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11133 | 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] | 11134 | |
| 11135 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11136 | unicode_index(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11137 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11138 | Py_ssize_t result; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11139 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 11140 | Py_ssize_t start; |
| 11141 | Py_ssize_t end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11142 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11143 | if (!stringlib_parse_args_finds_unicode("index", args, &substring, |
| 11144 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11145 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11146 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11147 | if (PyUnicode_READY(self) == -1) |
| 11148 | return NULL; |
| 11149 | if (PyUnicode_READY(substring) == -1) |
| 11150 | return NULL; |
| 11151 | |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 11152 | result = any_find_slice(1, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11153 | self, (PyObject*)substring, start, end |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11154 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11155 | |
| 11156 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11157 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11158 | if (result == -2) |
| 11159 | return NULL; |
| 11160 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11161 | if (result < 0) { |
| 11162 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 11163 | return NULL; |
| 11164 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11165 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 11166 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11167 | } |
| 11168 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11169 | PyDoc_STRVAR(islower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11170 | "S.islower() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11171 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11172 | 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] | 11173 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11174 | |
| 11175 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11176 | unicode_islower(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11177 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11178 | Py_ssize_t i, length; |
| 11179 | int kind; |
| 11180 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11181 | int cased; |
| 11182 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11183 | if (PyUnicode_READY(self) == -1) |
| 11184 | return NULL; |
| 11185 | length = PyUnicode_GET_LENGTH(self); |
| 11186 | kind = PyUnicode_KIND(self); |
| 11187 | data = PyUnicode_DATA(self); |
| 11188 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11189 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11190 | if (length == 1) |
| 11191 | return PyBool_FromLong( |
| 11192 | Py_UNICODE_ISLOWER(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11193 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11194 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11195 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11196 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11197 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11198 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11199 | for (i = 0; i < length; i++) { |
| 11200 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11201 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11202 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 11203 | return PyBool_FromLong(0); |
| 11204 | else if (!cased && Py_UNICODE_ISLOWER(ch)) |
| 11205 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11206 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11207 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11208 | } |
| 11209 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11210 | PyDoc_STRVAR(isupper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11211 | "S.isupper() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11212 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11213 | 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] | 11214 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11215 | |
| 11216 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11217 | unicode_isupper(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11218 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11219 | Py_ssize_t i, length; |
| 11220 | int kind; |
| 11221 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11222 | int cased; |
| 11223 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11224 | if (PyUnicode_READY(self) == -1) |
| 11225 | return NULL; |
| 11226 | length = PyUnicode_GET_LENGTH(self); |
| 11227 | kind = PyUnicode_KIND(self); |
| 11228 | data = PyUnicode_DATA(self); |
| 11229 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11230 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11231 | if (length == 1) |
| 11232 | return PyBool_FromLong( |
| 11233 | Py_UNICODE_ISUPPER(PyUnicode_READ(kind, data, 0)) != 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11234 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11235 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11236 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11237 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11238 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11239 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11240 | for (i = 0; i < length; i++) { |
| 11241 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11242 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11243 | if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 11244 | return PyBool_FromLong(0); |
| 11245 | else if (!cased && Py_UNICODE_ISUPPER(ch)) |
| 11246 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11247 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11248 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11249 | } |
| 11250 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11251 | PyDoc_STRVAR(istitle__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11252 | "S.istitle() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11253 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11254 | Return True if S is a titlecased string and there is at least one\n\ |
| 11255 | character in S, i.e. upper- and titlecase characters may only\n\ |
| 11256 | follow uncased characters and lowercase characters only cased ones.\n\ |
| 11257 | Return False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11258 | |
| 11259 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11260 | unicode_istitle(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11261 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11262 | Py_ssize_t i, length; |
| 11263 | int kind; |
| 11264 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11265 | int cased, previous_is_cased; |
| 11266 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11267 | if (PyUnicode_READY(self) == -1) |
| 11268 | return NULL; |
| 11269 | length = PyUnicode_GET_LENGTH(self); |
| 11270 | kind = PyUnicode_KIND(self); |
| 11271 | data = PyUnicode_DATA(self); |
| 11272 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11273 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11274 | if (length == 1) { |
| 11275 | Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11276 | return PyBool_FromLong((Py_UNICODE_ISTITLE(ch) != 0) || |
| 11277 | (Py_UNICODE_ISUPPER(ch) != 0)); |
| 11278 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11279 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11280 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11281 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11282 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11283 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11284 | cased = 0; |
| 11285 | previous_is_cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11286 | for (i = 0; i < length; i++) { |
| 11287 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11288 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11289 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { |
| 11290 | if (previous_is_cased) |
| 11291 | return PyBool_FromLong(0); |
| 11292 | previous_is_cased = 1; |
| 11293 | cased = 1; |
| 11294 | } |
| 11295 | else if (Py_UNICODE_ISLOWER(ch)) { |
| 11296 | if (!previous_is_cased) |
| 11297 | return PyBool_FromLong(0); |
| 11298 | previous_is_cased = 1; |
| 11299 | cased = 1; |
| 11300 | } |
| 11301 | else |
| 11302 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11303 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11304 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11305 | } |
| 11306 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11307 | PyDoc_STRVAR(isspace__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11308 | "S.isspace() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11309 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11310 | Return True if all characters in S are whitespace\n\ |
| 11311 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11312 | |
| 11313 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11314 | unicode_isspace(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11315 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11316 | Py_ssize_t i, length; |
| 11317 | int kind; |
| 11318 | void *data; |
| 11319 | |
| 11320 | if (PyUnicode_READY(self) == -1) |
| 11321 | return NULL; |
| 11322 | length = PyUnicode_GET_LENGTH(self); |
| 11323 | kind = PyUnicode_KIND(self); |
| 11324 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11325 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11326 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11327 | if (length == 1) |
| 11328 | return PyBool_FromLong( |
| 11329 | Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11330 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11331 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11332 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11333 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11334 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11335 | for (i = 0; i < length; i++) { |
| 11336 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11337 | if (!Py_UNICODE_ISSPACE(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11338 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11339 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11340 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11341 | } |
| 11342 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11343 | PyDoc_STRVAR(isalpha__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11344 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11345 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11346 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11347 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11348 | |
| 11349 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11350 | unicode_isalpha(PyObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11351 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11352 | Py_ssize_t i, length; |
| 11353 | int kind; |
| 11354 | void *data; |
| 11355 | |
| 11356 | if (PyUnicode_READY(self) == -1) |
| 11357 | return NULL; |
| 11358 | length = PyUnicode_GET_LENGTH(self); |
| 11359 | kind = PyUnicode_KIND(self); |
| 11360 | data = PyUnicode_DATA(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11361 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11362 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11363 | if (length == 1) |
| 11364 | return PyBool_FromLong( |
| 11365 | Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, 0))); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11366 | |
| 11367 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11368 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11369 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11370 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11371 | for (i = 0; i < length; i++) { |
| 11372 | if (!Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11373 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11374 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11375 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11376 | } |
| 11377 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11378 | PyDoc_STRVAR(isalnum__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11379 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11380 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11381 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11382 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11383 | |
| 11384 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11385 | unicode_isalnum(PyObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11386 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11387 | int kind; |
| 11388 | void *data; |
| 11389 | Py_ssize_t len, i; |
| 11390 | |
| 11391 | if (PyUnicode_READY(self) == -1) |
| 11392 | return NULL; |
| 11393 | |
| 11394 | kind = PyUnicode_KIND(self); |
| 11395 | data = PyUnicode_DATA(self); |
| 11396 | len = PyUnicode_GET_LENGTH(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11397 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11398 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11399 | if (len == 1) { |
| 11400 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11401 | return PyBool_FromLong(Py_UNICODE_ISALNUM(ch)); |
| 11402 | } |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11403 | |
| 11404 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11405 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11406 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11407 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11408 | for (i = 0; i < len; i++) { |
| 11409 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11410 | if (!Py_UNICODE_ISALNUM(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11411 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11412 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11413 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11414 | } |
| 11415 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11416 | PyDoc_STRVAR(isdecimal__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11417 | "S.isdecimal() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11418 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11419 | 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] | 11420 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11421 | |
| 11422 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11423 | unicode_isdecimal(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11424 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11425 | Py_ssize_t i, length; |
| 11426 | int kind; |
| 11427 | void *data; |
| 11428 | |
| 11429 | if (PyUnicode_READY(self) == -1) |
| 11430 | return NULL; |
| 11431 | length = PyUnicode_GET_LENGTH(self); |
| 11432 | kind = PyUnicode_KIND(self); |
| 11433 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11434 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11435 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11436 | if (length == 1) |
| 11437 | return PyBool_FromLong( |
| 11438 | Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11439 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11440 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11441 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11442 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11443 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11444 | for (i = 0; i < length; i++) { |
| 11445 | if (!Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11446 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11447 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11448 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11449 | } |
| 11450 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11451 | PyDoc_STRVAR(isdigit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11452 | "S.isdigit() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11453 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11454 | Return True if all characters in S are digits\n\ |
| 11455 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11456 | |
| 11457 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11458 | unicode_isdigit(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11459 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11460 | Py_ssize_t i, length; |
| 11461 | int kind; |
| 11462 | void *data; |
| 11463 | |
| 11464 | if (PyUnicode_READY(self) == -1) |
| 11465 | return NULL; |
| 11466 | length = PyUnicode_GET_LENGTH(self); |
| 11467 | kind = PyUnicode_KIND(self); |
| 11468 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11469 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11470 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11471 | if (length == 1) { |
| 11472 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11473 | return PyBool_FromLong(Py_UNICODE_ISDIGIT(ch)); |
| 11474 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11475 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11476 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11477 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11478 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11479 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11480 | for (i = 0; i < length; i++) { |
| 11481 | if (!Py_UNICODE_ISDIGIT(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11482 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11483 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11484 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11485 | } |
| 11486 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11487 | PyDoc_STRVAR(isnumeric__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11488 | "S.isnumeric() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11489 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11490 | 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] | 11491 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11492 | |
| 11493 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11494 | unicode_isnumeric(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11495 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11496 | Py_ssize_t i, length; |
| 11497 | int kind; |
| 11498 | void *data; |
| 11499 | |
| 11500 | if (PyUnicode_READY(self) == -1) |
| 11501 | return NULL; |
| 11502 | length = PyUnicode_GET_LENGTH(self); |
| 11503 | kind = PyUnicode_KIND(self); |
| 11504 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11505 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11506 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11507 | if (length == 1) |
| 11508 | return PyBool_FromLong( |
| 11509 | Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11510 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11511 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11512 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11513 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11514 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11515 | for (i = 0; i < length; i++) { |
| 11516 | if (!Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11517 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11518 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11519 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11520 | } |
| 11521 | |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11522 | int |
| 11523 | PyUnicode_IsIdentifier(PyObject *self) |
| 11524 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11525 | int kind; |
| 11526 | void *data; |
| 11527 | Py_ssize_t i; |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11528 | Py_UCS4 first; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11529 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11530 | if (PyUnicode_READY(self) == -1) { |
| 11531 | Py_FatalError("identifier not ready"); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11532 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11533 | } |
| 11534 | |
| 11535 | /* Special case for empty strings */ |
| 11536 | if (PyUnicode_GET_LENGTH(self) == 0) |
| 11537 | return 0; |
| 11538 | kind = PyUnicode_KIND(self); |
| 11539 | data = PyUnicode_DATA(self); |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11540 | |
| 11541 | /* PEP 3131 says that the first character must be in |
| 11542 | XID_Start and subsequent characters in XID_Continue, |
| 11543 | and for the ASCII range, the 2.x rules apply (i.e |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11544 | start with letters and underscore, continue with |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11545 | letters, digits, underscore). However, given the current |
| 11546 | definition of XID_Start and XID_Continue, it is sufficient |
| 11547 | to check just for these, except that _ must be allowed |
| 11548 | as starting an identifier. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11549 | first = PyUnicode_READ(kind, data, 0); |
Benjamin Peterson | f413b80 | 2011-08-12 22:17:18 -0500 | [diff] [blame] | 11550 | if (!_PyUnicode_IsXidStart(first) && first != 0x5F /* LOW LINE */) |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11551 | return 0; |
| 11552 | |
Benjamin Peterson | 9c6e6a0 | 2011-09-28 08:09:05 -0400 | [diff] [blame] | 11553 | for (i = 1; i < PyUnicode_GET_LENGTH(self); i++) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11554 | if (!_PyUnicode_IsXidContinue(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11555 | return 0; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11556 | return 1; |
| 11557 | } |
| 11558 | |
| 11559 | PyDoc_STRVAR(isidentifier__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11560 | "S.isidentifier() -> bool\n\ |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11561 | \n\ |
| 11562 | Return True if S is a valid identifier according\n\ |
| 11563 | to the language definition."); |
| 11564 | |
| 11565 | static PyObject* |
| 11566 | unicode_isidentifier(PyObject *self) |
| 11567 | { |
| 11568 | return PyBool_FromLong(PyUnicode_IsIdentifier(self)); |
| 11569 | } |
| 11570 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11571 | PyDoc_STRVAR(isprintable__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11572 | "S.isprintable() -> bool\n\ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11573 | \n\ |
| 11574 | Return True if all characters in S are considered\n\ |
| 11575 | printable in repr() or S is empty, False otherwise."); |
| 11576 | |
| 11577 | static PyObject* |
| 11578 | unicode_isprintable(PyObject *self) |
| 11579 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11580 | Py_ssize_t i, length; |
| 11581 | int kind; |
| 11582 | void *data; |
| 11583 | |
| 11584 | if (PyUnicode_READY(self) == -1) |
| 11585 | return NULL; |
| 11586 | length = PyUnicode_GET_LENGTH(self); |
| 11587 | kind = PyUnicode_KIND(self); |
| 11588 | data = PyUnicode_DATA(self); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11589 | |
| 11590 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11591 | if (length == 1) |
| 11592 | return PyBool_FromLong( |
| 11593 | Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, 0))); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11594 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11595 | for (i = 0; i < length; i++) { |
| 11596 | if (!Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, i))) { |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11597 | Py_RETURN_FALSE; |
| 11598 | } |
| 11599 | } |
| 11600 | Py_RETURN_TRUE; |
| 11601 | } |
| 11602 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11603 | PyDoc_STRVAR(join__doc__, |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 11604 | "S.join(iterable) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11605 | \n\ |
| 11606 | 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] | 11607 | iterable. The separator between elements is S."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11608 | |
| 11609 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11610 | unicode_join(PyObject *self, PyObject *data) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11611 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11612 | return PyUnicode_Join(self, data); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11613 | } |
| 11614 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11615 | static Py_ssize_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11616 | unicode_length(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11617 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11618 | if (PyUnicode_READY(self) == -1) |
| 11619 | return -1; |
| 11620 | return PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11621 | } |
| 11622 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11623 | PyDoc_STRVAR(ljust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11624 | "S.ljust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11625 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 11626 | 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] | 11627 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11628 | |
| 11629 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11630 | unicode_ljust(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11631 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11632 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11633 | Py_UCS4 fillchar = ' '; |
| 11634 | |
| 11635 | if (PyUnicode_READY(self) == -1) |
| 11636 | return NULL; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 11637 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11638 | if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11639 | return NULL; |
| 11640 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11641 | if (_PyUnicode_LENGTH(self) >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11642 | Py_INCREF(self); |
| 11643 | return (PyObject*) self; |
| 11644 | } |
| 11645 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11646 | return (PyObject*) pad(self, 0, width - _PyUnicode_LENGTH(self), fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11647 | } |
| 11648 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11649 | PyDoc_STRVAR(lower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11650 | "S.lower() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11651 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11652 | Return a copy of the string S converted to lowercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11653 | |
| 11654 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11655 | unicode_lower(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11656 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11657 | return fixup(self, fixlower); |
| 11658 | } |
| 11659 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11660 | #define LEFTSTRIP 0 |
| 11661 | #define RIGHTSTRIP 1 |
| 11662 | #define BOTHSTRIP 2 |
| 11663 | |
| 11664 | /* Arrays indexed by above */ |
| 11665 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 11666 | |
| 11667 | #define STRIPNAME(i) (stripformat[i]+3) |
| 11668 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11669 | /* externally visible for str.strip(unicode) */ |
| 11670 | PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11671 | _PyUnicode_XStrip(PyObject *self, int striptype, PyObject *sepobj) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11672 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11673 | void *data; |
| 11674 | int kind; |
| 11675 | Py_ssize_t i, j, len; |
| 11676 | BLOOM_MASK sepmask; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11677 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11678 | if (PyUnicode_READY(self) == -1 || PyUnicode_READY(sepobj) == -1) |
| 11679 | return NULL; |
| 11680 | |
| 11681 | kind = PyUnicode_KIND(self); |
| 11682 | data = PyUnicode_DATA(self); |
| 11683 | len = PyUnicode_GET_LENGTH(self); |
| 11684 | sepmask = make_bloom_mask(PyUnicode_KIND(sepobj), |
| 11685 | PyUnicode_DATA(sepobj), |
| 11686 | PyUnicode_GET_LENGTH(sepobj)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11687 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11688 | i = 0; |
| 11689 | if (striptype != RIGHTSTRIP) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11690 | while (i < len && |
| 11691 | BLOOM_MEMBER(sepmask, PyUnicode_READ(kind, data, i), sepobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11692 | i++; |
| 11693 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11694 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11695 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11696 | j = len; |
| 11697 | if (striptype != LEFTSTRIP) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11698 | do { |
| 11699 | j--; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11700 | } while (j >= i && |
| 11701 | BLOOM_MEMBER(sepmask, PyUnicode_READ(kind, data, j), sepobj)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11702 | j++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11703 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11704 | |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11705 | return PyUnicode_Substring((PyObject*)self, i, j); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11706 | } |
| 11707 | |
| 11708 | PyObject* |
| 11709 | PyUnicode_Substring(PyObject *self, Py_ssize_t start, Py_ssize_t end) |
| 11710 | { |
| 11711 | unsigned char *data; |
| 11712 | int kind; |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11713 | Py_ssize_t length; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11714 | |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 11715 | if (PyUnicode_READY(self) == -1) |
| 11716 | return NULL; |
| 11717 | |
| 11718 | end = Py_MIN(end, PyUnicode_GET_LENGTH(self)); |
| 11719 | |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11720 | if (start == 0 && end == PyUnicode_GET_LENGTH(self)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11721 | { |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11722 | if (PyUnicode_CheckExact(self)) { |
| 11723 | Py_INCREF(self); |
| 11724 | return self; |
| 11725 | } |
| 11726 | else |
| 11727 | return PyUnicode_Copy(self); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11728 | } |
| 11729 | |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11730 | length = end - start; |
| 11731 | if (length == 1) |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 11732 | return unicode_getitem(self, start); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11733 | |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 11734 | if (start < 0 || end < 0) { |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11735 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 11736 | return NULL; |
| 11737 | } |
| 11738 | |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 11739 | if (PyUnicode_IS_ASCII(self)) { |
| 11740 | kind = PyUnicode_KIND(self); |
| 11741 | data = PyUnicode_1BYTE_DATA(self); |
| 11742 | return unicode_fromascii(data + start, length); |
| 11743 | } |
| 11744 | else { |
| 11745 | kind = PyUnicode_KIND(self); |
| 11746 | data = PyUnicode_1BYTE_DATA(self); |
| 11747 | return PyUnicode_FromKindAndData(kind, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 11748 | data + kind * start, |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 11749 | length); |
| 11750 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11751 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11752 | |
| 11753 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11754 | do_strip(PyObject *self, int striptype) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11755 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11756 | int kind; |
| 11757 | void *data; |
| 11758 | Py_ssize_t len, i, j; |
| 11759 | |
| 11760 | if (PyUnicode_READY(self) == -1) |
| 11761 | return NULL; |
| 11762 | |
| 11763 | kind = PyUnicode_KIND(self); |
| 11764 | data = PyUnicode_DATA(self); |
| 11765 | len = PyUnicode_GET_LENGTH(self); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11766 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11767 | i = 0; |
| 11768 | if (striptype != RIGHTSTRIP) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11769 | while (i < len && Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, i))) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11770 | i++; |
| 11771 | } |
| 11772 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11773 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11774 | j = len; |
| 11775 | if (striptype != LEFTSTRIP) { |
| 11776 | do { |
| 11777 | j--; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11778 | } while (j >= i && Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, j))); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11779 | j++; |
| 11780 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11781 | |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11782 | return PyUnicode_Substring((PyObject*)self, i, j); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11783 | } |
| 11784 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11785 | |
| 11786 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11787 | do_argstrip(PyObject *self, int striptype, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11788 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11789 | PyObject *sep = NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11790 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11791 | if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) |
| 11792 | return NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11793 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11794 | if (sep != NULL && sep != Py_None) { |
| 11795 | if (PyUnicode_Check(sep)) |
| 11796 | return _PyUnicode_XStrip(self, striptype, sep); |
| 11797 | else { |
| 11798 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11799 | "%s arg must be None or str", |
| 11800 | STRIPNAME(striptype)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11801 | return NULL; |
| 11802 | } |
| 11803 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11804 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11805 | return do_strip(self, striptype); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11806 | } |
| 11807 | |
| 11808 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11809 | PyDoc_STRVAR(strip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11810 | "S.strip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11811 | \n\ |
| 11812 | Return a copy of the string S with leading and trailing\n\ |
| 11813 | whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11814 | 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] | 11815 | |
| 11816 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11817 | unicode_strip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11818 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11819 | if (PyTuple_GET_SIZE(args) == 0) |
| 11820 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 11821 | else |
| 11822 | return do_argstrip(self, BOTHSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11823 | } |
| 11824 | |
| 11825 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11826 | PyDoc_STRVAR(lstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11827 | "S.lstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11828 | \n\ |
| 11829 | Return a copy of the string S with leading whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11830 | 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] | 11831 | |
| 11832 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11833 | unicode_lstrip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11834 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11835 | if (PyTuple_GET_SIZE(args) == 0) |
| 11836 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 11837 | else |
| 11838 | return do_argstrip(self, LEFTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11839 | } |
| 11840 | |
| 11841 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11842 | PyDoc_STRVAR(rstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11843 | "S.rstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11844 | \n\ |
| 11845 | Return a copy of the string S with trailing whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11846 | 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] | 11847 | |
| 11848 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11849 | unicode_rstrip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11850 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11851 | if (PyTuple_GET_SIZE(args) == 0) |
| 11852 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 11853 | else |
| 11854 | return do_argstrip(self, RIGHTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11855 | } |
| 11856 | |
| 11857 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11858 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11859 | unicode_repeat(PyObject *str, Py_ssize_t len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11860 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11861 | PyObject *u; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11862 | Py_ssize_t nchars, n; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11863 | |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 11864 | if (len < 1) { |
| 11865 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 11866 | return unicode_empty; |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 11867 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11868 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 11869 | if (len == 1 && PyUnicode_CheckExact(str)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11870 | /* no repeat, return original string */ |
| 11871 | Py_INCREF(str); |
| 11872 | return (PyObject*) str; |
| 11873 | } |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 11874 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11875 | if (PyUnicode_READY(str) == -1) |
| 11876 | return NULL; |
| 11877 | |
Victor Stinner | c759f3e | 2011-10-01 03:09:58 +0200 | [diff] [blame] | 11878 | if (PyUnicode_GET_LENGTH(str) > PY_SSIZE_T_MAX / len) { |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11879 | PyErr_SetString(PyExc_OverflowError, |
| 11880 | "repeated string is too long"); |
| 11881 | return NULL; |
| 11882 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11883 | nchars = len * PyUnicode_GET_LENGTH(str); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11884 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11885 | u = PyUnicode_New(nchars, PyUnicode_MAX_CHAR_VALUE(str)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11886 | if (!u) |
| 11887 | return NULL; |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11888 | assert(PyUnicode_KIND(u) == PyUnicode_KIND(str)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11889 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11890 | if (PyUnicode_GET_LENGTH(str) == 1) { |
| 11891 | const int kind = PyUnicode_KIND(str); |
| 11892 | const Py_UCS4 fill_char = PyUnicode_READ(kind, PyUnicode_DATA(str), 0); |
| 11893 | void *to = PyUnicode_DATA(u); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11894 | if (kind == PyUnicode_1BYTE_KIND) |
| 11895 | memset(to, (unsigned char)fill_char, len); |
| 11896 | else { |
| 11897 | for (n = 0; n < len; ++n) |
| 11898 | PyUnicode_WRITE(kind, to, n, fill_char); |
| 11899 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11900 | } |
| 11901 | else { |
| 11902 | /* number of characters copied this far */ |
| 11903 | Py_ssize_t done = PyUnicode_GET_LENGTH(str); |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 11904 | const Py_ssize_t char_size = PyUnicode_KIND(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11905 | char *to = (char *) PyUnicode_DATA(u); |
| 11906 | Py_MEMCPY(to, PyUnicode_DATA(str), |
| 11907 | PyUnicode_GET_LENGTH(str) * char_size); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11908 | while (done < nchars) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11909 | n = (done <= nchars-done) ? done : nchars-done; |
| 11910 | Py_MEMCPY(to + (done * char_size), to, n * char_size); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11911 | done += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11912 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11913 | } |
| 11914 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 11915 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11916 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11917 | } |
| 11918 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 11919 | PyObject * |
| 11920 | PyUnicode_Replace(PyObject *obj, |
| 11921 | PyObject *subobj, |
| 11922 | PyObject *replobj, |
| 11923 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11924 | { |
| 11925 | PyObject *self; |
| 11926 | PyObject *str1; |
| 11927 | PyObject *str2; |
| 11928 | PyObject *result; |
| 11929 | |
| 11930 | self = PyUnicode_FromObject(obj); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11931 | if (self == NULL || PyUnicode_READY(self) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11932 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11933 | str1 = PyUnicode_FromObject(subobj); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11934 | if (str1 == NULL || PyUnicode_READY(str1) == -1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11935 | Py_DECREF(self); |
| 11936 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11937 | } |
| 11938 | str2 = PyUnicode_FromObject(replobj); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11939 | if (str2 == NULL || PyUnicode_READY(str2)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11940 | Py_DECREF(self); |
| 11941 | Py_DECREF(str1); |
| 11942 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11943 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11944 | result = replace(self, str1, str2, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11945 | Py_DECREF(self); |
| 11946 | Py_DECREF(str1); |
| 11947 | Py_DECREF(str2); |
| 11948 | return result; |
| 11949 | } |
| 11950 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11951 | PyDoc_STRVAR(replace__doc__, |
Ezio Melotti | c1897e7 | 2010-06-26 18:50:39 +0000 | [diff] [blame] | 11952 | "S.replace(old, new[, count]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11953 | \n\ |
| 11954 | Return a copy of S with all occurrences of substring\n\ |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 11955 | old replaced by new. If the optional argument count is\n\ |
| 11956 | given, only the first count occurrences are replaced."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11957 | |
| 11958 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11959 | unicode_replace(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11960 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11961 | PyObject *str1; |
| 11962 | PyObject *str2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11963 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11964 | PyObject *result; |
| 11965 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11966 | if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11967 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11968 | if (!PyUnicode_READY(self) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11969 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11970 | str1 = PyUnicode_FromObject(str1); |
| 11971 | if (str1 == NULL || PyUnicode_READY(str1) == -1) |
| 11972 | return NULL; |
| 11973 | str2 = PyUnicode_FromObject(str2); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11974 | if (str2 == NULL || PyUnicode_READY(str2) == -1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11975 | Py_DECREF(str1); |
| 11976 | return NULL; |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 11977 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11978 | |
| 11979 | result = replace(self, str1, str2, maxcount); |
| 11980 | |
| 11981 | Py_DECREF(str1); |
| 11982 | Py_DECREF(str2); |
| 11983 | return result; |
| 11984 | } |
| 11985 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 11986 | static PyObject * |
| 11987 | unicode_repr(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11988 | { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11989 | PyObject *repr; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11990 | Py_ssize_t isize; |
| 11991 | Py_ssize_t osize, squote, dquote, i, o; |
| 11992 | Py_UCS4 max, quote; |
| 11993 | int ikind, okind; |
| 11994 | void *idata, *odata; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11995 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11996 | if (PyUnicode_READY(unicode) == -1) |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11997 | return NULL; |
| 11998 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11999 | isize = PyUnicode_GET_LENGTH(unicode); |
| 12000 | idata = PyUnicode_DATA(unicode); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12001 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12002 | /* Compute length of output, quote characters, and |
| 12003 | maximum character */ |
| 12004 | osize = 2; /* quotes */ |
| 12005 | max = 127; |
| 12006 | squote = dquote = 0; |
| 12007 | ikind = PyUnicode_KIND(unicode); |
| 12008 | for (i = 0; i < isize; i++) { |
| 12009 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
| 12010 | switch (ch) { |
| 12011 | case '\'': squote++; osize++; break; |
| 12012 | case '"': dquote++; osize++; break; |
| 12013 | case '\\': case '\t': case '\r': case '\n': |
| 12014 | osize += 2; break; |
| 12015 | default: |
| 12016 | /* Fast-path ASCII */ |
| 12017 | if (ch < ' ' || ch == 0x7f) |
| 12018 | osize += 4; /* \xHH */ |
| 12019 | else if (ch < 0x7f) |
| 12020 | osize++; |
| 12021 | else if (Py_UNICODE_ISPRINTABLE(ch)) { |
| 12022 | osize++; |
| 12023 | max = ch > max ? ch : max; |
| 12024 | } |
| 12025 | else if (ch < 0x100) |
| 12026 | osize += 4; /* \xHH */ |
| 12027 | else if (ch < 0x10000) |
| 12028 | osize += 6; /* \uHHHH */ |
| 12029 | else |
| 12030 | osize += 10; /* \uHHHHHHHH */ |
| 12031 | } |
| 12032 | } |
| 12033 | |
| 12034 | quote = '\''; |
| 12035 | if (squote) { |
| 12036 | if (dquote) |
| 12037 | /* Both squote and dquote present. Use squote, |
| 12038 | and escape them */ |
| 12039 | osize += squote; |
| 12040 | else |
| 12041 | quote = '"'; |
| 12042 | } |
| 12043 | |
| 12044 | repr = PyUnicode_New(osize, max); |
| 12045 | if (repr == NULL) |
| 12046 | return NULL; |
| 12047 | okind = PyUnicode_KIND(repr); |
| 12048 | odata = PyUnicode_DATA(repr); |
| 12049 | |
| 12050 | PyUnicode_WRITE(okind, odata, 0, quote); |
| 12051 | PyUnicode_WRITE(okind, odata, osize-1, quote); |
| 12052 | |
| 12053 | for (i = 0, o = 1; i < isize; i++) { |
| 12054 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12055 | |
| 12056 | /* Escape quotes and backslashes */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12057 | if ((ch == quote) || (ch == '\\')) { |
| 12058 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12059 | PyUnicode_WRITE(okind, odata, o++, ch); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12060 | continue; |
| 12061 | } |
| 12062 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12063 | /* Map special whitespace to '\t', \n', '\r' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12064 | if (ch == '\t') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12065 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12066 | PyUnicode_WRITE(okind, odata, o++, 't'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12067 | } |
| 12068 | else if (ch == '\n') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12069 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12070 | PyUnicode_WRITE(okind, odata, o++, 'n'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12071 | } |
| 12072 | else if (ch == '\r') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12073 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12074 | PyUnicode_WRITE(okind, odata, o++, 'r'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12075 | } |
| 12076 | |
| 12077 | /* Map non-printable US ASCII to '\xhh' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12078 | else if (ch < ' ' || ch == 0x7F) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12079 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12080 | PyUnicode_WRITE(okind, odata, o++, 'x'); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 12081 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0x000F]); |
| 12082 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0x000F]); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12083 | } |
| 12084 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12085 | /* Copy ASCII characters as-is */ |
| 12086 | else if (ch < 0x7F) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12087 | PyUnicode_WRITE(okind, odata, o++, ch); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12088 | } |
| 12089 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12090 | /* Non-ASCII characters */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12091 | else { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12092 | /* Map Unicode whitespace and control characters |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12093 | (categories Z* and C* except ASCII space) |
| 12094 | */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12095 | if (!Py_UNICODE_ISPRINTABLE(ch)) { |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12096 | /* Map 8-bit characters to '\xhh' */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12097 | if (ch <= 0xff) { |
| 12098 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12099 | PyUnicode_WRITE(okind, odata, o++, 'x'); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 12100 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0x000F]); |
| 12101 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0x000F]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12102 | } |
| 12103 | /* Map 21-bit characters to '\U00xxxxxx' */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12104 | else if (ch >= 0x10000) { |
| 12105 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12106 | PyUnicode_WRITE(okind, odata, o++, 'U'); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 12107 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 28) & 0xF]); |
| 12108 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 24) & 0xF]); |
| 12109 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 20) & 0xF]); |
| 12110 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 16) & 0xF]); |
| 12111 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 12) & 0xF]); |
| 12112 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 8) & 0xF]); |
| 12113 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0xF]); |
| 12114 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0xF]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12115 | } |
| 12116 | /* Map 16-bit characters to '\uxxxx' */ |
| 12117 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12118 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12119 | PyUnicode_WRITE(okind, odata, o++, 'u'); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 12120 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 12) & 0xF]); |
| 12121 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 8) & 0xF]); |
| 12122 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0xF]); |
| 12123 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0xF]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12124 | } |
| 12125 | } |
| 12126 | /* Copy characters as-is */ |
| 12127 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12128 | PyUnicode_WRITE(okind, odata, o++, ch); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12129 | } |
| 12130 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12131 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12132 | /* Closing quote already added at the beginning */ |
Victor Stinner | 05d1189 | 2011-10-06 01:13:58 +0200 | [diff] [blame] | 12133 | assert(_PyUnicode_CheckConsistency(repr, 1)); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12134 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12135 | } |
| 12136 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12137 | PyDoc_STRVAR(rfind__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12138 | "S.rfind(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12139 | \n\ |
| 12140 | Return the highest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 12141 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12142 | arguments start and end are interpreted as in slice notation.\n\ |
| 12143 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12144 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12145 | |
| 12146 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12147 | unicode_rfind(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12148 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12149 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 12150 | Py_ssize_t start; |
| 12151 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12152 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12153 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12154 | if (!stringlib_parse_args_finds_unicode("rfind", args, &substring, |
| 12155 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12156 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12157 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12158 | if (PyUnicode_READY(self) == -1) |
| 12159 | return NULL; |
| 12160 | if (PyUnicode_READY(substring) == -1) |
| 12161 | return NULL; |
| 12162 | |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 12163 | result = any_find_slice(-1, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12164 | self, (PyObject*)substring, start, end |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12165 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12166 | |
| 12167 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12168 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12169 | if (result == -2) |
| 12170 | return NULL; |
| 12171 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12172 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12173 | } |
| 12174 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12175 | PyDoc_STRVAR(rindex__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12176 | "S.rindex(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12177 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12178 | 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] | 12179 | |
| 12180 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12181 | unicode_rindex(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12182 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12183 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 12184 | Py_ssize_t start; |
| 12185 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12186 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12187 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12188 | if (!stringlib_parse_args_finds_unicode("rindex", args, &substring, |
| 12189 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12190 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12191 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12192 | if (PyUnicode_READY(self) == -1) |
| 12193 | return NULL; |
| 12194 | if (PyUnicode_READY(substring) == -1) |
| 12195 | return NULL; |
| 12196 | |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 12197 | result = any_find_slice(-1, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12198 | self, (PyObject*)substring, start, end |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12199 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12200 | |
| 12201 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12202 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12203 | if (result == -2) |
| 12204 | return NULL; |
| 12205 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12206 | if (result < 0) { |
| 12207 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 12208 | return NULL; |
| 12209 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12210 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12211 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12212 | } |
| 12213 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12214 | PyDoc_STRVAR(rjust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12215 | "S.rjust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12216 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 12217 | 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] | 12218 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12219 | |
| 12220 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12221 | unicode_rjust(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12222 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12223 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12224 | Py_UCS4 fillchar = ' '; |
| 12225 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 12226 | 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] | 12227 | return NULL; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 12228 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 12229 | if (PyUnicode_READY(self) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12230 | return NULL; |
| 12231 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12232 | if (_PyUnicode_LENGTH(self) >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12233 | Py_INCREF(self); |
| 12234 | return (PyObject*) self; |
| 12235 | } |
| 12236 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12237 | return (PyObject*) pad(self, width - _PyUnicode_LENGTH(self), 0, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12238 | } |
| 12239 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12240 | PyObject * |
| 12241 | PyUnicode_Split(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12242 | { |
| 12243 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 12244 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12245 | s = PyUnicode_FromObject(s); |
| 12246 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12247 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12248 | if (sep != NULL) { |
| 12249 | sep = PyUnicode_FromObject(sep); |
| 12250 | if (sep == NULL) { |
| 12251 | Py_DECREF(s); |
| 12252 | return NULL; |
| 12253 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12254 | } |
| 12255 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12256 | result = split(s, sep, maxsplit); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12257 | |
| 12258 | Py_DECREF(s); |
| 12259 | Py_XDECREF(sep); |
| 12260 | return result; |
| 12261 | } |
| 12262 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12263 | PyDoc_STRVAR(split__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12264 | "S.split([sep[, maxsplit]]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12265 | \n\ |
| 12266 | Return a list of the words in S, using sep as the\n\ |
| 12267 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 12268 | 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] | 12269 | whitespace string is a separator and empty strings are\n\ |
| 12270 | removed from the result."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12271 | |
| 12272 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12273 | unicode_split(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12274 | { |
| 12275 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12276 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12277 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12278 | if (!PyArg_ParseTuple(args, "|On:split", &substring, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12279 | return NULL; |
| 12280 | |
| 12281 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12282 | return split(self, NULL, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12283 | else if (PyUnicode_Check(substring)) |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12284 | return split(self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12285 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12286 | return PyUnicode_Split((PyObject *)self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12287 | } |
| 12288 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12289 | PyObject * |
| 12290 | PyUnicode_Partition(PyObject *str_in, PyObject *sep_in) |
| 12291 | { |
| 12292 | PyObject* str_obj; |
| 12293 | PyObject* sep_obj; |
| 12294 | PyObject* out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12295 | int kind1, kind2, kind; |
| 12296 | void *buf1 = NULL, *buf2 = NULL; |
| 12297 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12298 | |
| 12299 | str_obj = PyUnicode_FromObject(str_in); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 12300 | if (!str_obj || PyUnicode_READY(str_obj) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12301 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12302 | sep_obj = PyUnicode_FromObject(sep_in); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12303 | if (!sep_obj || PyUnicode_READY(sep_obj) == -1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12304 | Py_DECREF(str_obj); |
| 12305 | return NULL; |
| 12306 | } |
| 12307 | |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12308 | kind1 = PyUnicode_KIND(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12309 | kind2 = PyUnicode_KIND(sep_obj); |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12310 | kind = Py_MAX(kind1, kind2); |
| 12311 | buf1 = PyUnicode_DATA(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12312 | if (kind1 != kind) |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12313 | buf1 = _PyUnicode_AsKind(str_obj, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12314 | if (!buf1) |
| 12315 | goto onError; |
| 12316 | buf2 = PyUnicode_DATA(sep_obj); |
| 12317 | if (kind2 != kind) |
| 12318 | buf2 = _PyUnicode_AsKind(sep_obj, kind); |
| 12319 | if (!buf2) |
| 12320 | goto onError; |
| 12321 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 12322 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
| 12323 | |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12324 | switch(PyUnicode_KIND(str_obj)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12325 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 12326 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sep_obj)) |
| 12327 | out = asciilib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12328 | else |
| 12329 | 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] | 12330 | break; |
| 12331 | case PyUnicode_2BYTE_KIND: |
| 12332 | out = ucs2lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12333 | break; |
| 12334 | case PyUnicode_4BYTE_KIND: |
| 12335 | out = ucs4lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12336 | break; |
| 12337 | default: |
| 12338 | assert(0); |
| 12339 | out = 0; |
| 12340 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12341 | |
| 12342 | Py_DECREF(sep_obj); |
| 12343 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12344 | if (kind1 != kind) |
| 12345 | PyMem_Free(buf1); |
| 12346 | if (kind2 != kind) |
| 12347 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12348 | |
| 12349 | return out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12350 | onError: |
| 12351 | Py_DECREF(sep_obj); |
| 12352 | Py_DECREF(str_obj); |
| 12353 | if (kind1 != kind && buf1) |
| 12354 | PyMem_Free(buf1); |
| 12355 | if (kind2 != kind && buf2) |
| 12356 | PyMem_Free(buf2); |
| 12357 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12358 | } |
| 12359 | |
| 12360 | |
| 12361 | PyObject * |
| 12362 | PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in) |
| 12363 | { |
| 12364 | PyObject* str_obj; |
| 12365 | PyObject* sep_obj; |
| 12366 | PyObject* out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12367 | int kind1, kind2, kind; |
| 12368 | void *buf1 = NULL, *buf2 = NULL; |
| 12369 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12370 | |
| 12371 | str_obj = PyUnicode_FromObject(str_in); |
| 12372 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12373 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12374 | sep_obj = PyUnicode_FromObject(sep_in); |
| 12375 | if (!sep_obj) { |
| 12376 | Py_DECREF(str_obj); |
| 12377 | return NULL; |
| 12378 | } |
| 12379 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12380 | kind1 = PyUnicode_KIND(str_in); |
| 12381 | kind2 = PyUnicode_KIND(sep_obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 12382 | kind = Py_MAX(kind1, kind2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12383 | buf1 = PyUnicode_DATA(str_in); |
| 12384 | if (kind1 != kind) |
| 12385 | buf1 = _PyUnicode_AsKind(str_in, kind); |
| 12386 | if (!buf1) |
| 12387 | goto onError; |
| 12388 | buf2 = PyUnicode_DATA(sep_obj); |
| 12389 | if (kind2 != kind) |
| 12390 | buf2 = _PyUnicode_AsKind(sep_obj, kind); |
| 12391 | if (!buf2) |
| 12392 | goto onError; |
| 12393 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 12394 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
| 12395 | |
| 12396 | switch(PyUnicode_KIND(str_in)) { |
| 12397 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 12398 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sep_obj)) |
| 12399 | out = asciilib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12400 | else |
| 12401 | 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] | 12402 | break; |
| 12403 | case PyUnicode_2BYTE_KIND: |
| 12404 | out = ucs2lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12405 | break; |
| 12406 | case PyUnicode_4BYTE_KIND: |
| 12407 | out = ucs4lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12408 | break; |
| 12409 | default: |
| 12410 | assert(0); |
| 12411 | out = 0; |
| 12412 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12413 | |
| 12414 | Py_DECREF(sep_obj); |
| 12415 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12416 | if (kind1 != kind) |
| 12417 | PyMem_Free(buf1); |
| 12418 | if (kind2 != kind) |
| 12419 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12420 | |
| 12421 | return out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12422 | onError: |
| 12423 | Py_DECREF(sep_obj); |
| 12424 | Py_DECREF(str_obj); |
| 12425 | if (kind1 != kind && buf1) |
| 12426 | PyMem_Free(buf1); |
| 12427 | if (kind2 != kind && buf2) |
| 12428 | PyMem_Free(buf2); |
| 12429 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12430 | } |
| 12431 | |
| 12432 | PyDoc_STRVAR(partition__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12433 | "S.partition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12434 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 12435 | 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] | 12436 | 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] | 12437 | found, return S and two empty strings."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12438 | |
| 12439 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12440 | unicode_partition(PyObject *self, PyObject *separator) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12441 | { |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12442 | return PyUnicode_Partition(self, separator); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12443 | } |
| 12444 | |
| 12445 | PyDoc_STRVAR(rpartition__doc__, |
Ezio Melotti | 5b2b242 | 2010-01-25 11:58:28 +0000 | [diff] [blame] | 12446 | "S.rpartition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12447 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 12448 | 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] | 12449 | 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] | 12450 | separator is not found, return two empty strings and S."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12451 | |
| 12452 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12453 | unicode_rpartition(PyObject *self, PyObject *separator) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12454 | { |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12455 | return PyUnicode_RPartition(self, separator); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12456 | } |
| 12457 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12458 | PyObject * |
| 12459 | PyUnicode_RSplit(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12460 | { |
| 12461 | PyObject *result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12462 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12463 | s = PyUnicode_FromObject(s); |
| 12464 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12465 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12466 | if (sep != NULL) { |
| 12467 | sep = PyUnicode_FromObject(sep); |
| 12468 | if (sep == NULL) { |
| 12469 | Py_DECREF(s); |
| 12470 | return NULL; |
| 12471 | } |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12472 | } |
| 12473 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12474 | result = rsplit(s, sep, maxsplit); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12475 | |
| 12476 | Py_DECREF(s); |
| 12477 | Py_XDECREF(sep); |
| 12478 | return result; |
| 12479 | } |
| 12480 | |
| 12481 | PyDoc_STRVAR(rsplit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12482 | "S.rsplit([sep[, maxsplit]]) -> list of strings\n\ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12483 | \n\ |
| 12484 | Return a list of the words in S, using sep as the\n\ |
| 12485 | delimiter string, starting at the end of the string and\n\ |
| 12486 | working to the front. If maxsplit is given, at most maxsplit\n\ |
| 12487 | splits are done. If sep is not specified, any whitespace string\n\ |
| 12488 | is a separator."); |
| 12489 | |
| 12490 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12491 | unicode_rsplit(PyObject *self, PyObject *args) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12492 | { |
| 12493 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12494 | Py_ssize_t maxcount = -1; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12495 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12496 | if (!PyArg_ParseTuple(args, "|On:rsplit", &substring, &maxcount)) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12497 | return NULL; |
| 12498 | |
| 12499 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12500 | return rsplit(self, NULL, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12501 | else if (PyUnicode_Check(substring)) |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12502 | return rsplit(self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12503 | else |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12504 | return PyUnicode_RSplit(self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12505 | } |
| 12506 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12507 | PyDoc_STRVAR(splitlines__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12508 | "S.splitlines([keepends]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12509 | \n\ |
| 12510 | 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] | 12511 | 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] | 12512 | is given and true."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12513 | |
| 12514 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12515 | unicode_splitlines(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12516 | { |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12517 | static char *kwlist[] = {"keepends", 0}; |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 12518 | int keepends = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12519 | |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12520 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|i:splitlines", |
| 12521 | kwlist, &keepends)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12522 | return NULL; |
| 12523 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12524 | return PyUnicode_Splitlines(self, keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12525 | } |
| 12526 | |
| 12527 | static |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 12528 | PyObject *unicode_str(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12529 | { |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 12530 | if (PyUnicode_CheckExact(self)) { |
| 12531 | Py_INCREF(self); |
| 12532 | return self; |
| 12533 | } else |
| 12534 | /* Subtype -- return genuine unicode string with the same value. */ |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 12535 | return PyUnicode_Copy(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12536 | } |
| 12537 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12538 | PyDoc_STRVAR(swapcase__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12539 | "S.swapcase() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12540 | \n\ |
| 12541 | 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] | 12542 | and vice versa."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12543 | |
| 12544 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12545 | unicode_swapcase(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12546 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12547 | return fixup(self, fixswapcase); |
| 12548 | } |
| 12549 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12550 | PyDoc_STRVAR(maketrans__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12551 | "str.maketrans(x[, y[, z]]) -> dict (static method)\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12552 | \n\ |
| 12553 | Return a translation table usable for str.translate().\n\ |
| 12554 | If there is only one argument, it must be a dictionary mapping Unicode\n\ |
| 12555 | ordinals (integers) or characters to Unicode ordinals, strings or None.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12556 | Character keys will be then converted to ordinals.\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12557 | If there are two arguments, they must be strings of equal length, and\n\ |
| 12558 | in the resulting dictionary, each character in x will be mapped to the\n\ |
| 12559 | character at the same position in y. If there is a third argument, it\n\ |
| 12560 | must be a string, whose characters will be mapped to None in the result."); |
| 12561 | |
| 12562 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12563 | unicode_maketrans(PyObject *null, PyObject *args) |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12564 | { |
| 12565 | PyObject *x, *y = NULL, *z = NULL; |
| 12566 | PyObject *new = NULL, *key, *value; |
| 12567 | Py_ssize_t i = 0; |
| 12568 | int res; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12569 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12570 | if (!PyArg_ParseTuple(args, "O|UU:maketrans", &x, &y, &z)) |
| 12571 | return NULL; |
| 12572 | new = PyDict_New(); |
| 12573 | if (!new) |
| 12574 | return NULL; |
| 12575 | if (y != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12576 | int x_kind, y_kind, z_kind; |
| 12577 | void *x_data, *y_data, *z_data; |
| 12578 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12579 | /* x must be a string too, of equal length */ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12580 | if (!PyUnicode_Check(x)) { |
| 12581 | PyErr_SetString(PyExc_TypeError, "first maketrans argument must " |
| 12582 | "be a string if there is a second argument"); |
| 12583 | goto err; |
| 12584 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12585 | if (PyUnicode_GET_LENGTH(x) != PyUnicode_GET_LENGTH(y)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12586 | PyErr_SetString(PyExc_ValueError, "the first two maketrans " |
| 12587 | "arguments must have equal length"); |
| 12588 | goto err; |
| 12589 | } |
| 12590 | /* 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] | 12591 | x_kind = PyUnicode_KIND(x); |
| 12592 | y_kind = PyUnicode_KIND(y); |
| 12593 | x_data = PyUnicode_DATA(x); |
| 12594 | y_data = PyUnicode_DATA(y); |
| 12595 | for (i = 0; i < PyUnicode_GET_LENGTH(x); i++) { |
| 12596 | key = PyLong_FromLong(PyUnicode_READ(x_kind, x_data, i)); |
| 12597 | value = PyLong_FromLong(PyUnicode_READ(y_kind, y_data, i)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12598 | if (!key || !value) |
| 12599 | goto err; |
| 12600 | res = PyDict_SetItem(new, key, value); |
| 12601 | Py_DECREF(key); |
| 12602 | Py_DECREF(value); |
| 12603 | if (res < 0) |
| 12604 | goto err; |
| 12605 | } |
| 12606 | /* create entries for deleting chars in z */ |
| 12607 | if (z != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12608 | z_kind = PyUnicode_KIND(z); |
| 12609 | z_data = PyUnicode_DATA(z); |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 12610 | for (i = 0; i < PyUnicode_GET_LENGTH(z); i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12611 | key = PyLong_FromLong(PyUnicode_READ(z_kind, z_data, i)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12612 | if (!key) |
| 12613 | goto err; |
| 12614 | res = PyDict_SetItem(new, key, Py_None); |
| 12615 | Py_DECREF(key); |
| 12616 | if (res < 0) |
| 12617 | goto err; |
| 12618 | } |
| 12619 | } |
| 12620 | } else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12621 | int kind; |
| 12622 | void *data; |
| 12623 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12624 | /* x must be a dict */ |
Raymond Hettinger | 3ad0576 | 2009-05-29 22:11:22 +0000 | [diff] [blame] | 12625 | if (!PyDict_CheckExact(x)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12626 | PyErr_SetString(PyExc_TypeError, "if you give only one argument " |
| 12627 | "to maketrans it must be a dict"); |
| 12628 | goto err; |
| 12629 | } |
| 12630 | /* copy entries into the new dict, converting string keys to int keys */ |
| 12631 | while (PyDict_Next(x, &i, &key, &value)) { |
| 12632 | if (PyUnicode_Check(key)) { |
| 12633 | /* convert string keys to integer keys */ |
| 12634 | PyObject *newkey; |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 12635 | if (PyUnicode_GET_LENGTH(key) != 1) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12636 | PyErr_SetString(PyExc_ValueError, "string keys in translate " |
| 12637 | "table must be of length 1"); |
| 12638 | goto err; |
| 12639 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12640 | kind = PyUnicode_KIND(key); |
| 12641 | data = PyUnicode_DATA(key); |
| 12642 | newkey = PyLong_FromLong(PyUnicode_READ(kind, data, 0)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12643 | if (!newkey) |
| 12644 | goto err; |
| 12645 | res = PyDict_SetItem(new, newkey, value); |
| 12646 | Py_DECREF(newkey); |
| 12647 | if (res < 0) |
| 12648 | goto err; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12649 | } else if (PyLong_Check(key)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12650 | /* just keep integer keys */ |
| 12651 | if (PyDict_SetItem(new, key, value) < 0) |
| 12652 | goto err; |
| 12653 | } else { |
| 12654 | PyErr_SetString(PyExc_TypeError, "keys in translate table must " |
| 12655 | "be strings or integers"); |
| 12656 | goto err; |
| 12657 | } |
| 12658 | } |
| 12659 | } |
| 12660 | return new; |
| 12661 | err: |
| 12662 | Py_DECREF(new); |
| 12663 | return NULL; |
| 12664 | } |
| 12665 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12666 | PyDoc_STRVAR(translate__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12667 | "S.translate(table) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12668 | \n\ |
| 12669 | Return a copy of the string S, where all characters have been mapped\n\ |
| 12670 | through the given translation table, which must be a mapping of\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12671 | Unicode ordinals to Unicode ordinals, strings, or None.\n\ |
Walter Dörwald | 5c1ee17 | 2002-09-04 20:31:32 +0000 | [diff] [blame] | 12672 | Unmapped characters are left untouched. Characters mapped to None\n\ |
| 12673 | are deleted."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12674 | |
| 12675 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12676 | unicode_translate(PyObject *self, PyObject *table) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12677 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12678 | return _PyUnicode_TranslateCharmap(self, table, "ignore"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12679 | } |
| 12680 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12681 | PyDoc_STRVAR(upper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12682 | "S.upper() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12683 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12684 | Return a copy of S converted to uppercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12685 | |
| 12686 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12687 | unicode_upper(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12688 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12689 | return fixup(self, fixupper); |
| 12690 | } |
| 12691 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12692 | PyDoc_STRVAR(zfill__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12693 | "S.zfill(width) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12694 | \n\ |
Benjamin Peterson | 9aa4299 | 2008-09-10 21:57:34 +0000 | [diff] [blame] | 12695 | Pad a numeric string S with zeros on the left, to fill a field\n\ |
| 12696 | of the specified width. The string S is never truncated."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12697 | |
| 12698 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12699 | unicode_zfill(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12700 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12701 | Py_ssize_t fill; |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12702 | PyObject *u; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12703 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12704 | int kind; |
| 12705 | void *data; |
| 12706 | Py_UCS4 chr; |
| 12707 | |
| 12708 | if (PyUnicode_READY(self) == -1) |
| 12709 | return NULL; |
| 12710 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12711 | if (!PyArg_ParseTuple(args, "n:zfill", &width)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12712 | return NULL; |
| 12713 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12714 | if (PyUnicode_GET_LENGTH(self) >= width) { |
Walter Dörwald | 0fe940c | 2002-04-15 18:42:15 +0000 | [diff] [blame] | 12715 | if (PyUnicode_CheckExact(self)) { |
| 12716 | Py_INCREF(self); |
| 12717 | return (PyObject*) self; |
| 12718 | } |
| 12719 | else |
Victor Stinner | 2219e0a | 2011-10-01 01:16:59 +0200 | [diff] [blame] | 12720 | return PyUnicode_Copy((PyObject*)self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12721 | } |
| 12722 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12723 | fill = width - _PyUnicode_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12724 | |
| 12725 | u = pad(self, fill, 0, '0'); |
| 12726 | |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 12727 | if (u == NULL) |
| 12728 | return NULL; |
| 12729 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12730 | kind = PyUnicode_KIND(u); |
| 12731 | data = PyUnicode_DATA(u); |
| 12732 | chr = PyUnicode_READ(kind, data, fill); |
| 12733 | |
| 12734 | if (chr == '+' || chr == '-') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12735 | /* move sign to beginning of string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12736 | PyUnicode_WRITE(kind, data, 0, chr); |
| 12737 | PyUnicode_WRITE(kind, data, fill, '0'); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12738 | } |
| 12739 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12740 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12741 | return (PyObject*) u; |
| 12742 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12743 | |
| 12744 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12745 | static PyObject * |
| 12746 | unicode__decimal2ascii(PyObject *self) |
| 12747 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12748 | return PyUnicode_TransformDecimalAndSpaceToASCII(self); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12749 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12750 | #endif |
| 12751 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12752 | PyDoc_STRVAR(startswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12753 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12754 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 12755 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 12756 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12757 | With optional end, stop comparing S at that position.\n\ |
| 12758 | prefix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12759 | |
| 12760 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12761 | unicode_startswith(PyObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12762 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12763 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12764 | PyObject *subobj; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12765 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12766 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12767 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12768 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12769 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12770 | if (!stringlib_parse_args_finds("startswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12771 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12772 | if (PyTuple_Check(subobj)) { |
| 12773 | Py_ssize_t i; |
| 12774 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12775 | substring = PyUnicode_FromObject(PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12776 | if (substring == NULL) |
| 12777 | return NULL; |
| 12778 | result = tailmatch(self, substring, start, end, -1); |
| 12779 | Py_DECREF(substring); |
| 12780 | if (result) { |
| 12781 | Py_RETURN_TRUE; |
| 12782 | } |
| 12783 | } |
| 12784 | /* nothing matched */ |
| 12785 | Py_RETURN_FALSE; |
| 12786 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12787 | substring = PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12788 | if (substring == NULL) { |
| 12789 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 12790 | PyErr_Format(PyExc_TypeError, "startswith first arg must be str or " |
| 12791 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12792 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12793 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12794 | result = tailmatch(self, substring, start, end, -1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12795 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12796 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12797 | } |
| 12798 | |
| 12799 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12800 | PyDoc_STRVAR(endswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12801 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12802 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 12803 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 12804 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12805 | With optional end, stop comparing S at that position.\n\ |
| 12806 | suffix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12807 | |
| 12808 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12809 | unicode_endswith(PyObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12810 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12811 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12812 | PyObject *subobj; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12813 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12814 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12815 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12816 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12817 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12818 | if (!stringlib_parse_args_finds("endswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12819 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12820 | if (PyTuple_Check(subobj)) { |
| 12821 | Py_ssize_t i; |
| 12822 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12823 | substring = PyUnicode_FromObject( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12824 | PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12825 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12826 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12827 | result = tailmatch(self, substring, start, end, +1); |
| 12828 | Py_DECREF(substring); |
| 12829 | if (result) { |
| 12830 | Py_RETURN_TRUE; |
| 12831 | } |
| 12832 | } |
| 12833 | Py_RETURN_FALSE; |
| 12834 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12835 | substring = PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12836 | if (substring == NULL) { |
| 12837 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 12838 | PyErr_Format(PyExc_TypeError, "endswith first arg must be str or " |
| 12839 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12840 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12841 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12842 | result = tailmatch(self, substring, start, end, +1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12843 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12844 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12845 | } |
| 12846 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12847 | #include "stringlib/unicode_format.h" |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12848 | |
| 12849 | PyDoc_STRVAR(format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12850 | "S.format(*args, **kwargs) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12851 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 12852 | Return a formatted version of S, using substitutions from args and kwargs.\n\ |
| 12853 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12854 | |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 12855 | PyDoc_STRVAR(format_map__doc__, |
| 12856 | "S.format_map(mapping) -> str\n\ |
| 12857 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 12858 | Return a formatted version of S, using substitutions from mapping.\n\ |
| 12859 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 12860 | |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 12861 | static PyObject * |
| 12862 | unicode__format__(PyObject* self, PyObject* args) |
| 12863 | { |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12864 | PyObject *format_spec, *out; |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 12865 | |
| 12866 | if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) |
| 12867 | return NULL; |
| 12868 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12869 | out = _PyUnicode_FormatAdvanced(self, format_spec, 0, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12870 | PyUnicode_GET_LENGTH(format_spec)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12871 | return out; |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 12872 | } |
| 12873 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12874 | PyDoc_STRVAR(p_format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12875 | "S.__format__(format_spec) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12876 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 12877 | Return a formatted version of S as described by format_spec."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12878 | |
| 12879 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12880 | unicode__sizeof__(PyObject *v) |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 12881 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12882 | Py_ssize_t size; |
| 12883 | |
| 12884 | /* If it's a compact object, account for base structure + |
| 12885 | character data. */ |
| 12886 | if (PyUnicode_IS_COMPACT_ASCII(v)) |
| 12887 | size = sizeof(PyASCIIObject) + PyUnicode_GET_LENGTH(v) + 1; |
| 12888 | else if (PyUnicode_IS_COMPACT(v)) |
| 12889 | size = sizeof(PyCompactUnicodeObject) + |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 12890 | (PyUnicode_GET_LENGTH(v) + 1) * PyUnicode_KIND(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12891 | else { |
| 12892 | /* If it is a two-block object, account for base object, and |
| 12893 | for character block if present. */ |
| 12894 | size = sizeof(PyUnicodeObject); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 12895 | if (_PyUnicode_DATA_ANY(v)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12896 | size += (PyUnicode_GET_LENGTH(v) + 1) * |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 12897 | PyUnicode_KIND(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12898 | } |
| 12899 | /* 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] | 12900 | with the data pointer. Check if the data is not shared. */ |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 12901 | if (_PyUnicode_HAS_WSTR_MEMORY(v)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12902 | size += (PyUnicode_WSTR_LENGTH(v) + 1) * sizeof(wchar_t); |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 12903 | if (_PyUnicode_HAS_UTF8_MEMORY(v)) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 12904 | size += PyUnicode_UTF8_LENGTH(v) + 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12905 | |
| 12906 | return PyLong_FromSsize_t(size); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 12907 | } |
| 12908 | |
| 12909 | PyDoc_STRVAR(sizeof__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12910 | "S.__sizeof__() -> size of S in memory, in bytes"); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 12911 | |
| 12912 | static PyObject * |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 12913 | unicode_getnewargs(PyObject *v) |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 12914 | { |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 12915 | PyObject *copy = PyUnicode_Copy(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12916 | if (!copy) |
| 12917 | return NULL; |
| 12918 | return Py_BuildValue("(N)", copy); |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 12919 | } |
| 12920 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12921 | static PyMethodDef unicode_methods[] = { |
| 12922 | |
| 12923 | /* Order is according to common usage: often used methods should |
| 12924 | appear first, since lookup is done sequentially. */ |
| 12925 | |
Benjamin Peterson | 28a4dce | 2010-12-12 01:33:04 +0000 | [diff] [blame] | 12926 | {"encode", (PyCFunction) unicode_encode, METH_VARARGS | METH_KEYWORDS, encode__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12927 | {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, |
| 12928 | {"split", (PyCFunction) unicode_split, METH_VARARGS, split__doc__}, |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12929 | {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS, rsplit__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12930 | {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, |
| 12931 | {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, |
| 12932 | {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, |
| 12933 | {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, |
| 12934 | {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, |
| 12935 | {"expandtabs", (PyCFunction) unicode_expandtabs, METH_VARARGS, expandtabs__doc__}, |
| 12936 | {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12937 | {"partition", (PyCFunction) unicode_partition, METH_O, partition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12938 | {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, |
| 12939 | {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, |
| 12940 | {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12941 | {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12942 | {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, |
| 12943 | {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, |
| 12944 | {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12945 | {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12946 | {"rpartition", (PyCFunction) unicode_rpartition, METH_O, rpartition__doc__}, |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12947 | {"splitlines", (PyCFunction) unicode_splitlines, METH_VARARGS | METH_KEYWORDS, splitlines__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12948 | {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12949 | {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, |
| 12950 | {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, |
| 12951 | {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, |
| 12952 | {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, |
| 12953 | {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, |
| 12954 | {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, |
| 12955 | {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, |
| 12956 | {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, |
| 12957 | {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, |
| 12958 | {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, |
| 12959 | {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, |
| 12960 | {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, |
| 12961 | {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, |
| 12962 | {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 12963 | {"isidentifier", (PyCFunction) unicode_isidentifier, METH_NOARGS, isidentifier__doc__}, |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12964 | {"isprintable", (PyCFunction) unicode_isprintable, METH_NOARGS, isprintable__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12965 | {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, |
Eric Smith | 9cd1e09 | 2007-08-31 18:39:38 +0000 | [diff] [blame] | 12966 | {"format", (PyCFunction) do_string_format, METH_VARARGS | METH_KEYWORDS, format__doc__}, |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 12967 | {"format_map", (PyCFunction) do_string_format_map, METH_O, format_map__doc__}, |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 12968 | {"__format__", (PyCFunction) unicode__format__, METH_VARARGS, p_format__doc__}, |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12969 | {"maketrans", (PyCFunction) unicode_maketrans, |
| 12970 | METH_VARARGS | METH_STATIC, maketrans__doc__}, |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 12971 | {"__sizeof__", (PyCFunction) unicode__sizeof__, METH_NOARGS, sizeof__doc__}, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 12972 | #if 0 |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12973 | {"capwords", (PyCFunction) unicode_capwords, METH_NOARGS, capwords__doc__}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12974 | #endif |
| 12975 | |
| 12976 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12977 | /* These methods are just used for debugging the implementation. */ |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12978 | {"_decimal2ascii", (PyCFunction) unicode__decimal2ascii, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12979 | #endif |
| 12980 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12981 | {"__getnewargs__", (PyCFunction)unicode_getnewargs, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12982 | {NULL, NULL} |
| 12983 | }; |
| 12984 | |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 12985 | static PyObject * |
| 12986 | unicode_mod(PyObject *v, PyObject *w) |
| 12987 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 12988 | if (!PyUnicode_Check(v)) |
| 12989 | Py_RETURN_NOTIMPLEMENTED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12990 | return PyUnicode_Format(v, w); |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 12991 | } |
| 12992 | |
| 12993 | static PyNumberMethods unicode_as_number = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12994 | 0, /*nb_add*/ |
| 12995 | 0, /*nb_subtract*/ |
| 12996 | 0, /*nb_multiply*/ |
| 12997 | unicode_mod, /*nb_remainder*/ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 12998 | }; |
| 12999 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13000 | static PySequenceMethods unicode_as_sequence = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13001 | (lenfunc) unicode_length, /* sq_length */ |
| 13002 | PyUnicode_Concat, /* sq_concat */ |
| 13003 | (ssizeargfunc) unicode_repeat, /* sq_repeat */ |
| 13004 | (ssizeargfunc) unicode_getitem, /* sq_item */ |
| 13005 | 0, /* sq_slice */ |
| 13006 | 0, /* sq_ass_item */ |
| 13007 | 0, /* sq_ass_slice */ |
| 13008 | PyUnicode_Contains, /* sq_contains */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13009 | }; |
| 13010 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13011 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13012 | unicode_subscript(PyObject* self, PyObject* item) |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13013 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13014 | if (PyUnicode_READY(self) == -1) |
| 13015 | return NULL; |
| 13016 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 13017 | if (PyIndex_Check(item)) { |
| 13018 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13019 | if (i == -1 && PyErr_Occurred()) |
| 13020 | return NULL; |
| 13021 | if (i < 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13022 | i += PyUnicode_GET_LENGTH(self); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13023 | return unicode_getitem(self, i); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13024 | } else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13025 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13026 | PyObject *result; |
| 13027 | void *src_data, *dest_data; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13028 | int src_kind, dest_kind; |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 13029 | Py_UCS4 ch, max_char, kind_limit; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13030 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13031 | if (PySlice_GetIndicesEx(item, PyUnicode_GET_LENGTH(self), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13032 | &start, &stop, &step, &slicelength) < 0) { |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13033 | return NULL; |
| 13034 | } |
| 13035 | |
| 13036 | if (slicelength <= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13037 | return PyUnicode_New(0, 0); |
| 13038 | } else if (start == 0 && step == 1 && |
| 13039 | slicelength == PyUnicode_GET_LENGTH(self) && |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 13040 | PyUnicode_CheckExact(self)) { |
| 13041 | Py_INCREF(self); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13042 | return self; |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 13043 | } else if (step == 1) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13044 | return PyUnicode_Substring(self, |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 13045 | start, start + slicelength); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13046 | } |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13047 | /* General case */ |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13048 | src_kind = PyUnicode_KIND(self); |
| 13049 | src_data = PyUnicode_DATA(self); |
Victor Stinner | 55c9911 | 2011-10-13 01:17:06 +0200 | [diff] [blame] | 13050 | if (!PyUnicode_IS_ASCII(self)) { |
| 13051 | kind_limit = kind_maxchar_limit(src_kind); |
| 13052 | max_char = 0; |
| 13053 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
| 13054 | ch = PyUnicode_READ(src_kind, src_data, cur); |
| 13055 | if (ch > max_char) { |
| 13056 | max_char = ch; |
| 13057 | if (max_char >= kind_limit) |
| 13058 | break; |
| 13059 | } |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 13060 | } |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13061 | } |
Victor Stinner | 55c9911 | 2011-10-13 01:17:06 +0200 | [diff] [blame] | 13062 | else |
| 13063 | max_char = 127; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13064 | result = PyUnicode_New(slicelength, max_char); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13065 | if (result == NULL) |
| 13066 | return NULL; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13067 | dest_kind = PyUnicode_KIND(result); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13068 | dest_data = PyUnicode_DATA(result); |
| 13069 | |
| 13070 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13071 | Py_UCS4 ch = PyUnicode_READ(src_kind, src_data, cur); |
| 13072 | PyUnicode_WRITE(dest_kind, dest_data, i, ch); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13073 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13074 | assert(_PyUnicode_CheckConsistency(result, 1)); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13075 | return result; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13076 | } else { |
| 13077 | PyErr_SetString(PyExc_TypeError, "string indices must be integers"); |
| 13078 | return NULL; |
| 13079 | } |
| 13080 | } |
| 13081 | |
| 13082 | static PyMappingMethods unicode_as_mapping = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13083 | (lenfunc)unicode_length, /* mp_length */ |
| 13084 | (binaryfunc)unicode_subscript, /* mp_subscript */ |
| 13085 | (objobjargproc)0, /* mp_ass_subscript */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13086 | }; |
| 13087 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13088 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13089 | /* Helpers for PyUnicode_Format() */ |
| 13090 | |
| 13091 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13092 | 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] | 13093 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13094 | Py_ssize_t argidx = *p_argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13095 | if (argidx < arglen) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13096 | (*p_argidx)++; |
| 13097 | if (arglen < 0) |
| 13098 | return args; |
| 13099 | else |
| 13100 | return PyTuple_GetItem(args, argidx); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13101 | } |
| 13102 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13103 | "not enough arguments for format string"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13104 | return NULL; |
| 13105 | } |
| 13106 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13107 | /* 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] | 13108 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13109 | static PyObject * |
| 13110 | formatfloat(PyObject *v, int flags, int prec, int type) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13111 | { |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13112 | char *p; |
| 13113 | PyObject *result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13114 | double x; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 13115 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13116 | x = PyFloat_AsDouble(v); |
| 13117 | if (x == -1.0 && PyErr_Occurred()) |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13118 | return NULL; |
| 13119 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13120 | if (prec < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13121 | prec = 6; |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 13122 | |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 13123 | p = PyOS_double_to_string(x, type, prec, |
| 13124 | (flags & F_ALT) ? Py_DTSF_ALT : 0, NULL); |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13125 | if (p == NULL) |
| 13126 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13127 | result = PyUnicode_DecodeASCII(p, strlen(p), NULL); |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 13128 | PyMem_Free(p); |
| 13129 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13130 | } |
| 13131 | |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13132 | static PyObject* |
| 13133 | formatlong(PyObject *val, int flags, int prec, int type) |
| 13134 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13135 | char *buf; |
| 13136 | int len; |
| 13137 | PyObject *str; /* temporary string object. */ |
| 13138 | PyObject *result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13139 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13140 | str = _PyBytes_FormatLong(val, flags, prec, type, &buf, &len); |
| 13141 | if (!str) |
| 13142 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13143 | result = PyUnicode_DecodeASCII(buf, len, NULL); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13144 | Py_DECREF(str); |
| 13145 | return result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13146 | } |
| 13147 | |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13148 | static Py_UCS4 |
| 13149 | formatchar(PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13150 | { |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 13151 | /* presume that the buffer is at least 3 characters long */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 13152 | if (PyUnicode_Check(v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13153 | if (PyUnicode_GET_LENGTH(v) == 1) { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13154 | return PyUnicode_READ_CHAR(v, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13155 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13156 | goto onError; |
| 13157 | } |
| 13158 | else { |
| 13159 | /* Integer input truncated to a character */ |
| 13160 | long x; |
| 13161 | x = PyLong_AsLong(v); |
| 13162 | if (x == -1 && PyErr_Occurred()) |
| 13163 | goto onError; |
| 13164 | |
| 13165 | if (x < 0 || x > 0x10ffff) { |
| 13166 | PyErr_SetString(PyExc_OverflowError, |
| 13167 | "%c arg not in range(0x110000)"); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13168 | return (Py_UCS4) -1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13169 | } |
| 13170 | |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13171 | return (Py_UCS4) x; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13172 | } |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 13173 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13174 | onError: |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 13175 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13176 | "%c requires int or char"); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13177 | return (Py_UCS4) -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13178 | } |
| 13179 | |
Antoine Pitrou | 978b9d2 | 2011-10-07 12:35:48 +0200 | [diff] [blame] | 13180 | static int |
| 13181 | repeat_accumulate(_PyAccu *acc, PyObject *obj, Py_ssize_t count) |
| 13182 | { |
| 13183 | int r; |
| 13184 | assert(count > 0); |
| 13185 | assert(PyUnicode_Check(obj)); |
| 13186 | if (count > 5) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13187 | PyObject *repeated = unicode_repeat(obj, count); |
Antoine Pitrou | 978b9d2 | 2011-10-07 12:35:48 +0200 | [diff] [blame] | 13188 | if (repeated == NULL) |
| 13189 | return -1; |
| 13190 | r = _PyAccu_Accumulate(acc, repeated); |
| 13191 | Py_DECREF(repeated); |
| 13192 | return r; |
| 13193 | } |
| 13194 | else { |
| 13195 | do { |
| 13196 | if (_PyAccu_Accumulate(acc, obj)) |
| 13197 | return -1; |
| 13198 | } while (--count); |
| 13199 | return 0; |
| 13200 | } |
| 13201 | } |
| 13202 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 13203 | PyObject * |
| 13204 | PyUnicode_Format(PyObject *format, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13205 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13206 | void *fmt; |
| 13207 | int fmtkind; |
| 13208 | PyObject *result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13209 | int kind; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13210 | int r; |
| 13211 | Py_ssize_t fmtcnt, fmtpos, arglen, argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13212 | int args_owned = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13213 | PyObject *dict = NULL; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13214 | PyObject *temp = NULL; |
| 13215 | PyObject *second = NULL; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13216 | PyObject *uformat; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13217 | _PyAccu acc; |
| 13218 | static PyObject *plus, *minus, *blank, *zero, *percent; |
| 13219 | |
| 13220 | if (!plus && !(plus = get_latin1_char('+'))) |
| 13221 | return NULL; |
| 13222 | if (!minus && !(minus = get_latin1_char('-'))) |
| 13223 | return NULL; |
| 13224 | if (!blank && !(blank = get_latin1_char(' '))) |
| 13225 | return NULL; |
| 13226 | if (!zero && !(zero = get_latin1_char('0'))) |
| 13227 | return NULL; |
| 13228 | if (!percent && !(percent = get_latin1_char('%'))) |
| 13229 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 13230 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13231 | if (format == NULL || args == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13232 | PyErr_BadInternalCall(); |
| 13233 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13234 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13235 | uformat = PyUnicode_FromObject(format); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13236 | if (uformat == NULL || PyUnicode_READY(uformat) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13237 | return NULL; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13238 | if (_PyAccu_Init(&acc)) |
| 13239 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13240 | fmt = PyUnicode_DATA(uformat); |
| 13241 | fmtkind = PyUnicode_KIND(uformat); |
| 13242 | fmtcnt = PyUnicode_GET_LENGTH(uformat); |
| 13243 | fmtpos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13244 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13245 | if (PyTuple_Check(args)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13246 | arglen = PyTuple_Size(args); |
| 13247 | argidx = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13248 | } |
| 13249 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13250 | arglen = -1; |
| 13251 | argidx = -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13252 | } |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 13253 | if (Py_TYPE(args)->tp_as_mapping && !PyTuple_Check(args) && |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 13254 | !PyUnicode_Check(args)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13255 | dict = args; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13256 | |
| 13257 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13258 | if (PyUnicode_READ(fmtkind, fmt, fmtpos) != '%') { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13259 | PyObject *nonfmt; |
| 13260 | Py_ssize_t nonfmtpos; |
| 13261 | nonfmtpos = fmtpos++; |
| 13262 | while (fmtcnt >= 0 && |
| 13263 | PyUnicode_READ(fmtkind, fmt, fmtpos) != '%') { |
| 13264 | fmtpos++; |
| 13265 | fmtcnt--; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13266 | } |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13267 | nonfmt = PyUnicode_Substring((PyObject *) uformat, nonfmtpos, fmtpos); |
| 13268 | if (nonfmt == NULL) |
| 13269 | goto onError; |
| 13270 | r = _PyAccu_Accumulate(&acc, nonfmt); |
| 13271 | Py_DECREF(nonfmt); |
| 13272 | if (r) |
| 13273 | goto onError; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13274 | } |
| 13275 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13276 | /* Got a format specifier */ |
| 13277 | int flags = 0; |
| 13278 | Py_ssize_t width = -1; |
| 13279 | int prec = -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13280 | Py_UCS4 c = '\0'; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13281 | Py_UCS4 fill, sign; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13282 | int isnumok; |
| 13283 | PyObject *v = NULL; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13284 | void *pbuf = NULL; |
| 13285 | Py_ssize_t pindex, len; |
| 13286 | PyObject *signobj = NULL, *fillobj = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13287 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13288 | fmtpos++; |
| 13289 | if (PyUnicode_READ(fmtkind, fmt, fmtpos) == '(') { |
| 13290 | Py_ssize_t keystart; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13291 | Py_ssize_t keylen; |
| 13292 | PyObject *key; |
| 13293 | int pcount = 1; |
Christian Heimes | a612dc0 | 2008-02-24 13:08:18 +0000 | [diff] [blame] | 13294 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13295 | if (dict == NULL) { |
| 13296 | PyErr_SetString(PyExc_TypeError, |
| 13297 | "format requires a mapping"); |
| 13298 | goto onError; |
| 13299 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13300 | ++fmtpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13301 | --fmtcnt; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13302 | keystart = fmtpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13303 | /* Skip over balanced parentheses */ |
| 13304 | while (pcount > 0 && --fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13305 | if (PyUnicode_READ(fmtkind, fmt, fmtpos) == ')') |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13306 | --pcount; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13307 | else if (PyUnicode_READ(fmtkind, fmt, fmtpos) == '(') |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13308 | ++pcount; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13309 | fmtpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13310 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13311 | keylen = fmtpos - keystart - 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13312 | if (fmtcnt < 0 || pcount > 0) { |
| 13313 | PyErr_SetString(PyExc_ValueError, |
| 13314 | "incomplete format key"); |
| 13315 | goto onError; |
| 13316 | } |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 13317 | key = PyUnicode_Substring((PyObject*)uformat, |
| 13318 | keystart, keystart + keylen); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13319 | if (key == NULL) |
| 13320 | goto onError; |
| 13321 | if (args_owned) { |
| 13322 | Py_DECREF(args); |
| 13323 | args_owned = 0; |
| 13324 | } |
| 13325 | args = PyObject_GetItem(dict, key); |
| 13326 | Py_DECREF(key); |
| 13327 | if (args == NULL) { |
| 13328 | goto onError; |
| 13329 | } |
| 13330 | args_owned = 1; |
| 13331 | arglen = -1; |
| 13332 | argidx = -2; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13333 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13334 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13335 | switch (c = PyUnicode_READ(fmtkind, fmt, fmtpos++)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13336 | case '-': flags |= F_LJUST; continue; |
| 13337 | case '+': flags |= F_SIGN; continue; |
| 13338 | case ' ': flags |= F_BLANK; continue; |
| 13339 | case '#': flags |= F_ALT; continue; |
| 13340 | case '0': flags |= F_ZERO; continue; |
| 13341 | } |
| 13342 | break; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13343 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13344 | if (c == '*') { |
| 13345 | v = getnextarg(args, arglen, &argidx); |
| 13346 | if (v == NULL) |
| 13347 | goto onError; |
| 13348 | if (!PyLong_Check(v)) { |
| 13349 | PyErr_SetString(PyExc_TypeError, |
| 13350 | "* wants int"); |
| 13351 | goto onError; |
| 13352 | } |
| 13353 | width = PyLong_AsLong(v); |
| 13354 | if (width == -1 && PyErr_Occurred()) |
| 13355 | goto onError; |
| 13356 | if (width < 0) { |
| 13357 | flags |= F_LJUST; |
| 13358 | width = -width; |
| 13359 | } |
| 13360 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13361 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13362 | } |
| 13363 | else if (c >= '0' && c <= '9') { |
| 13364 | width = c - '0'; |
| 13365 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13366 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13367 | if (c < '0' || c > '9') |
| 13368 | break; |
| 13369 | if ((width*10) / 10 != width) { |
| 13370 | PyErr_SetString(PyExc_ValueError, |
| 13371 | "width too big"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13372 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13373 | } |
| 13374 | width = width*10 + (c - '0'); |
| 13375 | } |
| 13376 | } |
| 13377 | if (c == '.') { |
| 13378 | prec = 0; |
| 13379 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13380 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13381 | if (c == '*') { |
| 13382 | v = getnextarg(args, arglen, &argidx); |
| 13383 | if (v == NULL) |
| 13384 | goto onError; |
| 13385 | if (!PyLong_Check(v)) { |
| 13386 | PyErr_SetString(PyExc_TypeError, |
| 13387 | "* wants int"); |
| 13388 | goto onError; |
| 13389 | } |
| 13390 | prec = PyLong_AsLong(v); |
| 13391 | if (prec == -1 && PyErr_Occurred()) |
| 13392 | goto onError; |
| 13393 | if (prec < 0) |
| 13394 | prec = 0; |
| 13395 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13396 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13397 | } |
| 13398 | else if (c >= '0' && c <= '9') { |
| 13399 | prec = c - '0'; |
| 13400 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13401 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13402 | if (c < '0' || c > '9') |
| 13403 | break; |
| 13404 | if ((prec*10) / 10 != prec) { |
| 13405 | PyErr_SetString(PyExc_ValueError, |
| 13406 | "prec too big"); |
| 13407 | goto onError; |
| 13408 | } |
| 13409 | prec = prec*10 + (c - '0'); |
| 13410 | } |
| 13411 | } |
| 13412 | } /* prec */ |
| 13413 | if (fmtcnt >= 0) { |
| 13414 | if (c == 'h' || c == 'l' || c == 'L') { |
| 13415 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13416 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13417 | } |
| 13418 | } |
| 13419 | if (fmtcnt < 0) { |
| 13420 | PyErr_SetString(PyExc_ValueError, |
| 13421 | "incomplete format"); |
| 13422 | goto onError; |
| 13423 | } |
| 13424 | if (c != '%') { |
| 13425 | v = getnextarg(args, arglen, &argidx); |
| 13426 | if (v == NULL) |
| 13427 | goto onError; |
| 13428 | } |
| 13429 | sign = 0; |
| 13430 | fill = ' '; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13431 | fillobj = blank; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13432 | switch (c) { |
| 13433 | |
| 13434 | case '%': |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13435 | _PyAccu_Accumulate(&acc, percent); |
| 13436 | continue; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13437 | |
| 13438 | case 's': |
| 13439 | case 'r': |
| 13440 | case 'a': |
Victor Stinner | 808fc0a | 2010-03-22 12:50:40 +0000 | [diff] [blame] | 13441 | if (PyUnicode_CheckExact(v) && c == 's') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13442 | temp = v; |
| 13443 | Py_INCREF(temp); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13444 | } |
| 13445 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13446 | if (c == 's') |
| 13447 | temp = PyObject_Str(v); |
| 13448 | else if (c == 'r') |
| 13449 | temp = PyObject_Repr(v); |
| 13450 | else |
| 13451 | temp = PyObject_ASCII(v); |
| 13452 | if (temp == NULL) |
| 13453 | goto onError; |
| 13454 | if (PyUnicode_Check(temp)) |
| 13455 | /* nothing to do */; |
| 13456 | else { |
| 13457 | Py_DECREF(temp); |
| 13458 | PyErr_SetString(PyExc_TypeError, |
| 13459 | "%s argument has non-string str()"); |
| 13460 | goto onError; |
| 13461 | } |
| 13462 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13463 | if (PyUnicode_READY(temp) == -1) { |
| 13464 | Py_CLEAR(temp); |
| 13465 | goto onError; |
| 13466 | } |
| 13467 | pbuf = PyUnicode_DATA(temp); |
| 13468 | kind = PyUnicode_KIND(temp); |
| 13469 | len = PyUnicode_GET_LENGTH(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13470 | if (prec >= 0 && len > prec) |
| 13471 | len = prec; |
| 13472 | break; |
| 13473 | |
| 13474 | case 'i': |
| 13475 | case 'd': |
| 13476 | case 'u': |
| 13477 | case 'o': |
| 13478 | case 'x': |
| 13479 | case 'X': |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13480 | isnumok = 0; |
| 13481 | if (PyNumber_Check(v)) { |
| 13482 | PyObject *iobj=NULL; |
| 13483 | |
| 13484 | if (PyLong_Check(v)) { |
| 13485 | iobj = v; |
| 13486 | Py_INCREF(iobj); |
| 13487 | } |
| 13488 | else { |
| 13489 | iobj = PyNumber_Long(v); |
| 13490 | } |
| 13491 | if (iobj!=NULL) { |
| 13492 | if (PyLong_Check(iobj)) { |
| 13493 | isnumok = 1; |
Senthil Kumaran | 9ebe08d | 2011-07-03 21:03:16 -0700 | [diff] [blame] | 13494 | temp = formatlong(iobj, flags, prec, (c == 'i'? 'd': c)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13495 | Py_DECREF(iobj); |
| 13496 | if (!temp) |
| 13497 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13498 | if (PyUnicode_READY(temp) == -1) { |
| 13499 | Py_CLEAR(temp); |
| 13500 | goto onError; |
| 13501 | } |
| 13502 | pbuf = PyUnicode_DATA(temp); |
| 13503 | kind = PyUnicode_KIND(temp); |
| 13504 | len = PyUnicode_GET_LENGTH(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13505 | sign = 1; |
| 13506 | } |
| 13507 | else { |
| 13508 | Py_DECREF(iobj); |
| 13509 | } |
| 13510 | } |
| 13511 | } |
| 13512 | if (!isnumok) { |
| 13513 | PyErr_Format(PyExc_TypeError, |
| 13514 | "%%%c format: a number is required, " |
| 13515 | "not %.200s", (char)c, Py_TYPE(v)->tp_name); |
| 13516 | goto onError; |
| 13517 | } |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13518 | if (flags & F_ZERO) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13519 | fill = '0'; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13520 | fillobj = zero; |
| 13521 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13522 | break; |
| 13523 | |
| 13524 | case 'e': |
| 13525 | case 'E': |
| 13526 | case 'f': |
| 13527 | case 'F': |
| 13528 | case 'g': |
| 13529 | case 'G': |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13530 | temp = formatfloat(v, flags, prec, c); |
| 13531 | if (!temp) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13532 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13533 | if (PyUnicode_READY(temp) == -1) { |
| 13534 | Py_CLEAR(temp); |
| 13535 | goto onError; |
| 13536 | } |
| 13537 | pbuf = PyUnicode_DATA(temp); |
| 13538 | kind = PyUnicode_KIND(temp); |
| 13539 | len = PyUnicode_GET_LENGTH(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13540 | sign = 1; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13541 | if (flags & F_ZERO) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13542 | fill = '0'; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13543 | fillobj = zero; |
| 13544 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13545 | break; |
| 13546 | |
| 13547 | case 'c': |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13548 | { |
| 13549 | Py_UCS4 ch = formatchar(v); |
| 13550 | if (ch == (Py_UCS4) -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13551 | goto onError; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13552 | temp = _PyUnicode_FromUCS4(&ch, 1); |
| 13553 | if (temp == NULL) |
| 13554 | goto onError; |
| 13555 | pbuf = PyUnicode_DATA(temp); |
| 13556 | kind = PyUnicode_KIND(temp); |
| 13557 | len = PyUnicode_GET_LENGTH(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13558 | break; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13559 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13560 | |
| 13561 | default: |
| 13562 | PyErr_Format(PyExc_ValueError, |
| 13563 | "unsupported format character '%c' (0x%x) " |
| 13564 | "at index %zd", |
| 13565 | (31<=c && c<=126) ? (char)c : '?', |
| 13566 | (int)c, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13567 | fmtpos - 1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13568 | goto onError; |
| 13569 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13570 | /* pbuf is initialized here. */ |
| 13571 | pindex = 0; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13572 | if (sign) { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13573 | if (PyUnicode_READ(kind, pbuf, pindex) == '-') { |
| 13574 | signobj = minus; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13575 | len--; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13576 | pindex++; |
| 13577 | } |
| 13578 | else if (PyUnicode_READ(kind, pbuf, pindex) == '+') { |
| 13579 | signobj = plus; |
| 13580 | len--; |
| 13581 | pindex++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13582 | } |
| 13583 | else if (flags & F_SIGN) |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13584 | signobj = plus; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13585 | else if (flags & F_BLANK) |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13586 | signobj = blank; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13587 | else |
| 13588 | sign = 0; |
| 13589 | } |
| 13590 | if (width < len) |
| 13591 | width = len; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13592 | if (sign) { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13593 | if (fill != ' ') { |
| 13594 | assert(signobj != NULL); |
| 13595 | if (_PyAccu_Accumulate(&acc, signobj)) |
| 13596 | goto onError; |
| 13597 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13598 | if (width > len) |
| 13599 | width--; |
| 13600 | } |
| 13601 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13602 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13603 | assert(PyUnicode_READ(kind, pbuf, pindex + 1) == c); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13604 | if (fill != ' ') { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13605 | second = get_latin1_char( |
| 13606 | PyUnicode_READ(kind, pbuf, pindex + 1)); |
| 13607 | pindex += 2; |
| 13608 | if (second == NULL || |
| 13609 | _PyAccu_Accumulate(&acc, zero) || |
| 13610 | _PyAccu_Accumulate(&acc, second)) |
| 13611 | goto onError; |
| 13612 | Py_CLEAR(second); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13613 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13614 | width -= 2; |
| 13615 | if (width < 0) |
| 13616 | width = 0; |
| 13617 | len -= 2; |
| 13618 | } |
| 13619 | if (width > len && !(flags & F_LJUST)) { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13620 | assert(fillobj != NULL); |
Antoine Pitrou | 978b9d2 | 2011-10-07 12:35:48 +0200 | [diff] [blame] | 13621 | if (repeat_accumulate(&acc, fillobj, width - len)) |
| 13622 | goto onError; |
| 13623 | width = len; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13624 | } |
| 13625 | if (fill == ' ') { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13626 | if (sign) { |
| 13627 | assert(signobj != NULL); |
| 13628 | if (_PyAccu_Accumulate(&acc, signobj)) |
| 13629 | goto onError; |
| 13630 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13631 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13632 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
| 13633 | assert(PyUnicode_READ(kind, pbuf, pindex+1) == c); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13634 | second = get_latin1_char( |
| 13635 | PyUnicode_READ(kind, pbuf, pindex + 1)); |
| 13636 | pindex += 2; |
| 13637 | if (second == NULL || |
| 13638 | _PyAccu_Accumulate(&acc, zero) || |
| 13639 | _PyAccu_Accumulate(&acc, second)) |
| 13640 | goto onError; |
| 13641 | Py_CLEAR(second); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13642 | } |
| 13643 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13644 | /* Copy all characters, preserving len */ |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13645 | if (temp != NULL) { |
| 13646 | assert(pbuf == PyUnicode_DATA(temp)); |
| 13647 | v = PyUnicode_Substring(temp, pindex, pindex + len); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13648 | } |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13649 | else { |
| 13650 | const char *p = (const char *) pbuf; |
| 13651 | assert(pbuf != NULL); |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 13652 | p += kind * pindex; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13653 | v = PyUnicode_FromKindAndData(kind, p, len); |
| 13654 | } |
| 13655 | if (v == NULL) |
| 13656 | goto onError; |
| 13657 | r = _PyAccu_Accumulate(&acc, v); |
| 13658 | Py_DECREF(v); |
| 13659 | if (r) |
| 13660 | goto onError; |
Antoine Pitrou | 978b9d2 | 2011-10-07 12:35:48 +0200 | [diff] [blame] | 13661 | if (width > len && repeat_accumulate(&acc, blank, width - len)) |
| 13662 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13663 | if (dict && (argidx < arglen) && c != '%') { |
| 13664 | PyErr_SetString(PyExc_TypeError, |
| 13665 | "not all arguments converted during string formatting"); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13666 | goto onError; |
| 13667 | } |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13668 | Py_CLEAR(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13669 | } /* '%' */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13670 | } /* until end */ |
| 13671 | if (argidx < arglen && !dict) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13672 | PyErr_SetString(PyExc_TypeError, |
| 13673 | "not all arguments converted during string formatting"); |
| 13674 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13675 | } |
| 13676 | |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13677 | result = _PyAccu_Finish(&acc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13678 | if (args_owned) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13679 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13680 | } |
| 13681 | Py_DECREF(uformat); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13682 | Py_XDECREF(temp); |
| 13683 | Py_XDECREF(second); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13684 | return (PyObject *)result; |
| 13685 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13686 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13687 | Py_DECREF(uformat); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13688 | Py_XDECREF(temp); |
| 13689 | Py_XDECREF(second); |
| 13690 | _PyAccu_Destroy(&acc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13691 | if (args_owned) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13692 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13693 | } |
| 13694 | return NULL; |
| 13695 | } |
| 13696 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 13697 | static PyObject * |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 13698 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 13699 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13700 | static PyObject * |
| 13701 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 13702 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13703 | PyObject *x = NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13704 | static char *kwlist[] = {"object", "encoding", "errors", 0}; |
| 13705 | char *encoding = NULL; |
| 13706 | char *errors = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13707 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13708 | if (type != &PyUnicode_Type) |
| 13709 | return unicode_subtype_new(type, args, kwds); |
| 13710 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:str", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13711 | kwlist, &x, &encoding, &errors)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13712 | return NULL; |
| 13713 | if (x == NULL) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13714 | return (PyObject *)PyUnicode_New(0, 0); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13715 | if (encoding == NULL && errors == NULL) |
| 13716 | return PyObject_Str(x); |
| 13717 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13718 | return PyUnicode_FromEncodedObject(x, encoding, errors); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13719 | } |
| 13720 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 13721 | static PyObject * |
| 13722 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 13723 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13724 | PyObject *unicode, *self; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13725 | Py_ssize_t length, char_size; |
| 13726 | int share_wstr, share_utf8; |
| 13727 | unsigned int kind; |
| 13728 | void *data; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 13729 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13730 | assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13731 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13732 | unicode = unicode_new(&PyUnicode_Type, args, kwds); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13733 | if (unicode == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13734 | return NULL; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 13735 | assert(_PyUnicode_CHECK(unicode)); |
Victor Stinner | e06e145 | 2011-10-04 20:52:31 +0200 | [diff] [blame] | 13736 | if (PyUnicode_READY(unicode)) |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13737 | return NULL; |
| 13738 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13739 | self = type->tp_alloc(type, 0); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13740 | if (self == NULL) { |
| 13741 | Py_DECREF(unicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13742 | return NULL; |
| 13743 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13744 | kind = PyUnicode_KIND(unicode); |
| 13745 | length = PyUnicode_GET_LENGTH(unicode); |
| 13746 | |
| 13747 | _PyUnicode_LENGTH(self) = length; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 13748 | #ifdef Py_DEBUG |
| 13749 | _PyUnicode_HASH(self) = -1; |
| 13750 | #else |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13751 | _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 13752 | #endif |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13753 | _PyUnicode_STATE(self).interned = 0; |
| 13754 | _PyUnicode_STATE(self).kind = kind; |
| 13755 | _PyUnicode_STATE(self).compact = 0; |
Victor Stinner | 3cf4637 | 2011-10-03 14:42:15 +0200 | [diff] [blame] | 13756 | _PyUnicode_STATE(self).ascii = _PyUnicode_STATE(unicode).ascii; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13757 | _PyUnicode_STATE(self).ready = 1; |
| 13758 | _PyUnicode_WSTR(self) = NULL; |
| 13759 | _PyUnicode_UTF8_LENGTH(self) = 0; |
| 13760 | _PyUnicode_UTF8(self) = NULL; |
| 13761 | _PyUnicode_WSTR_LENGTH(self) = 0; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 13762 | _PyUnicode_DATA_ANY(self) = NULL; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13763 | |
| 13764 | share_utf8 = 0; |
| 13765 | share_wstr = 0; |
| 13766 | if (kind == PyUnicode_1BYTE_KIND) { |
| 13767 | char_size = 1; |
| 13768 | if (PyUnicode_MAX_CHAR_VALUE(unicode) < 128) |
| 13769 | share_utf8 = 1; |
| 13770 | } |
| 13771 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 13772 | char_size = 2; |
| 13773 | if (sizeof(wchar_t) == 2) |
| 13774 | share_wstr = 1; |
| 13775 | } |
| 13776 | else { |
| 13777 | assert(kind == PyUnicode_4BYTE_KIND); |
| 13778 | char_size = 4; |
| 13779 | if (sizeof(wchar_t) == 4) |
| 13780 | share_wstr = 1; |
| 13781 | } |
| 13782 | |
| 13783 | /* Ensure we won't overflow the length. */ |
| 13784 | if (length > (PY_SSIZE_T_MAX / char_size - 1)) { |
| 13785 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13786 | goto onError; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13787 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13788 | data = PyObject_MALLOC((length + 1) * char_size); |
| 13789 | if (data == NULL) { |
| 13790 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13791 | goto onError; |
| 13792 | } |
| 13793 | |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 13794 | _PyUnicode_DATA_ANY(self) = data; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13795 | if (share_utf8) { |
| 13796 | _PyUnicode_UTF8_LENGTH(self) = length; |
| 13797 | _PyUnicode_UTF8(self) = data; |
| 13798 | } |
| 13799 | if (share_wstr) { |
| 13800 | _PyUnicode_WSTR_LENGTH(self) = length; |
| 13801 | _PyUnicode_WSTR(self) = (wchar_t *)data; |
| 13802 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13803 | |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13804 | Py_MEMCPY(data, PyUnicode_DATA(unicode), |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 13805 | kind * (length + 1)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13806 | assert(_PyUnicode_CheckConsistency(self, 1)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 13807 | #ifdef Py_DEBUG |
| 13808 | _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode); |
| 13809 | #endif |
Victor Stinner | dd18d3a | 2011-10-22 11:08:10 +0200 | [diff] [blame] | 13810 | Py_DECREF(unicode); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13811 | return (PyObject *)self; |
| 13812 | |
| 13813 | onError: |
| 13814 | Py_DECREF(unicode); |
| 13815 | Py_DECREF(self); |
| 13816 | return NULL; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 13817 | } |
| 13818 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 13819 | PyDoc_STRVAR(unicode_doc, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13820 | "str(string[, encoding[, errors]]) -> str\n\ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13821 | \n\ |
Collin Winter | d474ce8 | 2007-08-07 19:42:11 +0000 | [diff] [blame] | 13822 | Create a new string object from the given encoded string.\n\ |
Skip Montanaro | 35b37a5 | 2002-07-26 16:22:46 +0000 | [diff] [blame] | 13823 | encoding defaults to the current default string encoding.\n\ |
| 13824 | errors can be 'strict', 'replace' or 'ignore' and defaults to 'strict'."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13825 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13826 | static PyObject *unicode_iter(PyObject *seq); |
| 13827 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13828 | PyTypeObject PyUnicode_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 13829 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13830 | "str", /* tp_name */ |
| 13831 | sizeof(PyUnicodeObject), /* tp_size */ |
| 13832 | 0, /* tp_itemsize */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13833 | /* Slots */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13834 | (destructor)unicode_dealloc, /* tp_dealloc */ |
| 13835 | 0, /* tp_print */ |
| 13836 | 0, /* tp_getattr */ |
| 13837 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 13838 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13839 | unicode_repr, /* tp_repr */ |
| 13840 | &unicode_as_number, /* tp_as_number */ |
| 13841 | &unicode_as_sequence, /* tp_as_sequence */ |
| 13842 | &unicode_as_mapping, /* tp_as_mapping */ |
| 13843 | (hashfunc) unicode_hash, /* tp_hash*/ |
| 13844 | 0, /* tp_call*/ |
| 13845 | (reprfunc) unicode_str, /* tp_str */ |
| 13846 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 13847 | 0, /* tp_setattro */ |
| 13848 | 0, /* tp_as_buffer */ |
| 13849 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13850 | Py_TPFLAGS_UNICODE_SUBCLASS, /* tp_flags */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13851 | unicode_doc, /* tp_doc */ |
| 13852 | 0, /* tp_traverse */ |
| 13853 | 0, /* tp_clear */ |
| 13854 | PyUnicode_RichCompare, /* tp_richcompare */ |
| 13855 | 0, /* tp_weaklistoffset */ |
| 13856 | unicode_iter, /* tp_iter */ |
| 13857 | 0, /* tp_iternext */ |
| 13858 | unicode_methods, /* tp_methods */ |
| 13859 | 0, /* tp_members */ |
| 13860 | 0, /* tp_getset */ |
| 13861 | &PyBaseObject_Type, /* tp_base */ |
| 13862 | 0, /* tp_dict */ |
| 13863 | 0, /* tp_descr_get */ |
| 13864 | 0, /* tp_descr_set */ |
| 13865 | 0, /* tp_dictoffset */ |
| 13866 | 0, /* tp_init */ |
| 13867 | 0, /* tp_alloc */ |
| 13868 | unicode_new, /* tp_new */ |
| 13869 | PyObject_Del, /* tp_free */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13870 | }; |
| 13871 | |
| 13872 | /* Initialize the Unicode implementation */ |
| 13873 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 13874 | int _PyUnicode_Init(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13875 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 13876 | int i; |
| 13877 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13878 | /* XXX - move this array to unicodectype.c ? */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13879 | Py_UCS2 linebreak[] = { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13880 | 0x000A, /* LINE FEED */ |
| 13881 | 0x000D, /* CARRIAGE RETURN */ |
| 13882 | 0x001C, /* FILE SEPARATOR */ |
| 13883 | 0x001D, /* GROUP SEPARATOR */ |
| 13884 | 0x001E, /* RECORD SEPARATOR */ |
| 13885 | 0x0085, /* NEXT LINE */ |
| 13886 | 0x2028, /* LINE SEPARATOR */ |
| 13887 | 0x2029, /* PARAGRAPH SEPARATOR */ |
| 13888 | }; |
| 13889 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 13890 | /* Init the implementation */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 13891 | unicode_empty = PyUnicode_New(0, 0); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13892 | assert(_PyUnicode_CheckConsistency(unicode_empty, 1)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13893 | if (!unicode_empty) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13894 | Py_FatalError("Can't create empty string"); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13895 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 13896 | for (i = 0; i < 256; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13897 | unicode_latin1[i] = NULL; |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 13898 | if (PyType_Ready(&PyUnicode_Type) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13899 | Py_FatalError("Can't initialize 'unicode'"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13900 | |
| 13901 | /* initialize the linebreak bloom filter */ |
| 13902 | bloom_linebreak = make_bloom_mask( |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13903 | PyUnicode_2BYTE_KIND, linebreak, |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 13904 | Py_ARRAY_LENGTH(linebreak)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13905 | |
| 13906 | PyType_Ready(&EncodingMapType); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 13907 | |
| 13908 | #ifdef HAVE_MBCS |
| 13909 | winver.dwOSVersionInfoSize = sizeof(winver); |
| 13910 | if (!GetVersionEx((OSVERSIONINFO*)&winver)) { |
| 13911 | PyErr_SetFromWindowsErr(0); |
| 13912 | return -1; |
| 13913 | } |
| 13914 | #endif |
| 13915 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13916 | } |
| 13917 | |
| 13918 | /* Finalize the Unicode implementation */ |
| 13919 | |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 13920 | int |
| 13921 | PyUnicode_ClearFreeList(void) |
| 13922 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13923 | return 0; |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 13924 | } |
| 13925 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13926 | void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 13927 | _PyUnicode_Fini(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13928 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 13929 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13930 | |
Guido van Rossum | 4ae8ef8 | 2000-10-03 18:09:04 +0000 | [diff] [blame] | 13931 | Py_XDECREF(unicode_empty); |
| 13932 | unicode_empty = NULL; |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 13933 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 13934 | for (i = 0; i < 256; i++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13935 | if (unicode_latin1[i]) { |
| 13936 | Py_DECREF(unicode_latin1[i]); |
| 13937 | unicode_latin1[i] = NULL; |
| 13938 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 13939 | } |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 13940 | _PyUnicode_ClearStaticStrings(); |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 13941 | (void)PyUnicode_ClearFreeList(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13942 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 13943 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13944 | void |
| 13945 | PyUnicode_InternInPlace(PyObject **p) |
| 13946 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13947 | register PyObject *s = *p; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13948 | PyObject *t; |
Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 13949 | #ifdef Py_DEBUG |
| 13950 | assert(s != NULL); |
| 13951 | assert(_PyUnicode_CHECK(s)); |
| 13952 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13953 | if (s == NULL || !PyUnicode_Check(s)) |
Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 13954 | return; |
| 13955 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13956 | /* If it's a subclass, we don't really know what putting |
| 13957 | it in the interned dict might do. */ |
| 13958 | if (!PyUnicode_CheckExact(s)) |
| 13959 | return; |
| 13960 | if (PyUnicode_CHECK_INTERNED(s)) |
| 13961 | return; |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 13962 | if (_PyUnicode_READY_REPLACE(p)) { |
Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 13963 | assert(0 && "_PyUnicode_READY_REPLACE fail in PyUnicode_InternInPlace"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13964 | return; |
| 13965 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13966 | s = *p; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13967 | if (interned == NULL) { |
| 13968 | interned = PyDict_New(); |
| 13969 | if (interned == NULL) { |
| 13970 | PyErr_Clear(); /* Don't leave an exception */ |
| 13971 | return; |
| 13972 | } |
| 13973 | } |
| 13974 | /* It might be that the GetItem call fails even |
| 13975 | though the key is present in the dictionary, |
| 13976 | namely when this happens during a stack overflow. */ |
| 13977 | Py_ALLOW_RECURSION |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13978 | t = PyDict_GetItem(interned, (PyObject *)s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13979 | Py_END_ALLOW_RECURSION |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 13980 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13981 | if (t) { |
| 13982 | Py_INCREF(t); |
| 13983 | Py_DECREF(*p); |
| 13984 | *p = t; |
| 13985 | return; |
| 13986 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13987 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13988 | PyThreadState_GET()->recursion_critical = 1; |
| 13989 | if (PyDict_SetItem(interned, (PyObject *)s, (PyObject *)s) < 0) { |
| 13990 | PyErr_Clear(); |
| 13991 | PyThreadState_GET()->recursion_critical = 0; |
| 13992 | return; |
| 13993 | } |
| 13994 | PyThreadState_GET()->recursion_critical = 0; |
| 13995 | /* The two references in interned are not counted by refcnt. |
| 13996 | The deallocator will take care of this */ |
| 13997 | Py_REFCNT(s) -= 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13998 | _PyUnicode_STATE(s).interned = SSTATE_INTERNED_MORTAL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13999 | } |
| 14000 | |
| 14001 | void |
| 14002 | PyUnicode_InternImmortal(PyObject **p) |
| 14003 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14004 | PyUnicodeObject *u = (PyUnicodeObject *)*p; |
| 14005 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14006 | PyUnicode_InternInPlace(p); |
| 14007 | if (PyUnicode_CHECK_INTERNED(*p) != SSTATE_INTERNED_IMMORTAL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14008 | _PyUnicode_STATE(u).interned = SSTATE_INTERNED_IMMORTAL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14009 | Py_INCREF(*p); |
| 14010 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14011 | } |
| 14012 | |
| 14013 | PyObject * |
| 14014 | PyUnicode_InternFromString(const char *cp) |
| 14015 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14016 | PyObject *s = PyUnicode_FromString(cp); |
| 14017 | if (s == NULL) |
| 14018 | return NULL; |
| 14019 | PyUnicode_InternInPlace(&s); |
| 14020 | return s; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14021 | } |
| 14022 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 14023 | void |
| 14024 | _Py_ReleaseInternedUnicodeStrings(void) |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14025 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14026 | PyObject *keys; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14027 | PyObject *s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14028 | Py_ssize_t i, n; |
| 14029 | Py_ssize_t immortal_size = 0, mortal_size = 0; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14030 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14031 | if (interned == NULL || !PyDict_Check(interned)) |
| 14032 | return; |
| 14033 | keys = PyDict_Keys(interned); |
| 14034 | if (keys == NULL || !PyList_Check(keys)) { |
| 14035 | PyErr_Clear(); |
| 14036 | return; |
| 14037 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14038 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14039 | /* Since _Py_ReleaseInternedUnicodeStrings() is intended to help a leak |
| 14040 | detector, interned unicode strings are not forcibly deallocated; |
| 14041 | rather, we give them their stolen references back, and then clear |
| 14042 | and DECREF the interned dict. */ |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14043 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14044 | n = PyList_GET_SIZE(keys); |
| 14045 | fprintf(stderr, "releasing %" PY_FORMAT_SIZE_T "d interned strings\n", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14046 | n); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14047 | for (i = 0; i < n; i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14048 | s = PyList_GET_ITEM(keys, i); |
Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 14049 | if (PyUnicode_READY(s) == -1) { |
| 14050 | assert(0 && "could not ready string"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14051 | fprintf(stderr, "could not ready string\n"); |
Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 14052 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14053 | switch (PyUnicode_CHECK_INTERNED(s)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14054 | case SSTATE_NOT_INTERNED: |
| 14055 | /* XXX Shouldn't happen */ |
| 14056 | break; |
| 14057 | case SSTATE_INTERNED_IMMORTAL: |
| 14058 | Py_REFCNT(s) += 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14059 | immortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14060 | break; |
| 14061 | case SSTATE_INTERNED_MORTAL: |
| 14062 | Py_REFCNT(s) += 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14063 | mortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14064 | break; |
| 14065 | default: |
| 14066 | Py_FatalError("Inconsistent interned string state."); |
| 14067 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14068 | _PyUnicode_STATE(s).interned = SSTATE_NOT_INTERNED; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14069 | } |
| 14070 | fprintf(stderr, "total size of all interned strings: " |
| 14071 | "%" PY_FORMAT_SIZE_T "d/%" PY_FORMAT_SIZE_T "d " |
| 14072 | "mortal/immortal\n", mortal_size, immortal_size); |
| 14073 | Py_DECREF(keys); |
| 14074 | PyDict_Clear(interned); |
| 14075 | Py_DECREF(interned); |
| 14076 | interned = NULL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14077 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14078 | |
| 14079 | |
| 14080 | /********************* Unicode Iterator **************************/ |
| 14081 | |
| 14082 | typedef struct { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14083 | PyObject_HEAD |
| 14084 | Py_ssize_t it_index; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14085 | PyObject *it_seq; /* Set to NULL when iterator is exhausted */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14086 | } unicodeiterobject; |
| 14087 | |
| 14088 | static void |
| 14089 | unicodeiter_dealloc(unicodeiterobject *it) |
| 14090 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14091 | _PyObject_GC_UNTRACK(it); |
| 14092 | Py_XDECREF(it->it_seq); |
| 14093 | PyObject_GC_Del(it); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14094 | } |
| 14095 | |
| 14096 | static int |
| 14097 | unicodeiter_traverse(unicodeiterobject *it, visitproc visit, void *arg) |
| 14098 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14099 | Py_VISIT(it->it_seq); |
| 14100 | return 0; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14101 | } |
| 14102 | |
| 14103 | static PyObject * |
| 14104 | unicodeiter_next(unicodeiterobject *it) |
| 14105 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14106 | PyObject *seq, *item; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14107 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14108 | assert(it != NULL); |
| 14109 | seq = it->it_seq; |
| 14110 | if (seq == NULL) |
| 14111 | return NULL; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 14112 | assert(_PyUnicode_CHECK(seq)); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14113 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14114 | if (it->it_index < PyUnicode_GET_LENGTH(seq)) { |
| 14115 | int kind = PyUnicode_KIND(seq); |
| 14116 | void *data = PyUnicode_DATA(seq); |
| 14117 | Py_UCS4 chr = PyUnicode_READ(kind, data, it->it_index); |
| 14118 | item = PyUnicode_FromOrdinal(chr); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14119 | if (item != NULL) |
| 14120 | ++it->it_index; |
| 14121 | return item; |
| 14122 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14123 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14124 | Py_DECREF(seq); |
| 14125 | it->it_seq = NULL; |
| 14126 | return NULL; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14127 | } |
| 14128 | |
| 14129 | static PyObject * |
| 14130 | unicodeiter_len(unicodeiterobject *it) |
| 14131 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14132 | Py_ssize_t len = 0; |
| 14133 | if (it->it_seq) |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 14134 | len = PyUnicode_GET_LENGTH(it->it_seq) - it->it_index; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14135 | return PyLong_FromSsize_t(len); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14136 | } |
| 14137 | |
| 14138 | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
| 14139 | |
| 14140 | static PyMethodDef unicodeiter_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14141 | {"__length_hint__", (PyCFunction)unicodeiter_len, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14142 | length_hint_doc}, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14143 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14144 | }; |
| 14145 | |
| 14146 | PyTypeObject PyUnicodeIter_Type = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14147 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 14148 | "str_iterator", /* tp_name */ |
| 14149 | sizeof(unicodeiterobject), /* tp_basicsize */ |
| 14150 | 0, /* tp_itemsize */ |
| 14151 | /* methods */ |
| 14152 | (destructor)unicodeiter_dealloc, /* tp_dealloc */ |
| 14153 | 0, /* tp_print */ |
| 14154 | 0, /* tp_getattr */ |
| 14155 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 14156 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14157 | 0, /* tp_repr */ |
| 14158 | 0, /* tp_as_number */ |
| 14159 | 0, /* tp_as_sequence */ |
| 14160 | 0, /* tp_as_mapping */ |
| 14161 | 0, /* tp_hash */ |
| 14162 | 0, /* tp_call */ |
| 14163 | 0, /* tp_str */ |
| 14164 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 14165 | 0, /* tp_setattro */ |
| 14166 | 0, /* tp_as_buffer */ |
| 14167 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 14168 | 0, /* tp_doc */ |
| 14169 | (traverseproc)unicodeiter_traverse, /* tp_traverse */ |
| 14170 | 0, /* tp_clear */ |
| 14171 | 0, /* tp_richcompare */ |
| 14172 | 0, /* tp_weaklistoffset */ |
| 14173 | PyObject_SelfIter, /* tp_iter */ |
| 14174 | (iternextfunc)unicodeiter_next, /* tp_iternext */ |
| 14175 | unicodeiter_methods, /* tp_methods */ |
| 14176 | 0, |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14177 | }; |
| 14178 | |
| 14179 | static PyObject * |
| 14180 | unicode_iter(PyObject *seq) |
| 14181 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14182 | unicodeiterobject *it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14183 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14184 | if (!PyUnicode_Check(seq)) { |
| 14185 | PyErr_BadInternalCall(); |
| 14186 | return NULL; |
| 14187 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14188 | if (PyUnicode_READY(seq) == -1) |
| 14189 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14190 | it = PyObject_GC_New(unicodeiterobject, &PyUnicodeIter_Type); |
| 14191 | if (it == NULL) |
| 14192 | return NULL; |
| 14193 | it->it_index = 0; |
| 14194 | Py_INCREF(seq); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14195 | it->it_seq = seq; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14196 | _PyObject_GC_TRACK(it); |
| 14197 | return (PyObject *)it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14198 | } |
| 14199 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14200 | #define UNIOP(x) Py_UNICODE_##x |
| 14201 | #define UNIOP_t Py_UNICODE |
| 14202 | #include "uniops.h" |
| 14203 | #undef UNIOP |
| 14204 | #undef UNIOP_t |
| 14205 | #define UNIOP(x) Py_UCS4_##x |
| 14206 | #define UNIOP_t Py_UCS4 |
| 14207 | #include "uniops.h" |
| 14208 | #undef UNIOP |
| 14209 | #undef UNIOP_t |
Victor Stinner | 331ea92 | 2010-08-10 16:37:20 +0000 | [diff] [blame] | 14210 | |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14211 | Py_UNICODE* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14212 | PyUnicode_AsUnicodeCopy(PyObject *unicode) |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14213 | { |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 14214 | Py_UNICODE *u, *copy; |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14215 | Py_ssize_t size; |
| 14216 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14217 | if (!PyUnicode_Check(unicode)) { |
| 14218 | PyErr_BadArgument(); |
| 14219 | return NULL; |
| 14220 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14221 | u = PyUnicode_AsUnicode(unicode); |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 14222 | if (u == NULL) |
| 14223 | return NULL; |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14224 | /* Ensure we won't overflow the size. */ |
| 14225 | if (PyUnicode_GET_SIZE(unicode) > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
| 14226 | PyErr_NoMemory(); |
| 14227 | return NULL; |
| 14228 | } |
| 14229 | size = PyUnicode_GET_SIZE(unicode) + 1; /* copy the nul character */ |
| 14230 | size *= sizeof(Py_UNICODE); |
| 14231 | copy = PyMem_Malloc(size); |
| 14232 | if (copy == NULL) { |
| 14233 | PyErr_NoMemory(); |
| 14234 | return NULL; |
| 14235 | } |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 14236 | memcpy(copy, u, size); |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14237 | return copy; |
| 14238 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 14239 | |
Georg Brandl | 66c221e | 2010-10-14 07:04:07 +0000 | [diff] [blame] | 14240 | /* A _string module, to export formatter_parser and formatter_field_name_split |
| 14241 | to the string.Formatter class implemented in Python. */ |
| 14242 | |
| 14243 | static PyMethodDef _string_methods[] = { |
| 14244 | {"formatter_field_name_split", (PyCFunction) formatter_field_name_split, |
| 14245 | METH_O, PyDoc_STR("split the argument as a field name")}, |
| 14246 | {"formatter_parser", (PyCFunction) formatter_parser, |
| 14247 | METH_O, PyDoc_STR("parse the argument as a format string")}, |
| 14248 | {NULL, NULL} |
| 14249 | }; |
| 14250 | |
| 14251 | static struct PyModuleDef _string_module = { |
| 14252 | PyModuleDef_HEAD_INIT, |
| 14253 | "_string", |
| 14254 | PyDoc_STR("string helper module"), |
| 14255 | 0, |
| 14256 | _string_methods, |
| 14257 | NULL, |
| 14258 | NULL, |
| 14259 | NULL, |
| 14260 | NULL |
| 14261 | }; |
| 14262 | |
| 14263 | PyMODINIT_FUNC |
| 14264 | PyInit__string(void) |
| 14265 | { |
| 14266 | return PyModule_Create(&_string_module); |
| 14267 | } |
| 14268 | |
| 14269 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 14270 | #ifdef __cplusplus |
| 14271 | } |
| 14272 | #endif |