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 | /* Limit for the Unicode object free list */ |
| 54 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 55 | #define PyUnicode_MAXFREELIST 1024 |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 56 | |
| 57 | /* Limit for the Unicode object free list stay alive optimization. |
| 58 | |
| 59 | The implementation will keep allocated Unicode memory intact for |
| 60 | all objects on the free list having a size less than this |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 61 | limit. This reduces malloc() overhead for small Unicode objects. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 62 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 63 | At worst this will result in PyUnicode_MAXFREELIST * |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 64 | (sizeof(PyUnicodeObject) + KEEPALIVE_SIZE_LIMIT + |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 65 | malloc()-overhead) bytes of unused garbage. |
| 66 | |
| 67 | Setting the limit to 0 effectively turns the feature off. |
| 68 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 69 | Note: This is an experimental feature ! If you get core dumps when |
| 70 | using Unicode objects, turn this feature off. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 71 | |
| 72 | */ |
| 73 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 74 | #define KEEPALIVE_SIZE_LIMIT 9 |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 75 | |
| 76 | /* Endianness switches; defaults to little endian */ |
| 77 | |
| 78 | #ifdef WORDS_BIGENDIAN |
| 79 | # define BYTEORDER_IS_BIG_ENDIAN |
| 80 | #else |
| 81 | # define BYTEORDER_IS_LITTLE_ENDIAN |
| 82 | #endif |
| 83 | |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 84 | /* --- Globals ------------------------------------------------------------ |
| 85 | |
| 86 | The globals are initialized by the _PyUnicode_Init() API and should |
| 87 | not be used before calling that API. |
| 88 | |
| 89 | */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 90 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 91 | |
| 92 | #ifdef __cplusplus |
| 93 | extern "C" { |
| 94 | #endif |
| 95 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 96 | #ifdef Py_DEBUG |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 97 | # define _PyUnicode_CHECK(op) _PyUnicode_CheckConsistency(op, 0) |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 98 | #else |
| 99 | # define _PyUnicode_CHECK(op) PyUnicode_Check(op) |
| 100 | #endif |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 101 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 102 | #define _PyUnicode_UTF8(op) \ |
| 103 | (((PyCompactUnicodeObject*)(op))->utf8) |
| 104 | #define PyUnicode_UTF8(op) \ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 105 | (assert(_PyUnicode_CHECK(op)), \ |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 106 | assert(PyUnicode_IS_READY(op)), \ |
| 107 | PyUnicode_IS_COMPACT_ASCII(op) ? \ |
| 108 | ((char*)((PyASCIIObject*)(op) + 1)) : \ |
| 109 | _PyUnicode_UTF8(op)) |
Victor Stinner | bc8b81b | 2011-09-29 19:31:34 +0200 | [diff] [blame] | 110 | #define _PyUnicode_UTF8_LENGTH(op) \ |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 111 | (((PyCompactUnicodeObject*)(op))->utf8_length) |
| 112 | #define PyUnicode_UTF8_LENGTH(op) \ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 113 | (assert(_PyUnicode_CHECK(op)), \ |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 114 | assert(PyUnicode_IS_READY(op)), \ |
| 115 | PyUnicode_IS_COMPACT_ASCII(op) ? \ |
| 116 | ((PyASCIIObject*)(op))->length : \ |
| 117 | _PyUnicode_UTF8_LENGTH(op)) |
Victor Stinner | a5f9163 | 2011-10-04 01:07:11 +0200 | [diff] [blame] | 118 | #define _PyUnicode_WSTR(op) \ |
| 119 | (((PyASCIIObject*)(op))->wstr) |
| 120 | #define _PyUnicode_WSTR_LENGTH(op) \ |
| 121 | (((PyCompactUnicodeObject*)(op))->wstr_length) |
| 122 | #define _PyUnicode_LENGTH(op) \ |
| 123 | (((PyASCIIObject *)(op))->length) |
| 124 | #define _PyUnicode_STATE(op) \ |
| 125 | (((PyASCIIObject *)(op))->state) |
| 126 | #define _PyUnicode_HASH(op) \ |
| 127 | (((PyASCIIObject *)(op))->hash) |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 128 | #define _PyUnicode_KIND(op) \ |
| 129 | (assert(_PyUnicode_CHECK(op)), \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 130 | ((PyASCIIObject *)(op))->state.kind) |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 131 | #define _PyUnicode_GET_LENGTH(op) \ |
| 132 | (assert(_PyUnicode_CHECK(op)), \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 133 | ((PyASCIIObject *)(op))->length) |
Victor Stinner | a5f9163 | 2011-10-04 01:07:11 +0200 | [diff] [blame] | 134 | #define _PyUnicode_DATA_ANY(op) \ |
| 135 | (((PyUnicodeObject*)(op))->data.any) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 136 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 137 | #undef PyUnicode_READY |
| 138 | #define PyUnicode_READY(op) \ |
| 139 | (assert(_PyUnicode_CHECK(op)), \ |
| 140 | (PyUnicode_IS_READY(op) ? \ |
Victor Stinner | a5f9163 | 2011-10-04 01:07:11 +0200 | [diff] [blame] | 141 | 0 : \ |
| 142 | _PyUnicode_Ready((PyObject *)(op)))) |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 143 | |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 144 | #define _PyUnicode_READY_REPLACE(p_obj) \ |
| 145 | (assert(_PyUnicode_CHECK(*p_obj)), \ |
| 146 | (PyUnicode_IS_READY(*p_obj) ? \ |
| 147 | 0 : _PyUnicode_ReadyReplace((PyObject **)(p_obj)))) |
| 148 | |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 149 | #define _PyUnicode_SHARE_UTF8(op) \ |
| 150 | (assert(_PyUnicode_CHECK(op)), \ |
| 151 | assert(!PyUnicode_IS_COMPACT_ASCII(op)), \ |
| 152 | (_PyUnicode_UTF8(op) == PyUnicode_DATA(op))) |
| 153 | #define _PyUnicode_SHARE_WSTR(op) \ |
| 154 | (assert(_PyUnicode_CHECK(op)), \ |
| 155 | (_PyUnicode_WSTR(unicode) == PyUnicode_DATA(op))) |
| 156 | |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 157 | /* true if the Unicode object has an allocated UTF-8 memory block |
| 158 | (not shared with other data) */ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 159 | #define _PyUnicode_HAS_UTF8_MEMORY(op) \ |
| 160 | (assert(_PyUnicode_CHECK(op)), \ |
| 161 | (!PyUnicode_IS_COMPACT_ASCII(op) \ |
| 162 | && _PyUnicode_UTF8(op) \ |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 163 | && _PyUnicode_UTF8(op) != PyUnicode_DATA(op))) |
| 164 | |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 165 | /* true if the Unicode object has an allocated wstr memory block |
| 166 | (not shared with other data) */ |
| 167 | #define _PyUnicode_HAS_WSTR_MEMORY(op) \ |
| 168 | (assert(_PyUnicode_CHECK(op)), \ |
| 169 | (_PyUnicode_WSTR(op) && \ |
| 170 | (!PyUnicode_IS_READY(op) || \ |
| 171 | _PyUnicode_WSTR(op) != PyUnicode_DATA(op)))) |
| 172 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 173 | /* Generic helper macro to convert characters of different types. |
| 174 | from_type and to_type have to be valid type names, begin and end |
| 175 | are pointers to the source characters which should be of type |
| 176 | "from_type *". to is a pointer of type "to_type *" and points to the |
| 177 | buffer where the result characters are written to. */ |
| 178 | #define _PyUnicode_CONVERT_BYTES(from_type, to_type, begin, end, to) \ |
| 179 | do { \ |
| 180 | const from_type *iter_; to_type *to_; \ |
| 181 | for (iter_ = (begin), to_ = (to_type *)(to); \ |
| 182 | iter_ < (end); \ |
| 183 | ++iter_, ++to_) { \ |
| 184 | *to_ = (to_type)*iter_; \ |
| 185 | } \ |
| 186 | } while (0) |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 187 | |
Victor Stinner | b15d4d8 | 2011-09-28 23:59:20 +0200 | [diff] [blame] | 188 | /* The Unicode string has been modified: reset the hash */ |
| 189 | #define _PyUnicode_DIRTY(op) do { _PyUnicode_HASH(op) = -1; } while (0) |
| 190 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 191 | /* This dictionary holds all interned unicode strings. Note that references |
| 192 | to strings in this dictionary are *not* counted in the string's ob_refcnt. |
| 193 | When the interned string reaches a refcnt of 0 the string deallocation |
| 194 | function will delete the reference from this dictionary. |
| 195 | |
| 196 | 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] | 197 | 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] | 198 | */ |
| 199 | static PyObject *interned; |
| 200 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 201 | /* The empty Unicode object is shared to improve performance. */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 202 | static PyObject *unicode_empty; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 203 | |
| 204 | /* Single character Unicode strings in the Latin-1 range are being |
| 205 | shared as well. */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 206 | static PyObject *unicode_latin1[256]; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 207 | |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 208 | /* Fast detection of the most frequent whitespace characters */ |
| 209 | const unsigned char _Py_ascii_whitespace[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 210 | 0, 0, 0, 0, 0, 0, 0, 0, |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 211 | /* case 0x0009: * CHARACTER TABULATION */ |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 212 | /* case 0x000A: * LINE FEED */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 213 | /* case 0x000B: * LINE TABULATION */ |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 214 | /* case 0x000C: * FORM FEED */ |
| 215 | /* case 0x000D: * CARRIAGE RETURN */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 216 | 0, 1, 1, 1, 1, 1, 0, 0, |
| 217 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 218 | /* case 0x001C: * FILE SEPARATOR */ |
| 219 | /* case 0x001D: * GROUP SEPARATOR */ |
| 220 | /* case 0x001E: * RECORD SEPARATOR */ |
| 221 | /* case 0x001F: * UNIT SEPARATOR */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 222 | 0, 0, 0, 0, 1, 1, 1, 1, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 223 | /* case 0x0020: * SPACE */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 224 | 1, 0, 0, 0, 0, 0, 0, 0, |
| 225 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 226 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 227 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 228 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 229 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 230 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 231 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 232 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 233 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 234 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 235 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 236 | 0, 0, 0, 0, 0, 0, 0, 0 |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 237 | }; |
| 238 | |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 239 | /* forward */ |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 240 | static PyUnicodeObject *_PyUnicode_New(Py_ssize_t length); |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 241 | static PyObject* get_latin1_char(unsigned char ch); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 242 | static void copy_characters( |
| 243 | PyObject *to, Py_ssize_t to_start, |
| 244 | PyObject *from, Py_ssize_t from_start, |
| 245 | Py_ssize_t how_many); |
Victor Stinner | c729b8e | 2011-10-06 02:36:59 +0200 | [diff] [blame] | 246 | #ifdef Py_DEBUG |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 247 | static int unicode_is_singleton(PyObject *unicode); |
Victor Stinner | c729b8e | 2011-10-06 02:36:59 +0200 | [diff] [blame] | 248 | #endif |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 249 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 250 | static PyObject * |
| 251 | unicode_encode_call_errorhandler(const char *errors, |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 252 | PyObject **errorHandler,const char *encoding, const char *reason, |
| 253 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 254 | Py_ssize_t startpos, Py_ssize_t endpos, Py_ssize_t *newpos); |
| 255 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 256 | static void |
| 257 | raise_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 258 | const char *encoding, |
| 259 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 260 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 261 | const char *reason); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 262 | |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 263 | /* Same for linebreaks */ |
| 264 | static unsigned char ascii_linebreak[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 265 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 266 | /* 0x000A, * LINE FEED */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 267 | /* 0x000B, * LINE TABULATION */ |
| 268 | /* 0x000C, * FORM FEED */ |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 269 | /* 0x000D, * CARRIAGE RETURN */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 270 | 0, 0, 1, 1, 1, 1, 0, 0, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 271 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 272 | /* 0x001C, * FILE SEPARATOR */ |
| 273 | /* 0x001D, * GROUP SEPARATOR */ |
| 274 | /* 0x001E, * RECORD SEPARATOR */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 275 | 0, 0, 0, 0, 1, 1, 1, 0, |
| 276 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 277 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 278 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 279 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 280 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 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, |
| 287 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 288 | 0, 0, 0, 0, 0, 0, 0, 0 |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 289 | }; |
| 290 | |
Ezio Melotti | 48a2f8f | 2011-09-29 00:18:19 +0300 | [diff] [blame] | 291 | /* The max unicode value is always 0x10FFFF while using the PEP-393 API. |
| 292 | 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] | 293 | Py_UNICODE |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 294 | PyUnicode_GetMax(void) |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 295 | { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 296 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 297 | return 0x10FFFF; |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 298 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 299 | /* This is actually an illegal character, so it should |
| 300 | not be passed to unichr. */ |
| 301 | return 0xFFFF; |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 302 | #endif |
| 303 | } |
| 304 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 305 | #ifdef Py_DEBUG |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 306 | int |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 307 | /* FIXME: use PyObject* type for op */ |
| 308 | _PyUnicode_CheckConsistency(void *op, int check_content) |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 309 | { |
| 310 | PyASCIIObject *ascii; |
| 311 | unsigned int kind; |
| 312 | |
| 313 | assert(PyUnicode_Check(op)); |
| 314 | |
| 315 | ascii = (PyASCIIObject *)op; |
| 316 | kind = ascii->state.kind; |
| 317 | |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 318 | if (ascii->state.ascii == 1 && ascii->state.compact == 1) { |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 319 | assert(kind == PyUnicode_1BYTE_KIND); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 320 | assert(ascii->state.ready == 1); |
| 321 | } |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 322 | else { |
Victor Stinner | 85041a5 | 2011-10-03 14:42:39 +0200 | [diff] [blame] | 323 | PyCompactUnicodeObject *compact = (PyCompactUnicodeObject *)op; |
Victor Stinner | 7f11ad4 | 2011-10-04 00:00:20 +0200 | [diff] [blame] | 324 | void *data; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 325 | |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 326 | if (ascii->state.compact == 1) { |
| 327 | data = compact + 1; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 328 | assert(kind == PyUnicode_1BYTE_KIND |
| 329 | || kind == PyUnicode_2BYTE_KIND |
| 330 | || kind == PyUnicode_4BYTE_KIND); |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 331 | assert(ascii->state.ascii == 0); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 332 | assert(ascii->state.ready == 1); |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 333 | assert (compact->utf8 != data); |
| 334 | } else { |
| 335 | PyUnicodeObject *unicode = (PyUnicodeObject *)op; |
| 336 | |
| 337 | data = unicode->data.any; |
| 338 | if (kind == PyUnicode_WCHAR_KIND) { |
| 339 | assert(ascii->state.compact == 0); |
| 340 | assert(ascii->state.ascii == 0); |
| 341 | assert(ascii->state.ready == 0); |
| 342 | assert(ascii->wstr != NULL); |
| 343 | assert(data == NULL); |
| 344 | assert(compact->utf8 == NULL); |
| 345 | assert(ascii->state.interned == SSTATE_NOT_INTERNED); |
| 346 | } |
| 347 | else { |
| 348 | assert(kind == PyUnicode_1BYTE_KIND |
| 349 | || kind == PyUnicode_2BYTE_KIND |
| 350 | || kind == PyUnicode_4BYTE_KIND); |
| 351 | assert(ascii->state.compact == 0); |
| 352 | assert(ascii->state.ready == 1); |
| 353 | assert(data != NULL); |
| 354 | if (ascii->state.ascii) { |
| 355 | assert (compact->utf8 == data); |
| 356 | assert (compact->utf8_length == ascii->length); |
| 357 | } |
| 358 | else |
| 359 | assert (compact->utf8 != data); |
| 360 | } |
| 361 | } |
| 362 | if (kind != PyUnicode_WCHAR_KIND) { |
Victor Stinner | 7f11ad4 | 2011-10-04 00:00:20 +0200 | [diff] [blame] | 363 | if ( |
| 364 | #if SIZEOF_WCHAR_T == 2 |
| 365 | kind == PyUnicode_2BYTE_KIND |
| 366 | #else |
| 367 | kind == PyUnicode_4BYTE_KIND |
| 368 | #endif |
| 369 | ) |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 370 | { |
| 371 | assert(ascii->wstr == data); |
| 372 | assert(compact->wstr_length == ascii->length); |
| 373 | } else |
| 374 | assert(ascii->wstr != data); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 375 | } |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 376 | |
| 377 | if (compact->utf8 == NULL) |
| 378 | assert(compact->utf8_length == 0); |
| 379 | if (ascii->wstr == NULL) |
| 380 | assert(compact->wstr_length == 0); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 381 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 382 | /* check that the best kind is used */ |
| 383 | if (check_content && kind != PyUnicode_WCHAR_KIND) |
| 384 | { |
| 385 | Py_ssize_t i; |
| 386 | Py_UCS4 maxchar = 0; |
| 387 | void *data = PyUnicode_DATA(ascii); |
| 388 | for (i=0; i < ascii->length; i++) |
| 389 | { |
| 390 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 391 | if (ch > maxchar) |
| 392 | maxchar = ch; |
| 393 | } |
| 394 | if (kind == PyUnicode_1BYTE_KIND) { |
| 395 | if (ascii->state.ascii == 0) |
| 396 | assert(maxchar >= 128); |
| 397 | else |
| 398 | assert(maxchar < 128); |
| 399 | } |
| 400 | else if (kind == PyUnicode_2BYTE_KIND) |
| 401 | assert(maxchar >= 0x100); |
| 402 | else |
| 403 | assert(maxchar >= 0x10000); |
| 404 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 405 | if (check_content && !unicode_is_singleton((PyObject*)ascii)) |
| 406 | assert(ascii->hash == -1); |
Benjamin Peterson | ccc51c1 | 2011-10-03 19:34:12 -0400 | [diff] [blame] | 407 | return 1; |
| 408 | } |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 409 | #endif |
| 410 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 411 | /* --- Bloom Filters ----------------------------------------------------- */ |
| 412 | |
| 413 | /* stuff to implement simple "bloom filters" for Unicode characters. |
| 414 | to keep things simple, we use a single bitmask, using the least 5 |
| 415 | bits from each unicode characters as the bit index. */ |
| 416 | |
| 417 | /* the linebreak mask is set up by Unicode_Init below */ |
| 418 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 419 | #if LONG_BIT >= 128 |
| 420 | #define BLOOM_WIDTH 128 |
| 421 | #elif LONG_BIT >= 64 |
| 422 | #define BLOOM_WIDTH 64 |
| 423 | #elif LONG_BIT >= 32 |
| 424 | #define BLOOM_WIDTH 32 |
| 425 | #else |
| 426 | #error "LONG_BIT is smaller than 32" |
| 427 | #endif |
| 428 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 429 | #define BLOOM_MASK unsigned long |
| 430 | |
| 431 | static BLOOM_MASK bloom_linebreak; |
| 432 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 433 | #define BLOOM_ADD(mask, ch) ((mask |= (1UL << ((ch) & (BLOOM_WIDTH - 1))))) |
| 434 | #define BLOOM(mask, ch) ((mask & (1UL << ((ch) & (BLOOM_WIDTH - 1))))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 435 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 436 | #define BLOOM_LINEBREAK(ch) \ |
| 437 | ((ch) < 128U ? ascii_linebreak[(ch)] : \ |
| 438 | (BLOOM(bloom_linebreak, (ch)) && Py_UNICODE_ISLINEBREAK(ch))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 439 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 440 | Py_LOCAL_INLINE(BLOOM_MASK) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 441 | make_bloom_mask(int kind, void* ptr, Py_ssize_t len) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 442 | { |
| 443 | /* calculate simple bloom-style bitmask for a given unicode string */ |
| 444 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 445 | BLOOM_MASK mask; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 446 | Py_ssize_t i; |
| 447 | |
| 448 | mask = 0; |
| 449 | for (i = 0; i < len; i++) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 450 | BLOOM_ADD(mask, PyUnicode_READ(kind, ptr, i)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 451 | |
| 452 | return mask; |
| 453 | } |
| 454 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 455 | #define BLOOM_MEMBER(mask, chr, str) \ |
| 456 | (BLOOM(mask, chr) \ |
| 457 | && (PyUnicode_FindChar(str, chr, 0, PyUnicode_GET_LENGTH(str), 1) >= 0)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 458 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 459 | /* --- Unicode Object ----------------------------------------------------- */ |
| 460 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 461 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 462 | fixup(PyObject *self, Py_UCS4 (*fixfct)(PyObject *s)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 463 | |
| 464 | Py_LOCAL_INLINE(char *) findchar(void *s, int kind, |
| 465 | Py_ssize_t size, Py_UCS4 ch, |
| 466 | int direction) |
| 467 | { |
| 468 | /* like wcschr, but doesn't stop at NULL characters */ |
| 469 | Py_ssize_t i; |
| 470 | if (direction == 1) { |
| 471 | for(i = 0; i < size; i++) |
| 472 | if (PyUnicode_READ(kind, s, i) == ch) |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame^] | 473 | return (char*)s + kind * i; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 474 | } |
| 475 | else { |
| 476 | for(i = size-1; i >= 0; i--) |
| 477 | if (PyUnicode_READ(kind, s, i) == ch) |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame^] | 478 | return (char*)s + kind * i; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 479 | } |
| 480 | return NULL; |
| 481 | } |
| 482 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 483 | static PyObject* |
| 484 | resize_compact(PyObject *unicode, Py_ssize_t length) |
| 485 | { |
| 486 | Py_ssize_t char_size; |
| 487 | Py_ssize_t struct_size; |
| 488 | Py_ssize_t new_size; |
| 489 | int share_wstr; |
| 490 | |
| 491 | assert(PyUnicode_IS_READY(unicode)); |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame^] | 492 | char_size = PyUnicode_KIND(unicode); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 493 | if (PyUnicode_IS_COMPACT_ASCII(unicode)) |
| 494 | struct_size = sizeof(PyASCIIObject); |
| 495 | else |
| 496 | struct_size = sizeof(PyCompactUnicodeObject); |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 497 | share_wstr = _PyUnicode_SHARE_WSTR(unicode); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 498 | |
| 499 | _Py_DEC_REFTOTAL; |
| 500 | _Py_ForgetReference(unicode); |
| 501 | |
| 502 | if (length > ((PY_SSIZE_T_MAX - struct_size) / char_size - 1)) { |
| 503 | PyErr_NoMemory(); |
| 504 | return NULL; |
| 505 | } |
| 506 | new_size = (struct_size + (length + 1) * char_size); |
| 507 | |
| 508 | unicode = (PyObject *)PyObject_REALLOC((char *)unicode, new_size); |
| 509 | if (unicode == NULL) { |
| 510 | PyObject_Del(unicode); |
| 511 | PyErr_NoMemory(); |
| 512 | return NULL; |
| 513 | } |
| 514 | _Py_NewReference(unicode); |
| 515 | _PyUnicode_LENGTH(unicode) = length; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 516 | if (share_wstr) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 517 | _PyUnicode_WSTR(unicode) = PyUnicode_DATA(unicode); |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 518 | if (!PyUnicode_IS_COMPACT_ASCII(unicode)) |
| 519 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 520 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 521 | PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode), |
| 522 | length, 0); |
| 523 | return unicode; |
| 524 | } |
| 525 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 526 | static int |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 527 | resize_inplace(PyUnicodeObject *unicode, Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 528 | { |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 529 | wchar_t *wstr; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 530 | assert(!PyUnicode_IS_COMPACT(unicode)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 531 | assert(Py_REFCNT(unicode) == 1); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 532 | |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 533 | _PyUnicode_DIRTY(unicode); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 534 | |
| 535 | if (PyUnicode_IS_READY(unicode)) { |
| 536 | Py_ssize_t char_size; |
| 537 | Py_ssize_t new_size; |
Victor Stinner | 1c8d0c7 | 2011-10-03 12:11:00 +0200 | [diff] [blame] | 538 | int share_wstr, share_utf8; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 539 | void *data; |
| 540 | |
| 541 | data = _PyUnicode_DATA_ANY(unicode); |
| 542 | assert(data != NULL); |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame^] | 543 | char_size = PyUnicode_KIND(unicode); |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 544 | share_wstr = _PyUnicode_SHARE_WSTR(unicode); |
| 545 | share_utf8 = _PyUnicode_SHARE_UTF8(unicode); |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 546 | if (!share_utf8 && _PyUnicode_HAS_UTF8_MEMORY(unicode)) |
| 547 | { |
| 548 | PyObject_DEL(_PyUnicode_UTF8(unicode)); |
| 549 | _PyUnicode_UTF8(unicode) = NULL; |
| 550 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
| 551 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 552 | |
| 553 | if (length > (PY_SSIZE_T_MAX / char_size - 1)) { |
| 554 | PyErr_NoMemory(); |
| 555 | return -1; |
| 556 | } |
| 557 | new_size = (length + 1) * char_size; |
| 558 | |
| 559 | data = (PyObject *)PyObject_REALLOC(data, new_size); |
| 560 | if (data == NULL) { |
| 561 | PyErr_NoMemory(); |
| 562 | return -1; |
| 563 | } |
| 564 | _PyUnicode_DATA_ANY(unicode) = data; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 565 | if (share_wstr) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 566 | _PyUnicode_WSTR(unicode) = data; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 567 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 568 | } |
| 569 | if (share_utf8) { |
Victor Stinner | 1c8d0c7 | 2011-10-03 12:11:00 +0200 | [diff] [blame] | 570 | _PyUnicode_UTF8(unicode) = data; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 571 | _PyUnicode_UTF8_LENGTH(unicode) = length; |
| 572 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 573 | _PyUnicode_LENGTH(unicode) = length; |
| 574 | PyUnicode_WRITE(PyUnicode_KIND(unicode), data, length, 0); |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 575 | if (share_wstr || _PyUnicode_WSTR(unicode) == NULL) { |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 576 | assert(_PyUnicode_CheckConsistency(unicode, 0)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 577 | return 0; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 578 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 579 | } |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 580 | assert(_PyUnicode_WSTR(unicode) != NULL); |
| 581 | |
| 582 | /* check for integer overflow */ |
| 583 | if (length > PY_SSIZE_T_MAX / sizeof(wchar_t) - 1) { |
| 584 | PyErr_NoMemory(); |
| 585 | return -1; |
| 586 | } |
| 587 | wstr = _PyUnicode_WSTR(unicode); |
| 588 | wstr = PyObject_REALLOC(wstr, sizeof(wchar_t) * (length + 1)); |
| 589 | if (!wstr) { |
| 590 | PyErr_NoMemory(); |
| 591 | return -1; |
| 592 | } |
| 593 | _PyUnicode_WSTR(unicode) = wstr; |
| 594 | _PyUnicode_WSTR(unicode)[length] = 0; |
| 595 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 596 | assert(_PyUnicode_CheckConsistency(unicode, 0)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 597 | return 0; |
| 598 | } |
| 599 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 600 | static PyObject* |
| 601 | resize_copy(PyObject *unicode, Py_ssize_t length) |
| 602 | { |
| 603 | Py_ssize_t copy_length; |
| 604 | if (PyUnicode_IS_COMPACT(unicode)) { |
| 605 | PyObject *copy; |
| 606 | assert(PyUnicode_IS_READY(unicode)); |
| 607 | |
| 608 | copy = PyUnicode_New(length, PyUnicode_MAX_CHAR_VALUE(unicode)); |
| 609 | if (copy == NULL) |
| 610 | return NULL; |
| 611 | |
| 612 | copy_length = Py_MIN(length, PyUnicode_GET_LENGTH(unicode)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 613 | copy_characters(copy, 0, unicode, 0, copy_length); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 614 | return copy; |
Victor Stinner | 8cfcbed | 2011-10-03 23:19:21 +0200 | [diff] [blame] | 615 | } |
| 616 | else { |
Victor Stinner | 2fd8227 | 2011-10-03 04:06:05 +0200 | [diff] [blame] | 617 | PyUnicodeObject *w; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 618 | assert(_PyUnicode_WSTR(unicode) != NULL); |
| 619 | assert(_PyUnicode_DATA_ANY(unicode) == NULL); |
Victor Stinner | 2fd8227 | 2011-10-03 04:06:05 +0200 | [diff] [blame] | 620 | w = _PyUnicode_New(length); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 621 | if (w == NULL) |
| 622 | return NULL; |
| 623 | copy_length = _PyUnicode_WSTR_LENGTH(unicode); |
| 624 | copy_length = Py_MIN(copy_length, length); |
| 625 | Py_UNICODE_COPY(_PyUnicode_WSTR(w), _PyUnicode_WSTR(unicode), |
| 626 | copy_length); |
| 627 | return (PyObject*)w; |
| 628 | } |
| 629 | } |
| 630 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 631 | /* 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] | 632 | Ux0000 terminated; some code (e.g. new_identifier) |
| 633 | relies on that. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 634 | |
| 635 | XXX This allocator could further be enhanced by assuring that the |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 636 | free list never reduces its size below 1. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 637 | |
| 638 | */ |
| 639 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 640 | #ifdef Py_DEBUG |
| 641 | int unicode_old_new_calls = 0; |
| 642 | #endif |
| 643 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 644 | static PyUnicodeObject * |
| 645 | _PyUnicode_New(Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 646 | { |
| 647 | register PyUnicodeObject *unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 648 | size_t new_size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 649 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 650 | /* Optimization for empty strings */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 651 | if (length == 0 && unicode_empty != NULL) { |
| 652 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 653 | return (PyUnicodeObject*)unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 654 | } |
| 655 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 656 | /* Ensure we won't overflow the size. */ |
| 657 | if (length > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
| 658 | return (PyUnicodeObject *)PyErr_NoMemory(); |
| 659 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 660 | if (length < 0) { |
| 661 | PyErr_SetString(PyExc_SystemError, |
| 662 | "Negative size passed to _PyUnicode_New"); |
| 663 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 664 | } |
| 665 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 666 | #ifdef Py_DEBUG |
| 667 | ++unicode_old_new_calls; |
| 668 | #endif |
| 669 | |
| 670 | unicode = PyObject_New(PyUnicodeObject, &PyUnicode_Type); |
| 671 | if (unicode == NULL) |
| 672 | return NULL; |
| 673 | new_size = sizeof(Py_UNICODE) * ((size_t)length + 1); |
| 674 | _PyUnicode_WSTR(unicode) = (Py_UNICODE*) PyObject_MALLOC(new_size); |
| 675 | if (!_PyUnicode_WSTR(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 676 | PyErr_NoMemory(); |
| 677 | goto onError; |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 678 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 679 | |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 680 | /* Initialize the first element to guard against cases where |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 681 | * the caller fails before initializing str -- unicode_resize() |
| 682 | * reads str[0], and the Keep-Alive optimization can keep memory |
| 683 | * allocated for str alive across a call to unicode_dealloc(unicode). |
| 684 | * We don't want unicode_resize to read uninitialized memory in |
| 685 | * that case. |
| 686 | */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 687 | _PyUnicode_WSTR(unicode)[0] = 0; |
| 688 | _PyUnicode_WSTR(unicode)[length] = 0; |
| 689 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 690 | _PyUnicode_HASH(unicode) = -1; |
| 691 | _PyUnicode_STATE(unicode).interned = 0; |
| 692 | _PyUnicode_STATE(unicode).kind = 0; |
| 693 | _PyUnicode_STATE(unicode).compact = 0; |
| 694 | _PyUnicode_STATE(unicode).ready = 0; |
| 695 | _PyUnicode_STATE(unicode).ascii = 0; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 696 | _PyUnicode_DATA_ANY(unicode) = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 697 | _PyUnicode_LENGTH(unicode) = 0; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 698 | _PyUnicode_UTF8(unicode) = NULL; |
| 699 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 700 | return unicode; |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 701 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 702 | onError: |
Amaury Forgeot d'Arc | 7888d08 | 2008-08-01 01:06:32 +0000 | [diff] [blame] | 703 | /* XXX UNREF/NEWREF interface should be more symmetrical */ |
| 704 | _Py_DEC_REFTOTAL; |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 705 | _Py_ForgetReference((PyObject *)unicode); |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 706 | PyObject_Del(unicode); |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 707 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 708 | } |
| 709 | |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 710 | static const char* |
| 711 | unicode_kind_name(PyObject *unicode) |
| 712 | { |
Victor Stinner | 42dfd71 | 2011-10-03 14:41:45 +0200 | [diff] [blame] | 713 | /* don't check consistency: unicode_kind_name() is called from |
| 714 | _PyUnicode_Dump() */ |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 715 | if (!PyUnicode_IS_COMPACT(unicode)) |
| 716 | { |
| 717 | if (!PyUnicode_IS_READY(unicode)) |
| 718 | return "wstr"; |
| 719 | switch(PyUnicode_KIND(unicode)) |
| 720 | { |
| 721 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 722 | if (PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 723 | return "legacy ascii"; |
| 724 | else |
| 725 | return "legacy latin1"; |
| 726 | case PyUnicode_2BYTE_KIND: |
| 727 | return "legacy UCS2"; |
| 728 | case PyUnicode_4BYTE_KIND: |
| 729 | return "legacy UCS4"; |
| 730 | default: |
| 731 | return "<legacy invalid kind>"; |
| 732 | } |
| 733 | } |
| 734 | assert(PyUnicode_IS_READY(unicode)); |
| 735 | switch(PyUnicode_KIND(unicode)) |
| 736 | { |
| 737 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 738 | if (PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 739 | return "ascii"; |
| 740 | else |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 741 | return "latin1"; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 742 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 743 | return "UCS2"; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 744 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 745 | return "UCS4"; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 746 | default: |
| 747 | return "<invalid compact kind>"; |
| 748 | } |
| 749 | } |
| 750 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 751 | #ifdef Py_DEBUG |
| 752 | int unicode_new_new_calls = 0; |
| 753 | |
| 754 | /* Functions wrapping macros for use in debugger */ |
| 755 | char *_PyUnicode_utf8(void *unicode){ |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 756 | return PyUnicode_UTF8(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 757 | } |
| 758 | |
| 759 | void *_PyUnicode_compact_data(void *unicode) { |
| 760 | return _PyUnicode_COMPACT_DATA(unicode); |
| 761 | } |
| 762 | void *_PyUnicode_data(void *unicode){ |
| 763 | printf("obj %p\n", unicode); |
| 764 | printf("compact %d\n", PyUnicode_IS_COMPACT(unicode)); |
| 765 | printf("compact ascii %d\n", PyUnicode_IS_COMPACT_ASCII(unicode)); |
| 766 | printf("ascii op %p\n", ((void*)((PyASCIIObject*)(unicode) + 1))); |
| 767 | printf("compact op %p\n", ((void*)((PyCompactUnicodeObject*)(unicode) + 1))); |
| 768 | printf("compact data %p\n", _PyUnicode_COMPACT_DATA(unicode)); |
| 769 | return PyUnicode_DATA(unicode); |
| 770 | } |
Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 771 | |
| 772 | void |
| 773 | _PyUnicode_Dump(PyObject *op) |
| 774 | { |
| 775 | PyASCIIObject *ascii = (PyASCIIObject *)op; |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 776 | PyCompactUnicodeObject *compact = (PyCompactUnicodeObject *)op; |
| 777 | PyUnicodeObject *unicode = (PyUnicodeObject *)op; |
| 778 | void *data; |
| 779 | printf("%s: len=%zu, ",unicode_kind_name(op), ascii->length); |
| 780 | if (ascii->state.compact) |
| 781 | data = (compact + 1); |
| 782 | else |
| 783 | data = unicode->data.any; |
| 784 | if (ascii->wstr == data) |
| 785 | printf("shared "); |
| 786 | printf("wstr=%p", ascii->wstr); |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 787 | if (!(ascii->state.ascii == 1 && ascii->state.compact == 1)) { |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 788 | printf(" (%zu), ", compact->wstr_length); |
| 789 | if (!ascii->state.compact && compact->utf8 == unicode->data.any) |
| 790 | printf("shared "); |
| 791 | printf("utf8=%p (%zu)", compact->utf8, compact->utf8_length); |
Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 792 | } |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 793 | printf(", data=%p\n", data); |
Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 794 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 795 | #endif |
| 796 | |
| 797 | PyObject * |
| 798 | PyUnicode_New(Py_ssize_t size, Py_UCS4 maxchar) |
| 799 | { |
| 800 | PyObject *obj; |
| 801 | PyCompactUnicodeObject *unicode; |
| 802 | void *data; |
| 803 | int kind_state; |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 804 | int is_sharing, is_ascii; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 805 | Py_ssize_t char_size; |
| 806 | Py_ssize_t struct_size; |
| 807 | |
| 808 | /* Optimization for empty strings */ |
| 809 | if (size == 0 && unicode_empty != NULL) { |
| 810 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 811 | return unicode_empty; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 812 | } |
| 813 | |
| 814 | #ifdef Py_DEBUG |
| 815 | ++unicode_new_new_calls; |
| 816 | #endif |
| 817 | |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 818 | is_ascii = 0; |
| 819 | is_sharing = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 820 | struct_size = sizeof(PyCompactUnicodeObject); |
| 821 | if (maxchar < 128) { |
| 822 | kind_state = PyUnicode_1BYTE_KIND; |
| 823 | char_size = 1; |
| 824 | is_ascii = 1; |
| 825 | struct_size = sizeof(PyASCIIObject); |
| 826 | } |
| 827 | else if (maxchar < 256) { |
| 828 | kind_state = PyUnicode_1BYTE_KIND; |
| 829 | char_size = 1; |
| 830 | } |
| 831 | else if (maxchar < 65536) { |
| 832 | kind_state = PyUnicode_2BYTE_KIND; |
| 833 | char_size = 2; |
| 834 | if (sizeof(wchar_t) == 2) |
| 835 | is_sharing = 1; |
| 836 | } |
| 837 | else { |
| 838 | kind_state = PyUnicode_4BYTE_KIND; |
| 839 | char_size = 4; |
| 840 | if (sizeof(wchar_t) == 4) |
| 841 | is_sharing = 1; |
| 842 | } |
| 843 | |
| 844 | /* Ensure we won't overflow the size. */ |
| 845 | if (size < 0) { |
| 846 | PyErr_SetString(PyExc_SystemError, |
| 847 | "Negative size passed to PyUnicode_New"); |
| 848 | return NULL; |
| 849 | } |
| 850 | if (size > ((PY_SSIZE_T_MAX - struct_size) / char_size - 1)) |
| 851 | return PyErr_NoMemory(); |
| 852 | |
| 853 | /* Duplicated allocation code from _PyObject_New() instead of a call to |
| 854 | * PyObject_New() so we are able to allocate space for the object and |
| 855 | * it's data buffer. |
| 856 | */ |
| 857 | obj = (PyObject *) PyObject_MALLOC(struct_size + (size + 1) * char_size); |
| 858 | if (obj == NULL) |
| 859 | return PyErr_NoMemory(); |
| 860 | obj = PyObject_INIT(obj, &PyUnicode_Type); |
| 861 | if (obj == NULL) |
| 862 | return NULL; |
| 863 | |
| 864 | unicode = (PyCompactUnicodeObject *)obj; |
| 865 | if (is_ascii) |
| 866 | data = ((PyASCIIObject*)obj) + 1; |
| 867 | else |
| 868 | data = unicode + 1; |
| 869 | _PyUnicode_LENGTH(unicode) = size; |
| 870 | _PyUnicode_HASH(unicode) = -1; |
| 871 | _PyUnicode_STATE(unicode).interned = 0; |
| 872 | _PyUnicode_STATE(unicode).kind = kind_state; |
| 873 | _PyUnicode_STATE(unicode).compact = 1; |
| 874 | _PyUnicode_STATE(unicode).ready = 1; |
| 875 | _PyUnicode_STATE(unicode).ascii = is_ascii; |
| 876 | if (is_ascii) { |
| 877 | ((char*)data)[size] = 0; |
| 878 | _PyUnicode_WSTR(unicode) = NULL; |
| 879 | } |
| 880 | else if (kind_state == PyUnicode_1BYTE_KIND) { |
| 881 | ((char*)data)[size] = 0; |
| 882 | _PyUnicode_WSTR(unicode) = NULL; |
| 883 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 884 | unicode->utf8 = NULL; |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 885 | unicode->utf8_length = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 886 | } |
| 887 | else { |
| 888 | unicode->utf8 = NULL; |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 889 | unicode->utf8_length = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 890 | if (kind_state == PyUnicode_2BYTE_KIND) |
| 891 | ((Py_UCS2*)data)[size] = 0; |
| 892 | else /* kind_state == PyUnicode_4BYTE_KIND */ |
| 893 | ((Py_UCS4*)data)[size] = 0; |
| 894 | if (is_sharing) { |
| 895 | _PyUnicode_WSTR_LENGTH(unicode) = size; |
| 896 | _PyUnicode_WSTR(unicode) = (wchar_t *)data; |
| 897 | } |
| 898 | else { |
| 899 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 900 | _PyUnicode_WSTR(unicode) = NULL; |
| 901 | } |
| 902 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 903 | assert(_PyUnicode_CheckConsistency(unicode, 0)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 904 | return obj; |
| 905 | } |
| 906 | |
| 907 | #if SIZEOF_WCHAR_T == 2 |
| 908 | /* Helper function to convert a 16-bits wchar_t representation to UCS4, this |
| 909 | will decode surrogate pairs, the other conversions are implemented as macros |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 910 | for efficiency. |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 911 | |
| 912 | This function assumes that unicode can hold one more code point than wstr |
| 913 | characters for a terminating null character. */ |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 914 | static void |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 915 | unicode_convert_wchar_to_ucs4(const wchar_t *begin, const wchar_t *end, |
| 916 | PyUnicodeObject *unicode) |
| 917 | { |
| 918 | const wchar_t *iter; |
| 919 | Py_UCS4 *ucs4_out; |
| 920 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 921 | assert(unicode != NULL); |
| 922 | assert(_PyUnicode_CHECK(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 923 | assert(_PyUnicode_KIND(unicode) == PyUnicode_4BYTE_KIND); |
| 924 | ucs4_out = PyUnicode_4BYTE_DATA(unicode); |
| 925 | |
| 926 | for (iter = begin; iter < end; ) { |
| 927 | assert(ucs4_out < (PyUnicode_4BYTE_DATA(unicode) + |
| 928 | _PyUnicode_GET_LENGTH(unicode))); |
| 929 | if (*iter >= 0xD800 && *iter <= 0xDBFF |
| 930 | && (iter+1) < end && iter[1] >= 0xDC00 && iter[1] <= 0xDFFF) |
| 931 | { |
| 932 | *ucs4_out++ = (((iter[0] & 0x3FF)<<10) | (iter[1] & 0x3FF)) + 0x10000; |
| 933 | iter += 2; |
| 934 | } |
| 935 | else { |
| 936 | *ucs4_out++ = *iter; |
| 937 | iter++; |
| 938 | } |
| 939 | } |
| 940 | assert(ucs4_out == (PyUnicode_4BYTE_DATA(unicode) + |
| 941 | _PyUnicode_GET_LENGTH(unicode))); |
| 942 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 943 | } |
| 944 | #endif |
| 945 | |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 946 | static int |
| 947 | _PyUnicode_Dirty(PyObject *unicode) |
| 948 | { |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 949 | assert(_PyUnicode_CHECK(unicode)); |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 950 | if (Py_REFCNT(unicode) != 1) { |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 951 | PyErr_SetString(PyExc_SystemError, |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 952 | "Cannot modify a string having more than 1 reference"); |
| 953 | return -1; |
| 954 | } |
| 955 | _PyUnicode_DIRTY(unicode); |
| 956 | return 0; |
| 957 | } |
| 958 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 959 | static int |
| 960 | _copy_characters(PyObject *to, Py_ssize_t to_start, |
| 961 | PyObject *from, Py_ssize_t from_start, |
| 962 | Py_ssize_t how_many, int check_maxchar) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 963 | { |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 964 | unsigned int from_kind, to_kind; |
| 965 | void *from_data, *to_data; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 966 | int fast; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 967 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 968 | assert(PyUnicode_Check(from)); |
| 969 | assert(PyUnicode_Check(to)); |
| 970 | assert(PyUnicode_IS_READY(from)); |
| 971 | assert(PyUnicode_IS_READY(to)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 972 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 973 | assert(PyUnicode_GET_LENGTH(from) >= how_many); |
| 974 | assert(to_start + how_many <= PyUnicode_GET_LENGTH(to)); |
| 975 | assert(0 <= how_many); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 976 | |
Victor Stinner | f5ca1a2 | 2011-09-28 23:54:59 +0200 | [diff] [blame] | 977 | if (how_many == 0) |
| 978 | return 0; |
| 979 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 980 | from_kind = PyUnicode_KIND(from); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 981 | from_data = PyUnicode_DATA(from); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 982 | to_kind = PyUnicode_KIND(to); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 983 | to_data = PyUnicode_DATA(to); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 984 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 985 | #ifdef Py_DEBUG |
| 986 | if (!check_maxchar |
| 987 | && (from_kind > to_kind |
| 988 | || (!PyUnicode_IS_ASCII(from) && PyUnicode_IS_ASCII(to)))) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 989 | { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 990 | const Py_UCS4 to_maxchar = PyUnicode_MAX_CHAR_VALUE(to); |
| 991 | Py_UCS4 ch; |
| 992 | Py_ssize_t i; |
| 993 | for (i=0; i < how_many; i++) { |
| 994 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
| 995 | assert(ch <= to_maxchar); |
| 996 | } |
| 997 | } |
| 998 | #endif |
| 999 | fast = (from_kind == to_kind); |
| 1000 | if (check_maxchar |
| 1001 | && (!PyUnicode_IS_ASCII(from) && PyUnicode_IS_ASCII(to))) |
| 1002 | { |
| 1003 | /* deny latin1 => ascii */ |
| 1004 | fast = 0; |
| 1005 | } |
| 1006 | |
| 1007 | if (fast) { |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame^] | 1008 | Py_MEMCPY((char*)to_data + to_kind * to_start, |
| 1009 | (char*)from_data + from_kind * from_start, |
| 1010 | to_kind * how_many); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1011 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1012 | else if (from_kind == PyUnicode_1BYTE_KIND |
| 1013 | && to_kind == PyUnicode_2BYTE_KIND) |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1014 | { |
| 1015 | _PyUnicode_CONVERT_BYTES( |
| 1016 | Py_UCS1, Py_UCS2, |
| 1017 | PyUnicode_1BYTE_DATA(from) + from_start, |
| 1018 | PyUnicode_1BYTE_DATA(from) + from_start + how_many, |
| 1019 | PyUnicode_2BYTE_DATA(to) + to_start |
| 1020 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1021 | } |
Victor Stinner | 157f83f | 2011-09-28 21:41:31 +0200 | [diff] [blame] | 1022 | else if (from_kind == PyUnicode_1BYTE_KIND |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1023 | && to_kind == PyUnicode_4BYTE_KIND) |
| 1024 | { |
| 1025 | _PyUnicode_CONVERT_BYTES( |
| 1026 | Py_UCS1, Py_UCS4, |
| 1027 | PyUnicode_1BYTE_DATA(from) + from_start, |
| 1028 | PyUnicode_1BYTE_DATA(from) + from_start + how_many, |
| 1029 | PyUnicode_4BYTE_DATA(to) + to_start |
| 1030 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1031 | } |
| 1032 | else if (from_kind == PyUnicode_2BYTE_KIND |
| 1033 | && to_kind == PyUnicode_4BYTE_KIND) |
| 1034 | { |
| 1035 | _PyUnicode_CONVERT_BYTES( |
| 1036 | Py_UCS2, Py_UCS4, |
| 1037 | PyUnicode_2BYTE_DATA(from) + from_start, |
| 1038 | PyUnicode_2BYTE_DATA(from) + from_start + how_many, |
| 1039 | PyUnicode_4BYTE_DATA(to) + to_start |
| 1040 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1041 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1042 | else { |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 1043 | /* check if max_char(from substring) <= max_char(to) */ |
| 1044 | if (from_kind > to_kind |
| 1045 | /* latin1 => ascii */ |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1046 | || (!PyUnicode_IS_ASCII(from) && PyUnicode_IS_ASCII(to))) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 1047 | { |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1048 | /* slow path to check for character overflow */ |
| 1049 | const Py_UCS4 to_maxchar = PyUnicode_MAX_CHAR_VALUE(to); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1050 | Py_UCS4 ch; |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1051 | Py_ssize_t i; |
| 1052 | |
Victor Stinner | 56c161a | 2011-10-06 02:47:11 +0200 | [diff] [blame] | 1053 | #ifdef Py_DEBUG |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1054 | for (i=0; i < how_many; i++) { |
| 1055 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
Victor Stinner | 56c161a | 2011-10-06 02:47:11 +0200 | [diff] [blame] | 1056 | assert(ch <= to_maxchar); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1057 | PyUnicode_WRITE(to_kind, to_data, to_start + i, ch); |
| 1058 | } |
Victor Stinner | 56c161a | 2011-10-06 02:47:11 +0200 | [diff] [blame] | 1059 | #else |
| 1060 | if (!check_maxchar) { |
| 1061 | for (i=0; i < how_many; i++) { |
| 1062 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
| 1063 | PyUnicode_WRITE(to_kind, to_data, to_start + i, ch); |
| 1064 | } |
| 1065 | } |
| 1066 | else { |
| 1067 | for (i=0; i < how_many; i++) { |
| 1068 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
| 1069 | if (ch > to_maxchar) |
| 1070 | return 1; |
| 1071 | PyUnicode_WRITE(to_kind, to_data, to_start + i, ch); |
| 1072 | } |
| 1073 | } |
| 1074 | #endif |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1075 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1076 | else { |
Victor Stinner | 56c161a | 2011-10-06 02:47:11 +0200 | [diff] [blame] | 1077 | assert(0 && "inconsistent state"); |
| 1078 | return 1; |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1079 | } |
| 1080 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1081 | return 0; |
| 1082 | } |
| 1083 | |
| 1084 | static void |
| 1085 | copy_characters(PyObject *to, Py_ssize_t to_start, |
| 1086 | PyObject *from, Py_ssize_t from_start, |
| 1087 | Py_ssize_t how_many) |
| 1088 | { |
| 1089 | (void)_copy_characters(to, to_start, from, from_start, how_many, 0); |
| 1090 | } |
| 1091 | |
| 1092 | Py_ssize_t |
| 1093 | PyUnicode_CopyCharacters(PyObject *to, Py_ssize_t to_start, |
| 1094 | PyObject *from, Py_ssize_t from_start, |
| 1095 | Py_ssize_t how_many) |
| 1096 | { |
| 1097 | int err; |
| 1098 | |
| 1099 | if (!PyUnicode_Check(from) || !PyUnicode_Check(to)) { |
| 1100 | PyErr_BadInternalCall(); |
| 1101 | return -1; |
| 1102 | } |
| 1103 | |
| 1104 | if (PyUnicode_READY(from)) |
| 1105 | return -1; |
| 1106 | if (PyUnicode_READY(to)) |
| 1107 | return -1; |
| 1108 | |
| 1109 | how_many = Py_MIN(PyUnicode_GET_LENGTH(from), how_many); |
| 1110 | if (to_start + how_many > PyUnicode_GET_LENGTH(to)) { |
| 1111 | PyErr_Format(PyExc_SystemError, |
| 1112 | "Cannot write %zi characters at %zi " |
| 1113 | "in a string of %zi characters", |
| 1114 | how_many, to_start, PyUnicode_GET_LENGTH(to)); |
| 1115 | return -1; |
| 1116 | } |
| 1117 | |
| 1118 | if (how_many == 0) |
| 1119 | return 0; |
| 1120 | |
| 1121 | if (_PyUnicode_Dirty(to)) |
| 1122 | return -1; |
| 1123 | |
| 1124 | err = _copy_characters(to, to_start, from, from_start, how_many, 1); |
| 1125 | if (err) { |
| 1126 | PyErr_Format(PyExc_SystemError, |
| 1127 | "Cannot copy %s characters " |
| 1128 | "into a string of %s characters", |
| 1129 | unicode_kind_name(from), |
| 1130 | unicode_kind_name(to)); |
| 1131 | return -1; |
| 1132 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1133 | return how_many; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1134 | } |
| 1135 | |
Victor Stinner | 1722216 | 2011-09-28 22:15:37 +0200 | [diff] [blame] | 1136 | /* Find the maximum code point and count the number of surrogate pairs so a |
| 1137 | correct string length can be computed before converting a string to UCS4. |
| 1138 | This function counts single surrogates as a character and not as a pair. |
| 1139 | |
| 1140 | Return 0 on success, or -1 on error. */ |
| 1141 | static int |
| 1142 | find_maxchar_surrogates(const wchar_t *begin, const wchar_t *end, |
| 1143 | Py_UCS4 *maxchar, Py_ssize_t *num_surrogates) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1144 | { |
| 1145 | const wchar_t *iter; |
| 1146 | |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1147 | assert(num_surrogates != NULL && maxchar != NULL); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1148 | *num_surrogates = 0; |
| 1149 | *maxchar = 0; |
| 1150 | |
| 1151 | for (iter = begin; iter < end; ) { |
Victor Stinner | ae86485 | 2011-10-05 14:02:44 +0200 | [diff] [blame] | 1152 | if (*iter > *maxchar) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1153 | *maxchar = *iter; |
Victor Stinner | ae86485 | 2011-10-05 14:02:44 +0200 | [diff] [blame] | 1154 | #if SIZEOF_WCHAR_T != 2 |
| 1155 | if (*maxchar >= 0x10000) |
| 1156 | return 0; |
| 1157 | #endif |
| 1158 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1159 | #if SIZEOF_WCHAR_T == 2 |
| 1160 | if (*iter >= 0xD800 && *iter <= 0xDBFF |
| 1161 | && (iter+1) < end && iter[1] >= 0xDC00 && iter[1] <= 0xDFFF) |
| 1162 | { |
| 1163 | Py_UCS4 surrogate_val; |
| 1164 | surrogate_val = (((iter[0] & 0x3FF)<<10) |
| 1165 | | (iter[1] & 0x3FF)) + 0x10000; |
| 1166 | ++(*num_surrogates); |
| 1167 | if (surrogate_val > *maxchar) |
| 1168 | *maxchar = surrogate_val; |
| 1169 | iter += 2; |
| 1170 | } |
| 1171 | else |
| 1172 | iter++; |
| 1173 | #else |
| 1174 | iter++; |
| 1175 | #endif |
| 1176 | } |
| 1177 | return 0; |
| 1178 | } |
| 1179 | |
| 1180 | #ifdef Py_DEBUG |
| 1181 | int unicode_ready_calls = 0; |
| 1182 | #endif |
| 1183 | |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 1184 | static int |
| 1185 | unicode_ready(PyObject **p_obj, int replace) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1186 | { |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 1187 | PyUnicodeObject *unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1188 | wchar_t *end; |
| 1189 | Py_UCS4 maxchar = 0; |
| 1190 | Py_ssize_t num_surrogates; |
| 1191 | #if SIZEOF_WCHAR_T == 2 |
| 1192 | Py_ssize_t length_wo_surrogates; |
| 1193 | #endif |
| 1194 | |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 1195 | assert(p_obj != NULL); |
| 1196 | unicode = (PyUnicodeObject *)*p_obj; |
| 1197 | |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 1198 | /* _PyUnicode_Ready() is only intended for old-style API usage where |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1199 | strings were created using _PyObject_New() and where no canonical |
| 1200 | representation (the str field) has been set yet aka strings |
| 1201 | which are not yet ready. */ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1202 | assert(_PyUnicode_CHECK(unicode)); |
| 1203 | assert(_PyUnicode_KIND(unicode) == PyUnicode_WCHAR_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1204 | assert(_PyUnicode_WSTR(unicode) != NULL); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1205 | assert(_PyUnicode_DATA_ANY(unicode) == NULL); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1206 | assert(_PyUnicode_UTF8(unicode) == NULL); |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1207 | /* Actually, it should neither be interned nor be anything else: */ |
| 1208 | assert(_PyUnicode_STATE(unicode).interned == SSTATE_NOT_INTERNED); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1209 | |
| 1210 | #ifdef Py_DEBUG |
| 1211 | ++unicode_ready_calls; |
| 1212 | #endif |
| 1213 | |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 1214 | #ifdef Py_DEBUG |
| 1215 | assert(!replace || Py_REFCNT(unicode) == 1); |
| 1216 | #else |
| 1217 | if (replace && Py_REFCNT(unicode) != 1) |
| 1218 | replace = 0; |
| 1219 | #endif |
| 1220 | if (replace) { |
| 1221 | Py_ssize_t len = _PyUnicode_WSTR_LENGTH(unicode); |
| 1222 | wchar_t *wstr = _PyUnicode_WSTR(unicode); |
| 1223 | /* Optimization for empty strings */ |
| 1224 | if (len == 0) { |
| 1225 | Py_INCREF(unicode_empty); |
| 1226 | Py_DECREF(*p_obj); |
| 1227 | *p_obj = unicode_empty; |
| 1228 | return 0; |
| 1229 | } |
| 1230 | if (len == 1 && wstr[0] < 256) { |
| 1231 | PyObject *latin1_char = get_latin1_char((unsigned char)wstr[0]); |
| 1232 | if (latin1_char == NULL) |
| 1233 | return -1; |
| 1234 | Py_DECREF(*p_obj); |
| 1235 | *p_obj = latin1_char; |
| 1236 | return 0; |
| 1237 | } |
| 1238 | } |
| 1239 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1240 | end = _PyUnicode_WSTR(unicode) + _PyUnicode_WSTR_LENGTH(unicode); |
Victor Stinner | 1722216 | 2011-09-28 22:15:37 +0200 | [diff] [blame] | 1241 | if (find_maxchar_surrogates(_PyUnicode_WSTR(unicode), end, |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1242 | &maxchar, &num_surrogates) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1243 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1244 | |
| 1245 | if (maxchar < 256) { |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1246 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC(_PyUnicode_WSTR_LENGTH(unicode) + 1); |
| 1247 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1248 | PyErr_NoMemory(); |
| 1249 | return -1; |
| 1250 | } |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1251 | _PyUnicode_CONVERT_BYTES(wchar_t, unsigned char, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1252 | _PyUnicode_WSTR(unicode), end, |
| 1253 | PyUnicode_1BYTE_DATA(unicode)); |
| 1254 | PyUnicode_1BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1255 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1256 | _PyUnicode_STATE(unicode).kind = PyUnicode_1BYTE_KIND; |
| 1257 | if (maxchar < 128) { |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1258 | _PyUnicode_STATE(unicode).ascii = 1; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1259 | _PyUnicode_UTF8(unicode) = _PyUnicode_DATA_ANY(unicode); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1260 | _PyUnicode_UTF8_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1261 | } |
| 1262 | else { |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1263 | _PyUnicode_STATE(unicode).ascii = 0; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1264 | _PyUnicode_UTF8(unicode) = NULL; |
| 1265 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1266 | } |
| 1267 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1268 | _PyUnicode_WSTR(unicode) = NULL; |
| 1269 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1270 | } |
| 1271 | /* In this case we might have to convert down from 4-byte native |
| 1272 | wchar_t to 2-byte unicode. */ |
| 1273 | else if (maxchar < 65536) { |
| 1274 | assert(num_surrogates == 0 && |
| 1275 | "FindMaxCharAndNumSurrogatePairs() messed up"); |
| 1276 | |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1277 | #if SIZEOF_WCHAR_T == 2 |
| 1278 | /* We can share representations and are done. */ |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1279 | _PyUnicode_DATA_ANY(unicode) = _PyUnicode_WSTR(unicode); |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1280 | PyUnicode_2BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1281 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1282 | _PyUnicode_STATE(unicode).kind = PyUnicode_2BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1283 | _PyUnicode_UTF8(unicode) = NULL; |
| 1284 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1285 | #else |
| 1286 | /* sizeof(wchar_t) == 4 */ |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1287 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC( |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1288 | 2 * (_PyUnicode_WSTR_LENGTH(unicode) + 1)); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1289 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1290 | PyErr_NoMemory(); |
| 1291 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1292 | } |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1293 | _PyUnicode_CONVERT_BYTES(wchar_t, Py_UCS2, |
| 1294 | _PyUnicode_WSTR(unicode), end, |
| 1295 | PyUnicode_2BYTE_DATA(unicode)); |
| 1296 | PyUnicode_2BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1297 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1298 | _PyUnicode_STATE(unicode).kind = PyUnicode_2BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1299 | _PyUnicode_UTF8(unicode) = NULL; |
| 1300 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1301 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1302 | _PyUnicode_WSTR(unicode) = NULL; |
| 1303 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1304 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1305 | } |
| 1306 | /* maxchar exeeds 16 bit, wee need 4 bytes for unicode characters */ |
| 1307 | else { |
| 1308 | #if SIZEOF_WCHAR_T == 2 |
| 1309 | /* in case the native representation is 2-bytes, we need to allocate a |
| 1310 | new normalized 4-byte version. */ |
| 1311 | length_wo_surrogates = _PyUnicode_WSTR_LENGTH(unicode) - num_surrogates; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1312 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC(4 * (length_wo_surrogates + 1)); |
| 1313 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1314 | PyErr_NoMemory(); |
| 1315 | return -1; |
| 1316 | } |
| 1317 | _PyUnicode_LENGTH(unicode) = length_wo_surrogates; |
| 1318 | _PyUnicode_STATE(unicode).kind = PyUnicode_4BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1319 | _PyUnicode_UTF8(unicode) = NULL; |
| 1320 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 126c559 | 2011-10-03 04:17:10 +0200 | [diff] [blame] | 1321 | /* unicode_convert_wchar_to_ucs4() requires a ready string */ |
| 1322 | _PyUnicode_STATE(unicode).ready = 1; |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1323 | unicode_convert_wchar_to_ucs4(_PyUnicode_WSTR(unicode), end, unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1324 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1325 | _PyUnicode_WSTR(unicode) = NULL; |
| 1326 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1327 | #else |
| 1328 | assert(num_surrogates == 0); |
| 1329 | |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1330 | _PyUnicode_DATA_ANY(unicode) = _PyUnicode_WSTR(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1331 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1332 | _PyUnicode_UTF8(unicode) = NULL; |
| 1333 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1334 | _PyUnicode_STATE(unicode).kind = PyUnicode_4BYTE_KIND; |
| 1335 | #endif |
| 1336 | PyUnicode_4BYTE_DATA(unicode)[_PyUnicode_LENGTH(unicode)] = '\0'; |
| 1337 | } |
| 1338 | _PyUnicode_STATE(unicode).ready = 1; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1339 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1340 | return 0; |
| 1341 | } |
| 1342 | |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 1343 | int |
| 1344 | _PyUnicode_ReadyReplace(PyObject **op) |
| 1345 | { |
| 1346 | return unicode_ready(op, 1); |
| 1347 | } |
| 1348 | |
| 1349 | int |
| 1350 | _PyUnicode_Ready(PyObject *op) |
| 1351 | { |
| 1352 | return unicode_ready(&op, 0); |
| 1353 | } |
| 1354 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1355 | static void |
| 1356 | unicode_dealloc(register PyUnicodeObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1357 | { |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1358 | switch (PyUnicode_CHECK_INTERNED(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1359 | case SSTATE_NOT_INTERNED: |
| 1360 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1361 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1362 | case SSTATE_INTERNED_MORTAL: |
| 1363 | /* revive dead object temporarily for DelItem */ |
| 1364 | Py_REFCNT(unicode) = 3; |
| 1365 | if (PyDict_DelItem(interned, (PyObject *)unicode) != 0) |
| 1366 | Py_FatalError( |
| 1367 | "deletion of interned string failed"); |
| 1368 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1369 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1370 | case SSTATE_INTERNED_IMMORTAL: |
| 1371 | Py_FatalError("Immortal interned string died."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1372 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1373 | default: |
| 1374 | Py_FatalError("Inconsistent interned string state."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1375 | } |
| 1376 | |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 1377 | if (_PyUnicode_HAS_WSTR_MEMORY(unicode)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1378 | PyObject_DEL(_PyUnicode_WSTR(unicode)); |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 1379 | if (_PyUnicode_HAS_UTF8_MEMORY(unicode)) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1380 | PyObject_DEL(_PyUnicode_UTF8(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1381 | |
| 1382 | if (PyUnicode_IS_COMPACT(unicode)) { |
| 1383 | Py_TYPE(unicode)->tp_free((PyObject *)unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1384 | } |
| 1385 | else { |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1386 | if (_PyUnicode_DATA_ANY(unicode)) |
| 1387 | PyObject_DEL(_PyUnicode_DATA_ANY(unicode)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1388 | Py_TYPE(unicode)->tp_free((PyObject *)unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1389 | } |
| 1390 | } |
| 1391 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1392 | #ifdef Py_DEBUG |
| 1393 | static int |
| 1394 | unicode_is_singleton(PyObject *unicode) |
| 1395 | { |
| 1396 | PyASCIIObject *ascii = (PyASCIIObject *)unicode; |
| 1397 | if (unicode == unicode_empty) |
| 1398 | return 1; |
| 1399 | if (ascii->state.kind != PyUnicode_WCHAR_KIND && ascii->length == 1) |
| 1400 | { |
| 1401 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, 0); |
| 1402 | if (ch < 256 && unicode_latin1[ch] == unicode) |
| 1403 | return 1; |
| 1404 | } |
| 1405 | return 0; |
| 1406 | } |
| 1407 | #endif |
| 1408 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1409 | static int |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1410 | unicode_resizable(PyObject *unicode) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1411 | { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1412 | if (Py_REFCNT(unicode) != 1) |
| 1413 | return 0; |
| 1414 | if (PyUnicode_CHECK_INTERNED(unicode)) |
| 1415 | return 0; |
Victor Stinner | 77bb47b | 2011-10-03 20:06:05 +0200 | [diff] [blame] | 1416 | #ifdef Py_DEBUG |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1417 | /* singleton refcount is greater than 1 */ |
| 1418 | assert(!unicode_is_singleton(unicode)); |
Victor Stinner | 77bb47b | 2011-10-03 20:06:05 +0200 | [diff] [blame] | 1419 | #endif |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1420 | return 1; |
| 1421 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1422 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1423 | static int |
| 1424 | unicode_resize(PyObject **p_unicode, Py_ssize_t length) |
| 1425 | { |
| 1426 | PyObject *unicode; |
| 1427 | Py_ssize_t old_length; |
| 1428 | |
| 1429 | assert(p_unicode != NULL); |
| 1430 | unicode = *p_unicode; |
| 1431 | |
| 1432 | assert(unicode != NULL); |
| 1433 | assert(PyUnicode_Check(unicode)); |
| 1434 | assert(0 <= length); |
| 1435 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1436 | if (_PyUnicode_KIND(unicode) == PyUnicode_WCHAR_KIND) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1437 | old_length = PyUnicode_WSTR_LENGTH(unicode); |
| 1438 | else |
| 1439 | old_length = PyUnicode_GET_LENGTH(unicode); |
| 1440 | if (old_length == length) |
| 1441 | return 0; |
| 1442 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1443 | if (!unicode_resizable(unicode)) { |
| 1444 | PyObject *copy = resize_copy(unicode, length); |
| 1445 | if (copy == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1446 | return -1; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1447 | Py_DECREF(*p_unicode); |
| 1448 | *p_unicode = copy; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1449 | return 0; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1450 | } |
| 1451 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1452 | if (PyUnicode_IS_COMPACT(unicode)) { |
| 1453 | *p_unicode = resize_compact(unicode, length); |
| 1454 | if (*p_unicode == NULL) |
| 1455 | return -1; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1456 | assert(_PyUnicode_CheckConsistency(*p_unicode, 0)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1457 | return 0; |
Benjamin Peterson | 4bfce8f | 2011-10-03 19:35:07 -0400 | [diff] [blame] | 1458 | } |
| 1459 | return resize_inplace((PyUnicodeObject*)unicode, length); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1460 | } |
| 1461 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1462 | int |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1463 | PyUnicode_Resize(PyObject **p_unicode, Py_ssize_t length) |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 1464 | { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1465 | PyObject *unicode; |
| 1466 | if (p_unicode == NULL) { |
| 1467 | PyErr_BadInternalCall(); |
| 1468 | return -1; |
| 1469 | } |
| 1470 | unicode = *p_unicode; |
| 1471 | if (unicode == NULL || !PyUnicode_Check(unicode) || length < 0 |
| 1472 | || _PyUnicode_KIND(unicode) != PyUnicode_WCHAR_KIND) |
| 1473 | { |
| 1474 | PyErr_BadInternalCall(); |
| 1475 | return -1; |
| 1476 | } |
| 1477 | return unicode_resize(p_unicode, length); |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 1478 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1479 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1480 | static PyObject* |
| 1481 | get_latin1_char(unsigned char ch) |
| 1482 | { |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1483 | PyObject *unicode = unicode_latin1[ch]; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1484 | if (!unicode) { |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1485 | unicode = PyUnicode_New(1, ch); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1486 | if (!unicode) |
| 1487 | return NULL; |
| 1488 | PyUnicode_1BYTE_DATA(unicode)[0] = ch; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1489 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1490 | unicode_latin1[ch] = unicode; |
| 1491 | } |
| 1492 | Py_INCREF(unicode); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1493 | return unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1494 | } |
| 1495 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1496 | PyObject * |
| 1497 | PyUnicode_FromUnicode(const Py_UNICODE *u, Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1498 | { |
| 1499 | PyUnicodeObject *unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1500 | Py_UCS4 maxchar = 0; |
| 1501 | Py_ssize_t num_surrogates; |
| 1502 | |
| 1503 | if (u == NULL) |
| 1504 | return (PyObject*)_PyUnicode_New(size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1505 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1506 | /* If the Unicode data is known at construction time, we can apply |
| 1507 | some optimizations which share commonly used objects. */ |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1508 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1509 | /* Optimization for empty strings */ |
| 1510 | if (size == 0 && unicode_empty != NULL) { |
| 1511 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1512 | return unicode_empty; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1513 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1514 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1515 | /* Single character Unicode objects in the Latin-1 range are |
| 1516 | shared when using this constructor */ |
| 1517 | if (size == 1 && *u < 256) |
| 1518 | return get_latin1_char((unsigned char)*u); |
| 1519 | |
| 1520 | /* If not empty and not single character, copy the Unicode data |
| 1521 | into the new object */ |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1522 | if (find_maxchar_surrogates(u, u + size, |
| 1523 | &maxchar, &num_surrogates) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1524 | return NULL; |
| 1525 | |
| 1526 | unicode = (PyUnicodeObject *) PyUnicode_New(size - num_surrogates, |
| 1527 | maxchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1528 | if (!unicode) |
| 1529 | return NULL; |
| 1530 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1531 | switch (PyUnicode_KIND(unicode)) { |
| 1532 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1533 | _PyUnicode_CONVERT_BYTES(Py_UNICODE, unsigned char, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1534 | u, u + size, PyUnicode_1BYTE_DATA(unicode)); |
| 1535 | break; |
| 1536 | case PyUnicode_2BYTE_KIND: |
| 1537 | #if Py_UNICODE_SIZE == 2 |
| 1538 | Py_MEMCPY(PyUnicode_2BYTE_DATA(unicode), u, size * 2); |
| 1539 | #else |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1540 | _PyUnicode_CONVERT_BYTES(Py_UNICODE, Py_UCS2, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1541 | u, u + size, PyUnicode_2BYTE_DATA(unicode)); |
| 1542 | #endif |
| 1543 | break; |
| 1544 | case PyUnicode_4BYTE_KIND: |
| 1545 | #if SIZEOF_WCHAR_T == 2 |
| 1546 | /* This is the only case which has to process surrogates, thus |
| 1547 | a simple copy loop is not enough and we need a function. */ |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1548 | unicode_convert_wchar_to_ucs4(u, u + size, unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1549 | #else |
| 1550 | assert(num_surrogates == 0); |
| 1551 | Py_MEMCPY(PyUnicode_4BYTE_DATA(unicode), u, size * 4); |
| 1552 | #endif |
| 1553 | break; |
| 1554 | default: |
| 1555 | assert(0 && "Impossible state"); |
| 1556 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1557 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1558 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1559 | return (PyObject *)unicode; |
| 1560 | } |
| 1561 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1562 | PyObject * |
| 1563 | PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size) |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1564 | { |
| 1565 | PyUnicodeObject *unicode; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1566 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1567 | if (size < 0) { |
| 1568 | PyErr_SetString(PyExc_SystemError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1569 | "Negative size passed to PyUnicode_FromStringAndSize"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1570 | return NULL; |
| 1571 | } |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1572 | |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1573 | /* 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] | 1574 | some optimizations which share commonly used objects. |
| 1575 | Also, this means the input must be UTF-8, so fall back to the |
| 1576 | UTF-8 decoder at the end. */ |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1577 | if (u != NULL) { |
| 1578 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [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; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1583 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1584 | |
| 1585 | /* Single characters are shared when using this constructor. |
| 1586 | Restrict to ASCII, since the input must be UTF-8. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1587 | if (size == 1 && Py_CHARMASK(*u) < 128) |
| 1588 | return get_latin1_char(Py_CHARMASK(*u)); |
Martin v. Löwis | 9c12106 | 2007-08-05 20:26:11 +0000 | [diff] [blame] | 1589 | |
| 1590 | return PyUnicode_DecodeUTF8(u, size, NULL); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1591 | } |
| 1592 | |
Walter Dörwald | 5550731 | 2007-05-18 13:12:10 +0000 | [diff] [blame] | 1593 | unicode = _PyUnicode_New(size); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1594 | if (!unicode) |
| 1595 | return NULL; |
| 1596 | |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1597 | return (PyObject *)unicode; |
| 1598 | } |
| 1599 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1600 | PyObject * |
| 1601 | PyUnicode_FromString(const char *u) |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1602 | { |
| 1603 | size_t size = strlen(u); |
| 1604 | if (size > PY_SSIZE_T_MAX) { |
| 1605 | PyErr_SetString(PyExc_OverflowError, "input too long"); |
| 1606 | return NULL; |
| 1607 | } |
| 1608 | |
| 1609 | return PyUnicode_FromStringAndSize(u, size); |
| 1610 | } |
| 1611 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1612 | static PyObject* |
Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 1613 | unicode_fromascii(const unsigned char* s, Py_ssize_t size) |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1614 | { |
Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 1615 | PyObject *res; |
| 1616 | #ifdef Py_DEBUG |
| 1617 | const unsigned char *p; |
| 1618 | const unsigned char *end = s + size; |
| 1619 | for (p=s; p < end; p++) { |
| 1620 | assert(*p < 128); |
| 1621 | } |
| 1622 | #endif |
Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 1623 | if (size == 1) |
| 1624 | return get_latin1_char(s[0]); |
Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 1625 | res = PyUnicode_New(size, 127); |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1626 | if (!res) |
| 1627 | return NULL; |
Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 1628 | memcpy(PyUnicode_1BYTE_DATA(res), s, size); |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1629 | return res; |
| 1630 | } |
| 1631 | |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 1632 | static Py_UCS4 |
| 1633 | kind_maxchar_limit(unsigned int kind) |
| 1634 | { |
| 1635 | switch(kind) { |
| 1636 | case PyUnicode_1BYTE_KIND: |
| 1637 | return 0x80; |
| 1638 | case PyUnicode_2BYTE_KIND: |
| 1639 | return 0x100; |
| 1640 | case PyUnicode_4BYTE_KIND: |
| 1641 | return 0x10000; |
| 1642 | default: |
| 1643 | assert(0 && "invalid kind"); |
| 1644 | return 0x10ffff; |
| 1645 | } |
| 1646 | } |
| 1647 | |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1648 | static PyObject* |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1649 | _PyUnicode_FromUCS1(const unsigned char* u, Py_ssize_t size) |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1650 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1651 | PyObject *res; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1652 | unsigned char max_char = 127; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1653 | Py_ssize_t i; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1654 | |
| 1655 | assert(size >= 0); |
Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 1656 | if (size == 1) |
| 1657 | return get_latin1_char(u[0]); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1658 | for (i = 0; i < size; i++) { |
| 1659 | if (u[i] & 0x80) { |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1660 | max_char = 255; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1661 | break; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1662 | } |
| 1663 | } |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1664 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1665 | if (!res) |
| 1666 | return NULL; |
| 1667 | memcpy(PyUnicode_1BYTE_DATA(res), u, size); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1668 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1669 | return res; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1670 | } |
| 1671 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1672 | static PyObject* |
| 1673 | _PyUnicode_FromUCS2(const Py_UCS2 *u, Py_ssize_t size) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1674 | { |
| 1675 | PyObject *res; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1676 | Py_UCS2 max_char = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1677 | Py_ssize_t i; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1678 | |
| 1679 | assert(size >= 0); |
Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 1680 | if (size == 1 && u[0] < 256) |
| 1681 | return get_latin1_char(u[0]); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1682 | for (i = 0; i < size; i++) { |
| 1683 | if (u[i] > max_char) { |
| 1684 | max_char = u[i]; |
| 1685 | if (max_char >= 256) |
| 1686 | break; |
| 1687 | } |
| 1688 | } |
| 1689 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1690 | if (!res) |
| 1691 | return NULL; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1692 | if (max_char >= 256) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1693 | memcpy(PyUnicode_2BYTE_DATA(res), u, sizeof(Py_UCS2)*size); |
| 1694 | else |
| 1695 | for (i = 0; i < size; i++) |
| 1696 | PyUnicode_1BYTE_DATA(res)[i] = (Py_UCS1)u[i]; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1697 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1698 | return res; |
| 1699 | } |
| 1700 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1701 | static PyObject* |
| 1702 | _PyUnicode_FromUCS4(const Py_UCS4 *u, Py_ssize_t size) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1703 | { |
| 1704 | PyObject *res; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1705 | Py_UCS4 max_char = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1706 | Py_ssize_t i; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1707 | |
| 1708 | assert(size >= 0); |
Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 1709 | if (size == 1 && u[0] < 256) |
| 1710 | return get_latin1_char(u[0]); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1711 | for (i = 0; i < size; i++) { |
| 1712 | if (u[i] > max_char) { |
| 1713 | max_char = u[i]; |
| 1714 | if (max_char >= 0x10000) |
| 1715 | break; |
| 1716 | } |
| 1717 | } |
| 1718 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1719 | if (!res) |
| 1720 | return NULL; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1721 | if (max_char >= 0x10000) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1722 | memcpy(PyUnicode_4BYTE_DATA(res), u, sizeof(Py_UCS4)*size); |
| 1723 | else { |
| 1724 | int kind = PyUnicode_KIND(res); |
| 1725 | void *data = PyUnicode_DATA(res); |
| 1726 | for (i = 0; i < size; i++) |
| 1727 | PyUnicode_WRITE(kind, data, i, u[i]); |
| 1728 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1729 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1730 | return res; |
| 1731 | } |
| 1732 | |
| 1733 | PyObject* |
| 1734 | PyUnicode_FromKindAndData(int kind, const void *buffer, Py_ssize_t size) |
| 1735 | { |
| 1736 | switch(kind) { |
| 1737 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1738 | return _PyUnicode_FromUCS1(buffer, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1739 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1740 | return _PyUnicode_FromUCS2(buffer, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1741 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1742 | return _PyUnicode_FromUCS4(buffer, size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1743 | default: |
| 1744 | assert(0 && "invalid kind"); |
| 1745 | PyErr_SetString(PyExc_SystemError, "invalid kind"); |
| 1746 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1747 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1748 | } |
| 1749 | |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1750 | /* Ensure that a string uses the most efficient storage, if it is not the |
| 1751 | case: create a new string with of the right kind. Write NULL into *p_unicode |
| 1752 | on error. */ |
| 1753 | void |
| 1754 | unicode_adjust_maxchar(PyObject **p_unicode) |
| 1755 | { |
| 1756 | PyObject *unicode, *copy; |
| 1757 | Py_UCS4 max_char; |
| 1758 | Py_ssize_t i, len; |
| 1759 | unsigned int kind; |
| 1760 | |
| 1761 | assert(p_unicode != NULL); |
| 1762 | unicode = *p_unicode; |
| 1763 | assert(PyUnicode_IS_READY(unicode)); |
| 1764 | if (PyUnicode_IS_ASCII(unicode)) |
| 1765 | return; |
| 1766 | |
| 1767 | len = PyUnicode_GET_LENGTH(unicode); |
| 1768 | kind = PyUnicode_KIND(unicode); |
| 1769 | if (kind == PyUnicode_1BYTE_KIND) { |
| 1770 | const Py_UCS1 *u = PyUnicode_1BYTE_DATA(unicode); |
| 1771 | for (i = 0; i < len; i++) { |
| 1772 | if (u[i] & 0x80) |
| 1773 | return; |
| 1774 | } |
| 1775 | max_char = 127; |
| 1776 | } |
| 1777 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 1778 | const Py_UCS2 *u = PyUnicode_2BYTE_DATA(unicode); |
| 1779 | max_char = 0; |
| 1780 | for (i = 0; i < len; i++) { |
| 1781 | if (u[i] > max_char) { |
| 1782 | max_char = u[i]; |
| 1783 | if (max_char >= 256) |
| 1784 | return; |
| 1785 | } |
| 1786 | } |
| 1787 | } |
| 1788 | else { |
Antoine Pitrou | 15a66cf | 2011-10-06 15:25:32 +0200 | [diff] [blame] | 1789 | const Py_UCS4 *u; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1790 | assert(kind == PyUnicode_4BYTE_KIND); |
Antoine Pitrou | 15a66cf | 2011-10-06 15:25:32 +0200 | [diff] [blame] | 1791 | u = PyUnicode_4BYTE_DATA(unicode); |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1792 | max_char = 0; |
| 1793 | for (i = 0; i < len; i++) { |
| 1794 | if (u[i] > max_char) { |
| 1795 | max_char = u[i]; |
| 1796 | if (max_char >= 0x10000) |
| 1797 | return; |
| 1798 | } |
| 1799 | } |
| 1800 | } |
Victor Stinner | 200f213 | 2011-10-06 13:27:56 +0200 | [diff] [blame] | 1801 | assert(max_char < PyUnicode_MAX_CHAR_VALUE(unicode)); |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1802 | copy = PyUnicode_New(len, max_char); |
| 1803 | copy_characters(copy, 0, unicode, 0, len); |
| 1804 | Py_DECREF(unicode); |
| 1805 | *p_unicode = copy; |
| 1806 | } |
| 1807 | |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 1808 | PyObject* |
| 1809 | PyUnicode_Copy(PyObject *unicode) |
| 1810 | { |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 1811 | Py_ssize_t size; |
| 1812 | PyObject *copy; |
| 1813 | void *data; |
| 1814 | |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 1815 | if (!PyUnicode_Check(unicode)) { |
| 1816 | PyErr_BadInternalCall(); |
| 1817 | return NULL; |
| 1818 | } |
| 1819 | if (PyUnicode_READY(unicode)) |
| 1820 | return NULL; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 1821 | |
| 1822 | size = PyUnicode_GET_LENGTH(unicode); |
| 1823 | copy = PyUnicode_New(size, PyUnicode_MAX_CHAR_VALUE(unicode)); |
| 1824 | if (!copy) |
| 1825 | return NULL; |
| 1826 | assert(PyUnicode_KIND(copy) == PyUnicode_KIND(unicode)); |
| 1827 | |
| 1828 | data = PyUnicode_DATA(unicode); |
| 1829 | switch (PyUnicode_KIND(unicode)) |
| 1830 | { |
| 1831 | case PyUnicode_1BYTE_KIND: |
| 1832 | memcpy(PyUnicode_1BYTE_DATA(copy), data, size); |
| 1833 | break; |
| 1834 | case PyUnicode_2BYTE_KIND: |
| 1835 | memcpy(PyUnicode_2BYTE_DATA(copy), data, sizeof(Py_UCS2) * size); |
| 1836 | break; |
| 1837 | case PyUnicode_4BYTE_KIND: |
| 1838 | memcpy(PyUnicode_4BYTE_DATA(copy), data, sizeof(Py_UCS4) * size); |
| 1839 | break; |
| 1840 | default: |
| 1841 | assert(0); |
| 1842 | break; |
| 1843 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1844 | assert(_PyUnicode_CheckConsistency(copy, 1)); |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 1845 | return copy; |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 1846 | } |
| 1847 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1848 | |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1849 | /* Widen Unicode objects to larger buffers. Don't write terminating null |
| 1850 | character. Return NULL on error. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1851 | |
| 1852 | void* |
| 1853 | _PyUnicode_AsKind(PyObject *s, unsigned int kind) |
| 1854 | { |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1855 | Py_ssize_t len; |
| 1856 | void *result; |
| 1857 | unsigned int skind; |
| 1858 | |
| 1859 | if (PyUnicode_READY(s)) |
| 1860 | return NULL; |
| 1861 | |
| 1862 | len = PyUnicode_GET_LENGTH(s); |
| 1863 | skind = PyUnicode_KIND(s); |
| 1864 | if (skind >= kind) { |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 1865 | PyErr_SetString(PyExc_SystemError, "invalid widening attempt"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1866 | return NULL; |
| 1867 | } |
| 1868 | switch(kind) { |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1869 | case PyUnicode_2BYTE_KIND: |
| 1870 | result = PyMem_Malloc(len * sizeof(Py_UCS2)); |
| 1871 | if (!result) |
| 1872 | return PyErr_NoMemory(); |
| 1873 | assert(skind == PyUnicode_1BYTE_KIND); |
| 1874 | _PyUnicode_CONVERT_BYTES( |
| 1875 | Py_UCS1, Py_UCS2, |
| 1876 | PyUnicode_1BYTE_DATA(s), |
| 1877 | PyUnicode_1BYTE_DATA(s) + len, |
| 1878 | result); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1879 | return result; |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1880 | case PyUnicode_4BYTE_KIND: |
| 1881 | result = PyMem_Malloc(len * sizeof(Py_UCS4)); |
| 1882 | if (!result) |
| 1883 | return PyErr_NoMemory(); |
| 1884 | if (skind == PyUnicode_2BYTE_KIND) { |
| 1885 | _PyUnicode_CONVERT_BYTES( |
| 1886 | Py_UCS2, Py_UCS4, |
| 1887 | PyUnicode_2BYTE_DATA(s), |
| 1888 | PyUnicode_2BYTE_DATA(s) + len, |
| 1889 | result); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1890 | } |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1891 | else { |
| 1892 | assert(skind == PyUnicode_1BYTE_KIND); |
| 1893 | _PyUnicode_CONVERT_BYTES( |
| 1894 | Py_UCS1, Py_UCS4, |
| 1895 | PyUnicode_1BYTE_DATA(s), |
| 1896 | PyUnicode_1BYTE_DATA(s) + len, |
| 1897 | result); |
| 1898 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1899 | return result; |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1900 | default: |
| 1901 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1902 | } |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 1903 | PyErr_SetString(PyExc_SystemError, "invalid kind"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1904 | return NULL; |
| 1905 | } |
| 1906 | |
| 1907 | static Py_UCS4* |
| 1908 | as_ucs4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, |
| 1909 | int copy_null) |
| 1910 | { |
| 1911 | int kind; |
| 1912 | void *data; |
| 1913 | Py_ssize_t len, targetlen; |
| 1914 | if (PyUnicode_READY(string) == -1) |
| 1915 | return NULL; |
| 1916 | kind = PyUnicode_KIND(string); |
| 1917 | data = PyUnicode_DATA(string); |
| 1918 | len = PyUnicode_GET_LENGTH(string); |
| 1919 | targetlen = len; |
| 1920 | if (copy_null) |
| 1921 | targetlen++; |
| 1922 | if (!target) { |
| 1923 | if (PY_SSIZE_T_MAX / sizeof(Py_UCS4) < targetlen) { |
| 1924 | PyErr_NoMemory(); |
| 1925 | return NULL; |
| 1926 | } |
| 1927 | target = PyMem_Malloc(targetlen * sizeof(Py_UCS4)); |
| 1928 | if (!target) { |
| 1929 | PyErr_NoMemory(); |
| 1930 | return NULL; |
| 1931 | } |
| 1932 | } |
| 1933 | else { |
| 1934 | if (targetsize < targetlen) { |
| 1935 | PyErr_Format(PyExc_SystemError, |
| 1936 | "string is longer than the buffer"); |
| 1937 | if (copy_null && 0 < targetsize) |
| 1938 | target[0] = 0; |
| 1939 | return NULL; |
| 1940 | } |
| 1941 | } |
| 1942 | if (kind != PyUnicode_4BYTE_KIND) { |
| 1943 | Py_ssize_t i; |
| 1944 | for (i = 0; i < len; i++) |
| 1945 | target[i] = PyUnicode_READ(kind, data, i); |
| 1946 | } |
| 1947 | else |
| 1948 | Py_MEMCPY(target, data, len * sizeof(Py_UCS4)); |
| 1949 | if (copy_null) |
| 1950 | target[len] = 0; |
| 1951 | return target; |
| 1952 | } |
| 1953 | |
| 1954 | Py_UCS4* |
| 1955 | PyUnicode_AsUCS4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, |
| 1956 | int copy_null) |
| 1957 | { |
| 1958 | if (target == NULL || targetsize < 1) { |
| 1959 | PyErr_BadInternalCall(); |
| 1960 | return NULL; |
| 1961 | } |
| 1962 | return as_ucs4(string, target, targetsize, copy_null); |
| 1963 | } |
| 1964 | |
| 1965 | Py_UCS4* |
| 1966 | PyUnicode_AsUCS4Copy(PyObject *string) |
| 1967 | { |
| 1968 | return as_ucs4(string, NULL, 0, 1); |
| 1969 | } |
| 1970 | |
| 1971 | #ifdef HAVE_WCHAR_H |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1972 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1973 | PyObject * |
| 1974 | PyUnicode_FromWideChar(register const wchar_t *w, Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1975 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1976 | if (w == NULL) { |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 1977 | if (size == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1978 | return PyUnicode_New(0, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1979 | PyErr_BadInternalCall(); |
| 1980 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1981 | } |
| 1982 | |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 1983 | if (size == -1) { |
| 1984 | size = wcslen(w); |
| 1985 | } |
| 1986 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1987 | return PyUnicode_FromUnicode(w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1988 | } |
| 1989 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1990 | #endif /* HAVE_WCHAR_H */ |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1991 | |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 1992 | static void |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1993 | makefmt(char *fmt, int longflag, int longlongflag, int size_tflag, |
| 1994 | int zeropad, int width, int precision, char c) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 1995 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1996 | *fmt++ = '%'; |
| 1997 | if (width) { |
| 1998 | if (zeropad) |
| 1999 | *fmt++ = '0'; |
| 2000 | fmt += sprintf(fmt, "%d", width); |
| 2001 | } |
| 2002 | if (precision) |
| 2003 | fmt += sprintf(fmt, ".%d", precision); |
| 2004 | if (longflag) |
| 2005 | *fmt++ = 'l'; |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2006 | else if (longlongflag) { |
| 2007 | /* longlongflag should only ever be nonzero on machines with |
| 2008 | HAVE_LONG_LONG defined */ |
| 2009 | #ifdef HAVE_LONG_LONG |
| 2010 | char *f = PY_FORMAT_LONG_LONG; |
| 2011 | while (*f) |
| 2012 | *fmt++ = *f++; |
| 2013 | #else |
| 2014 | /* we shouldn't ever get here */ |
| 2015 | assert(0); |
| 2016 | *fmt++ = 'l'; |
| 2017 | #endif |
| 2018 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2019 | else if (size_tflag) { |
| 2020 | char *f = PY_FORMAT_SIZE_T; |
| 2021 | while (*f) |
| 2022 | *fmt++ = *f++; |
| 2023 | } |
| 2024 | *fmt++ = c; |
| 2025 | *fmt = '\0'; |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2026 | } |
| 2027 | |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2028 | /* helper for PyUnicode_FromFormatV() */ |
| 2029 | |
| 2030 | static const char* |
| 2031 | parse_format_flags(const char *f, |
| 2032 | int *p_width, int *p_precision, |
| 2033 | int *p_longflag, int *p_longlongflag, int *p_size_tflag) |
| 2034 | { |
| 2035 | int width, precision, longflag, longlongflag, size_tflag; |
| 2036 | |
| 2037 | /* parse the width.precision part, e.g. "%2.5s" => width=2, precision=5 */ |
| 2038 | f++; |
| 2039 | width = 0; |
| 2040 | while (Py_ISDIGIT((unsigned)*f)) |
| 2041 | width = (width*10) + *f++ - '0'; |
| 2042 | precision = 0; |
| 2043 | if (*f == '.') { |
| 2044 | f++; |
| 2045 | while (Py_ISDIGIT((unsigned)*f)) |
| 2046 | precision = (precision*10) + *f++ - '0'; |
| 2047 | if (*f == '%') { |
| 2048 | /* "%.3%s" => f points to "3" */ |
| 2049 | f--; |
| 2050 | } |
| 2051 | } |
| 2052 | if (*f == '\0') { |
| 2053 | /* bogus format "%.1" => go backward, f points to "1" */ |
| 2054 | f--; |
| 2055 | } |
| 2056 | if (p_width != NULL) |
| 2057 | *p_width = width; |
| 2058 | if (p_precision != NULL) |
| 2059 | *p_precision = precision; |
| 2060 | |
| 2061 | /* Handle %ld, %lu, %lld and %llu. */ |
| 2062 | longflag = 0; |
| 2063 | longlongflag = 0; |
Victor Stinner | e7faec1 | 2011-03-02 00:01:53 +0000 | [diff] [blame] | 2064 | size_tflag = 0; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2065 | |
| 2066 | if (*f == 'l') { |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2067 | if (f[1] == 'd' || f[1] == 'u' || f[1] == 'i') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2068 | longflag = 1; |
| 2069 | ++f; |
| 2070 | } |
| 2071 | #ifdef HAVE_LONG_LONG |
| 2072 | else if (f[1] == 'l' && |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2073 | (f[2] == 'd' || f[2] == 'u' || f[2] == 'i')) { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2074 | longlongflag = 1; |
| 2075 | f += 2; |
| 2076 | } |
| 2077 | #endif |
| 2078 | } |
| 2079 | /* handle the size_t flag. */ |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2080 | 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] | 2081 | size_tflag = 1; |
| 2082 | ++f; |
| 2083 | } |
| 2084 | if (p_longflag != NULL) |
| 2085 | *p_longflag = longflag; |
| 2086 | if (p_longlongflag != NULL) |
| 2087 | *p_longlongflag = longlongflag; |
| 2088 | if (p_size_tflag != NULL) |
| 2089 | *p_size_tflag = size_tflag; |
| 2090 | return f; |
| 2091 | } |
| 2092 | |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2093 | /* maximum number of characters required for output of %ld. 21 characters |
| 2094 | allows for 64-bit integers (in decimal) and an optional sign. */ |
| 2095 | #define MAX_LONG_CHARS 21 |
| 2096 | /* maximum number of characters required for output of %lld. |
| 2097 | We need at most ceil(log10(256)*SIZEOF_LONG_LONG) digits, |
| 2098 | plus 1 for the sign. 53/22 is an upper bound for log10(256). */ |
| 2099 | #define MAX_LONG_LONG_CHARS (2 + (SIZEOF_LONG_LONG*53-1) / 22) |
| 2100 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2101 | PyObject * |
| 2102 | PyUnicode_FromFormatV(const char *format, va_list vargs) |
| 2103 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2104 | va_list count; |
| 2105 | Py_ssize_t callcount = 0; |
| 2106 | PyObject **callresults = NULL; |
| 2107 | PyObject **callresult = NULL; |
| 2108 | Py_ssize_t n = 0; |
| 2109 | int width = 0; |
| 2110 | int precision = 0; |
| 2111 | int zeropad; |
| 2112 | const char* f; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2113 | PyObject *string; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2114 | /* used by sprintf */ |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2115 | char fmt[61]; /* should be enough for %0width.precisionlld */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2116 | Py_UCS4 maxchar = 127; /* result is ASCII by default */ |
| 2117 | Py_UCS4 argmaxchar; |
| 2118 | Py_ssize_t numbersize = 0; |
| 2119 | char *numberresults = NULL; |
| 2120 | char *numberresult = NULL; |
| 2121 | Py_ssize_t i; |
| 2122 | int kind; |
| 2123 | void *data; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2124 | |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 2125 | Py_VA_COPY(count, vargs); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2126 | /* step 1: count the number of %S/%R/%A/%s format specifications |
| 2127 | * (we call PyObject_Str()/PyObject_Repr()/PyObject_ASCII()/ |
| 2128 | * 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] | 2129 | * result in an array) |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 2130 | * also estimate a upper bound for all the number formats in the string, |
| 2131 | * 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] | 2132 | * buffer before putting everything together. */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2133 | for (f = format; *f; f++) { |
| 2134 | if (*f == '%') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2135 | int longlongflag; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2136 | /* skip width or width.precision (eg. "1.2" of "%1.2f") */ |
| 2137 | f = parse_format_flags(f, &width, NULL, NULL, &longlongflag, NULL); |
| 2138 | if (*f == 's' || *f=='S' || *f=='R' || *f=='A' || *f=='V') |
| 2139 | ++callcount; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2140 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2141 | else if (*f == 'd' || *f=='u' || *f=='i' || *f=='x' || *f=='p') { |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2142 | #ifdef HAVE_LONG_LONG |
| 2143 | if (longlongflag) { |
| 2144 | if (width < MAX_LONG_LONG_CHARS) |
| 2145 | width = MAX_LONG_LONG_CHARS; |
| 2146 | } |
| 2147 | else |
| 2148 | #endif |
| 2149 | /* MAX_LONG_CHARS is enough to hold a 64-bit integer, |
| 2150 | including sign. Decimal takes the most space. This |
| 2151 | isn't enough for octal. If a width is specified we |
| 2152 | need more (which we allocate later). */ |
| 2153 | if (width < MAX_LONG_CHARS) |
| 2154 | width = MAX_LONG_CHARS; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2155 | |
| 2156 | /* account for the size + '\0' to separate numbers |
| 2157 | inside of the numberresults buffer */ |
| 2158 | numbersize += (width + 1); |
| 2159 | } |
| 2160 | } |
| 2161 | else if ((unsigned char)*f > 127) { |
| 2162 | PyErr_Format(PyExc_ValueError, |
| 2163 | "PyUnicode_FromFormatV() expects an ASCII-encoded format " |
| 2164 | "string, got a non-ASCII byte: 0x%02x", |
| 2165 | (unsigned char)*f); |
| 2166 | return NULL; |
| 2167 | } |
| 2168 | } |
| 2169 | /* step 2: allocate memory for the results of |
| 2170 | * PyObject_Str()/PyObject_Repr()/PyUnicode_DecodeUTF8() calls */ |
| 2171 | if (callcount) { |
| 2172 | callresults = PyObject_Malloc(sizeof(PyObject *) * callcount); |
| 2173 | if (!callresults) { |
| 2174 | PyErr_NoMemory(); |
| 2175 | return NULL; |
| 2176 | } |
| 2177 | callresult = callresults; |
| 2178 | } |
| 2179 | /* step 2.5: allocate memory for the results of formating numbers */ |
| 2180 | if (numbersize) { |
| 2181 | numberresults = PyObject_Malloc(numbersize); |
| 2182 | if (!numberresults) { |
| 2183 | PyErr_NoMemory(); |
| 2184 | goto fail; |
| 2185 | } |
| 2186 | numberresult = numberresults; |
| 2187 | } |
| 2188 | |
| 2189 | /* step 3: format numbers and figure out how large a buffer we need */ |
| 2190 | for (f = format; *f; f++) { |
| 2191 | if (*f == '%') { |
| 2192 | const char* p; |
| 2193 | int longflag; |
| 2194 | int longlongflag; |
| 2195 | int size_tflag; |
| 2196 | int numprinted; |
| 2197 | |
| 2198 | p = f; |
| 2199 | zeropad = (f[1] == '0'); |
| 2200 | f = parse_format_flags(f, &width, &precision, |
| 2201 | &longflag, &longlongflag, &size_tflag); |
| 2202 | switch (*f) { |
| 2203 | case 'c': |
| 2204 | { |
| 2205 | Py_UCS4 ordinal = va_arg(count, int); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2206 | maxchar = Py_MAX(maxchar, ordinal); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2207 | n++; |
| 2208 | break; |
| 2209 | } |
| 2210 | case '%': |
| 2211 | n++; |
| 2212 | break; |
| 2213 | case 'i': |
| 2214 | case 'd': |
| 2215 | makefmt(fmt, longflag, longlongflag, size_tflag, zeropad, |
| 2216 | width, precision, *f); |
| 2217 | if (longflag) |
| 2218 | numprinted = sprintf(numberresult, fmt, |
| 2219 | va_arg(count, long)); |
| 2220 | #ifdef HAVE_LONG_LONG |
| 2221 | else if (longlongflag) |
| 2222 | numprinted = sprintf(numberresult, fmt, |
| 2223 | va_arg(count, PY_LONG_LONG)); |
| 2224 | #endif |
| 2225 | else if (size_tflag) |
| 2226 | numprinted = sprintf(numberresult, fmt, |
| 2227 | va_arg(count, Py_ssize_t)); |
| 2228 | else |
| 2229 | numprinted = sprintf(numberresult, fmt, |
| 2230 | va_arg(count, int)); |
| 2231 | n += numprinted; |
| 2232 | /* advance by +1 to skip over the '\0' */ |
| 2233 | numberresult += (numprinted + 1); |
| 2234 | assert(*(numberresult - 1) == '\0'); |
| 2235 | assert(*(numberresult - 2) != '\0'); |
| 2236 | assert(numprinted >= 0); |
| 2237 | assert(numberresult <= numberresults + numbersize); |
| 2238 | break; |
| 2239 | case 'u': |
| 2240 | makefmt(fmt, longflag, longlongflag, size_tflag, zeropad, |
| 2241 | width, precision, 'u'); |
| 2242 | if (longflag) |
| 2243 | numprinted = sprintf(numberresult, fmt, |
| 2244 | va_arg(count, unsigned long)); |
| 2245 | #ifdef HAVE_LONG_LONG |
| 2246 | else if (longlongflag) |
| 2247 | numprinted = sprintf(numberresult, fmt, |
| 2248 | va_arg(count, unsigned PY_LONG_LONG)); |
| 2249 | #endif |
| 2250 | else if (size_tflag) |
| 2251 | numprinted = sprintf(numberresult, fmt, |
| 2252 | va_arg(count, size_t)); |
| 2253 | else |
| 2254 | numprinted = sprintf(numberresult, fmt, |
| 2255 | va_arg(count, unsigned int)); |
| 2256 | n += numprinted; |
| 2257 | numberresult += (numprinted + 1); |
| 2258 | assert(*(numberresult - 1) == '\0'); |
| 2259 | assert(*(numberresult - 2) != '\0'); |
| 2260 | assert(numprinted >= 0); |
| 2261 | assert(numberresult <= numberresults + numbersize); |
| 2262 | break; |
| 2263 | case 'x': |
| 2264 | makefmt(fmt, 0, 0, 0, zeropad, width, precision, 'x'); |
| 2265 | numprinted = sprintf(numberresult, fmt, va_arg(count, int)); |
| 2266 | n += numprinted; |
| 2267 | numberresult += (numprinted + 1); |
| 2268 | assert(*(numberresult - 1) == '\0'); |
| 2269 | assert(*(numberresult - 2) != '\0'); |
| 2270 | assert(numprinted >= 0); |
| 2271 | assert(numberresult <= numberresults + numbersize); |
| 2272 | break; |
| 2273 | case 'p': |
| 2274 | numprinted = sprintf(numberresult, "%p", va_arg(count, void*)); |
| 2275 | /* %p is ill-defined: ensure leading 0x. */ |
| 2276 | if (numberresult[1] == 'X') |
| 2277 | numberresult[1] = 'x'; |
| 2278 | else if (numberresult[1] != 'x') { |
| 2279 | memmove(numberresult + 2, numberresult, |
| 2280 | strlen(numberresult) + 1); |
| 2281 | numberresult[0] = '0'; |
| 2282 | numberresult[1] = 'x'; |
| 2283 | numprinted += 2; |
| 2284 | } |
| 2285 | n += numprinted; |
| 2286 | numberresult += (numprinted + 1); |
| 2287 | assert(*(numberresult - 1) == '\0'); |
| 2288 | assert(*(numberresult - 2) != '\0'); |
| 2289 | assert(numprinted >= 0); |
| 2290 | assert(numberresult <= numberresults + numbersize); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2291 | break; |
| 2292 | case 's': |
| 2293 | { |
| 2294 | /* UTF-8 */ |
Georg Brandl | 780b2a6 | 2009-05-05 09:19:59 +0000 | [diff] [blame] | 2295 | const char *s = va_arg(count, const char*); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2296 | PyObject *str = PyUnicode_DecodeUTF8(s, strlen(s), "replace"); |
| 2297 | if (!str) |
| 2298 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2299 | /* since PyUnicode_DecodeUTF8 returns already flexible |
| 2300 | unicode objects, there is no need to call ready on them */ |
| 2301 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2302 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2303 | n += PyUnicode_GET_LENGTH(str); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2304 | /* Remember the str and switch to the next slot */ |
| 2305 | *callresult++ = str; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2306 | break; |
| 2307 | } |
| 2308 | case 'U': |
| 2309 | { |
| 2310 | PyObject *obj = va_arg(count, PyObject *); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 2311 | assert(obj && _PyUnicode_CHECK(obj)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2312 | if (PyUnicode_READY(obj) == -1) |
| 2313 | goto fail; |
| 2314 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2315 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2316 | n += PyUnicode_GET_LENGTH(obj); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2317 | break; |
| 2318 | } |
| 2319 | case 'V': |
| 2320 | { |
| 2321 | PyObject *obj = va_arg(count, PyObject *); |
| 2322 | const char *str = va_arg(count, const char *); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2323 | PyObject *str_obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2324 | assert(obj || str); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 2325 | assert(!obj || _PyUnicode_CHECK(obj)); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2326 | if (obj) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2327 | if (PyUnicode_READY(obj) == -1) |
| 2328 | goto fail; |
| 2329 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2330 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2331 | n += PyUnicode_GET_LENGTH(obj); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2332 | *callresult++ = NULL; |
| 2333 | } |
| 2334 | else { |
| 2335 | str_obj = PyUnicode_DecodeUTF8(str, strlen(str), "replace"); |
| 2336 | if (!str_obj) |
| 2337 | goto fail; |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 2338 | if (PyUnicode_READY(str_obj)) { |
| 2339 | Py_DECREF(str_obj); |
| 2340 | goto fail; |
| 2341 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2342 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str_obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2343 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2344 | n += PyUnicode_GET_LENGTH(str_obj); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2345 | *callresult++ = str_obj; |
| 2346 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2347 | break; |
| 2348 | } |
| 2349 | case 'S': |
| 2350 | { |
| 2351 | PyObject *obj = va_arg(count, PyObject *); |
| 2352 | PyObject *str; |
| 2353 | assert(obj); |
| 2354 | str = PyObject_Str(obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2355 | if (!str || PyUnicode_READY(str) == -1) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2356 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2357 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2358 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2359 | n += PyUnicode_GET_LENGTH(str); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2360 | /* Remember the str and switch to the next slot */ |
| 2361 | *callresult++ = str; |
| 2362 | break; |
| 2363 | } |
| 2364 | case 'R': |
| 2365 | { |
| 2366 | PyObject *obj = va_arg(count, PyObject *); |
| 2367 | PyObject *repr; |
| 2368 | assert(obj); |
| 2369 | repr = PyObject_Repr(obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2370 | if (!repr || PyUnicode_READY(repr) == -1) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2371 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2372 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(repr); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2373 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2374 | n += PyUnicode_GET_LENGTH(repr); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2375 | /* Remember the repr and switch to the next slot */ |
| 2376 | *callresult++ = repr; |
| 2377 | break; |
| 2378 | } |
| 2379 | case 'A': |
| 2380 | { |
| 2381 | PyObject *obj = va_arg(count, PyObject *); |
| 2382 | PyObject *ascii; |
| 2383 | assert(obj); |
| 2384 | ascii = PyObject_ASCII(obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2385 | if (!ascii || PyUnicode_READY(ascii) == -1) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2386 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2387 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(ascii); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2388 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2389 | n += PyUnicode_GET_LENGTH(ascii); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2390 | /* Remember the repr and switch to the next slot */ |
| 2391 | *callresult++ = ascii; |
| 2392 | break; |
| 2393 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2394 | default: |
| 2395 | /* if we stumble upon an unknown |
| 2396 | formatting code, copy the rest of |
| 2397 | the format string to the output |
| 2398 | string. (we cannot just skip the |
| 2399 | code, since there's no way to know |
| 2400 | what's in the argument list) */ |
| 2401 | n += strlen(p); |
| 2402 | goto expand; |
| 2403 | } |
| 2404 | } else |
| 2405 | n++; |
| 2406 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2407 | expand: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2408 | /* step 4: fill the buffer */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2409 | /* Since we've analyzed how much space we need, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2410 | we don't have to resize the string. |
| 2411 | There can be no errors beyond this point. */ |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2412 | string = PyUnicode_New(n, maxchar); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2413 | if (!string) |
| 2414 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2415 | kind = PyUnicode_KIND(string); |
| 2416 | data = PyUnicode_DATA(string); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2417 | callresult = callresults; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2418 | numberresult = numberresults; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2419 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2420 | for (i = 0, f = format; *f; f++) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2421 | if (*f == '%') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2422 | const char* p; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2423 | |
| 2424 | p = f; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2425 | f = parse_format_flags(f, NULL, NULL, NULL, NULL, NULL); |
| 2426 | /* checking for == because the last argument could be a empty |
| 2427 | string, which causes i to point to end, the assert at the end of |
| 2428 | the loop */ |
| 2429 | assert(i <= PyUnicode_GET_LENGTH(string)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2430 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2431 | switch (*f) { |
| 2432 | case 'c': |
Victor Stinner | 5ed8b2c | 2011-02-21 21:13:44 +0000 | [diff] [blame] | 2433 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2434 | const int ordinal = va_arg(vargs, int); |
| 2435 | PyUnicode_WRITE(kind, data, i++, ordinal); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2436 | break; |
Victor Stinner | 5ed8b2c | 2011-02-21 21:13:44 +0000 | [diff] [blame] | 2437 | } |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2438 | case 'i': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2439 | case 'd': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2440 | case 'u': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2441 | case 'x': |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2442 | case 'p': |
| 2443 | /* unused, since we already have the result */ |
| 2444 | if (*f == 'p') |
| 2445 | (void) va_arg(vargs, void *); |
| 2446 | else |
| 2447 | (void) va_arg(vargs, int); |
| 2448 | /* extract the result from numberresults and append. */ |
| 2449 | for (; *numberresult; ++i, ++numberresult) |
| 2450 | PyUnicode_WRITE(kind, data, i, *numberresult); |
| 2451 | /* skip over the separating '\0' */ |
| 2452 | assert(*numberresult == '\0'); |
| 2453 | numberresult++; |
| 2454 | assert(numberresult <= numberresults + numbersize); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2455 | break; |
| 2456 | case 's': |
| 2457 | { |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2458 | /* unused, since we already have the result */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2459 | Py_ssize_t size; |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2460 | (void) va_arg(vargs, char *); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2461 | size = PyUnicode_GET_LENGTH(*callresult); |
| 2462 | assert(PyUnicode_KIND(*callresult) <= PyUnicode_KIND(string)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2463 | copy_characters(string, i, *callresult, 0, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2464 | i += size; |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2465 | /* We're done with the unicode()/repr() => forget it */ |
| 2466 | Py_DECREF(*callresult); |
| 2467 | /* switch to next unicode()/repr() result */ |
| 2468 | ++callresult; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2469 | break; |
| 2470 | } |
| 2471 | case 'U': |
| 2472 | { |
| 2473 | PyObject *obj = va_arg(vargs, PyObject *); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2474 | Py_ssize_t size; |
| 2475 | assert(PyUnicode_KIND(obj) <= PyUnicode_KIND(string)); |
| 2476 | size = PyUnicode_GET_LENGTH(obj); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2477 | copy_characters(string, i, obj, 0, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2478 | i += size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2479 | break; |
| 2480 | } |
| 2481 | case 'V': |
| 2482 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2483 | Py_ssize_t size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2484 | PyObject *obj = va_arg(vargs, PyObject *); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2485 | va_arg(vargs, const char *); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2486 | if (obj) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2487 | size = PyUnicode_GET_LENGTH(obj); |
| 2488 | assert(PyUnicode_KIND(obj) <= PyUnicode_KIND(string)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2489 | copy_characters(string, i, obj, 0, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2490 | i += size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2491 | } else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2492 | size = PyUnicode_GET_LENGTH(*callresult); |
| 2493 | assert(PyUnicode_KIND(*callresult) <= |
| 2494 | PyUnicode_KIND(string)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2495 | copy_characters(string, i, *callresult, 0, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2496 | i += size; |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2497 | Py_DECREF(*callresult); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2498 | } |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2499 | ++callresult; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2500 | break; |
| 2501 | } |
| 2502 | case 'S': |
| 2503 | case 'R': |
Victor Stinner | 9a90900 | 2010-10-18 20:59:24 +0000 | [diff] [blame] | 2504 | case 'A': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2505 | { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2506 | Py_ssize_t size = PyUnicode_GET_LENGTH(*callresult); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2507 | /* unused, since we already have the result */ |
| 2508 | (void) va_arg(vargs, PyObject *); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2509 | assert(PyUnicode_KIND(*callresult) <= PyUnicode_KIND(string)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2510 | copy_characters(string, i, *callresult, 0, size); |
| 2511 | i += size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2512 | /* We're done with the unicode()/repr() => forget it */ |
| 2513 | Py_DECREF(*callresult); |
| 2514 | /* switch to next unicode()/repr() result */ |
| 2515 | ++callresult; |
| 2516 | break; |
| 2517 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2518 | case '%': |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2519 | PyUnicode_WRITE(kind, data, i++, '%'); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2520 | break; |
| 2521 | default: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2522 | for (; *p; ++p, ++i) |
| 2523 | PyUnicode_WRITE(kind, data, i, *p); |
| 2524 | assert(i == PyUnicode_GET_LENGTH(string)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2525 | goto end; |
| 2526 | } |
Victor Stinner | 1205f27 | 2010-09-11 00:54:47 +0000 | [diff] [blame] | 2527 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2528 | else { |
| 2529 | assert(i < PyUnicode_GET_LENGTH(string)); |
| 2530 | PyUnicode_WRITE(kind, data, i++, *f); |
| 2531 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2532 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2533 | assert(i == PyUnicode_GET_LENGTH(string)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2534 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2535 | end: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2536 | if (callresults) |
| 2537 | PyObject_Free(callresults); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2538 | if (numberresults) |
| 2539 | PyObject_Free(numberresults); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2540 | assert(_PyUnicode_CheckConsistency(string, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2541 | return (PyObject *)string; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2542 | fail: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2543 | if (callresults) { |
| 2544 | PyObject **callresult2 = callresults; |
| 2545 | while (callresult2 < callresult) { |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2546 | Py_XDECREF(*callresult2); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2547 | ++callresult2; |
| 2548 | } |
| 2549 | PyObject_Free(callresults); |
| 2550 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2551 | if (numberresults) |
| 2552 | PyObject_Free(numberresults); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2553 | return NULL; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2554 | } |
| 2555 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2556 | PyObject * |
| 2557 | PyUnicode_FromFormat(const char *format, ...) |
| 2558 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2559 | PyObject* ret; |
| 2560 | va_list vargs; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2561 | |
| 2562 | #ifdef HAVE_STDARG_PROTOTYPES |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2563 | va_start(vargs, format); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2564 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2565 | va_start(vargs); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2566 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2567 | ret = PyUnicode_FromFormatV(format, vargs); |
| 2568 | va_end(vargs); |
| 2569 | return ret; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2570 | } |
| 2571 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2572 | #ifdef HAVE_WCHAR_H |
| 2573 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2574 | /* Helper function for PyUnicode_AsWideChar() and PyUnicode_AsWideCharString(): |
| 2575 | convert a Unicode object to a wide character string. |
| 2576 | |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2577 | - 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] | 2578 | character) required to convert the unicode object. Ignore size argument. |
| 2579 | |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2580 | - Otherwise: return the number of wide characters (excluding the null |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2581 | character) written into w. Write at most size wide characters (including |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2582 | the null character). */ |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2583 | static Py_ssize_t |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2584 | unicode_aswidechar(PyUnicodeObject *unicode, |
| 2585 | wchar_t *w, |
| 2586 | Py_ssize_t size) |
| 2587 | { |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2588 | Py_ssize_t res; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2589 | const wchar_t *wstr; |
| 2590 | |
| 2591 | wstr = PyUnicode_AsUnicodeAndSize((PyObject *)unicode, &res); |
| 2592 | if (wstr == NULL) |
| 2593 | return -1; |
| 2594 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2595 | if (w != NULL) { |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2596 | if (size > res) |
| 2597 | size = res + 1; |
| 2598 | else |
| 2599 | res = size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2600 | Py_MEMCPY(w, wstr, size * sizeof(wchar_t)); |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2601 | return res; |
| 2602 | } |
| 2603 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2604 | return res + 1; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2605 | } |
| 2606 | |
| 2607 | Py_ssize_t |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2608 | PyUnicode_AsWideChar(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2609 | wchar_t *w, |
| 2610 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2611 | { |
| 2612 | if (unicode == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2613 | PyErr_BadInternalCall(); |
| 2614 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2615 | } |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2616 | return unicode_aswidechar((PyUnicodeObject*)unicode, w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2617 | } |
| 2618 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2619 | wchar_t* |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 2620 | PyUnicode_AsWideCharString(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2621 | Py_ssize_t *size) |
| 2622 | { |
| 2623 | wchar_t* buffer; |
| 2624 | Py_ssize_t buflen; |
| 2625 | |
| 2626 | if (unicode == NULL) { |
| 2627 | PyErr_BadInternalCall(); |
| 2628 | return NULL; |
| 2629 | } |
| 2630 | |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 2631 | buflen = unicode_aswidechar((PyUnicodeObject *)unicode, NULL, 0); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2632 | if (buflen == -1) |
| 2633 | return NULL; |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2634 | if (PY_SSIZE_T_MAX / sizeof(wchar_t) < buflen) { |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2635 | PyErr_NoMemory(); |
| 2636 | return NULL; |
| 2637 | } |
| 2638 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2639 | buffer = PyMem_MALLOC(buflen * sizeof(wchar_t)); |
| 2640 | if (buffer == NULL) { |
| 2641 | PyErr_NoMemory(); |
| 2642 | return NULL; |
| 2643 | } |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 2644 | buflen = unicode_aswidechar((PyUnicodeObject *)unicode, buffer, buflen); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2645 | if (buflen == -1) |
| 2646 | return NULL; |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2647 | if (size != NULL) |
| 2648 | *size = buflen; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2649 | return buffer; |
| 2650 | } |
| 2651 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2652 | #endif /* HAVE_WCHAR_H */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2653 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2654 | PyObject * |
| 2655 | PyUnicode_FromOrdinal(int ordinal) |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2656 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2657 | PyObject *v; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2658 | if (ordinal < 0 || ordinal > 0x10ffff) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2659 | PyErr_SetString(PyExc_ValueError, |
| 2660 | "chr() arg not in range(0x110000)"); |
| 2661 | return NULL; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2662 | } |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 2663 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2664 | if (ordinal < 256) |
| 2665 | return get_latin1_char(ordinal); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2666 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2667 | v = PyUnicode_New(1, ordinal); |
| 2668 | if (v == NULL) |
| 2669 | return NULL; |
| 2670 | PyUnicode_WRITE(PyUnicode_KIND(v), PyUnicode_DATA(v), 0, ordinal); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2671 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2672 | return v; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2673 | } |
| 2674 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2675 | PyObject * |
| 2676 | PyUnicode_FromObject(register PyObject *obj) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2677 | { |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2678 | /* XXX Perhaps we should make this API an alias of |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2679 | PyObject_Str() instead ?! */ |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2680 | if (PyUnicode_CheckExact(obj)) { |
Victor Stinner | d3a83d5 | 2011-10-01 03:09:33 +0200 | [diff] [blame] | 2681 | if (PyUnicode_READY(obj)) |
| 2682 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2683 | Py_INCREF(obj); |
| 2684 | return obj; |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2685 | } |
| 2686 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2687 | /* For a Unicode subtype that's not a Unicode object, |
| 2688 | return a true Unicode object with the same data. */ |
Victor Stinner | 2219e0a | 2011-10-01 01:16:59 +0200 | [diff] [blame] | 2689 | return PyUnicode_Copy(obj); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2690 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2691 | PyErr_Format(PyExc_TypeError, |
| 2692 | "Can't convert '%.100s' object to str implicitly", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2693 | Py_TYPE(obj)->tp_name); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2694 | return NULL; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2695 | } |
| 2696 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2697 | PyObject * |
| 2698 | PyUnicode_FromEncodedObject(register PyObject *obj, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2699 | const char *encoding, |
| 2700 | const char *errors) |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2701 | { |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2702 | Py_buffer buffer; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2703 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2704 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2705 | if (obj == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2706 | PyErr_BadInternalCall(); |
| 2707 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2708 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2709 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2710 | /* Decoding bytes objects is the most common case and should be fast */ |
| 2711 | if (PyBytes_Check(obj)) { |
| 2712 | if (PyBytes_GET_SIZE(obj) == 0) { |
| 2713 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 2714 | v = unicode_empty; |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2715 | } |
| 2716 | else { |
| 2717 | v = PyUnicode_Decode( |
| 2718 | PyBytes_AS_STRING(obj), PyBytes_GET_SIZE(obj), |
| 2719 | encoding, errors); |
| 2720 | } |
| 2721 | return v; |
| 2722 | } |
| 2723 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2724 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2725 | PyErr_SetString(PyExc_TypeError, |
| 2726 | "decoding str is not supported"); |
| 2727 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2728 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2729 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2730 | /* Retrieve a bytes buffer view through the PEP 3118 buffer interface */ |
| 2731 | if (PyObject_GetBuffer(obj, &buffer, PyBUF_SIMPLE) < 0) { |
| 2732 | PyErr_Format(PyExc_TypeError, |
| 2733 | "coercing to str: need bytes, bytearray " |
| 2734 | "or buffer-like object, %.80s found", |
| 2735 | Py_TYPE(obj)->tp_name); |
| 2736 | return NULL; |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 2737 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2738 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2739 | if (buffer.len == 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2740 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 2741 | v = unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2742 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2743 | else |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2744 | v = PyUnicode_Decode((char*) buffer.buf, buffer.len, encoding, errors); |
Marc-André Lemburg | ad7c98e | 2001-01-17 17:09:53 +0000 | [diff] [blame] | 2745 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2746 | PyBuffer_Release(&buffer); |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2747 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2748 | } |
| 2749 | |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2750 | /* Convert encoding to lower case and replace '_' with '-' in order to |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2751 | catch e.g. UTF_8. Return 0 on error (encoding is longer than lower_len-1), |
| 2752 | 1 on success. */ |
| 2753 | static int |
| 2754 | normalize_encoding(const char *encoding, |
| 2755 | char *lower, |
| 2756 | size_t lower_len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2757 | { |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2758 | const char *e; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2759 | char *l; |
| 2760 | char *l_end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2761 | |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2762 | e = encoding; |
| 2763 | l = lower; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2764 | l_end = &lower[lower_len - 1]; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2765 | while (*e) { |
| 2766 | if (l == l_end) |
| 2767 | return 0; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 2768 | if (Py_ISUPPER(*e)) { |
| 2769 | *l++ = Py_TOLOWER(*e++); |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2770 | } |
| 2771 | else if (*e == '_') { |
| 2772 | *l++ = '-'; |
| 2773 | e++; |
| 2774 | } |
| 2775 | else { |
| 2776 | *l++ = *e++; |
| 2777 | } |
| 2778 | } |
| 2779 | *l = '\0'; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2780 | return 1; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2781 | } |
| 2782 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2783 | PyObject * |
| 2784 | PyUnicode_Decode(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2785 | Py_ssize_t size, |
| 2786 | const char *encoding, |
| 2787 | const char *errors) |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2788 | { |
| 2789 | PyObject *buffer = NULL, *unicode; |
| 2790 | Py_buffer info; |
| 2791 | char lower[11]; /* Enough for any encoding shortcut */ |
| 2792 | |
| 2793 | if (encoding == NULL) |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2794 | return PyUnicode_DecodeUTF8(s, size, errors); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 2795 | |
| 2796 | /* Shortcuts for common default encodings */ |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2797 | if (normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2798 | if ((strcmp(lower, "utf-8") == 0) || |
| 2799 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2800 | return PyUnicode_DecodeUTF8(s, size, errors); |
| 2801 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2802 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2803 | (strcmp(lower, "iso-8859-1") == 0)) |
| 2804 | return PyUnicode_DecodeLatin1(s, size, errors); |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 2805 | #ifdef HAVE_MBCS |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2806 | else if (strcmp(lower, "mbcs") == 0) |
| 2807 | return PyUnicode_DecodeMBCS(s, size, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 2808 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2809 | else if (strcmp(lower, "ascii") == 0) |
| 2810 | return PyUnicode_DecodeASCII(s, size, errors); |
| 2811 | else if (strcmp(lower, "utf-16") == 0) |
| 2812 | return PyUnicode_DecodeUTF16(s, size, errors, 0); |
| 2813 | else if (strcmp(lower, "utf-32") == 0) |
| 2814 | return PyUnicode_DecodeUTF32(s, size, errors, 0); |
| 2815 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2816 | |
| 2817 | /* Decode via the codec registry */ |
Guido van Rossum | be801ac | 2007-10-08 03:32:34 +0000 | [diff] [blame] | 2818 | buffer = NULL; |
Antoine Pitrou | c3b3924 | 2009-01-03 16:59:18 +0000 | [diff] [blame] | 2819 | 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] | 2820 | goto onError; |
Antoine Pitrou | ee58fa4 | 2008-08-19 18:22:14 +0000 | [diff] [blame] | 2821 | buffer = PyMemoryView_FromBuffer(&info); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2822 | if (buffer == NULL) |
| 2823 | goto onError; |
| 2824 | unicode = PyCodec_Decode(buffer, encoding, errors); |
| 2825 | if (unicode == NULL) |
| 2826 | goto onError; |
| 2827 | if (!PyUnicode_Check(unicode)) { |
| 2828 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 2829 | "decoder did not return a str object (type=%.400s)", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2830 | Py_TYPE(unicode)->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2831 | Py_DECREF(unicode); |
| 2832 | goto onError; |
| 2833 | } |
| 2834 | Py_DECREF(buffer); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 2835 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 2836 | if (_PyUnicode_READY_REPLACE(&unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2837 | Py_DECREF(unicode); |
| 2838 | return NULL; |
| 2839 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 2840 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2841 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2842 | return unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2843 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2844 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2845 | Py_XDECREF(buffer); |
| 2846 | return NULL; |
| 2847 | } |
| 2848 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2849 | PyObject * |
| 2850 | PyUnicode_AsDecodedObject(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2851 | const char *encoding, |
| 2852 | const char *errors) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2853 | { |
| 2854 | PyObject *v; |
| 2855 | |
| 2856 | if (!PyUnicode_Check(unicode)) { |
| 2857 | PyErr_BadArgument(); |
| 2858 | goto onError; |
| 2859 | } |
| 2860 | |
| 2861 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2862 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2863 | |
| 2864 | /* Decode via the codec registry */ |
| 2865 | v = PyCodec_Decode(unicode, encoding, errors); |
| 2866 | if (v == NULL) |
| 2867 | goto onError; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2868 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2869 | return v; |
| 2870 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2871 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2872 | return NULL; |
| 2873 | } |
| 2874 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2875 | PyObject * |
| 2876 | PyUnicode_AsDecodedUnicode(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2877 | const char *encoding, |
| 2878 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2879 | { |
| 2880 | PyObject *v; |
| 2881 | |
| 2882 | if (!PyUnicode_Check(unicode)) { |
| 2883 | PyErr_BadArgument(); |
| 2884 | goto onError; |
| 2885 | } |
| 2886 | |
| 2887 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2888 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2889 | |
| 2890 | /* Decode via the codec registry */ |
| 2891 | v = PyCodec_Decode(unicode, encoding, errors); |
| 2892 | if (v == NULL) |
| 2893 | goto onError; |
| 2894 | if (!PyUnicode_Check(v)) { |
| 2895 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 2896 | "decoder did not return a str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2897 | Py_TYPE(v)->tp_name); |
| 2898 | Py_DECREF(v); |
| 2899 | goto onError; |
| 2900 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2901 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2902 | return v; |
| 2903 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2904 | onError: |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2905 | return NULL; |
| 2906 | } |
| 2907 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2908 | PyObject * |
| 2909 | PyUnicode_Encode(const Py_UNICODE *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2910 | Py_ssize_t size, |
| 2911 | const char *encoding, |
| 2912 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2913 | { |
| 2914 | PyObject *v, *unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2915 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2916 | unicode = PyUnicode_FromUnicode(s, size); |
| 2917 | if (unicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2918 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2919 | v = PyUnicode_AsEncodedString(unicode, encoding, errors); |
| 2920 | Py_DECREF(unicode); |
| 2921 | return v; |
| 2922 | } |
| 2923 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2924 | PyObject * |
| 2925 | PyUnicode_AsEncodedObject(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2926 | const char *encoding, |
| 2927 | const char *errors) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2928 | { |
| 2929 | PyObject *v; |
| 2930 | |
| 2931 | if (!PyUnicode_Check(unicode)) { |
| 2932 | PyErr_BadArgument(); |
| 2933 | goto onError; |
| 2934 | } |
| 2935 | |
| 2936 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2937 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2938 | |
| 2939 | /* Encode via the codec registry */ |
| 2940 | v = PyCodec_Encode(unicode, encoding, errors); |
| 2941 | if (v == NULL) |
| 2942 | goto onError; |
| 2943 | return v; |
| 2944 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2945 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2946 | return NULL; |
| 2947 | } |
| 2948 | |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 2949 | PyObject * |
| 2950 | PyUnicode_EncodeFSDefault(PyObject *unicode) |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 2951 | { |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 2952 | #ifdef HAVE_MBCS |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 2953 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
| 2954 | PyUnicode_GET_SIZE(unicode), |
| 2955 | NULL); |
| 2956 | #elif defined(__APPLE__) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2957 | return _PyUnicode_AsUTF8String(unicode, "surrogateescape"); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 2958 | #else |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 2959 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 2960 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 2961 | cannot use it to encode and decode filenames before it is loaded. Load |
| 2962 | the Python codec requires to encode at least its own filename. Use the C |
| 2963 | version of the locale codec until the codec registry is initialized and |
| 2964 | the Python codec is loaded. |
| 2965 | |
| 2966 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 2967 | cannot only rely on it: check also interp->fscodec_initialized for |
| 2968 | subinterpreters. */ |
| 2969 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 2970 | return PyUnicode_AsEncodedString(unicode, |
| 2971 | Py_FileSystemDefaultEncoding, |
| 2972 | "surrogateescape"); |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 2973 | } |
| 2974 | else { |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 2975 | /* locale encoding with surrogateescape */ |
| 2976 | wchar_t *wchar; |
| 2977 | char *bytes; |
| 2978 | PyObject *bytes_obj; |
Victor Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 2979 | size_t error_pos; |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 2980 | |
| 2981 | wchar = PyUnicode_AsWideCharString(unicode, NULL); |
| 2982 | if (wchar == NULL) |
| 2983 | return NULL; |
Victor Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 2984 | bytes = _Py_wchar2char(wchar, &error_pos); |
| 2985 | if (bytes == NULL) { |
| 2986 | if (error_pos != (size_t)-1) { |
| 2987 | char *errmsg = strerror(errno); |
| 2988 | PyObject *exc = NULL; |
| 2989 | if (errmsg == NULL) |
| 2990 | errmsg = "Py_wchar2char() failed"; |
| 2991 | raise_encode_exception(&exc, |
| 2992 | "filesystemencoding", |
| 2993 | PyUnicode_AS_UNICODE(unicode), PyUnicode_GET_SIZE(unicode), |
| 2994 | error_pos, error_pos+1, |
| 2995 | errmsg); |
| 2996 | Py_XDECREF(exc); |
| 2997 | } |
| 2998 | else |
| 2999 | PyErr_NoMemory(); |
| 3000 | PyMem_Free(wchar); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3001 | return NULL; |
Victor Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 3002 | } |
| 3003 | PyMem_Free(wchar); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3004 | |
| 3005 | bytes_obj = PyBytes_FromString(bytes); |
| 3006 | PyMem_Free(bytes); |
| 3007 | return bytes_obj; |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 3008 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3009 | #endif |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3010 | } |
| 3011 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3012 | PyObject * |
| 3013 | PyUnicode_AsEncodedString(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3014 | const char *encoding, |
| 3015 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3016 | { |
| 3017 | PyObject *v; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3018 | char lower[11]; /* Enough for any encoding shortcut */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3019 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3020 | if (!PyUnicode_Check(unicode)) { |
| 3021 | PyErr_BadArgument(); |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3022 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3023 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3024 | |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3025 | if (encoding == NULL) { |
| 3026 | if (errors == NULL || strcmp(errors, "strict") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3027 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3028 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3029 | return _PyUnicode_AsUTF8String(unicode, errors); |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3030 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3031 | |
| 3032 | /* Shortcuts for common default encodings */ |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3033 | if (normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3034 | if ((strcmp(lower, "utf-8") == 0) || |
| 3035 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 3036 | { |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3037 | if (errors == NULL || strcmp(errors, "strict") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3038 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3039 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3040 | return _PyUnicode_AsUTF8String(unicode, errors); |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 3041 | } |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3042 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3043 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3044 | (strcmp(lower, "iso-8859-1") == 0)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3045 | return _PyUnicode_AsLatin1String(unicode, errors); |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3046 | #ifdef HAVE_MBCS |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3047 | else if (strcmp(lower, "mbcs") == 0) |
| 3048 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
| 3049 | PyUnicode_GET_SIZE(unicode), |
| 3050 | errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 3051 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3052 | else if (strcmp(lower, "ascii") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3053 | return _PyUnicode_AsASCIIString(unicode, errors); |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3054 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3055 | |
| 3056 | /* Encode via the codec registry */ |
| 3057 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3058 | if (v == NULL) |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3059 | return NULL; |
| 3060 | |
| 3061 | /* The normal path */ |
| 3062 | if (PyBytes_Check(v)) |
| 3063 | return v; |
| 3064 | |
| 3065 | /* 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] | 3066 | if (PyByteArray_Check(v)) { |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 3067 | int error; |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3068 | PyObject *b; |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 3069 | |
| 3070 | error = PyErr_WarnFormat(PyExc_RuntimeWarning, 1, |
| 3071 | "encoder %s returned bytearray instead of bytes", |
| 3072 | encoding); |
| 3073 | if (error) { |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3074 | Py_DECREF(v); |
| 3075 | return NULL; |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3076 | } |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3077 | |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3078 | b = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(v), Py_SIZE(v)); |
| 3079 | Py_DECREF(v); |
| 3080 | return b; |
| 3081 | } |
| 3082 | |
| 3083 | PyErr_Format(PyExc_TypeError, |
| 3084 | "encoder did not return a bytes object (type=%.400s)", |
| 3085 | Py_TYPE(v)->tp_name); |
| 3086 | Py_DECREF(v); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3087 | return NULL; |
| 3088 | } |
| 3089 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3090 | PyObject * |
| 3091 | PyUnicode_AsEncodedUnicode(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3092 | const char *encoding, |
| 3093 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3094 | { |
| 3095 | PyObject *v; |
| 3096 | |
| 3097 | if (!PyUnicode_Check(unicode)) { |
| 3098 | PyErr_BadArgument(); |
| 3099 | goto onError; |
| 3100 | } |
| 3101 | |
| 3102 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3103 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3104 | |
| 3105 | /* Encode via the codec registry */ |
| 3106 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3107 | if (v == NULL) |
| 3108 | goto onError; |
| 3109 | if (!PyUnicode_Check(v)) { |
| 3110 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3111 | "encoder did not return an str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3112 | Py_TYPE(v)->tp_name); |
| 3113 | Py_DECREF(v); |
| 3114 | goto onError; |
| 3115 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3116 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3117 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3118 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3119 | return NULL; |
| 3120 | } |
| 3121 | |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3122 | PyObject* |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3123 | PyUnicode_DecodeFSDefault(const char *s) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3124 | Py_ssize_t size = (Py_ssize_t)strlen(s); |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3125 | return PyUnicode_DecodeFSDefaultAndSize(s, size); |
| 3126 | } |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3127 | |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3128 | PyObject* |
| 3129 | PyUnicode_DecodeFSDefaultAndSize(const char *s, Py_ssize_t size) |
| 3130 | { |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3131 | #ifdef HAVE_MBCS |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3132 | return PyUnicode_DecodeMBCS(s, size, NULL); |
| 3133 | #elif defined(__APPLE__) |
| 3134 | return PyUnicode_DecodeUTF8(s, size, "surrogateescape"); |
| 3135 | #else |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 3136 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 3137 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 3138 | cannot use it to encode and decode filenames before it is loaded. Load |
| 3139 | the Python codec requires to encode at least its own filename. Use the C |
| 3140 | version of the locale codec until the codec registry is initialized and |
| 3141 | the Python codec is loaded. |
| 3142 | |
| 3143 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 3144 | cannot only rely on it: check also interp->fscodec_initialized for |
| 3145 | subinterpreters. */ |
| 3146 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3147 | return PyUnicode_Decode(s, size, |
| 3148 | Py_FileSystemDefaultEncoding, |
Victor Stinner | b9a20ad | 2010-04-30 16:37:52 +0000 | [diff] [blame] | 3149 | "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3150 | } |
| 3151 | else { |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3152 | /* locale encoding with surrogateescape */ |
| 3153 | wchar_t *wchar; |
| 3154 | PyObject *unicode; |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 3155 | size_t len; |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3156 | |
| 3157 | if (s[size] != '\0' || size != strlen(s)) { |
| 3158 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
| 3159 | return NULL; |
| 3160 | } |
| 3161 | |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 3162 | wchar = _Py_char2wchar(s, &len); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3163 | if (wchar == NULL) |
Victor Stinner | d5af0a5 | 2010-11-08 23:34:29 +0000 | [diff] [blame] | 3164 | return PyErr_NoMemory(); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3165 | |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 3166 | unicode = PyUnicode_FromWideChar(wchar, len); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3167 | PyMem_Free(wchar); |
| 3168 | return unicode; |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3169 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3170 | #endif |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3171 | } |
| 3172 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3173 | |
| 3174 | int |
| 3175 | PyUnicode_FSConverter(PyObject* arg, void* addr) |
| 3176 | { |
| 3177 | PyObject *output = NULL; |
| 3178 | Py_ssize_t size; |
| 3179 | void *data; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3180 | if (arg == NULL) { |
| 3181 | Py_DECREF(*(PyObject**)addr); |
| 3182 | return 1; |
| 3183 | } |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 3184 | if (PyBytes_Check(arg)) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3185 | output = arg; |
| 3186 | Py_INCREF(output); |
| 3187 | } |
| 3188 | else { |
| 3189 | arg = PyUnicode_FromObject(arg); |
| 3190 | if (!arg) |
| 3191 | return 0; |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3192 | output = PyUnicode_EncodeFSDefault(arg); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3193 | Py_DECREF(arg); |
| 3194 | if (!output) |
| 3195 | return 0; |
| 3196 | if (!PyBytes_Check(output)) { |
| 3197 | Py_DECREF(output); |
| 3198 | PyErr_SetString(PyExc_TypeError, "encoder failed to return bytes"); |
| 3199 | return 0; |
| 3200 | } |
| 3201 | } |
Victor Stinner | 0ea2a46 | 2010-04-30 00:22:08 +0000 | [diff] [blame] | 3202 | size = PyBytes_GET_SIZE(output); |
| 3203 | data = PyBytes_AS_STRING(output); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3204 | if (size != strlen(data)) { |
Benjamin Peterson | 7a6b44a | 2011-08-18 13:51:47 -0500 | [diff] [blame] | 3205 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3206 | Py_DECREF(output); |
| 3207 | return 0; |
| 3208 | } |
| 3209 | *(PyObject**)addr = output; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3210 | return Py_CLEANUP_SUPPORTED; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3211 | } |
| 3212 | |
| 3213 | |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3214 | int |
| 3215 | PyUnicode_FSDecoder(PyObject* arg, void* addr) |
| 3216 | { |
| 3217 | PyObject *output = NULL; |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3218 | if (arg == NULL) { |
| 3219 | Py_DECREF(*(PyObject**)addr); |
| 3220 | return 1; |
| 3221 | } |
| 3222 | if (PyUnicode_Check(arg)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3223 | if (PyUnicode_READY(arg)) |
| 3224 | return 0; |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3225 | output = arg; |
| 3226 | Py_INCREF(output); |
| 3227 | } |
| 3228 | else { |
| 3229 | arg = PyBytes_FromObject(arg); |
| 3230 | if (!arg) |
| 3231 | return 0; |
| 3232 | output = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AS_STRING(arg), |
| 3233 | PyBytes_GET_SIZE(arg)); |
| 3234 | Py_DECREF(arg); |
| 3235 | if (!output) |
| 3236 | return 0; |
| 3237 | if (!PyUnicode_Check(output)) { |
| 3238 | Py_DECREF(output); |
| 3239 | PyErr_SetString(PyExc_TypeError, "decoder failed to return unicode"); |
| 3240 | return 0; |
| 3241 | } |
| 3242 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3243 | if (findchar(PyUnicode_DATA(output), PyUnicode_KIND(output), |
| 3244 | PyUnicode_GET_LENGTH(output), 0, 1)) { |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3245 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
| 3246 | Py_DECREF(output); |
| 3247 | return 0; |
| 3248 | } |
| 3249 | *(PyObject**)addr = output; |
| 3250 | return Py_CLEANUP_SUPPORTED; |
| 3251 | } |
| 3252 | |
| 3253 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3254 | char* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3255 | PyUnicode_AsUTF8AndSize(PyObject *unicode, Py_ssize_t *psize) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3256 | { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 3257 | PyObject *bytes; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3258 | PyUnicodeObject *u = (PyUnicodeObject *)unicode; |
| 3259 | |
Neal Norwitz | e0a0a6e | 2007-08-25 01:04:21 +0000 | [diff] [blame] | 3260 | if (!PyUnicode_Check(unicode)) { |
| 3261 | PyErr_BadArgument(); |
| 3262 | return NULL; |
| 3263 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3264 | if (PyUnicode_READY(u) == -1) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3265 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3266 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3267 | if (PyUnicode_UTF8(unicode) == NULL) { |
| 3268 | assert(!PyUnicode_IS_COMPACT_ASCII(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3269 | bytes = _PyUnicode_AsUTF8String(unicode, "strict"); |
| 3270 | if (bytes == NULL) |
| 3271 | return NULL; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3272 | _PyUnicode_UTF8(u) = PyObject_MALLOC(PyBytes_GET_SIZE(bytes) + 1); |
| 3273 | if (_PyUnicode_UTF8(u) == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3274 | Py_DECREF(bytes); |
| 3275 | return NULL; |
| 3276 | } |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3277 | _PyUnicode_UTF8_LENGTH(u) = PyBytes_GET_SIZE(bytes); |
| 3278 | Py_MEMCPY(_PyUnicode_UTF8(u), PyBytes_AS_STRING(bytes), _PyUnicode_UTF8_LENGTH(u) + 1); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3279 | Py_DECREF(bytes); |
| 3280 | } |
| 3281 | |
| 3282 | if (psize) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3283 | *psize = PyUnicode_UTF8_LENGTH(unicode); |
| 3284 | return PyUnicode_UTF8(unicode); |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 3285 | } |
| 3286 | |
| 3287 | char* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3288 | PyUnicode_AsUTF8(PyObject *unicode) |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 3289 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3290 | return PyUnicode_AsUTF8AndSize(unicode, NULL); |
| 3291 | } |
| 3292 | |
| 3293 | #ifdef Py_DEBUG |
| 3294 | int unicode_as_unicode_calls = 0; |
| 3295 | #endif |
| 3296 | |
| 3297 | |
| 3298 | Py_UNICODE * |
| 3299 | PyUnicode_AsUnicodeAndSize(PyObject *unicode, Py_ssize_t *size) |
| 3300 | { |
| 3301 | PyUnicodeObject *u; |
| 3302 | const unsigned char *one_byte; |
| 3303 | #if SIZEOF_WCHAR_T == 4 |
| 3304 | const Py_UCS2 *two_bytes; |
| 3305 | #else |
| 3306 | const Py_UCS4 *four_bytes; |
| 3307 | const Py_UCS4 *ucs4_end; |
| 3308 | Py_ssize_t num_surrogates; |
| 3309 | #endif |
| 3310 | wchar_t *w; |
| 3311 | wchar_t *wchar_end; |
| 3312 | |
| 3313 | if (!PyUnicode_Check(unicode)) { |
| 3314 | PyErr_BadArgument(); |
| 3315 | return NULL; |
| 3316 | } |
| 3317 | u = (PyUnicodeObject*)unicode; |
| 3318 | if (_PyUnicode_WSTR(u) == NULL) { |
| 3319 | /* Non-ASCII compact unicode object */ |
| 3320 | assert(_PyUnicode_KIND(u) != 0); |
| 3321 | assert(PyUnicode_IS_READY(u)); |
| 3322 | |
| 3323 | #ifdef Py_DEBUG |
| 3324 | ++unicode_as_unicode_calls; |
| 3325 | #endif |
| 3326 | |
| 3327 | if (PyUnicode_KIND(u) == PyUnicode_4BYTE_KIND) { |
| 3328 | #if SIZEOF_WCHAR_T == 2 |
| 3329 | four_bytes = PyUnicode_4BYTE_DATA(u); |
| 3330 | ucs4_end = four_bytes + _PyUnicode_LENGTH(u); |
| 3331 | num_surrogates = 0; |
| 3332 | |
| 3333 | for (; four_bytes < ucs4_end; ++four_bytes) { |
| 3334 | if (*four_bytes > 0xFFFF) |
| 3335 | ++num_surrogates; |
| 3336 | } |
| 3337 | |
| 3338 | _PyUnicode_WSTR(u) = (wchar_t *) PyObject_MALLOC( |
| 3339 | sizeof(wchar_t) * (_PyUnicode_LENGTH(u) + 1 + num_surrogates)); |
| 3340 | if (!_PyUnicode_WSTR(u)) { |
| 3341 | PyErr_NoMemory(); |
| 3342 | return NULL; |
| 3343 | } |
| 3344 | _PyUnicode_WSTR_LENGTH(u) = _PyUnicode_LENGTH(u) + num_surrogates; |
| 3345 | |
| 3346 | w = _PyUnicode_WSTR(u); |
| 3347 | wchar_end = w + _PyUnicode_WSTR_LENGTH(u); |
| 3348 | four_bytes = PyUnicode_4BYTE_DATA(u); |
| 3349 | for (; four_bytes < ucs4_end; ++four_bytes, ++w) { |
| 3350 | if (*four_bytes > 0xFFFF) { |
| 3351 | /* encode surrogate pair in this case */ |
| 3352 | *w++ = 0xD800 | ((*four_bytes - 0x10000) >> 10); |
| 3353 | *w = 0xDC00 | ((*four_bytes - 0x10000) & 0x3FF); |
| 3354 | } |
| 3355 | else |
| 3356 | *w = *four_bytes; |
| 3357 | |
| 3358 | if (w > wchar_end) { |
| 3359 | assert(0 && "Miscalculated string end"); |
| 3360 | } |
| 3361 | } |
| 3362 | *w = 0; |
| 3363 | #else |
| 3364 | /* sizeof(wchar_t) == 4 */ |
| 3365 | Py_FatalError("Impossible unicode object state, wstr and str " |
| 3366 | "should share memory already."); |
| 3367 | return NULL; |
| 3368 | #endif |
| 3369 | } |
| 3370 | else { |
| 3371 | _PyUnicode_WSTR(u) = (wchar_t *) PyObject_MALLOC(sizeof(wchar_t) * |
| 3372 | (_PyUnicode_LENGTH(u) + 1)); |
| 3373 | if (!_PyUnicode_WSTR(u)) { |
| 3374 | PyErr_NoMemory(); |
| 3375 | return NULL; |
| 3376 | } |
| 3377 | if (!PyUnicode_IS_COMPACT_ASCII(u)) |
| 3378 | _PyUnicode_WSTR_LENGTH(u) = _PyUnicode_LENGTH(u); |
| 3379 | w = _PyUnicode_WSTR(u); |
| 3380 | wchar_end = w + _PyUnicode_LENGTH(u); |
| 3381 | |
| 3382 | if (PyUnicode_KIND(u) == PyUnicode_1BYTE_KIND) { |
| 3383 | one_byte = PyUnicode_1BYTE_DATA(u); |
| 3384 | for (; w < wchar_end; ++one_byte, ++w) |
| 3385 | *w = *one_byte; |
| 3386 | /* null-terminate the wstr */ |
| 3387 | *w = 0; |
| 3388 | } |
| 3389 | else if (PyUnicode_KIND(u) == PyUnicode_2BYTE_KIND) { |
| 3390 | #if SIZEOF_WCHAR_T == 4 |
| 3391 | two_bytes = PyUnicode_2BYTE_DATA(u); |
| 3392 | for (; w < wchar_end; ++two_bytes, ++w) |
| 3393 | *w = *two_bytes; |
| 3394 | /* null-terminate the wstr */ |
| 3395 | *w = 0; |
| 3396 | #else |
| 3397 | /* sizeof(wchar_t) == 2 */ |
| 3398 | PyObject_FREE(_PyUnicode_WSTR(u)); |
| 3399 | _PyUnicode_WSTR(u) = NULL; |
| 3400 | Py_FatalError("Impossible unicode object state, wstr " |
| 3401 | "and str should share memory already."); |
| 3402 | return NULL; |
| 3403 | #endif |
| 3404 | } |
| 3405 | else { |
| 3406 | assert(0 && "This should never happen."); |
| 3407 | } |
| 3408 | } |
| 3409 | } |
| 3410 | if (size != NULL) |
| 3411 | *size = PyUnicode_WSTR_LENGTH(u); |
| 3412 | return _PyUnicode_WSTR(u); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3413 | } |
| 3414 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3415 | Py_UNICODE * |
| 3416 | PyUnicode_AsUnicode(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3417 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3418 | return PyUnicode_AsUnicodeAndSize(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3419 | } |
| 3420 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3421 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3422 | Py_ssize_t |
| 3423 | PyUnicode_GetSize(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3424 | { |
| 3425 | if (!PyUnicode_Check(unicode)) { |
| 3426 | PyErr_BadArgument(); |
| 3427 | goto onError; |
| 3428 | } |
| 3429 | return PyUnicode_GET_SIZE(unicode); |
| 3430 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3431 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3432 | return -1; |
| 3433 | } |
| 3434 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3435 | Py_ssize_t |
| 3436 | PyUnicode_GetLength(PyObject *unicode) |
| 3437 | { |
Victor Stinner | 5a706cf | 2011-10-02 00:36:53 +0200 | [diff] [blame] | 3438 | if (!PyUnicode_Check(unicode) || PyUnicode_READY(unicode) == -1) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3439 | PyErr_BadArgument(); |
| 3440 | return -1; |
| 3441 | } |
| 3442 | |
| 3443 | return PyUnicode_GET_LENGTH(unicode); |
| 3444 | } |
| 3445 | |
| 3446 | Py_UCS4 |
| 3447 | PyUnicode_ReadChar(PyObject *unicode, Py_ssize_t index) |
| 3448 | { |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 3449 | if (!PyUnicode_Check(unicode) || PyUnicode_READY(unicode) == -1) { |
| 3450 | PyErr_BadArgument(); |
| 3451 | return (Py_UCS4)-1; |
| 3452 | } |
| 3453 | if (index < 0 || index >= _PyUnicode_LENGTH(unicode)) { |
| 3454 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3455 | return (Py_UCS4)-1; |
| 3456 | } |
| 3457 | return PyUnicode_READ_CHAR(unicode, index); |
| 3458 | } |
| 3459 | |
| 3460 | int |
| 3461 | PyUnicode_WriteChar(PyObject *unicode, Py_ssize_t index, Py_UCS4 ch) |
| 3462 | { |
| 3463 | if (!PyUnicode_Check(unicode) || !PyUnicode_IS_COMPACT(unicode)) { |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 3464 | PyErr_BadArgument(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3465 | return -1; |
| 3466 | } |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 3467 | if (index < 0 || index >= _PyUnicode_LENGTH(unicode)) { |
| 3468 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 3469 | return -1; |
| 3470 | } |
| 3471 | if (_PyUnicode_Dirty(unicode)) |
| 3472 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3473 | PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode), |
| 3474 | index, ch); |
| 3475 | return 0; |
| 3476 | } |
| 3477 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3478 | const char * |
| 3479 | PyUnicode_GetDefaultEncoding(void) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3480 | { |
Victor Stinner | 42cb462 | 2010-09-01 19:39:01 +0000 | [diff] [blame] | 3481 | return "utf-8"; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3482 | } |
| 3483 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 3484 | /* create or adjust a UnicodeDecodeError */ |
| 3485 | static void |
| 3486 | make_decode_exception(PyObject **exceptionObject, |
| 3487 | const char *encoding, |
| 3488 | const char *input, Py_ssize_t length, |
| 3489 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 3490 | const char *reason) |
| 3491 | { |
| 3492 | if (*exceptionObject == NULL) { |
| 3493 | *exceptionObject = PyUnicodeDecodeError_Create( |
| 3494 | encoding, input, length, startpos, endpos, reason); |
| 3495 | } |
| 3496 | else { |
| 3497 | if (PyUnicodeDecodeError_SetStart(*exceptionObject, startpos)) |
| 3498 | goto onError; |
| 3499 | if (PyUnicodeDecodeError_SetEnd(*exceptionObject, endpos)) |
| 3500 | goto onError; |
| 3501 | if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) |
| 3502 | goto onError; |
| 3503 | } |
| 3504 | return; |
| 3505 | |
| 3506 | onError: |
| 3507 | Py_DECREF(*exceptionObject); |
| 3508 | *exceptionObject = NULL; |
| 3509 | } |
| 3510 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3511 | /* error handling callback helper: |
| 3512 | build arguments, call the callback and check the arguments, |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 3513 | if no exception occurred, copy the replacement to the output |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3514 | and adjust various state variables. |
| 3515 | return 0 on success, -1 on error |
| 3516 | */ |
| 3517 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3518 | static int |
| 3519 | unicode_decode_call_errorhandler(const char *errors, PyObject **errorHandler, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3520 | const char *encoding, const char *reason, |
| 3521 | const char **input, const char **inend, Py_ssize_t *startinpos, |
| 3522 | Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
| 3523 | PyUnicodeObject **output, Py_ssize_t *outpos, Py_UNICODE **outptr) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3524 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3525 | 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] | 3526 | |
| 3527 | PyObject *restuple = NULL; |
| 3528 | PyObject *repunicode = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3529 | Py_ssize_t outsize = PyUnicode_GET_SIZE(*output); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3530 | Py_ssize_t insize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3531 | Py_ssize_t requiredsize; |
| 3532 | Py_ssize_t newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3533 | const Py_UNICODE *repptr; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3534 | PyObject *inputobj = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3535 | Py_ssize_t repsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3536 | int res = -1; |
| 3537 | |
| 3538 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3539 | *errorHandler = PyCodec_LookupError(errors); |
| 3540 | if (*errorHandler == NULL) |
| 3541 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3542 | } |
| 3543 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 3544 | make_decode_exception(exceptionObject, |
| 3545 | encoding, |
| 3546 | *input, *inend - *input, |
| 3547 | *startinpos, *endinpos, |
| 3548 | reason); |
| 3549 | if (*exceptionObject == NULL) |
| 3550 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3551 | |
| 3552 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 3553 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3554 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3555 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 3556 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3557 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3558 | } |
| 3559 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3560 | goto onError; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3561 | |
| 3562 | /* Copy back the bytes variables, which might have been modified by the |
| 3563 | callback */ |
| 3564 | inputobj = PyUnicodeDecodeError_GetObject(*exceptionObject); |
| 3565 | if (!inputobj) |
| 3566 | goto onError; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3567 | if (!PyBytes_Check(inputobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3568 | PyErr_Format(PyExc_TypeError, "exception attribute object must be bytes"); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3569 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3570 | *input = PyBytes_AS_STRING(inputobj); |
| 3571 | insize = PyBytes_GET_SIZE(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3572 | *inend = *input + insize; |
Walter Dörwald | 36f938f | 2007-08-10 10:11:43 +0000 | [diff] [blame] | 3573 | /* we can DECREF safely, as the exception has another reference, |
| 3574 | so the object won't go away. */ |
| 3575 | Py_DECREF(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3576 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3577 | if (newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3578 | newpos = insize+newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 3579 | if (newpos<0 || newpos>insize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3580 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", newpos); |
| 3581 | goto onError; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 3582 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3583 | |
| 3584 | /* need more space? (at least enough for what we |
| 3585 | have+the replacement+the rest of the string (starting |
| 3586 | at the new input position), so we won't have to check space |
| 3587 | when there are no errors in the rest of the string) */ |
| 3588 | repptr = PyUnicode_AS_UNICODE(repunicode); |
| 3589 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 3590 | requiredsize = *outpos + repsize + insize-newpos; |
| 3591 | if (requiredsize > outsize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3592 | if (requiredsize<2*outsize) |
| 3593 | requiredsize = 2*outsize; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 3594 | if (PyUnicode_Resize((PyObject**)output, requiredsize) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3595 | goto onError; |
| 3596 | *outptr = PyUnicode_AS_UNICODE(*output) + *outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3597 | } |
| 3598 | *endinpos = newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3599 | *inptr = *input + newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3600 | Py_UNICODE_COPY(*outptr, repptr, repsize); |
| 3601 | *outptr += repsize; |
| 3602 | *outpos += repsize; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3603 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3604 | /* we made it! */ |
| 3605 | res = 0; |
| 3606 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3607 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3608 | Py_XDECREF(restuple); |
| 3609 | return res; |
| 3610 | } |
| 3611 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3612 | /* --- UTF-7 Codec -------------------------------------------------------- */ |
| 3613 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3614 | /* See RFC2152 for details. We encode conservatively and decode liberally. */ |
| 3615 | |
| 3616 | /* Three simple macros defining base-64. */ |
| 3617 | |
| 3618 | /* Is c a base-64 character? */ |
| 3619 | |
| 3620 | #define IS_BASE64(c) \ |
| 3621 | (((c) >= 'A' && (c) <= 'Z') || \ |
| 3622 | ((c) >= 'a' && (c) <= 'z') || \ |
| 3623 | ((c) >= '0' && (c) <= '9') || \ |
| 3624 | (c) == '+' || (c) == '/') |
| 3625 | |
| 3626 | /* given that c is a base-64 character, what is its base-64 value? */ |
| 3627 | |
| 3628 | #define FROM_BASE64(c) \ |
| 3629 | (((c) >= 'A' && (c) <= 'Z') ? (c) - 'A' : \ |
| 3630 | ((c) >= 'a' && (c) <= 'z') ? (c) - 'a' + 26 : \ |
| 3631 | ((c) >= '0' && (c) <= '9') ? (c) - '0' + 52 : \ |
| 3632 | (c) == '+' ? 62 : 63) |
| 3633 | |
| 3634 | /* What is the base-64 character of the bottom 6 bits of n? */ |
| 3635 | |
| 3636 | #define TO_BASE64(n) \ |
| 3637 | ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) |
| 3638 | |
| 3639 | /* DECODE_DIRECT: this byte encountered in a UTF-7 string should be |
| 3640 | * decoded as itself. We are permissive on decoding; the only ASCII |
| 3641 | * byte not decoding to itself is the + which begins a base64 |
| 3642 | * string. */ |
| 3643 | |
| 3644 | #define DECODE_DIRECT(c) \ |
| 3645 | ((c) <= 127 && (c) != '+') |
| 3646 | |
| 3647 | /* The UTF-7 encoder treats ASCII characters differently according to |
| 3648 | * whether they are Set D, Set O, Whitespace, or special (i.e. none of |
| 3649 | * the above). See RFC2152. This array identifies these different |
| 3650 | * sets: |
| 3651 | * 0 : "Set D" |
| 3652 | * alphanumeric and '(),-./:? |
| 3653 | * 1 : "Set O" |
| 3654 | * !"#$%&*;<=>@[]^_`{|} |
| 3655 | * 2 : "whitespace" |
| 3656 | * ht nl cr sp |
| 3657 | * 3 : special (must be base64 encoded) |
| 3658 | * everything else (i.e. +\~ and non-printing codes 0-8 11-12 14-31 127) |
| 3659 | */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3660 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3661 | static |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3662 | char utf7_category[128] = { |
| 3663 | /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */ |
| 3664 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 3, 3, 2, 3, 3, |
| 3665 | /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */ |
| 3666 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 3667 | /* sp ! " # $ % & ' ( ) * + , - . / */ |
| 3668 | 2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 3, 0, 0, 0, 0, |
| 3669 | /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */ |
| 3670 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, |
| 3671 | /* @ A B C D E F G H I J K L M N O */ |
| 3672 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 3673 | /* P Q R S T U V W X Y Z [ \ ] ^ _ */ |
| 3674 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 1, 1, 1, |
| 3675 | /* ` a b c d e f g h i j k l m n o */ |
| 3676 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 3677 | /* p q r s t u v w x y z { | } ~ del */ |
| 3678 | 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] | 3679 | }; |
| 3680 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3681 | /* ENCODE_DIRECT: this character should be encoded as itself. The |
| 3682 | * answer depends on whether we are encoding set O as itself, and also |
| 3683 | * on whether we are encoding whitespace as itself. RFC2152 makes it |
| 3684 | * clear that the answers to these questions vary between |
| 3685 | * applications, so this code needs to be flexible. */ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 3686 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3687 | #define ENCODE_DIRECT(c, directO, directWS) \ |
| 3688 | ((c) < 128 && (c) > 0 && \ |
| 3689 | ((utf7_category[(c)] == 0) || \ |
| 3690 | (directWS && (utf7_category[(c)] == 2)) || \ |
| 3691 | (directO && (utf7_category[(c)] == 1)))) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3692 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3693 | PyObject * |
| 3694 | PyUnicode_DecodeUTF7(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3695 | Py_ssize_t size, |
| 3696 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3697 | { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3698 | return PyUnicode_DecodeUTF7Stateful(s, size, errors, NULL); |
| 3699 | } |
| 3700 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3701 | /* The decoder. The only state we preserve is our read position, |
| 3702 | * i.e. how many characters we have consumed. So if we end in the |
| 3703 | * middle of a shift sequence we have to back off the read position |
| 3704 | * and the output to the beginning of the sequence, otherwise we lose |
| 3705 | * all the shift state (seen bits, number of bits seen, high |
| 3706 | * surrogate). */ |
| 3707 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3708 | PyObject * |
| 3709 | PyUnicode_DecodeUTF7Stateful(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3710 | Py_ssize_t size, |
| 3711 | const char *errors, |
| 3712 | Py_ssize_t *consumed) |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3713 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3714 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3715 | Py_ssize_t startinpos; |
| 3716 | Py_ssize_t endinpos; |
| 3717 | Py_ssize_t outpos; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3718 | const char *e; |
| 3719 | PyUnicodeObject *unicode; |
| 3720 | Py_UNICODE *p; |
| 3721 | const char *errmsg = ""; |
| 3722 | int inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3723 | Py_UNICODE *shiftOutStart; |
| 3724 | unsigned int base64bits = 0; |
| 3725 | unsigned long base64buffer = 0; |
| 3726 | Py_UNICODE surrogate = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3727 | PyObject *errorHandler = NULL; |
| 3728 | PyObject *exc = NULL; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3729 | |
| 3730 | unicode = _PyUnicode_New(size); |
| 3731 | if (!unicode) |
| 3732 | return NULL; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3733 | if (size == 0) { |
| 3734 | if (consumed) |
| 3735 | *consumed = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3736 | return (PyObject *)unicode; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3737 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3738 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3739 | p = PyUnicode_AS_UNICODE(unicode); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3740 | shiftOutStart = p; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3741 | e = s + size; |
| 3742 | |
| 3743 | while (s < e) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3744 | Py_UNICODE ch; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3745 | restart: |
Antoine Pitrou | 5ffd9e9 | 2008-07-25 18:05:24 +0000 | [diff] [blame] | 3746 | ch = (unsigned char) *s; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3747 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3748 | if (inShift) { /* in a base-64 section */ |
| 3749 | if (IS_BASE64(ch)) { /* consume a base-64 character */ |
| 3750 | base64buffer = (base64buffer << 6) | FROM_BASE64(ch); |
| 3751 | base64bits += 6; |
| 3752 | s++; |
| 3753 | if (base64bits >= 16) { |
| 3754 | /* we have enough bits for a UTF-16 value */ |
| 3755 | Py_UNICODE outCh = (Py_UNICODE) |
| 3756 | (base64buffer >> (base64bits-16)); |
| 3757 | base64bits -= 16; |
| 3758 | base64buffer &= (1 << base64bits) - 1; /* clear high bits */ |
| 3759 | if (surrogate) { |
| 3760 | /* expecting a second surrogate */ |
| 3761 | if (outCh >= 0xDC00 && outCh <= 0xDFFF) { |
| 3762 | #ifdef Py_UNICODE_WIDE |
| 3763 | *p++ = (((surrogate & 0x3FF)<<10) |
| 3764 | | (outCh & 0x3FF)) + 0x10000; |
| 3765 | #else |
| 3766 | *p++ = surrogate; |
| 3767 | *p++ = outCh; |
| 3768 | #endif |
| 3769 | surrogate = 0; |
| 3770 | } |
| 3771 | else { |
| 3772 | surrogate = 0; |
| 3773 | errmsg = "second surrogate missing"; |
| 3774 | goto utf7Error; |
| 3775 | } |
| 3776 | } |
| 3777 | else if (outCh >= 0xD800 && outCh <= 0xDBFF) { |
| 3778 | /* first surrogate */ |
| 3779 | surrogate = outCh; |
| 3780 | } |
| 3781 | else if (outCh >= 0xDC00 && outCh <= 0xDFFF) { |
| 3782 | errmsg = "unexpected second surrogate"; |
| 3783 | goto utf7Error; |
| 3784 | } |
| 3785 | else { |
| 3786 | *p++ = outCh; |
| 3787 | } |
| 3788 | } |
| 3789 | } |
| 3790 | else { /* now leaving a base-64 section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3791 | inShift = 0; |
| 3792 | s++; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3793 | if (surrogate) { |
| 3794 | errmsg = "second surrogate missing at end of shift sequence"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3795 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3796 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3797 | if (base64bits > 0) { /* left-over bits */ |
| 3798 | if (base64bits >= 6) { |
| 3799 | /* We've seen at least one base-64 character */ |
| 3800 | errmsg = "partial character in shift sequence"; |
| 3801 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3802 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3803 | else { |
| 3804 | /* Some bits remain; they should be zero */ |
| 3805 | if (base64buffer != 0) { |
| 3806 | errmsg = "non-zero padding bits in shift sequence"; |
| 3807 | goto utf7Error; |
| 3808 | } |
| 3809 | } |
| 3810 | } |
| 3811 | if (ch != '-') { |
| 3812 | /* '-' is absorbed; other terminating |
| 3813 | characters are preserved */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3814 | *p++ = ch; |
| 3815 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3816 | } |
| 3817 | } |
| 3818 | else if ( ch == '+' ) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3819 | startinpos = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3820 | s++; /* consume '+' */ |
| 3821 | if (s < e && *s == '-') { /* '+-' encodes '+' */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3822 | s++; |
| 3823 | *p++ = '+'; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3824 | } |
| 3825 | else { /* begin base64-encoded section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3826 | inShift = 1; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3827 | shiftOutStart = p; |
| 3828 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3829 | } |
| 3830 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3831 | else if (DECODE_DIRECT(ch)) { /* character decodes as itself */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3832 | *p++ = ch; |
| 3833 | s++; |
| 3834 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3835 | else { |
| 3836 | startinpos = s-starts; |
| 3837 | s++; |
| 3838 | errmsg = "unexpected special character"; |
| 3839 | goto utf7Error; |
| 3840 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3841 | continue; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3842 | utf7Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3843 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 3844 | endinpos = s-starts; |
| 3845 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3846 | errors, &errorHandler, |
| 3847 | "utf7", errmsg, |
| 3848 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 3849 | &unicode, &outpos, &p)) |
| 3850 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3851 | } |
| 3852 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3853 | /* end of string */ |
| 3854 | |
| 3855 | if (inShift && !consumed) { /* in shift sequence, no more to follow */ |
| 3856 | /* if we're in an inconsistent state, that's an error */ |
| 3857 | if (surrogate || |
| 3858 | (base64bits >= 6) || |
| 3859 | (base64bits > 0 && base64buffer != 0)) { |
| 3860 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 3861 | endinpos = size; |
| 3862 | if (unicode_decode_call_errorhandler( |
| 3863 | errors, &errorHandler, |
| 3864 | "utf7", "unterminated shift sequence", |
| 3865 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 3866 | &unicode, &outpos, &p)) |
| 3867 | goto onError; |
| 3868 | if (s < e) |
| 3869 | goto restart; |
| 3870 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3871 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3872 | |
| 3873 | /* return state */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3874 | if (consumed) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3875 | if (inShift) { |
| 3876 | p = shiftOutStart; /* back off output */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3877 | *consumed = startinpos; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3878 | } |
| 3879 | else { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3880 | *consumed = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3881 | } |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3882 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3883 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 3884 | if (PyUnicode_Resize((PyObject**)&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3885 | goto onError; |
| 3886 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3887 | Py_XDECREF(errorHandler); |
| 3888 | Py_XDECREF(exc); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 3889 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 3890 | if (_PyUnicode_READY_REPLACE(&unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3891 | Py_DECREF(unicode); |
| 3892 | return NULL; |
| 3893 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 3894 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 3895 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3896 | return (PyObject *)unicode; |
| 3897 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3898 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3899 | Py_XDECREF(errorHandler); |
| 3900 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3901 | Py_DECREF(unicode); |
| 3902 | return NULL; |
| 3903 | } |
| 3904 | |
| 3905 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3906 | PyObject * |
| 3907 | PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3908 | Py_ssize_t size, |
| 3909 | int base64SetO, |
| 3910 | int base64WhiteSpace, |
| 3911 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3912 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3913 | PyObject *v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3914 | /* It might be possible to tighten this worst case */ |
Alexandre Vassalotti | e85bd98 | 2009-07-21 00:39:03 +0000 | [diff] [blame] | 3915 | Py_ssize_t allocated = 8 * size; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3916 | int inShift = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3917 | Py_ssize_t i = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3918 | unsigned int base64bits = 0; |
| 3919 | unsigned long base64buffer = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3920 | char * out; |
| 3921 | char * start; |
| 3922 | |
| 3923 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3924 | return PyBytes_FromStringAndSize(NULL, 0); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3925 | |
Alexandre Vassalotti | e85bd98 | 2009-07-21 00:39:03 +0000 | [diff] [blame] | 3926 | if (allocated / 8 != size) |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3927 | return PyErr_NoMemory(); |
| 3928 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3929 | v = PyBytes_FromStringAndSize(NULL, allocated); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3930 | if (v == NULL) |
| 3931 | return NULL; |
| 3932 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3933 | start = out = PyBytes_AS_STRING(v); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3934 | for (;i < size; ++i) { |
| 3935 | Py_UNICODE ch = s[i]; |
| 3936 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3937 | if (inShift) { |
| 3938 | if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 3939 | /* shifting out */ |
| 3940 | if (base64bits) { /* output remaining bits */ |
| 3941 | *out++ = TO_BASE64(base64buffer << (6-base64bits)); |
| 3942 | base64buffer = 0; |
| 3943 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3944 | } |
| 3945 | inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3946 | /* Characters not in the BASE64 set implicitly unshift the sequence |
| 3947 | so no '-' is required, except if the character is itself a '-' */ |
| 3948 | if (IS_BASE64(ch) || ch == '-') { |
| 3949 | *out++ = '-'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3950 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3951 | *out++ = (char) ch; |
| 3952 | } |
| 3953 | else { |
| 3954 | goto encode_char; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3955 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3956 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3957 | else { /* not in a shift sequence */ |
| 3958 | if (ch == '+') { |
| 3959 | *out++ = '+'; |
| 3960 | *out++ = '-'; |
| 3961 | } |
| 3962 | else if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 3963 | *out++ = (char) ch; |
| 3964 | } |
| 3965 | else { |
| 3966 | *out++ = '+'; |
| 3967 | inShift = 1; |
| 3968 | goto encode_char; |
| 3969 | } |
| 3970 | } |
| 3971 | continue; |
| 3972 | encode_char: |
| 3973 | #ifdef Py_UNICODE_WIDE |
| 3974 | if (ch >= 0x10000) { |
| 3975 | /* code first surrogate */ |
| 3976 | base64bits += 16; |
| 3977 | base64buffer = (base64buffer << 16) | 0xd800 | ((ch-0x10000) >> 10); |
| 3978 | while (base64bits >= 6) { |
| 3979 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 3980 | base64bits -= 6; |
| 3981 | } |
| 3982 | /* prepare second surrogate */ |
| 3983 | ch = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 3984 | } |
| 3985 | #endif |
| 3986 | base64bits += 16; |
| 3987 | base64buffer = (base64buffer << 16) | ch; |
| 3988 | while (base64bits >= 6) { |
| 3989 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 3990 | base64bits -= 6; |
| 3991 | } |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 3992 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3993 | if (base64bits) |
| 3994 | *out++= TO_BASE64(base64buffer << (6-base64bits) ); |
| 3995 | if (inShift) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3996 | *out++ = '-'; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3997 | if (_PyBytes_Resize(&v, out - start) < 0) |
| 3998 | return NULL; |
| 3999 | return v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4000 | } |
| 4001 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4002 | #undef IS_BASE64 |
| 4003 | #undef FROM_BASE64 |
| 4004 | #undef TO_BASE64 |
| 4005 | #undef DECODE_DIRECT |
| 4006 | #undef ENCODE_DIRECT |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4007 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4008 | /* --- UTF-8 Codec -------------------------------------------------------- */ |
| 4009 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4010 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4011 | char utf8_code_length[256] = { |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4012 | /* Map UTF-8 encoded prefix byte to sequence length. Zero means |
| 4013 | illegal prefix. See RFC 3629 for details */ |
| 4014 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 00-0F */ |
| 4015 | 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] | 4016 | 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] | 4017 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 4018 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 4019 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 4020 | 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] | 4021 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 70-7F */ |
| 4022 | 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] | 4023 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4024 | 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] | 4025 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* B0-BF */ |
| 4026 | 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* C0-C1 + C2-CF */ |
| 4027 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* D0-DF */ |
| 4028 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* E0-EF */ |
| 4029 | 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] | 4030 | }; |
| 4031 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4032 | PyObject * |
| 4033 | PyUnicode_DecodeUTF8(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4034 | Py_ssize_t size, |
| 4035 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4036 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4037 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
| 4038 | } |
| 4039 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4040 | /* Mask to check or force alignment of a pointer to C 'long' boundaries */ |
| 4041 | #define LONG_PTR_MASK (size_t) (SIZEOF_LONG - 1) |
| 4042 | |
| 4043 | /* Mask to quickly check whether a C 'long' contains a |
| 4044 | non-ASCII, UTF8-encoded char. */ |
| 4045 | #if (SIZEOF_LONG == 8) |
| 4046 | # define ASCII_CHAR_MASK 0x8080808080808080L |
| 4047 | #elif (SIZEOF_LONG == 4) |
| 4048 | # define ASCII_CHAR_MASK 0x80808080L |
| 4049 | #else |
| 4050 | # error C 'long' size should be either 4 or 8! |
| 4051 | #endif |
| 4052 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4053 | /* Scans a UTF-8 string and returns the maximum character to be expected, |
| 4054 | the size of the decoded unicode string and if any major errors were |
| 4055 | encountered. |
| 4056 | |
| 4057 | This function does check basic UTF-8 sanity, it does however NOT CHECK |
| 4058 | if the string contains surrogates, and if all continuation bytes are |
| 4059 | within the correct ranges, these checks are performed in |
| 4060 | PyUnicode_DecodeUTF8Stateful. |
| 4061 | |
| 4062 | If it sets has_errors to 1, it means the value of unicode_size and max_char |
| 4063 | will be bogus and you should not rely on useful information in them. |
| 4064 | */ |
| 4065 | static Py_UCS4 |
| 4066 | utf8_max_char_size_and_has_errors(const char *s, Py_ssize_t string_size, |
| 4067 | Py_ssize_t *unicode_size, Py_ssize_t* consumed, |
| 4068 | int *has_errors) |
| 4069 | { |
| 4070 | Py_ssize_t n; |
| 4071 | Py_ssize_t char_count = 0; |
| 4072 | Py_UCS4 max_char = 127, new_max; |
| 4073 | Py_UCS4 upper_bound; |
| 4074 | const unsigned char *p = (const unsigned char *)s; |
| 4075 | const unsigned char *end = p + string_size; |
| 4076 | const unsigned char *aligned_end = (const unsigned char *) ((size_t) end & ~LONG_PTR_MASK); |
| 4077 | int err = 0; |
| 4078 | |
| 4079 | for (; p < end && !err; ++p, ++char_count) { |
| 4080 | /* Only check value if it's not a ASCII char... */ |
| 4081 | if (*p < 0x80) { |
| 4082 | /* Fast path, see below in PyUnicode_DecodeUTF8Stateful for |
| 4083 | an explanation. */ |
| 4084 | if (!((size_t) p & LONG_PTR_MASK)) { |
| 4085 | /* Help register allocation */ |
| 4086 | register const unsigned char *_p = p; |
| 4087 | while (_p < aligned_end) { |
| 4088 | unsigned long value = *(unsigned long *) _p; |
| 4089 | if (value & ASCII_CHAR_MASK) |
| 4090 | break; |
| 4091 | _p += SIZEOF_LONG; |
| 4092 | char_count += SIZEOF_LONG; |
| 4093 | } |
| 4094 | p = _p; |
| 4095 | if (p == end) |
| 4096 | break; |
| 4097 | } |
| 4098 | } |
| 4099 | if (*p >= 0x80) { |
| 4100 | n = utf8_code_length[*p]; |
| 4101 | new_max = max_char; |
| 4102 | switch (n) { |
| 4103 | /* invalid start byte */ |
| 4104 | case 0: |
| 4105 | err = 1; |
| 4106 | break; |
| 4107 | case 2: |
| 4108 | /* Code points between 0x00FF and 0x07FF inclusive. |
| 4109 | Approximate the upper bound of the code point, |
| 4110 | if this flips over 255 we can be sure it will be more |
| 4111 | than 255 and the string will need 2 bytes per code coint, |
| 4112 | if it stays under or equal to 255, we can be sure 1 byte |
| 4113 | is enough. |
| 4114 | ((*p & 0b00011111) << 6) | 0b00111111 */ |
| 4115 | upper_bound = ((*p & 0x1F) << 6) | 0x3F; |
| 4116 | if (max_char < upper_bound) |
| 4117 | new_max = upper_bound; |
| 4118 | /* Ensure we track at least that we left ASCII space. */ |
| 4119 | if (new_max < 128) |
| 4120 | new_max = 128; |
| 4121 | break; |
| 4122 | case 3: |
| 4123 | /* Between 0x0FFF and 0xFFFF inclusive, so values are |
| 4124 | always > 255 and <= 65535 and will always need 2 bytes. */ |
| 4125 | if (max_char < 65535) |
| 4126 | new_max = 65535; |
| 4127 | break; |
| 4128 | case 4: |
| 4129 | /* Code point will be above 0xFFFF for sure in this case. */ |
| 4130 | new_max = 65537; |
| 4131 | break; |
| 4132 | /* Internal error, this should be caught by the first if */ |
| 4133 | case 1: |
| 4134 | default: |
| 4135 | assert(0 && "Impossible case in utf8_max_char_and_size"); |
| 4136 | err = 1; |
| 4137 | } |
| 4138 | /* Instead of number of overall bytes for this code point, |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 4139 | n contains the number of following bytes: */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4140 | --n; |
| 4141 | /* Check if the follow up chars are all valid continuation bytes */ |
| 4142 | if (n >= 1) { |
| 4143 | const unsigned char *cont; |
| 4144 | if ((p + n) >= end) { |
| 4145 | if (consumed == 0) |
| 4146 | /* incomplete data, non-incremental decoding */ |
| 4147 | err = 1; |
| 4148 | break; |
| 4149 | } |
| 4150 | for (cont = p + 1; cont < (p + n); ++cont) { |
| 4151 | if ((*cont & 0xc0) != 0x80) { |
| 4152 | err = 1; |
| 4153 | break; |
| 4154 | } |
| 4155 | } |
| 4156 | p += n; |
| 4157 | } |
| 4158 | else |
| 4159 | err = 1; |
| 4160 | max_char = new_max; |
| 4161 | } |
| 4162 | } |
| 4163 | |
| 4164 | if (unicode_size) |
| 4165 | *unicode_size = char_count; |
| 4166 | if (has_errors) |
| 4167 | *has_errors = err; |
| 4168 | return max_char; |
| 4169 | } |
| 4170 | |
| 4171 | /* Similar to PyUnicode_WRITE but can also write into wstr field |
| 4172 | of the legacy unicode representation */ |
| 4173 | #define WRITE_FLEXIBLE_OR_WSTR(kind, buf, index, value) \ |
| 4174 | do { \ |
| 4175 | const int k_ = (kind); \ |
| 4176 | if (k_ == PyUnicode_WCHAR_KIND) \ |
| 4177 | ((Py_UNICODE *)(buf))[(index)] = (Py_UNICODE)(value); \ |
| 4178 | else if (k_ == PyUnicode_1BYTE_KIND) \ |
| 4179 | ((unsigned char *)(buf))[(index)] = (unsigned char)(value); \ |
| 4180 | else if (k_ == PyUnicode_2BYTE_KIND) \ |
| 4181 | ((Py_UCS2 *)(buf))[(index)] = (Py_UCS2)(value); \ |
| 4182 | else \ |
| 4183 | ((Py_UCS4 *)(buf))[(index)] = (Py_UCS4)(value); \ |
| 4184 | } while (0) |
| 4185 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4186 | PyObject * |
| 4187 | PyUnicode_DecodeUTF8Stateful(const char *s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4188 | Py_ssize_t size, |
| 4189 | const char *errors, |
| 4190 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4191 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4192 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4193 | int n; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4194 | int k; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4195 | Py_ssize_t startinpos; |
| 4196 | Py_ssize_t endinpos; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4197 | const char *e, *aligned_end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4198 | PyUnicodeObject *unicode; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4199 | const char *errmsg = ""; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4200 | PyObject *errorHandler = NULL; |
| 4201 | PyObject *exc = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4202 | Py_UCS4 maxchar = 0; |
| 4203 | Py_ssize_t unicode_size; |
| 4204 | Py_ssize_t i; |
| 4205 | int kind; |
| 4206 | void *data; |
| 4207 | int has_errors; |
| 4208 | Py_UNICODE *error_outptr; |
| 4209 | #if SIZEOF_WCHAR_T == 2 |
| 4210 | Py_ssize_t wchar_offset = 0; |
| 4211 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4212 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4213 | if (size == 0) { |
| 4214 | if (consumed) |
| 4215 | *consumed = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4216 | return (PyObject *)PyUnicode_New(0, 0); |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4217 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4218 | maxchar = utf8_max_char_size_and_has_errors(s, size, &unicode_size, |
| 4219 | consumed, &has_errors); |
| 4220 | if (has_errors) { |
| 4221 | unicode = _PyUnicode_New(size); |
| 4222 | if (!unicode) |
| 4223 | return NULL; |
| 4224 | kind = PyUnicode_WCHAR_KIND; |
| 4225 | data = PyUnicode_AS_UNICODE(unicode); |
| 4226 | assert(data != NULL); |
| 4227 | } |
| 4228 | else { |
| 4229 | unicode = (PyUnicodeObject *)PyUnicode_New(unicode_size, maxchar); |
| 4230 | if (!unicode) |
| 4231 | return NULL; |
| 4232 | /* When the string is ASCII only, just use memcpy and return. |
| 4233 | unicode_size may be != size if there is an incomplete UTF-8 |
| 4234 | sequence at the end of the ASCII block. */ |
| 4235 | if (maxchar < 128 && size == unicode_size) { |
| 4236 | Py_MEMCPY(PyUnicode_1BYTE_DATA(unicode), s, unicode_size); |
| 4237 | return (PyObject *)unicode; |
| 4238 | } |
| 4239 | kind = PyUnicode_KIND(unicode); |
| 4240 | data = PyUnicode_DATA(unicode); |
| 4241 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4242 | /* Unpack UTF-8 encoded data */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4243 | i = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4244 | e = s + size; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4245 | aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4246 | |
| 4247 | while (s < e) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 4248 | Py_UCS4 ch = (unsigned char)*s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4249 | |
| 4250 | if (ch < 0x80) { |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4251 | /* Fast path for runs of ASCII characters. Given that common UTF-8 |
| 4252 | input will consist of an overwhelming majority of ASCII |
| 4253 | characters, we try to optimize for this case by checking |
| 4254 | as many characters as a C 'long' can contain. |
| 4255 | First, check if we can do an aligned read, as most CPUs have |
| 4256 | a penalty for unaligned reads. |
| 4257 | */ |
| 4258 | if (!((size_t) s & LONG_PTR_MASK)) { |
| 4259 | /* Help register allocation */ |
| 4260 | register const char *_s = s; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4261 | register Py_ssize_t _i = i; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4262 | while (_s < aligned_end) { |
| 4263 | /* Read a whole long at a time (either 4 or 8 bytes), |
| 4264 | and do a fast unrolled copy if it only contains ASCII |
| 4265 | characters. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4266 | unsigned long value = *(unsigned long *) _s; |
| 4267 | if (value & ASCII_CHAR_MASK) |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4268 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4269 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+0, _s[0]); |
| 4270 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+1, _s[1]); |
| 4271 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+2, _s[2]); |
| 4272 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+3, _s[3]); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4273 | #if (SIZEOF_LONG == 8) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4274 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+4, _s[4]); |
| 4275 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+5, _s[5]); |
| 4276 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+6, _s[6]); |
| 4277 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+7, _s[7]); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4278 | #endif |
| 4279 | _s += SIZEOF_LONG; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4280 | _i += SIZEOF_LONG; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4281 | } |
| 4282 | s = _s; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4283 | i = _i; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4284 | if (s == e) |
| 4285 | break; |
| 4286 | ch = (unsigned char)*s; |
| 4287 | } |
| 4288 | } |
| 4289 | |
| 4290 | if (ch < 0x80) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4291 | WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4292 | s++; |
| 4293 | continue; |
| 4294 | } |
| 4295 | |
| 4296 | n = utf8_code_length[ch]; |
| 4297 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4298 | if (s + n > e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4299 | if (consumed) |
| 4300 | break; |
| 4301 | else { |
| 4302 | errmsg = "unexpected end of data"; |
| 4303 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4304 | endinpos = startinpos+1; |
| 4305 | for (k=1; (k < size-startinpos) && ((s[k]&0xC0) == 0x80); k++) |
| 4306 | endinpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4307 | goto utf8Error; |
| 4308 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4309 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4310 | |
| 4311 | switch (n) { |
| 4312 | |
| 4313 | case 0: |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4314 | errmsg = "invalid start byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4315 | startinpos = s-starts; |
| 4316 | endinpos = startinpos+1; |
| 4317 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4318 | |
| 4319 | case 1: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4320 | errmsg = "internal error"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4321 | startinpos = s-starts; |
| 4322 | endinpos = startinpos+1; |
| 4323 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4324 | |
| 4325 | case 2: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4326 | if ((s[1] & 0xc0) != 0x80) { |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4327 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4328 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4329 | endinpos = startinpos + 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4330 | goto utf8Error; |
| 4331 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4332 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4333 | assert ((ch > 0x007F) && (ch <= 0x07FF)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4334 | WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4335 | break; |
| 4336 | |
| 4337 | case 3: |
Ezio Melotti | 9bf2b3a | 2010-07-03 04:52:19 +0000 | [diff] [blame] | 4338 | /* Decoding UTF-8 sequences in range \xed\xa0\x80-\xed\xbf\xbf |
| 4339 | will result in surrogates in range d800-dfff. Surrogates are |
| 4340 | not valid UTF-8 so they are rejected. |
| 4341 | See http://www.unicode.org/versions/Unicode5.2.0/ch03.pdf |
| 4342 | (table 3-7) and http://www.rfc-editor.org/rfc/rfc3629.txt */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4343 | if ((s[1] & 0xc0) != 0x80 || |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4344 | (s[2] & 0xc0) != 0x80 || |
| 4345 | ((unsigned char)s[0] == 0xE0 && |
| 4346 | (unsigned char)s[1] < 0xA0) || |
| 4347 | ((unsigned char)s[0] == 0xED && |
| 4348 | (unsigned char)s[1] > 0x9F)) { |
| 4349 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4350 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4351 | endinpos = startinpos + 1; |
| 4352 | |
| 4353 | /* if s[1] first two bits are 1 and 0, then the invalid |
| 4354 | continuation byte is s[2], so increment endinpos by 1, |
| 4355 | if not, s[1] is invalid and endinpos doesn't need to |
| 4356 | be incremented. */ |
| 4357 | if ((s[1] & 0xC0) == 0x80) |
| 4358 | endinpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4359 | goto utf8Error; |
| 4360 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4361 | ch = ((s[0] & 0x0f) << 12) + ((s[1] & 0x3f) << 6) + (s[2] & 0x3f); |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4362 | assert ((ch > 0x07FF) && (ch <= 0xFFFF)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4363 | WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, ch); |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 4364 | break; |
| 4365 | |
| 4366 | case 4: |
| 4367 | if ((s[1] & 0xc0) != 0x80 || |
| 4368 | (s[2] & 0xc0) != 0x80 || |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4369 | (s[3] & 0xc0) != 0x80 || |
| 4370 | ((unsigned char)s[0] == 0xF0 && |
| 4371 | (unsigned char)s[1] < 0x90) || |
| 4372 | ((unsigned char)s[0] == 0xF4 && |
| 4373 | (unsigned char)s[1] > 0x8F)) { |
| 4374 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4375 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4376 | endinpos = startinpos + 1; |
| 4377 | if ((s[1] & 0xC0) == 0x80) { |
| 4378 | endinpos++; |
| 4379 | if ((s[2] & 0xC0) == 0x80) |
| 4380 | endinpos++; |
| 4381 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4382 | goto utf8Error; |
| 4383 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 4384 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4385 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
| 4386 | assert ((ch > 0xFFFF) && (ch <= 0x10ffff)); |
| 4387 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4388 | /* If the string is flexible or we have native UCS-4, write |
| 4389 | directly.. */ |
| 4390 | if (sizeof(Py_UNICODE) > 2 || kind != PyUnicode_WCHAR_KIND) |
| 4391 | WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, ch); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4392 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4393 | else { |
| 4394 | /* compute and append the two surrogates: */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4395 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4396 | /* translate from 10000..10FFFF to 0..FFFF */ |
| 4397 | ch -= 0x10000; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4398 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4399 | /* high surrogate = top 10 bits added to D800 */ |
| 4400 | WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, |
| 4401 | (Py_UNICODE)(0xD800 + (ch >> 10))); |
| 4402 | |
| 4403 | /* low surrogate = bottom 10 bits added to DC00 */ |
| 4404 | WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, |
| 4405 | (Py_UNICODE)(0xDC00 + (ch & 0x03FF))); |
| 4406 | } |
| 4407 | #if SIZEOF_WCHAR_T == 2 |
| 4408 | wchar_offset++; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 4409 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4410 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4411 | } |
| 4412 | s += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4413 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4414 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4415 | utf8Error: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4416 | /* If this is not yet a resizable string, make it one.. */ |
| 4417 | if (kind != PyUnicode_WCHAR_KIND) { |
| 4418 | const Py_UNICODE *u; |
| 4419 | PyUnicodeObject *new_unicode = _PyUnicode_New(size); |
| 4420 | if (!new_unicode) |
| 4421 | goto onError; |
| 4422 | u = PyUnicode_AsUnicode((PyObject *)unicode); |
| 4423 | if (!u) |
| 4424 | goto onError; |
| 4425 | #if SIZEOF_WCHAR_T == 2 |
| 4426 | i += wchar_offset; |
| 4427 | #endif |
| 4428 | Py_UNICODE_COPY(PyUnicode_AS_UNICODE(new_unicode), u, i); |
| 4429 | Py_DECREF(unicode); |
| 4430 | unicode = new_unicode; |
| 4431 | kind = 0; |
| 4432 | data = PyUnicode_AS_UNICODE(new_unicode); |
| 4433 | assert(data != NULL); |
| 4434 | } |
| 4435 | error_outptr = PyUnicode_AS_UNICODE(unicode) + i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4436 | if (unicode_decode_call_errorhandler( |
| 4437 | errors, &errorHandler, |
| 4438 | "utf8", errmsg, |
| 4439 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4440 | &unicode, &i, &error_outptr)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4441 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4442 | /* Update data because unicode_decode_call_errorhandler might have |
| 4443 | re-created or resized the unicode object. */ |
| 4444 | data = PyUnicode_AS_UNICODE(unicode); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4445 | aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4446 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4447 | /* Ensure the unicode_size calculation above was correct: */ |
| 4448 | assert(kind == PyUnicode_WCHAR_KIND || i == unicode_size); |
| 4449 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4450 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4451 | *consumed = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4452 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4453 | /* Adjust length and ready string when it contained errors and |
| 4454 | is of the old resizable kind. */ |
| 4455 | if (kind == PyUnicode_WCHAR_KIND) { |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 4456 | if (PyUnicode_Resize((PyObject**)&unicode, i) < 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4457 | goto onError; |
| 4458 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4459 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4460 | Py_XDECREF(errorHandler); |
| 4461 | Py_XDECREF(exc); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 4462 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 4463 | if (_PyUnicode_READY_REPLACE(&unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4464 | Py_DECREF(unicode); |
| 4465 | return NULL; |
| 4466 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 4467 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 4468 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4469 | return (PyObject *)unicode; |
| 4470 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4471 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4472 | Py_XDECREF(errorHandler); |
| 4473 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4474 | Py_DECREF(unicode); |
| 4475 | return NULL; |
| 4476 | } |
| 4477 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4478 | #undef WRITE_FLEXIBLE_OR_WSTR |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4479 | |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4480 | #ifdef __APPLE__ |
| 4481 | |
| 4482 | /* Simplified UTF-8 decoder using surrogateescape error handler, |
| 4483 | used to decode the command line arguments on Mac OS X. */ |
| 4484 | |
| 4485 | wchar_t* |
| 4486 | _Py_DecodeUTF8_surrogateescape(const char *s, Py_ssize_t size) |
| 4487 | { |
| 4488 | int n; |
| 4489 | const char *e; |
| 4490 | wchar_t *unicode, *p; |
| 4491 | |
| 4492 | /* Note: size will always be longer than the resulting Unicode |
| 4493 | character count */ |
| 4494 | if (PY_SSIZE_T_MAX / sizeof(wchar_t) < (size + 1)) { |
| 4495 | PyErr_NoMemory(); |
| 4496 | return NULL; |
| 4497 | } |
| 4498 | unicode = PyMem_Malloc((size + 1) * sizeof(wchar_t)); |
| 4499 | if (!unicode) |
| 4500 | return NULL; |
| 4501 | |
| 4502 | /* Unpack UTF-8 encoded data */ |
| 4503 | p = unicode; |
| 4504 | e = s + size; |
| 4505 | while (s < e) { |
| 4506 | Py_UCS4 ch = (unsigned char)*s; |
| 4507 | |
| 4508 | if (ch < 0x80) { |
| 4509 | *p++ = (wchar_t)ch; |
| 4510 | s++; |
| 4511 | continue; |
| 4512 | } |
| 4513 | |
| 4514 | n = utf8_code_length[ch]; |
| 4515 | if (s + n > e) { |
| 4516 | goto surrogateescape; |
| 4517 | } |
| 4518 | |
| 4519 | switch (n) { |
| 4520 | case 0: |
| 4521 | case 1: |
| 4522 | goto surrogateescape; |
| 4523 | |
| 4524 | case 2: |
| 4525 | if ((s[1] & 0xc0) != 0x80) |
| 4526 | goto surrogateescape; |
| 4527 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
| 4528 | assert ((ch > 0x007F) && (ch <= 0x07FF)); |
| 4529 | *p++ = (wchar_t)ch; |
| 4530 | break; |
| 4531 | |
| 4532 | case 3: |
| 4533 | /* Decoding UTF-8 sequences in range \xed\xa0\x80-\xed\xbf\xbf |
| 4534 | will result in surrogates in range d800-dfff. Surrogates are |
| 4535 | not valid UTF-8 so they are rejected. |
| 4536 | See http://www.unicode.org/versions/Unicode5.2.0/ch03.pdf |
| 4537 | (table 3-7) and http://www.rfc-editor.org/rfc/rfc3629.txt */ |
| 4538 | if ((s[1] & 0xc0) != 0x80 || |
| 4539 | (s[2] & 0xc0) != 0x80 || |
| 4540 | ((unsigned char)s[0] == 0xE0 && |
| 4541 | (unsigned char)s[1] < 0xA0) || |
| 4542 | ((unsigned char)s[0] == 0xED && |
| 4543 | (unsigned char)s[1] > 0x9F)) { |
| 4544 | |
| 4545 | goto surrogateescape; |
| 4546 | } |
| 4547 | ch = ((s[0] & 0x0f) << 12) + ((s[1] & 0x3f) << 6) + (s[2] & 0x3f); |
| 4548 | assert ((ch > 0x07FF) && (ch <= 0xFFFF)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4549 | *p++ = (wchar_t)ch; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4550 | break; |
| 4551 | |
| 4552 | case 4: |
| 4553 | if ((s[1] & 0xc0) != 0x80 || |
| 4554 | (s[2] & 0xc0) != 0x80 || |
| 4555 | (s[3] & 0xc0) != 0x80 || |
| 4556 | ((unsigned char)s[0] == 0xF0 && |
| 4557 | (unsigned char)s[1] < 0x90) || |
| 4558 | ((unsigned char)s[0] == 0xF4 && |
| 4559 | (unsigned char)s[1] > 0x8F)) { |
| 4560 | goto surrogateescape; |
| 4561 | } |
| 4562 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
| 4563 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
| 4564 | assert ((ch > 0xFFFF) && (ch <= 0x10ffff)); |
| 4565 | |
| 4566 | #if SIZEOF_WCHAR_T == 4 |
| 4567 | *p++ = (wchar_t)ch; |
| 4568 | #else |
| 4569 | /* compute and append the two surrogates: */ |
| 4570 | |
| 4571 | /* translate from 10000..10FFFF to 0..FFFF */ |
| 4572 | ch -= 0x10000; |
| 4573 | |
| 4574 | /* high surrogate = top 10 bits added to D800 */ |
| 4575 | *p++ = (wchar_t)(0xD800 + (ch >> 10)); |
| 4576 | |
| 4577 | /* low surrogate = bottom 10 bits added to DC00 */ |
| 4578 | *p++ = (wchar_t)(0xDC00 + (ch & 0x03FF)); |
| 4579 | #endif |
| 4580 | break; |
| 4581 | } |
| 4582 | s += n; |
| 4583 | continue; |
| 4584 | |
| 4585 | surrogateescape: |
| 4586 | *p++ = 0xDC00 + ch; |
| 4587 | s++; |
| 4588 | } |
| 4589 | *p = L'\0'; |
| 4590 | return unicode; |
| 4591 | } |
| 4592 | |
| 4593 | #endif /* __APPLE__ */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4594 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4595 | /* Primary internal function which creates utf8 encoded bytes objects. |
| 4596 | |
| 4597 | Allocation strategy: if the string is short, convert into a stack buffer |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4598 | and allocate exactly as much space needed at the end. Else allocate the |
| 4599 | maximum possible needed (4 result bytes per Unicode character), and return |
| 4600 | the excess memory at the end. |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4601 | */ |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 4602 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4603 | _PyUnicode_AsUTF8String(PyObject *obj, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4604 | { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4605 | #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] | 4606 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4607 | Py_ssize_t i; /* index into s of next input byte */ |
| 4608 | PyObject *result; /* result string object */ |
| 4609 | char *p; /* next free byte in output buffer */ |
| 4610 | Py_ssize_t nallocated; /* number of result bytes allocated */ |
| 4611 | Py_ssize_t nneeded; /* number of result bytes needed */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4612 | char stackbuf[MAX_SHORT_UNICHARS * 4]; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4613 | PyObject *errorHandler = NULL; |
| 4614 | PyObject *exc = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4615 | int kind; |
| 4616 | void *data; |
| 4617 | Py_ssize_t size; |
| 4618 | PyUnicodeObject *unicode = (PyUnicodeObject *)obj; |
| 4619 | #if SIZEOF_WCHAR_T == 2 |
| 4620 | Py_ssize_t wchar_offset = 0; |
| 4621 | #endif |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 4622 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4623 | if (!PyUnicode_Check(unicode)) { |
| 4624 | PyErr_BadArgument(); |
| 4625 | return NULL; |
| 4626 | } |
| 4627 | |
| 4628 | if (PyUnicode_READY(unicode) == -1) |
| 4629 | return NULL; |
| 4630 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 4631 | if (PyUnicode_UTF8(unicode)) |
| 4632 | return PyBytes_FromStringAndSize(PyUnicode_UTF8(unicode), |
| 4633 | PyUnicode_UTF8_LENGTH(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4634 | |
| 4635 | kind = PyUnicode_KIND(unicode); |
| 4636 | data = PyUnicode_DATA(unicode); |
| 4637 | size = PyUnicode_GET_LENGTH(unicode); |
| 4638 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4639 | assert(size >= 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4640 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4641 | if (size <= MAX_SHORT_UNICHARS) { |
| 4642 | /* Write into the stack buffer; nallocated can't overflow. |
| 4643 | * At the end, we'll allocate exactly as much heap space as it |
| 4644 | * turns out we need. |
| 4645 | */ |
| 4646 | nallocated = Py_SAFE_DOWNCAST(sizeof(stackbuf), size_t, int); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4647 | result = NULL; /* will allocate after we're done */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4648 | p = stackbuf; |
| 4649 | } |
| 4650 | else { |
| 4651 | /* Overallocate on the heap, and give the excess back at the end. */ |
| 4652 | nallocated = size * 4; |
| 4653 | if (nallocated / 4 != size) /* overflow! */ |
| 4654 | return PyErr_NoMemory(); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4655 | result = PyBytes_FromStringAndSize(NULL, nallocated); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4656 | if (result == NULL) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4657 | return NULL; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4658 | p = PyBytes_AS_STRING(result); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4659 | } |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4660 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4661 | for (i = 0; i < size;) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4662 | Py_UCS4 ch = PyUnicode_READ(kind, data, i++); |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 4663 | |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4664 | if (ch < 0x80) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4665 | /* Encode ASCII */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4666 | *p++ = (char) ch; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 4667 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4668 | else if (ch < 0x0800) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4669 | /* Encode Latin-1 */ |
Marc-André Lemburg | dc724d6 | 2002-02-06 18:20:19 +0000 | [diff] [blame] | 4670 | *p++ = (char)(0xc0 | (ch >> 6)); |
| 4671 | *p++ = (char)(0x80 | (ch & 0x3f)); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4672 | } else if (0xD800 <= ch && ch <= 0xDFFF) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4673 | Py_ssize_t newpos; |
| 4674 | PyObject *rep; |
| 4675 | Py_ssize_t repsize, k, startpos; |
| 4676 | startpos = i-1; |
| 4677 | #if SIZEOF_WCHAR_T == 2 |
| 4678 | startpos += wchar_offset; |
Victor Stinner | 445a623 | 2010-04-22 20:01:57 +0000 | [diff] [blame] | 4679 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4680 | rep = unicode_encode_call_errorhandler( |
| 4681 | errors, &errorHandler, "utf-8", "surrogates not allowed", |
| 4682 | PyUnicode_AS_UNICODE(unicode), PyUnicode_GET_SIZE(unicode), |
| 4683 | &exc, startpos, startpos+1, &newpos); |
| 4684 | if (!rep) |
| 4685 | goto error; |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4686 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4687 | if (PyBytes_Check(rep)) |
| 4688 | repsize = PyBytes_GET_SIZE(rep); |
| 4689 | else |
| 4690 | repsize = PyUnicode_GET_SIZE(rep); |
| 4691 | |
| 4692 | if (repsize > 4) { |
| 4693 | Py_ssize_t offset; |
| 4694 | |
| 4695 | if (result == NULL) |
| 4696 | offset = p - stackbuf; |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4697 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4698 | offset = p - PyBytes_AS_STRING(result); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4699 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4700 | if (nallocated > PY_SSIZE_T_MAX - repsize + 4) { |
| 4701 | /* integer overflow */ |
| 4702 | PyErr_NoMemory(); |
| 4703 | goto error; |
| 4704 | } |
| 4705 | nallocated += repsize - 4; |
| 4706 | if (result != NULL) { |
| 4707 | if (_PyBytes_Resize(&result, nallocated) < 0) |
| 4708 | goto error; |
| 4709 | } else { |
| 4710 | result = PyBytes_FromStringAndSize(NULL, nallocated); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4711 | if (result == NULL) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4712 | goto error; |
| 4713 | Py_MEMCPY(PyBytes_AS_STRING(result), stackbuf, offset); |
| 4714 | } |
| 4715 | p = PyBytes_AS_STRING(result) + offset; |
| 4716 | } |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4717 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4718 | if (PyBytes_Check(rep)) { |
| 4719 | char *prep = PyBytes_AS_STRING(rep); |
| 4720 | for(k = repsize; k > 0; k--) |
| 4721 | *p++ = *prep++; |
| 4722 | } else /* rep is unicode */ { |
| 4723 | const Py_UNICODE *prep = PyUnicode_AS_UNICODE(rep); |
| 4724 | Py_UNICODE c; |
| 4725 | |
| 4726 | for(k=0; k<repsize; k++) { |
| 4727 | c = prep[k]; |
| 4728 | if (0x80 <= c) { |
| 4729 | raise_encode_exception(&exc, "utf-8", |
| 4730 | PyUnicode_AS_UNICODE(unicode), |
| 4731 | size, i-1, i, |
| 4732 | "surrogates not allowed"); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4733 | goto error; |
| 4734 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4735 | *p++ = (char)prep[k]; |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4736 | } |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4737 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4738 | Py_DECREF(rep); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4739 | } else if (ch < 0x10000) { |
| 4740 | *p++ = (char)(0xe0 | (ch >> 12)); |
| 4741 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 4742 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 4743 | } else /* ch >= 0x10000 */ { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4744 | /* Encode UCS4 Unicode ordinals */ |
| 4745 | *p++ = (char)(0xf0 | (ch >> 18)); |
| 4746 | *p++ = (char)(0x80 | ((ch >> 12) & 0x3f)); |
| 4747 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 4748 | *p++ = (char)(0x80 | (ch & 0x3f)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4749 | #if SIZEOF_WCHAR_T == 2 |
| 4750 | wchar_offset++; |
| 4751 | #endif |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4752 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4753 | } |
Tim Peters | 0eca65c | 2002-04-21 17:28:06 +0000 | [diff] [blame] | 4754 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4755 | if (result == NULL) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4756 | /* This was stack allocated. */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4757 | nneeded = p - stackbuf; |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4758 | assert(nneeded <= nallocated); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4759 | result = PyBytes_FromStringAndSize(stackbuf, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4760 | } |
| 4761 | else { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 4762 | /* Cut back to size actually needed. */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4763 | nneeded = p - PyBytes_AS_STRING(result); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4764 | assert(nneeded <= nallocated); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4765 | _PyBytes_Resize(&result, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4766 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4767 | |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4768 | Py_XDECREF(errorHandler); |
| 4769 | Py_XDECREF(exc); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4770 | return result; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4771 | error: |
| 4772 | Py_XDECREF(errorHandler); |
| 4773 | Py_XDECREF(exc); |
| 4774 | Py_XDECREF(result); |
| 4775 | return NULL; |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4776 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4777 | #undef MAX_SHORT_UNICHARS |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4778 | } |
| 4779 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4780 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4781 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
| 4782 | Py_ssize_t size, |
| 4783 | const char *errors) |
| 4784 | { |
| 4785 | PyObject *v, *unicode; |
| 4786 | |
| 4787 | unicode = PyUnicode_FromUnicode(s, size); |
| 4788 | if (unicode == NULL) |
| 4789 | return NULL; |
| 4790 | v = _PyUnicode_AsUTF8String(unicode, errors); |
| 4791 | Py_DECREF(unicode); |
| 4792 | return v; |
| 4793 | } |
| 4794 | |
| 4795 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4796 | PyUnicode_AsUTF8String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4797 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4798 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4799 | } |
| 4800 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4801 | /* --- UTF-32 Codec ------------------------------------------------------- */ |
| 4802 | |
| 4803 | PyObject * |
| 4804 | PyUnicode_DecodeUTF32(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4805 | Py_ssize_t size, |
| 4806 | const char *errors, |
| 4807 | int *byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4808 | { |
| 4809 | return PyUnicode_DecodeUTF32Stateful(s, size, errors, byteorder, NULL); |
| 4810 | } |
| 4811 | |
| 4812 | PyObject * |
| 4813 | PyUnicode_DecodeUTF32Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4814 | Py_ssize_t size, |
| 4815 | const char *errors, |
| 4816 | int *byteorder, |
| 4817 | Py_ssize_t *consumed) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4818 | { |
| 4819 | const char *starts = s; |
| 4820 | Py_ssize_t startinpos; |
| 4821 | Py_ssize_t endinpos; |
| 4822 | Py_ssize_t outpos; |
| 4823 | PyUnicodeObject *unicode; |
| 4824 | Py_UNICODE *p; |
| 4825 | #ifndef Py_UNICODE_WIDE |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 4826 | int pairs = 0; |
Mark Dickinson | 7db923c | 2010-06-12 09:10:14 +0000 | [diff] [blame] | 4827 | const unsigned char *qq; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4828 | #else |
| 4829 | const int pairs = 0; |
| 4830 | #endif |
Mark Dickinson | 7db923c | 2010-06-12 09:10:14 +0000 | [diff] [blame] | 4831 | const unsigned char *q, *e; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4832 | int bo = 0; /* assume native ordering by default */ |
| 4833 | const char *errmsg = ""; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4834 | /* Offsets from q for retrieving bytes in the right order. */ |
| 4835 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 4836 | int iorder[] = {0, 1, 2, 3}; |
| 4837 | #else |
| 4838 | int iorder[] = {3, 2, 1, 0}; |
| 4839 | #endif |
| 4840 | PyObject *errorHandler = NULL; |
| 4841 | PyObject *exc = NULL; |
Victor Stinner | 313a120 | 2010-06-11 23:56:51 +0000 | [diff] [blame] | 4842 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4843 | q = (unsigned char *)s; |
| 4844 | e = q + size; |
| 4845 | |
| 4846 | if (byteorder) |
| 4847 | bo = *byteorder; |
| 4848 | |
| 4849 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 4850 | byte order setting accordingly. In native mode, the leading BOM |
| 4851 | mark is skipped, in all other modes, it is copied to the output |
| 4852 | stream as-is (giving a ZWNBSP character). */ |
| 4853 | if (bo == 0) { |
| 4854 | if (size >= 4) { |
| 4855 | const Py_UCS4 bom = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4856 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4857 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4858 | if (bom == 0x0000FEFF) { |
| 4859 | q += 4; |
| 4860 | bo = -1; |
| 4861 | } |
| 4862 | else if (bom == 0xFFFE0000) { |
| 4863 | q += 4; |
| 4864 | bo = 1; |
| 4865 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4866 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4867 | if (bom == 0x0000FEFF) { |
| 4868 | q += 4; |
| 4869 | bo = 1; |
| 4870 | } |
| 4871 | else if (bom == 0xFFFE0000) { |
| 4872 | q += 4; |
| 4873 | bo = -1; |
| 4874 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4875 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4876 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4877 | } |
| 4878 | |
| 4879 | if (bo == -1) { |
| 4880 | /* force LE */ |
| 4881 | iorder[0] = 0; |
| 4882 | iorder[1] = 1; |
| 4883 | iorder[2] = 2; |
| 4884 | iorder[3] = 3; |
| 4885 | } |
| 4886 | else if (bo == 1) { |
| 4887 | /* force BE */ |
| 4888 | iorder[0] = 3; |
| 4889 | iorder[1] = 2; |
| 4890 | iorder[2] = 1; |
| 4891 | iorder[3] = 0; |
| 4892 | } |
| 4893 | |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 4894 | /* On narrow builds we split characters outside the BMP into two |
| 4895 | codepoints => count how much extra space we need. */ |
| 4896 | #ifndef Py_UNICODE_WIDE |
| 4897 | for (qq = q; qq < e; qq += 4) |
| 4898 | if (qq[iorder[2]] != 0 || qq[iorder[3]] != 0) |
| 4899 | pairs++; |
| 4900 | #endif |
| 4901 | |
| 4902 | /* This might be one to much, because of a BOM */ |
| 4903 | unicode = _PyUnicode_New((size+3)/4+pairs); |
| 4904 | if (!unicode) |
| 4905 | return NULL; |
| 4906 | if (size == 0) |
| 4907 | return (PyObject *)unicode; |
| 4908 | |
| 4909 | /* Unpack UTF-32 encoded data */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4910 | p = PyUnicode_AS_UNICODE(unicode); |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 4911 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4912 | while (q < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4913 | Py_UCS4 ch; |
| 4914 | /* remaining bytes at the end? (size should be divisible by 4) */ |
| 4915 | if (e-q<4) { |
| 4916 | if (consumed) |
| 4917 | break; |
| 4918 | errmsg = "truncated data"; |
| 4919 | startinpos = ((const char *)q)-starts; |
| 4920 | endinpos = ((const char *)e)-starts; |
| 4921 | goto utf32Error; |
| 4922 | /* The remaining input chars are ignored if the callback |
| 4923 | chooses to skip the input */ |
| 4924 | } |
| 4925 | ch = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
| 4926 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4927 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4928 | if (ch >= 0x110000) |
| 4929 | { |
| 4930 | errmsg = "codepoint not in range(0x110000)"; |
| 4931 | startinpos = ((const char *)q)-starts; |
| 4932 | endinpos = startinpos+4; |
| 4933 | goto utf32Error; |
| 4934 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4935 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4936 | if (ch >= 0x10000) |
| 4937 | { |
| 4938 | *p++ = 0xD800 | ((ch-0x10000) >> 10); |
| 4939 | *p++ = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 4940 | } |
| 4941 | else |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4942 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4943 | *p++ = ch; |
| 4944 | q += 4; |
| 4945 | continue; |
| 4946 | utf32Error: |
| 4947 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 4948 | if (unicode_decode_call_errorhandler( |
| 4949 | errors, &errorHandler, |
| 4950 | "utf32", errmsg, |
| 4951 | &starts, (const char **)&e, &startinpos, &endinpos, &exc, (const char **)&q, |
| 4952 | &unicode, &outpos, &p)) |
| 4953 | goto onError; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4954 | } |
| 4955 | |
| 4956 | if (byteorder) |
| 4957 | *byteorder = bo; |
| 4958 | |
| 4959 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4960 | *consumed = (const char *)q-starts; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4961 | |
| 4962 | /* Adjust length */ |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 4963 | if (PyUnicode_Resize((PyObject**)&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4964 | goto onError; |
| 4965 | |
| 4966 | Py_XDECREF(errorHandler); |
| 4967 | Py_XDECREF(exc); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 4968 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 4969 | if (_PyUnicode_READY_REPLACE(&unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4970 | Py_DECREF(unicode); |
| 4971 | return NULL; |
| 4972 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 4973 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 4974 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4975 | return (PyObject *)unicode; |
| 4976 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4977 | onError: |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4978 | Py_DECREF(unicode); |
| 4979 | Py_XDECREF(errorHandler); |
| 4980 | Py_XDECREF(exc); |
| 4981 | return NULL; |
| 4982 | } |
| 4983 | |
| 4984 | PyObject * |
| 4985 | PyUnicode_EncodeUTF32(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4986 | Py_ssize_t size, |
| 4987 | const char *errors, |
| 4988 | int byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4989 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4990 | PyObject *v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4991 | unsigned char *p; |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4992 | Py_ssize_t nsize, bytesize; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4993 | #ifndef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4994 | Py_ssize_t i, pairs; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4995 | #else |
| 4996 | const int pairs = 0; |
| 4997 | #endif |
| 4998 | /* Offsets from p for storing byte pairs in the right order. */ |
| 4999 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5000 | int iorder[] = {0, 1, 2, 3}; |
| 5001 | #else |
| 5002 | int iorder[] = {3, 2, 1, 0}; |
| 5003 | #endif |
| 5004 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5005 | #define STORECHAR(CH) \ |
| 5006 | do { \ |
| 5007 | p[iorder[3]] = ((CH) >> 24) & 0xff; \ |
| 5008 | p[iorder[2]] = ((CH) >> 16) & 0xff; \ |
| 5009 | p[iorder[1]] = ((CH) >> 8) & 0xff; \ |
| 5010 | p[iorder[0]] = (CH) & 0xff; \ |
| 5011 | p += 4; \ |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5012 | } while(0) |
| 5013 | |
| 5014 | /* In narrow builds we can output surrogate pairs as one codepoint, |
| 5015 | so we need less space. */ |
| 5016 | #ifndef Py_UNICODE_WIDE |
| 5017 | for (i = pairs = 0; i < size-1; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5018 | if (0xD800 <= s[i] && s[i] <= 0xDBFF && |
| 5019 | 0xDC00 <= s[i+1] && s[i+1] <= 0xDFFF) |
| 5020 | pairs++; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5021 | #endif |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5022 | nsize = (size - pairs + (byteorder == 0)); |
| 5023 | bytesize = nsize * 4; |
| 5024 | if (bytesize / 4 != nsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5025 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5026 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5027 | if (v == NULL) |
| 5028 | return NULL; |
| 5029 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5030 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5031 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5032 | STORECHAR(0xFEFF); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5033 | if (size == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5034 | goto done; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5035 | |
| 5036 | if (byteorder == -1) { |
| 5037 | /* force LE */ |
| 5038 | iorder[0] = 0; |
| 5039 | iorder[1] = 1; |
| 5040 | iorder[2] = 2; |
| 5041 | iorder[3] = 3; |
| 5042 | } |
| 5043 | else if (byteorder == 1) { |
| 5044 | /* force BE */ |
| 5045 | iorder[0] = 3; |
| 5046 | iorder[1] = 2; |
| 5047 | iorder[2] = 1; |
| 5048 | iorder[3] = 0; |
| 5049 | } |
| 5050 | |
| 5051 | while (size-- > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5052 | Py_UCS4 ch = *s++; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5053 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5054 | if (0xD800 <= ch && ch <= 0xDBFF && size > 0) { |
| 5055 | Py_UCS4 ch2 = *s; |
| 5056 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
| 5057 | ch = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
| 5058 | s++; |
| 5059 | size--; |
| 5060 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5061 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5062 | #endif |
| 5063 | STORECHAR(ch); |
| 5064 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5065 | |
| 5066 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5067 | return v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5068 | #undef STORECHAR |
| 5069 | } |
| 5070 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5071 | PyObject * |
| 5072 | PyUnicode_AsUTF32String(PyObject *unicode) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5073 | { |
| 5074 | if (!PyUnicode_Check(unicode)) { |
| 5075 | PyErr_BadArgument(); |
| 5076 | return NULL; |
| 5077 | } |
| 5078 | return PyUnicode_EncodeUTF32(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5079 | PyUnicode_GET_SIZE(unicode), |
| 5080 | NULL, |
| 5081 | 0); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5082 | } |
| 5083 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5084 | /* --- UTF-16 Codec ------------------------------------------------------- */ |
| 5085 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5086 | PyObject * |
| 5087 | PyUnicode_DecodeUTF16(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5088 | Py_ssize_t size, |
| 5089 | const char *errors, |
| 5090 | int *byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5091 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5092 | return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL); |
| 5093 | } |
| 5094 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5095 | /* Two masks for fast checking of whether a C 'long' may contain |
| 5096 | UTF16-encoded surrogate characters. This is an efficient heuristic, |
| 5097 | assuming that non-surrogate characters with a code point >= 0x8000 are |
| 5098 | rare in most input. |
| 5099 | FAST_CHAR_MASK is used when the input is in native byte ordering, |
| 5100 | SWAPPED_FAST_CHAR_MASK when the input is in byteswapped ordering. |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5101 | */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5102 | #if (SIZEOF_LONG == 8) |
| 5103 | # define FAST_CHAR_MASK 0x8000800080008000L |
| 5104 | # define SWAPPED_FAST_CHAR_MASK 0x0080008000800080L |
| 5105 | #elif (SIZEOF_LONG == 4) |
| 5106 | # define FAST_CHAR_MASK 0x80008000L |
| 5107 | # define SWAPPED_FAST_CHAR_MASK 0x00800080L |
| 5108 | #else |
| 5109 | # error C 'long' size should be either 4 or 8! |
| 5110 | #endif |
| 5111 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5112 | PyObject * |
| 5113 | PyUnicode_DecodeUTF16Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5114 | Py_ssize_t size, |
| 5115 | const char *errors, |
| 5116 | int *byteorder, |
| 5117 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5118 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5119 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5120 | Py_ssize_t startinpos; |
| 5121 | Py_ssize_t endinpos; |
| 5122 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5123 | PyUnicodeObject *unicode; |
| 5124 | Py_UNICODE *p; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5125 | const unsigned char *q, *e, *aligned_end; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5126 | int bo = 0; /* assume native ordering by default */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5127 | int native_ordering = 0; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 5128 | const char *errmsg = ""; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5129 | /* Offsets from q for retrieving byte pairs in the right order. */ |
| 5130 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5131 | int ihi = 1, ilo = 0; |
| 5132 | #else |
| 5133 | int ihi = 0, ilo = 1; |
| 5134 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5135 | PyObject *errorHandler = NULL; |
| 5136 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5137 | |
| 5138 | /* Note: size will always be longer than the resulting Unicode |
| 5139 | character count */ |
| 5140 | unicode = _PyUnicode_New(size); |
| 5141 | if (!unicode) |
| 5142 | return NULL; |
| 5143 | if (size == 0) |
| 5144 | return (PyObject *)unicode; |
| 5145 | |
| 5146 | /* Unpack UTF-16 encoded data */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5147 | p = PyUnicode_AS_UNICODE(unicode); |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5148 | q = (unsigned char *)s; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5149 | e = q + size - 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5150 | |
| 5151 | if (byteorder) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5152 | bo = *byteorder; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5153 | |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5154 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 5155 | byte order setting accordingly. In native mode, the leading BOM |
| 5156 | mark is skipped, in all other modes, it is copied to the output |
| 5157 | stream as-is (giving a ZWNBSP character). */ |
| 5158 | if (bo == 0) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5159 | if (size >= 2) { |
| 5160 | const Py_UNICODE bom = (q[ihi] << 8) | q[ilo]; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5161 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5162 | if (bom == 0xFEFF) { |
| 5163 | q += 2; |
| 5164 | bo = -1; |
| 5165 | } |
| 5166 | else if (bom == 0xFFFE) { |
| 5167 | q += 2; |
| 5168 | bo = 1; |
| 5169 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5170 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5171 | if (bom == 0xFEFF) { |
| 5172 | q += 2; |
| 5173 | bo = 1; |
| 5174 | } |
| 5175 | else if (bom == 0xFFFE) { |
| 5176 | q += 2; |
| 5177 | bo = -1; |
| 5178 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5179 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5180 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5181 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5182 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5183 | if (bo == -1) { |
| 5184 | /* force LE */ |
| 5185 | ihi = 1; |
| 5186 | ilo = 0; |
| 5187 | } |
| 5188 | else if (bo == 1) { |
| 5189 | /* force BE */ |
| 5190 | ihi = 0; |
| 5191 | ilo = 1; |
| 5192 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5193 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5194 | native_ordering = ilo < ihi; |
| 5195 | #else |
| 5196 | native_ordering = ilo > ihi; |
| 5197 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5198 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5199 | aligned_end = (const unsigned char *) ((size_t) e & ~LONG_PTR_MASK); |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5200 | while (q < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5201 | Py_UNICODE ch; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5202 | /* First check for possible aligned read of a C 'long'. Unaligned |
| 5203 | reads are more expensive, better to defer to another iteration. */ |
| 5204 | if (!((size_t) q & LONG_PTR_MASK)) { |
| 5205 | /* Fast path for runs of non-surrogate chars. */ |
| 5206 | register const unsigned char *_q = q; |
| 5207 | Py_UNICODE *_p = p; |
| 5208 | if (native_ordering) { |
| 5209 | /* Native ordering is simple: as long as the input cannot |
| 5210 | possibly contain a surrogate char, do an unrolled copy |
| 5211 | of several 16-bit code points to the target object. |
| 5212 | The non-surrogate check is done on several input bytes |
| 5213 | at a time (as many as a C 'long' can contain). */ |
| 5214 | while (_q < aligned_end) { |
| 5215 | unsigned long data = * (unsigned long *) _q; |
| 5216 | if (data & FAST_CHAR_MASK) |
| 5217 | break; |
| 5218 | _p[0] = ((unsigned short *) _q)[0]; |
| 5219 | _p[1] = ((unsigned short *) _q)[1]; |
| 5220 | #if (SIZEOF_LONG == 8) |
| 5221 | _p[2] = ((unsigned short *) _q)[2]; |
| 5222 | _p[3] = ((unsigned short *) _q)[3]; |
| 5223 | #endif |
| 5224 | _q += SIZEOF_LONG; |
| 5225 | _p += SIZEOF_LONG / 2; |
| 5226 | } |
| 5227 | } |
| 5228 | else { |
| 5229 | /* Byteswapped ordering is similar, but we must decompose |
| 5230 | the copy bytewise, and take care of zero'ing out the |
| 5231 | upper bytes if the target object is in 32-bit units |
| 5232 | (that is, in UCS-4 builds). */ |
| 5233 | while (_q < aligned_end) { |
| 5234 | unsigned long data = * (unsigned long *) _q; |
| 5235 | if (data & SWAPPED_FAST_CHAR_MASK) |
| 5236 | break; |
| 5237 | /* Zero upper bytes in UCS-4 builds */ |
| 5238 | #if (Py_UNICODE_SIZE > 2) |
| 5239 | _p[0] = 0; |
| 5240 | _p[1] = 0; |
| 5241 | #if (SIZEOF_LONG == 8) |
| 5242 | _p[2] = 0; |
| 5243 | _p[3] = 0; |
| 5244 | #endif |
| 5245 | #endif |
Antoine Pitrou | d6e8de1 | 2009-01-11 23:56:55 +0000 | [diff] [blame] | 5246 | /* Issue #4916; UCS-4 builds on big endian machines must |
| 5247 | fill the two last bytes of each 4-byte unit. */ |
| 5248 | #if (!defined(BYTEORDER_IS_LITTLE_ENDIAN) && Py_UNICODE_SIZE > 2) |
| 5249 | # define OFF 2 |
| 5250 | #else |
| 5251 | # define OFF 0 |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5252 | #endif |
Antoine Pitrou | d6e8de1 | 2009-01-11 23:56:55 +0000 | [diff] [blame] | 5253 | ((unsigned char *) _p)[OFF + 1] = _q[0]; |
| 5254 | ((unsigned char *) _p)[OFF + 0] = _q[1]; |
| 5255 | ((unsigned char *) _p)[OFF + 1 + Py_UNICODE_SIZE] = _q[2]; |
| 5256 | ((unsigned char *) _p)[OFF + 0 + Py_UNICODE_SIZE] = _q[3]; |
| 5257 | #if (SIZEOF_LONG == 8) |
| 5258 | ((unsigned char *) _p)[OFF + 1 + 2 * Py_UNICODE_SIZE] = _q[4]; |
| 5259 | ((unsigned char *) _p)[OFF + 0 + 2 * Py_UNICODE_SIZE] = _q[5]; |
| 5260 | ((unsigned char *) _p)[OFF + 1 + 3 * Py_UNICODE_SIZE] = _q[6]; |
| 5261 | ((unsigned char *) _p)[OFF + 0 + 3 * Py_UNICODE_SIZE] = _q[7]; |
| 5262 | #endif |
| 5263 | #undef OFF |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5264 | _q += SIZEOF_LONG; |
| 5265 | _p += SIZEOF_LONG / 2; |
| 5266 | } |
| 5267 | } |
| 5268 | p = _p; |
| 5269 | q = _q; |
| 5270 | if (q >= e) |
| 5271 | break; |
| 5272 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5273 | ch = (q[ihi] << 8) | q[ilo]; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5274 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5275 | q += 2; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5276 | |
| 5277 | if (ch < 0xD800 || ch > 0xDFFF) { |
| 5278 | *p++ = ch; |
| 5279 | continue; |
| 5280 | } |
| 5281 | |
| 5282 | /* UTF-16 code pair: */ |
| 5283 | if (q > e) { |
| 5284 | errmsg = "unexpected end of data"; |
| 5285 | startinpos = (((const char *)q) - 2) - starts; |
| 5286 | endinpos = ((const char *)e) + 1 - starts; |
| 5287 | goto utf16Error; |
| 5288 | } |
| 5289 | if (0xD800 <= ch && ch <= 0xDBFF) { |
| 5290 | Py_UNICODE ch2 = (q[ihi] << 8) | q[ilo]; |
| 5291 | q += 2; |
| 5292 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 5293 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5294 | *p++ = ch; |
| 5295 | *p++ = ch2; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5296 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5297 | *p++ = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5298 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5299 | continue; |
| 5300 | } |
| 5301 | else { |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5302 | errmsg = "illegal UTF-16 surrogate"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5303 | startinpos = (((const char *)q)-4)-starts; |
| 5304 | endinpos = startinpos+2; |
| 5305 | goto utf16Error; |
| 5306 | } |
| 5307 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5308 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5309 | errmsg = "illegal encoding"; |
| 5310 | startinpos = (((const char *)q)-2)-starts; |
| 5311 | endinpos = startinpos+2; |
| 5312 | /* Fall through to report the error */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5313 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5314 | utf16Error: |
| 5315 | outpos = p - PyUnicode_AS_UNICODE(unicode); |
| 5316 | if (unicode_decode_call_errorhandler( |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5317 | errors, |
| 5318 | &errorHandler, |
| 5319 | "utf16", errmsg, |
| 5320 | &starts, |
| 5321 | (const char **)&e, |
| 5322 | &startinpos, |
| 5323 | &endinpos, |
| 5324 | &exc, |
| 5325 | (const char **)&q, |
| 5326 | &unicode, |
| 5327 | &outpos, |
| 5328 | &p)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5329 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5330 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5331 | /* remaining byte at the end? (size should be even) */ |
| 5332 | if (e == q) { |
| 5333 | if (!consumed) { |
| 5334 | errmsg = "truncated data"; |
| 5335 | startinpos = ((const char *)q) - starts; |
| 5336 | endinpos = ((const char *)e) + 1 - starts; |
| 5337 | outpos = p - PyUnicode_AS_UNICODE(unicode); |
| 5338 | if (unicode_decode_call_errorhandler( |
| 5339 | errors, |
| 5340 | &errorHandler, |
| 5341 | "utf16", errmsg, |
| 5342 | &starts, |
| 5343 | (const char **)&e, |
| 5344 | &startinpos, |
| 5345 | &endinpos, |
| 5346 | &exc, |
| 5347 | (const char **)&q, |
| 5348 | &unicode, |
| 5349 | &outpos, |
| 5350 | &p)) |
| 5351 | goto onError; |
| 5352 | /* The remaining input chars are ignored if the callback |
| 5353 | chooses to skip the input */ |
| 5354 | } |
| 5355 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5356 | |
| 5357 | if (byteorder) |
| 5358 | *byteorder = bo; |
| 5359 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5360 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5361 | *consumed = (const char *)q-starts; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5362 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5363 | /* Adjust length */ |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 5364 | if (PyUnicode_Resize((PyObject**)&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5365 | goto onError; |
| 5366 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5367 | Py_XDECREF(errorHandler); |
| 5368 | Py_XDECREF(exc); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 5369 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 5370 | if (_PyUnicode_READY_REPLACE(&unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5371 | Py_DECREF(unicode); |
| 5372 | return NULL; |
| 5373 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 5374 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 5375 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5376 | return (PyObject *)unicode; |
| 5377 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5378 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5379 | Py_DECREF(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5380 | Py_XDECREF(errorHandler); |
| 5381 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5382 | return NULL; |
| 5383 | } |
| 5384 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5385 | #undef FAST_CHAR_MASK |
| 5386 | #undef SWAPPED_FAST_CHAR_MASK |
| 5387 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5388 | PyObject * |
| 5389 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5390 | Py_ssize_t size, |
| 5391 | const char *errors, |
| 5392 | int byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5393 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5394 | PyObject *v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5395 | unsigned char *p; |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5396 | Py_ssize_t nsize, bytesize; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 5397 | #ifdef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5398 | Py_ssize_t i, pairs; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 5399 | #else |
| 5400 | const int pairs = 0; |
| 5401 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5402 | /* Offsets from p for storing byte pairs in the right order. */ |
| 5403 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5404 | int ihi = 1, ilo = 0; |
| 5405 | #else |
| 5406 | int ihi = 0, ilo = 1; |
| 5407 | #endif |
| 5408 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5409 | #define STORECHAR(CH) \ |
| 5410 | do { \ |
| 5411 | p[ihi] = ((CH) >> 8) & 0xff; \ |
| 5412 | p[ilo] = (CH) & 0xff; \ |
| 5413 | p += 2; \ |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5414 | } while(0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5415 | |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 5416 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5417 | for (i = pairs = 0; i < size; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5418 | if (s[i] >= 0x10000) |
| 5419 | pairs++; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 5420 | #endif |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5421 | /* 2 * (size + pairs + (byteorder == 0)) */ |
| 5422 | if (size > PY_SSIZE_T_MAX || |
| 5423 | size > PY_SSIZE_T_MAX - pairs - (byteorder == 0)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5424 | return PyErr_NoMemory(); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5425 | nsize = size + pairs + (byteorder == 0); |
| 5426 | bytesize = nsize * 2; |
| 5427 | if (bytesize / 2 != nsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5428 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5429 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5430 | if (v == NULL) |
| 5431 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5432 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5433 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5434 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5435 | STORECHAR(0xFEFF); |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 5436 | if (size == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5437 | goto done; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5438 | |
| 5439 | if (byteorder == -1) { |
| 5440 | /* force LE */ |
| 5441 | ihi = 1; |
| 5442 | ilo = 0; |
| 5443 | } |
| 5444 | else if (byteorder == 1) { |
| 5445 | /* force BE */ |
| 5446 | ihi = 0; |
| 5447 | ilo = 1; |
| 5448 | } |
| 5449 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5450 | while (size-- > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5451 | Py_UNICODE ch = *s++; |
| 5452 | Py_UNICODE ch2 = 0; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 5453 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5454 | if (ch >= 0x10000) { |
| 5455 | ch2 = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 5456 | ch = 0xD800 | ((ch-0x10000) >> 10); |
| 5457 | } |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 5458 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5459 | STORECHAR(ch); |
| 5460 | if (ch2) |
| 5461 | STORECHAR(ch2); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5462 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5463 | |
| 5464 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5465 | return v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5466 | #undef STORECHAR |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5467 | } |
| 5468 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5469 | PyObject * |
| 5470 | PyUnicode_AsUTF16String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5471 | { |
| 5472 | if (!PyUnicode_Check(unicode)) { |
| 5473 | PyErr_BadArgument(); |
| 5474 | return NULL; |
| 5475 | } |
| 5476 | return PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5477 | PyUnicode_GET_SIZE(unicode), |
| 5478 | NULL, |
| 5479 | 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5480 | } |
| 5481 | |
| 5482 | /* --- Unicode Escape Codec ----------------------------------------------- */ |
| 5483 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5484 | /* Helper function for PyUnicode_DecodeUnicodeEscape, determines |
| 5485 | if all the escapes in the string make it still a valid ASCII string. |
| 5486 | Returns -1 if any escapes were found which cause the string to |
| 5487 | pop out of ASCII range. Otherwise returns the length of the |
| 5488 | required buffer to hold the string. |
| 5489 | */ |
| 5490 | Py_ssize_t |
| 5491 | length_of_escaped_ascii_string(const char *s, Py_ssize_t size) |
| 5492 | { |
| 5493 | const unsigned char *p = (const unsigned char *)s; |
| 5494 | const unsigned char *end = p + size; |
| 5495 | Py_ssize_t length = 0; |
| 5496 | |
| 5497 | if (size < 0) |
| 5498 | return -1; |
| 5499 | |
| 5500 | for (; p < end; ++p) { |
| 5501 | if (*p > 127) { |
| 5502 | /* Non-ASCII */ |
| 5503 | return -1; |
| 5504 | } |
| 5505 | else if (*p != '\\') { |
| 5506 | /* Normal character */ |
| 5507 | ++length; |
| 5508 | } |
| 5509 | else { |
| 5510 | /* Backslash-escape, check next char */ |
| 5511 | ++p; |
| 5512 | /* Escape sequence reaches till end of string or |
| 5513 | non-ASCII follow-up. */ |
| 5514 | if (p >= end || *p > 127) |
| 5515 | return -1; |
| 5516 | switch (*p) { |
| 5517 | case '\n': |
| 5518 | /* backslash + \n result in zero characters */ |
| 5519 | break; |
| 5520 | case '\\': case '\'': case '\"': |
| 5521 | case 'b': case 'f': case 't': |
| 5522 | case 'n': case 'r': case 'v': case 'a': |
| 5523 | ++length; |
| 5524 | break; |
| 5525 | case '0': case '1': case '2': case '3': |
| 5526 | case '4': case '5': case '6': case '7': |
| 5527 | case 'x': case 'u': case 'U': case 'N': |
| 5528 | /* these do not guarantee ASCII characters */ |
| 5529 | return -1; |
| 5530 | default: |
| 5531 | /* count the backslash + the other character */ |
| 5532 | length += 2; |
| 5533 | } |
| 5534 | } |
| 5535 | } |
| 5536 | return length; |
| 5537 | } |
| 5538 | |
| 5539 | /* Similar to PyUnicode_WRITE but either write into wstr field |
| 5540 | or treat string as ASCII. */ |
| 5541 | #define WRITE_ASCII_OR_WSTR(kind, buf, index, value) \ |
| 5542 | do { \ |
| 5543 | if ((kind) != PyUnicode_WCHAR_KIND) \ |
| 5544 | ((unsigned char *)(buf))[(index)] = (unsigned char)(value); \ |
| 5545 | else \ |
| 5546 | ((Py_UNICODE *)(buf))[(index)] = (Py_UNICODE)(value); \ |
| 5547 | } while (0) |
| 5548 | |
| 5549 | #define WRITE_WSTR(buf, index, value) \ |
| 5550 | assert(kind == PyUnicode_WCHAR_KIND), \ |
| 5551 | ((Py_UNICODE *)(buf))[(index)] = (Py_UNICODE)(value) |
| 5552 | |
| 5553 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 5554 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; |
Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 5555 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5556 | PyObject * |
| 5557 | PyUnicode_DecodeUnicodeEscape(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5558 | Py_ssize_t size, |
Victor Stinner | c17f540 | 2011-09-29 00:16:58 +0200 | [diff] [blame] | 5559 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5560 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5561 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5562 | Py_ssize_t startinpos; |
| 5563 | Py_ssize_t endinpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5564 | int j; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5565 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5566 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5567 | const char *end; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5568 | char* message; |
| 5569 | Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5570 | PyObject *errorHandler = NULL; |
| 5571 | PyObject *exc = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5572 | Py_ssize_t ascii_length; |
| 5573 | Py_ssize_t i; |
| 5574 | int kind; |
| 5575 | void *data; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5576 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5577 | ascii_length = length_of_escaped_ascii_string(s, size); |
| 5578 | |
| 5579 | /* After length_of_escaped_ascii_string() there are two alternatives, |
| 5580 | either the string is pure ASCII with named escapes like \n, etc. |
| 5581 | and we determined it's exact size (common case) |
| 5582 | or it contains \x, \u, ... escape sequences. then we create a |
| 5583 | legacy wchar string and resize it at the end of this function. */ |
| 5584 | if (ascii_length >= 0) { |
| 5585 | v = (PyUnicodeObject *)PyUnicode_New(ascii_length, 127); |
| 5586 | if (!v) |
| 5587 | goto onError; |
| 5588 | assert(PyUnicode_KIND(v) == PyUnicode_1BYTE_KIND); |
| 5589 | kind = PyUnicode_1BYTE_KIND; |
| 5590 | data = PyUnicode_DATA(v); |
| 5591 | } |
| 5592 | else { |
| 5593 | /* Escaped strings will always be longer than the resulting |
| 5594 | Unicode string, so we start with size here and then reduce the |
| 5595 | length after conversion to the true value. |
| 5596 | (but if the error callback returns a long replacement string |
| 5597 | we'll have to allocate more space) */ |
| 5598 | v = _PyUnicode_New(size); |
| 5599 | if (!v) |
| 5600 | goto onError; |
| 5601 | kind = PyUnicode_WCHAR_KIND; |
| 5602 | data = PyUnicode_AS_UNICODE(v); |
| 5603 | } |
| 5604 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5605 | if (size == 0) |
| 5606 | return (PyObject *)v; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5607 | i = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5608 | end = s + size; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5609 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5610 | while (s < end) { |
| 5611 | unsigned char c; |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 5612 | Py_UNICODE x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5613 | int digits; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5614 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5615 | if (kind == PyUnicode_WCHAR_KIND) { |
| 5616 | assert(i < _PyUnicode_WSTR_LENGTH(v)); |
| 5617 | } |
| 5618 | else { |
| 5619 | /* The only case in which i == ascii_length is a backslash |
| 5620 | followed by a newline. */ |
| 5621 | assert(i <= ascii_length); |
| 5622 | } |
| 5623 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5624 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 5625 | if (*s != '\\') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5626 | WRITE_ASCII_OR_WSTR(kind, data, i++, (unsigned char) *s++); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5627 | continue; |
| 5628 | } |
| 5629 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5630 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5631 | /* \ - Escapes */ |
| 5632 | s++; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5633 | c = *s++; |
| 5634 | if (s > end) |
| 5635 | c = '\0'; /* Invalid after \ */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5636 | |
| 5637 | if (kind == PyUnicode_WCHAR_KIND) { |
| 5638 | assert(i < _PyUnicode_WSTR_LENGTH(v)); |
| 5639 | } |
| 5640 | else { |
| 5641 | /* The only case in which i == ascii_length is a backslash |
| 5642 | followed by a newline. */ |
| 5643 | assert(i < ascii_length || (i == ascii_length && c == '\n')); |
| 5644 | } |
| 5645 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5646 | switch (c) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5647 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5648 | /* \x escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5649 | case '\n': break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5650 | case '\\': WRITE_ASCII_OR_WSTR(kind, data, i++, '\\'); break; |
| 5651 | case '\'': WRITE_ASCII_OR_WSTR(kind, data, i++, '\''); break; |
| 5652 | case '\"': WRITE_ASCII_OR_WSTR(kind, data, i++, '\"'); break; |
| 5653 | case 'b': WRITE_ASCII_OR_WSTR(kind, data, i++, '\b'); break; |
| 5654 | /* FF */ |
| 5655 | case 'f': WRITE_ASCII_OR_WSTR(kind, data, i++, '\014'); break; |
| 5656 | case 't': WRITE_ASCII_OR_WSTR(kind, data, i++, '\t'); break; |
| 5657 | case 'n': WRITE_ASCII_OR_WSTR(kind, data, i++, '\n'); break; |
| 5658 | case 'r': WRITE_ASCII_OR_WSTR(kind, data, i++, '\r'); break; |
| 5659 | /* VT */ |
| 5660 | case 'v': WRITE_ASCII_OR_WSTR(kind, data, i++, '\013'); break; |
| 5661 | /* BEL, not classic C */ |
| 5662 | case 'a': WRITE_ASCII_OR_WSTR(kind, data, i++, '\007'); break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5663 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5664 | /* \OOO (octal) escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5665 | case '0': case '1': case '2': case '3': |
| 5666 | case '4': case '5': case '6': case '7': |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5667 | x = s[-1] - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5668 | if (s < end && '0' <= *s && *s <= '7') { |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5669 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5670 | if (s < end && '0' <= *s && *s <= '7') |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5671 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5672 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5673 | WRITE_WSTR(data, i++, x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5674 | break; |
| 5675 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5676 | /* hex escapes */ |
| 5677 | /* \xXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5678 | case 'x': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5679 | digits = 2; |
| 5680 | message = "truncated \\xXX escape"; |
| 5681 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5682 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5683 | /* \uXXXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5684 | case 'u': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5685 | digits = 4; |
| 5686 | message = "truncated \\uXXXX escape"; |
| 5687 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5688 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5689 | /* \UXXXXXXXX */ |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5690 | case 'U': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5691 | digits = 8; |
| 5692 | message = "truncated \\UXXXXXXXX escape"; |
| 5693 | hexescape: |
| 5694 | chr = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5695 | p = PyUnicode_AS_UNICODE(v) + i; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5696 | if (s+digits>end) { |
| 5697 | endinpos = size; |
| 5698 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5699 | errors, &errorHandler, |
| 5700 | "unicodeescape", "end of string in escape sequence", |
| 5701 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5702 | &v, &i, &p)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5703 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5704 | data = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5705 | goto nextByte; |
| 5706 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5707 | for (j = 0; j < digits; ++j) { |
| 5708 | c = (unsigned char) s[j]; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 5709 | if (!Py_ISXDIGIT(c)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5710 | endinpos = (s+j+1)-starts; |
| 5711 | p = PyUnicode_AS_UNICODE(v) + i; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5712 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5713 | errors, &errorHandler, |
| 5714 | "unicodeescape", message, |
| 5715 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5716 | &v, &i, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5717 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5718 | data = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5719 | goto nextByte; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5720 | } |
| 5721 | chr = (chr<<4) & ~0xF; |
| 5722 | if (c >= '0' && c <= '9') |
| 5723 | chr += c - '0'; |
| 5724 | else if (c >= 'a' && c <= 'f') |
| 5725 | chr += 10 + c - 'a'; |
| 5726 | else |
| 5727 | chr += 10 + c - 'A'; |
| 5728 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5729 | s += j; |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 5730 | if (chr == 0xffffffff && PyErr_Occurred()) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5731 | /* _decoding_error will have already written into the |
| 5732 | target buffer. */ |
| 5733 | break; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5734 | store: |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5735 | /* when we get here, chr is a 32-bit unicode character */ |
| 5736 | if (chr <= 0xffff) |
| 5737 | /* UCS-2 character */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5738 | WRITE_WSTR(data, i++, chr); |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5739 | else if (chr <= 0x10ffff) { |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 5740 | /* UCS-4 character. Either store directly, or as |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5741 | surrogate pair. */ |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 5742 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5743 | WRITE_WSTR(data, i++, chr); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5744 | #else |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5745 | chr -= 0x10000L; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5746 | WRITE_WSTR(data, i++, 0xD800 + (Py_UNICODE) (chr >> 10)); |
| 5747 | WRITE_WSTR(data, i++, 0xDC00 + (Py_UNICODE) (chr & 0x03FF)); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5748 | #endif |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5749 | } else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5750 | endinpos = s-starts; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5751 | p = PyUnicode_AS_UNICODE(v) + i; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5752 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5753 | errors, &errorHandler, |
| 5754 | "unicodeescape", "illegal Unicode character", |
| 5755 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5756 | &v, &i, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5757 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5758 | data = PyUnicode_AS_UNICODE(v); |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5759 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5760 | break; |
| 5761 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5762 | /* \N{name} */ |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5763 | case 'N': |
| 5764 | message = "malformed \\N character escape"; |
| 5765 | if (ucnhash_CAPI == NULL) { |
| 5766 | /* load the unicode data module */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5767 | ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCapsule_Import( |
| 5768 | PyUnicodeData_CAPSULE_NAME, 1); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5769 | if (ucnhash_CAPI == NULL) |
| 5770 | goto ucnhashError; |
| 5771 | } |
| 5772 | if (*s == '{') { |
| 5773 | const char *start = s+1; |
| 5774 | /* look for the closing brace */ |
| 5775 | while (*s != '}' && s < end) |
| 5776 | s++; |
| 5777 | if (s > start && s < end && *s == '}') { |
| 5778 | /* found a name. look it up in the unicode database */ |
| 5779 | message = "unknown Unicode character name"; |
| 5780 | s++; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5781 | if (ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), |
| 5782 | &chr)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5783 | goto store; |
| 5784 | } |
| 5785 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5786 | endinpos = s-starts; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5787 | p = PyUnicode_AS_UNICODE(v) + i; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5788 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5789 | errors, &errorHandler, |
| 5790 | "unicodeescape", message, |
| 5791 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5792 | &v, &i, &p)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5793 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5794 | data = PyUnicode_AS_UNICODE(v); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5795 | break; |
| 5796 | |
| 5797 | default: |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5798 | if (s > end) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5799 | assert(kind == PyUnicode_WCHAR_KIND); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5800 | message = "\\ at end of string"; |
| 5801 | s--; |
| 5802 | endinpos = s-starts; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5803 | p = PyUnicode_AS_UNICODE(v) + i; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5804 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5805 | errors, &errorHandler, |
| 5806 | "unicodeescape", message, |
| 5807 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5808 | &v, &i, &p)) |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5809 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5810 | data = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5811 | } |
| 5812 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5813 | WRITE_ASCII_OR_WSTR(kind, data, i++, '\\'); |
| 5814 | WRITE_ASCII_OR_WSTR(kind, data, i++, (unsigned char)s[-1]); |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5815 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5816 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5817 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5818 | nextByte: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5819 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5820 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5821 | /* Ensure the length prediction worked in case of ASCII strings */ |
| 5822 | assert(kind == PyUnicode_WCHAR_KIND || i == ascii_length); |
| 5823 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 5824 | if (kind == PyUnicode_WCHAR_KIND) |
| 5825 | { |
| 5826 | if (PyUnicode_Resize((PyObject**)&v, i) < 0) |
| 5827 | goto onError; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 5828 | } |
Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 5829 | Py_XDECREF(errorHandler); |
| 5830 | Py_XDECREF(exc); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 5831 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 5832 | if (_PyUnicode_READY_REPLACE(&v)) { |
| 5833 | Py_DECREF(v); |
| 5834 | return NULL; |
| 5835 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 5836 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 5837 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5838 | return (PyObject *)v; |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5839 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5840 | ucnhashError: |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 5841 | PyErr_SetString( |
| 5842 | PyExc_UnicodeError, |
| 5843 | "\\N escapes not supported (can't load unicodedata module)" |
| 5844 | ); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 5845 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5846 | Py_XDECREF(errorHandler); |
| 5847 | Py_XDECREF(exc); |
Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 5848 | return NULL; |
| 5849 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5850 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5851 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5852 | Py_XDECREF(errorHandler); |
| 5853 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5854 | return NULL; |
| 5855 | } |
| 5856 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5857 | #undef WRITE_ASCII_OR_WSTR |
| 5858 | #undef WRITE_WSTR |
| 5859 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5860 | /* Return a Unicode-Escape string version of the Unicode object. |
| 5861 | |
| 5862 | If quotes is true, the string is enclosed in u"" or u'' quotes as |
| 5863 | appropriate. |
| 5864 | |
| 5865 | */ |
| 5866 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5867 | static const char *hexdigits = "0123456789abcdef"; |
| 5868 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5869 | PyObject * |
| 5870 | PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5871 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5872 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5873 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5874 | char *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5875 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5876 | #ifdef Py_UNICODE_WIDE |
| 5877 | const Py_ssize_t expandsize = 10; |
| 5878 | #else |
| 5879 | const Py_ssize_t expandsize = 6; |
| 5880 | #endif |
| 5881 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5882 | /* XXX(nnorwitz): rather than over-allocating, it would be |
| 5883 | better to choose a different scheme. Perhaps scan the |
| 5884 | first N-chars of the string and allocate based on that size. |
| 5885 | */ |
| 5886 | /* Initial allocation is based on the longest-possible unichr |
| 5887 | escape. |
| 5888 | |
| 5889 | In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source |
| 5890 | unichr, so in this case it's the longest unichr escape. In |
| 5891 | narrow (UTF-16) builds this is five chars per source unichr |
| 5892 | since there are two unichrs in the surrogate pair, so in narrow |
| 5893 | (UTF-16) builds it's not the longest unichr escape. |
| 5894 | |
| 5895 | In wide or narrow builds '\uxxxx' is 6 chars per source unichr, |
| 5896 | so in the narrow (UTF-16) build case it's the longest unichr |
| 5897 | escape. |
| 5898 | */ |
| 5899 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5900 | if (size == 0) |
| 5901 | return PyBytes_FromStringAndSize(NULL, 0); |
| 5902 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5903 | if (size > (PY_SSIZE_T_MAX - 2 - 1) / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5904 | return PyErr_NoMemory(); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5905 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5906 | repr = PyBytes_FromStringAndSize(NULL, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5907 | 2 |
| 5908 | + expandsize*size |
| 5909 | + 1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5910 | if (repr == NULL) |
| 5911 | return NULL; |
| 5912 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5913 | p = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5914 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5915 | while (size-- > 0) { |
| 5916 | Py_UNICODE ch = *s++; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5917 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5918 | /* Escape backslashes */ |
| 5919 | if (ch == '\\') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5920 | *p++ = '\\'; |
| 5921 | *p++ = (char) ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5922 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5923 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5924 | |
Guido van Rossum | 0d42e0c | 2001-07-20 16:36:21 +0000 | [diff] [blame] | 5925 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5926 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 5927 | else if (ch >= 0x10000) { |
| 5928 | *p++ = '\\'; |
| 5929 | *p++ = 'U'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5930 | *p++ = hexdigits[(ch >> 28) & 0x0000000F]; |
| 5931 | *p++ = hexdigits[(ch >> 24) & 0x0000000F]; |
| 5932 | *p++ = hexdigits[(ch >> 20) & 0x0000000F]; |
| 5933 | *p++ = hexdigits[(ch >> 16) & 0x0000000F]; |
| 5934 | *p++ = hexdigits[(ch >> 12) & 0x0000000F]; |
| 5935 | *p++ = hexdigits[(ch >> 8) & 0x0000000F]; |
| 5936 | *p++ = hexdigits[(ch >> 4) & 0x0000000F]; |
| 5937 | *p++ = hexdigits[ch & 0x0000000F]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5938 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5939 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5940 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5941 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
| 5942 | else if (ch >= 0xD800 && ch < 0xDC00) { |
| 5943 | Py_UNICODE ch2; |
| 5944 | Py_UCS4 ucs; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5945 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5946 | ch2 = *s++; |
| 5947 | size--; |
Georg Brandl | 78eef3de | 2010-08-01 20:51:02 +0000 | [diff] [blame] | 5948 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5949 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 5950 | *p++ = '\\'; |
| 5951 | *p++ = 'U'; |
| 5952 | *p++ = hexdigits[(ucs >> 28) & 0x0000000F]; |
| 5953 | *p++ = hexdigits[(ucs >> 24) & 0x0000000F]; |
| 5954 | *p++ = hexdigits[(ucs >> 20) & 0x0000000F]; |
| 5955 | *p++ = hexdigits[(ucs >> 16) & 0x0000000F]; |
| 5956 | *p++ = hexdigits[(ucs >> 12) & 0x0000000F]; |
| 5957 | *p++ = hexdigits[(ucs >> 8) & 0x0000000F]; |
| 5958 | *p++ = hexdigits[(ucs >> 4) & 0x0000000F]; |
| 5959 | *p++ = hexdigits[ucs & 0x0000000F]; |
| 5960 | continue; |
| 5961 | } |
| 5962 | /* Fall through: isolated surrogates are copied as-is */ |
| 5963 | s--; |
| 5964 | size++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5965 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5966 | #endif |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 5967 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5968 | /* Map 16-bit characters to '\uxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 5969 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5970 | *p++ = '\\'; |
| 5971 | *p++ = 'u'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5972 | *p++ = hexdigits[(ch >> 12) & 0x000F]; |
| 5973 | *p++ = hexdigits[(ch >> 8) & 0x000F]; |
| 5974 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 5975 | *p++ = hexdigits[ch & 0x000F]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5976 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5977 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 5978 | /* Map special whitespace to '\t', \n', '\r' */ |
| 5979 | else if (ch == '\t') { |
| 5980 | *p++ = '\\'; |
| 5981 | *p++ = 't'; |
| 5982 | } |
| 5983 | else if (ch == '\n') { |
| 5984 | *p++ = '\\'; |
| 5985 | *p++ = 'n'; |
| 5986 | } |
| 5987 | else if (ch == '\r') { |
| 5988 | *p++ = '\\'; |
| 5989 | *p++ = 'r'; |
| 5990 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5991 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 5992 | /* Map non-printable US ASCII to '\xhh' */ |
Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 5993 | else if (ch < ' ' || ch >= 0x7F) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5994 | *p++ = '\\'; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 5995 | *p++ = 'x'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5996 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 5997 | *p++ = hexdigits[ch & 0x000F]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5998 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5999 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6000 | /* Copy everything else as-is */ |
| 6001 | else |
| 6002 | *p++ = (char) ch; |
| 6003 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6004 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6005 | assert(p - PyBytes_AS_STRING(repr) > 0); |
| 6006 | if (_PyBytes_Resize(&repr, p - PyBytes_AS_STRING(repr)) < 0) |
| 6007 | return NULL; |
| 6008 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6009 | } |
| 6010 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6011 | PyObject * |
| 6012 | PyUnicode_AsUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6013 | { |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 6014 | PyObject *s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6015 | if (!PyUnicode_Check(unicode)) { |
| 6016 | PyErr_BadArgument(); |
| 6017 | return NULL; |
| 6018 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 6019 | s = PyUnicode_EncodeUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 6020 | PyUnicode_GET_SIZE(unicode)); |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 6021 | return s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6022 | } |
| 6023 | |
| 6024 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ |
| 6025 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6026 | PyObject * |
| 6027 | PyUnicode_DecodeRawUnicodeEscape(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6028 | Py_ssize_t size, |
| 6029 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6030 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6031 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6032 | Py_ssize_t startinpos; |
| 6033 | Py_ssize_t endinpos; |
| 6034 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6035 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6036 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6037 | const char *end; |
| 6038 | const char *bs; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6039 | PyObject *errorHandler = NULL; |
| 6040 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6041 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6042 | /* Escaped strings will always be longer than the resulting |
| 6043 | 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] | 6044 | length after conversion to the true value. (But decoding error |
| 6045 | handler might have to resize the string) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6046 | v = _PyUnicode_New(size); |
| 6047 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6048 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6049 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6050 | return (PyObject *)v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6051 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6052 | end = s + size; |
| 6053 | while (s < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6054 | unsigned char c; |
| 6055 | Py_UCS4 x; |
| 6056 | int i; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6057 | int count; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6058 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6059 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 6060 | if (*s != '\\') { |
| 6061 | *p++ = (unsigned char)*s++; |
| 6062 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6063 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6064 | startinpos = s-starts; |
| 6065 | |
| 6066 | /* \u-escapes are only interpreted iff the number of leading |
| 6067 | backslashes if odd */ |
| 6068 | bs = s; |
| 6069 | for (;s < end;) { |
| 6070 | if (*s != '\\') |
| 6071 | break; |
| 6072 | *p++ = (unsigned char)*s++; |
| 6073 | } |
| 6074 | if (((s - bs) & 1) == 0 || |
| 6075 | s >= end || |
| 6076 | (*s != 'u' && *s != 'U')) { |
| 6077 | continue; |
| 6078 | } |
| 6079 | p--; |
| 6080 | count = *s=='u' ? 4 : 8; |
| 6081 | s++; |
| 6082 | |
| 6083 | /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ |
| 6084 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 6085 | for (x = 0, i = 0; i < count; ++i, ++s) { |
| 6086 | c = (unsigned char)*s; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 6087 | if (!Py_ISXDIGIT(c)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6088 | endinpos = s-starts; |
| 6089 | if (unicode_decode_call_errorhandler( |
| 6090 | errors, &errorHandler, |
| 6091 | "rawunicodeescape", "truncated \\uXXXX", |
| 6092 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 6093 | &v, &outpos, &p)) |
| 6094 | goto onError; |
| 6095 | goto nextByte; |
| 6096 | } |
| 6097 | x = (x<<4) & ~0xF; |
| 6098 | if (c >= '0' && c <= '9') |
| 6099 | x += c - '0'; |
| 6100 | else if (c >= 'a' && c <= 'f') |
| 6101 | x += 10 + c - 'a'; |
| 6102 | else |
| 6103 | x += 10 + c - 'A'; |
| 6104 | } |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 6105 | if (x <= 0xffff) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6106 | /* UCS-2 character */ |
| 6107 | *p++ = (Py_UNICODE) x; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 6108 | else if (x <= 0x10ffff) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6109 | /* UCS-4 character. Either store directly, or as |
| 6110 | surrogate pair. */ |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 6111 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6112 | *p++ = (Py_UNICODE) x; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 6113 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6114 | x -= 0x10000L; |
| 6115 | *p++ = 0xD800 + (Py_UNICODE) (x >> 10); |
| 6116 | *p++ = 0xDC00 + (Py_UNICODE) (x & 0x03FF); |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 6117 | #endif |
| 6118 | } else { |
| 6119 | endinpos = s-starts; |
| 6120 | outpos = p-PyUnicode_AS_UNICODE(v); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6121 | if (unicode_decode_call_errorhandler( |
| 6122 | errors, &errorHandler, |
| 6123 | "rawunicodeescape", "\\Uxxxxxxxx out of range", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6124 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 6125 | &v, &outpos, &p)) |
| 6126 | goto onError; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6127 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6128 | nextByte: |
| 6129 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6130 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 6131 | if (PyUnicode_Resize((PyObject**)&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6132 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6133 | Py_XDECREF(errorHandler); |
| 6134 | Py_XDECREF(exc); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 6135 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 6136 | if (_PyUnicode_READY_REPLACE(&v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6137 | Py_DECREF(v); |
| 6138 | return NULL; |
| 6139 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 6140 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 6141 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6142 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6143 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6144 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6145 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6146 | Py_XDECREF(errorHandler); |
| 6147 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6148 | return NULL; |
| 6149 | } |
| 6150 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6151 | PyObject * |
| 6152 | PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6153 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6154 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6155 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6156 | char *p; |
| 6157 | char *q; |
| 6158 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6159 | #ifdef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 6160 | const Py_ssize_t expandsize = 10; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6161 | #else |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 6162 | const Py_ssize_t expandsize = 6; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6163 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6164 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 6165 | if (size > PY_SSIZE_T_MAX / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6166 | return PyErr_NoMemory(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6167 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6168 | repr = PyBytes_FromStringAndSize(NULL, expandsize * size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6169 | if (repr == NULL) |
| 6170 | return NULL; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 6171 | if (size == 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6172 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6173 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6174 | p = q = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6175 | while (size-- > 0) { |
| 6176 | Py_UNICODE ch = *s++; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6177 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6178 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 6179 | if (ch >= 0x10000) { |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6180 | *p++ = '\\'; |
| 6181 | *p++ = 'U'; |
Walter Dörwald | db5d33e | 2007-05-12 11:13:47 +0000 | [diff] [blame] | 6182 | *p++ = hexdigits[(ch >> 28) & 0xf]; |
| 6183 | *p++ = hexdigits[(ch >> 24) & 0xf]; |
| 6184 | *p++ = hexdigits[(ch >> 20) & 0xf]; |
| 6185 | *p++ = hexdigits[(ch >> 16) & 0xf]; |
| 6186 | *p++ = hexdigits[(ch >> 12) & 0xf]; |
| 6187 | *p++ = hexdigits[(ch >> 8) & 0xf]; |
| 6188 | *p++ = hexdigits[(ch >> 4) & 0xf]; |
| 6189 | *p++ = hexdigits[ch & 15]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6190 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6191 | else |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 6192 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6193 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
| 6194 | if (ch >= 0xD800 && ch < 0xDC00) { |
| 6195 | Py_UNICODE ch2; |
| 6196 | Py_UCS4 ucs; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 6197 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6198 | ch2 = *s++; |
| 6199 | size--; |
Georg Brandl | 78eef3de | 2010-08-01 20:51:02 +0000 | [diff] [blame] | 6200 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6201 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 6202 | *p++ = '\\'; |
| 6203 | *p++ = 'U'; |
| 6204 | *p++ = hexdigits[(ucs >> 28) & 0xf]; |
| 6205 | *p++ = hexdigits[(ucs >> 24) & 0xf]; |
| 6206 | *p++ = hexdigits[(ucs >> 20) & 0xf]; |
| 6207 | *p++ = hexdigits[(ucs >> 16) & 0xf]; |
| 6208 | *p++ = hexdigits[(ucs >> 12) & 0xf]; |
| 6209 | *p++ = hexdigits[(ucs >> 8) & 0xf]; |
| 6210 | *p++ = hexdigits[(ucs >> 4) & 0xf]; |
| 6211 | *p++ = hexdigits[ucs & 0xf]; |
| 6212 | continue; |
| 6213 | } |
| 6214 | /* Fall through: isolated surrogates are copied as-is */ |
| 6215 | s--; |
| 6216 | size++; |
| 6217 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6218 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6219 | /* Map 16-bit characters to '\uxxxx' */ |
| 6220 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6221 | *p++ = '\\'; |
| 6222 | *p++ = 'u'; |
Walter Dörwald | db5d33e | 2007-05-12 11:13:47 +0000 | [diff] [blame] | 6223 | *p++ = hexdigits[(ch >> 12) & 0xf]; |
| 6224 | *p++ = hexdigits[(ch >> 8) & 0xf]; |
| 6225 | *p++ = hexdigits[(ch >> 4) & 0xf]; |
| 6226 | *p++ = hexdigits[ch & 15]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6227 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6228 | /* Copy everything else as-is */ |
| 6229 | else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6230 | *p++ = (char) ch; |
| 6231 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6232 | size = p - q; |
| 6233 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6234 | assert(size > 0); |
| 6235 | if (_PyBytes_Resize(&repr, size) < 0) |
| 6236 | return NULL; |
| 6237 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6238 | } |
| 6239 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6240 | PyObject * |
| 6241 | PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6242 | { |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 6243 | PyObject *s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6244 | if (!PyUnicode_Check(unicode)) { |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 6245 | PyErr_BadArgument(); |
| 6246 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6247 | } |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 6248 | s = PyUnicode_EncodeRawUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 6249 | PyUnicode_GET_SIZE(unicode)); |
| 6250 | |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 6251 | return s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6252 | } |
| 6253 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6254 | /* --- Unicode Internal Codec ------------------------------------------- */ |
| 6255 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6256 | PyObject * |
| 6257 | _PyUnicode_DecodeUnicodeInternal(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6258 | Py_ssize_t size, |
| 6259 | const char *errors) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6260 | { |
| 6261 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6262 | Py_ssize_t startinpos; |
| 6263 | Py_ssize_t endinpos; |
| 6264 | Py_ssize_t outpos; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6265 | PyUnicodeObject *v; |
| 6266 | Py_UNICODE *p; |
| 6267 | const char *end; |
| 6268 | const char *reason; |
| 6269 | PyObject *errorHandler = NULL; |
| 6270 | PyObject *exc = NULL; |
| 6271 | |
Neal Norwitz | d43069c | 2006-01-08 01:12:10 +0000 | [diff] [blame] | 6272 | #ifdef Py_UNICODE_WIDE |
| 6273 | Py_UNICODE unimax = PyUnicode_GetMax(); |
| 6274 | #endif |
| 6275 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6276 | /* XXX overflow detection missing */ |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6277 | v = _PyUnicode_New((size+Py_UNICODE_SIZE-1)/ Py_UNICODE_SIZE); |
| 6278 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6279 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6280 | /* Intentionally PyUnicode_GET_SIZE instead of PyUnicode_GET_LENGTH |
| 6281 | as string was created with the old API. */ |
| 6282 | if (PyUnicode_GET_SIZE(v) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6283 | return (PyObject *)v; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6284 | p = PyUnicode_AS_UNICODE(v); |
| 6285 | end = s + size; |
| 6286 | |
| 6287 | while (s < end) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6288 | memcpy(p, s, sizeof(Py_UNICODE)); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6289 | /* We have to sanity check the raw data, otherwise doom looms for |
| 6290 | some malformed UCS-4 data. */ |
| 6291 | if ( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6292 | #ifdef Py_UNICODE_WIDE |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6293 | *p > unimax || *p < 0 || |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6294 | #endif |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6295 | end-s < Py_UNICODE_SIZE |
| 6296 | ) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6297 | { |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6298 | startinpos = s - starts; |
| 6299 | if (end-s < Py_UNICODE_SIZE) { |
| 6300 | endinpos = end-starts; |
| 6301 | reason = "truncated input"; |
| 6302 | } |
| 6303 | else { |
| 6304 | endinpos = s - starts + Py_UNICODE_SIZE; |
| 6305 | reason = "illegal code point (> 0x10FFFF)"; |
| 6306 | } |
| 6307 | outpos = p - PyUnicode_AS_UNICODE(v); |
| 6308 | if (unicode_decode_call_errorhandler( |
| 6309 | errors, &errorHandler, |
| 6310 | "unicode_internal", reason, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 6311 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 6312 | &v, &outpos, &p)) { |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6313 | goto onError; |
| 6314 | } |
| 6315 | } |
| 6316 | else { |
| 6317 | p++; |
| 6318 | s += Py_UNICODE_SIZE; |
| 6319 | } |
| 6320 | } |
| 6321 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 6322 | if (PyUnicode_Resize((PyObject**)&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6323 | goto onError; |
| 6324 | Py_XDECREF(errorHandler); |
| 6325 | Py_XDECREF(exc); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 6326 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 6327 | if (_PyUnicode_READY_REPLACE(&v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6328 | Py_DECREF(v); |
| 6329 | return NULL; |
| 6330 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 6331 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 6332 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6333 | return (PyObject *)v; |
| 6334 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6335 | onError: |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6336 | Py_XDECREF(v); |
| 6337 | Py_XDECREF(errorHandler); |
| 6338 | Py_XDECREF(exc); |
| 6339 | return NULL; |
| 6340 | } |
| 6341 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6342 | /* --- Latin-1 Codec ------------------------------------------------------ */ |
| 6343 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6344 | PyObject * |
| 6345 | PyUnicode_DecodeLatin1(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6346 | Py_ssize_t size, |
| 6347 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6348 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6349 | /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 6350 | return _PyUnicode_FromUCS1((unsigned char*)s, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6351 | } |
| 6352 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6353 | /* create or adjust a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6354 | static void |
| 6355 | make_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6356 | const char *encoding, |
| 6357 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 6358 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6359 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6360 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6361 | if (*exceptionObject == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6362 | *exceptionObject = PyUnicodeEncodeError_Create( |
| 6363 | encoding, unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6364 | } |
| 6365 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6366 | if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 6367 | goto onError; |
| 6368 | if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) |
| 6369 | goto onError; |
| 6370 | if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) |
| 6371 | goto onError; |
| 6372 | return; |
| 6373 | onError: |
| 6374 | Py_DECREF(*exceptionObject); |
| 6375 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6376 | } |
| 6377 | } |
| 6378 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6379 | /* raises a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6380 | static void |
| 6381 | raise_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6382 | const char *encoding, |
| 6383 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 6384 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6385 | const char *reason) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6386 | { |
| 6387 | make_encode_exception(exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6388 | encoding, unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6389 | if (*exceptionObject != NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6390 | PyCodec_StrictErrors(*exceptionObject); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6391 | } |
| 6392 | |
| 6393 | /* error handling callback helper: |
| 6394 | build arguments, call the callback and check the arguments, |
| 6395 | put the result into newpos and return the replacement string, which |
| 6396 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6397 | static PyObject * |
| 6398 | unicode_encode_call_errorhandler(const char *errors, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6399 | PyObject **errorHandler, |
| 6400 | const char *encoding, const char *reason, |
| 6401 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 6402 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6403 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6404 | { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6405 | 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] | 6406 | |
| 6407 | PyObject *restuple; |
| 6408 | PyObject *resunicode; |
| 6409 | |
| 6410 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6411 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6412 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6413 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6414 | } |
| 6415 | |
| 6416 | make_encode_exception(exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6417 | encoding, unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6418 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6419 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6420 | |
| 6421 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6422 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6423 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6424 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6425 | if (!PyTuple_Check(restuple)) { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6426 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6427 | Py_DECREF(restuple); |
| 6428 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6429 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6430 | if (!PyArg_ParseTuple(restuple, argparse, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6431 | &resunicode, newpos)) { |
| 6432 | Py_DECREF(restuple); |
| 6433 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6434 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6435 | if (!PyUnicode_Check(resunicode) && !PyBytes_Check(resunicode)) { |
| 6436 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
| 6437 | Py_DECREF(restuple); |
| 6438 | return NULL; |
| 6439 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6440 | if (*newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6441 | *newpos = size+*newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 6442 | if (*newpos<0 || *newpos>size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6443 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 6444 | Py_DECREF(restuple); |
| 6445 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 6446 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6447 | Py_INCREF(resunicode); |
| 6448 | Py_DECREF(restuple); |
| 6449 | return resunicode; |
| 6450 | } |
| 6451 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6452 | static PyObject * |
| 6453 | unicode_encode_ucs1(const Py_UNICODE *p, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6454 | Py_ssize_t size, |
| 6455 | const char *errors, |
| 6456 | int limit) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6457 | { |
| 6458 | /* output object */ |
| 6459 | PyObject *res; |
| 6460 | /* pointers to the beginning and end+1 of input */ |
| 6461 | const Py_UNICODE *startp = p; |
| 6462 | const Py_UNICODE *endp = p + size; |
| 6463 | /* pointer to the beginning of the unencodable characters */ |
| 6464 | /* const Py_UNICODE *badp = NULL; */ |
| 6465 | /* pointer into the output */ |
| 6466 | char *str; |
| 6467 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6468 | Py_ssize_t ressize; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6469 | const char *encoding = (limit == 256) ? "latin-1" : "ascii"; |
| 6470 | 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] | 6471 | PyObject *errorHandler = NULL; |
| 6472 | PyObject *exc = NULL; |
| 6473 | /* the following variable is used for caching string comparisons |
| 6474 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 6475 | int known_errorHandler = -1; |
| 6476 | |
| 6477 | /* allocate enough for a simple encoding without |
| 6478 | replacements, if we need more, we'll resize */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6479 | if (size == 0) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 6480 | return PyBytes_FromStringAndSize(NULL, 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6481 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6482 | if (res == NULL) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6483 | return NULL; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6484 | str = PyBytes_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6485 | ressize = size; |
| 6486 | |
| 6487 | while (p<endp) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6488 | Py_UNICODE c = *p; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6489 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6490 | /* can we encode this? */ |
| 6491 | if (c<limit) { |
| 6492 | /* no overflow check, because we know that the space is enough */ |
| 6493 | *str++ = (char)c; |
| 6494 | ++p; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6495 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6496 | else { |
| 6497 | Py_ssize_t unicodepos = p-startp; |
| 6498 | Py_ssize_t requiredsize; |
| 6499 | PyObject *repunicode; |
| 6500 | Py_ssize_t repsize; |
| 6501 | Py_ssize_t newpos; |
| 6502 | Py_ssize_t respos; |
| 6503 | Py_UNICODE *uni2; |
| 6504 | /* startpos for collecting unencodable chars */ |
| 6505 | const Py_UNICODE *collstart = p; |
| 6506 | const Py_UNICODE *collend = p; |
| 6507 | /* find all unecodable characters */ |
| 6508 | while ((collend < endp) && ((*collend)>=limit)) |
| 6509 | ++collend; |
| 6510 | /* cache callback name lookup (if not done yet, i.e. it's the first error) */ |
| 6511 | if (known_errorHandler==-1) { |
| 6512 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 6513 | known_errorHandler = 1; |
| 6514 | else if (!strcmp(errors, "replace")) |
| 6515 | known_errorHandler = 2; |
| 6516 | else if (!strcmp(errors, "ignore")) |
| 6517 | known_errorHandler = 3; |
| 6518 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 6519 | known_errorHandler = 4; |
| 6520 | else |
| 6521 | known_errorHandler = 0; |
| 6522 | } |
| 6523 | switch (known_errorHandler) { |
| 6524 | case 1: /* strict */ |
| 6525 | raise_encode_exception(&exc, encoding, startp, size, collstart-startp, collend-startp, reason); |
| 6526 | goto onError; |
| 6527 | case 2: /* replace */ |
| 6528 | while (collstart++<collend) |
| 6529 | *str++ = '?'; /* fall through */ |
| 6530 | case 3: /* ignore */ |
| 6531 | p = collend; |
| 6532 | break; |
| 6533 | case 4: /* xmlcharrefreplace */ |
| 6534 | respos = str - PyBytes_AS_STRING(res); |
| 6535 | /* determine replacement size (temporarily (mis)uses p) */ |
| 6536 | for (p = collstart, repsize = 0; p < collend; ++p) { |
| 6537 | if (*p<10) |
| 6538 | repsize += 2+1+1; |
| 6539 | else if (*p<100) |
| 6540 | repsize += 2+2+1; |
| 6541 | else if (*p<1000) |
| 6542 | repsize += 2+3+1; |
| 6543 | else if (*p<10000) |
| 6544 | repsize += 2+4+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 6545 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6546 | else |
| 6547 | repsize += 2+5+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 6548 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6549 | else if (*p<100000) |
| 6550 | repsize += 2+5+1; |
| 6551 | else if (*p<1000000) |
| 6552 | repsize += 2+6+1; |
| 6553 | else |
| 6554 | repsize += 2+7+1; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 6555 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6556 | } |
| 6557 | requiredsize = respos+repsize+(endp-collend); |
| 6558 | if (requiredsize > ressize) { |
| 6559 | if (requiredsize<2*ressize) |
| 6560 | requiredsize = 2*ressize; |
| 6561 | if (_PyBytes_Resize(&res, requiredsize)) |
| 6562 | goto onError; |
| 6563 | str = PyBytes_AS_STRING(res) + respos; |
| 6564 | ressize = requiredsize; |
| 6565 | } |
| 6566 | /* generate replacement (temporarily (mis)uses p) */ |
| 6567 | for (p = collstart; p < collend; ++p) { |
| 6568 | str += sprintf(str, "&#%d;", (int)*p); |
| 6569 | } |
| 6570 | p = collend; |
| 6571 | break; |
| 6572 | default: |
| 6573 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 6574 | encoding, reason, startp, size, &exc, |
| 6575 | collstart-startp, collend-startp, &newpos); |
| 6576 | if (repunicode == NULL) |
| 6577 | goto onError; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6578 | if (PyBytes_Check(repunicode)) { |
| 6579 | /* Directly copy bytes result to output. */ |
| 6580 | repsize = PyBytes_Size(repunicode); |
| 6581 | if (repsize > 1) { |
| 6582 | /* Make room for all additional bytes. */ |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 6583 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6584 | if (_PyBytes_Resize(&res, ressize+repsize-1)) { |
| 6585 | Py_DECREF(repunicode); |
| 6586 | goto onError; |
| 6587 | } |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 6588 | str = PyBytes_AS_STRING(res) + respos; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6589 | ressize += repsize-1; |
| 6590 | } |
| 6591 | memcpy(str, PyBytes_AsString(repunicode), repsize); |
| 6592 | str += repsize; |
| 6593 | p = startp + newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6594 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6595 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6596 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6597 | /* need more space? (at least enough for what we |
| 6598 | have+the replacement+the rest of the string, so |
| 6599 | we won't have to check space for encodable characters) */ |
| 6600 | respos = str - PyBytes_AS_STRING(res); |
| 6601 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 6602 | requiredsize = respos+repsize+(endp-collend); |
| 6603 | if (requiredsize > ressize) { |
| 6604 | if (requiredsize<2*ressize) |
| 6605 | requiredsize = 2*ressize; |
| 6606 | if (_PyBytes_Resize(&res, requiredsize)) { |
| 6607 | Py_DECREF(repunicode); |
| 6608 | goto onError; |
| 6609 | } |
| 6610 | str = PyBytes_AS_STRING(res) + respos; |
| 6611 | ressize = requiredsize; |
| 6612 | } |
| 6613 | /* check if there is anything unencodable in the replacement |
| 6614 | and copy it to the output */ |
| 6615 | for (uni2 = PyUnicode_AS_UNICODE(repunicode);repsize-->0; ++uni2, ++str) { |
| 6616 | c = *uni2; |
| 6617 | if (c >= limit) { |
| 6618 | raise_encode_exception(&exc, encoding, startp, size, |
| 6619 | unicodepos, unicodepos+1, reason); |
| 6620 | Py_DECREF(repunicode); |
| 6621 | goto onError; |
| 6622 | } |
| 6623 | *str = (char)c; |
| 6624 | } |
| 6625 | p = startp + newpos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6626 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6627 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6628 | } |
| 6629 | } |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6630 | /* Resize if we allocated to much */ |
| 6631 | size = str - PyBytes_AS_STRING(res); |
| 6632 | if (size < ressize) { /* If this falls res will be NULL */ |
Alexandre Vassalotti | bad1b92 | 2008-12-27 09:49:09 +0000 | [diff] [blame] | 6633 | assert(size >= 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6634 | if (_PyBytes_Resize(&res, size) < 0) |
| 6635 | goto onError; |
| 6636 | } |
| 6637 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6638 | Py_XDECREF(errorHandler); |
| 6639 | Py_XDECREF(exc); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6640 | return res; |
| 6641 | |
| 6642 | onError: |
| 6643 | Py_XDECREF(res); |
| 6644 | Py_XDECREF(errorHandler); |
| 6645 | Py_XDECREF(exc); |
| 6646 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6647 | } |
| 6648 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6649 | PyObject * |
| 6650 | PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6651 | Py_ssize_t size, |
| 6652 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6653 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6654 | return unicode_encode_ucs1(p, size, errors, 256); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6655 | } |
| 6656 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6657 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6658 | _PyUnicode_AsLatin1String(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6659 | { |
| 6660 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6661 | PyErr_BadArgument(); |
| 6662 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6663 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6664 | if (PyUnicode_READY(unicode) == -1) |
| 6665 | return NULL; |
| 6666 | /* Fast path: if it is a one-byte string, construct |
| 6667 | bytes object directly. */ |
| 6668 | if (PyUnicode_KIND(unicode) == PyUnicode_1BYTE_KIND) |
| 6669 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 6670 | PyUnicode_GET_LENGTH(unicode)); |
| 6671 | /* Non-Latin-1 characters present. Defer to above function to |
| 6672 | raise the exception. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6673 | return PyUnicode_EncodeLatin1(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6674 | PyUnicode_GET_SIZE(unicode), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6675 | errors); |
| 6676 | } |
| 6677 | |
| 6678 | PyObject* |
| 6679 | PyUnicode_AsLatin1String(PyObject *unicode) |
| 6680 | { |
| 6681 | return _PyUnicode_AsLatin1String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6682 | } |
| 6683 | |
| 6684 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ |
| 6685 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6686 | PyObject * |
| 6687 | PyUnicode_DecodeASCII(const char *s, |
| 6688 | Py_ssize_t size, |
| 6689 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6690 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6691 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6692 | PyUnicodeObject *v; |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6693 | Py_UNICODE *u; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6694 | Py_ssize_t startinpos; |
| 6695 | Py_ssize_t endinpos; |
| 6696 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6697 | const char *e; |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6698 | int has_error; |
| 6699 | const unsigned char *p = (const unsigned char *)s; |
| 6700 | const unsigned char *end = p + size; |
| 6701 | 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] | 6702 | PyObject *errorHandler = NULL; |
| 6703 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6704 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6705 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6706 | if (size == 1 && (unsigned char)s[0] < 128) |
| 6707 | return get_latin1_char((unsigned char)s[0]); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6708 | |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6709 | has_error = 0; |
| 6710 | while (p < end && !has_error) { |
| 6711 | /* Fast path, see below in PyUnicode_DecodeUTF8Stateful for |
| 6712 | an explanation. */ |
| 6713 | if (!((size_t) p & LONG_PTR_MASK)) { |
| 6714 | /* Help register allocation */ |
| 6715 | register const unsigned char *_p = p; |
| 6716 | while (_p < aligned_end) { |
| 6717 | unsigned long value = *(unsigned long *) _p; |
| 6718 | if (value & ASCII_CHAR_MASK) { |
| 6719 | has_error = 1; |
| 6720 | break; |
| 6721 | } |
| 6722 | _p += SIZEOF_LONG; |
| 6723 | } |
| 6724 | if (_p == end) |
| 6725 | break; |
| 6726 | if (has_error) |
| 6727 | break; |
| 6728 | p = _p; |
| 6729 | } |
| 6730 | if (*p & 0x80) { |
| 6731 | has_error = 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6732 | break; |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6733 | } |
| 6734 | else { |
| 6735 | ++p; |
| 6736 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 6737 | } |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6738 | if (!has_error) |
| 6739 | return unicode_fromascii((const unsigned char *)s, size); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6740 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6741 | v = _PyUnicode_New(size); |
| 6742 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6743 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6744 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6745 | return (PyObject *)v; |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6746 | u = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6747 | e = s + size; |
| 6748 | while (s < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6749 | register unsigned char c = (unsigned char)*s; |
| 6750 | if (c < 128) { |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6751 | *u++ = c; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6752 | ++s; |
| 6753 | } |
| 6754 | else { |
| 6755 | startinpos = s-starts; |
| 6756 | endinpos = startinpos + 1; |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6757 | outpos = u - (Py_UNICODE *)PyUnicode_AS_UNICODE(v); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6758 | if (unicode_decode_call_errorhandler( |
| 6759 | errors, &errorHandler, |
| 6760 | "ascii", "ordinal not in range(128)", |
| 6761 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6762 | &v, &outpos, &u)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6763 | goto onError; |
| 6764 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6765 | } |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6766 | if (u - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
| 6767 | if (PyUnicode_Resize((PyObject**)&v, u - PyUnicode_AS_UNICODE(v)) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6768 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6769 | Py_XDECREF(errorHandler); |
| 6770 | Py_XDECREF(exc); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 6771 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 6772 | if (_PyUnicode_READY_REPLACE(&v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6773 | Py_DECREF(v); |
| 6774 | return NULL; |
| 6775 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 6776 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 6777 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6778 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6779 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6780 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6781 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6782 | Py_XDECREF(errorHandler); |
| 6783 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6784 | return NULL; |
| 6785 | } |
| 6786 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6787 | PyObject * |
| 6788 | PyUnicode_EncodeASCII(const Py_UNICODE *p, |
| 6789 | Py_ssize_t size, |
| 6790 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6791 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6792 | return unicode_encode_ucs1(p, size, errors, 128); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6793 | } |
| 6794 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6795 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6796 | _PyUnicode_AsASCIIString(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6797 | { |
| 6798 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6799 | PyErr_BadArgument(); |
| 6800 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6801 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6802 | if (PyUnicode_READY(unicode) == -1) |
| 6803 | return NULL; |
| 6804 | /* Fast path: if it is an ASCII-only string, construct bytes object |
| 6805 | directly. Else defer to above function to raise the exception. */ |
| 6806 | if (PyUnicode_MAX_CHAR_VALUE(unicode) < 128) |
| 6807 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 6808 | PyUnicode_GET_LENGTH(unicode)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6809 | return PyUnicode_EncodeASCII(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6810 | PyUnicode_GET_SIZE(unicode), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6811 | errors); |
| 6812 | } |
| 6813 | |
| 6814 | PyObject * |
| 6815 | PyUnicode_AsASCIIString(PyObject *unicode) |
| 6816 | { |
| 6817 | return _PyUnicode_AsASCIIString(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6818 | } |
| 6819 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 6820 | #ifdef HAVE_MBCS |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6821 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6822 | /* --- MBCS codecs for Windows -------------------------------------------- */ |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6823 | |
Hirokazu Yamamoto | 3530246 | 2009-03-21 13:23:27 +0000 | [diff] [blame] | 6824 | #if SIZEOF_INT < SIZEOF_SIZE_T |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6825 | #define NEED_RETRY |
| 6826 | #endif |
| 6827 | |
| 6828 | /* XXX This code is limited to "true" double-byte encodings, as |
| 6829 | a) it assumes an incomplete character consists of a single byte, and |
| 6830 | b) IsDBCSLeadByte (probably) does not work for non-DBCS multi-byte |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6831 | encodings, see IsDBCSLeadByteEx documentation. */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6832 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6833 | static int |
| 6834 | is_dbcs_lead_byte(const char *s, int offset) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6835 | { |
| 6836 | const char *curr = s + offset; |
| 6837 | |
| 6838 | if (IsDBCSLeadByte(*curr)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6839 | const char *prev = CharPrev(s, curr); |
| 6840 | return (prev == curr) || !IsDBCSLeadByte(*prev) || (curr - prev == 2); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6841 | } |
| 6842 | return 0; |
| 6843 | } |
| 6844 | |
| 6845 | /* |
| 6846 | * Decode MBCS string into unicode object. If 'final' is set, converts |
| 6847 | * trailing lead-byte too. Returns consumed size if succeed, -1 otherwise. |
| 6848 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6849 | static int |
| 6850 | decode_mbcs(PyUnicodeObject **v, |
| 6851 | const char *s, /* MBCS string */ |
| 6852 | int size, /* sizeof MBCS string */ |
| 6853 | int final, |
| 6854 | const char *errors) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6855 | { |
| 6856 | Py_UNICODE *p; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6857 | Py_ssize_t n; |
| 6858 | DWORD usize; |
| 6859 | DWORD flags; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6860 | |
| 6861 | assert(size >= 0); |
| 6862 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6863 | /* check and handle 'errors' arg */ |
| 6864 | if (errors==NULL || strcmp(errors, "strict")==0) |
| 6865 | flags = MB_ERR_INVALID_CHARS; |
| 6866 | else if (strcmp(errors, "ignore")==0) |
| 6867 | flags = 0; |
| 6868 | else { |
| 6869 | PyErr_Format(PyExc_ValueError, |
| 6870 | "mbcs encoding does not support errors='%s'", |
| 6871 | errors); |
| 6872 | return -1; |
| 6873 | } |
| 6874 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6875 | /* Skip trailing lead-byte unless 'final' is set */ |
| 6876 | if (!final && size >= 1 && is_dbcs_lead_byte(s, size - 1)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6877 | --size; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6878 | |
| 6879 | /* First get the size of the result */ |
| 6880 | if (size > 0) { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6881 | usize = MultiByteToWideChar(CP_ACP, flags, s, size, NULL, 0); |
| 6882 | if (usize==0) |
| 6883 | goto mbcs_decode_error; |
| 6884 | } else |
| 6885 | usize = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6886 | |
| 6887 | if (*v == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6888 | /* Create unicode object */ |
| 6889 | *v = _PyUnicode_New(usize); |
| 6890 | if (*v == NULL) |
| 6891 | return -1; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6892 | n = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6893 | } |
| 6894 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6895 | /* Extend unicode object */ |
| 6896 | n = PyUnicode_GET_SIZE(*v); |
Victor Stinner | 2fd8227 | 2011-10-03 04:06:05 +0200 | [diff] [blame] | 6897 | if (PyUnicode_Resize((PyObject**)v, n + usize) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6898 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6899 | } |
| 6900 | |
| 6901 | /* Do the conversion */ |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6902 | if (usize > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6903 | p = PyUnicode_AS_UNICODE(*v) + n; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6904 | if (0 == MultiByteToWideChar(CP_ACP, flags, s, size, p, usize)) { |
| 6905 | goto mbcs_decode_error; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6906 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6907 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6908 | return size; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6909 | |
| 6910 | mbcs_decode_error: |
| 6911 | /* If the last error was ERROR_NO_UNICODE_TRANSLATION, then |
| 6912 | we raise a UnicodeDecodeError - else it is a 'generic' |
| 6913 | windows error |
| 6914 | */ |
| 6915 | if (GetLastError()==ERROR_NO_UNICODE_TRANSLATION) { |
| 6916 | /* Ideally, we should get reason from FormatMessage - this |
| 6917 | is the Windows 2000 English version of the message |
| 6918 | */ |
| 6919 | PyObject *exc = NULL; |
| 6920 | const char *reason = "No mapping for the Unicode character exists " |
| 6921 | "in the target multi-byte code page."; |
| 6922 | make_decode_exception(&exc, "mbcs", s, size, 0, 0, reason); |
| 6923 | if (exc != NULL) { |
| 6924 | PyCodec_StrictErrors(exc); |
| 6925 | Py_DECREF(exc); |
| 6926 | } |
| 6927 | } else { |
| 6928 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 6929 | } |
| 6930 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6931 | } |
| 6932 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6933 | PyObject * |
| 6934 | PyUnicode_DecodeMBCSStateful(const char *s, |
| 6935 | Py_ssize_t size, |
| 6936 | const char *errors, |
| 6937 | Py_ssize_t *consumed) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6938 | { |
| 6939 | PyUnicodeObject *v = NULL; |
| 6940 | int done; |
| 6941 | |
| 6942 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6943 | *consumed = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6944 | |
| 6945 | #ifdef NEED_RETRY |
| 6946 | retry: |
| 6947 | if (size > INT_MAX) |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6948 | done = decode_mbcs(&v, s, INT_MAX, 0, errors); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6949 | else |
| 6950 | #endif |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6951 | done = decode_mbcs(&v, s, (int)size, !consumed, errors); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6952 | |
| 6953 | if (done < 0) { |
| 6954 | Py_XDECREF(v); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6955 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6956 | } |
| 6957 | |
| 6958 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6959 | *consumed += done; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6960 | |
| 6961 | #ifdef NEED_RETRY |
| 6962 | if (size > INT_MAX) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6963 | s += done; |
| 6964 | size -= done; |
| 6965 | goto retry; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6966 | } |
| 6967 | #endif |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 6968 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 6969 | if (_PyUnicode_READY_REPLACE(&v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6970 | Py_DECREF(v); |
| 6971 | return NULL; |
| 6972 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 6973 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 6974 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6975 | return (PyObject *)v; |
| 6976 | } |
| 6977 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6978 | PyObject * |
| 6979 | PyUnicode_DecodeMBCS(const char *s, |
| 6980 | Py_ssize_t size, |
| 6981 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6982 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6983 | return PyUnicode_DecodeMBCSStateful(s, size, errors, NULL); |
| 6984 | } |
| 6985 | |
| 6986 | /* |
| 6987 | * Convert unicode into string object (MBCS). |
| 6988 | * Returns 0 if succeed, -1 otherwise. |
| 6989 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6990 | static int |
| 6991 | encode_mbcs(PyObject **repr, |
| 6992 | const Py_UNICODE *p, /* unicode */ |
| 6993 | int size, /* size of unicode */ |
| 6994 | const char* errors) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6995 | { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6996 | BOOL usedDefaultChar = FALSE; |
| 6997 | BOOL *pusedDefaultChar; |
| 6998 | int mbcssize; |
| 6999 | Py_ssize_t n; |
| 7000 | PyObject *exc = NULL; |
| 7001 | DWORD flags; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7002 | |
| 7003 | assert(size >= 0); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7004 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7005 | /* check and handle 'errors' arg */ |
| 7006 | if (errors==NULL || strcmp(errors, "strict")==0) { |
| 7007 | flags = WC_NO_BEST_FIT_CHARS; |
| 7008 | pusedDefaultChar = &usedDefaultChar; |
| 7009 | } else if (strcmp(errors, "replace")==0) { |
| 7010 | flags = 0; |
| 7011 | pusedDefaultChar = NULL; |
| 7012 | } else { |
| 7013 | PyErr_Format(PyExc_ValueError, |
| 7014 | "mbcs encoding does not support errors='%s'", |
| 7015 | errors); |
| 7016 | return -1; |
| 7017 | } |
| 7018 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7019 | /* First get the size of the result */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7020 | if (size > 0) { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7021 | mbcssize = WideCharToMultiByte(CP_ACP, flags, p, size, NULL, 0, |
| 7022 | NULL, pusedDefaultChar); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7023 | if (mbcssize == 0) { |
| 7024 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 7025 | return -1; |
| 7026 | } |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7027 | /* If we used a default char, then we failed! */ |
| 7028 | if (pusedDefaultChar && *pusedDefaultChar) |
| 7029 | goto mbcs_encode_error; |
| 7030 | } else { |
| 7031 | mbcssize = 0; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7032 | } |
| 7033 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7034 | if (*repr == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7035 | /* Create string object */ |
| 7036 | *repr = PyBytes_FromStringAndSize(NULL, mbcssize); |
| 7037 | if (*repr == NULL) |
| 7038 | return -1; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7039 | n = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7040 | } |
| 7041 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7042 | /* Extend string object */ |
| 7043 | n = PyBytes_Size(*repr); |
| 7044 | if (_PyBytes_Resize(repr, n + mbcssize) < 0) |
| 7045 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7046 | } |
| 7047 | |
| 7048 | /* Do the conversion */ |
| 7049 | if (size > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7050 | char *s = PyBytes_AS_STRING(*repr) + n; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7051 | if (0 == WideCharToMultiByte(CP_ACP, flags, p, size, s, mbcssize, |
| 7052 | NULL, pusedDefaultChar)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7053 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 7054 | return -1; |
| 7055 | } |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7056 | if (pusedDefaultChar && *pusedDefaultChar) |
| 7057 | goto mbcs_encode_error; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7058 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7059 | return 0; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7060 | |
| 7061 | mbcs_encode_error: |
| 7062 | raise_encode_exception(&exc, "mbcs", p, size, 0, 0, "invalid character"); |
| 7063 | Py_XDECREF(exc); |
| 7064 | return -1; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7065 | } |
| 7066 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7067 | PyObject * |
| 7068 | PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
| 7069 | Py_ssize_t size, |
| 7070 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7071 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7072 | PyObject *repr = NULL; |
| 7073 | int ret; |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 7074 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7075 | #ifdef NEED_RETRY |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7076 | retry: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7077 | if (size > INT_MAX) |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7078 | ret = encode_mbcs(&repr, p, INT_MAX, errors); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7079 | else |
| 7080 | #endif |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7081 | ret = encode_mbcs(&repr, p, (int)size, errors); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7082 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7083 | if (ret < 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7084 | Py_XDECREF(repr); |
| 7085 | return NULL; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7086 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7087 | |
| 7088 | #ifdef NEED_RETRY |
| 7089 | if (size > INT_MAX) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7090 | p += INT_MAX; |
| 7091 | size -= INT_MAX; |
| 7092 | goto retry; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7093 | } |
| 7094 | #endif |
| 7095 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7096 | return repr; |
| 7097 | } |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 7098 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7099 | PyObject * |
| 7100 | PyUnicode_AsMBCSString(PyObject *unicode) |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 7101 | { |
| 7102 | if (!PyUnicode_Check(unicode)) { |
| 7103 | PyErr_BadArgument(); |
| 7104 | return NULL; |
| 7105 | } |
| 7106 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7107 | PyUnicode_GET_SIZE(unicode), |
| 7108 | NULL); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 7109 | } |
| 7110 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7111 | #undef NEED_RETRY |
| 7112 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 7113 | #endif /* HAVE_MBCS */ |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7114 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7115 | /* --- Character Mapping Codec -------------------------------------------- */ |
| 7116 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7117 | PyObject * |
| 7118 | PyUnicode_DecodeCharmap(const char *s, |
| 7119 | Py_ssize_t size, |
| 7120 | PyObject *mapping, |
| 7121 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7122 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7123 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7124 | Py_ssize_t startinpos; |
| 7125 | Py_ssize_t endinpos; |
| 7126 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7127 | const char *e; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7128 | PyUnicodeObject *v; |
| 7129 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7130 | Py_ssize_t extrachars = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7131 | PyObject *errorHandler = NULL; |
| 7132 | PyObject *exc = NULL; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7133 | Py_UNICODE *mapstring = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7134 | Py_ssize_t maplen = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7135 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7136 | /* Default to Latin-1 */ |
| 7137 | if (mapping == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7138 | return PyUnicode_DecodeLatin1(s, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7139 | |
| 7140 | v = _PyUnicode_New(size); |
| 7141 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7142 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7143 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7144 | return (PyObject *)v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7145 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7146 | e = s + size; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7147 | if (PyUnicode_CheckExact(mapping)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7148 | mapstring = PyUnicode_AS_UNICODE(mapping); |
| 7149 | maplen = PyUnicode_GET_SIZE(mapping); |
| 7150 | while (s < e) { |
| 7151 | unsigned char ch = *s; |
| 7152 | Py_UNICODE x = 0xfffe; /* illegal value */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7153 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7154 | if (ch < maplen) |
| 7155 | x = mapstring[ch]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7156 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7157 | if (x == 0xfffe) { |
| 7158 | /* undefined mapping */ |
| 7159 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 7160 | startinpos = s-starts; |
| 7161 | endinpos = startinpos+1; |
| 7162 | if (unicode_decode_call_errorhandler( |
| 7163 | errors, &errorHandler, |
| 7164 | "charmap", "character maps to <undefined>", |
| 7165 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 7166 | &v, &outpos, &p)) { |
| 7167 | goto onError; |
| 7168 | } |
| 7169 | continue; |
| 7170 | } |
| 7171 | *p++ = x; |
| 7172 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7173 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7174 | } |
| 7175 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7176 | while (s < e) { |
| 7177 | unsigned char ch = *s; |
| 7178 | PyObject *w, *x; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7179 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7180 | /* Get mapping (char ordinal -> integer, Unicode char or None) */ |
| 7181 | w = PyLong_FromLong((long)ch); |
| 7182 | if (w == NULL) |
| 7183 | goto onError; |
| 7184 | x = PyObject_GetItem(mapping, w); |
| 7185 | Py_DECREF(w); |
| 7186 | if (x == NULL) { |
| 7187 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 7188 | /* No mapping found means: mapping is undefined. */ |
| 7189 | PyErr_Clear(); |
| 7190 | x = Py_None; |
| 7191 | Py_INCREF(x); |
| 7192 | } else |
| 7193 | goto onError; |
| 7194 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7195 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7196 | /* Apply mapping */ |
| 7197 | if (PyLong_Check(x)) { |
| 7198 | long value = PyLong_AS_LONG(x); |
| 7199 | if (value < 0 || value > 65535) { |
| 7200 | PyErr_SetString(PyExc_TypeError, |
| 7201 | "character mapping must be in range(65536)"); |
| 7202 | Py_DECREF(x); |
| 7203 | goto onError; |
| 7204 | } |
| 7205 | *p++ = (Py_UNICODE)value; |
| 7206 | } |
| 7207 | else if (x == Py_None) { |
| 7208 | /* undefined mapping */ |
| 7209 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 7210 | startinpos = s-starts; |
| 7211 | endinpos = startinpos+1; |
| 7212 | if (unicode_decode_call_errorhandler( |
| 7213 | errors, &errorHandler, |
| 7214 | "charmap", "character maps to <undefined>", |
| 7215 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 7216 | &v, &outpos, &p)) { |
| 7217 | Py_DECREF(x); |
| 7218 | goto onError; |
| 7219 | } |
| 7220 | Py_DECREF(x); |
| 7221 | continue; |
| 7222 | } |
| 7223 | else if (PyUnicode_Check(x)) { |
| 7224 | Py_ssize_t targetsize = PyUnicode_GET_SIZE(x); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7225 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7226 | if (targetsize == 1) |
| 7227 | /* 1-1 mapping */ |
| 7228 | *p++ = *PyUnicode_AS_UNICODE(x); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7229 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7230 | else if (targetsize > 1) { |
| 7231 | /* 1-n mapping */ |
| 7232 | if (targetsize > extrachars) { |
| 7233 | /* resize first */ |
| 7234 | Py_ssize_t oldpos = p - PyUnicode_AS_UNICODE(v); |
| 7235 | Py_ssize_t needed = (targetsize - extrachars) + \ |
| 7236 | (targetsize << 2); |
| 7237 | extrachars += needed; |
| 7238 | /* XXX overflow detection missing */ |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 7239 | if (PyUnicode_Resize((PyObject**)&v, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7240 | PyUnicode_GET_SIZE(v) + needed) < 0) { |
| 7241 | Py_DECREF(x); |
| 7242 | goto onError; |
| 7243 | } |
| 7244 | p = PyUnicode_AS_UNICODE(v) + oldpos; |
| 7245 | } |
| 7246 | Py_UNICODE_COPY(p, |
| 7247 | PyUnicode_AS_UNICODE(x), |
| 7248 | targetsize); |
| 7249 | p += targetsize; |
| 7250 | extrachars -= targetsize; |
| 7251 | } |
| 7252 | /* 1-0 mapping: skip the character */ |
| 7253 | } |
| 7254 | else { |
| 7255 | /* wrong return value */ |
| 7256 | PyErr_SetString(PyExc_TypeError, |
| 7257 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7258 | Py_DECREF(x); |
| 7259 | goto onError; |
| 7260 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7261 | Py_DECREF(x); |
| 7262 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7263 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7264 | } |
| 7265 | if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 7266 | if (PyUnicode_Resize((PyObject**)&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7267 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7268 | Py_XDECREF(errorHandler); |
| 7269 | Py_XDECREF(exc); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 7270 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 7271 | if (_PyUnicode_READY_REPLACE(&v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7272 | Py_DECREF(v); |
| 7273 | return NULL; |
| 7274 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 7275 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 7276 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7277 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7278 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7279 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7280 | Py_XDECREF(errorHandler); |
| 7281 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7282 | Py_XDECREF(v); |
| 7283 | return NULL; |
| 7284 | } |
| 7285 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7286 | /* Charmap encoding: the lookup table */ |
| 7287 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7288 | struct encoding_map { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7289 | PyObject_HEAD |
| 7290 | unsigned char level1[32]; |
| 7291 | int count2, count3; |
| 7292 | unsigned char level23[1]; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7293 | }; |
| 7294 | |
| 7295 | static PyObject* |
| 7296 | encoding_map_size(PyObject *obj, PyObject* args) |
| 7297 | { |
| 7298 | struct encoding_map *map = (struct encoding_map*)obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7299 | return PyLong_FromLong(sizeof(*map) - 1 + 16*map->count2 + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7300 | 128*map->count3); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7301 | } |
| 7302 | |
| 7303 | static PyMethodDef encoding_map_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7304 | {"size", encoding_map_size, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7305 | PyDoc_STR("Return the size (in bytes) of this object") }, |
| 7306 | { 0 } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7307 | }; |
| 7308 | |
| 7309 | static void |
| 7310 | encoding_map_dealloc(PyObject* o) |
| 7311 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7312 | PyObject_FREE(o); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7313 | } |
| 7314 | |
| 7315 | static PyTypeObject EncodingMapType = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7316 | PyVarObject_HEAD_INIT(NULL, 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7317 | "EncodingMap", /*tp_name*/ |
| 7318 | sizeof(struct encoding_map), /*tp_basicsize*/ |
| 7319 | 0, /*tp_itemsize*/ |
| 7320 | /* methods */ |
| 7321 | encoding_map_dealloc, /*tp_dealloc*/ |
| 7322 | 0, /*tp_print*/ |
| 7323 | 0, /*tp_getattr*/ |
| 7324 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 7325 | 0, /*tp_reserved*/ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7326 | 0, /*tp_repr*/ |
| 7327 | 0, /*tp_as_number*/ |
| 7328 | 0, /*tp_as_sequence*/ |
| 7329 | 0, /*tp_as_mapping*/ |
| 7330 | 0, /*tp_hash*/ |
| 7331 | 0, /*tp_call*/ |
| 7332 | 0, /*tp_str*/ |
| 7333 | 0, /*tp_getattro*/ |
| 7334 | 0, /*tp_setattro*/ |
| 7335 | 0, /*tp_as_buffer*/ |
| 7336 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 7337 | 0, /*tp_doc*/ |
| 7338 | 0, /*tp_traverse*/ |
| 7339 | 0, /*tp_clear*/ |
| 7340 | 0, /*tp_richcompare*/ |
| 7341 | 0, /*tp_weaklistoffset*/ |
| 7342 | 0, /*tp_iter*/ |
| 7343 | 0, /*tp_iternext*/ |
| 7344 | encoding_map_methods, /*tp_methods*/ |
| 7345 | 0, /*tp_members*/ |
| 7346 | 0, /*tp_getset*/ |
| 7347 | 0, /*tp_base*/ |
| 7348 | 0, /*tp_dict*/ |
| 7349 | 0, /*tp_descr_get*/ |
| 7350 | 0, /*tp_descr_set*/ |
| 7351 | 0, /*tp_dictoffset*/ |
| 7352 | 0, /*tp_init*/ |
| 7353 | 0, /*tp_alloc*/ |
| 7354 | 0, /*tp_new*/ |
| 7355 | 0, /*tp_free*/ |
| 7356 | 0, /*tp_is_gc*/ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7357 | }; |
| 7358 | |
| 7359 | PyObject* |
| 7360 | PyUnicode_BuildEncodingMap(PyObject* string) |
| 7361 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7362 | PyObject *result; |
| 7363 | struct encoding_map *mresult; |
| 7364 | int i; |
| 7365 | int need_dict = 0; |
| 7366 | unsigned char level1[32]; |
| 7367 | unsigned char level2[512]; |
| 7368 | unsigned char *mlevel1, *mlevel2, *mlevel3; |
| 7369 | int count2 = 0, count3 = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7370 | int kind; |
| 7371 | void *data; |
| 7372 | Py_UCS4 ch; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7373 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7374 | if (!PyUnicode_Check(string) || PyUnicode_GET_LENGTH(string) != 256) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7375 | PyErr_BadArgument(); |
| 7376 | return NULL; |
| 7377 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7378 | kind = PyUnicode_KIND(string); |
| 7379 | data = PyUnicode_DATA(string); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7380 | memset(level1, 0xFF, sizeof level1); |
| 7381 | memset(level2, 0xFF, sizeof level2); |
| 7382 | |
| 7383 | /* If there isn't a one-to-one mapping of NULL to \0, |
| 7384 | or if there are non-BMP characters, we need to use |
| 7385 | a mapping dictionary. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7386 | if (PyUnicode_READ(kind, data, 0) != 0) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7387 | need_dict = 1; |
| 7388 | for (i = 1; i < 256; i++) { |
| 7389 | int l1, l2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7390 | ch = PyUnicode_READ(kind, data, i); |
| 7391 | if (ch == 0 || ch > 0xFFFF) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7392 | need_dict = 1; |
| 7393 | break; |
| 7394 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7395 | if (ch == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7396 | /* unmapped character */ |
| 7397 | continue; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7398 | l1 = ch >> 11; |
| 7399 | l2 = ch >> 7; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7400 | if (level1[l1] == 0xFF) |
| 7401 | level1[l1] = count2++; |
| 7402 | if (level2[l2] == 0xFF) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7403 | level2[l2] = count3++; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7404 | } |
| 7405 | |
| 7406 | if (count2 >= 0xFF || count3 >= 0xFF) |
| 7407 | need_dict = 1; |
| 7408 | |
| 7409 | if (need_dict) { |
| 7410 | PyObject *result = PyDict_New(); |
| 7411 | PyObject *key, *value; |
| 7412 | if (!result) |
| 7413 | return NULL; |
| 7414 | for (i = 0; i < 256; i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7415 | key = PyLong_FromLong(PyUnicode_READ(kind, data, i)); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7416 | value = PyLong_FromLong(i); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7417 | if (!key || !value) |
| 7418 | goto failed1; |
| 7419 | if (PyDict_SetItem(result, key, value) == -1) |
| 7420 | goto failed1; |
| 7421 | Py_DECREF(key); |
| 7422 | Py_DECREF(value); |
| 7423 | } |
| 7424 | return result; |
| 7425 | failed1: |
| 7426 | Py_XDECREF(key); |
| 7427 | Py_XDECREF(value); |
| 7428 | Py_DECREF(result); |
| 7429 | return NULL; |
| 7430 | } |
| 7431 | |
| 7432 | /* Create a three-level trie */ |
| 7433 | result = PyObject_MALLOC(sizeof(struct encoding_map) + |
| 7434 | 16*count2 + 128*count3 - 1); |
| 7435 | if (!result) |
| 7436 | return PyErr_NoMemory(); |
| 7437 | PyObject_Init(result, &EncodingMapType); |
| 7438 | mresult = (struct encoding_map*)result; |
| 7439 | mresult->count2 = count2; |
| 7440 | mresult->count3 = count3; |
| 7441 | mlevel1 = mresult->level1; |
| 7442 | mlevel2 = mresult->level23; |
| 7443 | mlevel3 = mresult->level23 + 16*count2; |
| 7444 | memcpy(mlevel1, level1, 32); |
| 7445 | memset(mlevel2, 0xFF, 16*count2); |
| 7446 | memset(mlevel3, 0, 128*count3); |
| 7447 | count3 = 0; |
| 7448 | for (i = 1; i < 256; i++) { |
| 7449 | int o1, o2, o3, i2, i3; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7450 | if (PyUnicode_READ(kind, data, i) == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7451 | /* unmapped character */ |
| 7452 | continue; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7453 | o1 = PyUnicode_READ(kind, data, i)>>11; |
| 7454 | o2 = (PyUnicode_READ(kind, data, i)>>7) & 0xF; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7455 | i2 = 16*mlevel1[o1] + o2; |
| 7456 | if (mlevel2[i2] == 0xFF) |
| 7457 | mlevel2[i2] = count3++; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7458 | o3 = PyUnicode_READ(kind, data, i) & 0x7F; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7459 | i3 = 128*mlevel2[i2] + o3; |
| 7460 | mlevel3[i3] = i; |
| 7461 | } |
| 7462 | return result; |
| 7463 | } |
| 7464 | |
| 7465 | static int |
| 7466 | encoding_map_lookup(Py_UNICODE c, PyObject *mapping) |
| 7467 | { |
| 7468 | struct encoding_map *map = (struct encoding_map*)mapping; |
| 7469 | int l1 = c>>11; |
| 7470 | int l2 = (c>>7) & 0xF; |
| 7471 | int l3 = c & 0x7F; |
| 7472 | int i; |
| 7473 | |
| 7474 | #ifdef Py_UNICODE_WIDE |
| 7475 | if (c > 0xFFFF) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7476 | return -1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7477 | } |
| 7478 | #endif |
| 7479 | if (c == 0) |
| 7480 | return 0; |
| 7481 | /* level 1*/ |
| 7482 | i = map->level1[l1]; |
| 7483 | if (i == 0xFF) { |
| 7484 | return -1; |
| 7485 | } |
| 7486 | /* level 2*/ |
| 7487 | i = map->level23[16*i+l2]; |
| 7488 | if (i == 0xFF) { |
| 7489 | return -1; |
| 7490 | } |
| 7491 | /* level 3 */ |
| 7492 | i = map->level23[16*map->count2 + 128*i + l3]; |
| 7493 | if (i == 0) { |
| 7494 | return -1; |
| 7495 | } |
| 7496 | return i; |
| 7497 | } |
| 7498 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7499 | /* Lookup the character ch in the mapping. If the character |
| 7500 | can't be found, Py_None is returned (or NULL, if another |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 7501 | error occurred). */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7502 | static PyObject * |
| 7503 | charmapencode_lookup(Py_UNICODE c, PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7504 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7505 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7506 | PyObject *x; |
| 7507 | |
| 7508 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7509 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7510 | x = PyObject_GetItem(mapping, w); |
| 7511 | Py_DECREF(w); |
| 7512 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7513 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 7514 | /* No mapping found means: mapping is undefined. */ |
| 7515 | PyErr_Clear(); |
| 7516 | x = Py_None; |
| 7517 | Py_INCREF(x); |
| 7518 | return x; |
| 7519 | } else |
| 7520 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7521 | } |
Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 7522 | else if (x == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7523 | return x; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7524 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7525 | long value = PyLong_AS_LONG(x); |
| 7526 | if (value < 0 || value > 255) { |
| 7527 | PyErr_SetString(PyExc_TypeError, |
| 7528 | "character mapping must be in range(256)"); |
| 7529 | Py_DECREF(x); |
| 7530 | return NULL; |
| 7531 | } |
| 7532 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7533 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7534 | else if (PyBytes_Check(x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7535 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7536 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7537 | /* wrong return value */ |
| 7538 | PyErr_Format(PyExc_TypeError, |
| 7539 | "character mapping must return integer, bytes or None, not %.400s", |
| 7540 | x->ob_type->tp_name); |
| 7541 | Py_DECREF(x); |
| 7542 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7543 | } |
| 7544 | } |
| 7545 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7546 | static int |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 7547 | charmapencode_resize(PyObject **outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7548 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7549 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
| 7550 | /* exponentially overallocate to minimize reallocations */ |
| 7551 | if (requiredsize < 2*outsize) |
| 7552 | requiredsize = 2*outsize; |
| 7553 | if (_PyBytes_Resize(outobj, requiredsize)) |
| 7554 | return -1; |
| 7555 | return 0; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7556 | } |
| 7557 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7558 | typedef enum charmapencode_result { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7559 | enc_SUCCESS, enc_FAILED, enc_EXCEPTION |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7560 | } charmapencode_result; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7561 | /* 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] | 7562 | various state variables. Resize the output bytes object if not enough |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7563 | space is available. Return a new reference to the object that |
| 7564 | was put in the output buffer, or Py_None, if the mapping was undefined |
| 7565 | (in which case no character was written) or NULL, if a |
Andrew M. Kuchling | 8294de5 | 2005-11-02 16:36:12 +0000 | [diff] [blame] | 7566 | reallocation error occurred. The caller must decref the result */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7567 | static charmapencode_result |
| 7568 | charmapencode_output(Py_UNICODE c, PyObject *mapping, |
| 7569 | PyObject **outobj, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7570 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7571 | PyObject *rep; |
| 7572 | char *outstart; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7573 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7574 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 7575 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7576 | int res = encoding_map_lookup(c, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7577 | Py_ssize_t requiredsize = *outpos+1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7578 | if (res == -1) |
| 7579 | return enc_FAILED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7580 | if (outsize<requiredsize) |
| 7581 | if (charmapencode_resize(outobj, outpos, requiredsize)) |
| 7582 | return enc_EXCEPTION; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7583 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7584 | outstart[(*outpos)++] = (char)res; |
| 7585 | return enc_SUCCESS; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7586 | } |
| 7587 | |
| 7588 | rep = charmapencode_lookup(c, mapping); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7589 | if (rep==NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7590 | return enc_EXCEPTION; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7591 | else if (rep==Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7592 | Py_DECREF(rep); |
| 7593 | return enc_FAILED; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7594 | } else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7595 | if (PyLong_Check(rep)) { |
| 7596 | Py_ssize_t requiredsize = *outpos+1; |
| 7597 | if (outsize<requiredsize) |
| 7598 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 7599 | Py_DECREF(rep); |
| 7600 | return enc_EXCEPTION; |
| 7601 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7602 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7603 | outstart[(*outpos)++] = (char)PyLong_AS_LONG(rep); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7604 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7605 | else { |
| 7606 | const char *repchars = PyBytes_AS_STRING(rep); |
| 7607 | Py_ssize_t repsize = PyBytes_GET_SIZE(rep); |
| 7608 | Py_ssize_t requiredsize = *outpos+repsize; |
| 7609 | if (outsize<requiredsize) |
| 7610 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 7611 | Py_DECREF(rep); |
| 7612 | return enc_EXCEPTION; |
| 7613 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7614 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7615 | memcpy(outstart + *outpos, repchars, repsize); |
| 7616 | *outpos += repsize; |
| 7617 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7618 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7619 | Py_DECREF(rep); |
| 7620 | return enc_SUCCESS; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7621 | } |
| 7622 | |
| 7623 | /* handle an error in PyUnicode_EncodeCharmap |
| 7624 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7625 | static int |
| 7626 | charmap_encoding_error( |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7627 | 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] | 7628 | PyObject **exceptionObject, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 7629 | int *known_errorHandler, PyObject **errorHandler, const char *errors, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 7630 | PyObject **res, Py_ssize_t *respos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7631 | { |
| 7632 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7633 | Py_ssize_t repsize; |
| 7634 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7635 | Py_UNICODE *uni2; |
| 7636 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7637 | Py_ssize_t collstartpos = *inpos; |
| 7638 | Py_ssize_t collendpos = *inpos+1; |
| 7639 | Py_ssize_t collpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7640 | char *encoding = "charmap"; |
| 7641 | char *reason = "character maps to <undefined>"; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7642 | charmapencode_result x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7643 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7644 | /* find all unencodable characters */ |
| 7645 | while (collendpos < size) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7646 | PyObject *rep; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 7647 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7648 | int res = encoding_map_lookup(p[collendpos], mapping); |
| 7649 | if (res != -1) |
| 7650 | break; |
| 7651 | ++collendpos; |
| 7652 | continue; |
| 7653 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7654 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7655 | rep = charmapencode_lookup(p[collendpos], mapping); |
| 7656 | if (rep==NULL) |
| 7657 | return -1; |
| 7658 | else if (rep!=Py_None) { |
| 7659 | Py_DECREF(rep); |
| 7660 | break; |
| 7661 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7662 | Py_DECREF(rep); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7663 | ++collendpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7664 | } |
| 7665 | /* cache callback name lookup |
| 7666 | * (if not done yet, i.e. it's the first error) */ |
| 7667 | if (*known_errorHandler==-1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7668 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 7669 | *known_errorHandler = 1; |
| 7670 | else if (!strcmp(errors, "replace")) |
| 7671 | *known_errorHandler = 2; |
| 7672 | else if (!strcmp(errors, "ignore")) |
| 7673 | *known_errorHandler = 3; |
| 7674 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 7675 | *known_errorHandler = 4; |
| 7676 | else |
| 7677 | *known_errorHandler = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7678 | } |
| 7679 | switch (*known_errorHandler) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7680 | case 1: /* strict */ |
| 7681 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 7682 | return -1; |
| 7683 | case 2: /* replace */ |
| 7684 | for (collpos = collstartpos; collpos<collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7685 | x = charmapencode_output('?', mapping, res, respos); |
| 7686 | if (x==enc_EXCEPTION) { |
| 7687 | return -1; |
| 7688 | } |
| 7689 | else if (x==enc_FAILED) { |
| 7690 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 7691 | return -1; |
| 7692 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7693 | } |
| 7694 | /* fall through */ |
| 7695 | case 3: /* ignore */ |
| 7696 | *inpos = collendpos; |
| 7697 | break; |
| 7698 | case 4: /* xmlcharrefreplace */ |
| 7699 | /* generate replacement (temporarily (mis)uses p) */ |
| 7700 | for (collpos = collstartpos; collpos < collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7701 | char buffer[2+29+1+1]; |
| 7702 | char *cp; |
| 7703 | sprintf(buffer, "&#%d;", (int)p[collpos]); |
| 7704 | for (cp = buffer; *cp; ++cp) { |
| 7705 | x = charmapencode_output(*cp, mapping, res, respos); |
| 7706 | if (x==enc_EXCEPTION) |
| 7707 | return -1; |
| 7708 | else if (x==enc_FAILED) { |
| 7709 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 7710 | return -1; |
| 7711 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7712 | } |
| 7713 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7714 | *inpos = collendpos; |
| 7715 | break; |
| 7716 | default: |
| 7717 | repunicode = unicode_encode_call_errorhandler(errors, errorHandler, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7718 | encoding, reason, p, size, exceptionObject, |
| 7719 | collstartpos, collendpos, &newpos); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7720 | if (repunicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7721 | return -1; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 7722 | if (PyBytes_Check(repunicode)) { |
| 7723 | /* Directly copy bytes result to output. */ |
| 7724 | Py_ssize_t outsize = PyBytes_Size(*res); |
| 7725 | Py_ssize_t requiredsize; |
| 7726 | repsize = PyBytes_Size(repunicode); |
| 7727 | requiredsize = *respos + repsize; |
| 7728 | if (requiredsize > outsize) |
| 7729 | /* Make room for all additional bytes. */ |
| 7730 | if (charmapencode_resize(res, respos, requiredsize)) { |
| 7731 | Py_DECREF(repunicode); |
| 7732 | return -1; |
| 7733 | } |
| 7734 | memcpy(PyBytes_AsString(*res) + *respos, |
| 7735 | PyBytes_AsString(repunicode), repsize); |
| 7736 | *respos += repsize; |
| 7737 | *inpos = newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 7738 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 7739 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 7740 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7741 | /* generate replacement */ |
| 7742 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 7743 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7744 | x = charmapencode_output(*uni2, mapping, res, respos); |
| 7745 | if (x==enc_EXCEPTION) { |
| 7746 | return -1; |
| 7747 | } |
| 7748 | else if (x==enc_FAILED) { |
| 7749 | Py_DECREF(repunicode); |
| 7750 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 7751 | return -1; |
| 7752 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7753 | } |
| 7754 | *inpos = newpos; |
| 7755 | Py_DECREF(repunicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7756 | } |
| 7757 | return 0; |
| 7758 | } |
| 7759 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7760 | PyObject * |
| 7761 | PyUnicode_EncodeCharmap(const Py_UNICODE *p, |
| 7762 | Py_ssize_t size, |
| 7763 | PyObject *mapping, |
| 7764 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7765 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7766 | /* output object */ |
| 7767 | PyObject *res = NULL; |
| 7768 | /* current input position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7769 | Py_ssize_t inpos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7770 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7771 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7772 | PyObject *errorHandler = NULL; |
| 7773 | PyObject *exc = NULL; |
| 7774 | /* the following variable is used for caching string comparisons |
| 7775 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 7776 | * 3=ignore, 4=xmlcharrefreplace */ |
| 7777 | int known_errorHandler = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7778 | |
| 7779 | /* Default to Latin-1 */ |
| 7780 | if (mapping == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7781 | return PyUnicode_EncodeLatin1(p, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7782 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7783 | /* allocate enough for a simple encoding without |
| 7784 | replacements, if we need more, we'll resize */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7785 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7786 | if (res == NULL) |
| 7787 | goto onError; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 7788 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7789 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7790 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7791 | while (inpos<size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7792 | /* try to encode it */ |
| 7793 | charmapencode_result x = charmapencode_output(p[inpos], mapping, &res, &respos); |
| 7794 | if (x==enc_EXCEPTION) /* error */ |
| 7795 | goto onError; |
| 7796 | if (x==enc_FAILED) { /* unencodable character */ |
| 7797 | if (charmap_encoding_error(p, size, &inpos, mapping, |
| 7798 | &exc, |
| 7799 | &known_errorHandler, &errorHandler, errors, |
| 7800 | &res, &respos)) { |
| 7801 | goto onError; |
| 7802 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7803 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7804 | else |
| 7805 | /* done with this character => adjust input position */ |
| 7806 | ++inpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7807 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7808 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7809 | /* Resize if we allocated to much */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7810 | if (respos<PyBytes_GET_SIZE(res)) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 7811 | if (_PyBytes_Resize(&res, respos) < 0) |
| 7812 | goto onError; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 7813 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7814 | Py_XDECREF(exc); |
| 7815 | Py_XDECREF(errorHandler); |
| 7816 | return res; |
| 7817 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7818 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7819 | Py_XDECREF(res); |
| 7820 | Py_XDECREF(exc); |
| 7821 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7822 | return NULL; |
| 7823 | } |
| 7824 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7825 | PyObject * |
| 7826 | PyUnicode_AsCharmapString(PyObject *unicode, |
| 7827 | PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7828 | { |
| 7829 | if (!PyUnicode_Check(unicode) || mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7830 | PyErr_BadArgument(); |
| 7831 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7832 | } |
| 7833 | return PyUnicode_EncodeCharmap(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7834 | PyUnicode_GET_SIZE(unicode), |
| 7835 | mapping, |
| 7836 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7837 | } |
| 7838 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7839 | /* create or adjust a UnicodeTranslateError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7840 | static void |
| 7841 | make_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7842 | PyObject *unicode, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7843 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 7844 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7845 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7846 | if (*exceptionObject == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7847 | *exceptionObject = _PyUnicodeTranslateError_Create( |
| 7848 | unicode, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7849 | } |
| 7850 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7851 | if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
| 7852 | goto onError; |
| 7853 | if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) |
| 7854 | goto onError; |
| 7855 | if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) |
| 7856 | goto onError; |
| 7857 | return; |
| 7858 | onError: |
| 7859 | Py_DECREF(*exceptionObject); |
| 7860 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7861 | } |
| 7862 | } |
| 7863 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7864 | /* raises a UnicodeTranslateError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7865 | static void |
| 7866 | raise_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7867 | PyObject *unicode, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7868 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 7869 | const char *reason) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7870 | { |
| 7871 | make_translate_exception(exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7872 | unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7873 | if (*exceptionObject != NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7874 | PyCodec_StrictErrors(*exceptionObject); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7875 | } |
| 7876 | |
| 7877 | /* error handling callback helper: |
| 7878 | build arguments, call the callback and check the arguments, |
| 7879 | put the result into newpos and return the replacement string, which |
| 7880 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7881 | static PyObject * |
| 7882 | unicode_translate_call_errorhandler(const char *errors, |
| 7883 | PyObject **errorHandler, |
| 7884 | const char *reason, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7885 | PyObject *unicode, PyObject **exceptionObject, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7886 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 7887 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7888 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 7889 | 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] | 7890 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7891 | Py_ssize_t i_newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7892 | PyObject *restuple; |
| 7893 | PyObject *resunicode; |
| 7894 | |
| 7895 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7896 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7897 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7898 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7899 | } |
| 7900 | |
| 7901 | make_translate_exception(exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7902 | unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7903 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7904 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7905 | |
| 7906 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7907 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7908 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7909 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7910 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 7911 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7912 | Py_DECREF(restuple); |
| 7913 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7914 | } |
| 7915 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7916 | &resunicode, &i_newpos)) { |
| 7917 | Py_DECREF(restuple); |
| 7918 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7919 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7920 | if (i_newpos<0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7921 | *newpos = PyUnicode_GET_LENGTH(unicode)+i_newpos; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7922 | else |
| 7923 | *newpos = i_newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7924 | if (*newpos<0 || *newpos>PyUnicode_GET_LENGTH(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7925 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 7926 | Py_DECREF(restuple); |
| 7927 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 7928 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7929 | Py_INCREF(resunicode); |
| 7930 | Py_DECREF(restuple); |
| 7931 | return resunicode; |
| 7932 | } |
| 7933 | |
| 7934 | /* Lookup the character ch in the mapping and put the result in result, |
| 7935 | which must be decrefed by the caller. |
| 7936 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7937 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7938 | charmaptranslate_lookup(Py_UCS4 c, PyObject *mapping, PyObject **result) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7939 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7940 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7941 | PyObject *x; |
| 7942 | |
| 7943 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7944 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7945 | x = PyObject_GetItem(mapping, w); |
| 7946 | Py_DECREF(w); |
| 7947 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7948 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 7949 | /* No mapping found means: use 1:1 mapping. */ |
| 7950 | PyErr_Clear(); |
| 7951 | *result = NULL; |
| 7952 | return 0; |
| 7953 | } else |
| 7954 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7955 | } |
| 7956 | else if (x == Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7957 | *result = x; |
| 7958 | return 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7959 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7960 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7961 | long value = PyLong_AS_LONG(x); |
| 7962 | long max = PyUnicode_GetMax(); |
| 7963 | if (value < 0 || value > max) { |
| 7964 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5a2f7e60 | 2007-10-24 21:13:09 +0000 | [diff] [blame] | 7965 | "character mapping must be in range(0x%x)", max+1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7966 | Py_DECREF(x); |
| 7967 | return -1; |
| 7968 | } |
| 7969 | *result = x; |
| 7970 | return 0; |
| 7971 | } |
| 7972 | else if (PyUnicode_Check(x)) { |
| 7973 | *result = x; |
| 7974 | return 0; |
| 7975 | } |
| 7976 | else { |
| 7977 | /* wrong return value */ |
| 7978 | PyErr_SetString(PyExc_TypeError, |
| 7979 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7980 | Py_DECREF(x); |
| 7981 | return -1; |
| 7982 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7983 | } |
| 7984 | /* ensure that *outobj is at least requiredsize characters long, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7985 | if not reallocate and adjust various state variables. |
| 7986 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7987 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7988 | charmaptranslate_makespace(Py_UCS4 **outobj, Py_ssize_t *psize, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7989 | Py_ssize_t requiredsize) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7990 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7991 | Py_ssize_t oldsize = *psize; |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 7992 | if (requiredsize > oldsize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7993 | /* exponentially overallocate to minimize reallocations */ |
| 7994 | if (requiredsize < 2 * oldsize) |
| 7995 | requiredsize = 2 * oldsize; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7996 | *outobj = PyMem_Realloc(*outobj, requiredsize * sizeof(Py_UCS4)); |
| 7997 | if (*outobj == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7998 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7999 | *psize = requiredsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8000 | } |
| 8001 | return 0; |
| 8002 | } |
| 8003 | /* lookup the character, put the result in the output string and adjust |
| 8004 | various state variables. Return a new reference to the object that |
| 8005 | was put in the output buffer in *result, or Py_None, if the mapping was |
| 8006 | undefined (in which case no character was written). |
| 8007 | The called must decref result. |
| 8008 | Return 0 on success, -1 on error. */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8009 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8010 | charmaptranslate_output(PyObject *input, Py_ssize_t ipos, |
| 8011 | PyObject *mapping, Py_UCS4 **output, |
| 8012 | Py_ssize_t *osize, Py_ssize_t *opos, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8013 | PyObject **res) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8014 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8015 | Py_UCS4 curinp = PyUnicode_READ_CHAR(input, ipos); |
| 8016 | if (charmaptranslate_lookup(curinp, mapping, res)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8017 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8018 | if (*res==NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8019 | /* not found => default to 1:1 mapping */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8020 | (*output)[(*opos)++] = curinp; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8021 | } |
| 8022 | else if (*res==Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8023 | ; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8024 | else if (PyLong_Check(*res)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8025 | /* 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] | 8026 | (*output)[(*opos)++] = (Py_UCS4)PyLong_AS_LONG(*res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8027 | } |
| 8028 | else if (PyUnicode_Check(*res)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8029 | Py_ssize_t repsize; |
| 8030 | if (PyUnicode_READY(*res) == -1) |
| 8031 | return -1; |
| 8032 | repsize = PyUnicode_GET_LENGTH(*res); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8033 | if (repsize==1) { |
| 8034 | /* 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] | 8035 | (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8036 | } |
| 8037 | else if (repsize!=0) { |
| 8038 | /* more than one character */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8039 | Py_ssize_t requiredsize = *opos + |
| 8040 | (PyUnicode_GET_LENGTH(input) - ipos) + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8041 | repsize - 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8042 | Py_ssize_t i; |
| 8043 | if (charmaptranslate_makespace(output, osize, requiredsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8044 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8045 | for(i = 0; i < repsize; i++) |
| 8046 | (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, i); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8047 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8048 | } |
| 8049 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8050 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8051 | return 0; |
| 8052 | } |
| 8053 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8054 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8055 | _PyUnicode_TranslateCharmap(PyObject *input, |
| 8056 | PyObject *mapping, |
| 8057 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8058 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8059 | /* input object */ |
| 8060 | char *idata; |
| 8061 | Py_ssize_t size, i; |
| 8062 | int kind; |
| 8063 | /* output buffer */ |
| 8064 | Py_UCS4 *output = NULL; |
| 8065 | Py_ssize_t osize; |
| 8066 | PyObject *res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8067 | /* current output position */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8068 | Py_ssize_t opos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8069 | char *reason = "character maps to <undefined>"; |
| 8070 | PyObject *errorHandler = NULL; |
| 8071 | PyObject *exc = NULL; |
| 8072 | /* the following variable is used for caching string comparisons |
| 8073 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 8074 | * 3=ignore, 4=xmlcharrefreplace */ |
| 8075 | int known_errorHandler = -1; |
| 8076 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8077 | if (mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8078 | PyErr_BadArgument(); |
| 8079 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8080 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8081 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8082 | if (PyUnicode_READY(input) == -1) |
| 8083 | return NULL; |
| 8084 | idata = (char*)PyUnicode_DATA(input); |
| 8085 | kind = PyUnicode_KIND(input); |
| 8086 | size = PyUnicode_GET_LENGTH(input); |
| 8087 | i = 0; |
| 8088 | |
| 8089 | if (size == 0) { |
| 8090 | Py_INCREF(input); |
| 8091 | return input; |
| 8092 | } |
| 8093 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8094 | /* allocate enough for a simple 1:1 translation without |
| 8095 | replacements, if we need more, we'll resize */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8096 | osize = size; |
| 8097 | output = PyMem_Malloc(osize * sizeof(Py_UCS4)); |
| 8098 | opos = 0; |
| 8099 | if (output == NULL) { |
| 8100 | PyErr_NoMemory(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8101 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8102 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8103 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8104 | while (i<size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8105 | /* try to encode it */ |
| 8106 | PyObject *x = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8107 | if (charmaptranslate_output(input, i, mapping, |
| 8108 | &output, &osize, &opos, &x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8109 | Py_XDECREF(x); |
| 8110 | goto onError; |
| 8111 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8112 | Py_XDECREF(x); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8113 | if (x!=Py_None) /* it worked => adjust input pointer */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8114 | ++i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8115 | else { /* untranslatable character */ |
| 8116 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
| 8117 | Py_ssize_t repsize; |
| 8118 | Py_ssize_t newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8119 | Py_ssize_t uni2; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8120 | /* startpos for collecting untranslatable chars */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8121 | Py_ssize_t collstart = i; |
| 8122 | Py_ssize_t collend = i+1; |
| 8123 | Py_ssize_t coll; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8124 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8125 | /* find all untranslatable characters */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8126 | while (collend < size) { |
| 8127 | if (charmaptranslate_lookup(PyUnicode_READ(kind,idata, collend), mapping, &x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8128 | goto onError; |
| 8129 | Py_XDECREF(x); |
| 8130 | if (x!=Py_None) |
| 8131 | break; |
| 8132 | ++collend; |
| 8133 | } |
| 8134 | /* cache callback name lookup |
| 8135 | * (if not done yet, i.e. it's the first error) */ |
| 8136 | if (known_errorHandler==-1) { |
| 8137 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 8138 | known_errorHandler = 1; |
| 8139 | else if (!strcmp(errors, "replace")) |
| 8140 | known_errorHandler = 2; |
| 8141 | else if (!strcmp(errors, "ignore")) |
| 8142 | known_errorHandler = 3; |
| 8143 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 8144 | known_errorHandler = 4; |
| 8145 | else |
| 8146 | known_errorHandler = 0; |
| 8147 | } |
| 8148 | switch (known_errorHandler) { |
| 8149 | case 1: /* strict */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8150 | raise_translate_exception(&exc, input, collstart, |
| 8151 | collend, reason); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8152 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8153 | case 2: /* replace */ |
| 8154 | /* 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] | 8155 | for (coll = collstart; coll<collend; coll++) |
| 8156 | output[opos++] = '?'; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8157 | /* fall through */ |
| 8158 | case 3: /* ignore */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8159 | i = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8160 | break; |
| 8161 | case 4: /* xmlcharrefreplace */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8162 | /* generate replacement (temporarily (mis)uses i) */ |
| 8163 | for (i = collstart; i < collend; ++i) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8164 | char buffer[2+29+1+1]; |
| 8165 | char *cp; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8166 | sprintf(buffer, "&#%d;", PyUnicode_READ(kind, idata, i)); |
| 8167 | if (charmaptranslate_makespace(&output, &osize, |
| 8168 | opos+strlen(buffer)+(size-collend))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8169 | goto onError; |
| 8170 | for (cp = buffer; *cp; ++cp) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8171 | output[opos++] = *cp; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8172 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8173 | i = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8174 | break; |
| 8175 | default: |
| 8176 | repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8177 | reason, input, &exc, |
| 8178 | collstart, collend, &newpos); |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 8179 | if (repunicode == NULL || _PyUnicode_READY_REPLACE(&repunicode)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8180 | goto onError; |
| 8181 | /* generate replacement */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8182 | repsize = PyUnicode_GET_LENGTH(repunicode); |
| 8183 | if (charmaptranslate_makespace(&output, &osize, |
| 8184 | opos+repsize+(size-collend))) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8185 | Py_DECREF(repunicode); |
| 8186 | goto onError; |
| 8187 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8188 | for (uni2 = 0; repsize-->0; ++uni2) |
| 8189 | output[opos++] = PyUnicode_READ_CHAR(repunicode, uni2); |
| 8190 | i = newpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8191 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8192 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8193 | } |
| 8194 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8195 | res = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, output, opos); |
| 8196 | if (!res) |
| 8197 | goto onError; |
| 8198 | PyMem_Free(output); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8199 | Py_XDECREF(exc); |
| 8200 | Py_XDECREF(errorHandler); |
| 8201 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8202 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8203 | onError: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8204 | PyMem_Free(output); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8205 | Py_XDECREF(exc); |
| 8206 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8207 | return NULL; |
| 8208 | } |
| 8209 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8210 | /* Deprecated. Use PyUnicode_Translate instead. */ |
| 8211 | PyObject * |
| 8212 | PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
| 8213 | Py_ssize_t size, |
| 8214 | PyObject *mapping, |
| 8215 | const char *errors) |
| 8216 | { |
| 8217 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 8218 | if (!unicode) |
| 8219 | return NULL; |
| 8220 | return _PyUnicode_TranslateCharmap(unicode, mapping, errors); |
| 8221 | } |
| 8222 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8223 | PyObject * |
| 8224 | PyUnicode_Translate(PyObject *str, |
| 8225 | PyObject *mapping, |
| 8226 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8227 | { |
| 8228 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8229 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8230 | str = PyUnicode_FromObject(str); |
| 8231 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8232 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8233 | result = _PyUnicode_TranslateCharmap(str, mapping, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8234 | Py_DECREF(str); |
| 8235 | return result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8236 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8237 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8238 | Py_XDECREF(str); |
| 8239 | return NULL; |
| 8240 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8241 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8242 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8243 | fix_decimal_and_space_to_ascii(PyObject *self) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8244 | { |
| 8245 | /* No need to call PyUnicode_READY(self) because this function is only |
| 8246 | called as a callback from fixup() which does it already. */ |
| 8247 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 8248 | const int kind = PyUnicode_KIND(self); |
| 8249 | void *data = PyUnicode_DATA(self); |
| 8250 | Py_UCS4 maxchar = 0, ch, fixed; |
| 8251 | Py_ssize_t i; |
| 8252 | |
| 8253 | for (i = 0; i < len; ++i) { |
| 8254 | ch = PyUnicode_READ(kind, data, i); |
| 8255 | fixed = 0; |
| 8256 | if (ch > 127) { |
| 8257 | if (Py_UNICODE_ISSPACE(ch)) |
| 8258 | fixed = ' '; |
| 8259 | else { |
| 8260 | const int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8261 | if (decimal >= 0) |
| 8262 | fixed = '0' + decimal; |
| 8263 | } |
| 8264 | if (fixed != 0) { |
| 8265 | if (fixed > maxchar) |
| 8266 | maxchar = fixed; |
| 8267 | PyUnicode_WRITE(kind, data, i, fixed); |
| 8268 | } |
| 8269 | else if (ch > maxchar) |
| 8270 | maxchar = ch; |
| 8271 | } |
| 8272 | else if (ch > maxchar) |
| 8273 | maxchar = ch; |
| 8274 | } |
| 8275 | |
| 8276 | return maxchar; |
| 8277 | } |
| 8278 | |
| 8279 | PyObject * |
| 8280 | _PyUnicode_TransformDecimalAndSpaceToASCII(PyObject *unicode) |
| 8281 | { |
| 8282 | if (!PyUnicode_Check(unicode)) { |
| 8283 | PyErr_BadInternalCall(); |
| 8284 | return NULL; |
| 8285 | } |
| 8286 | if (PyUnicode_READY(unicode) == -1) |
| 8287 | return NULL; |
| 8288 | if (PyUnicode_MAX_CHAR_VALUE(unicode) <= 127) { |
| 8289 | /* If the string is already ASCII, just return the same string */ |
| 8290 | Py_INCREF(unicode); |
| 8291 | return unicode; |
| 8292 | } |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8293 | return fixup(unicode, fix_decimal_and_space_to_ascii); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8294 | } |
| 8295 | |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8296 | PyObject * |
| 8297 | PyUnicode_TransformDecimalToASCII(Py_UNICODE *s, |
| 8298 | Py_ssize_t length) |
| 8299 | { |
| 8300 | PyObject *result; |
| 8301 | Py_UNICODE *p; /* write pointer into result */ |
| 8302 | Py_ssize_t i; |
| 8303 | /* Copy to a new string */ |
| 8304 | result = (PyObject *)_PyUnicode_New(length); |
| 8305 | Py_UNICODE_COPY(PyUnicode_AS_UNICODE(result), s, length); |
| 8306 | if (result == NULL) |
| 8307 | return result; |
| 8308 | p = PyUnicode_AS_UNICODE(result); |
| 8309 | /* Iterate over code points */ |
| 8310 | for (i = 0; i < length; i++) { |
| 8311 | Py_UNICODE ch =s[i]; |
| 8312 | if (ch > 127) { |
| 8313 | int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8314 | if (decimal >= 0) |
| 8315 | p[i] = '0' + decimal; |
| 8316 | } |
| 8317 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 8318 | #ifndef DONT_MAKE_RESULT_READY |
| 8319 | if (_PyUnicode_READY_REPLACE(&result)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8320 | Py_DECREF(result); |
| 8321 | return NULL; |
| 8322 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 8323 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 8324 | assert(_PyUnicode_CheckConsistency(result, 1)); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8325 | return result; |
| 8326 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8327 | /* --- Decimal Encoder ---------------------------------------------------- */ |
| 8328 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8329 | int |
| 8330 | PyUnicode_EncodeDecimal(Py_UNICODE *s, |
| 8331 | Py_ssize_t length, |
| 8332 | char *output, |
| 8333 | const char *errors) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8334 | { |
| 8335 | Py_UNICODE *p, *end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8336 | PyObject *errorHandler = NULL; |
| 8337 | PyObject *exc = NULL; |
| 8338 | const char *encoding = "decimal"; |
| 8339 | const char *reason = "invalid decimal Unicode string"; |
| 8340 | /* the following variable is used for caching string comparisons |
| 8341 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 8342 | int known_errorHandler = -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8343 | |
| 8344 | if (output == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8345 | PyErr_BadArgument(); |
| 8346 | return -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8347 | } |
| 8348 | |
| 8349 | p = s; |
| 8350 | end = s + length; |
| 8351 | while (p < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8352 | register Py_UNICODE ch = *p; |
| 8353 | int decimal; |
| 8354 | PyObject *repunicode; |
| 8355 | Py_ssize_t repsize; |
| 8356 | Py_ssize_t newpos; |
| 8357 | Py_UNICODE *uni2; |
| 8358 | Py_UNICODE *collstart; |
| 8359 | Py_UNICODE *collend; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8360 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8361 | if (Py_UNICODE_ISSPACE(ch)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8362 | *output++ = ' '; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8363 | ++p; |
| 8364 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8365 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8366 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 8367 | if (decimal >= 0) { |
| 8368 | *output++ = '0' + decimal; |
| 8369 | ++p; |
| 8370 | continue; |
| 8371 | } |
| 8372 | if (0 < ch && ch < 256) { |
| 8373 | *output++ = (char)ch; |
| 8374 | ++p; |
| 8375 | continue; |
| 8376 | } |
| 8377 | /* All other characters are considered unencodable */ |
| 8378 | collstart = p; |
| 8379 | collend = p+1; |
| 8380 | while (collend < end) { |
| 8381 | if ((0 < *collend && *collend < 256) || |
| 8382 | !Py_UNICODE_ISSPACE(*collend) || |
| 8383 | Py_UNICODE_TODECIMAL(*collend)) |
| 8384 | break; |
| 8385 | } |
| 8386 | /* cache callback name lookup |
| 8387 | * (if not done yet, i.e. it's the first error) */ |
| 8388 | if (known_errorHandler==-1) { |
| 8389 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 8390 | known_errorHandler = 1; |
| 8391 | else if (!strcmp(errors, "replace")) |
| 8392 | known_errorHandler = 2; |
| 8393 | else if (!strcmp(errors, "ignore")) |
| 8394 | known_errorHandler = 3; |
| 8395 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 8396 | known_errorHandler = 4; |
| 8397 | else |
| 8398 | known_errorHandler = 0; |
| 8399 | } |
| 8400 | switch (known_errorHandler) { |
| 8401 | case 1: /* strict */ |
| 8402 | raise_encode_exception(&exc, encoding, s, length, collstart-s, collend-s, reason); |
| 8403 | goto onError; |
| 8404 | case 2: /* replace */ |
| 8405 | for (p = collstart; p < collend; ++p) |
| 8406 | *output++ = '?'; |
| 8407 | /* fall through */ |
| 8408 | case 3: /* ignore */ |
| 8409 | p = collend; |
| 8410 | break; |
| 8411 | case 4: /* xmlcharrefreplace */ |
| 8412 | /* generate replacement (temporarily (mis)uses p) */ |
| 8413 | for (p = collstart; p < collend; ++p) |
| 8414 | output += sprintf(output, "&#%d;", (int)*p); |
| 8415 | p = collend; |
| 8416 | break; |
| 8417 | default: |
| 8418 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 8419 | encoding, reason, s, length, &exc, |
| 8420 | collstart-s, collend-s, &newpos); |
| 8421 | if (repunicode == NULL) |
| 8422 | goto onError; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 8423 | if (!PyUnicode_Check(repunicode)) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8424 | /* Byte results not supported, since they have no decimal property. */ |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 8425 | PyErr_SetString(PyExc_TypeError, "error handler should return unicode"); |
| 8426 | Py_DECREF(repunicode); |
| 8427 | goto onError; |
| 8428 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8429 | /* generate replacement */ |
| 8430 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 8431 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
| 8432 | Py_UNICODE ch = *uni2; |
| 8433 | if (Py_UNICODE_ISSPACE(ch)) |
| 8434 | *output++ = ' '; |
| 8435 | else { |
| 8436 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 8437 | if (decimal >= 0) |
| 8438 | *output++ = '0' + decimal; |
| 8439 | else if (0 < ch && ch < 256) |
| 8440 | *output++ = (char)ch; |
| 8441 | else { |
| 8442 | Py_DECREF(repunicode); |
| 8443 | raise_encode_exception(&exc, encoding, |
| 8444 | s, length, collstart-s, collend-s, reason); |
| 8445 | goto onError; |
| 8446 | } |
| 8447 | } |
| 8448 | } |
| 8449 | p = s + newpos; |
| 8450 | Py_DECREF(repunicode); |
| 8451 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8452 | } |
| 8453 | /* 0-terminate the output string */ |
| 8454 | *output++ = '\0'; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8455 | Py_XDECREF(exc); |
| 8456 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8457 | return 0; |
| 8458 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8459 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8460 | Py_XDECREF(exc); |
| 8461 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8462 | return -1; |
| 8463 | } |
| 8464 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8465 | /* --- Helpers ------------------------------------------------------------ */ |
| 8466 | |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 8467 | #include "stringlib/asciilib.h" |
| 8468 | #include "stringlib/fastsearch.h" |
| 8469 | #include "stringlib/partition.h" |
| 8470 | #include "stringlib/split.h" |
| 8471 | #include "stringlib/count.h" |
| 8472 | #include "stringlib/find.h" |
| 8473 | #include "stringlib/localeutil.h" |
| 8474 | #include "stringlib/undef.h" |
| 8475 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8476 | #include "stringlib/ucs1lib.h" |
| 8477 | #include "stringlib/fastsearch.h" |
| 8478 | #include "stringlib/partition.h" |
| 8479 | #include "stringlib/split.h" |
| 8480 | #include "stringlib/count.h" |
| 8481 | #include "stringlib/find.h" |
| 8482 | #include "stringlib/localeutil.h" |
| 8483 | #include "stringlib/undef.h" |
| 8484 | |
| 8485 | #include "stringlib/ucs2lib.h" |
| 8486 | #include "stringlib/fastsearch.h" |
| 8487 | #include "stringlib/partition.h" |
| 8488 | #include "stringlib/split.h" |
| 8489 | #include "stringlib/count.h" |
| 8490 | #include "stringlib/find.h" |
| 8491 | #include "stringlib/localeutil.h" |
| 8492 | #include "stringlib/undef.h" |
| 8493 | |
| 8494 | #include "stringlib/ucs4lib.h" |
| 8495 | #include "stringlib/fastsearch.h" |
| 8496 | #include "stringlib/partition.h" |
| 8497 | #include "stringlib/split.h" |
| 8498 | #include "stringlib/count.h" |
| 8499 | #include "stringlib/find.h" |
| 8500 | #include "stringlib/localeutil.h" |
| 8501 | #include "stringlib/undef.h" |
| 8502 | |
| 8503 | static Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 8504 | any_find_slice(Py_ssize_t Py_LOCAL_CALLBACK(ascii)(const Py_UCS1*, Py_ssize_t, |
| 8505 | const Py_UCS1*, Py_ssize_t, |
| 8506 | Py_ssize_t, Py_ssize_t), |
| 8507 | Py_ssize_t Py_LOCAL_CALLBACK(ucs1)(const Py_UCS1*, Py_ssize_t, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8508 | const Py_UCS1*, Py_ssize_t, |
| 8509 | Py_ssize_t, Py_ssize_t), |
| 8510 | Py_ssize_t Py_LOCAL_CALLBACK(ucs2)(const Py_UCS2*, Py_ssize_t, |
| 8511 | const Py_UCS2*, Py_ssize_t, |
| 8512 | Py_ssize_t, Py_ssize_t), |
| 8513 | Py_ssize_t Py_LOCAL_CALLBACK(ucs4)(const Py_UCS4*, Py_ssize_t, |
| 8514 | const Py_UCS4*, Py_ssize_t, |
| 8515 | Py_ssize_t, Py_ssize_t), |
| 8516 | PyObject* s1, PyObject* s2, |
| 8517 | Py_ssize_t start, |
| 8518 | Py_ssize_t end) |
| 8519 | { |
| 8520 | int kind1, kind2, kind; |
| 8521 | void *buf1, *buf2; |
| 8522 | Py_ssize_t len1, len2, result; |
| 8523 | |
| 8524 | kind1 = PyUnicode_KIND(s1); |
| 8525 | kind2 = PyUnicode_KIND(s2); |
| 8526 | kind = kind1 > kind2 ? kind1 : kind2; |
| 8527 | buf1 = PyUnicode_DATA(s1); |
| 8528 | buf2 = PyUnicode_DATA(s2); |
| 8529 | if (kind1 != kind) |
| 8530 | buf1 = _PyUnicode_AsKind(s1, kind); |
| 8531 | if (!buf1) |
| 8532 | return -2; |
| 8533 | if (kind2 != kind) |
| 8534 | buf2 = _PyUnicode_AsKind(s2, kind); |
| 8535 | if (!buf2) { |
| 8536 | if (kind1 != kind) PyMem_Free(buf1); |
| 8537 | return -2; |
| 8538 | } |
| 8539 | len1 = PyUnicode_GET_LENGTH(s1); |
| 8540 | len2 = PyUnicode_GET_LENGTH(s2); |
| 8541 | |
| 8542 | switch(kind) { |
| 8543 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 8544 | if (PyUnicode_IS_ASCII(s1) && PyUnicode_IS_ASCII(s2)) |
| 8545 | result = ascii(buf1, len1, buf2, len2, start, end); |
| 8546 | else |
| 8547 | result = ucs1(buf1, len1, buf2, len2, start, end); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8548 | break; |
| 8549 | case PyUnicode_2BYTE_KIND: |
| 8550 | result = ucs2(buf1, len1, buf2, len2, start, end); |
| 8551 | break; |
| 8552 | case PyUnicode_4BYTE_KIND: |
| 8553 | result = ucs4(buf1, len1, buf2, len2, start, end); |
| 8554 | break; |
| 8555 | default: |
| 8556 | assert(0); result = -2; |
| 8557 | } |
| 8558 | |
| 8559 | if (kind1 != kind) |
| 8560 | PyMem_Free(buf1); |
| 8561 | if (kind2 != kind) |
| 8562 | PyMem_Free(buf2); |
| 8563 | |
| 8564 | return result; |
| 8565 | } |
| 8566 | |
| 8567 | Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 8568 | _PyUnicode_InsertThousandsGrouping(PyObject *unicode, int kind, void *data, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8569 | Py_ssize_t n_buffer, |
| 8570 | void *digits, Py_ssize_t n_digits, |
| 8571 | Py_ssize_t min_width, |
| 8572 | const char *grouping, |
| 8573 | const char *thousands_sep) |
| 8574 | { |
| 8575 | switch(kind) { |
| 8576 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 8577 | if (unicode != NULL && PyUnicode_IS_ASCII(unicode)) |
| 8578 | return _PyUnicode_ascii_InsertThousandsGrouping( |
| 8579 | (Py_UCS1*)data, n_buffer, (Py_UCS1*)digits, n_digits, |
| 8580 | min_width, grouping, thousands_sep); |
| 8581 | else |
| 8582 | return _PyUnicode_ucs1_InsertThousandsGrouping( |
| 8583 | (Py_UCS1*)data, n_buffer, (Py_UCS1*)digits, n_digits, |
| 8584 | min_width, grouping, thousands_sep); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8585 | case PyUnicode_2BYTE_KIND: |
| 8586 | return _PyUnicode_ucs2_InsertThousandsGrouping( |
| 8587 | (Py_UCS2*)data, n_buffer, (Py_UCS2*)digits, n_digits, |
| 8588 | min_width, grouping, thousands_sep); |
| 8589 | case PyUnicode_4BYTE_KIND: |
| 8590 | return _PyUnicode_ucs4_InsertThousandsGrouping( |
| 8591 | (Py_UCS4*)data, n_buffer, (Py_UCS4*)digits, n_digits, |
| 8592 | min_width, grouping, thousands_sep); |
| 8593 | } |
| 8594 | assert(0); |
| 8595 | return -1; |
| 8596 | } |
| 8597 | |
| 8598 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 8599 | #include "stringlib/unicodedefs.h" |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8600 | #include "stringlib/fastsearch.h" |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 8601 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8602 | #include "stringlib/count.h" |
| 8603 | #include "stringlib/find.h" |
Eric Smith | 5807c41 | 2008-05-11 21:00:57 +0000 | [diff] [blame] | 8604 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8605 | /* helper macro to fixup start/end slice values */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 8606 | #define ADJUST_INDICES(start, end, len) \ |
| 8607 | if (end > len) \ |
| 8608 | end = len; \ |
| 8609 | else if (end < 0) { \ |
| 8610 | end += len; \ |
| 8611 | if (end < 0) \ |
| 8612 | end = 0; \ |
| 8613 | } \ |
| 8614 | if (start < 0) { \ |
| 8615 | start += len; \ |
| 8616 | if (start < 0) \ |
| 8617 | start = 0; \ |
| 8618 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8619 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8620 | Py_ssize_t |
| 8621 | PyUnicode_Count(PyObject *str, |
| 8622 | PyObject *substr, |
| 8623 | Py_ssize_t start, |
| 8624 | Py_ssize_t end) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8625 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8626 | Py_ssize_t result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8627 | PyUnicodeObject* str_obj; |
| 8628 | PyUnicodeObject* sub_obj; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8629 | int kind1, kind2, kind; |
| 8630 | void *buf1 = NULL, *buf2 = NULL; |
| 8631 | Py_ssize_t len1, len2; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8632 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8633 | str_obj = (PyUnicodeObject*) PyUnicode_FromObject(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8634 | if (!str_obj || PyUnicode_READY(str_obj) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8635 | return -1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8636 | sub_obj = (PyUnicodeObject*) PyUnicode_FromObject(substr); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 8637 | if (!sub_obj || PyUnicode_READY(sub_obj) == -1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8638 | Py_DECREF(str_obj); |
| 8639 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8640 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8641 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8642 | kind1 = PyUnicode_KIND(str_obj); |
| 8643 | kind2 = PyUnicode_KIND(sub_obj); |
| 8644 | kind = kind1 > kind2 ? kind1 : kind2; |
| 8645 | buf1 = PyUnicode_DATA(str_obj); |
| 8646 | if (kind1 != kind) |
| 8647 | buf1 = _PyUnicode_AsKind((PyObject*)str_obj, kind); |
| 8648 | if (!buf1) |
| 8649 | goto onError; |
| 8650 | buf2 = PyUnicode_DATA(sub_obj); |
| 8651 | if (kind2 != kind) |
| 8652 | buf2 = _PyUnicode_AsKind((PyObject*)sub_obj, kind); |
| 8653 | if (!buf2) |
| 8654 | goto onError; |
| 8655 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 8656 | len2 = PyUnicode_GET_LENGTH(sub_obj); |
| 8657 | |
| 8658 | ADJUST_INDICES(start, end, len1); |
| 8659 | switch(kind) { |
| 8660 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 8661 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sub_obj)) |
| 8662 | result = asciilib_count( |
| 8663 | ((Py_UCS1*)buf1) + start, end - start, |
| 8664 | buf2, len2, PY_SSIZE_T_MAX |
| 8665 | ); |
| 8666 | else |
| 8667 | result = ucs1lib_count( |
| 8668 | ((Py_UCS1*)buf1) + start, end - start, |
| 8669 | buf2, len2, PY_SSIZE_T_MAX |
| 8670 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8671 | break; |
| 8672 | case PyUnicode_2BYTE_KIND: |
| 8673 | result = ucs2lib_count( |
| 8674 | ((Py_UCS2*)buf1) + start, end - start, |
| 8675 | buf2, len2, PY_SSIZE_T_MAX |
| 8676 | ); |
| 8677 | break; |
| 8678 | case PyUnicode_4BYTE_KIND: |
| 8679 | result = ucs4lib_count( |
| 8680 | ((Py_UCS4*)buf1) + start, end - start, |
| 8681 | buf2, len2, PY_SSIZE_T_MAX |
| 8682 | ); |
| 8683 | break; |
| 8684 | default: |
| 8685 | assert(0); result = 0; |
| 8686 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8687 | |
| 8688 | Py_DECREF(sub_obj); |
| 8689 | Py_DECREF(str_obj); |
| 8690 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8691 | if (kind1 != kind) |
| 8692 | PyMem_Free(buf1); |
| 8693 | if (kind2 != kind) |
| 8694 | PyMem_Free(buf2); |
| 8695 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8696 | return result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8697 | onError: |
| 8698 | Py_DECREF(sub_obj); |
| 8699 | Py_DECREF(str_obj); |
| 8700 | if (kind1 != kind && buf1) |
| 8701 | PyMem_Free(buf1); |
| 8702 | if (kind2 != kind && buf2) |
| 8703 | PyMem_Free(buf2); |
| 8704 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8705 | } |
| 8706 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8707 | Py_ssize_t |
| 8708 | PyUnicode_Find(PyObject *str, |
| 8709 | PyObject *sub, |
| 8710 | Py_ssize_t start, |
| 8711 | Py_ssize_t end, |
| 8712 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8713 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8714 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8715 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8716 | str = PyUnicode_FromObject(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8717 | if (!str || PyUnicode_READY(str) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8718 | return -2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8719 | sub = PyUnicode_FromObject(sub); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8720 | if (!sub || PyUnicode_READY(sub) == -1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8721 | Py_DECREF(str); |
| 8722 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8723 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8724 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8725 | if (direction > 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8726 | result = any_find_slice( |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 8727 | asciilib_find_slice, ucs1lib_find_slice, |
| 8728 | ucs2lib_find_slice, ucs4lib_find_slice, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8729 | str, sub, start, end |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8730 | ); |
| 8731 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8732 | result = any_find_slice( |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 8733 | asciilib_find_slice, ucs1lib_rfind_slice, |
| 8734 | ucs2lib_rfind_slice, ucs4lib_rfind_slice, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8735 | str, sub, start, end |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8736 | ); |
| 8737 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8738 | Py_DECREF(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8739 | Py_DECREF(sub); |
| 8740 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8741 | return result; |
| 8742 | } |
| 8743 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8744 | Py_ssize_t |
| 8745 | PyUnicode_FindChar(PyObject *str, Py_UCS4 ch, |
| 8746 | Py_ssize_t start, Py_ssize_t end, |
| 8747 | int direction) |
| 8748 | { |
| 8749 | char *result; |
| 8750 | int kind; |
| 8751 | if (PyUnicode_READY(str) == -1) |
| 8752 | return -2; |
Victor Stinner | 267aa24 | 2011-10-02 01:08:37 +0200 | [diff] [blame] | 8753 | if (start < 0 || end < 0) { |
| 8754 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 8755 | return -2; |
| 8756 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8757 | if (end > PyUnicode_GET_LENGTH(str)) |
| 8758 | end = PyUnicode_GET_LENGTH(str); |
| 8759 | kind = PyUnicode_KIND(str); |
| 8760 | result = findchar(PyUnicode_1BYTE_DATA(str) |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame^] | 8761 | + kind*start, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8762 | kind, |
| 8763 | end-start, ch, direction); |
| 8764 | if (!result) |
| 8765 | return -1; |
| 8766 | return (result-(char*)PyUnicode_DATA(str)) >> (kind-1); |
| 8767 | } |
| 8768 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8769 | static int |
| 8770 | tailmatch(PyUnicodeObject *self, |
| 8771 | PyUnicodeObject *substring, |
| 8772 | Py_ssize_t start, |
| 8773 | Py_ssize_t end, |
| 8774 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8775 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8776 | int kind_self; |
| 8777 | int kind_sub; |
| 8778 | void *data_self; |
| 8779 | void *data_sub; |
| 8780 | Py_ssize_t offset; |
| 8781 | Py_ssize_t i; |
| 8782 | Py_ssize_t end_sub; |
| 8783 | |
| 8784 | if (PyUnicode_READY(self) == -1 || |
| 8785 | PyUnicode_READY(substring) == -1) |
| 8786 | return 0; |
| 8787 | |
| 8788 | if (PyUnicode_GET_LENGTH(substring) == 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8789 | return 1; |
| 8790 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8791 | ADJUST_INDICES(start, end, PyUnicode_GET_LENGTH(self)); |
| 8792 | end -= PyUnicode_GET_LENGTH(substring); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8793 | if (end < start) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8794 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8795 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8796 | kind_self = PyUnicode_KIND(self); |
| 8797 | data_self = PyUnicode_DATA(self); |
| 8798 | kind_sub = PyUnicode_KIND(substring); |
| 8799 | data_sub = PyUnicode_DATA(substring); |
| 8800 | end_sub = PyUnicode_GET_LENGTH(substring) - 1; |
| 8801 | |
| 8802 | if (direction > 0) |
| 8803 | offset = end; |
| 8804 | else |
| 8805 | offset = start; |
| 8806 | |
| 8807 | if (PyUnicode_READ(kind_self, data_self, offset) == |
| 8808 | PyUnicode_READ(kind_sub, data_sub, 0) && |
| 8809 | PyUnicode_READ(kind_self, data_self, offset + end_sub) == |
| 8810 | PyUnicode_READ(kind_sub, data_sub, end_sub)) { |
| 8811 | /* If both are of the same kind, memcmp is sufficient */ |
| 8812 | if (kind_self == kind_sub) { |
| 8813 | return ! memcmp((char *)data_self + |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame^] | 8814 | (offset * PyUnicode_KIND(substring)), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8815 | data_sub, |
| 8816 | PyUnicode_GET_LENGTH(substring) * |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame^] | 8817 | PyUnicode_KIND(substring)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8818 | } |
| 8819 | /* otherwise we have to compare each character by first accesing it */ |
| 8820 | else { |
| 8821 | /* We do not need to compare 0 and len(substring)-1 because |
| 8822 | the if statement above ensured already that they are equal |
| 8823 | when we end up here. */ |
| 8824 | // TODO: honor direction and do a forward or backwards search |
| 8825 | for (i = 1; i < end_sub; ++i) { |
| 8826 | if (PyUnicode_READ(kind_self, data_self, offset + i) != |
| 8827 | PyUnicode_READ(kind_sub, data_sub, i)) |
| 8828 | return 0; |
| 8829 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8830 | return 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8831 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8832 | } |
| 8833 | |
| 8834 | return 0; |
| 8835 | } |
| 8836 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8837 | Py_ssize_t |
| 8838 | PyUnicode_Tailmatch(PyObject *str, |
| 8839 | PyObject *substr, |
| 8840 | Py_ssize_t start, |
| 8841 | Py_ssize_t end, |
| 8842 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8843 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8844 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8845 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8846 | str = PyUnicode_FromObject(str); |
| 8847 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8848 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8849 | substr = PyUnicode_FromObject(substr); |
| 8850 | if (substr == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8851 | Py_DECREF(str); |
| 8852 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8853 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8854 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8855 | result = tailmatch((PyUnicodeObject *)str, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8856 | (PyUnicodeObject *)substr, |
| 8857 | start, end, direction); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8858 | Py_DECREF(str); |
| 8859 | Py_DECREF(substr); |
| 8860 | return result; |
| 8861 | } |
| 8862 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8863 | /* Apply fixfct filter to the Unicode object self and return a |
| 8864 | reference to the modified object */ |
| 8865 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8866 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8867 | fixup(PyObject *self, |
| 8868 | Py_UCS4 (*fixfct)(PyObject *s)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8869 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8870 | PyObject *u; |
| 8871 | Py_UCS4 maxchar_old, maxchar_new = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8872 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8873 | if (PyUnicode_READY(self) == -1) |
| 8874 | return NULL; |
| 8875 | maxchar_old = PyUnicode_MAX_CHAR_VALUE(self); |
| 8876 | u = PyUnicode_New(PyUnicode_GET_LENGTH(self), |
| 8877 | maxchar_old); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8878 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8879 | return NULL; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8880 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8881 | Py_MEMCPY(PyUnicode_1BYTE_DATA(u), PyUnicode_1BYTE_DATA(self), |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame^] | 8882 | PyUnicode_GET_LENGTH(u) * PyUnicode_KIND(u)); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8883 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8884 | /* fix functions return the new maximum character in a string, |
| 8885 | if the kind of the resulting unicode object does not change, |
| 8886 | everything is fine. Otherwise we need to change the string kind |
| 8887 | and re-run the fix function. */ |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8888 | maxchar_new = fixfct(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8889 | if (maxchar_new == 0) |
| 8890 | /* do nothing, keep maxchar_new at 0 which means no changes. */; |
| 8891 | else if (maxchar_new <= 127) |
| 8892 | maxchar_new = 127; |
| 8893 | else if (maxchar_new <= 255) |
| 8894 | maxchar_new = 255; |
| 8895 | else if (maxchar_new <= 65535) |
| 8896 | maxchar_new = 65535; |
| 8897 | else |
| 8898 | maxchar_new = 1114111; /* 0x10ffff */ |
| 8899 | |
| 8900 | if (!maxchar_new && PyUnicode_CheckExact(self)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8901 | /* fixfct should return TRUE if it modified the buffer. If |
| 8902 | FALSE, return a reference to the original buffer instead |
| 8903 | (to save space, not time) */ |
| 8904 | Py_INCREF(self); |
| 8905 | Py_DECREF(u); |
| 8906 | return (PyObject*) self; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8907 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8908 | else if (maxchar_new == maxchar_old) { |
| 8909 | return u; |
| 8910 | } |
| 8911 | else { |
| 8912 | /* In case the maximum character changed, we need to |
| 8913 | convert the string to the new category. */ |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 8914 | PyObject *v = PyUnicode_New(PyUnicode_GET_LENGTH(self), maxchar_new); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8915 | if (v == NULL) { |
| 8916 | Py_DECREF(u); |
| 8917 | return NULL; |
| 8918 | } |
| 8919 | if (maxchar_new > maxchar_old) { |
| 8920 | /* If the maxchar increased so that the kind changed, not all |
| 8921 | characters are representable anymore and we need to fix the |
| 8922 | string again. This only happens in very few cases. */ |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 8923 | copy_characters(v, 0, self, 0, PyUnicode_GET_LENGTH(self)); |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8924 | maxchar_old = fixfct(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8925 | assert(maxchar_old > 0 && maxchar_old <= maxchar_new); |
| 8926 | } |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 8927 | else { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 8928 | copy_characters(v, 0, u, 0, PyUnicode_GET_LENGTH(self)); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 8929 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8930 | |
| 8931 | Py_DECREF(u); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 8932 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8933 | return v; |
| 8934 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8935 | } |
| 8936 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8937 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8938 | fixupper(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8939 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8940 | /* No need to call PyUnicode_READY(self) because this function is only |
| 8941 | called as a callback from fixup() which does it already. */ |
| 8942 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 8943 | const int kind = PyUnicode_KIND(self); |
| 8944 | void *data = PyUnicode_DATA(self); |
| 8945 | int touched = 0; |
| 8946 | Py_UCS4 maxchar = 0; |
| 8947 | Py_ssize_t i; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8948 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8949 | for (i = 0; i < len; ++i) { |
| 8950 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 8951 | const Py_UCS4 up = Py_UNICODE_TOUPPER(ch); |
| 8952 | if (up != ch) { |
| 8953 | if (up > maxchar) |
| 8954 | maxchar = up; |
| 8955 | PyUnicode_WRITE(kind, data, i, up); |
| 8956 | touched = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8957 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8958 | else if (ch > maxchar) |
| 8959 | maxchar = ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8960 | } |
| 8961 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8962 | if (touched) |
| 8963 | return maxchar; |
| 8964 | else |
| 8965 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8966 | } |
| 8967 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8968 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8969 | fixlower(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8970 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8971 | /* No need to call PyUnicode_READY(self) because fixup() which does it. */ |
| 8972 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 8973 | const int kind = PyUnicode_KIND(self); |
| 8974 | void *data = PyUnicode_DATA(self); |
| 8975 | int touched = 0; |
| 8976 | Py_UCS4 maxchar = 0; |
| 8977 | Py_ssize_t i; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8978 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8979 | for(i = 0; i < len; ++i) { |
| 8980 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 8981 | const Py_UCS4 lo = Py_UNICODE_TOLOWER(ch); |
| 8982 | if (lo != ch) { |
| 8983 | if (lo > maxchar) |
| 8984 | maxchar = lo; |
| 8985 | PyUnicode_WRITE(kind, data, i, lo); |
| 8986 | touched = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8987 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8988 | else if (ch > maxchar) |
| 8989 | maxchar = ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8990 | } |
| 8991 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8992 | if (touched) |
| 8993 | return maxchar; |
| 8994 | else |
| 8995 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8996 | } |
| 8997 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8998 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8999 | fixswapcase(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9000 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9001 | /* No need to call PyUnicode_READY(self) because fixup() which does it. */ |
| 9002 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 9003 | const int kind = PyUnicode_KIND(self); |
| 9004 | void *data = PyUnicode_DATA(self); |
| 9005 | int touched = 0; |
| 9006 | Py_UCS4 maxchar = 0; |
| 9007 | Py_ssize_t i; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9008 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9009 | for(i = 0; i < len; ++i) { |
| 9010 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 9011 | Py_UCS4 nu = 0; |
| 9012 | |
| 9013 | if (Py_UNICODE_ISUPPER(ch)) |
| 9014 | nu = Py_UNICODE_TOLOWER(ch); |
| 9015 | else if (Py_UNICODE_ISLOWER(ch)) |
| 9016 | nu = Py_UNICODE_TOUPPER(ch); |
| 9017 | |
| 9018 | if (nu != 0) { |
| 9019 | if (nu > maxchar) |
| 9020 | maxchar = nu; |
| 9021 | PyUnicode_WRITE(kind, data, i, nu); |
| 9022 | touched = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9023 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9024 | else if (ch > maxchar) |
| 9025 | maxchar = ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9026 | } |
| 9027 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9028 | if (touched) |
| 9029 | return maxchar; |
| 9030 | else |
| 9031 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9032 | } |
| 9033 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9034 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9035 | fixcapitalize(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9036 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9037 | /* No need to call PyUnicode_READY(self) because fixup() which does it. */ |
| 9038 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 9039 | const int kind = PyUnicode_KIND(self); |
| 9040 | void *data = PyUnicode_DATA(self); |
| 9041 | int touched = 0; |
| 9042 | Py_UCS4 maxchar = 0; |
| 9043 | Py_ssize_t i = 0; |
| 9044 | Py_UCS4 ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9045 | |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9046 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9047 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9048 | |
| 9049 | ch = PyUnicode_READ(kind, data, i); |
| 9050 | if (!Py_UNICODE_ISUPPER(ch)) { |
| 9051 | maxchar = Py_UNICODE_TOUPPER(ch); |
| 9052 | PyUnicode_WRITE(kind, data, i, maxchar); |
| 9053 | touched = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9054 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9055 | ++i; |
| 9056 | for(; i < len; ++i) { |
| 9057 | ch = PyUnicode_READ(kind, data, i); |
| 9058 | if (!Py_UNICODE_ISLOWER(ch)) { |
| 9059 | const Py_UCS4 lo = Py_UNICODE_TOLOWER(ch); |
| 9060 | if (lo > maxchar) |
| 9061 | maxchar = lo; |
| 9062 | PyUnicode_WRITE(kind, data, i, lo); |
| 9063 | touched = 1; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9064 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9065 | else if (ch > maxchar) |
| 9066 | maxchar = ch; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9067 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9068 | |
| 9069 | if (touched) |
| 9070 | return maxchar; |
| 9071 | else |
| 9072 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9073 | } |
| 9074 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9075 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9076 | fixtitle(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9077 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9078 | /* No need to call PyUnicode_READY(self) because fixup() which does it. */ |
| 9079 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 9080 | const int kind = PyUnicode_KIND(self); |
| 9081 | void *data = PyUnicode_DATA(self); |
| 9082 | Py_UCS4 maxchar = 0; |
| 9083 | Py_ssize_t i = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9084 | int previous_is_cased; |
| 9085 | |
| 9086 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9087 | if (len == 1) { |
| 9088 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 9089 | const Py_UCS4 ti = Py_UNICODE_TOTITLE(ch); |
| 9090 | if (ti != ch) { |
| 9091 | PyUnicode_WRITE(kind, data, i, ti); |
| 9092 | return ti; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9093 | } |
| 9094 | else |
| 9095 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9096 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9097 | previous_is_cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9098 | for(; i < len; ++i) { |
| 9099 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 9100 | Py_UCS4 nu; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9101 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9102 | if (previous_is_cased) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9103 | nu = Py_UNICODE_TOLOWER(ch); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9104 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9105 | nu = Py_UNICODE_TOTITLE(ch); |
| 9106 | |
| 9107 | if (nu > maxchar) |
| 9108 | maxchar = nu; |
| 9109 | PyUnicode_WRITE(kind, data, i, nu); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9110 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9111 | if (Py_UNICODE_ISLOWER(ch) || |
| 9112 | Py_UNICODE_ISUPPER(ch) || |
| 9113 | Py_UNICODE_ISTITLE(ch)) |
| 9114 | previous_is_cased = 1; |
| 9115 | else |
| 9116 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9117 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9118 | return maxchar; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9119 | } |
| 9120 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9121 | PyObject * |
| 9122 | PyUnicode_Join(PyObject *separator, PyObject *seq) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9123 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9124 | PyObject *sep = NULL; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9125 | Py_ssize_t seplen; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9126 | PyObject *res = NULL; /* the result */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9127 | PyObject *fseq; /* PySequence_Fast(seq) */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9128 | Py_ssize_t seqlen; /* len(fseq) -- number of items in sequence */ |
| 9129 | PyObject **items; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9130 | PyObject *item; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9131 | Py_ssize_t sz, i, res_offset; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9132 | Py_UCS4 maxchar; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9133 | Py_UCS4 item_maxchar; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9134 | int use_memcpy; |
| 9135 | unsigned char *res_data = NULL, *sep_data = NULL; |
| 9136 | PyObject *last_obj; |
| 9137 | unsigned int kind = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9138 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9139 | fseq = PySequence_Fast(seq, ""); |
| 9140 | if (fseq == NULL) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9141 | return NULL; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9142 | } |
| 9143 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9144 | /* NOTE: the following code can't call back into Python code, |
| 9145 | * so we are sure that fseq won't be mutated. |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 9146 | */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9147 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9148 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 9149 | /* If empty sequence, return u"". */ |
| 9150 | if (seqlen == 0) { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9151 | Py_DECREF(fseq); |
| 9152 | Py_INCREF(unicode_empty); |
| 9153 | res = unicode_empty; |
| 9154 | return res; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9155 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9156 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9157 | /* If singleton sequence with an exact Unicode, return that. */ |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9158 | last_obj = NULL; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9159 | items = PySequence_Fast_ITEMS(fseq); |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9160 | if (seqlen == 1) { |
| 9161 | if (PyUnicode_CheckExact(items[0])) { |
| 9162 | res = items[0]; |
| 9163 | Py_INCREF(res); |
| 9164 | Py_DECREF(fseq); |
| 9165 | return res; |
| 9166 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9167 | seplen = 0; |
Victor Stinner | c6f0df7 | 2011-10-06 15:58:54 +0200 | [diff] [blame] | 9168 | maxchar = 0; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9169 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9170 | else { |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9171 | /* Set up sep and seplen */ |
| 9172 | if (separator == NULL) { |
| 9173 | /* fall back to a blank space separator */ |
| 9174 | sep = PyUnicode_FromOrdinal(' '); |
| 9175 | if (!sep) |
| 9176 | goto onError; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9177 | seplen = 1; |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9178 | maxchar = 32; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9179 | } |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9180 | else { |
| 9181 | if (!PyUnicode_Check(separator)) { |
| 9182 | PyErr_Format(PyExc_TypeError, |
| 9183 | "separator: expected str instance," |
| 9184 | " %.80s found", |
| 9185 | Py_TYPE(separator)->tp_name); |
| 9186 | goto onError; |
| 9187 | } |
| 9188 | if (PyUnicode_READY(separator)) |
| 9189 | goto onError; |
| 9190 | sep = separator; |
| 9191 | seplen = PyUnicode_GET_LENGTH(separator); |
| 9192 | maxchar = PyUnicode_MAX_CHAR_VALUE(separator); |
| 9193 | /* inc refcount to keep this code path symmetric with the |
| 9194 | above case of a blank separator */ |
| 9195 | Py_INCREF(sep); |
| 9196 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9197 | last_obj = sep; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9198 | } |
| 9199 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9200 | /* There are at least two things to join, or else we have a subclass |
| 9201 | * of str in the sequence. |
| 9202 | * Do a pre-pass to figure out the total amount of space we'll |
| 9203 | * need (sz), and see whether all argument are strings. |
| 9204 | */ |
| 9205 | sz = 0; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9206 | #ifdef Py_DEBUG |
| 9207 | use_memcpy = 0; |
| 9208 | #else |
| 9209 | use_memcpy = 1; |
| 9210 | #endif |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9211 | for (i = 0; i < seqlen; i++) { |
| 9212 | const Py_ssize_t old_sz = sz; |
| 9213 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9214 | if (!PyUnicode_Check(item)) { |
| 9215 | PyErr_Format(PyExc_TypeError, |
| 9216 | "sequence item %zd: expected str instance," |
| 9217 | " %.80s found", |
| 9218 | i, Py_TYPE(item)->tp_name); |
| 9219 | goto onError; |
| 9220 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9221 | if (PyUnicode_READY(item) == -1) |
| 9222 | goto onError; |
| 9223 | sz += PyUnicode_GET_LENGTH(item); |
| 9224 | item_maxchar = PyUnicode_MAX_CHAR_VALUE(item); |
Victor Stinner | c6f0df7 | 2011-10-06 15:58:54 +0200 | [diff] [blame] | 9225 | maxchar = Py_MAX(maxchar, item_maxchar); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9226 | if (i != 0) |
| 9227 | sz += seplen; |
| 9228 | if (sz < old_sz || sz > PY_SSIZE_T_MAX) { |
| 9229 | PyErr_SetString(PyExc_OverflowError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9230 | "join() result is too long for a Python string"); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9231 | goto onError; |
| 9232 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9233 | if (use_memcpy && last_obj != NULL) { |
| 9234 | if (PyUnicode_KIND(last_obj) != PyUnicode_KIND(item)) |
| 9235 | use_memcpy = 0; |
| 9236 | } |
| 9237 | last_obj = item; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9238 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9239 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9240 | res = PyUnicode_New(sz, maxchar); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9241 | if (res == NULL) |
| 9242 | goto onError; |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 9243 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9244 | /* Catenate everything. */ |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9245 | #ifdef Py_DEBUG |
| 9246 | use_memcpy = 0; |
| 9247 | #else |
| 9248 | if (use_memcpy) { |
| 9249 | res_data = PyUnicode_1BYTE_DATA(res); |
| 9250 | kind = PyUnicode_KIND(res); |
| 9251 | if (seplen != 0) |
| 9252 | sep_data = PyUnicode_1BYTE_DATA(sep); |
| 9253 | } |
| 9254 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9255 | for (i = 0, res_offset = 0; i < seqlen; ++i) { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9256 | Py_ssize_t itemlen; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9257 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9258 | /* Copy item, and maybe the separator. */ |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 9259 | if (i && seplen != 0) { |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9260 | if (use_memcpy) { |
| 9261 | Py_MEMCPY(res_data, |
| 9262 | sep_data, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame^] | 9263 | kind * seplen); |
| 9264 | res_data += kind * seplen; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9265 | } |
| 9266 | else { |
| 9267 | copy_characters(res, res_offset, sep, 0, seplen); |
| 9268 | res_offset += seplen; |
| 9269 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9270 | } |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 9271 | itemlen = PyUnicode_GET_LENGTH(item); |
| 9272 | if (itemlen != 0) { |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9273 | if (use_memcpy) { |
| 9274 | Py_MEMCPY(res_data, |
| 9275 | PyUnicode_DATA(item), |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame^] | 9276 | kind * itemlen); |
| 9277 | res_data += kind * itemlen; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9278 | } |
| 9279 | else { |
| 9280 | copy_characters(res, res_offset, item, 0, itemlen); |
| 9281 | res_offset += itemlen; |
| 9282 | } |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 9283 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9284 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9285 | if (use_memcpy) |
| 9286 | assert(res_data == PyUnicode_1BYTE_DATA(res) |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame^] | 9287 | + kind * PyUnicode_GET_LENGTH(res)); |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9288 | else |
| 9289 | assert(res_offset == PyUnicode_GET_LENGTH(res)); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9290 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9291 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9292 | Py_XDECREF(sep); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 9293 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9294 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9295 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9296 | onError: |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9297 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9298 | Py_XDECREF(sep); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9299 | Py_XDECREF(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9300 | return NULL; |
| 9301 | } |
| 9302 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9303 | #define FILL(kind, data, value, start, length) \ |
| 9304 | do { \ |
| 9305 | Py_ssize_t i_ = 0; \ |
| 9306 | assert(kind != PyUnicode_WCHAR_KIND); \ |
| 9307 | switch ((kind)) { \ |
| 9308 | case PyUnicode_1BYTE_KIND: { \ |
| 9309 | unsigned char * to_ = (unsigned char *)((data)) + (start); \ |
| 9310 | memset(to_, (unsigned char)value, length); \ |
| 9311 | break; \ |
| 9312 | } \ |
| 9313 | case PyUnicode_2BYTE_KIND: { \ |
| 9314 | Py_UCS2 * to_ = (Py_UCS2 *)((data)) + (start); \ |
| 9315 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 9316 | break; \ |
| 9317 | } \ |
| 9318 | default: { \ |
| 9319 | Py_UCS4 * to_ = (Py_UCS4 *)((data)) + (start); \ |
| 9320 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 9321 | break; \ |
| 9322 | } \ |
| 9323 | } \ |
| 9324 | } while (0) |
| 9325 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9326 | static PyObject * |
| 9327 | pad(PyObject *self, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9328 | Py_ssize_t left, |
| 9329 | Py_ssize_t right, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9330 | Py_UCS4 fill) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9331 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9332 | PyObject *u; |
| 9333 | Py_UCS4 maxchar; |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9334 | int kind; |
| 9335 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9336 | |
| 9337 | if (left < 0) |
| 9338 | left = 0; |
| 9339 | if (right < 0) |
| 9340 | right = 0; |
| 9341 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 9342 | if (left == 0 && right == 0 && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9343 | Py_INCREF(self); |
| 9344 | return self; |
| 9345 | } |
| 9346 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9347 | if (left > PY_SSIZE_T_MAX - _PyUnicode_LENGTH(self) || |
| 9348 | right > PY_SSIZE_T_MAX - (left + _PyUnicode_LENGTH(self))) { |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 9349 | PyErr_SetString(PyExc_OverflowError, "padded string is too long"); |
| 9350 | return NULL; |
| 9351 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9352 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
| 9353 | if (fill > maxchar) |
| 9354 | maxchar = fill; |
| 9355 | u = PyUnicode_New(left + _PyUnicode_LENGTH(self) + right, maxchar); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9356 | if (!u) |
| 9357 | return NULL; |
| 9358 | |
| 9359 | kind = PyUnicode_KIND(u); |
| 9360 | data = PyUnicode_DATA(u); |
| 9361 | if (left) |
| 9362 | FILL(kind, data, fill, 0, left); |
| 9363 | if (right) |
| 9364 | FILL(kind, data, fill, left + _PyUnicode_LENGTH(self), right); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9365 | copy_characters(u, left, self, 0, _PyUnicode_LENGTH(self)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 9366 | assert(_PyUnicode_CheckConsistency(u, 1)); |
| 9367 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9368 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9369 | #undef FILL |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9370 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9371 | PyObject * |
| 9372 | PyUnicode_Splitlines(PyObject *string, int keepends) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9373 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9374 | PyObject *list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9375 | |
| 9376 | string = PyUnicode_FromObject(string); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9377 | if (string == NULL || PyUnicode_READY(string) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9378 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9379 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9380 | switch(PyUnicode_KIND(string)) { |
| 9381 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9382 | if (PyUnicode_IS_ASCII(string)) |
| 9383 | list = asciilib_splitlines( |
| 9384 | (PyObject*) string, PyUnicode_1BYTE_DATA(string), |
| 9385 | PyUnicode_GET_LENGTH(string), keepends); |
| 9386 | else |
| 9387 | list = ucs1lib_splitlines( |
| 9388 | (PyObject*) string, PyUnicode_1BYTE_DATA(string), |
| 9389 | PyUnicode_GET_LENGTH(string), keepends); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9390 | break; |
| 9391 | case PyUnicode_2BYTE_KIND: |
| 9392 | list = ucs2lib_splitlines( |
| 9393 | (PyObject*) string, PyUnicode_2BYTE_DATA(string), |
| 9394 | PyUnicode_GET_LENGTH(string), keepends); |
| 9395 | break; |
| 9396 | case PyUnicode_4BYTE_KIND: |
| 9397 | list = ucs4lib_splitlines( |
| 9398 | (PyObject*) string, PyUnicode_4BYTE_DATA(string), |
| 9399 | PyUnicode_GET_LENGTH(string), keepends); |
| 9400 | break; |
| 9401 | default: |
| 9402 | assert(0); |
| 9403 | list = 0; |
| 9404 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9405 | Py_DECREF(string); |
| 9406 | return list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9407 | } |
| 9408 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9409 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9410 | split(PyObject *self, |
| 9411 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9412 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9413 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9414 | int kind1, kind2, kind; |
| 9415 | void *buf1, *buf2; |
| 9416 | Py_ssize_t len1, len2; |
| 9417 | PyObject* out; |
| 9418 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9419 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9420 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9421 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9422 | if (PyUnicode_READY(self) == -1) |
| 9423 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9424 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9425 | if (substring == NULL) |
| 9426 | switch(PyUnicode_KIND(self)) { |
| 9427 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9428 | if (PyUnicode_IS_ASCII(self)) |
| 9429 | return asciilib_split_whitespace( |
| 9430 | (PyObject*) self, PyUnicode_1BYTE_DATA(self), |
| 9431 | PyUnicode_GET_LENGTH(self), maxcount |
| 9432 | ); |
| 9433 | else |
| 9434 | return ucs1lib_split_whitespace( |
| 9435 | (PyObject*) self, PyUnicode_1BYTE_DATA(self), |
| 9436 | PyUnicode_GET_LENGTH(self), maxcount |
| 9437 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9438 | case PyUnicode_2BYTE_KIND: |
| 9439 | return ucs2lib_split_whitespace( |
| 9440 | (PyObject*) self, PyUnicode_2BYTE_DATA(self), |
| 9441 | PyUnicode_GET_LENGTH(self), maxcount |
| 9442 | ); |
| 9443 | case PyUnicode_4BYTE_KIND: |
| 9444 | return ucs4lib_split_whitespace( |
| 9445 | (PyObject*) self, PyUnicode_4BYTE_DATA(self), |
| 9446 | PyUnicode_GET_LENGTH(self), maxcount |
| 9447 | ); |
| 9448 | default: |
| 9449 | assert(0); |
| 9450 | return NULL; |
| 9451 | } |
| 9452 | |
| 9453 | if (PyUnicode_READY(substring) == -1) |
| 9454 | return NULL; |
| 9455 | |
| 9456 | kind1 = PyUnicode_KIND(self); |
| 9457 | kind2 = PyUnicode_KIND(substring); |
| 9458 | kind = kind1 > kind2 ? kind1 : kind2; |
| 9459 | buf1 = PyUnicode_DATA(self); |
| 9460 | buf2 = PyUnicode_DATA(substring); |
| 9461 | if (kind1 != kind) |
| 9462 | buf1 = _PyUnicode_AsKind((PyObject*)self, kind); |
| 9463 | if (!buf1) |
| 9464 | return NULL; |
| 9465 | if (kind2 != kind) |
| 9466 | buf2 = _PyUnicode_AsKind((PyObject*)substring, kind); |
| 9467 | if (!buf2) { |
| 9468 | if (kind1 != kind) PyMem_Free(buf1); |
| 9469 | return NULL; |
| 9470 | } |
| 9471 | len1 = PyUnicode_GET_LENGTH(self); |
| 9472 | len2 = PyUnicode_GET_LENGTH(substring); |
| 9473 | |
| 9474 | switch(kind) { |
| 9475 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9476 | if (PyUnicode_IS_ASCII(self) && PyUnicode_IS_ASCII(substring)) |
| 9477 | out = asciilib_split( |
| 9478 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
| 9479 | else |
| 9480 | out = ucs1lib_split( |
| 9481 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9482 | break; |
| 9483 | case PyUnicode_2BYTE_KIND: |
| 9484 | out = ucs2lib_split( |
| 9485 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
| 9486 | break; |
| 9487 | case PyUnicode_4BYTE_KIND: |
| 9488 | out = ucs4lib_split( |
| 9489 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
| 9490 | break; |
| 9491 | default: |
| 9492 | out = NULL; |
| 9493 | } |
| 9494 | if (kind1 != kind) |
| 9495 | PyMem_Free(buf1); |
| 9496 | if (kind2 != kind) |
| 9497 | PyMem_Free(buf2); |
| 9498 | return out; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9499 | } |
| 9500 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9501 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9502 | rsplit(PyObject *self, |
| 9503 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9504 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9505 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9506 | int kind1, kind2, kind; |
| 9507 | void *buf1, *buf2; |
| 9508 | Py_ssize_t len1, len2; |
| 9509 | PyObject* out; |
| 9510 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9511 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9512 | maxcount = PY_SSIZE_T_MAX; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9513 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9514 | if (PyUnicode_READY(self) == -1) |
| 9515 | return NULL; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9516 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9517 | if (substring == NULL) |
| 9518 | switch(PyUnicode_KIND(self)) { |
| 9519 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9520 | if (PyUnicode_IS_ASCII(self)) |
| 9521 | return asciilib_rsplit_whitespace( |
| 9522 | (PyObject*) self, PyUnicode_1BYTE_DATA(self), |
| 9523 | PyUnicode_GET_LENGTH(self), maxcount |
| 9524 | ); |
| 9525 | else |
| 9526 | return ucs1lib_rsplit_whitespace( |
| 9527 | (PyObject*) self, PyUnicode_1BYTE_DATA(self), |
| 9528 | PyUnicode_GET_LENGTH(self), maxcount |
| 9529 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9530 | case PyUnicode_2BYTE_KIND: |
| 9531 | return ucs2lib_rsplit_whitespace( |
| 9532 | (PyObject*) self, PyUnicode_2BYTE_DATA(self), |
| 9533 | PyUnicode_GET_LENGTH(self), maxcount |
| 9534 | ); |
| 9535 | case PyUnicode_4BYTE_KIND: |
| 9536 | return ucs4lib_rsplit_whitespace( |
| 9537 | (PyObject*) self, PyUnicode_4BYTE_DATA(self), |
| 9538 | PyUnicode_GET_LENGTH(self), maxcount |
| 9539 | ); |
| 9540 | default: |
| 9541 | assert(0); |
| 9542 | return NULL; |
| 9543 | } |
| 9544 | |
| 9545 | if (PyUnicode_READY(substring) == -1) |
| 9546 | return NULL; |
| 9547 | |
| 9548 | kind1 = PyUnicode_KIND(self); |
| 9549 | kind2 = PyUnicode_KIND(substring); |
| 9550 | kind = kind1 > kind2 ? kind1 : kind2; |
| 9551 | buf1 = PyUnicode_DATA(self); |
| 9552 | buf2 = PyUnicode_DATA(substring); |
| 9553 | if (kind1 != kind) |
| 9554 | buf1 = _PyUnicode_AsKind((PyObject*)self, kind); |
| 9555 | if (!buf1) |
| 9556 | return NULL; |
| 9557 | if (kind2 != kind) |
| 9558 | buf2 = _PyUnicode_AsKind((PyObject*)substring, kind); |
| 9559 | if (!buf2) { |
| 9560 | if (kind1 != kind) PyMem_Free(buf1); |
| 9561 | return NULL; |
| 9562 | } |
| 9563 | len1 = PyUnicode_GET_LENGTH(self); |
| 9564 | len2 = PyUnicode_GET_LENGTH(substring); |
| 9565 | |
| 9566 | switch(kind) { |
| 9567 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9568 | if (PyUnicode_IS_ASCII(self) && PyUnicode_IS_ASCII(substring)) |
| 9569 | out = asciilib_rsplit( |
| 9570 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
| 9571 | else |
| 9572 | out = ucs1lib_rsplit( |
| 9573 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9574 | break; |
| 9575 | case PyUnicode_2BYTE_KIND: |
| 9576 | out = ucs2lib_rsplit( |
| 9577 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
| 9578 | break; |
| 9579 | case PyUnicode_4BYTE_KIND: |
| 9580 | out = ucs4lib_rsplit( |
| 9581 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
| 9582 | break; |
| 9583 | default: |
| 9584 | out = NULL; |
| 9585 | } |
| 9586 | if (kind1 != kind) |
| 9587 | PyMem_Free(buf1); |
| 9588 | if (kind2 != kind) |
| 9589 | PyMem_Free(buf2); |
| 9590 | return out; |
| 9591 | } |
| 9592 | |
| 9593 | static Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9594 | anylib_find(int kind, PyObject *str1, void *buf1, Py_ssize_t len1, |
| 9595 | 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] | 9596 | { |
| 9597 | switch(kind) { |
| 9598 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9599 | if (PyUnicode_IS_ASCII(str1) && PyUnicode_IS_ASCII(str2)) |
| 9600 | return asciilib_find(buf1, len1, buf2, len2, offset); |
| 9601 | else |
| 9602 | return ucs1lib_find(buf1, len1, buf2, len2, offset); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9603 | case PyUnicode_2BYTE_KIND: |
| 9604 | return ucs2lib_find(buf1, len1, buf2, len2, offset); |
| 9605 | case PyUnicode_4BYTE_KIND: |
| 9606 | return ucs4lib_find(buf1, len1, buf2, len2, offset); |
| 9607 | } |
| 9608 | assert(0); |
| 9609 | return -1; |
| 9610 | } |
| 9611 | |
| 9612 | static Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9613 | anylib_count(int kind, PyObject *sstr, void* sbuf, Py_ssize_t slen, |
| 9614 | 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] | 9615 | { |
| 9616 | switch(kind) { |
| 9617 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9618 | if (PyUnicode_IS_ASCII(sstr) && PyUnicode_IS_ASCII(str1)) |
| 9619 | return asciilib_count(sbuf, slen, buf1, len1, maxcount); |
| 9620 | else |
| 9621 | return ucs1lib_count(sbuf, slen, buf1, len1, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9622 | case PyUnicode_2BYTE_KIND: |
| 9623 | return ucs2lib_count(sbuf, slen, buf1, len1, maxcount); |
| 9624 | case PyUnicode_4BYTE_KIND: |
| 9625 | return ucs4lib_count(sbuf, slen, buf1, len1, maxcount); |
| 9626 | } |
| 9627 | assert(0); |
| 9628 | return 0; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9629 | } |
| 9630 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9631 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9632 | replace(PyObject *self, PyObject *str1, |
| 9633 | PyObject *str2, Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9634 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9635 | PyObject *u; |
| 9636 | char *sbuf = PyUnicode_DATA(self); |
| 9637 | char *buf1 = PyUnicode_DATA(str1); |
| 9638 | char *buf2 = PyUnicode_DATA(str2); |
| 9639 | int srelease = 0, release1 = 0, release2 = 0; |
| 9640 | int skind = PyUnicode_KIND(self); |
| 9641 | int kind1 = PyUnicode_KIND(str1); |
| 9642 | int kind2 = PyUnicode_KIND(str2); |
| 9643 | Py_ssize_t slen = PyUnicode_GET_LENGTH(self); |
| 9644 | Py_ssize_t len1 = PyUnicode_GET_LENGTH(str1); |
| 9645 | Py_ssize_t len2 = PyUnicode_GET_LENGTH(str2); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9646 | |
| 9647 | if (maxcount < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9648 | maxcount = PY_SSIZE_T_MAX; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9649 | else if (maxcount == 0 || slen == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9650 | goto nothing; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9651 | |
Victor Stinner | 59de0ee | 2011-10-07 10:01:28 +0200 | [diff] [blame] | 9652 | if (str1 == str2) |
| 9653 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9654 | if (skind < kind1) |
| 9655 | /* substring too wide to be present */ |
| 9656 | goto nothing; |
| 9657 | |
| 9658 | if (len1 == len2) { |
Antoine Pitrou | cbfdee3 | 2010-01-13 08:58:08 +0000 | [diff] [blame] | 9659 | Py_ssize_t i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9660 | /* same length */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9661 | if (len1 == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9662 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9663 | if (len1 == 1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9664 | /* replace characters */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9665 | Py_UCS4 u1, u2, maxchar; |
| 9666 | int mayshrink, rkind; |
| 9667 | u1 = PyUnicode_READ_CHAR(str1, 0); |
| 9668 | if (!findchar(sbuf, PyUnicode_KIND(self), |
| 9669 | slen, u1, 1)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9670 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9671 | u2 = PyUnicode_READ_CHAR(str2, 0); |
| 9672 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
| 9673 | /* Replacing u1 with u2 may cause a maxchar reduction in the |
| 9674 | result string. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9675 | if (u2 > maxchar) { |
| 9676 | maxchar = u2; |
| 9677 | mayshrink = 0; |
| 9678 | } |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 9679 | else |
| 9680 | mayshrink = maxchar > 127; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9681 | u = PyUnicode_New(slen, maxchar); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9682 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9683 | goto error; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9684 | copy_characters(u, 0, self, 0, slen); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9685 | rkind = PyUnicode_KIND(u); |
| 9686 | for (i = 0; i < PyUnicode_GET_LENGTH(u); i++) |
| 9687 | if (PyUnicode_READ(rkind, PyUnicode_DATA(u), i) == u1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9688 | if (--maxcount < 0) |
| 9689 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9690 | PyUnicode_WRITE(rkind, PyUnicode_DATA(u), i, u2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9691 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9692 | if (mayshrink) { |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 9693 | unicode_adjust_maxchar(&u); |
| 9694 | if (u == NULL) |
| 9695 | goto error; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9696 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9697 | } else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9698 | int rkind = skind; |
| 9699 | char *res; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 9700 | PyObject *rstr; |
| 9701 | Py_UCS4 maxchar; |
| 9702 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9703 | if (kind1 < rkind) { |
| 9704 | /* widen substring */ |
| 9705 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 9706 | if (!buf1) goto error; |
| 9707 | release1 = 1; |
| 9708 | } |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9709 | i = anylib_find(rkind, self, sbuf, slen, str1, buf1, len1, 0); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9710 | if (i < 0) |
| 9711 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9712 | if (rkind > kind2) { |
| 9713 | /* widen replacement */ |
| 9714 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 9715 | if (!buf2) goto error; |
| 9716 | release2 = 1; |
| 9717 | } |
| 9718 | else if (rkind < kind2) { |
| 9719 | /* widen self and buf1 */ |
| 9720 | rkind = kind2; |
| 9721 | if (release1) PyMem_Free(buf1); |
| 9722 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 9723 | if (!sbuf) goto error; |
| 9724 | srelease = 1; |
| 9725 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 9726 | if (!buf1) goto error; |
| 9727 | release1 = 1; |
| 9728 | } |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 9729 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
| 9730 | maxchar = Py_MAX(maxchar, PyUnicode_MAX_CHAR_VALUE(str2)); |
| 9731 | rstr = PyUnicode_New(slen, maxchar); |
| 9732 | if (!rstr) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9733 | goto error; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 9734 | res = PyUnicode_DATA(rstr); |
| 9735 | |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame^] | 9736 | memcpy(res, sbuf, rkind * slen); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9737 | /* change everything in-place, starting with this one */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame^] | 9738 | memcpy(res + rkind * i, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9739 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame^] | 9740 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9741 | i += len1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9742 | |
| 9743 | while ( --maxcount > 0) { |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9744 | i = anylib_find(rkind, self, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame^] | 9745 | sbuf+rkind*i, slen-i, |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9746 | str1, buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9747 | if (i == -1) |
| 9748 | break; |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame^] | 9749 | memcpy(res + rkind * i, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9750 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame^] | 9751 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9752 | i += len1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9753 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9754 | |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 9755 | u = rstr; |
| 9756 | unicode_adjust_maxchar(&u); |
| 9757 | if (!u) |
| 9758 | goto error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9759 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9760 | } else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9761 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9762 | Py_ssize_t n, i, j, ires; |
| 9763 | Py_ssize_t product, new_size; |
| 9764 | int rkind = skind; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 9765 | PyObject *rstr; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9766 | char *res; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 9767 | Py_UCS4 maxchar; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9768 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9769 | if (kind1 < rkind) { |
| 9770 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 9771 | if (!buf1) goto error; |
| 9772 | release1 = 1; |
| 9773 | } |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9774 | n = anylib_count(rkind, self, sbuf, slen, str1, buf1, len1, maxcount); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9775 | if (n == 0) |
| 9776 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9777 | if (kind2 < rkind) { |
| 9778 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 9779 | if (!buf2) goto error; |
| 9780 | release2 = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9781 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9782 | else if (kind2 > rkind) { |
| 9783 | rkind = kind2; |
| 9784 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 9785 | if (!sbuf) goto error; |
| 9786 | srelease = 1; |
| 9787 | if (release1) PyMem_Free(buf1); |
| 9788 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 9789 | if (!buf1) goto error; |
| 9790 | release1 = 1; |
| 9791 | } |
| 9792 | /* new_size = PyUnicode_GET_LENGTH(self) + n * (PyUnicode_GET_LENGTH(str2) - |
| 9793 | PyUnicode_GET_LENGTH(str1))); */ |
| 9794 | product = n * (len2-len1); |
| 9795 | if ((product / (len2-len1)) != n) { |
| 9796 | PyErr_SetString(PyExc_OverflowError, |
| 9797 | "replace string is too long"); |
| 9798 | goto error; |
| 9799 | } |
| 9800 | new_size = slen + product; |
| 9801 | if (new_size < 0 || new_size > (PY_SSIZE_T_MAX >> (rkind-1))) { |
| 9802 | PyErr_SetString(PyExc_OverflowError, |
| 9803 | "replace string is too long"); |
| 9804 | goto error; |
| 9805 | } |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 9806 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
| 9807 | maxchar = Py_MAX(maxchar, PyUnicode_MAX_CHAR_VALUE(str2)); |
| 9808 | rstr = PyUnicode_New(new_size, maxchar); |
| 9809 | if (!rstr) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9810 | goto error; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 9811 | res = PyUnicode_DATA(rstr); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9812 | ires = i = 0; |
| 9813 | if (len1 > 0) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9814 | while (n-- > 0) { |
| 9815 | /* look for next match */ |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9816 | j = anylib_find(rkind, self, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame^] | 9817 | sbuf + rkind * i, slen-i, |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9818 | str1, buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9819 | if (j == -1) |
| 9820 | break; |
| 9821 | else if (j > i) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9822 | /* copy unchanged part [i:j] */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame^] | 9823 | memcpy(res + rkind * ires, |
| 9824 | sbuf + rkind * i, |
| 9825 | rkind * (j-i)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9826 | ires += j - i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9827 | } |
| 9828 | /* copy substitution string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9829 | if (len2 > 0) { |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame^] | 9830 | memcpy(res + rkind * ires, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9831 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame^] | 9832 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9833 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9834 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9835 | i = j + len1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9836 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9837 | if (i < slen) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9838 | /* copy tail [i:] */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame^] | 9839 | memcpy(res + rkind * ires, |
| 9840 | sbuf + rkind * i, |
| 9841 | rkind * (slen-i)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9842 | } else { |
| 9843 | /* interleave */ |
| 9844 | while (n > 0) { |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame^] | 9845 | memcpy(res + rkind * ires, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9846 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame^] | 9847 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9848 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9849 | if (--n <= 0) |
| 9850 | break; |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame^] | 9851 | memcpy(res + rkind * ires, |
| 9852 | sbuf + rkind * i, |
| 9853 | rkind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9854 | ires++; |
| 9855 | i++; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9856 | } |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame^] | 9857 | memcpy(res + rkind * ires, |
| 9858 | sbuf + rkind * i, |
| 9859 | rkind * (slen-i)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9860 | } |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 9861 | u = rstr; |
| 9862 | unicode_adjust_maxchar(&u); |
| 9863 | if (u == NULL) |
| 9864 | goto error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9865 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9866 | if (srelease) |
| 9867 | PyMem_FREE(sbuf); |
| 9868 | if (release1) |
| 9869 | PyMem_FREE(buf1); |
| 9870 | if (release2) |
| 9871 | PyMem_FREE(buf2); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 9872 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9873 | return u; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9874 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9875 | nothing: |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9876 | /* nothing to replace; return original string (when possible) */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9877 | if (srelease) |
| 9878 | PyMem_FREE(sbuf); |
| 9879 | if (release1) |
| 9880 | PyMem_FREE(buf1); |
| 9881 | if (release2) |
| 9882 | PyMem_FREE(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9883 | if (PyUnicode_CheckExact(self)) { |
| 9884 | Py_INCREF(self); |
| 9885 | return (PyObject *) self; |
| 9886 | } |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 9887 | return PyUnicode_Copy(self); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9888 | error: |
| 9889 | if (srelease && sbuf) |
| 9890 | PyMem_FREE(sbuf); |
| 9891 | if (release1 && buf1) |
| 9892 | PyMem_FREE(buf1); |
| 9893 | if (release2 && buf2) |
| 9894 | PyMem_FREE(buf2); |
| 9895 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9896 | } |
| 9897 | |
| 9898 | /* --- Unicode Object Methods --------------------------------------------- */ |
| 9899 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9900 | PyDoc_STRVAR(title__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9901 | "S.title() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9902 | \n\ |
| 9903 | 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] | 9904 | characters, all remaining cased characters have lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9905 | |
| 9906 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9907 | unicode_title(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9908 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9909 | return fixup(self, fixtitle); |
| 9910 | } |
| 9911 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9912 | PyDoc_STRVAR(capitalize__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9913 | "S.capitalize() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9914 | \n\ |
| 9915 | 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] | 9916 | have upper case and the rest lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9917 | |
| 9918 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9919 | unicode_capitalize(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9920 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9921 | return fixup(self, fixcapitalize); |
| 9922 | } |
| 9923 | |
| 9924 | #if 0 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9925 | PyDoc_STRVAR(capwords__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9926 | "S.capwords() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9927 | \n\ |
| 9928 | 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] | 9929 | normalized whitespace (all whitespace strings are replaced by ' ')."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9930 | |
| 9931 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 9932 | unicode_capwords(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9933 | { |
| 9934 | PyObject *list; |
| 9935 | PyObject *item; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9936 | Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9937 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9938 | /* Split into words */ |
| 9939 | list = split(self, NULL, -1); |
| 9940 | if (!list) |
| 9941 | return NULL; |
| 9942 | |
| 9943 | /* Capitalize each word */ |
| 9944 | for (i = 0; i < PyList_GET_SIZE(list); i++) { |
| 9945 | item = fixup((PyUnicodeObject *)PyList_GET_ITEM(list, i), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9946 | fixcapitalize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9947 | if (item == NULL) |
| 9948 | goto onError; |
| 9949 | Py_DECREF(PyList_GET_ITEM(list, i)); |
| 9950 | PyList_SET_ITEM(list, i, item); |
| 9951 | } |
| 9952 | |
| 9953 | /* Join the words to form a new string */ |
| 9954 | item = PyUnicode_Join(NULL, list); |
| 9955 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9956 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9957 | Py_DECREF(list); |
| 9958 | return (PyObject *)item; |
| 9959 | } |
| 9960 | #endif |
| 9961 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 9962 | /* Argument converter. Coerces to a single unicode character */ |
| 9963 | |
| 9964 | static int |
| 9965 | convert_uc(PyObject *obj, void *addr) |
| 9966 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9967 | Py_UCS4 *fillcharloc = (Py_UCS4 *)addr; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9968 | PyObject *uniobj; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 9969 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9970 | uniobj = PyUnicode_FromObject(obj); |
| 9971 | if (uniobj == NULL) { |
| 9972 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9973 | "The fill character cannot be converted to Unicode"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9974 | return 0; |
| 9975 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9976 | if (PyUnicode_GET_LENGTH(uniobj) != 1) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9977 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9978 | "The fill character must be exactly one character long"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9979 | Py_DECREF(uniobj); |
| 9980 | return 0; |
| 9981 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9982 | *fillcharloc = PyUnicode_READ_CHAR(uniobj, 0); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9983 | Py_DECREF(uniobj); |
| 9984 | return 1; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 9985 | } |
| 9986 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9987 | PyDoc_STRVAR(center__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9988 | "S.center(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9989 | \n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 9990 | Return S centered in a string of length width. Padding is\n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 9991 | done using the specified fill character (default is a space)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9992 | |
| 9993 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9994 | unicode_center(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9995 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9996 | Py_ssize_t marg, left; |
| 9997 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9998 | Py_UCS4 fillchar = ' '; |
| 9999 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 10000 | 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] | 10001 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10002 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 10003 | if (PyUnicode_READY(self) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10004 | return NULL; |
| 10005 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10006 | if (_PyUnicode_LENGTH(self) >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10007 | Py_INCREF(self); |
| 10008 | return (PyObject*) self; |
| 10009 | } |
| 10010 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10011 | marg = width - _PyUnicode_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10012 | left = marg / 2 + (marg & width & 1); |
| 10013 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10014 | return pad(self, left, marg - left, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10015 | } |
| 10016 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10017 | #if 0 |
| 10018 | |
| 10019 | /* This code should go into some future Unicode collation support |
| 10020 | module. The basic comparison should compare ordinals on a naive |
Georg Brandl | c6c3178 | 2009-06-08 13:41:29 +0000 | [diff] [blame] | 10021 | basis (this is what Java does and thus Jython too). */ |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10022 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 10023 | /* speedy UTF-16 code point order comparison */ |
| 10024 | /* gleaned from: */ |
| 10025 | /* http://www-4.ibm.com/software/developer/library/utf16.html?dwzone=unicode */ |
| 10026 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 10027 | static short utf16Fixup[32] = |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 10028 | { |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 10029 | 0, 0, 0, 0, 0, 0, 0, 0, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 10030 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 10031 | 0, 0, 0, 0, 0, 0, 0, 0, |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 10032 | 0, 0, 0, 0x2000, -0x800, -0x800, -0x800, -0x800 |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 10033 | }; |
| 10034 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10035 | static int |
| 10036 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 10037 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10038 | Py_ssize_t len1, len2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 10039 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10040 | Py_UNICODE *s1 = str1->str; |
| 10041 | Py_UNICODE *s2 = str2->str; |
| 10042 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10043 | len1 = str1->_base._base.length; |
| 10044 | len2 = str2->_base._base.length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 10045 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10046 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 10047 | Py_UNICODE c1, c2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 10048 | |
| 10049 | c1 = *s1++; |
| 10050 | c2 = *s2++; |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 10051 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10052 | if (c1 > (1<<11) * 26) |
| 10053 | c1 += utf16Fixup[c1>>11]; |
| 10054 | if (c2 > (1<<11) * 26) |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 10055 | c2 += utf16Fixup[c2>>11]; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 10056 | /* now c1 and c2 are in UTF-32-compatible order */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 10057 | |
| 10058 | if (c1 != c2) |
| 10059 | return (c1 < c2) ? -1 : 1; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 10060 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 10061 | len1--; len2--; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10062 | } |
| 10063 | |
| 10064 | return (len1 < len2) ? -1 : (len1 != len2); |
| 10065 | } |
| 10066 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10067 | #else |
| 10068 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10069 | /* This function assumes that str1 and str2 are readied by the caller. */ |
| 10070 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10071 | static int |
| 10072 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 10073 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10074 | int kind1, kind2; |
| 10075 | void *data1, *data2; |
| 10076 | Py_ssize_t len1, len2, i; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10077 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10078 | kind1 = PyUnicode_KIND(str1); |
| 10079 | kind2 = PyUnicode_KIND(str2); |
| 10080 | data1 = PyUnicode_DATA(str1); |
| 10081 | data2 = PyUnicode_DATA(str2); |
| 10082 | len1 = PyUnicode_GET_LENGTH(str1); |
| 10083 | len2 = PyUnicode_GET_LENGTH(str2); |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10084 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10085 | for (i = 0; i < len1 && i < len2; ++i) { |
| 10086 | Py_UCS4 c1, c2; |
| 10087 | c1 = PyUnicode_READ(kind1, data1, i); |
| 10088 | c2 = PyUnicode_READ(kind2, data2, i); |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 10089 | |
| 10090 | if (c1 != c2) |
| 10091 | return (c1 < c2) ? -1 : 1; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10092 | } |
| 10093 | |
| 10094 | return (len1 < len2) ? -1 : (len1 != len2); |
| 10095 | } |
| 10096 | |
| 10097 | #endif |
| 10098 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10099 | int |
| 10100 | PyUnicode_Compare(PyObject *left, PyObject *right) |
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 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 10103 | if (PyUnicode_READY(left) == -1 || |
| 10104 | PyUnicode_READY(right) == -1) |
| 10105 | return -1; |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 10106 | return unicode_compare((PyUnicodeObject *)left, |
| 10107 | (PyUnicodeObject *)right); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10108 | } |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 10109 | PyErr_Format(PyExc_TypeError, |
| 10110 | "Can't compare %.100s and %.100s", |
| 10111 | left->ob_type->tp_name, |
| 10112 | right->ob_type->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10113 | return -1; |
| 10114 | } |
| 10115 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10116 | int |
| 10117 | PyUnicode_CompareWithASCIIString(PyObject* uni, const char* str) |
| 10118 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10119 | Py_ssize_t i; |
| 10120 | int kind; |
| 10121 | void *data; |
| 10122 | Py_UCS4 chr; |
| 10123 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 10124 | assert(_PyUnicode_CHECK(uni)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10125 | if (PyUnicode_READY(uni) == -1) |
| 10126 | return -1; |
| 10127 | kind = PyUnicode_KIND(uni); |
| 10128 | data = PyUnicode_DATA(uni); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10129 | /* Compare Unicode string and source character set string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10130 | for (i = 0; (chr = PyUnicode_READ(kind, data, i)) && str[i]; i++) |
| 10131 | if (chr != str[i]) |
| 10132 | return (chr < (unsigned char)(str[i])) ? -1 : 1; |
Benjamin Peterson | 8667a9b | 2010-01-09 21:45:28 +0000 | [diff] [blame] | 10133 | /* This check keeps Python strings that end in '\0' from comparing equal |
| 10134 | to C strings identical up to that point. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10135 | if (PyUnicode_GET_LENGTH(uni) != i || chr) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10136 | return 1; /* uni is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10137 | if (str[i]) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10138 | return -1; /* str is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10139 | return 0; |
| 10140 | } |
| 10141 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10142 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10143 | #define TEST_COND(cond) \ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10144 | ((cond) ? Py_True : Py_False) |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10145 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10146 | PyObject * |
| 10147 | PyUnicode_RichCompare(PyObject *left, PyObject *right, int op) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10148 | { |
| 10149 | int result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10150 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10151 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 10152 | PyObject *v; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10153 | if (PyUnicode_READY(left) == -1 || |
| 10154 | PyUnicode_READY(right) == -1) |
| 10155 | return NULL; |
| 10156 | if (PyUnicode_GET_LENGTH(left) != PyUnicode_GET_LENGTH(right) || |
| 10157 | PyUnicode_KIND(left) != PyUnicode_KIND(right)) { |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10158 | if (op == Py_EQ) { |
| 10159 | Py_INCREF(Py_False); |
| 10160 | return Py_False; |
| 10161 | } |
| 10162 | if (op == Py_NE) { |
| 10163 | Py_INCREF(Py_True); |
| 10164 | return Py_True; |
| 10165 | } |
| 10166 | } |
| 10167 | if (left == right) |
| 10168 | result = 0; |
| 10169 | else |
| 10170 | result = unicode_compare((PyUnicodeObject *)left, |
| 10171 | (PyUnicodeObject *)right); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10172 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10173 | /* Convert the return value to a Boolean */ |
| 10174 | switch (op) { |
| 10175 | case Py_EQ: |
| 10176 | v = TEST_COND(result == 0); |
| 10177 | break; |
| 10178 | case Py_NE: |
| 10179 | v = TEST_COND(result != 0); |
| 10180 | break; |
| 10181 | case Py_LE: |
| 10182 | v = TEST_COND(result <= 0); |
| 10183 | break; |
| 10184 | case Py_GE: |
| 10185 | v = TEST_COND(result >= 0); |
| 10186 | break; |
| 10187 | case Py_LT: |
| 10188 | v = TEST_COND(result == -1); |
| 10189 | break; |
| 10190 | case Py_GT: |
| 10191 | v = TEST_COND(result == 1); |
| 10192 | break; |
| 10193 | default: |
| 10194 | PyErr_BadArgument(); |
| 10195 | return NULL; |
| 10196 | } |
| 10197 | Py_INCREF(v); |
| 10198 | return v; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10199 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10200 | |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 10201 | Py_RETURN_NOTIMPLEMENTED; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10202 | } |
| 10203 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10204 | int |
| 10205 | PyUnicode_Contains(PyObject *container, PyObject *element) |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10206 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10207 | PyObject *str, *sub; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10208 | int kind1, kind2, kind; |
| 10209 | void *buf1, *buf2; |
| 10210 | Py_ssize_t len1, len2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10211 | int result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10212 | |
| 10213 | /* Coerce the two arguments */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10214 | sub = PyUnicode_FromObject(element); |
| 10215 | if (!sub) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10216 | PyErr_Format(PyExc_TypeError, |
| 10217 | "'in <string>' requires string as left operand, not %s", |
| 10218 | element->ob_type->tp_name); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10219 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10220 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10221 | if (PyUnicode_READY(sub) == -1) |
| 10222 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10223 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10224 | str = PyUnicode_FromObject(container); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 10225 | if (!str || PyUnicode_READY(str) == -1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10226 | Py_DECREF(sub); |
| 10227 | return -1; |
| 10228 | } |
| 10229 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10230 | kind1 = PyUnicode_KIND(str); |
| 10231 | kind2 = PyUnicode_KIND(sub); |
| 10232 | kind = kind1 > kind2 ? kind1 : kind2; |
| 10233 | buf1 = PyUnicode_DATA(str); |
| 10234 | buf2 = PyUnicode_DATA(sub); |
| 10235 | if (kind1 != kind) |
| 10236 | buf1 = _PyUnicode_AsKind((PyObject*)str, kind); |
| 10237 | if (!buf1) { |
| 10238 | Py_DECREF(sub); |
| 10239 | return -1; |
| 10240 | } |
| 10241 | if (kind2 != kind) |
| 10242 | buf2 = _PyUnicode_AsKind((PyObject*)sub, kind); |
| 10243 | if (!buf2) { |
| 10244 | Py_DECREF(sub); |
| 10245 | if (kind1 != kind) PyMem_Free(buf1); |
| 10246 | return -1; |
| 10247 | } |
| 10248 | len1 = PyUnicode_GET_LENGTH(str); |
| 10249 | len2 = PyUnicode_GET_LENGTH(sub); |
| 10250 | |
| 10251 | switch(kind) { |
| 10252 | case PyUnicode_1BYTE_KIND: |
| 10253 | result = ucs1lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10254 | break; |
| 10255 | case PyUnicode_2BYTE_KIND: |
| 10256 | result = ucs2lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10257 | break; |
| 10258 | case PyUnicode_4BYTE_KIND: |
| 10259 | result = ucs4lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10260 | break; |
| 10261 | default: |
| 10262 | result = -1; |
| 10263 | assert(0); |
| 10264 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10265 | |
| 10266 | Py_DECREF(str); |
| 10267 | Py_DECREF(sub); |
| 10268 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10269 | if (kind1 != kind) |
| 10270 | PyMem_Free(buf1); |
| 10271 | if (kind2 != kind) |
| 10272 | PyMem_Free(buf2); |
| 10273 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10274 | return result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10275 | } |
| 10276 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10277 | /* Concat to string or Unicode object giving a new Unicode object. */ |
| 10278 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10279 | PyObject * |
| 10280 | PyUnicode_Concat(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10281 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10282 | PyObject *u = NULL, *v = NULL, *w; |
| 10283 | Py_UCS4 maxchar; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10284 | |
| 10285 | /* Coerce the two arguments */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10286 | u = PyUnicode_FromObject(left); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10287 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10288 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10289 | v = PyUnicode_FromObject(right); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10290 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10291 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10292 | |
| 10293 | /* Shortcuts */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 10294 | if (v == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10295 | Py_DECREF(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10296 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10297 | } |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 10298 | if (u == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10299 | Py_DECREF(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10300 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10301 | } |
| 10302 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10303 | maxchar = PyUnicode_MAX_CHAR_VALUE(u); |
Victor Stinner | ff9e50f | 2011-09-28 22:17:19 +0200 | [diff] [blame] | 10304 | maxchar = Py_MAX(maxchar, PyUnicode_MAX_CHAR_VALUE(v)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10305 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10306 | /* Concat the two Unicode strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10307 | w = PyUnicode_New( |
| 10308 | PyUnicode_GET_LENGTH(u) + PyUnicode_GET_LENGTH(v), |
| 10309 | maxchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10310 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10311 | goto onError; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 10312 | copy_characters(w, 0, u, 0, PyUnicode_GET_LENGTH(u)); |
| 10313 | 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] | 10314 | Py_DECREF(u); |
| 10315 | Py_DECREF(v); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10316 | assert(_PyUnicode_CheckConsistency(w, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10317 | return w; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10318 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10319 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10320 | Py_XDECREF(u); |
| 10321 | Py_XDECREF(v); |
| 10322 | return NULL; |
| 10323 | } |
| 10324 | |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10325 | static void |
| 10326 | unicode_append_inplace(PyObject **p_left, PyObject *right) |
| 10327 | { |
| 10328 | Py_ssize_t left_len, right_len, new_len; |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10329 | |
| 10330 | assert(PyUnicode_IS_READY(*p_left)); |
| 10331 | assert(PyUnicode_IS_READY(right)); |
| 10332 | |
| 10333 | left_len = PyUnicode_GET_LENGTH(*p_left); |
| 10334 | right_len = PyUnicode_GET_LENGTH(right); |
| 10335 | if (left_len > PY_SSIZE_T_MAX - right_len) { |
| 10336 | PyErr_SetString(PyExc_OverflowError, |
| 10337 | "strings are too large to concat"); |
| 10338 | goto error; |
| 10339 | } |
| 10340 | new_len = left_len + right_len; |
| 10341 | |
| 10342 | /* Now we own the last reference to 'left', so we can resize it |
| 10343 | * in-place. |
| 10344 | */ |
| 10345 | if (unicode_resize(p_left, new_len) != 0) { |
| 10346 | /* XXX if _PyUnicode_Resize() fails, 'left' has been |
| 10347 | * deallocated so it cannot be put back into |
| 10348 | * 'variable'. The MemoryError is raised when there |
| 10349 | * is no value in 'variable', which might (very |
| 10350 | * remotely) be a cause of incompatibilities. |
| 10351 | */ |
| 10352 | goto error; |
| 10353 | } |
| 10354 | /* copy 'right' into the newly allocated area of 'left' */ |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 10355 | copy_characters(*p_left, left_len, right, 0, right_len); |
| 10356 | _PyUnicode_DIRTY(*p_left); |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10357 | return; |
| 10358 | |
| 10359 | error: |
| 10360 | Py_DECREF(*p_left); |
| 10361 | *p_left = NULL; |
| 10362 | } |
| 10363 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10364 | void |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10365 | PyUnicode_Append(PyObject **p_left, PyObject *right) |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10366 | { |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10367 | PyObject *left, *res; |
| 10368 | |
| 10369 | if (p_left == NULL) { |
| 10370 | if (!PyErr_Occurred()) |
| 10371 | PyErr_BadInternalCall(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10372 | return; |
| 10373 | } |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10374 | left = *p_left; |
| 10375 | if (right == NULL || !PyUnicode_Check(left)) { |
| 10376 | if (!PyErr_Occurred()) |
| 10377 | PyErr_BadInternalCall(); |
| 10378 | goto error; |
| 10379 | } |
| 10380 | |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 10381 | if (PyUnicode_READY(left)) |
| 10382 | goto error; |
| 10383 | if (PyUnicode_READY(right)) |
| 10384 | goto error; |
| 10385 | |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10386 | if (PyUnicode_CheckExact(left) && left != unicode_empty |
| 10387 | && PyUnicode_CheckExact(right) && right != unicode_empty |
| 10388 | && unicode_resizable(left) |
| 10389 | && (_PyUnicode_KIND(right) <= _PyUnicode_KIND(left) |
| 10390 | || _PyUnicode_WSTR(left) != NULL)) |
| 10391 | { |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10392 | /* Don't resize for ascii += latin1. Convert ascii to latin1 requires |
| 10393 | to change the structure size, but characters are stored just after |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 10394 | the structure, and so it requires to move all characters which is |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10395 | not so different than duplicating the string. */ |
| 10396 | if (!(PyUnicode_IS_ASCII(left) && !PyUnicode_IS_ASCII(right))) |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10397 | { |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10398 | unicode_append_inplace(p_left, right); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10399 | if (p_left != NULL) |
| 10400 | assert(_PyUnicode_CheckConsistency(*p_left, 1)); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10401 | return; |
| 10402 | } |
| 10403 | } |
| 10404 | |
| 10405 | res = PyUnicode_Concat(left, right); |
| 10406 | if (res == NULL) |
| 10407 | goto error; |
| 10408 | Py_DECREF(left); |
| 10409 | *p_left = res; |
| 10410 | return; |
| 10411 | |
| 10412 | error: |
| 10413 | Py_DECREF(*p_left); |
| 10414 | *p_left = NULL; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10415 | } |
| 10416 | |
| 10417 | void |
| 10418 | PyUnicode_AppendAndDel(PyObject **pleft, PyObject *right) |
| 10419 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10420 | PyUnicode_Append(pleft, right); |
| 10421 | Py_XDECREF(right); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10422 | } |
| 10423 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10424 | PyDoc_STRVAR(count__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10425 | "S.count(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10426 | \n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10427 | Return the number of non-overlapping occurrences of substring sub in\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 10428 | 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] | 10429 | interpreted as in slice notation."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10430 | |
| 10431 | static PyObject * |
| 10432 | unicode_count(PyUnicodeObject *self, PyObject *args) |
| 10433 | { |
| 10434 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10435 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10436 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10437 | PyObject *result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10438 | int kind1, kind2, kind; |
| 10439 | void *buf1, *buf2; |
| 10440 | Py_ssize_t len1, len2, iresult; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10441 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10442 | if (!stringlib_parse_args_finds_unicode("count", args, &substring, |
| 10443 | &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10444 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 10445 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10446 | kind1 = PyUnicode_KIND(self); |
| 10447 | kind2 = PyUnicode_KIND(substring); |
| 10448 | kind = kind1 > kind2 ? kind1 : kind2; |
| 10449 | buf1 = PyUnicode_DATA(self); |
| 10450 | buf2 = PyUnicode_DATA(substring); |
| 10451 | if (kind1 != kind) |
| 10452 | buf1 = _PyUnicode_AsKind((PyObject*)self, kind); |
| 10453 | if (!buf1) { |
| 10454 | Py_DECREF(substring); |
| 10455 | return NULL; |
| 10456 | } |
| 10457 | if (kind2 != kind) |
| 10458 | buf2 = _PyUnicode_AsKind((PyObject*)substring, kind); |
| 10459 | if (!buf2) { |
| 10460 | Py_DECREF(substring); |
| 10461 | if (kind1 != kind) PyMem_Free(buf1); |
| 10462 | return NULL; |
| 10463 | } |
| 10464 | len1 = PyUnicode_GET_LENGTH(self); |
| 10465 | len2 = PyUnicode_GET_LENGTH(substring); |
| 10466 | |
| 10467 | ADJUST_INDICES(start, end, len1); |
| 10468 | switch(kind) { |
| 10469 | case PyUnicode_1BYTE_KIND: |
| 10470 | iresult = ucs1lib_count( |
| 10471 | ((Py_UCS1*)buf1) + start, end - start, |
| 10472 | buf2, len2, PY_SSIZE_T_MAX |
| 10473 | ); |
| 10474 | break; |
| 10475 | case PyUnicode_2BYTE_KIND: |
| 10476 | iresult = ucs2lib_count( |
| 10477 | ((Py_UCS2*)buf1) + start, end - start, |
| 10478 | buf2, len2, PY_SSIZE_T_MAX |
| 10479 | ); |
| 10480 | break; |
| 10481 | case PyUnicode_4BYTE_KIND: |
| 10482 | iresult = ucs4lib_count( |
| 10483 | ((Py_UCS4*)buf1) + start, end - start, |
| 10484 | buf2, len2, PY_SSIZE_T_MAX |
| 10485 | ); |
| 10486 | break; |
| 10487 | default: |
| 10488 | assert(0); iresult = 0; |
| 10489 | } |
| 10490 | |
| 10491 | result = PyLong_FromSsize_t(iresult); |
| 10492 | |
| 10493 | if (kind1 != kind) |
| 10494 | PyMem_Free(buf1); |
| 10495 | if (kind2 != kind) |
| 10496 | PyMem_Free(buf2); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10497 | |
| 10498 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10499 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10500 | return result; |
| 10501 | } |
| 10502 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10503 | PyDoc_STRVAR(encode__doc__, |
Victor Stinner | c911bbf | 2010-11-07 19:04:46 +0000 | [diff] [blame] | 10504 | "S.encode(encoding='utf-8', errors='strict') -> bytes\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10505 | \n\ |
Victor Stinner | e14e212 | 2010-11-07 18:41:46 +0000 | [diff] [blame] | 10506 | Encode S using the codec registered for encoding. Default encoding\n\ |
| 10507 | 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] | 10508 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 10509 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 10510 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 10511 | codecs.register_error that can handle UnicodeEncodeErrors."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10512 | |
| 10513 | static PyObject * |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 10514 | unicode_encode(PyUnicodeObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10515 | { |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 10516 | static char *kwlist[] = {"encoding", "errors", 0}; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10517 | char *encoding = NULL; |
| 10518 | char *errors = NULL; |
Guido van Rossum | 35d9428 | 2007-08-27 18:20:11 +0000 | [diff] [blame] | 10519 | |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 10520 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:encode", |
| 10521 | kwlist, &encoding, &errors)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10522 | return NULL; |
Georg Brandl | 3b9406b | 2010-12-03 07:54:09 +0000 | [diff] [blame] | 10523 | return PyUnicode_AsEncodedString((PyObject *)self, encoding, errors); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 10524 | } |
| 10525 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10526 | PyDoc_STRVAR(expandtabs__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10527 | "S.expandtabs([tabsize]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10528 | \n\ |
| 10529 | 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] | 10530 | 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] | 10531 | |
| 10532 | static PyObject* |
| 10533 | unicode_expandtabs(PyUnicodeObject *self, PyObject *args) |
| 10534 | { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10535 | Py_ssize_t i, j, line_pos, src_len, incr; |
| 10536 | Py_UCS4 ch; |
| 10537 | PyObject *u; |
| 10538 | void *src_data, *dest_data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10539 | int tabsize = 8; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10540 | int kind; |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 10541 | int found; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10542 | |
| 10543 | if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10544 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10545 | |
Antoine Pitrou | 2242522 | 2011-10-04 19:10:51 +0200 | [diff] [blame] | 10546 | if (PyUnicode_READY(self) == -1) |
| 10547 | return NULL; |
| 10548 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 10549 | /* First pass: determine size of output string */ |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10550 | src_len = PyUnicode_GET_LENGTH(self); |
| 10551 | i = j = line_pos = 0; |
| 10552 | kind = PyUnicode_KIND(self); |
| 10553 | src_data = PyUnicode_DATA(self); |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 10554 | found = 0; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10555 | for (; i < src_len; i++) { |
| 10556 | ch = PyUnicode_READ(kind, src_data, i); |
| 10557 | if (ch == '\t') { |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 10558 | found = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10559 | if (tabsize > 0) { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10560 | incr = tabsize - (line_pos % tabsize); /* cannot overflow */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10561 | if (j > PY_SSIZE_T_MAX - incr) |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10562 | goto overflow; |
| 10563 | line_pos += incr; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10564 | j += incr; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10565 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10566 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10567 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10568 | if (j > PY_SSIZE_T_MAX - 1) |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10569 | goto overflow; |
| 10570 | line_pos++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10571 | j++; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10572 | if (ch == '\n' || ch == '\r') |
| 10573 | line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10574 | } |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10575 | } |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 10576 | if (!found && PyUnicode_CheckExact(self)) { |
| 10577 | Py_INCREF((PyObject *) self); |
| 10578 | return (PyObject *) self; |
| 10579 | } |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 10580 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10581 | /* Second pass: create output string and fill it */ |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10582 | u = PyUnicode_New(j, PyUnicode_MAX_CHAR_VALUE(self)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10583 | if (!u) |
| 10584 | return NULL; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10585 | dest_data = PyUnicode_DATA(u); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10586 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10587 | i = j = line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10588 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10589 | for (; i < src_len; i++) { |
| 10590 | ch = PyUnicode_READ(kind, src_data, i); |
| 10591 | if (ch == '\t') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10592 | if (tabsize > 0) { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10593 | incr = tabsize - (line_pos % tabsize); |
| 10594 | line_pos += incr; |
| 10595 | while (incr--) { |
| 10596 | PyUnicode_WRITE(kind, dest_data, j, ' '); |
| 10597 | j++; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10598 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10599 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10600 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10601 | else { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10602 | line_pos++; |
| 10603 | PyUnicode_WRITE(kind, dest_data, j, ch); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10604 | j++; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10605 | if (ch == '\n' || ch == '\r') |
| 10606 | line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10607 | } |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10608 | } |
| 10609 | assert (j == PyUnicode_GET_LENGTH(u)); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 10610 | #ifndef DONT_MAKE_RESULT_READY |
| 10611 | if (_PyUnicode_READY_REPLACE(&u)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10612 | Py_DECREF(u); |
| 10613 | return NULL; |
| 10614 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 10615 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10616 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10617 | return (PyObject*) u; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10618 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10619 | overflow: |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10620 | PyErr_SetString(PyExc_OverflowError, "new string is too long"); |
| 10621 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10622 | } |
| 10623 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10624 | PyDoc_STRVAR(find__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10625 | "S.find(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10626 | \n\ |
| 10627 | Return the lowest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 10628 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10629 | arguments start and end are interpreted as in slice notation.\n\ |
| 10630 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10631 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10632 | |
| 10633 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10634 | unicode_find(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10635 | { |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10636 | PyUnicodeObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 10637 | Py_ssize_t start; |
| 10638 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10639 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10640 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10641 | if (!stringlib_parse_args_finds_unicode("find", args, &substring, |
| 10642 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10643 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10644 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10645 | if (PyUnicode_READY(self) == -1) |
| 10646 | return NULL; |
| 10647 | if (PyUnicode_READY(substring) == -1) |
| 10648 | return NULL; |
| 10649 | |
| 10650 | result = any_find_slice( |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10651 | asciilib_find_slice, ucs1lib_find_slice, |
| 10652 | ucs2lib_find_slice, ucs4lib_find_slice, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10653 | self, (PyObject*)substring, start, end |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10654 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10655 | |
| 10656 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10657 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10658 | if (result == -2) |
| 10659 | return NULL; |
| 10660 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 10661 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10662 | } |
| 10663 | |
| 10664 | static PyObject * |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 10665 | unicode_getitem(PyObject *self, Py_ssize_t index) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10666 | { |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 10667 | Py_UCS4 ch = PyUnicode_ReadChar(self, index); |
| 10668 | if (ch == (Py_UCS4)-1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10669 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10670 | return PyUnicode_FromOrdinal(ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10671 | } |
| 10672 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 10673 | /* Believe it or not, this produces the same value for ASCII strings |
Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 10674 | as bytes_hash(). */ |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 10675 | static Py_hash_t |
Neil Schemenauer | f8c37d1 | 2007-09-07 20:49:04 +0000 | [diff] [blame] | 10676 | unicode_hash(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10677 | { |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 10678 | Py_ssize_t len; |
Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 10679 | Py_uhash_t x; |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 10680 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10681 | if (_PyUnicode_HASH(self) != -1) |
| 10682 | return _PyUnicode_HASH(self); |
| 10683 | if (PyUnicode_READY(self) == -1) |
| 10684 | return -1; |
| 10685 | len = PyUnicode_GET_LENGTH(self); |
| 10686 | |
| 10687 | /* The hash function as a macro, gets expanded three times below. */ |
| 10688 | #define HASH(P) \ |
| 10689 | x = (Py_uhash_t)*P << 7; \ |
| 10690 | while (--len >= 0) \ |
| 10691 | x = (1000003*x) ^ (Py_uhash_t)*P++; |
| 10692 | |
| 10693 | switch (PyUnicode_KIND(self)) { |
| 10694 | case PyUnicode_1BYTE_KIND: { |
| 10695 | const unsigned char *c = PyUnicode_1BYTE_DATA(self); |
| 10696 | HASH(c); |
| 10697 | break; |
| 10698 | } |
| 10699 | case PyUnicode_2BYTE_KIND: { |
| 10700 | const Py_UCS2 *s = PyUnicode_2BYTE_DATA(self); |
| 10701 | HASH(s); |
| 10702 | break; |
| 10703 | } |
| 10704 | default: { |
| 10705 | Py_UCS4 *l; |
| 10706 | assert(PyUnicode_KIND(self) == PyUnicode_4BYTE_KIND && |
| 10707 | "Impossible switch case in unicode_hash"); |
| 10708 | l = PyUnicode_4BYTE_DATA(self); |
| 10709 | HASH(l); |
| 10710 | break; |
| 10711 | } |
| 10712 | } |
| 10713 | x ^= (Py_uhash_t)PyUnicode_GET_LENGTH(self); |
| 10714 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 10715 | if (x == -1) |
| 10716 | x = -2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10717 | _PyUnicode_HASH(self) = x; |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 10718 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10719 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10720 | #undef HASH |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10721 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10722 | PyDoc_STRVAR(index__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10723 | "S.index(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10724 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10725 | 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] | 10726 | |
| 10727 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10728 | unicode_index(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10729 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10730 | Py_ssize_t result; |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10731 | PyUnicodeObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 10732 | Py_ssize_t start; |
| 10733 | Py_ssize_t end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10734 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10735 | if (!stringlib_parse_args_finds_unicode("index", args, &substring, |
| 10736 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10737 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10738 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10739 | if (PyUnicode_READY(self) == -1) |
| 10740 | return NULL; |
| 10741 | if (PyUnicode_READY(substring) == -1) |
| 10742 | return NULL; |
| 10743 | |
| 10744 | result = any_find_slice( |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10745 | asciilib_find_slice, ucs1lib_find_slice, |
| 10746 | ucs2lib_find_slice, ucs4lib_find_slice, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10747 | self, (PyObject*)substring, start, end |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10748 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10749 | |
| 10750 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10751 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10752 | if (result == -2) |
| 10753 | return NULL; |
| 10754 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10755 | if (result < 0) { |
| 10756 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 10757 | return NULL; |
| 10758 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10759 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 10760 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10761 | } |
| 10762 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10763 | PyDoc_STRVAR(islower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10764 | "S.islower() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10765 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10766 | 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] | 10767 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10768 | |
| 10769 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10770 | unicode_islower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10771 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10772 | Py_ssize_t i, length; |
| 10773 | int kind; |
| 10774 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10775 | int cased; |
| 10776 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10777 | if (PyUnicode_READY(self) == -1) |
| 10778 | return NULL; |
| 10779 | length = PyUnicode_GET_LENGTH(self); |
| 10780 | kind = PyUnicode_KIND(self); |
| 10781 | data = PyUnicode_DATA(self); |
| 10782 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10783 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10784 | if (length == 1) |
| 10785 | return PyBool_FromLong( |
| 10786 | Py_UNICODE_ISLOWER(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10787 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10788 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10789 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10790 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10791 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10792 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10793 | for (i = 0; i < length; i++) { |
| 10794 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 10795 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10796 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 10797 | return PyBool_FromLong(0); |
| 10798 | else if (!cased && Py_UNICODE_ISLOWER(ch)) |
| 10799 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10800 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10801 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10802 | } |
| 10803 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10804 | PyDoc_STRVAR(isupper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10805 | "S.isupper() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10806 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 10807 | 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] | 10808 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10809 | |
| 10810 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10811 | unicode_isupper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10812 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10813 | Py_ssize_t i, length; |
| 10814 | int kind; |
| 10815 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10816 | int cased; |
| 10817 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10818 | if (PyUnicode_READY(self) == -1) |
| 10819 | return NULL; |
| 10820 | length = PyUnicode_GET_LENGTH(self); |
| 10821 | kind = PyUnicode_KIND(self); |
| 10822 | data = PyUnicode_DATA(self); |
| 10823 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10824 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10825 | if (length == 1) |
| 10826 | return PyBool_FromLong( |
| 10827 | Py_UNICODE_ISUPPER(PyUnicode_READ(kind, data, 0)) != 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10828 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10829 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10830 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10831 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10832 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10833 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10834 | for (i = 0; i < length; i++) { |
| 10835 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 10836 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10837 | if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 10838 | return PyBool_FromLong(0); |
| 10839 | else if (!cased && Py_UNICODE_ISUPPER(ch)) |
| 10840 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10841 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10842 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10843 | } |
| 10844 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10845 | PyDoc_STRVAR(istitle__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10846 | "S.istitle() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10847 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 10848 | Return True if S is a titlecased string and there is at least one\n\ |
| 10849 | character in S, i.e. upper- and titlecase characters may only\n\ |
| 10850 | follow uncased characters and lowercase characters only cased ones.\n\ |
| 10851 | Return False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10852 | |
| 10853 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10854 | unicode_istitle(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10855 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10856 | Py_ssize_t i, length; |
| 10857 | int kind; |
| 10858 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10859 | int cased, previous_is_cased; |
| 10860 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10861 | if (PyUnicode_READY(self) == -1) |
| 10862 | return NULL; |
| 10863 | length = PyUnicode_GET_LENGTH(self); |
| 10864 | kind = PyUnicode_KIND(self); |
| 10865 | data = PyUnicode_DATA(self); |
| 10866 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10867 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10868 | if (length == 1) { |
| 10869 | Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 10870 | return PyBool_FromLong((Py_UNICODE_ISTITLE(ch) != 0) || |
| 10871 | (Py_UNICODE_ISUPPER(ch) != 0)); |
| 10872 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10873 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10874 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10875 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10876 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10877 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10878 | cased = 0; |
| 10879 | previous_is_cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10880 | for (i = 0; i < length; i++) { |
| 10881 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 10882 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10883 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { |
| 10884 | if (previous_is_cased) |
| 10885 | return PyBool_FromLong(0); |
| 10886 | previous_is_cased = 1; |
| 10887 | cased = 1; |
| 10888 | } |
| 10889 | else if (Py_UNICODE_ISLOWER(ch)) { |
| 10890 | if (!previous_is_cased) |
| 10891 | return PyBool_FromLong(0); |
| 10892 | previous_is_cased = 1; |
| 10893 | cased = 1; |
| 10894 | } |
| 10895 | else |
| 10896 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10897 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10898 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10899 | } |
| 10900 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10901 | PyDoc_STRVAR(isspace__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10902 | "S.isspace() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10903 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 10904 | Return True if all characters in S are whitespace\n\ |
| 10905 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10906 | |
| 10907 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10908 | unicode_isspace(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10909 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10910 | Py_ssize_t i, length; |
| 10911 | int kind; |
| 10912 | void *data; |
| 10913 | |
| 10914 | if (PyUnicode_READY(self) == -1) |
| 10915 | return NULL; |
| 10916 | length = PyUnicode_GET_LENGTH(self); |
| 10917 | kind = PyUnicode_KIND(self); |
| 10918 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10919 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10920 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10921 | if (length == 1) |
| 10922 | return PyBool_FromLong( |
| 10923 | Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10924 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10925 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10926 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10927 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10928 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10929 | for (i = 0; i < length; i++) { |
| 10930 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 10931 | if (!Py_UNICODE_ISSPACE(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10932 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10933 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10934 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10935 | } |
| 10936 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10937 | PyDoc_STRVAR(isalpha__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10938 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10939 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 10940 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10941 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10942 | |
| 10943 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10944 | unicode_isalpha(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10945 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10946 | Py_ssize_t i, length; |
| 10947 | int kind; |
| 10948 | void *data; |
| 10949 | |
| 10950 | if (PyUnicode_READY(self) == -1) |
| 10951 | return NULL; |
| 10952 | length = PyUnicode_GET_LENGTH(self); |
| 10953 | kind = PyUnicode_KIND(self); |
| 10954 | data = PyUnicode_DATA(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10955 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10956 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10957 | if (length == 1) |
| 10958 | return PyBool_FromLong( |
| 10959 | Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, 0))); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10960 | |
| 10961 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10962 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10963 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10964 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10965 | for (i = 0; i < length; i++) { |
| 10966 | if (!Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10967 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10968 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10969 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10970 | } |
| 10971 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10972 | PyDoc_STRVAR(isalnum__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10973 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10974 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 10975 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10976 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10977 | |
| 10978 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10979 | unicode_isalnum(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10980 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10981 | int kind; |
| 10982 | void *data; |
| 10983 | Py_ssize_t len, i; |
| 10984 | |
| 10985 | if (PyUnicode_READY(self) == -1) |
| 10986 | return NULL; |
| 10987 | |
| 10988 | kind = PyUnicode_KIND(self); |
| 10989 | data = PyUnicode_DATA(self); |
| 10990 | len = PyUnicode_GET_LENGTH(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10991 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10992 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10993 | if (len == 1) { |
| 10994 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 10995 | return PyBool_FromLong(Py_UNICODE_ISALNUM(ch)); |
| 10996 | } |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10997 | |
| 10998 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10999 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11000 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11001 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11002 | for (i = 0; i < len; i++) { |
| 11003 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11004 | if (!Py_UNICODE_ISALNUM(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11005 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11006 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11007 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11008 | } |
| 11009 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11010 | PyDoc_STRVAR(isdecimal__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11011 | "S.isdecimal() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11012 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11013 | 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] | 11014 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11015 | |
| 11016 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11017 | unicode_isdecimal(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11018 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11019 | Py_ssize_t i, length; |
| 11020 | int kind; |
| 11021 | void *data; |
| 11022 | |
| 11023 | if (PyUnicode_READY(self) == -1) |
| 11024 | return NULL; |
| 11025 | length = PyUnicode_GET_LENGTH(self); |
| 11026 | kind = PyUnicode_KIND(self); |
| 11027 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11028 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11029 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11030 | if (length == 1) |
| 11031 | return PyBool_FromLong( |
| 11032 | Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11033 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11034 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11035 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11036 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11037 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11038 | for (i = 0; i < length; i++) { |
| 11039 | if (!Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11040 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11041 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11042 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11043 | } |
| 11044 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11045 | PyDoc_STRVAR(isdigit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11046 | "S.isdigit() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11047 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11048 | Return True if all characters in S are digits\n\ |
| 11049 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11050 | |
| 11051 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11052 | unicode_isdigit(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11053 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11054 | Py_ssize_t i, length; |
| 11055 | int kind; |
| 11056 | void *data; |
| 11057 | |
| 11058 | if (PyUnicode_READY(self) == -1) |
| 11059 | return NULL; |
| 11060 | length = PyUnicode_GET_LENGTH(self); |
| 11061 | kind = PyUnicode_KIND(self); |
| 11062 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11063 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11064 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11065 | if (length == 1) { |
| 11066 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11067 | return PyBool_FromLong(Py_UNICODE_ISDIGIT(ch)); |
| 11068 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11069 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11070 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11071 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11072 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11073 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11074 | for (i = 0; i < length; i++) { |
| 11075 | if (!Py_UNICODE_ISDIGIT(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11076 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11077 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11078 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11079 | } |
| 11080 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11081 | PyDoc_STRVAR(isnumeric__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11082 | "S.isnumeric() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11083 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11084 | 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] | 11085 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11086 | |
| 11087 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11088 | unicode_isnumeric(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11089 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11090 | Py_ssize_t i, length; |
| 11091 | int kind; |
| 11092 | void *data; |
| 11093 | |
| 11094 | if (PyUnicode_READY(self) == -1) |
| 11095 | return NULL; |
| 11096 | length = PyUnicode_GET_LENGTH(self); |
| 11097 | kind = PyUnicode_KIND(self); |
| 11098 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11099 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11100 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11101 | if (length == 1) |
| 11102 | return PyBool_FromLong( |
| 11103 | Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11104 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11105 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11106 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11107 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11108 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11109 | for (i = 0; i < length; i++) { |
| 11110 | if (!Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11111 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11112 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11113 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11114 | } |
| 11115 | |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11116 | int |
| 11117 | PyUnicode_IsIdentifier(PyObject *self) |
| 11118 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11119 | int kind; |
| 11120 | void *data; |
| 11121 | Py_ssize_t i; |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11122 | Py_UCS4 first; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11123 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11124 | if (PyUnicode_READY(self) == -1) { |
| 11125 | Py_FatalError("identifier not ready"); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11126 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11127 | } |
| 11128 | |
| 11129 | /* Special case for empty strings */ |
| 11130 | if (PyUnicode_GET_LENGTH(self) == 0) |
| 11131 | return 0; |
| 11132 | kind = PyUnicode_KIND(self); |
| 11133 | data = PyUnicode_DATA(self); |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11134 | |
| 11135 | /* PEP 3131 says that the first character must be in |
| 11136 | XID_Start and subsequent characters in XID_Continue, |
| 11137 | and for the ASCII range, the 2.x rules apply (i.e |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11138 | start with letters and underscore, continue with |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11139 | letters, digits, underscore). However, given the current |
| 11140 | definition of XID_Start and XID_Continue, it is sufficient |
| 11141 | to check just for these, except that _ must be allowed |
| 11142 | as starting an identifier. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11143 | first = PyUnicode_READ(kind, data, 0); |
Benjamin Peterson | f413b80 | 2011-08-12 22:17:18 -0500 | [diff] [blame] | 11144 | if (!_PyUnicode_IsXidStart(first) && first != 0x5F /* LOW LINE */) |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11145 | return 0; |
| 11146 | |
Benjamin Peterson | 9c6e6a0 | 2011-09-28 08:09:05 -0400 | [diff] [blame] | 11147 | for (i = 1; i < PyUnicode_GET_LENGTH(self); i++) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11148 | if (!_PyUnicode_IsXidContinue(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11149 | return 0; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11150 | return 1; |
| 11151 | } |
| 11152 | |
| 11153 | PyDoc_STRVAR(isidentifier__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11154 | "S.isidentifier() -> bool\n\ |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11155 | \n\ |
| 11156 | Return True if S is a valid identifier according\n\ |
| 11157 | to the language definition."); |
| 11158 | |
| 11159 | static PyObject* |
| 11160 | unicode_isidentifier(PyObject *self) |
| 11161 | { |
| 11162 | return PyBool_FromLong(PyUnicode_IsIdentifier(self)); |
| 11163 | } |
| 11164 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11165 | PyDoc_STRVAR(isprintable__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11166 | "S.isprintable() -> bool\n\ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11167 | \n\ |
| 11168 | Return True if all characters in S are considered\n\ |
| 11169 | printable in repr() or S is empty, False otherwise."); |
| 11170 | |
| 11171 | static PyObject* |
| 11172 | unicode_isprintable(PyObject *self) |
| 11173 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11174 | Py_ssize_t i, length; |
| 11175 | int kind; |
| 11176 | void *data; |
| 11177 | |
| 11178 | if (PyUnicode_READY(self) == -1) |
| 11179 | return NULL; |
| 11180 | length = PyUnicode_GET_LENGTH(self); |
| 11181 | kind = PyUnicode_KIND(self); |
| 11182 | data = PyUnicode_DATA(self); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11183 | |
| 11184 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11185 | if (length == 1) |
| 11186 | return PyBool_FromLong( |
| 11187 | Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, 0))); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11188 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11189 | for (i = 0; i < length; i++) { |
| 11190 | if (!Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, i))) { |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11191 | Py_RETURN_FALSE; |
| 11192 | } |
| 11193 | } |
| 11194 | Py_RETURN_TRUE; |
| 11195 | } |
| 11196 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11197 | PyDoc_STRVAR(join__doc__, |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 11198 | "S.join(iterable) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11199 | \n\ |
| 11200 | 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] | 11201 | iterable. The separator between elements is S."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11202 | |
| 11203 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11204 | unicode_join(PyObject *self, PyObject *data) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11205 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11206 | return PyUnicode_Join(self, data); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11207 | } |
| 11208 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11209 | static Py_ssize_t |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11210 | unicode_length(PyUnicodeObject *self) |
| 11211 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11212 | if (PyUnicode_READY(self) == -1) |
| 11213 | return -1; |
| 11214 | return PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11215 | } |
| 11216 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11217 | PyDoc_STRVAR(ljust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11218 | "S.ljust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11219 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 11220 | 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] | 11221 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11222 | |
| 11223 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11224 | unicode_ljust(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11225 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11226 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11227 | Py_UCS4 fillchar = ' '; |
| 11228 | |
| 11229 | if (PyUnicode_READY(self) == -1) |
| 11230 | return NULL; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 11231 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11232 | if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11233 | return NULL; |
| 11234 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11235 | if (_PyUnicode_LENGTH(self) >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11236 | Py_INCREF(self); |
| 11237 | return (PyObject*) self; |
| 11238 | } |
| 11239 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11240 | return (PyObject*) pad(self, 0, width - _PyUnicode_LENGTH(self), fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11241 | } |
| 11242 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11243 | PyDoc_STRVAR(lower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11244 | "S.lower() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11245 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11246 | Return a copy of the string S converted to lowercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11247 | |
| 11248 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11249 | unicode_lower(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11250 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11251 | return fixup(self, fixlower); |
| 11252 | } |
| 11253 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11254 | #define LEFTSTRIP 0 |
| 11255 | #define RIGHTSTRIP 1 |
| 11256 | #define BOTHSTRIP 2 |
| 11257 | |
| 11258 | /* Arrays indexed by above */ |
| 11259 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 11260 | |
| 11261 | #define STRIPNAME(i) (stripformat[i]+3) |
| 11262 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11263 | /* externally visible for str.strip(unicode) */ |
| 11264 | PyObject * |
| 11265 | _PyUnicode_XStrip(PyUnicodeObject *self, int striptype, PyObject *sepobj) |
| 11266 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11267 | void *data; |
| 11268 | int kind; |
| 11269 | Py_ssize_t i, j, len; |
| 11270 | BLOOM_MASK sepmask; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11271 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11272 | if (PyUnicode_READY(self) == -1 || PyUnicode_READY(sepobj) == -1) |
| 11273 | return NULL; |
| 11274 | |
| 11275 | kind = PyUnicode_KIND(self); |
| 11276 | data = PyUnicode_DATA(self); |
| 11277 | len = PyUnicode_GET_LENGTH(self); |
| 11278 | sepmask = make_bloom_mask(PyUnicode_KIND(sepobj), |
| 11279 | PyUnicode_DATA(sepobj), |
| 11280 | PyUnicode_GET_LENGTH(sepobj)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11281 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11282 | i = 0; |
| 11283 | if (striptype != RIGHTSTRIP) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11284 | while (i < len && |
| 11285 | BLOOM_MEMBER(sepmask, PyUnicode_READ(kind, data, i), sepobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11286 | i++; |
| 11287 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11288 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11289 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11290 | j = len; |
| 11291 | if (striptype != LEFTSTRIP) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11292 | do { |
| 11293 | j--; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11294 | } while (j >= i && |
| 11295 | BLOOM_MEMBER(sepmask, PyUnicode_READ(kind, data, j), sepobj)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11296 | j++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11297 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11298 | |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11299 | return PyUnicode_Substring((PyObject*)self, i, j); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11300 | } |
| 11301 | |
| 11302 | PyObject* |
| 11303 | PyUnicode_Substring(PyObject *self, Py_ssize_t start, Py_ssize_t end) |
| 11304 | { |
| 11305 | unsigned char *data; |
| 11306 | int kind; |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11307 | Py_ssize_t length; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11308 | |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 11309 | if (PyUnicode_READY(self) == -1) |
| 11310 | return NULL; |
| 11311 | |
| 11312 | end = Py_MIN(end, PyUnicode_GET_LENGTH(self)); |
| 11313 | |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11314 | if (start == 0 && end == PyUnicode_GET_LENGTH(self)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11315 | { |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11316 | if (PyUnicode_CheckExact(self)) { |
| 11317 | Py_INCREF(self); |
| 11318 | return self; |
| 11319 | } |
| 11320 | else |
| 11321 | return PyUnicode_Copy(self); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11322 | } |
| 11323 | |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11324 | length = end - start; |
| 11325 | if (length == 1) |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 11326 | return unicode_getitem(self, start); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11327 | |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 11328 | if (start < 0 || end < 0) { |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11329 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 11330 | return NULL; |
| 11331 | } |
| 11332 | |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 11333 | if (PyUnicode_IS_ASCII(self)) { |
| 11334 | kind = PyUnicode_KIND(self); |
| 11335 | data = PyUnicode_1BYTE_DATA(self); |
| 11336 | return unicode_fromascii(data + start, length); |
| 11337 | } |
| 11338 | else { |
| 11339 | kind = PyUnicode_KIND(self); |
| 11340 | data = PyUnicode_1BYTE_DATA(self); |
| 11341 | return PyUnicode_FromKindAndData(kind, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame^] | 11342 | data + kind * start, |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 11343 | length); |
| 11344 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11345 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11346 | |
| 11347 | static PyObject * |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11348 | do_strip(PyUnicodeObject *self, int striptype) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11349 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11350 | int kind; |
| 11351 | void *data; |
| 11352 | Py_ssize_t len, i, j; |
| 11353 | |
| 11354 | if (PyUnicode_READY(self) == -1) |
| 11355 | return NULL; |
| 11356 | |
| 11357 | kind = PyUnicode_KIND(self); |
| 11358 | data = PyUnicode_DATA(self); |
| 11359 | len = PyUnicode_GET_LENGTH(self); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11360 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11361 | i = 0; |
| 11362 | if (striptype != RIGHTSTRIP) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11363 | while (i < len && Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, i))) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11364 | i++; |
| 11365 | } |
| 11366 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11367 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11368 | j = len; |
| 11369 | if (striptype != LEFTSTRIP) { |
| 11370 | do { |
| 11371 | j--; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11372 | } while (j >= i && Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, j))); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11373 | j++; |
| 11374 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11375 | |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11376 | return PyUnicode_Substring((PyObject*)self, i, j); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11377 | } |
| 11378 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11379 | |
| 11380 | static PyObject * |
| 11381 | do_argstrip(PyUnicodeObject *self, int striptype, PyObject *args) |
| 11382 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11383 | PyObject *sep = NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11384 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11385 | if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) |
| 11386 | return NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11387 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11388 | if (sep != NULL && sep != Py_None) { |
| 11389 | if (PyUnicode_Check(sep)) |
| 11390 | return _PyUnicode_XStrip(self, striptype, sep); |
| 11391 | else { |
| 11392 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11393 | "%s arg must be None or str", |
| 11394 | STRIPNAME(striptype)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11395 | return NULL; |
| 11396 | } |
| 11397 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11398 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11399 | return do_strip(self, striptype); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11400 | } |
| 11401 | |
| 11402 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11403 | PyDoc_STRVAR(strip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11404 | "S.strip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11405 | \n\ |
| 11406 | Return a copy of the string S with leading and trailing\n\ |
| 11407 | whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11408 | 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] | 11409 | |
| 11410 | static PyObject * |
| 11411 | unicode_strip(PyUnicodeObject *self, PyObject *args) |
| 11412 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11413 | if (PyTuple_GET_SIZE(args) == 0) |
| 11414 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 11415 | else |
| 11416 | return do_argstrip(self, BOTHSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11417 | } |
| 11418 | |
| 11419 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11420 | PyDoc_STRVAR(lstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11421 | "S.lstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11422 | \n\ |
| 11423 | Return a copy of the string S with leading whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11424 | 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] | 11425 | |
| 11426 | static PyObject * |
| 11427 | unicode_lstrip(PyUnicodeObject *self, PyObject *args) |
| 11428 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11429 | if (PyTuple_GET_SIZE(args) == 0) |
| 11430 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 11431 | else |
| 11432 | return do_argstrip(self, LEFTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11433 | } |
| 11434 | |
| 11435 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11436 | PyDoc_STRVAR(rstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11437 | "S.rstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11438 | \n\ |
| 11439 | Return a copy of the string S with trailing whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11440 | 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] | 11441 | |
| 11442 | static PyObject * |
| 11443 | unicode_rstrip(PyUnicodeObject *self, PyObject *args) |
| 11444 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11445 | if (PyTuple_GET_SIZE(args) == 0) |
| 11446 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 11447 | else |
| 11448 | return do_argstrip(self, RIGHTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11449 | } |
| 11450 | |
| 11451 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11452 | static PyObject* |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11453 | unicode_repeat(PyUnicodeObject *str, Py_ssize_t len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11454 | { |
| 11455 | PyUnicodeObject *u; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11456 | Py_ssize_t nchars, n; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11457 | |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 11458 | if (len < 1) { |
| 11459 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 11460 | return unicode_empty; |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 11461 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11462 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 11463 | if (len == 1 && PyUnicode_CheckExact(str)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11464 | /* no repeat, return original string */ |
| 11465 | Py_INCREF(str); |
| 11466 | return (PyObject*) str; |
| 11467 | } |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 11468 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11469 | if (PyUnicode_READY(str) == -1) |
| 11470 | return NULL; |
| 11471 | |
Victor Stinner | c759f3e | 2011-10-01 03:09:58 +0200 | [diff] [blame] | 11472 | if (PyUnicode_GET_LENGTH(str) > PY_SSIZE_T_MAX / len) { |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11473 | PyErr_SetString(PyExc_OverflowError, |
| 11474 | "repeated string is too long"); |
| 11475 | return NULL; |
| 11476 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11477 | nchars = len * PyUnicode_GET_LENGTH(str); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11478 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11479 | u = (PyUnicodeObject *)PyUnicode_New(nchars, PyUnicode_MAX_CHAR_VALUE(str)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11480 | if (!u) |
| 11481 | return NULL; |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11482 | assert(PyUnicode_KIND(u) == PyUnicode_KIND(str)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11483 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11484 | if (PyUnicode_GET_LENGTH(str) == 1) { |
| 11485 | const int kind = PyUnicode_KIND(str); |
| 11486 | const Py_UCS4 fill_char = PyUnicode_READ(kind, PyUnicode_DATA(str), 0); |
| 11487 | void *to = PyUnicode_DATA(u); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11488 | if (kind == PyUnicode_1BYTE_KIND) |
| 11489 | memset(to, (unsigned char)fill_char, len); |
| 11490 | else { |
| 11491 | for (n = 0; n < len; ++n) |
| 11492 | PyUnicode_WRITE(kind, to, n, fill_char); |
| 11493 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11494 | } |
| 11495 | else { |
| 11496 | /* number of characters copied this far */ |
| 11497 | Py_ssize_t done = PyUnicode_GET_LENGTH(str); |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame^] | 11498 | const Py_ssize_t char_size = PyUnicode_KIND(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11499 | char *to = (char *) PyUnicode_DATA(u); |
| 11500 | Py_MEMCPY(to, PyUnicode_DATA(str), |
| 11501 | PyUnicode_GET_LENGTH(str) * char_size); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11502 | while (done < nchars) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11503 | n = (done <= nchars-done) ? done : nchars-done; |
| 11504 | Py_MEMCPY(to + (done * char_size), to, n * char_size); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11505 | done += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11506 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11507 | } |
| 11508 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 11509 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11510 | return (PyObject*) u; |
| 11511 | } |
| 11512 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 11513 | PyObject * |
| 11514 | PyUnicode_Replace(PyObject *obj, |
| 11515 | PyObject *subobj, |
| 11516 | PyObject *replobj, |
| 11517 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11518 | { |
| 11519 | PyObject *self; |
| 11520 | PyObject *str1; |
| 11521 | PyObject *str2; |
| 11522 | PyObject *result; |
| 11523 | |
| 11524 | self = PyUnicode_FromObject(obj); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11525 | if (self == NULL || PyUnicode_READY(self) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11526 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11527 | str1 = PyUnicode_FromObject(subobj); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11528 | if (str1 == NULL || PyUnicode_READY(str1) == -1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11529 | Py_DECREF(self); |
| 11530 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11531 | } |
| 11532 | str2 = PyUnicode_FromObject(replobj); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11533 | if (str2 == NULL || PyUnicode_READY(str2)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11534 | Py_DECREF(self); |
| 11535 | Py_DECREF(str1); |
| 11536 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11537 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11538 | result = replace(self, str1, str2, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11539 | Py_DECREF(self); |
| 11540 | Py_DECREF(str1); |
| 11541 | Py_DECREF(str2); |
| 11542 | return result; |
| 11543 | } |
| 11544 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11545 | PyDoc_STRVAR(replace__doc__, |
Ezio Melotti | c1897e7 | 2010-06-26 18:50:39 +0000 | [diff] [blame] | 11546 | "S.replace(old, new[, count]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11547 | \n\ |
| 11548 | Return a copy of S with all occurrences of substring\n\ |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 11549 | old replaced by new. If the optional argument count is\n\ |
| 11550 | given, only the first count occurrences are replaced."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11551 | |
| 11552 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11553 | unicode_replace(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11554 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11555 | PyObject *str1; |
| 11556 | PyObject *str2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11557 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11558 | PyObject *result; |
| 11559 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11560 | if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11561 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11562 | if (!PyUnicode_READY(self) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11563 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11564 | str1 = PyUnicode_FromObject(str1); |
| 11565 | if (str1 == NULL || PyUnicode_READY(str1) == -1) |
| 11566 | return NULL; |
| 11567 | str2 = PyUnicode_FromObject(str2); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11568 | if (str2 == NULL || PyUnicode_READY(str2) == -1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11569 | Py_DECREF(str1); |
| 11570 | return NULL; |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 11571 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11572 | |
| 11573 | result = replace(self, str1, str2, maxcount); |
| 11574 | |
| 11575 | Py_DECREF(str1); |
| 11576 | Py_DECREF(str2); |
| 11577 | return result; |
| 11578 | } |
| 11579 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 11580 | static PyObject * |
| 11581 | unicode_repr(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11582 | { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11583 | PyObject *repr; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11584 | Py_ssize_t isize; |
| 11585 | Py_ssize_t osize, squote, dquote, i, o; |
| 11586 | Py_UCS4 max, quote; |
| 11587 | int ikind, okind; |
| 11588 | void *idata, *odata; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11589 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11590 | if (PyUnicode_READY(unicode) == -1) |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11591 | return NULL; |
| 11592 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11593 | isize = PyUnicode_GET_LENGTH(unicode); |
| 11594 | idata = PyUnicode_DATA(unicode); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11595 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11596 | /* Compute length of output, quote characters, and |
| 11597 | maximum character */ |
| 11598 | osize = 2; /* quotes */ |
| 11599 | max = 127; |
| 11600 | squote = dquote = 0; |
| 11601 | ikind = PyUnicode_KIND(unicode); |
| 11602 | for (i = 0; i < isize; i++) { |
| 11603 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
| 11604 | switch (ch) { |
| 11605 | case '\'': squote++; osize++; break; |
| 11606 | case '"': dquote++; osize++; break; |
| 11607 | case '\\': case '\t': case '\r': case '\n': |
| 11608 | osize += 2; break; |
| 11609 | default: |
| 11610 | /* Fast-path ASCII */ |
| 11611 | if (ch < ' ' || ch == 0x7f) |
| 11612 | osize += 4; /* \xHH */ |
| 11613 | else if (ch < 0x7f) |
| 11614 | osize++; |
| 11615 | else if (Py_UNICODE_ISPRINTABLE(ch)) { |
| 11616 | osize++; |
| 11617 | max = ch > max ? ch : max; |
| 11618 | } |
| 11619 | else if (ch < 0x100) |
| 11620 | osize += 4; /* \xHH */ |
| 11621 | else if (ch < 0x10000) |
| 11622 | osize += 6; /* \uHHHH */ |
| 11623 | else |
| 11624 | osize += 10; /* \uHHHHHHHH */ |
| 11625 | } |
| 11626 | } |
| 11627 | |
| 11628 | quote = '\''; |
| 11629 | if (squote) { |
| 11630 | if (dquote) |
| 11631 | /* Both squote and dquote present. Use squote, |
| 11632 | and escape them */ |
| 11633 | osize += squote; |
| 11634 | else |
| 11635 | quote = '"'; |
| 11636 | } |
| 11637 | |
| 11638 | repr = PyUnicode_New(osize, max); |
| 11639 | if (repr == NULL) |
| 11640 | return NULL; |
| 11641 | okind = PyUnicode_KIND(repr); |
| 11642 | odata = PyUnicode_DATA(repr); |
| 11643 | |
| 11644 | PyUnicode_WRITE(okind, odata, 0, quote); |
| 11645 | PyUnicode_WRITE(okind, odata, osize-1, quote); |
| 11646 | |
| 11647 | for (i = 0, o = 1; i < isize; i++) { |
| 11648 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11649 | |
| 11650 | /* Escape quotes and backslashes */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11651 | if ((ch == quote) || (ch == '\\')) { |
| 11652 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 11653 | PyUnicode_WRITE(okind, odata, o++, ch); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11654 | continue; |
| 11655 | } |
| 11656 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11657 | /* Map special whitespace to '\t', \n', '\r' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11658 | if (ch == '\t') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11659 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 11660 | PyUnicode_WRITE(okind, odata, o++, 't'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11661 | } |
| 11662 | else if (ch == '\n') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11663 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 11664 | PyUnicode_WRITE(okind, odata, o++, 'n'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11665 | } |
| 11666 | else if (ch == '\r') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11667 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 11668 | PyUnicode_WRITE(okind, odata, o++, 'r'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11669 | } |
| 11670 | |
| 11671 | /* Map non-printable US ASCII to '\xhh' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11672 | else if (ch < ' ' || ch == 0x7F) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11673 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 11674 | PyUnicode_WRITE(okind, odata, o++, 'x'); |
| 11675 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 4) & 0x000F]); |
| 11676 | PyUnicode_WRITE(okind, odata, o++, hexdigits[ch & 0x000F]); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11677 | } |
| 11678 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11679 | /* Copy ASCII characters as-is */ |
| 11680 | else if (ch < 0x7F) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11681 | PyUnicode_WRITE(okind, odata, o++, ch); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11682 | } |
| 11683 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11684 | /* Non-ASCII characters */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11685 | else { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11686 | /* Map Unicode whitespace and control characters |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11687 | (categories Z* and C* except ASCII space) |
| 11688 | */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11689 | if (!Py_UNICODE_ISPRINTABLE(ch)) { |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11690 | /* Map 8-bit characters to '\xhh' */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11691 | if (ch <= 0xff) { |
| 11692 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 11693 | PyUnicode_WRITE(okind, odata, o++, 'x'); |
| 11694 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 4) & 0x000F]); |
| 11695 | PyUnicode_WRITE(okind, odata, o++, hexdigits[ch & 0x000F]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11696 | } |
| 11697 | /* Map 21-bit characters to '\U00xxxxxx' */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11698 | else if (ch >= 0x10000) { |
| 11699 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 11700 | PyUnicode_WRITE(okind, odata, o++, 'U'); |
| 11701 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 28) & 0xF]); |
| 11702 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 24) & 0xF]); |
| 11703 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 20) & 0xF]); |
| 11704 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 16) & 0xF]); |
| 11705 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 12) & 0xF]); |
| 11706 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 8) & 0xF]); |
| 11707 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 4) & 0xF]); |
| 11708 | PyUnicode_WRITE(okind, odata, o++, hexdigits[ch & 0xF]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11709 | } |
| 11710 | /* Map 16-bit characters to '\uxxxx' */ |
| 11711 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11712 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 11713 | PyUnicode_WRITE(okind, odata, o++, 'u'); |
| 11714 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 12) & 0xF]); |
| 11715 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 8) & 0xF]); |
| 11716 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 4) & 0xF]); |
| 11717 | PyUnicode_WRITE(okind, odata, o++, hexdigits[ch & 0xF]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11718 | } |
| 11719 | } |
| 11720 | /* Copy characters as-is */ |
| 11721 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11722 | PyUnicode_WRITE(okind, odata, o++, ch); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11723 | } |
| 11724 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11725 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11726 | /* Closing quote already added at the beginning */ |
Victor Stinner | 05d1189 | 2011-10-06 01:13:58 +0200 | [diff] [blame] | 11727 | assert(_PyUnicode_CheckConsistency(repr, 1)); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11728 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11729 | } |
| 11730 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11731 | PyDoc_STRVAR(rfind__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11732 | "S.rfind(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11733 | \n\ |
| 11734 | Return the highest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 11735 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11736 | arguments start and end are interpreted as in slice notation.\n\ |
| 11737 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11738 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11739 | |
| 11740 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11741 | unicode_rfind(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11742 | { |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11743 | PyUnicodeObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 11744 | Py_ssize_t start; |
| 11745 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11746 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11747 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11748 | if (!stringlib_parse_args_finds_unicode("rfind", args, &substring, |
| 11749 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11750 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11751 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11752 | if (PyUnicode_READY(self) == -1) |
| 11753 | return NULL; |
| 11754 | if (PyUnicode_READY(substring) == -1) |
| 11755 | return NULL; |
| 11756 | |
| 11757 | result = any_find_slice( |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 11758 | asciilib_rfind_slice, ucs1lib_rfind_slice, |
| 11759 | ucs2lib_rfind_slice, ucs4lib_rfind_slice, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11760 | self, (PyObject*)substring, start, end |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11761 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11762 | |
| 11763 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11764 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11765 | if (result == -2) |
| 11766 | return NULL; |
| 11767 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 11768 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11769 | } |
| 11770 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11771 | PyDoc_STRVAR(rindex__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11772 | "S.rindex(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11773 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11774 | 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] | 11775 | |
| 11776 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11777 | unicode_rindex(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11778 | { |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11779 | PyUnicodeObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 11780 | Py_ssize_t start; |
| 11781 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11782 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11783 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11784 | if (!stringlib_parse_args_finds_unicode("rindex", args, &substring, |
| 11785 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11786 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11787 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11788 | if (PyUnicode_READY(self) == -1) |
| 11789 | return NULL; |
| 11790 | if (PyUnicode_READY(substring) == -1) |
| 11791 | return NULL; |
| 11792 | |
| 11793 | result = any_find_slice( |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 11794 | asciilib_rfind_slice, ucs1lib_rfind_slice, |
| 11795 | ucs2lib_rfind_slice, ucs4lib_rfind_slice, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11796 | self, (PyObject*)substring, start, end |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11797 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11798 | |
| 11799 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11800 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11801 | if (result == -2) |
| 11802 | return NULL; |
| 11803 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11804 | if (result < 0) { |
| 11805 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 11806 | return NULL; |
| 11807 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11808 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 11809 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11810 | } |
| 11811 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11812 | PyDoc_STRVAR(rjust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11813 | "S.rjust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11814 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 11815 | 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] | 11816 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11817 | |
| 11818 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11819 | unicode_rjust(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11820 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11821 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11822 | Py_UCS4 fillchar = ' '; |
| 11823 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11824 | 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] | 11825 | return NULL; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 11826 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11827 | if (PyUnicode_READY(self) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11828 | return NULL; |
| 11829 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11830 | if (_PyUnicode_LENGTH(self) >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11831 | Py_INCREF(self); |
| 11832 | return (PyObject*) self; |
| 11833 | } |
| 11834 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11835 | return (PyObject*) pad(self, width - _PyUnicode_LENGTH(self), 0, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11836 | } |
| 11837 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 11838 | PyObject * |
| 11839 | PyUnicode_Split(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11840 | { |
| 11841 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11842 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11843 | s = PyUnicode_FromObject(s); |
| 11844 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11845 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11846 | if (sep != NULL) { |
| 11847 | sep = PyUnicode_FromObject(sep); |
| 11848 | if (sep == NULL) { |
| 11849 | Py_DECREF(s); |
| 11850 | return NULL; |
| 11851 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11852 | } |
| 11853 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11854 | result = split(s, sep, maxsplit); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11855 | |
| 11856 | Py_DECREF(s); |
| 11857 | Py_XDECREF(sep); |
| 11858 | return result; |
| 11859 | } |
| 11860 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11861 | PyDoc_STRVAR(split__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11862 | "S.split([sep[, maxsplit]]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11863 | \n\ |
| 11864 | Return a list of the words in S, using sep as the\n\ |
| 11865 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 11866 | 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] | 11867 | whitespace string is a separator and empty strings are\n\ |
| 11868 | removed from the result."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11869 | |
| 11870 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11871 | unicode_split(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11872 | { |
| 11873 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11874 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11875 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11876 | if (!PyArg_ParseTuple(args, "|On:split", &substring, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11877 | return NULL; |
| 11878 | |
| 11879 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11880 | return split(self, NULL, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11881 | else if (PyUnicode_Check(substring)) |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11882 | return split(self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11883 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11884 | return PyUnicode_Split((PyObject *)self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11885 | } |
| 11886 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11887 | PyObject * |
| 11888 | PyUnicode_Partition(PyObject *str_in, PyObject *sep_in) |
| 11889 | { |
| 11890 | PyObject* str_obj; |
| 11891 | PyObject* sep_obj; |
| 11892 | PyObject* out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11893 | int kind1, kind2, kind; |
| 11894 | void *buf1 = NULL, *buf2 = NULL; |
| 11895 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11896 | |
| 11897 | str_obj = PyUnicode_FromObject(str_in); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11898 | if (!str_obj || PyUnicode_READY(str_obj) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11899 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11900 | sep_obj = PyUnicode_FromObject(sep_in); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11901 | if (!sep_obj || PyUnicode_READY(sep_obj) == -1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11902 | Py_DECREF(str_obj); |
| 11903 | return NULL; |
| 11904 | } |
| 11905 | |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 11906 | kind1 = PyUnicode_KIND(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11907 | kind2 = PyUnicode_KIND(sep_obj); |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 11908 | kind = Py_MAX(kind1, kind2); |
| 11909 | buf1 = PyUnicode_DATA(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11910 | if (kind1 != kind) |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 11911 | buf1 = _PyUnicode_AsKind(str_obj, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11912 | if (!buf1) |
| 11913 | goto onError; |
| 11914 | buf2 = PyUnicode_DATA(sep_obj); |
| 11915 | if (kind2 != kind) |
| 11916 | buf2 = _PyUnicode_AsKind(sep_obj, kind); |
| 11917 | if (!buf2) |
| 11918 | goto onError; |
| 11919 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 11920 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
| 11921 | |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 11922 | switch(PyUnicode_KIND(str_obj)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11923 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 11924 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sep_obj)) |
| 11925 | out = asciilib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 11926 | else |
| 11927 | 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] | 11928 | break; |
| 11929 | case PyUnicode_2BYTE_KIND: |
| 11930 | out = ucs2lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 11931 | break; |
| 11932 | case PyUnicode_4BYTE_KIND: |
| 11933 | out = ucs4lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 11934 | break; |
| 11935 | default: |
| 11936 | assert(0); |
| 11937 | out = 0; |
| 11938 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11939 | |
| 11940 | Py_DECREF(sep_obj); |
| 11941 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11942 | if (kind1 != kind) |
| 11943 | PyMem_Free(buf1); |
| 11944 | if (kind2 != kind) |
| 11945 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11946 | |
| 11947 | return out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11948 | onError: |
| 11949 | Py_DECREF(sep_obj); |
| 11950 | Py_DECREF(str_obj); |
| 11951 | if (kind1 != kind && buf1) |
| 11952 | PyMem_Free(buf1); |
| 11953 | if (kind2 != kind && buf2) |
| 11954 | PyMem_Free(buf2); |
| 11955 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11956 | } |
| 11957 | |
| 11958 | |
| 11959 | PyObject * |
| 11960 | PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in) |
| 11961 | { |
| 11962 | PyObject* str_obj; |
| 11963 | PyObject* sep_obj; |
| 11964 | PyObject* out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11965 | int kind1, kind2, kind; |
| 11966 | void *buf1 = NULL, *buf2 = NULL; |
| 11967 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11968 | |
| 11969 | str_obj = PyUnicode_FromObject(str_in); |
| 11970 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11971 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11972 | sep_obj = PyUnicode_FromObject(sep_in); |
| 11973 | if (!sep_obj) { |
| 11974 | Py_DECREF(str_obj); |
| 11975 | return NULL; |
| 11976 | } |
| 11977 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11978 | kind1 = PyUnicode_KIND(str_in); |
| 11979 | kind2 = PyUnicode_KIND(sep_obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 11980 | kind = Py_MAX(kind1, kind2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11981 | buf1 = PyUnicode_DATA(str_in); |
| 11982 | if (kind1 != kind) |
| 11983 | buf1 = _PyUnicode_AsKind(str_in, kind); |
| 11984 | if (!buf1) |
| 11985 | goto onError; |
| 11986 | buf2 = PyUnicode_DATA(sep_obj); |
| 11987 | if (kind2 != kind) |
| 11988 | buf2 = _PyUnicode_AsKind(sep_obj, kind); |
| 11989 | if (!buf2) |
| 11990 | goto onError; |
| 11991 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 11992 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
| 11993 | |
| 11994 | switch(PyUnicode_KIND(str_in)) { |
| 11995 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 11996 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sep_obj)) |
| 11997 | out = asciilib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 11998 | else |
| 11999 | 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] | 12000 | break; |
| 12001 | case PyUnicode_2BYTE_KIND: |
| 12002 | out = ucs2lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12003 | break; |
| 12004 | case PyUnicode_4BYTE_KIND: |
| 12005 | out = ucs4lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12006 | break; |
| 12007 | default: |
| 12008 | assert(0); |
| 12009 | out = 0; |
| 12010 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12011 | |
| 12012 | Py_DECREF(sep_obj); |
| 12013 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12014 | if (kind1 != kind) |
| 12015 | PyMem_Free(buf1); |
| 12016 | if (kind2 != kind) |
| 12017 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12018 | |
| 12019 | return out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12020 | onError: |
| 12021 | Py_DECREF(sep_obj); |
| 12022 | Py_DECREF(str_obj); |
| 12023 | if (kind1 != kind && buf1) |
| 12024 | PyMem_Free(buf1); |
| 12025 | if (kind2 != kind && buf2) |
| 12026 | PyMem_Free(buf2); |
| 12027 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12028 | } |
| 12029 | |
| 12030 | PyDoc_STRVAR(partition__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12031 | "S.partition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12032 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 12033 | 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] | 12034 | 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] | 12035 | found, return S and two empty strings."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12036 | |
| 12037 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12038 | unicode_partition(PyObject *self, PyObject *separator) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12039 | { |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12040 | return PyUnicode_Partition(self, separator); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12041 | } |
| 12042 | |
| 12043 | PyDoc_STRVAR(rpartition__doc__, |
Ezio Melotti | 5b2b242 | 2010-01-25 11:58:28 +0000 | [diff] [blame] | 12044 | "S.rpartition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12045 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 12046 | 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] | 12047 | 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] | 12048 | separator is not found, return two empty strings and S."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12049 | |
| 12050 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12051 | unicode_rpartition(PyObject *self, PyObject *separator) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12052 | { |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12053 | return PyUnicode_RPartition(self, separator); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12054 | } |
| 12055 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12056 | PyObject * |
| 12057 | PyUnicode_RSplit(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12058 | { |
| 12059 | PyObject *result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12060 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12061 | s = PyUnicode_FromObject(s); |
| 12062 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12063 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12064 | if (sep != NULL) { |
| 12065 | sep = PyUnicode_FromObject(sep); |
| 12066 | if (sep == NULL) { |
| 12067 | Py_DECREF(s); |
| 12068 | return NULL; |
| 12069 | } |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12070 | } |
| 12071 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12072 | result = rsplit(s, sep, maxsplit); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12073 | |
| 12074 | Py_DECREF(s); |
| 12075 | Py_XDECREF(sep); |
| 12076 | return result; |
| 12077 | } |
| 12078 | |
| 12079 | PyDoc_STRVAR(rsplit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12080 | "S.rsplit([sep[, maxsplit]]) -> list of strings\n\ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12081 | \n\ |
| 12082 | Return a list of the words in S, using sep as the\n\ |
| 12083 | delimiter string, starting at the end of the string and\n\ |
| 12084 | working to the front. If maxsplit is given, at most maxsplit\n\ |
| 12085 | splits are done. If sep is not specified, any whitespace string\n\ |
| 12086 | is a separator."); |
| 12087 | |
| 12088 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12089 | unicode_rsplit(PyObject *self, PyObject *args) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12090 | { |
| 12091 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12092 | Py_ssize_t maxcount = -1; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12093 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12094 | if (!PyArg_ParseTuple(args, "|On:rsplit", &substring, &maxcount)) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12095 | return NULL; |
| 12096 | |
| 12097 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12098 | return rsplit(self, NULL, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12099 | else if (PyUnicode_Check(substring)) |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12100 | return rsplit(self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12101 | else |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12102 | return PyUnicode_RSplit(self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12103 | } |
| 12104 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12105 | PyDoc_STRVAR(splitlines__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12106 | "S.splitlines([keepends]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12107 | \n\ |
| 12108 | 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] | 12109 | 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] | 12110 | is given and true."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12111 | |
| 12112 | static PyObject* |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12113 | unicode_splitlines(PyUnicodeObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12114 | { |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12115 | static char *kwlist[] = {"keepends", 0}; |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 12116 | int keepends = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12117 | |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12118 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|i:splitlines", |
| 12119 | kwlist, &keepends)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12120 | return NULL; |
| 12121 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 12122 | return PyUnicode_Splitlines((PyObject *)self, keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12123 | } |
| 12124 | |
| 12125 | static |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 12126 | PyObject *unicode_str(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12127 | { |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 12128 | if (PyUnicode_CheckExact(self)) { |
| 12129 | Py_INCREF(self); |
| 12130 | return self; |
| 12131 | } else |
| 12132 | /* Subtype -- return genuine unicode string with the same value. */ |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 12133 | return PyUnicode_Copy(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12134 | } |
| 12135 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12136 | PyDoc_STRVAR(swapcase__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12137 | "S.swapcase() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12138 | \n\ |
| 12139 | 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] | 12140 | and vice versa."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12141 | |
| 12142 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12143 | unicode_swapcase(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12144 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12145 | return fixup(self, fixswapcase); |
| 12146 | } |
| 12147 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12148 | PyDoc_STRVAR(maketrans__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12149 | "str.maketrans(x[, y[, z]]) -> dict (static method)\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12150 | \n\ |
| 12151 | Return a translation table usable for str.translate().\n\ |
| 12152 | If there is only one argument, it must be a dictionary mapping Unicode\n\ |
| 12153 | ordinals (integers) or characters to Unicode ordinals, strings or None.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12154 | Character keys will be then converted to ordinals.\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12155 | If there are two arguments, they must be strings of equal length, and\n\ |
| 12156 | in the resulting dictionary, each character in x will be mapped to the\n\ |
| 12157 | character at the same position in y. If there is a third argument, it\n\ |
| 12158 | must be a string, whose characters will be mapped to None in the result."); |
| 12159 | |
| 12160 | static PyObject* |
| 12161 | unicode_maketrans(PyUnicodeObject *null, PyObject *args) |
| 12162 | { |
| 12163 | PyObject *x, *y = NULL, *z = NULL; |
| 12164 | PyObject *new = NULL, *key, *value; |
| 12165 | Py_ssize_t i = 0; |
| 12166 | int res; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12167 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12168 | if (!PyArg_ParseTuple(args, "O|UU:maketrans", &x, &y, &z)) |
| 12169 | return NULL; |
| 12170 | new = PyDict_New(); |
| 12171 | if (!new) |
| 12172 | return NULL; |
| 12173 | if (y != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12174 | int x_kind, y_kind, z_kind; |
| 12175 | void *x_data, *y_data, *z_data; |
| 12176 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12177 | /* x must be a string too, of equal length */ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12178 | if (!PyUnicode_Check(x)) { |
| 12179 | PyErr_SetString(PyExc_TypeError, "first maketrans argument must " |
| 12180 | "be a string if there is a second argument"); |
| 12181 | goto err; |
| 12182 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12183 | if (PyUnicode_GET_LENGTH(x) != PyUnicode_GET_LENGTH(y)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12184 | PyErr_SetString(PyExc_ValueError, "the first two maketrans " |
| 12185 | "arguments must have equal length"); |
| 12186 | goto err; |
| 12187 | } |
| 12188 | /* 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] | 12189 | x_kind = PyUnicode_KIND(x); |
| 12190 | y_kind = PyUnicode_KIND(y); |
| 12191 | x_data = PyUnicode_DATA(x); |
| 12192 | y_data = PyUnicode_DATA(y); |
| 12193 | for (i = 0; i < PyUnicode_GET_LENGTH(x); i++) { |
| 12194 | key = PyLong_FromLong(PyUnicode_READ(x_kind, x_data, i)); |
| 12195 | value = PyLong_FromLong(PyUnicode_READ(y_kind, y_data, i)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12196 | if (!key || !value) |
| 12197 | goto err; |
| 12198 | res = PyDict_SetItem(new, key, value); |
| 12199 | Py_DECREF(key); |
| 12200 | Py_DECREF(value); |
| 12201 | if (res < 0) |
| 12202 | goto err; |
| 12203 | } |
| 12204 | /* create entries for deleting chars in z */ |
| 12205 | if (z != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12206 | z_kind = PyUnicode_KIND(z); |
| 12207 | z_data = PyUnicode_DATA(z); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12208 | for (i = 0; i < PyUnicode_GET_SIZE(z); i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12209 | key = PyLong_FromLong(PyUnicode_READ(z_kind, z_data, i)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12210 | if (!key) |
| 12211 | goto err; |
| 12212 | res = PyDict_SetItem(new, key, Py_None); |
| 12213 | Py_DECREF(key); |
| 12214 | if (res < 0) |
| 12215 | goto err; |
| 12216 | } |
| 12217 | } |
| 12218 | } else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12219 | int kind; |
| 12220 | void *data; |
| 12221 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12222 | /* x must be a dict */ |
Raymond Hettinger | 3ad0576 | 2009-05-29 22:11:22 +0000 | [diff] [blame] | 12223 | if (!PyDict_CheckExact(x)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12224 | PyErr_SetString(PyExc_TypeError, "if you give only one argument " |
| 12225 | "to maketrans it must be a dict"); |
| 12226 | goto err; |
| 12227 | } |
| 12228 | /* copy entries into the new dict, converting string keys to int keys */ |
| 12229 | while (PyDict_Next(x, &i, &key, &value)) { |
| 12230 | if (PyUnicode_Check(key)) { |
| 12231 | /* convert string keys to integer keys */ |
| 12232 | PyObject *newkey; |
| 12233 | if (PyUnicode_GET_SIZE(key) != 1) { |
| 12234 | PyErr_SetString(PyExc_ValueError, "string keys in translate " |
| 12235 | "table must be of length 1"); |
| 12236 | goto err; |
| 12237 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12238 | kind = PyUnicode_KIND(key); |
| 12239 | data = PyUnicode_DATA(key); |
| 12240 | newkey = PyLong_FromLong(PyUnicode_READ(kind, data, 0)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12241 | if (!newkey) |
| 12242 | goto err; |
| 12243 | res = PyDict_SetItem(new, newkey, value); |
| 12244 | Py_DECREF(newkey); |
| 12245 | if (res < 0) |
| 12246 | goto err; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12247 | } else if (PyLong_Check(key)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12248 | /* just keep integer keys */ |
| 12249 | if (PyDict_SetItem(new, key, value) < 0) |
| 12250 | goto err; |
| 12251 | } else { |
| 12252 | PyErr_SetString(PyExc_TypeError, "keys in translate table must " |
| 12253 | "be strings or integers"); |
| 12254 | goto err; |
| 12255 | } |
| 12256 | } |
| 12257 | } |
| 12258 | return new; |
| 12259 | err: |
| 12260 | Py_DECREF(new); |
| 12261 | return NULL; |
| 12262 | } |
| 12263 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12264 | PyDoc_STRVAR(translate__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12265 | "S.translate(table) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12266 | \n\ |
| 12267 | Return a copy of the string S, where all characters have been mapped\n\ |
| 12268 | through the given translation table, which must be a mapping of\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12269 | Unicode ordinals to Unicode ordinals, strings, or None.\n\ |
Walter Dörwald | 5c1ee17 | 2002-09-04 20:31:32 +0000 | [diff] [blame] | 12270 | Unmapped characters are left untouched. Characters mapped to None\n\ |
| 12271 | are deleted."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12272 | |
| 12273 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12274 | unicode_translate(PyObject *self, PyObject *table) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12275 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12276 | return _PyUnicode_TranslateCharmap(self, table, "ignore"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12277 | } |
| 12278 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12279 | PyDoc_STRVAR(upper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12280 | "S.upper() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12281 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12282 | Return a copy of S converted to uppercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12283 | |
| 12284 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12285 | unicode_upper(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12286 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12287 | return fixup(self, fixupper); |
| 12288 | } |
| 12289 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12290 | PyDoc_STRVAR(zfill__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12291 | "S.zfill(width) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12292 | \n\ |
Benjamin Peterson | 9aa4299 | 2008-09-10 21:57:34 +0000 | [diff] [blame] | 12293 | Pad a numeric string S with zeros on the left, to fill a field\n\ |
| 12294 | of the specified width. The string S is never truncated."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12295 | |
| 12296 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12297 | unicode_zfill(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12298 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12299 | Py_ssize_t fill; |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12300 | PyObject *u; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12301 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12302 | int kind; |
| 12303 | void *data; |
| 12304 | Py_UCS4 chr; |
| 12305 | |
| 12306 | if (PyUnicode_READY(self) == -1) |
| 12307 | return NULL; |
| 12308 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12309 | if (!PyArg_ParseTuple(args, "n:zfill", &width)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12310 | return NULL; |
| 12311 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12312 | if (PyUnicode_GET_LENGTH(self) >= width) { |
Walter Dörwald | 0fe940c | 2002-04-15 18:42:15 +0000 | [diff] [blame] | 12313 | if (PyUnicode_CheckExact(self)) { |
| 12314 | Py_INCREF(self); |
| 12315 | return (PyObject*) self; |
| 12316 | } |
| 12317 | else |
Victor Stinner | 2219e0a | 2011-10-01 01:16:59 +0200 | [diff] [blame] | 12318 | return PyUnicode_Copy((PyObject*)self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12319 | } |
| 12320 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12321 | fill = width - _PyUnicode_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12322 | |
| 12323 | u = pad(self, fill, 0, '0'); |
| 12324 | |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 12325 | if (u == NULL) |
| 12326 | return NULL; |
| 12327 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12328 | kind = PyUnicode_KIND(u); |
| 12329 | data = PyUnicode_DATA(u); |
| 12330 | chr = PyUnicode_READ(kind, data, fill); |
| 12331 | |
| 12332 | if (chr == '+' || chr == '-') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12333 | /* move sign to beginning of string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12334 | PyUnicode_WRITE(kind, data, 0, chr); |
| 12335 | PyUnicode_WRITE(kind, data, fill, '0'); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12336 | } |
| 12337 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12338 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12339 | return (PyObject*) u; |
| 12340 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12341 | |
| 12342 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12343 | static PyObject * |
| 12344 | unicode__decimal2ascii(PyObject *self) |
| 12345 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12346 | return PyUnicode_TransformDecimalAndSpaceToASCII(self); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12347 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12348 | #endif |
| 12349 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12350 | PyDoc_STRVAR(startswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12351 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12352 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 12353 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 12354 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12355 | With optional end, stop comparing S at that position.\n\ |
| 12356 | prefix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12357 | |
| 12358 | static PyObject * |
| 12359 | unicode_startswith(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12360 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12361 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12362 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12363 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12364 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12365 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12366 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12367 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12368 | if (!stringlib_parse_args_finds("startswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12369 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12370 | if (PyTuple_Check(subobj)) { |
| 12371 | Py_ssize_t i; |
| 12372 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 12373 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12374 | PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12375 | if (substring == NULL) |
| 12376 | return NULL; |
| 12377 | result = tailmatch(self, substring, start, end, -1); |
| 12378 | Py_DECREF(substring); |
| 12379 | if (result) { |
| 12380 | Py_RETURN_TRUE; |
| 12381 | } |
| 12382 | } |
| 12383 | /* nothing matched */ |
| 12384 | Py_RETURN_FALSE; |
| 12385 | } |
| 12386 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12387 | if (substring == NULL) { |
| 12388 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 12389 | PyErr_Format(PyExc_TypeError, "startswith first arg must be str or " |
| 12390 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12391 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12392 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12393 | result = tailmatch(self, substring, start, end, -1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12394 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12395 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12396 | } |
| 12397 | |
| 12398 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12399 | PyDoc_STRVAR(endswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12400 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12401 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 12402 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 12403 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12404 | With optional end, stop comparing S at that position.\n\ |
| 12405 | suffix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12406 | |
| 12407 | static PyObject * |
| 12408 | unicode_endswith(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12409 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12410 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12411 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12412 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12413 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12414 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12415 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12416 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12417 | if (!stringlib_parse_args_finds("endswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12418 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12419 | if (PyTuple_Check(subobj)) { |
| 12420 | Py_ssize_t i; |
| 12421 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 12422 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12423 | PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12424 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12425 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12426 | result = tailmatch(self, substring, start, end, +1); |
| 12427 | Py_DECREF(substring); |
| 12428 | if (result) { |
| 12429 | Py_RETURN_TRUE; |
| 12430 | } |
| 12431 | } |
| 12432 | Py_RETURN_FALSE; |
| 12433 | } |
| 12434 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12435 | if (substring == NULL) { |
| 12436 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 12437 | PyErr_Format(PyExc_TypeError, "endswith first arg must be str or " |
| 12438 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12439 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12440 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12441 | result = tailmatch(self, substring, start, end, +1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12442 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12443 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12444 | } |
| 12445 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12446 | #include "stringlib/unicode_format.h" |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12447 | |
| 12448 | PyDoc_STRVAR(format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12449 | "S.format(*args, **kwargs) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12450 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 12451 | Return a formatted version of S, using substitutions from args and kwargs.\n\ |
| 12452 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12453 | |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 12454 | PyDoc_STRVAR(format_map__doc__, |
| 12455 | "S.format_map(mapping) -> str\n\ |
| 12456 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 12457 | Return a formatted version of S, using substitutions from mapping.\n\ |
| 12458 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 12459 | |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 12460 | static PyObject * |
| 12461 | unicode__format__(PyObject* self, PyObject* args) |
| 12462 | { |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12463 | PyObject *format_spec, *out; |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 12464 | |
| 12465 | if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) |
| 12466 | return NULL; |
| 12467 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12468 | out = _PyUnicode_FormatAdvanced(self, format_spec, 0, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12469 | PyUnicode_GET_LENGTH(format_spec)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12470 | return out; |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 12471 | } |
| 12472 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12473 | PyDoc_STRVAR(p_format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12474 | "S.__format__(format_spec) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12475 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 12476 | Return a formatted version of S as described by format_spec."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12477 | |
| 12478 | static PyObject * |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 12479 | unicode__sizeof__(PyUnicodeObject *v) |
| 12480 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12481 | Py_ssize_t size; |
| 12482 | |
| 12483 | /* If it's a compact object, account for base structure + |
| 12484 | character data. */ |
| 12485 | if (PyUnicode_IS_COMPACT_ASCII(v)) |
| 12486 | size = sizeof(PyASCIIObject) + PyUnicode_GET_LENGTH(v) + 1; |
| 12487 | else if (PyUnicode_IS_COMPACT(v)) |
| 12488 | size = sizeof(PyCompactUnicodeObject) + |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame^] | 12489 | (PyUnicode_GET_LENGTH(v) + 1) * PyUnicode_KIND(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12490 | else { |
| 12491 | /* If it is a two-block object, account for base object, and |
| 12492 | for character block if present. */ |
| 12493 | size = sizeof(PyUnicodeObject); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 12494 | if (_PyUnicode_DATA_ANY(v)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12495 | size += (PyUnicode_GET_LENGTH(v) + 1) * |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame^] | 12496 | PyUnicode_KIND(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12497 | } |
| 12498 | /* 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] | 12499 | with the data pointer. Check if the data is not shared. */ |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 12500 | if (_PyUnicode_HAS_WSTR_MEMORY(v)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12501 | size += (PyUnicode_WSTR_LENGTH(v) + 1) * sizeof(wchar_t); |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 12502 | if (_PyUnicode_HAS_UTF8_MEMORY(v)) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 12503 | size += PyUnicode_UTF8_LENGTH(v) + 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12504 | |
| 12505 | return PyLong_FromSsize_t(size); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 12506 | } |
| 12507 | |
| 12508 | PyDoc_STRVAR(sizeof__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12509 | "S.__sizeof__() -> size of S in memory, in bytes"); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 12510 | |
| 12511 | static PyObject * |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 12512 | unicode_getnewargs(PyObject *v) |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 12513 | { |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 12514 | PyObject *copy = PyUnicode_Copy(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12515 | if (!copy) |
| 12516 | return NULL; |
| 12517 | return Py_BuildValue("(N)", copy); |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 12518 | } |
| 12519 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12520 | static PyMethodDef unicode_methods[] = { |
| 12521 | |
| 12522 | /* Order is according to common usage: often used methods should |
| 12523 | appear first, since lookup is done sequentially. */ |
| 12524 | |
Benjamin Peterson | 28a4dce | 2010-12-12 01:33:04 +0000 | [diff] [blame] | 12525 | {"encode", (PyCFunction) unicode_encode, METH_VARARGS | METH_KEYWORDS, encode__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12526 | {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, |
| 12527 | {"split", (PyCFunction) unicode_split, METH_VARARGS, split__doc__}, |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12528 | {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS, rsplit__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12529 | {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, |
| 12530 | {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, |
| 12531 | {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, |
| 12532 | {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, |
| 12533 | {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, |
| 12534 | {"expandtabs", (PyCFunction) unicode_expandtabs, METH_VARARGS, expandtabs__doc__}, |
| 12535 | {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12536 | {"partition", (PyCFunction) unicode_partition, METH_O, partition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12537 | {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, |
| 12538 | {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, |
| 12539 | {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12540 | {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12541 | {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, |
| 12542 | {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, |
| 12543 | {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12544 | {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12545 | {"rpartition", (PyCFunction) unicode_rpartition, METH_O, rpartition__doc__}, |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12546 | {"splitlines", (PyCFunction) unicode_splitlines, METH_VARARGS | METH_KEYWORDS, splitlines__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12547 | {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12548 | {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, |
| 12549 | {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, |
| 12550 | {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, |
| 12551 | {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, |
| 12552 | {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, |
| 12553 | {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, |
| 12554 | {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, |
| 12555 | {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, |
| 12556 | {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, |
| 12557 | {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, |
| 12558 | {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, |
| 12559 | {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, |
| 12560 | {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, |
| 12561 | {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 12562 | {"isidentifier", (PyCFunction) unicode_isidentifier, METH_NOARGS, isidentifier__doc__}, |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12563 | {"isprintable", (PyCFunction) unicode_isprintable, METH_NOARGS, isprintable__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12564 | {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, |
Eric Smith | 9cd1e09 | 2007-08-31 18:39:38 +0000 | [diff] [blame] | 12565 | {"format", (PyCFunction) do_string_format, METH_VARARGS | METH_KEYWORDS, format__doc__}, |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 12566 | {"format_map", (PyCFunction) do_string_format_map, METH_O, format_map__doc__}, |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 12567 | {"__format__", (PyCFunction) unicode__format__, METH_VARARGS, p_format__doc__}, |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12568 | {"maketrans", (PyCFunction) unicode_maketrans, |
| 12569 | METH_VARARGS | METH_STATIC, maketrans__doc__}, |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 12570 | {"__sizeof__", (PyCFunction) unicode__sizeof__, METH_NOARGS, sizeof__doc__}, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 12571 | #if 0 |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12572 | {"capwords", (PyCFunction) unicode_capwords, METH_NOARGS, capwords__doc__}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12573 | #endif |
| 12574 | |
| 12575 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12576 | /* These methods are just used for debugging the implementation. */ |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12577 | {"_decimal2ascii", (PyCFunction) unicode__decimal2ascii, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12578 | #endif |
| 12579 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12580 | {"__getnewargs__", (PyCFunction)unicode_getnewargs, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12581 | {NULL, NULL} |
| 12582 | }; |
| 12583 | |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 12584 | static PyObject * |
| 12585 | unicode_mod(PyObject *v, PyObject *w) |
| 12586 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 12587 | if (!PyUnicode_Check(v)) |
| 12588 | Py_RETURN_NOTIMPLEMENTED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12589 | return PyUnicode_Format(v, w); |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 12590 | } |
| 12591 | |
| 12592 | static PyNumberMethods unicode_as_number = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12593 | 0, /*nb_add*/ |
| 12594 | 0, /*nb_subtract*/ |
| 12595 | 0, /*nb_multiply*/ |
| 12596 | unicode_mod, /*nb_remainder*/ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 12597 | }; |
| 12598 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12599 | static PySequenceMethods unicode_as_sequence = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12600 | (lenfunc) unicode_length, /* sq_length */ |
| 12601 | PyUnicode_Concat, /* sq_concat */ |
| 12602 | (ssizeargfunc) unicode_repeat, /* sq_repeat */ |
| 12603 | (ssizeargfunc) unicode_getitem, /* sq_item */ |
| 12604 | 0, /* sq_slice */ |
| 12605 | 0, /* sq_ass_item */ |
| 12606 | 0, /* sq_ass_slice */ |
| 12607 | PyUnicode_Contains, /* sq_contains */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12608 | }; |
| 12609 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12610 | static PyObject* |
| 12611 | unicode_subscript(PyUnicodeObject* self, PyObject* item) |
| 12612 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12613 | if (PyUnicode_READY(self) == -1) |
| 12614 | return NULL; |
| 12615 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 12616 | if (PyIndex_Check(item)) { |
| 12617 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12618 | if (i == -1 && PyErr_Occurred()) |
| 12619 | return NULL; |
| 12620 | if (i < 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12621 | i += PyUnicode_GET_LENGTH(self); |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 12622 | return unicode_getitem((PyObject*)self, i); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12623 | } else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12624 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 12625 | PyObject *result; |
| 12626 | void *src_data, *dest_data; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 12627 | int src_kind, dest_kind; |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 12628 | Py_UCS4 ch, max_char, kind_limit; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12629 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12630 | if (PySlice_GetIndicesEx(item, PyUnicode_GET_LENGTH(self), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12631 | &start, &stop, &step, &slicelength) < 0) { |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12632 | return NULL; |
| 12633 | } |
| 12634 | |
| 12635 | if (slicelength <= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12636 | return PyUnicode_New(0, 0); |
| 12637 | } else if (start == 0 && step == 1 && |
| 12638 | slicelength == PyUnicode_GET_LENGTH(self) && |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 12639 | PyUnicode_CheckExact(self)) { |
| 12640 | Py_INCREF(self); |
| 12641 | return (PyObject *)self; |
| 12642 | } else if (step == 1) { |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 12643 | return PyUnicode_Substring((PyObject*)self, |
| 12644 | start, start + slicelength); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12645 | } |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 12646 | /* General case */ |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 12647 | max_char = 0; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 12648 | src_kind = PyUnicode_KIND(self); |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 12649 | kind_limit = kind_maxchar_limit(src_kind); |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 12650 | src_data = PyUnicode_DATA(self); |
| 12651 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
| 12652 | ch = PyUnicode_READ(src_kind, src_data, cur); |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 12653 | if (ch > max_char) { |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 12654 | max_char = ch; |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 12655 | if (max_char >= kind_limit) |
| 12656 | break; |
| 12657 | } |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 12658 | } |
| 12659 | result = PyUnicode_New(slicelength, max_char); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 12660 | if (result == NULL) |
| 12661 | return NULL; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 12662 | dest_kind = PyUnicode_KIND(result); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 12663 | dest_data = PyUnicode_DATA(result); |
| 12664 | |
| 12665 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 12666 | Py_UCS4 ch = PyUnicode_READ(src_kind, src_data, cur); |
| 12667 | PyUnicode_WRITE(dest_kind, dest_data, i, ch); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 12668 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12669 | assert(_PyUnicode_CheckConsistency(result, 1)); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 12670 | return result; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12671 | } else { |
| 12672 | PyErr_SetString(PyExc_TypeError, "string indices must be integers"); |
| 12673 | return NULL; |
| 12674 | } |
| 12675 | } |
| 12676 | |
| 12677 | static PyMappingMethods unicode_as_mapping = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12678 | (lenfunc)unicode_length, /* mp_length */ |
| 12679 | (binaryfunc)unicode_subscript, /* mp_subscript */ |
| 12680 | (objobjargproc)0, /* mp_ass_subscript */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12681 | }; |
| 12682 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12683 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12684 | /* Helpers for PyUnicode_Format() */ |
| 12685 | |
| 12686 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12687 | 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] | 12688 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12689 | Py_ssize_t argidx = *p_argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12690 | if (argidx < arglen) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12691 | (*p_argidx)++; |
| 12692 | if (arglen < 0) |
| 12693 | return args; |
| 12694 | else |
| 12695 | return PyTuple_GetItem(args, argidx); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12696 | } |
| 12697 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12698 | "not enough arguments for format string"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12699 | return NULL; |
| 12700 | } |
| 12701 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 12702 | /* 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] | 12703 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 12704 | static PyObject * |
| 12705 | formatfloat(PyObject *v, int flags, int prec, int type) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12706 | { |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 12707 | char *p; |
| 12708 | PyObject *result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12709 | double x; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 12710 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12711 | x = PyFloat_AsDouble(v); |
| 12712 | if (x == -1.0 && PyErr_Occurred()) |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 12713 | return NULL; |
| 12714 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12715 | if (prec < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12716 | prec = 6; |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 12717 | |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 12718 | p = PyOS_double_to_string(x, type, prec, |
| 12719 | (flags & F_ALT) ? Py_DTSF_ALT : 0, NULL); |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 12720 | if (p == NULL) |
| 12721 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12722 | result = PyUnicode_DecodeASCII(p, strlen(p), NULL); |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 12723 | PyMem_Free(p); |
| 12724 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12725 | } |
| 12726 | |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 12727 | static PyObject* |
| 12728 | formatlong(PyObject *val, int flags, int prec, int type) |
| 12729 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12730 | char *buf; |
| 12731 | int len; |
| 12732 | PyObject *str; /* temporary string object. */ |
| 12733 | PyObject *result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 12734 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12735 | str = _PyBytes_FormatLong(val, flags, prec, type, &buf, &len); |
| 12736 | if (!str) |
| 12737 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12738 | result = PyUnicode_DecodeASCII(buf, len, NULL); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12739 | Py_DECREF(str); |
| 12740 | return result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 12741 | } |
| 12742 | |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 12743 | static Py_UCS4 |
| 12744 | formatchar(PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12745 | { |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 12746 | /* presume that the buffer is at least 3 characters long */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 12747 | if (PyUnicode_Check(v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12748 | if (PyUnicode_GET_LENGTH(v) == 1) { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 12749 | return PyUnicode_READ_CHAR(v, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12750 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12751 | goto onError; |
| 12752 | } |
| 12753 | else { |
| 12754 | /* Integer input truncated to a character */ |
| 12755 | long x; |
| 12756 | x = PyLong_AsLong(v); |
| 12757 | if (x == -1 && PyErr_Occurred()) |
| 12758 | goto onError; |
| 12759 | |
| 12760 | if (x < 0 || x > 0x10ffff) { |
| 12761 | PyErr_SetString(PyExc_OverflowError, |
| 12762 | "%c arg not in range(0x110000)"); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 12763 | return (Py_UCS4) -1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12764 | } |
| 12765 | |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 12766 | return (Py_UCS4) x; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12767 | } |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 12768 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12769 | onError: |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 12770 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12771 | "%c requires int or char"); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 12772 | return (Py_UCS4) -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12773 | } |
| 12774 | |
Antoine Pitrou | 978b9d2 | 2011-10-07 12:35:48 +0200 | [diff] [blame] | 12775 | static int |
| 12776 | repeat_accumulate(_PyAccu *acc, PyObject *obj, Py_ssize_t count) |
| 12777 | { |
| 12778 | int r; |
| 12779 | assert(count > 0); |
| 12780 | assert(PyUnicode_Check(obj)); |
| 12781 | if (count > 5) { |
| 12782 | PyObject *repeated = unicode_repeat((PyUnicodeObject *) obj, count); |
| 12783 | if (repeated == NULL) |
| 12784 | return -1; |
| 12785 | r = _PyAccu_Accumulate(acc, repeated); |
| 12786 | Py_DECREF(repeated); |
| 12787 | return r; |
| 12788 | } |
| 12789 | else { |
| 12790 | do { |
| 12791 | if (_PyAccu_Accumulate(acc, obj)) |
| 12792 | return -1; |
| 12793 | } while (--count); |
| 12794 | return 0; |
| 12795 | } |
| 12796 | } |
| 12797 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12798 | PyObject * |
| 12799 | PyUnicode_Format(PyObject *format, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12800 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12801 | void *fmt; |
| 12802 | int fmtkind; |
| 12803 | PyObject *result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12804 | int kind; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 12805 | int r; |
| 12806 | Py_ssize_t fmtcnt, fmtpos, arglen, argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12807 | int args_owned = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12808 | PyObject *dict = NULL; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 12809 | PyObject *temp = NULL; |
| 12810 | PyObject *second = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12811 | PyUnicodeObject *uformat; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 12812 | _PyAccu acc; |
| 12813 | static PyObject *plus, *minus, *blank, *zero, *percent; |
| 12814 | |
| 12815 | if (!plus && !(plus = get_latin1_char('+'))) |
| 12816 | return NULL; |
| 12817 | if (!minus && !(minus = get_latin1_char('-'))) |
| 12818 | return NULL; |
| 12819 | if (!blank && !(blank = get_latin1_char(' '))) |
| 12820 | return NULL; |
| 12821 | if (!zero && !(zero = get_latin1_char('0'))) |
| 12822 | return NULL; |
| 12823 | if (!percent && !(percent = get_latin1_char('%'))) |
| 12824 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 12825 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12826 | if (format == NULL || args == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12827 | PyErr_BadInternalCall(); |
| 12828 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12829 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12830 | uformat = (PyUnicodeObject*)PyUnicode_FromObject(format); |
| 12831 | if (uformat == NULL || PyUnicode_READY(uformat) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12832 | return NULL; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 12833 | if (_PyAccu_Init(&acc)) |
| 12834 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12835 | fmt = PyUnicode_DATA(uformat); |
| 12836 | fmtkind = PyUnicode_KIND(uformat); |
| 12837 | fmtcnt = PyUnicode_GET_LENGTH(uformat); |
| 12838 | fmtpos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12839 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12840 | if (PyTuple_Check(args)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12841 | arglen = PyTuple_Size(args); |
| 12842 | argidx = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12843 | } |
| 12844 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12845 | arglen = -1; |
| 12846 | argidx = -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12847 | } |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 12848 | if (Py_TYPE(args)->tp_as_mapping && !PyTuple_Check(args) && |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 12849 | !PyUnicode_Check(args)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12850 | dict = args; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12851 | |
| 12852 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12853 | if (PyUnicode_READ(fmtkind, fmt, fmtpos) != '%') { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 12854 | PyObject *nonfmt; |
| 12855 | Py_ssize_t nonfmtpos; |
| 12856 | nonfmtpos = fmtpos++; |
| 12857 | while (fmtcnt >= 0 && |
| 12858 | PyUnicode_READ(fmtkind, fmt, fmtpos) != '%') { |
| 12859 | fmtpos++; |
| 12860 | fmtcnt--; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12861 | } |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 12862 | nonfmt = PyUnicode_Substring((PyObject *) uformat, nonfmtpos, fmtpos); |
| 12863 | if (nonfmt == NULL) |
| 12864 | goto onError; |
| 12865 | r = _PyAccu_Accumulate(&acc, nonfmt); |
| 12866 | Py_DECREF(nonfmt); |
| 12867 | if (r) |
| 12868 | goto onError; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12869 | } |
| 12870 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12871 | /* Got a format specifier */ |
| 12872 | int flags = 0; |
| 12873 | Py_ssize_t width = -1; |
| 12874 | int prec = -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12875 | Py_UCS4 c = '\0'; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 12876 | Py_UCS4 fill, sign; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12877 | int isnumok; |
| 12878 | PyObject *v = NULL; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 12879 | void *pbuf = NULL; |
| 12880 | Py_ssize_t pindex, len; |
| 12881 | PyObject *signobj = NULL, *fillobj = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12882 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12883 | fmtpos++; |
| 12884 | if (PyUnicode_READ(fmtkind, fmt, fmtpos) == '(') { |
| 12885 | Py_ssize_t keystart; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12886 | Py_ssize_t keylen; |
| 12887 | PyObject *key; |
| 12888 | int pcount = 1; |
Christian Heimes | a612dc0 | 2008-02-24 13:08:18 +0000 | [diff] [blame] | 12889 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12890 | if (dict == NULL) { |
| 12891 | PyErr_SetString(PyExc_TypeError, |
| 12892 | "format requires a mapping"); |
| 12893 | goto onError; |
| 12894 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12895 | ++fmtpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12896 | --fmtcnt; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12897 | keystart = fmtpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12898 | /* Skip over balanced parentheses */ |
| 12899 | while (pcount > 0 && --fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12900 | if (PyUnicode_READ(fmtkind, fmt, fmtpos) == ')') |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12901 | --pcount; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12902 | else if (PyUnicode_READ(fmtkind, fmt, fmtpos) == '(') |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12903 | ++pcount; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12904 | fmtpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12905 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12906 | keylen = fmtpos - keystart - 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12907 | if (fmtcnt < 0 || pcount > 0) { |
| 12908 | PyErr_SetString(PyExc_ValueError, |
| 12909 | "incomplete format key"); |
| 12910 | goto onError; |
| 12911 | } |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 12912 | key = PyUnicode_Substring((PyObject*)uformat, |
| 12913 | keystart, keystart + keylen); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12914 | if (key == NULL) |
| 12915 | goto onError; |
| 12916 | if (args_owned) { |
| 12917 | Py_DECREF(args); |
| 12918 | args_owned = 0; |
| 12919 | } |
| 12920 | args = PyObject_GetItem(dict, key); |
| 12921 | Py_DECREF(key); |
| 12922 | if (args == NULL) { |
| 12923 | goto onError; |
| 12924 | } |
| 12925 | args_owned = 1; |
| 12926 | arglen = -1; |
| 12927 | argidx = -2; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12928 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12929 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12930 | switch (c = PyUnicode_READ(fmtkind, fmt, fmtpos++)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12931 | case '-': flags |= F_LJUST; continue; |
| 12932 | case '+': flags |= F_SIGN; continue; |
| 12933 | case ' ': flags |= F_BLANK; continue; |
| 12934 | case '#': flags |= F_ALT; continue; |
| 12935 | case '0': flags |= F_ZERO; continue; |
| 12936 | } |
| 12937 | break; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12938 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12939 | if (c == '*') { |
| 12940 | v = getnextarg(args, arglen, &argidx); |
| 12941 | if (v == NULL) |
| 12942 | goto onError; |
| 12943 | if (!PyLong_Check(v)) { |
| 12944 | PyErr_SetString(PyExc_TypeError, |
| 12945 | "* wants int"); |
| 12946 | goto onError; |
| 12947 | } |
| 12948 | width = PyLong_AsLong(v); |
| 12949 | if (width == -1 && PyErr_Occurred()) |
| 12950 | goto onError; |
| 12951 | if (width < 0) { |
| 12952 | flags |= F_LJUST; |
| 12953 | width = -width; |
| 12954 | } |
| 12955 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12956 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12957 | } |
| 12958 | else if (c >= '0' && c <= '9') { |
| 12959 | width = c - '0'; |
| 12960 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12961 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12962 | if (c < '0' || c > '9') |
| 12963 | break; |
| 12964 | if ((width*10) / 10 != width) { |
| 12965 | PyErr_SetString(PyExc_ValueError, |
| 12966 | "width too big"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12967 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12968 | } |
| 12969 | width = width*10 + (c - '0'); |
| 12970 | } |
| 12971 | } |
| 12972 | if (c == '.') { |
| 12973 | prec = 0; |
| 12974 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12975 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12976 | if (c == '*') { |
| 12977 | v = getnextarg(args, arglen, &argidx); |
| 12978 | if (v == NULL) |
| 12979 | goto onError; |
| 12980 | if (!PyLong_Check(v)) { |
| 12981 | PyErr_SetString(PyExc_TypeError, |
| 12982 | "* wants int"); |
| 12983 | goto onError; |
| 12984 | } |
| 12985 | prec = PyLong_AsLong(v); |
| 12986 | if (prec == -1 && PyErr_Occurred()) |
| 12987 | goto onError; |
| 12988 | if (prec < 0) |
| 12989 | prec = 0; |
| 12990 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12991 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12992 | } |
| 12993 | else if (c >= '0' && c <= '9') { |
| 12994 | prec = c - '0'; |
| 12995 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12996 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12997 | if (c < '0' || c > '9') |
| 12998 | break; |
| 12999 | if ((prec*10) / 10 != prec) { |
| 13000 | PyErr_SetString(PyExc_ValueError, |
| 13001 | "prec too big"); |
| 13002 | goto onError; |
| 13003 | } |
| 13004 | prec = prec*10 + (c - '0'); |
| 13005 | } |
| 13006 | } |
| 13007 | } /* prec */ |
| 13008 | if (fmtcnt >= 0) { |
| 13009 | if (c == 'h' || c == 'l' || c == 'L') { |
| 13010 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13011 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13012 | } |
| 13013 | } |
| 13014 | if (fmtcnt < 0) { |
| 13015 | PyErr_SetString(PyExc_ValueError, |
| 13016 | "incomplete format"); |
| 13017 | goto onError; |
| 13018 | } |
| 13019 | if (c != '%') { |
| 13020 | v = getnextarg(args, arglen, &argidx); |
| 13021 | if (v == NULL) |
| 13022 | goto onError; |
| 13023 | } |
| 13024 | sign = 0; |
| 13025 | fill = ' '; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13026 | fillobj = blank; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13027 | switch (c) { |
| 13028 | |
| 13029 | case '%': |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13030 | _PyAccu_Accumulate(&acc, percent); |
| 13031 | continue; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13032 | |
| 13033 | case 's': |
| 13034 | case 'r': |
| 13035 | case 'a': |
Victor Stinner | 808fc0a | 2010-03-22 12:50:40 +0000 | [diff] [blame] | 13036 | if (PyUnicode_CheckExact(v) && c == 's') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13037 | temp = v; |
| 13038 | Py_INCREF(temp); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13039 | } |
| 13040 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13041 | if (c == 's') |
| 13042 | temp = PyObject_Str(v); |
| 13043 | else if (c == 'r') |
| 13044 | temp = PyObject_Repr(v); |
| 13045 | else |
| 13046 | temp = PyObject_ASCII(v); |
| 13047 | if (temp == NULL) |
| 13048 | goto onError; |
| 13049 | if (PyUnicode_Check(temp)) |
| 13050 | /* nothing to do */; |
| 13051 | else { |
| 13052 | Py_DECREF(temp); |
| 13053 | PyErr_SetString(PyExc_TypeError, |
| 13054 | "%s argument has non-string str()"); |
| 13055 | goto onError; |
| 13056 | } |
| 13057 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13058 | if (PyUnicode_READY(temp) == -1) { |
| 13059 | Py_CLEAR(temp); |
| 13060 | goto onError; |
| 13061 | } |
| 13062 | pbuf = PyUnicode_DATA(temp); |
| 13063 | kind = PyUnicode_KIND(temp); |
| 13064 | len = PyUnicode_GET_LENGTH(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13065 | if (prec >= 0 && len > prec) |
| 13066 | len = prec; |
| 13067 | break; |
| 13068 | |
| 13069 | case 'i': |
| 13070 | case 'd': |
| 13071 | case 'u': |
| 13072 | case 'o': |
| 13073 | case 'x': |
| 13074 | case 'X': |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13075 | isnumok = 0; |
| 13076 | if (PyNumber_Check(v)) { |
| 13077 | PyObject *iobj=NULL; |
| 13078 | |
| 13079 | if (PyLong_Check(v)) { |
| 13080 | iobj = v; |
| 13081 | Py_INCREF(iobj); |
| 13082 | } |
| 13083 | else { |
| 13084 | iobj = PyNumber_Long(v); |
| 13085 | } |
| 13086 | if (iobj!=NULL) { |
| 13087 | if (PyLong_Check(iobj)) { |
| 13088 | isnumok = 1; |
Senthil Kumaran | 9ebe08d | 2011-07-03 21:03:16 -0700 | [diff] [blame] | 13089 | temp = formatlong(iobj, flags, prec, (c == 'i'? 'd': c)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13090 | Py_DECREF(iobj); |
| 13091 | if (!temp) |
| 13092 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13093 | if (PyUnicode_READY(temp) == -1) { |
| 13094 | Py_CLEAR(temp); |
| 13095 | goto onError; |
| 13096 | } |
| 13097 | pbuf = PyUnicode_DATA(temp); |
| 13098 | kind = PyUnicode_KIND(temp); |
| 13099 | len = PyUnicode_GET_LENGTH(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13100 | sign = 1; |
| 13101 | } |
| 13102 | else { |
| 13103 | Py_DECREF(iobj); |
| 13104 | } |
| 13105 | } |
| 13106 | } |
| 13107 | if (!isnumok) { |
| 13108 | PyErr_Format(PyExc_TypeError, |
| 13109 | "%%%c format: a number is required, " |
| 13110 | "not %.200s", (char)c, Py_TYPE(v)->tp_name); |
| 13111 | goto onError; |
| 13112 | } |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13113 | if (flags & F_ZERO) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13114 | fill = '0'; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13115 | fillobj = zero; |
| 13116 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13117 | break; |
| 13118 | |
| 13119 | case 'e': |
| 13120 | case 'E': |
| 13121 | case 'f': |
| 13122 | case 'F': |
| 13123 | case 'g': |
| 13124 | case 'G': |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13125 | temp = formatfloat(v, flags, prec, c); |
| 13126 | if (!temp) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13127 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13128 | if (PyUnicode_READY(temp) == -1) { |
| 13129 | Py_CLEAR(temp); |
| 13130 | goto onError; |
| 13131 | } |
| 13132 | pbuf = PyUnicode_DATA(temp); |
| 13133 | kind = PyUnicode_KIND(temp); |
| 13134 | len = PyUnicode_GET_LENGTH(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13135 | sign = 1; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13136 | if (flags & F_ZERO) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13137 | fill = '0'; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13138 | fillobj = zero; |
| 13139 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13140 | break; |
| 13141 | |
| 13142 | case 'c': |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13143 | { |
| 13144 | Py_UCS4 ch = formatchar(v); |
| 13145 | if (ch == (Py_UCS4) -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13146 | goto onError; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13147 | temp = _PyUnicode_FromUCS4(&ch, 1); |
| 13148 | if (temp == NULL) |
| 13149 | goto onError; |
| 13150 | pbuf = PyUnicode_DATA(temp); |
| 13151 | kind = PyUnicode_KIND(temp); |
| 13152 | len = PyUnicode_GET_LENGTH(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13153 | break; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13154 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13155 | |
| 13156 | default: |
| 13157 | PyErr_Format(PyExc_ValueError, |
| 13158 | "unsupported format character '%c' (0x%x) " |
| 13159 | "at index %zd", |
| 13160 | (31<=c && c<=126) ? (char)c : '?', |
| 13161 | (int)c, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13162 | fmtpos - 1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13163 | goto onError; |
| 13164 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13165 | /* pbuf is initialized here. */ |
| 13166 | pindex = 0; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13167 | if (sign) { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13168 | if (PyUnicode_READ(kind, pbuf, pindex) == '-') { |
| 13169 | signobj = minus; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13170 | len--; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13171 | pindex++; |
| 13172 | } |
| 13173 | else if (PyUnicode_READ(kind, pbuf, pindex) == '+') { |
| 13174 | signobj = plus; |
| 13175 | len--; |
| 13176 | pindex++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13177 | } |
| 13178 | else if (flags & F_SIGN) |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13179 | signobj = plus; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13180 | else if (flags & F_BLANK) |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13181 | signobj = blank; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13182 | else |
| 13183 | sign = 0; |
| 13184 | } |
| 13185 | if (width < len) |
| 13186 | width = len; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13187 | if (sign) { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13188 | if (fill != ' ') { |
| 13189 | assert(signobj != NULL); |
| 13190 | if (_PyAccu_Accumulate(&acc, signobj)) |
| 13191 | goto onError; |
| 13192 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13193 | if (width > len) |
| 13194 | width--; |
| 13195 | } |
| 13196 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13197 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13198 | assert(PyUnicode_READ(kind, pbuf, pindex + 1) == c); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13199 | if (fill != ' ') { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13200 | second = get_latin1_char( |
| 13201 | PyUnicode_READ(kind, pbuf, pindex + 1)); |
| 13202 | pindex += 2; |
| 13203 | if (second == NULL || |
| 13204 | _PyAccu_Accumulate(&acc, zero) || |
| 13205 | _PyAccu_Accumulate(&acc, second)) |
| 13206 | goto onError; |
| 13207 | Py_CLEAR(second); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13208 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13209 | width -= 2; |
| 13210 | if (width < 0) |
| 13211 | width = 0; |
| 13212 | len -= 2; |
| 13213 | } |
| 13214 | if (width > len && !(flags & F_LJUST)) { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13215 | assert(fillobj != NULL); |
Antoine Pitrou | 978b9d2 | 2011-10-07 12:35:48 +0200 | [diff] [blame] | 13216 | if (repeat_accumulate(&acc, fillobj, width - len)) |
| 13217 | goto onError; |
| 13218 | width = len; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13219 | } |
| 13220 | if (fill == ' ') { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13221 | if (sign) { |
| 13222 | assert(signobj != NULL); |
| 13223 | if (_PyAccu_Accumulate(&acc, signobj)) |
| 13224 | goto onError; |
| 13225 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13226 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13227 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
| 13228 | assert(PyUnicode_READ(kind, pbuf, pindex+1) == c); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13229 | second = get_latin1_char( |
| 13230 | PyUnicode_READ(kind, pbuf, pindex + 1)); |
| 13231 | pindex += 2; |
| 13232 | if (second == NULL || |
| 13233 | _PyAccu_Accumulate(&acc, zero) || |
| 13234 | _PyAccu_Accumulate(&acc, second)) |
| 13235 | goto onError; |
| 13236 | Py_CLEAR(second); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13237 | } |
| 13238 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13239 | /* Copy all characters, preserving len */ |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13240 | if (temp != NULL) { |
| 13241 | assert(pbuf == PyUnicode_DATA(temp)); |
| 13242 | v = PyUnicode_Substring(temp, pindex, pindex + len); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13243 | } |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13244 | else { |
| 13245 | const char *p = (const char *) pbuf; |
| 13246 | assert(pbuf != NULL); |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame^] | 13247 | p += kind * pindex; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13248 | v = PyUnicode_FromKindAndData(kind, p, len); |
| 13249 | } |
| 13250 | if (v == NULL) |
| 13251 | goto onError; |
| 13252 | r = _PyAccu_Accumulate(&acc, v); |
| 13253 | Py_DECREF(v); |
| 13254 | if (r) |
| 13255 | goto onError; |
Antoine Pitrou | 978b9d2 | 2011-10-07 12:35:48 +0200 | [diff] [blame] | 13256 | if (width > len && repeat_accumulate(&acc, blank, width - len)) |
| 13257 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13258 | if (dict && (argidx < arglen) && c != '%') { |
| 13259 | PyErr_SetString(PyExc_TypeError, |
| 13260 | "not all arguments converted during string formatting"); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13261 | goto onError; |
| 13262 | } |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13263 | Py_CLEAR(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13264 | } /* '%' */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13265 | } /* until end */ |
| 13266 | if (argidx < arglen && !dict) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13267 | PyErr_SetString(PyExc_TypeError, |
| 13268 | "not all arguments converted during string formatting"); |
| 13269 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13270 | } |
| 13271 | |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13272 | result = _PyAccu_Finish(&acc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13273 | if (args_owned) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13274 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13275 | } |
| 13276 | Py_DECREF(uformat); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13277 | Py_XDECREF(temp); |
| 13278 | Py_XDECREF(second); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13279 | return (PyObject *)result; |
| 13280 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13281 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13282 | Py_DECREF(uformat); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13283 | Py_XDECREF(temp); |
| 13284 | Py_XDECREF(second); |
| 13285 | _PyAccu_Destroy(&acc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13286 | if (args_owned) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13287 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13288 | } |
| 13289 | return NULL; |
| 13290 | } |
| 13291 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 13292 | static PyObject * |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 13293 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 13294 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13295 | static PyObject * |
| 13296 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 13297 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13298 | PyObject *x = NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13299 | static char *kwlist[] = {"object", "encoding", "errors", 0}; |
| 13300 | char *encoding = NULL; |
| 13301 | char *errors = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13302 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13303 | if (type != &PyUnicode_Type) |
| 13304 | return unicode_subtype_new(type, args, kwds); |
| 13305 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:str", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13306 | kwlist, &x, &encoding, &errors)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13307 | return NULL; |
| 13308 | if (x == NULL) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13309 | return (PyObject *)PyUnicode_New(0, 0); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13310 | if (encoding == NULL && errors == NULL) |
| 13311 | return PyObject_Str(x); |
| 13312 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13313 | return PyUnicode_FromEncodedObject(x, encoding, errors); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13314 | } |
| 13315 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 13316 | static PyObject * |
| 13317 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 13318 | { |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13319 | PyUnicodeObject *unicode, *self; |
| 13320 | Py_ssize_t length, char_size; |
| 13321 | int share_wstr, share_utf8; |
| 13322 | unsigned int kind; |
| 13323 | void *data; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 13324 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13325 | assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13326 | |
| 13327 | unicode = (PyUnicodeObject *)unicode_new(&PyUnicode_Type, args, kwds); |
| 13328 | if (unicode == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13329 | return NULL; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 13330 | assert(_PyUnicode_CHECK(unicode)); |
Victor Stinner | e06e145 | 2011-10-04 20:52:31 +0200 | [diff] [blame] | 13331 | if (PyUnicode_READY(unicode)) |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13332 | return NULL; |
| 13333 | |
| 13334 | self = (PyUnicodeObject *) type->tp_alloc(type, 0); |
| 13335 | if (self == NULL) { |
| 13336 | Py_DECREF(unicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13337 | return NULL; |
| 13338 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13339 | kind = PyUnicode_KIND(unicode); |
| 13340 | length = PyUnicode_GET_LENGTH(unicode); |
| 13341 | |
| 13342 | _PyUnicode_LENGTH(self) = length; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 13343 | #ifdef Py_DEBUG |
| 13344 | _PyUnicode_HASH(self) = -1; |
| 13345 | #else |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13346 | _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 13347 | #endif |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13348 | _PyUnicode_STATE(self).interned = 0; |
| 13349 | _PyUnicode_STATE(self).kind = kind; |
| 13350 | _PyUnicode_STATE(self).compact = 0; |
Victor Stinner | 3cf4637 | 2011-10-03 14:42:15 +0200 | [diff] [blame] | 13351 | _PyUnicode_STATE(self).ascii = _PyUnicode_STATE(unicode).ascii; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13352 | _PyUnicode_STATE(self).ready = 1; |
| 13353 | _PyUnicode_WSTR(self) = NULL; |
| 13354 | _PyUnicode_UTF8_LENGTH(self) = 0; |
| 13355 | _PyUnicode_UTF8(self) = NULL; |
| 13356 | _PyUnicode_WSTR_LENGTH(self) = 0; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 13357 | _PyUnicode_DATA_ANY(self) = NULL; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13358 | |
| 13359 | share_utf8 = 0; |
| 13360 | share_wstr = 0; |
| 13361 | if (kind == PyUnicode_1BYTE_KIND) { |
| 13362 | char_size = 1; |
| 13363 | if (PyUnicode_MAX_CHAR_VALUE(unicode) < 128) |
| 13364 | share_utf8 = 1; |
| 13365 | } |
| 13366 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 13367 | char_size = 2; |
| 13368 | if (sizeof(wchar_t) == 2) |
| 13369 | share_wstr = 1; |
| 13370 | } |
| 13371 | else { |
| 13372 | assert(kind == PyUnicode_4BYTE_KIND); |
| 13373 | char_size = 4; |
| 13374 | if (sizeof(wchar_t) == 4) |
| 13375 | share_wstr = 1; |
| 13376 | } |
| 13377 | |
| 13378 | /* Ensure we won't overflow the length. */ |
| 13379 | if (length > (PY_SSIZE_T_MAX / char_size - 1)) { |
| 13380 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13381 | goto onError; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13382 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13383 | data = PyObject_MALLOC((length + 1) * char_size); |
| 13384 | if (data == NULL) { |
| 13385 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13386 | goto onError; |
| 13387 | } |
| 13388 | |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 13389 | _PyUnicode_DATA_ANY(self) = data; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13390 | if (share_utf8) { |
| 13391 | _PyUnicode_UTF8_LENGTH(self) = length; |
| 13392 | _PyUnicode_UTF8(self) = data; |
| 13393 | } |
| 13394 | if (share_wstr) { |
| 13395 | _PyUnicode_WSTR_LENGTH(self) = length; |
| 13396 | _PyUnicode_WSTR(self) = (wchar_t *)data; |
| 13397 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13398 | |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13399 | Py_MEMCPY(data, PyUnicode_DATA(unicode), |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame^] | 13400 | kind * (length + 1)); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13401 | Py_DECREF(unicode); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13402 | assert(_PyUnicode_CheckConsistency(self, 1)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 13403 | #ifdef Py_DEBUG |
| 13404 | _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode); |
| 13405 | #endif |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13406 | return (PyObject *)self; |
| 13407 | |
| 13408 | onError: |
| 13409 | Py_DECREF(unicode); |
| 13410 | Py_DECREF(self); |
| 13411 | return NULL; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 13412 | } |
| 13413 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 13414 | PyDoc_STRVAR(unicode_doc, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13415 | "str(string[, encoding[, errors]]) -> str\n\ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13416 | \n\ |
Collin Winter | d474ce8 | 2007-08-07 19:42:11 +0000 | [diff] [blame] | 13417 | Create a new string object from the given encoded string.\n\ |
Skip Montanaro | 35b37a5 | 2002-07-26 16:22:46 +0000 | [diff] [blame] | 13418 | encoding defaults to the current default string encoding.\n\ |
| 13419 | errors can be 'strict', 'replace' or 'ignore' and defaults to 'strict'."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13420 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13421 | static PyObject *unicode_iter(PyObject *seq); |
| 13422 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13423 | PyTypeObject PyUnicode_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 13424 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13425 | "str", /* tp_name */ |
| 13426 | sizeof(PyUnicodeObject), /* tp_size */ |
| 13427 | 0, /* tp_itemsize */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13428 | /* Slots */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13429 | (destructor)unicode_dealloc, /* tp_dealloc */ |
| 13430 | 0, /* tp_print */ |
| 13431 | 0, /* tp_getattr */ |
| 13432 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 13433 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13434 | unicode_repr, /* tp_repr */ |
| 13435 | &unicode_as_number, /* tp_as_number */ |
| 13436 | &unicode_as_sequence, /* tp_as_sequence */ |
| 13437 | &unicode_as_mapping, /* tp_as_mapping */ |
| 13438 | (hashfunc) unicode_hash, /* tp_hash*/ |
| 13439 | 0, /* tp_call*/ |
| 13440 | (reprfunc) unicode_str, /* tp_str */ |
| 13441 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 13442 | 0, /* tp_setattro */ |
| 13443 | 0, /* tp_as_buffer */ |
| 13444 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13445 | Py_TPFLAGS_UNICODE_SUBCLASS, /* tp_flags */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13446 | unicode_doc, /* tp_doc */ |
| 13447 | 0, /* tp_traverse */ |
| 13448 | 0, /* tp_clear */ |
| 13449 | PyUnicode_RichCompare, /* tp_richcompare */ |
| 13450 | 0, /* tp_weaklistoffset */ |
| 13451 | unicode_iter, /* tp_iter */ |
| 13452 | 0, /* tp_iternext */ |
| 13453 | unicode_methods, /* tp_methods */ |
| 13454 | 0, /* tp_members */ |
| 13455 | 0, /* tp_getset */ |
| 13456 | &PyBaseObject_Type, /* tp_base */ |
| 13457 | 0, /* tp_dict */ |
| 13458 | 0, /* tp_descr_get */ |
| 13459 | 0, /* tp_descr_set */ |
| 13460 | 0, /* tp_dictoffset */ |
| 13461 | 0, /* tp_init */ |
| 13462 | 0, /* tp_alloc */ |
| 13463 | unicode_new, /* tp_new */ |
| 13464 | PyObject_Del, /* tp_free */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13465 | }; |
| 13466 | |
| 13467 | /* Initialize the Unicode implementation */ |
| 13468 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 13469 | void _PyUnicode_Init(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13470 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 13471 | int i; |
| 13472 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13473 | /* XXX - move this array to unicodectype.c ? */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13474 | Py_UCS2 linebreak[] = { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13475 | 0x000A, /* LINE FEED */ |
| 13476 | 0x000D, /* CARRIAGE RETURN */ |
| 13477 | 0x001C, /* FILE SEPARATOR */ |
| 13478 | 0x001D, /* GROUP SEPARATOR */ |
| 13479 | 0x001E, /* RECORD SEPARATOR */ |
| 13480 | 0x0085, /* NEXT LINE */ |
| 13481 | 0x2028, /* LINE SEPARATOR */ |
| 13482 | 0x2029, /* PARAGRAPH SEPARATOR */ |
| 13483 | }; |
| 13484 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 13485 | /* Init the implementation */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 13486 | unicode_empty = PyUnicode_New(0, 0); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13487 | assert(_PyUnicode_CheckConsistency(unicode_empty, 1)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13488 | if (!unicode_empty) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13489 | Py_FatalError("Can't create empty string"); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13490 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 13491 | for (i = 0; i < 256; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13492 | unicode_latin1[i] = NULL; |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 13493 | if (PyType_Ready(&PyUnicode_Type) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13494 | Py_FatalError("Can't initialize 'unicode'"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13495 | |
| 13496 | /* initialize the linebreak bloom filter */ |
| 13497 | bloom_linebreak = make_bloom_mask( |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13498 | PyUnicode_2BYTE_KIND, linebreak, |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 13499 | Py_ARRAY_LENGTH(linebreak)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13500 | |
| 13501 | PyType_Ready(&EncodingMapType); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13502 | } |
| 13503 | |
| 13504 | /* Finalize the Unicode implementation */ |
| 13505 | |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 13506 | int |
| 13507 | PyUnicode_ClearFreeList(void) |
| 13508 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13509 | return 0; |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 13510 | } |
| 13511 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13512 | void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 13513 | _PyUnicode_Fini(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13514 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 13515 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13516 | |
Guido van Rossum | 4ae8ef8 | 2000-10-03 18:09:04 +0000 | [diff] [blame] | 13517 | Py_XDECREF(unicode_empty); |
| 13518 | unicode_empty = NULL; |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 13519 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 13520 | for (i = 0; i < 256; i++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13521 | if (unicode_latin1[i]) { |
| 13522 | Py_DECREF(unicode_latin1[i]); |
| 13523 | unicode_latin1[i] = NULL; |
| 13524 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 13525 | } |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 13526 | (void)PyUnicode_ClearFreeList(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13527 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 13528 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13529 | void |
| 13530 | PyUnicode_InternInPlace(PyObject **p) |
| 13531 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13532 | register PyUnicodeObject *s = (PyUnicodeObject *)(*p); |
| 13533 | PyObject *t; |
Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 13534 | #ifdef Py_DEBUG |
| 13535 | assert(s != NULL); |
| 13536 | assert(_PyUnicode_CHECK(s)); |
| 13537 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13538 | if (s == NULL || !PyUnicode_Check(s)) |
Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 13539 | return; |
| 13540 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13541 | /* If it's a subclass, we don't really know what putting |
| 13542 | it in the interned dict might do. */ |
| 13543 | if (!PyUnicode_CheckExact(s)) |
| 13544 | return; |
| 13545 | if (PyUnicode_CHECK_INTERNED(s)) |
| 13546 | return; |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 13547 | if (_PyUnicode_READY_REPLACE(p)) { |
Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 13548 | assert(0 && "_PyUnicode_READY_REPLACE fail in PyUnicode_InternInPlace"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13549 | return; |
| 13550 | } |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 13551 | s = (PyUnicodeObject *)(*p); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13552 | if (interned == NULL) { |
| 13553 | interned = PyDict_New(); |
| 13554 | if (interned == NULL) { |
| 13555 | PyErr_Clear(); /* Don't leave an exception */ |
| 13556 | return; |
| 13557 | } |
| 13558 | } |
| 13559 | /* It might be that the GetItem call fails even |
| 13560 | though the key is present in the dictionary, |
| 13561 | namely when this happens during a stack overflow. */ |
| 13562 | Py_ALLOW_RECURSION |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13563 | t = PyDict_GetItem(interned, (PyObject *)s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13564 | Py_END_ALLOW_RECURSION |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 13565 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13566 | if (t) { |
| 13567 | Py_INCREF(t); |
| 13568 | Py_DECREF(*p); |
| 13569 | *p = t; |
| 13570 | return; |
| 13571 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13572 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13573 | PyThreadState_GET()->recursion_critical = 1; |
| 13574 | if (PyDict_SetItem(interned, (PyObject *)s, (PyObject *)s) < 0) { |
| 13575 | PyErr_Clear(); |
| 13576 | PyThreadState_GET()->recursion_critical = 0; |
| 13577 | return; |
| 13578 | } |
| 13579 | PyThreadState_GET()->recursion_critical = 0; |
| 13580 | /* The two references in interned are not counted by refcnt. |
| 13581 | The deallocator will take care of this */ |
| 13582 | Py_REFCNT(s) -= 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13583 | _PyUnicode_STATE(s).interned = SSTATE_INTERNED_MORTAL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13584 | } |
| 13585 | |
| 13586 | void |
| 13587 | PyUnicode_InternImmortal(PyObject **p) |
| 13588 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13589 | PyUnicodeObject *u = (PyUnicodeObject *)*p; |
| 13590 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13591 | PyUnicode_InternInPlace(p); |
| 13592 | if (PyUnicode_CHECK_INTERNED(*p) != SSTATE_INTERNED_IMMORTAL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13593 | _PyUnicode_STATE(u).interned = SSTATE_INTERNED_IMMORTAL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13594 | Py_INCREF(*p); |
| 13595 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13596 | } |
| 13597 | |
| 13598 | PyObject * |
| 13599 | PyUnicode_InternFromString(const char *cp) |
| 13600 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13601 | PyObject *s = PyUnicode_FromString(cp); |
| 13602 | if (s == NULL) |
| 13603 | return NULL; |
| 13604 | PyUnicode_InternInPlace(&s); |
| 13605 | return s; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13606 | } |
| 13607 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 13608 | void |
| 13609 | _Py_ReleaseInternedUnicodeStrings(void) |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13610 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13611 | PyObject *keys; |
| 13612 | PyUnicodeObject *s; |
| 13613 | Py_ssize_t i, n; |
| 13614 | Py_ssize_t immortal_size = 0, mortal_size = 0; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13615 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13616 | if (interned == NULL || !PyDict_Check(interned)) |
| 13617 | return; |
| 13618 | keys = PyDict_Keys(interned); |
| 13619 | if (keys == NULL || !PyList_Check(keys)) { |
| 13620 | PyErr_Clear(); |
| 13621 | return; |
| 13622 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13623 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13624 | /* Since _Py_ReleaseInternedUnicodeStrings() is intended to help a leak |
| 13625 | detector, interned unicode strings are not forcibly deallocated; |
| 13626 | rather, we give them their stolen references back, and then clear |
| 13627 | and DECREF the interned dict. */ |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13628 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13629 | n = PyList_GET_SIZE(keys); |
| 13630 | fprintf(stderr, "releasing %" PY_FORMAT_SIZE_T "d interned strings\n", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13631 | n); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13632 | for (i = 0; i < n; i++) { |
| 13633 | s = (PyUnicodeObject *) PyList_GET_ITEM(keys, i); |
Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 13634 | if (PyUnicode_READY(s) == -1) { |
| 13635 | assert(0 && "could not ready string"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13636 | fprintf(stderr, "could not ready string\n"); |
Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 13637 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13638 | switch (PyUnicode_CHECK_INTERNED(s)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13639 | case SSTATE_NOT_INTERNED: |
| 13640 | /* XXX Shouldn't happen */ |
| 13641 | break; |
| 13642 | case SSTATE_INTERNED_IMMORTAL: |
| 13643 | Py_REFCNT(s) += 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13644 | immortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13645 | break; |
| 13646 | case SSTATE_INTERNED_MORTAL: |
| 13647 | Py_REFCNT(s) += 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13648 | mortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13649 | break; |
| 13650 | default: |
| 13651 | Py_FatalError("Inconsistent interned string state."); |
| 13652 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13653 | _PyUnicode_STATE(s).interned = SSTATE_NOT_INTERNED; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13654 | } |
| 13655 | fprintf(stderr, "total size of all interned strings: " |
| 13656 | "%" PY_FORMAT_SIZE_T "d/%" PY_FORMAT_SIZE_T "d " |
| 13657 | "mortal/immortal\n", mortal_size, immortal_size); |
| 13658 | Py_DECREF(keys); |
| 13659 | PyDict_Clear(interned); |
| 13660 | Py_DECREF(interned); |
| 13661 | interned = NULL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13662 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13663 | |
| 13664 | |
| 13665 | /********************* Unicode Iterator **************************/ |
| 13666 | |
| 13667 | typedef struct { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13668 | PyObject_HEAD |
| 13669 | Py_ssize_t it_index; |
| 13670 | PyUnicodeObject *it_seq; /* Set to NULL when iterator is exhausted */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13671 | } unicodeiterobject; |
| 13672 | |
| 13673 | static void |
| 13674 | unicodeiter_dealloc(unicodeiterobject *it) |
| 13675 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13676 | _PyObject_GC_UNTRACK(it); |
| 13677 | Py_XDECREF(it->it_seq); |
| 13678 | PyObject_GC_Del(it); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13679 | } |
| 13680 | |
| 13681 | static int |
| 13682 | unicodeiter_traverse(unicodeiterobject *it, visitproc visit, void *arg) |
| 13683 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13684 | Py_VISIT(it->it_seq); |
| 13685 | return 0; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13686 | } |
| 13687 | |
| 13688 | static PyObject * |
| 13689 | unicodeiter_next(unicodeiterobject *it) |
| 13690 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13691 | PyUnicodeObject *seq; |
| 13692 | PyObject *item; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13693 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13694 | assert(it != NULL); |
| 13695 | seq = it->it_seq; |
| 13696 | if (seq == NULL) |
| 13697 | return NULL; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 13698 | assert(_PyUnicode_CHECK(seq)); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13699 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13700 | if (it->it_index < PyUnicode_GET_LENGTH(seq)) { |
| 13701 | int kind = PyUnicode_KIND(seq); |
| 13702 | void *data = PyUnicode_DATA(seq); |
| 13703 | Py_UCS4 chr = PyUnicode_READ(kind, data, it->it_index); |
| 13704 | item = PyUnicode_FromOrdinal(chr); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13705 | if (item != NULL) |
| 13706 | ++it->it_index; |
| 13707 | return item; |
| 13708 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13709 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13710 | Py_DECREF(seq); |
| 13711 | it->it_seq = NULL; |
| 13712 | return NULL; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13713 | } |
| 13714 | |
| 13715 | static PyObject * |
| 13716 | unicodeiter_len(unicodeiterobject *it) |
| 13717 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13718 | Py_ssize_t len = 0; |
| 13719 | if (it->it_seq) |
| 13720 | len = PyUnicode_GET_SIZE(it->it_seq) - it->it_index; |
| 13721 | return PyLong_FromSsize_t(len); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13722 | } |
| 13723 | |
| 13724 | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
| 13725 | |
| 13726 | static PyMethodDef unicodeiter_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13727 | {"__length_hint__", (PyCFunction)unicodeiter_len, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13728 | length_hint_doc}, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13729 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13730 | }; |
| 13731 | |
| 13732 | PyTypeObject PyUnicodeIter_Type = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13733 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 13734 | "str_iterator", /* tp_name */ |
| 13735 | sizeof(unicodeiterobject), /* tp_basicsize */ |
| 13736 | 0, /* tp_itemsize */ |
| 13737 | /* methods */ |
| 13738 | (destructor)unicodeiter_dealloc, /* tp_dealloc */ |
| 13739 | 0, /* tp_print */ |
| 13740 | 0, /* tp_getattr */ |
| 13741 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 13742 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13743 | 0, /* tp_repr */ |
| 13744 | 0, /* tp_as_number */ |
| 13745 | 0, /* tp_as_sequence */ |
| 13746 | 0, /* tp_as_mapping */ |
| 13747 | 0, /* tp_hash */ |
| 13748 | 0, /* tp_call */ |
| 13749 | 0, /* tp_str */ |
| 13750 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 13751 | 0, /* tp_setattro */ |
| 13752 | 0, /* tp_as_buffer */ |
| 13753 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 13754 | 0, /* tp_doc */ |
| 13755 | (traverseproc)unicodeiter_traverse, /* tp_traverse */ |
| 13756 | 0, /* tp_clear */ |
| 13757 | 0, /* tp_richcompare */ |
| 13758 | 0, /* tp_weaklistoffset */ |
| 13759 | PyObject_SelfIter, /* tp_iter */ |
| 13760 | (iternextfunc)unicodeiter_next, /* tp_iternext */ |
| 13761 | unicodeiter_methods, /* tp_methods */ |
| 13762 | 0, |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13763 | }; |
| 13764 | |
| 13765 | static PyObject * |
| 13766 | unicode_iter(PyObject *seq) |
| 13767 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13768 | unicodeiterobject *it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13769 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13770 | if (!PyUnicode_Check(seq)) { |
| 13771 | PyErr_BadInternalCall(); |
| 13772 | return NULL; |
| 13773 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13774 | if (PyUnicode_READY(seq) == -1) |
| 13775 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13776 | it = PyObject_GC_New(unicodeiterobject, &PyUnicodeIter_Type); |
| 13777 | if (it == NULL) |
| 13778 | return NULL; |
| 13779 | it->it_index = 0; |
| 13780 | Py_INCREF(seq); |
| 13781 | it->it_seq = (PyUnicodeObject *)seq; |
| 13782 | _PyObject_GC_TRACK(it); |
| 13783 | return (PyObject *)it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13784 | } |
| 13785 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13786 | #define UNIOP(x) Py_UNICODE_##x |
| 13787 | #define UNIOP_t Py_UNICODE |
| 13788 | #include "uniops.h" |
| 13789 | #undef UNIOP |
| 13790 | #undef UNIOP_t |
| 13791 | #define UNIOP(x) Py_UCS4_##x |
| 13792 | #define UNIOP_t Py_UCS4 |
| 13793 | #include "uniops.h" |
| 13794 | #undef UNIOP |
| 13795 | #undef UNIOP_t |
Victor Stinner | 331ea92 | 2010-08-10 16:37:20 +0000 | [diff] [blame] | 13796 | |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 13797 | Py_UNICODE* |
Victor Stinner | 4640860 | 2010-09-03 16:18:00 +0000 | [diff] [blame] | 13798 | PyUnicode_AsUnicodeCopy(PyObject *object) |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 13799 | { |
| 13800 | PyUnicodeObject *unicode = (PyUnicodeObject *)object; |
| 13801 | Py_UNICODE *copy; |
| 13802 | Py_ssize_t size; |
| 13803 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13804 | if (!PyUnicode_Check(unicode)) { |
| 13805 | PyErr_BadArgument(); |
| 13806 | return NULL; |
| 13807 | } |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 13808 | /* Ensure we won't overflow the size. */ |
| 13809 | if (PyUnicode_GET_SIZE(unicode) > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
| 13810 | PyErr_NoMemory(); |
| 13811 | return NULL; |
| 13812 | } |
| 13813 | size = PyUnicode_GET_SIZE(unicode) + 1; /* copy the nul character */ |
| 13814 | size *= sizeof(Py_UNICODE); |
| 13815 | copy = PyMem_Malloc(size); |
| 13816 | if (copy == NULL) { |
| 13817 | PyErr_NoMemory(); |
| 13818 | return NULL; |
| 13819 | } |
| 13820 | memcpy(copy, PyUnicode_AS_UNICODE(unicode), size); |
| 13821 | return copy; |
| 13822 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 13823 | |
Georg Brandl | 66c221e | 2010-10-14 07:04:07 +0000 | [diff] [blame] | 13824 | /* A _string module, to export formatter_parser and formatter_field_name_split |
| 13825 | to the string.Formatter class implemented in Python. */ |
| 13826 | |
| 13827 | static PyMethodDef _string_methods[] = { |
| 13828 | {"formatter_field_name_split", (PyCFunction) formatter_field_name_split, |
| 13829 | METH_O, PyDoc_STR("split the argument as a field name")}, |
| 13830 | {"formatter_parser", (PyCFunction) formatter_parser, |
| 13831 | METH_O, PyDoc_STR("parse the argument as a format string")}, |
| 13832 | {NULL, NULL} |
| 13833 | }; |
| 13834 | |
| 13835 | static struct PyModuleDef _string_module = { |
| 13836 | PyModuleDef_HEAD_INIT, |
| 13837 | "_string", |
| 13838 | PyDoc_STR("string helper module"), |
| 13839 | 0, |
| 13840 | _string_methods, |
| 13841 | NULL, |
| 13842 | NULL, |
| 13843 | NULL, |
| 13844 | NULL |
| 13845 | }; |
| 13846 | |
| 13847 | PyMODINIT_FUNC |
| 13848 | PyInit__string(void) |
| 13849 | { |
| 13850 | return PyModule_Create(&_string_module); |
| 13851 | } |
| 13852 | |
| 13853 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 13854 | #ifdef __cplusplus |
| 13855 | } |
| 13856 | #endif |