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) |
| 473 | return (char*)s + PyUnicode_KIND_SIZE(kind, i); |
| 474 | } |
| 475 | else { |
| 476 | for(i = size-1; i >= 0; i--) |
| 477 | if (PyUnicode_READ(kind, s, i) == ch) |
| 478 | return (char*)s + PyUnicode_KIND_SIZE(kind, i); |
| 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)); |
| 492 | char_size = PyUnicode_CHARACTER_SIZE(unicode); |
| 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); |
| 543 | char_size = PyUnicode_CHARACTER_SIZE(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) { |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1008 | Py_MEMCPY((char*)to_data |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1009 | + PyUnicode_KIND_SIZE(to_kind, to_start), |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1010 | (char*)from_data |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1011 | + PyUnicode_KIND_SIZE(from_kind, from_start), |
| 1012 | PyUnicode_KIND_SIZE(to_kind, how_many)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1013 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1014 | else if (from_kind == PyUnicode_1BYTE_KIND |
| 1015 | && to_kind == PyUnicode_2BYTE_KIND) |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1016 | { |
| 1017 | _PyUnicode_CONVERT_BYTES( |
| 1018 | Py_UCS1, Py_UCS2, |
| 1019 | PyUnicode_1BYTE_DATA(from) + from_start, |
| 1020 | PyUnicode_1BYTE_DATA(from) + from_start + how_many, |
| 1021 | PyUnicode_2BYTE_DATA(to) + to_start |
| 1022 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1023 | } |
Victor Stinner | 157f83f | 2011-09-28 21:41:31 +0200 | [diff] [blame] | 1024 | else if (from_kind == PyUnicode_1BYTE_KIND |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1025 | && to_kind == PyUnicode_4BYTE_KIND) |
| 1026 | { |
| 1027 | _PyUnicode_CONVERT_BYTES( |
| 1028 | Py_UCS1, Py_UCS4, |
| 1029 | PyUnicode_1BYTE_DATA(from) + from_start, |
| 1030 | PyUnicode_1BYTE_DATA(from) + from_start + how_many, |
| 1031 | PyUnicode_4BYTE_DATA(to) + to_start |
| 1032 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1033 | } |
| 1034 | else if (from_kind == PyUnicode_2BYTE_KIND |
| 1035 | && to_kind == PyUnicode_4BYTE_KIND) |
| 1036 | { |
| 1037 | _PyUnicode_CONVERT_BYTES( |
| 1038 | Py_UCS2, Py_UCS4, |
| 1039 | PyUnicode_2BYTE_DATA(from) + from_start, |
| 1040 | PyUnicode_2BYTE_DATA(from) + from_start + how_many, |
| 1041 | PyUnicode_4BYTE_DATA(to) + to_start |
| 1042 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1043 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1044 | else { |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 1045 | /* check if max_char(from substring) <= max_char(to) */ |
| 1046 | if (from_kind > to_kind |
| 1047 | /* latin1 => ascii */ |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1048 | || (!PyUnicode_IS_ASCII(from) && PyUnicode_IS_ASCII(to))) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 1049 | { |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1050 | /* slow path to check for character overflow */ |
| 1051 | const Py_UCS4 to_maxchar = PyUnicode_MAX_CHAR_VALUE(to); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1052 | Py_UCS4 ch; |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1053 | Py_ssize_t i; |
| 1054 | |
Victor Stinner | 56c161a | 2011-10-06 02:47:11 +0200 | [diff] [blame] | 1055 | #ifdef Py_DEBUG |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1056 | for (i=0; i < how_many; i++) { |
| 1057 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
Victor Stinner | 56c161a | 2011-10-06 02:47:11 +0200 | [diff] [blame] | 1058 | assert(ch <= to_maxchar); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1059 | PyUnicode_WRITE(to_kind, to_data, to_start + i, ch); |
| 1060 | } |
Victor Stinner | 56c161a | 2011-10-06 02:47:11 +0200 | [diff] [blame] | 1061 | #else |
| 1062 | if (!check_maxchar) { |
| 1063 | for (i=0; i < how_many; i++) { |
| 1064 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
| 1065 | PyUnicode_WRITE(to_kind, to_data, to_start + i, ch); |
| 1066 | } |
| 1067 | } |
| 1068 | else { |
| 1069 | for (i=0; i < how_many; i++) { |
| 1070 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
| 1071 | if (ch > to_maxchar) |
| 1072 | return 1; |
| 1073 | PyUnicode_WRITE(to_kind, to_data, to_start + i, ch); |
| 1074 | } |
| 1075 | } |
| 1076 | #endif |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1077 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1078 | else { |
Victor Stinner | 56c161a | 2011-10-06 02:47:11 +0200 | [diff] [blame] | 1079 | assert(0 && "inconsistent state"); |
| 1080 | return 1; |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1081 | } |
| 1082 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1083 | return 0; |
| 1084 | } |
| 1085 | |
| 1086 | static void |
| 1087 | copy_characters(PyObject *to, Py_ssize_t to_start, |
| 1088 | PyObject *from, Py_ssize_t from_start, |
| 1089 | Py_ssize_t how_many) |
| 1090 | { |
| 1091 | (void)_copy_characters(to, to_start, from, from_start, how_many, 0); |
| 1092 | } |
| 1093 | |
| 1094 | Py_ssize_t |
| 1095 | PyUnicode_CopyCharacters(PyObject *to, Py_ssize_t to_start, |
| 1096 | PyObject *from, Py_ssize_t from_start, |
| 1097 | Py_ssize_t how_many) |
| 1098 | { |
| 1099 | int err; |
| 1100 | |
| 1101 | if (!PyUnicode_Check(from) || !PyUnicode_Check(to)) { |
| 1102 | PyErr_BadInternalCall(); |
| 1103 | return -1; |
| 1104 | } |
| 1105 | |
| 1106 | if (PyUnicode_READY(from)) |
| 1107 | return -1; |
| 1108 | if (PyUnicode_READY(to)) |
| 1109 | return -1; |
| 1110 | |
| 1111 | how_many = Py_MIN(PyUnicode_GET_LENGTH(from), how_many); |
| 1112 | if (to_start + how_many > PyUnicode_GET_LENGTH(to)) { |
| 1113 | PyErr_Format(PyExc_SystemError, |
| 1114 | "Cannot write %zi characters at %zi " |
| 1115 | "in a string of %zi characters", |
| 1116 | how_many, to_start, PyUnicode_GET_LENGTH(to)); |
| 1117 | return -1; |
| 1118 | } |
| 1119 | |
| 1120 | if (how_many == 0) |
| 1121 | return 0; |
| 1122 | |
| 1123 | if (_PyUnicode_Dirty(to)) |
| 1124 | return -1; |
| 1125 | |
| 1126 | err = _copy_characters(to, to_start, from, from_start, how_many, 1); |
| 1127 | if (err) { |
| 1128 | PyErr_Format(PyExc_SystemError, |
| 1129 | "Cannot copy %s characters " |
| 1130 | "into a string of %s characters", |
| 1131 | unicode_kind_name(from), |
| 1132 | unicode_kind_name(to)); |
| 1133 | return -1; |
| 1134 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1135 | return how_many; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1136 | } |
| 1137 | |
Victor Stinner | 1722216 | 2011-09-28 22:15:37 +0200 | [diff] [blame] | 1138 | /* Find the maximum code point and count the number of surrogate pairs so a |
| 1139 | correct string length can be computed before converting a string to UCS4. |
| 1140 | This function counts single surrogates as a character and not as a pair. |
| 1141 | |
| 1142 | Return 0 on success, or -1 on error. */ |
| 1143 | static int |
| 1144 | find_maxchar_surrogates(const wchar_t *begin, const wchar_t *end, |
| 1145 | Py_UCS4 *maxchar, Py_ssize_t *num_surrogates) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1146 | { |
| 1147 | const wchar_t *iter; |
| 1148 | |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1149 | assert(num_surrogates != NULL && maxchar != NULL); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1150 | *num_surrogates = 0; |
| 1151 | *maxchar = 0; |
| 1152 | |
| 1153 | for (iter = begin; iter < end; ) { |
Victor Stinner | ae86485 | 2011-10-05 14:02:44 +0200 | [diff] [blame] | 1154 | if (*iter > *maxchar) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1155 | *maxchar = *iter; |
Victor Stinner | ae86485 | 2011-10-05 14:02:44 +0200 | [diff] [blame] | 1156 | #if SIZEOF_WCHAR_T != 2 |
| 1157 | if (*maxchar >= 0x10000) |
| 1158 | return 0; |
| 1159 | #endif |
| 1160 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1161 | #if SIZEOF_WCHAR_T == 2 |
| 1162 | if (*iter >= 0xD800 && *iter <= 0xDBFF |
| 1163 | && (iter+1) < end && iter[1] >= 0xDC00 && iter[1] <= 0xDFFF) |
| 1164 | { |
| 1165 | Py_UCS4 surrogate_val; |
| 1166 | surrogate_val = (((iter[0] & 0x3FF)<<10) |
| 1167 | | (iter[1] & 0x3FF)) + 0x10000; |
| 1168 | ++(*num_surrogates); |
| 1169 | if (surrogate_val > *maxchar) |
| 1170 | *maxchar = surrogate_val; |
| 1171 | iter += 2; |
| 1172 | } |
| 1173 | else |
| 1174 | iter++; |
| 1175 | #else |
| 1176 | iter++; |
| 1177 | #endif |
| 1178 | } |
| 1179 | return 0; |
| 1180 | } |
| 1181 | |
| 1182 | #ifdef Py_DEBUG |
| 1183 | int unicode_ready_calls = 0; |
| 1184 | #endif |
| 1185 | |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 1186 | static int |
| 1187 | unicode_ready(PyObject **p_obj, int replace) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1188 | { |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 1189 | PyUnicodeObject *unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1190 | wchar_t *end; |
| 1191 | Py_UCS4 maxchar = 0; |
| 1192 | Py_ssize_t num_surrogates; |
| 1193 | #if SIZEOF_WCHAR_T == 2 |
| 1194 | Py_ssize_t length_wo_surrogates; |
| 1195 | #endif |
| 1196 | |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 1197 | assert(p_obj != NULL); |
| 1198 | unicode = (PyUnicodeObject *)*p_obj; |
| 1199 | |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 1200 | /* _PyUnicode_Ready() is only intended for old-style API usage where |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1201 | strings were created using _PyObject_New() and where no canonical |
| 1202 | representation (the str field) has been set yet aka strings |
| 1203 | which are not yet ready. */ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1204 | assert(_PyUnicode_CHECK(unicode)); |
| 1205 | assert(_PyUnicode_KIND(unicode) == PyUnicode_WCHAR_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1206 | assert(_PyUnicode_WSTR(unicode) != NULL); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1207 | assert(_PyUnicode_DATA_ANY(unicode) == NULL); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1208 | assert(_PyUnicode_UTF8(unicode) == NULL); |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1209 | /* Actually, it should neither be interned nor be anything else: */ |
| 1210 | assert(_PyUnicode_STATE(unicode).interned == SSTATE_NOT_INTERNED); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1211 | |
| 1212 | #ifdef Py_DEBUG |
| 1213 | ++unicode_ready_calls; |
| 1214 | #endif |
| 1215 | |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 1216 | #ifdef Py_DEBUG |
| 1217 | assert(!replace || Py_REFCNT(unicode) == 1); |
| 1218 | #else |
| 1219 | if (replace && Py_REFCNT(unicode) != 1) |
| 1220 | replace = 0; |
| 1221 | #endif |
| 1222 | if (replace) { |
| 1223 | Py_ssize_t len = _PyUnicode_WSTR_LENGTH(unicode); |
| 1224 | wchar_t *wstr = _PyUnicode_WSTR(unicode); |
| 1225 | /* Optimization for empty strings */ |
| 1226 | if (len == 0) { |
| 1227 | Py_INCREF(unicode_empty); |
| 1228 | Py_DECREF(*p_obj); |
| 1229 | *p_obj = unicode_empty; |
| 1230 | return 0; |
| 1231 | } |
| 1232 | if (len == 1 && wstr[0] < 256) { |
| 1233 | PyObject *latin1_char = get_latin1_char((unsigned char)wstr[0]); |
| 1234 | if (latin1_char == NULL) |
| 1235 | return -1; |
| 1236 | Py_DECREF(*p_obj); |
| 1237 | *p_obj = latin1_char; |
| 1238 | return 0; |
| 1239 | } |
| 1240 | } |
| 1241 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1242 | end = _PyUnicode_WSTR(unicode) + _PyUnicode_WSTR_LENGTH(unicode); |
Victor Stinner | 1722216 | 2011-09-28 22:15:37 +0200 | [diff] [blame] | 1243 | if (find_maxchar_surrogates(_PyUnicode_WSTR(unicode), end, |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1244 | &maxchar, &num_surrogates) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1245 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1246 | |
| 1247 | if (maxchar < 256) { |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1248 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC(_PyUnicode_WSTR_LENGTH(unicode) + 1); |
| 1249 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1250 | PyErr_NoMemory(); |
| 1251 | return -1; |
| 1252 | } |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1253 | _PyUnicode_CONVERT_BYTES(wchar_t, unsigned char, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1254 | _PyUnicode_WSTR(unicode), end, |
| 1255 | PyUnicode_1BYTE_DATA(unicode)); |
| 1256 | PyUnicode_1BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1257 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1258 | _PyUnicode_STATE(unicode).kind = PyUnicode_1BYTE_KIND; |
| 1259 | if (maxchar < 128) { |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1260 | _PyUnicode_STATE(unicode).ascii = 1; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1261 | _PyUnicode_UTF8(unicode) = _PyUnicode_DATA_ANY(unicode); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1262 | _PyUnicode_UTF8_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1263 | } |
| 1264 | else { |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1265 | _PyUnicode_STATE(unicode).ascii = 0; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1266 | _PyUnicode_UTF8(unicode) = NULL; |
| 1267 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1268 | } |
| 1269 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1270 | _PyUnicode_WSTR(unicode) = NULL; |
| 1271 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1272 | } |
| 1273 | /* In this case we might have to convert down from 4-byte native |
| 1274 | wchar_t to 2-byte unicode. */ |
| 1275 | else if (maxchar < 65536) { |
| 1276 | assert(num_surrogates == 0 && |
| 1277 | "FindMaxCharAndNumSurrogatePairs() messed up"); |
| 1278 | |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1279 | #if SIZEOF_WCHAR_T == 2 |
| 1280 | /* We can share representations and are done. */ |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1281 | _PyUnicode_DATA_ANY(unicode) = _PyUnicode_WSTR(unicode); |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1282 | PyUnicode_2BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1283 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1284 | _PyUnicode_STATE(unicode).kind = PyUnicode_2BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1285 | _PyUnicode_UTF8(unicode) = NULL; |
| 1286 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1287 | #else |
| 1288 | /* sizeof(wchar_t) == 4 */ |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1289 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC( |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1290 | 2 * (_PyUnicode_WSTR_LENGTH(unicode) + 1)); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1291 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1292 | PyErr_NoMemory(); |
| 1293 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1294 | } |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1295 | _PyUnicode_CONVERT_BYTES(wchar_t, Py_UCS2, |
| 1296 | _PyUnicode_WSTR(unicode), end, |
| 1297 | PyUnicode_2BYTE_DATA(unicode)); |
| 1298 | PyUnicode_2BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1299 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1300 | _PyUnicode_STATE(unicode).kind = PyUnicode_2BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1301 | _PyUnicode_UTF8(unicode) = NULL; |
| 1302 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1303 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1304 | _PyUnicode_WSTR(unicode) = NULL; |
| 1305 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1306 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1307 | } |
| 1308 | /* maxchar exeeds 16 bit, wee need 4 bytes for unicode characters */ |
| 1309 | else { |
| 1310 | #if SIZEOF_WCHAR_T == 2 |
| 1311 | /* in case the native representation is 2-bytes, we need to allocate a |
| 1312 | new normalized 4-byte version. */ |
| 1313 | length_wo_surrogates = _PyUnicode_WSTR_LENGTH(unicode) - num_surrogates; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1314 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC(4 * (length_wo_surrogates + 1)); |
| 1315 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1316 | PyErr_NoMemory(); |
| 1317 | return -1; |
| 1318 | } |
| 1319 | _PyUnicode_LENGTH(unicode) = length_wo_surrogates; |
| 1320 | _PyUnicode_STATE(unicode).kind = PyUnicode_4BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1321 | _PyUnicode_UTF8(unicode) = NULL; |
| 1322 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 126c559 | 2011-10-03 04:17:10 +0200 | [diff] [blame] | 1323 | /* unicode_convert_wchar_to_ucs4() requires a ready string */ |
| 1324 | _PyUnicode_STATE(unicode).ready = 1; |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1325 | unicode_convert_wchar_to_ucs4(_PyUnicode_WSTR(unicode), end, unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1326 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1327 | _PyUnicode_WSTR(unicode) = NULL; |
| 1328 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1329 | #else |
| 1330 | assert(num_surrogates == 0); |
| 1331 | |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1332 | _PyUnicode_DATA_ANY(unicode) = _PyUnicode_WSTR(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1333 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1334 | _PyUnicode_UTF8(unicode) = NULL; |
| 1335 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1336 | _PyUnicode_STATE(unicode).kind = PyUnicode_4BYTE_KIND; |
| 1337 | #endif |
| 1338 | PyUnicode_4BYTE_DATA(unicode)[_PyUnicode_LENGTH(unicode)] = '\0'; |
| 1339 | } |
| 1340 | _PyUnicode_STATE(unicode).ready = 1; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1341 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1342 | return 0; |
| 1343 | } |
| 1344 | |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 1345 | int |
| 1346 | _PyUnicode_ReadyReplace(PyObject **op) |
| 1347 | { |
| 1348 | return unicode_ready(op, 1); |
| 1349 | } |
| 1350 | |
| 1351 | int |
| 1352 | _PyUnicode_Ready(PyObject *op) |
| 1353 | { |
| 1354 | return unicode_ready(&op, 0); |
| 1355 | } |
| 1356 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1357 | static void |
| 1358 | unicode_dealloc(register PyUnicodeObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1359 | { |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1360 | switch (PyUnicode_CHECK_INTERNED(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1361 | case SSTATE_NOT_INTERNED: |
| 1362 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1363 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1364 | case SSTATE_INTERNED_MORTAL: |
| 1365 | /* revive dead object temporarily for DelItem */ |
| 1366 | Py_REFCNT(unicode) = 3; |
| 1367 | if (PyDict_DelItem(interned, (PyObject *)unicode) != 0) |
| 1368 | Py_FatalError( |
| 1369 | "deletion of interned string failed"); |
| 1370 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1371 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1372 | case SSTATE_INTERNED_IMMORTAL: |
| 1373 | Py_FatalError("Immortal interned string died."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1374 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1375 | default: |
| 1376 | Py_FatalError("Inconsistent interned string state."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1377 | } |
| 1378 | |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 1379 | if (_PyUnicode_HAS_WSTR_MEMORY(unicode)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1380 | PyObject_DEL(_PyUnicode_WSTR(unicode)); |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 1381 | if (_PyUnicode_HAS_UTF8_MEMORY(unicode)) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1382 | PyObject_DEL(_PyUnicode_UTF8(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1383 | |
| 1384 | if (PyUnicode_IS_COMPACT(unicode)) { |
| 1385 | Py_TYPE(unicode)->tp_free((PyObject *)unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1386 | } |
| 1387 | else { |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1388 | if (_PyUnicode_DATA_ANY(unicode)) |
| 1389 | PyObject_DEL(_PyUnicode_DATA_ANY(unicode)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1390 | Py_TYPE(unicode)->tp_free((PyObject *)unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1391 | } |
| 1392 | } |
| 1393 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1394 | #ifdef Py_DEBUG |
| 1395 | static int |
| 1396 | unicode_is_singleton(PyObject *unicode) |
| 1397 | { |
| 1398 | PyASCIIObject *ascii = (PyASCIIObject *)unicode; |
| 1399 | if (unicode == unicode_empty) |
| 1400 | return 1; |
| 1401 | if (ascii->state.kind != PyUnicode_WCHAR_KIND && ascii->length == 1) |
| 1402 | { |
| 1403 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, 0); |
| 1404 | if (ch < 256 && unicode_latin1[ch] == unicode) |
| 1405 | return 1; |
| 1406 | } |
| 1407 | return 0; |
| 1408 | } |
| 1409 | #endif |
| 1410 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1411 | static int |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1412 | unicode_resizable(PyObject *unicode) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1413 | { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1414 | if (Py_REFCNT(unicode) != 1) |
| 1415 | return 0; |
| 1416 | if (PyUnicode_CHECK_INTERNED(unicode)) |
| 1417 | return 0; |
Victor Stinner | 77bb47b | 2011-10-03 20:06:05 +0200 | [diff] [blame] | 1418 | #ifdef Py_DEBUG |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1419 | /* singleton refcount is greater than 1 */ |
| 1420 | assert(!unicode_is_singleton(unicode)); |
Victor Stinner | 77bb47b | 2011-10-03 20:06:05 +0200 | [diff] [blame] | 1421 | #endif |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1422 | return 1; |
| 1423 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1424 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1425 | static int |
| 1426 | unicode_resize(PyObject **p_unicode, Py_ssize_t length) |
| 1427 | { |
| 1428 | PyObject *unicode; |
| 1429 | Py_ssize_t old_length; |
| 1430 | |
| 1431 | assert(p_unicode != NULL); |
| 1432 | unicode = *p_unicode; |
| 1433 | |
| 1434 | assert(unicode != NULL); |
| 1435 | assert(PyUnicode_Check(unicode)); |
| 1436 | assert(0 <= length); |
| 1437 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1438 | if (_PyUnicode_KIND(unicode) == PyUnicode_WCHAR_KIND) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1439 | old_length = PyUnicode_WSTR_LENGTH(unicode); |
| 1440 | else |
| 1441 | old_length = PyUnicode_GET_LENGTH(unicode); |
| 1442 | if (old_length == length) |
| 1443 | return 0; |
| 1444 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1445 | if (!unicode_resizable(unicode)) { |
| 1446 | PyObject *copy = resize_copy(unicode, length); |
| 1447 | if (copy == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1448 | return -1; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1449 | Py_DECREF(*p_unicode); |
| 1450 | *p_unicode = copy; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1451 | return 0; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1452 | } |
| 1453 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1454 | if (PyUnicode_IS_COMPACT(unicode)) { |
| 1455 | *p_unicode = resize_compact(unicode, length); |
| 1456 | if (*p_unicode == NULL) |
| 1457 | return -1; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1458 | assert(_PyUnicode_CheckConsistency(*p_unicode, 0)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1459 | return 0; |
Benjamin Peterson | 4bfce8f | 2011-10-03 19:35:07 -0400 | [diff] [blame] | 1460 | } |
| 1461 | return resize_inplace((PyUnicodeObject*)unicode, length); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1462 | } |
| 1463 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1464 | int |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1465 | PyUnicode_Resize(PyObject **p_unicode, Py_ssize_t length) |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 1466 | { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1467 | PyObject *unicode; |
| 1468 | if (p_unicode == NULL) { |
| 1469 | PyErr_BadInternalCall(); |
| 1470 | return -1; |
| 1471 | } |
| 1472 | unicode = *p_unicode; |
| 1473 | if (unicode == NULL || !PyUnicode_Check(unicode) || length < 0 |
| 1474 | || _PyUnicode_KIND(unicode) != PyUnicode_WCHAR_KIND) |
| 1475 | { |
| 1476 | PyErr_BadInternalCall(); |
| 1477 | return -1; |
| 1478 | } |
| 1479 | return unicode_resize(p_unicode, length); |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 1480 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1481 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1482 | static PyObject* |
| 1483 | get_latin1_char(unsigned char ch) |
| 1484 | { |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1485 | PyObject *unicode = unicode_latin1[ch]; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1486 | if (!unicode) { |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1487 | unicode = PyUnicode_New(1, ch); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1488 | if (!unicode) |
| 1489 | return NULL; |
| 1490 | PyUnicode_1BYTE_DATA(unicode)[0] = ch; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1491 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1492 | unicode_latin1[ch] = unicode; |
| 1493 | } |
| 1494 | Py_INCREF(unicode); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1495 | return unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1496 | } |
| 1497 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1498 | PyObject * |
| 1499 | PyUnicode_FromUnicode(const Py_UNICODE *u, Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1500 | { |
| 1501 | PyUnicodeObject *unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1502 | Py_UCS4 maxchar = 0; |
| 1503 | Py_ssize_t num_surrogates; |
| 1504 | |
| 1505 | if (u == NULL) |
| 1506 | return (PyObject*)_PyUnicode_New(size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1507 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1508 | /* If the Unicode data is known at construction time, we can apply |
| 1509 | some optimizations which share commonly used objects. */ |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1510 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1511 | /* Optimization for empty strings */ |
| 1512 | if (size == 0 && unicode_empty != NULL) { |
| 1513 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1514 | return unicode_empty; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1515 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1516 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1517 | /* Single character Unicode objects in the Latin-1 range are |
| 1518 | shared when using this constructor */ |
| 1519 | if (size == 1 && *u < 256) |
| 1520 | return get_latin1_char((unsigned char)*u); |
| 1521 | |
| 1522 | /* If not empty and not single character, copy the Unicode data |
| 1523 | into the new object */ |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1524 | if (find_maxchar_surrogates(u, u + size, |
| 1525 | &maxchar, &num_surrogates) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1526 | return NULL; |
| 1527 | |
| 1528 | unicode = (PyUnicodeObject *) PyUnicode_New(size - num_surrogates, |
| 1529 | maxchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1530 | if (!unicode) |
| 1531 | return NULL; |
| 1532 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1533 | switch (PyUnicode_KIND(unicode)) { |
| 1534 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1535 | _PyUnicode_CONVERT_BYTES(Py_UNICODE, unsigned char, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1536 | u, u + size, PyUnicode_1BYTE_DATA(unicode)); |
| 1537 | break; |
| 1538 | case PyUnicode_2BYTE_KIND: |
| 1539 | #if Py_UNICODE_SIZE == 2 |
| 1540 | Py_MEMCPY(PyUnicode_2BYTE_DATA(unicode), u, size * 2); |
| 1541 | #else |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1542 | _PyUnicode_CONVERT_BYTES(Py_UNICODE, Py_UCS2, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1543 | u, u + size, PyUnicode_2BYTE_DATA(unicode)); |
| 1544 | #endif |
| 1545 | break; |
| 1546 | case PyUnicode_4BYTE_KIND: |
| 1547 | #if SIZEOF_WCHAR_T == 2 |
| 1548 | /* This is the only case which has to process surrogates, thus |
| 1549 | a simple copy loop is not enough and we need a function. */ |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1550 | unicode_convert_wchar_to_ucs4(u, u + size, unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1551 | #else |
| 1552 | assert(num_surrogates == 0); |
| 1553 | Py_MEMCPY(PyUnicode_4BYTE_DATA(unicode), u, size * 4); |
| 1554 | #endif |
| 1555 | break; |
| 1556 | default: |
| 1557 | assert(0 && "Impossible state"); |
| 1558 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1559 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1560 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1561 | return (PyObject *)unicode; |
| 1562 | } |
| 1563 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1564 | PyObject * |
| 1565 | PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size) |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1566 | { |
| 1567 | PyUnicodeObject *unicode; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1568 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1569 | if (size < 0) { |
| 1570 | PyErr_SetString(PyExc_SystemError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1571 | "Negative size passed to PyUnicode_FromStringAndSize"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1572 | return NULL; |
| 1573 | } |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1574 | |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1575 | /* 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] | 1576 | some optimizations which share commonly used objects. |
| 1577 | Also, this means the input must be UTF-8, so fall back to the |
| 1578 | UTF-8 decoder at the end. */ |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1579 | if (u != NULL) { |
| 1580 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1581 | /* Optimization for empty strings */ |
| 1582 | if (size == 0 && unicode_empty != NULL) { |
| 1583 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1584 | return unicode_empty; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1585 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1586 | |
| 1587 | /* Single characters are shared when using this constructor. |
| 1588 | Restrict to ASCII, since the input must be UTF-8. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1589 | if (size == 1 && Py_CHARMASK(*u) < 128) |
| 1590 | return get_latin1_char(Py_CHARMASK(*u)); |
Martin v. Löwis | 9c12106 | 2007-08-05 20:26:11 +0000 | [diff] [blame] | 1591 | |
| 1592 | return PyUnicode_DecodeUTF8(u, size, NULL); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1593 | } |
| 1594 | |
Walter Dörwald | 5550731 | 2007-05-18 13:12:10 +0000 | [diff] [blame] | 1595 | unicode = _PyUnicode_New(size); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1596 | if (!unicode) |
| 1597 | return NULL; |
| 1598 | |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1599 | return (PyObject *)unicode; |
| 1600 | } |
| 1601 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1602 | PyObject * |
| 1603 | PyUnicode_FromString(const char *u) |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1604 | { |
| 1605 | size_t size = strlen(u); |
| 1606 | if (size > PY_SSIZE_T_MAX) { |
| 1607 | PyErr_SetString(PyExc_OverflowError, "input too long"); |
| 1608 | return NULL; |
| 1609 | } |
| 1610 | |
| 1611 | return PyUnicode_FromStringAndSize(u, size); |
| 1612 | } |
| 1613 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1614 | static PyObject* |
Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 1615 | unicode_fromascii(const unsigned char* s, Py_ssize_t size) |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1616 | { |
Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 1617 | PyObject *res; |
| 1618 | #ifdef Py_DEBUG |
| 1619 | const unsigned char *p; |
| 1620 | const unsigned char *end = s + size; |
| 1621 | for (p=s; p < end; p++) { |
| 1622 | assert(*p < 128); |
| 1623 | } |
| 1624 | #endif |
| 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); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1656 | for (i = 0; i < size; i++) { |
| 1657 | if (u[i] & 0x80) { |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1658 | max_char = 255; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1659 | break; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1660 | } |
| 1661 | } |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1662 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1663 | if (!res) |
| 1664 | return NULL; |
| 1665 | memcpy(PyUnicode_1BYTE_DATA(res), u, size); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1666 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1667 | return res; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1668 | } |
| 1669 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1670 | static PyObject* |
| 1671 | _PyUnicode_FromUCS2(const Py_UCS2 *u, Py_ssize_t size) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1672 | { |
| 1673 | PyObject *res; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1674 | Py_UCS2 max_char = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1675 | Py_ssize_t i; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1676 | |
| 1677 | assert(size >= 0); |
| 1678 | for (i = 0; i < size; i++) { |
| 1679 | if (u[i] > max_char) { |
| 1680 | max_char = u[i]; |
| 1681 | if (max_char >= 256) |
| 1682 | break; |
| 1683 | } |
| 1684 | } |
| 1685 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1686 | if (!res) |
| 1687 | return NULL; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1688 | if (max_char >= 256) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1689 | memcpy(PyUnicode_2BYTE_DATA(res), u, sizeof(Py_UCS2)*size); |
| 1690 | else |
| 1691 | for (i = 0; i < size; i++) |
| 1692 | PyUnicode_1BYTE_DATA(res)[i] = (Py_UCS1)u[i]; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1693 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1694 | return res; |
| 1695 | } |
| 1696 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1697 | static PyObject* |
| 1698 | _PyUnicode_FromUCS4(const Py_UCS4 *u, Py_ssize_t size) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1699 | { |
| 1700 | PyObject *res; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1701 | Py_UCS4 max_char = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1702 | Py_ssize_t i; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1703 | |
| 1704 | assert(size >= 0); |
| 1705 | for (i = 0; i < size; i++) { |
| 1706 | if (u[i] > max_char) { |
| 1707 | max_char = u[i]; |
| 1708 | if (max_char >= 0x10000) |
| 1709 | break; |
| 1710 | } |
| 1711 | } |
| 1712 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1713 | if (!res) |
| 1714 | return NULL; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1715 | if (max_char >= 0x10000) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1716 | memcpy(PyUnicode_4BYTE_DATA(res), u, sizeof(Py_UCS4)*size); |
| 1717 | else { |
| 1718 | int kind = PyUnicode_KIND(res); |
| 1719 | void *data = PyUnicode_DATA(res); |
| 1720 | for (i = 0; i < size; i++) |
| 1721 | PyUnicode_WRITE(kind, data, i, u[i]); |
| 1722 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1723 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1724 | return res; |
| 1725 | } |
| 1726 | |
| 1727 | PyObject* |
| 1728 | PyUnicode_FromKindAndData(int kind, const void *buffer, Py_ssize_t size) |
| 1729 | { |
| 1730 | switch(kind) { |
| 1731 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1732 | return _PyUnicode_FromUCS1(buffer, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1733 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1734 | return _PyUnicode_FromUCS2(buffer, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1735 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1736 | return _PyUnicode_FromUCS4(buffer, size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1737 | default: |
| 1738 | assert(0 && "invalid kind"); |
| 1739 | PyErr_SetString(PyExc_SystemError, "invalid kind"); |
| 1740 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1741 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1742 | } |
| 1743 | |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1744 | /* Ensure that a string uses the most efficient storage, if it is not the |
| 1745 | case: create a new string with of the right kind. Write NULL into *p_unicode |
| 1746 | on error. */ |
| 1747 | void |
| 1748 | unicode_adjust_maxchar(PyObject **p_unicode) |
| 1749 | { |
| 1750 | PyObject *unicode, *copy; |
| 1751 | Py_UCS4 max_char; |
| 1752 | Py_ssize_t i, len; |
| 1753 | unsigned int kind; |
| 1754 | |
| 1755 | assert(p_unicode != NULL); |
| 1756 | unicode = *p_unicode; |
| 1757 | assert(PyUnicode_IS_READY(unicode)); |
| 1758 | if (PyUnicode_IS_ASCII(unicode)) |
| 1759 | return; |
| 1760 | |
| 1761 | len = PyUnicode_GET_LENGTH(unicode); |
| 1762 | kind = PyUnicode_KIND(unicode); |
| 1763 | if (kind == PyUnicode_1BYTE_KIND) { |
| 1764 | const Py_UCS1 *u = PyUnicode_1BYTE_DATA(unicode); |
| 1765 | for (i = 0; i < len; i++) { |
| 1766 | if (u[i] & 0x80) |
| 1767 | return; |
| 1768 | } |
| 1769 | max_char = 127; |
| 1770 | } |
| 1771 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 1772 | const Py_UCS2 *u = PyUnicode_2BYTE_DATA(unicode); |
| 1773 | max_char = 0; |
| 1774 | for (i = 0; i < len; i++) { |
| 1775 | if (u[i] > max_char) { |
| 1776 | max_char = u[i]; |
| 1777 | if (max_char >= 256) |
| 1778 | return; |
| 1779 | } |
| 1780 | } |
| 1781 | } |
| 1782 | else { |
Antoine Pitrou | 15a66cf | 2011-10-06 15:25:32 +0200 | [diff] [blame] | 1783 | const Py_UCS4 *u; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1784 | assert(kind == PyUnicode_4BYTE_KIND); |
Antoine Pitrou | 15a66cf | 2011-10-06 15:25:32 +0200 | [diff] [blame] | 1785 | u = PyUnicode_4BYTE_DATA(unicode); |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1786 | max_char = 0; |
| 1787 | for (i = 0; i < len; i++) { |
| 1788 | if (u[i] > max_char) { |
| 1789 | max_char = u[i]; |
| 1790 | if (max_char >= 0x10000) |
| 1791 | return; |
| 1792 | } |
| 1793 | } |
| 1794 | } |
Victor Stinner | 200f213 | 2011-10-06 13:27:56 +0200 | [diff] [blame] | 1795 | assert(max_char < PyUnicode_MAX_CHAR_VALUE(unicode)); |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1796 | copy = PyUnicode_New(len, max_char); |
| 1797 | copy_characters(copy, 0, unicode, 0, len); |
| 1798 | Py_DECREF(unicode); |
| 1799 | *p_unicode = copy; |
| 1800 | } |
| 1801 | |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 1802 | PyObject* |
| 1803 | PyUnicode_Copy(PyObject *unicode) |
| 1804 | { |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 1805 | Py_ssize_t size; |
| 1806 | PyObject *copy; |
| 1807 | void *data; |
| 1808 | |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 1809 | if (!PyUnicode_Check(unicode)) { |
| 1810 | PyErr_BadInternalCall(); |
| 1811 | return NULL; |
| 1812 | } |
| 1813 | if (PyUnicode_READY(unicode)) |
| 1814 | return NULL; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 1815 | |
| 1816 | size = PyUnicode_GET_LENGTH(unicode); |
| 1817 | copy = PyUnicode_New(size, PyUnicode_MAX_CHAR_VALUE(unicode)); |
| 1818 | if (!copy) |
| 1819 | return NULL; |
| 1820 | assert(PyUnicode_KIND(copy) == PyUnicode_KIND(unicode)); |
| 1821 | |
| 1822 | data = PyUnicode_DATA(unicode); |
| 1823 | switch (PyUnicode_KIND(unicode)) |
| 1824 | { |
| 1825 | case PyUnicode_1BYTE_KIND: |
| 1826 | memcpy(PyUnicode_1BYTE_DATA(copy), data, size); |
| 1827 | break; |
| 1828 | case PyUnicode_2BYTE_KIND: |
| 1829 | memcpy(PyUnicode_2BYTE_DATA(copy), data, sizeof(Py_UCS2) * size); |
| 1830 | break; |
| 1831 | case PyUnicode_4BYTE_KIND: |
| 1832 | memcpy(PyUnicode_4BYTE_DATA(copy), data, sizeof(Py_UCS4) * size); |
| 1833 | break; |
| 1834 | default: |
| 1835 | assert(0); |
| 1836 | break; |
| 1837 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1838 | assert(_PyUnicode_CheckConsistency(copy, 1)); |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 1839 | return copy; |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 1840 | } |
| 1841 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1842 | |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1843 | /* Widen Unicode objects to larger buffers. Don't write terminating null |
| 1844 | character. Return NULL on error. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1845 | |
| 1846 | void* |
| 1847 | _PyUnicode_AsKind(PyObject *s, unsigned int kind) |
| 1848 | { |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1849 | Py_ssize_t len; |
| 1850 | void *result; |
| 1851 | unsigned int skind; |
| 1852 | |
| 1853 | if (PyUnicode_READY(s)) |
| 1854 | return NULL; |
| 1855 | |
| 1856 | len = PyUnicode_GET_LENGTH(s); |
| 1857 | skind = PyUnicode_KIND(s); |
| 1858 | if (skind >= kind) { |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 1859 | PyErr_SetString(PyExc_SystemError, "invalid widening attempt"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1860 | return NULL; |
| 1861 | } |
| 1862 | switch(kind) { |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1863 | case PyUnicode_2BYTE_KIND: |
| 1864 | result = PyMem_Malloc(len * sizeof(Py_UCS2)); |
| 1865 | if (!result) |
| 1866 | return PyErr_NoMemory(); |
| 1867 | assert(skind == PyUnicode_1BYTE_KIND); |
| 1868 | _PyUnicode_CONVERT_BYTES( |
| 1869 | Py_UCS1, Py_UCS2, |
| 1870 | PyUnicode_1BYTE_DATA(s), |
| 1871 | PyUnicode_1BYTE_DATA(s) + len, |
| 1872 | result); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1873 | return result; |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1874 | case PyUnicode_4BYTE_KIND: |
| 1875 | result = PyMem_Malloc(len * sizeof(Py_UCS4)); |
| 1876 | if (!result) |
| 1877 | return PyErr_NoMemory(); |
| 1878 | if (skind == PyUnicode_2BYTE_KIND) { |
| 1879 | _PyUnicode_CONVERT_BYTES( |
| 1880 | Py_UCS2, Py_UCS4, |
| 1881 | PyUnicode_2BYTE_DATA(s), |
| 1882 | PyUnicode_2BYTE_DATA(s) + len, |
| 1883 | result); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1884 | } |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1885 | else { |
| 1886 | assert(skind == PyUnicode_1BYTE_KIND); |
| 1887 | _PyUnicode_CONVERT_BYTES( |
| 1888 | Py_UCS1, Py_UCS4, |
| 1889 | PyUnicode_1BYTE_DATA(s), |
| 1890 | PyUnicode_1BYTE_DATA(s) + len, |
| 1891 | result); |
| 1892 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1893 | return result; |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1894 | default: |
| 1895 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1896 | } |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 1897 | PyErr_SetString(PyExc_SystemError, "invalid kind"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1898 | return NULL; |
| 1899 | } |
| 1900 | |
| 1901 | static Py_UCS4* |
| 1902 | as_ucs4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, |
| 1903 | int copy_null) |
| 1904 | { |
| 1905 | int kind; |
| 1906 | void *data; |
| 1907 | Py_ssize_t len, targetlen; |
| 1908 | if (PyUnicode_READY(string) == -1) |
| 1909 | return NULL; |
| 1910 | kind = PyUnicode_KIND(string); |
| 1911 | data = PyUnicode_DATA(string); |
| 1912 | len = PyUnicode_GET_LENGTH(string); |
| 1913 | targetlen = len; |
| 1914 | if (copy_null) |
| 1915 | targetlen++; |
| 1916 | if (!target) { |
| 1917 | if (PY_SSIZE_T_MAX / sizeof(Py_UCS4) < targetlen) { |
| 1918 | PyErr_NoMemory(); |
| 1919 | return NULL; |
| 1920 | } |
| 1921 | target = PyMem_Malloc(targetlen * sizeof(Py_UCS4)); |
| 1922 | if (!target) { |
| 1923 | PyErr_NoMemory(); |
| 1924 | return NULL; |
| 1925 | } |
| 1926 | } |
| 1927 | else { |
| 1928 | if (targetsize < targetlen) { |
| 1929 | PyErr_Format(PyExc_SystemError, |
| 1930 | "string is longer than the buffer"); |
| 1931 | if (copy_null && 0 < targetsize) |
| 1932 | target[0] = 0; |
| 1933 | return NULL; |
| 1934 | } |
| 1935 | } |
| 1936 | if (kind != PyUnicode_4BYTE_KIND) { |
| 1937 | Py_ssize_t i; |
| 1938 | for (i = 0; i < len; i++) |
| 1939 | target[i] = PyUnicode_READ(kind, data, i); |
| 1940 | } |
| 1941 | else |
| 1942 | Py_MEMCPY(target, data, len * sizeof(Py_UCS4)); |
| 1943 | if (copy_null) |
| 1944 | target[len] = 0; |
| 1945 | return target; |
| 1946 | } |
| 1947 | |
| 1948 | Py_UCS4* |
| 1949 | PyUnicode_AsUCS4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, |
| 1950 | int copy_null) |
| 1951 | { |
| 1952 | if (target == NULL || targetsize < 1) { |
| 1953 | PyErr_BadInternalCall(); |
| 1954 | return NULL; |
| 1955 | } |
| 1956 | return as_ucs4(string, target, targetsize, copy_null); |
| 1957 | } |
| 1958 | |
| 1959 | Py_UCS4* |
| 1960 | PyUnicode_AsUCS4Copy(PyObject *string) |
| 1961 | { |
| 1962 | return as_ucs4(string, NULL, 0, 1); |
| 1963 | } |
| 1964 | |
| 1965 | #ifdef HAVE_WCHAR_H |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1966 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1967 | PyObject * |
| 1968 | PyUnicode_FromWideChar(register const wchar_t *w, Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1969 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1970 | if (w == NULL) { |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 1971 | if (size == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1972 | return PyUnicode_New(0, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1973 | PyErr_BadInternalCall(); |
| 1974 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1975 | } |
| 1976 | |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 1977 | if (size == -1) { |
| 1978 | size = wcslen(w); |
| 1979 | } |
| 1980 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1981 | return PyUnicode_FromUnicode(w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1982 | } |
| 1983 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1984 | #endif /* HAVE_WCHAR_H */ |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1985 | |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 1986 | static void |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1987 | makefmt(char *fmt, int longflag, int longlongflag, int size_tflag, |
| 1988 | int zeropad, int width, int precision, char c) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 1989 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1990 | *fmt++ = '%'; |
| 1991 | if (width) { |
| 1992 | if (zeropad) |
| 1993 | *fmt++ = '0'; |
| 1994 | fmt += sprintf(fmt, "%d", width); |
| 1995 | } |
| 1996 | if (precision) |
| 1997 | fmt += sprintf(fmt, ".%d", precision); |
| 1998 | if (longflag) |
| 1999 | *fmt++ = 'l'; |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2000 | else if (longlongflag) { |
| 2001 | /* longlongflag should only ever be nonzero on machines with |
| 2002 | HAVE_LONG_LONG defined */ |
| 2003 | #ifdef HAVE_LONG_LONG |
| 2004 | char *f = PY_FORMAT_LONG_LONG; |
| 2005 | while (*f) |
| 2006 | *fmt++ = *f++; |
| 2007 | #else |
| 2008 | /* we shouldn't ever get here */ |
| 2009 | assert(0); |
| 2010 | *fmt++ = 'l'; |
| 2011 | #endif |
| 2012 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2013 | else if (size_tflag) { |
| 2014 | char *f = PY_FORMAT_SIZE_T; |
| 2015 | while (*f) |
| 2016 | *fmt++ = *f++; |
| 2017 | } |
| 2018 | *fmt++ = c; |
| 2019 | *fmt = '\0'; |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2020 | } |
| 2021 | |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2022 | /* helper for PyUnicode_FromFormatV() */ |
| 2023 | |
| 2024 | static const char* |
| 2025 | parse_format_flags(const char *f, |
| 2026 | int *p_width, int *p_precision, |
| 2027 | int *p_longflag, int *p_longlongflag, int *p_size_tflag) |
| 2028 | { |
| 2029 | int width, precision, longflag, longlongflag, size_tflag; |
| 2030 | |
| 2031 | /* parse the width.precision part, e.g. "%2.5s" => width=2, precision=5 */ |
| 2032 | f++; |
| 2033 | width = 0; |
| 2034 | while (Py_ISDIGIT((unsigned)*f)) |
| 2035 | width = (width*10) + *f++ - '0'; |
| 2036 | precision = 0; |
| 2037 | if (*f == '.') { |
| 2038 | f++; |
| 2039 | while (Py_ISDIGIT((unsigned)*f)) |
| 2040 | precision = (precision*10) + *f++ - '0'; |
| 2041 | if (*f == '%') { |
| 2042 | /* "%.3%s" => f points to "3" */ |
| 2043 | f--; |
| 2044 | } |
| 2045 | } |
| 2046 | if (*f == '\0') { |
| 2047 | /* bogus format "%.1" => go backward, f points to "1" */ |
| 2048 | f--; |
| 2049 | } |
| 2050 | if (p_width != NULL) |
| 2051 | *p_width = width; |
| 2052 | if (p_precision != NULL) |
| 2053 | *p_precision = precision; |
| 2054 | |
| 2055 | /* Handle %ld, %lu, %lld and %llu. */ |
| 2056 | longflag = 0; |
| 2057 | longlongflag = 0; |
Victor Stinner | e7faec1 | 2011-03-02 00:01:53 +0000 | [diff] [blame] | 2058 | size_tflag = 0; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2059 | |
| 2060 | if (*f == 'l') { |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2061 | if (f[1] == 'd' || f[1] == 'u' || f[1] == 'i') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2062 | longflag = 1; |
| 2063 | ++f; |
| 2064 | } |
| 2065 | #ifdef HAVE_LONG_LONG |
| 2066 | else if (f[1] == 'l' && |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2067 | (f[2] == 'd' || f[2] == 'u' || f[2] == 'i')) { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2068 | longlongflag = 1; |
| 2069 | f += 2; |
| 2070 | } |
| 2071 | #endif |
| 2072 | } |
| 2073 | /* handle the size_t flag. */ |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2074 | 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] | 2075 | size_tflag = 1; |
| 2076 | ++f; |
| 2077 | } |
| 2078 | if (p_longflag != NULL) |
| 2079 | *p_longflag = longflag; |
| 2080 | if (p_longlongflag != NULL) |
| 2081 | *p_longlongflag = longlongflag; |
| 2082 | if (p_size_tflag != NULL) |
| 2083 | *p_size_tflag = size_tflag; |
| 2084 | return f; |
| 2085 | } |
| 2086 | |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2087 | /* maximum number of characters required for output of %ld. 21 characters |
| 2088 | allows for 64-bit integers (in decimal) and an optional sign. */ |
| 2089 | #define MAX_LONG_CHARS 21 |
| 2090 | /* maximum number of characters required for output of %lld. |
| 2091 | We need at most ceil(log10(256)*SIZEOF_LONG_LONG) digits, |
| 2092 | plus 1 for the sign. 53/22 is an upper bound for log10(256). */ |
| 2093 | #define MAX_LONG_LONG_CHARS (2 + (SIZEOF_LONG_LONG*53-1) / 22) |
| 2094 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2095 | PyObject * |
| 2096 | PyUnicode_FromFormatV(const char *format, va_list vargs) |
| 2097 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2098 | va_list count; |
| 2099 | Py_ssize_t callcount = 0; |
| 2100 | PyObject **callresults = NULL; |
| 2101 | PyObject **callresult = NULL; |
| 2102 | Py_ssize_t n = 0; |
| 2103 | int width = 0; |
| 2104 | int precision = 0; |
| 2105 | int zeropad; |
| 2106 | const char* f; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2107 | PyObject *string; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2108 | /* used by sprintf */ |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2109 | char fmt[61]; /* should be enough for %0width.precisionlld */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2110 | Py_UCS4 maxchar = 127; /* result is ASCII by default */ |
| 2111 | Py_UCS4 argmaxchar; |
| 2112 | Py_ssize_t numbersize = 0; |
| 2113 | char *numberresults = NULL; |
| 2114 | char *numberresult = NULL; |
| 2115 | Py_ssize_t i; |
| 2116 | int kind; |
| 2117 | void *data; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2118 | |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 2119 | Py_VA_COPY(count, vargs); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2120 | /* step 1: count the number of %S/%R/%A/%s format specifications |
| 2121 | * (we call PyObject_Str()/PyObject_Repr()/PyObject_ASCII()/ |
| 2122 | * 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] | 2123 | * result in an array) |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 2124 | * also estimate a upper bound for all the number formats in the string, |
| 2125 | * 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] | 2126 | * buffer before putting everything together. */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2127 | for (f = format; *f; f++) { |
| 2128 | if (*f == '%') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2129 | int longlongflag; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2130 | /* skip width or width.precision (eg. "1.2" of "%1.2f") */ |
| 2131 | f = parse_format_flags(f, &width, NULL, NULL, &longlongflag, NULL); |
| 2132 | if (*f == 's' || *f=='S' || *f=='R' || *f=='A' || *f=='V') |
| 2133 | ++callcount; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2134 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2135 | else if (*f == 'd' || *f=='u' || *f=='i' || *f=='x' || *f=='p') { |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2136 | #ifdef HAVE_LONG_LONG |
| 2137 | if (longlongflag) { |
| 2138 | if (width < MAX_LONG_LONG_CHARS) |
| 2139 | width = MAX_LONG_LONG_CHARS; |
| 2140 | } |
| 2141 | else |
| 2142 | #endif |
| 2143 | /* MAX_LONG_CHARS is enough to hold a 64-bit integer, |
| 2144 | including sign. Decimal takes the most space. This |
| 2145 | isn't enough for octal. If a width is specified we |
| 2146 | need more (which we allocate later). */ |
| 2147 | if (width < MAX_LONG_CHARS) |
| 2148 | width = MAX_LONG_CHARS; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2149 | |
| 2150 | /* account for the size + '\0' to separate numbers |
| 2151 | inside of the numberresults buffer */ |
| 2152 | numbersize += (width + 1); |
| 2153 | } |
| 2154 | } |
| 2155 | else if ((unsigned char)*f > 127) { |
| 2156 | PyErr_Format(PyExc_ValueError, |
| 2157 | "PyUnicode_FromFormatV() expects an ASCII-encoded format " |
| 2158 | "string, got a non-ASCII byte: 0x%02x", |
| 2159 | (unsigned char)*f); |
| 2160 | return NULL; |
| 2161 | } |
| 2162 | } |
| 2163 | /* step 2: allocate memory for the results of |
| 2164 | * PyObject_Str()/PyObject_Repr()/PyUnicode_DecodeUTF8() calls */ |
| 2165 | if (callcount) { |
| 2166 | callresults = PyObject_Malloc(sizeof(PyObject *) * callcount); |
| 2167 | if (!callresults) { |
| 2168 | PyErr_NoMemory(); |
| 2169 | return NULL; |
| 2170 | } |
| 2171 | callresult = callresults; |
| 2172 | } |
| 2173 | /* step 2.5: allocate memory for the results of formating numbers */ |
| 2174 | if (numbersize) { |
| 2175 | numberresults = PyObject_Malloc(numbersize); |
| 2176 | if (!numberresults) { |
| 2177 | PyErr_NoMemory(); |
| 2178 | goto fail; |
| 2179 | } |
| 2180 | numberresult = numberresults; |
| 2181 | } |
| 2182 | |
| 2183 | /* step 3: format numbers and figure out how large a buffer we need */ |
| 2184 | for (f = format; *f; f++) { |
| 2185 | if (*f == '%') { |
| 2186 | const char* p; |
| 2187 | int longflag; |
| 2188 | int longlongflag; |
| 2189 | int size_tflag; |
| 2190 | int numprinted; |
| 2191 | |
| 2192 | p = f; |
| 2193 | zeropad = (f[1] == '0'); |
| 2194 | f = parse_format_flags(f, &width, &precision, |
| 2195 | &longflag, &longlongflag, &size_tflag); |
| 2196 | switch (*f) { |
| 2197 | case 'c': |
| 2198 | { |
| 2199 | Py_UCS4 ordinal = va_arg(count, int); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2200 | maxchar = Py_MAX(maxchar, ordinal); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2201 | n++; |
| 2202 | break; |
| 2203 | } |
| 2204 | case '%': |
| 2205 | n++; |
| 2206 | break; |
| 2207 | case 'i': |
| 2208 | case 'd': |
| 2209 | makefmt(fmt, longflag, longlongflag, size_tflag, zeropad, |
| 2210 | width, precision, *f); |
| 2211 | if (longflag) |
| 2212 | numprinted = sprintf(numberresult, fmt, |
| 2213 | va_arg(count, long)); |
| 2214 | #ifdef HAVE_LONG_LONG |
| 2215 | else if (longlongflag) |
| 2216 | numprinted = sprintf(numberresult, fmt, |
| 2217 | va_arg(count, PY_LONG_LONG)); |
| 2218 | #endif |
| 2219 | else if (size_tflag) |
| 2220 | numprinted = sprintf(numberresult, fmt, |
| 2221 | va_arg(count, Py_ssize_t)); |
| 2222 | else |
| 2223 | numprinted = sprintf(numberresult, fmt, |
| 2224 | va_arg(count, int)); |
| 2225 | n += numprinted; |
| 2226 | /* advance by +1 to skip over the '\0' */ |
| 2227 | numberresult += (numprinted + 1); |
| 2228 | assert(*(numberresult - 1) == '\0'); |
| 2229 | assert(*(numberresult - 2) != '\0'); |
| 2230 | assert(numprinted >= 0); |
| 2231 | assert(numberresult <= numberresults + numbersize); |
| 2232 | break; |
| 2233 | case 'u': |
| 2234 | makefmt(fmt, longflag, longlongflag, size_tflag, zeropad, |
| 2235 | width, precision, 'u'); |
| 2236 | if (longflag) |
| 2237 | numprinted = sprintf(numberresult, fmt, |
| 2238 | va_arg(count, unsigned long)); |
| 2239 | #ifdef HAVE_LONG_LONG |
| 2240 | else if (longlongflag) |
| 2241 | numprinted = sprintf(numberresult, fmt, |
| 2242 | va_arg(count, unsigned PY_LONG_LONG)); |
| 2243 | #endif |
| 2244 | else if (size_tflag) |
| 2245 | numprinted = sprintf(numberresult, fmt, |
| 2246 | va_arg(count, size_t)); |
| 2247 | else |
| 2248 | numprinted = sprintf(numberresult, fmt, |
| 2249 | va_arg(count, unsigned int)); |
| 2250 | n += numprinted; |
| 2251 | numberresult += (numprinted + 1); |
| 2252 | assert(*(numberresult - 1) == '\0'); |
| 2253 | assert(*(numberresult - 2) != '\0'); |
| 2254 | assert(numprinted >= 0); |
| 2255 | assert(numberresult <= numberresults + numbersize); |
| 2256 | break; |
| 2257 | case 'x': |
| 2258 | makefmt(fmt, 0, 0, 0, zeropad, width, precision, 'x'); |
| 2259 | numprinted = sprintf(numberresult, fmt, va_arg(count, int)); |
| 2260 | n += numprinted; |
| 2261 | numberresult += (numprinted + 1); |
| 2262 | assert(*(numberresult - 1) == '\0'); |
| 2263 | assert(*(numberresult - 2) != '\0'); |
| 2264 | assert(numprinted >= 0); |
| 2265 | assert(numberresult <= numberresults + numbersize); |
| 2266 | break; |
| 2267 | case 'p': |
| 2268 | numprinted = sprintf(numberresult, "%p", va_arg(count, void*)); |
| 2269 | /* %p is ill-defined: ensure leading 0x. */ |
| 2270 | if (numberresult[1] == 'X') |
| 2271 | numberresult[1] = 'x'; |
| 2272 | else if (numberresult[1] != 'x') { |
| 2273 | memmove(numberresult + 2, numberresult, |
| 2274 | strlen(numberresult) + 1); |
| 2275 | numberresult[0] = '0'; |
| 2276 | numberresult[1] = 'x'; |
| 2277 | numprinted += 2; |
| 2278 | } |
| 2279 | n += numprinted; |
| 2280 | numberresult += (numprinted + 1); |
| 2281 | assert(*(numberresult - 1) == '\0'); |
| 2282 | assert(*(numberresult - 2) != '\0'); |
| 2283 | assert(numprinted >= 0); |
| 2284 | assert(numberresult <= numberresults + numbersize); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2285 | break; |
| 2286 | case 's': |
| 2287 | { |
| 2288 | /* UTF-8 */ |
Georg Brandl | 780b2a6 | 2009-05-05 09:19:59 +0000 | [diff] [blame] | 2289 | const char *s = va_arg(count, const char*); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2290 | PyObject *str = PyUnicode_DecodeUTF8(s, strlen(s), "replace"); |
| 2291 | if (!str) |
| 2292 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2293 | /* since PyUnicode_DecodeUTF8 returns already flexible |
| 2294 | unicode objects, there is no need to call ready on them */ |
| 2295 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2296 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2297 | n += PyUnicode_GET_LENGTH(str); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2298 | /* Remember the str and switch to the next slot */ |
| 2299 | *callresult++ = str; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2300 | break; |
| 2301 | } |
| 2302 | case 'U': |
| 2303 | { |
| 2304 | PyObject *obj = va_arg(count, PyObject *); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 2305 | assert(obj && _PyUnicode_CHECK(obj)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2306 | if (PyUnicode_READY(obj) == -1) |
| 2307 | goto fail; |
| 2308 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2309 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2310 | n += PyUnicode_GET_LENGTH(obj); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2311 | break; |
| 2312 | } |
| 2313 | case 'V': |
| 2314 | { |
| 2315 | PyObject *obj = va_arg(count, PyObject *); |
| 2316 | const char *str = va_arg(count, const char *); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2317 | PyObject *str_obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2318 | assert(obj || str); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 2319 | assert(!obj || _PyUnicode_CHECK(obj)); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2320 | if (obj) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2321 | if (PyUnicode_READY(obj) == -1) |
| 2322 | goto fail; |
| 2323 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2324 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2325 | n += PyUnicode_GET_LENGTH(obj); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2326 | *callresult++ = NULL; |
| 2327 | } |
| 2328 | else { |
| 2329 | str_obj = PyUnicode_DecodeUTF8(str, strlen(str), "replace"); |
| 2330 | if (!str_obj) |
| 2331 | goto fail; |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 2332 | if (PyUnicode_READY(str_obj)) { |
| 2333 | Py_DECREF(str_obj); |
| 2334 | goto fail; |
| 2335 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2336 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str_obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2337 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2338 | n += PyUnicode_GET_LENGTH(str_obj); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2339 | *callresult++ = str_obj; |
| 2340 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2341 | break; |
| 2342 | } |
| 2343 | case 'S': |
| 2344 | { |
| 2345 | PyObject *obj = va_arg(count, PyObject *); |
| 2346 | PyObject *str; |
| 2347 | assert(obj); |
| 2348 | str = PyObject_Str(obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2349 | if (!str || PyUnicode_READY(str) == -1) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2350 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2351 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2352 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2353 | n += PyUnicode_GET_LENGTH(str); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2354 | /* Remember the str and switch to the next slot */ |
| 2355 | *callresult++ = str; |
| 2356 | break; |
| 2357 | } |
| 2358 | case 'R': |
| 2359 | { |
| 2360 | PyObject *obj = va_arg(count, PyObject *); |
| 2361 | PyObject *repr; |
| 2362 | assert(obj); |
| 2363 | repr = PyObject_Repr(obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2364 | if (!repr || PyUnicode_READY(repr) == -1) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2365 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2366 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(repr); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2367 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2368 | n += PyUnicode_GET_LENGTH(repr); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2369 | /* Remember the repr and switch to the next slot */ |
| 2370 | *callresult++ = repr; |
| 2371 | break; |
| 2372 | } |
| 2373 | case 'A': |
| 2374 | { |
| 2375 | PyObject *obj = va_arg(count, PyObject *); |
| 2376 | PyObject *ascii; |
| 2377 | assert(obj); |
| 2378 | ascii = PyObject_ASCII(obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2379 | if (!ascii || PyUnicode_READY(ascii) == -1) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2380 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2381 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(ascii); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2382 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2383 | n += PyUnicode_GET_LENGTH(ascii); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2384 | /* Remember the repr and switch to the next slot */ |
| 2385 | *callresult++ = ascii; |
| 2386 | break; |
| 2387 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2388 | default: |
| 2389 | /* if we stumble upon an unknown |
| 2390 | formatting code, copy the rest of |
| 2391 | the format string to the output |
| 2392 | string. (we cannot just skip the |
| 2393 | code, since there's no way to know |
| 2394 | what's in the argument list) */ |
| 2395 | n += strlen(p); |
| 2396 | goto expand; |
| 2397 | } |
| 2398 | } else |
| 2399 | n++; |
| 2400 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2401 | expand: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2402 | /* step 4: fill the buffer */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2403 | /* Since we've analyzed how much space we need, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2404 | we don't have to resize the string. |
| 2405 | There can be no errors beyond this point. */ |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2406 | string = PyUnicode_New(n, maxchar); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2407 | if (!string) |
| 2408 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2409 | kind = PyUnicode_KIND(string); |
| 2410 | data = PyUnicode_DATA(string); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2411 | callresult = callresults; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2412 | numberresult = numberresults; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2413 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2414 | for (i = 0, f = format; *f; f++) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2415 | if (*f == '%') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2416 | const char* p; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2417 | |
| 2418 | p = f; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2419 | f = parse_format_flags(f, NULL, NULL, NULL, NULL, NULL); |
| 2420 | /* checking for == because the last argument could be a empty |
| 2421 | string, which causes i to point to end, the assert at the end of |
| 2422 | the loop */ |
| 2423 | assert(i <= PyUnicode_GET_LENGTH(string)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2424 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2425 | switch (*f) { |
| 2426 | case 'c': |
Victor Stinner | 5ed8b2c | 2011-02-21 21:13:44 +0000 | [diff] [blame] | 2427 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2428 | const int ordinal = va_arg(vargs, int); |
| 2429 | PyUnicode_WRITE(kind, data, i++, ordinal); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2430 | break; |
Victor Stinner | 5ed8b2c | 2011-02-21 21:13:44 +0000 | [diff] [blame] | 2431 | } |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2432 | case 'i': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2433 | case 'd': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2434 | case 'u': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2435 | case 'x': |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2436 | case 'p': |
| 2437 | /* unused, since we already have the result */ |
| 2438 | if (*f == 'p') |
| 2439 | (void) va_arg(vargs, void *); |
| 2440 | else |
| 2441 | (void) va_arg(vargs, int); |
| 2442 | /* extract the result from numberresults and append. */ |
| 2443 | for (; *numberresult; ++i, ++numberresult) |
| 2444 | PyUnicode_WRITE(kind, data, i, *numberresult); |
| 2445 | /* skip over the separating '\0' */ |
| 2446 | assert(*numberresult == '\0'); |
| 2447 | numberresult++; |
| 2448 | assert(numberresult <= numberresults + numbersize); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2449 | break; |
| 2450 | case 's': |
| 2451 | { |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2452 | /* unused, since we already have the result */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2453 | Py_ssize_t size; |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2454 | (void) va_arg(vargs, char *); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2455 | size = PyUnicode_GET_LENGTH(*callresult); |
| 2456 | assert(PyUnicode_KIND(*callresult) <= PyUnicode_KIND(string)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2457 | copy_characters(string, i, *callresult, 0, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2458 | i += size; |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2459 | /* We're done with the unicode()/repr() => forget it */ |
| 2460 | Py_DECREF(*callresult); |
| 2461 | /* switch to next unicode()/repr() result */ |
| 2462 | ++callresult; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2463 | break; |
| 2464 | } |
| 2465 | case 'U': |
| 2466 | { |
| 2467 | PyObject *obj = va_arg(vargs, PyObject *); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2468 | Py_ssize_t size; |
| 2469 | assert(PyUnicode_KIND(obj) <= PyUnicode_KIND(string)); |
| 2470 | size = PyUnicode_GET_LENGTH(obj); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2471 | copy_characters(string, i, obj, 0, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2472 | i += size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2473 | break; |
| 2474 | } |
| 2475 | case 'V': |
| 2476 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2477 | Py_ssize_t size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2478 | PyObject *obj = va_arg(vargs, PyObject *); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2479 | va_arg(vargs, const char *); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2480 | if (obj) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2481 | size = PyUnicode_GET_LENGTH(obj); |
| 2482 | assert(PyUnicode_KIND(obj) <= PyUnicode_KIND(string)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2483 | copy_characters(string, i, obj, 0, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2484 | i += size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2485 | } else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2486 | size = PyUnicode_GET_LENGTH(*callresult); |
| 2487 | assert(PyUnicode_KIND(*callresult) <= |
| 2488 | PyUnicode_KIND(string)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2489 | copy_characters(string, i, *callresult, 0, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2490 | i += size; |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2491 | Py_DECREF(*callresult); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2492 | } |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2493 | ++callresult; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2494 | break; |
| 2495 | } |
| 2496 | case 'S': |
| 2497 | case 'R': |
Victor Stinner | 9a90900 | 2010-10-18 20:59:24 +0000 | [diff] [blame] | 2498 | case 'A': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2499 | { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2500 | Py_ssize_t size = PyUnicode_GET_LENGTH(*callresult); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2501 | /* unused, since we already have the result */ |
| 2502 | (void) va_arg(vargs, PyObject *); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2503 | assert(PyUnicode_KIND(*callresult) <= PyUnicode_KIND(string)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2504 | copy_characters(string, i, *callresult, 0, size); |
| 2505 | i += size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2506 | /* We're done with the unicode()/repr() => forget it */ |
| 2507 | Py_DECREF(*callresult); |
| 2508 | /* switch to next unicode()/repr() result */ |
| 2509 | ++callresult; |
| 2510 | break; |
| 2511 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2512 | case '%': |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2513 | PyUnicode_WRITE(kind, data, i++, '%'); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2514 | break; |
| 2515 | default: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2516 | for (; *p; ++p, ++i) |
| 2517 | PyUnicode_WRITE(kind, data, i, *p); |
| 2518 | assert(i == PyUnicode_GET_LENGTH(string)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2519 | goto end; |
| 2520 | } |
Victor Stinner | 1205f27 | 2010-09-11 00:54:47 +0000 | [diff] [blame] | 2521 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2522 | else { |
| 2523 | assert(i < PyUnicode_GET_LENGTH(string)); |
| 2524 | PyUnicode_WRITE(kind, data, i++, *f); |
| 2525 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2526 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2527 | assert(i == PyUnicode_GET_LENGTH(string)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2528 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2529 | end: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2530 | if (callresults) |
| 2531 | PyObject_Free(callresults); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2532 | if (numberresults) |
| 2533 | PyObject_Free(numberresults); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2534 | assert(_PyUnicode_CheckConsistency(string, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2535 | return (PyObject *)string; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2536 | fail: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2537 | if (callresults) { |
| 2538 | PyObject **callresult2 = callresults; |
| 2539 | while (callresult2 < callresult) { |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2540 | Py_XDECREF(*callresult2); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2541 | ++callresult2; |
| 2542 | } |
| 2543 | PyObject_Free(callresults); |
| 2544 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2545 | if (numberresults) |
| 2546 | PyObject_Free(numberresults); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2547 | return NULL; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2548 | } |
| 2549 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2550 | PyObject * |
| 2551 | PyUnicode_FromFormat(const char *format, ...) |
| 2552 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2553 | PyObject* ret; |
| 2554 | va_list vargs; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2555 | |
| 2556 | #ifdef HAVE_STDARG_PROTOTYPES |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2557 | va_start(vargs, format); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2558 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2559 | va_start(vargs); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2560 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2561 | ret = PyUnicode_FromFormatV(format, vargs); |
| 2562 | va_end(vargs); |
| 2563 | return ret; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2564 | } |
| 2565 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2566 | #ifdef HAVE_WCHAR_H |
| 2567 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2568 | /* Helper function for PyUnicode_AsWideChar() and PyUnicode_AsWideCharString(): |
| 2569 | convert a Unicode object to a wide character string. |
| 2570 | |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2571 | - 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] | 2572 | character) required to convert the unicode object. Ignore size argument. |
| 2573 | |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2574 | - Otherwise: return the number of wide characters (excluding the null |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2575 | character) written into w. Write at most size wide characters (including |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2576 | the null character). */ |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2577 | static Py_ssize_t |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2578 | unicode_aswidechar(PyUnicodeObject *unicode, |
| 2579 | wchar_t *w, |
| 2580 | Py_ssize_t size) |
| 2581 | { |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2582 | Py_ssize_t res; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2583 | const wchar_t *wstr; |
| 2584 | |
| 2585 | wstr = PyUnicode_AsUnicodeAndSize((PyObject *)unicode, &res); |
| 2586 | if (wstr == NULL) |
| 2587 | return -1; |
| 2588 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2589 | if (w != NULL) { |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2590 | if (size > res) |
| 2591 | size = res + 1; |
| 2592 | else |
| 2593 | res = size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2594 | Py_MEMCPY(w, wstr, size * sizeof(wchar_t)); |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2595 | return res; |
| 2596 | } |
| 2597 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2598 | return res + 1; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2599 | } |
| 2600 | |
| 2601 | Py_ssize_t |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2602 | PyUnicode_AsWideChar(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2603 | wchar_t *w, |
| 2604 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2605 | { |
| 2606 | if (unicode == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2607 | PyErr_BadInternalCall(); |
| 2608 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2609 | } |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2610 | return unicode_aswidechar((PyUnicodeObject*)unicode, w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2611 | } |
| 2612 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2613 | wchar_t* |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 2614 | PyUnicode_AsWideCharString(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2615 | Py_ssize_t *size) |
| 2616 | { |
| 2617 | wchar_t* buffer; |
| 2618 | Py_ssize_t buflen; |
| 2619 | |
| 2620 | if (unicode == NULL) { |
| 2621 | PyErr_BadInternalCall(); |
| 2622 | return NULL; |
| 2623 | } |
| 2624 | |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 2625 | buflen = unicode_aswidechar((PyUnicodeObject *)unicode, NULL, 0); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2626 | if (buflen == -1) |
| 2627 | return NULL; |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2628 | if (PY_SSIZE_T_MAX / sizeof(wchar_t) < buflen) { |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2629 | PyErr_NoMemory(); |
| 2630 | return NULL; |
| 2631 | } |
| 2632 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2633 | buffer = PyMem_MALLOC(buflen * sizeof(wchar_t)); |
| 2634 | if (buffer == NULL) { |
| 2635 | PyErr_NoMemory(); |
| 2636 | return NULL; |
| 2637 | } |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 2638 | buflen = unicode_aswidechar((PyUnicodeObject *)unicode, buffer, buflen); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2639 | if (buflen == -1) |
| 2640 | return NULL; |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2641 | if (size != NULL) |
| 2642 | *size = buflen; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2643 | return buffer; |
| 2644 | } |
| 2645 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2646 | #endif /* HAVE_WCHAR_H */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2647 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2648 | PyObject * |
| 2649 | PyUnicode_FromOrdinal(int ordinal) |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2650 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2651 | PyObject *v; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2652 | if (ordinal < 0 || ordinal > 0x10ffff) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2653 | PyErr_SetString(PyExc_ValueError, |
| 2654 | "chr() arg not in range(0x110000)"); |
| 2655 | return NULL; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2656 | } |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 2657 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2658 | if (ordinal < 256) |
| 2659 | return get_latin1_char(ordinal); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2660 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2661 | v = PyUnicode_New(1, ordinal); |
| 2662 | if (v == NULL) |
| 2663 | return NULL; |
| 2664 | PyUnicode_WRITE(PyUnicode_KIND(v), PyUnicode_DATA(v), 0, ordinal); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2665 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2666 | return v; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2667 | } |
| 2668 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2669 | PyObject * |
| 2670 | PyUnicode_FromObject(register PyObject *obj) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2671 | { |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2672 | /* XXX Perhaps we should make this API an alias of |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2673 | PyObject_Str() instead ?! */ |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2674 | if (PyUnicode_CheckExact(obj)) { |
Victor Stinner | d3a83d5 | 2011-10-01 03:09:33 +0200 | [diff] [blame] | 2675 | if (PyUnicode_READY(obj)) |
| 2676 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2677 | Py_INCREF(obj); |
| 2678 | return obj; |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2679 | } |
| 2680 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2681 | /* For a Unicode subtype that's not a Unicode object, |
| 2682 | return a true Unicode object with the same data. */ |
Victor Stinner | 2219e0a | 2011-10-01 01:16:59 +0200 | [diff] [blame] | 2683 | return PyUnicode_Copy(obj); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2684 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2685 | PyErr_Format(PyExc_TypeError, |
| 2686 | "Can't convert '%.100s' object to str implicitly", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2687 | Py_TYPE(obj)->tp_name); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2688 | return NULL; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2689 | } |
| 2690 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2691 | PyObject * |
| 2692 | PyUnicode_FromEncodedObject(register PyObject *obj, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2693 | const char *encoding, |
| 2694 | const char *errors) |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2695 | { |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2696 | Py_buffer buffer; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2697 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2698 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2699 | if (obj == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2700 | PyErr_BadInternalCall(); |
| 2701 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2702 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2703 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2704 | /* Decoding bytes objects is the most common case and should be fast */ |
| 2705 | if (PyBytes_Check(obj)) { |
| 2706 | if (PyBytes_GET_SIZE(obj) == 0) { |
| 2707 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 2708 | v = unicode_empty; |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2709 | } |
| 2710 | else { |
| 2711 | v = PyUnicode_Decode( |
| 2712 | PyBytes_AS_STRING(obj), PyBytes_GET_SIZE(obj), |
| 2713 | encoding, errors); |
| 2714 | } |
| 2715 | return v; |
| 2716 | } |
| 2717 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2718 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2719 | PyErr_SetString(PyExc_TypeError, |
| 2720 | "decoding str is not supported"); |
| 2721 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2722 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2723 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2724 | /* Retrieve a bytes buffer view through the PEP 3118 buffer interface */ |
| 2725 | if (PyObject_GetBuffer(obj, &buffer, PyBUF_SIMPLE) < 0) { |
| 2726 | PyErr_Format(PyExc_TypeError, |
| 2727 | "coercing to str: need bytes, bytearray " |
| 2728 | "or buffer-like object, %.80s found", |
| 2729 | Py_TYPE(obj)->tp_name); |
| 2730 | return NULL; |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 2731 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2732 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2733 | if (buffer.len == 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2734 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 2735 | v = unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2736 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2737 | else |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2738 | v = PyUnicode_Decode((char*) buffer.buf, buffer.len, encoding, errors); |
Marc-André Lemburg | ad7c98e | 2001-01-17 17:09:53 +0000 | [diff] [blame] | 2739 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2740 | PyBuffer_Release(&buffer); |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2741 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2742 | } |
| 2743 | |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2744 | /* Convert encoding to lower case and replace '_' with '-' in order to |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2745 | catch e.g. UTF_8. Return 0 on error (encoding is longer than lower_len-1), |
| 2746 | 1 on success. */ |
| 2747 | static int |
| 2748 | normalize_encoding(const char *encoding, |
| 2749 | char *lower, |
| 2750 | size_t lower_len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2751 | { |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2752 | const char *e; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2753 | char *l; |
| 2754 | char *l_end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2755 | |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2756 | e = encoding; |
| 2757 | l = lower; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2758 | l_end = &lower[lower_len - 1]; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2759 | while (*e) { |
| 2760 | if (l == l_end) |
| 2761 | return 0; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 2762 | if (Py_ISUPPER(*e)) { |
| 2763 | *l++ = Py_TOLOWER(*e++); |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2764 | } |
| 2765 | else if (*e == '_') { |
| 2766 | *l++ = '-'; |
| 2767 | e++; |
| 2768 | } |
| 2769 | else { |
| 2770 | *l++ = *e++; |
| 2771 | } |
| 2772 | } |
| 2773 | *l = '\0'; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2774 | return 1; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2775 | } |
| 2776 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2777 | PyObject * |
| 2778 | PyUnicode_Decode(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2779 | Py_ssize_t size, |
| 2780 | const char *encoding, |
| 2781 | const char *errors) |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2782 | { |
| 2783 | PyObject *buffer = NULL, *unicode; |
| 2784 | Py_buffer info; |
| 2785 | char lower[11]; /* Enough for any encoding shortcut */ |
| 2786 | |
| 2787 | if (encoding == NULL) |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2788 | return PyUnicode_DecodeUTF8(s, size, errors); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 2789 | |
| 2790 | /* Shortcuts for common default encodings */ |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2791 | if (normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2792 | if ((strcmp(lower, "utf-8") == 0) || |
| 2793 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2794 | return PyUnicode_DecodeUTF8(s, size, errors); |
| 2795 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2796 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2797 | (strcmp(lower, "iso-8859-1") == 0)) |
| 2798 | return PyUnicode_DecodeLatin1(s, size, errors); |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 2799 | #ifdef HAVE_MBCS |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2800 | else if (strcmp(lower, "mbcs") == 0) |
| 2801 | return PyUnicode_DecodeMBCS(s, size, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 2802 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2803 | else if (strcmp(lower, "ascii") == 0) |
| 2804 | return PyUnicode_DecodeASCII(s, size, errors); |
| 2805 | else if (strcmp(lower, "utf-16") == 0) |
| 2806 | return PyUnicode_DecodeUTF16(s, size, errors, 0); |
| 2807 | else if (strcmp(lower, "utf-32") == 0) |
| 2808 | return PyUnicode_DecodeUTF32(s, size, errors, 0); |
| 2809 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2810 | |
| 2811 | /* Decode via the codec registry */ |
Guido van Rossum | be801ac | 2007-10-08 03:32:34 +0000 | [diff] [blame] | 2812 | buffer = NULL; |
Antoine Pitrou | c3b3924 | 2009-01-03 16:59:18 +0000 | [diff] [blame] | 2813 | 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] | 2814 | goto onError; |
Antoine Pitrou | ee58fa4 | 2008-08-19 18:22:14 +0000 | [diff] [blame] | 2815 | buffer = PyMemoryView_FromBuffer(&info); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2816 | if (buffer == NULL) |
| 2817 | goto onError; |
| 2818 | unicode = PyCodec_Decode(buffer, encoding, errors); |
| 2819 | if (unicode == NULL) |
| 2820 | goto onError; |
| 2821 | if (!PyUnicode_Check(unicode)) { |
| 2822 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 2823 | "decoder did not return a str object (type=%.400s)", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2824 | Py_TYPE(unicode)->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2825 | Py_DECREF(unicode); |
| 2826 | goto onError; |
| 2827 | } |
| 2828 | Py_DECREF(buffer); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 2829 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 2830 | if (_PyUnicode_READY_REPLACE(&unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2831 | Py_DECREF(unicode); |
| 2832 | return NULL; |
| 2833 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 2834 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2835 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2836 | return unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2837 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2838 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2839 | Py_XDECREF(buffer); |
| 2840 | return NULL; |
| 2841 | } |
| 2842 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2843 | PyObject * |
| 2844 | PyUnicode_AsDecodedObject(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2845 | const char *encoding, |
| 2846 | const char *errors) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2847 | { |
| 2848 | PyObject *v; |
| 2849 | |
| 2850 | if (!PyUnicode_Check(unicode)) { |
| 2851 | PyErr_BadArgument(); |
| 2852 | goto onError; |
| 2853 | } |
| 2854 | |
| 2855 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2856 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2857 | |
| 2858 | /* Decode via the codec registry */ |
| 2859 | v = PyCodec_Decode(unicode, encoding, errors); |
| 2860 | if (v == NULL) |
| 2861 | goto onError; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2862 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2863 | return v; |
| 2864 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2865 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2866 | return NULL; |
| 2867 | } |
| 2868 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2869 | PyObject * |
| 2870 | PyUnicode_AsDecodedUnicode(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2871 | const char *encoding, |
| 2872 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2873 | { |
| 2874 | PyObject *v; |
| 2875 | |
| 2876 | if (!PyUnicode_Check(unicode)) { |
| 2877 | PyErr_BadArgument(); |
| 2878 | goto onError; |
| 2879 | } |
| 2880 | |
| 2881 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2882 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2883 | |
| 2884 | /* Decode via the codec registry */ |
| 2885 | v = PyCodec_Decode(unicode, encoding, errors); |
| 2886 | if (v == NULL) |
| 2887 | goto onError; |
| 2888 | if (!PyUnicode_Check(v)) { |
| 2889 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 2890 | "decoder did not return a str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2891 | Py_TYPE(v)->tp_name); |
| 2892 | Py_DECREF(v); |
| 2893 | goto onError; |
| 2894 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2895 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2896 | return v; |
| 2897 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2898 | onError: |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2899 | return NULL; |
| 2900 | } |
| 2901 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2902 | PyObject * |
| 2903 | PyUnicode_Encode(const Py_UNICODE *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2904 | Py_ssize_t size, |
| 2905 | const char *encoding, |
| 2906 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2907 | { |
| 2908 | PyObject *v, *unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2909 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2910 | unicode = PyUnicode_FromUnicode(s, size); |
| 2911 | if (unicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2912 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2913 | v = PyUnicode_AsEncodedString(unicode, encoding, errors); |
| 2914 | Py_DECREF(unicode); |
| 2915 | return v; |
| 2916 | } |
| 2917 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2918 | PyObject * |
| 2919 | PyUnicode_AsEncodedObject(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2920 | const char *encoding, |
| 2921 | const char *errors) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2922 | { |
| 2923 | PyObject *v; |
| 2924 | |
| 2925 | if (!PyUnicode_Check(unicode)) { |
| 2926 | PyErr_BadArgument(); |
| 2927 | goto onError; |
| 2928 | } |
| 2929 | |
| 2930 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2931 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2932 | |
| 2933 | /* Encode via the codec registry */ |
| 2934 | v = PyCodec_Encode(unicode, encoding, errors); |
| 2935 | if (v == NULL) |
| 2936 | goto onError; |
| 2937 | return v; |
| 2938 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2939 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2940 | return NULL; |
| 2941 | } |
| 2942 | |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 2943 | PyObject * |
| 2944 | PyUnicode_EncodeFSDefault(PyObject *unicode) |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 2945 | { |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 2946 | #ifdef HAVE_MBCS |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 2947 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
| 2948 | PyUnicode_GET_SIZE(unicode), |
| 2949 | NULL); |
| 2950 | #elif defined(__APPLE__) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2951 | return _PyUnicode_AsUTF8String(unicode, "surrogateescape"); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 2952 | #else |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 2953 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 2954 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 2955 | cannot use it to encode and decode filenames before it is loaded. Load |
| 2956 | the Python codec requires to encode at least its own filename. Use the C |
| 2957 | version of the locale codec until the codec registry is initialized and |
| 2958 | the Python codec is loaded. |
| 2959 | |
| 2960 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 2961 | cannot only rely on it: check also interp->fscodec_initialized for |
| 2962 | subinterpreters. */ |
| 2963 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 2964 | return PyUnicode_AsEncodedString(unicode, |
| 2965 | Py_FileSystemDefaultEncoding, |
| 2966 | "surrogateescape"); |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 2967 | } |
| 2968 | else { |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 2969 | /* locale encoding with surrogateescape */ |
| 2970 | wchar_t *wchar; |
| 2971 | char *bytes; |
| 2972 | PyObject *bytes_obj; |
Victor Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 2973 | size_t error_pos; |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 2974 | |
| 2975 | wchar = PyUnicode_AsWideCharString(unicode, NULL); |
| 2976 | if (wchar == NULL) |
| 2977 | return NULL; |
Victor Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 2978 | bytes = _Py_wchar2char(wchar, &error_pos); |
| 2979 | if (bytes == NULL) { |
| 2980 | if (error_pos != (size_t)-1) { |
| 2981 | char *errmsg = strerror(errno); |
| 2982 | PyObject *exc = NULL; |
| 2983 | if (errmsg == NULL) |
| 2984 | errmsg = "Py_wchar2char() failed"; |
| 2985 | raise_encode_exception(&exc, |
| 2986 | "filesystemencoding", |
| 2987 | PyUnicode_AS_UNICODE(unicode), PyUnicode_GET_SIZE(unicode), |
| 2988 | error_pos, error_pos+1, |
| 2989 | errmsg); |
| 2990 | Py_XDECREF(exc); |
| 2991 | } |
| 2992 | else |
| 2993 | PyErr_NoMemory(); |
| 2994 | PyMem_Free(wchar); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 2995 | return NULL; |
Victor Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 2996 | } |
| 2997 | PyMem_Free(wchar); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 2998 | |
| 2999 | bytes_obj = PyBytes_FromString(bytes); |
| 3000 | PyMem_Free(bytes); |
| 3001 | return bytes_obj; |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 3002 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3003 | #endif |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3004 | } |
| 3005 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3006 | PyObject * |
| 3007 | PyUnicode_AsEncodedString(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3008 | const char *encoding, |
| 3009 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3010 | { |
| 3011 | PyObject *v; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3012 | char lower[11]; /* Enough for any encoding shortcut */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3013 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3014 | if (!PyUnicode_Check(unicode)) { |
| 3015 | PyErr_BadArgument(); |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3016 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3017 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3018 | |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3019 | if (encoding == NULL) { |
| 3020 | if (errors == NULL || strcmp(errors, "strict") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3021 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3022 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3023 | return _PyUnicode_AsUTF8String(unicode, errors); |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3024 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3025 | |
| 3026 | /* Shortcuts for common default encodings */ |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3027 | if (normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3028 | if ((strcmp(lower, "utf-8") == 0) || |
| 3029 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 3030 | { |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3031 | if (errors == NULL || strcmp(errors, "strict") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3032 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3033 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3034 | return _PyUnicode_AsUTF8String(unicode, errors); |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 3035 | } |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3036 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3037 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3038 | (strcmp(lower, "iso-8859-1") == 0)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3039 | return _PyUnicode_AsLatin1String(unicode, errors); |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3040 | #ifdef HAVE_MBCS |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3041 | else if (strcmp(lower, "mbcs") == 0) |
| 3042 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
| 3043 | PyUnicode_GET_SIZE(unicode), |
| 3044 | errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 3045 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3046 | else if (strcmp(lower, "ascii") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3047 | return _PyUnicode_AsASCIIString(unicode, errors); |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3048 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3049 | |
| 3050 | /* Encode via the codec registry */ |
| 3051 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3052 | if (v == NULL) |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3053 | return NULL; |
| 3054 | |
| 3055 | /* The normal path */ |
| 3056 | if (PyBytes_Check(v)) |
| 3057 | return v; |
| 3058 | |
| 3059 | /* 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] | 3060 | if (PyByteArray_Check(v)) { |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 3061 | int error; |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3062 | PyObject *b; |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 3063 | |
| 3064 | error = PyErr_WarnFormat(PyExc_RuntimeWarning, 1, |
| 3065 | "encoder %s returned bytearray instead of bytes", |
| 3066 | encoding); |
| 3067 | if (error) { |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3068 | Py_DECREF(v); |
| 3069 | return NULL; |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3070 | } |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3071 | |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3072 | b = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(v), Py_SIZE(v)); |
| 3073 | Py_DECREF(v); |
| 3074 | return b; |
| 3075 | } |
| 3076 | |
| 3077 | PyErr_Format(PyExc_TypeError, |
| 3078 | "encoder did not return a bytes object (type=%.400s)", |
| 3079 | Py_TYPE(v)->tp_name); |
| 3080 | Py_DECREF(v); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3081 | return NULL; |
| 3082 | } |
| 3083 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3084 | PyObject * |
| 3085 | PyUnicode_AsEncodedUnicode(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3086 | const char *encoding, |
| 3087 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3088 | { |
| 3089 | PyObject *v; |
| 3090 | |
| 3091 | if (!PyUnicode_Check(unicode)) { |
| 3092 | PyErr_BadArgument(); |
| 3093 | goto onError; |
| 3094 | } |
| 3095 | |
| 3096 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3097 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3098 | |
| 3099 | /* Encode via the codec registry */ |
| 3100 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3101 | if (v == NULL) |
| 3102 | goto onError; |
| 3103 | if (!PyUnicode_Check(v)) { |
| 3104 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3105 | "encoder did not return an str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3106 | Py_TYPE(v)->tp_name); |
| 3107 | Py_DECREF(v); |
| 3108 | goto onError; |
| 3109 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3110 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3111 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3112 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3113 | return NULL; |
| 3114 | } |
| 3115 | |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3116 | PyObject* |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3117 | PyUnicode_DecodeFSDefault(const char *s) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3118 | Py_ssize_t size = (Py_ssize_t)strlen(s); |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3119 | return PyUnicode_DecodeFSDefaultAndSize(s, size); |
| 3120 | } |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3121 | |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3122 | PyObject* |
| 3123 | PyUnicode_DecodeFSDefaultAndSize(const char *s, Py_ssize_t size) |
| 3124 | { |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3125 | #ifdef HAVE_MBCS |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3126 | return PyUnicode_DecodeMBCS(s, size, NULL); |
| 3127 | #elif defined(__APPLE__) |
| 3128 | return PyUnicode_DecodeUTF8(s, size, "surrogateescape"); |
| 3129 | #else |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 3130 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 3131 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 3132 | cannot use it to encode and decode filenames before it is loaded. Load |
| 3133 | the Python codec requires to encode at least its own filename. Use the C |
| 3134 | version of the locale codec until the codec registry is initialized and |
| 3135 | the Python codec is loaded. |
| 3136 | |
| 3137 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 3138 | cannot only rely on it: check also interp->fscodec_initialized for |
| 3139 | subinterpreters. */ |
| 3140 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3141 | return PyUnicode_Decode(s, size, |
| 3142 | Py_FileSystemDefaultEncoding, |
Victor Stinner | b9a20ad | 2010-04-30 16:37:52 +0000 | [diff] [blame] | 3143 | "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3144 | } |
| 3145 | else { |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3146 | /* locale encoding with surrogateescape */ |
| 3147 | wchar_t *wchar; |
| 3148 | PyObject *unicode; |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 3149 | size_t len; |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3150 | |
| 3151 | if (s[size] != '\0' || size != strlen(s)) { |
| 3152 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
| 3153 | return NULL; |
| 3154 | } |
| 3155 | |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 3156 | wchar = _Py_char2wchar(s, &len); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3157 | if (wchar == NULL) |
Victor Stinner | d5af0a5 | 2010-11-08 23:34:29 +0000 | [diff] [blame] | 3158 | return PyErr_NoMemory(); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3159 | |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 3160 | unicode = PyUnicode_FromWideChar(wchar, len); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3161 | PyMem_Free(wchar); |
| 3162 | return unicode; |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3163 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3164 | #endif |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3165 | } |
| 3166 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3167 | |
| 3168 | int |
| 3169 | PyUnicode_FSConverter(PyObject* arg, void* addr) |
| 3170 | { |
| 3171 | PyObject *output = NULL; |
| 3172 | Py_ssize_t size; |
| 3173 | void *data; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3174 | if (arg == NULL) { |
| 3175 | Py_DECREF(*(PyObject**)addr); |
| 3176 | return 1; |
| 3177 | } |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 3178 | if (PyBytes_Check(arg)) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3179 | output = arg; |
| 3180 | Py_INCREF(output); |
| 3181 | } |
| 3182 | else { |
| 3183 | arg = PyUnicode_FromObject(arg); |
| 3184 | if (!arg) |
| 3185 | return 0; |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3186 | output = PyUnicode_EncodeFSDefault(arg); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3187 | Py_DECREF(arg); |
| 3188 | if (!output) |
| 3189 | return 0; |
| 3190 | if (!PyBytes_Check(output)) { |
| 3191 | Py_DECREF(output); |
| 3192 | PyErr_SetString(PyExc_TypeError, "encoder failed to return bytes"); |
| 3193 | return 0; |
| 3194 | } |
| 3195 | } |
Victor Stinner | 0ea2a46 | 2010-04-30 00:22:08 +0000 | [diff] [blame] | 3196 | size = PyBytes_GET_SIZE(output); |
| 3197 | data = PyBytes_AS_STRING(output); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3198 | if (size != strlen(data)) { |
Benjamin Peterson | 7a6b44a | 2011-08-18 13:51:47 -0500 | [diff] [blame] | 3199 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3200 | Py_DECREF(output); |
| 3201 | return 0; |
| 3202 | } |
| 3203 | *(PyObject**)addr = output; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3204 | return Py_CLEANUP_SUPPORTED; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3205 | } |
| 3206 | |
| 3207 | |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3208 | int |
| 3209 | PyUnicode_FSDecoder(PyObject* arg, void* addr) |
| 3210 | { |
| 3211 | PyObject *output = NULL; |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3212 | if (arg == NULL) { |
| 3213 | Py_DECREF(*(PyObject**)addr); |
| 3214 | return 1; |
| 3215 | } |
| 3216 | if (PyUnicode_Check(arg)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3217 | if (PyUnicode_READY(arg)) |
| 3218 | return 0; |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3219 | output = arg; |
| 3220 | Py_INCREF(output); |
| 3221 | } |
| 3222 | else { |
| 3223 | arg = PyBytes_FromObject(arg); |
| 3224 | if (!arg) |
| 3225 | return 0; |
| 3226 | output = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AS_STRING(arg), |
| 3227 | PyBytes_GET_SIZE(arg)); |
| 3228 | Py_DECREF(arg); |
| 3229 | if (!output) |
| 3230 | return 0; |
| 3231 | if (!PyUnicode_Check(output)) { |
| 3232 | Py_DECREF(output); |
| 3233 | PyErr_SetString(PyExc_TypeError, "decoder failed to return unicode"); |
| 3234 | return 0; |
| 3235 | } |
| 3236 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3237 | if (findchar(PyUnicode_DATA(output), PyUnicode_KIND(output), |
| 3238 | PyUnicode_GET_LENGTH(output), 0, 1)) { |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3239 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
| 3240 | Py_DECREF(output); |
| 3241 | return 0; |
| 3242 | } |
| 3243 | *(PyObject**)addr = output; |
| 3244 | return Py_CLEANUP_SUPPORTED; |
| 3245 | } |
| 3246 | |
| 3247 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3248 | char* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3249 | PyUnicode_AsUTF8AndSize(PyObject *unicode, Py_ssize_t *psize) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3250 | { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 3251 | PyObject *bytes; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3252 | PyUnicodeObject *u = (PyUnicodeObject *)unicode; |
| 3253 | |
Neal Norwitz | e0a0a6e | 2007-08-25 01:04:21 +0000 | [diff] [blame] | 3254 | if (!PyUnicode_Check(unicode)) { |
| 3255 | PyErr_BadArgument(); |
| 3256 | return NULL; |
| 3257 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3258 | if (PyUnicode_READY(u) == -1) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3259 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3260 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3261 | if (PyUnicode_UTF8(unicode) == NULL) { |
| 3262 | assert(!PyUnicode_IS_COMPACT_ASCII(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3263 | bytes = _PyUnicode_AsUTF8String(unicode, "strict"); |
| 3264 | if (bytes == NULL) |
| 3265 | return NULL; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3266 | _PyUnicode_UTF8(u) = PyObject_MALLOC(PyBytes_GET_SIZE(bytes) + 1); |
| 3267 | if (_PyUnicode_UTF8(u) == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3268 | Py_DECREF(bytes); |
| 3269 | return NULL; |
| 3270 | } |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3271 | _PyUnicode_UTF8_LENGTH(u) = PyBytes_GET_SIZE(bytes); |
| 3272 | 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] | 3273 | Py_DECREF(bytes); |
| 3274 | } |
| 3275 | |
| 3276 | if (psize) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3277 | *psize = PyUnicode_UTF8_LENGTH(unicode); |
| 3278 | return PyUnicode_UTF8(unicode); |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 3279 | } |
| 3280 | |
| 3281 | char* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3282 | PyUnicode_AsUTF8(PyObject *unicode) |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 3283 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3284 | return PyUnicode_AsUTF8AndSize(unicode, NULL); |
| 3285 | } |
| 3286 | |
| 3287 | #ifdef Py_DEBUG |
| 3288 | int unicode_as_unicode_calls = 0; |
| 3289 | #endif |
| 3290 | |
| 3291 | |
| 3292 | Py_UNICODE * |
| 3293 | PyUnicode_AsUnicodeAndSize(PyObject *unicode, Py_ssize_t *size) |
| 3294 | { |
| 3295 | PyUnicodeObject *u; |
| 3296 | const unsigned char *one_byte; |
| 3297 | #if SIZEOF_WCHAR_T == 4 |
| 3298 | const Py_UCS2 *two_bytes; |
| 3299 | #else |
| 3300 | const Py_UCS4 *four_bytes; |
| 3301 | const Py_UCS4 *ucs4_end; |
| 3302 | Py_ssize_t num_surrogates; |
| 3303 | #endif |
| 3304 | wchar_t *w; |
| 3305 | wchar_t *wchar_end; |
| 3306 | |
| 3307 | if (!PyUnicode_Check(unicode)) { |
| 3308 | PyErr_BadArgument(); |
| 3309 | return NULL; |
| 3310 | } |
| 3311 | u = (PyUnicodeObject*)unicode; |
| 3312 | if (_PyUnicode_WSTR(u) == NULL) { |
| 3313 | /* Non-ASCII compact unicode object */ |
| 3314 | assert(_PyUnicode_KIND(u) != 0); |
| 3315 | assert(PyUnicode_IS_READY(u)); |
| 3316 | |
| 3317 | #ifdef Py_DEBUG |
| 3318 | ++unicode_as_unicode_calls; |
| 3319 | #endif |
| 3320 | |
| 3321 | if (PyUnicode_KIND(u) == PyUnicode_4BYTE_KIND) { |
| 3322 | #if SIZEOF_WCHAR_T == 2 |
| 3323 | four_bytes = PyUnicode_4BYTE_DATA(u); |
| 3324 | ucs4_end = four_bytes + _PyUnicode_LENGTH(u); |
| 3325 | num_surrogates = 0; |
| 3326 | |
| 3327 | for (; four_bytes < ucs4_end; ++four_bytes) { |
| 3328 | if (*four_bytes > 0xFFFF) |
| 3329 | ++num_surrogates; |
| 3330 | } |
| 3331 | |
| 3332 | _PyUnicode_WSTR(u) = (wchar_t *) PyObject_MALLOC( |
| 3333 | sizeof(wchar_t) * (_PyUnicode_LENGTH(u) + 1 + num_surrogates)); |
| 3334 | if (!_PyUnicode_WSTR(u)) { |
| 3335 | PyErr_NoMemory(); |
| 3336 | return NULL; |
| 3337 | } |
| 3338 | _PyUnicode_WSTR_LENGTH(u) = _PyUnicode_LENGTH(u) + num_surrogates; |
| 3339 | |
| 3340 | w = _PyUnicode_WSTR(u); |
| 3341 | wchar_end = w + _PyUnicode_WSTR_LENGTH(u); |
| 3342 | four_bytes = PyUnicode_4BYTE_DATA(u); |
| 3343 | for (; four_bytes < ucs4_end; ++four_bytes, ++w) { |
| 3344 | if (*four_bytes > 0xFFFF) { |
| 3345 | /* encode surrogate pair in this case */ |
| 3346 | *w++ = 0xD800 | ((*four_bytes - 0x10000) >> 10); |
| 3347 | *w = 0xDC00 | ((*four_bytes - 0x10000) & 0x3FF); |
| 3348 | } |
| 3349 | else |
| 3350 | *w = *four_bytes; |
| 3351 | |
| 3352 | if (w > wchar_end) { |
| 3353 | assert(0 && "Miscalculated string end"); |
| 3354 | } |
| 3355 | } |
| 3356 | *w = 0; |
| 3357 | #else |
| 3358 | /* sizeof(wchar_t) == 4 */ |
| 3359 | Py_FatalError("Impossible unicode object state, wstr and str " |
| 3360 | "should share memory already."); |
| 3361 | return NULL; |
| 3362 | #endif |
| 3363 | } |
| 3364 | else { |
| 3365 | _PyUnicode_WSTR(u) = (wchar_t *) PyObject_MALLOC(sizeof(wchar_t) * |
| 3366 | (_PyUnicode_LENGTH(u) + 1)); |
| 3367 | if (!_PyUnicode_WSTR(u)) { |
| 3368 | PyErr_NoMemory(); |
| 3369 | return NULL; |
| 3370 | } |
| 3371 | if (!PyUnicode_IS_COMPACT_ASCII(u)) |
| 3372 | _PyUnicode_WSTR_LENGTH(u) = _PyUnicode_LENGTH(u); |
| 3373 | w = _PyUnicode_WSTR(u); |
| 3374 | wchar_end = w + _PyUnicode_LENGTH(u); |
| 3375 | |
| 3376 | if (PyUnicode_KIND(u) == PyUnicode_1BYTE_KIND) { |
| 3377 | one_byte = PyUnicode_1BYTE_DATA(u); |
| 3378 | for (; w < wchar_end; ++one_byte, ++w) |
| 3379 | *w = *one_byte; |
| 3380 | /* null-terminate the wstr */ |
| 3381 | *w = 0; |
| 3382 | } |
| 3383 | else if (PyUnicode_KIND(u) == PyUnicode_2BYTE_KIND) { |
| 3384 | #if SIZEOF_WCHAR_T == 4 |
| 3385 | two_bytes = PyUnicode_2BYTE_DATA(u); |
| 3386 | for (; w < wchar_end; ++two_bytes, ++w) |
| 3387 | *w = *two_bytes; |
| 3388 | /* null-terminate the wstr */ |
| 3389 | *w = 0; |
| 3390 | #else |
| 3391 | /* sizeof(wchar_t) == 2 */ |
| 3392 | PyObject_FREE(_PyUnicode_WSTR(u)); |
| 3393 | _PyUnicode_WSTR(u) = NULL; |
| 3394 | Py_FatalError("Impossible unicode object state, wstr " |
| 3395 | "and str should share memory already."); |
| 3396 | return NULL; |
| 3397 | #endif |
| 3398 | } |
| 3399 | else { |
| 3400 | assert(0 && "This should never happen."); |
| 3401 | } |
| 3402 | } |
| 3403 | } |
| 3404 | if (size != NULL) |
| 3405 | *size = PyUnicode_WSTR_LENGTH(u); |
| 3406 | return _PyUnicode_WSTR(u); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3407 | } |
| 3408 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3409 | Py_UNICODE * |
| 3410 | PyUnicode_AsUnicode(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3411 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3412 | return PyUnicode_AsUnicodeAndSize(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3413 | } |
| 3414 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3415 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3416 | Py_ssize_t |
| 3417 | PyUnicode_GetSize(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3418 | { |
| 3419 | if (!PyUnicode_Check(unicode)) { |
| 3420 | PyErr_BadArgument(); |
| 3421 | goto onError; |
| 3422 | } |
| 3423 | return PyUnicode_GET_SIZE(unicode); |
| 3424 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3425 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3426 | return -1; |
| 3427 | } |
| 3428 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3429 | Py_ssize_t |
| 3430 | PyUnicode_GetLength(PyObject *unicode) |
| 3431 | { |
Victor Stinner | 5a706cf | 2011-10-02 00:36:53 +0200 | [diff] [blame] | 3432 | if (!PyUnicode_Check(unicode) || PyUnicode_READY(unicode) == -1) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3433 | PyErr_BadArgument(); |
| 3434 | return -1; |
| 3435 | } |
| 3436 | |
| 3437 | return PyUnicode_GET_LENGTH(unicode); |
| 3438 | } |
| 3439 | |
| 3440 | Py_UCS4 |
| 3441 | PyUnicode_ReadChar(PyObject *unicode, Py_ssize_t index) |
| 3442 | { |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 3443 | if (!PyUnicode_Check(unicode) || PyUnicode_READY(unicode) == -1) { |
| 3444 | PyErr_BadArgument(); |
| 3445 | return (Py_UCS4)-1; |
| 3446 | } |
| 3447 | if (index < 0 || index >= _PyUnicode_LENGTH(unicode)) { |
| 3448 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3449 | return (Py_UCS4)-1; |
| 3450 | } |
| 3451 | return PyUnicode_READ_CHAR(unicode, index); |
| 3452 | } |
| 3453 | |
| 3454 | int |
| 3455 | PyUnicode_WriteChar(PyObject *unicode, Py_ssize_t index, Py_UCS4 ch) |
| 3456 | { |
| 3457 | if (!PyUnicode_Check(unicode) || !PyUnicode_IS_COMPACT(unicode)) { |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 3458 | PyErr_BadArgument(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3459 | return -1; |
| 3460 | } |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 3461 | if (index < 0 || index >= _PyUnicode_LENGTH(unicode)) { |
| 3462 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 3463 | return -1; |
| 3464 | } |
| 3465 | if (_PyUnicode_Dirty(unicode)) |
| 3466 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3467 | PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode), |
| 3468 | index, ch); |
| 3469 | return 0; |
| 3470 | } |
| 3471 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3472 | const char * |
| 3473 | PyUnicode_GetDefaultEncoding(void) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3474 | { |
Victor Stinner | 42cb462 | 2010-09-01 19:39:01 +0000 | [diff] [blame] | 3475 | return "utf-8"; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3476 | } |
| 3477 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 3478 | /* create or adjust a UnicodeDecodeError */ |
| 3479 | static void |
| 3480 | make_decode_exception(PyObject **exceptionObject, |
| 3481 | const char *encoding, |
| 3482 | const char *input, Py_ssize_t length, |
| 3483 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 3484 | const char *reason) |
| 3485 | { |
| 3486 | if (*exceptionObject == NULL) { |
| 3487 | *exceptionObject = PyUnicodeDecodeError_Create( |
| 3488 | encoding, input, length, startpos, endpos, reason); |
| 3489 | } |
| 3490 | else { |
| 3491 | if (PyUnicodeDecodeError_SetStart(*exceptionObject, startpos)) |
| 3492 | goto onError; |
| 3493 | if (PyUnicodeDecodeError_SetEnd(*exceptionObject, endpos)) |
| 3494 | goto onError; |
| 3495 | if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) |
| 3496 | goto onError; |
| 3497 | } |
| 3498 | return; |
| 3499 | |
| 3500 | onError: |
| 3501 | Py_DECREF(*exceptionObject); |
| 3502 | *exceptionObject = NULL; |
| 3503 | } |
| 3504 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3505 | /* error handling callback helper: |
| 3506 | build arguments, call the callback and check the arguments, |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 3507 | if no exception occurred, copy the replacement to the output |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3508 | and adjust various state variables. |
| 3509 | return 0 on success, -1 on error |
| 3510 | */ |
| 3511 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3512 | static int |
| 3513 | unicode_decode_call_errorhandler(const char *errors, PyObject **errorHandler, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3514 | const char *encoding, const char *reason, |
| 3515 | const char **input, const char **inend, Py_ssize_t *startinpos, |
| 3516 | Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
| 3517 | PyUnicodeObject **output, Py_ssize_t *outpos, Py_UNICODE **outptr) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3518 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3519 | 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] | 3520 | |
| 3521 | PyObject *restuple = NULL; |
| 3522 | PyObject *repunicode = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3523 | Py_ssize_t outsize = PyUnicode_GET_SIZE(*output); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3524 | Py_ssize_t insize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3525 | Py_ssize_t requiredsize; |
| 3526 | Py_ssize_t newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3527 | const Py_UNICODE *repptr; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3528 | PyObject *inputobj = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3529 | Py_ssize_t repsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3530 | int res = -1; |
| 3531 | |
| 3532 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3533 | *errorHandler = PyCodec_LookupError(errors); |
| 3534 | if (*errorHandler == NULL) |
| 3535 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3536 | } |
| 3537 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 3538 | make_decode_exception(exceptionObject, |
| 3539 | encoding, |
| 3540 | *input, *inend - *input, |
| 3541 | *startinpos, *endinpos, |
| 3542 | reason); |
| 3543 | if (*exceptionObject == NULL) |
| 3544 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3545 | |
| 3546 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 3547 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3548 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3549 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 3550 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3551 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3552 | } |
| 3553 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3554 | goto onError; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3555 | |
| 3556 | /* Copy back the bytes variables, which might have been modified by the |
| 3557 | callback */ |
| 3558 | inputobj = PyUnicodeDecodeError_GetObject(*exceptionObject); |
| 3559 | if (!inputobj) |
| 3560 | goto onError; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3561 | if (!PyBytes_Check(inputobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3562 | PyErr_Format(PyExc_TypeError, "exception attribute object must be bytes"); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3563 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3564 | *input = PyBytes_AS_STRING(inputobj); |
| 3565 | insize = PyBytes_GET_SIZE(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3566 | *inend = *input + insize; |
Walter Dörwald | 36f938f | 2007-08-10 10:11:43 +0000 | [diff] [blame] | 3567 | /* we can DECREF safely, as the exception has another reference, |
| 3568 | so the object won't go away. */ |
| 3569 | Py_DECREF(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3570 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3571 | if (newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3572 | newpos = insize+newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 3573 | if (newpos<0 || newpos>insize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3574 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", newpos); |
| 3575 | goto onError; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 3576 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3577 | |
| 3578 | /* need more space? (at least enough for what we |
| 3579 | have+the replacement+the rest of the string (starting |
| 3580 | at the new input position), so we won't have to check space |
| 3581 | when there are no errors in the rest of the string) */ |
| 3582 | repptr = PyUnicode_AS_UNICODE(repunicode); |
| 3583 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 3584 | requiredsize = *outpos + repsize + insize-newpos; |
| 3585 | if (requiredsize > outsize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3586 | if (requiredsize<2*outsize) |
| 3587 | requiredsize = 2*outsize; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 3588 | if (PyUnicode_Resize((PyObject**)output, requiredsize) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3589 | goto onError; |
| 3590 | *outptr = PyUnicode_AS_UNICODE(*output) + *outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3591 | } |
| 3592 | *endinpos = newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3593 | *inptr = *input + newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3594 | Py_UNICODE_COPY(*outptr, repptr, repsize); |
| 3595 | *outptr += repsize; |
| 3596 | *outpos += repsize; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3597 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3598 | /* we made it! */ |
| 3599 | res = 0; |
| 3600 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3601 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3602 | Py_XDECREF(restuple); |
| 3603 | return res; |
| 3604 | } |
| 3605 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3606 | /* --- UTF-7 Codec -------------------------------------------------------- */ |
| 3607 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3608 | /* See RFC2152 for details. We encode conservatively and decode liberally. */ |
| 3609 | |
| 3610 | /* Three simple macros defining base-64. */ |
| 3611 | |
| 3612 | /* Is c a base-64 character? */ |
| 3613 | |
| 3614 | #define IS_BASE64(c) \ |
| 3615 | (((c) >= 'A' && (c) <= 'Z') || \ |
| 3616 | ((c) >= 'a' && (c) <= 'z') || \ |
| 3617 | ((c) >= '0' && (c) <= '9') || \ |
| 3618 | (c) == '+' || (c) == '/') |
| 3619 | |
| 3620 | /* given that c is a base-64 character, what is its base-64 value? */ |
| 3621 | |
| 3622 | #define FROM_BASE64(c) \ |
| 3623 | (((c) >= 'A' && (c) <= 'Z') ? (c) - 'A' : \ |
| 3624 | ((c) >= 'a' && (c) <= 'z') ? (c) - 'a' + 26 : \ |
| 3625 | ((c) >= '0' && (c) <= '9') ? (c) - '0' + 52 : \ |
| 3626 | (c) == '+' ? 62 : 63) |
| 3627 | |
| 3628 | /* What is the base-64 character of the bottom 6 bits of n? */ |
| 3629 | |
| 3630 | #define TO_BASE64(n) \ |
| 3631 | ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) |
| 3632 | |
| 3633 | /* DECODE_DIRECT: this byte encountered in a UTF-7 string should be |
| 3634 | * decoded as itself. We are permissive on decoding; the only ASCII |
| 3635 | * byte not decoding to itself is the + which begins a base64 |
| 3636 | * string. */ |
| 3637 | |
| 3638 | #define DECODE_DIRECT(c) \ |
| 3639 | ((c) <= 127 && (c) != '+') |
| 3640 | |
| 3641 | /* The UTF-7 encoder treats ASCII characters differently according to |
| 3642 | * whether they are Set D, Set O, Whitespace, or special (i.e. none of |
| 3643 | * the above). See RFC2152. This array identifies these different |
| 3644 | * sets: |
| 3645 | * 0 : "Set D" |
| 3646 | * alphanumeric and '(),-./:? |
| 3647 | * 1 : "Set O" |
| 3648 | * !"#$%&*;<=>@[]^_`{|} |
| 3649 | * 2 : "whitespace" |
| 3650 | * ht nl cr sp |
| 3651 | * 3 : special (must be base64 encoded) |
| 3652 | * everything else (i.e. +\~ and non-printing codes 0-8 11-12 14-31 127) |
| 3653 | */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3654 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3655 | static |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3656 | char utf7_category[128] = { |
| 3657 | /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */ |
| 3658 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 3, 3, 2, 3, 3, |
| 3659 | /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */ |
| 3660 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 3661 | /* sp ! " # $ % & ' ( ) * + , - . / */ |
| 3662 | 2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 3, 0, 0, 0, 0, |
| 3663 | /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */ |
| 3664 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, |
| 3665 | /* @ A B C D E F G H I J K L M N O */ |
| 3666 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 3667 | /* P Q R S T U V W X Y Z [ \ ] ^ _ */ |
| 3668 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 1, 1, 1, |
| 3669 | /* ` a b c d e f g h i j k l m n o */ |
| 3670 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 3671 | /* p q r s t u v w x y z { | } ~ del */ |
| 3672 | 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] | 3673 | }; |
| 3674 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3675 | /* ENCODE_DIRECT: this character should be encoded as itself. The |
| 3676 | * answer depends on whether we are encoding set O as itself, and also |
| 3677 | * on whether we are encoding whitespace as itself. RFC2152 makes it |
| 3678 | * clear that the answers to these questions vary between |
| 3679 | * applications, so this code needs to be flexible. */ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 3680 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3681 | #define ENCODE_DIRECT(c, directO, directWS) \ |
| 3682 | ((c) < 128 && (c) > 0 && \ |
| 3683 | ((utf7_category[(c)] == 0) || \ |
| 3684 | (directWS && (utf7_category[(c)] == 2)) || \ |
| 3685 | (directO && (utf7_category[(c)] == 1)))) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3686 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3687 | PyObject * |
| 3688 | PyUnicode_DecodeUTF7(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3689 | Py_ssize_t size, |
| 3690 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3691 | { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3692 | return PyUnicode_DecodeUTF7Stateful(s, size, errors, NULL); |
| 3693 | } |
| 3694 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3695 | /* The decoder. The only state we preserve is our read position, |
| 3696 | * i.e. how many characters we have consumed. So if we end in the |
| 3697 | * middle of a shift sequence we have to back off the read position |
| 3698 | * and the output to the beginning of the sequence, otherwise we lose |
| 3699 | * all the shift state (seen bits, number of bits seen, high |
| 3700 | * surrogate). */ |
| 3701 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3702 | PyObject * |
| 3703 | PyUnicode_DecodeUTF7Stateful(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3704 | Py_ssize_t size, |
| 3705 | const char *errors, |
| 3706 | Py_ssize_t *consumed) |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3707 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3708 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3709 | Py_ssize_t startinpos; |
| 3710 | Py_ssize_t endinpos; |
| 3711 | Py_ssize_t outpos; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3712 | const char *e; |
| 3713 | PyUnicodeObject *unicode; |
| 3714 | Py_UNICODE *p; |
| 3715 | const char *errmsg = ""; |
| 3716 | int inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3717 | Py_UNICODE *shiftOutStart; |
| 3718 | unsigned int base64bits = 0; |
| 3719 | unsigned long base64buffer = 0; |
| 3720 | Py_UNICODE surrogate = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3721 | PyObject *errorHandler = NULL; |
| 3722 | PyObject *exc = NULL; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3723 | |
| 3724 | unicode = _PyUnicode_New(size); |
| 3725 | if (!unicode) |
| 3726 | return NULL; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3727 | if (size == 0) { |
| 3728 | if (consumed) |
| 3729 | *consumed = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3730 | return (PyObject *)unicode; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3731 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3732 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3733 | p = PyUnicode_AS_UNICODE(unicode); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3734 | shiftOutStart = p; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3735 | e = s + size; |
| 3736 | |
| 3737 | while (s < e) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3738 | Py_UNICODE ch; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3739 | restart: |
Antoine Pitrou | 5ffd9e9 | 2008-07-25 18:05:24 +0000 | [diff] [blame] | 3740 | ch = (unsigned char) *s; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3741 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3742 | if (inShift) { /* in a base-64 section */ |
| 3743 | if (IS_BASE64(ch)) { /* consume a base-64 character */ |
| 3744 | base64buffer = (base64buffer << 6) | FROM_BASE64(ch); |
| 3745 | base64bits += 6; |
| 3746 | s++; |
| 3747 | if (base64bits >= 16) { |
| 3748 | /* we have enough bits for a UTF-16 value */ |
| 3749 | Py_UNICODE outCh = (Py_UNICODE) |
| 3750 | (base64buffer >> (base64bits-16)); |
| 3751 | base64bits -= 16; |
| 3752 | base64buffer &= (1 << base64bits) - 1; /* clear high bits */ |
| 3753 | if (surrogate) { |
| 3754 | /* expecting a second surrogate */ |
| 3755 | if (outCh >= 0xDC00 && outCh <= 0xDFFF) { |
| 3756 | #ifdef Py_UNICODE_WIDE |
| 3757 | *p++ = (((surrogate & 0x3FF)<<10) |
| 3758 | | (outCh & 0x3FF)) + 0x10000; |
| 3759 | #else |
| 3760 | *p++ = surrogate; |
| 3761 | *p++ = outCh; |
| 3762 | #endif |
| 3763 | surrogate = 0; |
| 3764 | } |
| 3765 | else { |
| 3766 | surrogate = 0; |
| 3767 | errmsg = "second surrogate missing"; |
| 3768 | goto utf7Error; |
| 3769 | } |
| 3770 | } |
| 3771 | else if (outCh >= 0xD800 && outCh <= 0xDBFF) { |
| 3772 | /* first surrogate */ |
| 3773 | surrogate = outCh; |
| 3774 | } |
| 3775 | else if (outCh >= 0xDC00 && outCh <= 0xDFFF) { |
| 3776 | errmsg = "unexpected second surrogate"; |
| 3777 | goto utf7Error; |
| 3778 | } |
| 3779 | else { |
| 3780 | *p++ = outCh; |
| 3781 | } |
| 3782 | } |
| 3783 | } |
| 3784 | else { /* now leaving a base-64 section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3785 | inShift = 0; |
| 3786 | s++; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3787 | if (surrogate) { |
| 3788 | errmsg = "second surrogate missing at end of shift sequence"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3789 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3790 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3791 | if (base64bits > 0) { /* left-over bits */ |
| 3792 | if (base64bits >= 6) { |
| 3793 | /* We've seen at least one base-64 character */ |
| 3794 | errmsg = "partial character in shift sequence"; |
| 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 | else { |
| 3798 | /* Some bits remain; they should be zero */ |
| 3799 | if (base64buffer != 0) { |
| 3800 | errmsg = "non-zero padding bits in shift sequence"; |
| 3801 | goto utf7Error; |
| 3802 | } |
| 3803 | } |
| 3804 | } |
| 3805 | if (ch != '-') { |
| 3806 | /* '-' is absorbed; other terminating |
| 3807 | characters are preserved */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3808 | *p++ = ch; |
| 3809 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3810 | } |
| 3811 | } |
| 3812 | else if ( ch == '+' ) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3813 | startinpos = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3814 | s++; /* consume '+' */ |
| 3815 | if (s < e && *s == '-') { /* '+-' encodes '+' */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3816 | s++; |
| 3817 | *p++ = '+'; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3818 | } |
| 3819 | else { /* begin base64-encoded section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3820 | inShift = 1; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3821 | shiftOutStart = p; |
| 3822 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3823 | } |
| 3824 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3825 | else if (DECODE_DIRECT(ch)) { /* character decodes as itself */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3826 | *p++ = ch; |
| 3827 | s++; |
| 3828 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3829 | else { |
| 3830 | startinpos = s-starts; |
| 3831 | s++; |
| 3832 | errmsg = "unexpected special character"; |
| 3833 | goto utf7Error; |
| 3834 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3835 | continue; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3836 | utf7Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3837 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 3838 | endinpos = s-starts; |
| 3839 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3840 | errors, &errorHandler, |
| 3841 | "utf7", errmsg, |
| 3842 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 3843 | &unicode, &outpos, &p)) |
| 3844 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3845 | } |
| 3846 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3847 | /* end of string */ |
| 3848 | |
| 3849 | if (inShift && !consumed) { /* in shift sequence, no more to follow */ |
| 3850 | /* if we're in an inconsistent state, that's an error */ |
| 3851 | if (surrogate || |
| 3852 | (base64bits >= 6) || |
| 3853 | (base64bits > 0 && base64buffer != 0)) { |
| 3854 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 3855 | endinpos = size; |
| 3856 | if (unicode_decode_call_errorhandler( |
| 3857 | errors, &errorHandler, |
| 3858 | "utf7", "unterminated shift sequence", |
| 3859 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 3860 | &unicode, &outpos, &p)) |
| 3861 | goto onError; |
| 3862 | if (s < e) |
| 3863 | goto restart; |
| 3864 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3865 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3866 | |
| 3867 | /* return state */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3868 | if (consumed) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3869 | if (inShift) { |
| 3870 | p = shiftOutStart; /* back off output */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3871 | *consumed = startinpos; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3872 | } |
| 3873 | else { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3874 | *consumed = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3875 | } |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3876 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3877 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 3878 | if (PyUnicode_Resize((PyObject**)&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3879 | goto onError; |
| 3880 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3881 | Py_XDECREF(errorHandler); |
| 3882 | Py_XDECREF(exc); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 3883 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 3884 | if (_PyUnicode_READY_REPLACE(&unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3885 | Py_DECREF(unicode); |
| 3886 | return NULL; |
| 3887 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 3888 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 3889 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3890 | return (PyObject *)unicode; |
| 3891 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3892 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3893 | Py_XDECREF(errorHandler); |
| 3894 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3895 | Py_DECREF(unicode); |
| 3896 | return NULL; |
| 3897 | } |
| 3898 | |
| 3899 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3900 | PyObject * |
| 3901 | PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3902 | Py_ssize_t size, |
| 3903 | int base64SetO, |
| 3904 | int base64WhiteSpace, |
| 3905 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3906 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3907 | PyObject *v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3908 | /* It might be possible to tighten this worst case */ |
Alexandre Vassalotti | e85bd98 | 2009-07-21 00:39:03 +0000 | [diff] [blame] | 3909 | Py_ssize_t allocated = 8 * size; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3910 | int inShift = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3911 | Py_ssize_t i = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3912 | unsigned int base64bits = 0; |
| 3913 | unsigned long base64buffer = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3914 | char * out; |
| 3915 | char * start; |
| 3916 | |
| 3917 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3918 | return PyBytes_FromStringAndSize(NULL, 0); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3919 | |
Alexandre Vassalotti | e85bd98 | 2009-07-21 00:39:03 +0000 | [diff] [blame] | 3920 | if (allocated / 8 != size) |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3921 | return PyErr_NoMemory(); |
| 3922 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3923 | v = PyBytes_FromStringAndSize(NULL, allocated); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3924 | if (v == NULL) |
| 3925 | return NULL; |
| 3926 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3927 | start = out = PyBytes_AS_STRING(v); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3928 | for (;i < size; ++i) { |
| 3929 | Py_UNICODE ch = s[i]; |
| 3930 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3931 | if (inShift) { |
| 3932 | if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 3933 | /* shifting out */ |
| 3934 | if (base64bits) { /* output remaining bits */ |
| 3935 | *out++ = TO_BASE64(base64buffer << (6-base64bits)); |
| 3936 | base64buffer = 0; |
| 3937 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3938 | } |
| 3939 | inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3940 | /* Characters not in the BASE64 set implicitly unshift the sequence |
| 3941 | so no '-' is required, except if the character is itself a '-' */ |
| 3942 | if (IS_BASE64(ch) || ch == '-') { |
| 3943 | *out++ = '-'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3944 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3945 | *out++ = (char) ch; |
| 3946 | } |
| 3947 | else { |
| 3948 | goto encode_char; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3949 | } |
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 | else { /* not in a shift sequence */ |
| 3952 | if (ch == '+') { |
| 3953 | *out++ = '+'; |
| 3954 | *out++ = '-'; |
| 3955 | } |
| 3956 | else if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 3957 | *out++ = (char) ch; |
| 3958 | } |
| 3959 | else { |
| 3960 | *out++ = '+'; |
| 3961 | inShift = 1; |
| 3962 | goto encode_char; |
| 3963 | } |
| 3964 | } |
| 3965 | continue; |
| 3966 | encode_char: |
| 3967 | #ifdef Py_UNICODE_WIDE |
| 3968 | if (ch >= 0x10000) { |
| 3969 | /* code first surrogate */ |
| 3970 | base64bits += 16; |
| 3971 | base64buffer = (base64buffer << 16) | 0xd800 | ((ch-0x10000) >> 10); |
| 3972 | while (base64bits >= 6) { |
| 3973 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 3974 | base64bits -= 6; |
| 3975 | } |
| 3976 | /* prepare second surrogate */ |
| 3977 | ch = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 3978 | } |
| 3979 | #endif |
| 3980 | base64bits += 16; |
| 3981 | base64buffer = (base64buffer << 16) | ch; |
| 3982 | while (base64bits >= 6) { |
| 3983 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 3984 | base64bits -= 6; |
| 3985 | } |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 3986 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3987 | if (base64bits) |
| 3988 | *out++= TO_BASE64(base64buffer << (6-base64bits) ); |
| 3989 | if (inShift) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3990 | *out++ = '-'; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3991 | if (_PyBytes_Resize(&v, out - start) < 0) |
| 3992 | return NULL; |
| 3993 | return v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3994 | } |
| 3995 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3996 | #undef IS_BASE64 |
| 3997 | #undef FROM_BASE64 |
| 3998 | #undef TO_BASE64 |
| 3999 | #undef DECODE_DIRECT |
| 4000 | #undef ENCODE_DIRECT |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4001 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4002 | /* --- UTF-8 Codec -------------------------------------------------------- */ |
| 4003 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4004 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4005 | char utf8_code_length[256] = { |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4006 | /* Map UTF-8 encoded prefix byte to sequence length. Zero means |
| 4007 | illegal prefix. See RFC 3629 for details */ |
| 4008 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 00-0F */ |
| 4009 | 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] | 4010 | 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] | 4011 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 4012 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 4013 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 4014 | 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] | 4015 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 70-7F */ |
| 4016 | 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] | 4017 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4018 | 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] | 4019 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* B0-BF */ |
| 4020 | 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* C0-C1 + C2-CF */ |
| 4021 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* D0-DF */ |
| 4022 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* E0-EF */ |
| 4023 | 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] | 4024 | }; |
| 4025 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4026 | PyObject * |
| 4027 | PyUnicode_DecodeUTF8(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4028 | Py_ssize_t size, |
| 4029 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4030 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4031 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
| 4032 | } |
| 4033 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4034 | /* Mask to check or force alignment of a pointer to C 'long' boundaries */ |
| 4035 | #define LONG_PTR_MASK (size_t) (SIZEOF_LONG - 1) |
| 4036 | |
| 4037 | /* Mask to quickly check whether a C 'long' contains a |
| 4038 | non-ASCII, UTF8-encoded char. */ |
| 4039 | #if (SIZEOF_LONG == 8) |
| 4040 | # define ASCII_CHAR_MASK 0x8080808080808080L |
| 4041 | #elif (SIZEOF_LONG == 4) |
| 4042 | # define ASCII_CHAR_MASK 0x80808080L |
| 4043 | #else |
| 4044 | # error C 'long' size should be either 4 or 8! |
| 4045 | #endif |
| 4046 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4047 | /* Scans a UTF-8 string and returns the maximum character to be expected, |
| 4048 | the size of the decoded unicode string and if any major errors were |
| 4049 | encountered. |
| 4050 | |
| 4051 | This function does check basic UTF-8 sanity, it does however NOT CHECK |
| 4052 | if the string contains surrogates, and if all continuation bytes are |
| 4053 | within the correct ranges, these checks are performed in |
| 4054 | PyUnicode_DecodeUTF8Stateful. |
| 4055 | |
| 4056 | If it sets has_errors to 1, it means the value of unicode_size and max_char |
| 4057 | will be bogus and you should not rely on useful information in them. |
| 4058 | */ |
| 4059 | static Py_UCS4 |
| 4060 | utf8_max_char_size_and_has_errors(const char *s, Py_ssize_t string_size, |
| 4061 | Py_ssize_t *unicode_size, Py_ssize_t* consumed, |
| 4062 | int *has_errors) |
| 4063 | { |
| 4064 | Py_ssize_t n; |
| 4065 | Py_ssize_t char_count = 0; |
| 4066 | Py_UCS4 max_char = 127, new_max; |
| 4067 | Py_UCS4 upper_bound; |
| 4068 | const unsigned char *p = (const unsigned char *)s; |
| 4069 | const unsigned char *end = p + string_size; |
| 4070 | const unsigned char *aligned_end = (const unsigned char *) ((size_t) end & ~LONG_PTR_MASK); |
| 4071 | int err = 0; |
| 4072 | |
| 4073 | for (; p < end && !err; ++p, ++char_count) { |
| 4074 | /* Only check value if it's not a ASCII char... */ |
| 4075 | if (*p < 0x80) { |
| 4076 | /* Fast path, see below in PyUnicode_DecodeUTF8Stateful for |
| 4077 | an explanation. */ |
| 4078 | if (!((size_t) p & LONG_PTR_MASK)) { |
| 4079 | /* Help register allocation */ |
| 4080 | register const unsigned char *_p = p; |
| 4081 | while (_p < aligned_end) { |
| 4082 | unsigned long value = *(unsigned long *) _p; |
| 4083 | if (value & ASCII_CHAR_MASK) |
| 4084 | break; |
| 4085 | _p += SIZEOF_LONG; |
| 4086 | char_count += SIZEOF_LONG; |
| 4087 | } |
| 4088 | p = _p; |
| 4089 | if (p == end) |
| 4090 | break; |
| 4091 | } |
| 4092 | } |
| 4093 | if (*p >= 0x80) { |
| 4094 | n = utf8_code_length[*p]; |
| 4095 | new_max = max_char; |
| 4096 | switch (n) { |
| 4097 | /* invalid start byte */ |
| 4098 | case 0: |
| 4099 | err = 1; |
| 4100 | break; |
| 4101 | case 2: |
| 4102 | /* Code points between 0x00FF and 0x07FF inclusive. |
| 4103 | Approximate the upper bound of the code point, |
| 4104 | if this flips over 255 we can be sure it will be more |
| 4105 | than 255 and the string will need 2 bytes per code coint, |
| 4106 | if it stays under or equal to 255, we can be sure 1 byte |
| 4107 | is enough. |
| 4108 | ((*p & 0b00011111) << 6) | 0b00111111 */ |
| 4109 | upper_bound = ((*p & 0x1F) << 6) | 0x3F; |
| 4110 | if (max_char < upper_bound) |
| 4111 | new_max = upper_bound; |
| 4112 | /* Ensure we track at least that we left ASCII space. */ |
| 4113 | if (new_max < 128) |
| 4114 | new_max = 128; |
| 4115 | break; |
| 4116 | case 3: |
| 4117 | /* Between 0x0FFF and 0xFFFF inclusive, so values are |
| 4118 | always > 255 and <= 65535 and will always need 2 bytes. */ |
| 4119 | if (max_char < 65535) |
| 4120 | new_max = 65535; |
| 4121 | break; |
| 4122 | case 4: |
| 4123 | /* Code point will be above 0xFFFF for sure in this case. */ |
| 4124 | new_max = 65537; |
| 4125 | break; |
| 4126 | /* Internal error, this should be caught by the first if */ |
| 4127 | case 1: |
| 4128 | default: |
| 4129 | assert(0 && "Impossible case in utf8_max_char_and_size"); |
| 4130 | err = 1; |
| 4131 | } |
| 4132 | /* Instead of number of overall bytes for this code point, |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 4133 | n contains the number of following bytes: */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4134 | --n; |
| 4135 | /* Check if the follow up chars are all valid continuation bytes */ |
| 4136 | if (n >= 1) { |
| 4137 | const unsigned char *cont; |
| 4138 | if ((p + n) >= end) { |
| 4139 | if (consumed == 0) |
| 4140 | /* incomplete data, non-incremental decoding */ |
| 4141 | err = 1; |
| 4142 | break; |
| 4143 | } |
| 4144 | for (cont = p + 1; cont < (p + n); ++cont) { |
| 4145 | if ((*cont & 0xc0) != 0x80) { |
| 4146 | err = 1; |
| 4147 | break; |
| 4148 | } |
| 4149 | } |
| 4150 | p += n; |
| 4151 | } |
| 4152 | else |
| 4153 | err = 1; |
| 4154 | max_char = new_max; |
| 4155 | } |
| 4156 | } |
| 4157 | |
| 4158 | if (unicode_size) |
| 4159 | *unicode_size = char_count; |
| 4160 | if (has_errors) |
| 4161 | *has_errors = err; |
| 4162 | return max_char; |
| 4163 | } |
| 4164 | |
| 4165 | /* Similar to PyUnicode_WRITE but can also write into wstr field |
| 4166 | of the legacy unicode representation */ |
| 4167 | #define WRITE_FLEXIBLE_OR_WSTR(kind, buf, index, value) \ |
| 4168 | do { \ |
| 4169 | const int k_ = (kind); \ |
| 4170 | if (k_ == PyUnicode_WCHAR_KIND) \ |
| 4171 | ((Py_UNICODE *)(buf))[(index)] = (Py_UNICODE)(value); \ |
| 4172 | else if (k_ == PyUnicode_1BYTE_KIND) \ |
| 4173 | ((unsigned char *)(buf))[(index)] = (unsigned char)(value); \ |
| 4174 | else if (k_ == PyUnicode_2BYTE_KIND) \ |
| 4175 | ((Py_UCS2 *)(buf))[(index)] = (Py_UCS2)(value); \ |
| 4176 | else \ |
| 4177 | ((Py_UCS4 *)(buf))[(index)] = (Py_UCS4)(value); \ |
| 4178 | } while (0) |
| 4179 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4180 | PyObject * |
| 4181 | PyUnicode_DecodeUTF8Stateful(const char *s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4182 | Py_ssize_t size, |
| 4183 | const char *errors, |
| 4184 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4185 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4186 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4187 | int n; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4188 | int k; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4189 | Py_ssize_t startinpos; |
| 4190 | Py_ssize_t endinpos; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4191 | const char *e, *aligned_end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4192 | PyUnicodeObject *unicode; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4193 | const char *errmsg = ""; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4194 | PyObject *errorHandler = NULL; |
| 4195 | PyObject *exc = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4196 | Py_UCS4 maxchar = 0; |
| 4197 | Py_ssize_t unicode_size; |
| 4198 | Py_ssize_t i; |
| 4199 | int kind; |
| 4200 | void *data; |
| 4201 | int has_errors; |
| 4202 | Py_UNICODE *error_outptr; |
| 4203 | #if SIZEOF_WCHAR_T == 2 |
| 4204 | Py_ssize_t wchar_offset = 0; |
| 4205 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4206 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4207 | if (size == 0) { |
| 4208 | if (consumed) |
| 4209 | *consumed = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4210 | return (PyObject *)PyUnicode_New(0, 0); |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4211 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4212 | maxchar = utf8_max_char_size_and_has_errors(s, size, &unicode_size, |
| 4213 | consumed, &has_errors); |
| 4214 | if (has_errors) { |
| 4215 | unicode = _PyUnicode_New(size); |
| 4216 | if (!unicode) |
| 4217 | return NULL; |
| 4218 | kind = PyUnicode_WCHAR_KIND; |
| 4219 | data = PyUnicode_AS_UNICODE(unicode); |
| 4220 | assert(data != NULL); |
| 4221 | } |
| 4222 | else { |
| 4223 | unicode = (PyUnicodeObject *)PyUnicode_New(unicode_size, maxchar); |
| 4224 | if (!unicode) |
| 4225 | return NULL; |
| 4226 | /* When the string is ASCII only, just use memcpy and return. |
| 4227 | unicode_size may be != size if there is an incomplete UTF-8 |
| 4228 | sequence at the end of the ASCII block. */ |
| 4229 | if (maxchar < 128 && size == unicode_size) { |
| 4230 | Py_MEMCPY(PyUnicode_1BYTE_DATA(unicode), s, unicode_size); |
| 4231 | return (PyObject *)unicode; |
| 4232 | } |
| 4233 | kind = PyUnicode_KIND(unicode); |
| 4234 | data = PyUnicode_DATA(unicode); |
| 4235 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4236 | /* Unpack UTF-8 encoded data */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4237 | i = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4238 | e = s + size; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4239 | aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4240 | |
| 4241 | while (s < e) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 4242 | Py_UCS4 ch = (unsigned char)*s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4243 | |
| 4244 | if (ch < 0x80) { |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4245 | /* Fast path for runs of ASCII characters. Given that common UTF-8 |
| 4246 | input will consist of an overwhelming majority of ASCII |
| 4247 | characters, we try to optimize for this case by checking |
| 4248 | as many characters as a C 'long' can contain. |
| 4249 | First, check if we can do an aligned read, as most CPUs have |
| 4250 | a penalty for unaligned reads. |
| 4251 | */ |
| 4252 | if (!((size_t) s & LONG_PTR_MASK)) { |
| 4253 | /* Help register allocation */ |
| 4254 | register const char *_s = s; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4255 | register Py_ssize_t _i = i; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4256 | while (_s < aligned_end) { |
| 4257 | /* Read a whole long at a time (either 4 or 8 bytes), |
| 4258 | and do a fast unrolled copy if it only contains ASCII |
| 4259 | characters. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4260 | unsigned long value = *(unsigned long *) _s; |
| 4261 | if (value & ASCII_CHAR_MASK) |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4262 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4263 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+0, _s[0]); |
| 4264 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+1, _s[1]); |
| 4265 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+2, _s[2]); |
| 4266 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+3, _s[3]); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4267 | #if (SIZEOF_LONG == 8) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4268 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+4, _s[4]); |
| 4269 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+5, _s[5]); |
| 4270 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+6, _s[6]); |
| 4271 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+7, _s[7]); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4272 | #endif |
| 4273 | _s += SIZEOF_LONG; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4274 | _i += SIZEOF_LONG; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4275 | } |
| 4276 | s = _s; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4277 | i = _i; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4278 | if (s == e) |
| 4279 | break; |
| 4280 | ch = (unsigned char)*s; |
| 4281 | } |
| 4282 | } |
| 4283 | |
| 4284 | if (ch < 0x80) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4285 | WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4286 | s++; |
| 4287 | continue; |
| 4288 | } |
| 4289 | |
| 4290 | n = utf8_code_length[ch]; |
| 4291 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4292 | if (s + n > e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4293 | if (consumed) |
| 4294 | break; |
| 4295 | else { |
| 4296 | errmsg = "unexpected end of data"; |
| 4297 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4298 | endinpos = startinpos+1; |
| 4299 | for (k=1; (k < size-startinpos) && ((s[k]&0xC0) == 0x80); k++) |
| 4300 | endinpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4301 | goto utf8Error; |
| 4302 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4303 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4304 | |
| 4305 | switch (n) { |
| 4306 | |
| 4307 | case 0: |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4308 | errmsg = "invalid start byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4309 | startinpos = s-starts; |
| 4310 | endinpos = startinpos+1; |
| 4311 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4312 | |
| 4313 | case 1: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4314 | errmsg = "internal error"; |
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 2: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4320 | if ((s[1] & 0xc0) != 0x80) { |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4321 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4322 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4323 | endinpos = startinpos + 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4324 | goto utf8Error; |
| 4325 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4326 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4327 | assert ((ch > 0x007F) && (ch <= 0x07FF)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4328 | WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4329 | break; |
| 4330 | |
| 4331 | case 3: |
Ezio Melotti | 9bf2b3a | 2010-07-03 04:52:19 +0000 | [diff] [blame] | 4332 | /* Decoding UTF-8 sequences in range \xed\xa0\x80-\xed\xbf\xbf |
| 4333 | will result in surrogates in range d800-dfff. Surrogates are |
| 4334 | not valid UTF-8 so they are rejected. |
| 4335 | See http://www.unicode.org/versions/Unicode5.2.0/ch03.pdf |
| 4336 | (table 3-7) and http://www.rfc-editor.org/rfc/rfc3629.txt */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4337 | if ((s[1] & 0xc0) != 0x80 || |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4338 | (s[2] & 0xc0) != 0x80 || |
| 4339 | ((unsigned char)s[0] == 0xE0 && |
| 4340 | (unsigned char)s[1] < 0xA0) || |
| 4341 | ((unsigned char)s[0] == 0xED && |
| 4342 | (unsigned char)s[1] > 0x9F)) { |
| 4343 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4344 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4345 | endinpos = startinpos + 1; |
| 4346 | |
| 4347 | /* if s[1] first two bits are 1 and 0, then the invalid |
| 4348 | continuation byte is s[2], so increment endinpos by 1, |
| 4349 | if not, s[1] is invalid and endinpos doesn't need to |
| 4350 | be incremented. */ |
| 4351 | if ((s[1] & 0xC0) == 0x80) |
| 4352 | endinpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4353 | goto utf8Error; |
| 4354 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4355 | ch = ((s[0] & 0x0f) << 12) + ((s[1] & 0x3f) << 6) + (s[2] & 0x3f); |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4356 | assert ((ch > 0x07FF) && (ch <= 0xFFFF)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4357 | WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, ch); |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 4358 | break; |
| 4359 | |
| 4360 | case 4: |
| 4361 | if ((s[1] & 0xc0) != 0x80 || |
| 4362 | (s[2] & 0xc0) != 0x80 || |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4363 | (s[3] & 0xc0) != 0x80 || |
| 4364 | ((unsigned char)s[0] == 0xF0 && |
| 4365 | (unsigned char)s[1] < 0x90) || |
| 4366 | ((unsigned char)s[0] == 0xF4 && |
| 4367 | (unsigned char)s[1] > 0x8F)) { |
| 4368 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4369 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4370 | endinpos = startinpos + 1; |
| 4371 | if ((s[1] & 0xC0) == 0x80) { |
| 4372 | endinpos++; |
| 4373 | if ((s[2] & 0xC0) == 0x80) |
| 4374 | endinpos++; |
| 4375 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4376 | goto utf8Error; |
| 4377 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 4378 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4379 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
| 4380 | assert ((ch > 0xFFFF) && (ch <= 0x10ffff)); |
| 4381 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4382 | /* If the string is flexible or we have native UCS-4, write |
| 4383 | directly.. */ |
| 4384 | if (sizeof(Py_UNICODE) > 2 || kind != PyUnicode_WCHAR_KIND) |
| 4385 | WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, ch); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4386 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4387 | else { |
| 4388 | /* compute and append the two surrogates: */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4389 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4390 | /* translate from 10000..10FFFF to 0..FFFF */ |
| 4391 | ch -= 0x10000; |
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 | /* high surrogate = top 10 bits added to D800 */ |
| 4394 | WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, |
| 4395 | (Py_UNICODE)(0xD800 + (ch >> 10))); |
| 4396 | |
| 4397 | /* low surrogate = bottom 10 bits added to DC00 */ |
| 4398 | WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, |
| 4399 | (Py_UNICODE)(0xDC00 + (ch & 0x03FF))); |
| 4400 | } |
| 4401 | #if SIZEOF_WCHAR_T == 2 |
| 4402 | wchar_offset++; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 4403 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4404 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4405 | } |
| 4406 | s += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4407 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4408 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4409 | utf8Error: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4410 | /* If this is not yet a resizable string, make it one.. */ |
| 4411 | if (kind != PyUnicode_WCHAR_KIND) { |
| 4412 | const Py_UNICODE *u; |
| 4413 | PyUnicodeObject *new_unicode = _PyUnicode_New(size); |
| 4414 | if (!new_unicode) |
| 4415 | goto onError; |
| 4416 | u = PyUnicode_AsUnicode((PyObject *)unicode); |
| 4417 | if (!u) |
| 4418 | goto onError; |
| 4419 | #if SIZEOF_WCHAR_T == 2 |
| 4420 | i += wchar_offset; |
| 4421 | #endif |
| 4422 | Py_UNICODE_COPY(PyUnicode_AS_UNICODE(new_unicode), u, i); |
| 4423 | Py_DECREF(unicode); |
| 4424 | unicode = new_unicode; |
| 4425 | kind = 0; |
| 4426 | data = PyUnicode_AS_UNICODE(new_unicode); |
| 4427 | assert(data != NULL); |
| 4428 | } |
| 4429 | error_outptr = PyUnicode_AS_UNICODE(unicode) + i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4430 | if (unicode_decode_call_errorhandler( |
| 4431 | errors, &errorHandler, |
| 4432 | "utf8", errmsg, |
| 4433 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4434 | &unicode, &i, &error_outptr)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4435 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4436 | /* Update data because unicode_decode_call_errorhandler might have |
| 4437 | re-created or resized the unicode object. */ |
| 4438 | data = PyUnicode_AS_UNICODE(unicode); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4439 | aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4440 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4441 | /* Ensure the unicode_size calculation above was correct: */ |
| 4442 | assert(kind == PyUnicode_WCHAR_KIND || i == unicode_size); |
| 4443 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4444 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4445 | *consumed = s-starts; |
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 | /* Adjust length and ready string when it contained errors and |
| 4448 | is of the old resizable kind. */ |
| 4449 | if (kind == PyUnicode_WCHAR_KIND) { |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 4450 | if (PyUnicode_Resize((PyObject**)&unicode, i) < 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4451 | goto onError; |
| 4452 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4453 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4454 | Py_XDECREF(errorHandler); |
| 4455 | Py_XDECREF(exc); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 4456 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 4457 | if (_PyUnicode_READY_REPLACE(&unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4458 | Py_DECREF(unicode); |
| 4459 | return NULL; |
| 4460 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 4461 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 4462 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4463 | return (PyObject *)unicode; |
| 4464 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4465 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4466 | Py_XDECREF(errorHandler); |
| 4467 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4468 | Py_DECREF(unicode); |
| 4469 | return NULL; |
| 4470 | } |
| 4471 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4472 | #undef WRITE_FLEXIBLE_OR_WSTR |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4473 | |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4474 | #ifdef __APPLE__ |
| 4475 | |
| 4476 | /* Simplified UTF-8 decoder using surrogateescape error handler, |
| 4477 | used to decode the command line arguments on Mac OS X. */ |
| 4478 | |
| 4479 | wchar_t* |
| 4480 | _Py_DecodeUTF8_surrogateescape(const char *s, Py_ssize_t size) |
| 4481 | { |
| 4482 | int n; |
| 4483 | const char *e; |
| 4484 | wchar_t *unicode, *p; |
| 4485 | |
| 4486 | /* Note: size will always be longer than the resulting Unicode |
| 4487 | character count */ |
| 4488 | if (PY_SSIZE_T_MAX / sizeof(wchar_t) < (size + 1)) { |
| 4489 | PyErr_NoMemory(); |
| 4490 | return NULL; |
| 4491 | } |
| 4492 | unicode = PyMem_Malloc((size + 1) * sizeof(wchar_t)); |
| 4493 | if (!unicode) |
| 4494 | return NULL; |
| 4495 | |
| 4496 | /* Unpack UTF-8 encoded data */ |
| 4497 | p = unicode; |
| 4498 | e = s + size; |
| 4499 | while (s < e) { |
| 4500 | Py_UCS4 ch = (unsigned char)*s; |
| 4501 | |
| 4502 | if (ch < 0x80) { |
| 4503 | *p++ = (wchar_t)ch; |
| 4504 | s++; |
| 4505 | continue; |
| 4506 | } |
| 4507 | |
| 4508 | n = utf8_code_length[ch]; |
| 4509 | if (s + n > e) { |
| 4510 | goto surrogateescape; |
| 4511 | } |
| 4512 | |
| 4513 | switch (n) { |
| 4514 | case 0: |
| 4515 | case 1: |
| 4516 | goto surrogateescape; |
| 4517 | |
| 4518 | case 2: |
| 4519 | if ((s[1] & 0xc0) != 0x80) |
| 4520 | goto surrogateescape; |
| 4521 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
| 4522 | assert ((ch > 0x007F) && (ch <= 0x07FF)); |
| 4523 | *p++ = (wchar_t)ch; |
| 4524 | break; |
| 4525 | |
| 4526 | case 3: |
| 4527 | /* Decoding UTF-8 sequences in range \xed\xa0\x80-\xed\xbf\xbf |
| 4528 | will result in surrogates in range d800-dfff. Surrogates are |
| 4529 | not valid UTF-8 so they are rejected. |
| 4530 | See http://www.unicode.org/versions/Unicode5.2.0/ch03.pdf |
| 4531 | (table 3-7) and http://www.rfc-editor.org/rfc/rfc3629.txt */ |
| 4532 | if ((s[1] & 0xc0) != 0x80 || |
| 4533 | (s[2] & 0xc0) != 0x80 || |
| 4534 | ((unsigned char)s[0] == 0xE0 && |
| 4535 | (unsigned char)s[1] < 0xA0) || |
| 4536 | ((unsigned char)s[0] == 0xED && |
| 4537 | (unsigned char)s[1] > 0x9F)) { |
| 4538 | |
| 4539 | goto surrogateescape; |
| 4540 | } |
| 4541 | ch = ((s[0] & 0x0f) << 12) + ((s[1] & 0x3f) << 6) + (s[2] & 0x3f); |
| 4542 | assert ((ch > 0x07FF) && (ch <= 0xFFFF)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4543 | *p++ = (wchar_t)ch; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4544 | break; |
| 4545 | |
| 4546 | case 4: |
| 4547 | if ((s[1] & 0xc0) != 0x80 || |
| 4548 | (s[2] & 0xc0) != 0x80 || |
| 4549 | (s[3] & 0xc0) != 0x80 || |
| 4550 | ((unsigned char)s[0] == 0xF0 && |
| 4551 | (unsigned char)s[1] < 0x90) || |
| 4552 | ((unsigned char)s[0] == 0xF4 && |
| 4553 | (unsigned char)s[1] > 0x8F)) { |
| 4554 | goto surrogateescape; |
| 4555 | } |
| 4556 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
| 4557 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
| 4558 | assert ((ch > 0xFFFF) && (ch <= 0x10ffff)); |
| 4559 | |
| 4560 | #if SIZEOF_WCHAR_T == 4 |
| 4561 | *p++ = (wchar_t)ch; |
| 4562 | #else |
| 4563 | /* compute and append the two surrogates: */ |
| 4564 | |
| 4565 | /* translate from 10000..10FFFF to 0..FFFF */ |
| 4566 | ch -= 0x10000; |
| 4567 | |
| 4568 | /* high surrogate = top 10 bits added to D800 */ |
| 4569 | *p++ = (wchar_t)(0xD800 + (ch >> 10)); |
| 4570 | |
| 4571 | /* low surrogate = bottom 10 bits added to DC00 */ |
| 4572 | *p++ = (wchar_t)(0xDC00 + (ch & 0x03FF)); |
| 4573 | #endif |
| 4574 | break; |
| 4575 | } |
| 4576 | s += n; |
| 4577 | continue; |
| 4578 | |
| 4579 | surrogateescape: |
| 4580 | *p++ = 0xDC00 + ch; |
| 4581 | s++; |
| 4582 | } |
| 4583 | *p = L'\0'; |
| 4584 | return unicode; |
| 4585 | } |
| 4586 | |
| 4587 | #endif /* __APPLE__ */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4588 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4589 | /* Primary internal function which creates utf8 encoded bytes objects. |
| 4590 | |
| 4591 | Allocation strategy: if the string is short, convert into a stack buffer |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4592 | and allocate exactly as much space needed at the end. Else allocate the |
| 4593 | maximum possible needed (4 result bytes per Unicode character), and return |
| 4594 | the excess memory at the end. |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4595 | */ |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 4596 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4597 | _PyUnicode_AsUTF8String(PyObject *obj, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4598 | { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4599 | #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] | 4600 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4601 | Py_ssize_t i; /* index into s of next input byte */ |
| 4602 | PyObject *result; /* result string object */ |
| 4603 | char *p; /* next free byte in output buffer */ |
| 4604 | Py_ssize_t nallocated; /* number of result bytes allocated */ |
| 4605 | Py_ssize_t nneeded; /* number of result bytes needed */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4606 | char stackbuf[MAX_SHORT_UNICHARS * 4]; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4607 | PyObject *errorHandler = NULL; |
| 4608 | PyObject *exc = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4609 | int kind; |
| 4610 | void *data; |
| 4611 | Py_ssize_t size; |
| 4612 | PyUnicodeObject *unicode = (PyUnicodeObject *)obj; |
| 4613 | #if SIZEOF_WCHAR_T == 2 |
| 4614 | Py_ssize_t wchar_offset = 0; |
| 4615 | #endif |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 4616 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4617 | if (!PyUnicode_Check(unicode)) { |
| 4618 | PyErr_BadArgument(); |
| 4619 | return NULL; |
| 4620 | } |
| 4621 | |
| 4622 | if (PyUnicode_READY(unicode) == -1) |
| 4623 | return NULL; |
| 4624 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 4625 | if (PyUnicode_UTF8(unicode)) |
| 4626 | return PyBytes_FromStringAndSize(PyUnicode_UTF8(unicode), |
| 4627 | PyUnicode_UTF8_LENGTH(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4628 | |
| 4629 | kind = PyUnicode_KIND(unicode); |
| 4630 | data = PyUnicode_DATA(unicode); |
| 4631 | size = PyUnicode_GET_LENGTH(unicode); |
| 4632 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4633 | assert(size >= 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4634 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4635 | if (size <= MAX_SHORT_UNICHARS) { |
| 4636 | /* Write into the stack buffer; nallocated can't overflow. |
| 4637 | * At the end, we'll allocate exactly as much heap space as it |
| 4638 | * turns out we need. |
| 4639 | */ |
| 4640 | nallocated = Py_SAFE_DOWNCAST(sizeof(stackbuf), size_t, int); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4641 | result = NULL; /* will allocate after we're done */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4642 | p = stackbuf; |
| 4643 | } |
| 4644 | else { |
| 4645 | /* Overallocate on the heap, and give the excess back at the end. */ |
| 4646 | nallocated = size * 4; |
| 4647 | if (nallocated / 4 != size) /* overflow! */ |
| 4648 | return PyErr_NoMemory(); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4649 | result = PyBytes_FromStringAndSize(NULL, nallocated); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4650 | if (result == NULL) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4651 | return NULL; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4652 | p = PyBytes_AS_STRING(result); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4653 | } |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4654 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4655 | for (i = 0; i < size;) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4656 | Py_UCS4 ch = PyUnicode_READ(kind, data, i++); |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 4657 | |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4658 | if (ch < 0x80) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4659 | /* Encode ASCII */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4660 | *p++ = (char) ch; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 4661 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4662 | else if (ch < 0x0800) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4663 | /* Encode Latin-1 */ |
Marc-André Lemburg | dc724d6 | 2002-02-06 18:20:19 +0000 | [diff] [blame] | 4664 | *p++ = (char)(0xc0 | (ch >> 6)); |
| 4665 | *p++ = (char)(0x80 | (ch & 0x3f)); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4666 | } else if (0xD800 <= ch && ch <= 0xDFFF) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4667 | Py_ssize_t newpos; |
| 4668 | PyObject *rep; |
| 4669 | Py_ssize_t repsize, k, startpos; |
| 4670 | startpos = i-1; |
| 4671 | #if SIZEOF_WCHAR_T == 2 |
| 4672 | startpos += wchar_offset; |
Victor Stinner | 445a623 | 2010-04-22 20:01:57 +0000 | [diff] [blame] | 4673 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4674 | rep = unicode_encode_call_errorhandler( |
| 4675 | errors, &errorHandler, "utf-8", "surrogates not allowed", |
| 4676 | PyUnicode_AS_UNICODE(unicode), PyUnicode_GET_SIZE(unicode), |
| 4677 | &exc, startpos, startpos+1, &newpos); |
| 4678 | if (!rep) |
| 4679 | goto error; |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4680 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4681 | if (PyBytes_Check(rep)) |
| 4682 | repsize = PyBytes_GET_SIZE(rep); |
| 4683 | else |
| 4684 | repsize = PyUnicode_GET_SIZE(rep); |
| 4685 | |
| 4686 | if (repsize > 4) { |
| 4687 | Py_ssize_t offset; |
| 4688 | |
| 4689 | if (result == NULL) |
| 4690 | offset = p - stackbuf; |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4691 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4692 | offset = p - PyBytes_AS_STRING(result); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4693 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4694 | if (nallocated > PY_SSIZE_T_MAX - repsize + 4) { |
| 4695 | /* integer overflow */ |
| 4696 | PyErr_NoMemory(); |
| 4697 | goto error; |
| 4698 | } |
| 4699 | nallocated += repsize - 4; |
| 4700 | if (result != NULL) { |
| 4701 | if (_PyBytes_Resize(&result, nallocated) < 0) |
| 4702 | goto error; |
| 4703 | } else { |
| 4704 | result = PyBytes_FromStringAndSize(NULL, nallocated); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4705 | if (result == NULL) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4706 | goto error; |
| 4707 | Py_MEMCPY(PyBytes_AS_STRING(result), stackbuf, offset); |
| 4708 | } |
| 4709 | p = PyBytes_AS_STRING(result) + offset; |
| 4710 | } |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4711 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4712 | if (PyBytes_Check(rep)) { |
| 4713 | char *prep = PyBytes_AS_STRING(rep); |
| 4714 | for(k = repsize; k > 0; k--) |
| 4715 | *p++ = *prep++; |
| 4716 | } else /* rep is unicode */ { |
| 4717 | const Py_UNICODE *prep = PyUnicode_AS_UNICODE(rep); |
| 4718 | Py_UNICODE c; |
| 4719 | |
| 4720 | for(k=0; k<repsize; k++) { |
| 4721 | c = prep[k]; |
| 4722 | if (0x80 <= c) { |
| 4723 | raise_encode_exception(&exc, "utf-8", |
| 4724 | PyUnicode_AS_UNICODE(unicode), |
| 4725 | size, i-1, i, |
| 4726 | "surrogates not allowed"); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4727 | goto error; |
| 4728 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4729 | *p++ = (char)prep[k]; |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4730 | } |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4731 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4732 | Py_DECREF(rep); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4733 | } else if (ch < 0x10000) { |
| 4734 | *p++ = (char)(0xe0 | (ch >> 12)); |
| 4735 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 4736 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 4737 | } else /* ch >= 0x10000 */ { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4738 | /* Encode UCS4 Unicode ordinals */ |
| 4739 | *p++ = (char)(0xf0 | (ch >> 18)); |
| 4740 | *p++ = (char)(0x80 | ((ch >> 12) & 0x3f)); |
| 4741 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 4742 | *p++ = (char)(0x80 | (ch & 0x3f)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4743 | #if SIZEOF_WCHAR_T == 2 |
| 4744 | wchar_offset++; |
| 4745 | #endif |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4746 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4747 | } |
Tim Peters | 0eca65c | 2002-04-21 17:28:06 +0000 | [diff] [blame] | 4748 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4749 | if (result == NULL) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4750 | /* This was stack allocated. */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4751 | nneeded = p - stackbuf; |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4752 | assert(nneeded <= nallocated); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4753 | result = PyBytes_FromStringAndSize(stackbuf, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4754 | } |
| 4755 | else { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 4756 | /* Cut back to size actually needed. */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4757 | nneeded = p - PyBytes_AS_STRING(result); |
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 | _PyBytes_Resize(&result, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4760 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4761 | |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4762 | Py_XDECREF(errorHandler); |
| 4763 | Py_XDECREF(exc); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4764 | return result; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4765 | error: |
| 4766 | Py_XDECREF(errorHandler); |
| 4767 | Py_XDECREF(exc); |
| 4768 | Py_XDECREF(result); |
| 4769 | return NULL; |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4770 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4771 | #undef MAX_SHORT_UNICHARS |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4772 | } |
| 4773 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4774 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4775 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
| 4776 | Py_ssize_t size, |
| 4777 | const char *errors) |
| 4778 | { |
| 4779 | PyObject *v, *unicode; |
| 4780 | |
| 4781 | unicode = PyUnicode_FromUnicode(s, size); |
| 4782 | if (unicode == NULL) |
| 4783 | return NULL; |
| 4784 | v = _PyUnicode_AsUTF8String(unicode, errors); |
| 4785 | Py_DECREF(unicode); |
| 4786 | return v; |
| 4787 | } |
| 4788 | |
| 4789 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4790 | PyUnicode_AsUTF8String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4791 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4792 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4793 | } |
| 4794 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4795 | /* --- UTF-32 Codec ------------------------------------------------------- */ |
| 4796 | |
| 4797 | PyObject * |
| 4798 | PyUnicode_DecodeUTF32(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4799 | Py_ssize_t size, |
| 4800 | const char *errors, |
| 4801 | int *byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4802 | { |
| 4803 | return PyUnicode_DecodeUTF32Stateful(s, size, errors, byteorder, NULL); |
| 4804 | } |
| 4805 | |
| 4806 | PyObject * |
| 4807 | PyUnicode_DecodeUTF32Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4808 | Py_ssize_t size, |
| 4809 | const char *errors, |
| 4810 | int *byteorder, |
| 4811 | Py_ssize_t *consumed) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4812 | { |
| 4813 | const char *starts = s; |
| 4814 | Py_ssize_t startinpos; |
| 4815 | Py_ssize_t endinpos; |
| 4816 | Py_ssize_t outpos; |
| 4817 | PyUnicodeObject *unicode; |
| 4818 | Py_UNICODE *p; |
| 4819 | #ifndef Py_UNICODE_WIDE |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 4820 | int pairs = 0; |
Mark Dickinson | 7db923c | 2010-06-12 09:10:14 +0000 | [diff] [blame] | 4821 | const unsigned char *qq; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4822 | #else |
| 4823 | const int pairs = 0; |
| 4824 | #endif |
Mark Dickinson | 7db923c | 2010-06-12 09:10:14 +0000 | [diff] [blame] | 4825 | const unsigned char *q, *e; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4826 | int bo = 0; /* assume native ordering by default */ |
| 4827 | const char *errmsg = ""; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4828 | /* Offsets from q for retrieving bytes in the right order. */ |
| 4829 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 4830 | int iorder[] = {0, 1, 2, 3}; |
| 4831 | #else |
| 4832 | int iorder[] = {3, 2, 1, 0}; |
| 4833 | #endif |
| 4834 | PyObject *errorHandler = NULL; |
| 4835 | PyObject *exc = NULL; |
Victor Stinner | 313a120 | 2010-06-11 23:56:51 +0000 | [diff] [blame] | 4836 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4837 | q = (unsigned char *)s; |
| 4838 | e = q + size; |
| 4839 | |
| 4840 | if (byteorder) |
| 4841 | bo = *byteorder; |
| 4842 | |
| 4843 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 4844 | byte order setting accordingly. In native mode, the leading BOM |
| 4845 | mark is skipped, in all other modes, it is copied to the output |
| 4846 | stream as-is (giving a ZWNBSP character). */ |
| 4847 | if (bo == 0) { |
| 4848 | if (size >= 4) { |
| 4849 | const Py_UCS4 bom = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4850 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4851 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4852 | if (bom == 0x0000FEFF) { |
| 4853 | q += 4; |
| 4854 | bo = -1; |
| 4855 | } |
| 4856 | else if (bom == 0xFFFE0000) { |
| 4857 | q += 4; |
| 4858 | bo = 1; |
| 4859 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4860 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4861 | if (bom == 0x0000FEFF) { |
| 4862 | q += 4; |
| 4863 | bo = 1; |
| 4864 | } |
| 4865 | else if (bom == 0xFFFE0000) { |
| 4866 | q += 4; |
| 4867 | bo = -1; |
| 4868 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4869 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4870 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4871 | } |
| 4872 | |
| 4873 | if (bo == -1) { |
| 4874 | /* force LE */ |
| 4875 | iorder[0] = 0; |
| 4876 | iorder[1] = 1; |
| 4877 | iorder[2] = 2; |
| 4878 | iorder[3] = 3; |
| 4879 | } |
| 4880 | else if (bo == 1) { |
| 4881 | /* force BE */ |
| 4882 | iorder[0] = 3; |
| 4883 | iorder[1] = 2; |
| 4884 | iorder[2] = 1; |
| 4885 | iorder[3] = 0; |
| 4886 | } |
| 4887 | |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 4888 | /* On narrow builds we split characters outside the BMP into two |
| 4889 | codepoints => count how much extra space we need. */ |
| 4890 | #ifndef Py_UNICODE_WIDE |
| 4891 | for (qq = q; qq < e; qq += 4) |
| 4892 | if (qq[iorder[2]] != 0 || qq[iorder[3]] != 0) |
| 4893 | pairs++; |
| 4894 | #endif |
| 4895 | |
| 4896 | /* This might be one to much, because of a BOM */ |
| 4897 | unicode = _PyUnicode_New((size+3)/4+pairs); |
| 4898 | if (!unicode) |
| 4899 | return NULL; |
| 4900 | if (size == 0) |
| 4901 | return (PyObject *)unicode; |
| 4902 | |
| 4903 | /* Unpack UTF-32 encoded data */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4904 | p = PyUnicode_AS_UNICODE(unicode); |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 4905 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4906 | while (q < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4907 | Py_UCS4 ch; |
| 4908 | /* remaining bytes at the end? (size should be divisible by 4) */ |
| 4909 | if (e-q<4) { |
| 4910 | if (consumed) |
| 4911 | break; |
| 4912 | errmsg = "truncated data"; |
| 4913 | startinpos = ((const char *)q)-starts; |
| 4914 | endinpos = ((const char *)e)-starts; |
| 4915 | goto utf32Error; |
| 4916 | /* The remaining input chars are ignored if the callback |
| 4917 | chooses to skip the input */ |
| 4918 | } |
| 4919 | ch = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
| 4920 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4921 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4922 | if (ch >= 0x110000) |
| 4923 | { |
| 4924 | errmsg = "codepoint not in range(0x110000)"; |
| 4925 | startinpos = ((const char *)q)-starts; |
| 4926 | endinpos = startinpos+4; |
| 4927 | goto utf32Error; |
| 4928 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4929 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4930 | if (ch >= 0x10000) |
| 4931 | { |
| 4932 | *p++ = 0xD800 | ((ch-0x10000) >> 10); |
| 4933 | *p++ = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 4934 | } |
| 4935 | else |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4936 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4937 | *p++ = ch; |
| 4938 | q += 4; |
| 4939 | continue; |
| 4940 | utf32Error: |
| 4941 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 4942 | if (unicode_decode_call_errorhandler( |
| 4943 | errors, &errorHandler, |
| 4944 | "utf32", errmsg, |
| 4945 | &starts, (const char **)&e, &startinpos, &endinpos, &exc, (const char **)&q, |
| 4946 | &unicode, &outpos, &p)) |
| 4947 | goto onError; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4948 | } |
| 4949 | |
| 4950 | if (byteorder) |
| 4951 | *byteorder = bo; |
| 4952 | |
| 4953 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4954 | *consumed = (const char *)q-starts; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4955 | |
| 4956 | /* Adjust length */ |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 4957 | if (PyUnicode_Resize((PyObject**)&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4958 | goto onError; |
| 4959 | |
| 4960 | Py_XDECREF(errorHandler); |
| 4961 | Py_XDECREF(exc); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 4962 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 4963 | if (_PyUnicode_READY_REPLACE(&unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4964 | Py_DECREF(unicode); |
| 4965 | return NULL; |
| 4966 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 4967 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 4968 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4969 | return (PyObject *)unicode; |
| 4970 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4971 | onError: |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4972 | Py_DECREF(unicode); |
| 4973 | Py_XDECREF(errorHandler); |
| 4974 | Py_XDECREF(exc); |
| 4975 | return NULL; |
| 4976 | } |
| 4977 | |
| 4978 | PyObject * |
| 4979 | PyUnicode_EncodeUTF32(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4980 | Py_ssize_t size, |
| 4981 | const char *errors, |
| 4982 | int byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4983 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4984 | PyObject *v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4985 | unsigned char *p; |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4986 | Py_ssize_t nsize, bytesize; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4987 | #ifndef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4988 | Py_ssize_t i, pairs; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4989 | #else |
| 4990 | const int pairs = 0; |
| 4991 | #endif |
| 4992 | /* Offsets from p for storing byte pairs in the right order. */ |
| 4993 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 4994 | int iorder[] = {0, 1, 2, 3}; |
| 4995 | #else |
| 4996 | int iorder[] = {3, 2, 1, 0}; |
| 4997 | #endif |
| 4998 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4999 | #define STORECHAR(CH) \ |
| 5000 | do { \ |
| 5001 | p[iorder[3]] = ((CH) >> 24) & 0xff; \ |
| 5002 | p[iorder[2]] = ((CH) >> 16) & 0xff; \ |
| 5003 | p[iorder[1]] = ((CH) >> 8) & 0xff; \ |
| 5004 | p[iorder[0]] = (CH) & 0xff; \ |
| 5005 | p += 4; \ |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5006 | } while(0) |
| 5007 | |
| 5008 | /* In narrow builds we can output surrogate pairs as one codepoint, |
| 5009 | so we need less space. */ |
| 5010 | #ifndef Py_UNICODE_WIDE |
| 5011 | for (i = pairs = 0; i < size-1; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5012 | if (0xD800 <= s[i] && s[i] <= 0xDBFF && |
| 5013 | 0xDC00 <= s[i+1] && s[i+1] <= 0xDFFF) |
| 5014 | pairs++; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5015 | #endif |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5016 | nsize = (size - pairs + (byteorder == 0)); |
| 5017 | bytesize = nsize * 4; |
| 5018 | if (bytesize / 4 != nsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5019 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5020 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5021 | if (v == NULL) |
| 5022 | return NULL; |
| 5023 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5024 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5025 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5026 | STORECHAR(0xFEFF); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5027 | if (size == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5028 | goto done; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5029 | |
| 5030 | if (byteorder == -1) { |
| 5031 | /* force LE */ |
| 5032 | iorder[0] = 0; |
| 5033 | iorder[1] = 1; |
| 5034 | iorder[2] = 2; |
| 5035 | iorder[3] = 3; |
| 5036 | } |
| 5037 | else if (byteorder == 1) { |
| 5038 | /* force BE */ |
| 5039 | iorder[0] = 3; |
| 5040 | iorder[1] = 2; |
| 5041 | iorder[2] = 1; |
| 5042 | iorder[3] = 0; |
| 5043 | } |
| 5044 | |
| 5045 | while (size-- > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5046 | Py_UCS4 ch = *s++; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5047 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5048 | if (0xD800 <= ch && ch <= 0xDBFF && size > 0) { |
| 5049 | Py_UCS4 ch2 = *s; |
| 5050 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
| 5051 | ch = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
| 5052 | s++; |
| 5053 | size--; |
| 5054 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5055 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5056 | #endif |
| 5057 | STORECHAR(ch); |
| 5058 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5059 | |
| 5060 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5061 | return v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5062 | #undef STORECHAR |
| 5063 | } |
| 5064 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5065 | PyObject * |
| 5066 | PyUnicode_AsUTF32String(PyObject *unicode) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5067 | { |
| 5068 | if (!PyUnicode_Check(unicode)) { |
| 5069 | PyErr_BadArgument(); |
| 5070 | return NULL; |
| 5071 | } |
| 5072 | return PyUnicode_EncodeUTF32(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5073 | PyUnicode_GET_SIZE(unicode), |
| 5074 | NULL, |
| 5075 | 0); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5076 | } |
| 5077 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5078 | /* --- UTF-16 Codec ------------------------------------------------------- */ |
| 5079 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5080 | PyObject * |
| 5081 | PyUnicode_DecodeUTF16(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5082 | Py_ssize_t size, |
| 5083 | const char *errors, |
| 5084 | int *byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5085 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5086 | return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL); |
| 5087 | } |
| 5088 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5089 | /* Two masks for fast checking of whether a C 'long' may contain |
| 5090 | UTF16-encoded surrogate characters. This is an efficient heuristic, |
| 5091 | assuming that non-surrogate characters with a code point >= 0x8000 are |
| 5092 | rare in most input. |
| 5093 | FAST_CHAR_MASK is used when the input is in native byte ordering, |
| 5094 | SWAPPED_FAST_CHAR_MASK when the input is in byteswapped ordering. |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5095 | */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5096 | #if (SIZEOF_LONG == 8) |
| 5097 | # define FAST_CHAR_MASK 0x8000800080008000L |
| 5098 | # define SWAPPED_FAST_CHAR_MASK 0x0080008000800080L |
| 5099 | #elif (SIZEOF_LONG == 4) |
| 5100 | # define FAST_CHAR_MASK 0x80008000L |
| 5101 | # define SWAPPED_FAST_CHAR_MASK 0x00800080L |
| 5102 | #else |
| 5103 | # error C 'long' size should be either 4 or 8! |
| 5104 | #endif |
| 5105 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5106 | PyObject * |
| 5107 | PyUnicode_DecodeUTF16Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5108 | Py_ssize_t size, |
| 5109 | const char *errors, |
| 5110 | int *byteorder, |
| 5111 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5112 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5113 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5114 | Py_ssize_t startinpos; |
| 5115 | Py_ssize_t endinpos; |
| 5116 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5117 | PyUnicodeObject *unicode; |
| 5118 | Py_UNICODE *p; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5119 | const unsigned char *q, *e, *aligned_end; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5120 | int bo = 0; /* assume native ordering by default */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5121 | int native_ordering = 0; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 5122 | const char *errmsg = ""; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5123 | /* Offsets from q for retrieving byte pairs in the right order. */ |
| 5124 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5125 | int ihi = 1, ilo = 0; |
| 5126 | #else |
| 5127 | int ihi = 0, ilo = 1; |
| 5128 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5129 | PyObject *errorHandler = NULL; |
| 5130 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5131 | |
| 5132 | /* Note: size will always be longer than the resulting Unicode |
| 5133 | character count */ |
| 5134 | unicode = _PyUnicode_New(size); |
| 5135 | if (!unicode) |
| 5136 | return NULL; |
| 5137 | if (size == 0) |
| 5138 | return (PyObject *)unicode; |
| 5139 | |
| 5140 | /* Unpack UTF-16 encoded data */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5141 | p = PyUnicode_AS_UNICODE(unicode); |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5142 | q = (unsigned char *)s; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5143 | e = q + size - 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5144 | |
| 5145 | if (byteorder) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5146 | bo = *byteorder; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5147 | |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5148 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 5149 | byte order setting accordingly. In native mode, the leading BOM |
| 5150 | mark is skipped, in all other modes, it is copied to the output |
| 5151 | stream as-is (giving a ZWNBSP character). */ |
| 5152 | if (bo == 0) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5153 | if (size >= 2) { |
| 5154 | const Py_UNICODE bom = (q[ihi] << 8) | q[ilo]; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5155 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5156 | if (bom == 0xFEFF) { |
| 5157 | q += 2; |
| 5158 | bo = -1; |
| 5159 | } |
| 5160 | else if (bom == 0xFFFE) { |
| 5161 | q += 2; |
| 5162 | bo = 1; |
| 5163 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5164 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5165 | if (bom == 0xFEFF) { |
| 5166 | q += 2; |
| 5167 | bo = 1; |
| 5168 | } |
| 5169 | else if (bom == 0xFFFE) { |
| 5170 | q += 2; |
| 5171 | bo = -1; |
| 5172 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5173 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5174 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5175 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5176 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5177 | if (bo == -1) { |
| 5178 | /* force LE */ |
| 5179 | ihi = 1; |
| 5180 | ilo = 0; |
| 5181 | } |
| 5182 | else if (bo == 1) { |
| 5183 | /* force BE */ |
| 5184 | ihi = 0; |
| 5185 | ilo = 1; |
| 5186 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5187 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5188 | native_ordering = ilo < ihi; |
| 5189 | #else |
| 5190 | native_ordering = ilo > ihi; |
| 5191 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5192 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5193 | aligned_end = (const unsigned char *) ((size_t) e & ~LONG_PTR_MASK); |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5194 | while (q < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5195 | Py_UNICODE ch; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5196 | /* First check for possible aligned read of a C 'long'. Unaligned |
| 5197 | reads are more expensive, better to defer to another iteration. */ |
| 5198 | if (!((size_t) q & LONG_PTR_MASK)) { |
| 5199 | /* Fast path for runs of non-surrogate chars. */ |
| 5200 | register const unsigned char *_q = q; |
| 5201 | Py_UNICODE *_p = p; |
| 5202 | if (native_ordering) { |
| 5203 | /* Native ordering is simple: as long as the input cannot |
| 5204 | possibly contain a surrogate char, do an unrolled copy |
| 5205 | of several 16-bit code points to the target object. |
| 5206 | The non-surrogate check is done on several input bytes |
| 5207 | at a time (as many as a C 'long' can contain). */ |
| 5208 | while (_q < aligned_end) { |
| 5209 | unsigned long data = * (unsigned long *) _q; |
| 5210 | if (data & FAST_CHAR_MASK) |
| 5211 | break; |
| 5212 | _p[0] = ((unsigned short *) _q)[0]; |
| 5213 | _p[1] = ((unsigned short *) _q)[1]; |
| 5214 | #if (SIZEOF_LONG == 8) |
| 5215 | _p[2] = ((unsigned short *) _q)[2]; |
| 5216 | _p[3] = ((unsigned short *) _q)[3]; |
| 5217 | #endif |
| 5218 | _q += SIZEOF_LONG; |
| 5219 | _p += SIZEOF_LONG / 2; |
| 5220 | } |
| 5221 | } |
| 5222 | else { |
| 5223 | /* Byteswapped ordering is similar, but we must decompose |
| 5224 | the copy bytewise, and take care of zero'ing out the |
| 5225 | upper bytes if the target object is in 32-bit units |
| 5226 | (that is, in UCS-4 builds). */ |
| 5227 | while (_q < aligned_end) { |
| 5228 | unsigned long data = * (unsigned long *) _q; |
| 5229 | if (data & SWAPPED_FAST_CHAR_MASK) |
| 5230 | break; |
| 5231 | /* Zero upper bytes in UCS-4 builds */ |
| 5232 | #if (Py_UNICODE_SIZE > 2) |
| 5233 | _p[0] = 0; |
| 5234 | _p[1] = 0; |
| 5235 | #if (SIZEOF_LONG == 8) |
| 5236 | _p[2] = 0; |
| 5237 | _p[3] = 0; |
| 5238 | #endif |
| 5239 | #endif |
Antoine Pitrou | d6e8de1 | 2009-01-11 23:56:55 +0000 | [diff] [blame] | 5240 | /* Issue #4916; UCS-4 builds on big endian machines must |
| 5241 | fill the two last bytes of each 4-byte unit. */ |
| 5242 | #if (!defined(BYTEORDER_IS_LITTLE_ENDIAN) && Py_UNICODE_SIZE > 2) |
| 5243 | # define OFF 2 |
| 5244 | #else |
| 5245 | # define OFF 0 |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5246 | #endif |
Antoine Pitrou | d6e8de1 | 2009-01-11 23:56:55 +0000 | [diff] [blame] | 5247 | ((unsigned char *) _p)[OFF + 1] = _q[0]; |
| 5248 | ((unsigned char *) _p)[OFF + 0] = _q[1]; |
| 5249 | ((unsigned char *) _p)[OFF + 1 + Py_UNICODE_SIZE] = _q[2]; |
| 5250 | ((unsigned char *) _p)[OFF + 0 + Py_UNICODE_SIZE] = _q[3]; |
| 5251 | #if (SIZEOF_LONG == 8) |
| 5252 | ((unsigned char *) _p)[OFF + 1 + 2 * Py_UNICODE_SIZE] = _q[4]; |
| 5253 | ((unsigned char *) _p)[OFF + 0 + 2 * Py_UNICODE_SIZE] = _q[5]; |
| 5254 | ((unsigned char *) _p)[OFF + 1 + 3 * Py_UNICODE_SIZE] = _q[6]; |
| 5255 | ((unsigned char *) _p)[OFF + 0 + 3 * Py_UNICODE_SIZE] = _q[7]; |
| 5256 | #endif |
| 5257 | #undef OFF |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5258 | _q += SIZEOF_LONG; |
| 5259 | _p += SIZEOF_LONG / 2; |
| 5260 | } |
| 5261 | } |
| 5262 | p = _p; |
| 5263 | q = _q; |
| 5264 | if (q >= e) |
| 5265 | break; |
| 5266 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5267 | ch = (q[ihi] << 8) | q[ilo]; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5268 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5269 | q += 2; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5270 | |
| 5271 | if (ch < 0xD800 || ch > 0xDFFF) { |
| 5272 | *p++ = ch; |
| 5273 | continue; |
| 5274 | } |
| 5275 | |
| 5276 | /* UTF-16 code pair: */ |
| 5277 | if (q > e) { |
| 5278 | errmsg = "unexpected end of data"; |
| 5279 | startinpos = (((const char *)q) - 2) - starts; |
| 5280 | endinpos = ((const char *)e) + 1 - starts; |
| 5281 | goto utf16Error; |
| 5282 | } |
| 5283 | if (0xD800 <= ch && ch <= 0xDBFF) { |
| 5284 | Py_UNICODE ch2 = (q[ihi] << 8) | q[ilo]; |
| 5285 | q += 2; |
| 5286 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 5287 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5288 | *p++ = ch; |
| 5289 | *p++ = ch2; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5290 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5291 | *p++ = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5292 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5293 | continue; |
| 5294 | } |
| 5295 | else { |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5296 | errmsg = "illegal UTF-16 surrogate"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5297 | startinpos = (((const char *)q)-4)-starts; |
| 5298 | endinpos = startinpos+2; |
| 5299 | goto utf16Error; |
| 5300 | } |
| 5301 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5302 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5303 | errmsg = "illegal encoding"; |
| 5304 | startinpos = (((const char *)q)-2)-starts; |
| 5305 | endinpos = startinpos+2; |
| 5306 | /* Fall through to report the error */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5307 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5308 | utf16Error: |
| 5309 | outpos = p - PyUnicode_AS_UNICODE(unicode); |
| 5310 | if (unicode_decode_call_errorhandler( |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5311 | errors, |
| 5312 | &errorHandler, |
| 5313 | "utf16", errmsg, |
| 5314 | &starts, |
| 5315 | (const char **)&e, |
| 5316 | &startinpos, |
| 5317 | &endinpos, |
| 5318 | &exc, |
| 5319 | (const char **)&q, |
| 5320 | &unicode, |
| 5321 | &outpos, |
| 5322 | &p)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5323 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5324 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5325 | /* remaining byte at the end? (size should be even) */ |
| 5326 | if (e == q) { |
| 5327 | if (!consumed) { |
| 5328 | errmsg = "truncated data"; |
| 5329 | startinpos = ((const char *)q) - starts; |
| 5330 | endinpos = ((const char *)e) + 1 - starts; |
| 5331 | outpos = p - PyUnicode_AS_UNICODE(unicode); |
| 5332 | if (unicode_decode_call_errorhandler( |
| 5333 | errors, |
| 5334 | &errorHandler, |
| 5335 | "utf16", errmsg, |
| 5336 | &starts, |
| 5337 | (const char **)&e, |
| 5338 | &startinpos, |
| 5339 | &endinpos, |
| 5340 | &exc, |
| 5341 | (const char **)&q, |
| 5342 | &unicode, |
| 5343 | &outpos, |
| 5344 | &p)) |
| 5345 | goto onError; |
| 5346 | /* The remaining input chars are ignored if the callback |
| 5347 | chooses to skip the input */ |
| 5348 | } |
| 5349 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5350 | |
| 5351 | if (byteorder) |
| 5352 | *byteorder = bo; |
| 5353 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5354 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5355 | *consumed = (const char *)q-starts; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5356 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5357 | /* Adjust length */ |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 5358 | if (PyUnicode_Resize((PyObject**)&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5359 | goto onError; |
| 5360 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5361 | Py_XDECREF(errorHandler); |
| 5362 | Py_XDECREF(exc); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 5363 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 5364 | if (_PyUnicode_READY_REPLACE(&unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5365 | Py_DECREF(unicode); |
| 5366 | return NULL; |
| 5367 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 5368 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 5369 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5370 | return (PyObject *)unicode; |
| 5371 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5372 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5373 | Py_DECREF(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5374 | Py_XDECREF(errorHandler); |
| 5375 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5376 | return NULL; |
| 5377 | } |
| 5378 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5379 | #undef FAST_CHAR_MASK |
| 5380 | #undef SWAPPED_FAST_CHAR_MASK |
| 5381 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5382 | PyObject * |
| 5383 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5384 | Py_ssize_t size, |
| 5385 | const char *errors, |
| 5386 | int byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5387 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5388 | PyObject *v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5389 | unsigned char *p; |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5390 | Py_ssize_t nsize, bytesize; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 5391 | #ifdef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5392 | Py_ssize_t i, pairs; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 5393 | #else |
| 5394 | const int pairs = 0; |
| 5395 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5396 | /* Offsets from p for storing byte pairs in the right order. */ |
| 5397 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5398 | int ihi = 1, ilo = 0; |
| 5399 | #else |
| 5400 | int ihi = 0, ilo = 1; |
| 5401 | #endif |
| 5402 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5403 | #define STORECHAR(CH) \ |
| 5404 | do { \ |
| 5405 | p[ihi] = ((CH) >> 8) & 0xff; \ |
| 5406 | p[ilo] = (CH) & 0xff; \ |
| 5407 | p += 2; \ |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5408 | } while(0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5409 | |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 5410 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5411 | for (i = pairs = 0; i < size; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5412 | if (s[i] >= 0x10000) |
| 5413 | pairs++; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 5414 | #endif |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5415 | /* 2 * (size + pairs + (byteorder == 0)) */ |
| 5416 | if (size > PY_SSIZE_T_MAX || |
| 5417 | size > PY_SSIZE_T_MAX - pairs - (byteorder == 0)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5418 | return PyErr_NoMemory(); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5419 | nsize = size + pairs + (byteorder == 0); |
| 5420 | bytesize = nsize * 2; |
| 5421 | if (bytesize / 2 != nsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5422 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5423 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5424 | if (v == NULL) |
| 5425 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5426 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5427 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5428 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5429 | STORECHAR(0xFEFF); |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 5430 | if (size == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5431 | goto done; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5432 | |
| 5433 | if (byteorder == -1) { |
| 5434 | /* force LE */ |
| 5435 | ihi = 1; |
| 5436 | ilo = 0; |
| 5437 | } |
| 5438 | else if (byteorder == 1) { |
| 5439 | /* force BE */ |
| 5440 | ihi = 0; |
| 5441 | ilo = 1; |
| 5442 | } |
| 5443 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5444 | while (size-- > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5445 | Py_UNICODE ch = *s++; |
| 5446 | Py_UNICODE ch2 = 0; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 5447 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5448 | if (ch >= 0x10000) { |
| 5449 | ch2 = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 5450 | ch = 0xD800 | ((ch-0x10000) >> 10); |
| 5451 | } |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 5452 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5453 | STORECHAR(ch); |
| 5454 | if (ch2) |
| 5455 | STORECHAR(ch2); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5456 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5457 | |
| 5458 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5459 | return v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5460 | #undef STORECHAR |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5461 | } |
| 5462 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5463 | PyObject * |
| 5464 | PyUnicode_AsUTF16String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5465 | { |
| 5466 | if (!PyUnicode_Check(unicode)) { |
| 5467 | PyErr_BadArgument(); |
| 5468 | return NULL; |
| 5469 | } |
| 5470 | return PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5471 | PyUnicode_GET_SIZE(unicode), |
| 5472 | NULL, |
| 5473 | 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5474 | } |
| 5475 | |
| 5476 | /* --- Unicode Escape Codec ----------------------------------------------- */ |
| 5477 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5478 | /* Helper function for PyUnicode_DecodeUnicodeEscape, determines |
| 5479 | if all the escapes in the string make it still a valid ASCII string. |
| 5480 | Returns -1 if any escapes were found which cause the string to |
| 5481 | pop out of ASCII range. Otherwise returns the length of the |
| 5482 | required buffer to hold the string. |
| 5483 | */ |
| 5484 | Py_ssize_t |
| 5485 | length_of_escaped_ascii_string(const char *s, Py_ssize_t size) |
| 5486 | { |
| 5487 | const unsigned char *p = (const unsigned char *)s; |
| 5488 | const unsigned char *end = p + size; |
| 5489 | Py_ssize_t length = 0; |
| 5490 | |
| 5491 | if (size < 0) |
| 5492 | return -1; |
| 5493 | |
| 5494 | for (; p < end; ++p) { |
| 5495 | if (*p > 127) { |
| 5496 | /* Non-ASCII */ |
| 5497 | return -1; |
| 5498 | } |
| 5499 | else if (*p != '\\') { |
| 5500 | /* Normal character */ |
| 5501 | ++length; |
| 5502 | } |
| 5503 | else { |
| 5504 | /* Backslash-escape, check next char */ |
| 5505 | ++p; |
| 5506 | /* Escape sequence reaches till end of string or |
| 5507 | non-ASCII follow-up. */ |
| 5508 | if (p >= end || *p > 127) |
| 5509 | return -1; |
| 5510 | switch (*p) { |
| 5511 | case '\n': |
| 5512 | /* backslash + \n result in zero characters */ |
| 5513 | break; |
| 5514 | case '\\': case '\'': case '\"': |
| 5515 | case 'b': case 'f': case 't': |
| 5516 | case 'n': case 'r': case 'v': case 'a': |
| 5517 | ++length; |
| 5518 | break; |
| 5519 | case '0': case '1': case '2': case '3': |
| 5520 | case '4': case '5': case '6': case '7': |
| 5521 | case 'x': case 'u': case 'U': case 'N': |
| 5522 | /* these do not guarantee ASCII characters */ |
| 5523 | return -1; |
| 5524 | default: |
| 5525 | /* count the backslash + the other character */ |
| 5526 | length += 2; |
| 5527 | } |
| 5528 | } |
| 5529 | } |
| 5530 | return length; |
| 5531 | } |
| 5532 | |
| 5533 | /* Similar to PyUnicode_WRITE but either write into wstr field |
| 5534 | or treat string as ASCII. */ |
| 5535 | #define WRITE_ASCII_OR_WSTR(kind, buf, index, value) \ |
| 5536 | do { \ |
| 5537 | if ((kind) != PyUnicode_WCHAR_KIND) \ |
| 5538 | ((unsigned char *)(buf))[(index)] = (unsigned char)(value); \ |
| 5539 | else \ |
| 5540 | ((Py_UNICODE *)(buf))[(index)] = (Py_UNICODE)(value); \ |
| 5541 | } while (0) |
| 5542 | |
| 5543 | #define WRITE_WSTR(buf, index, value) \ |
| 5544 | assert(kind == PyUnicode_WCHAR_KIND), \ |
| 5545 | ((Py_UNICODE *)(buf))[(index)] = (Py_UNICODE)(value) |
| 5546 | |
| 5547 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 5548 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; |
Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 5549 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5550 | PyObject * |
| 5551 | PyUnicode_DecodeUnicodeEscape(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5552 | Py_ssize_t size, |
Victor Stinner | c17f540 | 2011-09-29 00:16:58 +0200 | [diff] [blame] | 5553 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5554 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5555 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5556 | Py_ssize_t startinpos; |
| 5557 | Py_ssize_t endinpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5558 | int j; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5559 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5560 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5561 | const char *end; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5562 | char* message; |
| 5563 | Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5564 | PyObject *errorHandler = NULL; |
| 5565 | PyObject *exc = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5566 | Py_ssize_t ascii_length; |
| 5567 | Py_ssize_t i; |
| 5568 | int kind; |
| 5569 | void *data; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5570 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5571 | ascii_length = length_of_escaped_ascii_string(s, size); |
| 5572 | |
| 5573 | /* After length_of_escaped_ascii_string() there are two alternatives, |
| 5574 | either the string is pure ASCII with named escapes like \n, etc. |
| 5575 | and we determined it's exact size (common case) |
| 5576 | or it contains \x, \u, ... escape sequences. then we create a |
| 5577 | legacy wchar string and resize it at the end of this function. */ |
| 5578 | if (ascii_length >= 0) { |
| 5579 | v = (PyUnicodeObject *)PyUnicode_New(ascii_length, 127); |
| 5580 | if (!v) |
| 5581 | goto onError; |
| 5582 | assert(PyUnicode_KIND(v) == PyUnicode_1BYTE_KIND); |
| 5583 | kind = PyUnicode_1BYTE_KIND; |
| 5584 | data = PyUnicode_DATA(v); |
| 5585 | } |
| 5586 | else { |
| 5587 | /* Escaped strings will always be longer than the resulting |
| 5588 | Unicode string, so we start with size here and then reduce the |
| 5589 | length after conversion to the true value. |
| 5590 | (but if the error callback returns a long replacement string |
| 5591 | we'll have to allocate more space) */ |
| 5592 | v = _PyUnicode_New(size); |
| 5593 | if (!v) |
| 5594 | goto onError; |
| 5595 | kind = PyUnicode_WCHAR_KIND; |
| 5596 | data = PyUnicode_AS_UNICODE(v); |
| 5597 | } |
| 5598 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5599 | if (size == 0) |
| 5600 | return (PyObject *)v; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5601 | i = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5602 | end = s + size; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5603 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5604 | while (s < end) { |
| 5605 | unsigned char c; |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 5606 | Py_UNICODE x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5607 | int digits; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5608 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5609 | if (kind == PyUnicode_WCHAR_KIND) { |
| 5610 | assert(i < _PyUnicode_WSTR_LENGTH(v)); |
| 5611 | } |
| 5612 | else { |
| 5613 | /* The only case in which i == ascii_length is a backslash |
| 5614 | followed by a newline. */ |
| 5615 | assert(i <= ascii_length); |
| 5616 | } |
| 5617 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5618 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 5619 | if (*s != '\\') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5620 | WRITE_ASCII_OR_WSTR(kind, data, i++, (unsigned char) *s++); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5621 | continue; |
| 5622 | } |
| 5623 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5624 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5625 | /* \ - Escapes */ |
| 5626 | s++; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5627 | c = *s++; |
| 5628 | if (s > end) |
| 5629 | c = '\0'; /* Invalid after \ */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5630 | |
| 5631 | if (kind == PyUnicode_WCHAR_KIND) { |
| 5632 | assert(i < _PyUnicode_WSTR_LENGTH(v)); |
| 5633 | } |
| 5634 | else { |
| 5635 | /* The only case in which i == ascii_length is a backslash |
| 5636 | followed by a newline. */ |
| 5637 | assert(i < ascii_length || (i == ascii_length && c == '\n')); |
| 5638 | } |
| 5639 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5640 | switch (c) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5641 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5642 | /* \x escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5643 | case '\n': break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5644 | case '\\': WRITE_ASCII_OR_WSTR(kind, data, i++, '\\'); break; |
| 5645 | case '\'': WRITE_ASCII_OR_WSTR(kind, data, i++, '\''); break; |
| 5646 | case '\"': WRITE_ASCII_OR_WSTR(kind, data, i++, '\"'); break; |
| 5647 | case 'b': WRITE_ASCII_OR_WSTR(kind, data, i++, '\b'); break; |
| 5648 | /* FF */ |
| 5649 | case 'f': WRITE_ASCII_OR_WSTR(kind, data, i++, '\014'); break; |
| 5650 | case 't': WRITE_ASCII_OR_WSTR(kind, data, i++, '\t'); break; |
| 5651 | case 'n': WRITE_ASCII_OR_WSTR(kind, data, i++, '\n'); break; |
| 5652 | case 'r': WRITE_ASCII_OR_WSTR(kind, data, i++, '\r'); break; |
| 5653 | /* VT */ |
| 5654 | case 'v': WRITE_ASCII_OR_WSTR(kind, data, i++, '\013'); break; |
| 5655 | /* BEL, not classic C */ |
| 5656 | case 'a': WRITE_ASCII_OR_WSTR(kind, data, i++, '\007'); break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5657 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5658 | /* \OOO (octal) escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5659 | case '0': case '1': case '2': case '3': |
| 5660 | case '4': case '5': case '6': case '7': |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5661 | x = s[-1] - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5662 | if (s < end && '0' <= *s && *s <= '7') { |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5663 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5664 | if (s < end && '0' <= *s && *s <= '7') |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5665 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5666 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5667 | WRITE_WSTR(data, i++, x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5668 | break; |
| 5669 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5670 | /* hex escapes */ |
| 5671 | /* \xXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5672 | case 'x': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5673 | digits = 2; |
| 5674 | message = "truncated \\xXX escape"; |
| 5675 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5676 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5677 | /* \uXXXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5678 | case 'u': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5679 | digits = 4; |
| 5680 | message = "truncated \\uXXXX 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 | /* \UXXXXXXXX */ |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5684 | case 'U': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5685 | digits = 8; |
| 5686 | message = "truncated \\UXXXXXXXX escape"; |
| 5687 | hexescape: |
| 5688 | chr = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5689 | p = PyUnicode_AS_UNICODE(v) + i; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5690 | if (s+digits>end) { |
| 5691 | endinpos = size; |
| 5692 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5693 | errors, &errorHandler, |
| 5694 | "unicodeescape", "end of string in escape sequence", |
| 5695 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5696 | &v, &i, &p)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5697 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5698 | data = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5699 | goto nextByte; |
| 5700 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5701 | for (j = 0; j < digits; ++j) { |
| 5702 | c = (unsigned char) s[j]; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 5703 | if (!Py_ISXDIGIT(c)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5704 | endinpos = (s+j+1)-starts; |
| 5705 | p = PyUnicode_AS_UNICODE(v) + i; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5706 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5707 | errors, &errorHandler, |
| 5708 | "unicodeescape", message, |
| 5709 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5710 | &v, &i, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5711 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5712 | data = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5713 | goto nextByte; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5714 | } |
| 5715 | chr = (chr<<4) & ~0xF; |
| 5716 | if (c >= '0' && c <= '9') |
| 5717 | chr += c - '0'; |
| 5718 | else if (c >= 'a' && c <= 'f') |
| 5719 | chr += 10 + c - 'a'; |
| 5720 | else |
| 5721 | chr += 10 + c - 'A'; |
| 5722 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5723 | s += j; |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 5724 | if (chr == 0xffffffff && PyErr_Occurred()) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5725 | /* _decoding_error will have already written into the |
| 5726 | target buffer. */ |
| 5727 | break; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5728 | store: |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5729 | /* when we get here, chr is a 32-bit unicode character */ |
| 5730 | if (chr <= 0xffff) |
| 5731 | /* UCS-2 character */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5732 | WRITE_WSTR(data, i++, chr); |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5733 | else if (chr <= 0x10ffff) { |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 5734 | /* UCS-4 character. Either store directly, or as |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5735 | surrogate pair. */ |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 5736 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5737 | WRITE_WSTR(data, i++, chr); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5738 | #else |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5739 | chr -= 0x10000L; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5740 | WRITE_WSTR(data, i++, 0xD800 + (Py_UNICODE) (chr >> 10)); |
| 5741 | WRITE_WSTR(data, i++, 0xDC00 + (Py_UNICODE) (chr & 0x03FF)); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5742 | #endif |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5743 | } else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5744 | endinpos = s-starts; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5745 | p = PyUnicode_AS_UNICODE(v) + i; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5746 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5747 | errors, &errorHandler, |
| 5748 | "unicodeescape", "illegal Unicode character", |
| 5749 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5750 | &v, &i, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5751 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5752 | data = PyUnicode_AS_UNICODE(v); |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5753 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5754 | break; |
| 5755 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5756 | /* \N{name} */ |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5757 | case 'N': |
| 5758 | message = "malformed \\N character escape"; |
| 5759 | if (ucnhash_CAPI == NULL) { |
| 5760 | /* load the unicode data module */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5761 | ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCapsule_Import( |
| 5762 | PyUnicodeData_CAPSULE_NAME, 1); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5763 | if (ucnhash_CAPI == NULL) |
| 5764 | goto ucnhashError; |
| 5765 | } |
| 5766 | if (*s == '{') { |
| 5767 | const char *start = s+1; |
| 5768 | /* look for the closing brace */ |
| 5769 | while (*s != '}' && s < end) |
| 5770 | s++; |
| 5771 | if (s > start && s < end && *s == '}') { |
| 5772 | /* found a name. look it up in the unicode database */ |
| 5773 | message = "unknown Unicode character name"; |
| 5774 | s++; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5775 | if (ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), |
| 5776 | &chr)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5777 | goto store; |
| 5778 | } |
| 5779 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5780 | endinpos = s-starts; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5781 | p = PyUnicode_AS_UNICODE(v) + i; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5782 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5783 | errors, &errorHandler, |
| 5784 | "unicodeescape", message, |
| 5785 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5786 | &v, &i, &p)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5787 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5788 | data = PyUnicode_AS_UNICODE(v); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5789 | break; |
| 5790 | |
| 5791 | default: |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5792 | if (s > end) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5793 | assert(kind == PyUnicode_WCHAR_KIND); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5794 | message = "\\ at end of string"; |
| 5795 | s--; |
| 5796 | endinpos = s-starts; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5797 | p = PyUnicode_AS_UNICODE(v) + i; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5798 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5799 | errors, &errorHandler, |
| 5800 | "unicodeescape", message, |
| 5801 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5802 | &v, &i, &p)) |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5803 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5804 | data = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5805 | } |
| 5806 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5807 | WRITE_ASCII_OR_WSTR(kind, data, i++, '\\'); |
| 5808 | WRITE_ASCII_OR_WSTR(kind, data, i++, (unsigned char)s[-1]); |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5809 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5810 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5811 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5812 | nextByte: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5813 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5814 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5815 | /* Ensure the length prediction worked in case of ASCII strings */ |
| 5816 | assert(kind == PyUnicode_WCHAR_KIND || i == ascii_length); |
| 5817 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 5818 | if (kind == PyUnicode_WCHAR_KIND) |
| 5819 | { |
| 5820 | if (PyUnicode_Resize((PyObject**)&v, i) < 0) |
| 5821 | goto onError; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 5822 | } |
Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 5823 | Py_XDECREF(errorHandler); |
| 5824 | Py_XDECREF(exc); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 5825 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 5826 | if (_PyUnicode_READY_REPLACE(&v)) { |
| 5827 | Py_DECREF(v); |
| 5828 | return NULL; |
| 5829 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 5830 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 5831 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5832 | return (PyObject *)v; |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5833 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5834 | ucnhashError: |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 5835 | PyErr_SetString( |
| 5836 | PyExc_UnicodeError, |
| 5837 | "\\N escapes not supported (can't load unicodedata module)" |
| 5838 | ); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 5839 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5840 | Py_XDECREF(errorHandler); |
| 5841 | Py_XDECREF(exc); |
Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 5842 | return NULL; |
| 5843 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5844 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +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); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5848 | return NULL; |
| 5849 | } |
| 5850 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5851 | #undef WRITE_ASCII_OR_WSTR |
| 5852 | #undef WRITE_WSTR |
| 5853 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5854 | /* Return a Unicode-Escape string version of the Unicode object. |
| 5855 | |
| 5856 | If quotes is true, the string is enclosed in u"" or u'' quotes as |
| 5857 | appropriate. |
| 5858 | |
| 5859 | */ |
| 5860 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5861 | static const char *hexdigits = "0123456789abcdef"; |
| 5862 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5863 | PyObject * |
| 5864 | PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5865 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5866 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5867 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5868 | char *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5869 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5870 | #ifdef Py_UNICODE_WIDE |
| 5871 | const Py_ssize_t expandsize = 10; |
| 5872 | #else |
| 5873 | const Py_ssize_t expandsize = 6; |
| 5874 | #endif |
| 5875 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5876 | /* XXX(nnorwitz): rather than over-allocating, it would be |
| 5877 | better to choose a different scheme. Perhaps scan the |
| 5878 | first N-chars of the string and allocate based on that size. |
| 5879 | */ |
| 5880 | /* Initial allocation is based on the longest-possible unichr |
| 5881 | escape. |
| 5882 | |
| 5883 | In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source |
| 5884 | unichr, so in this case it's the longest unichr escape. In |
| 5885 | narrow (UTF-16) builds this is five chars per source unichr |
| 5886 | since there are two unichrs in the surrogate pair, so in narrow |
| 5887 | (UTF-16) builds it's not the longest unichr escape. |
| 5888 | |
| 5889 | In wide or narrow builds '\uxxxx' is 6 chars per source unichr, |
| 5890 | so in the narrow (UTF-16) build case it's the longest unichr |
| 5891 | escape. |
| 5892 | */ |
| 5893 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5894 | if (size == 0) |
| 5895 | return PyBytes_FromStringAndSize(NULL, 0); |
| 5896 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5897 | if (size > (PY_SSIZE_T_MAX - 2 - 1) / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5898 | return PyErr_NoMemory(); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5899 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5900 | repr = PyBytes_FromStringAndSize(NULL, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5901 | 2 |
| 5902 | + expandsize*size |
| 5903 | + 1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5904 | if (repr == NULL) |
| 5905 | return NULL; |
| 5906 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5907 | p = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5908 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5909 | while (size-- > 0) { |
| 5910 | Py_UNICODE ch = *s++; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5911 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5912 | /* Escape backslashes */ |
| 5913 | if (ch == '\\') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5914 | *p++ = '\\'; |
| 5915 | *p++ = (char) ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5916 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5917 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5918 | |
Guido van Rossum | 0d42e0c | 2001-07-20 16:36:21 +0000 | [diff] [blame] | 5919 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5920 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 5921 | else if (ch >= 0x10000) { |
| 5922 | *p++ = '\\'; |
| 5923 | *p++ = 'U'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5924 | *p++ = hexdigits[(ch >> 28) & 0x0000000F]; |
| 5925 | *p++ = hexdigits[(ch >> 24) & 0x0000000F]; |
| 5926 | *p++ = hexdigits[(ch >> 20) & 0x0000000F]; |
| 5927 | *p++ = hexdigits[(ch >> 16) & 0x0000000F]; |
| 5928 | *p++ = hexdigits[(ch >> 12) & 0x0000000F]; |
| 5929 | *p++ = hexdigits[(ch >> 8) & 0x0000000F]; |
| 5930 | *p++ = hexdigits[(ch >> 4) & 0x0000000F]; |
| 5931 | *p++ = hexdigits[ch & 0x0000000F]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5932 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5933 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5934 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5935 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
| 5936 | else if (ch >= 0xD800 && ch < 0xDC00) { |
| 5937 | Py_UNICODE ch2; |
| 5938 | Py_UCS4 ucs; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5939 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5940 | ch2 = *s++; |
| 5941 | size--; |
Georg Brandl | 78eef3de | 2010-08-01 20:51:02 +0000 | [diff] [blame] | 5942 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5943 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 5944 | *p++ = '\\'; |
| 5945 | *p++ = 'U'; |
| 5946 | *p++ = hexdigits[(ucs >> 28) & 0x0000000F]; |
| 5947 | *p++ = hexdigits[(ucs >> 24) & 0x0000000F]; |
| 5948 | *p++ = hexdigits[(ucs >> 20) & 0x0000000F]; |
| 5949 | *p++ = hexdigits[(ucs >> 16) & 0x0000000F]; |
| 5950 | *p++ = hexdigits[(ucs >> 12) & 0x0000000F]; |
| 5951 | *p++ = hexdigits[(ucs >> 8) & 0x0000000F]; |
| 5952 | *p++ = hexdigits[(ucs >> 4) & 0x0000000F]; |
| 5953 | *p++ = hexdigits[ucs & 0x0000000F]; |
| 5954 | continue; |
| 5955 | } |
| 5956 | /* Fall through: isolated surrogates are copied as-is */ |
| 5957 | s--; |
| 5958 | size++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5959 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5960 | #endif |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 5961 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5962 | /* Map 16-bit characters to '\uxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 5963 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5964 | *p++ = '\\'; |
| 5965 | *p++ = 'u'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5966 | *p++ = hexdigits[(ch >> 12) & 0x000F]; |
| 5967 | *p++ = hexdigits[(ch >> 8) & 0x000F]; |
| 5968 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 5969 | *p++ = hexdigits[ch & 0x000F]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5970 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5971 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 5972 | /* Map special whitespace to '\t', \n', '\r' */ |
| 5973 | else if (ch == '\t') { |
| 5974 | *p++ = '\\'; |
| 5975 | *p++ = 't'; |
| 5976 | } |
| 5977 | else if (ch == '\n') { |
| 5978 | *p++ = '\\'; |
| 5979 | *p++ = 'n'; |
| 5980 | } |
| 5981 | else if (ch == '\r') { |
| 5982 | *p++ = '\\'; |
| 5983 | *p++ = 'r'; |
| 5984 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5985 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 5986 | /* Map non-printable US ASCII to '\xhh' */ |
Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 5987 | else if (ch < ' ' || ch >= 0x7F) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5988 | *p++ = '\\'; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 5989 | *p++ = 'x'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5990 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 5991 | *p++ = hexdigits[ch & 0x000F]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5992 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5993 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5994 | /* Copy everything else as-is */ |
| 5995 | else |
| 5996 | *p++ = (char) ch; |
| 5997 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5998 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5999 | assert(p - PyBytes_AS_STRING(repr) > 0); |
| 6000 | if (_PyBytes_Resize(&repr, p - PyBytes_AS_STRING(repr)) < 0) |
| 6001 | return NULL; |
| 6002 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6003 | } |
| 6004 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6005 | PyObject * |
| 6006 | PyUnicode_AsUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6007 | { |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 6008 | PyObject *s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6009 | if (!PyUnicode_Check(unicode)) { |
| 6010 | PyErr_BadArgument(); |
| 6011 | return NULL; |
| 6012 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 6013 | s = PyUnicode_EncodeUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 6014 | PyUnicode_GET_SIZE(unicode)); |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 6015 | return s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6016 | } |
| 6017 | |
| 6018 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ |
| 6019 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6020 | PyObject * |
| 6021 | PyUnicode_DecodeRawUnicodeEscape(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6022 | Py_ssize_t size, |
| 6023 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6024 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6025 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6026 | Py_ssize_t startinpos; |
| 6027 | Py_ssize_t endinpos; |
| 6028 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6029 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6030 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6031 | const char *end; |
| 6032 | const char *bs; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6033 | PyObject *errorHandler = NULL; |
| 6034 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6035 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6036 | /* Escaped strings will always be longer than the resulting |
| 6037 | 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] | 6038 | length after conversion to the true value. (But decoding error |
| 6039 | handler might have to resize the string) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6040 | v = _PyUnicode_New(size); |
| 6041 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6042 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6043 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6044 | return (PyObject *)v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6045 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6046 | end = s + size; |
| 6047 | while (s < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6048 | unsigned char c; |
| 6049 | Py_UCS4 x; |
| 6050 | int i; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6051 | int count; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6052 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6053 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 6054 | if (*s != '\\') { |
| 6055 | *p++ = (unsigned char)*s++; |
| 6056 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6057 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6058 | startinpos = s-starts; |
| 6059 | |
| 6060 | /* \u-escapes are only interpreted iff the number of leading |
| 6061 | backslashes if odd */ |
| 6062 | bs = s; |
| 6063 | for (;s < end;) { |
| 6064 | if (*s != '\\') |
| 6065 | break; |
| 6066 | *p++ = (unsigned char)*s++; |
| 6067 | } |
| 6068 | if (((s - bs) & 1) == 0 || |
| 6069 | s >= end || |
| 6070 | (*s != 'u' && *s != 'U')) { |
| 6071 | continue; |
| 6072 | } |
| 6073 | p--; |
| 6074 | count = *s=='u' ? 4 : 8; |
| 6075 | s++; |
| 6076 | |
| 6077 | /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ |
| 6078 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 6079 | for (x = 0, i = 0; i < count; ++i, ++s) { |
| 6080 | c = (unsigned char)*s; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 6081 | if (!Py_ISXDIGIT(c)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6082 | endinpos = s-starts; |
| 6083 | if (unicode_decode_call_errorhandler( |
| 6084 | errors, &errorHandler, |
| 6085 | "rawunicodeescape", "truncated \\uXXXX", |
| 6086 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 6087 | &v, &outpos, &p)) |
| 6088 | goto onError; |
| 6089 | goto nextByte; |
| 6090 | } |
| 6091 | x = (x<<4) & ~0xF; |
| 6092 | if (c >= '0' && c <= '9') |
| 6093 | x += c - '0'; |
| 6094 | else if (c >= 'a' && c <= 'f') |
| 6095 | x += 10 + c - 'a'; |
| 6096 | else |
| 6097 | x += 10 + c - 'A'; |
| 6098 | } |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 6099 | if (x <= 0xffff) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6100 | /* UCS-2 character */ |
| 6101 | *p++ = (Py_UNICODE) x; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 6102 | else if (x <= 0x10ffff) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6103 | /* UCS-4 character. Either store directly, or as |
| 6104 | surrogate pair. */ |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 6105 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6106 | *p++ = (Py_UNICODE) x; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 6107 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6108 | x -= 0x10000L; |
| 6109 | *p++ = 0xD800 + (Py_UNICODE) (x >> 10); |
| 6110 | *p++ = 0xDC00 + (Py_UNICODE) (x & 0x03FF); |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 6111 | #endif |
| 6112 | } else { |
| 6113 | endinpos = s-starts; |
| 6114 | outpos = p-PyUnicode_AS_UNICODE(v); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6115 | if (unicode_decode_call_errorhandler( |
| 6116 | errors, &errorHandler, |
| 6117 | "rawunicodeescape", "\\Uxxxxxxxx out of range", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6118 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 6119 | &v, &outpos, &p)) |
| 6120 | goto onError; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6121 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6122 | nextByte: |
| 6123 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6124 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 6125 | if (PyUnicode_Resize((PyObject**)&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6126 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6127 | Py_XDECREF(errorHandler); |
| 6128 | Py_XDECREF(exc); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 6129 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 6130 | if (_PyUnicode_READY_REPLACE(&v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6131 | Py_DECREF(v); |
| 6132 | return NULL; |
| 6133 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 6134 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 6135 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6136 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6137 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6138 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6139 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6140 | Py_XDECREF(errorHandler); |
| 6141 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6142 | return NULL; |
| 6143 | } |
| 6144 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6145 | PyObject * |
| 6146 | PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6147 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6148 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6149 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6150 | char *p; |
| 6151 | char *q; |
| 6152 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6153 | #ifdef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 6154 | const Py_ssize_t expandsize = 10; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6155 | #else |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 6156 | const Py_ssize_t expandsize = 6; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6157 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6158 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 6159 | if (size > PY_SSIZE_T_MAX / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6160 | return PyErr_NoMemory(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6161 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6162 | repr = PyBytes_FromStringAndSize(NULL, expandsize * size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6163 | if (repr == NULL) |
| 6164 | return NULL; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 6165 | if (size == 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6166 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6167 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6168 | p = q = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6169 | while (size-- > 0) { |
| 6170 | Py_UNICODE ch = *s++; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6171 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6172 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 6173 | if (ch >= 0x10000) { |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6174 | *p++ = '\\'; |
| 6175 | *p++ = 'U'; |
Walter Dörwald | db5d33e | 2007-05-12 11:13:47 +0000 | [diff] [blame] | 6176 | *p++ = hexdigits[(ch >> 28) & 0xf]; |
| 6177 | *p++ = hexdigits[(ch >> 24) & 0xf]; |
| 6178 | *p++ = hexdigits[(ch >> 20) & 0xf]; |
| 6179 | *p++ = hexdigits[(ch >> 16) & 0xf]; |
| 6180 | *p++ = hexdigits[(ch >> 12) & 0xf]; |
| 6181 | *p++ = hexdigits[(ch >> 8) & 0xf]; |
| 6182 | *p++ = hexdigits[(ch >> 4) & 0xf]; |
| 6183 | *p++ = hexdigits[ch & 15]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6184 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6185 | else |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 6186 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6187 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
| 6188 | if (ch >= 0xD800 && ch < 0xDC00) { |
| 6189 | Py_UNICODE ch2; |
| 6190 | Py_UCS4 ucs; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 6191 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6192 | ch2 = *s++; |
| 6193 | size--; |
Georg Brandl | 78eef3de | 2010-08-01 20:51:02 +0000 | [diff] [blame] | 6194 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6195 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 6196 | *p++ = '\\'; |
| 6197 | *p++ = 'U'; |
| 6198 | *p++ = hexdigits[(ucs >> 28) & 0xf]; |
| 6199 | *p++ = hexdigits[(ucs >> 24) & 0xf]; |
| 6200 | *p++ = hexdigits[(ucs >> 20) & 0xf]; |
| 6201 | *p++ = hexdigits[(ucs >> 16) & 0xf]; |
| 6202 | *p++ = hexdigits[(ucs >> 12) & 0xf]; |
| 6203 | *p++ = hexdigits[(ucs >> 8) & 0xf]; |
| 6204 | *p++ = hexdigits[(ucs >> 4) & 0xf]; |
| 6205 | *p++ = hexdigits[ucs & 0xf]; |
| 6206 | continue; |
| 6207 | } |
| 6208 | /* Fall through: isolated surrogates are copied as-is */ |
| 6209 | s--; |
| 6210 | size++; |
| 6211 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6212 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6213 | /* Map 16-bit characters to '\uxxxx' */ |
| 6214 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6215 | *p++ = '\\'; |
| 6216 | *p++ = 'u'; |
Walter Dörwald | db5d33e | 2007-05-12 11:13:47 +0000 | [diff] [blame] | 6217 | *p++ = hexdigits[(ch >> 12) & 0xf]; |
| 6218 | *p++ = hexdigits[(ch >> 8) & 0xf]; |
| 6219 | *p++ = hexdigits[(ch >> 4) & 0xf]; |
| 6220 | *p++ = hexdigits[ch & 15]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6221 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6222 | /* Copy everything else as-is */ |
| 6223 | else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6224 | *p++ = (char) ch; |
| 6225 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6226 | size = p - q; |
| 6227 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6228 | assert(size > 0); |
| 6229 | if (_PyBytes_Resize(&repr, size) < 0) |
| 6230 | return NULL; |
| 6231 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6232 | } |
| 6233 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6234 | PyObject * |
| 6235 | PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6236 | { |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 6237 | PyObject *s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6238 | if (!PyUnicode_Check(unicode)) { |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 6239 | PyErr_BadArgument(); |
| 6240 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6241 | } |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 6242 | s = PyUnicode_EncodeRawUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 6243 | PyUnicode_GET_SIZE(unicode)); |
| 6244 | |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 6245 | return s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6246 | } |
| 6247 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6248 | /* --- Unicode Internal Codec ------------------------------------------- */ |
| 6249 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6250 | PyObject * |
| 6251 | _PyUnicode_DecodeUnicodeInternal(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6252 | Py_ssize_t size, |
| 6253 | const char *errors) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6254 | { |
| 6255 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6256 | Py_ssize_t startinpos; |
| 6257 | Py_ssize_t endinpos; |
| 6258 | Py_ssize_t outpos; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6259 | PyUnicodeObject *v; |
| 6260 | Py_UNICODE *p; |
| 6261 | const char *end; |
| 6262 | const char *reason; |
| 6263 | PyObject *errorHandler = NULL; |
| 6264 | PyObject *exc = NULL; |
| 6265 | |
Neal Norwitz | d43069c | 2006-01-08 01:12:10 +0000 | [diff] [blame] | 6266 | #ifdef Py_UNICODE_WIDE |
| 6267 | Py_UNICODE unimax = PyUnicode_GetMax(); |
| 6268 | #endif |
| 6269 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6270 | /* XXX overflow detection missing */ |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6271 | v = _PyUnicode_New((size+Py_UNICODE_SIZE-1)/ Py_UNICODE_SIZE); |
| 6272 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6273 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6274 | /* Intentionally PyUnicode_GET_SIZE instead of PyUnicode_GET_LENGTH |
| 6275 | as string was created with the old API. */ |
| 6276 | if (PyUnicode_GET_SIZE(v) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6277 | return (PyObject *)v; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6278 | p = PyUnicode_AS_UNICODE(v); |
| 6279 | end = s + size; |
| 6280 | |
| 6281 | while (s < end) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6282 | memcpy(p, s, sizeof(Py_UNICODE)); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6283 | /* We have to sanity check the raw data, otherwise doom looms for |
| 6284 | some malformed UCS-4 data. */ |
| 6285 | if ( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6286 | #ifdef Py_UNICODE_WIDE |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6287 | *p > unimax || *p < 0 || |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6288 | #endif |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6289 | end-s < Py_UNICODE_SIZE |
| 6290 | ) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6291 | { |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6292 | startinpos = s - starts; |
| 6293 | if (end-s < Py_UNICODE_SIZE) { |
| 6294 | endinpos = end-starts; |
| 6295 | reason = "truncated input"; |
| 6296 | } |
| 6297 | else { |
| 6298 | endinpos = s - starts + Py_UNICODE_SIZE; |
| 6299 | reason = "illegal code point (> 0x10FFFF)"; |
| 6300 | } |
| 6301 | outpos = p - PyUnicode_AS_UNICODE(v); |
| 6302 | if (unicode_decode_call_errorhandler( |
| 6303 | errors, &errorHandler, |
| 6304 | "unicode_internal", reason, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 6305 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 6306 | &v, &outpos, &p)) { |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6307 | goto onError; |
| 6308 | } |
| 6309 | } |
| 6310 | else { |
| 6311 | p++; |
| 6312 | s += Py_UNICODE_SIZE; |
| 6313 | } |
| 6314 | } |
| 6315 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 6316 | if (PyUnicode_Resize((PyObject**)&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6317 | goto onError; |
| 6318 | Py_XDECREF(errorHandler); |
| 6319 | Py_XDECREF(exc); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 6320 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 6321 | if (_PyUnicode_READY_REPLACE(&v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6322 | Py_DECREF(v); |
| 6323 | return NULL; |
| 6324 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 6325 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 6326 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6327 | return (PyObject *)v; |
| 6328 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6329 | onError: |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6330 | Py_XDECREF(v); |
| 6331 | Py_XDECREF(errorHandler); |
| 6332 | Py_XDECREF(exc); |
| 6333 | return NULL; |
| 6334 | } |
| 6335 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6336 | /* --- Latin-1 Codec ------------------------------------------------------ */ |
| 6337 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6338 | PyObject * |
| 6339 | PyUnicode_DecodeLatin1(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6340 | Py_ssize_t size, |
| 6341 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6342 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6343 | /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 6344 | return _PyUnicode_FromUCS1((unsigned char*)s, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6345 | } |
| 6346 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6347 | /* create or adjust a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6348 | static void |
| 6349 | make_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6350 | const char *encoding, |
| 6351 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 6352 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6353 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6354 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6355 | if (*exceptionObject == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6356 | *exceptionObject = PyUnicodeEncodeError_Create( |
| 6357 | encoding, unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6358 | } |
| 6359 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6360 | if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 6361 | goto onError; |
| 6362 | if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) |
| 6363 | goto onError; |
| 6364 | if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) |
| 6365 | goto onError; |
| 6366 | return; |
| 6367 | onError: |
| 6368 | Py_DECREF(*exceptionObject); |
| 6369 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6370 | } |
| 6371 | } |
| 6372 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6373 | /* raises a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6374 | static void |
| 6375 | raise_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6376 | const char *encoding, |
| 6377 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 6378 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6379 | const char *reason) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6380 | { |
| 6381 | make_encode_exception(exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6382 | encoding, unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6383 | if (*exceptionObject != NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6384 | PyCodec_StrictErrors(*exceptionObject); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6385 | } |
| 6386 | |
| 6387 | /* error handling callback helper: |
| 6388 | build arguments, call the callback and check the arguments, |
| 6389 | put the result into newpos and return the replacement string, which |
| 6390 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6391 | static PyObject * |
| 6392 | unicode_encode_call_errorhandler(const char *errors, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6393 | PyObject **errorHandler, |
| 6394 | const char *encoding, const char *reason, |
| 6395 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 6396 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6397 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6398 | { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6399 | 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] | 6400 | |
| 6401 | PyObject *restuple; |
| 6402 | PyObject *resunicode; |
| 6403 | |
| 6404 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6405 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6406 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6407 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6408 | } |
| 6409 | |
| 6410 | make_encode_exception(exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6411 | encoding, unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6412 | if (*exceptionObject == 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 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6416 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6417 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6418 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6419 | if (!PyTuple_Check(restuple)) { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6420 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6421 | Py_DECREF(restuple); |
| 6422 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6423 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6424 | if (!PyArg_ParseTuple(restuple, argparse, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6425 | &resunicode, newpos)) { |
| 6426 | Py_DECREF(restuple); |
| 6427 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6428 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6429 | if (!PyUnicode_Check(resunicode) && !PyBytes_Check(resunicode)) { |
| 6430 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
| 6431 | Py_DECREF(restuple); |
| 6432 | return NULL; |
| 6433 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6434 | if (*newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6435 | *newpos = size+*newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 6436 | if (*newpos<0 || *newpos>size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6437 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 6438 | Py_DECREF(restuple); |
| 6439 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 6440 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6441 | Py_INCREF(resunicode); |
| 6442 | Py_DECREF(restuple); |
| 6443 | return resunicode; |
| 6444 | } |
| 6445 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6446 | static PyObject * |
| 6447 | unicode_encode_ucs1(const Py_UNICODE *p, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6448 | Py_ssize_t size, |
| 6449 | const char *errors, |
| 6450 | int limit) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6451 | { |
| 6452 | /* output object */ |
| 6453 | PyObject *res; |
| 6454 | /* pointers to the beginning and end+1 of input */ |
| 6455 | const Py_UNICODE *startp = p; |
| 6456 | const Py_UNICODE *endp = p + size; |
| 6457 | /* pointer to the beginning of the unencodable characters */ |
| 6458 | /* const Py_UNICODE *badp = NULL; */ |
| 6459 | /* pointer into the output */ |
| 6460 | char *str; |
| 6461 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6462 | Py_ssize_t ressize; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6463 | const char *encoding = (limit == 256) ? "latin-1" : "ascii"; |
| 6464 | 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] | 6465 | PyObject *errorHandler = NULL; |
| 6466 | PyObject *exc = NULL; |
| 6467 | /* the following variable is used for caching string comparisons |
| 6468 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 6469 | int known_errorHandler = -1; |
| 6470 | |
| 6471 | /* allocate enough for a simple encoding without |
| 6472 | replacements, if we need more, we'll resize */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6473 | if (size == 0) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 6474 | return PyBytes_FromStringAndSize(NULL, 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6475 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6476 | if (res == NULL) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6477 | return NULL; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6478 | str = PyBytes_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6479 | ressize = size; |
| 6480 | |
| 6481 | while (p<endp) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6482 | Py_UNICODE c = *p; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6483 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6484 | /* can we encode this? */ |
| 6485 | if (c<limit) { |
| 6486 | /* no overflow check, because we know that the space is enough */ |
| 6487 | *str++ = (char)c; |
| 6488 | ++p; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6489 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6490 | else { |
| 6491 | Py_ssize_t unicodepos = p-startp; |
| 6492 | Py_ssize_t requiredsize; |
| 6493 | PyObject *repunicode; |
| 6494 | Py_ssize_t repsize; |
| 6495 | Py_ssize_t newpos; |
| 6496 | Py_ssize_t respos; |
| 6497 | Py_UNICODE *uni2; |
| 6498 | /* startpos for collecting unencodable chars */ |
| 6499 | const Py_UNICODE *collstart = p; |
| 6500 | const Py_UNICODE *collend = p; |
| 6501 | /* find all unecodable characters */ |
| 6502 | while ((collend < endp) && ((*collend)>=limit)) |
| 6503 | ++collend; |
| 6504 | /* cache callback name lookup (if not done yet, i.e. it's the first error) */ |
| 6505 | if (known_errorHandler==-1) { |
| 6506 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 6507 | known_errorHandler = 1; |
| 6508 | else if (!strcmp(errors, "replace")) |
| 6509 | known_errorHandler = 2; |
| 6510 | else if (!strcmp(errors, "ignore")) |
| 6511 | known_errorHandler = 3; |
| 6512 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 6513 | known_errorHandler = 4; |
| 6514 | else |
| 6515 | known_errorHandler = 0; |
| 6516 | } |
| 6517 | switch (known_errorHandler) { |
| 6518 | case 1: /* strict */ |
| 6519 | raise_encode_exception(&exc, encoding, startp, size, collstart-startp, collend-startp, reason); |
| 6520 | goto onError; |
| 6521 | case 2: /* replace */ |
| 6522 | while (collstart++<collend) |
| 6523 | *str++ = '?'; /* fall through */ |
| 6524 | case 3: /* ignore */ |
| 6525 | p = collend; |
| 6526 | break; |
| 6527 | case 4: /* xmlcharrefreplace */ |
| 6528 | respos = str - PyBytes_AS_STRING(res); |
| 6529 | /* determine replacement size (temporarily (mis)uses p) */ |
| 6530 | for (p = collstart, repsize = 0; p < collend; ++p) { |
| 6531 | if (*p<10) |
| 6532 | repsize += 2+1+1; |
| 6533 | else if (*p<100) |
| 6534 | repsize += 2+2+1; |
| 6535 | else if (*p<1000) |
| 6536 | repsize += 2+3+1; |
| 6537 | else if (*p<10000) |
| 6538 | repsize += 2+4+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 6539 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6540 | else |
| 6541 | repsize += 2+5+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 6542 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6543 | else if (*p<100000) |
| 6544 | repsize += 2+5+1; |
| 6545 | else if (*p<1000000) |
| 6546 | repsize += 2+6+1; |
| 6547 | else |
| 6548 | repsize += 2+7+1; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 6549 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6550 | } |
| 6551 | requiredsize = respos+repsize+(endp-collend); |
| 6552 | if (requiredsize > ressize) { |
| 6553 | if (requiredsize<2*ressize) |
| 6554 | requiredsize = 2*ressize; |
| 6555 | if (_PyBytes_Resize(&res, requiredsize)) |
| 6556 | goto onError; |
| 6557 | str = PyBytes_AS_STRING(res) + respos; |
| 6558 | ressize = requiredsize; |
| 6559 | } |
| 6560 | /* generate replacement (temporarily (mis)uses p) */ |
| 6561 | for (p = collstart; p < collend; ++p) { |
| 6562 | str += sprintf(str, "&#%d;", (int)*p); |
| 6563 | } |
| 6564 | p = collend; |
| 6565 | break; |
| 6566 | default: |
| 6567 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 6568 | encoding, reason, startp, size, &exc, |
| 6569 | collstart-startp, collend-startp, &newpos); |
| 6570 | if (repunicode == NULL) |
| 6571 | goto onError; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6572 | if (PyBytes_Check(repunicode)) { |
| 6573 | /* Directly copy bytes result to output. */ |
| 6574 | repsize = PyBytes_Size(repunicode); |
| 6575 | if (repsize > 1) { |
| 6576 | /* Make room for all additional bytes. */ |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 6577 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6578 | if (_PyBytes_Resize(&res, ressize+repsize-1)) { |
| 6579 | Py_DECREF(repunicode); |
| 6580 | goto onError; |
| 6581 | } |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 6582 | str = PyBytes_AS_STRING(res) + respos; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6583 | ressize += repsize-1; |
| 6584 | } |
| 6585 | memcpy(str, PyBytes_AsString(repunicode), repsize); |
| 6586 | str += repsize; |
| 6587 | p = startp + newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6588 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6589 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6590 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6591 | /* need more space? (at least enough for what we |
| 6592 | have+the replacement+the rest of the string, so |
| 6593 | we won't have to check space for encodable characters) */ |
| 6594 | respos = str - PyBytes_AS_STRING(res); |
| 6595 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 6596 | requiredsize = respos+repsize+(endp-collend); |
| 6597 | if (requiredsize > ressize) { |
| 6598 | if (requiredsize<2*ressize) |
| 6599 | requiredsize = 2*ressize; |
| 6600 | if (_PyBytes_Resize(&res, requiredsize)) { |
| 6601 | Py_DECREF(repunicode); |
| 6602 | goto onError; |
| 6603 | } |
| 6604 | str = PyBytes_AS_STRING(res) + respos; |
| 6605 | ressize = requiredsize; |
| 6606 | } |
| 6607 | /* check if there is anything unencodable in the replacement |
| 6608 | and copy it to the output */ |
| 6609 | for (uni2 = PyUnicode_AS_UNICODE(repunicode);repsize-->0; ++uni2, ++str) { |
| 6610 | c = *uni2; |
| 6611 | if (c >= limit) { |
| 6612 | raise_encode_exception(&exc, encoding, startp, size, |
| 6613 | unicodepos, unicodepos+1, reason); |
| 6614 | Py_DECREF(repunicode); |
| 6615 | goto onError; |
| 6616 | } |
| 6617 | *str = (char)c; |
| 6618 | } |
| 6619 | p = startp + newpos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6620 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6621 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6622 | } |
| 6623 | } |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6624 | /* Resize if we allocated to much */ |
| 6625 | size = str - PyBytes_AS_STRING(res); |
| 6626 | if (size < ressize) { /* If this falls res will be NULL */ |
Alexandre Vassalotti | bad1b92 | 2008-12-27 09:49:09 +0000 | [diff] [blame] | 6627 | assert(size >= 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6628 | if (_PyBytes_Resize(&res, size) < 0) |
| 6629 | goto onError; |
| 6630 | } |
| 6631 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6632 | Py_XDECREF(errorHandler); |
| 6633 | Py_XDECREF(exc); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6634 | return res; |
| 6635 | |
| 6636 | onError: |
| 6637 | Py_XDECREF(res); |
| 6638 | Py_XDECREF(errorHandler); |
| 6639 | Py_XDECREF(exc); |
| 6640 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6641 | } |
| 6642 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6643 | PyObject * |
| 6644 | PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6645 | Py_ssize_t size, |
| 6646 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6647 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6648 | return unicode_encode_ucs1(p, size, errors, 256); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6649 | } |
| 6650 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6651 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6652 | _PyUnicode_AsLatin1String(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6653 | { |
| 6654 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6655 | PyErr_BadArgument(); |
| 6656 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6657 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6658 | if (PyUnicode_READY(unicode) == -1) |
| 6659 | return NULL; |
| 6660 | /* Fast path: if it is a one-byte string, construct |
| 6661 | bytes object directly. */ |
| 6662 | if (PyUnicode_KIND(unicode) == PyUnicode_1BYTE_KIND) |
| 6663 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 6664 | PyUnicode_GET_LENGTH(unicode)); |
| 6665 | /* Non-Latin-1 characters present. Defer to above function to |
| 6666 | raise the exception. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6667 | return PyUnicode_EncodeLatin1(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6668 | PyUnicode_GET_SIZE(unicode), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6669 | errors); |
| 6670 | } |
| 6671 | |
| 6672 | PyObject* |
| 6673 | PyUnicode_AsLatin1String(PyObject *unicode) |
| 6674 | { |
| 6675 | return _PyUnicode_AsLatin1String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6676 | } |
| 6677 | |
| 6678 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ |
| 6679 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6680 | PyObject * |
| 6681 | PyUnicode_DecodeASCII(const char *s, |
| 6682 | Py_ssize_t size, |
| 6683 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6684 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6685 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6686 | PyUnicodeObject *v; |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6687 | Py_UNICODE *u; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6688 | Py_ssize_t startinpos; |
| 6689 | Py_ssize_t endinpos; |
| 6690 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6691 | const char *e; |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6692 | int has_error; |
| 6693 | const unsigned char *p = (const unsigned char *)s; |
| 6694 | const unsigned char *end = p + size; |
| 6695 | 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] | 6696 | PyObject *errorHandler = NULL; |
| 6697 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6698 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6699 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6700 | if (size == 1 && (unsigned char)s[0] < 128) |
| 6701 | return get_latin1_char((unsigned char)s[0]); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6702 | |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6703 | has_error = 0; |
| 6704 | while (p < end && !has_error) { |
| 6705 | /* Fast path, see below in PyUnicode_DecodeUTF8Stateful for |
| 6706 | an explanation. */ |
| 6707 | if (!((size_t) p & LONG_PTR_MASK)) { |
| 6708 | /* Help register allocation */ |
| 6709 | register const unsigned char *_p = p; |
| 6710 | while (_p < aligned_end) { |
| 6711 | unsigned long value = *(unsigned long *) _p; |
| 6712 | if (value & ASCII_CHAR_MASK) { |
| 6713 | has_error = 1; |
| 6714 | break; |
| 6715 | } |
| 6716 | _p += SIZEOF_LONG; |
| 6717 | } |
| 6718 | if (_p == end) |
| 6719 | break; |
| 6720 | if (has_error) |
| 6721 | break; |
| 6722 | p = _p; |
| 6723 | } |
| 6724 | if (*p & 0x80) { |
| 6725 | has_error = 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6726 | break; |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6727 | } |
| 6728 | else { |
| 6729 | ++p; |
| 6730 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 6731 | } |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6732 | if (!has_error) |
| 6733 | return unicode_fromascii((const unsigned char *)s, size); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6734 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6735 | v = _PyUnicode_New(size); |
| 6736 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6737 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6738 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6739 | return (PyObject *)v; |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6740 | u = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6741 | e = s + size; |
| 6742 | while (s < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6743 | register unsigned char c = (unsigned char)*s; |
| 6744 | if (c < 128) { |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6745 | *u++ = c; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6746 | ++s; |
| 6747 | } |
| 6748 | else { |
| 6749 | startinpos = s-starts; |
| 6750 | endinpos = startinpos + 1; |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6751 | outpos = u - (Py_UNICODE *)PyUnicode_AS_UNICODE(v); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6752 | if (unicode_decode_call_errorhandler( |
| 6753 | errors, &errorHandler, |
| 6754 | "ascii", "ordinal not in range(128)", |
| 6755 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6756 | &v, &outpos, &u)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6757 | goto onError; |
| 6758 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6759 | } |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6760 | if (u - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
| 6761 | if (PyUnicode_Resize((PyObject**)&v, u - PyUnicode_AS_UNICODE(v)) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6762 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6763 | Py_XDECREF(errorHandler); |
| 6764 | Py_XDECREF(exc); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 6765 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 6766 | if (_PyUnicode_READY_REPLACE(&v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6767 | Py_DECREF(v); |
| 6768 | return NULL; |
| 6769 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 6770 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 6771 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6772 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6773 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6774 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6775 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6776 | Py_XDECREF(errorHandler); |
| 6777 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6778 | return NULL; |
| 6779 | } |
| 6780 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6781 | PyObject * |
| 6782 | PyUnicode_EncodeASCII(const Py_UNICODE *p, |
| 6783 | Py_ssize_t size, |
| 6784 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6785 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6786 | return unicode_encode_ucs1(p, size, errors, 128); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6787 | } |
| 6788 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6789 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6790 | _PyUnicode_AsASCIIString(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6791 | { |
| 6792 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6793 | PyErr_BadArgument(); |
| 6794 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6795 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6796 | if (PyUnicode_READY(unicode) == -1) |
| 6797 | return NULL; |
| 6798 | /* Fast path: if it is an ASCII-only string, construct bytes object |
| 6799 | directly. Else defer to above function to raise the exception. */ |
| 6800 | if (PyUnicode_MAX_CHAR_VALUE(unicode) < 128) |
| 6801 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 6802 | PyUnicode_GET_LENGTH(unicode)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6803 | return PyUnicode_EncodeASCII(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6804 | PyUnicode_GET_SIZE(unicode), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6805 | errors); |
| 6806 | } |
| 6807 | |
| 6808 | PyObject * |
| 6809 | PyUnicode_AsASCIIString(PyObject *unicode) |
| 6810 | { |
| 6811 | return _PyUnicode_AsASCIIString(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6812 | } |
| 6813 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 6814 | #ifdef HAVE_MBCS |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6815 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6816 | /* --- MBCS codecs for Windows -------------------------------------------- */ |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6817 | |
Hirokazu Yamamoto | 3530246 | 2009-03-21 13:23:27 +0000 | [diff] [blame] | 6818 | #if SIZEOF_INT < SIZEOF_SIZE_T |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6819 | #define NEED_RETRY |
| 6820 | #endif |
| 6821 | |
| 6822 | /* XXX This code is limited to "true" double-byte encodings, as |
| 6823 | a) it assumes an incomplete character consists of a single byte, and |
| 6824 | b) IsDBCSLeadByte (probably) does not work for non-DBCS multi-byte |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6825 | encodings, see IsDBCSLeadByteEx documentation. */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6826 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6827 | static int |
| 6828 | is_dbcs_lead_byte(const char *s, int offset) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6829 | { |
| 6830 | const char *curr = s + offset; |
| 6831 | |
| 6832 | if (IsDBCSLeadByte(*curr)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6833 | const char *prev = CharPrev(s, curr); |
| 6834 | return (prev == curr) || !IsDBCSLeadByte(*prev) || (curr - prev == 2); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6835 | } |
| 6836 | return 0; |
| 6837 | } |
| 6838 | |
| 6839 | /* |
| 6840 | * Decode MBCS string into unicode object. If 'final' is set, converts |
| 6841 | * trailing lead-byte too. Returns consumed size if succeed, -1 otherwise. |
| 6842 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6843 | static int |
| 6844 | decode_mbcs(PyUnicodeObject **v, |
| 6845 | const char *s, /* MBCS string */ |
| 6846 | int size, /* sizeof MBCS string */ |
| 6847 | int final, |
| 6848 | const char *errors) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6849 | { |
| 6850 | Py_UNICODE *p; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6851 | Py_ssize_t n; |
| 6852 | DWORD usize; |
| 6853 | DWORD flags; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6854 | |
| 6855 | assert(size >= 0); |
| 6856 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6857 | /* check and handle 'errors' arg */ |
| 6858 | if (errors==NULL || strcmp(errors, "strict")==0) |
| 6859 | flags = MB_ERR_INVALID_CHARS; |
| 6860 | else if (strcmp(errors, "ignore")==0) |
| 6861 | flags = 0; |
| 6862 | else { |
| 6863 | PyErr_Format(PyExc_ValueError, |
| 6864 | "mbcs encoding does not support errors='%s'", |
| 6865 | errors); |
| 6866 | return -1; |
| 6867 | } |
| 6868 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6869 | /* Skip trailing lead-byte unless 'final' is set */ |
| 6870 | if (!final && size >= 1 && is_dbcs_lead_byte(s, size - 1)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6871 | --size; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6872 | |
| 6873 | /* First get the size of the result */ |
| 6874 | if (size > 0) { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6875 | usize = MultiByteToWideChar(CP_ACP, flags, s, size, NULL, 0); |
| 6876 | if (usize==0) |
| 6877 | goto mbcs_decode_error; |
| 6878 | } else |
| 6879 | usize = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6880 | |
| 6881 | if (*v == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6882 | /* Create unicode object */ |
| 6883 | *v = _PyUnicode_New(usize); |
| 6884 | if (*v == NULL) |
| 6885 | return -1; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6886 | n = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6887 | } |
| 6888 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6889 | /* Extend unicode object */ |
| 6890 | n = PyUnicode_GET_SIZE(*v); |
Victor Stinner | 2fd8227 | 2011-10-03 04:06:05 +0200 | [diff] [blame] | 6891 | if (PyUnicode_Resize((PyObject**)v, n + usize) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6892 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6893 | } |
| 6894 | |
| 6895 | /* Do the conversion */ |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6896 | if (usize > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6897 | p = PyUnicode_AS_UNICODE(*v) + n; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6898 | if (0 == MultiByteToWideChar(CP_ACP, flags, s, size, p, usize)) { |
| 6899 | goto mbcs_decode_error; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6900 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6901 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6902 | return size; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6903 | |
| 6904 | mbcs_decode_error: |
| 6905 | /* If the last error was ERROR_NO_UNICODE_TRANSLATION, then |
| 6906 | we raise a UnicodeDecodeError - else it is a 'generic' |
| 6907 | windows error |
| 6908 | */ |
| 6909 | if (GetLastError()==ERROR_NO_UNICODE_TRANSLATION) { |
| 6910 | /* Ideally, we should get reason from FormatMessage - this |
| 6911 | is the Windows 2000 English version of the message |
| 6912 | */ |
| 6913 | PyObject *exc = NULL; |
| 6914 | const char *reason = "No mapping for the Unicode character exists " |
| 6915 | "in the target multi-byte code page."; |
| 6916 | make_decode_exception(&exc, "mbcs", s, size, 0, 0, reason); |
| 6917 | if (exc != NULL) { |
| 6918 | PyCodec_StrictErrors(exc); |
| 6919 | Py_DECREF(exc); |
| 6920 | } |
| 6921 | } else { |
| 6922 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 6923 | } |
| 6924 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6925 | } |
| 6926 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6927 | PyObject * |
| 6928 | PyUnicode_DecodeMBCSStateful(const char *s, |
| 6929 | Py_ssize_t size, |
| 6930 | const char *errors, |
| 6931 | Py_ssize_t *consumed) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6932 | { |
| 6933 | PyUnicodeObject *v = NULL; |
| 6934 | int done; |
| 6935 | |
| 6936 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6937 | *consumed = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6938 | |
| 6939 | #ifdef NEED_RETRY |
| 6940 | retry: |
| 6941 | if (size > INT_MAX) |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6942 | done = decode_mbcs(&v, s, INT_MAX, 0, errors); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6943 | else |
| 6944 | #endif |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6945 | done = decode_mbcs(&v, s, (int)size, !consumed, errors); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6946 | |
| 6947 | if (done < 0) { |
| 6948 | Py_XDECREF(v); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6949 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6950 | } |
| 6951 | |
| 6952 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6953 | *consumed += done; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6954 | |
| 6955 | #ifdef NEED_RETRY |
| 6956 | if (size > INT_MAX) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6957 | s += done; |
| 6958 | size -= done; |
| 6959 | goto retry; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6960 | } |
| 6961 | #endif |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 6962 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 6963 | if (_PyUnicode_READY_REPLACE(&v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6964 | Py_DECREF(v); |
| 6965 | return NULL; |
| 6966 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 6967 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 6968 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6969 | return (PyObject *)v; |
| 6970 | } |
| 6971 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6972 | PyObject * |
| 6973 | PyUnicode_DecodeMBCS(const char *s, |
| 6974 | Py_ssize_t size, |
| 6975 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6976 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6977 | return PyUnicode_DecodeMBCSStateful(s, size, errors, NULL); |
| 6978 | } |
| 6979 | |
| 6980 | /* |
| 6981 | * Convert unicode into string object (MBCS). |
| 6982 | * Returns 0 if succeed, -1 otherwise. |
| 6983 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6984 | static int |
| 6985 | encode_mbcs(PyObject **repr, |
| 6986 | const Py_UNICODE *p, /* unicode */ |
| 6987 | int size, /* size of unicode */ |
| 6988 | const char* errors) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6989 | { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6990 | BOOL usedDefaultChar = FALSE; |
| 6991 | BOOL *pusedDefaultChar; |
| 6992 | int mbcssize; |
| 6993 | Py_ssize_t n; |
| 6994 | PyObject *exc = NULL; |
| 6995 | DWORD flags; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6996 | |
| 6997 | assert(size >= 0); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6998 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6999 | /* check and handle 'errors' arg */ |
| 7000 | if (errors==NULL || strcmp(errors, "strict")==0) { |
| 7001 | flags = WC_NO_BEST_FIT_CHARS; |
| 7002 | pusedDefaultChar = &usedDefaultChar; |
| 7003 | } else if (strcmp(errors, "replace")==0) { |
| 7004 | flags = 0; |
| 7005 | pusedDefaultChar = NULL; |
| 7006 | } else { |
| 7007 | PyErr_Format(PyExc_ValueError, |
| 7008 | "mbcs encoding does not support errors='%s'", |
| 7009 | errors); |
| 7010 | return -1; |
| 7011 | } |
| 7012 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7013 | /* First get the size of the result */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7014 | if (size > 0) { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7015 | mbcssize = WideCharToMultiByte(CP_ACP, flags, p, size, NULL, 0, |
| 7016 | NULL, pusedDefaultChar); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7017 | if (mbcssize == 0) { |
| 7018 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 7019 | return -1; |
| 7020 | } |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7021 | /* If we used a default char, then we failed! */ |
| 7022 | if (pusedDefaultChar && *pusedDefaultChar) |
| 7023 | goto mbcs_encode_error; |
| 7024 | } else { |
| 7025 | mbcssize = 0; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7026 | } |
| 7027 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7028 | if (*repr == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7029 | /* Create string object */ |
| 7030 | *repr = PyBytes_FromStringAndSize(NULL, mbcssize); |
| 7031 | if (*repr == NULL) |
| 7032 | return -1; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7033 | n = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7034 | } |
| 7035 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7036 | /* Extend string object */ |
| 7037 | n = PyBytes_Size(*repr); |
| 7038 | if (_PyBytes_Resize(repr, n + mbcssize) < 0) |
| 7039 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7040 | } |
| 7041 | |
| 7042 | /* Do the conversion */ |
| 7043 | if (size > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7044 | char *s = PyBytes_AS_STRING(*repr) + n; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7045 | if (0 == WideCharToMultiByte(CP_ACP, flags, p, size, s, mbcssize, |
| 7046 | NULL, pusedDefaultChar)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7047 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 7048 | return -1; |
| 7049 | } |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7050 | if (pusedDefaultChar && *pusedDefaultChar) |
| 7051 | goto mbcs_encode_error; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7052 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7053 | return 0; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7054 | |
| 7055 | mbcs_encode_error: |
| 7056 | raise_encode_exception(&exc, "mbcs", p, size, 0, 0, "invalid character"); |
| 7057 | Py_XDECREF(exc); |
| 7058 | return -1; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7059 | } |
| 7060 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7061 | PyObject * |
| 7062 | PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
| 7063 | Py_ssize_t size, |
| 7064 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7065 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7066 | PyObject *repr = NULL; |
| 7067 | int ret; |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 7068 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7069 | #ifdef NEED_RETRY |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7070 | retry: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7071 | if (size > INT_MAX) |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7072 | ret = encode_mbcs(&repr, p, INT_MAX, errors); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7073 | else |
| 7074 | #endif |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7075 | ret = encode_mbcs(&repr, p, (int)size, errors); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7076 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7077 | if (ret < 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7078 | Py_XDECREF(repr); |
| 7079 | return NULL; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7080 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7081 | |
| 7082 | #ifdef NEED_RETRY |
| 7083 | if (size > INT_MAX) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7084 | p += INT_MAX; |
| 7085 | size -= INT_MAX; |
| 7086 | goto retry; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7087 | } |
| 7088 | #endif |
| 7089 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7090 | return repr; |
| 7091 | } |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 7092 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7093 | PyObject * |
| 7094 | PyUnicode_AsMBCSString(PyObject *unicode) |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 7095 | { |
| 7096 | if (!PyUnicode_Check(unicode)) { |
| 7097 | PyErr_BadArgument(); |
| 7098 | return NULL; |
| 7099 | } |
| 7100 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7101 | PyUnicode_GET_SIZE(unicode), |
| 7102 | NULL); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 7103 | } |
| 7104 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7105 | #undef NEED_RETRY |
| 7106 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 7107 | #endif /* HAVE_MBCS */ |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7108 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7109 | /* --- Character Mapping Codec -------------------------------------------- */ |
| 7110 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7111 | PyObject * |
| 7112 | PyUnicode_DecodeCharmap(const char *s, |
| 7113 | Py_ssize_t size, |
| 7114 | PyObject *mapping, |
| 7115 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7116 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7117 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7118 | Py_ssize_t startinpos; |
| 7119 | Py_ssize_t endinpos; |
| 7120 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7121 | const char *e; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7122 | PyUnicodeObject *v; |
| 7123 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7124 | Py_ssize_t extrachars = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7125 | PyObject *errorHandler = NULL; |
| 7126 | PyObject *exc = NULL; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7127 | Py_UNICODE *mapstring = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7128 | Py_ssize_t maplen = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7129 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7130 | /* Default to Latin-1 */ |
| 7131 | if (mapping == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7132 | return PyUnicode_DecodeLatin1(s, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7133 | |
| 7134 | v = _PyUnicode_New(size); |
| 7135 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7136 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7137 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7138 | return (PyObject *)v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7139 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7140 | e = s + size; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7141 | if (PyUnicode_CheckExact(mapping)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7142 | mapstring = PyUnicode_AS_UNICODE(mapping); |
| 7143 | maplen = PyUnicode_GET_SIZE(mapping); |
| 7144 | while (s < e) { |
| 7145 | unsigned char ch = *s; |
| 7146 | Py_UNICODE x = 0xfffe; /* illegal value */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7147 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7148 | if (ch < maplen) |
| 7149 | x = mapstring[ch]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7150 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7151 | if (x == 0xfffe) { |
| 7152 | /* undefined mapping */ |
| 7153 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 7154 | startinpos = s-starts; |
| 7155 | endinpos = startinpos+1; |
| 7156 | if (unicode_decode_call_errorhandler( |
| 7157 | errors, &errorHandler, |
| 7158 | "charmap", "character maps to <undefined>", |
| 7159 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 7160 | &v, &outpos, &p)) { |
| 7161 | goto onError; |
| 7162 | } |
| 7163 | continue; |
| 7164 | } |
| 7165 | *p++ = x; |
| 7166 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7167 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7168 | } |
| 7169 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7170 | while (s < e) { |
| 7171 | unsigned char ch = *s; |
| 7172 | PyObject *w, *x; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7173 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7174 | /* Get mapping (char ordinal -> integer, Unicode char or None) */ |
| 7175 | w = PyLong_FromLong((long)ch); |
| 7176 | if (w == NULL) |
| 7177 | goto onError; |
| 7178 | x = PyObject_GetItem(mapping, w); |
| 7179 | Py_DECREF(w); |
| 7180 | if (x == NULL) { |
| 7181 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 7182 | /* No mapping found means: mapping is undefined. */ |
| 7183 | PyErr_Clear(); |
| 7184 | x = Py_None; |
| 7185 | Py_INCREF(x); |
| 7186 | } else |
| 7187 | goto onError; |
| 7188 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7189 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7190 | /* Apply mapping */ |
| 7191 | if (PyLong_Check(x)) { |
| 7192 | long value = PyLong_AS_LONG(x); |
| 7193 | if (value < 0 || value > 65535) { |
| 7194 | PyErr_SetString(PyExc_TypeError, |
| 7195 | "character mapping must be in range(65536)"); |
| 7196 | Py_DECREF(x); |
| 7197 | goto onError; |
| 7198 | } |
| 7199 | *p++ = (Py_UNICODE)value; |
| 7200 | } |
| 7201 | else if (x == Py_None) { |
| 7202 | /* undefined mapping */ |
| 7203 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 7204 | startinpos = s-starts; |
| 7205 | endinpos = startinpos+1; |
| 7206 | if (unicode_decode_call_errorhandler( |
| 7207 | errors, &errorHandler, |
| 7208 | "charmap", "character maps to <undefined>", |
| 7209 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 7210 | &v, &outpos, &p)) { |
| 7211 | Py_DECREF(x); |
| 7212 | goto onError; |
| 7213 | } |
| 7214 | Py_DECREF(x); |
| 7215 | continue; |
| 7216 | } |
| 7217 | else if (PyUnicode_Check(x)) { |
| 7218 | Py_ssize_t targetsize = PyUnicode_GET_SIZE(x); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7219 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7220 | if (targetsize == 1) |
| 7221 | /* 1-1 mapping */ |
| 7222 | *p++ = *PyUnicode_AS_UNICODE(x); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7223 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7224 | else if (targetsize > 1) { |
| 7225 | /* 1-n mapping */ |
| 7226 | if (targetsize > extrachars) { |
| 7227 | /* resize first */ |
| 7228 | Py_ssize_t oldpos = p - PyUnicode_AS_UNICODE(v); |
| 7229 | Py_ssize_t needed = (targetsize - extrachars) + \ |
| 7230 | (targetsize << 2); |
| 7231 | extrachars += needed; |
| 7232 | /* XXX overflow detection missing */ |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 7233 | if (PyUnicode_Resize((PyObject**)&v, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7234 | PyUnicode_GET_SIZE(v) + needed) < 0) { |
| 7235 | Py_DECREF(x); |
| 7236 | goto onError; |
| 7237 | } |
| 7238 | p = PyUnicode_AS_UNICODE(v) + oldpos; |
| 7239 | } |
| 7240 | Py_UNICODE_COPY(p, |
| 7241 | PyUnicode_AS_UNICODE(x), |
| 7242 | targetsize); |
| 7243 | p += targetsize; |
| 7244 | extrachars -= targetsize; |
| 7245 | } |
| 7246 | /* 1-0 mapping: skip the character */ |
| 7247 | } |
| 7248 | else { |
| 7249 | /* wrong return value */ |
| 7250 | PyErr_SetString(PyExc_TypeError, |
| 7251 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7252 | Py_DECREF(x); |
| 7253 | goto onError; |
| 7254 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7255 | Py_DECREF(x); |
| 7256 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7257 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7258 | } |
| 7259 | if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 7260 | if (PyUnicode_Resize((PyObject**)&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7261 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7262 | Py_XDECREF(errorHandler); |
| 7263 | Py_XDECREF(exc); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 7264 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 7265 | if (_PyUnicode_READY_REPLACE(&v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7266 | Py_DECREF(v); |
| 7267 | return NULL; |
| 7268 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 7269 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 7270 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7271 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7272 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7273 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7274 | Py_XDECREF(errorHandler); |
| 7275 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7276 | Py_XDECREF(v); |
| 7277 | return NULL; |
| 7278 | } |
| 7279 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7280 | /* Charmap encoding: the lookup table */ |
| 7281 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7282 | struct encoding_map { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7283 | PyObject_HEAD |
| 7284 | unsigned char level1[32]; |
| 7285 | int count2, count3; |
| 7286 | unsigned char level23[1]; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7287 | }; |
| 7288 | |
| 7289 | static PyObject* |
| 7290 | encoding_map_size(PyObject *obj, PyObject* args) |
| 7291 | { |
| 7292 | struct encoding_map *map = (struct encoding_map*)obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7293 | return PyLong_FromLong(sizeof(*map) - 1 + 16*map->count2 + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7294 | 128*map->count3); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7295 | } |
| 7296 | |
| 7297 | static PyMethodDef encoding_map_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7298 | {"size", encoding_map_size, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7299 | PyDoc_STR("Return the size (in bytes) of this object") }, |
| 7300 | { 0 } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7301 | }; |
| 7302 | |
| 7303 | static void |
| 7304 | encoding_map_dealloc(PyObject* o) |
| 7305 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7306 | PyObject_FREE(o); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7307 | } |
| 7308 | |
| 7309 | static PyTypeObject EncodingMapType = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7310 | PyVarObject_HEAD_INIT(NULL, 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7311 | "EncodingMap", /*tp_name*/ |
| 7312 | sizeof(struct encoding_map), /*tp_basicsize*/ |
| 7313 | 0, /*tp_itemsize*/ |
| 7314 | /* methods */ |
| 7315 | encoding_map_dealloc, /*tp_dealloc*/ |
| 7316 | 0, /*tp_print*/ |
| 7317 | 0, /*tp_getattr*/ |
| 7318 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 7319 | 0, /*tp_reserved*/ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7320 | 0, /*tp_repr*/ |
| 7321 | 0, /*tp_as_number*/ |
| 7322 | 0, /*tp_as_sequence*/ |
| 7323 | 0, /*tp_as_mapping*/ |
| 7324 | 0, /*tp_hash*/ |
| 7325 | 0, /*tp_call*/ |
| 7326 | 0, /*tp_str*/ |
| 7327 | 0, /*tp_getattro*/ |
| 7328 | 0, /*tp_setattro*/ |
| 7329 | 0, /*tp_as_buffer*/ |
| 7330 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 7331 | 0, /*tp_doc*/ |
| 7332 | 0, /*tp_traverse*/ |
| 7333 | 0, /*tp_clear*/ |
| 7334 | 0, /*tp_richcompare*/ |
| 7335 | 0, /*tp_weaklistoffset*/ |
| 7336 | 0, /*tp_iter*/ |
| 7337 | 0, /*tp_iternext*/ |
| 7338 | encoding_map_methods, /*tp_methods*/ |
| 7339 | 0, /*tp_members*/ |
| 7340 | 0, /*tp_getset*/ |
| 7341 | 0, /*tp_base*/ |
| 7342 | 0, /*tp_dict*/ |
| 7343 | 0, /*tp_descr_get*/ |
| 7344 | 0, /*tp_descr_set*/ |
| 7345 | 0, /*tp_dictoffset*/ |
| 7346 | 0, /*tp_init*/ |
| 7347 | 0, /*tp_alloc*/ |
| 7348 | 0, /*tp_new*/ |
| 7349 | 0, /*tp_free*/ |
| 7350 | 0, /*tp_is_gc*/ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7351 | }; |
| 7352 | |
| 7353 | PyObject* |
| 7354 | PyUnicode_BuildEncodingMap(PyObject* string) |
| 7355 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7356 | PyObject *result; |
| 7357 | struct encoding_map *mresult; |
| 7358 | int i; |
| 7359 | int need_dict = 0; |
| 7360 | unsigned char level1[32]; |
| 7361 | unsigned char level2[512]; |
| 7362 | unsigned char *mlevel1, *mlevel2, *mlevel3; |
| 7363 | int count2 = 0, count3 = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7364 | int kind; |
| 7365 | void *data; |
| 7366 | Py_UCS4 ch; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7367 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7368 | if (!PyUnicode_Check(string) || PyUnicode_GET_LENGTH(string) != 256) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7369 | PyErr_BadArgument(); |
| 7370 | return NULL; |
| 7371 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7372 | kind = PyUnicode_KIND(string); |
| 7373 | data = PyUnicode_DATA(string); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7374 | memset(level1, 0xFF, sizeof level1); |
| 7375 | memset(level2, 0xFF, sizeof level2); |
| 7376 | |
| 7377 | /* If there isn't a one-to-one mapping of NULL to \0, |
| 7378 | or if there are non-BMP characters, we need to use |
| 7379 | a mapping dictionary. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7380 | if (PyUnicode_READ(kind, data, 0) != 0) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7381 | need_dict = 1; |
| 7382 | for (i = 1; i < 256; i++) { |
| 7383 | int l1, l2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7384 | ch = PyUnicode_READ(kind, data, i); |
| 7385 | if (ch == 0 || ch > 0xFFFF) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7386 | need_dict = 1; |
| 7387 | break; |
| 7388 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7389 | if (ch == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7390 | /* unmapped character */ |
| 7391 | continue; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7392 | l1 = ch >> 11; |
| 7393 | l2 = ch >> 7; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7394 | if (level1[l1] == 0xFF) |
| 7395 | level1[l1] = count2++; |
| 7396 | if (level2[l2] == 0xFF) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7397 | level2[l2] = count3++; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7398 | } |
| 7399 | |
| 7400 | if (count2 >= 0xFF || count3 >= 0xFF) |
| 7401 | need_dict = 1; |
| 7402 | |
| 7403 | if (need_dict) { |
| 7404 | PyObject *result = PyDict_New(); |
| 7405 | PyObject *key, *value; |
| 7406 | if (!result) |
| 7407 | return NULL; |
| 7408 | for (i = 0; i < 256; i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7409 | key = PyLong_FromLong(PyUnicode_READ(kind, data, i)); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7410 | value = PyLong_FromLong(i); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7411 | if (!key || !value) |
| 7412 | goto failed1; |
| 7413 | if (PyDict_SetItem(result, key, value) == -1) |
| 7414 | goto failed1; |
| 7415 | Py_DECREF(key); |
| 7416 | Py_DECREF(value); |
| 7417 | } |
| 7418 | return result; |
| 7419 | failed1: |
| 7420 | Py_XDECREF(key); |
| 7421 | Py_XDECREF(value); |
| 7422 | Py_DECREF(result); |
| 7423 | return NULL; |
| 7424 | } |
| 7425 | |
| 7426 | /* Create a three-level trie */ |
| 7427 | result = PyObject_MALLOC(sizeof(struct encoding_map) + |
| 7428 | 16*count2 + 128*count3 - 1); |
| 7429 | if (!result) |
| 7430 | return PyErr_NoMemory(); |
| 7431 | PyObject_Init(result, &EncodingMapType); |
| 7432 | mresult = (struct encoding_map*)result; |
| 7433 | mresult->count2 = count2; |
| 7434 | mresult->count3 = count3; |
| 7435 | mlevel1 = mresult->level1; |
| 7436 | mlevel2 = mresult->level23; |
| 7437 | mlevel3 = mresult->level23 + 16*count2; |
| 7438 | memcpy(mlevel1, level1, 32); |
| 7439 | memset(mlevel2, 0xFF, 16*count2); |
| 7440 | memset(mlevel3, 0, 128*count3); |
| 7441 | count3 = 0; |
| 7442 | for (i = 1; i < 256; i++) { |
| 7443 | int o1, o2, o3, i2, i3; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7444 | if (PyUnicode_READ(kind, data, i) == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7445 | /* unmapped character */ |
| 7446 | continue; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7447 | o1 = PyUnicode_READ(kind, data, i)>>11; |
| 7448 | o2 = (PyUnicode_READ(kind, data, i)>>7) & 0xF; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7449 | i2 = 16*mlevel1[o1] + o2; |
| 7450 | if (mlevel2[i2] == 0xFF) |
| 7451 | mlevel2[i2] = count3++; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7452 | o3 = PyUnicode_READ(kind, data, i) & 0x7F; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7453 | i3 = 128*mlevel2[i2] + o3; |
| 7454 | mlevel3[i3] = i; |
| 7455 | } |
| 7456 | return result; |
| 7457 | } |
| 7458 | |
| 7459 | static int |
| 7460 | encoding_map_lookup(Py_UNICODE c, PyObject *mapping) |
| 7461 | { |
| 7462 | struct encoding_map *map = (struct encoding_map*)mapping; |
| 7463 | int l1 = c>>11; |
| 7464 | int l2 = (c>>7) & 0xF; |
| 7465 | int l3 = c & 0x7F; |
| 7466 | int i; |
| 7467 | |
| 7468 | #ifdef Py_UNICODE_WIDE |
| 7469 | if (c > 0xFFFF) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7470 | return -1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7471 | } |
| 7472 | #endif |
| 7473 | if (c == 0) |
| 7474 | return 0; |
| 7475 | /* level 1*/ |
| 7476 | i = map->level1[l1]; |
| 7477 | if (i == 0xFF) { |
| 7478 | return -1; |
| 7479 | } |
| 7480 | /* level 2*/ |
| 7481 | i = map->level23[16*i+l2]; |
| 7482 | if (i == 0xFF) { |
| 7483 | return -1; |
| 7484 | } |
| 7485 | /* level 3 */ |
| 7486 | i = map->level23[16*map->count2 + 128*i + l3]; |
| 7487 | if (i == 0) { |
| 7488 | return -1; |
| 7489 | } |
| 7490 | return i; |
| 7491 | } |
| 7492 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7493 | /* Lookup the character ch in the mapping. If the character |
| 7494 | can't be found, Py_None is returned (or NULL, if another |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 7495 | error occurred). */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7496 | static PyObject * |
| 7497 | charmapencode_lookup(Py_UNICODE c, PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7498 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7499 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7500 | PyObject *x; |
| 7501 | |
| 7502 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7503 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7504 | x = PyObject_GetItem(mapping, w); |
| 7505 | Py_DECREF(w); |
| 7506 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7507 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 7508 | /* No mapping found means: mapping is undefined. */ |
| 7509 | PyErr_Clear(); |
| 7510 | x = Py_None; |
| 7511 | Py_INCREF(x); |
| 7512 | return x; |
| 7513 | } else |
| 7514 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7515 | } |
Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 7516 | else if (x == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7517 | return x; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7518 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7519 | long value = PyLong_AS_LONG(x); |
| 7520 | if (value < 0 || value > 255) { |
| 7521 | PyErr_SetString(PyExc_TypeError, |
| 7522 | "character mapping must be in range(256)"); |
| 7523 | Py_DECREF(x); |
| 7524 | return NULL; |
| 7525 | } |
| 7526 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7527 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7528 | else if (PyBytes_Check(x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7529 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7530 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7531 | /* wrong return value */ |
| 7532 | PyErr_Format(PyExc_TypeError, |
| 7533 | "character mapping must return integer, bytes or None, not %.400s", |
| 7534 | x->ob_type->tp_name); |
| 7535 | Py_DECREF(x); |
| 7536 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7537 | } |
| 7538 | } |
| 7539 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7540 | static int |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 7541 | charmapencode_resize(PyObject **outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7542 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7543 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
| 7544 | /* exponentially overallocate to minimize reallocations */ |
| 7545 | if (requiredsize < 2*outsize) |
| 7546 | requiredsize = 2*outsize; |
| 7547 | if (_PyBytes_Resize(outobj, requiredsize)) |
| 7548 | return -1; |
| 7549 | return 0; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7550 | } |
| 7551 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7552 | typedef enum charmapencode_result { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7553 | enc_SUCCESS, enc_FAILED, enc_EXCEPTION |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7554 | } charmapencode_result; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7555 | /* 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] | 7556 | various state variables. Resize the output bytes object if not enough |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7557 | space is available. Return a new reference to the object that |
| 7558 | was put in the output buffer, or Py_None, if the mapping was undefined |
| 7559 | (in which case no character was written) or NULL, if a |
Andrew M. Kuchling | 8294de5 | 2005-11-02 16:36:12 +0000 | [diff] [blame] | 7560 | reallocation error occurred. The caller must decref the result */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7561 | static charmapencode_result |
| 7562 | charmapencode_output(Py_UNICODE c, PyObject *mapping, |
| 7563 | PyObject **outobj, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7564 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7565 | PyObject *rep; |
| 7566 | char *outstart; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7567 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7568 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 7569 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7570 | int res = encoding_map_lookup(c, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7571 | Py_ssize_t requiredsize = *outpos+1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7572 | if (res == -1) |
| 7573 | return enc_FAILED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7574 | if (outsize<requiredsize) |
| 7575 | if (charmapencode_resize(outobj, outpos, requiredsize)) |
| 7576 | return enc_EXCEPTION; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7577 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7578 | outstart[(*outpos)++] = (char)res; |
| 7579 | return enc_SUCCESS; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7580 | } |
| 7581 | |
| 7582 | rep = charmapencode_lookup(c, mapping); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7583 | if (rep==NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7584 | return enc_EXCEPTION; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7585 | else if (rep==Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7586 | Py_DECREF(rep); |
| 7587 | return enc_FAILED; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7588 | } else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7589 | if (PyLong_Check(rep)) { |
| 7590 | Py_ssize_t requiredsize = *outpos+1; |
| 7591 | if (outsize<requiredsize) |
| 7592 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 7593 | Py_DECREF(rep); |
| 7594 | return enc_EXCEPTION; |
| 7595 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7596 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7597 | outstart[(*outpos)++] = (char)PyLong_AS_LONG(rep); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7598 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7599 | else { |
| 7600 | const char *repchars = PyBytes_AS_STRING(rep); |
| 7601 | Py_ssize_t repsize = PyBytes_GET_SIZE(rep); |
| 7602 | Py_ssize_t requiredsize = *outpos+repsize; |
| 7603 | if (outsize<requiredsize) |
| 7604 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 7605 | Py_DECREF(rep); |
| 7606 | return enc_EXCEPTION; |
| 7607 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7608 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7609 | memcpy(outstart + *outpos, repchars, repsize); |
| 7610 | *outpos += repsize; |
| 7611 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7612 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7613 | Py_DECREF(rep); |
| 7614 | return enc_SUCCESS; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7615 | } |
| 7616 | |
| 7617 | /* handle an error in PyUnicode_EncodeCharmap |
| 7618 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7619 | static int |
| 7620 | charmap_encoding_error( |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7621 | 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] | 7622 | PyObject **exceptionObject, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 7623 | int *known_errorHandler, PyObject **errorHandler, const char *errors, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 7624 | PyObject **res, Py_ssize_t *respos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7625 | { |
| 7626 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7627 | Py_ssize_t repsize; |
| 7628 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7629 | Py_UNICODE *uni2; |
| 7630 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7631 | Py_ssize_t collstartpos = *inpos; |
| 7632 | Py_ssize_t collendpos = *inpos+1; |
| 7633 | Py_ssize_t collpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7634 | char *encoding = "charmap"; |
| 7635 | char *reason = "character maps to <undefined>"; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7636 | charmapencode_result x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7637 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7638 | /* find all unencodable characters */ |
| 7639 | while (collendpos < size) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7640 | PyObject *rep; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 7641 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7642 | int res = encoding_map_lookup(p[collendpos], mapping); |
| 7643 | if (res != -1) |
| 7644 | break; |
| 7645 | ++collendpos; |
| 7646 | continue; |
| 7647 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7648 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7649 | rep = charmapencode_lookup(p[collendpos], mapping); |
| 7650 | if (rep==NULL) |
| 7651 | return -1; |
| 7652 | else if (rep!=Py_None) { |
| 7653 | Py_DECREF(rep); |
| 7654 | break; |
| 7655 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7656 | Py_DECREF(rep); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7657 | ++collendpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7658 | } |
| 7659 | /* cache callback name lookup |
| 7660 | * (if not done yet, i.e. it's the first error) */ |
| 7661 | if (*known_errorHandler==-1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7662 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 7663 | *known_errorHandler = 1; |
| 7664 | else if (!strcmp(errors, "replace")) |
| 7665 | *known_errorHandler = 2; |
| 7666 | else if (!strcmp(errors, "ignore")) |
| 7667 | *known_errorHandler = 3; |
| 7668 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 7669 | *known_errorHandler = 4; |
| 7670 | else |
| 7671 | *known_errorHandler = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7672 | } |
| 7673 | switch (*known_errorHandler) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7674 | case 1: /* strict */ |
| 7675 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 7676 | return -1; |
| 7677 | case 2: /* replace */ |
| 7678 | for (collpos = collstartpos; collpos<collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7679 | x = charmapencode_output('?', mapping, res, respos); |
| 7680 | if (x==enc_EXCEPTION) { |
| 7681 | return -1; |
| 7682 | } |
| 7683 | else if (x==enc_FAILED) { |
| 7684 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 7685 | return -1; |
| 7686 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7687 | } |
| 7688 | /* fall through */ |
| 7689 | case 3: /* ignore */ |
| 7690 | *inpos = collendpos; |
| 7691 | break; |
| 7692 | case 4: /* xmlcharrefreplace */ |
| 7693 | /* generate replacement (temporarily (mis)uses p) */ |
| 7694 | for (collpos = collstartpos; collpos < collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7695 | char buffer[2+29+1+1]; |
| 7696 | char *cp; |
| 7697 | sprintf(buffer, "&#%d;", (int)p[collpos]); |
| 7698 | for (cp = buffer; *cp; ++cp) { |
| 7699 | x = charmapencode_output(*cp, mapping, res, respos); |
| 7700 | if (x==enc_EXCEPTION) |
| 7701 | return -1; |
| 7702 | else if (x==enc_FAILED) { |
| 7703 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 7704 | return -1; |
| 7705 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7706 | } |
| 7707 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7708 | *inpos = collendpos; |
| 7709 | break; |
| 7710 | default: |
| 7711 | repunicode = unicode_encode_call_errorhandler(errors, errorHandler, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7712 | encoding, reason, p, size, exceptionObject, |
| 7713 | collstartpos, collendpos, &newpos); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7714 | if (repunicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7715 | return -1; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 7716 | if (PyBytes_Check(repunicode)) { |
| 7717 | /* Directly copy bytes result to output. */ |
| 7718 | Py_ssize_t outsize = PyBytes_Size(*res); |
| 7719 | Py_ssize_t requiredsize; |
| 7720 | repsize = PyBytes_Size(repunicode); |
| 7721 | requiredsize = *respos + repsize; |
| 7722 | if (requiredsize > outsize) |
| 7723 | /* Make room for all additional bytes. */ |
| 7724 | if (charmapencode_resize(res, respos, requiredsize)) { |
| 7725 | Py_DECREF(repunicode); |
| 7726 | return -1; |
| 7727 | } |
| 7728 | memcpy(PyBytes_AsString(*res) + *respos, |
| 7729 | PyBytes_AsString(repunicode), repsize); |
| 7730 | *respos += repsize; |
| 7731 | *inpos = newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 7732 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 7733 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 7734 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7735 | /* generate replacement */ |
| 7736 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 7737 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7738 | x = charmapencode_output(*uni2, mapping, res, respos); |
| 7739 | if (x==enc_EXCEPTION) { |
| 7740 | return -1; |
| 7741 | } |
| 7742 | else if (x==enc_FAILED) { |
| 7743 | Py_DECREF(repunicode); |
| 7744 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 7745 | return -1; |
| 7746 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7747 | } |
| 7748 | *inpos = newpos; |
| 7749 | Py_DECREF(repunicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7750 | } |
| 7751 | return 0; |
| 7752 | } |
| 7753 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7754 | PyObject * |
| 7755 | PyUnicode_EncodeCharmap(const Py_UNICODE *p, |
| 7756 | Py_ssize_t size, |
| 7757 | PyObject *mapping, |
| 7758 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7759 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7760 | /* output object */ |
| 7761 | PyObject *res = NULL; |
| 7762 | /* current input position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7763 | Py_ssize_t inpos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7764 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7765 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7766 | PyObject *errorHandler = NULL; |
| 7767 | PyObject *exc = NULL; |
| 7768 | /* the following variable is used for caching string comparisons |
| 7769 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 7770 | * 3=ignore, 4=xmlcharrefreplace */ |
| 7771 | int known_errorHandler = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7772 | |
| 7773 | /* Default to Latin-1 */ |
| 7774 | if (mapping == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7775 | return PyUnicode_EncodeLatin1(p, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7776 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7777 | /* allocate enough for a simple encoding without |
| 7778 | replacements, if we need more, we'll resize */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7779 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7780 | if (res == NULL) |
| 7781 | goto onError; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 7782 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7783 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7784 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7785 | while (inpos<size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7786 | /* try to encode it */ |
| 7787 | charmapencode_result x = charmapencode_output(p[inpos], mapping, &res, &respos); |
| 7788 | if (x==enc_EXCEPTION) /* error */ |
| 7789 | goto onError; |
| 7790 | if (x==enc_FAILED) { /* unencodable character */ |
| 7791 | if (charmap_encoding_error(p, size, &inpos, mapping, |
| 7792 | &exc, |
| 7793 | &known_errorHandler, &errorHandler, errors, |
| 7794 | &res, &respos)) { |
| 7795 | goto onError; |
| 7796 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7797 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7798 | else |
| 7799 | /* done with this character => adjust input position */ |
| 7800 | ++inpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7801 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7802 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7803 | /* Resize if we allocated to much */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7804 | if (respos<PyBytes_GET_SIZE(res)) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 7805 | if (_PyBytes_Resize(&res, respos) < 0) |
| 7806 | goto onError; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 7807 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7808 | Py_XDECREF(exc); |
| 7809 | Py_XDECREF(errorHandler); |
| 7810 | return res; |
| 7811 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7812 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7813 | Py_XDECREF(res); |
| 7814 | Py_XDECREF(exc); |
| 7815 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7816 | return NULL; |
| 7817 | } |
| 7818 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7819 | PyObject * |
| 7820 | PyUnicode_AsCharmapString(PyObject *unicode, |
| 7821 | PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7822 | { |
| 7823 | if (!PyUnicode_Check(unicode) || mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7824 | PyErr_BadArgument(); |
| 7825 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7826 | } |
| 7827 | return PyUnicode_EncodeCharmap(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7828 | PyUnicode_GET_SIZE(unicode), |
| 7829 | mapping, |
| 7830 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7831 | } |
| 7832 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7833 | /* create or adjust a UnicodeTranslateError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7834 | static void |
| 7835 | make_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7836 | PyObject *unicode, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7837 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 7838 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7839 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7840 | if (*exceptionObject == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7841 | *exceptionObject = _PyUnicodeTranslateError_Create( |
| 7842 | unicode, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7843 | } |
| 7844 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7845 | if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
| 7846 | goto onError; |
| 7847 | if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) |
| 7848 | goto onError; |
| 7849 | if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) |
| 7850 | goto onError; |
| 7851 | return; |
| 7852 | onError: |
| 7853 | Py_DECREF(*exceptionObject); |
| 7854 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7855 | } |
| 7856 | } |
| 7857 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7858 | /* raises a UnicodeTranslateError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7859 | static void |
| 7860 | raise_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7861 | PyObject *unicode, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7862 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 7863 | const char *reason) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7864 | { |
| 7865 | make_translate_exception(exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7866 | unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7867 | if (*exceptionObject != NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7868 | PyCodec_StrictErrors(*exceptionObject); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7869 | } |
| 7870 | |
| 7871 | /* error handling callback helper: |
| 7872 | build arguments, call the callback and check the arguments, |
| 7873 | put the result into newpos and return the replacement string, which |
| 7874 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7875 | static PyObject * |
| 7876 | unicode_translate_call_errorhandler(const char *errors, |
| 7877 | PyObject **errorHandler, |
| 7878 | const char *reason, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7879 | PyObject *unicode, PyObject **exceptionObject, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7880 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 7881 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7882 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 7883 | 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] | 7884 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7885 | Py_ssize_t i_newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7886 | PyObject *restuple; |
| 7887 | PyObject *resunicode; |
| 7888 | |
| 7889 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7890 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7891 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7892 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7893 | } |
| 7894 | |
| 7895 | make_translate_exception(exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7896 | unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7897 | if (*exceptionObject == 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 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7901 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7902 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7903 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7904 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 7905 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7906 | Py_DECREF(restuple); |
| 7907 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7908 | } |
| 7909 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7910 | &resunicode, &i_newpos)) { |
| 7911 | Py_DECREF(restuple); |
| 7912 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7913 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7914 | if (i_newpos<0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7915 | *newpos = PyUnicode_GET_LENGTH(unicode)+i_newpos; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7916 | else |
| 7917 | *newpos = i_newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7918 | if (*newpos<0 || *newpos>PyUnicode_GET_LENGTH(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7919 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 7920 | Py_DECREF(restuple); |
| 7921 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 7922 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7923 | Py_INCREF(resunicode); |
| 7924 | Py_DECREF(restuple); |
| 7925 | return resunicode; |
| 7926 | } |
| 7927 | |
| 7928 | /* Lookup the character ch in the mapping and put the result in result, |
| 7929 | which must be decrefed by the caller. |
| 7930 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7931 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7932 | charmaptranslate_lookup(Py_UCS4 c, PyObject *mapping, PyObject **result) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7933 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7934 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7935 | PyObject *x; |
| 7936 | |
| 7937 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7938 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7939 | x = PyObject_GetItem(mapping, w); |
| 7940 | Py_DECREF(w); |
| 7941 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7942 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 7943 | /* No mapping found means: use 1:1 mapping. */ |
| 7944 | PyErr_Clear(); |
| 7945 | *result = NULL; |
| 7946 | return 0; |
| 7947 | } else |
| 7948 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7949 | } |
| 7950 | else if (x == Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7951 | *result = x; |
| 7952 | return 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7953 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7954 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7955 | long value = PyLong_AS_LONG(x); |
| 7956 | long max = PyUnicode_GetMax(); |
| 7957 | if (value < 0 || value > max) { |
| 7958 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5a2f7e60 | 2007-10-24 21:13:09 +0000 | [diff] [blame] | 7959 | "character mapping must be in range(0x%x)", max+1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7960 | Py_DECREF(x); |
| 7961 | return -1; |
| 7962 | } |
| 7963 | *result = x; |
| 7964 | return 0; |
| 7965 | } |
| 7966 | else if (PyUnicode_Check(x)) { |
| 7967 | *result = x; |
| 7968 | return 0; |
| 7969 | } |
| 7970 | else { |
| 7971 | /* wrong return value */ |
| 7972 | PyErr_SetString(PyExc_TypeError, |
| 7973 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7974 | Py_DECREF(x); |
| 7975 | return -1; |
| 7976 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7977 | } |
| 7978 | /* ensure that *outobj is at least requiredsize characters long, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7979 | if not reallocate and adjust various state variables. |
| 7980 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7981 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7982 | charmaptranslate_makespace(Py_UCS4 **outobj, Py_ssize_t *psize, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7983 | Py_ssize_t requiredsize) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7984 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7985 | Py_ssize_t oldsize = *psize; |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 7986 | if (requiredsize > oldsize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7987 | /* exponentially overallocate to minimize reallocations */ |
| 7988 | if (requiredsize < 2 * oldsize) |
| 7989 | requiredsize = 2 * oldsize; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7990 | *outobj = PyMem_Realloc(*outobj, requiredsize * sizeof(Py_UCS4)); |
| 7991 | if (*outobj == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7992 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7993 | *psize = requiredsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7994 | } |
| 7995 | return 0; |
| 7996 | } |
| 7997 | /* lookup the character, put the result in the output string and adjust |
| 7998 | various state variables. Return a new reference to the object that |
| 7999 | was put in the output buffer in *result, or Py_None, if the mapping was |
| 8000 | undefined (in which case no character was written). |
| 8001 | The called must decref result. |
| 8002 | Return 0 on success, -1 on error. */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8003 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8004 | charmaptranslate_output(PyObject *input, Py_ssize_t ipos, |
| 8005 | PyObject *mapping, Py_UCS4 **output, |
| 8006 | Py_ssize_t *osize, Py_ssize_t *opos, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8007 | PyObject **res) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8008 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8009 | Py_UCS4 curinp = PyUnicode_READ_CHAR(input, ipos); |
| 8010 | if (charmaptranslate_lookup(curinp, mapping, res)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8011 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8012 | if (*res==NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8013 | /* not found => default to 1:1 mapping */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8014 | (*output)[(*opos)++] = curinp; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8015 | } |
| 8016 | else if (*res==Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8017 | ; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8018 | else if (PyLong_Check(*res)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8019 | /* 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] | 8020 | (*output)[(*opos)++] = (Py_UCS4)PyLong_AS_LONG(*res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8021 | } |
| 8022 | else if (PyUnicode_Check(*res)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8023 | Py_ssize_t repsize; |
| 8024 | if (PyUnicode_READY(*res) == -1) |
| 8025 | return -1; |
| 8026 | repsize = PyUnicode_GET_LENGTH(*res); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8027 | if (repsize==1) { |
| 8028 | /* 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] | 8029 | (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8030 | } |
| 8031 | else if (repsize!=0) { |
| 8032 | /* more than one character */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8033 | Py_ssize_t requiredsize = *opos + |
| 8034 | (PyUnicode_GET_LENGTH(input) - ipos) + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8035 | repsize - 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8036 | Py_ssize_t i; |
| 8037 | if (charmaptranslate_makespace(output, osize, requiredsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8038 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8039 | for(i = 0; i < repsize; i++) |
| 8040 | (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, i); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8041 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8042 | } |
| 8043 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8044 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8045 | return 0; |
| 8046 | } |
| 8047 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8048 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8049 | _PyUnicode_TranslateCharmap(PyObject *input, |
| 8050 | PyObject *mapping, |
| 8051 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8052 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8053 | /* input object */ |
| 8054 | char *idata; |
| 8055 | Py_ssize_t size, i; |
| 8056 | int kind; |
| 8057 | /* output buffer */ |
| 8058 | Py_UCS4 *output = NULL; |
| 8059 | Py_ssize_t osize; |
| 8060 | PyObject *res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8061 | /* current output position */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8062 | Py_ssize_t opos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8063 | char *reason = "character maps to <undefined>"; |
| 8064 | PyObject *errorHandler = NULL; |
| 8065 | PyObject *exc = NULL; |
| 8066 | /* the following variable is used for caching string comparisons |
| 8067 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 8068 | * 3=ignore, 4=xmlcharrefreplace */ |
| 8069 | int known_errorHandler = -1; |
| 8070 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8071 | if (mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8072 | PyErr_BadArgument(); |
| 8073 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8074 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8075 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8076 | if (PyUnicode_READY(input) == -1) |
| 8077 | return NULL; |
| 8078 | idata = (char*)PyUnicode_DATA(input); |
| 8079 | kind = PyUnicode_KIND(input); |
| 8080 | size = PyUnicode_GET_LENGTH(input); |
| 8081 | i = 0; |
| 8082 | |
| 8083 | if (size == 0) { |
| 8084 | Py_INCREF(input); |
| 8085 | return input; |
| 8086 | } |
| 8087 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8088 | /* allocate enough for a simple 1:1 translation without |
| 8089 | replacements, if we need more, we'll resize */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8090 | osize = size; |
| 8091 | output = PyMem_Malloc(osize * sizeof(Py_UCS4)); |
| 8092 | opos = 0; |
| 8093 | if (output == NULL) { |
| 8094 | PyErr_NoMemory(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8095 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8096 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8097 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8098 | while (i<size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8099 | /* try to encode it */ |
| 8100 | PyObject *x = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8101 | if (charmaptranslate_output(input, i, mapping, |
| 8102 | &output, &osize, &opos, &x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8103 | Py_XDECREF(x); |
| 8104 | goto onError; |
| 8105 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8106 | Py_XDECREF(x); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8107 | if (x!=Py_None) /* it worked => adjust input pointer */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8108 | ++i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8109 | else { /* untranslatable character */ |
| 8110 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
| 8111 | Py_ssize_t repsize; |
| 8112 | Py_ssize_t newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8113 | Py_ssize_t uni2; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8114 | /* startpos for collecting untranslatable chars */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8115 | Py_ssize_t collstart = i; |
| 8116 | Py_ssize_t collend = i+1; |
| 8117 | Py_ssize_t coll; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8118 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8119 | /* find all untranslatable characters */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8120 | while (collend < size) { |
| 8121 | if (charmaptranslate_lookup(PyUnicode_READ(kind,idata, collend), mapping, &x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8122 | goto onError; |
| 8123 | Py_XDECREF(x); |
| 8124 | if (x!=Py_None) |
| 8125 | break; |
| 8126 | ++collend; |
| 8127 | } |
| 8128 | /* cache callback name lookup |
| 8129 | * (if not done yet, i.e. it's the first error) */ |
| 8130 | if (known_errorHandler==-1) { |
| 8131 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 8132 | known_errorHandler = 1; |
| 8133 | else if (!strcmp(errors, "replace")) |
| 8134 | known_errorHandler = 2; |
| 8135 | else if (!strcmp(errors, "ignore")) |
| 8136 | known_errorHandler = 3; |
| 8137 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 8138 | known_errorHandler = 4; |
| 8139 | else |
| 8140 | known_errorHandler = 0; |
| 8141 | } |
| 8142 | switch (known_errorHandler) { |
| 8143 | case 1: /* strict */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8144 | raise_translate_exception(&exc, input, collstart, |
| 8145 | collend, reason); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8146 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8147 | case 2: /* replace */ |
| 8148 | /* 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] | 8149 | for (coll = collstart; coll<collend; coll++) |
| 8150 | output[opos++] = '?'; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8151 | /* fall through */ |
| 8152 | case 3: /* ignore */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8153 | i = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8154 | break; |
| 8155 | case 4: /* xmlcharrefreplace */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8156 | /* generate replacement (temporarily (mis)uses i) */ |
| 8157 | for (i = collstart; i < collend; ++i) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8158 | char buffer[2+29+1+1]; |
| 8159 | char *cp; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8160 | sprintf(buffer, "&#%d;", PyUnicode_READ(kind, idata, i)); |
| 8161 | if (charmaptranslate_makespace(&output, &osize, |
| 8162 | opos+strlen(buffer)+(size-collend))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8163 | goto onError; |
| 8164 | for (cp = buffer; *cp; ++cp) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8165 | output[opos++] = *cp; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8166 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8167 | i = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8168 | break; |
| 8169 | default: |
| 8170 | repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8171 | reason, input, &exc, |
| 8172 | collstart, collend, &newpos); |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 8173 | if (repunicode == NULL || _PyUnicode_READY_REPLACE(&repunicode)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8174 | goto onError; |
| 8175 | /* generate replacement */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8176 | repsize = PyUnicode_GET_LENGTH(repunicode); |
| 8177 | if (charmaptranslate_makespace(&output, &osize, |
| 8178 | opos+repsize+(size-collend))) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8179 | Py_DECREF(repunicode); |
| 8180 | goto onError; |
| 8181 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8182 | for (uni2 = 0; repsize-->0; ++uni2) |
| 8183 | output[opos++] = PyUnicode_READ_CHAR(repunicode, uni2); |
| 8184 | i = newpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8185 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8186 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8187 | } |
| 8188 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8189 | res = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, output, opos); |
| 8190 | if (!res) |
| 8191 | goto onError; |
| 8192 | PyMem_Free(output); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8193 | Py_XDECREF(exc); |
| 8194 | Py_XDECREF(errorHandler); |
| 8195 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8196 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8197 | onError: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 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); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8201 | return NULL; |
| 8202 | } |
| 8203 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8204 | /* Deprecated. Use PyUnicode_Translate instead. */ |
| 8205 | PyObject * |
| 8206 | PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
| 8207 | Py_ssize_t size, |
| 8208 | PyObject *mapping, |
| 8209 | const char *errors) |
| 8210 | { |
| 8211 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 8212 | if (!unicode) |
| 8213 | return NULL; |
| 8214 | return _PyUnicode_TranslateCharmap(unicode, mapping, errors); |
| 8215 | } |
| 8216 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8217 | PyObject * |
| 8218 | PyUnicode_Translate(PyObject *str, |
| 8219 | PyObject *mapping, |
| 8220 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8221 | { |
| 8222 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8223 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8224 | str = PyUnicode_FromObject(str); |
| 8225 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8226 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8227 | result = _PyUnicode_TranslateCharmap(str, mapping, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8228 | Py_DECREF(str); |
| 8229 | return result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8230 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8231 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8232 | Py_XDECREF(str); |
| 8233 | return NULL; |
| 8234 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8235 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8236 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8237 | fix_decimal_and_space_to_ascii(PyObject *self) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8238 | { |
| 8239 | /* No need to call PyUnicode_READY(self) because this function is only |
| 8240 | called as a callback from fixup() which does it already. */ |
| 8241 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 8242 | const int kind = PyUnicode_KIND(self); |
| 8243 | void *data = PyUnicode_DATA(self); |
| 8244 | Py_UCS4 maxchar = 0, ch, fixed; |
| 8245 | Py_ssize_t i; |
| 8246 | |
| 8247 | for (i = 0; i < len; ++i) { |
| 8248 | ch = PyUnicode_READ(kind, data, i); |
| 8249 | fixed = 0; |
| 8250 | if (ch > 127) { |
| 8251 | if (Py_UNICODE_ISSPACE(ch)) |
| 8252 | fixed = ' '; |
| 8253 | else { |
| 8254 | const int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8255 | if (decimal >= 0) |
| 8256 | fixed = '0' + decimal; |
| 8257 | } |
| 8258 | if (fixed != 0) { |
| 8259 | if (fixed > maxchar) |
| 8260 | maxchar = fixed; |
| 8261 | PyUnicode_WRITE(kind, data, i, fixed); |
| 8262 | } |
| 8263 | else if (ch > maxchar) |
| 8264 | maxchar = ch; |
| 8265 | } |
| 8266 | else if (ch > maxchar) |
| 8267 | maxchar = ch; |
| 8268 | } |
| 8269 | |
| 8270 | return maxchar; |
| 8271 | } |
| 8272 | |
| 8273 | PyObject * |
| 8274 | _PyUnicode_TransformDecimalAndSpaceToASCII(PyObject *unicode) |
| 8275 | { |
| 8276 | if (!PyUnicode_Check(unicode)) { |
| 8277 | PyErr_BadInternalCall(); |
| 8278 | return NULL; |
| 8279 | } |
| 8280 | if (PyUnicode_READY(unicode) == -1) |
| 8281 | return NULL; |
| 8282 | if (PyUnicode_MAX_CHAR_VALUE(unicode) <= 127) { |
| 8283 | /* If the string is already ASCII, just return the same string */ |
| 8284 | Py_INCREF(unicode); |
| 8285 | return unicode; |
| 8286 | } |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8287 | return fixup(unicode, fix_decimal_and_space_to_ascii); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8288 | } |
| 8289 | |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8290 | PyObject * |
| 8291 | PyUnicode_TransformDecimalToASCII(Py_UNICODE *s, |
| 8292 | Py_ssize_t length) |
| 8293 | { |
| 8294 | PyObject *result; |
| 8295 | Py_UNICODE *p; /* write pointer into result */ |
| 8296 | Py_ssize_t i; |
| 8297 | /* Copy to a new string */ |
| 8298 | result = (PyObject *)_PyUnicode_New(length); |
| 8299 | Py_UNICODE_COPY(PyUnicode_AS_UNICODE(result), s, length); |
| 8300 | if (result == NULL) |
| 8301 | return result; |
| 8302 | p = PyUnicode_AS_UNICODE(result); |
| 8303 | /* Iterate over code points */ |
| 8304 | for (i = 0; i < length; i++) { |
| 8305 | Py_UNICODE ch =s[i]; |
| 8306 | if (ch > 127) { |
| 8307 | int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8308 | if (decimal >= 0) |
| 8309 | p[i] = '0' + decimal; |
| 8310 | } |
| 8311 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 8312 | #ifndef DONT_MAKE_RESULT_READY |
| 8313 | if (_PyUnicode_READY_REPLACE(&result)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8314 | Py_DECREF(result); |
| 8315 | return NULL; |
| 8316 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 8317 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 8318 | assert(_PyUnicode_CheckConsistency(result, 1)); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8319 | return result; |
| 8320 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8321 | /* --- Decimal Encoder ---------------------------------------------------- */ |
| 8322 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8323 | int |
| 8324 | PyUnicode_EncodeDecimal(Py_UNICODE *s, |
| 8325 | Py_ssize_t length, |
| 8326 | char *output, |
| 8327 | const char *errors) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8328 | { |
| 8329 | Py_UNICODE *p, *end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8330 | PyObject *errorHandler = NULL; |
| 8331 | PyObject *exc = NULL; |
| 8332 | const char *encoding = "decimal"; |
| 8333 | const char *reason = "invalid decimal Unicode string"; |
| 8334 | /* the following variable is used for caching string comparisons |
| 8335 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 8336 | int known_errorHandler = -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8337 | |
| 8338 | if (output == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8339 | PyErr_BadArgument(); |
| 8340 | return -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8341 | } |
| 8342 | |
| 8343 | p = s; |
| 8344 | end = s + length; |
| 8345 | while (p < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8346 | register Py_UNICODE ch = *p; |
| 8347 | int decimal; |
| 8348 | PyObject *repunicode; |
| 8349 | Py_ssize_t repsize; |
| 8350 | Py_ssize_t newpos; |
| 8351 | Py_UNICODE *uni2; |
| 8352 | Py_UNICODE *collstart; |
| 8353 | Py_UNICODE *collend; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8354 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8355 | if (Py_UNICODE_ISSPACE(ch)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8356 | *output++ = ' '; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8357 | ++p; |
| 8358 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8359 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8360 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 8361 | if (decimal >= 0) { |
| 8362 | *output++ = '0' + decimal; |
| 8363 | ++p; |
| 8364 | continue; |
| 8365 | } |
| 8366 | if (0 < ch && ch < 256) { |
| 8367 | *output++ = (char)ch; |
| 8368 | ++p; |
| 8369 | continue; |
| 8370 | } |
| 8371 | /* All other characters are considered unencodable */ |
| 8372 | collstart = p; |
| 8373 | collend = p+1; |
| 8374 | while (collend < end) { |
| 8375 | if ((0 < *collend && *collend < 256) || |
| 8376 | !Py_UNICODE_ISSPACE(*collend) || |
| 8377 | Py_UNICODE_TODECIMAL(*collend)) |
| 8378 | break; |
| 8379 | } |
| 8380 | /* cache callback name lookup |
| 8381 | * (if not done yet, i.e. it's the first error) */ |
| 8382 | if (known_errorHandler==-1) { |
| 8383 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 8384 | known_errorHandler = 1; |
| 8385 | else if (!strcmp(errors, "replace")) |
| 8386 | known_errorHandler = 2; |
| 8387 | else if (!strcmp(errors, "ignore")) |
| 8388 | known_errorHandler = 3; |
| 8389 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 8390 | known_errorHandler = 4; |
| 8391 | else |
| 8392 | known_errorHandler = 0; |
| 8393 | } |
| 8394 | switch (known_errorHandler) { |
| 8395 | case 1: /* strict */ |
| 8396 | raise_encode_exception(&exc, encoding, s, length, collstart-s, collend-s, reason); |
| 8397 | goto onError; |
| 8398 | case 2: /* replace */ |
| 8399 | for (p = collstart; p < collend; ++p) |
| 8400 | *output++ = '?'; |
| 8401 | /* fall through */ |
| 8402 | case 3: /* ignore */ |
| 8403 | p = collend; |
| 8404 | break; |
| 8405 | case 4: /* xmlcharrefreplace */ |
| 8406 | /* generate replacement (temporarily (mis)uses p) */ |
| 8407 | for (p = collstart; p < collend; ++p) |
| 8408 | output += sprintf(output, "&#%d;", (int)*p); |
| 8409 | p = collend; |
| 8410 | break; |
| 8411 | default: |
| 8412 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 8413 | encoding, reason, s, length, &exc, |
| 8414 | collstart-s, collend-s, &newpos); |
| 8415 | if (repunicode == NULL) |
| 8416 | goto onError; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 8417 | if (!PyUnicode_Check(repunicode)) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8418 | /* Byte results not supported, since they have no decimal property. */ |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 8419 | PyErr_SetString(PyExc_TypeError, "error handler should return unicode"); |
| 8420 | Py_DECREF(repunicode); |
| 8421 | goto onError; |
| 8422 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8423 | /* generate replacement */ |
| 8424 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 8425 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
| 8426 | Py_UNICODE ch = *uni2; |
| 8427 | if (Py_UNICODE_ISSPACE(ch)) |
| 8428 | *output++ = ' '; |
| 8429 | else { |
| 8430 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 8431 | if (decimal >= 0) |
| 8432 | *output++ = '0' + decimal; |
| 8433 | else if (0 < ch && ch < 256) |
| 8434 | *output++ = (char)ch; |
| 8435 | else { |
| 8436 | Py_DECREF(repunicode); |
| 8437 | raise_encode_exception(&exc, encoding, |
| 8438 | s, length, collstart-s, collend-s, reason); |
| 8439 | goto onError; |
| 8440 | } |
| 8441 | } |
| 8442 | } |
| 8443 | p = s + newpos; |
| 8444 | Py_DECREF(repunicode); |
| 8445 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8446 | } |
| 8447 | /* 0-terminate the output string */ |
| 8448 | *output++ = '\0'; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8449 | Py_XDECREF(exc); |
| 8450 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8451 | return 0; |
| 8452 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8453 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8454 | Py_XDECREF(exc); |
| 8455 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8456 | return -1; |
| 8457 | } |
| 8458 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8459 | /* --- Helpers ------------------------------------------------------------ */ |
| 8460 | |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 8461 | #include "stringlib/asciilib.h" |
| 8462 | #include "stringlib/fastsearch.h" |
| 8463 | #include "stringlib/partition.h" |
| 8464 | #include "stringlib/split.h" |
| 8465 | #include "stringlib/count.h" |
| 8466 | #include "stringlib/find.h" |
| 8467 | #include "stringlib/localeutil.h" |
| 8468 | #include "stringlib/undef.h" |
| 8469 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8470 | #include "stringlib/ucs1lib.h" |
| 8471 | #include "stringlib/fastsearch.h" |
| 8472 | #include "stringlib/partition.h" |
| 8473 | #include "stringlib/split.h" |
| 8474 | #include "stringlib/count.h" |
| 8475 | #include "stringlib/find.h" |
| 8476 | #include "stringlib/localeutil.h" |
| 8477 | #include "stringlib/undef.h" |
| 8478 | |
| 8479 | #include "stringlib/ucs2lib.h" |
| 8480 | #include "stringlib/fastsearch.h" |
| 8481 | #include "stringlib/partition.h" |
| 8482 | #include "stringlib/split.h" |
| 8483 | #include "stringlib/count.h" |
| 8484 | #include "stringlib/find.h" |
| 8485 | #include "stringlib/localeutil.h" |
| 8486 | #include "stringlib/undef.h" |
| 8487 | |
| 8488 | #include "stringlib/ucs4lib.h" |
| 8489 | #include "stringlib/fastsearch.h" |
| 8490 | #include "stringlib/partition.h" |
| 8491 | #include "stringlib/split.h" |
| 8492 | #include "stringlib/count.h" |
| 8493 | #include "stringlib/find.h" |
| 8494 | #include "stringlib/localeutil.h" |
| 8495 | #include "stringlib/undef.h" |
| 8496 | |
| 8497 | static Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 8498 | any_find_slice(Py_ssize_t Py_LOCAL_CALLBACK(ascii)(const Py_UCS1*, Py_ssize_t, |
| 8499 | const Py_UCS1*, Py_ssize_t, |
| 8500 | Py_ssize_t, Py_ssize_t), |
| 8501 | 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] | 8502 | const Py_UCS1*, Py_ssize_t, |
| 8503 | Py_ssize_t, Py_ssize_t), |
| 8504 | Py_ssize_t Py_LOCAL_CALLBACK(ucs2)(const Py_UCS2*, Py_ssize_t, |
| 8505 | const Py_UCS2*, Py_ssize_t, |
| 8506 | Py_ssize_t, Py_ssize_t), |
| 8507 | Py_ssize_t Py_LOCAL_CALLBACK(ucs4)(const Py_UCS4*, Py_ssize_t, |
| 8508 | const Py_UCS4*, Py_ssize_t, |
| 8509 | Py_ssize_t, Py_ssize_t), |
| 8510 | PyObject* s1, PyObject* s2, |
| 8511 | Py_ssize_t start, |
| 8512 | Py_ssize_t end) |
| 8513 | { |
| 8514 | int kind1, kind2, kind; |
| 8515 | void *buf1, *buf2; |
| 8516 | Py_ssize_t len1, len2, result; |
| 8517 | |
| 8518 | kind1 = PyUnicode_KIND(s1); |
| 8519 | kind2 = PyUnicode_KIND(s2); |
| 8520 | kind = kind1 > kind2 ? kind1 : kind2; |
| 8521 | buf1 = PyUnicode_DATA(s1); |
| 8522 | buf2 = PyUnicode_DATA(s2); |
| 8523 | if (kind1 != kind) |
| 8524 | buf1 = _PyUnicode_AsKind(s1, kind); |
| 8525 | if (!buf1) |
| 8526 | return -2; |
| 8527 | if (kind2 != kind) |
| 8528 | buf2 = _PyUnicode_AsKind(s2, kind); |
| 8529 | if (!buf2) { |
| 8530 | if (kind1 != kind) PyMem_Free(buf1); |
| 8531 | return -2; |
| 8532 | } |
| 8533 | len1 = PyUnicode_GET_LENGTH(s1); |
| 8534 | len2 = PyUnicode_GET_LENGTH(s2); |
| 8535 | |
| 8536 | switch(kind) { |
| 8537 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 8538 | if (PyUnicode_IS_ASCII(s1) && PyUnicode_IS_ASCII(s2)) |
| 8539 | result = ascii(buf1, len1, buf2, len2, start, end); |
| 8540 | else |
| 8541 | result = ucs1(buf1, len1, buf2, len2, start, end); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8542 | break; |
| 8543 | case PyUnicode_2BYTE_KIND: |
| 8544 | result = ucs2(buf1, len1, buf2, len2, start, end); |
| 8545 | break; |
| 8546 | case PyUnicode_4BYTE_KIND: |
| 8547 | result = ucs4(buf1, len1, buf2, len2, start, end); |
| 8548 | break; |
| 8549 | default: |
| 8550 | assert(0); result = -2; |
| 8551 | } |
| 8552 | |
| 8553 | if (kind1 != kind) |
| 8554 | PyMem_Free(buf1); |
| 8555 | if (kind2 != kind) |
| 8556 | PyMem_Free(buf2); |
| 8557 | |
| 8558 | return result; |
| 8559 | } |
| 8560 | |
| 8561 | Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 8562 | _PyUnicode_InsertThousandsGrouping(PyObject *unicode, int kind, void *data, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8563 | Py_ssize_t n_buffer, |
| 8564 | void *digits, Py_ssize_t n_digits, |
| 8565 | Py_ssize_t min_width, |
| 8566 | const char *grouping, |
| 8567 | const char *thousands_sep) |
| 8568 | { |
| 8569 | switch(kind) { |
| 8570 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 8571 | if (unicode != NULL && PyUnicode_IS_ASCII(unicode)) |
| 8572 | return _PyUnicode_ascii_InsertThousandsGrouping( |
| 8573 | (Py_UCS1*)data, n_buffer, (Py_UCS1*)digits, n_digits, |
| 8574 | min_width, grouping, thousands_sep); |
| 8575 | else |
| 8576 | return _PyUnicode_ucs1_InsertThousandsGrouping( |
| 8577 | (Py_UCS1*)data, n_buffer, (Py_UCS1*)digits, n_digits, |
| 8578 | min_width, grouping, thousands_sep); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8579 | case PyUnicode_2BYTE_KIND: |
| 8580 | return _PyUnicode_ucs2_InsertThousandsGrouping( |
| 8581 | (Py_UCS2*)data, n_buffer, (Py_UCS2*)digits, n_digits, |
| 8582 | min_width, grouping, thousands_sep); |
| 8583 | case PyUnicode_4BYTE_KIND: |
| 8584 | return _PyUnicode_ucs4_InsertThousandsGrouping( |
| 8585 | (Py_UCS4*)data, n_buffer, (Py_UCS4*)digits, n_digits, |
| 8586 | min_width, grouping, thousands_sep); |
| 8587 | } |
| 8588 | assert(0); |
| 8589 | return -1; |
| 8590 | } |
| 8591 | |
| 8592 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 8593 | #include "stringlib/unicodedefs.h" |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8594 | #include "stringlib/fastsearch.h" |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 8595 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8596 | #include "stringlib/count.h" |
| 8597 | #include "stringlib/find.h" |
Eric Smith | 5807c41 | 2008-05-11 21:00:57 +0000 | [diff] [blame] | 8598 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8599 | /* helper macro to fixup start/end slice values */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 8600 | #define ADJUST_INDICES(start, end, len) \ |
| 8601 | if (end > len) \ |
| 8602 | end = len; \ |
| 8603 | else if (end < 0) { \ |
| 8604 | end += len; \ |
| 8605 | if (end < 0) \ |
| 8606 | end = 0; \ |
| 8607 | } \ |
| 8608 | if (start < 0) { \ |
| 8609 | start += len; \ |
| 8610 | if (start < 0) \ |
| 8611 | start = 0; \ |
| 8612 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8613 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8614 | Py_ssize_t |
| 8615 | PyUnicode_Count(PyObject *str, |
| 8616 | PyObject *substr, |
| 8617 | Py_ssize_t start, |
| 8618 | Py_ssize_t end) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8619 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8620 | Py_ssize_t result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8621 | PyUnicodeObject* str_obj; |
| 8622 | PyUnicodeObject* sub_obj; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8623 | int kind1, kind2, kind; |
| 8624 | void *buf1 = NULL, *buf2 = NULL; |
| 8625 | Py_ssize_t len1, len2; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8626 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8627 | str_obj = (PyUnicodeObject*) PyUnicode_FromObject(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8628 | if (!str_obj || PyUnicode_READY(str_obj) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8629 | return -1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8630 | sub_obj = (PyUnicodeObject*) PyUnicode_FromObject(substr); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 8631 | if (!sub_obj || PyUnicode_READY(sub_obj) == -1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8632 | Py_DECREF(str_obj); |
| 8633 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8634 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8635 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8636 | kind1 = PyUnicode_KIND(str_obj); |
| 8637 | kind2 = PyUnicode_KIND(sub_obj); |
| 8638 | kind = kind1 > kind2 ? kind1 : kind2; |
| 8639 | buf1 = PyUnicode_DATA(str_obj); |
| 8640 | if (kind1 != kind) |
| 8641 | buf1 = _PyUnicode_AsKind((PyObject*)str_obj, kind); |
| 8642 | if (!buf1) |
| 8643 | goto onError; |
| 8644 | buf2 = PyUnicode_DATA(sub_obj); |
| 8645 | if (kind2 != kind) |
| 8646 | buf2 = _PyUnicode_AsKind((PyObject*)sub_obj, kind); |
| 8647 | if (!buf2) |
| 8648 | goto onError; |
| 8649 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 8650 | len2 = PyUnicode_GET_LENGTH(sub_obj); |
| 8651 | |
| 8652 | ADJUST_INDICES(start, end, len1); |
| 8653 | switch(kind) { |
| 8654 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 8655 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sub_obj)) |
| 8656 | result = asciilib_count( |
| 8657 | ((Py_UCS1*)buf1) + start, end - start, |
| 8658 | buf2, len2, PY_SSIZE_T_MAX |
| 8659 | ); |
| 8660 | else |
| 8661 | result = ucs1lib_count( |
| 8662 | ((Py_UCS1*)buf1) + start, end - start, |
| 8663 | buf2, len2, PY_SSIZE_T_MAX |
| 8664 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8665 | break; |
| 8666 | case PyUnicode_2BYTE_KIND: |
| 8667 | result = ucs2lib_count( |
| 8668 | ((Py_UCS2*)buf1) + start, end - start, |
| 8669 | buf2, len2, PY_SSIZE_T_MAX |
| 8670 | ); |
| 8671 | break; |
| 8672 | case PyUnicode_4BYTE_KIND: |
| 8673 | result = ucs4lib_count( |
| 8674 | ((Py_UCS4*)buf1) + start, end - start, |
| 8675 | buf2, len2, PY_SSIZE_T_MAX |
| 8676 | ); |
| 8677 | break; |
| 8678 | default: |
| 8679 | assert(0); result = 0; |
| 8680 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8681 | |
| 8682 | Py_DECREF(sub_obj); |
| 8683 | Py_DECREF(str_obj); |
| 8684 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8685 | if (kind1 != kind) |
| 8686 | PyMem_Free(buf1); |
| 8687 | if (kind2 != kind) |
| 8688 | PyMem_Free(buf2); |
| 8689 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8690 | return result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8691 | onError: |
| 8692 | Py_DECREF(sub_obj); |
| 8693 | Py_DECREF(str_obj); |
| 8694 | if (kind1 != kind && buf1) |
| 8695 | PyMem_Free(buf1); |
| 8696 | if (kind2 != kind && buf2) |
| 8697 | PyMem_Free(buf2); |
| 8698 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8699 | } |
| 8700 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8701 | Py_ssize_t |
| 8702 | PyUnicode_Find(PyObject *str, |
| 8703 | PyObject *sub, |
| 8704 | Py_ssize_t start, |
| 8705 | Py_ssize_t end, |
| 8706 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8707 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8708 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8709 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8710 | str = PyUnicode_FromObject(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8711 | if (!str || PyUnicode_READY(str) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8712 | return -2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8713 | sub = PyUnicode_FromObject(sub); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8714 | if (!sub || PyUnicode_READY(sub) == -1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8715 | Py_DECREF(str); |
| 8716 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8717 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8718 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8719 | if (direction > 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8720 | result = any_find_slice( |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 8721 | asciilib_find_slice, ucs1lib_find_slice, |
| 8722 | ucs2lib_find_slice, ucs4lib_find_slice, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8723 | str, sub, start, end |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8724 | ); |
| 8725 | else |
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_rfind_slice, |
| 8728 | ucs2lib_rfind_slice, ucs4lib_rfind_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 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8732 | Py_DECREF(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8733 | Py_DECREF(sub); |
| 8734 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8735 | return result; |
| 8736 | } |
| 8737 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8738 | Py_ssize_t |
| 8739 | PyUnicode_FindChar(PyObject *str, Py_UCS4 ch, |
| 8740 | Py_ssize_t start, Py_ssize_t end, |
| 8741 | int direction) |
| 8742 | { |
| 8743 | char *result; |
| 8744 | int kind; |
| 8745 | if (PyUnicode_READY(str) == -1) |
| 8746 | return -2; |
Victor Stinner | 267aa24 | 2011-10-02 01:08:37 +0200 | [diff] [blame] | 8747 | if (start < 0 || end < 0) { |
| 8748 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 8749 | return -2; |
| 8750 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8751 | if (end > PyUnicode_GET_LENGTH(str)) |
| 8752 | end = PyUnicode_GET_LENGTH(str); |
| 8753 | kind = PyUnicode_KIND(str); |
| 8754 | result = findchar(PyUnicode_1BYTE_DATA(str) |
| 8755 | + PyUnicode_KIND_SIZE(kind, start), |
| 8756 | kind, |
| 8757 | end-start, ch, direction); |
| 8758 | if (!result) |
| 8759 | return -1; |
| 8760 | return (result-(char*)PyUnicode_DATA(str)) >> (kind-1); |
| 8761 | } |
| 8762 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8763 | static int |
| 8764 | tailmatch(PyUnicodeObject *self, |
| 8765 | PyUnicodeObject *substring, |
| 8766 | Py_ssize_t start, |
| 8767 | Py_ssize_t end, |
| 8768 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8769 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8770 | int kind_self; |
| 8771 | int kind_sub; |
| 8772 | void *data_self; |
| 8773 | void *data_sub; |
| 8774 | Py_ssize_t offset; |
| 8775 | Py_ssize_t i; |
| 8776 | Py_ssize_t end_sub; |
| 8777 | |
| 8778 | if (PyUnicode_READY(self) == -1 || |
| 8779 | PyUnicode_READY(substring) == -1) |
| 8780 | return 0; |
| 8781 | |
| 8782 | if (PyUnicode_GET_LENGTH(substring) == 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8783 | return 1; |
| 8784 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8785 | ADJUST_INDICES(start, end, PyUnicode_GET_LENGTH(self)); |
| 8786 | end -= PyUnicode_GET_LENGTH(substring); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8787 | if (end < start) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8788 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8789 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8790 | kind_self = PyUnicode_KIND(self); |
| 8791 | data_self = PyUnicode_DATA(self); |
| 8792 | kind_sub = PyUnicode_KIND(substring); |
| 8793 | data_sub = PyUnicode_DATA(substring); |
| 8794 | end_sub = PyUnicode_GET_LENGTH(substring) - 1; |
| 8795 | |
| 8796 | if (direction > 0) |
| 8797 | offset = end; |
| 8798 | else |
| 8799 | offset = start; |
| 8800 | |
| 8801 | if (PyUnicode_READ(kind_self, data_self, offset) == |
| 8802 | PyUnicode_READ(kind_sub, data_sub, 0) && |
| 8803 | PyUnicode_READ(kind_self, data_self, offset + end_sub) == |
| 8804 | PyUnicode_READ(kind_sub, data_sub, end_sub)) { |
| 8805 | /* If both are of the same kind, memcmp is sufficient */ |
| 8806 | if (kind_self == kind_sub) { |
| 8807 | return ! memcmp((char *)data_self + |
| 8808 | (offset * PyUnicode_CHARACTER_SIZE(substring)), |
| 8809 | data_sub, |
| 8810 | PyUnicode_GET_LENGTH(substring) * |
| 8811 | PyUnicode_CHARACTER_SIZE(substring)); |
| 8812 | } |
| 8813 | /* otherwise we have to compare each character by first accesing it */ |
| 8814 | else { |
| 8815 | /* We do not need to compare 0 and len(substring)-1 because |
| 8816 | the if statement above ensured already that they are equal |
| 8817 | when we end up here. */ |
| 8818 | // TODO: honor direction and do a forward or backwards search |
| 8819 | for (i = 1; i < end_sub; ++i) { |
| 8820 | if (PyUnicode_READ(kind_self, data_self, offset + i) != |
| 8821 | PyUnicode_READ(kind_sub, data_sub, i)) |
| 8822 | return 0; |
| 8823 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8824 | return 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8825 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8826 | } |
| 8827 | |
| 8828 | return 0; |
| 8829 | } |
| 8830 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8831 | Py_ssize_t |
| 8832 | PyUnicode_Tailmatch(PyObject *str, |
| 8833 | PyObject *substr, |
| 8834 | Py_ssize_t start, |
| 8835 | Py_ssize_t end, |
| 8836 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8837 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8838 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8839 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8840 | str = PyUnicode_FromObject(str); |
| 8841 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8842 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8843 | substr = PyUnicode_FromObject(substr); |
| 8844 | if (substr == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8845 | Py_DECREF(str); |
| 8846 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8847 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8848 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8849 | result = tailmatch((PyUnicodeObject *)str, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8850 | (PyUnicodeObject *)substr, |
| 8851 | start, end, direction); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8852 | Py_DECREF(str); |
| 8853 | Py_DECREF(substr); |
| 8854 | return result; |
| 8855 | } |
| 8856 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8857 | /* Apply fixfct filter to the Unicode object self and return a |
| 8858 | reference to the modified object */ |
| 8859 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8860 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8861 | fixup(PyObject *self, |
| 8862 | Py_UCS4 (*fixfct)(PyObject *s)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8863 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8864 | PyObject *u; |
| 8865 | Py_UCS4 maxchar_old, maxchar_new = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8866 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8867 | if (PyUnicode_READY(self) == -1) |
| 8868 | return NULL; |
| 8869 | maxchar_old = PyUnicode_MAX_CHAR_VALUE(self); |
| 8870 | u = PyUnicode_New(PyUnicode_GET_LENGTH(self), |
| 8871 | maxchar_old); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8872 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8873 | return NULL; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8874 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8875 | Py_MEMCPY(PyUnicode_1BYTE_DATA(u), PyUnicode_1BYTE_DATA(self), |
| 8876 | PyUnicode_GET_LENGTH(u) * PyUnicode_CHARACTER_SIZE(u)); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8877 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8878 | /* fix functions return the new maximum character in a string, |
| 8879 | if the kind of the resulting unicode object does not change, |
| 8880 | everything is fine. Otherwise we need to change the string kind |
| 8881 | and re-run the fix function. */ |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8882 | maxchar_new = fixfct(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8883 | if (maxchar_new == 0) |
| 8884 | /* do nothing, keep maxchar_new at 0 which means no changes. */; |
| 8885 | else if (maxchar_new <= 127) |
| 8886 | maxchar_new = 127; |
| 8887 | else if (maxchar_new <= 255) |
| 8888 | maxchar_new = 255; |
| 8889 | else if (maxchar_new <= 65535) |
| 8890 | maxchar_new = 65535; |
| 8891 | else |
| 8892 | maxchar_new = 1114111; /* 0x10ffff */ |
| 8893 | |
| 8894 | if (!maxchar_new && PyUnicode_CheckExact(self)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8895 | /* fixfct should return TRUE if it modified the buffer. If |
| 8896 | FALSE, return a reference to the original buffer instead |
| 8897 | (to save space, not time) */ |
| 8898 | Py_INCREF(self); |
| 8899 | Py_DECREF(u); |
| 8900 | return (PyObject*) self; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8901 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8902 | else if (maxchar_new == maxchar_old) { |
| 8903 | return u; |
| 8904 | } |
| 8905 | else { |
| 8906 | /* In case the maximum character changed, we need to |
| 8907 | convert the string to the new category. */ |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 8908 | PyObject *v = PyUnicode_New(PyUnicode_GET_LENGTH(self), maxchar_new); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8909 | if (v == NULL) { |
| 8910 | Py_DECREF(u); |
| 8911 | return NULL; |
| 8912 | } |
| 8913 | if (maxchar_new > maxchar_old) { |
| 8914 | /* If the maxchar increased so that the kind changed, not all |
| 8915 | characters are representable anymore and we need to fix the |
| 8916 | string again. This only happens in very few cases. */ |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 8917 | copy_characters(v, 0, self, 0, PyUnicode_GET_LENGTH(self)); |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8918 | maxchar_old = fixfct(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8919 | assert(maxchar_old > 0 && maxchar_old <= maxchar_new); |
| 8920 | } |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 8921 | else { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 8922 | copy_characters(v, 0, u, 0, PyUnicode_GET_LENGTH(self)); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 8923 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8924 | |
| 8925 | Py_DECREF(u); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 8926 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8927 | return v; |
| 8928 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8929 | } |
| 8930 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8931 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8932 | fixupper(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8933 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8934 | /* No need to call PyUnicode_READY(self) because this function is only |
| 8935 | called as a callback from fixup() which does it already. */ |
| 8936 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 8937 | const int kind = PyUnicode_KIND(self); |
| 8938 | void *data = PyUnicode_DATA(self); |
| 8939 | int touched = 0; |
| 8940 | Py_UCS4 maxchar = 0; |
| 8941 | Py_ssize_t i; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8942 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8943 | for (i = 0; i < len; ++i) { |
| 8944 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 8945 | const Py_UCS4 up = Py_UNICODE_TOUPPER(ch); |
| 8946 | if (up != ch) { |
| 8947 | if (up > maxchar) |
| 8948 | maxchar = up; |
| 8949 | PyUnicode_WRITE(kind, data, i, up); |
| 8950 | touched = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8951 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8952 | else if (ch > maxchar) |
| 8953 | maxchar = ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8954 | } |
| 8955 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8956 | if (touched) |
| 8957 | return maxchar; |
| 8958 | else |
| 8959 | return 0; |
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 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8963 | fixlower(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8964 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8965 | /* No need to call PyUnicode_READY(self) because fixup() which does it. */ |
| 8966 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 8967 | const int kind = PyUnicode_KIND(self); |
| 8968 | void *data = PyUnicode_DATA(self); |
| 8969 | int touched = 0; |
| 8970 | Py_UCS4 maxchar = 0; |
| 8971 | Py_ssize_t i; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8972 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8973 | for(i = 0; i < len; ++i) { |
| 8974 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 8975 | const Py_UCS4 lo = Py_UNICODE_TOLOWER(ch); |
| 8976 | if (lo != ch) { |
| 8977 | if (lo > maxchar) |
| 8978 | maxchar = lo; |
| 8979 | PyUnicode_WRITE(kind, data, i, lo); |
| 8980 | touched = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8981 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8982 | else if (ch > maxchar) |
| 8983 | maxchar = ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8984 | } |
| 8985 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8986 | if (touched) |
| 8987 | return maxchar; |
| 8988 | else |
| 8989 | return 0; |
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 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8993 | fixswapcase(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8994 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8995 | /* No need to call PyUnicode_READY(self) because fixup() which does it. */ |
| 8996 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 8997 | const int kind = PyUnicode_KIND(self); |
| 8998 | void *data = PyUnicode_DATA(self); |
| 8999 | int touched = 0; |
| 9000 | Py_UCS4 maxchar = 0; |
| 9001 | Py_ssize_t i; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9002 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9003 | for(i = 0; i < len; ++i) { |
| 9004 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 9005 | Py_UCS4 nu = 0; |
| 9006 | |
| 9007 | if (Py_UNICODE_ISUPPER(ch)) |
| 9008 | nu = Py_UNICODE_TOLOWER(ch); |
| 9009 | else if (Py_UNICODE_ISLOWER(ch)) |
| 9010 | nu = Py_UNICODE_TOUPPER(ch); |
| 9011 | |
| 9012 | if (nu != 0) { |
| 9013 | if (nu > maxchar) |
| 9014 | maxchar = nu; |
| 9015 | PyUnicode_WRITE(kind, data, i, nu); |
| 9016 | touched = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9017 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9018 | else if (ch > maxchar) |
| 9019 | maxchar = ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9020 | } |
| 9021 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9022 | if (touched) |
| 9023 | return maxchar; |
| 9024 | else |
| 9025 | return 0; |
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 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9029 | fixcapitalize(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9030 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9031 | /* No need to call PyUnicode_READY(self) because fixup() which does it. */ |
| 9032 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 9033 | const int kind = PyUnicode_KIND(self); |
| 9034 | void *data = PyUnicode_DATA(self); |
| 9035 | int touched = 0; |
| 9036 | Py_UCS4 maxchar = 0; |
| 9037 | Py_ssize_t i = 0; |
| 9038 | Py_UCS4 ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9039 | |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9040 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9041 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9042 | |
| 9043 | ch = PyUnicode_READ(kind, data, i); |
| 9044 | if (!Py_UNICODE_ISUPPER(ch)) { |
| 9045 | maxchar = Py_UNICODE_TOUPPER(ch); |
| 9046 | PyUnicode_WRITE(kind, data, i, maxchar); |
| 9047 | touched = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9048 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9049 | ++i; |
| 9050 | for(; i < len; ++i) { |
| 9051 | ch = PyUnicode_READ(kind, data, i); |
| 9052 | if (!Py_UNICODE_ISLOWER(ch)) { |
| 9053 | const Py_UCS4 lo = Py_UNICODE_TOLOWER(ch); |
| 9054 | if (lo > maxchar) |
| 9055 | maxchar = lo; |
| 9056 | PyUnicode_WRITE(kind, data, i, lo); |
| 9057 | touched = 1; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9058 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9059 | else if (ch > maxchar) |
| 9060 | maxchar = ch; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9061 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9062 | |
| 9063 | if (touched) |
| 9064 | return maxchar; |
| 9065 | else |
| 9066 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9067 | } |
| 9068 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9069 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9070 | fixtitle(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9071 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9072 | /* No need to call PyUnicode_READY(self) because fixup() which does it. */ |
| 9073 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 9074 | const int kind = PyUnicode_KIND(self); |
| 9075 | void *data = PyUnicode_DATA(self); |
| 9076 | Py_UCS4 maxchar = 0; |
| 9077 | Py_ssize_t i = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9078 | int previous_is_cased; |
| 9079 | |
| 9080 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9081 | if (len == 1) { |
| 9082 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 9083 | const Py_UCS4 ti = Py_UNICODE_TOTITLE(ch); |
| 9084 | if (ti != ch) { |
| 9085 | PyUnicode_WRITE(kind, data, i, ti); |
| 9086 | return ti; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9087 | } |
| 9088 | else |
| 9089 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9090 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9091 | previous_is_cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9092 | for(; i < len; ++i) { |
| 9093 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 9094 | Py_UCS4 nu; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9095 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9096 | if (previous_is_cased) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9097 | nu = Py_UNICODE_TOLOWER(ch); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9098 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9099 | nu = Py_UNICODE_TOTITLE(ch); |
| 9100 | |
| 9101 | if (nu > maxchar) |
| 9102 | maxchar = nu; |
| 9103 | PyUnicode_WRITE(kind, data, i, nu); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9104 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9105 | if (Py_UNICODE_ISLOWER(ch) || |
| 9106 | Py_UNICODE_ISUPPER(ch) || |
| 9107 | Py_UNICODE_ISTITLE(ch)) |
| 9108 | previous_is_cased = 1; |
| 9109 | else |
| 9110 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9111 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9112 | return maxchar; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9113 | } |
| 9114 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9115 | PyObject * |
| 9116 | PyUnicode_Join(PyObject *separator, PyObject *seq) |
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 | PyObject *sep = NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9119 | Py_ssize_t seplen = 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9120 | PyObject *res = NULL; /* the result */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9121 | PyObject *fseq; /* PySequence_Fast(seq) */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9122 | Py_ssize_t seqlen; /* len(fseq) -- number of items in sequence */ |
| 9123 | PyObject **items; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9124 | PyObject *item; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9125 | Py_ssize_t sz, i, res_offset; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9126 | Py_UCS4 maxchar; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9127 | Py_UCS4 item_maxchar; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9128 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9129 | fseq = PySequence_Fast(seq, ""); |
| 9130 | if (fseq == NULL) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9131 | return NULL; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9132 | } |
| 9133 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9134 | /* NOTE: the following code can't call back into Python code, |
| 9135 | * so we are sure that fseq won't be mutated. |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 9136 | */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9137 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9138 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 9139 | /* If empty sequence, return u"". */ |
| 9140 | if (seqlen == 0) { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9141 | Py_DECREF(fseq); |
| 9142 | Py_INCREF(unicode_empty); |
| 9143 | res = unicode_empty; |
| 9144 | return res; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9145 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9146 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9147 | /* If singleton sequence with an exact Unicode, return that. */ |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9148 | items = PySequence_Fast_ITEMS(fseq); |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9149 | if (seqlen == 1) { |
| 9150 | if (PyUnicode_CheckExact(items[0])) { |
| 9151 | res = items[0]; |
| 9152 | Py_INCREF(res); |
| 9153 | Py_DECREF(fseq); |
| 9154 | return res; |
| 9155 | } |
| 9156 | sep = NULL; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9157 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9158 | else { |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9159 | /* Set up sep and seplen */ |
| 9160 | if (separator == NULL) { |
| 9161 | /* fall back to a blank space separator */ |
| 9162 | sep = PyUnicode_FromOrdinal(' '); |
| 9163 | if (!sep) |
| 9164 | goto onError; |
| 9165 | maxchar = 32; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9166 | } |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9167 | else { |
| 9168 | if (!PyUnicode_Check(separator)) { |
| 9169 | PyErr_Format(PyExc_TypeError, |
| 9170 | "separator: expected str instance," |
| 9171 | " %.80s found", |
| 9172 | Py_TYPE(separator)->tp_name); |
| 9173 | goto onError; |
| 9174 | } |
| 9175 | if (PyUnicode_READY(separator)) |
| 9176 | goto onError; |
| 9177 | sep = separator; |
| 9178 | seplen = PyUnicode_GET_LENGTH(separator); |
| 9179 | maxchar = PyUnicode_MAX_CHAR_VALUE(separator); |
| 9180 | /* inc refcount to keep this code path symmetric with the |
| 9181 | above case of a blank separator */ |
| 9182 | Py_INCREF(sep); |
| 9183 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9184 | } |
| 9185 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9186 | /* There are at least two things to join, or else we have a subclass |
| 9187 | * of str in the sequence. |
| 9188 | * Do a pre-pass to figure out the total amount of space we'll |
| 9189 | * need (sz), and see whether all argument are strings. |
| 9190 | */ |
| 9191 | sz = 0; |
| 9192 | for (i = 0; i < seqlen; i++) { |
| 9193 | const Py_ssize_t old_sz = sz; |
| 9194 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9195 | if (!PyUnicode_Check(item)) { |
| 9196 | PyErr_Format(PyExc_TypeError, |
| 9197 | "sequence item %zd: expected str instance," |
| 9198 | " %.80s found", |
| 9199 | i, Py_TYPE(item)->tp_name); |
| 9200 | goto onError; |
| 9201 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9202 | if (PyUnicode_READY(item) == -1) |
| 9203 | goto onError; |
| 9204 | sz += PyUnicode_GET_LENGTH(item); |
| 9205 | item_maxchar = PyUnicode_MAX_CHAR_VALUE(item); |
| 9206 | if (item_maxchar > maxchar) |
| 9207 | maxchar = item_maxchar; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9208 | if (i != 0) |
| 9209 | sz += seplen; |
| 9210 | if (sz < old_sz || sz > PY_SSIZE_T_MAX) { |
| 9211 | PyErr_SetString(PyExc_OverflowError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9212 | "join() result is too long for a Python string"); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9213 | goto onError; |
| 9214 | } |
| 9215 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9216 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9217 | res = PyUnicode_New(sz, maxchar); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9218 | if (res == NULL) |
| 9219 | goto onError; |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 9220 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9221 | /* Catenate everything. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9222 | for (i = 0, res_offset = 0; i < seqlen; ++i) { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9223 | Py_ssize_t itemlen; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9224 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9225 | /* Copy item, and maybe the separator. */ |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 9226 | if (i && seplen != 0) { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9227 | copy_characters(res, res_offset, sep, 0, seplen); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9228 | res_offset += seplen; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9229 | } |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 9230 | itemlen = PyUnicode_GET_LENGTH(item); |
| 9231 | if (itemlen != 0) { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9232 | copy_characters(res, res_offset, item, 0, itemlen); |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 9233 | res_offset += itemlen; |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 9234 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9235 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9236 | assert(res_offset == PyUnicode_GET_LENGTH(res)); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9237 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9238 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9239 | Py_XDECREF(sep); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 9240 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9241 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9242 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9243 | onError: |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9244 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9245 | Py_XDECREF(sep); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9246 | Py_XDECREF(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9247 | return NULL; |
| 9248 | } |
| 9249 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9250 | #define FILL(kind, data, value, start, length) \ |
| 9251 | do { \ |
| 9252 | Py_ssize_t i_ = 0; \ |
| 9253 | assert(kind != PyUnicode_WCHAR_KIND); \ |
| 9254 | switch ((kind)) { \ |
| 9255 | case PyUnicode_1BYTE_KIND: { \ |
| 9256 | unsigned char * to_ = (unsigned char *)((data)) + (start); \ |
| 9257 | memset(to_, (unsigned char)value, length); \ |
| 9258 | break; \ |
| 9259 | } \ |
| 9260 | case PyUnicode_2BYTE_KIND: { \ |
| 9261 | Py_UCS2 * to_ = (Py_UCS2 *)((data)) + (start); \ |
| 9262 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 9263 | break; \ |
| 9264 | } \ |
| 9265 | default: { \ |
| 9266 | Py_UCS4 * to_ = (Py_UCS4 *)((data)) + (start); \ |
| 9267 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 9268 | break; \ |
| 9269 | } \ |
| 9270 | } \ |
| 9271 | } while (0) |
| 9272 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9273 | static PyObject * |
| 9274 | pad(PyObject *self, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9275 | Py_ssize_t left, |
| 9276 | Py_ssize_t right, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9277 | Py_UCS4 fill) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9278 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9279 | PyObject *u; |
| 9280 | Py_UCS4 maxchar; |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9281 | int kind; |
| 9282 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9283 | |
| 9284 | if (left < 0) |
| 9285 | left = 0; |
| 9286 | if (right < 0) |
| 9287 | right = 0; |
| 9288 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 9289 | if (left == 0 && right == 0 && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9290 | Py_INCREF(self); |
| 9291 | return self; |
| 9292 | } |
| 9293 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9294 | if (left > PY_SSIZE_T_MAX - _PyUnicode_LENGTH(self) || |
| 9295 | right > PY_SSIZE_T_MAX - (left + _PyUnicode_LENGTH(self))) { |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 9296 | PyErr_SetString(PyExc_OverflowError, "padded string is too long"); |
| 9297 | return NULL; |
| 9298 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9299 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
| 9300 | if (fill > maxchar) |
| 9301 | maxchar = fill; |
| 9302 | u = PyUnicode_New(left + _PyUnicode_LENGTH(self) + right, maxchar); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9303 | if (!u) |
| 9304 | return NULL; |
| 9305 | |
| 9306 | kind = PyUnicode_KIND(u); |
| 9307 | data = PyUnicode_DATA(u); |
| 9308 | if (left) |
| 9309 | FILL(kind, data, fill, 0, left); |
| 9310 | if (right) |
| 9311 | FILL(kind, data, fill, left + _PyUnicode_LENGTH(self), right); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9312 | copy_characters(u, left, self, 0, _PyUnicode_LENGTH(self)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 9313 | assert(_PyUnicode_CheckConsistency(u, 1)); |
| 9314 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9315 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9316 | #undef FILL |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9317 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9318 | PyObject * |
| 9319 | PyUnicode_Splitlines(PyObject *string, int keepends) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9320 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9321 | PyObject *list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9322 | |
| 9323 | string = PyUnicode_FromObject(string); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9324 | if (string == NULL || PyUnicode_READY(string) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9325 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9326 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9327 | switch(PyUnicode_KIND(string)) { |
| 9328 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9329 | if (PyUnicode_IS_ASCII(string)) |
| 9330 | list = asciilib_splitlines( |
| 9331 | (PyObject*) string, PyUnicode_1BYTE_DATA(string), |
| 9332 | PyUnicode_GET_LENGTH(string), keepends); |
| 9333 | else |
| 9334 | list = ucs1lib_splitlines( |
| 9335 | (PyObject*) string, PyUnicode_1BYTE_DATA(string), |
| 9336 | PyUnicode_GET_LENGTH(string), keepends); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9337 | break; |
| 9338 | case PyUnicode_2BYTE_KIND: |
| 9339 | list = ucs2lib_splitlines( |
| 9340 | (PyObject*) string, PyUnicode_2BYTE_DATA(string), |
| 9341 | PyUnicode_GET_LENGTH(string), keepends); |
| 9342 | break; |
| 9343 | case PyUnicode_4BYTE_KIND: |
| 9344 | list = ucs4lib_splitlines( |
| 9345 | (PyObject*) string, PyUnicode_4BYTE_DATA(string), |
| 9346 | PyUnicode_GET_LENGTH(string), keepends); |
| 9347 | break; |
| 9348 | default: |
| 9349 | assert(0); |
| 9350 | list = 0; |
| 9351 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9352 | Py_DECREF(string); |
| 9353 | return list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9354 | } |
| 9355 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9356 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9357 | split(PyObject *self, |
| 9358 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9359 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9360 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9361 | int kind1, kind2, kind; |
| 9362 | void *buf1, *buf2; |
| 9363 | Py_ssize_t len1, len2; |
| 9364 | PyObject* out; |
| 9365 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9366 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9367 | maxcount = PY_SSIZE_T_MAX; |
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 | if (PyUnicode_READY(self) == -1) |
| 9370 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9371 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9372 | if (substring == NULL) |
| 9373 | switch(PyUnicode_KIND(self)) { |
| 9374 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9375 | if (PyUnicode_IS_ASCII(self)) |
| 9376 | return asciilib_split_whitespace( |
| 9377 | (PyObject*) self, PyUnicode_1BYTE_DATA(self), |
| 9378 | PyUnicode_GET_LENGTH(self), maxcount |
| 9379 | ); |
| 9380 | else |
| 9381 | return ucs1lib_split_whitespace( |
| 9382 | (PyObject*) self, PyUnicode_1BYTE_DATA(self), |
| 9383 | PyUnicode_GET_LENGTH(self), maxcount |
| 9384 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9385 | case PyUnicode_2BYTE_KIND: |
| 9386 | return ucs2lib_split_whitespace( |
| 9387 | (PyObject*) self, PyUnicode_2BYTE_DATA(self), |
| 9388 | PyUnicode_GET_LENGTH(self), maxcount |
| 9389 | ); |
| 9390 | case PyUnicode_4BYTE_KIND: |
| 9391 | return ucs4lib_split_whitespace( |
| 9392 | (PyObject*) self, PyUnicode_4BYTE_DATA(self), |
| 9393 | PyUnicode_GET_LENGTH(self), maxcount |
| 9394 | ); |
| 9395 | default: |
| 9396 | assert(0); |
| 9397 | return NULL; |
| 9398 | } |
| 9399 | |
| 9400 | if (PyUnicode_READY(substring) == -1) |
| 9401 | return NULL; |
| 9402 | |
| 9403 | kind1 = PyUnicode_KIND(self); |
| 9404 | kind2 = PyUnicode_KIND(substring); |
| 9405 | kind = kind1 > kind2 ? kind1 : kind2; |
| 9406 | buf1 = PyUnicode_DATA(self); |
| 9407 | buf2 = PyUnicode_DATA(substring); |
| 9408 | if (kind1 != kind) |
| 9409 | buf1 = _PyUnicode_AsKind((PyObject*)self, kind); |
| 9410 | if (!buf1) |
| 9411 | return NULL; |
| 9412 | if (kind2 != kind) |
| 9413 | buf2 = _PyUnicode_AsKind((PyObject*)substring, kind); |
| 9414 | if (!buf2) { |
| 9415 | if (kind1 != kind) PyMem_Free(buf1); |
| 9416 | return NULL; |
| 9417 | } |
| 9418 | len1 = PyUnicode_GET_LENGTH(self); |
| 9419 | len2 = PyUnicode_GET_LENGTH(substring); |
| 9420 | |
| 9421 | switch(kind) { |
| 9422 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9423 | if (PyUnicode_IS_ASCII(self) && PyUnicode_IS_ASCII(substring)) |
| 9424 | out = asciilib_split( |
| 9425 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
| 9426 | else |
| 9427 | out = ucs1lib_split( |
| 9428 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9429 | break; |
| 9430 | case PyUnicode_2BYTE_KIND: |
| 9431 | out = ucs2lib_split( |
| 9432 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
| 9433 | break; |
| 9434 | case PyUnicode_4BYTE_KIND: |
| 9435 | out = ucs4lib_split( |
| 9436 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
| 9437 | break; |
| 9438 | default: |
| 9439 | out = NULL; |
| 9440 | } |
| 9441 | if (kind1 != kind) |
| 9442 | PyMem_Free(buf1); |
| 9443 | if (kind2 != kind) |
| 9444 | PyMem_Free(buf2); |
| 9445 | return out; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9446 | } |
| 9447 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9448 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9449 | rsplit(PyObject *self, |
| 9450 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9451 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9452 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9453 | int kind1, kind2, kind; |
| 9454 | void *buf1, *buf2; |
| 9455 | Py_ssize_t len1, len2; |
| 9456 | PyObject* out; |
| 9457 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9458 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9459 | maxcount = PY_SSIZE_T_MAX; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9460 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9461 | if (PyUnicode_READY(self) == -1) |
| 9462 | return NULL; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9463 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9464 | if (substring == NULL) |
| 9465 | switch(PyUnicode_KIND(self)) { |
| 9466 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9467 | if (PyUnicode_IS_ASCII(self)) |
| 9468 | return asciilib_rsplit_whitespace( |
| 9469 | (PyObject*) self, PyUnicode_1BYTE_DATA(self), |
| 9470 | PyUnicode_GET_LENGTH(self), maxcount |
| 9471 | ); |
| 9472 | else |
| 9473 | return ucs1lib_rsplit_whitespace( |
| 9474 | (PyObject*) self, PyUnicode_1BYTE_DATA(self), |
| 9475 | PyUnicode_GET_LENGTH(self), maxcount |
| 9476 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9477 | case PyUnicode_2BYTE_KIND: |
| 9478 | return ucs2lib_rsplit_whitespace( |
| 9479 | (PyObject*) self, PyUnicode_2BYTE_DATA(self), |
| 9480 | PyUnicode_GET_LENGTH(self), maxcount |
| 9481 | ); |
| 9482 | case PyUnicode_4BYTE_KIND: |
| 9483 | return ucs4lib_rsplit_whitespace( |
| 9484 | (PyObject*) self, PyUnicode_4BYTE_DATA(self), |
| 9485 | PyUnicode_GET_LENGTH(self), maxcount |
| 9486 | ); |
| 9487 | default: |
| 9488 | assert(0); |
| 9489 | return NULL; |
| 9490 | } |
| 9491 | |
| 9492 | if (PyUnicode_READY(substring) == -1) |
| 9493 | return NULL; |
| 9494 | |
| 9495 | kind1 = PyUnicode_KIND(self); |
| 9496 | kind2 = PyUnicode_KIND(substring); |
| 9497 | kind = kind1 > kind2 ? kind1 : kind2; |
| 9498 | buf1 = PyUnicode_DATA(self); |
| 9499 | buf2 = PyUnicode_DATA(substring); |
| 9500 | if (kind1 != kind) |
| 9501 | buf1 = _PyUnicode_AsKind((PyObject*)self, kind); |
| 9502 | if (!buf1) |
| 9503 | return NULL; |
| 9504 | if (kind2 != kind) |
| 9505 | buf2 = _PyUnicode_AsKind((PyObject*)substring, kind); |
| 9506 | if (!buf2) { |
| 9507 | if (kind1 != kind) PyMem_Free(buf1); |
| 9508 | return NULL; |
| 9509 | } |
| 9510 | len1 = PyUnicode_GET_LENGTH(self); |
| 9511 | len2 = PyUnicode_GET_LENGTH(substring); |
| 9512 | |
| 9513 | switch(kind) { |
| 9514 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9515 | if (PyUnicode_IS_ASCII(self) && PyUnicode_IS_ASCII(substring)) |
| 9516 | out = asciilib_rsplit( |
| 9517 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
| 9518 | else |
| 9519 | out = ucs1lib_rsplit( |
| 9520 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9521 | break; |
| 9522 | case PyUnicode_2BYTE_KIND: |
| 9523 | out = ucs2lib_rsplit( |
| 9524 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
| 9525 | break; |
| 9526 | case PyUnicode_4BYTE_KIND: |
| 9527 | out = ucs4lib_rsplit( |
| 9528 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
| 9529 | break; |
| 9530 | default: |
| 9531 | out = NULL; |
| 9532 | } |
| 9533 | if (kind1 != kind) |
| 9534 | PyMem_Free(buf1); |
| 9535 | if (kind2 != kind) |
| 9536 | PyMem_Free(buf2); |
| 9537 | return out; |
| 9538 | } |
| 9539 | |
| 9540 | static Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9541 | anylib_find(int kind, PyObject *str1, void *buf1, Py_ssize_t len1, |
| 9542 | 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] | 9543 | { |
| 9544 | switch(kind) { |
| 9545 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9546 | if (PyUnicode_IS_ASCII(str1) && PyUnicode_IS_ASCII(str2)) |
| 9547 | return asciilib_find(buf1, len1, buf2, len2, offset); |
| 9548 | else |
| 9549 | return ucs1lib_find(buf1, len1, buf2, len2, offset); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9550 | case PyUnicode_2BYTE_KIND: |
| 9551 | return ucs2lib_find(buf1, len1, buf2, len2, offset); |
| 9552 | case PyUnicode_4BYTE_KIND: |
| 9553 | return ucs4lib_find(buf1, len1, buf2, len2, offset); |
| 9554 | } |
| 9555 | assert(0); |
| 9556 | return -1; |
| 9557 | } |
| 9558 | |
| 9559 | static Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9560 | anylib_count(int kind, PyObject *sstr, void* sbuf, Py_ssize_t slen, |
| 9561 | 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] | 9562 | { |
| 9563 | switch(kind) { |
| 9564 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9565 | if (PyUnicode_IS_ASCII(sstr) && PyUnicode_IS_ASCII(str1)) |
| 9566 | return asciilib_count(sbuf, slen, buf1, len1, maxcount); |
| 9567 | else |
| 9568 | return ucs1lib_count(sbuf, slen, buf1, len1, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9569 | case PyUnicode_2BYTE_KIND: |
| 9570 | return ucs2lib_count(sbuf, slen, buf1, len1, maxcount); |
| 9571 | case PyUnicode_4BYTE_KIND: |
| 9572 | return ucs4lib_count(sbuf, slen, buf1, len1, maxcount); |
| 9573 | } |
| 9574 | assert(0); |
| 9575 | return 0; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9576 | } |
| 9577 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9578 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9579 | replace(PyObject *self, PyObject *str1, |
| 9580 | PyObject *str2, Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9581 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9582 | PyObject *u; |
| 9583 | char *sbuf = PyUnicode_DATA(self); |
| 9584 | char *buf1 = PyUnicode_DATA(str1); |
| 9585 | char *buf2 = PyUnicode_DATA(str2); |
| 9586 | int srelease = 0, release1 = 0, release2 = 0; |
| 9587 | int skind = PyUnicode_KIND(self); |
| 9588 | int kind1 = PyUnicode_KIND(str1); |
| 9589 | int kind2 = PyUnicode_KIND(str2); |
| 9590 | Py_ssize_t slen = PyUnicode_GET_LENGTH(self); |
| 9591 | Py_ssize_t len1 = PyUnicode_GET_LENGTH(str1); |
| 9592 | Py_ssize_t len2 = PyUnicode_GET_LENGTH(str2); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9593 | |
| 9594 | if (maxcount < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9595 | maxcount = PY_SSIZE_T_MAX; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9596 | else if (maxcount == 0 || slen == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9597 | goto nothing; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9598 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9599 | if (skind < kind1) |
| 9600 | /* substring too wide to be present */ |
| 9601 | goto nothing; |
| 9602 | |
| 9603 | if (len1 == len2) { |
Antoine Pitrou | cbfdee3 | 2010-01-13 08:58:08 +0000 | [diff] [blame] | 9604 | Py_ssize_t i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9605 | /* same length */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9606 | if (len1 == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9607 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9608 | if (len1 == 1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9609 | /* replace characters */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9610 | Py_UCS4 u1, u2, maxchar; |
| 9611 | int mayshrink, rkind; |
| 9612 | u1 = PyUnicode_READ_CHAR(str1, 0); |
| 9613 | if (!findchar(sbuf, PyUnicode_KIND(self), |
| 9614 | slen, u1, 1)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9615 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9616 | u2 = PyUnicode_READ_CHAR(str2, 0); |
| 9617 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
| 9618 | /* Replacing u1 with u2 may cause a maxchar reduction in the |
| 9619 | result string. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9620 | if (u2 > maxchar) { |
| 9621 | maxchar = u2; |
| 9622 | mayshrink = 0; |
| 9623 | } |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 9624 | else |
| 9625 | mayshrink = maxchar > 127; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9626 | u = PyUnicode_New(slen, maxchar); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9627 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9628 | goto error; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9629 | copy_characters(u, 0, self, 0, slen); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9630 | rkind = PyUnicode_KIND(u); |
| 9631 | for (i = 0; i < PyUnicode_GET_LENGTH(u); i++) |
| 9632 | if (PyUnicode_READ(rkind, PyUnicode_DATA(u), i) == u1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9633 | if (--maxcount < 0) |
| 9634 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9635 | PyUnicode_WRITE(rkind, PyUnicode_DATA(u), i, u2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9636 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9637 | if (mayshrink) { |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 9638 | unicode_adjust_maxchar(&u); |
| 9639 | if (u == NULL) |
| 9640 | goto error; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9641 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9642 | } else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9643 | int rkind = skind; |
| 9644 | char *res; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 9645 | PyObject *rstr; |
| 9646 | Py_UCS4 maxchar; |
| 9647 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9648 | if (kind1 < rkind) { |
| 9649 | /* widen substring */ |
| 9650 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 9651 | if (!buf1) goto error; |
| 9652 | release1 = 1; |
| 9653 | } |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9654 | i = anylib_find(rkind, self, sbuf, slen, str1, buf1, len1, 0); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9655 | if (i < 0) |
| 9656 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9657 | if (rkind > kind2) { |
| 9658 | /* widen replacement */ |
| 9659 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 9660 | if (!buf2) goto error; |
| 9661 | release2 = 1; |
| 9662 | } |
| 9663 | else if (rkind < kind2) { |
| 9664 | /* widen self and buf1 */ |
| 9665 | rkind = kind2; |
| 9666 | if (release1) PyMem_Free(buf1); |
| 9667 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 9668 | if (!sbuf) goto error; |
| 9669 | srelease = 1; |
| 9670 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 9671 | if (!buf1) goto error; |
| 9672 | release1 = 1; |
| 9673 | } |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 9674 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
| 9675 | maxchar = Py_MAX(maxchar, PyUnicode_MAX_CHAR_VALUE(str2)); |
| 9676 | rstr = PyUnicode_New(slen, maxchar); |
| 9677 | if (!rstr) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9678 | goto error; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 9679 | res = PyUnicode_DATA(rstr); |
| 9680 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9681 | memcpy(res, sbuf, PyUnicode_KIND_SIZE(rkind, slen)); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9682 | /* change everything in-place, starting with this one */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9683 | memcpy(res + PyUnicode_KIND_SIZE(rkind, i), |
| 9684 | buf2, |
| 9685 | PyUnicode_KIND_SIZE(rkind, len2)); |
| 9686 | i += len1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9687 | |
| 9688 | while ( --maxcount > 0) { |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9689 | i = anylib_find(rkind, self, |
| 9690 | sbuf+PyUnicode_KIND_SIZE(rkind, i), slen-i, |
| 9691 | str1, buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9692 | if (i == -1) |
| 9693 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9694 | memcpy(res + PyUnicode_KIND_SIZE(rkind, i), |
| 9695 | buf2, |
| 9696 | PyUnicode_KIND_SIZE(rkind, len2)); |
| 9697 | i += len1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9698 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9699 | |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 9700 | u = rstr; |
| 9701 | unicode_adjust_maxchar(&u); |
| 9702 | if (!u) |
| 9703 | goto error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9704 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9705 | } else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9706 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9707 | Py_ssize_t n, i, j, ires; |
| 9708 | Py_ssize_t product, new_size; |
| 9709 | int rkind = skind; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 9710 | PyObject *rstr; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9711 | char *res; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 9712 | Py_UCS4 maxchar; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9713 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9714 | if (kind1 < rkind) { |
| 9715 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 9716 | if (!buf1) goto error; |
| 9717 | release1 = 1; |
| 9718 | } |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9719 | n = anylib_count(rkind, self, sbuf, slen, str1, buf1, len1, maxcount); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9720 | if (n == 0) |
| 9721 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9722 | if (kind2 < rkind) { |
| 9723 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 9724 | if (!buf2) goto error; |
| 9725 | release2 = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9726 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9727 | else if (kind2 > rkind) { |
| 9728 | rkind = kind2; |
| 9729 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 9730 | if (!sbuf) goto error; |
| 9731 | srelease = 1; |
| 9732 | if (release1) PyMem_Free(buf1); |
| 9733 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 9734 | if (!buf1) goto error; |
| 9735 | release1 = 1; |
| 9736 | } |
| 9737 | /* new_size = PyUnicode_GET_LENGTH(self) + n * (PyUnicode_GET_LENGTH(str2) - |
| 9738 | PyUnicode_GET_LENGTH(str1))); */ |
| 9739 | product = n * (len2-len1); |
| 9740 | if ((product / (len2-len1)) != n) { |
| 9741 | PyErr_SetString(PyExc_OverflowError, |
| 9742 | "replace string is too long"); |
| 9743 | goto error; |
| 9744 | } |
| 9745 | new_size = slen + product; |
| 9746 | if (new_size < 0 || new_size > (PY_SSIZE_T_MAX >> (rkind-1))) { |
| 9747 | PyErr_SetString(PyExc_OverflowError, |
| 9748 | "replace string is too long"); |
| 9749 | goto error; |
| 9750 | } |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 9751 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
| 9752 | maxchar = Py_MAX(maxchar, PyUnicode_MAX_CHAR_VALUE(str2)); |
| 9753 | rstr = PyUnicode_New(new_size, maxchar); |
| 9754 | if (!rstr) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9755 | goto error; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 9756 | res = PyUnicode_DATA(rstr); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9757 | ires = i = 0; |
| 9758 | if (len1 > 0) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9759 | while (n-- > 0) { |
| 9760 | /* look for next match */ |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9761 | j = anylib_find(rkind, self, |
| 9762 | sbuf + PyUnicode_KIND_SIZE(rkind, i), slen-i, |
| 9763 | str1, buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9764 | if (j == -1) |
| 9765 | break; |
| 9766 | else if (j > i) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9767 | /* copy unchanged part [i:j] */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9768 | memcpy(res + PyUnicode_KIND_SIZE(rkind, ires), |
| 9769 | sbuf + PyUnicode_KIND_SIZE(rkind, i), |
| 9770 | PyUnicode_KIND_SIZE(rkind, j-i)); |
| 9771 | ires += j - i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9772 | } |
| 9773 | /* copy substitution string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9774 | if (len2 > 0) { |
| 9775 | memcpy(res + PyUnicode_KIND_SIZE(rkind, ires), |
| 9776 | buf2, |
| 9777 | PyUnicode_KIND_SIZE(rkind, len2)); |
| 9778 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9779 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9780 | i = j + len1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9781 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9782 | if (i < slen) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9783 | /* copy tail [i:] */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9784 | memcpy(res + PyUnicode_KIND_SIZE(rkind, ires), |
| 9785 | sbuf + PyUnicode_KIND_SIZE(rkind, i), |
| 9786 | PyUnicode_KIND_SIZE(rkind, slen-i)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9787 | } else { |
| 9788 | /* interleave */ |
| 9789 | while (n > 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9790 | memcpy(res + PyUnicode_KIND_SIZE(rkind, ires), |
| 9791 | buf2, |
| 9792 | PyUnicode_KIND_SIZE(rkind, len2)); |
| 9793 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9794 | if (--n <= 0) |
| 9795 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9796 | memcpy(res + PyUnicode_KIND_SIZE(rkind, ires), |
| 9797 | sbuf + PyUnicode_KIND_SIZE(rkind, i), |
| 9798 | PyUnicode_KIND_SIZE(rkind, 1)); |
| 9799 | ires++; |
| 9800 | i++; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9801 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9802 | memcpy(res + PyUnicode_KIND_SIZE(rkind, ires), |
| 9803 | sbuf + PyUnicode_KIND_SIZE(rkind, i), |
| 9804 | PyUnicode_KIND_SIZE(rkind, slen-i)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9805 | } |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 9806 | u = rstr; |
| 9807 | unicode_adjust_maxchar(&u); |
| 9808 | if (u == NULL) |
| 9809 | goto error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9810 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9811 | if (srelease) |
| 9812 | PyMem_FREE(sbuf); |
| 9813 | if (release1) |
| 9814 | PyMem_FREE(buf1); |
| 9815 | if (release2) |
| 9816 | PyMem_FREE(buf2); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 9817 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9818 | return u; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9819 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9820 | nothing: |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9821 | /* nothing to replace; return original string (when possible) */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9822 | if (srelease) |
| 9823 | PyMem_FREE(sbuf); |
| 9824 | if (release1) |
| 9825 | PyMem_FREE(buf1); |
| 9826 | if (release2) |
| 9827 | PyMem_FREE(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9828 | if (PyUnicode_CheckExact(self)) { |
| 9829 | Py_INCREF(self); |
| 9830 | return (PyObject *) self; |
| 9831 | } |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 9832 | return PyUnicode_Copy(self); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9833 | error: |
| 9834 | if (srelease && sbuf) |
| 9835 | PyMem_FREE(sbuf); |
| 9836 | if (release1 && buf1) |
| 9837 | PyMem_FREE(buf1); |
| 9838 | if (release2 && buf2) |
| 9839 | PyMem_FREE(buf2); |
| 9840 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9841 | } |
| 9842 | |
| 9843 | /* --- Unicode Object Methods --------------------------------------------- */ |
| 9844 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9845 | PyDoc_STRVAR(title__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9846 | "S.title() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9847 | \n\ |
| 9848 | 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] | 9849 | characters, all remaining cased characters have lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9850 | |
| 9851 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9852 | unicode_title(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9853 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9854 | return fixup(self, fixtitle); |
| 9855 | } |
| 9856 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9857 | PyDoc_STRVAR(capitalize__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9858 | "S.capitalize() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9859 | \n\ |
| 9860 | 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] | 9861 | have upper case and the rest lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9862 | |
| 9863 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9864 | unicode_capitalize(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9865 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9866 | return fixup(self, fixcapitalize); |
| 9867 | } |
| 9868 | |
| 9869 | #if 0 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9870 | PyDoc_STRVAR(capwords__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9871 | "S.capwords() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9872 | \n\ |
| 9873 | 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] | 9874 | normalized whitespace (all whitespace strings are replaced by ' ')."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9875 | |
| 9876 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 9877 | unicode_capwords(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9878 | { |
| 9879 | PyObject *list; |
| 9880 | PyObject *item; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9881 | Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9882 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9883 | /* Split into words */ |
| 9884 | list = split(self, NULL, -1); |
| 9885 | if (!list) |
| 9886 | return NULL; |
| 9887 | |
| 9888 | /* Capitalize each word */ |
| 9889 | for (i = 0; i < PyList_GET_SIZE(list); i++) { |
| 9890 | item = fixup((PyUnicodeObject *)PyList_GET_ITEM(list, i), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9891 | fixcapitalize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9892 | if (item == NULL) |
| 9893 | goto onError; |
| 9894 | Py_DECREF(PyList_GET_ITEM(list, i)); |
| 9895 | PyList_SET_ITEM(list, i, item); |
| 9896 | } |
| 9897 | |
| 9898 | /* Join the words to form a new string */ |
| 9899 | item = PyUnicode_Join(NULL, list); |
| 9900 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9901 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9902 | Py_DECREF(list); |
| 9903 | return (PyObject *)item; |
| 9904 | } |
| 9905 | #endif |
| 9906 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 9907 | /* Argument converter. Coerces to a single unicode character */ |
| 9908 | |
| 9909 | static int |
| 9910 | convert_uc(PyObject *obj, void *addr) |
| 9911 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9912 | Py_UCS4 *fillcharloc = (Py_UCS4 *)addr; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9913 | PyObject *uniobj; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 9914 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9915 | uniobj = PyUnicode_FromObject(obj); |
| 9916 | if (uniobj == NULL) { |
| 9917 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9918 | "The fill character cannot be converted to Unicode"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9919 | return 0; |
| 9920 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9921 | if (PyUnicode_GET_LENGTH(uniobj) != 1) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9922 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9923 | "The fill character must be exactly one character long"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9924 | Py_DECREF(uniobj); |
| 9925 | return 0; |
| 9926 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9927 | *fillcharloc = PyUnicode_READ_CHAR(uniobj, 0); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9928 | Py_DECREF(uniobj); |
| 9929 | return 1; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 9930 | } |
| 9931 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9932 | PyDoc_STRVAR(center__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9933 | "S.center(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9934 | \n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 9935 | Return S centered in a string of length width. Padding is\n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 9936 | done using the specified fill character (default is a space)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9937 | |
| 9938 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9939 | unicode_center(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9940 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9941 | Py_ssize_t marg, left; |
| 9942 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9943 | Py_UCS4 fillchar = ' '; |
| 9944 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 9945 | 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] | 9946 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9947 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 9948 | if (PyUnicode_READY(self) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9949 | return NULL; |
| 9950 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9951 | if (_PyUnicode_LENGTH(self) >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9952 | Py_INCREF(self); |
| 9953 | return (PyObject*) self; |
| 9954 | } |
| 9955 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9956 | marg = width - _PyUnicode_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9957 | left = marg / 2 + (marg & width & 1); |
| 9958 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9959 | return pad(self, left, marg - left, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9960 | } |
| 9961 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 9962 | #if 0 |
| 9963 | |
| 9964 | /* This code should go into some future Unicode collation support |
| 9965 | module. The basic comparison should compare ordinals on a naive |
Georg Brandl | c6c3178 | 2009-06-08 13:41:29 +0000 | [diff] [blame] | 9966 | basis (this is what Java does and thus Jython too). */ |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 9967 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9968 | /* speedy UTF-16 code point order comparison */ |
| 9969 | /* gleaned from: */ |
| 9970 | /* http://www-4.ibm.com/software/developer/library/utf16.html?dwzone=unicode */ |
| 9971 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 9972 | static short utf16Fixup[32] = |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9973 | { |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9974 | 0, 0, 0, 0, 0, 0, 0, 0, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9975 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 9976 | 0, 0, 0, 0, 0, 0, 0, 0, |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 9977 | 0, 0, 0, 0x2000, -0x800, -0x800, -0x800, -0x800 |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9978 | }; |
| 9979 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9980 | static int |
| 9981 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 9982 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9983 | Py_ssize_t len1, len2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9984 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9985 | Py_UNICODE *s1 = str1->str; |
| 9986 | Py_UNICODE *s2 = str2->str; |
| 9987 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9988 | len1 = str1->_base._base.length; |
| 9989 | len2 = str2->_base._base.length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9990 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9991 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9992 | Py_UNICODE c1, c2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9993 | |
| 9994 | c1 = *s1++; |
| 9995 | c2 = *s2++; |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 9996 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9997 | if (c1 > (1<<11) * 26) |
| 9998 | c1 += utf16Fixup[c1>>11]; |
| 9999 | if (c2 > (1<<11) * 26) |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 10000 | c2 += utf16Fixup[c2>>11]; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 10001 | /* now c1 and c2 are in UTF-32-compatible order */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 10002 | |
| 10003 | if (c1 != c2) |
| 10004 | return (c1 < c2) ? -1 : 1; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 10005 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 10006 | len1--; len2--; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10007 | } |
| 10008 | |
| 10009 | return (len1 < len2) ? -1 : (len1 != len2); |
| 10010 | } |
| 10011 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10012 | #else |
| 10013 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10014 | /* This function assumes that str1 and str2 are readied by the caller. */ |
| 10015 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10016 | static int |
| 10017 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 10018 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10019 | int kind1, kind2; |
| 10020 | void *data1, *data2; |
| 10021 | Py_ssize_t len1, len2, i; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10022 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10023 | kind1 = PyUnicode_KIND(str1); |
| 10024 | kind2 = PyUnicode_KIND(str2); |
| 10025 | data1 = PyUnicode_DATA(str1); |
| 10026 | data2 = PyUnicode_DATA(str2); |
| 10027 | len1 = PyUnicode_GET_LENGTH(str1); |
| 10028 | len2 = PyUnicode_GET_LENGTH(str2); |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10029 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10030 | for (i = 0; i < len1 && i < len2; ++i) { |
| 10031 | Py_UCS4 c1, c2; |
| 10032 | c1 = PyUnicode_READ(kind1, data1, i); |
| 10033 | c2 = PyUnicode_READ(kind2, data2, i); |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 10034 | |
| 10035 | if (c1 != c2) |
| 10036 | return (c1 < c2) ? -1 : 1; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10037 | } |
| 10038 | |
| 10039 | return (len1 < len2) ? -1 : (len1 != len2); |
| 10040 | } |
| 10041 | |
| 10042 | #endif |
| 10043 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10044 | int |
| 10045 | PyUnicode_Compare(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10046 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10047 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 10048 | if (PyUnicode_READY(left) == -1 || |
| 10049 | PyUnicode_READY(right) == -1) |
| 10050 | return -1; |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 10051 | return unicode_compare((PyUnicodeObject *)left, |
| 10052 | (PyUnicodeObject *)right); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10053 | } |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 10054 | PyErr_Format(PyExc_TypeError, |
| 10055 | "Can't compare %.100s and %.100s", |
| 10056 | left->ob_type->tp_name, |
| 10057 | right->ob_type->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10058 | return -1; |
| 10059 | } |
| 10060 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10061 | int |
| 10062 | PyUnicode_CompareWithASCIIString(PyObject* uni, const char* str) |
| 10063 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10064 | Py_ssize_t i; |
| 10065 | int kind; |
| 10066 | void *data; |
| 10067 | Py_UCS4 chr; |
| 10068 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 10069 | assert(_PyUnicode_CHECK(uni)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10070 | if (PyUnicode_READY(uni) == -1) |
| 10071 | return -1; |
| 10072 | kind = PyUnicode_KIND(uni); |
| 10073 | data = PyUnicode_DATA(uni); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10074 | /* Compare Unicode string and source character set string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10075 | for (i = 0; (chr = PyUnicode_READ(kind, data, i)) && str[i]; i++) |
| 10076 | if (chr != str[i]) |
| 10077 | return (chr < (unsigned char)(str[i])) ? -1 : 1; |
Benjamin Peterson | 8667a9b | 2010-01-09 21:45:28 +0000 | [diff] [blame] | 10078 | /* This check keeps Python strings that end in '\0' from comparing equal |
| 10079 | to C strings identical up to that point. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10080 | if (PyUnicode_GET_LENGTH(uni) != i || chr) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10081 | return 1; /* uni is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10082 | if (str[i]) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10083 | return -1; /* str is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10084 | return 0; |
| 10085 | } |
| 10086 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10087 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10088 | #define TEST_COND(cond) \ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10089 | ((cond) ? Py_True : Py_False) |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10090 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10091 | PyObject * |
| 10092 | PyUnicode_RichCompare(PyObject *left, PyObject *right, int op) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10093 | { |
| 10094 | int result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10095 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10096 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 10097 | PyObject *v; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10098 | if (PyUnicode_READY(left) == -1 || |
| 10099 | PyUnicode_READY(right) == -1) |
| 10100 | return NULL; |
| 10101 | if (PyUnicode_GET_LENGTH(left) != PyUnicode_GET_LENGTH(right) || |
| 10102 | PyUnicode_KIND(left) != PyUnicode_KIND(right)) { |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10103 | if (op == Py_EQ) { |
| 10104 | Py_INCREF(Py_False); |
| 10105 | return Py_False; |
| 10106 | } |
| 10107 | if (op == Py_NE) { |
| 10108 | Py_INCREF(Py_True); |
| 10109 | return Py_True; |
| 10110 | } |
| 10111 | } |
| 10112 | if (left == right) |
| 10113 | result = 0; |
| 10114 | else |
| 10115 | result = unicode_compare((PyUnicodeObject *)left, |
| 10116 | (PyUnicodeObject *)right); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10117 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10118 | /* Convert the return value to a Boolean */ |
| 10119 | switch (op) { |
| 10120 | case Py_EQ: |
| 10121 | v = TEST_COND(result == 0); |
| 10122 | break; |
| 10123 | case Py_NE: |
| 10124 | v = TEST_COND(result != 0); |
| 10125 | break; |
| 10126 | case Py_LE: |
| 10127 | v = TEST_COND(result <= 0); |
| 10128 | break; |
| 10129 | case Py_GE: |
| 10130 | v = TEST_COND(result >= 0); |
| 10131 | break; |
| 10132 | case Py_LT: |
| 10133 | v = TEST_COND(result == -1); |
| 10134 | break; |
| 10135 | case Py_GT: |
| 10136 | v = TEST_COND(result == 1); |
| 10137 | break; |
| 10138 | default: |
| 10139 | PyErr_BadArgument(); |
| 10140 | return NULL; |
| 10141 | } |
| 10142 | Py_INCREF(v); |
| 10143 | return v; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10144 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10145 | |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 10146 | Py_RETURN_NOTIMPLEMENTED; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10147 | } |
| 10148 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10149 | int |
| 10150 | PyUnicode_Contains(PyObject *container, PyObject *element) |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10151 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10152 | PyObject *str, *sub; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10153 | int kind1, kind2, kind; |
| 10154 | void *buf1, *buf2; |
| 10155 | Py_ssize_t len1, len2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10156 | int result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10157 | |
| 10158 | /* Coerce the two arguments */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10159 | sub = PyUnicode_FromObject(element); |
| 10160 | if (!sub) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10161 | PyErr_Format(PyExc_TypeError, |
| 10162 | "'in <string>' requires string as left operand, not %s", |
| 10163 | element->ob_type->tp_name); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10164 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10165 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10166 | if (PyUnicode_READY(sub) == -1) |
| 10167 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10168 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10169 | str = PyUnicode_FromObject(container); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 10170 | if (!str || PyUnicode_READY(str) == -1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10171 | Py_DECREF(sub); |
| 10172 | return -1; |
| 10173 | } |
| 10174 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10175 | kind1 = PyUnicode_KIND(str); |
| 10176 | kind2 = PyUnicode_KIND(sub); |
| 10177 | kind = kind1 > kind2 ? kind1 : kind2; |
| 10178 | buf1 = PyUnicode_DATA(str); |
| 10179 | buf2 = PyUnicode_DATA(sub); |
| 10180 | if (kind1 != kind) |
| 10181 | buf1 = _PyUnicode_AsKind((PyObject*)str, kind); |
| 10182 | if (!buf1) { |
| 10183 | Py_DECREF(sub); |
| 10184 | return -1; |
| 10185 | } |
| 10186 | if (kind2 != kind) |
| 10187 | buf2 = _PyUnicode_AsKind((PyObject*)sub, kind); |
| 10188 | if (!buf2) { |
| 10189 | Py_DECREF(sub); |
| 10190 | if (kind1 != kind) PyMem_Free(buf1); |
| 10191 | return -1; |
| 10192 | } |
| 10193 | len1 = PyUnicode_GET_LENGTH(str); |
| 10194 | len2 = PyUnicode_GET_LENGTH(sub); |
| 10195 | |
| 10196 | switch(kind) { |
| 10197 | case PyUnicode_1BYTE_KIND: |
| 10198 | result = ucs1lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10199 | break; |
| 10200 | case PyUnicode_2BYTE_KIND: |
| 10201 | result = ucs2lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10202 | break; |
| 10203 | case PyUnicode_4BYTE_KIND: |
| 10204 | result = ucs4lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10205 | break; |
| 10206 | default: |
| 10207 | result = -1; |
| 10208 | assert(0); |
| 10209 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10210 | |
| 10211 | Py_DECREF(str); |
| 10212 | Py_DECREF(sub); |
| 10213 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10214 | if (kind1 != kind) |
| 10215 | PyMem_Free(buf1); |
| 10216 | if (kind2 != kind) |
| 10217 | PyMem_Free(buf2); |
| 10218 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10219 | return result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10220 | } |
| 10221 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10222 | /* Concat to string or Unicode object giving a new Unicode object. */ |
| 10223 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10224 | PyObject * |
| 10225 | PyUnicode_Concat(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10226 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10227 | PyObject *u = NULL, *v = NULL, *w; |
| 10228 | Py_UCS4 maxchar; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10229 | |
| 10230 | /* Coerce the two arguments */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10231 | u = PyUnicode_FromObject(left); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10232 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10233 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10234 | v = PyUnicode_FromObject(right); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10235 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10236 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10237 | |
| 10238 | /* Shortcuts */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 10239 | if (v == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10240 | Py_DECREF(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10241 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10242 | } |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 10243 | if (u == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10244 | Py_DECREF(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10245 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10246 | } |
| 10247 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10248 | maxchar = PyUnicode_MAX_CHAR_VALUE(u); |
Victor Stinner | ff9e50f | 2011-09-28 22:17:19 +0200 | [diff] [blame] | 10249 | maxchar = Py_MAX(maxchar, PyUnicode_MAX_CHAR_VALUE(v)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10250 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10251 | /* Concat the two Unicode strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10252 | w = PyUnicode_New( |
| 10253 | PyUnicode_GET_LENGTH(u) + PyUnicode_GET_LENGTH(v), |
| 10254 | maxchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10255 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10256 | goto onError; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 10257 | copy_characters(w, 0, u, 0, PyUnicode_GET_LENGTH(u)); |
| 10258 | 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] | 10259 | Py_DECREF(u); |
| 10260 | Py_DECREF(v); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10261 | assert(_PyUnicode_CheckConsistency(w, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10262 | return w; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10263 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10264 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10265 | Py_XDECREF(u); |
| 10266 | Py_XDECREF(v); |
| 10267 | return NULL; |
| 10268 | } |
| 10269 | |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10270 | static void |
| 10271 | unicode_append_inplace(PyObject **p_left, PyObject *right) |
| 10272 | { |
| 10273 | Py_ssize_t left_len, right_len, new_len; |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10274 | |
| 10275 | assert(PyUnicode_IS_READY(*p_left)); |
| 10276 | assert(PyUnicode_IS_READY(right)); |
| 10277 | |
| 10278 | left_len = PyUnicode_GET_LENGTH(*p_left); |
| 10279 | right_len = PyUnicode_GET_LENGTH(right); |
| 10280 | if (left_len > PY_SSIZE_T_MAX - right_len) { |
| 10281 | PyErr_SetString(PyExc_OverflowError, |
| 10282 | "strings are too large to concat"); |
| 10283 | goto error; |
| 10284 | } |
| 10285 | new_len = left_len + right_len; |
| 10286 | |
| 10287 | /* Now we own the last reference to 'left', so we can resize it |
| 10288 | * in-place. |
| 10289 | */ |
| 10290 | if (unicode_resize(p_left, new_len) != 0) { |
| 10291 | /* XXX if _PyUnicode_Resize() fails, 'left' has been |
| 10292 | * deallocated so it cannot be put back into |
| 10293 | * 'variable'. The MemoryError is raised when there |
| 10294 | * is no value in 'variable', which might (very |
| 10295 | * remotely) be a cause of incompatibilities. |
| 10296 | */ |
| 10297 | goto error; |
| 10298 | } |
| 10299 | /* copy 'right' into the newly allocated area of 'left' */ |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 10300 | copy_characters(*p_left, left_len, right, 0, right_len); |
| 10301 | _PyUnicode_DIRTY(*p_left); |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10302 | return; |
| 10303 | |
| 10304 | error: |
| 10305 | Py_DECREF(*p_left); |
| 10306 | *p_left = NULL; |
| 10307 | } |
| 10308 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10309 | void |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10310 | PyUnicode_Append(PyObject **p_left, PyObject *right) |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10311 | { |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10312 | PyObject *left, *res; |
| 10313 | |
| 10314 | if (p_left == NULL) { |
| 10315 | if (!PyErr_Occurred()) |
| 10316 | PyErr_BadInternalCall(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10317 | return; |
| 10318 | } |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10319 | left = *p_left; |
| 10320 | if (right == NULL || !PyUnicode_Check(left)) { |
| 10321 | if (!PyErr_Occurred()) |
| 10322 | PyErr_BadInternalCall(); |
| 10323 | goto error; |
| 10324 | } |
| 10325 | |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 10326 | if (PyUnicode_READY(left)) |
| 10327 | goto error; |
| 10328 | if (PyUnicode_READY(right)) |
| 10329 | goto error; |
| 10330 | |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10331 | if (PyUnicode_CheckExact(left) && left != unicode_empty |
| 10332 | && PyUnicode_CheckExact(right) && right != unicode_empty |
| 10333 | && unicode_resizable(left) |
| 10334 | && (_PyUnicode_KIND(right) <= _PyUnicode_KIND(left) |
| 10335 | || _PyUnicode_WSTR(left) != NULL)) |
| 10336 | { |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10337 | /* Don't resize for ascii += latin1. Convert ascii to latin1 requires |
| 10338 | to change the structure size, but characters are stored just after |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 10339 | the structure, and so it requires to move all characters which is |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10340 | not so different than duplicating the string. */ |
| 10341 | if (!(PyUnicode_IS_ASCII(left) && !PyUnicode_IS_ASCII(right))) |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10342 | { |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10343 | unicode_append_inplace(p_left, right); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10344 | if (p_left != NULL) |
| 10345 | assert(_PyUnicode_CheckConsistency(*p_left, 1)); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10346 | return; |
| 10347 | } |
| 10348 | } |
| 10349 | |
| 10350 | res = PyUnicode_Concat(left, right); |
| 10351 | if (res == NULL) |
| 10352 | goto error; |
| 10353 | Py_DECREF(left); |
| 10354 | *p_left = res; |
| 10355 | return; |
| 10356 | |
| 10357 | error: |
| 10358 | Py_DECREF(*p_left); |
| 10359 | *p_left = NULL; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10360 | } |
| 10361 | |
| 10362 | void |
| 10363 | PyUnicode_AppendAndDel(PyObject **pleft, PyObject *right) |
| 10364 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10365 | PyUnicode_Append(pleft, right); |
| 10366 | Py_XDECREF(right); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10367 | } |
| 10368 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10369 | PyDoc_STRVAR(count__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10370 | "S.count(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10371 | \n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10372 | Return the number of non-overlapping occurrences of substring sub in\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 10373 | 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] | 10374 | interpreted as in slice notation."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10375 | |
| 10376 | static PyObject * |
| 10377 | unicode_count(PyUnicodeObject *self, PyObject *args) |
| 10378 | { |
| 10379 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10380 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10381 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10382 | PyObject *result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10383 | int kind1, kind2, kind; |
| 10384 | void *buf1, *buf2; |
| 10385 | Py_ssize_t len1, len2, iresult; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10386 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10387 | if (!stringlib_parse_args_finds_unicode("count", args, &substring, |
| 10388 | &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10389 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 10390 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10391 | kind1 = PyUnicode_KIND(self); |
| 10392 | kind2 = PyUnicode_KIND(substring); |
| 10393 | kind = kind1 > kind2 ? kind1 : kind2; |
| 10394 | buf1 = PyUnicode_DATA(self); |
| 10395 | buf2 = PyUnicode_DATA(substring); |
| 10396 | if (kind1 != kind) |
| 10397 | buf1 = _PyUnicode_AsKind((PyObject*)self, kind); |
| 10398 | if (!buf1) { |
| 10399 | Py_DECREF(substring); |
| 10400 | return NULL; |
| 10401 | } |
| 10402 | if (kind2 != kind) |
| 10403 | buf2 = _PyUnicode_AsKind((PyObject*)substring, kind); |
| 10404 | if (!buf2) { |
| 10405 | Py_DECREF(substring); |
| 10406 | if (kind1 != kind) PyMem_Free(buf1); |
| 10407 | return NULL; |
| 10408 | } |
| 10409 | len1 = PyUnicode_GET_LENGTH(self); |
| 10410 | len2 = PyUnicode_GET_LENGTH(substring); |
| 10411 | |
| 10412 | ADJUST_INDICES(start, end, len1); |
| 10413 | switch(kind) { |
| 10414 | case PyUnicode_1BYTE_KIND: |
| 10415 | iresult = ucs1lib_count( |
| 10416 | ((Py_UCS1*)buf1) + start, end - start, |
| 10417 | buf2, len2, PY_SSIZE_T_MAX |
| 10418 | ); |
| 10419 | break; |
| 10420 | case PyUnicode_2BYTE_KIND: |
| 10421 | iresult = ucs2lib_count( |
| 10422 | ((Py_UCS2*)buf1) + start, end - start, |
| 10423 | buf2, len2, PY_SSIZE_T_MAX |
| 10424 | ); |
| 10425 | break; |
| 10426 | case PyUnicode_4BYTE_KIND: |
| 10427 | iresult = ucs4lib_count( |
| 10428 | ((Py_UCS4*)buf1) + start, end - start, |
| 10429 | buf2, len2, PY_SSIZE_T_MAX |
| 10430 | ); |
| 10431 | break; |
| 10432 | default: |
| 10433 | assert(0); iresult = 0; |
| 10434 | } |
| 10435 | |
| 10436 | result = PyLong_FromSsize_t(iresult); |
| 10437 | |
| 10438 | if (kind1 != kind) |
| 10439 | PyMem_Free(buf1); |
| 10440 | if (kind2 != kind) |
| 10441 | PyMem_Free(buf2); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10442 | |
| 10443 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10444 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10445 | return result; |
| 10446 | } |
| 10447 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10448 | PyDoc_STRVAR(encode__doc__, |
Victor Stinner | c911bbf | 2010-11-07 19:04:46 +0000 | [diff] [blame] | 10449 | "S.encode(encoding='utf-8', errors='strict') -> bytes\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10450 | \n\ |
Victor Stinner | e14e212 | 2010-11-07 18:41:46 +0000 | [diff] [blame] | 10451 | Encode S using the codec registered for encoding. Default encoding\n\ |
| 10452 | 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] | 10453 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 10454 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 10455 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 10456 | codecs.register_error that can handle UnicodeEncodeErrors."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10457 | |
| 10458 | static PyObject * |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 10459 | unicode_encode(PyUnicodeObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10460 | { |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 10461 | static char *kwlist[] = {"encoding", "errors", 0}; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10462 | char *encoding = NULL; |
| 10463 | char *errors = NULL; |
Guido van Rossum | 35d9428 | 2007-08-27 18:20:11 +0000 | [diff] [blame] | 10464 | |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 10465 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:encode", |
| 10466 | kwlist, &encoding, &errors)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10467 | return NULL; |
Georg Brandl | 3b9406b | 2010-12-03 07:54:09 +0000 | [diff] [blame] | 10468 | return PyUnicode_AsEncodedString((PyObject *)self, encoding, errors); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 10469 | } |
| 10470 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10471 | PyDoc_STRVAR(expandtabs__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10472 | "S.expandtabs([tabsize]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10473 | \n\ |
| 10474 | 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] | 10475 | 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] | 10476 | |
| 10477 | static PyObject* |
| 10478 | unicode_expandtabs(PyUnicodeObject *self, PyObject *args) |
| 10479 | { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10480 | Py_ssize_t i, j, line_pos, src_len, incr; |
| 10481 | Py_UCS4 ch; |
| 10482 | PyObject *u; |
| 10483 | void *src_data, *dest_data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10484 | int tabsize = 8; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10485 | int kind; |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 10486 | int found; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10487 | |
| 10488 | if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10489 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10490 | |
Antoine Pitrou | 2242522 | 2011-10-04 19:10:51 +0200 | [diff] [blame] | 10491 | if (PyUnicode_READY(self) == -1) |
| 10492 | return NULL; |
| 10493 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 10494 | /* First pass: determine size of output string */ |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10495 | src_len = PyUnicode_GET_LENGTH(self); |
| 10496 | i = j = line_pos = 0; |
| 10497 | kind = PyUnicode_KIND(self); |
| 10498 | src_data = PyUnicode_DATA(self); |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 10499 | found = 0; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10500 | for (; i < src_len; i++) { |
| 10501 | ch = PyUnicode_READ(kind, src_data, i); |
| 10502 | if (ch == '\t') { |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 10503 | found = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10504 | if (tabsize > 0) { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10505 | incr = tabsize - (line_pos % tabsize); /* cannot overflow */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10506 | if (j > PY_SSIZE_T_MAX - incr) |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10507 | goto overflow; |
| 10508 | line_pos += incr; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10509 | j += incr; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10510 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10511 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10512 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10513 | if (j > PY_SSIZE_T_MAX - 1) |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10514 | goto overflow; |
| 10515 | line_pos++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10516 | j++; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10517 | if (ch == '\n' || ch == '\r') |
| 10518 | line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10519 | } |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10520 | } |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 10521 | if (!found && PyUnicode_CheckExact(self)) { |
| 10522 | Py_INCREF((PyObject *) self); |
| 10523 | return (PyObject *) self; |
| 10524 | } |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 10525 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10526 | /* Second pass: create output string and fill it */ |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10527 | u = PyUnicode_New(j, PyUnicode_MAX_CHAR_VALUE(self)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10528 | if (!u) |
| 10529 | return NULL; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10530 | dest_data = PyUnicode_DATA(u); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10531 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10532 | i = j = line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10533 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10534 | for (; i < src_len; i++) { |
| 10535 | ch = PyUnicode_READ(kind, src_data, i); |
| 10536 | if (ch == '\t') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10537 | if (tabsize > 0) { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10538 | incr = tabsize - (line_pos % tabsize); |
| 10539 | line_pos += incr; |
| 10540 | while (incr--) { |
| 10541 | PyUnicode_WRITE(kind, dest_data, j, ' '); |
| 10542 | j++; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10543 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10544 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10545 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10546 | else { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10547 | line_pos++; |
| 10548 | PyUnicode_WRITE(kind, dest_data, j, ch); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10549 | j++; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10550 | if (ch == '\n' || ch == '\r') |
| 10551 | line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10552 | } |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10553 | } |
| 10554 | assert (j == PyUnicode_GET_LENGTH(u)); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 10555 | #ifndef DONT_MAKE_RESULT_READY |
| 10556 | if (_PyUnicode_READY_REPLACE(&u)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10557 | Py_DECREF(u); |
| 10558 | return NULL; |
| 10559 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 10560 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10561 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10562 | return (PyObject*) u; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10563 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10564 | overflow: |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10565 | PyErr_SetString(PyExc_OverflowError, "new string is too long"); |
| 10566 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10567 | } |
| 10568 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10569 | PyDoc_STRVAR(find__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10570 | "S.find(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10571 | \n\ |
| 10572 | Return the lowest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 10573 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10574 | arguments start and end are interpreted as in slice notation.\n\ |
| 10575 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10576 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10577 | |
| 10578 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10579 | unicode_find(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10580 | { |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10581 | PyUnicodeObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 10582 | Py_ssize_t start; |
| 10583 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10584 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10585 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10586 | if (!stringlib_parse_args_finds_unicode("find", args, &substring, |
| 10587 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10588 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10589 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10590 | if (PyUnicode_READY(self) == -1) |
| 10591 | return NULL; |
| 10592 | if (PyUnicode_READY(substring) == -1) |
| 10593 | return NULL; |
| 10594 | |
| 10595 | result = any_find_slice( |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10596 | asciilib_find_slice, ucs1lib_find_slice, |
| 10597 | ucs2lib_find_slice, ucs4lib_find_slice, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10598 | self, (PyObject*)substring, start, end |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10599 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10600 | |
| 10601 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10602 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10603 | if (result == -2) |
| 10604 | return NULL; |
| 10605 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 10606 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10607 | } |
| 10608 | |
| 10609 | static PyObject * |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 10610 | unicode_getitem(PyObject *self, Py_ssize_t index) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10611 | { |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 10612 | Py_UCS4 ch = PyUnicode_ReadChar(self, index); |
| 10613 | if (ch == (Py_UCS4)-1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10614 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10615 | return PyUnicode_FromOrdinal(ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10616 | } |
| 10617 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 10618 | /* Believe it or not, this produces the same value for ASCII strings |
Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 10619 | as bytes_hash(). */ |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 10620 | static Py_hash_t |
Neil Schemenauer | f8c37d1 | 2007-09-07 20:49:04 +0000 | [diff] [blame] | 10621 | unicode_hash(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10622 | { |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 10623 | Py_ssize_t len; |
Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 10624 | Py_uhash_t x; |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 10625 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10626 | if (_PyUnicode_HASH(self) != -1) |
| 10627 | return _PyUnicode_HASH(self); |
| 10628 | if (PyUnicode_READY(self) == -1) |
| 10629 | return -1; |
| 10630 | len = PyUnicode_GET_LENGTH(self); |
| 10631 | |
| 10632 | /* The hash function as a macro, gets expanded three times below. */ |
| 10633 | #define HASH(P) \ |
| 10634 | x = (Py_uhash_t)*P << 7; \ |
| 10635 | while (--len >= 0) \ |
| 10636 | x = (1000003*x) ^ (Py_uhash_t)*P++; |
| 10637 | |
| 10638 | switch (PyUnicode_KIND(self)) { |
| 10639 | case PyUnicode_1BYTE_KIND: { |
| 10640 | const unsigned char *c = PyUnicode_1BYTE_DATA(self); |
| 10641 | HASH(c); |
| 10642 | break; |
| 10643 | } |
| 10644 | case PyUnicode_2BYTE_KIND: { |
| 10645 | const Py_UCS2 *s = PyUnicode_2BYTE_DATA(self); |
| 10646 | HASH(s); |
| 10647 | break; |
| 10648 | } |
| 10649 | default: { |
| 10650 | Py_UCS4 *l; |
| 10651 | assert(PyUnicode_KIND(self) == PyUnicode_4BYTE_KIND && |
| 10652 | "Impossible switch case in unicode_hash"); |
| 10653 | l = PyUnicode_4BYTE_DATA(self); |
| 10654 | HASH(l); |
| 10655 | break; |
| 10656 | } |
| 10657 | } |
| 10658 | x ^= (Py_uhash_t)PyUnicode_GET_LENGTH(self); |
| 10659 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 10660 | if (x == -1) |
| 10661 | x = -2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10662 | _PyUnicode_HASH(self) = x; |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 10663 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10664 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10665 | #undef HASH |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10666 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10667 | PyDoc_STRVAR(index__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10668 | "S.index(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10669 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10670 | 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] | 10671 | |
| 10672 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10673 | unicode_index(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10674 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10675 | Py_ssize_t result; |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10676 | PyUnicodeObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 10677 | Py_ssize_t start; |
| 10678 | Py_ssize_t end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10679 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10680 | if (!stringlib_parse_args_finds_unicode("index", args, &substring, |
| 10681 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10682 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10683 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10684 | if (PyUnicode_READY(self) == -1) |
| 10685 | return NULL; |
| 10686 | if (PyUnicode_READY(substring) == -1) |
| 10687 | return NULL; |
| 10688 | |
| 10689 | result = any_find_slice( |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10690 | asciilib_find_slice, ucs1lib_find_slice, |
| 10691 | ucs2lib_find_slice, ucs4lib_find_slice, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10692 | self, (PyObject*)substring, start, end |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10693 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10694 | |
| 10695 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10696 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10697 | if (result == -2) |
| 10698 | return NULL; |
| 10699 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10700 | if (result < 0) { |
| 10701 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 10702 | return NULL; |
| 10703 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10704 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 10705 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10706 | } |
| 10707 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10708 | PyDoc_STRVAR(islower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10709 | "S.islower() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10710 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10711 | 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] | 10712 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10713 | |
| 10714 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10715 | unicode_islower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10716 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10717 | Py_ssize_t i, length; |
| 10718 | int kind; |
| 10719 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10720 | int cased; |
| 10721 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10722 | if (PyUnicode_READY(self) == -1) |
| 10723 | return NULL; |
| 10724 | length = PyUnicode_GET_LENGTH(self); |
| 10725 | kind = PyUnicode_KIND(self); |
| 10726 | data = PyUnicode_DATA(self); |
| 10727 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10728 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10729 | if (length == 1) |
| 10730 | return PyBool_FromLong( |
| 10731 | Py_UNICODE_ISLOWER(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10732 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10733 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10734 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10735 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10736 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10737 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10738 | for (i = 0; i < length; i++) { |
| 10739 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 10740 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10741 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 10742 | return PyBool_FromLong(0); |
| 10743 | else if (!cased && Py_UNICODE_ISLOWER(ch)) |
| 10744 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10745 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10746 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10747 | } |
| 10748 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10749 | PyDoc_STRVAR(isupper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10750 | "S.isupper() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10751 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 10752 | 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] | 10753 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10754 | |
| 10755 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10756 | unicode_isupper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10757 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10758 | Py_ssize_t i, length; |
| 10759 | int kind; |
| 10760 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10761 | int cased; |
| 10762 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10763 | if (PyUnicode_READY(self) == -1) |
| 10764 | return NULL; |
| 10765 | length = PyUnicode_GET_LENGTH(self); |
| 10766 | kind = PyUnicode_KIND(self); |
| 10767 | data = PyUnicode_DATA(self); |
| 10768 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10769 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10770 | if (length == 1) |
| 10771 | return PyBool_FromLong( |
| 10772 | Py_UNICODE_ISUPPER(PyUnicode_READ(kind, data, 0)) != 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10773 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10774 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10775 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10776 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10777 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10778 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10779 | for (i = 0; i < length; i++) { |
| 10780 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 10781 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10782 | if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 10783 | return PyBool_FromLong(0); |
| 10784 | else if (!cased && Py_UNICODE_ISUPPER(ch)) |
| 10785 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10786 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10787 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10788 | } |
| 10789 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10790 | PyDoc_STRVAR(istitle__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10791 | "S.istitle() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10792 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 10793 | Return True if S is a titlecased string and there is at least one\n\ |
| 10794 | character in S, i.e. upper- and titlecase characters may only\n\ |
| 10795 | follow uncased characters and lowercase characters only cased ones.\n\ |
| 10796 | Return False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10797 | |
| 10798 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10799 | unicode_istitle(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10800 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10801 | Py_ssize_t i, length; |
| 10802 | int kind; |
| 10803 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10804 | int cased, previous_is_cased; |
| 10805 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10806 | if (PyUnicode_READY(self) == -1) |
| 10807 | return NULL; |
| 10808 | length = PyUnicode_GET_LENGTH(self); |
| 10809 | kind = PyUnicode_KIND(self); |
| 10810 | data = PyUnicode_DATA(self); |
| 10811 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10812 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10813 | if (length == 1) { |
| 10814 | Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 10815 | return PyBool_FromLong((Py_UNICODE_ISTITLE(ch) != 0) || |
| 10816 | (Py_UNICODE_ISUPPER(ch) != 0)); |
| 10817 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10818 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10819 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10820 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10821 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10822 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10823 | cased = 0; |
| 10824 | previous_is_cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10825 | for (i = 0; i < length; i++) { |
| 10826 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 10827 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10828 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { |
| 10829 | if (previous_is_cased) |
| 10830 | return PyBool_FromLong(0); |
| 10831 | previous_is_cased = 1; |
| 10832 | cased = 1; |
| 10833 | } |
| 10834 | else if (Py_UNICODE_ISLOWER(ch)) { |
| 10835 | if (!previous_is_cased) |
| 10836 | return PyBool_FromLong(0); |
| 10837 | previous_is_cased = 1; |
| 10838 | cased = 1; |
| 10839 | } |
| 10840 | else |
| 10841 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10842 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10843 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10844 | } |
| 10845 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10846 | PyDoc_STRVAR(isspace__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10847 | "S.isspace() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10848 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 10849 | Return True if all characters in S are whitespace\n\ |
| 10850 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10851 | |
| 10852 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10853 | unicode_isspace(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10854 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10855 | Py_ssize_t i, length; |
| 10856 | int kind; |
| 10857 | void *data; |
| 10858 | |
| 10859 | if (PyUnicode_READY(self) == -1) |
| 10860 | return NULL; |
| 10861 | length = PyUnicode_GET_LENGTH(self); |
| 10862 | kind = PyUnicode_KIND(self); |
| 10863 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10864 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10865 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10866 | if (length == 1) |
| 10867 | return PyBool_FromLong( |
| 10868 | Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10869 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10870 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10871 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10872 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10873 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10874 | for (i = 0; i < length; i++) { |
| 10875 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 10876 | if (!Py_UNICODE_ISSPACE(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10877 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10878 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10879 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10880 | } |
| 10881 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10882 | PyDoc_STRVAR(isalpha__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10883 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10884 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 10885 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10886 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10887 | |
| 10888 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10889 | unicode_isalpha(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10890 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10891 | Py_ssize_t i, length; |
| 10892 | int kind; |
| 10893 | void *data; |
| 10894 | |
| 10895 | if (PyUnicode_READY(self) == -1) |
| 10896 | return NULL; |
| 10897 | length = PyUnicode_GET_LENGTH(self); |
| 10898 | kind = PyUnicode_KIND(self); |
| 10899 | data = PyUnicode_DATA(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10900 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10901 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10902 | if (length == 1) |
| 10903 | return PyBool_FromLong( |
| 10904 | Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, 0))); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10905 | |
| 10906 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10907 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10908 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10909 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10910 | for (i = 0; i < length; i++) { |
| 10911 | if (!Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10912 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10913 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10914 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10915 | } |
| 10916 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10917 | PyDoc_STRVAR(isalnum__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10918 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10919 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 10920 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10921 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10922 | |
| 10923 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10924 | unicode_isalnum(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10925 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10926 | int kind; |
| 10927 | void *data; |
| 10928 | Py_ssize_t len, i; |
| 10929 | |
| 10930 | if (PyUnicode_READY(self) == -1) |
| 10931 | return NULL; |
| 10932 | |
| 10933 | kind = PyUnicode_KIND(self); |
| 10934 | data = PyUnicode_DATA(self); |
| 10935 | len = PyUnicode_GET_LENGTH(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10936 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10937 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10938 | if (len == 1) { |
| 10939 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 10940 | return PyBool_FromLong(Py_UNICODE_ISALNUM(ch)); |
| 10941 | } |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10942 | |
| 10943 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10944 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10945 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10946 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10947 | for (i = 0; i < len; i++) { |
| 10948 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 10949 | if (!Py_UNICODE_ISALNUM(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10950 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10951 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10952 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10953 | } |
| 10954 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10955 | PyDoc_STRVAR(isdecimal__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10956 | "S.isdecimal() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10957 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10958 | 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] | 10959 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10960 | |
| 10961 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10962 | unicode_isdecimal(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10963 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10964 | Py_ssize_t i, length; |
| 10965 | int kind; |
| 10966 | void *data; |
| 10967 | |
| 10968 | if (PyUnicode_READY(self) == -1) |
| 10969 | return NULL; |
| 10970 | length = PyUnicode_GET_LENGTH(self); |
| 10971 | kind = PyUnicode_KIND(self); |
| 10972 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10973 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10974 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10975 | if (length == 1) |
| 10976 | return PyBool_FromLong( |
| 10977 | Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10978 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10979 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10980 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10981 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10982 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10983 | for (i = 0; i < length; i++) { |
| 10984 | if (!Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10985 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10986 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10987 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10988 | } |
| 10989 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10990 | PyDoc_STRVAR(isdigit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10991 | "S.isdigit() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10992 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 10993 | Return True if all characters in S are digits\n\ |
| 10994 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10995 | |
| 10996 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10997 | unicode_isdigit(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10998 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10999 | Py_ssize_t i, length; |
| 11000 | int kind; |
| 11001 | void *data; |
| 11002 | |
| 11003 | if (PyUnicode_READY(self) == -1) |
| 11004 | return NULL; |
| 11005 | length = PyUnicode_GET_LENGTH(self); |
| 11006 | kind = PyUnicode_KIND(self); |
| 11007 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11008 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11009 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11010 | if (length == 1) { |
| 11011 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11012 | return PyBool_FromLong(Py_UNICODE_ISDIGIT(ch)); |
| 11013 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11014 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11015 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11016 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11017 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11018 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11019 | for (i = 0; i < length; i++) { |
| 11020 | if (!Py_UNICODE_ISDIGIT(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11021 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11022 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11023 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11024 | } |
| 11025 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11026 | PyDoc_STRVAR(isnumeric__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11027 | "S.isnumeric() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11028 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11029 | 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] | 11030 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11031 | |
| 11032 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11033 | unicode_isnumeric(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11034 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11035 | Py_ssize_t i, length; |
| 11036 | int kind; |
| 11037 | void *data; |
| 11038 | |
| 11039 | if (PyUnicode_READY(self) == -1) |
| 11040 | return NULL; |
| 11041 | length = PyUnicode_GET_LENGTH(self); |
| 11042 | kind = PyUnicode_KIND(self); |
| 11043 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11044 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11045 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11046 | if (length == 1) |
| 11047 | return PyBool_FromLong( |
| 11048 | Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11049 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11050 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11051 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11052 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11053 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11054 | for (i = 0; i < length; i++) { |
| 11055 | if (!Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11056 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11057 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11058 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11059 | } |
| 11060 | |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11061 | int |
| 11062 | PyUnicode_IsIdentifier(PyObject *self) |
| 11063 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11064 | int kind; |
| 11065 | void *data; |
| 11066 | Py_ssize_t i; |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11067 | Py_UCS4 first; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11068 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11069 | if (PyUnicode_READY(self) == -1) { |
| 11070 | Py_FatalError("identifier not ready"); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11071 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11072 | } |
| 11073 | |
| 11074 | /* Special case for empty strings */ |
| 11075 | if (PyUnicode_GET_LENGTH(self) == 0) |
| 11076 | return 0; |
| 11077 | kind = PyUnicode_KIND(self); |
| 11078 | data = PyUnicode_DATA(self); |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11079 | |
| 11080 | /* PEP 3131 says that the first character must be in |
| 11081 | XID_Start and subsequent characters in XID_Continue, |
| 11082 | and for the ASCII range, the 2.x rules apply (i.e |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11083 | start with letters and underscore, continue with |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11084 | letters, digits, underscore). However, given the current |
| 11085 | definition of XID_Start and XID_Continue, it is sufficient |
| 11086 | to check just for these, except that _ must be allowed |
| 11087 | as starting an identifier. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11088 | first = PyUnicode_READ(kind, data, 0); |
Benjamin Peterson | f413b80 | 2011-08-12 22:17:18 -0500 | [diff] [blame] | 11089 | if (!_PyUnicode_IsXidStart(first) && first != 0x5F /* LOW LINE */) |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11090 | return 0; |
| 11091 | |
Benjamin Peterson | 9c6e6a0 | 2011-09-28 08:09:05 -0400 | [diff] [blame] | 11092 | for (i = 1; i < PyUnicode_GET_LENGTH(self); i++) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11093 | if (!_PyUnicode_IsXidContinue(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11094 | return 0; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11095 | return 1; |
| 11096 | } |
| 11097 | |
| 11098 | PyDoc_STRVAR(isidentifier__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11099 | "S.isidentifier() -> bool\n\ |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11100 | \n\ |
| 11101 | Return True if S is a valid identifier according\n\ |
| 11102 | to the language definition."); |
| 11103 | |
| 11104 | static PyObject* |
| 11105 | unicode_isidentifier(PyObject *self) |
| 11106 | { |
| 11107 | return PyBool_FromLong(PyUnicode_IsIdentifier(self)); |
| 11108 | } |
| 11109 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11110 | PyDoc_STRVAR(isprintable__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11111 | "S.isprintable() -> bool\n\ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11112 | \n\ |
| 11113 | Return True if all characters in S are considered\n\ |
| 11114 | printable in repr() or S is empty, False otherwise."); |
| 11115 | |
| 11116 | static PyObject* |
| 11117 | unicode_isprintable(PyObject *self) |
| 11118 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11119 | Py_ssize_t i, length; |
| 11120 | int kind; |
| 11121 | void *data; |
| 11122 | |
| 11123 | if (PyUnicode_READY(self) == -1) |
| 11124 | return NULL; |
| 11125 | length = PyUnicode_GET_LENGTH(self); |
| 11126 | kind = PyUnicode_KIND(self); |
| 11127 | data = PyUnicode_DATA(self); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11128 | |
| 11129 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11130 | if (length == 1) |
| 11131 | return PyBool_FromLong( |
| 11132 | Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, 0))); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11133 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11134 | for (i = 0; i < length; i++) { |
| 11135 | if (!Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, i))) { |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11136 | Py_RETURN_FALSE; |
| 11137 | } |
| 11138 | } |
| 11139 | Py_RETURN_TRUE; |
| 11140 | } |
| 11141 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11142 | PyDoc_STRVAR(join__doc__, |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 11143 | "S.join(iterable) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11144 | \n\ |
| 11145 | 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] | 11146 | iterable. The separator between elements is S."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11147 | |
| 11148 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11149 | unicode_join(PyObject *self, PyObject *data) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11150 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11151 | return PyUnicode_Join(self, data); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11152 | } |
| 11153 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11154 | static Py_ssize_t |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11155 | unicode_length(PyUnicodeObject *self) |
| 11156 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11157 | if (PyUnicode_READY(self) == -1) |
| 11158 | return -1; |
| 11159 | return PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11160 | } |
| 11161 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11162 | PyDoc_STRVAR(ljust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11163 | "S.ljust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11164 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 11165 | 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] | 11166 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11167 | |
| 11168 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11169 | unicode_ljust(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11170 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11171 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11172 | Py_UCS4 fillchar = ' '; |
| 11173 | |
| 11174 | if (PyUnicode_READY(self) == -1) |
| 11175 | return NULL; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 11176 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11177 | if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11178 | return NULL; |
| 11179 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11180 | if (_PyUnicode_LENGTH(self) >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11181 | Py_INCREF(self); |
| 11182 | return (PyObject*) self; |
| 11183 | } |
| 11184 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11185 | return (PyObject*) pad(self, 0, width - _PyUnicode_LENGTH(self), fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11186 | } |
| 11187 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11188 | PyDoc_STRVAR(lower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11189 | "S.lower() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11190 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11191 | Return a copy of the string S converted to lowercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11192 | |
| 11193 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11194 | unicode_lower(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11195 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11196 | return fixup(self, fixlower); |
| 11197 | } |
| 11198 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11199 | #define LEFTSTRIP 0 |
| 11200 | #define RIGHTSTRIP 1 |
| 11201 | #define BOTHSTRIP 2 |
| 11202 | |
| 11203 | /* Arrays indexed by above */ |
| 11204 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 11205 | |
| 11206 | #define STRIPNAME(i) (stripformat[i]+3) |
| 11207 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11208 | /* externally visible for str.strip(unicode) */ |
| 11209 | PyObject * |
| 11210 | _PyUnicode_XStrip(PyUnicodeObject *self, int striptype, PyObject *sepobj) |
| 11211 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11212 | void *data; |
| 11213 | int kind; |
| 11214 | Py_ssize_t i, j, len; |
| 11215 | BLOOM_MASK sepmask; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11216 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11217 | if (PyUnicode_READY(self) == -1 || PyUnicode_READY(sepobj) == -1) |
| 11218 | return NULL; |
| 11219 | |
| 11220 | kind = PyUnicode_KIND(self); |
| 11221 | data = PyUnicode_DATA(self); |
| 11222 | len = PyUnicode_GET_LENGTH(self); |
| 11223 | sepmask = make_bloom_mask(PyUnicode_KIND(sepobj), |
| 11224 | PyUnicode_DATA(sepobj), |
| 11225 | PyUnicode_GET_LENGTH(sepobj)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11226 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11227 | i = 0; |
| 11228 | if (striptype != RIGHTSTRIP) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11229 | while (i < len && |
| 11230 | BLOOM_MEMBER(sepmask, PyUnicode_READ(kind, data, i), sepobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11231 | i++; |
| 11232 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11233 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11234 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11235 | j = len; |
| 11236 | if (striptype != LEFTSTRIP) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11237 | do { |
| 11238 | j--; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11239 | } while (j >= i && |
| 11240 | BLOOM_MEMBER(sepmask, PyUnicode_READ(kind, data, j), sepobj)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11241 | j++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11242 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11243 | |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11244 | return PyUnicode_Substring((PyObject*)self, i, j); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11245 | } |
| 11246 | |
| 11247 | PyObject* |
| 11248 | PyUnicode_Substring(PyObject *self, Py_ssize_t start, Py_ssize_t end) |
| 11249 | { |
| 11250 | unsigned char *data; |
| 11251 | int kind; |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11252 | Py_ssize_t length; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11253 | |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 11254 | if (PyUnicode_READY(self) == -1) |
| 11255 | return NULL; |
| 11256 | |
| 11257 | end = Py_MIN(end, PyUnicode_GET_LENGTH(self)); |
| 11258 | |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11259 | if (start == 0 && end == PyUnicode_GET_LENGTH(self)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11260 | { |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11261 | if (PyUnicode_CheckExact(self)) { |
| 11262 | Py_INCREF(self); |
| 11263 | return self; |
| 11264 | } |
| 11265 | else |
| 11266 | return PyUnicode_Copy(self); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11267 | } |
| 11268 | |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11269 | length = end - start; |
| 11270 | if (length == 1) |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 11271 | return unicode_getitem(self, start); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11272 | |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 11273 | if (start < 0 || end < 0) { |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11274 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 11275 | return NULL; |
| 11276 | } |
| 11277 | |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 11278 | if (PyUnicode_IS_ASCII(self)) { |
| 11279 | kind = PyUnicode_KIND(self); |
| 11280 | data = PyUnicode_1BYTE_DATA(self); |
| 11281 | return unicode_fromascii(data + start, length); |
| 11282 | } |
| 11283 | else { |
| 11284 | kind = PyUnicode_KIND(self); |
| 11285 | data = PyUnicode_1BYTE_DATA(self); |
| 11286 | return PyUnicode_FromKindAndData(kind, |
| 11287 | data + PyUnicode_KIND_SIZE(kind, start), |
| 11288 | length); |
| 11289 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11290 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11291 | |
| 11292 | static PyObject * |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11293 | do_strip(PyUnicodeObject *self, int striptype) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11294 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11295 | int kind; |
| 11296 | void *data; |
| 11297 | Py_ssize_t len, i, j; |
| 11298 | |
| 11299 | if (PyUnicode_READY(self) == -1) |
| 11300 | return NULL; |
| 11301 | |
| 11302 | kind = PyUnicode_KIND(self); |
| 11303 | data = PyUnicode_DATA(self); |
| 11304 | len = PyUnicode_GET_LENGTH(self); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11305 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11306 | i = 0; |
| 11307 | if (striptype != RIGHTSTRIP) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11308 | while (i < len && Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, i))) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11309 | i++; |
| 11310 | } |
| 11311 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11312 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11313 | j = len; |
| 11314 | if (striptype != LEFTSTRIP) { |
| 11315 | do { |
| 11316 | j--; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11317 | } while (j >= i && Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, j))); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11318 | j++; |
| 11319 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11320 | |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11321 | return PyUnicode_Substring((PyObject*)self, i, j); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11322 | } |
| 11323 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11324 | |
| 11325 | static PyObject * |
| 11326 | do_argstrip(PyUnicodeObject *self, int striptype, PyObject *args) |
| 11327 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11328 | PyObject *sep = NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11329 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11330 | if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) |
| 11331 | return NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11332 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11333 | if (sep != NULL && sep != Py_None) { |
| 11334 | if (PyUnicode_Check(sep)) |
| 11335 | return _PyUnicode_XStrip(self, striptype, sep); |
| 11336 | else { |
| 11337 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11338 | "%s arg must be None or str", |
| 11339 | STRIPNAME(striptype)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11340 | return NULL; |
| 11341 | } |
| 11342 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11343 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11344 | return do_strip(self, striptype); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11345 | } |
| 11346 | |
| 11347 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11348 | PyDoc_STRVAR(strip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11349 | "S.strip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11350 | \n\ |
| 11351 | Return a copy of the string S with leading and trailing\n\ |
| 11352 | whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11353 | 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] | 11354 | |
| 11355 | static PyObject * |
| 11356 | unicode_strip(PyUnicodeObject *self, PyObject *args) |
| 11357 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11358 | if (PyTuple_GET_SIZE(args) == 0) |
| 11359 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 11360 | else |
| 11361 | return do_argstrip(self, BOTHSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11362 | } |
| 11363 | |
| 11364 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11365 | PyDoc_STRVAR(lstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11366 | "S.lstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11367 | \n\ |
| 11368 | Return a copy of the string S with leading whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11369 | 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] | 11370 | |
| 11371 | static PyObject * |
| 11372 | unicode_lstrip(PyUnicodeObject *self, PyObject *args) |
| 11373 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11374 | if (PyTuple_GET_SIZE(args) == 0) |
| 11375 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 11376 | else |
| 11377 | return do_argstrip(self, LEFTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11378 | } |
| 11379 | |
| 11380 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11381 | PyDoc_STRVAR(rstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11382 | "S.rstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11383 | \n\ |
| 11384 | Return a copy of the string S with trailing whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11385 | 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] | 11386 | |
| 11387 | static PyObject * |
| 11388 | unicode_rstrip(PyUnicodeObject *self, PyObject *args) |
| 11389 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11390 | if (PyTuple_GET_SIZE(args) == 0) |
| 11391 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 11392 | else |
| 11393 | return do_argstrip(self, RIGHTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11394 | } |
| 11395 | |
| 11396 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11397 | static PyObject* |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11398 | unicode_repeat(PyUnicodeObject *str, Py_ssize_t len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11399 | { |
| 11400 | PyUnicodeObject *u; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11401 | Py_ssize_t nchars, n; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11402 | |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 11403 | if (len < 1) { |
| 11404 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 11405 | return unicode_empty; |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 11406 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11407 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 11408 | if (len == 1 && PyUnicode_CheckExact(str)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11409 | /* no repeat, return original string */ |
| 11410 | Py_INCREF(str); |
| 11411 | return (PyObject*) str; |
| 11412 | } |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 11413 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11414 | if (PyUnicode_READY(str) == -1) |
| 11415 | return NULL; |
| 11416 | |
Victor Stinner | c759f3e | 2011-10-01 03:09:58 +0200 | [diff] [blame] | 11417 | if (PyUnicode_GET_LENGTH(str) > PY_SSIZE_T_MAX / len) { |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11418 | PyErr_SetString(PyExc_OverflowError, |
| 11419 | "repeated string is too long"); |
| 11420 | return NULL; |
| 11421 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11422 | nchars = len * PyUnicode_GET_LENGTH(str); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11423 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11424 | u = (PyUnicodeObject *)PyUnicode_New(nchars, PyUnicode_MAX_CHAR_VALUE(str)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11425 | if (!u) |
| 11426 | return NULL; |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11427 | assert(PyUnicode_KIND(u) == PyUnicode_KIND(str)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11428 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11429 | if (PyUnicode_GET_LENGTH(str) == 1) { |
| 11430 | const int kind = PyUnicode_KIND(str); |
| 11431 | const Py_UCS4 fill_char = PyUnicode_READ(kind, PyUnicode_DATA(str), 0); |
| 11432 | void *to = PyUnicode_DATA(u); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11433 | if (kind == PyUnicode_1BYTE_KIND) |
| 11434 | memset(to, (unsigned char)fill_char, len); |
| 11435 | else { |
| 11436 | for (n = 0; n < len; ++n) |
| 11437 | PyUnicode_WRITE(kind, to, n, fill_char); |
| 11438 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11439 | } |
| 11440 | else { |
| 11441 | /* number of characters copied this far */ |
| 11442 | Py_ssize_t done = PyUnicode_GET_LENGTH(str); |
| 11443 | const Py_ssize_t char_size = PyUnicode_CHARACTER_SIZE(str); |
| 11444 | char *to = (char *) PyUnicode_DATA(u); |
| 11445 | Py_MEMCPY(to, PyUnicode_DATA(str), |
| 11446 | PyUnicode_GET_LENGTH(str) * char_size); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11447 | while (done < nchars) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11448 | n = (done <= nchars-done) ? done : nchars-done; |
| 11449 | Py_MEMCPY(to + (done * char_size), to, n * char_size); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11450 | done += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11451 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11452 | } |
| 11453 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 11454 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11455 | return (PyObject*) u; |
| 11456 | } |
| 11457 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 11458 | PyObject * |
| 11459 | PyUnicode_Replace(PyObject *obj, |
| 11460 | PyObject *subobj, |
| 11461 | PyObject *replobj, |
| 11462 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11463 | { |
| 11464 | PyObject *self; |
| 11465 | PyObject *str1; |
| 11466 | PyObject *str2; |
| 11467 | PyObject *result; |
| 11468 | |
| 11469 | self = PyUnicode_FromObject(obj); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11470 | if (self == NULL || PyUnicode_READY(self) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11471 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11472 | str1 = PyUnicode_FromObject(subobj); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11473 | if (str1 == NULL || PyUnicode_READY(str1) == -1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11474 | Py_DECREF(self); |
| 11475 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11476 | } |
| 11477 | str2 = PyUnicode_FromObject(replobj); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11478 | if (str2 == NULL || PyUnicode_READY(str2)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11479 | Py_DECREF(self); |
| 11480 | Py_DECREF(str1); |
| 11481 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11482 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11483 | result = replace(self, str1, str2, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11484 | Py_DECREF(self); |
| 11485 | Py_DECREF(str1); |
| 11486 | Py_DECREF(str2); |
| 11487 | return result; |
| 11488 | } |
| 11489 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11490 | PyDoc_STRVAR(replace__doc__, |
Ezio Melotti | c1897e7 | 2010-06-26 18:50:39 +0000 | [diff] [blame] | 11491 | "S.replace(old, new[, count]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11492 | \n\ |
| 11493 | Return a copy of S with all occurrences of substring\n\ |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 11494 | old replaced by new. If the optional argument count is\n\ |
| 11495 | given, only the first count occurrences are replaced."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11496 | |
| 11497 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11498 | unicode_replace(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11499 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11500 | PyObject *str1; |
| 11501 | PyObject *str2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11502 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11503 | PyObject *result; |
| 11504 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11505 | if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11506 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11507 | if (!PyUnicode_READY(self) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11508 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11509 | str1 = PyUnicode_FromObject(str1); |
| 11510 | if (str1 == NULL || PyUnicode_READY(str1) == -1) |
| 11511 | return NULL; |
| 11512 | str2 = PyUnicode_FromObject(str2); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11513 | if (str2 == NULL || PyUnicode_READY(str2) == -1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11514 | Py_DECREF(str1); |
| 11515 | return NULL; |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 11516 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11517 | |
| 11518 | result = replace(self, str1, str2, maxcount); |
| 11519 | |
| 11520 | Py_DECREF(str1); |
| 11521 | Py_DECREF(str2); |
| 11522 | return result; |
| 11523 | } |
| 11524 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 11525 | static PyObject * |
| 11526 | unicode_repr(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11527 | { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11528 | PyObject *repr; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11529 | Py_ssize_t isize; |
| 11530 | Py_ssize_t osize, squote, dquote, i, o; |
| 11531 | Py_UCS4 max, quote; |
| 11532 | int ikind, okind; |
| 11533 | void *idata, *odata; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11534 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11535 | if (PyUnicode_READY(unicode) == -1) |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11536 | return NULL; |
| 11537 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11538 | isize = PyUnicode_GET_LENGTH(unicode); |
| 11539 | idata = PyUnicode_DATA(unicode); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11540 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11541 | /* Compute length of output, quote characters, and |
| 11542 | maximum character */ |
| 11543 | osize = 2; /* quotes */ |
| 11544 | max = 127; |
| 11545 | squote = dquote = 0; |
| 11546 | ikind = PyUnicode_KIND(unicode); |
| 11547 | for (i = 0; i < isize; i++) { |
| 11548 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
| 11549 | switch (ch) { |
| 11550 | case '\'': squote++; osize++; break; |
| 11551 | case '"': dquote++; osize++; break; |
| 11552 | case '\\': case '\t': case '\r': case '\n': |
| 11553 | osize += 2; break; |
| 11554 | default: |
| 11555 | /* Fast-path ASCII */ |
| 11556 | if (ch < ' ' || ch == 0x7f) |
| 11557 | osize += 4; /* \xHH */ |
| 11558 | else if (ch < 0x7f) |
| 11559 | osize++; |
| 11560 | else if (Py_UNICODE_ISPRINTABLE(ch)) { |
| 11561 | osize++; |
| 11562 | max = ch > max ? ch : max; |
| 11563 | } |
| 11564 | else if (ch < 0x100) |
| 11565 | osize += 4; /* \xHH */ |
| 11566 | else if (ch < 0x10000) |
| 11567 | osize += 6; /* \uHHHH */ |
| 11568 | else |
| 11569 | osize += 10; /* \uHHHHHHHH */ |
| 11570 | } |
| 11571 | } |
| 11572 | |
| 11573 | quote = '\''; |
| 11574 | if (squote) { |
| 11575 | if (dquote) |
| 11576 | /* Both squote and dquote present. Use squote, |
| 11577 | and escape them */ |
| 11578 | osize += squote; |
| 11579 | else |
| 11580 | quote = '"'; |
| 11581 | } |
| 11582 | |
| 11583 | repr = PyUnicode_New(osize, max); |
| 11584 | if (repr == NULL) |
| 11585 | return NULL; |
| 11586 | okind = PyUnicode_KIND(repr); |
| 11587 | odata = PyUnicode_DATA(repr); |
| 11588 | |
| 11589 | PyUnicode_WRITE(okind, odata, 0, quote); |
| 11590 | PyUnicode_WRITE(okind, odata, osize-1, quote); |
| 11591 | |
| 11592 | for (i = 0, o = 1; i < isize; i++) { |
| 11593 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11594 | |
| 11595 | /* Escape quotes and backslashes */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11596 | if ((ch == quote) || (ch == '\\')) { |
| 11597 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 11598 | PyUnicode_WRITE(okind, odata, o++, ch); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11599 | continue; |
| 11600 | } |
| 11601 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11602 | /* Map special whitespace to '\t', \n', '\r' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11603 | if (ch == '\t') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11604 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 11605 | PyUnicode_WRITE(okind, odata, o++, 't'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11606 | } |
| 11607 | else if (ch == '\n') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11608 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 11609 | PyUnicode_WRITE(okind, odata, o++, 'n'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11610 | } |
| 11611 | else if (ch == '\r') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11612 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 11613 | PyUnicode_WRITE(okind, odata, o++, 'r'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11614 | } |
| 11615 | |
| 11616 | /* Map non-printable US ASCII to '\xhh' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11617 | else if (ch < ' ' || ch == 0x7F) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11618 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 11619 | PyUnicode_WRITE(okind, odata, o++, 'x'); |
| 11620 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 4) & 0x000F]); |
| 11621 | PyUnicode_WRITE(okind, odata, o++, hexdigits[ch & 0x000F]); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11622 | } |
| 11623 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11624 | /* Copy ASCII characters as-is */ |
| 11625 | else if (ch < 0x7F) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11626 | PyUnicode_WRITE(okind, odata, o++, ch); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11627 | } |
| 11628 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11629 | /* Non-ASCII characters */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11630 | else { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11631 | /* Map Unicode whitespace and control characters |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11632 | (categories Z* and C* except ASCII space) |
| 11633 | */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11634 | if (!Py_UNICODE_ISPRINTABLE(ch)) { |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11635 | /* Map 8-bit characters to '\xhh' */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11636 | if (ch <= 0xff) { |
| 11637 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 11638 | PyUnicode_WRITE(okind, odata, o++, 'x'); |
| 11639 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 4) & 0x000F]); |
| 11640 | PyUnicode_WRITE(okind, odata, o++, hexdigits[ch & 0x000F]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11641 | } |
| 11642 | /* Map 21-bit characters to '\U00xxxxxx' */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11643 | else if (ch >= 0x10000) { |
| 11644 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 11645 | PyUnicode_WRITE(okind, odata, o++, 'U'); |
| 11646 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 28) & 0xF]); |
| 11647 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 24) & 0xF]); |
| 11648 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 20) & 0xF]); |
| 11649 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 16) & 0xF]); |
| 11650 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 12) & 0xF]); |
| 11651 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 8) & 0xF]); |
| 11652 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 4) & 0xF]); |
| 11653 | PyUnicode_WRITE(okind, odata, o++, hexdigits[ch & 0xF]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11654 | } |
| 11655 | /* Map 16-bit characters to '\uxxxx' */ |
| 11656 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11657 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 11658 | PyUnicode_WRITE(okind, odata, o++, 'u'); |
| 11659 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 12) & 0xF]); |
| 11660 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 8) & 0xF]); |
| 11661 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 4) & 0xF]); |
| 11662 | PyUnicode_WRITE(okind, odata, o++, hexdigits[ch & 0xF]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11663 | } |
| 11664 | } |
| 11665 | /* Copy characters as-is */ |
| 11666 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11667 | PyUnicode_WRITE(okind, odata, o++, ch); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11668 | } |
| 11669 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11670 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11671 | /* Closing quote already added at the beginning */ |
Victor Stinner | 05d1189 | 2011-10-06 01:13:58 +0200 | [diff] [blame] | 11672 | assert(_PyUnicode_CheckConsistency(repr, 1)); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11673 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11674 | } |
| 11675 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11676 | PyDoc_STRVAR(rfind__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11677 | "S.rfind(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11678 | \n\ |
| 11679 | Return the highest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 11680 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11681 | arguments start and end are interpreted as in slice notation.\n\ |
| 11682 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11683 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11684 | |
| 11685 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11686 | unicode_rfind(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11687 | { |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11688 | PyUnicodeObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 11689 | Py_ssize_t start; |
| 11690 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11691 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11692 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11693 | if (!stringlib_parse_args_finds_unicode("rfind", args, &substring, |
| 11694 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11695 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11696 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11697 | if (PyUnicode_READY(self) == -1) |
| 11698 | return NULL; |
| 11699 | if (PyUnicode_READY(substring) == -1) |
| 11700 | return NULL; |
| 11701 | |
| 11702 | result = any_find_slice( |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 11703 | asciilib_rfind_slice, ucs1lib_rfind_slice, |
| 11704 | ucs2lib_rfind_slice, ucs4lib_rfind_slice, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11705 | self, (PyObject*)substring, start, end |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11706 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11707 | |
| 11708 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11709 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11710 | if (result == -2) |
| 11711 | return NULL; |
| 11712 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 11713 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11714 | } |
| 11715 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11716 | PyDoc_STRVAR(rindex__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11717 | "S.rindex(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11718 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11719 | 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] | 11720 | |
| 11721 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11722 | unicode_rindex(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11723 | { |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11724 | PyUnicodeObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 11725 | Py_ssize_t start; |
| 11726 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11727 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11728 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11729 | if (!stringlib_parse_args_finds_unicode("rindex", args, &substring, |
| 11730 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11731 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11732 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11733 | if (PyUnicode_READY(self) == -1) |
| 11734 | return NULL; |
| 11735 | if (PyUnicode_READY(substring) == -1) |
| 11736 | return NULL; |
| 11737 | |
| 11738 | result = any_find_slice( |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 11739 | asciilib_rfind_slice, ucs1lib_rfind_slice, |
| 11740 | ucs2lib_rfind_slice, ucs4lib_rfind_slice, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11741 | self, (PyObject*)substring, start, end |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11742 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11743 | |
| 11744 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11745 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11746 | if (result == -2) |
| 11747 | return NULL; |
| 11748 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11749 | if (result < 0) { |
| 11750 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 11751 | return NULL; |
| 11752 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11753 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 11754 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11755 | } |
| 11756 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11757 | PyDoc_STRVAR(rjust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11758 | "S.rjust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11759 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 11760 | 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] | 11761 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11762 | |
| 11763 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11764 | unicode_rjust(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11765 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11766 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11767 | Py_UCS4 fillchar = ' '; |
| 11768 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11769 | 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] | 11770 | return NULL; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 11771 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11772 | if (PyUnicode_READY(self) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11773 | return NULL; |
| 11774 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11775 | if (_PyUnicode_LENGTH(self) >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11776 | Py_INCREF(self); |
| 11777 | return (PyObject*) self; |
| 11778 | } |
| 11779 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11780 | return (PyObject*) pad(self, width - _PyUnicode_LENGTH(self), 0, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11781 | } |
| 11782 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 11783 | PyObject * |
| 11784 | PyUnicode_Split(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11785 | { |
| 11786 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11787 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11788 | s = PyUnicode_FromObject(s); |
| 11789 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11790 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11791 | if (sep != NULL) { |
| 11792 | sep = PyUnicode_FromObject(sep); |
| 11793 | if (sep == NULL) { |
| 11794 | Py_DECREF(s); |
| 11795 | return NULL; |
| 11796 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11797 | } |
| 11798 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11799 | result = split(s, sep, maxsplit); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11800 | |
| 11801 | Py_DECREF(s); |
| 11802 | Py_XDECREF(sep); |
| 11803 | return result; |
| 11804 | } |
| 11805 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11806 | PyDoc_STRVAR(split__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11807 | "S.split([sep[, maxsplit]]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11808 | \n\ |
| 11809 | Return a list of the words in S, using sep as the\n\ |
| 11810 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 11811 | 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] | 11812 | whitespace string is a separator and empty strings are\n\ |
| 11813 | removed from the result."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11814 | |
| 11815 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11816 | unicode_split(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11817 | { |
| 11818 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11819 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11820 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11821 | if (!PyArg_ParseTuple(args, "|On:split", &substring, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11822 | return NULL; |
| 11823 | |
| 11824 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11825 | return split(self, NULL, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11826 | else if (PyUnicode_Check(substring)) |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11827 | return split(self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11828 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11829 | return PyUnicode_Split((PyObject *)self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11830 | } |
| 11831 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11832 | PyObject * |
| 11833 | PyUnicode_Partition(PyObject *str_in, PyObject *sep_in) |
| 11834 | { |
| 11835 | PyObject* str_obj; |
| 11836 | PyObject* sep_obj; |
| 11837 | PyObject* out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11838 | int kind1, kind2, kind; |
| 11839 | void *buf1 = NULL, *buf2 = NULL; |
| 11840 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11841 | |
| 11842 | str_obj = PyUnicode_FromObject(str_in); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11843 | if (!str_obj || PyUnicode_READY(str_obj) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11844 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11845 | sep_obj = PyUnicode_FromObject(sep_in); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11846 | if (!sep_obj || PyUnicode_READY(sep_obj) == -1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11847 | Py_DECREF(str_obj); |
| 11848 | return NULL; |
| 11849 | } |
| 11850 | |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 11851 | kind1 = PyUnicode_KIND(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11852 | kind2 = PyUnicode_KIND(sep_obj); |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 11853 | kind = Py_MAX(kind1, kind2); |
| 11854 | buf1 = PyUnicode_DATA(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11855 | if (kind1 != kind) |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 11856 | buf1 = _PyUnicode_AsKind(str_obj, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11857 | if (!buf1) |
| 11858 | goto onError; |
| 11859 | buf2 = PyUnicode_DATA(sep_obj); |
| 11860 | if (kind2 != kind) |
| 11861 | buf2 = _PyUnicode_AsKind(sep_obj, kind); |
| 11862 | if (!buf2) |
| 11863 | goto onError; |
| 11864 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 11865 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
| 11866 | |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 11867 | switch(PyUnicode_KIND(str_obj)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11868 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 11869 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sep_obj)) |
| 11870 | out = asciilib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 11871 | else |
| 11872 | 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] | 11873 | break; |
| 11874 | case PyUnicode_2BYTE_KIND: |
| 11875 | out = ucs2lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 11876 | break; |
| 11877 | case PyUnicode_4BYTE_KIND: |
| 11878 | out = ucs4lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 11879 | break; |
| 11880 | default: |
| 11881 | assert(0); |
| 11882 | out = 0; |
| 11883 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11884 | |
| 11885 | Py_DECREF(sep_obj); |
| 11886 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11887 | if (kind1 != kind) |
| 11888 | PyMem_Free(buf1); |
| 11889 | if (kind2 != kind) |
| 11890 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11891 | |
| 11892 | return out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11893 | onError: |
| 11894 | Py_DECREF(sep_obj); |
| 11895 | Py_DECREF(str_obj); |
| 11896 | if (kind1 != kind && buf1) |
| 11897 | PyMem_Free(buf1); |
| 11898 | if (kind2 != kind && buf2) |
| 11899 | PyMem_Free(buf2); |
| 11900 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11901 | } |
| 11902 | |
| 11903 | |
| 11904 | PyObject * |
| 11905 | PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in) |
| 11906 | { |
| 11907 | PyObject* str_obj; |
| 11908 | PyObject* sep_obj; |
| 11909 | PyObject* out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11910 | int kind1, kind2, kind; |
| 11911 | void *buf1 = NULL, *buf2 = NULL; |
| 11912 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11913 | |
| 11914 | str_obj = PyUnicode_FromObject(str_in); |
| 11915 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11916 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11917 | sep_obj = PyUnicode_FromObject(sep_in); |
| 11918 | if (!sep_obj) { |
| 11919 | Py_DECREF(str_obj); |
| 11920 | return NULL; |
| 11921 | } |
| 11922 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11923 | kind1 = PyUnicode_KIND(str_in); |
| 11924 | kind2 = PyUnicode_KIND(sep_obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 11925 | kind = Py_MAX(kind1, kind2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11926 | buf1 = PyUnicode_DATA(str_in); |
| 11927 | if (kind1 != kind) |
| 11928 | buf1 = _PyUnicode_AsKind(str_in, kind); |
| 11929 | if (!buf1) |
| 11930 | goto onError; |
| 11931 | buf2 = PyUnicode_DATA(sep_obj); |
| 11932 | if (kind2 != kind) |
| 11933 | buf2 = _PyUnicode_AsKind(sep_obj, kind); |
| 11934 | if (!buf2) |
| 11935 | goto onError; |
| 11936 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 11937 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
| 11938 | |
| 11939 | switch(PyUnicode_KIND(str_in)) { |
| 11940 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 11941 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sep_obj)) |
| 11942 | out = asciilib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 11943 | else |
| 11944 | 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] | 11945 | break; |
| 11946 | case PyUnicode_2BYTE_KIND: |
| 11947 | out = ucs2lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 11948 | break; |
| 11949 | case PyUnicode_4BYTE_KIND: |
| 11950 | out = ucs4lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 11951 | break; |
| 11952 | default: |
| 11953 | assert(0); |
| 11954 | out = 0; |
| 11955 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11956 | |
| 11957 | Py_DECREF(sep_obj); |
| 11958 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11959 | if (kind1 != kind) |
| 11960 | PyMem_Free(buf1); |
| 11961 | if (kind2 != kind) |
| 11962 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11963 | |
| 11964 | return out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11965 | onError: |
| 11966 | Py_DECREF(sep_obj); |
| 11967 | Py_DECREF(str_obj); |
| 11968 | if (kind1 != kind && buf1) |
| 11969 | PyMem_Free(buf1); |
| 11970 | if (kind2 != kind && buf2) |
| 11971 | PyMem_Free(buf2); |
| 11972 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11973 | } |
| 11974 | |
| 11975 | PyDoc_STRVAR(partition__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11976 | "S.partition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11977 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 11978 | 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] | 11979 | 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] | 11980 | found, return S and two empty strings."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11981 | |
| 11982 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11983 | unicode_partition(PyObject *self, PyObject *separator) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11984 | { |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11985 | return PyUnicode_Partition(self, separator); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11986 | } |
| 11987 | |
| 11988 | PyDoc_STRVAR(rpartition__doc__, |
Ezio Melotti | 5b2b242 | 2010-01-25 11:58:28 +0000 | [diff] [blame] | 11989 | "S.rpartition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11990 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 11991 | 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] | 11992 | 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] | 11993 | separator is not found, return two empty strings and S."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11994 | |
| 11995 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11996 | unicode_rpartition(PyObject *self, PyObject *separator) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11997 | { |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11998 | return PyUnicode_RPartition(self, separator); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11999 | } |
| 12000 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12001 | PyObject * |
| 12002 | PyUnicode_RSplit(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12003 | { |
| 12004 | PyObject *result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12005 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12006 | s = PyUnicode_FromObject(s); |
| 12007 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12008 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12009 | if (sep != NULL) { |
| 12010 | sep = PyUnicode_FromObject(sep); |
| 12011 | if (sep == NULL) { |
| 12012 | Py_DECREF(s); |
| 12013 | return NULL; |
| 12014 | } |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12015 | } |
| 12016 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12017 | result = rsplit(s, sep, maxsplit); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12018 | |
| 12019 | Py_DECREF(s); |
| 12020 | Py_XDECREF(sep); |
| 12021 | return result; |
| 12022 | } |
| 12023 | |
| 12024 | PyDoc_STRVAR(rsplit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12025 | "S.rsplit([sep[, maxsplit]]) -> list of strings\n\ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12026 | \n\ |
| 12027 | Return a list of the words in S, using sep as the\n\ |
| 12028 | delimiter string, starting at the end of the string and\n\ |
| 12029 | working to the front. If maxsplit is given, at most maxsplit\n\ |
| 12030 | splits are done. If sep is not specified, any whitespace string\n\ |
| 12031 | is a separator."); |
| 12032 | |
| 12033 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12034 | unicode_rsplit(PyObject *self, PyObject *args) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12035 | { |
| 12036 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12037 | Py_ssize_t maxcount = -1; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12038 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12039 | if (!PyArg_ParseTuple(args, "|On:rsplit", &substring, &maxcount)) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12040 | return NULL; |
| 12041 | |
| 12042 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12043 | return rsplit(self, NULL, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12044 | else if (PyUnicode_Check(substring)) |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12045 | return rsplit(self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12046 | else |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12047 | return PyUnicode_RSplit(self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12048 | } |
| 12049 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12050 | PyDoc_STRVAR(splitlines__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12051 | "S.splitlines([keepends]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12052 | \n\ |
| 12053 | 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] | 12054 | 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] | 12055 | is given and true."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12056 | |
| 12057 | static PyObject* |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12058 | unicode_splitlines(PyUnicodeObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12059 | { |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12060 | static char *kwlist[] = {"keepends", 0}; |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 12061 | int keepends = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12062 | |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12063 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|i:splitlines", |
| 12064 | kwlist, &keepends)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12065 | return NULL; |
| 12066 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 12067 | return PyUnicode_Splitlines((PyObject *)self, keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12068 | } |
| 12069 | |
| 12070 | static |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 12071 | PyObject *unicode_str(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12072 | { |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 12073 | if (PyUnicode_CheckExact(self)) { |
| 12074 | Py_INCREF(self); |
| 12075 | return self; |
| 12076 | } else |
| 12077 | /* Subtype -- return genuine unicode string with the same value. */ |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 12078 | return PyUnicode_Copy(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12079 | } |
| 12080 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12081 | PyDoc_STRVAR(swapcase__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12082 | "S.swapcase() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12083 | \n\ |
| 12084 | 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] | 12085 | and vice versa."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12086 | |
| 12087 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12088 | unicode_swapcase(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12089 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12090 | return fixup(self, fixswapcase); |
| 12091 | } |
| 12092 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12093 | PyDoc_STRVAR(maketrans__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12094 | "str.maketrans(x[, y[, z]]) -> dict (static method)\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12095 | \n\ |
| 12096 | Return a translation table usable for str.translate().\n\ |
| 12097 | If there is only one argument, it must be a dictionary mapping Unicode\n\ |
| 12098 | ordinals (integers) or characters to Unicode ordinals, strings or None.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12099 | Character keys will be then converted to ordinals.\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12100 | If there are two arguments, they must be strings of equal length, and\n\ |
| 12101 | in the resulting dictionary, each character in x will be mapped to the\n\ |
| 12102 | character at the same position in y. If there is a third argument, it\n\ |
| 12103 | must be a string, whose characters will be mapped to None in the result."); |
| 12104 | |
| 12105 | static PyObject* |
| 12106 | unicode_maketrans(PyUnicodeObject *null, PyObject *args) |
| 12107 | { |
| 12108 | PyObject *x, *y = NULL, *z = NULL; |
| 12109 | PyObject *new = NULL, *key, *value; |
| 12110 | Py_ssize_t i = 0; |
| 12111 | int res; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12112 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12113 | if (!PyArg_ParseTuple(args, "O|UU:maketrans", &x, &y, &z)) |
| 12114 | return NULL; |
| 12115 | new = PyDict_New(); |
| 12116 | if (!new) |
| 12117 | return NULL; |
| 12118 | if (y != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12119 | int x_kind, y_kind, z_kind; |
| 12120 | void *x_data, *y_data, *z_data; |
| 12121 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12122 | /* x must be a string too, of equal length */ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12123 | if (!PyUnicode_Check(x)) { |
| 12124 | PyErr_SetString(PyExc_TypeError, "first maketrans argument must " |
| 12125 | "be a string if there is a second argument"); |
| 12126 | goto err; |
| 12127 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12128 | if (PyUnicode_GET_LENGTH(x) != PyUnicode_GET_LENGTH(y)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12129 | PyErr_SetString(PyExc_ValueError, "the first two maketrans " |
| 12130 | "arguments must have equal length"); |
| 12131 | goto err; |
| 12132 | } |
| 12133 | /* 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] | 12134 | x_kind = PyUnicode_KIND(x); |
| 12135 | y_kind = PyUnicode_KIND(y); |
| 12136 | x_data = PyUnicode_DATA(x); |
| 12137 | y_data = PyUnicode_DATA(y); |
| 12138 | for (i = 0; i < PyUnicode_GET_LENGTH(x); i++) { |
| 12139 | key = PyLong_FromLong(PyUnicode_READ(x_kind, x_data, i)); |
| 12140 | value = PyLong_FromLong(PyUnicode_READ(y_kind, y_data, i)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12141 | if (!key || !value) |
| 12142 | goto err; |
| 12143 | res = PyDict_SetItem(new, key, value); |
| 12144 | Py_DECREF(key); |
| 12145 | Py_DECREF(value); |
| 12146 | if (res < 0) |
| 12147 | goto err; |
| 12148 | } |
| 12149 | /* create entries for deleting chars in z */ |
| 12150 | if (z != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12151 | z_kind = PyUnicode_KIND(z); |
| 12152 | z_data = PyUnicode_DATA(z); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12153 | for (i = 0; i < PyUnicode_GET_SIZE(z); i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12154 | key = PyLong_FromLong(PyUnicode_READ(z_kind, z_data, i)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12155 | if (!key) |
| 12156 | goto err; |
| 12157 | res = PyDict_SetItem(new, key, Py_None); |
| 12158 | Py_DECREF(key); |
| 12159 | if (res < 0) |
| 12160 | goto err; |
| 12161 | } |
| 12162 | } |
| 12163 | } else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12164 | int kind; |
| 12165 | void *data; |
| 12166 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12167 | /* x must be a dict */ |
Raymond Hettinger | 3ad0576 | 2009-05-29 22:11:22 +0000 | [diff] [blame] | 12168 | if (!PyDict_CheckExact(x)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12169 | PyErr_SetString(PyExc_TypeError, "if you give only one argument " |
| 12170 | "to maketrans it must be a dict"); |
| 12171 | goto err; |
| 12172 | } |
| 12173 | /* copy entries into the new dict, converting string keys to int keys */ |
| 12174 | while (PyDict_Next(x, &i, &key, &value)) { |
| 12175 | if (PyUnicode_Check(key)) { |
| 12176 | /* convert string keys to integer keys */ |
| 12177 | PyObject *newkey; |
| 12178 | if (PyUnicode_GET_SIZE(key) != 1) { |
| 12179 | PyErr_SetString(PyExc_ValueError, "string keys in translate " |
| 12180 | "table must be of length 1"); |
| 12181 | goto err; |
| 12182 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12183 | kind = PyUnicode_KIND(key); |
| 12184 | data = PyUnicode_DATA(key); |
| 12185 | newkey = PyLong_FromLong(PyUnicode_READ(kind, data, 0)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12186 | if (!newkey) |
| 12187 | goto err; |
| 12188 | res = PyDict_SetItem(new, newkey, value); |
| 12189 | Py_DECREF(newkey); |
| 12190 | if (res < 0) |
| 12191 | goto err; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12192 | } else if (PyLong_Check(key)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12193 | /* just keep integer keys */ |
| 12194 | if (PyDict_SetItem(new, key, value) < 0) |
| 12195 | goto err; |
| 12196 | } else { |
| 12197 | PyErr_SetString(PyExc_TypeError, "keys in translate table must " |
| 12198 | "be strings or integers"); |
| 12199 | goto err; |
| 12200 | } |
| 12201 | } |
| 12202 | } |
| 12203 | return new; |
| 12204 | err: |
| 12205 | Py_DECREF(new); |
| 12206 | return NULL; |
| 12207 | } |
| 12208 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12209 | PyDoc_STRVAR(translate__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12210 | "S.translate(table) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12211 | \n\ |
| 12212 | Return a copy of the string S, where all characters have been mapped\n\ |
| 12213 | through the given translation table, which must be a mapping of\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12214 | Unicode ordinals to Unicode ordinals, strings, or None.\n\ |
Walter Dörwald | 5c1ee17 | 2002-09-04 20:31:32 +0000 | [diff] [blame] | 12215 | Unmapped characters are left untouched. Characters mapped to None\n\ |
| 12216 | are deleted."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12217 | |
| 12218 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12219 | unicode_translate(PyObject *self, PyObject *table) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12220 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12221 | return _PyUnicode_TranslateCharmap(self, table, "ignore"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12222 | } |
| 12223 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12224 | PyDoc_STRVAR(upper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12225 | "S.upper() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12226 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12227 | Return a copy of S converted to uppercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12228 | |
| 12229 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12230 | unicode_upper(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12231 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12232 | return fixup(self, fixupper); |
| 12233 | } |
| 12234 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12235 | PyDoc_STRVAR(zfill__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12236 | "S.zfill(width) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12237 | \n\ |
Benjamin Peterson | 9aa4299 | 2008-09-10 21:57:34 +0000 | [diff] [blame] | 12238 | Pad a numeric string S with zeros on the left, to fill a field\n\ |
| 12239 | of the specified width. The string S is never truncated."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12240 | |
| 12241 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12242 | unicode_zfill(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12243 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12244 | Py_ssize_t fill; |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12245 | PyObject *u; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12246 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12247 | int kind; |
| 12248 | void *data; |
| 12249 | Py_UCS4 chr; |
| 12250 | |
| 12251 | if (PyUnicode_READY(self) == -1) |
| 12252 | return NULL; |
| 12253 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12254 | if (!PyArg_ParseTuple(args, "n:zfill", &width)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12255 | return NULL; |
| 12256 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12257 | if (PyUnicode_GET_LENGTH(self) >= width) { |
Walter Dörwald | 0fe940c | 2002-04-15 18:42:15 +0000 | [diff] [blame] | 12258 | if (PyUnicode_CheckExact(self)) { |
| 12259 | Py_INCREF(self); |
| 12260 | return (PyObject*) self; |
| 12261 | } |
| 12262 | else |
Victor Stinner | 2219e0a | 2011-10-01 01:16:59 +0200 | [diff] [blame] | 12263 | return PyUnicode_Copy((PyObject*)self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12264 | } |
| 12265 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12266 | fill = width - _PyUnicode_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12267 | |
| 12268 | u = pad(self, fill, 0, '0'); |
| 12269 | |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 12270 | if (u == NULL) |
| 12271 | return NULL; |
| 12272 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12273 | kind = PyUnicode_KIND(u); |
| 12274 | data = PyUnicode_DATA(u); |
| 12275 | chr = PyUnicode_READ(kind, data, fill); |
| 12276 | |
| 12277 | if (chr == '+' || chr == '-') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12278 | /* move sign to beginning of string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12279 | PyUnicode_WRITE(kind, data, 0, chr); |
| 12280 | PyUnicode_WRITE(kind, data, fill, '0'); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12281 | } |
| 12282 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12283 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12284 | return (PyObject*) u; |
| 12285 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12286 | |
| 12287 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12288 | static PyObject * |
| 12289 | unicode__decimal2ascii(PyObject *self) |
| 12290 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12291 | return PyUnicode_TransformDecimalAndSpaceToASCII(self); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12292 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12293 | #endif |
| 12294 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12295 | PyDoc_STRVAR(startswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12296 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12297 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 12298 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 12299 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12300 | With optional end, stop comparing S at that position.\n\ |
| 12301 | prefix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12302 | |
| 12303 | static PyObject * |
| 12304 | unicode_startswith(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12305 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12306 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12307 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12308 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12309 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12310 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12311 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12312 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12313 | if (!stringlib_parse_args_finds("startswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12314 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12315 | if (PyTuple_Check(subobj)) { |
| 12316 | Py_ssize_t i; |
| 12317 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 12318 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12319 | PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12320 | if (substring == NULL) |
| 12321 | return NULL; |
| 12322 | result = tailmatch(self, substring, start, end, -1); |
| 12323 | Py_DECREF(substring); |
| 12324 | if (result) { |
| 12325 | Py_RETURN_TRUE; |
| 12326 | } |
| 12327 | } |
| 12328 | /* nothing matched */ |
| 12329 | Py_RETURN_FALSE; |
| 12330 | } |
| 12331 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12332 | if (substring == NULL) { |
| 12333 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 12334 | PyErr_Format(PyExc_TypeError, "startswith first arg must be str or " |
| 12335 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12336 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12337 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12338 | result = tailmatch(self, substring, start, end, -1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12339 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12340 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12341 | } |
| 12342 | |
| 12343 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12344 | PyDoc_STRVAR(endswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12345 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12346 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 12347 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 12348 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12349 | With optional end, stop comparing S at that position.\n\ |
| 12350 | suffix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12351 | |
| 12352 | static PyObject * |
| 12353 | unicode_endswith(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12354 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12355 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12356 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12357 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12358 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12359 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12360 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12361 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12362 | if (!stringlib_parse_args_finds("endswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12363 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12364 | if (PyTuple_Check(subobj)) { |
| 12365 | Py_ssize_t i; |
| 12366 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 12367 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12368 | PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12369 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12370 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12371 | result = tailmatch(self, substring, start, end, +1); |
| 12372 | Py_DECREF(substring); |
| 12373 | if (result) { |
| 12374 | Py_RETURN_TRUE; |
| 12375 | } |
| 12376 | } |
| 12377 | Py_RETURN_FALSE; |
| 12378 | } |
| 12379 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12380 | if (substring == NULL) { |
| 12381 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 12382 | PyErr_Format(PyExc_TypeError, "endswith first arg must be str or " |
| 12383 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12384 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12385 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12386 | result = tailmatch(self, substring, start, end, +1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12387 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12388 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12389 | } |
| 12390 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12391 | #include "stringlib/unicode_format.h" |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12392 | |
| 12393 | PyDoc_STRVAR(format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12394 | "S.format(*args, **kwargs) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12395 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 12396 | Return a formatted version of S, using substitutions from args and kwargs.\n\ |
| 12397 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12398 | |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 12399 | PyDoc_STRVAR(format_map__doc__, |
| 12400 | "S.format_map(mapping) -> str\n\ |
| 12401 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 12402 | Return a formatted version of S, using substitutions from mapping.\n\ |
| 12403 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 12404 | |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 12405 | static PyObject * |
| 12406 | unicode__format__(PyObject* self, PyObject* args) |
| 12407 | { |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12408 | PyObject *format_spec, *out; |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 12409 | |
| 12410 | if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) |
| 12411 | return NULL; |
| 12412 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12413 | out = _PyUnicode_FormatAdvanced(self, format_spec, 0, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12414 | PyUnicode_GET_LENGTH(format_spec)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12415 | return out; |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 12416 | } |
| 12417 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12418 | PyDoc_STRVAR(p_format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12419 | "S.__format__(format_spec) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12420 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 12421 | Return a formatted version of S as described by format_spec."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12422 | |
| 12423 | static PyObject * |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 12424 | unicode__sizeof__(PyUnicodeObject *v) |
| 12425 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12426 | Py_ssize_t size; |
| 12427 | |
| 12428 | /* If it's a compact object, account for base structure + |
| 12429 | character data. */ |
| 12430 | if (PyUnicode_IS_COMPACT_ASCII(v)) |
| 12431 | size = sizeof(PyASCIIObject) + PyUnicode_GET_LENGTH(v) + 1; |
| 12432 | else if (PyUnicode_IS_COMPACT(v)) |
| 12433 | size = sizeof(PyCompactUnicodeObject) + |
| 12434 | (PyUnicode_GET_LENGTH(v) + 1) * PyUnicode_CHARACTER_SIZE(v); |
| 12435 | else { |
| 12436 | /* If it is a two-block object, account for base object, and |
| 12437 | for character block if present. */ |
| 12438 | size = sizeof(PyUnicodeObject); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 12439 | if (_PyUnicode_DATA_ANY(v)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12440 | size += (PyUnicode_GET_LENGTH(v) + 1) * |
| 12441 | PyUnicode_CHARACTER_SIZE(v); |
| 12442 | } |
| 12443 | /* 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] | 12444 | with the data pointer. Check if the data is not shared. */ |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 12445 | if (_PyUnicode_HAS_WSTR_MEMORY(v)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12446 | size += (PyUnicode_WSTR_LENGTH(v) + 1) * sizeof(wchar_t); |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 12447 | if (_PyUnicode_HAS_UTF8_MEMORY(v)) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 12448 | size += PyUnicode_UTF8_LENGTH(v) + 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12449 | |
| 12450 | return PyLong_FromSsize_t(size); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 12451 | } |
| 12452 | |
| 12453 | PyDoc_STRVAR(sizeof__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12454 | "S.__sizeof__() -> size of S in memory, in bytes"); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 12455 | |
| 12456 | static PyObject * |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 12457 | unicode_getnewargs(PyObject *v) |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 12458 | { |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 12459 | PyObject *copy = PyUnicode_Copy(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12460 | if (!copy) |
| 12461 | return NULL; |
| 12462 | return Py_BuildValue("(N)", copy); |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 12463 | } |
| 12464 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12465 | static PyMethodDef unicode_methods[] = { |
| 12466 | |
| 12467 | /* Order is according to common usage: often used methods should |
| 12468 | appear first, since lookup is done sequentially. */ |
| 12469 | |
Benjamin Peterson | 28a4dce | 2010-12-12 01:33:04 +0000 | [diff] [blame] | 12470 | {"encode", (PyCFunction) unicode_encode, METH_VARARGS | METH_KEYWORDS, encode__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12471 | {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, |
| 12472 | {"split", (PyCFunction) unicode_split, METH_VARARGS, split__doc__}, |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12473 | {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS, rsplit__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12474 | {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, |
| 12475 | {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, |
| 12476 | {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, |
| 12477 | {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, |
| 12478 | {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, |
| 12479 | {"expandtabs", (PyCFunction) unicode_expandtabs, METH_VARARGS, expandtabs__doc__}, |
| 12480 | {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12481 | {"partition", (PyCFunction) unicode_partition, METH_O, partition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12482 | {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, |
| 12483 | {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, |
| 12484 | {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12485 | {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12486 | {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, |
| 12487 | {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, |
| 12488 | {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12489 | {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12490 | {"rpartition", (PyCFunction) unicode_rpartition, METH_O, rpartition__doc__}, |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12491 | {"splitlines", (PyCFunction) unicode_splitlines, METH_VARARGS | METH_KEYWORDS, splitlines__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12492 | {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12493 | {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, |
| 12494 | {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, |
| 12495 | {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, |
| 12496 | {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, |
| 12497 | {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, |
| 12498 | {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, |
| 12499 | {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, |
| 12500 | {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, |
| 12501 | {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, |
| 12502 | {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, |
| 12503 | {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, |
| 12504 | {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, |
| 12505 | {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, |
| 12506 | {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 12507 | {"isidentifier", (PyCFunction) unicode_isidentifier, METH_NOARGS, isidentifier__doc__}, |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12508 | {"isprintable", (PyCFunction) unicode_isprintable, METH_NOARGS, isprintable__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12509 | {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, |
Eric Smith | 9cd1e09 | 2007-08-31 18:39:38 +0000 | [diff] [blame] | 12510 | {"format", (PyCFunction) do_string_format, METH_VARARGS | METH_KEYWORDS, format__doc__}, |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 12511 | {"format_map", (PyCFunction) do_string_format_map, METH_O, format_map__doc__}, |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 12512 | {"__format__", (PyCFunction) unicode__format__, METH_VARARGS, p_format__doc__}, |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12513 | {"maketrans", (PyCFunction) unicode_maketrans, |
| 12514 | METH_VARARGS | METH_STATIC, maketrans__doc__}, |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 12515 | {"__sizeof__", (PyCFunction) unicode__sizeof__, METH_NOARGS, sizeof__doc__}, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 12516 | #if 0 |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12517 | {"capwords", (PyCFunction) unicode_capwords, METH_NOARGS, capwords__doc__}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12518 | #endif |
| 12519 | |
| 12520 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12521 | /* These methods are just used for debugging the implementation. */ |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12522 | {"_decimal2ascii", (PyCFunction) unicode__decimal2ascii, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12523 | #endif |
| 12524 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12525 | {"__getnewargs__", (PyCFunction)unicode_getnewargs, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12526 | {NULL, NULL} |
| 12527 | }; |
| 12528 | |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 12529 | static PyObject * |
| 12530 | unicode_mod(PyObject *v, PyObject *w) |
| 12531 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 12532 | if (!PyUnicode_Check(v)) |
| 12533 | Py_RETURN_NOTIMPLEMENTED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12534 | return PyUnicode_Format(v, w); |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 12535 | } |
| 12536 | |
| 12537 | static PyNumberMethods unicode_as_number = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12538 | 0, /*nb_add*/ |
| 12539 | 0, /*nb_subtract*/ |
| 12540 | 0, /*nb_multiply*/ |
| 12541 | unicode_mod, /*nb_remainder*/ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 12542 | }; |
| 12543 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12544 | static PySequenceMethods unicode_as_sequence = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12545 | (lenfunc) unicode_length, /* sq_length */ |
| 12546 | PyUnicode_Concat, /* sq_concat */ |
| 12547 | (ssizeargfunc) unicode_repeat, /* sq_repeat */ |
| 12548 | (ssizeargfunc) unicode_getitem, /* sq_item */ |
| 12549 | 0, /* sq_slice */ |
| 12550 | 0, /* sq_ass_item */ |
| 12551 | 0, /* sq_ass_slice */ |
| 12552 | PyUnicode_Contains, /* sq_contains */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12553 | }; |
| 12554 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12555 | static PyObject* |
| 12556 | unicode_subscript(PyUnicodeObject* self, PyObject* item) |
| 12557 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12558 | if (PyUnicode_READY(self) == -1) |
| 12559 | return NULL; |
| 12560 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 12561 | if (PyIndex_Check(item)) { |
| 12562 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12563 | if (i == -1 && PyErr_Occurred()) |
| 12564 | return NULL; |
| 12565 | if (i < 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12566 | i += PyUnicode_GET_LENGTH(self); |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 12567 | return unicode_getitem((PyObject*)self, i); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12568 | } else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12569 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 12570 | PyObject *result; |
| 12571 | void *src_data, *dest_data; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 12572 | int src_kind, dest_kind; |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 12573 | Py_UCS4 ch, max_char, kind_limit; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12574 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12575 | if (PySlice_GetIndicesEx(item, PyUnicode_GET_LENGTH(self), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12576 | &start, &stop, &step, &slicelength) < 0) { |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12577 | return NULL; |
| 12578 | } |
| 12579 | |
| 12580 | if (slicelength <= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12581 | return PyUnicode_New(0, 0); |
| 12582 | } else if (start == 0 && step == 1 && |
| 12583 | slicelength == PyUnicode_GET_LENGTH(self) && |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 12584 | PyUnicode_CheckExact(self)) { |
| 12585 | Py_INCREF(self); |
| 12586 | return (PyObject *)self; |
| 12587 | } else if (step == 1) { |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 12588 | return PyUnicode_Substring((PyObject*)self, |
| 12589 | start, start + slicelength); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12590 | } |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 12591 | /* General case */ |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 12592 | max_char = 0; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 12593 | src_kind = PyUnicode_KIND(self); |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 12594 | kind_limit = kind_maxchar_limit(src_kind); |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 12595 | src_data = PyUnicode_DATA(self); |
| 12596 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
| 12597 | ch = PyUnicode_READ(src_kind, src_data, cur); |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 12598 | if (ch > max_char) { |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 12599 | max_char = ch; |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 12600 | if (max_char >= kind_limit) |
| 12601 | break; |
| 12602 | } |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 12603 | } |
| 12604 | result = PyUnicode_New(slicelength, max_char); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 12605 | if (result == NULL) |
| 12606 | return NULL; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 12607 | dest_kind = PyUnicode_KIND(result); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 12608 | dest_data = PyUnicode_DATA(result); |
| 12609 | |
| 12610 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 12611 | Py_UCS4 ch = PyUnicode_READ(src_kind, src_data, cur); |
| 12612 | PyUnicode_WRITE(dest_kind, dest_data, i, ch); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 12613 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12614 | assert(_PyUnicode_CheckConsistency(result, 1)); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 12615 | return result; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12616 | } else { |
| 12617 | PyErr_SetString(PyExc_TypeError, "string indices must be integers"); |
| 12618 | return NULL; |
| 12619 | } |
| 12620 | } |
| 12621 | |
| 12622 | static PyMappingMethods unicode_as_mapping = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12623 | (lenfunc)unicode_length, /* mp_length */ |
| 12624 | (binaryfunc)unicode_subscript, /* mp_subscript */ |
| 12625 | (objobjargproc)0, /* mp_ass_subscript */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12626 | }; |
| 12627 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12628 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12629 | /* Helpers for PyUnicode_Format() */ |
| 12630 | |
| 12631 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12632 | 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] | 12633 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12634 | Py_ssize_t argidx = *p_argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12635 | if (argidx < arglen) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12636 | (*p_argidx)++; |
| 12637 | if (arglen < 0) |
| 12638 | return args; |
| 12639 | else |
| 12640 | return PyTuple_GetItem(args, argidx); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12641 | } |
| 12642 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12643 | "not enough arguments for format string"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12644 | return NULL; |
| 12645 | } |
| 12646 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 12647 | /* 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] | 12648 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 12649 | static PyObject * |
| 12650 | formatfloat(PyObject *v, int flags, int prec, int type) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12651 | { |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 12652 | char *p; |
| 12653 | PyObject *result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12654 | double x; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 12655 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12656 | x = PyFloat_AsDouble(v); |
| 12657 | if (x == -1.0 && PyErr_Occurred()) |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 12658 | return NULL; |
| 12659 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12660 | if (prec < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12661 | prec = 6; |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 12662 | |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 12663 | p = PyOS_double_to_string(x, type, prec, |
| 12664 | (flags & F_ALT) ? Py_DTSF_ALT : 0, NULL); |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 12665 | if (p == NULL) |
| 12666 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12667 | result = PyUnicode_DecodeASCII(p, strlen(p), NULL); |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 12668 | PyMem_Free(p); |
| 12669 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12670 | } |
| 12671 | |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 12672 | static PyObject* |
| 12673 | formatlong(PyObject *val, int flags, int prec, int type) |
| 12674 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12675 | char *buf; |
| 12676 | int len; |
| 12677 | PyObject *str; /* temporary string object. */ |
| 12678 | PyObject *result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 12679 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12680 | str = _PyBytes_FormatLong(val, flags, prec, type, &buf, &len); |
| 12681 | if (!str) |
| 12682 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12683 | result = PyUnicode_DecodeASCII(buf, len, NULL); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12684 | Py_DECREF(str); |
| 12685 | return result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 12686 | } |
| 12687 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12688 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12689 | formatchar(Py_UCS4 *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 12690 | size_t buflen, |
| 12691 | PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12692 | { |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 12693 | /* presume that the buffer is at least 3 characters long */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 12694 | if (PyUnicode_Check(v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12695 | if (PyUnicode_GET_LENGTH(v) == 1) { |
| 12696 | buf[0] = PyUnicode_READ_CHAR(v, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12697 | buf[1] = '\0'; |
| 12698 | return 1; |
| 12699 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12700 | goto onError; |
| 12701 | } |
| 12702 | else { |
| 12703 | /* Integer input truncated to a character */ |
| 12704 | long x; |
| 12705 | x = PyLong_AsLong(v); |
| 12706 | if (x == -1 && PyErr_Occurred()) |
| 12707 | goto onError; |
| 12708 | |
| 12709 | if (x < 0 || x > 0x10ffff) { |
| 12710 | PyErr_SetString(PyExc_OverflowError, |
| 12711 | "%c arg not in range(0x110000)"); |
| 12712 | return -1; |
| 12713 | } |
| 12714 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12715 | buf[0] = (Py_UCS4) x; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12716 | buf[1] = '\0'; |
| 12717 | return 1; |
| 12718 | } |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 12719 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12720 | onError: |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 12721 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12722 | "%c requires int or char"); |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 12723 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12724 | } |
| 12725 | |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 12726 | /* fmt%(v1,v2,...) is roughly equivalent to sprintf(fmt, v1, v2, ...) |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 12727 | FORMATBUFLEN is the length of the buffer in which chars are formatted. |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 12728 | */ |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 12729 | #define FORMATBUFLEN (size_t)10 |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 12730 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12731 | PyObject * |
| 12732 | PyUnicode_Format(PyObject *format, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12733 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12734 | void *fmt; |
| 12735 | int fmtkind; |
| 12736 | PyObject *result; |
| 12737 | Py_UCS4 *res, *res0; |
| 12738 | Py_UCS4 max; |
| 12739 | int kind; |
| 12740 | Py_ssize_t fmtcnt, fmtpos, rescnt, reslen, arglen, argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12741 | int args_owned = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12742 | PyObject *dict = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12743 | PyUnicodeObject *uformat; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 12744 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12745 | if (format == NULL || args == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12746 | PyErr_BadInternalCall(); |
| 12747 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12748 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12749 | uformat = (PyUnicodeObject*)PyUnicode_FromObject(format); |
| 12750 | if (uformat == NULL || PyUnicode_READY(uformat) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12751 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12752 | fmt = PyUnicode_DATA(uformat); |
| 12753 | fmtkind = PyUnicode_KIND(uformat); |
| 12754 | fmtcnt = PyUnicode_GET_LENGTH(uformat); |
| 12755 | fmtpos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12756 | |
| 12757 | reslen = rescnt = fmtcnt + 100; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12758 | res = res0 = PyMem_Malloc(reslen * sizeof(Py_UCS4)); |
| 12759 | if (res0 == NULL) { |
| 12760 | PyErr_NoMemory(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12761 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12762 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12763 | |
| 12764 | if (PyTuple_Check(args)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12765 | arglen = PyTuple_Size(args); |
| 12766 | argidx = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12767 | } |
| 12768 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12769 | arglen = -1; |
| 12770 | argidx = -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12771 | } |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 12772 | if (Py_TYPE(args)->tp_as_mapping && !PyTuple_Check(args) && |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 12773 | !PyUnicode_Check(args)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12774 | dict = args; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12775 | |
| 12776 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12777 | if (PyUnicode_READ(fmtkind, fmt, fmtpos) != '%') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12778 | if (--rescnt < 0) { |
| 12779 | rescnt = fmtcnt + 100; |
| 12780 | reslen += rescnt; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12781 | res0 = PyMem_Realloc(res0, reslen*sizeof(Py_UCS4)); |
| 12782 | if (res0 == NULL){ |
| 12783 | PyErr_NoMemory(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12784 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12785 | } |
| 12786 | res = res0 + reslen - rescnt; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12787 | --rescnt; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12788 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12789 | *res++ = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12790 | } |
| 12791 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12792 | /* Got a format specifier */ |
| 12793 | int flags = 0; |
| 12794 | Py_ssize_t width = -1; |
| 12795 | int prec = -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12796 | Py_UCS4 c = '\0'; |
| 12797 | Py_UCS4 fill; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12798 | int isnumok; |
| 12799 | PyObject *v = NULL; |
| 12800 | PyObject *temp = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12801 | void *pbuf; |
| 12802 | Py_ssize_t pindex; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12803 | Py_UNICODE sign; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12804 | Py_ssize_t len, len1; |
| 12805 | Py_UCS4 formatbuf[FORMATBUFLEN]; /* For formatchar() */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12806 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12807 | fmtpos++; |
| 12808 | if (PyUnicode_READ(fmtkind, fmt, fmtpos) == '(') { |
| 12809 | Py_ssize_t keystart; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12810 | Py_ssize_t keylen; |
| 12811 | PyObject *key; |
| 12812 | int pcount = 1; |
Christian Heimes | a612dc0 | 2008-02-24 13:08:18 +0000 | [diff] [blame] | 12813 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12814 | if (dict == NULL) { |
| 12815 | PyErr_SetString(PyExc_TypeError, |
| 12816 | "format requires a mapping"); |
| 12817 | goto onError; |
| 12818 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12819 | ++fmtpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12820 | --fmtcnt; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12821 | keystart = fmtpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12822 | /* Skip over balanced parentheses */ |
| 12823 | while (pcount > 0 && --fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12824 | if (PyUnicode_READ(fmtkind, fmt, fmtpos) == ')') |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12825 | --pcount; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12826 | else if (PyUnicode_READ(fmtkind, fmt, fmtpos) == '(') |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12827 | ++pcount; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12828 | fmtpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12829 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12830 | keylen = fmtpos - keystart - 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12831 | if (fmtcnt < 0 || pcount > 0) { |
| 12832 | PyErr_SetString(PyExc_ValueError, |
| 12833 | "incomplete format key"); |
| 12834 | goto onError; |
| 12835 | } |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 12836 | key = PyUnicode_Substring((PyObject*)uformat, |
| 12837 | keystart, keystart + keylen); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12838 | if (key == NULL) |
| 12839 | goto onError; |
| 12840 | if (args_owned) { |
| 12841 | Py_DECREF(args); |
| 12842 | args_owned = 0; |
| 12843 | } |
| 12844 | args = PyObject_GetItem(dict, key); |
| 12845 | Py_DECREF(key); |
| 12846 | if (args == NULL) { |
| 12847 | goto onError; |
| 12848 | } |
| 12849 | args_owned = 1; |
| 12850 | arglen = -1; |
| 12851 | argidx = -2; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12852 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12853 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12854 | switch (c = PyUnicode_READ(fmtkind, fmt, fmtpos++)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12855 | case '-': flags |= F_LJUST; continue; |
| 12856 | case '+': flags |= F_SIGN; continue; |
| 12857 | case ' ': flags |= F_BLANK; continue; |
| 12858 | case '#': flags |= F_ALT; continue; |
| 12859 | case '0': flags |= F_ZERO; continue; |
| 12860 | } |
| 12861 | break; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12862 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12863 | if (c == '*') { |
| 12864 | v = getnextarg(args, arglen, &argidx); |
| 12865 | if (v == NULL) |
| 12866 | goto onError; |
| 12867 | if (!PyLong_Check(v)) { |
| 12868 | PyErr_SetString(PyExc_TypeError, |
| 12869 | "* wants int"); |
| 12870 | goto onError; |
| 12871 | } |
| 12872 | width = PyLong_AsLong(v); |
| 12873 | if (width == -1 && PyErr_Occurred()) |
| 12874 | goto onError; |
| 12875 | if (width < 0) { |
| 12876 | flags |= F_LJUST; |
| 12877 | width = -width; |
| 12878 | } |
| 12879 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12880 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12881 | } |
| 12882 | else if (c >= '0' && c <= '9') { |
| 12883 | width = c - '0'; |
| 12884 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12885 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12886 | if (c < '0' || c > '9') |
| 12887 | break; |
| 12888 | if ((width*10) / 10 != width) { |
| 12889 | PyErr_SetString(PyExc_ValueError, |
| 12890 | "width too big"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12891 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12892 | } |
| 12893 | width = width*10 + (c - '0'); |
| 12894 | } |
| 12895 | } |
| 12896 | if (c == '.') { |
| 12897 | prec = 0; |
| 12898 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12899 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12900 | if (c == '*') { |
| 12901 | v = getnextarg(args, arglen, &argidx); |
| 12902 | if (v == NULL) |
| 12903 | goto onError; |
| 12904 | if (!PyLong_Check(v)) { |
| 12905 | PyErr_SetString(PyExc_TypeError, |
| 12906 | "* wants int"); |
| 12907 | goto onError; |
| 12908 | } |
| 12909 | prec = PyLong_AsLong(v); |
| 12910 | if (prec == -1 && PyErr_Occurred()) |
| 12911 | goto onError; |
| 12912 | if (prec < 0) |
| 12913 | prec = 0; |
| 12914 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12915 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12916 | } |
| 12917 | else if (c >= '0' && c <= '9') { |
| 12918 | prec = c - '0'; |
| 12919 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12920 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12921 | if (c < '0' || c > '9') |
| 12922 | break; |
| 12923 | if ((prec*10) / 10 != prec) { |
| 12924 | PyErr_SetString(PyExc_ValueError, |
| 12925 | "prec too big"); |
| 12926 | goto onError; |
| 12927 | } |
| 12928 | prec = prec*10 + (c - '0'); |
| 12929 | } |
| 12930 | } |
| 12931 | } /* prec */ |
| 12932 | if (fmtcnt >= 0) { |
| 12933 | if (c == 'h' || c == 'l' || c == 'L') { |
| 12934 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12935 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12936 | } |
| 12937 | } |
| 12938 | if (fmtcnt < 0) { |
| 12939 | PyErr_SetString(PyExc_ValueError, |
| 12940 | "incomplete format"); |
| 12941 | goto onError; |
| 12942 | } |
| 12943 | if (c != '%') { |
| 12944 | v = getnextarg(args, arglen, &argidx); |
| 12945 | if (v == NULL) |
| 12946 | goto onError; |
| 12947 | } |
| 12948 | sign = 0; |
| 12949 | fill = ' '; |
| 12950 | switch (c) { |
| 12951 | |
| 12952 | case '%': |
| 12953 | pbuf = formatbuf; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12954 | kind = PyUnicode_4BYTE_KIND; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12955 | /* presume that buffer length is at least 1 */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12956 | PyUnicode_WRITE(kind, pbuf, 0, '%'); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12957 | len = 1; |
| 12958 | break; |
| 12959 | |
| 12960 | case 's': |
| 12961 | case 'r': |
| 12962 | case 'a': |
Victor Stinner | 808fc0a | 2010-03-22 12:50:40 +0000 | [diff] [blame] | 12963 | if (PyUnicode_CheckExact(v) && c == 's') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12964 | temp = v; |
| 12965 | Py_INCREF(temp); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12966 | } |
| 12967 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12968 | if (c == 's') |
| 12969 | temp = PyObject_Str(v); |
| 12970 | else if (c == 'r') |
| 12971 | temp = PyObject_Repr(v); |
| 12972 | else |
| 12973 | temp = PyObject_ASCII(v); |
| 12974 | if (temp == NULL) |
| 12975 | goto onError; |
| 12976 | if (PyUnicode_Check(temp)) |
| 12977 | /* nothing to do */; |
| 12978 | else { |
| 12979 | Py_DECREF(temp); |
| 12980 | PyErr_SetString(PyExc_TypeError, |
| 12981 | "%s argument has non-string str()"); |
| 12982 | goto onError; |
| 12983 | } |
| 12984 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12985 | if (PyUnicode_READY(temp) == -1) { |
| 12986 | Py_CLEAR(temp); |
| 12987 | goto onError; |
| 12988 | } |
| 12989 | pbuf = PyUnicode_DATA(temp); |
| 12990 | kind = PyUnicode_KIND(temp); |
| 12991 | len = PyUnicode_GET_LENGTH(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12992 | if (prec >= 0 && len > prec) |
| 12993 | len = prec; |
| 12994 | break; |
| 12995 | |
| 12996 | case 'i': |
| 12997 | case 'd': |
| 12998 | case 'u': |
| 12999 | case 'o': |
| 13000 | case 'x': |
| 13001 | case 'X': |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13002 | isnumok = 0; |
| 13003 | if (PyNumber_Check(v)) { |
| 13004 | PyObject *iobj=NULL; |
| 13005 | |
| 13006 | if (PyLong_Check(v)) { |
| 13007 | iobj = v; |
| 13008 | Py_INCREF(iobj); |
| 13009 | } |
| 13010 | else { |
| 13011 | iobj = PyNumber_Long(v); |
| 13012 | } |
| 13013 | if (iobj!=NULL) { |
| 13014 | if (PyLong_Check(iobj)) { |
| 13015 | isnumok = 1; |
Senthil Kumaran | 9ebe08d | 2011-07-03 21:03:16 -0700 | [diff] [blame] | 13016 | temp = formatlong(iobj, flags, prec, (c == 'i'? 'd': c)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13017 | Py_DECREF(iobj); |
| 13018 | if (!temp) |
| 13019 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13020 | if (PyUnicode_READY(temp) == -1) { |
| 13021 | Py_CLEAR(temp); |
| 13022 | goto onError; |
| 13023 | } |
| 13024 | pbuf = PyUnicode_DATA(temp); |
| 13025 | kind = PyUnicode_KIND(temp); |
| 13026 | len = PyUnicode_GET_LENGTH(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13027 | sign = 1; |
| 13028 | } |
| 13029 | else { |
| 13030 | Py_DECREF(iobj); |
| 13031 | } |
| 13032 | } |
| 13033 | } |
| 13034 | if (!isnumok) { |
| 13035 | PyErr_Format(PyExc_TypeError, |
| 13036 | "%%%c format: a number is required, " |
| 13037 | "not %.200s", (char)c, Py_TYPE(v)->tp_name); |
| 13038 | goto onError; |
| 13039 | } |
| 13040 | if (flags & F_ZERO) |
| 13041 | fill = '0'; |
| 13042 | break; |
| 13043 | |
| 13044 | case 'e': |
| 13045 | case 'E': |
| 13046 | case 'f': |
| 13047 | case 'F': |
| 13048 | case 'g': |
| 13049 | case 'G': |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13050 | temp = formatfloat(v, flags, prec, c); |
| 13051 | if (!temp) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13052 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13053 | if (PyUnicode_READY(temp) == -1) { |
| 13054 | Py_CLEAR(temp); |
| 13055 | goto onError; |
| 13056 | } |
| 13057 | pbuf = PyUnicode_DATA(temp); |
| 13058 | kind = PyUnicode_KIND(temp); |
| 13059 | len = PyUnicode_GET_LENGTH(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13060 | sign = 1; |
| 13061 | if (flags & F_ZERO) |
| 13062 | fill = '0'; |
| 13063 | break; |
| 13064 | |
| 13065 | case 'c': |
| 13066 | pbuf = formatbuf; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13067 | kind = PyUnicode_4BYTE_KIND; |
Victor Stinner | b9dcffb | 2011-09-29 00:39:24 +0200 | [diff] [blame] | 13068 | len = formatchar(pbuf, Py_ARRAY_LENGTH(formatbuf), v); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13069 | if (len < 0) |
| 13070 | goto onError; |
| 13071 | break; |
| 13072 | |
| 13073 | default: |
| 13074 | PyErr_Format(PyExc_ValueError, |
| 13075 | "unsupported format character '%c' (0x%x) " |
| 13076 | "at index %zd", |
| 13077 | (31<=c && c<=126) ? (char)c : '?', |
| 13078 | (int)c, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13079 | fmtpos - 1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13080 | goto onError; |
| 13081 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13082 | /* pbuf is initialized here. */ |
| 13083 | pindex = 0; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13084 | if (sign) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13085 | if (PyUnicode_READ(kind, pbuf, pindex) == '-' || |
| 13086 | PyUnicode_READ(kind, pbuf, pindex) == '+') { |
| 13087 | sign = PyUnicode_READ(kind, pbuf, pindex++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13088 | len--; |
| 13089 | } |
| 13090 | else if (flags & F_SIGN) |
| 13091 | sign = '+'; |
| 13092 | else if (flags & F_BLANK) |
| 13093 | sign = ' '; |
| 13094 | else |
| 13095 | sign = 0; |
| 13096 | } |
| 13097 | if (width < len) |
| 13098 | width = len; |
| 13099 | if (rescnt - (sign != 0) < width) { |
| 13100 | reslen -= rescnt; |
| 13101 | rescnt = width + fmtcnt + 100; |
| 13102 | reslen += rescnt; |
| 13103 | if (reslen < 0) { |
| 13104 | Py_XDECREF(temp); |
| 13105 | PyErr_NoMemory(); |
| 13106 | goto onError; |
| 13107 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13108 | res0 = PyMem_Realloc(res0, reslen*sizeof(Py_UCS4)); |
| 13109 | if (res0 == 0) { |
| 13110 | PyErr_NoMemory(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13111 | Py_XDECREF(temp); |
| 13112 | goto onError; |
| 13113 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13114 | res = res0 + reslen - rescnt; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13115 | } |
| 13116 | if (sign) { |
| 13117 | if (fill != ' ') |
| 13118 | *res++ = sign; |
| 13119 | rescnt--; |
| 13120 | if (width > len) |
| 13121 | width--; |
| 13122 | } |
| 13123 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13124 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
| 13125 | assert(PyUnicode_READ(kind, pbuf, pindex+1) == c); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13126 | if (fill != ' ') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13127 | *res++ = PyUnicode_READ(kind, pbuf, pindex++); |
| 13128 | *res++ = PyUnicode_READ(kind, pbuf, pindex++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13129 | } |
| 13130 | rescnt -= 2; |
| 13131 | width -= 2; |
| 13132 | if (width < 0) |
| 13133 | width = 0; |
| 13134 | len -= 2; |
| 13135 | } |
| 13136 | if (width > len && !(flags & F_LJUST)) { |
| 13137 | do { |
| 13138 | --rescnt; |
| 13139 | *res++ = fill; |
| 13140 | } while (--width > len); |
| 13141 | } |
| 13142 | if (fill == ' ') { |
| 13143 | if (sign) |
| 13144 | *res++ = sign; |
| 13145 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13146 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
| 13147 | assert(PyUnicode_READ(kind, pbuf, pindex+1) == c); |
| 13148 | *res++ = PyUnicode_READ(kind, pbuf, pindex++); |
| 13149 | *res++ = PyUnicode_READ(kind, pbuf, pindex++); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13150 | } |
| 13151 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13152 | /* Copy all characters, preserving len */ |
| 13153 | len1 = len; |
| 13154 | while (len1--) { |
| 13155 | *res++ = PyUnicode_READ(kind, pbuf, pindex++); |
| 13156 | rescnt--; |
| 13157 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13158 | while (--width >= len) { |
| 13159 | --rescnt; |
| 13160 | *res++ = ' '; |
| 13161 | } |
| 13162 | if (dict && (argidx < arglen) && c != '%') { |
| 13163 | PyErr_SetString(PyExc_TypeError, |
| 13164 | "not all arguments converted during string formatting"); |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 13165 | Py_XDECREF(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13166 | goto onError; |
| 13167 | } |
| 13168 | Py_XDECREF(temp); |
| 13169 | } /* '%' */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13170 | } /* until end */ |
| 13171 | if (argidx < arglen && !dict) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13172 | PyErr_SetString(PyExc_TypeError, |
| 13173 | "not all arguments converted during string formatting"); |
| 13174 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13175 | } |
| 13176 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13177 | |
| 13178 | for (max=0, res = res0; res < res0+reslen-rescnt; res++) |
| 13179 | if (*res > max) |
| 13180 | max = *res; |
| 13181 | result = PyUnicode_New(reslen - rescnt, max); |
| 13182 | if (!result) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13183 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13184 | kind = PyUnicode_KIND(result); |
| 13185 | for (res = res0; res < res0+reslen-rescnt; res++) |
| 13186 | PyUnicode_WRITE(kind, PyUnicode_DATA(result), res-res0, *res); |
| 13187 | PyMem_Free(res0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13188 | if (args_owned) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13189 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13190 | } |
| 13191 | Py_DECREF(uformat); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13192 | assert(_PyUnicode_CheckConsistency(result, 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13193 | return (PyObject *)result; |
| 13194 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13195 | onError: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13196 | PyMem_Free(res0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13197 | Py_DECREF(uformat); |
| 13198 | if (args_owned) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13199 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13200 | } |
| 13201 | return NULL; |
| 13202 | } |
| 13203 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 13204 | static PyObject * |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 13205 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 13206 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13207 | static PyObject * |
| 13208 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 13209 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13210 | PyObject *x = NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13211 | static char *kwlist[] = {"object", "encoding", "errors", 0}; |
| 13212 | char *encoding = NULL; |
| 13213 | char *errors = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13214 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13215 | if (type != &PyUnicode_Type) |
| 13216 | return unicode_subtype_new(type, args, kwds); |
| 13217 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:str", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13218 | kwlist, &x, &encoding, &errors)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13219 | return NULL; |
| 13220 | if (x == NULL) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13221 | return (PyObject *)PyUnicode_New(0, 0); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13222 | if (encoding == NULL && errors == NULL) |
| 13223 | return PyObject_Str(x); |
| 13224 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13225 | return PyUnicode_FromEncodedObject(x, encoding, errors); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13226 | } |
| 13227 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 13228 | static PyObject * |
| 13229 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 13230 | { |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13231 | PyUnicodeObject *unicode, *self; |
| 13232 | Py_ssize_t length, char_size; |
| 13233 | int share_wstr, share_utf8; |
| 13234 | unsigned int kind; |
| 13235 | void *data; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 13236 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13237 | assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13238 | |
| 13239 | unicode = (PyUnicodeObject *)unicode_new(&PyUnicode_Type, args, kwds); |
| 13240 | if (unicode == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13241 | return NULL; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 13242 | assert(_PyUnicode_CHECK(unicode)); |
Victor Stinner | e06e145 | 2011-10-04 20:52:31 +0200 | [diff] [blame] | 13243 | if (PyUnicode_READY(unicode)) |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13244 | return NULL; |
| 13245 | |
| 13246 | self = (PyUnicodeObject *) type->tp_alloc(type, 0); |
| 13247 | if (self == NULL) { |
| 13248 | Py_DECREF(unicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13249 | return NULL; |
| 13250 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13251 | kind = PyUnicode_KIND(unicode); |
| 13252 | length = PyUnicode_GET_LENGTH(unicode); |
| 13253 | |
| 13254 | _PyUnicode_LENGTH(self) = length; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 13255 | #ifdef Py_DEBUG |
| 13256 | _PyUnicode_HASH(self) = -1; |
| 13257 | #else |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13258 | _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 13259 | #endif |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13260 | _PyUnicode_STATE(self).interned = 0; |
| 13261 | _PyUnicode_STATE(self).kind = kind; |
| 13262 | _PyUnicode_STATE(self).compact = 0; |
Victor Stinner | 3cf4637 | 2011-10-03 14:42:15 +0200 | [diff] [blame] | 13263 | _PyUnicode_STATE(self).ascii = _PyUnicode_STATE(unicode).ascii; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13264 | _PyUnicode_STATE(self).ready = 1; |
| 13265 | _PyUnicode_WSTR(self) = NULL; |
| 13266 | _PyUnicode_UTF8_LENGTH(self) = 0; |
| 13267 | _PyUnicode_UTF8(self) = NULL; |
| 13268 | _PyUnicode_WSTR_LENGTH(self) = 0; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 13269 | _PyUnicode_DATA_ANY(self) = NULL; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13270 | |
| 13271 | share_utf8 = 0; |
| 13272 | share_wstr = 0; |
| 13273 | if (kind == PyUnicode_1BYTE_KIND) { |
| 13274 | char_size = 1; |
| 13275 | if (PyUnicode_MAX_CHAR_VALUE(unicode) < 128) |
| 13276 | share_utf8 = 1; |
| 13277 | } |
| 13278 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 13279 | char_size = 2; |
| 13280 | if (sizeof(wchar_t) == 2) |
| 13281 | share_wstr = 1; |
| 13282 | } |
| 13283 | else { |
| 13284 | assert(kind == PyUnicode_4BYTE_KIND); |
| 13285 | char_size = 4; |
| 13286 | if (sizeof(wchar_t) == 4) |
| 13287 | share_wstr = 1; |
| 13288 | } |
| 13289 | |
| 13290 | /* Ensure we won't overflow the length. */ |
| 13291 | if (length > (PY_SSIZE_T_MAX / char_size - 1)) { |
| 13292 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13293 | goto onError; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13294 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13295 | data = PyObject_MALLOC((length + 1) * char_size); |
| 13296 | if (data == NULL) { |
| 13297 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13298 | goto onError; |
| 13299 | } |
| 13300 | |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 13301 | _PyUnicode_DATA_ANY(self) = data; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13302 | if (share_utf8) { |
| 13303 | _PyUnicode_UTF8_LENGTH(self) = length; |
| 13304 | _PyUnicode_UTF8(self) = data; |
| 13305 | } |
| 13306 | if (share_wstr) { |
| 13307 | _PyUnicode_WSTR_LENGTH(self) = length; |
| 13308 | _PyUnicode_WSTR(self) = (wchar_t *)data; |
| 13309 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13310 | |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13311 | Py_MEMCPY(data, PyUnicode_DATA(unicode), |
| 13312 | PyUnicode_KIND_SIZE(kind, length + 1)); |
| 13313 | Py_DECREF(unicode); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13314 | assert(_PyUnicode_CheckConsistency(self, 1)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 13315 | #ifdef Py_DEBUG |
| 13316 | _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode); |
| 13317 | #endif |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13318 | return (PyObject *)self; |
| 13319 | |
| 13320 | onError: |
| 13321 | Py_DECREF(unicode); |
| 13322 | Py_DECREF(self); |
| 13323 | return NULL; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 13324 | } |
| 13325 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 13326 | PyDoc_STRVAR(unicode_doc, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13327 | "str(string[, encoding[, errors]]) -> str\n\ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13328 | \n\ |
Collin Winter | d474ce8 | 2007-08-07 19:42:11 +0000 | [diff] [blame] | 13329 | Create a new string object from the given encoded string.\n\ |
Skip Montanaro | 35b37a5 | 2002-07-26 16:22:46 +0000 | [diff] [blame] | 13330 | encoding defaults to the current default string encoding.\n\ |
| 13331 | errors can be 'strict', 'replace' or 'ignore' and defaults to 'strict'."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13332 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13333 | static PyObject *unicode_iter(PyObject *seq); |
| 13334 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13335 | PyTypeObject PyUnicode_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 13336 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13337 | "str", /* tp_name */ |
| 13338 | sizeof(PyUnicodeObject), /* tp_size */ |
| 13339 | 0, /* tp_itemsize */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13340 | /* Slots */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13341 | (destructor)unicode_dealloc, /* tp_dealloc */ |
| 13342 | 0, /* tp_print */ |
| 13343 | 0, /* tp_getattr */ |
| 13344 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 13345 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13346 | unicode_repr, /* tp_repr */ |
| 13347 | &unicode_as_number, /* tp_as_number */ |
| 13348 | &unicode_as_sequence, /* tp_as_sequence */ |
| 13349 | &unicode_as_mapping, /* tp_as_mapping */ |
| 13350 | (hashfunc) unicode_hash, /* tp_hash*/ |
| 13351 | 0, /* tp_call*/ |
| 13352 | (reprfunc) unicode_str, /* tp_str */ |
| 13353 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 13354 | 0, /* tp_setattro */ |
| 13355 | 0, /* tp_as_buffer */ |
| 13356 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13357 | Py_TPFLAGS_UNICODE_SUBCLASS, /* tp_flags */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13358 | unicode_doc, /* tp_doc */ |
| 13359 | 0, /* tp_traverse */ |
| 13360 | 0, /* tp_clear */ |
| 13361 | PyUnicode_RichCompare, /* tp_richcompare */ |
| 13362 | 0, /* tp_weaklistoffset */ |
| 13363 | unicode_iter, /* tp_iter */ |
| 13364 | 0, /* tp_iternext */ |
| 13365 | unicode_methods, /* tp_methods */ |
| 13366 | 0, /* tp_members */ |
| 13367 | 0, /* tp_getset */ |
| 13368 | &PyBaseObject_Type, /* tp_base */ |
| 13369 | 0, /* tp_dict */ |
| 13370 | 0, /* tp_descr_get */ |
| 13371 | 0, /* tp_descr_set */ |
| 13372 | 0, /* tp_dictoffset */ |
| 13373 | 0, /* tp_init */ |
| 13374 | 0, /* tp_alloc */ |
| 13375 | unicode_new, /* tp_new */ |
| 13376 | PyObject_Del, /* tp_free */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13377 | }; |
| 13378 | |
| 13379 | /* Initialize the Unicode implementation */ |
| 13380 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 13381 | void _PyUnicode_Init(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13382 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 13383 | int i; |
| 13384 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13385 | /* XXX - move this array to unicodectype.c ? */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13386 | Py_UCS2 linebreak[] = { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13387 | 0x000A, /* LINE FEED */ |
| 13388 | 0x000D, /* CARRIAGE RETURN */ |
| 13389 | 0x001C, /* FILE SEPARATOR */ |
| 13390 | 0x001D, /* GROUP SEPARATOR */ |
| 13391 | 0x001E, /* RECORD SEPARATOR */ |
| 13392 | 0x0085, /* NEXT LINE */ |
| 13393 | 0x2028, /* LINE SEPARATOR */ |
| 13394 | 0x2029, /* PARAGRAPH SEPARATOR */ |
| 13395 | }; |
| 13396 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 13397 | /* Init the implementation */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 13398 | unicode_empty = PyUnicode_New(0, 0); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13399 | assert(_PyUnicode_CheckConsistency(unicode_empty, 1)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13400 | if (!unicode_empty) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13401 | Py_FatalError("Can't create empty string"); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13402 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 13403 | for (i = 0; i < 256; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13404 | unicode_latin1[i] = NULL; |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 13405 | if (PyType_Ready(&PyUnicode_Type) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13406 | Py_FatalError("Can't initialize 'unicode'"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13407 | |
| 13408 | /* initialize the linebreak bloom filter */ |
| 13409 | bloom_linebreak = make_bloom_mask( |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13410 | PyUnicode_2BYTE_KIND, linebreak, |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 13411 | Py_ARRAY_LENGTH(linebreak)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13412 | |
| 13413 | PyType_Ready(&EncodingMapType); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13414 | } |
| 13415 | |
| 13416 | /* Finalize the Unicode implementation */ |
| 13417 | |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 13418 | int |
| 13419 | PyUnicode_ClearFreeList(void) |
| 13420 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13421 | return 0; |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 13422 | } |
| 13423 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13424 | void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 13425 | _PyUnicode_Fini(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13426 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 13427 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13428 | |
Guido van Rossum | 4ae8ef8 | 2000-10-03 18:09:04 +0000 | [diff] [blame] | 13429 | Py_XDECREF(unicode_empty); |
| 13430 | unicode_empty = NULL; |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 13431 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 13432 | for (i = 0; i < 256; i++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13433 | if (unicode_latin1[i]) { |
| 13434 | Py_DECREF(unicode_latin1[i]); |
| 13435 | unicode_latin1[i] = NULL; |
| 13436 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 13437 | } |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 13438 | (void)PyUnicode_ClearFreeList(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13439 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 13440 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13441 | void |
| 13442 | PyUnicode_InternInPlace(PyObject **p) |
| 13443 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13444 | register PyUnicodeObject *s = (PyUnicodeObject *)(*p); |
| 13445 | PyObject *t; |
Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 13446 | #ifdef Py_DEBUG |
| 13447 | assert(s != NULL); |
| 13448 | assert(_PyUnicode_CHECK(s)); |
| 13449 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13450 | if (s == NULL || !PyUnicode_Check(s)) |
Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 13451 | return; |
| 13452 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13453 | /* If it's a subclass, we don't really know what putting |
| 13454 | it in the interned dict might do. */ |
| 13455 | if (!PyUnicode_CheckExact(s)) |
| 13456 | return; |
| 13457 | if (PyUnicode_CHECK_INTERNED(s)) |
| 13458 | return; |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 13459 | if (_PyUnicode_READY_REPLACE(p)) { |
Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 13460 | assert(0 && "_PyUnicode_READY_REPLACE fail in PyUnicode_InternInPlace"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13461 | return; |
| 13462 | } |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 13463 | s = (PyUnicodeObject *)(*p); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13464 | if (interned == NULL) { |
| 13465 | interned = PyDict_New(); |
| 13466 | if (interned == NULL) { |
| 13467 | PyErr_Clear(); /* Don't leave an exception */ |
| 13468 | return; |
| 13469 | } |
| 13470 | } |
| 13471 | /* It might be that the GetItem call fails even |
| 13472 | though the key is present in the dictionary, |
| 13473 | namely when this happens during a stack overflow. */ |
| 13474 | Py_ALLOW_RECURSION |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13475 | t = PyDict_GetItem(interned, (PyObject *)s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13476 | Py_END_ALLOW_RECURSION |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 13477 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13478 | if (t) { |
| 13479 | Py_INCREF(t); |
| 13480 | Py_DECREF(*p); |
| 13481 | *p = t; |
| 13482 | return; |
| 13483 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13484 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13485 | PyThreadState_GET()->recursion_critical = 1; |
| 13486 | if (PyDict_SetItem(interned, (PyObject *)s, (PyObject *)s) < 0) { |
| 13487 | PyErr_Clear(); |
| 13488 | PyThreadState_GET()->recursion_critical = 0; |
| 13489 | return; |
| 13490 | } |
| 13491 | PyThreadState_GET()->recursion_critical = 0; |
| 13492 | /* The two references in interned are not counted by refcnt. |
| 13493 | The deallocator will take care of this */ |
| 13494 | Py_REFCNT(s) -= 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13495 | _PyUnicode_STATE(s).interned = SSTATE_INTERNED_MORTAL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13496 | } |
| 13497 | |
| 13498 | void |
| 13499 | PyUnicode_InternImmortal(PyObject **p) |
| 13500 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13501 | PyUnicodeObject *u = (PyUnicodeObject *)*p; |
| 13502 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13503 | PyUnicode_InternInPlace(p); |
| 13504 | if (PyUnicode_CHECK_INTERNED(*p) != SSTATE_INTERNED_IMMORTAL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13505 | _PyUnicode_STATE(u).interned = SSTATE_INTERNED_IMMORTAL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13506 | Py_INCREF(*p); |
| 13507 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13508 | } |
| 13509 | |
| 13510 | PyObject * |
| 13511 | PyUnicode_InternFromString(const char *cp) |
| 13512 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13513 | PyObject *s = PyUnicode_FromString(cp); |
| 13514 | if (s == NULL) |
| 13515 | return NULL; |
| 13516 | PyUnicode_InternInPlace(&s); |
| 13517 | return s; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13518 | } |
| 13519 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 13520 | void |
| 13521 | _Py_ReleaseInternedUnicodeStrings(void) |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13522 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13523 | PyObject *keys; |
| 13524 | PyUnicodeObject *s; |
| 13525 | Py_ssize_t i, n; |
| 13526 | Py_ssize_t immortal_size = 0, mortal_size = 0; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13527 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13528 | if (interned == NULL || !PyDict_Check(interned)) |
| 13529 | return; |
| 13530 | keys = PyDict_Keys(interned); |
| 13531 | if (keys == NULL || !PyList_Check(keys)) { |
| 13532 | PyErr_Clear(); |
| 13533 | return; |
| 13534 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13535 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13536 | /* Since _Py_ReleaseInternedUnicodeStrings() is intended to help a leak |
| 13537 | detector, interned unicode strings are not forcibly deallocated; |
| 13538 | rather, we give them their stolen references back, and then clear |
| 13539 | and DECREF the interned dict. */ |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13540 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13541 | n = PyList_GET_SIZE(keys); |
| 13542 | fprintf(stderr, "releasing %" PY_FORMAT_SIZE_T "d interned strings\n", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13543 | n); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13544 | for (i = 0; i < n; i++) { |
| 13545 | s = (PyUnicodeObject *) PyList_GET_ITEM(keys, i); |
Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 13546 | if (PyUnicode_READY(s) == -1) { |
| 13547 | assert(0 && "could not ready string"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13548 | fprintf(stderr, "could not ready string\n"); |
Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 13549 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13550 | switch (PyUnicode_CHECK_INTERNED(s)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13551 | case SSTATE_NOT_INTERNED: |
| 13552 | /* XXX Shouldn't happen */ |
| 13553 | break; |
| 13554 | case SSTATE_INTERNED_IMMORTAL: |
| 13555 | Py_REFCNT(s) += 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13556 | immortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13557 | break; |
| 13558 | case SSTATE_INTERNED_MORTAL: |
| 13559 | Py_REFCNT(s) += 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13560 | mortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13561 | break; |
| 13562 | default: |
| 13563 | Py_FatalError("Inconsistent interned string state."); |
| 13564 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13565 | _PyUnicode_STATE(s).interned = SSTATE_NOT_INTERNED; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13566 | } |
| 13567 | fprintf(stderr, "total size of all interned strings: " |
| 13568 | "%" PY_FORMAT_SIZE_T "d/%" PY_FORMAT_SIZE_T "d " |
| 13569 | "mortal/immortal\n", mortal_size, immortal_size); |
| 13570 | Py_DECREF(keys); |
| 13571 | PyDict_Clear(interned); |
| 13572 | Py_DECREF(interned); |
| 13573 | interned = NULL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13574 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13575 | |
| 13576 | |
| 13577 | /********************* Unicode Iterator **************************/ |
| 13578 | |
| 13579 | typedef struct { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13580 | PyObject_HEAD |
| 13581 | Py_ssize_t it_index; |
| 13582 | PyUnicodeObject *it_seq; /* Set to NULL when iterator is exhausted */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13583 | } unicodeiterobject; |
| 13584 | |
| 13585 | static void |
| 13586 | unicodeiter_dealloc(unicodeiterobject *it) |
| 13587 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13588 | _PyObject_GC_UNTRACK(it); |
| 13589 | Py_XDECREF(it->it_seq); |
| 13590 | PyObject_GC_Del(it); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13591 | } |
| 13592 | |
| 13593 | static int |
| 13594 | unicodeiter_traverse(unicodeiterobject *it, visitproc visit, void *arg) |
| 13595 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13596 | Py_VISIT(it->it_seq); |
| 13597 | return 0; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13598 | } |
| 13599 | |
| 13600 | static PyObject * |
| 13601 | unicodeiter_next(unicodeiterobject *it) |
| 13602 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13603 | PyUnicodeObject *seq; |
| 13604 | PyObject *item; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13605 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13606 | assert(it != NULL); |
| 13607 | seq = it->it_seq; |
| 13608 | if (seq == NULL) |
| 13609 | return NULL; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 13610 | assert(_PyUnicode_CHECK(seq)); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13611 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13612 | if (it->it_index < PyUnicode_GET_LENGTH(seq)) { |
| 13613 | int kind = PyUnicode_KIND(seq); |
| 13614 | void *data = PyUnicode_DATA(seq); |
| 13615 | Py_UCS4 chr = PyUnicode_READ(kind, data, it->it_index); |
| 13616 | item = PyUnicode_FromOrdinal(chr); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13617 | if (item != NULL) |
| 13618 | ++it->it_index; |
| 13619 | return item; |
| 13620 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13621 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13622 | Py_DECREF(seq); |
| 13623 | it->it_seq = NULL; |
| 13624 | return NULL; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13625 | } |
| 13626 | |
| 13627 | static PyObject * |
| 13628 | unicodeiter_len(unicodeiterobject *it) |
| 13629 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13630 | Py_ssize_t len = 0; |
| 13631 | if (it->it_seq) |
| 13632 | len = PyUnicode_GET_SIZE(it->it_seq) - it->it_index; |
| 13633 | return PyLong_FromSsize_t(len); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13634 | } |
| 13635 | |
| 13636 | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
| 13637 | |
| 13638 | static PyMethodDef unicodeiter_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13639 | {"__length_hint__", (PyCFunction)unicodeiter_len, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13640 | length_hint_doc}, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13641 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13642 | }; |
| 13643 | |
| 13644 | PyTypeObject PyUnicodeIter_Type = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13645 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 13646 | "str_iterator", /* tp_name */ |
| 13647 | sizeof(unicodeiterobject), /* tp_basicsize */ |
| 13648 | 0, /* tp_itemsize */ |
| 13649 | /* methods */ |
| 13650 | (destructor)unicodeiter_dealloc, /* tp_dealloc */ |
| 13651 | 0, /* tp_print */ |
| 13652 | 0, /* tp_getattr */ |
| 13653 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 13654 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13655 | 0, /* tp_repr */ |
| 13656 | 0, /* tp_as_number */ |
| 13657 | 0, /* tp_as_sequence */ |
| 13658 | 0, /* tp_as_mapping */ |
| 13659 | 0, /* tp_hash */ |
| 13660 | 0, /* tp_call */ |
| 13661 | 0, /* tp_str */ |
| 13662 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 13663 | 0, /* tp_setattro */ |
| 13664 | 0, /* tp_as_buffer */ |
| 13665 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 13666 | 0, /* tp_doc */ |
| 13667 | (traverseproc)unicodeiter_traverse, /* tp_traverse */ |
| 13668 | 0, /* tp_clear */ |
| 13669 | 0, /* tp_richcompare */ |
| 13670 | 0, /* tp_weaklistoffset */ |
| 13671 | PyObject_SelfIter, /* tp_iter */ |
| 13672 | (iternextfunc)unicodeiter_next, /* tp_iternext */ |
| 13673 | unicodeiter_methods, /* tp_methods */ |
| 13674 | 0, |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13675 | }; |
| 13676 | |
| 13677 | static PyObject * |
| 13678 | unicode_iter(PyObject *seq) |
| 13679 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13680 | unicodeiterobject *it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13681 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13682 | if (!PyUnicode_Check(seq)) { |
| 13683 | PyErr_BadInternalCall(); |
| 13684 | return NULL; |
| 13685 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13686 | if (PyUnicode_READY(seq) == -1) |
| 13687 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13688 | it = PyObject_GC_New(unicodeiterobject, &PyUnicodeIter_Type); |
| 13689 | if (it == NULL) |
| 13690 | return NULL; |
| 13691 | it->it_index = 0; |
| 13692 | Py_INCREF(seq); |
| 13693 | it->it_seq = (PyUnicodeObject *)seq; |
| 13694 | _PyObject_GC_TRACK(it); |
| 13695 | return (PyObject *)it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13696 | } |
| 13697 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13698 | #define UNIOP(x) Py_UNICODE_##x |
| 13699 | #define UNIOP_t Py_UNICODE |
| 13700 | #include "uniops.h" |
| 13701 | #undef UNIOP |
| 13702 | #undef UNIOP_t |
| 13703 | #define UNIOP(x) Py_UCS4_##x |
| 13704 | #define UNIOP_t Py_UCS4 |
| 13705 | #include "uniops.h" |
| 13706 | #undef UNIOP |
| 13707 | #undef UNIOP_t |
Victor Stinner | 331ea92 | 2010-08-10 16:37:20 +0000 | [diff] [blame] | 13708 | |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 13709 | Py_UNICODE* |
Victor Stinner | 4640860 | 2010-09-03 16:18:00 +0000 | [diff] [blame] | 13710 | PyUnicode_AsUnicodeCopy(PyObject *object) |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 13711 | { |
| 13712 | PyUnicodeObject *unicode = (PyUnicodeObject *)object; |
| 13713 | Py_UNICODE *copy; |
| 13714 | Py_ssize_t size; |
| 13715 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13716 | if (!PyUnicode_Check(unicode)) { |
| 13717 | PyErr_BadArgument(); |
| 13718 | return NULL; |
| 13719 | } |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 13720 | /* Ensure we won't overflow the size. */ |
| 13721 | if (PyUnicode_GET_SIZE(unicode) > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
| 13722 | PyErr_NoMemory(); |
| 13723 | return NULL; |
| 13724 | } |
| 13725 | size = PyUnicode_GET_SIZE(unicode) + 1; /* copy the nul character */ |
| 13726 | size *= sizeof(Py_UNICODE); |
| 13727 | copy = PyMem_Malloc(size); |
| 13728 | if (copy == NULL) { |
| 13729 | PyErr_NoMemory(); |
| 13730 | return NULL; |
| 13731 | } |
| 13732 | memcpy(copy, PyUnicode_AS_UNICODE(unicode), size); |
| 13733 | return copy; |
| 13734 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 13735 | |
Georg Brandl | 66c221e | 2010-10-14 07:04:07 +0000 | [diff] [blame] | 13736 | /* A _string module, to export formatter_parser and formatter_field_name_split |
| 13737 | to the string.Formatter class implemented in Python. */ |
| 13738 | |
| 13739 | static PyMethodDef _string_methods[] = { |
| 13740 | {"formatter_field_name_split", (PyCFunction) formatter_field_name_split, |
| 13741 | METH_O, PyDoc_STR("split the argument as a field name")}, |
| 13742 | {"formatter_parser", (PyCFunction) formatter_parser, |
| 13743 | METH_O, PyDoc_STR("parse the argument as a format string")}, |
| 13744 | {NULL, NULL} |
| 13745 | }; |
| 13746 | |
| 13747 | static struct PyModuleDef _string_module = { |
| 13748 | PyModuleDef_HEAD_INIT, |
| 13749 | "_string", |
| 13750 | PyDoc_STR("string helper module"), |
| 13751 | 0, |
| 13752 | _string_methods, |
| 13753 | NULL, |
| 13754 | NULL, |
| 13755 | NULL, |
| 13756 | NULL |
| 13757 | }; |
| 13758 | |
| 13759 | PyMODINIT_FUNC |
| 13760 | PyInit__string(void) |
| 13761 | { |
| 13762 | return PyModule_Create(&_string_module); |
| 13763 | } |
| 13764 | |
| 13765 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 13766 | #ifdef __cplusplus |
| 13767 | } |
| 13768 | #endif |