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 { |
| 1783 | assert(kind == PyUnicode_4BYTE_KIND); |
| 1784 | const Py_UCS4 *u = PyUnicode_4BYTE_DATA(unicode); |
| 1785 | max_char = 0; |
| 1786 | for (i = 0; i < len; i++) { |
| 1787 | if (u[i] > max_char) { |
| 1788 | max_char = u[i]; |
| 1789 | if (max_char >= 0x10000) |
| 1790 | return; |
| 1791 | } |
| 1792 | } |
| 1793 | } |
| 1794 | assert(max_char > PyUnicode_MAX_CHAR_VALUE(unicode)); |
| 1795 | copy = PyUnicode_New(len, max_char); |
| 1796 | copy_characters(copy, 0, unicode, 0, len); |
| 1797 | Py_DECREF(unicode); |
| 1798 | *p_unicode = copy; |
| 1799 | } |
| 1800 | |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 1801 | PyObject* |
| 1802 | PyUnicode_Copy(PyObject *unicode) |
| 1803 | { |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 1804 | Py_ssize_t size; |
| 1805 | PyObject *copy; |
| 1806 | void *data; |
| 1807 | |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 1808 | if (!PyUnicode_Check(unicode)) { |
| 1809 | PyErr_BadInternalCall(); |
| 1810 | return NULL; |
| 1811 | } |
| 1812 | if (PyUnicode_READY(unicode)) |
| 1813 | return NULL; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 1814 | |
| 1815 | size = PyUnicode_GET_LENGTH(unicode); |
| 1816 | copy = PyUnicode_New(size, PyUnicode_MAX_CHAR_VALUE(unicode)); |
| 1817 | if (!copy) |
| 1818 | return NULL; |
| 1819 | assert(PyUnicode_KIND(copy) == PyUnicode_KIND(unicode)); |
| 1820 | |
| 1821 | data = PyUnicode_DATA(unicode); |
| 1822 | switch (PyUnicode_KIND(unicode)) |
| 1823 | { |
| 1824 | case PyUnicode_1BYTE_KIND: |
| 1825 | memcpy(PyUnicode_1BYTE_DATA(copy), data, size); |
| 1826 | break; |
| 1827 | case PyUnicode_2BYTE_KIND: |
| 1828 | memcpy(PyUnicode_2BYTE_DATA(copy), data, sizeof(Py_UCS2) * size); |
| 1829 | break; |
| 1830 | case PyUnicode_4BYTE_KIND: |
| 1831 | memcpy(PyUnicode_4BYTE_DATA(copy), data, sizeof(Py_UCS4) * size); |
| 1832 | break; |
| 1833 | default: |
| 1834 | assert(0); |
| 1835 | break; |
| 1836 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1837 | assert(_PyUnicode_CheckConsistency(copy, 1)); |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 1838 | return copy; |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 1839 | } |
| 1840 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1841 | |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1842 | /* Widen Unicode objects to larger buffers. Don't write terminating null |
| 1843 | character. Return NULL on error. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1844 | |
| 1845 | void* |
| 1846 | _PyUnicode_AsKind(PyObject *s, unsigned int kind) |
| 1847 | { |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1848 | Py_ssize_t len; |
| 1849 | void *result; |
| 1850 | unsigned int skind; |
| 1851 | |
| 1852 | if (PyUnicode_READY(s)) |
| 1853 | return NULL; |
| 1854 | |
| 1855 | len = PyUnicode_GET_LENGTH(s); |
| 1856 | skind = PyUnicode_KIND(s); |
| 1857 | if (skind >= kind) { |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 1858 | PyErr_SetString(PyExc_SystemError, "invalid widening attempt"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1859 | return NULL; |
| 1860 | } |
| 1861 | switch(kind) { |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1862 | case PyUnicode_2BYTE_KIND: |
| 1863 | result = PyMem_Malloc(len * sizeof(Py_UCS2)); |
| 1864 | if (!result) |
| 1865 | return PyErr_NoMemory(); |
| 1866 | assert(skind == PyUnicode_1BYTE_KIND); |
| 1867 | _PyUnicode_CONVERT_BYTES( |
| 1868 | Py_UCS1, Py_UCS2, |
| 1869 | PyUnicode_1BYTE_DATA(s), |
| 1870 | PyUnicode_1BYTE_DATA(s) + len, |
| 1871 | result); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1872 | return result; |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1873 | case PyUnicode_4BYTE_KIND: |
| 1874 | result = PyMem_Malloc(len * sizeof(Py_UCS4)); |
| 1875 | if (!result) |
| 1876 | return PyErr_NoMemory(); |
| 1877 | if (skind == PyUnicode_2BYTE_KIND) { |
| 1878 | _PyUnicode_CONVERT_BYTES( |
| 1879 | Py_UCS2, Py_UCS4, |
| 1880 | PyUnicode_2BYTE_DATA(s), |
| 1881 | PyUnicode_2BYTE_DATA(s) + len, |
| 1882 | result); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1883 | } |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1884 | else { |
| 1885 | assert(skind == PyUnicode_1BYTE_KIND); |
| 1886 | _PyUnicode_CONVERT_BYTES( |
| 1887 | Py_UCS1, Py_UCS4, |
| 1888 | PyUnicode_1BYTE_DATA(s), |
| 1889 | PyUnicode_1BYTE_DATA(s) + len, |
| 1890 | result); |
| 1891 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1892 | return result; |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1893 | default: |
| 1894 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1895 | } |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 1896 | PyErr_SetString(PyExc_SystemError, "invalid kind"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1897 | return NULL; |
| 1898 | } |
| 1899 | |
| 1900 | static Py_UCS4* |
| 1901 | as_ucs4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, |
| 1902 | int copy_null) |
| 1903 | { |
| 1904 | int kind; |
| 1905 | void *data; |
| 1906 | Py_ssize_t len, targetlen; |
| 1907 | if (PyUnicode_READY(string) == -1) |
| 1908 | return NULL; |
| 1909 | kind = PyUnicode_KIND(string); |
| 1910 | data = PyUnicode_DATA(string); |
| 1911 | len = PyUnicode_GET_LENGTH(string); |
| 1912 | targetlen = len; |
| 1913 | if (copy_null) |
| 1914 | targetlen++; |
| 1915 | if (!target) { |
| 1916 | if (PY_SSIZE_T_MAX / sizeof(Py_UCS4) < targetlen) { |
| 1917 | PyErr_NoMemory(); |
| 1918 | return NULL; |
| 1919 | } |
| 1920 | target = PyMem_Malloc(targetlen * sizeof(Py_UCS4)); |
| 1921 | if (!target) { |
| 1922 | PyErr_NoMemory(); |
| 1923 | return NULL; |
| 1924 | } |
| 1925 | } |
| 1926 | else { |
| 1927 | if (targetsize < targetlen) { |
| 1928 | PyErr_Format(PyExc_SystemError, |
| 1929 | "string is longer than the buffer"); |
| 1930 | if (copy_null && 0 < targetsize) |
| 1931 | target[0] = 0; |
| 1932 | return NULL; |
| 1933 | } |
| 1934 | } |
| 1935 | if (kind != PyUnicode_4BYTE_KIND) { |
| 1936 | Py_ssize_t i; |
| 1937 | for (i = 0; i < len; i++) |
| 1938 | target[i] = PyUnicode_READ(kind, data, i); |
| 1939 | } |
| 1940 | else |
| 1941 | Py_MEMCPY(target, data, len * sizeof(Py_UCS4)); |
| 1942 | if (copy_null) |
| 1943 | target[len] = 0; |
| 1944 | return target; |
| 1945 | } |
| 1946 | |
| 1947 | Py_UCS4* |
| 1948 | PyUnicode_AsUCS4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, |
| 1949 | int copy_null) |
| 1950 | { |
| 1951 | if (target == NULL || targetsize < 1) { |
| 1952 | PyErr_BadInternalCall(); |
| 1953 | return NULL; |
| 1954 | } |
| 1955 | return as_ucs4(string, target, targetsize, copy_null); |
| 1956 | } |
| 1957 | |
| 1958 | Py_UCS4* |
| 1959 | PyUnicode_AsUCS4Copy(PyObject *string) |
| 1960 | { |
| 1961 | return as_ucs4(string, NULL, 0, 1); |
| 1962 | } |
| 1963 | |
| 1964 | #ifdef HAVE_WCHAR_H |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1965 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1966 | PyObject * |
| 1967 | PyUnicode_FromWideChar(register const wchar_t *w, Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1968 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1969 | if (w == NULL) { |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 1970 | if (size == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1971 | return PyUnicode_New(0, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1972 | PyErr_BadInternalCall(); |
| 1973 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1974 | } |
| 1975 | |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 1976 | if (size == -1) { |
| 1977 | size = wcslen(w); |
| 1978 | } |
| 1979 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1980 | return PyUnicode_FromUnicode(w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1981 | } |
| 1982 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1983 | #endif /* HAVE_WCHAR_H */ |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1984 | |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 1985 | static void |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1986 | makefmt(char *fmt, int longflag, int longlongflag, int size_tflag, |
| 1987 | int zeropad, int width, int precision, char c) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 1988 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1989 | *fmt++ = '%'; |
| 1990 | if (width) { |
| 1991 | if (zeropad) |
| 1992 | *fmt++ = '0'; |
| 1993 | fmt += sprintf(fmt, "%d", width); |
| 1994 | } |
| 1995 | if (precision) |
| 1996 | fmt += sprintf(fmt, ".%d", precision); |
| 1997 | if (longflag) |
| 1998 | *fmt++ = 'l'; |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1999 | else if (longlongflag) { |
| 2000 | /* longlongflag should only ever be nonzero on machines with |
| 2001 | HAVE_LONG_LONG defined */ |
| 2002 | #ifdef HAVE_LONG_LONG |
| 2003 | char *f = PY_FORMAT_LONG_LONG; |
| 2004 | while (*f) |
| 2005 | *fmt++ = *f++; |
| 2006 | #else |
| 2007 | /* we shouldn't ever get here */ |
| 2008 | assert(0); |
| 2009 | *fmt++ = 'l'; |
| 2010 | #endif |
| 2011 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2012 | else if (size_tflag) { |
| 2013 | char *f = PY_FORMAT_SIZE_T; |
| 2014 | while (*f) |
| 2015 | *fmt++ = *f++; |
| 2016 | } |
| 2017 | *fmt++ = c; |
| 2018 | *fmt = '\0'; |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2019 | } |
| 2020 | |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2021 | /* helper for PyUnicode_FromFormatV() */ |
| 2022 | |
| 2023 | static const char* |
| 2024 | parse_format_flags(const char *f, |
| 2025 | int *p_width, int *p_precision, |
| 2026 | int *p_longflag, int *p_longlongflag, int *p_size_tflag) |
| 2027 | { |
| 2028 | int width, precision, longflag, longlongflag, size_tflag; |
| 2029 | |
| 2030 | /* parse the width.precision part, e.g. "%2.5s" => width=2, precision=5 */ |
| 2031 | f++; |
| 2032 | width = 0; |
| 2033 | while (Py_ISDIGIT((unsigned)*f)) |
| 2034 | width = (width*10) + *f++ - '0'; |
| 2035 | precision = 0; |
| 2036 | if (*f == '.') { |
| 2037 | f++; |
| 2038 | while (Py_ISDIGIT((unsigned)*f)) |
| 2039 | precision = (precision*10) + *f++ - '0'; |
| 2040 | if (*f == '%') { |
| 2041 | /* "%.3%s" => f points to "3" */ |
| 2042 | f--; |
| 2043 | } |
| 2044 | } |
| 2045 | if (*f == '\0') { |
| 2046 | /* bogus format "%.1" => go backward, f points to "1" */ |
| 2047 | f--; |
| 2048 | } |
| 2049 | if (p_width != NULL) |
| 2050 | *p_width = width; |
| 2051 | if (p_precision != NULL) |
| 2052 | *p_precision = precision; |
| 2053 | |
| 2054 | /* Handle %ld, %lu, %lld and %llu. */ |
| 2055 | longflag = 0; |
| 2056 | longlongflag = 0; |
Victor Stinner | e7faec1 | 2011-03-02 00:01:53 +0000 | [diff] [blame] | 2057 | size_tflag = 0; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2058 | |
| 2059 | if (*f == 'l') { |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2060 | if (f[1] == 'd' || f[1] == 'u' || f[1] == 'i') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2061 | longflag = 1; |
| 2062 | ++f; |
| 2063 | } |
| 2064 | #ifdef HAVE_LONG_LONG |
| 2065 | else if (f[1] == 'l' && |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2066 | (f[2] == 'd' || f[2] == 'u' || f[2] == 'i')) { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2067 | longlongflag = 1; |
| 2068 | f += 2; |
| 2069 | } |
| 2070 | #endif |
| 2071 | } |
| 2072 | /* handle the size_t flag. */ |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2073 | 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] | 2074 | size_tflag = 1; |
| 2075 | ++f; |
| 2076 | } |
| 2077 | if (p_longflag != NULL) |
| 2078 | *p_longflag = longflag; |
| 2079 | if (p_longlongflag != NULL) |
| 2080 | *p_longlongflag = longlongflag; |
| 2081 | if (p_size_tflag != NULL) |
| 2082 | *p_size_tflag = size_tflag; |
| 2083 | return f; |
| 2084 | } |
| 2085 | |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2086 | /* maximum number of characters required for output of %ld. 21 characters |
| 2087 | allows for 64-bit integers (in decimal) and an optional sign. */ |
| 2088 | #define MAX_LONG_CHARS 21 |
| 2089 | /* maximum number of characters required for output of %lld. |
| 2090 | We need at most ceil(log10(256)*SIZEOF_LONG_LONG) digits, |
| 2091 | plus 1 for the sign. 53/22 is an upper bound for log10(256). */ |
| 2092 | #define MAX_LONG_LONG_CHARS (2 + (SIZEOF_LONG_LONG*53-1) / 22) |
| 2093 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2094 | PyObject * |
| 2095 | PyUnicode_FromFormatV(const char *format, va_list vargs) |
| 2096 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2097 | va_list count; |
| 2098 | Py_ssize_t callcount = 0; |
| 2099 | PyObject **callresults = NULL; |
| 2100 | PyObject **callresult = NULL; |
| 2101 | Py_ssize_t n = 0; |
| 2102 | int width = 0; |
| 2103 | int precision = 0; |
| 2104 | int zeropad; |
| 2105 | const char* f; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2106 | PyObject *string; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2107 | /* used by sprintf */ |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2108 | char fmt[61]; /* should be enough for %0width.precisionlld */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2109 | Py_UCS4 maxchar = 127; /* result is ASCII by default */ |
| 2110 | Py_UCS4 argmaxchar; |
| 2111 | Py_ssize_t numbersize = 0; |
| 2112 | char *numberresults = NULL; |
| 2113 | char *numberresult = NULL; |
| 2114 | Py_ssize_t i; |
| 2115 | int kind; |
| 2116 | void *data; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2117 | |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 2118 | Py_VA_COPY(count, vargs); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2119 | /* step 1: count the number of %S/%R/%A/%s format specifications |
| 2120 | * (we call PyObject_Str()/PyObject_Repr()/PyObject_ASCII()/ |
| 2121 | * 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] | 2122 | * result in an array) |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 2123 | * also estimate a upper bound for all the number formats in the string, |
| 2124 | * 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] | 2125 | * buffer before putting everything together. */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2126 | for (f = format; *f; f++) { |
| 2127 | if (*f == '%') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2128 | int longlongflag; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2129 | /* skip width or width.precision (eg. "1.2" of "%1.2f") */ |
| 2130 | f = parse_format_flags(f, &width, NULL, NULL, &longlongflag, NULL); |
| 2131 | if (*f == 's' || *f=='S' || *f=='R' || *f=='A' || *f=='V') |
| 2132 | ++callcount; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2133 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2134 | else if (*f == 'd' || *f=='u' || *f=='i' || *f=='x' || *f=='p') { |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2135 | #ifdef HAVE_LONG_LONG |
| 2136 | if (longlongflag) { |
| 2137 | if (width < MAX_LONG_LONG_CHARS) |
| 2138 | width = MAX_LONG_LONG_CHARS; |
| 2139 | } |
| 2140 | else |
| 2141 | #endif |
| 2142 | /* MAX_LONG_CHARS is enough to hold a 64-bit integer, |
| 2143 | including sign. Decimal takes the most space. This |
| 2144 | isn't enough for octal. If a width is specified we |
| 2145 | need more (which we allocate later). */ |
| 2146 | if (width < MAX_LONG_CHARS) |
| 2147 | width = MAX_LONG_CHARS; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2148 | |
| 2149 | /* account for the size + '\0' to separate numbers |
| 2150 | inside of the numberresults buffer */ |
| 2151 | numbersize += (width + 1); |
| 2152 | } |
| 2153 | } |
| 2154 | else if ((unsigned char)*f > 127) { |
| 2155 | PyErr_Format(PyExc_ValueError, |
| 2156 | "PyUnicode_FromFormatV() expects an ASCII-encoded format " |
| 2157 | "string, got a non-ASCII byte: 0x%02x", |
| 2158 | (unsigned char)*f); |
| 2159 | return NULL; |
| 2160 | } |
| 2161 | } |
| 2162 | /* step 2: allocate memory for the results of |
| 2163 | * PyObject_Str()/PyObject_Repr()/PyUnicode_DecodeUTF8() calls */ |
| 2164 | if (callcount) { |
| 2165 | callresults = PyObject_Malloc(sizeof(PyObject *) * callcount); |
| 2166 | if (!callresults) { |
| 2167 | PyErr_NoMemory(); |
| 2168 | return NULL; |
| 2169 | } |
| 2170 | callresult = callresults; |
| 2171 | } |
| 2172 | /* step 2.5: allocate memory for the results of formating numbers */ |
| 2173 | if (numbersize) { |
| 2174 | numberresults = PyObject_Malloc(numbersize); |
| 2175 | if (!numberresults) { |
| 2176 | PyErr_NoMemory(); |
| 2177 | goto fail; |
| 2178 | } |
| 2179 | numberresult = numberresults; |
| 2180 | } |
| 2181 | |
| 2182 | /* step 3: format numbers and figure out how large a buffer we need */ |
| 2183 | for (f = format; *f; f++) { |
| 2184 | if (*f == '%') { |
| 2185 | const char* p; |
| 2186 | int longflag; |
| 2187 | int longlongflag; |
| 2188 | int size_tflag; |
| 2189 | int numprinted; |
| 2190 | |
| 2191 | p = f; |
| 2192 | zeropad = (f[1] == '0'); |
| 2193 | f = parse_format_flags(f, &width, &precision, |
| 2194 | &longflag, &longlongflag, &size_tflag); |
| 2195 | switch (*f) { |
| 2196 | case 'c': |
| 2197 | { |
| 2198 | Py_UCS4 ordinal = va_arg(count, int); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2199 | maxchar = Py_MAX(maxchar, ordinal); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2200 | n++; |
| 2201 | break; |
| 2202 | } |
| 2203 | case '%': |
| 2204 | n++; |
| 2205 | break; |
| 2206 | case 'i': |
| 2207 | case 'd': |
| 2208 | makefmt(fmt, longflag, longlongflag, size_tflag, zeropad, |
| 2209 | width, precision, *f); |
| 2210 | if (longflag) |
| 2211 | numprinted = sprintf(numberresult, fmt, |
| 2212 | va_arg(count, long)); |
| 2213 | #ifdef HAVE_LONG_LONG |
| 2214 | else if (longlongflag) |
| 2215 | numprinted = sprintf(numberresult, fmt, |
| 2216 | va_arg(count, PY_LONG_LONG)); |
| 2217 | #endif |
| 2218 | else if (size_tflag) |
| 2219 | numprinted = sprintf(numberresult, fmt, |
| 2220 | va_arg(count, Py_ssize_t)); |
| 2221 | else |
| 2222 | numprinted = sprintf(numberresult, fmt, |
| 2223 | va_arg(count, int)); |
| 2224 | n += numprinted; |
| 2225 | /* advance by +1 to skip over the '\0' */ |
| 2226 | numberresult += (numprinted + 1); |
| 2227 | assert(*(numberresult - 1) == '\0'); |
| 2228 | assert(*(numberresult - 2) != '\0'); |
| 2229 | assert(numprinted >= 0); |
| 2230 | assert(numberresult <= numberresults + numbersize); |
| 2231 | break; |
| 2232 | case 'u': |
| 2233 | makefmt(fmt, longflag, longlongflag, size_tflag, zeropad, |
| 2234 | width, precision, 'u'); |
| 2235 | if (longflag) |
| 2236 | numprinted = sprintf(numberresult, fmt, |
| 2237 | va_arg(count, unsigned long)); |
| 2238 | #ifdef HAVE_LONG_LONG |
| 2239 | else if (longlongflag) |
| 2240 | numprinted = sprintf(numberresult, fmt, |
| 2241 | va_arg(count, unsigned PY_LONG_LONG)); |
| 2242 | #endif |
| 2243 | else if (size_tflag) |
| 2244 | numprinted = sprintf(numberresult, fmt, |
| 2245 | va_arg(count, size_t)); |
| 2246 | else |
| 2247 | numprinted = sprintf(numberresult, fmt, |
| 2248 | va_arg(count, unsigned int)); |
| 2249 | n += numprinted; |
| 2250 | numberresult += (numprinted + 1); |
| 2251 | assert(*(numberresult - 1) == '\0'); |
| 2252 | assert(*(numberresult - 2) != '\0'); |
| 2253 | assert(numprinted >= 0); |
| 2254 | assert(numberresult <= numberresults + numbersize); |
| 2255 | break; |
| 2256 | case 'x': |
| 2257 | makefmt(fmt, 0, 0, 0, zeropad, width, precision, 'x'); |
| 2258 | numprinted = sprintf(numberresult, fmt, va_arg(count, int)); |
| 2259 | n += numprinted; |
| 2260 | numberresult += (numprinted + 1); |
| 2261 | assert(*(numberresult - 1) == '\0'); |
| 2262 | assert(*(numberresult - 2) != '\0'); |
| 2263 | assert(numprinted >= 0); |
| 2264 | assert(numberresult <= numberresults + numbersize); |
| 2265 | break; |
| 2266 | case 'p': |
| 2267 | numprinted = sprintf(numberresult, "%p", va_arg(count, void*)); |
| 2268 | /* %p is ill-defined: ensure leading 0x. */ |
| 2269 | if (numberresult[1] == 'X') |
| 2270 | numberresult[1] = 'x'; |
| 2271 | else if (numberresult[1] != 'x') { |
| 2272 | memmove(numberresult + 2, numberresult, |
| 2273 | strlen(numberresult) + 1); |
| 2274 | numberresult[0] = '0'; |
| 2275 | numberresult[1] = 'x'; |
| 2276 | numprinted += 2; |
| 2277 | } |
| 2278 | n += numprinted; |
| 2279 | numberresult += (numprinted + 1); |
| 2280 | assert(*(numberresult - 1) == '\0'); |
| 2281 | assert(*(numberresult - 2) != '\0'); |
| 2282 | assert(numprinted >= 0); |
| 2283 | assert(numberresult <= numberresults + numbersize); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2284 | break; |
| 2285 | case 's': |
| 2286 | { |
| 2287 | /* UTF-8 */ |
Georg Brandl | 780b2a6 | 2009-05-05 09:19:59 +0000 | [diff] [blame] | 2288 | const char *s = va_arg(count, const char*); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2289 | PyObject *str = PyUnicode_DecodeUTF8(s, strlen(s), "replace"); |
| 2290 | if (!str) |
| 2291 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2292 | /* since PyUnicode_DecodeUTF8 returns already flexible |
| 2293 | unicode objects, there is no need to call ready on them */ |
| 2294 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2295 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2296 | n += PyUnicode_GET_LENGTH(str); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2297 | /* Remember the str and switch to the next slot */ |
| 2298 | *callresult++ = str; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2299 | break; |
| 2300 | } |
| 2301 | case 'U': |
| 2302 | { |
| 2303 | PyObject *obj = va_arg(count, PyObject *); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 2304 | assert(obj && _PyUnicode_CHECK(obj)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2305 | if (PyUnicode_READY(obj) == -1) |
| 2306 | goto fail; |
| 2307 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2308 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2309 | n += PyUnicode_GET_LENGTH(obj); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2310 | break; |
| 2311 | } |
| 2312 | case 'V': |
| 2313 | { |
| 2314 | PyObject *obj = va_arg(count, PyObject *); |
| 2315 | const char *str = va_arg(count, const char *); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2316 | PyObject *str_obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2317 | assert(obj || str); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 2318 | assert(!obj || _PyUnicode_CHECK(obj)); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2319 | if (obj) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2320 | if (PyUnicode_READY(obj) == -1) |
| 2321 | goto fail; |
| 2322 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2323 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2324 | n += PyUnicode_GET_LENGTH(obj); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2325 | *callresult++ = NULL; |
| 2326 | } |
| 2327 | else { |
| 2328 | str_obj = PyUnicode_DecodeUTF8(str, strlen(str), "replace"); |
| 2329 | if (!str_obj) |
| 2330 | goto fail; |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 2331 | if (PyUnicode_READY(str_obj)) { |
| 2332 | Py_DECREF(str_obj); |
| 2333 | goto fail; |
| 2334 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2335 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str_obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2336 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2337 | n += PyUnicode_GET_LENGTH(str_obj); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2338 | *callresult++ = str_obj; |
| 2339 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2340 | break; |
| 2341 | } |
| 2342 | case 'S': |
| 2343 | { |
| 2344 | PyObject *obj = va_arg(count, PyObject *); |
| 2345 | PyObject *str; |
| 2346 | assert(obj); |
| 2347 | str = PyObject_Str(obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2348 | if (!str || PyUnicode_READY(str) == -1) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2349 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2350 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2351 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2352 | n += PyUnicode_GET_LENGTH(str); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2353 | /* Remember the str and switch to the next slot */ |
| 2354 | *callresult++ = str; |
| 2355 | break; |
| 2356 | } |
| 2357 | case 'R': |
| 2358 | { |
| 2359 | PyObject *obj = va_arg(count, PyObject *); |
| 2360 | PyObject *repr; |
| 2361 | assert(obj); |
| 2362 | repr = PyObject_Repr(obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2363 | if (!repr || PyUnicode_READY(repr) == -1) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2364 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2365 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(repr); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2366 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2367 | n += PyUnicode_GET_LENGTH(repr); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2368 | /* Remember the repr and switch to the next slot */ |
| 2369 | *callresult++ = repr; |
| 2370 | break; |
| 2371 | } |
| 2372 | case 'A': |
| 2373 | { |
| 2374 | PyObject *obj = va_arg(count, PyObject *); |
| 2375 | PyObject *ascii; |
| 2376 | assert(obj); |
| 2377 | ascii = PyObject_ASCII(obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2378 | if (!ascii || PyUnicode_READY(ascii) == -1) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2379 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2380 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(ascii); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2381 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2382 | n += PyUnicode_GET_LENGTH(ascii); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2383 | /* Remember the repr and switch to the next slot */ |
| 2384 | *callresult++ = ascii; |
| 2385 | break; |
| 2386 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2387 | default: |
| 2388 | /* if we stumble upon an unknown |
| 2389 | formatting code, copy the rest of |
| 2390 | the format string to the output |
| 2391 | string. (we cannot just skip the |
| 2392 | code, since there's no way to know |
| 2393 | what's in the argument list) */ |
| 2394 | n += strlen(p); |
| 2395 | goto expand; |
| 2396 | } |
| 2397 | } else |
| 2398 | n++; |
| 2399 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2400 | expand: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2401 | /* step 4: fill the buffer */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2402 | /* Since we've analyzed how much space we need, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2403 | we don't have to resize the string. |
| 2404 | There can be no errors beyond this point. */ |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2405 | string = PyUnicode_New(n, maxchar); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2406 | if (!string) |
| 2407 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2408 | kind = PyUnicode_KIND(string); |
| 2409 | data = PyUnicode_DATA(string); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2410 | callresult = callresults; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2411 | numberresult = numberresults; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2412 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2413 | for (i = 0, f = format; *f; f++) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2414 | if (*f == '%') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2415 | const char* p; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2416 | |
| 2417 | p = f; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2418 | f = parse_format_flags(f, NULL, NULL, NULL, NULL, NULL); |
| 2419 | /* checking for == because the last argument could be a empty |
| 2420 | string, which causes i to point to end, the assert at the end of |
| 2421 | the loop */ |
| 2422 | assert(i <= PyUnicode_GET_LENGTH(string)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2423 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2424 | switch (*f) { |
| 2425 | case 'c': |
Victor Stinner | 5ed8b2c | 2011-02-21 21:13:44 +0000 | [diff] [blame] | 2426 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2427 | const int ordinal = va_arg(vargs, int); |
| 2428 | PyUnicode_WRITE(kind, data, i++, ordinal); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2429 | break; |
Victor Stinner | 5ed8b2c | 2011-02-21 21:13:44 +0000 | [diff] [blame] | 2430 | } |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2431 | case 'i': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2432 | case 'd': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2433 | case 'u': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2434 | case 'x': |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2435 | case 'p': |
| 2436 | /* unused, since we already have the result */ |
| 2437 | if (*f == 'p') |
| 2438 | (void) va_arg(vargs, void *); |
| 2439 | else |
| 2440 | (void) va_arg(vargs, int); |
| 2441 | /* extract the result from numberresults and append. */ |
| 2442 | for (; *numberresult; ++i, ++numberresult) |
| 2443 | PyUnicode_WRITE(kind, data, i, *numberresult); |
| 2444 | /* skip over the separating '\0' */ |
| 2445 | assert(*numberresult == '\0'); |
| 2446 | numberresult++; |
| 2447 | assert(numberresult <= numberresults + numbersize); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2448 | break; |
| 2449 | case 's': |
| 2450 | { |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2451 | /* unused, since we already have the result */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2452 | Py_ssize_t size; |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2453 | (void) va_arg(vargs, char *); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2454 | size = PyUnicode_GET_LENGTH(*callresult); |
| 2455 | assert(PyUnicode_KIND(*callresult) <= PyUnicode_KIND(string)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2456 | copy_characters(string, i, *callresult, 0, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2457 | i += size; |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2458 | /* We're done with the unicode()/repr() => forget it */ |
| 2459 | Py_DECREF(*callresult); |
| 2460 | /* switch to next unicode()/repr() result */ |
| 2461 | ++callresult; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2462 | break; |
| 2463 | } |
| 2464 | case 'U': |
| 2465 | { |
| 2466 | PyObject *obj = va_arg(vargs, PyObject *); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2467 | Py_ssize_t size; |
| 2468 | assert(PyUnicode_KIND(obj) <= PyUnicode_KIND(string)); |
| 2469 | size = PyUnicode_GET_LENGTH(obj); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2470 | copy_characters(string, i, obj, 0, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2471 | i += size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2472 | break; |
| 2473 | } |
| 2474 | case 'V': |
| 2475 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2476 | Py_ssize_t size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2477 | PyObject *obj = va_arg(vargs, PyObject *); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2478 | va_arg(vargs, const char *); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2479 | if (obj) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2480 | size = PyUnicode_GET_LENGTH(obj); |
| 2481 | assert(PyUnicode_KIND(obj) <= PyUnicode_KIND(string)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2482 | copy_characters(string, i, obj, 0, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2483 | i += size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2484 | } else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2485 | size = PyUnicode_GET_LENGTH(*callresult); |
| 2486 | assert(PyUnicode_KIND(*callresult) <= |
| 2487 | PyUnicode_KIND(string)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2488 | copy_characters(string, i, *callresult, 0, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2489 | i += size; |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2490 | Py_DECREF(*callresult); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2491 | } |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2492 | ++callresult; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2493 | break; |
| 2494 | } |
| 2495 | case 'S': |
| 2496 | case 'R': |
Victor Stinner | 9a90900 | 2010-10-18 20:59:24 +0000 | [diff] [blame] | 2497 | case 'A': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2498 | { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2499 | Py_ssize_t size = PyUnicode_GET_LENGTH(*callresult); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2500 | /* unused, since we already have the result */ |
| 2501 | (void) va_arg(vargs, PyObject *); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2502 | assert(PyUnicode_KIND(*callresult) <= PyUnicode_KIND(string)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2503 | copy_characters(string, i, *callresult, 0, size); |
| 2504 | i += size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2505 | /* We're done with the unicode()/repr() => forget it */ |
| 2506 | Py_DECREF(*callresult); |
| 2507 | /* switch to next unicode()/repr() result */ |
| 2508 | ++callresult; |
| 2509 | break; |
| 2510 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2511 | case '%': |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2512 | PyUnicode_WRITE(kind, data, i++, '%'); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2513 | break; |
| 2514 | default: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2515 | for (; *p; ++p, ++i) |
| 2516 | PyUnicode_WRITE(kind, data, i, *p); |
| 2517 | assert(i == PyUnicode_GET_LENGTH(string)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2518 | goto end; |
| 2519 | } |
Victor Stinner | 1205f27 | 2010-09-11 00:54:47 +0000 | [diff] [blame] | 2520 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2521 | else { |
| 2522 | assert(i < PyUnicode_GET_LENGTH(string)); |
| 2523 | PyUnicode_WRITE(kind, data, i++, *f); |
| 2524 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2525 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2526 | assert(i == PyUnicode_GET_LENGTH(string)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2527 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2528 | end: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2529 | if (callresults) |
| 2530 | PyObject_Free(callresults); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2531 | if (numberresults) |
| 2532 | PyObject_Free(numberresults); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2533 | assert(_PyUnicode_CheckConsistency(string, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2534 | return (PyObject *)string; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2535 | fail: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2536 | if (callresults) { |
| 2537 | PyObject **callresult2 = callresults; |
| 2538 | while (callresult2 < callresult) { |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2539 | Py_XDECREF(*callresult2); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2540 | ++callresult2; |
| 2541 | } |
| 2542 | PyObject_Free(callresults); |
| 2543 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2544 | if (numberresults) |
| 2545 | PyObject_Free(numberresults); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2546 | return NULL; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2547 | } |
| 2548 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2549 | PyObject * |
| 2550 | PyUnicode_FromFormat(const char *format, ...) |
| 2551 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2552 | PyObject* ret; |
| 2553 | va_list vargs; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2554 | |
| 2555 | #ifdef HAVE_STDARG_PROTOTYPES |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2556 | va_start(vargs, format); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2557 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2558 | va_start(vargs); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2559 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2560 | ret = PyUnicode_FromFormatV(format, vargs); |
| 2561 | va_end(vargs); |
| 2562 | return ret; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2563 | } |
| 2564 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2565 | #ifdef HAVE_WCHAR_H |
| 2566 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2567 | /* Helper function for PyUnicode_AsWideChar() and PyUnicode_AsWideCharString(): |
| 2568 | convert a Unicode object to a wide character string. |
| 2569 | |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2570 | - 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] | 2571 | character) required to convert the unicode object. Ignore size argument. |
| 2572 | |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2573 | - Otherwise: return the number of wide characters (excluding the null |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2574 | character) written into w. Write at most size wide characters (including |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2575 | the null character). */ |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2576 | static Py_ssize_t |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2577 | unicode_aswidechar(PyUnicodeObject *unicode, |
| 2578 | wchar_t *w, |
| 2579 | Py_ssize_t size) |
| 2580 | { |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2581 | Py_ssize_t res; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2582 | const wchar_t *wstr; |
| 2583 | |
| 2584 | wstr = PyUnicode_AsUnicodeAndSize((PyObject *)unicode, &res); |
| 2585 | if (wstr == NULL) |
| 2586 | return -1; |
| 2587 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2588 | if (w != NULL) { |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2589 | if (size > res) |
| 2590 | size = res + 1; |
| 2591 | else |
| 2592 | res = size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2593 | Py_MEMCPY(w, wstr, size * sizeof(wchar_t)); |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2594 | return res; |
| 2595 | } |
| 2596 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2597 | return res + 1; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2598 | } |
| 2599 | |
| 2600 | Py_ssize_t |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2601 | PyUnicode_AsWideChar(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2602 | wchar_t *w, |
| 2603 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2604 | { |
| 2605 | if (unicode == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2606 | PyErr_BadInternalCall(); |
| 2607 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2608 | } |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2609 | return unicode_aswidechar((PyUnicodeObject*)unicode, w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2610 | } |
| 2611 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2612 | wchar_t* |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 2613 | PyUnicode_AsWideCharString(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2614 | Py_ssize_t *size) |
| 2615 | { |
| 2616 | wchar_t* buffer; |
| 2617 | Py_ssize_t buflen; |
| 2618 | |
| 2619 | if (unicode == NULL) { |
| 2620 | PyErr_BadInternalCall(); |
| 2621 | return NULL; |
| 2622 | } |
| 2623 | |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 2624 | buflen = unicode_aswidechar((PyUnicodeObject *)unicode, NULL, 0); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2625 | if (buflen == -1) |
| 2626 | return NULL; |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2627 | if (PY_SSIZE_T_MAX / sizeof(wchar_t) < buflen) { |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2628 | PyErr_NoMemory(); |
| 2629 | return NULL; |
| 2630 | } |
| 2631 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2632 | buffer = PyMem_MALLOC(buflen * sizeof(wchar_t)); |
| 2633 | if (buffer == NULL) { |
| 2634 | PyErr_NoMemory(); |
| 2635 | return NULL; |
| 2636 | } |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 2637 | buflen = unicode_aswidechar((PyUnicodeObject *)unicode, buffer, buflen); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2638 | if (buflen == -1) |
| 2639 | return NULL; |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2640 | if (size != NULL) |
| 2641 | *size = buflen; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2642 | return buffer; |
| 2643 | } |
| 2644 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2645 | #endif /* HAVE_WCHAR_H */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2646 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2647 | PyObject * |
| 2648 | PyUnicode_FromOrdinal(int ordinal) |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2649 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2650 | PyObject *v; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2651 | if (ordinal < 0 || ordinal > 0x10ffff) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2652 | PyErr_SetString(PyExc_ValueError, |
| 2653 | "chr() arg not in range(0x110000)"); |
| 2654 | return NULL; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2655 | } |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 2656 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2657 | if (ordinal < 256) |
| 2658 | return get_latin1_char(ordinal); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2659 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2660 | v = PyUnicode_New(1, ordinal); |
| 2661 | if (v == NULL) |
| 2662 | return NULL; |
| 2663 | PyUnicode_WRITE(PyUnicode_KIND(v), PyUnicode_DATA(v), 0, ordinal); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2664 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2665 | return v; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2666 | } |
| 2667 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2668 | PyObject * |
| 2669 | PyUnicode_FromObject(register PyObject *obj) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2670 | { |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2671 | /* XXX Perhaps we should make this API an alias of |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2672 | PyObject_Str() instead ?! */ |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2673 | if (PyUnicode_CheckExact(obj)) { |
Victor Stinner | d3a83d5 | 2011-10-01 03:09:33 +0200 | [diff] [blame] | 2674 | if (PyUnicode_READY(obj)) |
| 2675 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2676 | Py_INCREF(obj); |
| 2677 | return obj; |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2678 | } |
| 2679 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2680 | /* For a Unicode subtype that's not a Unicode object, |
| 2681 | return a true Unicode object with the same data. */ |
Victor Stinner | 2219e0a | 2011-10-01 01:16:59 +0200 | [diff] [blame] | 2682 | return PyUnicode_Copy(obj); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2683 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2684 | PyErr_Format(PyExc_TypeError, |
| 2685 | "Can't convert '%.100s' object to str implicitly", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2686 | Py_TYPE(obj)->tp_name); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2687 | return NULL; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2688 | } |
| 2689 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2690 | PyObject * |
| 2691 | PyUnicode_FromEncodedObject(register PyObject *obj, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2692 | const char *encoding, |
| 2693 | const char *errors) |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2694 | { |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2695 | Py_buffer buffer; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2696 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2697 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2698 | if (obj == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2699 | PyErr_BadInternalCall(); |
| 2700 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2701 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2702 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2703 | /* Decoding bytes objects is the most common case and should be fast */ |
| 2704 | if (PyBytes_Check(obj)) { |
| 2705 | if (PyBytes_GET_SIZE(obj) == 0) { |
| 2706 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 2707 | v = unicode_empty; |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2708 | } |
| 2709 | else { |
| 2710 | v = PyUnicode_Decode( |
| 2711 | PyBytes_AS_STRING(obj), PyBytes_GET_SIZE(obj), |
| 2712 | encoding, errors); |
| 2713 | } |
| 2714 | return v; |
| 2715 | } |
| 2716 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2717 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2718 | PyErr_SetString(PyExc_TypeError, |
| 2719 | "decoding str is not supported"); |
| 2720 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2721 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2722 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2723 | /* Retrieve a bytes buffer view through the PEP 3118 buffer interface */ |
| 2724 | if (PyObject_GetBuffer(obj, &buffer, PyBUF_SIMPLE) < 0) { |
| 2725 | PyErr_Format(PyExc_TypeError, |
| 2726 | "coercing to str: need bytes, bytearray " |
| 2727 | "or buffer-like object, %.80s found", |
| 2728 | Py_TYPE(obj)->tp_name); |
| 2729 | return NULL; |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 2730 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2731 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2732 | if (buffer.len == 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2733 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 2734 | v = unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2735 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2736 | else |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2737 | v = PyUnicode_Decode((char*) buffer.buf, buffer.len, encoding, errors); |
Marc-André Lemburg | ad7c98e | 2001-01-17 17:09:53 +0000 | [diff] [blame] | 2738 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2739 | PyBuffer_Release(&buffer); |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2740 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2741 | } |
| 2742 | |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2743 | /* Convert encoding to lower case and replace '_' with '-' in order to |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2744 | catch e.g. UTF_8. Return 0 on error (encoding is longer than lower_len-1), |
| 2745 | 1 on success. */ |
| 2746 | static int |
| 2747 | normalize_encoding(const char *encoding, |
| 2748 | char *lower, |
| 2749 | size_t lower_len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2750 | { |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2751 | const char *e; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2752 | char *l; |
| 2753 | char *l_end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2754 | |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2755 | e = encoding; |
| 2756 | l = lower; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2757 | l_end = &lower[lower_len - 1]; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2758 | while (*e) { |
| 2759 | if (l == l_end) |
| 2760 | return 0; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 2761 | if (Py_ISUPPER(*e)) { |
| 2762 | *l++ = Py_TOLOWER(*e++); |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2763 | } |
| 2764 | else if (*e == '_') { |
| 2765 | *l++ = '-'; |
| 2766 | e++; |
| 2767 | } |
| 2768 | else { |
| 2769 | *l++ = *e++; |
| 2770 | } |
| 2771 | } |
| 2772 | *l = '\0'; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2773 | return 1; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2774 | } |
| 2775 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2776 | PyObject * |
| 2777 | PyUnicode_Decode(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2778 | Py_ssize_t size, |
| 2779 | const char *encoding, |
| 2780 | const char *errors) |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2781 | { |
| 2782 | PyObject *buffer = NULL, *unicode; |
| 2783 | Py_buffer info; |
| 2784 | char lower[11]; /* Enough for any encoding shortcut */ |
| 2785 | |
| 2786 | if (encoding == NULL) |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2787 | return PyUnicode_DecodeUTF8(s, size, errors); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 2788 | |
| 2789 | /* Shortcuts for common default encodings */ |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2790 | if (normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2791 | if ((strcmp(lower, "utf-8") == 0) || |
| 2792 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2793 | return PyUnicode_DecodeUTF8(s, size, errors); |
| 2794 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2795 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2796 | (strcmp(lower, "iso-8859-1") == 0)) |
| 2797 | return PyUnicode_DecodeLatin1(s, size, errors); |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 2798 | #ifdef HAVE_MBCS |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2799 | else if (strcmp(lower, "mbcs") == 0) |
| 2800 | return PyUnicode_DecodeMBCS(s, size, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 2801 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2802 | else if (strcmp(lower, "ascii") == 0) |
| 2803 | return PyUnicode_DecodeASCII(s, size, errors); |
| 2804 | else if (strcmp(lower, "utf-16") == 0) |
| 2805 | return PyUnicode_DecodeUTF16(s, size, errors, 0); |
| 2806 | else if (strcmp(lower, "utf-32") == 0) |
| 2807 | return PyUnicode_DecodeUTF32(s, size, errors, 0); |
| 2808 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2809 | |
| 2810 | /* Decode via the codec registry */ |
Guido van Rossum | be801ac | 2007-10-08 03:32:34 +0000 | [diff] [blame] | 2811 | buffer = NULL; |
Antoine Pitrou | c3b3924 | 2009-01-03 16:59:18 +0000 | [diff] [blame] | 2812 | 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] | 2813 | goto onError; |
Antoine Pitrou | ee58fa4 | 2008-08-19 18:22:14 +0000 | [diff] [blame] | 2814 | buffer = PyMemoryView_FromBuffer(&info); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2815 | if (buffer == NULL) |
| 2816 | goto onError; |
| 2817 | unicode = PyCodec_Decode(buffer, encoding, errors); |
| 2818 | if (unicode == NULL) |
| 2819 | goto onError; |
| 2820 | if (!PyUnicode_Check(unicode)) { |
| 2821 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 2822 | "decoder did not return a str object (type=%.400s)", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2823 | Py_TYPE(unicode)->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2824 | Py_DECREF(unicode); |
| 2825 | goto onError; |
| 2826 | } |
| 2827 | Py_DECREF(buffer); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 2828 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 2829 | if (_PyUnicode_READY_REPLACE(&unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2830 | Py_DECREF(unicode); |
| 2831 | return NULL; |
| 2832 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 2833 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2834 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2835 | return unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2836 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2837 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2838 | Py_XDECREF(buffer); |
| 2839 | return NULL; |
| 2840 | } |
| 2841 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2842 | PyObject * |
| 2843 | PyUnicode_AsDecodedObject(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2844 | const char *encoding, |
| 2845 | const char *errors) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2846 | { |
| 2847 | PyObject *v; |
| 2848 | |
| 2849 | if (!PyUnicode_Check(unicode)) { |
| 2850 | PyErr_BadArgument(); |
| 2851 | goto onError; |
| 2852 | } |
| 2853 | |
| 2854 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2855 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2856 | |
| 2857 | /* Decode via the codec registry */ |
| 2858 | v = PyCodec_Decode(unicode, encoding, errors); |
| 2859 | if (v == NULL) |
| 2860 | goto onError; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2861 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2862 | return v; |
| 2863 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2864 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2865 | return NULL; |
| 2866 | } |
| 2867 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2868 | PyObject * |
| 2869 | PyUnicode_AsDecodedUnicode(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2870 | const char *encoding, |
| 2871 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2872 | { |
| 2873 | PyObject *v; |
| 2874 | |
| 2875 | if (!PyUnicode_Check(unicode)) { |
| 2876 | PyErr_BadArgument(); |
| 2877 | goto onError; |
| 2878 | } |
| 2879 | |
| 2880 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2881 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2882 | |
| 2883 | /* Decode via the codec registry */ |
| 2884 | v = PyCodec_Decode(unicode, encoding, errors); |
| 2885 | if (v == NULL) |
| 2886 | goto onError; |
| 2887 | if (!PyUnicode_Check(v)) { |
| 2888 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 2889 | "decoder did not return a str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2890 | Py_TYPE(v)->tp_name); |
| 2891 | Py_DECREF(v); |
| 2892 | goto onError; |
| 2893 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2894 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2895 | return v; |
| 2896 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2897 | onError: |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2898 | return NULL; |
| 2899 | } |
| 2900 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2901 | PyObject * |
| 2902 | PyUnicode_Encode(const Py_UNICODE *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2903 | Py_ssize_t size, |
| 2904 | const char *encoding, |
| 2905 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2906 | { |
| 2907 | PyObject *v, *unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2908 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2909 | unicode = PyUnicode_FromUnicode(s, size); |
| 2910 | if (unicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2911 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2912 | v = PyUnicode_AsEncodedString(unicode, encoding, errors); |
| 2913 | Py_DECREF(unicode); |
| 2914 | return v; |
| 2915 | } |
| 2916 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2917 | PyObject * |
| 2918 | PyUnicode_AsEncodedObject(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2919 | const char *encoding, |
| 2920 | const char *errors) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2921 | { |
| 2922 | PyObject *v; |
| 2923 | |
| 2924 | if (!PyUnicode_Check(unicode)) { |
| 2925 | PyErr_BadArgument(); |
| 2926 | goto onError; |
| 2927 | } |
| 2928 | |
| 2929 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2930 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2931 | |
| 2932 | /* Encode via the codec registry */ |
| 2933 | v = PyCodec_Encode(unicode, encoding, errors); |
| 2934 | if (v == NULL) |
| 2935 | goto onError; |
| 2936 | return v; |
| 2937 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2938 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2939 | return NULL; |
| 2940 | } |
| 2941 | |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 2942 | PyObject * |
| 2943 | PyUnicode_EncodeFSDefault(PyObject *unicode) |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 2944 | { |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 2945 | #ifdef HAVE_MBCS |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 2946 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
| 2947 | PyUnicode_GET_SIZE(unicode), |
| 2948 | NULL); |
| 2949 | #elif defined(__APPLE__) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2950 | return _PyUnicode_AsUTF8String(unicode, "surrogateescape"); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 2951 | #else |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 2952 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 2953 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 2954 | cannot use it to encode and decode filenames before it is loaded. Load |
| 2955 | the Python codec requires to encode at least its own filename. Use the C |
| 2956 | version of the locale codec until the codec registry is initialized and |
| 2957 | the Python codec is loaded. |
| 2958 | |
| 2959 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 2960 | cannot only rely on it: check also interp->fscodec_initialized for |
| 2961 | subinterpreters. */ |
| 2962 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 2963 | return PyUnicode_AsEncodedString(unicode, |
| 2964 | Py_FileSystemDefaultEncoding, |
| 2965 | "surrogateescape"); |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 2966 | } |
| 2967 | else { |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 2968 | /* locale encoding with surrogateescape */ |
| 2969 | wchar_t *wchar; |
| 2970 | char *bytes; |
| 2971 | PyObject *bytes_obj; |
Victor Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 2972 | size_t error_pos; |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 2973 | |
| 2974 | wchar = PyUnicode_AsWideCharString(unicode, NULL); |
| 2975 | if (wchar == NULL) |
| 2976 | return NULL; |
Victor Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 2977 | bytes = _Py_wchar2char(wchar, &error_pos); |
| 2978 | if (bytes == NULL) { |
| 2979 | if (error_pos != (size_t)-1) { |
| 2980 | char *errmsg = strerror(errno); |
| 2981 | PyObject *exc = NULL; |
| 2982 | if (errmsg == NULL) |
| 2983 | errmsg = "Py_wchar2char() failed"; |
| 2984 | raise_encode_exception(&exc, |
| 2985 | "filesystemencoding", |
| 2986 | PyUnicode_AS_UNICODE(unicode), PyUnicode_GET_SIZE(unicode), |
| 2987 | error_pos, error_pos+1, |
| 2988 | errmsg); |
| 2989 | Py_XDECREF(exc); |
| 2990 | } |
| 2991 | else |
| 2992 | PyErr_NoMemory(); |
| 2993 | PyMem_Free(wchar); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 2994 | return NULL; |
Victor Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 2995 | } |
| 2996 | PyMem_Free(wchar); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 2997 | |
| 2998 | bytes_obj = PyBytes_FromString(bytes); |
| 2999 | PyMem_Free(bytes); |
| 3000 | return bytes_obj; |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 3001 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3002 | #endif |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3003 | } |
| 3004 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3005 | PyObject * |
| 3006 | PyUnicode_AsEncodedString(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3007 | const char *encoding, |
| 3008 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3009 | { |
| 3010 | PyObject *v; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3011 | char lower[11]; /* Enough for any encoding shortcut */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3012 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3013 | if (!PyUnicode_Check(unicode)) { |
| 3014 | PyErr_BadArgument(); |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3015 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3016 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3017 | |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3018 | if (encoding == NULL) { |
| 3019 | if (errors == NULL || strcmp(errors, "strict") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3020 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3021 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3022 | return _PyUnicode_AsUTF8String(unicode, errors); |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3023 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3024 | |
| 3025 | /* Shortcuts for common default encodings */ |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3026 | if (normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3027 | if ((strcmp(lower, "utf-8") == 0) || |
| 3028 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 3029 | { |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3030 | if (errors == NULL || strcmp(errors, "strict") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3031 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3032 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3033 | return _PyUnicode_AsUTF8String(unicode, errors); |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 3034 | } |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3035 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3036 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3037 | (strcmp(lower, "iso-8859-1") == 0)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3038 | return _PyUnicode_AsLatin1String(unicode, errors); |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3039 | #ifdef HAVE_MBCS |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3040 | else if (strcmp(lower, "mbcs") == 0) |
| 3041 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
| 3042 | PyUnicode_GET_SIZE(unicode), |
| 3043 | errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 3044 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3045 | else if (strcmp(lower, "ascii") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3046 | return _PyUnicode_AsASCIIString(unicode, errors); |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3047 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3048 | |
| 3049 | /* Encode via the codec registry */ |
| 3050 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3051 | if (v == NULL) |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3052 | return NULL; |
| 3053 | |
| 3054 | /* The normal path */ |
| 3055 | if (PyBytes_Check(v)) |
| 3056 | return v; |
| 3057 | |
| 3058 | /* 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] | 3059 | if (PyByteArray_Check(v)) { |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 3060 | int error; |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3061 | PyObject *b; |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 3062 | |
| 3063 | error = PyErr_WarnFormat(PyExc_RuntimeWarning, 1, |
| 3064 | "encoder %s returned bytearray instead of bytes", |
| 3065 | encoding); |
| 3066 | if (error) { |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3067 | Py_DECREF(v); |
| 3068 | return NULL; |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3069 | } |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3070 | |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3071 | b = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(v), Py_SIZE(v)); |
| 3072 | Py_DECREF(v); |
| 3073 | return b; |
| 3074 | } |
| 3075 | |
| 3076 | PyErr_Format(PyExc_TypeError, |
| 3077 | "encoder did not return a bytes object (type=%.400s)", |
| 3078 | Py_TYPE(v)->tp_name); |
| 3079 | Py_DECREF(v); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3080 | return NULL; |
| 3081 | } |
| 3082 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3083 | PyObject * |
| 3084 | PyUnicode_AsEncodedUnicode(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3085 | const char *encoding, |
| 3086 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3087 | { |
| 3088 | PyObject *v; |
| 3089 | |
| 3090 | if (!PyUnicode_Check(unicode)) { |
| 3091 | PyErr_BadArgument(); |
| 3092 | goto onError; |
| 3093 | } |
| 3094 | |
| 3095 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3096 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3097 | |
| 3098 | /* Encode via the codec registry */ |
| 3099 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3100 | if (v == NULL) |
| 3101 | goto onError; |
| 3102 | if (!PyUnicode_Check(v)) { |
| 3103 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3104 | "encoder did not return an str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3105 | Py_TYPE(v)->tp_name); |
| 3106 | Py_DECREF(v); |
| 3107 | goto onError; |
| 3108 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3109 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3110 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3111 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3112 | return NULL; |
| 3113 | } |
| 3114 | |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3115 | PyObject* |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3116 | PyUnicode_DecodeFSDefault(const char *s) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3117 | Py_ssize_t size = (Py_ssize_t)strlen(s); |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3118 | return PyUnicode_DecodeFSDefaultAndSize(s, size); |
| 3119 | } |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3120 | |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3121 | PyObject* |
| 3122 | PyUnicode_DecodeFSDefaultAndSize(const char *s, Py_ssize_t size) |
| 3123 | { |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3124 | #ifdef HAVE_MBCS |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3125 | return PyUnicode_DecodeMBCS(s, size, NULL); |
| 3126 | #elif defined(__APPLE__) |
| 3127 | return PyUnicode_DecodeUTF8(s, size, "surrogateescape"); |
| 3128 | #else |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 3129 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 3130 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 3131 | cannot use it to encode and decode filenames before it is loaded. Load |
| 3132 | the Python codec requires to encode at least its own filename. Use the C |
| 3133 | version of the locale codec until the codec registry is initialized and |
| 3134 | the Python codec is loaded. |
| 3135 | |
| 3136 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 3137 | cannot only rely on it: check also interp->fscodec_initialized for |
| 3138 | subinterpreters. */ |
| 3139 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3140 | return PyUnicode_Decode(s, size, |
| 3141 | Py_FileSystemDefaultEncoding, |
Victor Stinner | b9a20ad | 2010-04-30 16:37:52 +0000 | [diff] [blame] | 3142 | "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3143 | } |
| 3144 | else { |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3145 | /* locale encoding with surrogateescape */ |
| 3146 | wchar_t *wchar; |
| 3147 | PyObject *unicode; |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 3148 | size_t len; |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3149 | |
| 3150 | if (s[size] != '\0' || size != strlen(s)) { |
| 3151 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
| 3152 | return NULL; |
| 3153 | } |
| 3154 | |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 3155 | wchar = _Py_char2wchar(s, &len); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3156 | if (wchar == NULL) |
Victor Stinner | d5af0a5 | 2010-11-08 23:34:29 +0000 | [diff] [blame] | 3157 | return PyErr_NoMemory(); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3158 | |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 3159 | unicode = PyUnicode_FromWideChar(wchar, len); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3160 | PyMem_Free(wchar); |
| 3161 | return unicode; |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3162 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3163 | #endif |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3164 | } |
| 3165 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3166 | |
| 3167 | int |
| 3168 | PyUnicode_FSConverter(PyObject* arg, void* addr) |
| 3169 | { |
| 3170 | PyObject *output = NULL; |
| 3171 | Py_ssize_t size; |
| 3172 | void *data; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3173 | if (arg == NULL) { |
| 3174 | Py_DECREF(*(PyObject**)addr); |
| 3175 | return 1; |
| 3176 | } |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 3177 | if (PyBytes_Check(arg)) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3178 | output = arg; |
| 3179 | Py_INCREF(output); |
| 3180 | } |
| 3181 | else { |
| 3182 | arg = PyUnicode_FromObject(arg); |
| 3183 | if (!arg) |
| 3184 | return 0; |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3185 | output = PyUnicode_EncodeFSDefault(arg); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3186 | Py_DECREF(arg); |
| 3187 | if (!output) |
| 3188 | return 0; |
| 3189 | if (!PyBytes_Check(output)) { |
| 3190 | Py_DECREF(output); |
| 3191 | PyErr_SetString(PyExc_TypeError, "encoder failed to return bytes"); |
| 3192 | return 0; |
| 3193 | } |
| 3194 | } |
Victor Stinner | 0ea2a46 | 2010-04-30 00:22:08 +0000 | [diff] [blame] | 3195 | size = PyBytes_GET_SIZE(output); |
| 3196 | data = PyBytes_AS_STRING(output); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3197 | if (size != strlen(data)) { |
Benjamin Peterson | 7a6b44a | 2011-08-18 13:51:47 -0500 | [diff] [blame] | 3198 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3199 | Py_DECREF(output); |
| 3200 | return 0; |
| 3201 | } |
| 3202 | *(PyObject**)addr = output; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3203 | return Py_CLEANUP_SUPPORTED; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3204 | } |
| 3205 | |
| 3206 | |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3207 | int |
| 3208 | PyUnicode_FSDecoder(PyObject* arg, void* addr) |
| 3209 | { |
| 3210 | PyObject *output = NULL; |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3211 | if (arg == NULL) { |
| 3212 | Py_DECREF(*(PyObject**)addr); |
| 3213 | return 1; |
| 3214 | } |
| 3215 | if (PyUnicode_Check(arg)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3216 | if (PyUnicode_READY(arg)) |
| 3217 | return 0; |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3218 | output = arg; |
| 3219 | Py_INCREF(output); |
| 3220 | } |
| 3221 | else { |
| 3222 | arg = PyBytes_FromObject(arg); |
| 3223 | if (!arg) |
| 3224 | return 0; |
| 3225 | output = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AS_STRING(arg), |
| 3226 | PyBytes_GET_SIZE(arg)); |
| 3227 | Py_DECREF(arg); |
| 3228 | if (!output) |
| 3229 | return 0; |
| 3230 | if (!PyUnicode_Check(output)) { |
| 3231 | Py_DECREF(output); |
| 3232 | PyErr_SetString(PyExc_TypeError, "decoder failed to return unicode"); |
| 3233 | return 0; |
| 3234 | } |
| 3235 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3236 | if (findchar(PyUnicode_DATA(output), PyUnicode_KIND(output), |
| 3237 | PyUnicode_GET_LENGTH(output), 0, 1)) { |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3238 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
| 3239 | Py_DECREF(output); |
| 3240 | return 0; |
| 3241 | } |
| 3242 | *(PyObject**)addr = output; |
| 3243 | return Py_CLEANUP_SUPPORTED; |
| 3244 | } |
| 3245 | |
| 3246 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3247 | char* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3248 | PyUnicode_AsUTF8AndSize(PyObject *unicode, Py_ssize_t *psize) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3249 | { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 3250 | PyObject *bytes; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3251 | PyUnicodeObject *u = (PyUnicodeObject *)unicode; |
| 3252 | |
Neal Norwitz | e0a0a6e | 2007-08-25 01:04:21 +0000 | [diff] [blame] | 3253 | if (!PyUnicode_Check(unicode)) { |
| 3254 | PyErr_BadArgument(); |
| 3255 | return NULL; |
| 3256 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3257 | if (PyUnicode_READY(u) == -1) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3258 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3259 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3260 | if (PyUnicode_UTF8(unicode) == NULL) { |
| 3261 | assert(!PyUnicode_IS_COMPACT_ASCII(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3262 | bytes = _PyUnicode_AsUTF8String(unicode, "strict"); |
| 3263 | if (bytes == NULL) |
| 3264 | return NULL; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3265 | _PyUnicode_UTF8(u) = PyObject_MALLOC(PyBytes_GET_SIZE(bytes) + 1); |
| 3266 | if (_PyUnicode_UTF8(u) == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3267 | Py_DECREF(bytes); |
| 3268 | return NULL; |
| 3269 | } |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3270 | _PyUnicode_UTF8_LENGTH(u) = PyBytes_GET_SIZE(bytes); |
| 3271 | 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] | 3272 | Py_DECREF(bytes); |
| 3273 | } |
| 3274 | |
| 3275 | if (psize) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3276 | *psize = PyUnicode_UTF8_LENGTH(unicode); |
| 3277 | return PyUnicode_UTF8(unicode); |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 3278 | } |
| 3279 | |
| 3280 | char* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3281 | PyUnicode_AsUTF8(PyObject *unicode) |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 3282 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3283 | return PyUnicode_AsUTF8AndSize(unicode, NULL); |
| 3284 | } |
| 3285 | |
| 3286 | #ifdef Py_DEBUG |
| 3287 | int unicode_as_unicode_calls = 0; |
| 3288 | #endif |
| 3289 | |
| 3290 | |
| 3291 | Py_UNICODE * |
| 3292 | PyUnicode_AsUnicodeAndSize(PyObject *unicode, Py_ssize_t *size) |
| 3293 | { |
| 3294 | PyUnicodeObject *u; |
| 3295 | const unsigned char *one_byte; |
| 3296 | #if SIZEOF_WCHAR_T == 4 |
| 3297 | const Py_UCS2 *two_bytes; |
| 3298 | #else |
| 3299 | const Py_UCS4 *four_bytes; |
| 3300 | const Py_UCS4 *ucs4_end; |
| 3301 | Py_ssize_t num_surrogates; |
| 3302 | #endif |
| 3303 | wchar_t *w; |
| 3304 | wchar_t *wchar_end; |
| 3305 | |
| 3306 | if (!PyUnicode_Check(unicode)) { |
| 3307 | PyErr_BadArgument(); |
| 3308 | return NULL; |
| 3309 | } |
| 3310 | u = (PyUnicodeObject*)unicode; |
| 3311 | if (_PyUnicode_WSTR(u) == NULL) { |
| 3312 | /* Non-ASCII compact unicode object */ |
| 3313 | assert(_PyUnicode_KIND(u) != 0); |
| 3314 | assert(PyUnicode_IS_READY(u)); |
| 3315 | |
| 3316 | #ifdef Py_DEBUG |
| 3317 | ++unicode_as_unicode_calls; |
| 3318 | #endif |
| 3319 | |
| 3320 | if (PyUnicode_KIND(u) == PyUnicode_4BYTE_KIND) { |
| 3321 | #if SIZEOF_WCHAR_T == 2 |
| 3322 | four_bytes = PyUnicode_4BYTE_DATA(u); |
| 3323 | ucs4_end = four_bytes + _PyUnicode_LENGTH(u); |
| 3324 | num_surrogates = 0; |
| 3325 | |
| 3326 | for (; four_bytes < ucs4_end; ++four_bytes) { |
| 3327 | if (*four_bytes > 0xFFFF) |
| 3328 | ++num_surrogates; |
| 3329 | } |
| 3330 | |
| 3331 | _PyUnicode_WSTR(u) = (wchar_t *) PyObject_MALLOC( |
| 3332 | sizeof(wchar_t) * (_PyUnicode_LENGTH(u) + 1 + num_surrogates)); |
| 3333 | if (!_PyUnicode_WSTR(u)) { |
| 3334 | PyErr_NoMemory(); |
| 3335 | return NULL; |
| 3336 | } |
| 3337 | _PyUnicode_WSTR_LENGTH(u) = _PyUnicode_LENGTH(u) + num_surrogates; |
| 3338 | |
| 3339 | w = _PyUnicode_WSTR(u); |
| 3340 | wchar_end = w + _PyUnicode_WSTR_LENGTH(u); |
| 3341 | four_bytes = PyUnicode_4BYTE_DATA(u); |
| 3342 | for (; four_bytes < ucs4_end; ++four_bytes, ++w) { |
| 3343 | if (*four_bytes > 0xFFFF) { |
| 3344 | /* encode surrogate pair in this case */ |
| 3345 | *w++ = 0xD800 | ((*four_bytes - 0x10000) >> 10); |
| 3346 | *w = 0xDC00 | ((*four_bytes - 0x10000) & 0x3FF); |
| 3347 | } |
| 3348 | else |
| 3349 | *w = *four_bytes; |
| 3350 | |
| 3351 | if (w > wchar_end) { |
| 3352 | assert(0 && "Miscalculated string end"); |
| 3353 | } |
| 3354 | } |
| 3355 | *w = 0; |
| 3356 | #else |
| 3357 | /* sizeof(wchar_t) == 4 */ |
| 3358 | Py_FatalError("Impossible unicode object state, wstr and str " |
| 3359 | "should share memory already."); |
| 3360 | return NULL; |
| 3361 | #endif |
| 3362 | } |
| 3363 | else { |
| 3364 | _PyUnicode_WSTR(u) = (wchar_t *) PyObject_MALLOC(sizeof(wchar_t) * |
| 3365 | (_PyUnicode_LENGTH(u) + 1)); |
| 3366 | if (!_PyUnicode_WSTR(u)) { |
| 3367 | PyErr_NoMemory(); |
| 3368 | return NULL; |
| 3369 | } |
| 3370 | if (!PyUnicode_IS_COMPACT_ASCII(u)) |
| 3371 | _PyUnicode_WSTR_LENGTH(u) = _PyUnicode_LENGTH(u); |
| 3372 | w = _PyUnicode_WSTR(u); |
| 3373 | wchar_end = w + _PyUnicode_LENGTH(u); |
| 3374 | |
| 3375 | if (PyUnicode_KIND(u) == PyUnicode_1BYTE_KIND) { |
| 3376 | one_byte = PyUnicode_1BYTE_DATA(u); |
| 3377 | for (; w < wchar_end; ++one_byte, ++w) |
| 3378 | *w = *one_byte; |
| 3379 | /* null-terminate the wstr */ |
| 3380 | *w = 0; |
| 3381 | } |
| 3382 | else if (PyUnicode_KIND(u) == PyUnicode_2BYTE_KIND) { |
| 3383 | #if SIZEOF_WCHAR_T == 4 |
| 3384 | two_bytes = PyUnicode_2BYTE_DATA(u); |
| 3385 | for (; w < wchar_end; ++two_bytes, ++w) |
| 3386 | *w = *two_bytes; |
| 3387 | /* null-terminate the wstr */ |
| 3388 | *w = 0; |
| 3389 | #else |
| 3390 | /* sizeof(wchar_t) == 2 */ |
| 3391 | PyObject_FREE(_PyUnicode_WSTR(u)); |
| 3392 | _PyUnicode_WSTR(u) = NULL; |
| 3393 | Py_FatalError("Impossible unicode object state, wstr " |
| 3394 | "and str should share memory already."); |
| 3395 | return NULL; |
| 3396 | #endif |
| 3397 | } |
| 3398 | else { |
| 3399 | assert(0 && "This should never happen."); |
| 3400 | } |
| 3401 | } |
| 3402 | } |
| 3403 | if (size != NULL) |
| 3404 | *size = PyUnicode_WSTR_LENGTH(u); |
| 3405 | return _PyUnicode_WSTR(u); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3406 | } |
| 3407 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3408 | Py_UNICODE * |
| 3409 | PyUnicode_AsUnicode(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3410 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3411 | return PyUnicode_AsUnicodeAndSize(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3412 | } |
| 3413 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3414 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3415 | Py_ssize_t |
| 3416 | PyUnicode_GetSize(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3417 | { |
| 3418 | if (!PyUnicode_Check(unicode)) { |
| 3419 | PyErr_BadArgument(); |
| 3420 | goto onError; |
| 3421 | } |
| 3422 | return PyUnicode_GET_SIZE(unicode); |
| 3423 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3424 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3425 | return -1; |
| 3426 | } |
| 3427 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3428 | Py_ssize_t |
| 3429 | PyUnicode_GetLength(PyObject *unicode) |
| 3430 | { |
Victor Stinner | 5a706cf | 2011-10-02 00:36:53 +0200 | [diff] [blame] | 3431 | if (!PyUnicode_Check(unicode) || PyUnicode_READY(unicode) == -1) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3432 | PyErr_BadArgument(); |
| 3433 | return -1; |
| 3434 | } |
| 3435 | |
| 3436 | return PyUnicode_GET_LENGTH(unicode); |
| 3437 | } |
| 3438 | |
| 3439 | Py_UCS4 |
| 3440 | PyUnicode_ReadChar(PyObject *unicode, Py_ssize_t index) |
| 3441 | { |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 3442 | if (!PyUnicode_Check(unicode) || PyUnicode_READY(unicode) == -1) { |
| 3443 | PyErr_BadArgument(); |
| 3444 | return (Py_UCS4)-1; |
| 3445 | } |
| 3446 | if (index < 0 || index >= _PyUnicode_LENGTH(unicode)) { |
| 3447 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3448 | return (Py_UCS4)-1; |
| 3449 | } |
| 3450 | return PyUnicode_READ_CHAR(unicode, index); |
| 3451 | } |
| 3452 | |
| 3453 | int |
| 3454 | PyUnicode_WriteChar(PyObject *unicode, Py_ssize_t index, Py_UCS4 ch) |
| 3455 | { |
| 3456 | if (!PyUnicode_Check(unicode) || !PyUnicode_IS_COMPACT(unicode)) { |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 3457 | PyErr_BadArgument(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3458 | return -1; |
| 3459 | } |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 3460 | if (index < 0 || index >= _PyUnicode_LENGTH(unicode)) { |
| 3461 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 3462 | return -1; |
| 3463 | } |
| 3464 | if (_PyUnicode_Dirty(unicode)) |
| 3465 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3466 | PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode), |
| 3467 | index, ch); |
| 3468 | return 0; |
| 3469 | } |
| 3470 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3471 | const char * |
| 3472 | PyUnicode_GetDefaultEncoding(void) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3473 | { |
Victor Stinner | 42cb462 | 2010-09-01 19:39:01 +0000 | [diff] [blame] | 3474 | return "utf-8"; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3475 | } |
| 3476 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 3477 | /* create or adjust a UnicodeDecodeError */ |
| 3478 | static void |
| 3479 | make_decode_exception(PyObject **exceptionObject, |
| 3480 | const char *encoding, |
| 3481 | const char *input, Py_ssize_t length, |
| 3482 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 3483 | const char *reason) |
| 3484 | { |
| 3485 | if (*exceptionObject == NULL) { |
| 3486 | *exceptionObject = PyUnicodeDecodeError_Create( |
| 3487 | encoding, input, length, startpos, endpos, reason); |
| 3488 | } |
| 3489 | else { |
| 3490 | if (PyUnicodeDecodeError_SetStart(*exceptionObject, startpos)) |
| 3491 | goto onError; |
| 3492 | if (PyUnicodeDecodeError_SetEnd(*exceptionObject, endpos)) |
| 3493 | goto onError; |
| 3494 | if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) |
| 3495 | goto onError; |
| 3496 | } |
| 3497 | return; |
| 3498 | |
| 3499 | onError: |
| 3500 | Py_DECREF(*exceptionObject); |
| 3501 | *exceptionObject = NULL; |
| 3502 | } |
| 3503 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3504 | /* error handling callback helper: |
| 3505 | build arguments, call the callback and check the arguments, |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 3506 | if no exception occurred, copy the replacement to the output |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3507 | and adjust various state variables. |
| 3508 | return 0 on success, -1 on error |
| 3509 | */ |
| 3510 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3511 | static int |
| 3512 | unicode_decode_call_errorhandler(const char *errors, PyObject **errorHandler, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3513 | const char *encoding, const char *reason, |
| 3514 | const char **input, const char **inend, Py_ssize_t *startinpos, |
| 3515 | Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
| 3516 | PyUnicodeObject **output, Py_ssize_t *outpos, Py_UNICODE **outptr) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3517 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3518 | 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] | 3519 | |
| 3520 | PyObject *restuple = NULL; |
| 3521 | PyObject *repunicode = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3522 | Py_ssize_t outsize = PyUnicode_GET_SIZE(*output); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3523 | Py_ssize_t insize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3524 | Py_ssize_t requiredsize; |
| 3525 | Py_ssize_t newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3526 | const Py_UNICODE *repptr; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3527 | PyObject *inputobj = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3528 | Py_ssize_t repsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3529 | int res = -1; |
| 3530 | |
| 3531 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3532 | *errorHandler = PyCodec_LookupError(errors); |
| 3533 | if (*errorHandler == NULL) |
| 3534 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3535 | } |
| 3536 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 3537 | make_decode_exception(exceptionObject, |
| 3538 | encoding, |
| 3539 | *input, *inend - *input, |
| 3540 | *startinpos, *endinpos, |
| 3541 | reason); |
| 3542 | if (*exceptionObject == NULL) |
| 3543 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3544 | |
| 3545 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 3546 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3547 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3548 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 3549 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3550 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3551 | } |
| 3552 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3553 | goto onError; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3554 | |
| 3555 | /* Copy back the bytes variables, which might have been modified by the |
| 3556 | callback */ |
| 3557 | inputobj = PyUnicodeDecodeError_GetObject(*exceptionObject); |
| 3558 | if (!inputobj) |
| 3559 | goto onError; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3560 | if (!PyBytes_Check(inputobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3561 | PyErr_Format(PyExc_TypeError, "exception attribute object must be bytes"); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3562 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3563 | *input = PyBytes_AS_STRING(inputobj); |
| 3564 | insize = PyBytes_GET_SIZE(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3565 | *inend = *input + insize; |
Walter Dörwald | 36f938f | 2007-08-10 10:11:43 +0000 | [diff] [blame] | 3566 | /* we can DECREF safely, as the exception has another reference, |
| 3567 | so the object won't go away. */ |
| 3568 | Py_DECREF(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3569 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3570 | if (newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3571 | newpos = insize+newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 3572 | if (newpos<0 || newpos>insize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3573 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", newpos); |
| 3574 | goto onError; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 3575 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3576 | |
| 3577 | /* need more space? (at least enough for what we |
| 3578 | have+the replacement+the rest of the string (starting |
| 3579 | at the new input position), so we won't have to check space |
| 3580 | when there are no errors in the rest of the string) */ |
| 3581 | repptr = PyUnicode_AS_UNICODE(repunicode); |
| 3582 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 3583 | requiredsize = *outpos + repsize + insize-newpos; |
| 3584 | if (requiredsize > outsize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3585 | if (requiredsize<2*outsize) |
| 3586 | requiredsize = 2*outsize; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 3587 | if (PyUnicode_Resize((PyObject**)output, requiredsize) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3588 | goto onError; |
| 3589 | *outptr = PyUnicode_AS_UNICODE(*output) + *outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3590 | } |
| 3591 | *endinpos = newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3592 | *inptr = *input + newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3593 | Py_UNICODE_COPY(*outptr, repptr, repsize); |
| 3594 | *outptr += repsize; |
| 3595 | *outpos += repsize; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3596 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3597 | /* we made it! */ |
| 3598 | res = 0; |
| 3599 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3600 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3601 | Py_XDECREF(restuple); |
| 3602 | return res; |
| 3603 | } |
| 3604 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3605 | /* --- UTF-7 Codec -------------------------------------------------------- */ |
| 3606 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3607 | /* See RFC2152 for details. We encode conservatively and decode liberally. */ |
| 3608 | |
| 3609 | /* Three simple macros defining base-64. */ |
| 3610 | |
| 3611 | /* Is c a base-64 character? */ |
| 3612 | |
| 3613 | #define IS_BASE64(c) \ |
| 3614 | (((c) >= 'A' && (c) <= 'Z') || \ |
| 3615 | ((c) >= 'a' && (c) <= 'z') || \ |
| 3616 | ((c) >= '0' && (c) <= '9') || \ |
| 3617 | (c) == '+' || (c) == '/') |
| 3618 | |
| 3619 | /* given that c is a base-64 character, what is its base-64 value? */ |
| 3620 | |
| 3621 | #define FROM_BASE64(c) \ |
| 3622 | (((c) >= 'A' && (c) <= 'Z') ? (c) - 'A' : \ |
| 3623 | ((c) >= 'a' && (c) <= 'z') ? (c) - 'a' + 26 : \ |
| 3624 | ((c) >= '0' && (c) <= '9') ? (c) - '0' + 52 : \ |
| 3625 | (c) == '+' ? 62 : 63) |
| 3626 | |
| 3627 | /* What is the base-64 character of the bottom 6 bits of n? */ |
| 3628 | |
| 3629 | #define TO_BASE64(n) \ |
| 3630 | ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) |
| 3631 | |
| 3632 | /* DECODE_DIRECT: this byte encountered in a UTF-7 string should be |
| 3633 | * decoded as itself. We are permissive on decoding; the only ASCII |
| 3634 | * byte not decoding to itself is the + which begins a base64 |
| 3635 | * string. */ |
| 3636 | |
| 3637 | #define DECODE_DIRECT(c) \ |
| 3638 | ((c) <= 127 && (c) != '+') |
| 3639 | |
| 3640 | /* The UTF-7 encoder treats ASCII characters differently according to |
| 3641 | * whether they are Set D, Set O, Whitespace, or special (i.e. none of |
| 3642 | * the above). See RFC2152. This array identifies these different |
| 3643 | * sets: |
| 3644 | * 0 : "Set D" |
| 3645 | * alphanumeric and '(),-./:? |
| 3646 | * 1 : "Set O" |
| 3647 | * !"#$%&*;<=>@[]^_`{|} |
| 3648 | * 2 : "whitespace" |
| 3649 | * ht nl cr sp |
| 3650 | * 3 : special (must be base64 encoded) |
| 3651 | * everything else (i.e. +\~ and non-printing codes 0-8 11-12 14-31 127) |
| 3652 | */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3653 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3654 | static |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3655 | char utf7_category[128] = { |
| 3656 | /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */ |
| 3657 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 3, 3, 2, 3, 3, |
| 3658 | /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */ |
| 3659 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 3660 | /* sp ! " # $ % & ' ( ) * + , - . / */ |
| 3661 | 2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 3, 0, 0, 0, 0, |
| 3662 | /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */ |
| 3663 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, |
| 3664 | /* @ A B C D E F G H I J K L M N O */ |
| 3665 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 3666 | /* P Q R S T U V W X Y Z [ \ ] ^ _ */ |
| 3667 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 1, 1, 1, |
| 3668 | /* ` a b c d e f g h i j k l m n o */ |
| 3669 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 3670 | /* p q r s t u v w x y z { | } ~ del */ |
| 3671 | 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] | 3672 | }; |
| 3673 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3674 | /* ENCODE_DIRECT: this character should be encoded as itself. The |
| 3675 | * answer depends on whether we are encoding set O as itself, and also |
| 3676 | * on whether we are encoding whitespace as itself. RFC2152 makes it |
| 3677 | * clear that the answers to these questions vary between |
| 3678 | * applications, so this code needs to be flexible. */ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 3679 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3680 | #define ENCODE_DIRECT(c, directO, directWS) \ |
| 3681 | ((c) < 128 && (c) > 0 && \ |
| 3682 | ((utf7_category[(c)] == 0) || \ |
| 3683 | (directWS && (utf7_category[(c)] == 2)) || \ |
| 3684 | (directO && (utf7_category[(c)] == 1)))) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3685 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3686 | PyObject * |
| 3687 | PyUnicode_DecodeUTF7(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3688 | Py_ssize_t size, |
| 3689 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3690 | { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3691 | return PyUnicode_DecodeUTF7Stateful(s, size, errors, NULL); |
| 3692 | } |
| 3693 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3694 | /* The decoder. The only state we preserve is our read position, |
| 3695 | * i.e. how many characters we have consumed. So if we end in the |
| 3696 | * middle of a shift sequence we have to back off the read position |
| 3697 | * and the output to the beginning of the sequence, otherwise we lose |
| 3698 | * all the shift state (seen bits, number of bits seen, high |
| 3699 | * surrogate). */ |
| 3700 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3701 | PyObject * |
| 3702 | PyUnicode_DecodeUTF7Stateful(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3703 | Py_ssize_t size, |
| 3704 | const char *errors, |
| 3705 | Py_ssize_t *consumed) |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3706 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3707 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3708 | Py_ssize_t startinpos; |
| 3709 | Py_ssize_t endinpos; |
| 3710 | Py_ssize_t outpos; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3711 | const char *e; |
| 3712 | PyUnicodeObject *unicode; |
| 3713 | Py_UNICODE *p; |
| 3714 | const char *errmsg = ""; |
| 3715 | int inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3716 | Py_UNICODE *shiftOutStart; |
| 3717 | unsigned int base64bits = 0; |
| 3718 | unsigned long base64buffer = 0; |
| 3719 | Py_UNICODE surrogate = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3720 | PyObject *errorHandler = NULL; |
| 3721 | PyObject *exc = NULL; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3722 | |
| 3723 | unicode = _PyUnicode_New(size); |
| 3724 | if (!unicode) |
| 3725 | return NULL; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3726 | if (size == 0) { |
| 3727 | if (consumed) |
| 3728 | *consumed = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3729 | return (PyObject *)unicode; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3730 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3731 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3732 | p = PyUnicode_AS_UNICODE(unicode); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3733 | shiftOutStart = p; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3734 | e = s + size; |
| 3735 | |
| 3736 | while (s < e) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3737 | Py_UNICODE ch; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3738 | restart: |
Antoine Pitrou | 5ffd9e9 | 2008-07-25 18:05:24 +0000 | [diff] [blame] | 3739 | ch = (unsigned char) *s; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3740 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3741 | if (inShift) { /* in a base-64 section */ |
| 3742 | if (IS_BASE64(ch)) { /* consume a base-64 character */ |
| 3743 | base64buffer = (base64buffer << 6) | FROM_BASE64(ch); |
| 3744 | base64bits += 6; |
| 3745 | s++; |
| 3746 | if (base64bits >= 16) { |
| 3747 | /* we have enough bits for a UTF-16 value */ |
| 3748 | Py_UNICODE outCh = (Py_UNICODE) |
| 3749 | (base64buffer >> (base64bits-16)); |
| 3750 | base64bits -= 16; |
| 3751 | base64buffer &= (1 << base64bits) - 1; /* clear high bits */ |
| 3752 | if (surrogate) { |
| 3753 | /* expecting a second surrogate */ |
| 3754 | if (outCh >= 0xDC00 && outCh <= 0xDFFF) { |
| 3755 | #ifdef Py_UNICODE_WIDE |
| 3756 | *p++ = (((surrogate & 0x3FF)<<10) |
| 3757 | | (outCh & 0x3FF)) + 0x10000; |
| 3758 | #else |
| 3759 | *p++ = surrogate; |
| 3760 | *p++ = outCh; |
| 3761 | #endif |
| 3762 | surrogate = 0; |
| 3763 | } |
| 3764 | else { |
| 3765 | surrogate = 0; |
| 3766 | errmsg = "second surrogate missing"; |
| 3767 | goto utf7Error; |
| 3768 | } |
| 3769 | } |
| 3770 | else if (outCh >= 0xD800 && outCh <= 0xDBFF) { |
| 3771 | /* first surrogate */ |
| 3772 | surrogate = outCh; |
| 3773 | } |
| 3774 | else if (outCh >= 0xDC00 && outCh <= 0xDFFF) { |
| 3775 | errmsg = "unexpected second surrogate"; |
| 3776 | goto utf7Error; |
| 3777 | } |
| 3778 | else { |
| 3779 | *p++ = outCh; |
| 3780 | } |
| 3781 | } |
| 3782 | } |
| 3783 | else { /* now leaving a base-64 section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3784 | inShift = 0; |
| 3785 | s++; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3786 | if (surrogate) { |
| 3787 | errmsg = "second surrogate missing at end of shift sequence"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3788 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3789 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3790 | if (base64bits > 0) { /* left-over bits */ |
| 3791 | if (base64bits >= 6) { |
| 3792 | /* We've seen at least one base-64 character */ |
| 3793 | errmsg = "partial character in shift sequence"; |
| 3794 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3795 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3796 | else { |
| 3797 | /* Some bits remain; they should be zero */ |
| 3798 | if (base64buffer != 0) { |
| 3799 | errmsg = "non-zero padding bits in shift sequence"; |
| 3800 | goto utf7Error; |
| 3801 | } |
| 3802 | } |
| 3803 | } |
| 3804 | if (ch != '-') { |
| 3805 | /* '-' is absorbed; other terminating |
| 3806 | characters are preserved */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3807 | *p++ = ch; |
| 3808 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3809 | } |
| 3810 | } |
| 3811 | else if ( ch == '+' ) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3812 | startinpos = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3813 | s++; /* consume '+' */ |
| 3814 | if (s < e && *s == '-') { /* '+-' encodes '+' */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3815 | s++; |
| 3816 | *p++ = '+'; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3817 | } |
| 3818 | else { /* begin base64-encoded section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3819 | inShift = 1; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3820 | shiftOutStart = p; |
| 3821 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3822 | } |
| 3823 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3824 | else if (DECODE_DIRECT(ch)) { /* character decodes as itself */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3825 | *p++ = ch; |
| 3826 | s++; |
| 3827 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3828 | else { |
| 3829 | startinpos = s-starts; |
| 3830 | s++; |
| 3831 | errmsg = "unexpected special character"; |
| 3832 | goto utf7Error; |
| 3833 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3834 | continue; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3835 | utf7Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3836 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 3837 | endinpos = s-starts; |
| 3838 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3839 | errors, &errorHandler, |
| 3840 | "utf7", errmsg, |
| 3841 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 3842 | &unicode, &outpos, &p)) |
| 3843 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3844 | } |
| 3845 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3846 | /* end of string */ |
| 3847 | |
| 3848 | if (inShift && !consumed) { /* in shift sequence, no more to follow */ |
| 3849 | /* if we're in an inconsistent state, that's an error */ |
| 3850 | if (surrogate || |
| 3851 | (base64bits >= 6) || |
| 3852 | (base64bits > 0 && base64buffer != 0)) { |
| 3853 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 3854 | endinpos = size; |
| 3855 | if (unicode_decode_call_errorhandler( |
| 3856 | errors, &errorHandler, |
| 3857 | "utf7", "unterminated shift sequence", |
| 3858 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 3859 | &unicode, &outpos, &p)) |
| 3860 | goto onError; |
| 3861 | if (s < e) |
| 3862 | goto restart; |
| 3863 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3864 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3865 | |
| 3866 | /* return state */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3867 | if (consumed) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3868 | if (inShift) { |
| 3869 | p = shiftOutStart; /* back off output */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3870 | *consumed = startinpos; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3871 | } |
| 3872 | else { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3873 | *consumed = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3874 | } |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3875 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3876 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 3877 | if (PyUnicode_Resize((PyObject**)&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3878 | goto onError; |
| 3879 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3880 | Py_XDECREF(errorHandler); |
| 3881 | Py_XDECREF(exc); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 3882 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 3883 | if (_PyUnicode_READY_REPLACE(&unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3884 | Py_DECREF(unicode); |
| 3885 | return NULL; |
| 3886 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 3887 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 3888 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3889 | return (PyObject *)unicode; |
| 3890 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3891 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3892 | Py_XDECREF(errorHandler); |
| 3893 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3894 | Py_DECREF(unicode); |
| 3895 | return NULL; |
| 3896 | } |
| 3897 | |
| 3898 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3899 | PyObject * |
| 3900 | PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3901 | Py_ssize_t size, |
| 3902 | int base64SetO, |
| 3903 | int base64WhiteSpace, |
| 3904 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3905 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3906 | PyObject *v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3907 | /* It might be possible to tighten this worst case */ |
Alexandre Vassalotti | e85bd98 | 2009-07-21 00:39:03 +0000 | [diff] [blame] | 3908 | Py_ssize_t allocated = 8 * size; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3909 | int inShift = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3910 | Py_ssize_t i = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3911 | unsigned int base64bits = 0; |
| 3912 | unsigned long base64buffer = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3913 | char * out; |
| 3914 | char * start; |
| 3915 | |
| 3916 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3917 | return PyBytes_FromStringAndSize(NULL, 0); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3918 | |
Alexandre Vassalotti | e85bd98 | 2009-07-21 00:39:03 +0000 | [diff] [blame] | 3919 | if (allocated / 8 != size) |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3920 | return PyErr_NoMemory(); |
| 3921 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3922 | v = PyBytes_FromStringAndSize(NULL, allocated); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3923 | if (v == NULL) |
| 3924 | return NULL; |
| 3925 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3926 | start = out = PyBytes_AS_STRING(v); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3927 | for (;i < size; ++i) { |
| 3928 | Py_UNICODE ch = s[i]; |
| 3929 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3930 | if (inShift) { |
| 3931 | if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 3932 | /* shifting out */ |
| 3933 | if (base64bits) { /* output remaining bits */ |
| 3934 | *out++ = TO_BASE64(base64buffer << (6-base64bits)); |
| 3935 | base64buffer = 0; |
| 3936 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3937 | } |
| 3938 | inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3939 | /* Characters not in the BASE64 set implicitly unshift the sequence |
| 3940 | so no '-' is required, except if the character is itself a '-' */ |
| 3941 | if (IS_BASE64(ch) || ch == '-') { |
| 3942 | *out++ = '-'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3943 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3944 | *out++ = (char) ch; |
| 3945 | } |
| 3946 | else { |
| 3947 | goto encode_char; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3948 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3949 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3950 | else { /* not in a shift sequence */ |
| 3951 | if (ch == '+') { |
| 3952 | *out++ = '+'; |
| 3953 | *out++ = '-'; |
| 3954 | } |
| 3955 | else if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 3956 | *out++ = (char) ch; |
| 3957 | } |
| 3958 | else { |
| 3959 | *out++ = '+'; |
| 3960 | inShift = 1; |
| 3961 | goto encode_char; |
| 3962 | } |
| 3963 | } |
| 3964 | continue; |
| 3965 | encode_char: |
| 3966 | #ifdef Py_UNICODE_WIDE |
| 3967 | if (ch >= 0x10000) { |
| 3968 | /* code first surrogate */ |
| 3969 | base64bits += 16; |
| 3970 | base64buffer = (base64buffer << 16) | 0xd800 | ((ch-0x10000) >> 10); |
| 3971 | while (base64bits >= 6) { |
| 3972 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 3973 | base64bits -= 6; |
| 3974 | } |
| 3975 | /* prepare second surrogate */ |
| 3976 | ch = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 3977 | } |
| 3978 | #endif |
| 3979 | base64bits += 16; |
| 3980 | base64buffer = (base64buffer << 16) | ch; |
| 3981 | while (base64bits >= 6) { |
| 3982 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 3983 | base64bits -= 6; |
| 3984 | } |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 3985 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3986 | if (base64bits) |
| 3987 | *out++= TO_BASE64(base64buffer << (6-base64bits) ); |
| 3988 | if (inShift) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3989 | *out++ = '-'; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3990 | if (_PyBytes_Resize(&v, out - start) < 0) |
| 3991 | return NULL; |
| 3992 | return v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3993 | } |
| 3994 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3995 | #undef IS_BASE64 |
| 3996 | #undef FROM_BASE64 |
| 3997 | #undef TO_BASE64 |
| 3998 | #undef DECODE_DIRECT |
| 3999 | #undef ENCODE_DIRECT |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4000 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4001 | /* --- UTF-8 Codec -------------------------------------------------------- */ |
| 4002 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4003 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4004 | char utf8_code_length[256] = { |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4005 | /* Map UTF-8 encoded prefix byte to sequence length. Zero means |
| 4006 | illegal prefix. See RFC 3629 for details */ |
| 4007 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 00-0F */ |
| 4008 | 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] | 4009 | 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] | 4010 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 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, |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4014 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 70-7F */ |
| 4015 | 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] | 4016 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4017 | 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] | 4018 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* B0-BF */ |
| 4019 | 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* C0-C1 + C2-CF */ |
| 4020 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* D0-DF */ |
| 4021 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* E0-EF */ |
| 4022 | 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] | 4023 | }; |
| 4024 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4025 | PyObject * |
| 4026 | PyUnicode_DecodeUTF8(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4027 | Py_ssize_t size, |
| 4028 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4029 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4030 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
| 4031 | } |
| 4032 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4033 | /* Mask to check or force alignment of a pointer to C 'long' boundaries */ |
| 4034 | #define LONG_PTR_MASK (size_t) (SIZEOF_LONG - 1) |
| 4035 | |
| 4036 | /* Mask to quickly check whether a C 'long' contains a |
| 4037 | non-ASCII, UTF8-encoded char. */ |
| 4038 | #if (SIZEOF_LONG == 8) |
| 4039 | # define ASCII_CHAR_MASK 0x8080808080808080L |
| 4040 | #elif (SIZEOF_LONG == 4) |
| 4041 | # define ASCII_CHAR_MASK 0x80808080L |
| 4042 | #else |
| 4043 | # error C 'long' size should be either 4 or 8! |
| 4044 | #endif |
| 4045 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4046 | /* Scans a UTF-8 string and returns the maximum character to be expected, |
| 4047 | the size of the decoded unicode string and if any major errors were |
| 4048 | encountered. |
| 4049 | |
| 4050 | This function does check basic UTF-8 sanity, it does however NOT CHECK |
| 4051 | if the string contains surrogates, and if all continuation bytes are |
| 4052 | within the correct ranges, these checks are performed in |
| 4053 | PyUnicode_DecodeUTF8Stateful. |
| 4054 | |
| 4055 | If it sets has_errors to 1, it means the value of unicode_size and max_char |
| 4056 | will be bogus and you should not rely on useful information in them. |
| 4057 | */ |
| 4058 | static Py_UCS4 |
| 4059 | utf8_max_char_size_and_has_errors(const char *s, Py_ssize_t string_size, |
| 4060 | Py_ssize_t *unicode_size, Py_ssize_t* consumed, |
| 4061 | int *has_errors) |
| 4062 | { |
| 4063 | Py_ssize_t n; |
| 4064 | Py_ssize_t char_count = 0; |
| 4065 | Py_UCS4 max_char = 127, new_max; |
| 4066 | Py_UCS4 upper_bound; |
| 4067 | const unsigned char *p = (const unsigned char *)s; |
| 4068 | const unsigned char *end = p + string_size; |
| 4069 | const unsigned char *aligned_end = (const unsigned char *) ((size_t) end & ~LONG_PTR_MASK); |
| 4070 | int err = 0; |
| 4071 | |
| 4072 | for (; p < end && !err; ++p, ++char_count) { |
| 4073 | /* Only check value if it's not a ASCII char... */ |
| 4074 | if (*p < 0x80) { |
| 4075 | /* Fast path, see below in PyUnicode_DecodeUTF8Stateful for |
| 4076 | an explanation. */ |
| 4077 | if (!((size_t) p & LONG_PTR_MASK)) { |
| 4078 | /* Help register allocation */ |
| 4079 | register const unsigned char *_p = p; |
| 4080 | while (_p < aligned_end) { |
| 4081 | unsigned long value = *(unsigned long *) _p; |
| 4082 | if (value & ASCII_CHAR_MASK) |
| 4083 | break; |
| 4084 | _p += SIZEOF_LONG; |
| 4085 | char_count += SIZEOF_LONG; |
| 4086 | } |
| 4087 | p = _p; |
| 4088 | if (p == end) |
| 4089 | break; |
| 4090 | } |
| 4091 | } |
| 4092 | if (*p >= 0x80) { |
| 4093 | n = utf8_code_length[*p]; |
| 4094 | new_max = max_char; |
| 4095 | switch (n) { |
| 4096 | /* invalid start byte */ |
| 4097 | case 0: |
| 4098 | err = 1; |
| 4099 | break; |
| 4100 | case 2: |
| 4101 | /* Code points between 0x00FF and 0x07FF inclusive. |
| 4102 | Approximate the upper bound of the code point, |
| 4103 | if this flips over 255 we can be sure it will be more |
| 4104 | than 255 and the string will need 2 bytes per code coint, |
| 4105 | if it stays under or equal to 255, we can be sure 1 byte |
| 4106 | is enough. |
| 4107 | ((*p & 0b00011111) << 6) | 0b00111111 */ |
| 4108 | upper_bound = ((*p & 0x1F) << 6) | 0x3F; |
| 4109 | if (max_char < upper_bound) |
| 4110 | new_max = upper_bound; |
| 4111 | /* Ensure we track at least that we left ASCII space. */ |
| 4112 | if (new_max < 128) |
| 4113 | new_max = 128; |
| 4114 | break; |
| 4115 | case 3: |
| 4116 | /* Between 0x0FFF and 0xFFFF inclusive, so values are |
| 4117 | always > 255 and <= 65535 and will always need 2 bytes. */ |
| 4118 | if (max_char < 65535) |
| 4119 | new_max = 65535; |
| 4120 | break; |
| 4121 | case 4: |
| 4122 | /* Code point will be above 0xFFFF for sure in this case. */ |
| 4123 | new_max = 65537; |
| 4124 | break; |
| 4125 | /* Internal error, this should be caught by the first if */ |
| 4126 | case 1: |
| 4127 | default: |
| 4128 | assert(0 && "Impossible case in utf8_max_char_and_size"); |
| 4129 | err = 1; |
| 4130 | } |
| 4131 | /* Instead of number of overall bytes for this code point, |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 4132 | n contains the number of following bytes: */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4133 | --n; |
| 4134 | /* Check if the follow up chars are all valid continuation bytes */ |
| 4135 | if (n >= 1) { |
| 4136 | const unsigned char *cont; |
| 4137 | if ((p + n) >= end) { |
| 4138 | if (consumed == 0) |
| 4139 | /* incomplete data, non-incremental decoding */ |
| 4140 | err = 1; |
| 4141 | break; |
| 4142 | } |
| 4143 | for (cont = p + 1; cont < (p + n); ++cont) { |
| 4144 | if ((*cont & 0xc0) != 0x80) { |
| 4145 | err = 1; |
| 4146 | break; |
| 4147 | } |
| 4148 | } |
| 4149 | p += n; |
| 4150 | } |
| 4151 | else |
| 4152 | err = 1; |
| 4153 | max_char = new_max; |
| 4154 | } |
| 4155 | } |
| 4156 | |
| 4157 | if (unicode_size) |
| 4158 | *unicode_size = char_count; |
| 4159 | if (has_errors) |
| 4160 | *has_errors = err; |
| 4161 | return max_char; |
| 4162 | } |
| 4163 | |
| 4164 | /* Similar to PyUnicode_WRITE but can also write into wstr field |
| 4165 | of the legacy unicode representation */ |
| 4166 | #define WRITE_FLEXIBLE_OR_WSTR(kind, buf, index, value) \ |
| 4167 | do { \ |
| 4168 | const int k_ = (kind); \ |
| 4169 | if (k_ == PyUnicode_WCHAR_KIND) \ |
| 4170 | ((Py_UNICODE *)(buf))[(index)] = (Py_UNICODE)(value); \ |
| 4171 | else if (k_ == PyUnicode_1BYTE_KIND) \ |
| 4172 | ((unsigned char *)(buf))[(index)] = (unsigned char)(value); \ |
| 4173 | else if (k_ == PyUnicode_2BYTE_KIND) \ |
| 4174 | ((Py_UCS2 *)(buf))[(index)] = (Py_UCS2)(value); \ |
| 4175 | else \ |
| 4176 | ((Py_UCS4 *)(buf))[(index)] = (Py_UCS4)(value); \ |
| 4177 | } while (0) |
| 4178 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4179 | PyObject * |
| 4180 | PyUnicode_DecodeUTF8Stateful(const char *s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4181 | Py_ssize_t size, |
| 4182 | const char *errors, |
| 4183 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4184 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4185 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4186 | int n; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4187 | int k; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4188 | Py_ssize_t startinpos; |
| 4189 | Py_ssize_t endinpos; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4190 | const char *e, *aligned_end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4191 | PyUnicodeObject *unicode; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4192 | const char *errmsg = ""; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4193 | PyObject *errorHandler = NULL; |
| 4194 | PyObject *exc = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4195 | Py_UCS4 maxchar = 0; |
| 4196 | Py_ssize_t unicode_size; |
| 4197 | Py_ssize_t i; |
| 4198 | int kind; |
| 4199 | void *data; |
| 4200 | int has_errors; |
| 4201 | Py_UNICODE *error_outptr; |
| 4202 | #if SIZEOF_WCHAR_T == 2 |
| 4203 | Py_ssize_t wchar_offset = 0; |
| 4204 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4205 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4206 | if (size == 0) { |
| 4207 | if (consumed) |
| 4208 | *consumed = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4209 | return (PyObject *)PyUnicode_New(0, 0); |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4210 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4211 | maxchar = utf8_max_char_size_and_has_errors(s, size, &unicode_size, |
| 4212 | consumed, &has_errors); |
| 4213 | if (has_errors) { |
| 4214 | unicode = _PyUnicode_New(size); |
| 4215 | if (!unicode) |
| 4216 | return NULL; |
| 4217 | kind = PyUnicode_WCHAR_KIND; |
| 4218 | data = PyUnicode_AS_UNICODE(unicode); |
| 4219 | assert(data != NULL); |
| 4220 | } |
| 4221 | else { |
| 4222 | unicode = (PyUnicodeObject *)PyUnicode_New(unicode_size, maxchar); |
| 4223 | if (!unicode) |
| 4224 | return NULL; |
| 4225 | /* When the string is ASCII only, just use memcpy and return. |
| 4226 | unicode_size may be != size if there is an incomplete UTF-8 |
| 4227 | sequence at the end of the ASCII block. */ |
| 4228 | if (maxchar < 128 && size == unicode_size) { |
| 4229 | Py_MEMCPY(PyUnicode_1BYTE_DATA(unicode), s, unicode_size); |
| 4230 | return (PyObject *)unicode; |
| 4231 | } |
| 4232 | kind = PyUnicode_KIND(unicode); |
| 4233 | data = PyUnicode_DATA(unicode); |
| 4234 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4235 | /* Unpack UTF-8 encoded data */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4236 | i = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4237 | e = s + size; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4238 | aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4239 | |
| 4240 | while (s < e) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 4241 | Py_UCS4 ch = (unsigned char)*s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4242 | |
| 4243 | if (ch < 0x80) { |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4244 | /* Fast path for runs of ASCII characters. Given that common UTF-8 |
| 4245 | input will consist of an overwhelming majority of ASCII |
| 4246 | characters, we try to optimize for this case by checking |
| 4247 | as many characters as a C 'long' can contain. |
| 4248 | First, check if we can do an aligned read, as most CPUs have |
| 4249 | a penalty for unaligned reads. |
| 4250 | */ |
| 4251 | if (!((size_t) s & LONG_PTR_MASK)) { |
| 4252 | /* Help register allocation */ |
| 4253 | register const char *_s = s; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4254 | register Py_ssize_t _i = i; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4255 | while (_s < aligned_end) { |
| 4256 | /* Read a whole long at a time (either 4 or 8 bytes), |
| 4257 | and do a fast unrolled copy if it only contains ASCII |
| 4258 | characters. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4259 | unsigned long value = *(unsigned long *) _s; |
| 4260 | if (value & ASCII_CHAR_MASK) |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4261 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4262 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+0, _s[0]); |
| 4263 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+1, _s[1]); |
| 4264 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+2, _s[2]); |
| 4265 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+3, _s[3]); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4266 | #if (SIZEOF_LONG == 8) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4267 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+4, _s[4]); |
| 4268 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+5, _s[5]); |
| 4269 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+6, _s[6]); |
| 4270 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+7, _s[7]); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4271 | #endif |
| 4272 | _s += SIZEOF_LONG; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4273 | _i += SIZEOF_LONG; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4274 | } |
| 4275 | s = _s; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4276 | i = _i; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4277 | if (s == e) |
| 4278 | break; |
| 4279 | ch = (unsigned char)*s; |
| 4280 | } |
| 4281 | } |
| 4282 | |
| 4283 | if (ch < 0x80) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4284 | WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4285 | s++; |
| 4286 | continue; |
| 4287 | } |
| 4288 | |
| 4289 | n = utf8_code_length[ch]; |
| 4290 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4291 | if (s + n > e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4292 | if (consumed) |
| 4293 | break; |
| 4294 | else { |
| 4295 | errmsg = "unexpected end of data"; |
| 4296 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4297 | endinpos = startinpos+1; |
| 4298 | for (k=1; (k < size-startinpos) && ((s[k]&0xC0) == 0x80); k++) |
| 4299 | endinpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4300 | goto utf8Error; |
| 4301 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4302 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4303 | |
| 4304 | switch (n) { |
| 4305 | |
| 4306 | case 0: |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4307 | errmsg = "invalid start byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4308 | startinpos = s-starts; |
| 4309 | endinpos = startinpos+1; |
| 4310 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4311 | |
| 4312 | case 1: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4313 | errmsg = "internal error"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4314 | startinpos = s-starts; |
| 4315 | endinpos = startinpos+1; |
| 4316 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4317 | |
| 4318 | case 2: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4319 | if ((s[1] & 0xc0) != 0x80) { |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4320 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4321 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4322 | endinpos = startinpos + 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4323 | goto utf8Error; |
| 4324 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4325 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4326 | assert ((ch > 0x007F) && (ch <= 0x07FF)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4327 | WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4328 | break; |
| 4329 | |
| 4330 | case 3: |
Ezio Melotti | 9bf2b3a | 2010-07-03 04:52:19 +0000 | [diff] [blame] | 4331 | /* Decoding UTF-8 sequences in range \xed\xa0\x80-\xed\xbf\xbf |
| 4332 | will result in surrogates in range d800-dfff. Surrogates are |
| 4333 | not valid UTF-8 so they are rejected. |
| 4334 | See http://www.unicode.org/versions/Unicode5.2.0/ch03.pdf |
| 4335 | (table 3-7) and http://www.rfc-editor.org/rfc/rfc3629.txt */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4336 | if ((s[1] & 0xc0) != 0x80 || |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4337 | (s[2] & 0xc0) != 0x80 || |
| 4338 | ((unsigned char)s[0] == 0xE0 && |
| 4339 | (unsigned char)s[1] < 0xA0) || |
| 4340 | ((unsigned char)s[0] == 0xED && |
| 4341 | (unsigned char)s[1] > 0x9F)) { |
| 4342 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4343 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4344 | endinpos = startinpos + 1; |
| 4345 | |
| 4346 | /* if s[1] first two bits are 1 and 0, then the invalid |
| 4347 | continuation byte is s[2], so increment endinpos by 1, |
| 4348 | if not, s[1] is invalid and endinpos doesn't need to |
| 4349 | be incremented. */ |
| 4350 | if ((s[1] & 0xC0) == 0x80) |
| 4351 | endinpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4352 | goto utf8Error; |
| 4353 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4354 | ch = ((s[0] & 0x0f) << 12) + ((s[1] & 0x3f) << 6) + (s[2] & 0x3f); |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4355 | assert ((ch > 0x07FF) && (ch <= 0xFFFF)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4356 | WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, ch); |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 4357 | break; |
| 4358 | |
| 4359 | case 4: |
| 4360 | if ((s[1] & 0xc0) != 0x80 || |
| 4361 | (s[2] & 0xc0) != 0x80 || |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4362 | (s[3] & 0xc0) != 0x80 || |
| 4363 | ((unsigned char)s[0] == 0xF0 && |
| 4364 | (unsigned char)s[1] < 0x90) || |
| 4365 | ((unsigned char)s[0] == 0xF4 && |
| 4366 | (unsigned char)s[1] > 0x8F)) { |
| 4367 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4368 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4369 | endinpos = startinpos + 1; |
| 4370 | if ((s[1] & 0xC0) == 0x80) { |
| 4371 | endinpos++; |
| 4372 | if ((s[2] & 0xC0) == 0x80) |
| 4373 | endinpos++; |
| 4374 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4375 | goto utf8Error; |
| 4376 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 4377 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4378 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
| 4379 | assert ((ch > 0xFFFF) && (ch <= 0x10ffff)); |
| 4380 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4381 | /* If the string is flexible or we have native UCS-4, write |
| 4382 | directly.. */ |
| 4383 | if (sizeof(Py_UNICODE) > 2 || kind != PyUnicode_WCHAR_KIND) |
| 4384 | WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, ch); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4385 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4386 | else { |
| 4387 | /* compute and append the two surrogates: */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4388 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4389 | /* translate from 10000..10FFFF to 0..FFFF */ |
| 4390 | ch -= 0x10000; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4391 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4392 | /* high surrogate = top 10 bits added to D800 */ |
| 4393 | WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, |
| 4394 | (Py_UNICODE)(0xD800 + (ch >> 10))); |
| 4395 | |
| 4396 | /* low surrogate = bottom 10 bits added to DC00 */ |
| 4397 | WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, |
| 4398 | (Py_UNICODE)(0xDC00 + (ch & 0x03FF))); |
| 4399 | } |
| 4400 | #if SIZEOF_WCHAR_T == 2 |
| 4401 | wchar_offset++; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 4402 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4403 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4404 | } |
| 4405 | s += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4406 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4407 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4408 | utf8Error: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4409 | /* If this is not yet a resizable string, make it one.. */ |
| 4410 | if (kind != PyUnicode_WCHAR_KIND) { |
| 4411 | const Py_UNICODE *u; |
| 4412 | PyUnicodeObject *new_unicode = _PyUnicode_New(size); |
| 4413 | if (!new_unicode) |
| 4414 | goto onError; |
| 4415 | u = PyUnicode_AsUnicode((PyObject *)unicode); |
| 4416 | if (!u) |
| 4417 | goto onError; |
| 4418 | #if SIZEOF_WCHAR_T == 2 |
| 4419 | i += wchar_offset; |
| 4420 | #endif |
| 4421 | Py_UNICODE_COPY(PyUnicode_AS_UNICODE(new_unicode), u, i); |
| 4422 | Py_DECREF(unicode); |
| 4423 | unicode = new_unicode; |
| 4424 | kind = 0; |
| 4425 | data = PyUnicode_AS_UNICODE(new_unicode); |
| 4426 | assert(data != NULL); |
| 4427 | } |
| 4428 | error_outptr = PyUnicode_AS_UNICODE(unicode) + i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4429 | if (unicode_decode_call_errorhandler( |
| 4430 | errors, &errorHandler, |
| 4431 | "utf8", errmsg, |
| 4432 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4433 | &unicode, &i, &error_outptr)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4434 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4435 | /* Update data because unicode_decode_call_errorhandler might have |
| 4436 | re-created or resized the unicode object. */ |
| 4437 | data = PyUnicode_AS_UNICODE(unicode); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4438 | aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4439 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4440 | /* Ensure the unicode_size calculation above was correct: */ |
| 4441 | assert(kind == PyUnicode_WCHAR_KIND || i == unicode_size); |
| 4442 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4443 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4444 | *consumed = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4445 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4446 | /* Adjust length and ready string when it contained errors and |
| 4447 | is of the old resizable kind. */ |
| 4448 | if (kind == PyUnicode_WCHAR_KIND) { |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 4449 | if (PyUnicode_Resize((PyObject**)&unicode, i) < 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4450 | goto onError; |
| 4451 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4452 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4453 | Py_XDECREF(errorHandler); |
| 4454 | Py_XDECREF(exc); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 4455 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 4456 | if (_PyUnicode_READY_REPLACE(&unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4457 | Py_DECREF(unicode); |
| 4458 | return NULL; |
| 4459 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 4460 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 4461 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4462 | return (PyObject *)unicode; |
| 4463 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4464 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4465 | Py_XDECREF(errorHandler); |
| 4466 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4467 | Py_DECREF(unicode); |
| 4468 | return NULL; |
| 4469 | } |
| 4470 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4471 | #undef WRITE_FLEXIBLE_OR_WSTR |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4472 | |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4473 | #ifdef __APPLE__ |
| 4474 | |
| 4475 | /* Simplified UTF-8 decoder using surrogateescape error handler, |
| 4476 | used to decode the command line arguments on Mac OS X. */ |
| 4477 | |
| 4478 | wchar_t* |
| 4479 | _Py_DecodeUTF8_surrogateescape(const char *s, Py_ssize_t size) |
| 4480 | { |
| 4481 | int n; |
| 4482 | const char *e; |
| 4483 | wchar_t *unicode, *p; |
| 4484 | |
| 4485 | /* Note: size will always be longer than the resulting Unicode |
| 4486 | character count */ |
| 4487 | if (PY_SSIZE_T_MAX / sizeof(wchar_t) < (size + 1)) { |
| 4488 | PyErr_NoMemory(); |
| 4489 | return NULL; |
| 4490 | } |
| 4491 | unicode = PyMem_Malloc((size + 1) * sizeof(wchar_t)); |
| 4492 | if (!unicode) |
| 4493 | return NULL; |
| 4494 | |
| 4495 | /* Unpack UTF-8 encoded data */ |
| 4496 | p = unicode; |
| 4497 | e = s + size; |
| 4498 | while (s < e) { |
| 4499 | Py_UCS4 ch = (unsigned char)*s; |
| 4500 | |
| 4501 | if (ch < 0x80) { |
| 4502 | *p++ = (wchar_t)ch; |
| 4503 | s++; |
| 4504 | continue; |
| 4505 | } |
| 4506 | |
| 4507 | n = utf8_code_length[ch]; |
| 4508 | if (s + n > e) { |
| 4509 | goto surrogateescape; |
| 4510 | } |
| 4511 | |
| 4512 | switch (n) { |
| 4513 | case 0: |
| 4514 | case 1: |
| 4515 | goto surrogateescape; |
| 4516 | |
| 4517 | case 2: |
| 4518 | if ((s[1] & 0xc0) != 0x80) |
| 4519 | goto surrogateescape; |
| 4520 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
| 4521 | assert ((ch > 0x007F) && (ch <= 0x07FF)); |
| 4522 | *p++ = (wchar_t)ch; |
| 4523 | break; |
| 4524 | |
| 4525 | case 3: |
| 4526 | /* Decoding UTF-8 sequences in range \xed\xa0\x80-\xed\xbf\xbf |
| 4527 | will result in surrogates in range d800-dfff. Surrogates are |
| 4528 | not valid UTF-8 so they are rejected. |
| 4529 | See http://www.unicode.org/versions/Unicode5.2.0/ch03.pdf |
| 4530 | (table 3-7) and http://www.rfc-editor.org/rfc/rfc3629.txt */ |
| 4531 | if ((s[1] & 0xc0) != 0x80 || |
| 4532 | (s[2] & 0xc0) != 0x80 || |
| 4533 | ((unsigned char)s[0] == 0xE0 && |
| 4534 | (unsigned char)s[1] < 0xA0) || |
| 4535 | ((unsigned char)s[0] == 0xED && |
| 4536 | (unsigned char)s[1] > 0x9F)) { |
| 4537 | |
| 4538 | goto surrogateescape; |
| 4539 | } |
| 4540 | ch = ((s[0] & 0x0f) << 12) + ((s[1] & 0x3f) << 6) + (s[2] & 0x3f); |
| 4541 | assert ((ch > 0x07FF) && (ch <= 0xFFFF)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4542 | *p++ = (wchar_t)ch; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4543 | break; |
| 4544 | |
| 4545 | case 4: |
| 4546 | if ((s[1] & 0xc0) != 0x80 || |
| 4547 | (s[2] & 0xc0) != 0x80 || |
| 4548 | (s[3] & 0xc0) != 0x80 || |
| 4549 | ((unsigned char)s[0] == 0xF0 && |
| 4550 | (unsigned char)s[1] < 0x90) || |
| 4551 | ((unsigned char)s[0] == 0xF4 && |
| 4552 | (unsigned char)s[1] > 0x8F)) { |
| 4553 | goto surrogateescape; |
| 4554 | } |
| 4555 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
| 4556 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
| 4557 | assert ((ch > 0xFFFF) && (ch <= 0x10ffff)); |
| 4558 | |
| 4559 | #if SIZEOF_WCHAR_T == 4 |
| 4560 | *p++ = (wchar_t)ch; |
| 4561 | #else |
| 4562 | /* compute and append the two surrogates: */ |
| 4563 | |
| 4564 | /* translate from 10000..10FFFF to 0..FFFF */ |
| 4565 | ch -= 0x10000; |
| 4566 | |
| 4567 | /* high surrogate = top 10 bits added to D800 */ |
| 4568 | *p++ = (wchar_t)(0xD800 + (ch >> 10)); |
| 4569 | |
| 4570 | /* low surrogate = bottom 10 bits added to DC00 */ |
| 4571 | *p++ = (wchar_t)(0xDC00 + (ch & 0x03FF)); |
| 4572 | #endif |
| 4573 | break; |
| 4574 | } |
| 4575 | s += n; |
| 4576 | continue; |
| 4577 | |
| 4578 | surrogateescape: |
| 4579 | *p++ = 0xDC00 + ch; |
| 4580 | s++; |
| 4581 | } |
| 4582 | *p = L'\0'; |
| 4583 | return unicode; |
| 4584 | } |
| 4585 | |
| 4586 | #endif /* __APPLE__ */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4587 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4588 | /* Primary internal function which creates utf8 encoded bytes objects. |
| 4589 | |
| 4590 | Allocation strategy: if the string is short, convert into a stack buffer |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4591 | and allocate exactly as much space needed at the end. Else allocate the |
| 4592 | maximum possible needed (4 result bytes per Unicode character), and return |
| 4593 | the excess memory at the end. |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4594 | */ |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 4595 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4596 | _PyUnicode_AsUTF8String(PyObject *obj, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4597 | { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4598 | #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] | 4599 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4600 | Py_ssize_t i; /* index into s of next input byte */ |
| 4601 | PyObject *result; /* result string object */ |
| 4602 | char *p; /* next free byte in output buffer */ |
| 4603 | Py_ssize_t nallocated; /* number of result bytes allocated */ |
| 4604 | Py_ssize_t nneeded; /* number of result bytes needed */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4605 | char stackbuf[MAX_SHORT_UNICHARS * 4]; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4606 | PyObject *errorHandler = NULL; |
| 4607 | PyObject *exc = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4608 | int kind; |
| 4609 | void *data; |
| 4610 | Py_ssize_t size; |
| 4611 | PyUnicodeObject *unicode = (PyUnicodeObject *)obj; |
| 4612 | #if SIZEOF_WCHAR_T == 2 |
| 4613 | Py_ssize_t wchar_offset = 0; |
| 4614 | #endif |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 4615 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4616 | if (!PyUnicode_Check(unicode)) { |
| 4617 | PyErr_BadArgument(); |
| 4618 | return NULL; |
| 4619 | } |
| 4620 | |
| 4621 | if (PyUnicode_READY(unicode) == -1) |
| 4622 | return NULL; |
| 4623 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 4624 | if (PyUnicode_UTF8(unicode)) |
| 4625 | return PyBytes_FromStringAndSize(PyUnicode_UTF8(unicode), |
| 4626 | PyUnicode_UTF8_LENGTH(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4627 | |
| 4628 | kind = PyUnicode_KIND(unicode); |
| 4629 | data = PyUnicode_DATA(unicode); |
| 4630 | size = PyUnicode_GET_LENGTH(unicode); |
| 4631 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4632 | assert(size >= 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4633 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4634 | if (size <= MAX_SHORT_UNICHARS) { |
| 4635 | /* Write into the stack buffer; nallocated can't overflow. |
| 4636 | * At the end, we'll allocate exactly as much heap space as it |
| 4637 | * turns out we need. |
| 4638 | */ |
| 4639 | nallocated = Py_SAFE_DOWNCAST(sizeof(stackbuf), size_t, int); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4640 | result = NULL; /* will allocate after we're done */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4641 | p = stackbuf; |
| 4642 | } |
| 4643 | else { |
| 4644 | /* Overallocate on the heap, and give the excess back at the end. */ |
| 4645 | nallocated = size * 4; |
| 4646 | if (nallocated / 4 != size) /* overflow! */ |
| 4647 | return PyErr_NoMemory(); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4648 | result = PyBytes_FromStringAndSize(NULL, nallocated); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4649 | if (result == NULL) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4650 | return NULL; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4651 | p = PyBytes_AS_STRING(result); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4652 | } |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4653 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4654 | for (i = 0; i < size;) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4655 | Py_UCS4 ch = PyUnicode_READ(kind, data, i++); |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 4656 | |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4657 | if (ch < 0x80) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4658 | /* Encode ASCII */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4659 | *p++ = (char) ch; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 4660 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4661 | else if (ch < 0x0800) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4662 | /* Encode Latin-1 */ |
Marc-André Lemburg | dc724d6 | 2002-02-06 18:20:19 +0000 | [diff] [blame] | 4663 | *p++ = (char)(0xc0 | (ch >> 6)); |
| 4664 | *p++ = (char)(0x80 | (ch & 0x3f)); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4665 | } else if (0xD800 <= ch && ch <= 0xDFFF) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4666 | Py_ssize_t newpos; |
| 4667 | PyObject *rep; |
| 4668 | Py_ssize_t repsize, k, startpos; |
| 4669 | startpos = i-1; |
| 4670 | #if SIZEOF_WCHAR_T == 2 |
| 4671 | startpos += wchar_offset; |
Victor Stinner | 445a623 | 2010-04-22 20:01:57 +0000 | [diff] [blame] | 4672 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4673 | rep = unicode_encode_call_errorhandler( |
| 4674 | errors, &errorHandler, "utf-8", "surrogates not allowed", |
| 4675 | PyUnicode_AS_UNICODE(unicode), PyUnicode_GET_SIZE(unicode), |
| 4676 | &exc, startpos, startpos+1, &newpos); |
| 4677 | if (!rep) |
| 4678 | goto error; |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4679 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4680 | if (PyBytes_Check(rep)) |
| 4681 | repsize = PyBytes_GET_SIZE(rep); |
| 4682 | else |
| 4683 | repsize = PyUnicode_GET_SIZE(rep); |
| 4684 | |
| 4685 | if (repsize > 4) { |
| 4686 | Py_ssize_t offset; |
| 4687 | |
| 4688 | if (result == NULL) |
| 4689 | offset = p - stackbuf; |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4690 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4691 | offset = p - PyBytes_AS_STRING(result); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4692 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4693 | if (nallocated > PY_SSIZE_T_MAX - repsize + 4) { |
| 4694 | /* integer overflow */ |
| 4695 | PyErr_NoMemory(); |
| 4696 | goto error; |
| 4697 | } |
| 4698 | nallocated += repsize - 4; |
| 4699 | if (result != NULL) { |
| 4700 | if (_PyBytes_Resize(&result, nallocated) < 0) |
| 4701 | goto error; |
| 4702 | } else { |
| 4703 | result = PyBytes_FromStringAndSize(NULL, nallocated); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4704 | if (result == NULL) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4705 | goto error; |
| 4706 | Py_MEMCPY(PyBytes_AS_STRING(result), stackbuf, offset); |
| 4707 | } |
| 4708 | p = PyBytes_AS_STRING(result) + offset; |
| 4709 | } |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4710 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4711 | if (PyBytes_Check(rep)) { |
| 4712 | char *prep = PyBytes_AS_STRING(rep); |
| 4713 | for(k = repsize; k > 0; k--) |
| 4714 | *p++ = *prep++; |
| 4715 | } else /* rep is unicode */ { |
| 4716 | const Py_UNICODE *prep = PyUnicode_AS_UNICODE(rep); |
| 4717 | Py_UNICODE c; |
| 4718 | |
| 4719 | for(k=0; k<repsize; k++) { |
| 4720 | c = prep[k]; |
| 4721 | if (0x80 <= c) { |
| 4722 | raise_encode_exception(&exc, "utf-8", |
| 4723 | PyUnicode_AS_UNICODE(unicode), |
| 4724 | size, i-1, i, |
| 4725 | "surrogates not allowed"); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4726 | goto error; |
| 4727 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4728 | *p++ = (char)prep[k]; |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4729 | } |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4730 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4731 | Py_DECREF(rep); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4732 | } else if (ch < 0x10000) { |
| 4733 | *p++ = (char)(0xe0 | (ch >> 12)); |
| 4734 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 4735 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 4736 | } else /* ch >= 0x10000 */ { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4737 | /* Encode UCS4 Unicode ordinals */ |
| 4738 | *p++ = (char)(0xf0 | (ch >> 18)); |
| 4739 | *p++ = (char)(0x80 | ((ch >> 12) & 0x3f)); |
| 4740 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 4741 | *p++ = (char)(0x80 | (ch & 0x3f)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4742 | #if SIZEOF_WCHAR_T == 2 |
| 4743 | wchar_offset++; |
| 4744 | #endif |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4745 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4746 | } |
Tim Peters | 0eca65c | 2002-04-21 17:28:06 +0000 | [diff] [blame] | 4747 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4748 | if (result == NULL) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4749 | /* This was stack allocated. */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4750 | nneeded = p - stackbuf; |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4751 | assert(nneeded <= nallocated); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4752 | result = PyBytes_FromStringAndSize(stackbuf, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4753 | } |
| 4754 | else { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 4755 | /* Cut back to size actually needed. */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4756 | nneeded = p - PyBytes_AS_STRING(result); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4757 | assert(nneeded <= nallocated); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4758 | _PyBytes_Resize(&result, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4759 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4760 | |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4761 | Py_XDECREF(errorHandler); |
| 4762 | Py_XDECREF(exc); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4763 | return result; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4764 | error: |
| 4765 | Py_XDECREF(errorHandler); |
| 4766 | Py_XDECREF(exc); |
| 4767 | Py_XDECREF(result); |
| 4768 | return NULL; |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4769 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4770 | #undef MAX_SHORT_UNICHARS |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4771 | } |
| 4772 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4773 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4774 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
| 4775 | Py_ssize_t size, |
| 4776 | const char *errors) |
| 4777 | { |
| 4778 | PyObject *v, *unicode; |
| 4779 | |
| 4780 | unicode = PyUnicode_FromUnicode(s, size); |
| 4781 | if (unicode == NULL) |
| 4782 | return NULL; |
| 4783 | v = _PyUnicode_AsUTF8String(unicode, errors); |
| 4784 | Py_DECREF(unicode); |
| 4785 | return v; |
| 4786 | } |
| 4787 | |
| 4788 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4789 | PyUnicode_AsUTF8String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4790 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4791 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4792 | } |
| 4793 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4794 | /* --- UTF-32 Codec ------------------------------------------------------- */ |
| 4795 | |
| 4796 | PyObject * |
| 4797 | PyUnicode_DecodeUTF32(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4798 | Py_ssize_t size, |
| 4799 | const char *errors, |
| 4800 | int *byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4801 | { |
| 4802 | return PyUnicode_DecodeUTF32Stateful(s, size, errors, byteorder, NULL); |
| 4803 | } |
| 4804 | |
| 4805 | PyObject * |
| 4806 | PyUnicode_DecodeUTF32Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4807 | Py_ssize_t size, |
| 4808 | const char *errors, |
| 4809 | int *byteorder, |
| 4810 | Py_ssize_t *consumed) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4811 | { |
| 4812 | const char *starts = s; |
| 4813 | Py_ssize_t startinpos; |
| 4814 | Py_ssize_t endinpos; |
| 4815 | Py_ssize_t outpos; |
| 4816 | PyUnicodeObject *unicode; |
| 4817 | Py_UNICODE *p; |
| 4818 | #ifndef Py_UNICODE_WIDE |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 4819 | int pairs = 0; |
Mark Dickinson | 7db923c | 2010-06-12 09:10:14 +0000 | [diff] [blame] | 4820 | const unsigned char *qq; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4821 | #else |
| 4822 | const int pairs = 0; |
| 4823 | #endif |
Mark Dickinson | 7db923c | 2010-06-12 09:10:14 +0000 | [diff] [blame] | 4824 | const unsigned char *q, *e; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4825 | int bo = 0; /* assume native ordering by default */ |
| 4826 | const char *errmsg = ""; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4827 | /* Offsets from q for retrieving bytes in the right order. */ |
| 4828 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 4829 | int iorder[] = {0, 1, 2, 3}; |
| 4830 | #else |
| 4831 | int iorder[] = {3, 2, 1, 0}; |
| 4832 | #endif |
| 4833 | PyObject *errorHandler = NULL; |
| 4834 | PyObject *exc = NULL; |
Victor Stinner | 313a120 | 2010-06-11 23:56:51 +0000 | [diff] [blame] | 4835 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4836 | q = (unsigned char *)s; |
| 4837 | e = q + size; |
| 4838 | |
| 4839 | if (byteorder) |
| 4840 | bo = *byteorder; |
| 4841 | |
| 4842 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 4843 | byte order setting accordingly. In native mode, the leading BOM |
| 4844 | mark is skipped, in all other modes, it is copied to the output |
| 4845 | stream as-is (giving a ZWNBSP character). */ |
| 4846 | if (bo == 0) { |
| 4847 | if (size >= 4) { |
| 4848 | const Py_UCS4 bom = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4849 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4850 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4851 | if (bom == 0x0000FEFF) { |
| 4852 | q += 4; |
| 4853 | bo = -1; |
| 4854 | } |
| 4855 | else if (bom == 0xFFFE0000) { |
| 4856 | q += 4; |
| 4857 | bo = 1; |
| 4858 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4859 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4860 | if (bom == 0x0000FEFF) { |
| 4861 | q += 4; |
| 4862 | bo = 1; |
| 4863 | } |
| 4864 | else if (bom == 0xFFFE0000) { |
| 4865 | q += 4; |
| 4866 | bo = -1; |
| 4867 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4868 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4869 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4870 | } |
| 4871 | |
| 4872 | if (bo == -1) { |
| 4873 | /* force LE */ |
| 4874 | iorder[0] = 0; |
| 4875 | iorder[1] = 1; |
| 4876 | iorder[2] = 2; |
| 4877 | iorder[3] = 3; |
| 4878 | } |
| 4879 | else if (bo == 1) { |
| 4880 | /* force BE */ |
| 4881 | iorder[0] = 3; |
| 4882 | iorder[1] = 2; |
| 4883 | iorder[2] = 1; |
| 4884 | iorder[3] = 0; |
| 4885 | } |
| 4886 | |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 4887 | /* On narrow builds we split characters outside the BMP into two |
| 4888 | codepoints => count how much extra space we need. */ |
| 4889 | #ifndef Py_UNICODE_WIDE |
| 4890 | for (qq = q; qq < e; qq += 4) |
| 4891 | if (qq[iorder[2]] != 0 || qq[iorder[3]] != 0) |
| 4892 | pairs++; |
| 4893 | #endif |
| 4894 | |
| 4895 | /* This might be one to much, because of a BOM */ |
| 4896 | unicode = _PyUnicode_New((size+3)/4+pairs); |
| 4897 | if (!unicode) |
| 4898 | return NULL; |
| 4899 | if (size == 0) |
| 4900 | return (PyObject *)unicode; |
| 4901 | |
| 4902 | /* Unpack UTF-32 encoded data */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4903 | p = PyUnicode_AS_UNICODE(unicode); |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 4904 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4905 | while (q < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4906 | Py_UCS4 ch; |
| 4907 | /* remaining bytes at the end? (size should be divisible by 4) */ |
| 4908 | if (e-q<4) { |
| 4909 | if (consumed) |
| 4910 | break; |
| 4911 | errmsg = "truncated data"; |
| 4912 | startinpos = ((const char *)q)-starts; |
| 4913 | endinpos = ((const char *)e)-starts; |
| 4914 | goto utf32Error; |
| 4915 | /* The remaining input chars are ignored if the callback |
| 4916 | chooses to skip the input */ |
| 4917 | } |
| 4918 | ch = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
| 4919 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4920 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4921 | if (ch >= 0x110000) |
| 4922 | { |
| 4923 | errmsg = "codepoint not in range(0x110000)"; |
| 4924 | startinpos = ((const char *)q)-starts; |
| 4925 | endinpos = startinpos+4; |
| 4926 | goto utf32Error; |
| 4927 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4928 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4929 | if (ch >= 0x10000) |
| 4930 | { |
| 4931 | *p++ = 0xD800 | ((ch-0x10000) >> 10); |
| 4932 | *p++ = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 4933 | } |
| 4934 | else |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4935 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4936 | *p++ = ch; |
| 4937 | q += 4; |
| 4938 | continue; |
| 4939 | utf32Error: |
| 4940 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 4941 | if (unicode_decode_call_errorhandler( |
| 4942 | errors, &errorHandler, |
| 4943 | "utf32", errmsg, |
| 4944 | &starts, (const char **)&e, &startinpos, &endinpos, &exc, (const char **)&q, |
| 4945 | &unicode, &outpos, &p)) |
| 4946 | goto onError; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4947 | } |
| 4948 | |
| 4949 | if (byteorder) |
| 4950 | *byteorder = bo; |
| 4951 | |
| 4952 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4953 | *consumed = (const char *)q-starts; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4954 | |
| 4955 | /* Adjust length */ |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 4956 | if (PyUnicode_Resize((PyObject**)&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4957 | goto onError; |
| 4958 | |
| 4959 | Py_XDECREF(errorHandler); |
| 4960 | Py_XDECREF(exc); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 4961 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 4962 | if (_PyUnicode_READY_REPLACE(&unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4963 | Py_DECREF(unicode); |
| 4964 | return NULL; |
| 4965 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 4966 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 4967 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4968 | return (PyObject *)unicode; |
| 4969 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4970 | onError: |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4971 | Py_DECREF(unicode); |
| 4972 | Py_XDECREF(errorHandler); |
| 4973 | Py_XDECREF(exc); |
| 4974 | return NULL; |
| 4975 | } |
| 4976 | |
| 4977 | PyObject * |
| 4978 | PyUnicode_EncodeUTF32(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4979 | Py_ssize_t size, |
| 4980 | const char *errors, |
| 4981 | int byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4982 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4983 | PyObject *v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4984 | unsigned char *p; |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4985 | Py_ssize_t nsize, bytesize; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4986 | #ifndef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4987 | Py_ssize_t i, pairs; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4988 | #else |
| 4989 | const int pairs = 0; |
| 4990 | #endif |
| 4991 | /* Offsets from p for storing byte pairs in the right order. */ |
| 4992 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 4993 | int iorder[] = {0, 1, 2, 3}; |
| 4994 | #else |
| 4995 | int iorder[] = {3, 2, 1, 0}; |
| 4996 | #endif |
| 4997 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4998 | #define STORECHAR(CH) \ |
| 4999 | do { \ |
| 5000 | p[iorder[3]] = ((CH) >> 24) & 0xff; \ |
| 5001 | p[iorder[2]] = ((CH) >> 16) & 0xff; \ |
| 5002 | p[iorder[1]] = ((CH) >> 8) & 0xff; \ |
| 5003 | p[iorder[0]] = (CH) & 0xff; \ |
| 5004 | p += 4; \ |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5005 | } while(0) |
| 5006 | |
| 5007 | /* In narrow builds we can output surrogate pairs as one codepoint, |
| 5008 | so we need less space. */ |
| 5009 | #ifndef Py_UNICODE_WIDE |
| 5010 | for (i = pairs = 0; i < size-1; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5011 | if (0xD800 <= s[i] && s[i] <= 0xDBFF && |
| 5012 | 0xDC00 <= s[i+1] && s[i+1] <= 0xDFFF) |
| 5013 | pairs++; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5014 | #endif |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5015 | nsize = (size - pairs + (byteorder == 0)); |
| 5016 | bytesize = nsize * 4; |
| 5017 | if (bytesize / 4 != nsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5018 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5019 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5020 | if (v == NULL) |
| 5021 | return NULL; |
| 5022 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5023 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5024 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5025 | STORECHAR(0xFEFF); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5026 | if (size == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5027 | goto done; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5028 | |
| 5029 | if (byteorder == -1) { |
| 5030 | /* force LE */ |
| 5031 | iorder[0] = 0; |
| 5032 | iorder[1] = 1; |
| 5033 | iorder[2] = 2; |
| 5034 | iorder[3] = 3; |
| 5035 | } |
| 5036 | else if (byteorder == 1) { |
| 5037 | /* force BE */ |
| 5038 | iorder[0] = 3; |
| 5039 | iorder[1] = 2; |
| 5040 | iorder[2] = 1; |
| 5041 | iorder[3] = 0; |
| 5042 | } |
| 5043 | |
| 5044 | while (size-- > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5045 | Py_UCS4 ch = *s++; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5046 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5047 | if (0xD800 <= ch && ch <= 0xDBFF && size > 0) { |
| 5048 | Py_UCS4 ch2 = *s; |
| 5049 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
| 5050 | ch = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
| 5051 | s++; |
| 5052 | size--; |
| 5053 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5054 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5055 | #endif |
| 5056 | STORECHAR(ch); |
| 5057 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5058 | |
| 5059 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5060 | return v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5061 | #undef STORECHAR |
| 5062 | } |
| 5063 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5064 | PyObject * |
| 5065 | PyUnicode_AsUTF32String(PyObject *unicode) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5066 | { |
| 5067 | if (!PyUnicode_Check(unicode)) { |
| 5068 | PyErr_BadArgument(); |
| 5069 | return NULL; |
| 5070 | } |
| 5071 | return PyUnicode_EncodeUTF32(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5072 | PyUnicode_GET_SIZE(unicode), |
| 5073 | NULL, |
| 5074 | 0); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5075 | } |
| 5076 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5077 | /* --- UTF-16 Codec ------------------------------------------------------- */ |
| 5078 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5079 | PyObject * |
| 5080 | PyUnicode_DecodeUTF16(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5081 | Py_ssize_t size, |
| 5082 | const char *errors, |
| 5083 | int *byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5084 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5085 | return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL); |
| 5086 | } |
| 5087 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5088 | /* Two masks for fast checking of whether a C 'long' may contain |
| 5089 | UTF16-encoded surrogate characters. This is an efficient heuristic, |
| 5090 | assuming that non-surrogate characters with a code point >= 0x8000 are |
| 5091 | rare in most input. |
| 5092 | FAST_CHAR_MASK is used when the input is in native byte ordering, |
| 5093 | SWAPPED_FAST_CHAR_MASK when the input is in byteswapped ordering. |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5094 | */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5095 | #if (SIZEOF_LONG == 8) |
| 5096 | # define FAST_CHAR_MASK 0x8000800080008000L |
| 5097 | # define SWAPPED_FAST_CHAR_MASK 0x0080008000800080L |
| 5098 | #elif (SIZEOF_LONG == 4) |
| 5099 | # define FAST_CHAR_MASK 0x80008000L |
| 5100 | # define SWAPPED_FAST_CHAR_MASK 0x00800080L |
| 5101 | #else |
| 5102 | # error C 'long' size should be either 4 or 8! |
| 5103 | #endif |
| 5104 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5105 | PyObject * |
| 5106 | PyUnicode_DecodeUTF16Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5107 | Py_ssize_t size, |
| 5108 | const char *errors, |
| 5109 | int *byteorder, |
| 5110 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5111 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5112 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5113 | Py_ssize_t startinpos; |
| 5114 | Py_ssize_t endinpos; |
| 5115 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5116 | PyUnicodeObject *unicode; |
| 5117 | Py_UNICODE *p; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5118 | const unsigned char *q, *e, *aligned_end; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5119 | int bo = 0; /* assume native ordering by default */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5120 | int native_ordering = 0; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 5121 | const char *errmsg = ""; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5122 | /* Offsets from q for retrieving byte pairs in the right order. */ |
| 5123 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5124 | int ihi = 1, ilo = 0; |
| 5125 | #else |
| 5126 | int ihi = 0, ilo = 1; |
| 5127 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5128 | PyObject *errorHandler = NULL; |
| 5129 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5130 | |
| 5131 | /* Note: size will always be longer than the resulting Unicode |
| 5132 | character count */ |
| 5133 | unicode = _PyUnicode_New(size); |
| 5134 | if (!unicode) |
| 5135 | return NULL; |
| 5136 | if (size == 0) |
| 5137 | return (PyObject *)unicode; |
| 5138 | |
| 5139 | /* Unpack UTF-16 encoded data */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5140 | p = PyUnicode_AS_UNICODE(unicode); |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5141 | q = (unsigned char *)s; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5142 | e = q + size - 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5143 | |
| 5144 | if (byteorder) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5145 | bo = *byteorder; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5146 | |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5147 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 5148 | byte order setting accordingly. In native mode, the leading BOM |
| 5149 | mark is skipped, in all other modes, it is copied to the output |
| 5150 | stream as-is (giving a ZWNBSP character). */ |
| 5151 | if (bo == 0) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5152 | if (size >= 2) { |
| 5153 | const Py_UNICODE bom = (q[ihi] << 8) | q[ilo]; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5154 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5155 | if (bom == 0xFEFF) { |
| 5156 | q += 2; |
| 5157 | bo = -1; |
| 5158 | } |
| 5159 | else if (bom == 0xFFFE) { |
| 5160 | q += 2; |
| 5161 | bo = 1; |
| 5162 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5163 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5164 | if (bom == 0xFEFF) { |
| 5165 | q += 2; |
| 5166 | bo = 1; |
| 5167 | } |
| 5168 | else if (bom == 0xFFFE) { |
| 5169 | q += 2; |
| 5170 | bo = -1; |
| 5171 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5172 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5173 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5174 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5175 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5176 | if (bo == -1) { |
| 5177 | /* force LE */ |
| 5178 | ihi = 1; |
| 5179 | ilo = 0; |
| 5180 | } |
| 5181 | else if (bo == 1) { |
| 5182 | /* force BE */ |
| 5183 | ihi = 0; |
| 5184 | ilo = 1; |
| 5185 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5186 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5187 | native_ordering = ilo < ihi; |
| 5188 | #else |
| 5189 | native_ordering = ilo > ihi; |
| 5190 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5191 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5192 | aligned_end = (const unsigned char *) ((size_t) e & ~LONG_PTR_MASK); |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5193 | while (q < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5194 | Py_UNICODE ch; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5195 | /* First check for possible aligned read of a C 'long'. Unaligned |
| 5196 | reads are more expensive, better to defer to another iteration. */ |
| 5197 | if (!((size_t) q & LONG_PTR_MASK)) { |
| 5198 | /* Fast path for runs of non-surrogate chars. */ |
| 5199 | register const unsigned char *_q = q; |
| 5200 | Py_UNICODE *_p = p; |
| 5201 | if (native_ordering) { |
| 5202 | /* Native ordering is simple: as long as the input cannot |
| 5203 | possibly contain a surrogate char, do an unrolled copy |
| 5204 | of several 16-bit code points to the target object. |
| 5205 | The non-surrogate check is done on several input bytes |
| 5206 | at a time (as many as a C 'long' can contain). */ |
| 5207 | while (_q < aligned_end) { |
| 5208 | unsigned long data = * (unsigned long *) _q; |
| 5209 | if (data & FAST_CHAR_MASK) |
| 5210 | break; |
| 5211 | _p[0] = ((unsigned short *) _q)[0]; |
| 5212 | _p[1] = ((unsigned short *) _q)[1]; |
| 5213 | #if (SIZEOF_LONG == 8) |
| 5214 | _p[2] = ((unsigned short *) _q)[2]; |
| 5215 | _p[3] = ((unsigned short *) _q)[3]; |
| 5216 | #endif |
| 5217 | _q += SIZEOF_LONG; |
| 5218 | _p += SIZEOF_LONG / 2; |
| 5219 | } |
| 5220 | } |
| 5221 | else { |
| 5222 | /* Byteswapped ordering is similar, but we must decompose |
| 5223 | the copy bytewise, and take care of zero'ing out the |
| 5224 | upper bytes if the target object is in 32-bit units |
| 5225 | (that is, in UCS-4 builds). */ |
| 5226 | while (_q < aligned_end) { |
| 5227 | unsigned long data = * (unsigned long *) _q; |
| 5228 | if (data & SWAPPED_FAST_CHAR_MASK) |
| 5229 | break; |
| 5230 | /* Zero upper bytes in UCS-4 builds */ |
| 5231 | #if (Py_UNICODE_SIZE > 2) |
| 5232 | _p[0] = 0; |
| 5233 | _p[1] = 0; |
| 5234 | #if (SIZEOF_LONG == 8) |
| 5235 | _p[2] = 0; |
| 5236 | _p[3] = 0; |
| 5237 | #endif |
| 5238 | #endif |
Antoine Pitrou | d6e8de1 | 2009-01-11 23:56:55 +0000 | [diff] [blame] | 5239 | /* Issue #4916; UCS-4 builds on big endian machines must |
| 5240 | fill the two last bytes of each 4-byte unit. */ |
| 5241 | #if (!defined(BYTEORDER_IS_LITTLE_ENDIAN) && Py_UNICODE_SIZE > 2) |
| 5242 | # define OFF 2 |
| 5243 | #else |
| 5244 | # define OFF 0 |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5245 | #endif |
Antoine Pitrou | d6e8de1 | 2009-01-11 23:56:55 +0000 | [diff] [blame] | 5246 | ((unsigned char *) _p)[OFF + 1] = _q[0]; |
| 5247 | ((unsigned char *) _p)[OFF + 0] = _q[1]; |
| 5248 | ((unsigned char *) _p)[OFF + 1 + Py_UNICODE_SIZE] = _q[2]; |
| 5249 | ((unsigned char *) _p)[OFF + 0 + Py_UNICODE_SIZE] = _q[3]; |
| 5250 | #if (SIZEOF_LONG == 8) |
| 5251 | ((unsigned char *) _p)[OFF + 1 + 2 * Py_UNICODE_SIZE] = _q[4]; |
| 5252 | ((unsigned char *) _p)[OFF + 0 + 2 * Py_UNICODE_SIZE] = _q[5]; |
| 5253 | ((unsigned char *) _p)[OFF + 1 + 3 * Py_UNICODE_SIZE] = _q[6]; |
| 5254 | ((unsigned char *) _p)[OFF + 0 + 3 * Py_UNICODE_SIZE] = _q[7]; |
| 5255 | #endif |
| 5256 | #undef OFF |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5257 | _q += SIZEOF_LONG; |
| 5258 | _p += SIZEOF_LONG / 2; |
| 5259 | } |
| 5260 | } |
| 5261 | p = _p; |
| 5262 | q = _q; |
| 5263 | if (q >= e) |
| 5264 | break; |
| 5265 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5266 | ch = (q[ihi] << 8) | q[ilo]; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5267 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5268 | q += 2; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5269 | |
| 5270 | if (ch < 0xD800 || ch > 0xDFFF) { |
| 5271 | *p++ = ch; |
| 5272 | continue; |
| 5273 | } |
| 5274 | |
| 5275 | /* UTF-16 code pair: */ |
| 5276 | if (q > e) { |
| 5277 | errmsg = "unexpected end of data"; |
| 5278 | startinpos = (((const char *)q) - 2) - starts; |
| 5279 | endinpos = ((const char *)e) + 1 - starts; |
| 5280 | goto utf16Error; |
| 5281 | } |
| 5282 | if (0xD800 <= ch && ch <= 0xDBFF) { |
| 5283 | Py_UNICODE ch2 = (q[ihi] << 8) | q[ilo]; |
| 5284 | q += 2; |
| 5285 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 5286 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5287 | *p++ = ch; |
| 5288 | *p++ = ch2; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5289 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5290 | *p++ = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5291 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5292 | continue; |
| 5293 | } |
| 5294 | else { |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5295 | errmsg = "illegal UTF-16 surrogate"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5296 | startinpos = (((const char *)q)-4)-starts; |
| 5297 | endinpos = startinpos+2; |
| 5298 | goto utf16Error; |
| 5299 | } |
| 5300 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5301 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5302 | errmsg = "illegal encoding"; |
| 5303 | startinpos = (((const char *)q)-2)-starts; |
| 5304 | endinpos = startinpos+2; |
| 5305 | /* Fall through to report the error */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5306 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5307 | utf16Error: |
| 5308 | outpos = p - PyUnicode_AS_UNICODE(unicode); |
| 5309 | if (unicode_decode_call_errorhandler( |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5310 | errors, |
| 5311 | &errorHandler, |
| 5312 | "utf16", errmsg, |
| 5313 | &starts, |
| 5314 | (const char **)&e, |
| 5315 | &startinpos, |
| 5316 | &endinpos, |
| 5317 | &exc, |
| 5318 | (const char **)&q, |
| 5319 | &unicode, |
| 5320 | &outpos, |
| 5321 | &p)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5322 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5323 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5324 | /* remaining byte at the end? (size should be even) */ |
| 5325 | if (e == q) { |
| 5326 | if (!consumed) { |
| 5327 | errmsg = "truncated data"; |
| 5328 | startinpos = ((const char *)q) - starts; |
| 5329 | endinpos = ((const char *)e) + 1 - starts; |
| 5330 | outpos = p - PyUnicode_AS_UNICODE(unicode); |
| 5331 | if (unicode_decode_call_errorhandler( |
| 5332 | errors, |
| 5333 | &errorHandler, |
| 5334 | "utf16", errmsg, |
| 5335 | &starts, |
| 5336 | (const char **)&e, |
| 5337 | &startinpos, |
| 5338 | &endinpos, |
| 5339 | &exc, |
| 5340 | (const char **)&q, |
| 5341 | &unicode, |
| 5342 | &outpos, |
| 5343 | &p)) |
| 5344 | goto onError; |
| 5345 | /* The remaining input chars are ignored if the callback |
| 5346 | chooses to skip the input */ |
| 5347 | } |
| 5348 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5349 | |
| 5350 | if (byteorder) |
| 5351 | *byteorder = bo; |
| 5352 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5353 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5354 | *consumed = (const char *)q-starts; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5355 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5356 | /* Adjust length */ |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 5357 | if (PyUnicode_Resize((PyObject**)&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5358 | goto onError; |
| 5359 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5360 | Py_XDECREF(errorHandler); |
| 5361 | Py_XDECREF(exc); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 5362 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 5363 | if (_PyUnicode_READY_REPLACE(&unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5364 | Py_DECREF(unicode); |
| 5365 | return NULL; |
| 5366 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 5367 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 5368 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5369 | return (PyObject *)unicode; |
| 5370 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5371 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5372 | Py_DECREF(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5373 | Py_XDECREF(errorHandler); |
| 5374 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5375 | return NULL; |
| 5376 | } |
| 5377 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5378 | #undef FAST_CHAR_MASK |
| 5379 | #undef SWAPPED_FAST_CHAR_MASK |
| 5380 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5381 | PyObject * |
| 5382 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5383 | Py_ssize_t size, |
| 5384 | const char *errors, |
| 5385 | int byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5386 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5387 | PyObject *v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5388 | unsigned char *p; |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5389 | Py_ssize_t nsize, bytesize; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 5390 | #ifdef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5391 | Py_ssize_t i, pairs; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 5392 | #else |
| 5393 | const int pairs = 0; |
| 5394 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5395 | /* Offsets from p for storing byte pairs in the right order. */ |
| 5396 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5397 | int ihi = 1, ilo = 0; |
| 5398 | #else |
| 5399 | int ihi = 0, ilo = 1; |
| 5400 | #endif |
| 5401 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5402 | #define STORECHAR(CH) \ |
| 5403 | do { \ |
| 5404 | p[ihi] = ((CH) >> 8) & 0xff; \ |
| 5405 | p[ilo] = (CH) & 0xff; \ |
| 5406 | p += 2; \ |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5407 | } while(0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5408 | |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 5409 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5410 | for (i = pairs = 0; i < size; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5411 | if (s[i] >= 0x10000) |
| 5412 | pairs++; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 5413 | #endif |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5414 | /* 2 * (size + pairs + (byteorder == 0)) */ |
| 5415 | if (size > PY_SSIZE_T_MAX || |
| 5416 | size > PY_SSIZE_T_MAX - pairs - (byteorder == 0)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5417 | return PyErr_NoMemory(); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5418 | nsize = size + pairs + (byteorder == 0); |
| 5419 | bytesize = nsize * 2; |
| 5420 | if (bytesize / 2 != nsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5421 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5422 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5423 | if (v == NULL) |
| 5424 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5425 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5426 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5427 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5428 | STORECHAR(0xFEFF); |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 5429 | if (size == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5430 | goto done; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5431 | |
| 5432 | if (byteorder == -1) { |
| 5433 | /* force LE */ |
| 5434 | ihi = 1; |
| 5435 | ilo = 0; |
| 5436 | } |
| 5437 | else if (byteorder == 1) { |
| 5438 | /* force BE */ |
| 5439 | ihi = 0; |
| 5440 | ilo = 1; |
| 5441 | } |
| 5442 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5443 | while (size-- > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5444 | Py_UNICODE ch = *s++; |
| 5445 | Py_UNICODE ch2 = 0; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 5446 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5447 | if (ch >= 0x10000) { |
| 5448 | ch2 = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 5449 | ch = 0xD800 | ((ch-0x10000) >> 10); |
| 5450 | } |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 5451 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5452 | STORECHAR(ch); |
| 5453 | if (ch2) |
| 5454 | STORECHAR(ch2); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5455 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5456 | |
| 5457 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5458 | return v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5459 | #undef STORECHAR |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5460 | } |
| 5461 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5462 | PyObject * |
| 5463 | PyUnicode_AsUTF16String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5464 | { |
| 5465 | if (!PyUnicode_Check(unicode)) { |
| 5466 | PyErr_BadArgument(); |
| 5467 | return NULL; |
| 5468 | } |
| 5469 | return PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5470 | PyUnicode_GET_SIZE(unicode), |
| 5471 | NULL, |
| 5472 | 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5473 | } |
| 5474 | |
| 5475 | /* --- Unicode Escape Codec ----------------------------------------------- */ |
| 5476 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5477 | /* Helper function for PyUnicode_DecodeUnicodeEscape, determines |
| 5478 | if all the escapes in the string make it still a valid ASCII string. |
| 5479 | Returns -1 if any escapes were found which cause the string to |
| 5480 | pop out of ASCII range. Otherwise returns the length of the |
| 5481 | required buffer to hold the string. |
| 5482 | */ |
| 5483 | Py_ssize_t |
| 5484 | length_of_escaped_ascii_string(const char *s, Py_ssize_t size) |
| 5485 | { |
| 5486 | const unsigned char *p = (const unsigned char *)s; |
| 5487 | const unsigned char *end = p + size; |
| 5488 | Py_ssize_t length = 0; |
| 5489 | |
| 5490 | if (size < 0) |
| 5491 | return -1; |
| 5492 | |
| 5493 | for (; p < end; ++p) { |
| 5494 | if (*p > 127) { |
| 5495 | /* Non-ASCII */ |
| 5496 | return -1; |
| 5497 | } |
| 5498 | else if (*p != '\\') { |
| 5499 | /* Normal character */ |
| 5500 | ++length; |
| 5501 | } |
| 5502 | else { |
| 5503 | /* Backslash-escape, check next char */ |
| 5504 | ++p; |
| 5505 | /* Escape sequence reaches till end of string or |
| 5506 | non-ASCII follow-up. */ |
| 5507 | if (p >= end || *p > 127) |
| 5508 | return -1; |
| 5509 | switch (*p) { |
| 5510 | case '\n': |
| 5511 | /* backslash + \n result in zero characters */ |
| 5512 | break; |
| 5513 | case '\\': case '\'': case '\"': |
| 5514 | case 'b': case 'f': case 't': |
| 5515 | case 'n': case 'r': case 'v': case 'a': |
| 5516 | ++length; |
| 5517 | break; |
| 5518 | case '0': case '1': case '2': case '3': |
| 5519 | case '4': case '5': case '6': case '7': |
| 5520 | case 'x': case 'u': case 'U': case 'N': |
| 5521 | /* these do not guarantee ASCII characters */ |
| 5522 | return -1; |
| 5523 | default: |
| 5524 | /* count the backslash + the other character */ |
| 5525 | length += 2; |
| 5526 | } |
| 5527 | } |
| 5528 | } |
| 5529 | return length; |
| 5530 | } |
| 5531 | |
| 5532 | /* Similar to PyUnicode_WRITE but either write into wstr field |
| 5533 | or treat string as ASCII. */ |
| 5534 | #define WRITE_ASCII_OR_WSTR(kind, buf, index, value) \ |
| 5535 | do { \ |
| 5536 | if ((kind) != PyUnicode_WCHAR_KIND) \ |
| 5537 | ((unsigned char *)(buf))[(index)] = (unsigned char)(value); \ |
| 5538 | else \ |
| 5539 | ((Py_UNICODE *)(buf))[(index)] = (Py_UNICODE)(value); \ |
| 5540 | } while (0) |
| 5541 | |
| 5542 | #define WRITE_WSTR(buf, index, value) \ |
| 5543 | assert(kind == PyUnicode_WCHAR_KIND), \ |
| 5544 | ((Py_UNICODE *)(buf))[(index)] = (Py_UNICODE)(value) |
| 5545 | |
| 5546 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 5547 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; |
Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 5548 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5549 | PyObject * |
| 5550 | PyUnicode_DecodeUnicodeEscape(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5551 | Py_ssize_t size, |
Victor Stinner | c17f540 | 2011-09-29 00:16:58 +0200 | [diff] [blame] | 5552 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5553 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5554 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5555 | Py_ssize_t startinpos; |
| 5556 | Py_ssize_t endinpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5557 | int j; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5558 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5559 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5560 | const char *end; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5561 | char* message; |
| 5562 | Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5563 | PyObject *errorHandler = NULL; |
| 5564 | PyObject *exc = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5565 | Py_ssize_t ascii_length; |
| 5566 | Py_ssize_t i; |
| 5567 | int kind; |
| 5568 | void *data; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5569 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5570 | ascii_length = length_of_escaped_ascii_string(s, size); |
| 5571 | |
| 5572 | /* After length_of_escaped_ascii_string() there are two alternatives, |
| 5573 | either the string is pure ASCII with named escapes like \n, etc. |
| 5574 | and we determined it's exact size (common case) |
| 5575 | or it contains \x, \u, ... escape sequences. then we create a |
| 5576 | legacy wchar string and resize it at the end of this function. */ |
| 5577 | if (ascii_length >= 0) { |
| 5578 | v = (PyUnicodeObject *)PyUnicode_New(ascii_length, 127); |
| 5579 | if (!v) |
| 5580 | goto onError; |
| 5581 | assert(PyUnicode_KIND(v) == PyUnicode_1BYTE_KIND); |
| 5582 | kind = PyUnicode_1BYTE_KIND; |
| 5583 | data = PyUnicode_DATA(v); |
| 5584 | } |
| 5585 | else { |
| 5586 | /* Escaped strings will always be longer than the resulting |
| 5587 | Unicode string, so we start with size here and then reduce the |
| 5588 | length after conversion to the true value. |
| 5589 | (but if the error callback returns a long replacement string |
| 5590 | we'll have to allocate more space) */ |
| 5591 | v = _PyUnicode_New(size); |
| 5592 | if (!v) |
| 5593 | goto onError; |
| 5594 | kind = PyUnicode_WCHAR_KIND; |
| 5595 | data = PyUnicode_AS_UNICODE(v); |
| 5596 | } |
| 5597 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5598 | if (size == 0) |
| 5599 | return (PyObject *)v; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5600 | i = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5601 | end = s + size; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5602 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5603 | while (s < end) { |
| 5604 | unsigned char c; |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 5605 | Py_UNICODE x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5606 | int digits; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5607 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5608 | if (kind == PyUnicode_WCHAR_KIND) { |
| 5609 | assert(i < _PyUnicode_WSTR_LENGTH(v)); |
| 5610 | } |
| 5611 | else { |
| 5612 | /* The only case in which i == ascii_length is a backslash |
| 5613 | followed by a newline. */ |
| 5614 | assert(i <= ascii_length); |
| 5615 | } |
| 5616 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5617 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 5618 | if (*s != '\\') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5619 | WRITE_ASCII_OR_WSTR(kind, data, i++, (unsigned char) *s++); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5620 | continue; |
| 5621 | } |
| 5622 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5623 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5624 | /* \ - Escapes */ |
| 5625 | s++; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5626 | c = *s++; |
| 5627 | if (s > end) |
| 5628 | c = '\0'; /* Invalid after \ */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5629 | |
| 5630 | if (kind == PyUnicode_WCHAR_KIND) { |
| 5631 | assert(i < _PyUnicode_WSTR_LENGTH(v)); |
| 5632 | } |
| 5633 | else { |
| 5634 | /* The only case in which i == ascii_length is a backslash |
| 5635 | followed by a newline. */ |
| 5636 | assert(i < ascii_length || (i == ascii_length && c == '\n')); |
| 5637 | } |
| 5638 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5639 | switch (c) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5640 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5641 | /* \x escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5642 | case '\n': break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5643 | case '\\': WRITE_ASCII_OR_WSTR(kind, data, i++, '\\'); break; |
| 5644 | case '\'': WRITE_ASCII_OR_WSTR(kind, data, i++, '\''); break; |
| 5645 | case '\"': WRITE_ASCII_OR_WSTR(kind, data, i++, '\"'); break; |
| 5646 | case 'b': WRITE_ASCII_OR_WSTR(kind, data, i++, '\b'); break; |
| 5647 | /* FF */ |
| 5648 | case 'f': WRITE_ASCII_OR_WSTR(kind, data, i++, '\014'); break; |
| 5649 | case 't': WRITE_ASCII_OR_WSTR(kind, data, i++, '\t'); break; |
| 5650 | case 'n': WRITE_ASCII_OR_WSTR(kind, data, i++, '\n'); break; |
| 5651 | case 'r': WRITE_ASCII_OR_WSTR(kind, data, i++, '\r'); break; |
| 5652 | /* VT */ |
| 5653 | case 'v': WRITE_ASCII_OR_WSTR(kind, data, i++, '\013'); break; |
| 5654 | /* BEL, not classic C */ |
| 5655 | case 'a': WRITE_ASCII_OR_WSTR(kind, data, i++, '\007'); break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5656 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5657 | /* \OOO (octal) escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5658 | case '0': case '1': case '2': case '3': |
| 5659 | case '4': case '5': case '6': case '7': |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5660 | x = s[-1] - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5661 | if (s < end && '0' <= *s && *s <= '7') { |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5662 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5663 | if (s < end && '0' <= *s && *s <= '7') |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5664 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5665 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5666 | WRITE_WSTR(data, i++, x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5667 | break; |
| 5668 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5669 | /* hex escapes */ |
| 5670 | /* \xXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5671 | case 'x': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5672 | digits = 2; |
| 5673 | message = "truncated \\xXX escape"; |
| 5674 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5675 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5676 | /* \uXXXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5677 | case 'u': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5678 | digits = 4; |
| 5679 | message = "truncated \\uXXXX escape"; |
| 5680 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5681 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5682 | /* \UXXXXXXXX */ |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5683 | case 'U': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5684 | digits = 8; |
| 5685 | message = "truncated \\UXXXXXXXX escape"; |
| 5686 | hexescape: |
| 5687 | chr = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5688 | p = PyUnicode_AS_UNICODE(v) + i; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5689 | if (s+digits>end) { |
| 5690 | endinpos = size; |
| 5691 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5692 | errors, &errorHandler, |
| 5693 | "unicodeescape", "end of string in escape sequence", |
| 5694 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5695 | &v, &i, &p)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5696 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5697 | data = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5698 | goto nextByte; |
| 5699 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5700 | for (j = 0; j < digits; ++j) { |
| 5701 | c = (unsigned char) s[j]; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 5702 | if (!Py_ISXDIGIT(c)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5703 | endinpos = (s+j+1)-starts; |
| 5704 | p = PyUnicode_AS_UNICODE(v) + i; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5705 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5706 | errors, &errorHandler, |
| 5707 | "unicodeescape", message, |
| 5708 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5709 | &v, &i, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5710 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5711 | data = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5712 | goto nextByte; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5713 | } |
| 5714 | chr = (chr<<4) & ~0xF; |
| 5715 | if (c >= '0' && c <= '9') |
| 5716 | chr += c - '0'; |
| 5717 | else if (c >= 'a' && c <= 'f') |
| 5718 | chr += 10 + c - 'a'; |
| 5719 | else |
| 5720 | chr += 10 + c - 'A'; |
| 5721 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5722 | s += j; |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 5723 | if (chr == 0xffffffff && PyErr_Occurred()) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5724 | /* _decoding_error will have already written into the |
| 5725 | target buffer. */ |
| 5726 | break; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5727 | store: |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5728 | /* when we get here, chr is a 32-bit unicode character */ |
| 5729 | if (chr <= 0xffff) |
| 5730 | /* UCS-2 character */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5731 | WRITE_WSTR(data, i++, chr); |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5732 | else if (chr <= 0x10ffff) { |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 5733 | /* UCS-4 character. Either store directly, or as |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5734 | surrogate pair. */ |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 5735 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5736 | WRITE_WSTR(data, i++, chr); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5737 | #else |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5738 | chr -= 0x10000L; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5739 | WRITE_WSTR(data, i++, 0xD800 + (Py_UNICODE) (chr >> 10)); |
| 5740 | WRITE_WSTR(data, i++, 0xDC00 + (Py_UNICODE) (chr & 0x03FF)); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5741 | #endif |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5742 | } else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5743 | endinpos = s-starts; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5744 | p = PyUnicode_AS_UNICODE(v) + i; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5745 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5746 | errors, &errorHandler, |
| 5747 | "unicodeescape", "illegal Unicode character", |
| 5748 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5749 | &v, &i, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5750 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5751 | data = PyUnicode_AS_UNICODE(v); |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5752 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5753 | break; |
| 5754 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5755 | /* \N{name} */ |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5756 | case 'N': |
| 5757 | message = "malformed \\N character escape"; |
| 5758 | if (ucnhash_CAPI == NULL) { |
| 5759 | /* load the unicode data module */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5760 | ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCapsule_Import( |
| 5761 | PyUnicodeData_CAPSULE_NAME, 1); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5762 | if (ucnhash_CAPI == NULL) |
| 5763 | goto ucnhashError; |
| 5764 | } |
| 5765 | if (*s == '{') { |
| 5766 | const char *start = s+1; |
| 5767 | /* look for the closing brace */ |
| 5768 | while (*s != '}' && s < end) |
| 5769 | s++; |
| 5770 | if (s > start && s < end && *s == '}') { |
| 5771 | /* found a name. look it up in the unicode database */ |
| 5772 | message = "unknown Unicode character name"; |
| 5773 | s++; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5774 | if (ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), |
| 5775 | &chr)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5776 | goto store; |
| 5777 | } |
| 5778 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5779 | endinpos = s-starts; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5780 | p = PyUnicode_AS_UNICODE(v) + i; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5781 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5782 | errors, &errorHandler, |
| 5783 | "unicodeescape", message, |
| 5784 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5785 | &v, &i, &p)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5786 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5787 | data = PyUnicode_AS_UNICODE(v); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5788 | break; |
| 5789 | |
| 5790 | default: |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5791 | if (s > end) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5792 | assert(kind == PyUnicode_WCHAR_KIND); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5793 | message = "\\ at end of string"; |
| 5794 | s--; |
| 5795 | endinpos = s-starts; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5796 | p = PyUnicode_AS_UNICODE(v) + i; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5797 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5798 | errors, &errorHandler, |
| 5799 | "unicodeescape", message, |
| 5800 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5801 | &v, &i, &p)) |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5802 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5803 | data = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5804 | } |
| 5805 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5806 | WRITE_ASCII_OR_WSTR(kind, data, i++, '\\'); |
| 5807 | WRITE_ASCII_OR_WSTR(kind, data, i++, (unsigned char)s[-1]); |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5808 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5809 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5810 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5811 | nextByte: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5812 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5813 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5814 | /* Ensure the length prediction worked in case of ASCII strings */ |
| 5815 | assert(kind == PyUnicode_WCHAR_KIND || i == ascii_length); |
| 5816 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 5817 | if (kind == PyUnicode_WCHAR_KIND) |
| 5818 | { |
| 5819 | if (PyUnicode_Resize((PyObject**)&v, i) < 0) |
| 5820 | goto onError; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 5821 | } |
Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 5822 | Py_XDECREF(errorHandler); |
| 5823 | Py_XDECREF(exc); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 5824 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 5825 | if (_PyUnicode_READY_REPLACE(&v)) { |
| 5826 | Py_DECREF(v); |
| 5827 | return NULL; |
| 5828 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 5829 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 5830 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5831 | return (PyObject *)v; |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5832 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5833 | ucnhashError: |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 5834 | PyErr_SetString( |
| 5835 | PyExc_UnicodeError, |
| 5836 | "\\N escapes not supported (can't load unicodedata module)" |
| 5837 | ); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 5838 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5839 | Py_XDECREF(errorHandler); |
| 5840 | Py_XDECREF(exc); |
Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 5841 | return NULL; |
| 5842 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5843 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5844 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5845 | Py_XDECREF(errorHandler); |
| 5846 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5847 | return NULL; |
| 5848 | } |
| 5849 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5850 | #undef WRITE_ASCII_OR_WSTR |
| 5851 | #undef WRITE_WSTR |
| 5852 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5853 | /* Return a Unicode-Escape string version of the Unicode object. |
| 5854 | |
| 5855 | If quotes is true, the string is enclosed in u"" or u'' quotes as |
| 5856 | appropriate. |
| 5857 | |
| 5858 | */ |
| 5859 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5860 | static const char *hexdigits = "0123456789abcdef"; |
| 5861 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5862 | PyObject * |
| 5863 | PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5864 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5865 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5866 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5867 | char *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5868 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5869 | #ifdef Py_UNICODE_WIDE |
| 5870 | const Py_ssize_t expandsize = 10; |
| 5871 | #else |
| 5872 | const Py_ssize_t expandsize = 6; |
| 5873 | #endif |
| 5874 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5875 | /* XXX(nnorwitz): rather than over-allocating, it would be |
| 5876 | better to choose a different scheme. Perhaps scan the |
| 5877 | first N-chars of the string and allocate based on that size. |
| 5878 | */ |
| 5879 | /* Initial allocation is based on the longest-possible unichr |
| 5880 | escape. |
| 5881 | |
| 5882 | In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source |
| 5883 | unichr, so in this case it's the longest unichr escape. In |
| 5884 | narrow (UTF-16) builds this is five chars per source unichr |
| 5885 | since there are two unichrs in the surrogate pair, so in narrow |
| 5886 | (UTF-16) builds it's not the longest unichr escape. |
| 5887 | |
| 5888 | In wide or narrow builds '\uxxxx' is 6 chars per source unichr, |
| 5889 | so in the narrow (UTF-16) build case it's the longest unichr |
| 5890 | escape. |
| 5891 | */ |
| 5892 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5893 | if (size == 0) |
| 5894 | return PyBytes_FromStringAndSize(NULL, 0); |
| 5895 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5896 | if (size > (PY_SSIZE_T_MAX - 2 - 1) / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5897 | return PyErr_NoMemory(); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5898 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5899 | repr = PyBytes_FromStringAndSize(NULL, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5900 | 2 |
| 5901 | + expandsize*size |
| 5902 | + 1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5903 | if (repr == NULL) |
| 5904 | return NULL; |
| 5905 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5906 | p = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5907 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5908 | while (size-- > 0) { |
| 5909 | Py_UNICODE ch = *s++; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5910 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5911 | /* Escape backslashes */ |
| 5912 | if (ch == '\\') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5913 | *p++ = '\\'; |
| 5914 | *p++ = (char) ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5915 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5916 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5917 | |
Guido van Rossum | 0d42e0c | 2001-07-20 16:36:21 +0000 | [diff] [blame] | 5918 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5919 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 5920 | else if (ch >= 0x10000) { |
| 5921 | *p++ = '\\'; |
| 5922 | *p++ = 'U'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5923 | *p++ = hexdigits[(ch >> 28) & 0x0000000F]; |
| 5924 | *p++ = hexdigits[(ch >> 24) & 0x0000000F]; |
| 5925 | *p++ = hexdigits[(ch >> 20) & 0x0000000F]; |
| 5926 | *p++ = hexdigits[(ch >> 16) & 0x0000000F]; |
| 5927 | *p++ = hexdigits[(ch >> 12) & 0x0000000F]; |
| 5928 | *p++ = hexdigits[(ch >> 8) & 0x0000000F]; |
| 5929 | *p++ = hexdigits[(ch >> 4) & 0x0000000F]; |
| 5930 | *p++ = hexdigits[ch & 0x0000000F]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5931 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5932 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5933 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5934 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
| 5935 | else if (ch >= 0xD800 && ch < 0xDC00) { |
| 5936 | Py_UNICODE ch2; |
| 5937 | Py_UCS4 ucs; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5938 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5939 | ch2 = *s++; |
| 5940 | size--; |
Georg Brandl | 78eef3de | 2010-08-01 20:51:02 +0000 | [diff] [blame] | 5941 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5942 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 5943 | *p++ = '\\'; |
| 5944 | *p++ = 'U'; |
| 5945 | *p++ = hexdigits[(ucs >> 28) & 0x0000000F]; |
| 5946 | *p++ = hexdigits[(ucs >> 24) & 0x0000000F]; |
| 5947 | *p++ = hexdigits[(ucs >> 20) & 0x0000000F]; |
| 5948 | *p++ = hexdigits[(ucs >> 16) & 0x0000000F]; |
| 5949 | *p++ = hexdigits[(ucs >> 12) & 0x0000000F]; |
| 5950 | *p++ = hexdigits[(ucs >> 8) & 0x0000000F]; |
| 5951 | *p++ = hexdigits[(ucs >> 4) & 0x0000000F]; |
| 5952 | *p++ = hexdigits[ucs & 0x0000000F]; |
| 5953 | continue; |
| 5954 | } |
| 5955 | /* Fall through: isolated surrogates are copied as-is */ |
| 5956 | s--; |
| 5957 | size++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5958 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5959 | #endif |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 5960 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5961 | /* Map 16-bit characters to '\uxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 5962 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5963 | *p++ = '\\'; |
| 5964 | *p++ = 'u'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5965 | *p++ = hexdigits[(ch >> 12) & 0x000F]; |
| 5966 | *p++ = hexdigits[(ch >> 8) & 0x000F]; |
| 5967 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 5968 | *p++ = hexdigits[ch & 0x000F]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5969 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5970 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 5971 | /* Map special whitespace to '\t', \n', '\r' */ |
| 5972 | else if (ch == '\t') { |
| 5973 | *p++ = '\\'; |
| 5974 | *p++ = 't'; |
| 5975 | } |
| 5976 | else if (ch == '\n') { |
| 5977 | *p++ = '\\'; |
| 5978 | *p++ = 'n'; |
| 5979 | } |
| 5980 | else if (ch == '\r') { |
| 5981 | *p++ = '\\'; |
| 5982 | *p++ = 'r'; |
| 5983 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5984 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 5985 | /* Map non-printable US ASCII to '\xhh' */ |
Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 5986 | else if (ch < ' ' || ch >= 0x7F) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5987 | *p++ = '\\'; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 5988 | *p++ = 'x'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5989 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 5990 | *p++ = hexdigits[ch & 0x000F]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5991 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5992 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5993 | /* Copy everything else as-is */ |
| 5994 | else |
| 5995 | *p++ = (char) ch; |
| 5996 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5997 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5998 | assert(p - PyBytes_AS_STRING(repr) > 0); |
| 5999 | if (_PyBytes_Resize(&repr, p - PyBytes_AS_STRING(repr)) < 0) |
| 6000 | return NULL; |
| 6001 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6002 | } |
| 6003 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6004 | PyObject * |
| 6005 | PyUnicode_AsUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6006 | { |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 6007 | PyObject *s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6008 | if (!PyUnicode_Check(unicode)) { |
| 6009 | PyErr_BadArgument(); |
| 6010 | return NULL; |
| 6011 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 6012 | s = PyUnicode_EncodeUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 6013 | PyUnicode_GET_SIZE(unicode)); |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 6014 | return s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6015 | } |
| 6016 | |
| 6017 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ |
| 6018 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6019 | PyObject * |
| 6020 | PyUnicode_DecodeRawUnicodeEscape(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6021 | Py_ssize_t size, |
| 6022 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6023 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6024 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6025 | Py_ssize_t startinpos; |
| 6026 | Py_ssize_t endinpos; |
| 6027 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6028 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6029 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6030 | const char *end; |
| 6031 | const char *bs; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6032 | PyObject *errorHandler = NULL; |
| 6033 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6034 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6035 | /* Escaped strings will always be longer than the resulting |
| 6036 | 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] | 6037 | length after conversion to the true value. (But decoding error |
| 6038 | handler might have to resize the string) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6039 | v = _PyUnicode_New(size); |
| 6040 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6041 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6042 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6043 | return (PyObject *)v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6044 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6045 | end = s + size; |
| 6046 | while (s < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6047 | unsigned char c; |
| 6048 | Py_UCS4 x; |
| 6049 | int i; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6050 | int count; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6051 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6052 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 6053 | if (*s != '\\') { |
| 6054 | *p++ = (unsigned char)*s++; |
| 6055 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6056 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6057 | startinpos = s-starts; |
| 6058 | |
| 6059 | /* \u-escapes are only interpreted iff the number of leading |
| 6060 | backslashes if odd */ |
| 6061 | bs = s; |
| 6062 | for (;s < end;) { |
| 6063 | if (*s != '\\') |
| 6064 | break; |
| 6065 | *p++ = (unsigned char)*s++; |
| 6066 | } |
| 6067 | if (((s - bs) & 1) == 0 || |
| 6068 | s >= end || |
| 6069 | (*s != 'u' && *s != 'U')) { |
| 6070 | continue; |
| 6071 | } |
| 6072 | p--; |
| 6073 | count = *s=='u' ? 4 : 8; |
| 6074 | s++; |
| 6075 | |
| 6076 | /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ |
| 6077 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 6078 | for (x = 0, i = 0; i < count; ++i, ++s) { |
| 6079 | c = (unsigned char)*s; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 6080 | if (!Py_ISXDIGIT(c)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6081 | endinpos = s-starts; |
| 6082 | if (unicode_decode_call_errorhandler( |
| 6083 | errors, &errorHandler, |
| 6084 | "rawunicodeescape", "truncated \\uXXXX", |
| 6085 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 6086 | &v, &outpos, &p)) |
| 6087 | goto onError; |
| 6088 | goto nextByte; |
| 6089 | } |
| 6090 | x = (x<<4) & ~0xF; |
| 6091 | if (c >= '0' && c <= '9') |
| 6092 | x += c - '0'; |
| 6093 | else if (c >= 'a' && c <= 'f') |
| 6094 | x += 10 + c - 'a'; |
| 6095 | else |
| 6096 | x += 10 + c - 'A'; |
| 6097 | } |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 6098 | if (x <= 0xffff) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6099 | /* UCS-2 character */ |
| 6100 | *p++ = (Py_UNICODE) x; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 6101 | else if (x <= 0x10ffff) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6102 | /* UCS-4 character. Either store directly, or as |
| 6103 | surrogate pair. */ |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 6104 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6105 | *p++ = (Py_UNICODE) x; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 6106 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6107 | x -= 0x10000L; |
| 6108 | *p++ = 0xD800 + (Py_UNICODE) (x >> 10); |
| 6109 | *p++ = 0xDC00 + (Py_UNICODE) (x & 0x03FF); |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 6110 | #endif |
| 6111 | } else { |
| 6112 | endinpos = s-starts; |
| 6113 | outpos = p-PyUnicode_AS_UNICODE(v); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6114 | if (unicode_decode_call_errorhandler( |
| 6115 | errors, &errorHandler, |
| 6116 | "rawunicodeescape", "\\Uxxxxxxxx out of range", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6117 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 6118 | &v, &outpos, &p)) |
| 6119 | goto onError; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6120 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6121 | nextByte: |
| 6122 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6123 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 6124 | if (PyUnicode_Resize((PyObject**)&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6125 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6126 | Py_XDECREF(errorHandler); |
| 6127 | Py_XDECREF(exc); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 6128 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 6129 | if (_PyUnicode_READY_REPLACE(&v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6130 | Py_DECREF(v); |
| 6131 | return NULL; |
| 6132 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 6133 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 6134 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6135 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6136 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6137 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6138 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6139 | Py_XDECREF(errorHandler); |
| 6140 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6141 | return NULL; |
| 6142 | } |
| 6143 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6144 | PyObject * |
| 6145 | PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6146 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6147 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6148 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6149 | char *p; |
| 6150 | char *q; |
| 6151 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6152 | #ifdef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 6153 | const Py_ssize_t expandsize = 10; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6154 | #else |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 6155 | const Py_ssize_t expandsize = 6; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6156 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6157 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 6158 | if (size > PY_SSIZE_T_MAX / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6159 | return PyErr_NoMemory(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6160 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6161 | repr = PyBytes_FromStringAndSize(NULL, expandsize * size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6162 | if (repr == NULL) |
| 6163 | return NULL; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 6164 | if (size == 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6165 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6166 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6167 | p = q = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6168 | while (size-- > 0) { |
| 6169 | Py_UNICODE ch = *s++; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6170 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6171 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 6172 | if (ch >= 0x10000) { |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6173 | *p++ = '\\'; |
| 6174 | *p++ = 'U'; |
Walter Dörwald | db5d33e | 2007-05-12 11:13:47 +0000 | [diff] [blame] | 6175 | *p++ = hexdigits[(ch >> 28) & 0xf]; |
| 6176 | *p++ = hexdigits[(ch >> 24) & 0xf]; |
| 6177 | *p++ = hexdigits[(ch >> 20) & 0xf]; |
| 6178 | *p++ = hexdigits[(ch >> 16) & 0xf]; |
| 6179 | *p++ = hexdigits[(ch >> 12) & 0xf]; |
| 6180 | *p++ = hexdigits[(ch >> 8) & 0xf]; |
| 6181 | *p++ = hexdigits[(ch >> 4) & 0xf]; |
| 6182 | *p++ = hexdigits[ch & 15]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6183 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6184 | else |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 6185 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6186 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
| 6187 | if (ch >= 0xD800 && ch < 0xDC00) { |
| 6188 | Py_UNICODE ch2; |
| 6189 | Py_UCS4 ucs; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 6190 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6191 | ch2 = *s++; |
| 6192 | size--; |
Georg Brandl | 78eef3de | 2010-08-01 20:51:02 +0000 | [diff] [blame] | 6193 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6194 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 6195 | *p++ = '\\'; |
| 6196 | *p++ = 'U'; |
| 6197 | *p++ = hexdigits[(ucs >> 28) & 0xf]; |
| 6198 | *p++ = hexdigits[(ucs >> 24) & 0xf]; |
| 6199 | *p++ = hexdigits[(ucs >> 20) & 0xf]; |
| 6200 | *p++ = hexdigits[(ucs >> 16) & 0xf]; |
| 6201 | *p++ = hexdigits[(ucs >> 12) & 0xf]; |
| 6202 | *p++ = hexdigits[(ucs >> 8) & 0xf]; |
| 6203 | *p++ = hexdigits[(ucs >> 4) & 0xf]; |
| 6204 | *p++ = hexdigits[ucs & 0xf]; |
| 6205 | continue; |
| 6206 | } |
| 6207 | /* Fall through: isolated surrogates are copied as-is */ |
| 6208 | s--; |
| 6209 | size++; |
| 6210 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6211 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6212 | /* Map 16-bit characters to '\uxxxx' */ |
| 6213 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6214 | *p++ = '\\'; |
| 6215 | *p++ = 'u'; |
Walter Dörwald | db5d33e | 2007-05-12 11:13:47 +0000 | [diff] [blame] | 6216 | *p++ = hexdigits[(ch >> 12) & 0xf]; |
| 6217 | *p++ = hexdigits[(ch >> 8) & 0xf]; |
| 6218 | *p++ = hexdigits[(ch >> 4) & 0xf]; |
| 6219 | *p++ = hexdigits[ch & 15]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6220 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6221 | /* Copy everything else as-is */ |
| 6222 | else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6223 | *p++ = (char) ch; |
| 6224 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6225 | size = p - q; |
| 6226 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6227 | assert(size > 0); |
| 6228 | if (_PyBytes_Resize(&repr, size) < 0) |
| 6229 | return NULL; |
| 6230 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6231 | } |
| 6232 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6233 | PyObject * |
| 6234 | PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6235 | { |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 6236 | PyObject *s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6237 | if (!PyUnicode_Check(unicode)) { |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 6238 | PyErr_BadArgument(); |
| 6239 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6240 | } |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 6241 | s = PyUnicode_EncodeRawUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 6242 | PyUnicode_GET_SIZE(unicode)); |
| 6243 | |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 6244 | return s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6245 | } |
| 6246 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6247 | /* --- Unicode Internal Codec ------------------------------------------- */ |
| 6248 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6249 | PyObject * |
| 6250 | _PyUnicode_DecodeUnicodeInternal(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6251 | Py_ssize_t size, |
| 6252 | const char *errors) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6253 | { |
| 6254 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6255 | Py_ssize_t startinpos; |
| 6256 | Py_ssize_t endinpos; |
| 6257 | Py_ssize_t outpos; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6258 | PyUnicodeObject *v; |
| 6259 | Py_UNICODE *p; |
| 6260 | const char *end; |
| 6261 | const char *reason; |
| 6262 | PyObject *errorHandler = NULL; |
| 6263 | PyObject *exc = NULL; |
| 6264 | |
Neal Norwitz | d43069c | 2006-01-08 01:12:10 +0000 | [diff] [blame] | 6265 | #ifdef Py_UNICODE_WIDE |
| 6266 | Py_UNICODE unimax = PyUnicode_GetMax(); |
| 6267 | #endif |
| 6268 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6269 | /* XXX overflow detection missing */ |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6270 | v = _PyUnicode_New((size+Py_UNICODE_SIZE-1)/ Py_UNICODE_SIZE); |
| 6271 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6272 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6273 | /* Intentionally PyUnicode_GET_SIZE instead of PyUnicode_GET_LENGTH |
| 6274 | as string was created with the old API. */ |
| 6275 | if (PyUnicode_GET_SIZE(v) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6276 | return (PyObject *)v; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6277 | p = PyUnicode_AS_UNICODE(v); |
| 6278 | end = s + size; |
| 6279 | |
| 6280 | while (s < end) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6281 | memcpy(p, s, sizeof(Py_UNICODE)); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6282 | /* We have to sanity check the raw data, otherwise doom looms for |
| 6283 | some malformed UCS-4 data. */ |
| 6284 | if ( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6285 | #ifdef Py_UNICODE_WIDE |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6286 | *p > unimax || *p < 0 || |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6287 | #endif |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6288 | end-s < Py_UNICODE_SIZE |
| 6289 | ) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6290 | { |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6291 | startinpos = s - starts; |
| 6292 | if (end-s < Py_UNICODE_SIZE) { |
| 6293 | endinpos = end-starts; |
| 6294 | reason = "truncated input"; |
| 6295 | } |
| 6296 | else { |
| 6297 | endinpos = s - starts + Py_UNICODE_SIZE; |
| 6298 | reason = "illegal code point (> 0x10FFFF)"; |
| 6299 | } |
| 6300 | outpos = p - PyUnicode_AS_UNICODE(v); |
| 6301 | if (unicode_decode_call_errorhandler( |
| 6302 | errors, &errorHandler, |
| 6303 | "unicode_internal", reason, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 6304 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 6305 | &v, &outpos, &p)) { |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6306 | goto onError; |
| 6307 | } |
| 6308 | } |
| 6309 | else { |
| 6310 | p++; |
| 6311 | s += Py_UNICODE_SIZE; |
| 6312 | } |
| 6313 | } |
| 6314 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 6315 | if (PyUnicode_Resize((PyObject**)&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6316 | goto onError; |
| 6317 | Py_XDECREF(errorHandler); |
| 6318 | Py_XDECREF(exc); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 6319 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 6320 | if (_PyUnicode_READY_REPLACE(&v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6321 | Py_DECREF(v); |
| 6322 | return NULL; |
| 6323 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 6324 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 6325 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6326 | return (PyObject *)v; |
| 6327 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6328 | onError: |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6329 | Py_XDECREF(v); |
| 6330 | Py_XDECREF(errorHandler); |
| 6331 | Py_XDECREF(exc); |
| 6332 | return NULL; |
| 6333 | } |
| 6334 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6335 | /* --- Latin-1 Codec ------------------------------------------------------ */ |
| 6336 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6337 | PyObject * |
| 6338 | PyUnicode_DecodeLatin1(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6339 | Py_ssize_t size, |
| 6340 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6341 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6342 | /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 6343 | return _PyUnicode_FromUCS1((unsigned char*)s, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6344 | } |
| 6345 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6346 | /* create or adjust a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6347 | static void |
| 6348 | make_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6349 | const char *encoding, |
| 6350 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 6351 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6352 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6353 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6354 | if (*exceptionObject == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6355 | *exceptionObject = PyUnicodeEncodeError_Create( |
| 6356 | encoding, unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6357 | } |
| 6358 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6359 | if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 6360 | goto onError; |
| 6361 | if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) |
| 6362 | goto onError; |
| 6363 | if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) |
| 6364 | goto onError; |
| 6365 | return; |
| 6366 | onError: |
| 6367 | Py_DECREF(*exceptionObject); |
| 6368 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6369 | } |
| 6370 | } |
| 6371 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6372 | /* raises a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6373 | static void |
| 6374 | raise_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6375 | const char *encoding, |
| 6376 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 6377 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6378 | const char *reason) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6379 | { |
| 6380 | make_encode_exception(exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6381 | encoding, unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6382 | if (*exceptionObject != NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6383 | PyCodec_StrictErrors(*exceptionObject); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6384 | } |
| 6385 | |
| 6386 | /* error handling callback helper: |
| 6387 | build arguments, call the callback and check the arguments, |
| 6388 | put the result into newpos and return the replacement string, which |
| 6389 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6390 | static PyObject * |
| 6391 | unicode_encode_call_errorhandler(const char *errors, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6392 | PyObject **errorHandler, |
| 6393 | const char *encoding, const char *reason, |
| 6394 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 6395 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6396 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6397 | { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6398 | 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] | 6399 | |
| 6400 | PyObject *restuple; |
| 6401 | PyObject *resunicode; |
| 6402 | |
| 6403 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6404 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6405 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6406 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6407 | } |
| 6408 | |
| 6409 | make_encode_exception(exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6410 | encoding, unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6411 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6412 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6413 | |
| 6414 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6415 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6416 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6417 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6418 | if (!PyTuple_Check(restuple)) { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6419 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6420 | Py_DECREF(restuple); |
| 6421 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6422 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6423 | if (!PyArg_ParseTuple(restuple, argparse, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6424 | &resunicode, newpos)) { |
| 6425 | Py_DECREF(restuple); |
| 6426 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6427 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6428 | if (!PyUnicode_Check(resunicode) && !PyBytes_Check(resunicode)) { |
| 6429 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
| 6430 | Py_DECREF(restuple); |
| 6431 | return NULL; |
| 6432 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6433 | if (*newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6434 | *newpos = size+*newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 6435 | if (*newpos<0 || *newpos>size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6436 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 6437 | Py_DECREF(restuple); |
| 6438 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 6439 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6440 | Py_INCREF(resunicode); |
| 6441 | Py_DECREF(restuple); |
| 6442 | return resunicode; |
| 6443 | } |
| 6444 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6445 | static PyObject * |
| 6446 | unicode_encode_ucs1(const Py_UNICODE *p, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6447 | Py_ssize_t size, |
| 6448 | const char *errors, |
| 6449 | int limit) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6450 | { |
| 6451 | /* output object */ |
| 6452 | PyObject *res; |
| 6453 | /* pointers to the beginning and end+1 of input */ |
| 6454 | const Py_UNICODE *startp = p; |
| 6455 | const Py_UNICODE *endp = p + size; |
| 6456 | /* pointer to the beginning of the unencodable characters */ |
| 6457 | /* const Py_UNICODE *badp = NULL; */ |
| 6458 | /* pointer into the output */ |
| 6459 | char *str; |
| 6460 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6461 | Py_ssize_t ressize; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6462 | const char *encoding = (limit == 256) ? "latin-1" : "ascii"; |
| 6463 | 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] | 6464 | PyObject *errorHandler = NULL; |
| 6465 | PyObject *exc = NULL; |
| 6466 | /* the following variable is used for caching string comparisons |
| 6467 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 6468 | int known_errorHandler = -1; |
| 6469 | |
| 6470 | /* allocate enough for a simple encoding without |
| 6471 | replacements, if we need more, we'll resize */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6472 | if (size == 0) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 6473 | return PyBytes_FromStringAndSize(NULL, 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6474 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6475 | if (res == NULL) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6476 | return NULL; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6477 | str = PyBytes_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6478 | ressize = size; |
| 6479 | |
| 6480 | while (p<endp) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6481 | Py_UNICODE c = *p; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6482 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6483 | /* can we encode this? */ |
| 6484 | if (c<limit) { |
| 6485 | /* no overflow check, because we know that the space is enough */ |
| 6486 | *str++ = (char)c; |
| 6487 | ++p; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6488 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6489 | else { |
| 6490 | Py_ssize_t unicodepos = p-startp; |
| 6491 | Py_ssize_t requiredsize; |
| 6492 | PyObject *repunicode; |
| 6493 | Py_ssize_t repsize; |
| 6494 | Py_ssize_t newpos; |
| 6495 | Py_ssize_t respos; |
| 6496 | Py_UNICODE *uni2; |
| 6497 | /* startpos for collecting unencodable chars */ |
| 6498 | const Py_UNICODE *collstart = p; |
| 6499 | const Py_UNICODE *collend = p; |
| 6500 | /* find all unecodable characters */ |
| 6501 | while ((collend < endp) && ((*collend)>=limit)) |
| 6502 | ++collend; |
| 6503 | /* cache callback name lookup (if not done yet, i.e. it's the first error) */ |
| 6504 | if (known_errorHandler==-1) { |
| 6505 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 6506 | known_errorHandler = 1; |
| 6507 | else if (!strcmp(errors, "replace")) |
| 6508 | known_errorHandler = 2; |
| 6509 | else if (!strcmp(errors, "ignore")) |
| 6510 | known_errorHandler = 3; |
| 6511 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 6512 | known_errorHandler = 4; |
| 6513 | else |
| 6514 | known_errorHandler = 0; |
| 6515 | } |
| 6516 | switch (known_errorHandler) { |
| 6517 | case 1: /* strict */ |
| 6518 | raise_encode_exception(&exc, encoding, startp, size, collstart-startp, collend-startp, reason); |
| 6519 | goto onError; |
| 6520 | case 2: /* replace */ |
| 6521 | while (collstart++<collend) |
| 6522 | *str++ = '?'; /* fall through */ |
| 6523 | case 3: /* ignore */ |
| 6524 | p = collend; |
| 6525 | break; |
| 6526 | case 4: /* xmlcharrefreplace */ |
| 6527 | respos = str - PyBytes_AS_STRING(res); |
| 6528 | /* determine replacement size (temporarily (mis)uses p) */ |
| 6529 | for (p = collstart, repsize = 0; p < collend; ++p) { |
| 6530 | if (*p<10) |
| 6531 | repsize += 2+1+1; |
| 6532 | else if (*p<100) |
| 6533 | repsize += 2+2+1; |
| 6534 | else if (*p<1000) |
| 6535 | repsize += 2+3+1; |
| 6536 | else if (*p<10000) |
| 6537 | repsize += 2+4+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 6538 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6539 | else |
| 6540 | repsize += 2+5+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 6541 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6542 | else if (*p<100000) |
| 6543 | repsize += 2+5+1; |
| 6544 | else if (*p<1000000) |
| 6545 | repsize += 2+6+1; |
| 6546 | else |
| 6547 | repsize += 2+7+1; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 6548 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6549 | } |
| 6550 | requiredsize = respos+repsize+(endp-collend); |
| 6551 | if (requiredsize > ressize) { |
| 6552 | if (requiredsize<2*ressize) |
| 6553 | requiredsize = 2*ressize; |
| 6554 | if (_PyBytes_Resize(&res, requiredsize)) |
| 6555 | goto onError; |
| 6556 | str = PyBytes_AS_STRING(res) + respos; |
| 6557 | ressize = requiredsize; |
| 6558 | } |
| 6559 | /* generate replacement (temporarily (mis)uses p) */ |
| 6560 | for (p = collstart; p < collend; ++p) { |
| 6561 | str += sprintf(str, "&#%d;", (int)*p); |
| 6562 | } |
| 6563 | p = collend; |
| 6564 | break; |
| 6565 | default: |
| 6566 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 6567 | encoding, reason, startp, size, &exc, |
| 6568 | collstart-startp, collend-startp, &newpos); |
| 6569 | if (repunicode == NULL) |
| 6570 | goto onError; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6571 | if (PyBytes_Check(repunicode)) { |
| 6572 | /* Directly copy bytes result to output. */ |
| 6573 | repsize = PyBytes_Size(repunicode); |
| 6574 | if (repsize > 1) { |
| 6575 | /* Make room for all additional bytes. */ |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 6576 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6577 | if (_PyBytes_Resize(&res, ressize+repsize-1)) { |
| 6578 | Py_DECREF(repunicode); |
| 6579 | goto onError; |
| 6580 | } |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 6581 | str = PyBytes_AS_STRING(res) + respos; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6582 | ressize += repsize-1; |
| 6583 | } |
| 6584 | memcpy(str, PyBytes_AsString(repunicode), repsize); |
| 6585 | str += repsize; |
| 6586 | p = startp + newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6587 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6588 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6589 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6590 | /* need more space? (at least enough for what we |
| 6591 | have+the replacement+the rest of the string, so |
| 6592 | we won't have to check space for encodable characters) */ |
| 6593 | respos = str - PyBytes_AS_STRING(res); |
| 6594 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 6595 | requiredsize = respos+repsize+(endp-collend); |
| 6596 | if (requiredsize > ressize) { |
| 6597 | if (requiredsize<2*ressize) |
| 6598 | requiredsize = 2*ressize; |
| 6599 | if (_PyBytes_Resize(&res, requiredsize)) { |
| 6600 | Py_DECREF(repunicode); |
| 6601 | goto onError; |
| 6602 | } |
| 6603 | str = PyBytes_AS_STRING(res) + respos; |
| 6604 | ressize = requiredsize; |
| 6605 | } |
| 6606 | /* check if there is anything unencodable in the replacement |
| 6607 | and copy it to the output */ |
| 6608 | for (uni2 = PyUnicode_AS_UNICODE(repunicode);repsize-->0; ++uni2, ++str) { |
| 6609 | c = *uni2; |
| 6610 | if (c >= limit) { |
| 6611 | raise_encode_exception(&exc, encoding, startp, size, |
| 6612 | unicodepos, unicodepos+1, reason); |
| 6613 | Py_DECREF(repunicode); |
| 6614 | goto onError; |
| 6615 | } |
| 6616 | *str = (char)c; |
| 6617 | } |
| 6618 | p = startp + newpos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6619 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6620 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6621 | } |
| 6622 | } |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6623 | /* Resize if we allocated to much */ |
| 6624 | size = str - PyBytes_AS_STRING(res); |
| 6625 | if (size < ressize) { /* If this falls res will be NULL */ |
Alexandre Vassalotti | bad1b92 | 2008-12-27 09:49:09 +0000 | [diff] [blame] | 6626 | assert(size >= 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6627 | if (_PyBytes_Resize(&res, size) < 0) |
| 6628 | goto onError; |
| 6629 | } |
| 6630 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6631 | Py_XDECREF(errorHandler); |
| 6632 | Py_XDECREF(exc); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6633 | return res; |
| 6634 | |
| 6635 | onError: |
| 6636 | Py_XDECREF(res); |
| 6637 | Py_XDECREF(errorHandler); |
| 6638 | Py_XDECREF(exc); |
| 6639 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6640 | } |
| 6641 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6642 | PyObject * |
| 6643 | PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6644 | Py_ssize_t size, |
| 6645 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6646 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6647 | return unicode_encode_ucs1(p, size, errors, 256); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6648 | } |
| 6649 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6650 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6651 | _PyUnicode_AsLatin1String(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6652 | { |
| 6653 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6654 | PyErr_BadArgument(); |
| 6655 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6656 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6657 | if (PyUnicode_READY(unicode) == -1) |
| 6658 | return NULL; |
| 6659 | /* Fast path: if it is a one-byte string, construct |
| 6660 | bytes object directly. */ |
| 6661 | if (PyUnicode_KIND(unicode) == PyUnicode_1BYTE_KIND) |
| 6662 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 6663 | PyUnicode_GET_LENGTH(unicode)); |
| 6664 | /* Non-Latin-1 characters present. Defer to above function to |
| 6665 | raise the exception. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6666 | return PyUnicode_EncodeLatin1(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6667 | PyUnicode_GET_SIZE(unicode), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6668 | errors); |
| 6669 | } |
| 6670 | |
| 6671 | PyObject* |
| 6672 | PyUnicode_AsLatin1String(PyObject *unicode) |
| 6673 | { |
| 6674 | return _PyUnicode_AsLatin1String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6675 | } |
| 6676 | |
| 6677 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ |
| 6678 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6679 | PyObject * |
| 6680 | PyUnicode_DecodeASCII(const char *s, |
| 6681 | Py_ssize_t size, |
| 6682 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6683 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6684 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6685 | PyUnicodeObject *v; |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6686 | Py_UNICODE *u; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6687 | Py_ssize_t startinpos; |
| 6688 | Py_ssize_t endinpos; |
| 6689 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6690 | const char *e; |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6691 | int has_error; |
| 6692 | const unsigned char *p = (const unsigned char *)s; |
| 6693 | const unsigned char *end = p + size; |
| 6694 | 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] | 6695 | PyObject *errorHandler = NULL; |
| 6696 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6697 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6698 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6699 | if (size == 1 && (unsigned char)s[0] < 128) |
| 6700 | return get_latin1_char((unsigned char)s[0]); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6701 | |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6702 | has_error = 0; |
| 6703 | while (p < end && !has_error) { |
| 6704 | /* Fast path, see below in PyUnicode_DecodeUTF8Stateful for |
| 6705 | an explanation. */ |
| 6706 | if (!((size_t) p & LONG_PTR_MASK)) { |
| 6707 | /* Help register allocation */ |
| 6708 | register const unsigned char *_p = p; |
| 6709 | while (_p < aligned_end) { |
| 6710 | unsigned long value = *(unsigned long *) _p; |
| 6711 | if (value & ASCII_CHAR_MASK) { |
| 6712 | has_error = 1; |
| 6713 | break; |
| 6714 | } |
| 6715 | _p += SIZEOF_LONG; |
| 6716 | } |
| 6717 | if (_p == end) |
| 6718 | break; |
| 6719 | if (has_error) |
| 6720 | break; |
| 6721 | p = _p; |
| 6722 | } |
| 6723 | if (*p & 0x80) { |
| 6724 | has_error = 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6725 | break; |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6726 | } |
| 6727 | else { |
| 6728 | ++p; |
| 6729 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 6730 | } |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6731 | if (!has_error) |
| 6732 | return unicode_fromascii((const unsigned char *)s, size); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6733 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6734 | v = _PyUnicode_New(size); |
| 6735 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6736 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6737 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6738 | return (PyObject *)v; |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6739 | u = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6740 | e = s + size; |
| 6741 | while (s < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6742 | register unsigned char c = (unsigned char)*s; |
| 6743 | if (c < 128) { |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6744 | *u++ = c; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6745 | ++s; |
| 6746 | } |
| 6747 | else { |
| 6748 | startinpos = s-starts; |
| 6749 | endinpos = startinpos + 1; |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6750 | outpos = u - (Py_UNICODE *)PyUnicode_AS_UNICODE(v); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6751 | if (unicode_decode_call_errorhandler( |
| 6752 | errors, &errorHandler, |
| 6753 | "ascii", "ordinal not in range(128)", |
| 6754 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6755 | &v, &outpos, &u)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6756 | goto onError; |
| 6757 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6758 | } |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6759 | if (u - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
| 6760 | if (PyUnicode_Resize((PyObject**)&v, u - PyUnicode_AS_UNICODE(v)) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6761 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6762 | Py_XDECREF(errorHandler); |
| 6763 | Py_XDECREF(exc); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 6764 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 6765 | if (_PyUnicode_READY_REPLACE(&v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6766 | Py_DECREF(v); |
| 6767 | return NULL; |
| 6768 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 6769 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 6770 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6771 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6772 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6773 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6774 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6775 | Py_XDECREF(errorHandler); |
| 6776 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6777 | return NULL; |
| 6778 | } |
| 6779 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6780 | PyObject * |
| 6781 | PyUnicode_EncodeASCII(const Py_UNICODE *p, |
| 6782 | Py_ssize_t size, |
| 6783 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6784 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6785 | return unicode_encode_ucs1(p, size, errors, 128); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6786 | } |
| 6787 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6788 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6789 | _PyUnicode_AsASCIIString(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6790 | { |
| 6791 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6792 | PyErr_BadArgument(); |
| 6793 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6794 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6795 | if (PyUnicode_READY(unicode) == -1) |
| 6796 | return NULL; |
| 6797 | /* Fast path: if it is an ASCII-only string, construct bytes object |
| 6798 | directly. Else defer to above function to raise the exception. */ |
| 6799 | if (PyUnicode_MAX_CHAR_VALUE(unicode) < 128) |
| 6800 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 6801 | PyUnicode_GET_LENGTH(unicode)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6802 | return PyUnicode_EncodeASCII(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6803 | PyUnicode_GET_SIZE(unicode), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6804 | errors); |
| 6805 | } |
| 6806 | |
| 6807 | PyObject * |
| 6808 | PyUnicode_AsASCIIString(PyObject *unicode) |
| 6809 | { |
| 6810 | return _PyUnicode_AsASCIIString(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6811 | } |
| 6812 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 6813 | #ifdef HAVE_MBCS |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6814 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6815 | /* --- MBCS codecs for Windows -------------------------------------------- */ |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6816 | |
Hirokazu Yamamoto | 3530246 | 2009-03-21 13:23:27 +0000 | [diff] [blame] | 6817 | #if SIZEOF_INT < SIZEOF_SIZE_T |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6818 | #define NEED_RETRY |
| 6819 | #endif |
| 6820 | |
| 6821 | /* XXX This code is limited to "true" double-byte encodings, as |
| 6822 | a) it assumes an incomplete character consists of a single byte, and |
| 6823 | b) IsDBCSLeadByte (probably) does not work for non-DBCS multi-byte |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6824 | encodings, see IsDBCSLeadByteEx documentation. */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6825 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6826 | static int |
| 6827 | is_dbcs_lead_byte(const char *s, int offset) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6828 | { |
| 6829 | const char *curr = s + offset; |
| 6830 | |
| 6831 | if (IsDBCSLeadByte(*curr)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6832 | const char *prev = CharPrev(s, curr); |
| 6833 | return (prev == curr) || !IsDBCSLeadByte(*prev) || (curr - prev == 2); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6834 | } |
| 6835 | return 0; |
| 6836 | } |
| 6837 | |
| 6838 | /* |
| 6839 | * Decode MBCS string into unicode object. If 'final' is set, converts |
| 6840 | * trailing lead-byte too. Returns consumed size if succeed, -1 otherwise. |
| 6841 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6842 | static int |
| 6843 | decode_mbcs(PyUnicodeObject **v, |
| 6844 | const char *s, /* MBCS string */ |
| 6845 | int size, /* sizeof MBCS string */ |
| 6846 | int final, |
| 6847 | const char *errors) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6848 | { |
| 6849 | Py_UNICODE *p; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6850 | Py_ssize_t n; |
| 6851 | DWORD usize; |
| 6852 | DWORD flags; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6853 | |
| 6854 | assert(size >= 0); |
| 6855 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6856 | /* check and handle 'errors' arg */ |
| 6857 | if (errors==NULL || strcmp(errors, "strict")==0) |
| 6858 | flags = MB_ERR_INVALID_CHARS; |
| 6859 | else if (strcmp(errors, "ignore")==0) |
| 6860 | flags = 0; |
| 6861 | else { |
| 6862 | PyErr_Format(PyExc_ValueError, |
| 6863 | "mbcs encoding does not support errors='%s'", |
| 6864 | errors); |
| 6865 | return -1; |
| 6866 | } |
| 6867 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6868 | /* Skip trailing lead-byte unless 'final' is set */ |
| 6869 | if (!final && size >= 1 && is_dbcs_lead_byte(s, size - 1)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6870 | --size; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6871 | |
| 6872 | /* First get the size of the result */ |
| 6873 | if (size > 0) { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6874 | usize = MultiByteToWideChar(CP_ACP, flags, s, size, NULL, 0); |
| 6875 | if (usize==0) |
| 6876 | goto mbcs_decode_error; |
| 6877 | } else |
| 6878 | usize = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6879 | |
| 6880 | if (*v == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6881 | /* Create unicode object */ |
| 6882 | *v = _PyUnicode_New(usize); |
| 6883 | if (*v == NULL) |
| 6884 | return -1; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6885 | n = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6886 | } |
| 6887 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6888 | /* Extend unicode object */ |
| 6889 | n = PyUnicode_GET_SIZE(*v); |
Victor Stinner | 2fd8227 | 2011-10-03 04:06:05 +0200 | [diff] [blame] | 6890 | if (PyUnicode_Resize((PyObject**)v, n + usize) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6891 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6892 | } |
| 6893 | |
| 6894 | /* Do the conversion */ |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6895 | if (usize > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6896 | p = PyUnicode_AS_UNICODE(*v) + n; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6897 | if (0 == MultiByteToWideChar(CP_ACP, flags, s, size, p, usize)) { |
| 6898 | goto mbcs_decode_error; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6899 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6900 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6901 | return size; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6902 | |
| 6903 | mbcs_decode_error: |
| 6904 | /* If the last error was ERROR_NO_UNICODE_TRANSLATION, then |
| 6905 | we raise a UnicodeDecodeError - else it is a 'generic' |
| 6906 | windows error |
| 6907 | */ |
| 6908 | if (GetLastError()==ERROR_NO_UNICODE_TRANSLATION) { |
| 6909 | /* Ideally, we should get reason from FormatMessage - this |
| 6910 | is the Windows 2000 English version of the message |
| 6911 | */ |
| 6912 | PyObject *exc = NULL; |
| 6913 | const char *reason = "No mapping for the Unicode character exists " |
| 6914 | "in the target multi-byte code page."; |
| 6915 | make_decode_exception(&exc, "mbcs", s, size, 0, 0, reason); |
| 6916 | if (exc != NULL) { |
| 6917 | PyCodec_StrictErrors(exc); |
| 6918 | Py_DECREF(exc); |
| 6919 | } |
| 6920 | } else { |
| 6921 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 6922 | } |
| 6923 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6924 | } |
| 6925 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6926 | PyObject * |
| 6927 | PyUnicode_DecodeMBCSStateful(const char *s, |
| 6928 | Py_ssize_t size, |
| 6929 | const char *errors, |
| 6930 | Py_ssize_t *consumed) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6931 | { |
| 6932 | PyUnicodeObject *v = NULL; |
| 6933 | int done; |
| 6934 | |
| 6935 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6936 | *consumed = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6937 | |
| 6938 | #ifdef NEED_RETRY |
| 6939 | retry: |
| 6940 | if (size > INT_MAX) |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6941 | done = decode_mbcs(&v, s, INT_MAX, 0, errors); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6942 | else |
| 6943 | #endif |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6944 | done = decode_mbcs(&v, s, (int)size, !consumed, errors); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6945 | |
| 6946 | if (done < 0) { |
| 6947 | Py_XDECREF(v); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6948 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6949 | } |
| 6950 | |
| 6951 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6952 | *consumed += done; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6953 | |
| 6954 | #ifdef NEED_RETRY |
| 6955 | if (size > INT_MAX) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6956 | s += done; |
| 6957 | size -= done; |
| 6958 | goto retry; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6959 | } |
| 6960 | #endif |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 6961 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 6962 | if (_PyUnicode_READY_REPLACE(&v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6963 | Py_DECREF(v); |
| 6964 | return NULL; |
| 6965 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 6966 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 6967 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6968 | return (PyObject *)v; |
| 6969 | } |
| 6970 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6971 | PyObject * |
| 6972 | PyUnicode_DecodeMBCS(const char *s, |
| 6973 | Py_ssize_t size, |
| 6974 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6975 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6976 | return PyUnicode_DecodeMBCSStateful(s, size, errors, NULL); |
| 6977 | } |
| 6978 | |
| 6979 | /* |
| 6980 | * Convert unicode into string object (MBCS). |
| 6981 | * Returns 0 if succeed, -1 otherwise. |
| 6982 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6983 | static int |
| 6984 | encode_mbcs(PyObject **repr, |
| 6985 | const Py_UNICODE *p, /* unicode */ |
| 6986 | int size, /* size of unicode */ |
| 6987 | const char* errors) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6988 | { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6989 | BOOL usedDefaultChar = FALSE; |
| 6990 | BOOL *pusedDefaultChar; |
| 6991 | int mbcssize; |
| 6992 | Py_ssize_t n; |
| 6993 | PyObject *exc = NULL; |
| 6994 | DWORD flags; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6995 | |
| 6996 | assert(size >= 0); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6997 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6998 | /* check and handle 'errors' arg */ |
| 6999 | if (errors==NULL || strcmp(errors, "strict")==0) { |
| 7000 | flags = WC_NO_BEST_FIT_CHARS; |
| 7001 | pusedDefaultChar = &usedDefaultChar; |
| 7002 | } else if (strcmp(errors, "replace")==0) { |
| 7003 | flags = 0; |
| 7004 | pusedDefaultChar = NULL; |
| 7005 | } else { |
| 7006 | PyErr_Format(PyExc_ValueError, |
| 7007 | "mbcs encoding does not support errors='%s'", |
| 7008 | errors); |
| 7009 | return -1; |
| 7010 | } |
| 7011 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7012 | /* First get the size of the result */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7013 | if (size > 0) { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7014 | mbcssize = WideCharToMultiByte(CP_ACP, flags, p, size, NULL, 0, |
| 7015 | NULL, pusedDefaultChar); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7016 | if (mbcssize == 0) { |
| 7017 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 7018 | return -1; |
| 7019 | } |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7020 | /* If we used a default char, then we failed! */ |
| 7021 | if (pusedDefaultChar && *pusedDefaultChar) |
| 7022 | goto mbcs_encode_error; |
| 7023 | } else { |
| 7024 | mbcssize = 0; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7025 | } |
| 7026 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7027 | if (*repr == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7028 | /* Create string object */ |
| 7029 | *repr = PyBytes_FromStringAndSize(NULL, mbcssize); |
| 7030 | if (*repr == NULL) |
| 7031 | return -1; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7032 | n = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7033 | } |
| 7034 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7035 | /* Extend string object */ |
| 7036 | n = PyBytes_Size(*repr); |
| 7037 | if (_PyBytes_Resize(repr, n + mbcssize) < 0) |
| 7038 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7039 | } |
| 7040 | |
| 7041 | /* Do the conversion */ |
| 7042 | if (size > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7043 | char *s = PyBytes_AS_STRING(*repr) + n; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7044 | if (0 == WideCharToMultiByte(CP_ACP, flags, p, size, s, mbcssize, |
| 7045 | NULL, pusedDefaultChar)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7046 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 7047 | return -1; |
| 7048 | } |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7049 | if (pusedDefaultChar && *pusedDefaultChar) |
| 7050 | goto mbcs_encode_error; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7051 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7052 | return 0; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7053 | |
| 7054 | mbcs_encode_error: |
| 7055 | raise_encode_exception(&exc, "mbcs", p, size, 0, 0, "invalid character"); |
| 7056 | Py_XDECREF(exc); |
| 7057 | return -1; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7058 | } |
| 7059 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7060 | PyObject * |
| 7061 | PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
| 7062 | Py_ssize_t size, |
| 7063 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7064 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7065 | PyObject *repr = NULL; |
| 7066 | int ret; |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 7067 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7068 | #ifdef NEED_RETRY |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7069 | retry: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7070 | if (size > INT_MAX) |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7071 | ret = encode_mbcs(&repr, p, INT_MAX, errors); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7072 | else |
| 7073 | #endif |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7074 | ret = encode_mbcs(&repr, p, (int)size, errors); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7075 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7076 | if (ret < 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7077 | Py_XDECREF(repr); |
| 7078 | return NULL; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7079 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7080 | |
| 7081 | #ifdef NEED_RETRY |
| 7082 | if (size > INT_MAX) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7083 | p += INT_MAX; |
| 7084 | size -= INT_MAX; |
| 7085 | goto retry; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7086 | } |
| 7087 | #endif |
| 7088 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7089 | return repr; |
| 7090 | } |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 7091 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7092 | PyObject * |
| 7093 | PyUnicode_AsMBCSString(PyObject *unicode) |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 7094 | { |
| 7095 | if (!PyUnicode_Check(unicode)) { |
| 7096 | PyErr_BadArgument(); |
| 7097 | return NULL; |
| 7098 | } |
| 7099 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7100 | PyUnicode_GET_SIZE(unicode), |
| 7101 | NULL); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 7102 | } |
| 7103 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7104 | #undef NEED_RETRY |
| 7105 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 7106 | #endif /* HAVE_MBCS */ |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7107 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7108 | /* --- Character Mapping Codec -------------------------------------------- */ |
| 7109 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7110 | PyObject * |
| 7111 | PyUnicode_DecodeCharmap(const char *s, |
| 7112 | Py_ssize_t size, |
| 7113 | PyObject *mapping, |
| 7114 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7115 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7116 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7117 | Py_ssize_t startinpos; |
| 7118 | Py_ssize_t endinpos; |
| 7119 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7120 | const char *e; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7121 | PyUnicodeObject *v; |
| 7122 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7123 | Py_ssize_t extrachars = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7124 | PyObject *errorHandler = NULL; |
| 7125 | PyObject *exc = NULL; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7126 | Py_UNICODE *mapstring = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7127 | Py_ssize_t maplen = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7128 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7129 | /* Default to Latin-1 */ |
| 7130 | if (mapping == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7131 | return PyUnicode_DecodeLatin1(s, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7132 | |
| 7133 | v = _PyUnicode_New(size); |
| 7134 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7135 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7136 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7137 | return (PyObject *)v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7138 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7139 | e = s + size; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7140 | if (PyUnicode_CheckExact(mapping)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7141 | mapstring = PyUnicode_AS_UNICODE(mapping); |
| 7142 | maplen = PyUnicode_GET_SIZE(mapping); |
| 7143 | while (s < e) { |
| 7144 | unsigned char ch = *s; |
| 7145 | Py_UNICODE x = 0xfffe; /* illegal value */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7146 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7147 | if (ch < maplen) |
| 7148 | x = mapstring[ch]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7149 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7150 | if (x == 0xfffe) { |
| 7151 | /* undefined mapping */ |
| 7152 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 7153 | startinpos = s-starts; |
| 7154 | endinpos = startinpos+1; |
| 7155 | if (unicode_decode_call_errorhandler( |
| 7156 | errors, &errorHandler, |
| 7157 | "charmap", "character maps to <undefined>", |
| 7158 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 7159 | &v, &outpos, &p)) { |
| 7160 | goto onError; |
| 7161 | } |
| 7162 | continue; |
| 7163 | } |
| 7164 | *p++ = x; |
| 7165 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7166 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7167 | } |
| 7168 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7169 | while (s < e) { |
| 7170 | unsigned char ch = *s; |
| 7171 | PyObject *w, *x; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7172 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7173 | /* Get mapping (char ordinal -> integer, Unicode char or None) */ |
| 7174 | w = PyLong_FromLong((long)ch); |
| 7175 | if (w == NULL) |
| 7176 | goto onError; |
| 7177 | x = PyObject_GetItem(mapping, w); |
| 7178 | Py_DECREF(w); |
| 7179 | if (x == NULL) { |
| 7180 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 7181 | /* No mapping found means: mapping is undefined. */ |
| 7182 | PyErr_Clear(); |
| 7183 | x = Py_None; |
| 7184 | Py_INCREF(x); |
| 7185 | } else |
| 7186 | goto onError; |
| 7187 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7188 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7189 | /* Apply mapping */ |
| 7190 | if (PyLong_Check(x)) { |
| 7191 | long value = PyLong_AS_LONG(x); |
| 7192 | if (value < 0 || value > 65535) { |
| 7193 | PyErr_SetString(PyExc_TypeError, |
| 7194 | "character mapping must be in range(65536)"); |
| 7195 | Py_DECREF(x); |
| 7196 | goto onError; |
| 7197 | } |
| 7198 | *p++ = (Py_UNICODE)value; |
| 7199 | } |
| 7200 | else if (x == Py_None) { |
| 7201 | /* undefined mapping */ |
| 7202 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 7203 | startinpos = s-starts; |
| 7204 | endinpos = startinpos+1; |
| 7205 | if (unicode_decode_call_errorhandler( |
| 7206 | errors, &errorHandler, |
| 7207 | "charmap", "character maps to <undefined>", |
| 7208 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 7209 | &v, &outpos, &p)) { |
| 7210 | Py_DECREF(x); |
| 7211 | goto onError; |
| 7212 | } |
| 7213 | Py_DECREF(x); |
| 7214 | continue; |
| 7215 | } |
| 7216 | else if (PyUnicode_Check(x)) { |
| 7217 | Py_ssize_t targetsize = PyUnicode_GET_SIZE(x); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7218 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7219 | if (targetsize == 1) |
| 7220 | /* 1-1 mapping */ |
| 7221 | *p++ = *PyUnicode_AS_UNICODE(x); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7222 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7223 | else if (targetsize > 1) { |
| 7224 | /* 1-n mapping */ |
| 7225 | if (targetsize > extrachars) { |
| 7226 | /* resize first */ |
| 7227 | Py_ssize_t oldpos = p - PyUnicode_AS_UNICODE(v); |
| 7228 | Py_ssize_t needed = (targetsize - extrachars) + \ |
| 7229 | (targetsize << 2); |
| 7230 | extrachars += needed; |
| 7231 | /* XXX overflow detection missing */ |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 7232 | if (PyUnicode_Resize((PyObject**)&v, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7233 | PyUnicode_GET_SIZE(v) + needed) < 0) { |
| 7234 | Py_DECREF(x); |
| 7235 | goto onError; |
| 7236 | } |
| 7237 | p = PyUnicode_AS_UNICODE(v) + oldpos; |
| 7238 | } |
| 7239 | Py_UNICODE_COPY(p, |
| 7240 | PyUnicode_AS_UNICODE(x), |
| 7241 | targetsize); |
| 7242 | p += targetsize; |
| 7243 | extrachars -= targetsize; |
| 7244 | } |
| 7245 | /* 1-0 mapping: skip the character */ |
| 7246 | } |
| 7247 | else { |
| 7248 | /* wrong return value */ |
| 7249 | PyErr_SetString(PyExc_TypeError, |
| 7250 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7251 | Py_DECREF(x); |
| 7252 | goto onError; |
| 7253 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7254 | Py_DECREF(x); |
| 7255 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7256 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7257 | } |
| 7258 | if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 7259 | if (PyUnicode_Resize((PyObject**)&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7260 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7261 | Py_XDECREF(errorHandler); |
| 7262 | Py_XDECREF(exc); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 7263 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 7264 | if (_PyUnicode_READY_REPLACE(&v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7265 | Py_DECREF(v); |
| 7266 | return NULL; |
| 7267 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 7268 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 7269 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7270 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7271 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7272 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7273 | Py_XDECREF(errorHandler); |
| 7274 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7275 | Py_XDECREF(v); |
| 7276 | return NULL; |
| 7277 | } |
| 7278 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7279 | /* Charmap encoding: the lookup table */ |
| 7280 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7281 | struct encoding_map { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7282 | PyObject_HEAD |
| 7283 | unsigned char level1[32]; |
| 7284 | int count2, count3; |
| 7285 | unsigned char level23[1]; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7286 | }; |
| 7287 | |
| 7288 | static PyObject* |
| 7289 | encoding_map_size(PyObject *obj, PyObject* args) |
| 7290 | { |
| 7291 | struct encoding_map *map = (struct encoding_map*)obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7292 | return PyLong_FromLong(sizeof(*map) - 1 + 16*map->count2 + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7293 | 128*map->count3); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7294 | } |
| 7295 | |
| 7296 | static PyMethodDef encoding_map_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7297 | {"size", encoding_map_size, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7298 | PyDoc_STR("Return the size (in bytes) of this object") }, |
| 7299 | { 0 } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7300 | }; |
| 7301 | |
| 7302 | static void |
| 7303 | encoding_map_dealloc(PyObject* o) |
| 7304 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7305 | PyObject_FREE(o); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7306 | } |
| 7307 | |
| 7308 | static PyTypeObject EncodingMapType = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7309 | PyVarObject_HEAD_INIT(NULL, 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7310 | "EncodingMap", /*tp_name*/ |
| 7311 | sizeof(struct encoding_map), /*tp_basicsize*/ |
| 7312 | 0, /*tp_itemsize*/ |
| 7313 | /* methods */ |
| 7314 | encoding_map_dealloc, /*tp_dealloc*/ |
| 7315 | 0, /*tp_print*/ |
| 7316 | 0, /*tp_getattr*/ |
| 7317 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 7318 | 0, /*tp_reserved*/ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7319 | 0, /*tp_repr*/ |
| 7320 | 0, /*tp_as_number*/ |
| 7321 | 0, /*tp_as_sequence*/ |
| 7322 | 0, /*tp_as_mapping*/ |
| 7323 | 0, /*tp_hash*/ |
| 7324 | 0, /*tp_call*/ |
| 7325 | 0, /*tp_str*/ |
| 7326 | 0, /*tp_getattro*/ |
| 7327 | 0, /*tp_setattro*/ |
| 7328 | 0, /*tp_as_buffer*/ |
| 7329 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 7330 | 0, /*tp_doc*/ |
| 7331 | 0, /*tp_traverse*/ |
| 7332 | 0, /*tp_clear*/ |
| 7333 | 0, /*tp_richcompare*/ |
| 7334 | 0, /*tp_weaklistoffset*/ |
| 7335 | 0, /*tp_iter*/ |
| 7336 | 0, /*tp_iternext*/ |
| 7337 | encoding_map_methods, /*tp_methods*/ |
| 7338 | 0, /*tp_members*/ |
| 7339 | 0, /*tp_getset*/ |
| 7340 | 0, /*tp_base*/ |
| 7341 | 0, /*tp_dict*/ |
| 7342 | 0, /*tp_descr_get*/ |
| 7343 | 0, /*tp_descr_set*/ |
| 7344 | 0, /*tp_dictoffset*/ |
| 7345 | 0, /*tp_init*/ |
| 7346 | 0, /*tp_alloc*/ |
| 7347 | 0, /*tp_new*/ |
| 7348 | 0, /*tp_free*/ |
| 7349 | 0, /*tp_is_gc*/ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7350 | }; |
| 7351 | |
| 7352 | PyObject* |
| 7353 | PyUnicode_BuildEncodingMap(PyObject* string) |
| 7354 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7355 | PyObject *result; |
| 7356 | struct encoding_map *mresult; |
| 7357 | int i; |
| 7358 | int need_dict = 0; |
| 7359 | unsigned char level1[32]; |
| 7360 | unsigned char level2[512]; |
| 7361 | unsigned char *mlevel1, *mlevel2, *mlevel3; |
| 7362 | int count2 = 0, count3 = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7363 | int kind; |
| 7364 | void *data; |
| 7365 | Py_UCS4 ch; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7366 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7367 | if (!PyUnicode_Check(string) || PyUnicode_GET_LENGTH(string) != 256) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7368 | PyErr_BadArgument(); |
| 7369 | return NULL; |
| 7370 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7371 | kind = PyUnicode_KIND(string); |
| 7372 | data = PyUnicode_DATA(string); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7373 | memset(level1, 0xFF, sizeof level1); |
| 7374 | memset(level2, 0xFF, sizeof level2); |
| 7375 | |
| 7376 | /* If there isn't a one-to-one mapping of NULL to \0, |
| 7377 | or if there are non-BMP characters, we need to use |
| 7378 | a mapping dictionary. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7379 | if (PyUnicode_READ(kind, data, 0) != 0) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7380 | need_dict = 1; |
| 7381 | for (i = 1; i < 256; i++) { |
| 7382 | int l1, l2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7383 | ch = PyUnicode_READ(kind, data, i); |
| 7384 | if (ch == 0 || ch > 0xFFFF) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7385 | need_dict = 1; |
| 7386 | break; |
| 7387 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7388 | if (ch == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7389 | /* unmapped character */ |
| 7390 | continue; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7391 | l1 = ch >> 11; |
| 7392 | l2 = ch >> 7; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7393 | if (level1[l1] == 0xFF) |
| 7394 | level1[l1] = count2++; |
| 7395 | if (level2[l2] == 0xFF) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7396 | level2[l2] = count3++; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7397 | } |
| 7398 | |
| 7399 | if (count2 >= 0xFF || count3 >= 0xFF) |
| 7400 | need_dict = 1; |
| 7401 | |
| 7402 | if (need_dict) { |
| 7403 | PyObject *result = PyDict_New(); |
| 7404 | PyObject *key, *value; |
| 7405 | if (!result) |
| 7406 | return NULL; |
| 7407 | for (i = 0; i < 256; i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7408 | key = PyLong_FromLong(PyUnicode_READ(kind, data, i)); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7409 | value = PyLong_FromLong(i); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7410 | if (!key || !value) |
| 7411 | goto failed1; |
| 7412 | if (PyDict_SetItem(result, key, value) == -1) |
| 7413 | goto failed1; |
| 7414 | Py_DECREF(key); |
| 7415 | Py_DECREF(value); |
| 7416 | } |
| 7417 | return result; |
| 7418 | failed1: |
| 7419 | Py_XDECREF(key); |
| 7420 | Py_XDECREF(value); |
| 7421 | Py_DECREF(result); |
| 7422 | return NULL; |
| 7423 | } |
| 7424 | |
| 7425 | /* Create a three-level trie */ |
| 7426 | result = PyObject_MALLOC(sizeof(struct encoding_map) + |
| 7427 | 16*count2 + 128*count3 - 1); |
| 7428 | if (!result) |
| 7429 | return PyErr_NoMemory(); |
| 7430 | PyObject_Init(result, &EncodingMapType); |
| 7431 | mresult = (struct encoding_map*)result; |
| 7432 | mresult->count2 = count2; |
| 7433 | mresult->count3 = count3; |
| 7434 | mlevel1 = mresult->level1; |
| 7435 | mlevel2 = mresult->level23; |
| 7436 | mlevel3 = mresult->level23 + 16*count2; |
| 7437 | memcpy(mlevel1, level1, 32); |
| 7438 | memset(mlevel2, 0xFF, 16*count2); |
| 7439 | memset(mlevel3, 0, 128*count3); |
| 7440 | count3 = 0; |
| 7441 | for (i = 1; i < 256; i++) { |
| 7442 | int o1, o2, o3, i2, i3; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7443 | if (PyUnicode_READ(kind, data, i) == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7444 | /* unmapped character */ |
| 7445 | continue; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7446 | o1 = PyUnicode_READ(kind, data, i)>>11; |
| 7447 | o2 = (PyUnicode_READ(kind, data, i)>>7) & 0xF; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7448 | i2 = 16*mlevel1[o1] + o2; |
| 7449 | if (mlevel2[i2] == 0xFF) |
| 7450 | mlevel2[i2] = count3++; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7451 | o3 = PyUnicode_READ(kind, data, i) & 0x7F; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7452 | i3 = 128*mlevel2[i2] + o3; |
| 7453 | mlevel3[i3] = i; |
| 7454 | } |
| 7455 | return result; |
| 7456 | } |
| 7457 | |
| 7458 | static int |
| 7459 | encoding_map_lookup(Py_UNICODE c, PyObject *mapping) |
| 7460 | { |
| 7461 | struct encoding_map *map = (struct encoding_map*)mapping; |
| 7462 | int l1 = c>>11; |
| 7463 | int l2 = (c>>7) & 0xF; |
| 7464 | int l3 = c & 0x7F; |
| 7465 | int i; |
| 7466 | |
| 7467 | #ifdef Py_UNICODE_WIDE |
| 7468 | if (c > 0xFFFF) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7469 | return -1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7470 | } |
| 7471 | #endif |
| 7472 | if (c == 0) |
| 7473 | return 0; |
| 7474 | /* level 1*/ |
| 7475 | i = map->level1[l1]; |
| 7476 | if (i == 0xFF) { |
| 7477 | return -1; |
| 7478 | } |
| 7479 | /* level 2*/ |
| 7480 | i = map->level23[16*i+l2]; |
| 7481 | if (i == 0xFF) { |
| 7482 | return -1; |
| 7483 | } |
| 7484 | /* level 3 */ |
| 7485 | i = map->level23[16*map->count2 + 128*i + l3]; |
| 7486 | if (i == 0) { |
| 7487 | return -1; |
| 7488 | } |
| 7489 | return i; |
| 7490 | } |
| 7491 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7492 | /* Lookup the character ch in the mapping. If the character |
| 7493 | can't be found, Py_None is returned (or NULL, if another |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 7494 | error occurred). */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7495 | static PyObject * |
| 7496 | charmapencode_lookup(Py_UNICODE c, PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7497 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7498 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7499 | PyObject *x; |
| 7500 | |
| 7501 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7502 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7503 | x = PyObject_GetItem(mapping, w); |
| 7504 | Py_DECREF(w); |
| 7505 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7506 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 7507 | /* No mapping found means: mapping is undefined. */ |
| 7508 | PyErr_Clear(); |
| 7509 | x = Py_None; |
| 7510 | Py_INCREF(x); |
| 7511 | return x; |
| 7512 | } else |
| 7513 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7514 | } |
Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 7515 | else if (x == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7516 | return x; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7517 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7518 | long value = PyLong_AS_LONG(x); |
| 7519 | if (value < 0 || value > 255) { |
| 7520 | PyErr_SetString(PyExc_TypeError, |
| 7521 | "character mapping must be in range(256)"); |
| 7522 | Py_DECREF(x); |
| 7523 | return NULL; |
| 7524 | } |
| 7525 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7526 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7527 | else if (PyBytes_Check(x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7528 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7529 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7530 | /* wrong return value */ |
| 7531 | PyErr_Format(PyExc_TypeError, |
| 7532 | "character mapping must return integer, bytes or None, not %.400s", |
| 7533 | x->ob_type->tp_name); |
| 7534 | Py_DECREF(x); |
| 7535 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7536 | } |
| 7537 | } |
| 7538 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7539 | static int |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 7540 | charmapencode_resize(PyObject **outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7541 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7542 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
| 7543 | /* exponentially overallocate to minimize reallocations */ |
| 7544 | if (requiredsize < 2*outsize) |
| 7545 | requiredsize = 2*outsize; |
| 7546 | if (_PyBytes_Resize(outobj, requiredsize)) |
| 7547 | return -1; |
| 7548 | return 0; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7549 | } |
| 7550 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7551 | typedef enum charmapencode_result { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7552 | enc_SUCCESS, enc_FAILED, enc_EXCEPTION |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7553 | } charmapencode_result; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7554 | /* 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] | 7555 | various state variables. Resize the output bytes object if not enough |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7556 | space is available. Return a new reference to the object that |
| 7557 | was put in the output buffer, or Py_None, if the mapping was undefined |
| 7558 | (in which case no character was written) or NULL, if a |
Andrew M. Kuchling | 8294de5 | 2005-11-02 16:36:12 +0000 | [diff] [blame] | 7559 | reallocation error occurred. The caller must decref the result */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7560 | static charmapencode_result |
| 7561 | charmapencode_output(Py_UNICODE c, PyObject *mapping, |
| 7562 | PyObject **outobj, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7563 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7564 | PyObject *rep; |
| 7565 | char *outstart; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7566 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7567 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 7568 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7569 | int res = encoding_map_lookup(c, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7570 | Py_ssize_t requiredsize = *outpos+1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7571 | if (res == -1) |
| 7572 | return enc_FAILED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7573 | if (outsize<requiredsize) |
| 7574 | if (charmapencode_resize(outobj, outpos, requiredsize)) |
| 7575 | return enc_EXCEPTION; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7576 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7577 | outstart[(*outpos)++] = (char)res; |
| 7578 | return enc_SUCCESS; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7579 | } |
| 7580 | |
| 7581 | rep = charmapencode_lookup(c, mapping); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7582 | if (rep==NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7583 | return enc_EXCEPTION; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7584 | else if (rep==Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7585 | Py_DECREF(rep); |
| 7586 | return enc_FAILED; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7587 | } else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7588 | if (PyLong_Check(rep)) { |
| 7589 | Py_ssize_t requiredsize = *outpos+1; |
| 7590 | if (outsize<requiredsize) |
| 7591 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 7592 | Py_DECREF(rep); |
| 7593 | return enc_EXCEPTION; |
| 7594 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7595 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7596 | outstart[(*outpos)++] = (char)PyLong_AS_LONG(rep); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7597 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7598 | else { |
| 7599 | const char *repchars = PyBytes_AS_STRING(rep); |
| 7600 | Py_ssize_t repsize = PyBytes_GET_SIZE(rep); |
| 7601 | Py_ssize_t requiredsize = *outpos+repsize; |
| 7602 | if (outsize<requiredsize) |
| 7603 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 7604 | Py_DECREF(rep); |
| 7605 | return enc_EXCEPTION; |
| 7606 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7607 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7608 | memcpy(outstart + *outpos, repchars, repsize); |
| 7609 | *outpos += repsize; |
| 7610 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7611 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7612 | Py_DECREF(rep); |
| 7613 | return enc_SUCCESS; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7614 | } |
| 7615 | |
| 7616 | /* handle an error in PyUnicode_EncodeCharmap |
| 7617 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7618 | static int |
| 7619 | charmap_encoding_error( |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7620 | 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] | 7621 | PyObject **exceptionObject, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 7622 | int *known_errorHandler, PyObject **errorHandler, const char *errors, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 7623 | PyObject **res, Py_ssize_t *respos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7624 | { |
| 7625 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7626 | Py_ssize_t repsize; |
| 7627 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7628 | Py_UNICODE *uni2; |
| 7629 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7630 | Py_ssize_t collstartpos = *inpos; |
| 7631 | Py_ssize_t collendpos = *inpos+1; |
| 7632 | Py_ssize_t collpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7633 | char *encoding = "charmap"; |
| 7634 | char *reason = "character maps to <undefined>"; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7635 | charmapencode_result x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7636 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7637 | /* find all unencodable characters */ |
| 7638 | while (collendpos < size) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7639 | PyObject *rep; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 7640 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7641 | int res = encoding_map_lookup(p[collendpos], mapping); |
| 7642 | if (res != -1) |
| 7643 | break; |
| 7644 | ++collendpos; |
| 7645 | continue; |
| 7646 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7647 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7648 | rep = charmapencode_lookup(p[collendpos], mapping); |
| 7649 | if (rep==NULL) |
| 7650 | return -1; |
| 7651 | else if (rep!=Py_None) { |
| 7652 | Py_DECREF(rep); |
| 7653 | break; |
| 7654 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7655 | Py_DECREF(rep); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7656 | ++collendpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7657 | } |
| 7658 | /* cache callback name lookup |
| 7659 | * (if not done yet, i.e. it's the first error) */ |
| 7660 | if (*known_errorHandler==-1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7661 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 7662 | *known_errorHandler = 1; |
| 7663 | else if (!strcmp(errors, "replace")) |
| 7664 | *known_errorHandler = 2; |
| 7665 | else if (!strcmp(errors, "ignore")) |
| 7666 | *known_errorHandler = 3; |
| 7667 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 7668 | *known_errorHandler = 4; |
| 7669 | else |
| 7670 | *known_errorHandler = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7671 | } |
| 7672 | switch (*known_errorHandler) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7673 | case 1: /* strict */ |
| 7674 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 7675 | return -1; |
| 7676 | case 2: /* replace */ |
| 7677 | for (collpos = collstartpos; collpos<collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7678 | x = charmapencode_output('?', mapping, res, respos); |
| 7679 | if (x==enc_EXCEPTION) { |
| 7680 | return -1; |
| 7681 | } |
| 7682 | else if (x==enc_FAILED) { |
| 7683 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 7684 | return -1; |
| 7685 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7686 | } |
| 7687 | /* fall through */ |
| 7688 | case 3: /* ignore */ |
| 7689 | *inpos = collendpos; |
| 7690 | break; |
| 7691 | case 4: /* xmlcharrefreplace */ |
| 7692 | /* generate replacement (temporarily (mis)uses p) */ |
| 7693 | for (collpos = collstartpos; collpos < collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7694 | char buffer[2+29+1+1]; |
| 7695 | char *cp; |
| 7696 | sprintf(buffer, "&#%d;", (int)p[collpos]); |
| 7697 | for (cp = buffer; *cp; ++cp) { |
| 7698 | x = charmapencode_output(*cp, mapping, res, respos); |
| 7699 | if (x==enc_EXCEPTION) |
| 7700 | return -1; |
| 7701 | else if (x==enc_FAILED) { |
| 7702 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 7703 | return -1; |
| 7704 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7705 | } |
| 7706 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7707 | *inpos = collendpos; |
| 7708 | break; |
| 7709 | default: |
| 7710 | repunicode = unicode_encode_call_errorhandler(errors, errorHandler, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7711 | encoding, reason, p, size, exceptionObject, |
| 7712 | collstartpos, collendpos, &newpos); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7713 | if (repunicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7714 | return -1; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 7715 | if (PyBytes_Check(repunicode)) { |
| 7716 | /* Directly copy bytes result to output. */ |
| 7717 | Py_ssize_t outsize = PyBytes_Size(*res); |
| 7718 | Py_ssize_t requiredsize; |
| 7719 | repsize = PyBytes_Size(repunicode); |
| 7720 | requiredsize = *respos + repsize; |
| 7721 | if (requiredsize > outsize) |
| 7722 | /* Make room for all additional bytes. */ |
| 7723 | if (charmapencode_resize(res, respos, requiredsize)) { |
| 7724 | Py_DECREF(repunicode); |
| 7725 | return -1; |
| 7726 | } |
| 7727 | memcpy(PyBytes_AsString(*res) + *respos, |
| 7728 | PyBytes_AsString(repunicode), repsize); |
| 7729 | *respos += repsize; |
| 7730 | *inpos = newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 7731 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 7732 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 7733 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7734 | /* generate replacement */ |
| 7735 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 7736 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7737 | x = charmapencode_output(*uni2, mapping, res, respos); |
| 7738 | if (x==enc_EXCEPTION) { |
| 7739 | return -1; |
| 7740 | } |
| 7741 | else if (x==enc_FAILED) { |
| 7742 | Py_DECREF(repunicode); |
| 7743 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 7744 | return -1; |
| 7745 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7746 | } |
| 7747 | *inpos = newpos; |
| 7748 | Py_DECREF(repunicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7749 | } |
| 7750 | return 0; |
| 7751 | } |
| 7752 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7753 | PyObject * |
| 7754 | PyUnicode_EncodeCharmap(const Py_UNICODE *p, |
| 7755 | Py_ssize_t size, |
| 7756 | PyObject *mapping, |
| 7757 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7758 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7759 | /* output object */ |
| 7760 | PyObject *res = NULL; |
| 7761 | /* current input position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7762 | Py_ssize_t inpos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7763 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7764 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7765 | PyObject *errorHandler = NULL; |
| 7766 | PyObject *exc = NULL; |
| 7767 | /* the following variable is used for caching string comparisons |
| 7768 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 7769 | * 3=ignore, 4=xmlcharrefreplace */ |
| 7770 | int known_errorHandler = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7771 | |
| 7772 | /* Default to Latin-1 */ |
| 7773 | if (mapping == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7774 | return PyUnicode_EncodeLatin1(p, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7775 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7776 | /* allocate enough for a simple encoding without |
| 7777 | replacements, if we need more, we'll resize */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7778 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7779 | if (res == NULL) |
| 7780 | goto onError; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 7781 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7782 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7783 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7784 | while (inpos<size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7785 | /* try to encode it */ |
| 7786 | charmapencode_result x = charmapencode_output(p[inpos], mapping, &res, &respos); |
| 7787 | if (x==enc_EXCEPTION) /* error */ |
| 7788 | goto onError; |
| 7789 | if (x==enc_FAILED) { /* unencodable character */ |
| 7790 | if (charmap_encoding_error(p, size, &inpos, mapping, |
| 7791 | &exc, |
| 7792 | &known_errorHandler, &errorHandler, errors, |
| 7793 | &res, &respos)) { |
| 7794 | goto onError; |
| 7795 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7796 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7797 | else |
| 7798 | /* done with this character => adjust input position */ |
| 7799 | ++inpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7800 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7801 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7802 | /* Resize if we allocated to much */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7803 | if (respos<PyBytes_GET_SIZE(res)) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 7804 | if (_PyBytes_Resize(&res, respos) < 0) |
| 7805 | goto onError; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 7806 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7807 | Py_XDECREF(exc); |
| 7808 | Py_XDECREF(errorHandler); |
| 7809 | return res; |
| 7810 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7811 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7812 | Py_XDECREF(res); |
| 7813 | Py_XDECREF(exc); |
| 7814 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7815 | return NULL; |
| 7816 | } |
| 7817 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7818 | PyObject * |
| 7819 | PyUnicode_AsCharmapString(PyObject *unicode, |
| 7820 | PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7821 | { |
| 7822 | if (!PyUnicode_Check(unicode) || mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7823 | PyErr_BadArgument(); |
| 7824 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7825 | } |
| 7826 | return PyUnicode_EncodeCharmap(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7827 | PyUnicode_GET_SIZE(unicode), |
| 7828 | mapping, |
| 7829 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7830 | } |
| 7831 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7832 | /* create or adjust a UnicodeTranslateError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7833 | static void |
| 7834 | make_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7835 | PyObject *unicode, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7836 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 7837 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7838 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7839 | if (*exceptionObject == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7840 | *exceptionObject = _PyUnicodeTranslateError_Create( |
| 7841 | unicode, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7842 | } |
| 7843 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7844 | if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
| 7845 | goto onError; |
| 7846 | if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) |
| 7847 | goto onError; |
| 7848 | if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) |
| 7849 | goto onError; |
| 7850 | return; |
| 7851 | onError: |
| 7852 | Py_DECREF(*exceptionObject); |
| 7853 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7854 | } |
| 7855 | } |
| 7856 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7857 | /* raises a UnicodeTranslateError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7858 | static void |
| 7859 | raise_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7860 | PyObject *unicode, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7861 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 7862 | const char *reason) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7863 | { |
| 7864 | make_translate_exception(exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7865 | unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7866 | if (*exceptionObject != NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7867 | PyCodec_StrictErrors(*exceptionObject); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7868 | } |
| 7869 | |
| 7870 | /* error handling callback helper: |
| 7871 | build arguments, call the callback and check the arguments, |
| 7872 | put the result into newpos and return the replacement string, which |
| 7873 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7874 | static PyObject * |
| 7875 | unicode_translate_call_errorhandler(const char *errors, |
| 7876 | PyObject **errorHandler, |
| 7877 | const char *reason, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7878 | PyObject *unicode, PyObject **exceptionObject, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7879 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 7880 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7881 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 7882 | 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] | 7883 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7884 | Py_ssize_t i_newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7885 | PyObject *restuple; |
| 7886 | PyObject *resunicode; |
| 7887 | |
| 7888 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7889 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7890 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7891 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7892 | } |
| 7893 | |
| 7894 | make_translate_exception(exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7895 | unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7896 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7897 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7898 | |
| 7899 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7900 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7901 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7902 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7903 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 7904 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7905 | Py_DECREF(restuple); |
| 7906 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7907 | } |
| 7908 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7909 | &resunicode, &i_newpos)) { |
| 7910 | Py_DECREF(restuple); |
| 7911 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7912 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7913 | if (i_newpos<0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7914 | *newpos = PyUnicode_GET_LENGTH(unicode)+i_newpos; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7915 | else |
| 7916 | *newpos = i_newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7917 | if (*newpos<0 || *newpos>PyUnicode_GET_LENGTH(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7918 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 7919 | Py_DECREF(restuple); |
| 7920 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 7921 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7922 | Py_INCREF(resunicode); |
| 7923 | Py_DECREF(restuple); |
| 7924 | return resunicode; |
| 7925 | } |
| 7926 | |
| 7927 | /* Lookup the character ch in the mapping and put the result in result, |
| 7928 | which must be decrefed by the caller. |
| 7929 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7930 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7931 | charmaptranslate_lookup(Py_UCS4 c, PyObject *mapping, PyObject **result) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7932 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7933 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7934 | PyObject *x; |
| 7935 | |
| 7936 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7937 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7938 | x = PyObject_GetItem(mapping, w); |
| 7939 | Py_DECREF(w); |
| 7940 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7941 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 7942 | /* No mapping found means: use 1:1 mapping. */ |
| 7943 | PyErr_Clear(); |
| 7944 | *result = NULL; |
| 7945 | return 0; |
| 7946 | } else |
| 7947 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7948 | } |
| 7949 | else if (x == Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7950 | *result = x; |
| 7951 | return 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7952 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7953 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7954 | long value = PyLong_AS_LONG(x); |
| 7955 | long max = PyUnicode_GetMax(); |
| 7956 | if (value < 0 || value > max) { |
| 7957 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5a2f7e60 | 2007-10-24 21:13:09 +0000 | [diff] [blame] | 7958 | "character mapping must be in range(0x%x)", max+1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7959 | Py_DECREF(x); |
| 7960 | return -1; |
| 7961 | } |
| 7962 | *result = x; |
| 7963 | return 0; |
| 7964 | } |
| 7965 | else if (PyUnicode_Check(x)) { |
| 7966 | *result = x; |
| 7967 | return 0; |
| 7968 | } |
| 7969 | else { |
| 7970 | /* wrong return value */ |
| 7971 | PyErr_SetString(PyExc_TypeError, |
| 7972 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7973 | Py_DECREF(x); |
| 7974 | return -1; |
| 7975 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7976 | } |
| 7977 | /* ensure that *outobj is at least requiredsize characters long, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7978 | if not reallocate and adjust various state variables. |
| 7979 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7980 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7981 | charmaptranslate_makespace(Py_UCS4 **outobj, Py_ssize_t *psize, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7982 | Py_ssize_t requiredsize) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7983 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7984 | Py_ssize_t oldsize = *psize; |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 7985 | if (requiredsize > oldsize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7986 | /* exponentially overallocate to minimize reallocations */ |
| 7987 | if (requiredsize < 2 * oldsize) |
| 7988 | requiredsize = 2 * oldsize; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7989 | *outobj = PyMem_Realloc(*outobj, requiredsize * sizeof(Py_UCS4)); |
| 7990 | if (*outobj == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7991 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7992 | *psize = requiredsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7993 | } |
| 7994 | return 0; |
| 7995 | } |
| 7996 | /* lookup the character, put the result in the output string and adjust |
| 7997 | various state variables. Return a new reference to the object that |
| 7998 | was put in the output buffer in *result, or Py_None, if the mapping was |
| 7999 | undefined (in which case no character was written). |
| 8000 | The called must decref result. |
| 8001 | Return 0 on success, -1 on error. */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8002 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8003 | charmaptranslate_output(PyObject *input, Py_ssize_t ipos, |
| 8004 | PyObject *mapping, Py_UCS4 **output, |
| 8005 | Py_ssize_t *osize, Py_ssize_t *opos, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8006 | PyObject **res) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8007 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8008 | Py_UCS4 curinp = PyUnicode_READ_CHAR(input, ipos); |
| 8009 | if (charmaptranslate_lookup(curinp, mapping, res)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8010 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8011 | if (*res==NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8012 | /* not found => default to 1:1 mapping */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8013 | (*output)[(*opos)++] = curinp; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8014 | } |
| 8015 | else if (*res==Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8016 | ; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8017 | else if (PyLong_Check(*res)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8018 | /* 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] | 8019 | (*output)[(*opos)++] = (Py_UCS4)PyLong_AS_LONG(*res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8020 | } |
| 8021 | else if (PyUnicode_Check(*res)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8022 | Py_ssize_t repsize; |
| 8023 | if (PyUnicode_READY(*res) == -1) |
| 8024 | return -1; |
| 8025 | repsize = PyUnicode_GET_LENGTH(*res); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8026 | if (repsize==1) { |
| 8027 | /* 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] | 8028 | (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8029 | } |
| 8030 | else if (repsize!=0) { |
| 8031 | /* more than one character */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8032 | Py_ssize_t requiredsize = *opos + |
| 8033 | (PyUnicode_GET_LENGTH(input) - ipos) + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8034 | repsize - 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8035 | Py_ssize_t i; |
| 8036 | if (charmaptranslate_makespace(output, osize, requiredsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8037 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8038 | for(i = 0; i < repsize; i++) |
| 8039 | (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, i); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8040 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8041 | } |
| 8042 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8043 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8044 | return 0; |
| 8045 | } |
| 8046 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8047 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8048 | _PyUnicode_TranslateCharmap(PyObject *input, |
| 8049 | PyObject *mapping, |
| 8050 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8051 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8052 | /* input object */ |
| 8053 | char *idata; |
| 8054 | Py_ssize_t size, i; |
| 8055 | int kind; |
| 8056 | /* output buffer */ |
| 8057 | Py_UCS4 *output = NULL; |
| 8058 | Py_ssize_t osize; |
| 8059 | PyObject *res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8060 | /* current output position */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8061 | Py_ssize_t opos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8062 | char *reason = "character maps to <undefined>"; |
| 8063 | PyObject *errorHandler = NULL; |
| 8064 | PyObject *exc = NULL; |
| 8065 | /* the following variable is used for caching string comparisons |
| 8066 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 8067 | * 3=ignore, 4=xmlcharrefreplace */ |
| 8068 | int known_errorHandler = -1; |
| 8069 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8070 | if (mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8071 | PyErr_BadArgument(); |
| 8072 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8073 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8074 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8075 | if (PyUnicode_READY(input) == -1) |
| 8076 | return NULL; |
| 8077 | idata = (char*)PyUnicode_DATA(input); |
| 8078 | kind = PyUnicode_KIND(input); |
| 8079 | size = PyUnicode_GET_LENGTH(input); |
| 8080 | i = 0; |
| 8081 | |
| 8082 | if (size == 0) { |
| 8083 | Py_INCREF(input); |
| 8084 | return input; |
| 8085 | } |
| 8086 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8087 | /* allocate enough for a simple 1:1 translation without |
| 8088 | replacements, if we need more, we'll resize */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8089 | osize = size; |
| 8090 | output = PyMem_Malloc(osize * sizeof(Py_UCS4)); |
| 8091 | opos = 0; |
| 8092 | if (output == NULL) { |
| 8093 | PyErr_NoMemory(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8094 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8095 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8096 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8097 | while (i<size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8098 | /* try to encode it */ |
| 8099 | PyObject *x = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8100 | if (charmaptranslate_output(input, i, mapping, |
| 8101 | &output, &osize, &opos, &x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8102 | Py_XDECREF(x); |
| 8103 | goto onError; |
| 8104 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8105 | Py_XDECREF(x); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8106 | if (x!=Py_None) /* it worked => adjust input pointer */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8107 | ++i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8108 | else { /* untranslatable character */ |
| 8109 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
| 8110 | Py_ssize_t repsize; |
| 8111 | Py_ssize_t newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8112 | Py_ssize_t uni2; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8113 | /* startpos for collecting untranslatable chars */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8114 | Py_ssize_t collstart = i; |
| 8115 | Py_ssize_t collend = i+1; |
| 8116 | Py_ssize_t coll; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8117 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8118 | /* find all untranslatable characters */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8119 | while (collend < size) { |
| 8120 | if (charmaptranslate_lookup(PyUnicode_READ(kind,idata, collend), mapping, &x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8121 | goto onError; |
| 8122 | Py_XDECREF(x); |
| 8123 | if (x!=Py_None) |
| 8124 | break; |
| 8125 | ++collend; |
| 8126 | } |
| 8127 | /* cache callback name lookup |
| 8128 | * (if not done yet, i.e. it's the first error) */ |
| 8129 | if (known_errorHandler==-1) { |
| 8130 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 8131 | known_errorHandler = 1; |
| 8132 | else if (!strcmp(errors, "replace")) |
| 8133 | known_errorHandler = 2; |
| 8134 | else if (!strcmp(errors, "ignore")) |
| 8135 | known_errorHandler = 3; |
| 8136 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 8137 | known_errorHandler = 4; |
| 8138 | else |
| 8139 | known_errorHandler = 0; |
| 8140 | } |
| 8141 | switch (known_errorHandler) { |
| 8142 | case 1: /* strict */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8143 | raise_translate_exception(&exc, input, collstart, |
| 8144 | collend, reason); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8145 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8146 | case 2: /* replace */ |
| 8147 | /* 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] | 8148 | for (coll = collstart; coll<collend; coll++) |
| 8149 | output[opos++] = '?'; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8150 | /* fall through */ |
| 8151 | case 3: /* ignore */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8152 | i = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8153 | break; |
| 8154 | case 4: /* xmlcharrefreplace */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8155 | /* generate replacement (temporarily (mis)uses i) */ |
| 8156 | for (i = collstart; i < collend; ++i) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8157 | char buffer[2+29+1+1]; |
| 8158 | char *cp; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8159 | sprintf(buffer, "&#%d;", PyUnicode_READ(kind, idata, i)); |
| 8160 | if (charmaptranslate_makespace(&output, &osize, |
| 8161 | opos+strlen(buffer)+(size-collend))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8162 | goto onError; |
| 8163 | for (cp = buffer; *cp; ++cp) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8164 | output[opos++] = *cp; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8165 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8166 | i = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8167 | break; |
| 8168 | default: |
| 8169 | repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8170 | reason, input, &exc, |
| 8171 | collstart, collend, &newpos); |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 8172 | if (repunicode == NULL || _PyUnicode_READY_REPLACE(&repunicode)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8173 | goto onError; |
| 8174 | /* generate replacement */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8175 | repsize = PyUnicode_GET_LENGTH(repunicode); |
| 8176 | if (charmaptranslate_makespace(&output, &osize, |
| 8177 | opos+repsize+(size-collend))) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8178 | Py_DECREF(repunicode); |
| 8179 | goto onError; |
| 8180 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8181 | for (uni2 = 0; repsize-->0; ++uni2) |
| 8182 | output[opos++] = PyUnicode_READ_CHAR(repunicode, uni2); |
| 8183 | i = newpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8184 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8185 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8186 | } |
| 8187 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8188 | res = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, output, opos); |
| 8189 | if (!res) |
| 8190 | goto onError; |
| 8191 | PyMem_Free(output); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8192 | Py_XDECREF(exc); |
| 8193 | Py_XDECREF(errorHandler); |
| 8194 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8195 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8196 | onError: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8197 | PyMem_Free(output); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8198 | Py_XDECREF(exc); |
| 8199 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8200 | return NULL; |
| 8201 | } |
| 8202 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8203 | /* Deprecated. Use PyUnicode_Translate instead. */ |
| 8204 | PyObject * |
| 8205 | PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
| 8206 | Py_ssize_t size, |
| 8207 | PyObject *mapping, |
| 8208 | const char *errors) |
| 8209 | { |
| 8210 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 8211 | if (!unicode) |
| 8212 | return NULL; |
| 8213 | return _PyUnicode_TranslateCharmap(unicode, mapping, errors); |
| 8214 | } |
| 8215 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8216 | PyObject * |
| 8217 | PyUnicode_Translate(PyObject *str, |
| 8218 | PyObject *mapping, |
| 8219 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8220 | { |
| 8221 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8222 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8223 | str = PyUnicode_FromObject(str); |
| 8224 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8225 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8226 | result = _PyUnicode_TranslateCharmap(str, mapping, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8227 | Py_DECREF(str); |
| 8228 | return result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8229 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8230 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8231 | Py_XDECREF(str); |
| 8232 | return NULL; |
| 8233 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8234 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8235 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8236 | fix_decimal_and_space_to_ascii(PyObject *self) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8237 | { |
| 8238 | /* No need to call PyUnicode_READY(self) because this function is only |
| 8239 | called as a callback from fixup() which does it already. */ |
| 8240 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 8241 | const int kind = PyUnicode_KIND(self); |
| 8242 | void *data = PyUnicode_DATA(self); |
| 8243 | Py_UCS4 maxchar = 0, ch, fixed; |
| 8244 | Py_ssize_t i; |
| 8245 | |
| 8246 | for (i = 0; i < len; ++i) { |
| 8247 | ch = PyUnicode_READ(kind, data, i); |
| 8248 | fixed = 0; |
| 8249 | if (ch > 127) { |
| 8250 | if (Py_UNICODE_ISSPACE(ch)) |
| 8251 | fixed = ' '; |
| 8252 | else { |
| 8253 | const int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8254 | if (decimal >= 0) |
| 8255 | fixed = '0' + decimal; |
| 8256 | } |
| 8257 | if (fixed != 0) { |
| 8258 | if (fixed > maxchar) |
| 8259 | maxchar = fixed; |
| 8260 | PyUnicode_WRITE(kind, data, i, fixed); |
| 8261 | } |
| 8262 | else if (ch > maxchar) |
| 8263 | maxchar = ch; |
| 8264 | } |
| 8265 | else if (ch > maxchar) |
| 8266 | maxchar = ch; |
| 8267 | } |
| 8268 | |
| 8269 | return maxchar; |
| 8270 | } |
| 8271 | |
| 8272 | PyObject * |
| 8273 | _PyUnicode_TransformDecimalAndSpaceToASCII(PyObject *unicode) |
| 8274 | { |
| 8275 | if (!PyUnicode_Check(unicode)) { |
| 8276 | PyErr_BadInternalCall(); |
| 8277 | return NULL; |
| 8278 | } |
| 8279 | if (PyUnicode_READY(unicode) == -1) |
| 8280 | return NULL; |
| 8281 | if (PyUnicode_MAX_CHAR_VALUE(unicode) <= 127) { |
| 8282 | /* If the string is already ASCII, just return the same string */ |
| 8283 | Py_INCREF(unicode); |
| 8284 | return unicode; |
| 8285 | } |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8286 | return fixup(unicode, fix_decimal_and_space_to_ascii); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8287 | } |
| 8288 | |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8289 | PyObject * |
| 8290 | PyUnicode_TransformDecimalToASCII(Py_UNICODE *s, |
| 8291 | Py_ssize_t length) |
| 8292 | { |
| 8293 | PyObject *result; |
| 8294 | Py_UNICODE *p; /* write pointer into result */ |
| 8295 | Py_ssize_t i; |
| 8296 | /* Copy to a new string */ |
| 8297 | result = (PyObject *)_PyUnicode_New(length); |
| 8298 | Py_UNICODE_COPY(PyUnicode_AS_UNICODE(result), s, length); |
| 8299 | if (result == NULL) |
| 8300 | return result; |
| 8301 | p = PyUnicode_AS_UNICODE(result); |
| 8302 | /* Iterate over code points */ |
| 8303 | for (i = 0; i < length; i++) { |
| 8304 | Py_UNICODE ch =s[i]; |
| 8305 | if (ch > 127) { |
| 8306 | int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8307 | if (decimal >= 0) |
| 8308 | p[i] = '0' + decimal; |
| 8309 | } |
| 8310 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 8311 | #ifndef DONT_MAKE_RESULT_READY |
| 8312 | if (_PyUnicode_READY_REPLACE(&result)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8313 | Py_DECREF(result); |
| 8314 | return NULL; |
| 8315 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 8316 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 8317 | assert(_PyUnicode_CheckConsistency(result, 1)); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8318 | return result; |
| 8319 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8320 | /* --- Decimal Encoder ---------------------------------------------------- */ |
| 8321 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8322 | int |
| 8323 | PyUnicode_EncodeDecimal(Py_UNICODE *s, |
| 8324 | Py_ssize_t length, |
| 8325 | char *output, |
| 8326 | const char *errors) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8327 | { |
| 8328 | Py_UNICODE *p, *end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8329 | PyObject *errorHandler = NULL; |
| 8330 | PyObject *exc = NULL; |
| 8331 | const char *encoding = "decimal"; |
| 8332 | const char *reason = "invalid decimal Unicode string"; |
| 8333 | /* the following variable is used for caching string comparisons |
| 8334 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 8335 | int known_errorHandler = -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8336 | |
| 8337 | if (output == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8338 | PyErr_BadArgument(); |
| 8339 | return -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8340 | } |
| 8341 | |
| 8342 | p = s; |
| 8343 | end = s + length; |
| 8344 | while (p < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8345 | register Py_UNICODE ch = *p; |
| 8346 | int decimal; |
| 8347 | PyObject *repunicode; |
| 8348 | Py_ssize_t repsize; |
| 8349 | Py_ssize_t newpos; |
| 8350 | Py_UNICODE *uni2; |
| 8351 | Py_UNICODE *collstart; |
| 8352 | Py_UNICODE *collend; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8353 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8354 | if (Py_UNICODE_ISSPACE(ch)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8355 | *output++ = ' '; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8356 | ++p; |
| 8357 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8358 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8359 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 8360 | if (decimal >= 0) { |
| 8361 | *output++ = '0' + decimal; |
| 8362 | ++p; |
| 8363 | continue; |
| 8364 | } |
| 8365 | if (0 < ch && ch < 256) { |
| 8366 | *output++ = (char)ch; |
| 8367 | ++p; |
| 8368 | continue; |
| 8369 | } |
| 8370 | /* All other characters are considered unencodable */ |
| 8371 | collstart = p; |
| 8372 | collend = p+1; |
| 8373 | while (collend < end) { |
| 8374 | if ((0 < *collend && *collend < 256) || |
| 8375 | !Py_UNICODE_ISSPACE(*collend) || |
| 8376 | Py_UNICODE_TODECIMAL(*collend)) |
| 8377 | break; |
| 8378 | } |
| 8379 | /* cache callback name lookup |
| 8380 | * (if not done yet, i.e. it's the first error) */ |
| 8381 | if (known_errorHandler==-1) { |
| 8382 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 8383 | known_errorHandler = 1; |
| 8384 | else if (!strcmp(errors, "replace")) |
| 8385 | known_errorHandler = 2; |
| 8386 | else if (!strcmp(errors, "ignore")) |
| 8387 | known_errorHandler = 3; |
| 8388 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 8389 | known_errorHandler = 4; |
| 8390 | else |
| 8391 | known_errorHandler = 0; |
| 8392 | } |
| 8393 | switch (known_errorHandler) { |
| 8394 | case 1: /* strict */ |
| 8395 | raise_encode_exception(&exc, encoding, s, length, collstart-s, collend-s, reason); |
| 8396 | goto onError; |
| 8397 | case 2: /* replace */ |
| 8398 | for (p = collstart; p < collend; ++p) |
| 8399 | *output++ = '?'; |
| 8400 | /* fall through */ |
| 8401 | case 3: /* ignore */ |
| 8402 | p = collend; |
| 8403 | break; |
| 8404 | case 4: /* xmlcharrefreplace */ |
| 8405 | /* generate replacement (temporarily (mis)uses p) */ |
| 8406 | for (p = collstart; p < collend; ++p) |
| 8407 | output += sprintf(output, "&#%d;", (int)*p); |
| 8408 | p = collend; |
| 8409 | break; |
| 8410 | default: |
| 8411 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 8412 | encoding, reason, s, length, &exc, |
| 8413 | collstart-s, collend-s, &newpos); |
| 8414 | if (repunicode == NULL) |
| 8415 | goto onError; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 8416 | if (!PyUnicode_Check(repunicode)) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8417 | /* Byte results not supported, since they have no decimal property. */ |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 8418 | PyErr_SetString(PyExc_TypeError, "error handler should return unicode"); |
| 8419 | Py_DECREF(repunicode); |
| 8420 | goto onError; |
| 8421 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8422 | /* generate replacement */ |
| 8423 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 8424 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
| 8425 | Py_UNICODE ch = *uni2; |
| 8426 | if (Py_UNICODE_ISSPACE(ch)) |
| 8427 | *output++ = ' '; |
| 8428 | else { |
| 8429 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 8430 | if (decimal >= 0) |
| 8431 | *output++ = '0' + decimal; |
| 8432 | else if (0 < ch && ch < 256) |
| 8433 | *output++ = (char)ch; |
| 8434 | else { |
| 8435 | Py_DECREF(repunicode); |
| 8436 | raise_encode_exception(&exc, encoding, |
| 8437 | s, length, collstart-s, collend-s, reason); |
| 8438 | goto onError; |
| 8439 | } |
| 8440 | } |
| 8441 | } |
| 8442 | p = s + newpos; |
| 8443 | Py_DECREF(repunicode); |
| 8444 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8445 | } |
| 8446 | /* 0-terminate the output string */ |
| 8447 | *output++ = '\0'; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8448 | Py_XDECREF(exc); |
| 8449 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8450 | return 0; |
| 8451 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8452 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8453 | Py_XDECREF(exc); |
| 8454 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8455 | return -1; |
| 8456 | } |
| 8457 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8458 | /* --- Helpers ------------------------------------------------------------ */ |
| 8459 | |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 8460 | #include "stringlib/asciilib.h" |
| 8461 | #include "stringlib/fastsearch.h" |
| 8462 | #include "stringlib/partition.h" |
| 8463 | #include "stringlib/split.h" |
| 8464 | #include "stringlib/count.h" |
| 8465 | #include "stringlib/find.h" |
| 8466 | #include "stringlib/localeutil.h" |
| 8467 | #include "stringlib/undef.h" |
| 8468 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8469 | #include "stringlib/ucs1lib.h" |
| 8470 | #include "stringlib/fastsearch.h" |
| 8471 | #include "stringlib/partition.h" |
| 8472 | #include "stringlib/split.h" |
| 8473 | #include "stringlib/count.h" |
| 8474 | #include "stringlib/find.h" |
| 8475 | #include "stringlib/localeutil.h" |
| 8476 | #include "stringlib/undef.h" |
| 8477 | |
| 8478 | #include "stringlib/ucs2lib.h" |
| 8479 | #include "stringlib/fastsearch.h" |
| 8480 | #include "stringlib/partition.h" |
| 8481 | #include "stringlib/split.h" |
| 8482 | #include "stringlib/count.h" |
| 8483 | #include "stringlib/find.h" |
| 8484 | #include "stringlib/localeutil.h" |
| 8485 | #include "stringlib/undef.h" |
| 8486 | |
| 8487 | #include "stringlib/ucs4lib.h" |
| 8488 | #include "stringlib/fastsearch.h" |
| 8489 | #include "stringlib/partition.h" |
| 8490 | #include "stringlib/split.h" |
| 8491 | #include "stringlib/count.h" |
| 8492 | #include "stringlib/find.h" |
| 8493 | #include "stringlib/localeutil.h" |
| 8494 | #include "stringlib/undef.h" |
| 8495 | |
| 8496 | static Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 8497 | any_find_slice(Py_ssize_t Py_LOCAL_CALLBACK(ascii)(const Py_UCS1*, Py_ssize_t, |
| 8498 | const Py_UCS1*, Py_ssize_t, |
| 8499 | Py_ssize_t, Py_ssize_t), |
| 8500 | 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] | 8501 | const Py_UCS1*, Py_ssize_t, |
| 8502 | Py_ssize_t, Py_ssize_t), |
| 8503 | Py_ssize_t Py_LOCAL_CALLBACK(ucs2)(const Py_UCS2*, Py_ssize_t, |
| 8504 | const Py_UCS2*, Py_ssize_t, |
| 8505 | Py_ssize_t, Py_ssize_t), |
| 8506 | Py_ssize_t Py_LOCAL_CALLBACK(ucs4)(const Py_UCS4*, Py_ssize_t, |
| 8507 | const Py_UCS4*, Py_ssize_t, |
| 8508 | Py_ssize_t, Py_ssize_t), |
| 8509 | PyObject* s1, PyObject* s2, |
| 8510 | Py_ssize_t start, |
| 8511 | Py_ssize_t end) |
| 8512 | { |
| 8513 | int kind1, kind2, kind; |
| 8514 | void *buf1, *buf2; |
| 8515 | Py_ssize_t len1, len2, result; |
| 8516 | |
| 8517 | kind1 = PyUnicode_KIND(s1); |
| 8518 | kind2 = PyUnicode_KIND(s2); |
| 8519 | kind = kind1 > kind2 ? kind1 : kind2; |
| 8520 | buf1 = PyUnicode_DATA(s1); |
| 8521 | buf2 = PyUnicode_DATA(s2); |
| 8522 | if (kind1 != kind) |
| 8523 | buf1 = _PyUnicode_AsKind(s1, kind); |
| 8524 | if (!buf1) |
| 8525 | return -2; |
| 8526 | if (kind2 != kind) |
| 8527 | buf2 = _PyUnicode_AsKind(s2, kind); |
| 8528 | if (!buf2) { |
| 8529 | if (kind1 != kind) PyMem_Free(buf1); |
| 8530 | return -2; |
| 8531 | } |
| 8532 | len1 = PyUnicode_GET_LENGTH(s1); |
| 8533 | len2 = PyUnicode_GET_LENGTH(s2); |
| 8534 | |
| 8535 | switch(kind) { |
| 8536 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 8537 | if (PyUnicode_IS_ASCII(s1) && PyUnicode_IS_ASCII(s2)) |
| 8538 | result = ascii(buf1, len1, buf2, len2, start, end); |
| 8539 | else |
| 8540 | result = ucs1(buf1, len1, buf2, len2, start, end); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8541 | break; |
| 8542 | case PyUnicode_2BYTE_KIND: |
| 8543 | result = ucs2(buf1, len1, buf2, len2, start, end); |
| 8544 | break; |
| 8545 | case PyUnicode_4BYTE_KIND: |
| 8546 | result = ucs4(buf1, len1, buf2, len2, start, end); |
| 8547 | break; |
| 8548 | default: |
| 8549 | assert(0); result = -2; |
| 8550 | } |
| 8551 | |
| 8552 | if (kind1 != kind) |
| 8553 | PyMem_Free(buf1); |
| 8554 | if (kind2 != kind) |
| 8555 | PyMem_Free(buf2); |
| 8556 | |
| 8557 | return result; |
| 8558 | } |
| 8559 | |
| 8560 | Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 8561 | _PyUnicode_InsertThousandsGrouping(PyObject *unicode, int kind, void *data, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8562 | Py_ssize_t n_buffer, |
| 8563 | void *digits, Py_ssize_t n_digits, |
| 8564 | Py_ssize_t min_width, |
| 8565 | const char *grouping, |
| 8566 | const char *thousands_sep) |
| 8567 | { |
| 8568 | switch(kind) { |
| 8569 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 8570 | if (unicode != NULL && PyUnicode_IS_ASCII(unicode)) |
| 8571 | return _PyUnicode_ascii_InsertThousandsGrouping( |
| 8572 | (Py_UCS1*)data, n_buffer, (Py_UCS1*)digits, n_digits, |
| 8573 | min_width, grouping, thousands_sep); |
| 8574 | else |
| 8575 | return _PyUnicode_ucs1_InsertThousandsGrouping( |
| 8576 | (Py_UCS1*)data, n_buffer, (Py_UCS1*)digits, n_digits, |
| 8577 | min_width, grouping, thousands_sep); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8578 | case PyUnicode_2BYTE_KIND: |
| 8579 | return _PyUnicode_ucs2_InsertThousandsGrouping( |
| 8580 | (Py_UCS2*)data, n_buffer, (Py_UCS2*)digits, n_digits, |
| 8581 | min_width, grouping, thousands_sep); |
| 8582 | case PyUnicode_4BYTE_KIND: |
| 8583 | return _PyUnicode_ucs4_InsertThousandsGrouping( |
| 8584 | (Py_UCS4*)data, n_buffer, (Py_UCS4*)digits, n_digits, |
| 8585 | min_width, grouping, thousands_sep); |
| 8586 | } |
| 8587 | assert(0); |
| 8588 | return -1; |
| 8589 | } |
| 8590 | |
| 8591 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 8592 | #include "stringlib/unicodedefs.h" |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8593 | #include "stringlib/fastsearch.h" |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 8594 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8595 | #include "stringlib/count.h" |
| 8596 | #include "stringlib/find.h" |
Eric Smith | 5807c41 | 2008-05-11 21:00:57 +0000 | [diff] [blame] | 8597 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8598 | /* helper macro to fixup start/end slice values */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 8599 | #define ADJUST_INDICES(start, end, len) \ |
| 8600 | if (end > len) \ |
| 8601 | end = len; \ |
| 8602 | else if (end < 0) { \ |
| 8603 | end += len; \ |
| 8604 | if (end < 0) \ |
| 8605 | end = 0; \ |
| 8606 | } \ |
| 8607 | if (start < 0) { \ |
| 8608 | start += len; \ |
| 8609 | if (start < 0) \ |
| 8610 | start = 0; \ |
| 8611 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8612 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8613 | Py_ssize_t |
| 8614 | PyUnicode_Count(PyObject *str, |
| 8615 | PyObject *substr, |
| 8616 | Py_ssize_t start, |
| 8617 | Py_ssize_t end) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8618 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8619 | Py_ssize_t result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8620 | PyUnicodeObject* str_obj; |
| 8621 | PyUnicodeObject* sub_obj; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8622 | int kind1, kind2, kind; |
| 8623 | void *buf1 = NULL, *buf2 = NULL; |
| 8624 | Py_ssize_t len1, len2; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8625 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8626 | str_obj = (PyUnicodeObject*) PyUnicode_FromObject(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8627 | if (!str_obj || PyUnicode_READY(str_obj) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8628 | return -1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8629 | sub_obj = (PyUnicodeObject*) PyUnicode_FromObject(substr); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 8630 | if (!sub_obj || PyUnicode_READY(sub_obj) == -1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8631 | Py_DECREF(str_obj); |
| 8632 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8633 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8634 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8635 | kind1 = PyUnicode_KIND(str_obj); |
| 8636 | kind2 = PyUnicode_KIND(sub_obj); |
| 8637 | kind = kind1 > kind2 ? kind1 : kind2; |
| 8638 | buf1 = PyUnicode_DATA(str_obj); |
| 8639 | if (kind1 != kind) |
| 8640 | buf1 = _PyUnicode_AsKind((PyObject*)str_obj, kind); |
| 8641 | if (!buf1) |
| 8642 | goto onError; |
| 8643 | buf2 = PyUnicode_DATA(sub_obj); |
| 8644 | if (kind2 != kind) |
| 8645 | buf2 = _PyUnicode_AsKind((PyObject*)sub_obj, kind); |
| 8646 | if (!buf2) |
| 8647 | goto onError; |
| 8648 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 8649 | len2 = PyUnicode_GET_LENGTH(sub_obj); |
| 8650 | |
| 8651 | ADJUST_INDICES(start, end, len1); |
| 8652 | switch(kind) { |
| 8653 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 8654 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sub_obj)) |
| 8655 | result = asciilib_count( |
| 8656 | ((Py_UCS1*)buf1) + start, end - start, |
| 8657 | buf2, len2, PY_SSIZE_T_MAX |
| 8658 | ); |
| 8659 | else |
| 8660 | result = ucs1lib_count( |
| 8661 | ((Py_UCS1*)buf1) + start, end - start, |
| 8662 | buf2, len2, PY_SSIZE_T_MAX |
| 8663 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8664 | break; |
| 8665 | case PyUnicode_2BYTE_KIND: |
| 8666 | result = ucs2lib_count( |
| 8667 | ((Py_UCS2*)buf1) + start, end - start, |
| 8668 | buf2, len2, PY_SSIZE_T_MAX |
| 8669 | ); |
| 8670 | break; |
| 8671 | case PyUnicode_4BYTE_KIND: |
| 8672 | result = ucs4lib_count( |
| 8673 | ((Py_UCS4*)buf1) + start, end - start, |
| 8674 | buf2, len2, PY_SSIZE_T_MAX |
| 8675 | ); |
| 8676 | break; |
| 8677 | default: |
| 8678 | assert(0); result = 0; |
| 8679 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8680 | |
| 8681 | Py_DECREF(sub_obj); |
| 8682 | Py_DECREF(str_obj); |
| 8683 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8684 | if (kind1 != kind) |
| 8685 | PyMem_Free(buf1); |
| 8686 | if (kind2 != kind) |
| 8687 | PyMem_Free(buf2); |
| 8688 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8689 | return result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8690 | onError: |
| 8691 | Py_DECREF(sub_obj); |
| 8692 | Py_DECREF(str_obj); |
| 8693 | if (kind1 != kind && buf1) |
| 8694 | PyMem_Free(buf1); |
| 8695 | if (kind2 != kind && buf2) |
| 8696 | PyMem_Free(buf2); |
| 8697 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8698 | } |
| 8699 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8700 | Py_ssize_t |
| 8701 | PyUnicode_Find(PyObject *str, |
| 8702 | PyObject *sub, |
| 8703 | Py_ssize_t start, |
| 8704 | Py_ssize_t end, |
| 8705 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8706 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8707 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8708 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8709 | str = PyUnicode_FromObject(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8710 | if (!str || PyUnicode_READY(str) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8711 | return -2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8712 | sub = PyUnicode_FromObject(sub); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8713 | if (!sub || PyUnicode_READY(sub) == -1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8714 | Py_DECREF(str); |
| 8715 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8716 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8717 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8718 | if (direction > 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8719 | result = any_find_slice( |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 8720 | asciilib_find_slice, ucs1lib_find_slice, |
| 8721 | ucs2lib_find_slice, ucs4lib_find_slice, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8722 | str, sub, start, end |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8723 | ); |
| 8724 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8725 | result = any_find_slice( |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 8726 | asciilib_find_slice, ucs1lib_rfind_slice, |
| 8727 | ucs2lib_rfind_slice, ucs4lib_rfind_slice, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8728 | str, sub, start, end |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8729 | ); |
| 8730 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8731 | Py_DECREF(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8732 | Py_DECREF(sub); |
| 8733 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8734 | return result; |
| 8735 | } |
| 8736 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8737 | Py_ssize_t |
| 8738 | PyUnicode_FindChar(PyObject *str, Py_UCS4 ch, |
| 8739 | Py_ssize_t start, Py_ssize_t end, |
| 8740 | int direction) |
| 8741 | { |
| 8742 | char *result; |
| 8743 | int kind; |
| 8744 | if (PyUnicode_READY(str) == -1) |
| 8745 | return -2; |
Victor Stinner | 267aa24 | 2011-10-02 01:08:37 +0200 | [diff] [blame] | 8746 | if (start < 0 || end < 0) { |
| 8747 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 8748 | return -2; |
| 8749 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8750 | if (end > PyUnicode_GET_LENGTH(str)) |
| 8751 | end = PyUnicode_GET_LENGTH(str); |
| 8752 | kind = PyUnicode_KIND(str); |
| 8753 | result = findchar(PyUnicode_1BYTE_DATA(str) |
| 8754 | + PyUnicode_KIND_SIZE(kind, start), |
| 8755 | kind, |
| 8756 | end-start, ch, direction); |
| 8757 | if (!result) |
| 8758 | return -1; |
| 8759 | return (result-(char*)PyUnicode_DATA(str)) >> (kind-1); |
| 8760 | } |
| 8761 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8762 | static int |
| 8763 | tailmatch(PyUnicodeObject *self, |
| 8764 | PyUnicodeObject *substring, |
| 8765 | Py_ssize_t start, |
| 8766 | Py_ssize_t end, |
| 8767 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8768 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8769 | int kind_self; |
| 8770 | int kind_sub; |
| 8771 | void *data_self; |
| 8772 | void *data_sub; |
| 8773 | Py_ssize_t offset; |
| 8774 | Py_ssize_t i; |
| 8775 | Py_ssize_t end_sub; |
| 8776 | |
| 8777 | if (PyUnicode_READY(self) == -1 || |
| 8778 | PyUnicode_READY(substring) == -1) |
| 8779 | return 0; |
| 8780 | |
| 8781 | if (PyUnicode_GET_LENGTH(substring) == 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8782 | return 1; |
| 8783 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8784 | ADJUST_INDICES(start, end, PyUnicode_GET_LENGTH(self)); |
| 8785 | end -= PyUnicode_GET_LENGTH(substring); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8786 | if (end < start) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8787 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8788 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8789 | kind_self = PyUnicode_KIND(self); |
| 8790 | data_self = PyUnicode_DATA(self); |
| 8791 | kind_sub = PyUnicode_KIND(substring); |
| 8792 | data_sub = PyUnicode_DATA(substring); |
| 8793 | end_sub = PyUnicode_GET_LENGTH(substring) - 1; |
| 8794 | |
| 8795 | if (direction > 0) |
| 8796 | offset = end; |
| 8797 | else |
| 8798 | offset = start; |
| 8799 | |
| 8800 | if (PyUnicode_READ(kind_self, data_self, offset) == |
| 8801 | PyUnicode_READ(kind_sub, data_sub, 0) && |
| 8802 | PyUnicode_READ(kind_self, data_self, offset + end_sub) == |
| 8803 | PyUnicode_READ(kind_sub, data_sub, end_sub)) { |
| 8804 | /* If both are of the same kind, memcmp is sufficient */ |
| 8805 | if (kind_self == kind_sub) { |
| 8806 | return ! memcmp((char *)data_self + |
| 8807 | (offset * PyUnicode_CHARACTER_SIZE(substring)), |
| 8808 | data_sub, |
| 8809 | PyUnicode_GET_LENGTH(substring) * |
| 8810 | PyUnicode_CHARACTER_SIZE(substring)); |
| 8811 | } |
| 8812 | /* otherwise we have to compare each character by first accesing it */ |
| 8813 | else { |
| 8814 | /* We do not need to compare 0 and len(substring)-1 because |
| 8815 | the if statement above ensured already that they are equal |
| 8816 | when we end up here. */ |
| 8817 | // TODO: honor direction and do a forward or backwards search |
| 8818 | for (i = 1; i < end_sub; ++i) { |
| 8819 | if (PyUnicode_READ(kind_self, data_self, offset + i) != |
| 8820 | PyUnicode_READ(kind_sub, data_sub, i)) |
| 8821 | return 0; |
| 8822 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8823 | return 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8824 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8825 | } |
| 8826 | |
| 8827 | return 0; |
| 8828 | } |
| 8829 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8830 | Py_ssize_t |
| 8831 | PyUnicode_Tailmatch(PyObject *str, |
| 8832 | PyObject *substr, |
| 8833 | Py_ssize_t start, |
| 8834 | Py_ssize_t end, |
| 8835 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8836 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8837 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8838 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8839 | str = PyUnicode_FromObject(str); |
| 8840 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8841 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8842 | substr = PyUnicode_FromObject(substr); |
| 8843 | if (substr == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8844 | Py_DECREF(str); |
| 8845 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8846 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8847 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8848 | result = tailmatch((PyUnicodeObject *)str, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8849 | (PyUnicodeObject *)substr, |
| 8850 | start, end, direction); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8851 | Py_DECREF(str); |
| 8852 | Py_DECREF(substr); |
| 8853 | return result; |
| 8854 | } |
| 8855 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8856 | /* Apply fixfct filter to the Unicode object self and return a |
| 8857 | reference to the modified object */ |
| 8858 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8859 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8860 | fixup(PyObject *self, |
| 8861 | Py_UCS4 (*fixfct)(PyObject *s)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8862 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8863 | PyObject *u; |
| 8864 | Py_UCS4 maxchar_old, maxchar_new = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8865 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8866 | if (PyUnicode_READY(self) == -1) |
| 8867 | return NULL; |
| 8868 | maxchar_old = PyUnicode_MAX_CHAR_VALUE(self); |
| 8869 | u = PyUnicode_New(PyUnicode_GET_LENGTH(self), |
| 8870 | maxchar_old); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8871 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8872 | return NULL; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8873 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8874 | Py_MEMCPY(PyUnicode_1BYTE_DATA(u), PyUnicode_1BYTE_DATA(self), |
| 8875 | PyUnicode_GET_LENGTH(u) * PyUnicode_CHARACTER_SIZE(u)); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8876 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8877 | /* fix functions return the new maximum character in a string, |
| 8878 | if the kind of the resulting unicode object does not change, |
| 8879 | everything is fine. Otherwise we need to change the string kind |
| 8880 | and re-run the fix function. */ |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8881 | maxchar_new = fixfct(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8882 | if (maxchar_new == 0) |
| 8883 | /* do nothing, keep maxchar_new at 0 which means no changes. */; |
| 8884 | else if (maxchar_new <= 127) |
| 8885 | maxchar_new = 127; |
| 8886 | else if (maxchar_new <= 255) |
| 8887 | maxchar_new = 255; |
| 8888 | else if (maxchar_new <= 65535) |
| 8889 | maxchar_new = 65535; |
| 8890 | else |
| 8891 | maxchar_new = 1114111; /* 0x10ffff */ |
| 8892 | |
| 8893 | if (!maxchar_new && PyUnicode_CheckExact(self)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8894 | /* fixfct should return TRUE if it modified the buffer. If |
| 8895 | FALSE, return a reference to the original buffer instead |
| 8896 | (to save space, not time) */ |
| 8897 | Py_INCREF(self); |
| 8898 | Py_DECREF(u); |
| 8899 | return (PyObject*) self; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8900 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8901 | else if (maxchar_new == maxchar_old) { |
| 8902 | return u; |
| 8903 | } |
| 8904 | else { |
| 8905 | /* In case the maximum character changed, we need to |
| 8906 | convert the string to the new category. */ |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 8907 | PyObject *v = PyUnicode_New(PyUnicode_GET_LENGTH(self), maxchar_new); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8908 | if (v == NULL) { |
| 8909 | Py_DECREF(u); |
| 8910 | return NULL; |
| 8911 | } |
| 8912 | if (maxchar_new > maxchar_old) { |
| 8913 | /* If the maxchar increased so that the kind changed, not all |
| 8914 | characters are representable anymore and we need to fix the |
| 8915 | string again. This only happens in very few cases. */ |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 8916 | copy_characters(v, 0, self, 0, PyUnicode_GET_LENGTH(self)); |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8917 | maxchar_old = fixfct(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8918 | assert(maxchar_old > 0 && maxchar_old <= maxchar_new); |
| 8919 | } |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 8920 | else { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 8921 | copy_characters(v, 0, u, 0, PyUnicode_GET_LENGTH(self)); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 8922 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8923 | |
| 8924 | Py_DECREF(u); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 8925 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8926 | return v; |
| 8927 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8928 | } |
| 8929 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8930 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8931 | fixupper(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8932 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8933 | /* No need to call PyUnicode_READY(self) because this function is only |
| 8934 | called as a callback from fixup() which does it already. */ |
| 8935 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 8936 | const int kind = PyUnicode_KIND(self); |
| 8937 | void *data = PyUnicode_DATA(self); |
| 8938 | int touched = 0; |
| 8939 | Py_UCS4 maxchar = 0; |
| 8940 | Py_ssize_t i; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8941 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8942 | for (i = 0; i < len; ++i) { |
| 8943 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 8944 | const Py_UCS4 up = Py_UNICODE_TOUPPER(ch); |
| 8945 | if (up != ch) { |
| 8946 | if (up > maxchar) |
| 8947 | maxchar = up; |
| 8948 | PyUnicode_WRITE(kind, data, i, up); |
| 8949 | touched = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8950 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8951 | else if (ch > maxchar) |
| 8952 | maxchar = ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8953 | } |
| 8954 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8955 | if (touched) |
| 8956 | return maxchar; |
| 8957 | else |
| 8958 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8959 | } |
| 8960 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8961 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8962 | fixlower(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8963 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8964 | /* No need to call PyUnicode_READY(self) because fixup() which does it. */ |
| 8965 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 8966 | const int kind = PyUnicode_KIND(self); |
| 8967 | void *data = PyUnicode_DATA(self); |
| 8968 | int touched = 0; |
| 8969 | Py_UCS4 maxchar = 0; |
| 8970 | Py_ssize_t i; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8971 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8972 | for(i = 0; i < len; ++i) { |
| 8973 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 8974 | const Py_UCS4 lo = Py_UNICODE_TOLOWER(ch); |
| 8975 | if (lo != ch) { |
| 8976 | if (lo > maxchar) |
| 8977 | maxchar = lo; |
| 8978 | PyUnicode_WRITE(kind, data, i, lo); |
| 8979 | touched = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8980 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8981 | else if (ch > maxchar) |
| 8982 | maxchar = ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8983 | } |
| 8984 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8985 | if (touched) |
| 8986 | return maxchar; |
| 8987 | else |
| 8988 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8989 | } |
| 8990 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8991 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8992 | fixswapcase(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8993 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8994 | /* No need to call PyUnicode_READY(self) because fixup() which does it. */ |
| 8995 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 8996 | const int kind = PyUnicode_KIND(self); |
| 8997 | void *data = PyUnicode_DATA(self); |
| 8998 | int touched = 0; |
| 8999 | Py_UCS4 maxchar = 0; |
| 9000 | Py_ssize_t i; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9001 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9002 | for(i = 0; i < len; ++i) { |
| 9003 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 9004 | Py_UCS4 nu = 0; |
| 9005 | |
| 9006 | if (Py_UNICODE_ISUPPER(ch)) |
| 9007 | nu = Py_UNICODE_TOLOWER(ch); |
| 9008 | else if (Py_UNICODE_ISLOWER(ch)) |
| 9009 | nu = Py_UNICODE_TOUPPER(ch); |
| 9010 | |
| 9011 | if (nu != 0) { |
| 9012 | if (nu > maxchar) |
| 9013 | maxchar = nu; |
| 9014 | PyUnicode_WRITE(kind, data, i, nu); |
| 9015 | touched = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9016 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9017 | else if (ch > maxchar) |
| 9018 | maxchar = ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9019 | } |
| 9020 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9021 | if (touched) |
| 9022 | return maxchar; |
| 9023 | else |
| 9024 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9025 | } |
| 9026 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9027 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9028 | fixcapitalize(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9029 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9030 | /* No need to call PyUnicode_READY(self) because fixup() which does it. */ |
| 9031 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 9032 | const int kind = PyUnicode_KIND(self); |
| 9033 | void *data = PyUnicode_DATA(self); |
| 9034 | int touched = 0; |
| 9035 | Py_UCS4 maxchar = 0; |
| 9036 | Py_ssize_t i = 0; |
| 9037 | Py_UCS4 ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9038 | |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9039 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9040 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9041 | |
| 9042 | ch = PyUnicode_READ(kind, data, i); |
| 9043 | if (!Py_UNICODE_ISUPPER(ch)) { |
| 9044 | maxchar = Py_UNICODE_TOUPPER(ch); |
| 9045 | PyUnicode_WRITE(kind, data, i, maxchar); |
| 9046 | touched = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9047 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9048 | ++i; |
| 9049 | for(; i < len; ++i) { |
| 9050 | ch = PyUnicode_READ(kind, data, i); |
| 9051 | if (!Py_UNICODE_ISLOWER(ch)) { |
| 9052 | const Py_UCS4 lo = Py_UNICODE_TOLOWER(ch); |
| 9053 | if (lo > maxchar) |
| 9054 | maxchar = lo; |
| 9055 | PyUnicode_WRITE(kind, data, i, lo); |
| 9056 | touched = 1; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9057 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9058 | else if (ch > maxchar) |
| 9059 | maxchar = ch; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9060 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9061 | |
| 9062 | if (touched) |
| 9063 | return maxchar; |
| 9064 | else |
| 9065 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9066 | } |
| 9067 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9068 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9069 | fixtitle(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9070 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9071 | /* No need to call PyUnicode_READY(self) because fixup() which does it. */ |
| 9072 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 9073 | const int kind = PyUnicode_KIND(self); |
| 9074 | void *data = PyUnicode_DATA(self); |
| 9075 | Py_UCS4 maxchar = 0; |
| 9076 | Py_ssize_t i = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9077 | int previous_is_cased; |
| 9078 | |
| 9079 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9080 | if (len == 1) { |
| 9081 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 9082 | const Py_UCS4 ti = Py_UNICODE_TOTITLE(ch); |
| 9083 | if (ti != ch) { |
| 9084 | PyUnicode_WRITE(kind, data, i, ti); |
| 9085 | return ti; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9086 | } |
| 9087 | else |
| 9088 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9089 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9090 | previous_is_cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9091 | for(; i < len; ++i) { |
| 9092 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 9093 | Py_UCS4 nu; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9094 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9095 | if (previous_is_cased) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9096 | nu = Py_UNICODE_TOLOWER(ch); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9097 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9098 | nu = Py_UNICODE_TOTITLE(ch); |
| 9099 | |
| 9100 | if (nu > maxchar) |
| 9101 | maxchar = nu; |
| 9102 | PyUnicode_WRITE(kind, data, i, nu); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9103 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9104 | if (Py_UNICODE_ISLOWER(ch) || |
| 9105 | Py_UNICODE_ISUPPER(ch) || |
| 9106 | Py_UNICODE_ISTITLE(ch)) |
| 9107 | previous_is_cased = 1; |
| 9108 | else |
| 9109 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9110 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9111 | return maxchar; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9112 | } |
| 9113 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9114 | PyObject * |
| 9115 | PyUnicode_Join(PyObject *separator, PyObject *seq) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9116 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9117 | PyObject *sep = NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9118 | Py_ssize_t seplen = 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9119 | PyObject *res = NULL; /* the result */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9120 | PyObject *fseq; /* PySequence_Fast(seq) */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9121 | Py_ssize_t seqlen; /* len(fseq) -- number of items in sequence */ |
| 9122 | PyObject **items; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9123 | PyObject *item; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9124 | Py_ssize_t sz, i, res_offset; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9125 | Py_UCS4 maxchar; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9126 | Py_UCS4 item_maxchar; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9127 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9128 | fseq = PySequence_Fast(seq, ""); |
| 9129 | if (fseq == NULL) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9130 | return NULL; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9131 | } |
| 9132 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9133 | /* NOTE: the following code can't call back into Python code, |
| 9134 | * so we are sure that fseq won't be mutated. |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 9135 | */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9136 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9137 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 9138 | /* If empty sequence, return u"". */ |
| 9139 | if (seqlen == 0) { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9140 | Py_DECREF(fseq); |
| 9141 | Py_INCREF(unicode_empty); |
| 9142 | res = unicode_empty; |
| 9143 | return res; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9144 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9145 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9146 | /* If singleton sequence with an exact Unicode, return that. */ |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9147 | items = PySequence_Fast_ITEMS(fseq); |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame^] | 9148 | if (seqlen == 1) { |
| 9149 | if (PyUnicode_CheckExact(items[0])) { |
| 9150 | res = items[0]; |
| 9151 | Py_INCREF(res); |
| 9152 | Py_DECREF(fseq); |
| 9153 | return res; |
| 9154 | } |
| 9155 | sep = NULL; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9156 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9157 | else { |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame^] | 9158 | /* Set up sep and seplen */ |
| 9159 | if (separator == NULL) { |
| 9160 | /* fall back to a blank space separator */ |
| 9161 | sep = PyUnicode_FromOrdinal(' '); |
| 9162 | if (!sep) |
| 9163 | goto onError; |
| 9164 | maxchar = 32; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9165 | } |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame^] | 9166 | else { |
| 9167 | if (!PyUnicode_Check(separator)) { |
| 9168 | PyErr_Format(PyExc_TypeError, |
| 9169 | "separator: expected str instance," |
| 9170 | " %.80s found", |
| 9171 | Py_TYPE(separator)->tp_name); |
| 9172 | goto onError; |
| 9173 | } |
| 9174 | if (PyUnicode_READY(separator)) |
| 9175 | goto onError; |
| 9176 | sep = separator; |
| 9177 | seplen = PyUnicode_GET_LENGTH(separator); |
| 9178 | maxchar = PyUnicode_MAX_CHAR_VALUE(separator); |
| 9179 | /* inc refcount to keep this code path symmetric with the |
| 9180 | above case of a blank separator */ |
| 9181 | Py_INCREF(sep); |
| 9182 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9183 | } |
| 9184 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9185 | /* There are at least two things to join, or else we have a subclass |
| 9186 | * of str in the sequence. |
| 9187 | * Do a pre-pass to figure out the total amount of space we'll |
| 9188 | * need (sz), and see whether all argument are strings. |
| 9189 | */ |
| 9190 | sz = 0; |
| 9191 | for (i = 0; i < seqlen; i++) { |
| 9192 | const Py_ssize_t old_sz = sz; |
| 9193 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9194 | if (!PyUnicode_Check(item)) { |
| 9195 | PyErr_Format(PyExc_TypeError, |
| 9196 | "sequence item %zd: expected str instance," |
| 9197 | " %.80s found", |
| 9198 | i, Py_TYPE(item)->tp_name); |
| 9199 | goto onError; |
| 9200 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9201 | if (PyUnicode_READY(item) == -1) |
| 9202 | goto onError; |
| 9203 | sz += PyUnicode_GET_LENGTH(item); |
| 9204 | item_maxchar = PyUnicode_MAX_CHAR_VALUE(item); |
| 9205 | if (item_maxchar > maxchar) |
| 9206 | maxchar = item_maxchar; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9207 | if (i != 0) |
| 9208 | sz += seplen; |
| 9209 | if (sz < old_sz || sz > PY_SSIZE_T_MAX) { |
| 9210 | PyErr_SetString(PyExc_OverflowError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9211 | "join() result is too long for a Python string"); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9212 | goto onError; |
| 9213 | } |
| 9214 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9215 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9216 | res = PyUnicode_New(sz, maxchar); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9217 | if (res == NULL) |
| 9218 | goto onError; |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 9219 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9220 | /* Catenate everything. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9221 | for (i = 0, res_offset = 0; i < seqlen; ++i) { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9222 | Py_ssize_t itemlen; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9223 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9224 | /* Copy item, and maybe the separator. */ |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 9225 | if (i && seplen != 0) { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9226 | copy_characters(res, res_offset, sep, 0, seplen); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9227 | res_offset += seplen; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9228 | } |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 9229 | itemlen = PyUnicode_GET_LENGTH(item); |
| 9230 | if (itemlen != 0) { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9231 | copy_characters(res, res_offset, item, 0, itemlen); |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 9232 | res_offset += itemlen; |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 9233 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9234 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9235 | assert(res_offset == PyUnicode_GET_LENGTH(res)); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9236 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9237 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9238 | Py_XDECREF(sep); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 9239 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9240 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9241 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9242 | onError: |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9243 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9244 | Py_XDECREF(sep); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9245 | Py_XDECREF(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9246 | return NULL; |
| 9247 | } |
| 9248 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9249 | #define FILL(kind, data, value, start, length) \ |
| 9250 | do { \ |
| 9251 | Py_ssize_t i_ = 0; \ |
| 9252 | assert(kind != PyUnicode_WCHAR_KIND); \ |
| 9253 | switch ((kind)) { \ |
| 9254 | case PyUnicode_1BYTE_KIND: { \ |
| 9255 | unsigned char * to_ = (unsigned char *)((data)) + (start); \ |
| 9256 | memset(to_, (unsigned char)value, length); \ |
| 9257 | break; \ |
| 9258 | } \ |
| 9259 | case PyUnicode_2BYTE_KIND: { \ |
| 9260 | Py_UCS2 * to_ = (Py_UCS2 *)((data)) + (start); \ |
| 9261 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 9262 | break; \ |
| 9263 | } \ |
| 9264 | default: { \ |
| 9265 | Py_UCS4 * to_ = (Py_UCS4 *)((data)) + (start); \ |
| 9266 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 9267 | break; \ |
| 9268 | } \ |
| 9269 | } \ |
| 9270 | } while (0) |
| 9271 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9272 | static PyObject * |
| 9273 | pad(PyObject *self, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9274 | Py_ssize_t left, |
| 9275 | Py_ssize_t right, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9276 | Py_UCS4 fill) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9277 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9278 | PyObject *u; |
| 9279 | Py_UCS4 maxchar; |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9280 | int kind; |
| 9281 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9282 | |
| 9283 | if (left < 0) |
| 9284 | left = 0; |
| 9285 | if (right < 0) |
| 9286 | right = 0; |
| 9287 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 9288 | if (left == 0 && right == 0 && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9289 | Py_INCREF(self); |
| 9290 | return self; |
| 9291 | } |
| 9292 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9293 | if (left > PY_SSIZE_T_MAX - _PyUnicode_LENGTH(self) || |
| 9294 | right > PY_SSIZE_T_MAX - (left + _PyUnicode_LENGTH(self))) { |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 9295 | PyErr_SetString(PyExc_OverflowError, "padded string is too long"); |
| 9296 | return NULL; |
| 9297 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9298 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
| 9299 | if (fill > maxchar) |
| 9300 | maxchar = fill; |
| 9301 | u = PyUnicode_New(left + _PyUnicode_LENGTH(self) + right, maxchar); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9302 | if (!u) |
| 9303 | return NULL; |
| 9304 | |
| 9305 | kind = PyUnicode_KIND(u); |
| 9306 | data = PyUnicode_DATA(u); |
| 9307 | if (left) |
| 9308 | FILL(kind, data, fill, 0, left); |
| 9309 | if (right) |
| 9310 | FILL(kind, data, fill, left + _PyUnicode_LENGTH(self), right); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9311 | copy_characters(u, left, self, 0, _PyUnicode_LENGTH(self)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 9312 | assert(_PyUnicode_CheckConsistency(u, 1)); |
| 9313 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9314 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9315 | #undef FILL |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9316 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9317 | PyObject * |
| 9318 | PyUnicode_Splitlines(PyObject *string, int keepends) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9319 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9320 | PyObject *list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9321 | |
| 9322 | string = PyUnicode_FromObject(string); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9323 | if (string == NULL || PyUnicode_READY(string) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9324 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9325 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9326 | switch(PyUnicode_KIND(string)) { |
| 9327 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9328 | if (PyUnicode_IS_ASCII(string)) |
| 9329 | list = asciilib_splitlines( |
| 9330 | (PyObject*) string, PyUnicode_1BYTE_DATA(string), |
| 9331 | PyUnicode_GET_LENGTH(string), keepends); |
| 9332 | else |
| 9333 | list = ucs1lib_splitlines( |
| 9334 | (PyObject*) string, PyUnicode_1BYTE_DATA(string), |
| 9335 | PyUnicode_GET_LENGTH(string), keepends); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9336 | break; |
| 9337 | case PyUnicode_2BYTE_KIND: |
| 9338 | list = ucs2lib_splitlines( |
| 9339 | (PyObject*) string, PyUnicode_2BYTE_DATA(string), |
| 9340 | PyUnicode_GET_LENGTH(string), keepends); |
| 9341 | break; |
| 9342 | case PyUnicode_4BYTE_KIND: |
| 9343 | list = ucs4lib_splitlines( |
| 9344 | (PyObject*) string, PyUnicode_4BYTE_DATA(string), |
| 9345 | PyUnicode_GET_LENGTH(string), keepends); |
| 9346 | break; |
| 9347 | default: |
| 9348 | assert(0); |
| 9349 | list = 0; |
| 9350 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9351 | Py_DECREF(string); |
| 9352 | return list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9353 | } |
| 9354 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9355 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9356 | split(PyObject *self, |
| 9357 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9358 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9359 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9360 | int kind1, kind2, kind; |
| 9361 | void *buf1, *buf2; |
| 9362 | Py_ssize_t len1, len2; |
| 9363 | PyObject* out; |
| 9364 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9365 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9366 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9367 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9368 | if (PyUnicode_READY(self) == -1) |
| 9369 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9370 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9371 | if (substring == NULL) |
| 9372 | switch(PyUnicode_KIND(self)) { |
| 9373 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9374 | if (PyUnicode_IS_ASCII(self)) |
| 9375 | return asciilib_split_whitespace( |
| 9376 | (PyObject*) self, PyUnicode_1BYTE_DATA(self), |
| 9377 | PyUnicode_GET_LENGTH(self), maxcount |
| 9378 | ); |
| 9379 | else |
| 9380 | return ucs1lib_split_whitespace( |
| 9381 | (PyObject*) self, PyUnicode_1BYTE_DATA(self), |
| 9382 | PyUnicode_GET_LENGTH(self), maxcount |
| 9383 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9384 | case PyUnicode_2BYTE_KIND: |
| 9385 | return ucs2lib_split_whitespace( |
| 9386 | (PyObject*) self, PyUnicode_2BYTE_DATA(self), |
| 9387 | PyUnicode_GET_LENGTH(self), maxcount |
| 9388 | ); |
| 9389 | case PyUnicode_4BYTE_KIND: |
| 9390 | return ucs4lib_split_whitespace( |
| 9391 | (PyObject*) self, PyUnicode_4BYTE_DATA(self), |
| 9392 | PyUnicode_GET_LENGTH(self), maxcount |
| 9393 | ); |
| 9394 | default: |
| 9395 | assert(0); |
| 9396 | return NULL; |
| 9397 | } |
| 9398 | |
| 9399 | if (PyUnicode_READY(substring) == -1) |
| 9400 | return NULL; |
| 9401 | |
| 9402 | kind1 = PyUnicode_KIND(self); |
| 9403 | kind2 = PyUnicode_KIND(substring); |
| 9404 | kind = kind1 > kind2 ? kind1 : kind2; |
| 9405 | buf1 = PyUnicode_DATA(self); |
| 9406 | buf2 = PyUnicode_DATA(substring); |
| 9407 | if (kind1 != kind) |
| 9408 | buf1 = _PyUnicode_AsKind((PyObject*)self, kind); |
| 9409 | if (!buf1) |
| 9410 | return NULL; |
| 9411 | if (kind2 != kind) |
| 9412 | buf2 = _PyUnicode_AsKind((PyObject*)substring, kind); |
| 9413 | if (!buf2) { |
| 9414 | if (kind1 != kind) PyMem_Free(buf1); |
| 9415 | return NULL; |
| 9416 | } |
| 9417 | len1 = PyUnicode_GET_LENGTH(self); |
| 9418 | len2 = PyUnicode_GET_LENGTH(substring); |
| 9419 | |
| 9420 | switch(kind) { |
| 9421 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9422 | if (PyUnicode_IS_ASCII(self) && PyUnicode_IS_ASCII(substring)) |
| 9423 | out = asciilib_split( |
| 9424 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
| 9425 | else |
| 9426 | out = ucs1lib_split( |
| 9427 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9428 | break; |
| 9429 | case PyUnicode_2BYTE_KIND: |
| 9430 | out = ucs2lib_split( |
| 9431 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
| 9432 | break; |
| 9433 | case PyUnicode_4BYTE_KIND: |
| 9434 | out = ucs4lib_split( |
| 9435 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
| 9436 | break; |
| 9437 | default: |
| 9438 | out = NULL; |
| 9439 | } |
| 9440 | if (kind1 != kind) |
| 9441 | PyMem_Free(buf1); |
| 9442 | if (kind2 != kind) |
| 9443 | PyMem_Free(buf2); |
| 9444 | return out; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9445 | } |
| 9446 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9447 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9448 | rsplit(PyObject *self, |
| 9449 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9450 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9451 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9452 | int kind1, kind2, kind; |
| 9453 | void *buf1, *buf2; |
| 9454 | Py_ssize_t len1, len2; |
| 9455 | PyObject* out; |
| 9456 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9457 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9458 | maxcount = PY_SSIZE_T_MAX; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9459 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9460 | if (PyUnicode_READY(self) == -1) |
| 9461 | return NULL; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9462 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9463 | if (substring == NULL) |
| 9464 | switch(PyUnicode_KIND(self)) { |
| 9465 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9466 | if (PyUnicode_IS_ASCII(self)) |
| 9467 | return asciilib_rsplit_whitespace( |
| 9468 | (PyObject*) self, PyUnicode_1BYTE_DATA(self), |
| 9469 | PyUnicode_GET_LENGTH(self), maxcount |
| 9470 | ); |
| 9471 | else |
| 9472 | return ucs1lib_rsplit_whitespace( |
| 9473 | (PyObject*) self, PyUnicode_1BYTE_DATA(self), |
| 9474 | PyUnicode_GET_LENGTH(self), maxcount |
| 9475 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9476 | case PyUnicode_2BYTE_KIND: |
| 9477 | return ucs2lib_rsplit_whitespace( |
| 9478 | (PyObject*) self, PyUnicode_2BYTE_DATA(self), |
| 9479 | PyUnicode_GET_LENGTH(self), maxcount |
| 9480 | ); |
| 9481 | case PyUnicode_4BYTE_KIND: |
| 9482 | return ucs4lib_rsplit_whitespace( |
| 9483 | (PyObject*) self, PyUnicode_4BYTE_DATA(self), |
| 9484 | PyUnicode_GET_LENGTH(self), maxcount |
| 9485 | ); |
| 9486 | default: |
| 9487 | assert(0); |
| 9488 | return NULL; |
| 9489 | } |
| 9490 | |
| 9491 | if (PyUnicode_READY(substring) == -1) |
| 9492 | return NULL; |
| 9493 | |
| 9494 | kind1 = PyUnicode_KIND(self); |
| 9495 | kind2 = PyUnicode_KIND(substring); |
| 9496 | kind = kind1 > kind2 ? kind1 : kind2; |
| 9497 | buf1 = PyUnicode_DATA(self); |
| 9498 | buf2 = PyUnicode_DATA(substring); |
| 9499 | if (kind1 != kind) |
| 9500 | buf1 = _PyUnicode_AsKind((PyObject*)self, kind); |
| 9501 | if (!buf1) |
| 9502 | return NULL; |
| 9503 | if (kind2 != kind) |
| 9504 | buf2 = _PyUnicode_AsKind((PyObject*)substring, kind); |
| 9505 | if (!buf2) { |
| 9506 | if (kind1 != kind) PyMem_Free(buf1); |
| 9507 | return NULL; |
| 9508 | } |
| 9509 | len1 = PyUnicode_GET_LENGTH(self); |
| 9510 | len2 = PyUnicode_GET_LENGTH(substring); |
| 9511 | |
| 9512 | switch(kind) { |
| 9513 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9514 | if (PyUnicode_IS_ASCII(self) && PyUnicode_IS_ASCII(substring)) |
| 9515 | out = asciilib_rsplit( |
| 9516 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
| 9517 | else |
| 9518 | out = ucs1lib_rsplit( |
| 9519 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9520 | break; |
| 9521 | case PyUnicode_2BYTE_KIND: |
| 9522 | out = ucs2lib_rsplit( |
| 9523 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
| 9524 | break; |
| 9525 | case PyUnicode_4BYTE_KIND: |
| 9526 | out = ucs4lib_rsplit( |
| 9527 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
| 9528 | break; |
| 9529 | default: |
| 9530 | out = NULL; |
| 9531 | } |
| 9532 | if (kind1 != kind) |
| 9533 | PyMem_Free(buf1); |
| 9534 | if (kind2 != kind) |
| 9535 | PyMem_Free(buf2); |
| 9536 | return out; |
| 9537 | } |
| 9538 | |
| 9539 | static Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9540 | anylib_find(int kind, PyObject *str1, void *buf1, Py_ssize_t len1, |
| 9541 | 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] | 9542 | { |
| 9543 | switch(kind) { |
| 9544 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9545 | if (PyUnicode_IS_ASCII(str1) && PyUnicode_IS_ASCII(str2)) |
| 9546 | return asciilib_find(buf1, len1, buf2, len2, offset); |
| 9547 | else |
| 9548 | return ucs1lib_find(buf1, len1, buf2, len2, offset); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9549 | case PyUnicode_2BYTE_KIND: |
| 9550 | return ucs2lib_find(buf1, len1, buf2, len2, offset); |
| 9551 | case PyUnicode_4BYTE_KIND: |
| 9552 | return ucs4lib_find(buf1, len1, buf2, len2, offset); |
| 9553 | } |
| 9554 | assert(0); |
| 9555 | return -1; |
| 9556 | } |
| 9557 | |
| 9558 | static Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9559 | anylib_count(int kind, PyObject *sstr, void* sbuf, Py_ssize_t slen, |
| 9560 | 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] | 9561 | { |
| 9562 | switch(kind) { |
| 9563 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9564 | if (PyUnicode_IS_ASCII(sstr) && PyUnicode_IS_ASCII(str1)) |
| 9565 | return asciilib_count(sbuf, slen, buf1, len1, maxcount); |
| 9566 | else |
| 9567 | return ucs1lib_count(sbuf, slen, buf1, len1, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9568 | case PyUnicode_2BYTE_KIND: |
| 9569 | return ucs2lib_count(sbuf, slen, buf1, len1, maxcount); |
| 9570 | case PyUnicode_4BYTE_KIND: |
| 9571 | return ucs4lib_count(sbuf, slen, buf1, len1, maxcount); |
| 9572 | } |
| 9573 | assert(0); |
| 9574 | return 0; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9575 | } |
| 9576 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9577 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9578 | replace(PyObject *self, PyObject *str1, |
| 9579 | PyObject *str2, Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9580 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9581 | PyObject *u; |
| 9582 | char *sbuf = PyUnicode_DATA(self); |
| 9583 | char *buf1 = PyUnicode_DATA(str1); |
| 9584 | char *buf2 = PyUnicode_DATA(str2); |
| 9585 | int srelease = 0, release1 = 0, release2 = 0; |
| 9586 | int skind = PyUnicode_KIND(self); |
| 9587 | int kind1 = PyUnicode_KIND(str1); |
| 9588 | int kind2 = PyUnicode_KIND(str2); |
| 9589 | Py_ssize_t slen = PyUnicode_GET_LENGTH(self); |
| 9590 | Py_ssize_t len1 = PyUnicode_GET_LENGTH(str1); |
| 9591 | Py_ssize_t len2 = PyUnicode_GET_LENGTH(str2); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9592 | |
| 9593 | if (maxcount < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9594 | maxcount = PY_SSIZE_T_MAX; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9595 | else if (maxcount == 0 || slen == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9596 | goto nothing; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9597 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9598 | if (skind < kind1) |
| 9599 | /* substring too wide to be present */ |
| 9600 | goto nothing; |
| 9601 | |
| 9602 | if (len1 == len2) { |
Antoine Pitrou | cbfdee3 | 2010-01-13 08:58:08 +0000 | [diff] [blame] | 9603 | Py_ssize_t i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9604 | /* same length */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9605 | if (len1 == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9606 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9607 | if (len1 == 1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9608 | /* replace characters */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9609 | Py_UCS4 u1, u2, maxchar; |
| 9610 | int mayshrink, rkind; |
| 9611 | u1 = PyUnicode_READ_CHAR(str1, 0); |
| 9612 | if (!findchar(sbuf, PyUnicode_KIND(self), |
| 9613 | slen, u1, 1)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9614 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9615 | u2 = PyUnicode_READ_CHAR(str2, 0); |
| 9616 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
| 9617 | /* Replacing u1 with u2 may cause a maxchar reduction in the |
| 9618 | result string. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9619 | if (u2 > maxchar) { |
| 9620 | maxchar = u2; |
| 9621 | mayshrink = 0; |
| 9622 | } |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 9623 | else |
| 9624 | mayshrink = maxchar > 127; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9625 | u = PyUnicode_New(slen, maxchar); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9626 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9627 | goto error; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9628 | copy_characters(u, 0, self, 0, slen); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9629 | rkind = PyUnicode_KIND(u); |
| 9630 | for (i = 0; i < PyUnicode_GET_LENGTH(u); i++) |
| 9631 | if (PyUnicode_READ(rkind, PyUnicode_DATA(u), i) == u1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9632 | if (--maxcount < 0) |
| 9633 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9634 | PyUnicode_WRITE(rkind, PyUnicode_DATA(u), i, u2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9635 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9636 | if (mayshrink) { |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 9637 | unicode_adjust_maxchar(&u); |
| 9638 | if (u == NULL) |
| 9639 | goto error; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9640 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9641 | } else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9642 | int rkind = skind; |
| 9643 | char *res; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 9644 | PyObject *rstr; |
| 9645 | Py_UCS4 maxchar; |
| 9646 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9647 | if (kind1 < rkind) { |
| 9648 | /* widen substring */ |
| 9649 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 9650 | if (!buf1) goto error; |
| 9651 | release1 = 1; |
| 9652 | } |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9653 | i = anylib_find(rkind, self, sbuf, slen, str1, buf1, len1, 0); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9654 | if (i < 0) |
| 9655 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9656 | if (rkind > kind2) { |
| 9657 | /* widen replacement */ |
| 9658 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 9659 | if (!buf2) goto error; |
| 9660 | release2 = 1; |
| 9661 | } |
| 9662 | else if (rkind < kind2) { |
| 9663 | /* widen self and buf1 */ |
| 9664 | rkind = kind2; |
| 9665 | if (release1) PyMem_Free(buf1); |
| 9666 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 9667 | if (!sbuf) goto error; |
| 9668 | srelease = 1; |
| 9669 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 9670 | if (!buf1) goto error; |
| 9671 | release1 = 1; |
| 9672 | } |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 9673 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
| 9674 | maxchar = Py_MAX(maxchar, PyUnicode_MAX_CHAR_VALUE(str2)); |
| 9675 | rstr = PyUnicode_New(slen, maxchar); |
| 9676 | if (!rstr) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9677 | goto error; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 9678 | res = PyUnicode_DATA(rstr); |
| 9679 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9680 | memcpy(res, sbuf, PyUnicode_KIND_SIZE(rkind, slen)); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9681 | /* change everything in-place, starting with this one */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9682 | memcpy(res + PyUnicode_KIND_SIZE(rkind, i), |
| 9683 | buf2, |
| 9684 | PyUnicode_KIND_SIZE(rkind, len2)); |
| 9685 | i += len1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9686 | |
| 9687 | while ( --maxcount > 0) { |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9688 | i = anylib_find(rkind, self, |
| 9689 | sbuf+PyUnicode_KIND_SIZE(rkind, i), slen-i, |
| 9690 | str1, buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9691 | if (i == -1) |
| 9692 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9693 | memcpy(res + PyUnicode_KIND_SIZE(rkind, i), |
| 9694 | buf2, |
| 9695 | PyUnicode_KIND_SIZE(rkind, len2)); |
| 9696 | i += len1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9697 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9698 | |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 9699 | u = rstr; |
| 9700 | unicode_adjust_maxchar(&u); |
| 9701 | if (!u) |
| 9702 | goto error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9703 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9704 | } else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9705 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9706 | Py_ssize_t n, i, j, ires; |
| 9707 | Py_ssize_t product, new_size; |
| 9708 | int rkind = skind; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 9709 | PyObject *rstr; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9710 | char *res; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 9711 | Py_UCS4 maxchar; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9712 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9713 | if (kind1 < rkind) { |
| 9714 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 9715 | if (!buf1) goto error; |
| 9716 | release1 = 1; |
| 9717 | } |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9718 | n = anylib_count(rkind, self, sbuf, slen, str1, buf1, len1, maxcount); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9719 | if (n == 0) |
| 9720 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9721 | if (kind2 < rkind) { |
| 9722 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 9723 | if (!buf2) goto error; |
| 9724 | release2 = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9725 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9726 | else if (kind2 > rkind) { |
| 9727 | rkind = kind2; |
| 9728 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 9729 | if (!sbuf) goto error; |
| 9730 | srelease = 1; |
| 9731 | if (release1) PyMem_Free(buf1); |
| 9732 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 9733 | if (!buf1) goto error; |
| 9734 | release1 = 1; |
| 9735 | } |
| 9736 | /* new_size = PyUnicode_GET_LENGTH(self) + n * (PyUnicode_GET_LENGTH(str2) - |
| 9737 | PyUnicode_GET_LENGTH(str1))); */ |
| 9738 | product = n * (len2-len1); |
| 9739 | if ((product / (len2-len1)) != n) { |
| 9740 | PyErr_SetString(PyExc_OverflowError, |
| 9741 | "replace string is too long"); |
| 9742 | goto error; |
| 9743 | } |
| 9744 | new_size = slen + product; |
| 9745 | if (new_size < 0 || new_size > (PY_SSIZE_T_MAX >> (rkind-1))) { |
| 9746 | PyErr_SetString(PyExc_OverflowError, |
| 9747 | "replace string is too long"); |
| 9748 | goto error; |
| 9749 | } |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 9750 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
| 9751 | maxchar = Py_MAX(maxchar, PyUnicode_MAX_CHAR_VALUE(str2)); |
| 9752 | rstr = PyUnicode_New(new_size, maxchar); |
| 9753 | if (!rstr) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9754 | goto error; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 9755 | res = PyUnicode_DATA(rstr); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9756 | ires = i = 0; |
| 9757 | if (len1 > 0) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9758 | while (n-- > 0) { |
| 9759 | /* look for next match */ |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9760 | j = anylib_find(rkind, self, |
| 9761 | sbuf + PyUnicode_KIND_SIZE(rkind, i), slen-i, |
| 9762 | str1, buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9763 | if (j == -1) |
| 9764 | break; |
| 9765 | else if (j > i) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9766 | /* copy unchanged part [i:j] */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9767 | memcpy(res + PyUnicode_KIND_SIZE(rkind, ires), |
| 9768 | sbuf + PyUnicode_KIND_SIZE(rkind, i), |
| 9769 | PyUnicode_KIND_SIZE(rkind, j-i)); |
| 9770 | ires += j - i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9771 | } |
| 9772 | /* copy substitution string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9773 | if (len2 > 0) { |
| 9774 | memcpy(res + PyUnicode_KIND_SIZE(rkind, ires), |
| 9775 | buf2, |
| 9776 | PyUnicode_KIND_SIZE(rkind, len2)); |
| 9777 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9778 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9779 | i = j + len1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9780 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9781 | if (i < slen) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9782 | /* copy tail [i:] */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9783 | memcpy(res + PyUnicode_KIND_SIZE(rkind, ires), |
| 9784 | sbuf + PyUnicode_KIND_SIZE(rkind, i), |
| 9785 | PyUnicode_KIND_SIZE(rkind, slen-i)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9786 | } else { |
| 9787 | /* interleave */ |
| 9788 | while (n > 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9789 | memcpy(res + PyUnicode_KIND_SIZE(rkind, ires), |
| 9790 | buf2, |
| 9791 | PyUnicode_KIND_SIZE(rkind, len2)); |
| 9792 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9793 | if (--n <= 0) |
| 9794 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9795 | memcpy(res + PyUnicode_KIND_SIZE(rkind, ires), |
| 9796 | sbuf + PyUnicode_KIND_SIZE(rkind, i), |
| 9797 | PyUnicode_KIND_SIZE(rkind, 1)); |
| 9798 | ires++; |
| 9799 | i++; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9800 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9801 | memcpy(res + PyUnicode_KIND_SIZE(rkind, ires), |
| 9802 | sbuf + PyUnicode_KIND_SIZE(rkind, i), |
| 9803 | PyUnicode_KIND_SIZE(rkind, slen-i)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9804 | } |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 9805 | u = rstr; |
| 9806 | unicode_adjust_maxchar(&u); |
| 9807 | if (u == NULL) |
| 9808 | goto error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9809 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9810 | if (srelease) |
| 9811 | PyMem_FREE(sbuf); |
| 9812 | if (release1) |
| 9813 | PyMem_FREE(buf1); |
| 9814 | if (release2) |
| 9815 | PyMem_FREE(buf2); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 9816 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9817 | return u; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9818 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9819 | nothing: |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9820 | /* nothing to replace; return original string (when possible) */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9821 | if (srelease) |
| 9822 | PyMem_FREE(sbuf); |
| 9823 | if (release1) |
| 9824 | PyMem_FREE(buf1); |
| 9825 | if (release2) |
| 9826 | PyMem_FREE(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9827 | if (PyUnicode_CheckExact(self)) { |
| 9828 | Py_INCREF(self); |
| 9829 | return (PyObject *) self; |
| 9830 | } |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 9831 | return PyUnicode_Copy(self); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9832 | error: |
| 9833 | if (srelease && sbuf) |
| 9834 | PyMem_FREE(sbuf); |
| 9835 | if (release1 && buf1) |
| 9836 | PyMem_FREE(buf1); |
| 9837 | if (release2 && buf2) |
| 9838 | PyMem_FREE(buf2); |
| 9839 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9840 | } |
| 9841 | |
| 9842 | /* --- Unicode Object Methods --------------------------------------------- */ |
| 9843 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9844 | PyDoc_STRVAR(title__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9845 | "S.title() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9846 | \n\ |
| 9847 | 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] | 9848 | characters, all remaining cased characters have lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9849 | |
| 9850 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9851 | unicode_title(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9852 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9853 | return fixup(self, fixtitle); |
| 9854 | } |
| 9855 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9856 | PyDoc_STRVAR(capitalize__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9857 | "S.capitalize() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9858 | \n\ |
| 9859 | 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] | 9860 | have upper case and the rest lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9861 | |
| 9862 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9863 | unicode_capitalize(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9864 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9865 | return fixup(self, fixcapitalize); |
| 9866 | } |
| 9867 | |
| 9868 | #if 0 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9869 | PyDoc_STRVAR(capwords__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9870 | "S.capwords() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9871 | \n\ |
| 9872 | 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] | 9873 | normalized whitespace (all whitespace strings are replaced by ' ')."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9874 | |
| 9875 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 9876 | unicode_capwords(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9877 | { |
| 9878 | PyObject *list; |
| 9879 | PyObject *item; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9880 | Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9881 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9882 | /* Split into words */ |
| 9883 | list = split(self, NULL, -1); |
| 9884 | if (!list) |
| 9885 | return NULL; |
| 9886 | |
| 9887 | /* Capitalize each word */ |
| 9888 | for (i = 0; i < PyList_GET_SIZE(list); i++) { |
| 9889 | item = fixup((PyUnicodeObject *)PyList_GET_ITEM(list, i), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9890 | fixcapitalize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9891 | if (item == NULL) |
| 9892 | goto onError; |
| 9893 | Py_DECREF(PyList_GET_ITEM(list, i)); |
| 9894 | PyList_SET_ITEM(list, i, item); |
| 9895 | } |
| 9896 | |
| 9897 | /* Join the words to form a new string */ |
| 9898 | item = PyUnicode_Join(NULL, list); |
| 9899 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9900 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9901 | Py_DECREF(list); |
| 9902 | return (PyObject *)item; |
| 9903 | } |
| 9904 | #endif |
| 9905 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 9906 | /* Argument converter. Coerces to a single unicode character */ |
| 9907 | |
| 9908 | static int |
| 9909 | convert_uc(PyObject *obj, void *addr) |
| 9910 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9911 | Py_UCS4 *fillcharloc = (Py_UCS4 *)addr; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9912 | PyObject *uniobj; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 9913 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9914 | uniobj = PyUnicode_FromObject(obj); |
| 9915 | if (uniobj == NULL) { |
| 9916 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9917 | "The fill character cannot be converted to Unicode"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9918 | return 0; |
| 9919 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9920 | if (PyUnicode_GET_LENGTH(uniobj) != 1) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9921 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9922 | "The fill character must be exactly one character long"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9923 | Py_DECREF(uniobj); |
| 9924 | return 0; |
| 9925 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9926 | *fillcharloc = PyUnicode_READ_CHAR(uniobj, 0); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9927 | Py_DECREF(uniobj); |
| 9928 | return 1; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 9929 | } |
| 9930 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9931 | PyDoc_STRVAR(center__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9932 | "S.center(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9933 | \n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 9934 | Return S centered in a string of length width. Padding is\n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 9935 | done using the specified fill character (default is a space)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9936 | |
| 9937 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9938 | unicode_center(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9939 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9940 | Py_ssize_t marg, left; |
| 9941 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9942 | Py_UCS4 fillchar = ' '; |
| 9943 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 9944 | 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] | 9945 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9946 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 9947 | if (PyUnicode_READY(self) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9948 | return NULL; |
| 9949 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9950 | if (_PyUnicode_LENGTH(self) >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9951 | Py_INCREF(self); |
| 9952 | return (PyObject*) self; |
| 9953 | } |
| 9954 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9955 | marg = width - _PyUnicode_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9956 | left = marg / 2 + (marg & width & 1); |
| 9957 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9958 | return pad(self, left, marg - left, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9959 | } |
| 9960 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 9961 | #if 0 |
| 9962 | |
| 9963 | /* This code should go into some future Unicode collation support |
| 9964 | module. The basic comparison should compare ordinals on a naive |
Georg Brandl | c6c3178 | 2009-06-08 13:41:29 +0000 | [diff] [blame] | 9965 | basis (this is what Java does and thus Jython too). */ |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 9966 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9967 | /* speedy UTF-16 code point order comparison */ |
| 9968 | /* gleaned from: */ |
| 9969 | /* http://www-4.ibm.com/software/developer/library/utf16.html?dwzone=unicode */ |
| 9970 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 9971 | static short utf16Fixup[32] = |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9972 | { |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9973 | 0, 0, 0, 0, 0, 0, 0, 0, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9974 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 9975 | 0, 0, 0, 0, 0, 0, 0, 0, |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 9976 | 0, 0, 0, 0x2000, -0x800, -0x800, -0x800, -0x800 |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9977 | }; |
| 9978 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9979 | static int |
| 9980 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 9981 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9982 | Py_ssize_t len1, len2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9983 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9984 | Py_UNICODE *s1 = str1->str; |
| 9985 | Py_UNICODE *s2 = str2->str; |
| 9986 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9987 | len1 = str1->_base._base.length; |
| 9988 | len2 = str2->_base._base.length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9989 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9990 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9991 | Py_UNICODE c1, c2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9992 | |
| 9993 | c1 = *s1++; |
| 9994 | c2 = *s2++; |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 9995 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9996 | if (c1 > (1<<11) * 26) |
| 9997 | c1 += utf16Fixup[c1>>11]; |
| 9998 | if (c2 > (1<<11) * 26) |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9999 | c2 += utf16Fixup[c2>>11]; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 10000 | /* now c1 and c2 are in UTF-32-compatible order */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 10001 | |
| 10002 | if (c1 != c2) |
| 10003 | return (c1 < c2) ? -1 : 1; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 10004 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 10005 | len1--; len2--; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10006 | } |
| 10007 | |
| 10008 | return (len1 < len2) ? -1 : (len1 != len2); |
| 10009 | } |
| 10010 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10011 | #else |
| 10012 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10013 | /* This function assumes that str1 and str2 are readied by the caller. */ |
| 10014 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10015 | static int |
| 10016 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 10017 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10018 | int kind1, kind2; |
| 10019 | void *data1, *data2; |
| 10020 | Py_ssize_t len1, len2, i; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10021 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10022 | kind1 = PyUnicode_KIND(str1); |
| 10023 | kind2 = PyUnicode_KIND(str2); |
| 10024 | data1 = PyUnicode_DATA(str1); |
| 10025 | data2 = PyUnicode_DATA(str2); |
| 10026 | len1 = PyUnicode_GET_LENGTH(str1); |
| 10027 | len2 = PyUnicode_GET_LENGTH(str2); |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10028 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10029 | for (i = 0; i < len1 && i < len2; ++i) { |
| 10030 | Py_UCS4 c1, c2; |
| 10031 | c1 = PyUnicode_READ(kind1, data1, i); |
| 10032 | c2 = PyUnicode_READ(kind2, data2, i); |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 10033 | |
| 10034 | if (c1 != c2) |
| 10035 | return (c1 < c2) ? -1 : 1; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10036 | } |
| 10037 | |
| 10038 | return (len1 < len2) ? -1 : (len1 != len2); |
| 10039 | } |
| 10040 | |
| 10041 | #endif |
| 10042 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10043 | int |
| 10044 | PyUnicode_Compare(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10045 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10046 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 10047 | if (PyUnicode_READY(left) == -1 || |
| 10048 | PyUnicode_READY(right) == -1) |
| 10049 | return -1; |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 10050 | return unicode_compare((PyUnicodeObject *)left, |
| 10051 | (PyUnicodeObject *)right); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10052 | } |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 10053 | PyErr_Format(PyExc_TypeError, |
| 10054 | "Can't compare %.100s and %.100s", |
| 10055 | left->ob_type->tp_name, |
| 10056 | right->ob_type->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10057 | return -1; |
| 10058 | } |
| 10059 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10060 | int |
| 10061 | PyUnicode_CompareWithASCIIString(PyObject* uni, const char* str) |
| 10062 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10063 | Py_ssize_t i; |
| 10064 | int kind; |
| 10065 | void *data; |
| 10066 | Py_UCS4 chr; |
| 10067 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 10068 | assert(_PyUnicode_CHECK(uni)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10069 | if (PyUnicode_READY(uni) == -1) |
| 10070 | return -1; |
| 10071 | kind = PyUnicode_KIND(uni); |
| 10072 | data = PyUnicode_DATA(uni); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10073 | /* Compare Unicode string and source character set string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10074 | for (i = 0; (chr = PyUnicode_READ(kind, data, i)) && str[i]; i++) |
| 10075 | if (chr != str[i]) |
| 10076 | return (chr < (unsigned char)(str[i])) ? -1 : 1; |
Benjamin Peterson | 8667a9b | 2010-01-09 21:45:28 +0000 | [diff] [blame] | 10077 | /* This check keeps Python strings that end in '\0' from comparing equal |
| 10078 | to C strings identical up to that point. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10079 | if (PyUnicode_GET_LENGTH(uni) != i || chr) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10080 | return 1; /* uni is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10081 | if (str[i]) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10082 | return -1; /* str is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10083 | return 0; |
| 10084 | } |
| 10085 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10086 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10087 | #define TEST_COND(cond) \ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10088 | ((cond) ? Py_True : Py_False) |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10089 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10090 | PyObject * |
| 10091 | PyUnicode_RichCompare(PyObject *left, PyObject *right, int op) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10092 | { |
| 10093 | int result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10094 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10095 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 10096 | PyObject *v; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10097 | if (PyUnicode_READY(left) == -1 || |
| 10098 | PyUnicode_READY(right) == -1) |
| 10099 | return NULL; |
| 10100 | if (PyUnicode_GET_LENGTH(left) != PyUnicode_GET_LENGTH(right) || |
| 10101 | PyUnicode_KIND(left) != PyUnicode_KIND(right)) { |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10102 | if (op == Py_EQ) { |
| 10103 | Py_INCREF(Py_False); |
| 10104 | return Py_False; |
| 10105 | } |
| 10106 | if (op == Py_NE) { |
| 10107 | Py_INCREF(Py_True); |
| 10108 | return Py_True; |
| 10109 | } |
| 10110 | } |
| 10111 | if (left == right) |
| 10112 | result = 0; |
| 10113 | else |
| 10114 | result = unicode_compare((PyUnicodeObject *)left, |
| 10115 | (PyUnicodeObject *)right); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10116 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10117 | /* Convert the return value to a Boolean */ |
| 10118 | switch (op) { |
| 10119 | case Py_EQ: |
| 10120 | v = TEST_COND(result == 0); |
| 10121 | break; |
| 10122 | case Py_NE: |
| 10123 | v = TEST_COND(result != 0); |
| 10124 | break; |
| 10125 | case Py_LE: |
| 10126 | v = TEST_COND(result <= 0); |
| 10127 | break; |
| 10128 | case Py_GE: |
| 10129 | v = TEST_COND(result >= 0); |
| 10130 | break; |
| 10131 | case Py_LT: |
| 10132 | v = TEST_COND(result == -1); |
| 10133 | break; |
| 10134 | case Py_GT: |
| 10135 | v = TEST_COND(result == 1); |
| 10136 | break; |
| 10137 | default: |
| 10138 | PyErr_BadArgument(); |
| 10139 | return NULL; |
| 10140 | } |
| 10141 | Py_INCREF(v); |
| 10142 | return v; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10143 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10144 | |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 10145 | Py_RETURN_NOTIMPLEMENTED; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10146 | } |
| 10147 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10148 | int |
| 10149 | PyUnicode_Contains(PyObject *container, PyObject *element) |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10150 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10151 | PyObject *str, *sub; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10152 | int kind1, kind2, kind; |
| 10153 | void *buf1, *buf2; |
| 10154 | Py_ssize_t len1, len2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10155 | int result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10156 | |
| 10157 | /* Coerce the two arguments */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10158 | sub = PyUnicode_FromObject(element); |
| 10159 | if (!sub) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10160 | PyErr_Format(PyExc_TypeError, |
| 10161 | "'in <string>' requires string as left operand, not %s", |
| 10162 | element->ob_type->tp_name); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10163 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10164 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10165 | if (PyUnicode_READY(sub) == -1) |
| 10166 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10167 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10168 | str = PyUnicode_FromObject(container); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 10169 | if (!str || PyUnicode_READY(str) == -1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10170 | Py_DECREF(sub); |
| 10171 | return -1; |
| 10172 | } |
| 10173 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10174 | kind1 = PyUnicode_KIND(str); |
| 10175 | kind2 = PyUnicode_KIND(sub); |
| 10176 | kind = kind1 > kind2 ? kind1 : kind2; |
| 10177 | buf1 = PyUnicode_DATA(str); |
| 10178 | buf2 = PyUnicode_DATA(sub); |
| 10179 | if (kind1 != kind) |
| 10180 | buf1 = _PyUnicode_AsKind((PyObject*)str, kind); |
| 10181 | if (!buf1) { |
| 10182 | Py_DECREF(sub); |
| 10183 | return -1; |
| 10184 | } |
| 10185 | if (kind2 != kind) |
| 10186 | buf2 = _PyUnicode_AsKind((PyObject*)sub, kind); |
| 10187 | if (!buf2) { |
| 10188 | Py_DECREF(sub); |
| 10189 | if (kind1 != kind) PyMem_Free(buf1); |
| 10190 | return -1; |
| 10191 | } |
| 10192 | len1 = PyUnicode_GET_LENGTH(str); |
| 10193 | len2 = PyUnicode_GET_LENGTH(sub); |
| 10194 | |
| 10195 | switch(kind) { |
| 10196 | case PyUnicode_1BYTE_KIND: |
| 10197 | result = ucs1lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10198 | break; |
| 10199 | case PyUnicode_2BYTE_KIND: |
| 10200 | result = ucs2lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10201 | break; |
| 10202 | case PyUnicode_4BYTE_KIND: |
| 10203 | result = ucs4lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10204 | break; |
| 10205 | default: |
| 10206 | result = -1; |
| 10207 | assert(0); |
| 10208 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10209 | |
| 10210 | Py_DECREF(str); |
| 10211 | Py_DECREF(sub); |
| 10212 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10213 | if (kind1 != kind) |
| 10214 | PyMem_Free(buf1); |
| 10215 | if (kind2 != kind) |
| 10216 | PyMem_Free(buf2); |
| 10217 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10218 | return result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10219 | } |
| 10220 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10221 | /* Concat to string or Unicode object giving a new Unicode object. */ |
| 10222 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10223 | PyObject * |
| 10224 | PyUnicode_Concat(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10225 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10226 | PyObject *u = NULL, *v = NULL, *w; |
| 10227 | Py_UCS4 maxchar; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10228 | |
| 10229 | /* Coerce the two arguments */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10230 | u = PyUnicode_FromObject(left); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10231 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10232 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10233 | v = PyUnicode_FromObject(right); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10234 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10235 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10236 | |
| 10237 | /* Shortcuts */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 10238 | if (v == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10239 | Py_DECREF(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10240 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10241 | } |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 10242 | if (u == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10243 | Py_DECREF(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10244 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10245 | } |
| 10246 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10247 | maxchar = PyUnicode_MAX_CHAR_VALUE(u); |
Victor Stinner | ff9e50f | 2011-09-28 22:17:19 +0200 | [diff] [blame] | 10248 | maxchar = Py_MAX(maxchar, PyUnicode_MAX_CHAR_VALUE(v)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10249 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10250 | /* Concat the two Unicode strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10251 | w = PyUnicode_New( |
| 10252 | PyUnicode_GET_LENGTH(u) + PyUnicode_GET_LENGTH(v), |
| 10253 | maxchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10254 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10255 | goto onError; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 10256 | copy_characters(w, 0, u, 0, PyUnicode_GET_LENGTH(u)); |
| 10257 | 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] | 10258 | Py_DECREF(u); |
| 10259 | Py_DECREF(v); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10260 | assert(_PyUnicode_CheckConsistency(w, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10261 | return w; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10262 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10263 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10264 | Py_XDECREF(u); |
| 10265 | Py_XDECREF(v); |
| 10266 | return NULL; |
| 10267 | } |
| 10268 | |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10269 | static void |
| 10270 | unicode_append_inplace(PyObject **p_left, PyObject *right) |
| 10271 | { |
| 10272 | Py_ssize_t left_len, right_len, new_len; |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10273 | |
| 10274 | assert(PyUnicode_IS_READY(*p_left)); |
| 10275 | assert(PyUnicode_IS_READY(right)); |
| 10276 | |
| 10277 | left_len = PyUnicode_GET_LENGTH(*p_left); |
| 10278 | right_len = PyUnicode_GET_LENGTH(right); |
| 10279 | if (left_len > PY_SSIZE_T_MAX - right_len) { |
| 10280 | PyErr_SetString(PyExc_OverflowError, |
| 10281 | "strings are too large to concat"); |
| 10282 | goto error; |
| 10283 | } |
| 10284 | new_len = left_len + right_len; |
| 10285 | |
| 10286 | /* Now we own the last reference to 'left', so we can resize it |
| 10287 | * in-place. |
| 10288 | */ |
| 10289 | if (unicode_resize(p_left, new_len) != 0) { |
| 10290 | /* XXX if _PyUnicode_Resize() fails, 'left' has been |
| 10291 | * deallocated so it cannot be put back into |
| 10292 | * 'variable'. The MemoryError is raised when there |
| 10293 | * is no value in 'variable', which might (very |
| 10294 | * remotely) be a cause of incompatibilities. |
| 10295 | */ |
| 10296 | goto error; |
| 10297 | } |
| 10298 | /* copy 'right' into the newly allocated area of 'left' */ |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 10299 | copy_characters(*p_left, left_len, right, 0, right_len); |
| 10300 | _PyUnicode_DIRTY(*p_left); |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10301 | return; |
| 10302 | |
| 10303 | error: |
| 10304 | Py_DECREF(*p_left); |
| 10305 | *p_left = NULL; |
| 10306 | } |
| 10307 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10308 | void |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10309 | PyUnicode_Append(PyObject **p_left, PyObject *right) |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10310 | { |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10311 | PyObject *left, *res; |
| 10312 | |
| 10313 | if (p_left == NULL) { |
| 10314 | if (!PyErr_Occurred()) |
| 10315 | PyErr_BadInternalCall(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10316 | return; |
| 10317 | } |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10318 | left = *p_left; |
| 10319 | if (right == NULL || !PyUnicode_Check(left)) { |
| 10320 | if (!PyErr_Occurred()) |
| 10321 | PyErr_BadInternalCall(); |
| 10322 | goto error; |
| 10323 | } |
| 10324 | |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 10325 | if (PyUnicode_READY(left)) |
| 10326 | goto error; |
| 10327 | if (PyUnicode_READY(right)) |
| 10328 | goto error; |
| 10329 | |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10330 | if (PyUnicode_CheckExact(left) && left != unicode_empty |
| 10331 | && PyUnicode_CheckExact(right) && right != unicode_empty |
| 10332 | && unicode_resizable(left) |
| 10333 | && (_PyUnicode_KIND(right) <= _PyUnicode_KIND(left) |
| 10334 | || _PyUnicode_WSTR(left) != NULL)) |
| 10335 | { |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10336 | /* Don't resize for ascii += latin1. Convert ascii to latin1 requires |
| 10337 | to change the structure size, but characters are stored just after |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 10338 | the structure, and so it requires to move all characters which is |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10339 | not so different than duplicating the string. */ |
| 10340 | if (!(PyUnicode_IS_ASCII(left) && !PyUnicode_IS_ASCII(right))) |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10341 | { |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10342 | unicode_append_inplace(p_left, right); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10343 | if (p_left != NULL) |
| 10344 | assert(_PyUnicode_CheckConsistency(*p_left, 1)); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10345 | return; |
| 10346 | } |
| 10347 | } |
| 10348 | |
| 10349 | res = PyUnicode_Concat(left, right); |
| 10350 | if (res == NULL) |
| 10351 | goto error; |
| 10352 | Py_DECREF(left); |
| 10353 | *p_left = res; |
| 10354 | return; |
| 10355 | |
| 10356 | error: |
| 10357 | Py_DECREF(*p_left); |
| 10358 | *p_left = NULL; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10359 | } |
| 10360 | |
| 10361 | void |
| 10362 | PyUnicode_AppendAndDel(PyObject **pleft, PyObject *right) |
| 10363 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10364 | PyUnicode_Append(pleft, right); |
| 10365 | Py_XDECREF(right); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10366 | } |
| 10367 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10368 | PyDoc_STRVAR(count__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10369 | "S.count(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10370 | \n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10371 | Return the number of non-overlapping occurrences of substring sub in\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 10372 | 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] | 10373 | interpreted as in slice notation."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10374 | |
| 10375 | static PyObject * |
| 10376 | unicode_count(PyUnicodeObject *self, PyObject *args) |
| 10377 | { |
| 10378 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10379 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10380 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10381 | PyObject *result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10382 | int kind1, kind2, kind; |
| 10383 | void *buf1, *buf2; |
| 10384 | Py_ssize_t len1, len2, iresult; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10385 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10386 | if (!stringlib_parse_args_finds_unicode("count", args, &substring, |
| 10387 | &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10388 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 10389 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10390 | kind1 = PyUnicode_KIND(self); |
| 10391 | kind2 = PyUnicode_KIND(substring); |
| 10392 | kind = kind1 > kind2 ? kind1 : kind2; |
| 10393 | buf1 = PyUnicode_DATA(self); |
| 10394 | buf2 = PyUnicode_DATA(substring); |
| 10395 | if (kind1 != kind) |
| 10396 | buf1 = _PyUnicode_AsKind((PyObject*)self, kind); |
| 10397 | if (!buf1) { |
| 10398 | Py_DECREF(substring); |
| 10399 | return NULL; |
| 10400 | } |
| 10401 | if (kind2 != kind) |
| 10402 | buf2 = _PyUnicode_AsKind((PyObject*)substring, kind); |
| 10403 | if (!buf2) { |
| 10404 | Py_DECREF(substring); |
| 10405 | if (kind1 != kind) PyMem_Free(buf1); |
| 10406 | return NULL; |
| 10407 | } |
| 10408 | len1 = PyUnicode_GET_LENGTH(self); |
| 10409 | len2 = PyUnicode_GET_LENGTH(substring); |
| 10410 | |
| 10411 | ADJUST_INDICES(start, end, len1); |
| 10412 | switch(kind) { |
| 10413 | case PyUnicode_1BYTE_KIND: |
| 10414 | iresult = ucs1lib_count( |
| 10415 | ((Py_UCS1*)buf1) + start, end - start, |
| 10416 | buf2, len2, PY_SSIZE_T_MAX |
| 10417 | ); |
| 10418 | break; |
| 10419 | case PyUnicode_2BYTE_KIND: |
| 10420 | iresult = ucs2lib_count( |
| 10421 | ((Py_UCS2*)buf1) + start, end - start, |
| 10422 | buf2, len2, PY_SSIZE_T_MAX |
| 10423 | ); |
| 10424 | break; |
| 10425 | case PyUnicode_4BYTE_KIND: |
| 10426 | iresult = ucs4lib_count( |
| 10427 | ((Py_UCS4*)buf1) + start, end - start, |
| 10428 | buf2, len2, PY_SSIZE_T_MAX |
| 10429 | ); |
| 10430 | break; |
| 10431 | default: |
| 10432 | assert(0); iresult = 0; |
| 10433 | } |
| 10434 | |
| 10435 | result = PyLong_FromSsize_t(iresult); |
| 10436 | |
| 10437 | if (kind1 != kind) |
| 10438 | PyMem_Free(buf1); |
| 10439 | if (kind2 != kind) |
| 10440 | PyMem_Free(buf2); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10441 | |
| 10442 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10443 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10444 | return result; |
| 10445 | } |
| 10446 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10447 | PyDoc_STRVAR(encode__doc__, |
Victor Stinner | c911bbf | 2010-11-07 19:04:46 +0000 | [diff] [blame] | 10448 | "S.encode(encoding='utf-8', errors='strict') -> bytes\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10449 | \n\ |
Victor Stinner | e14e212 | 2010-11-07 18:41:46 +0000 | [diff] [blame] | 10450 | Encode S using the codec registered for encoding. Default encoding\n\ |
| 10451 | 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] | 10452 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 10453 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 10454 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 10455 | codecs.register_error that can handle UnicodeEncodeErrors."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10456 | |
| 10457 | static PyObject * |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 10458 | unicode_encode(PyUnicodeObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10459 | { |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 10460 | static char *kwlist[] = {"encoding", "errors", 0}; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10461 | char *encoding = NULL; |
| 10462 | char *errors = NULL; |
Guido van Rossum | 35d9428 | 2007-08-27 18:20:11 +0000 | [diff] [blame] | 10463 | |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 10464 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:encode", |
| 10465 | kwlist, &encoding, &errors)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10466 | return NULL; |
Georg Brandl | 3b9406b | 2010-12-03 07:54:09 +0000 | [diff] [blame] | 10467 | return PyUnicode_AsEncodedString((PyObject *)self, encoding, errors); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 10468 | } |
| 10469 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10470 | PyDoc_STRVAR(expandtabs__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10471 | "S.expandtabs([tabsize]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10472 | \n\ |
| 10473 | 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] | 10474 | 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] | 10475 | |
| 10476 | static PyObject* |
| 10477 | unicode_expandtabs(PyUnicodeObject *self, PyObject *args) |
| 10478 | { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10479 | Py_ssize_t i, j, line_pos, src_len, incr; |
| 10480 | Py_UCS4 ch; |
| 10481 | PyObject *u; |
| 10482 | void *src_data, *dest_data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10483 | int tabsize = 8; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10484 | int kind; |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 10485 | int found; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10486 | |
| 10487 | if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10488 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10489 | |
Antoine Pitrou | 2242522 | 2011-10-04 19:10:51 +0200 | [diff] [blame] | 10490 | if (PyUnicode_READY(self) == -1) |
| 10491 | return NULL; |
| 10492 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 10493 | /* First pass: determine size of output string */ |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10494 | src_len = PyUnicode_GET_LENGTH(self); |
| 10495 | i = j = line_pos = 0; |
| 10496 | kind = PyUnicode_KIND(self); |
| 10497 | src_data = PyUnicode_DATA(self); |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 10498 | found = 0; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10499 | for (; i < src_len; i++) { |
| 10500 | ch = PyUnicode_READ(kind, src_data, i); |
| 10501 | if (ch == '\t') { |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 10502 | found = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10503 | if (tabsize > 0) { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10504 | incr = tabsize - (line_pos % tabsize); /* cannot overflow */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10505 | if (j > PY_SSIZE_T_MAX - incr) |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10506 | goto overflow; |
| 10507 | line_pos += incr; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10508 | j += incr; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10509 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10510 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10511 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10512 | if (j > PY_SSIZE_T_MAX - 1) |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10513 | goto overflow; |
| 10514 | line_pos++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10515 | j++; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10516 | if (ch == '\n' || ch == '\r') |
| 10517 | line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10518 | } |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10519 | } |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 10520 | if (!found && PyUnicode_CheckExact(self)) { |
| 10521 | Py_INCREF((PyObject *) self); |
| 10522 | return (PyObject *) self; |
| 10523 | } |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 10524 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10525 | /* Second pass: create output string and fill it */ |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10526 | u = PyUnicode_New(j, PyUnicode_MAX_CHAR_VALUE(self)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10527 | if (!u) |
| 10528 | return NULL; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10529 | dest_data = PyUnicode_DATA(u); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10530 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10531 | i = j = line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10532 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10533 | for (; i < src_len; i++) { |
| 10534 | ch = PyUnicode_READ(kind, src_data, i); |
| 10535 | if (ch == '\t') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10536 | if (tabsize > 0) { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10537 | incr = tabsize - (line_pos % tabsize); |
| 10538 | line_pos += incr; |
| 10539 | while (incr--) { |
| 10540 | PyUnicode_WRITE(kind, dest_data, j, ' '); |
| 10541 | j++; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10542 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10543 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10544 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10545 | else { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10546 | line_pos++; |
| 10547 | PyUnicode_WRITE(kind, dest_data, j, ch); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10548 | j++; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10549 | if (ch == '\n' || ch == '\r') |
| 10550 | line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10551 | } |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10552 | } |
| 10553 | assert (j == PyUnicode_GET_LENGTH(u)); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 10554 | #ifndef DONT_MAKE_RESULT_READY |
| 10555 | if (_PyUnicode_READY_REPLACE(&u)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10556 | Py_DECREF(u); |
| 10557 | return NULL; |
| 10558 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 10559 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10560 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10561 | return (PyObject*) u; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10562 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10563 | overflow: |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10564 | PyErr_SetString(PyExc_OverflowError, "new string is too long"); |
| 10565 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10566 | } |
| 10567 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10568 | PyDoc_STRVAR(find__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10569 | "S.find(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10570 | \n\ |
| 10571 | Return the lowest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 10572 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10573 | arguments start and end are interpreted as in slice notation.\n\ |
| 10574 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10575 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10576 | |
| 10577 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10578 | unicode_find(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10579 | { |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10580 | PyUnicodeObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 10581 | Py_ssize_t start; |
| 10582 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10583 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10584 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10585 | if (!stringlib_parse_args_finds_unicode("find", args, &substring, |
| 10586 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10587 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10588 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10589 | if (PyUnicode_READY(self) == -1) |
| 10590 | return NULL; |
| 10591 | if (PyUnicode_READY(substring) == -1) |
| 10592 | return NULL; |
| 10593 | |
| 10594 | result = any_find_slice( |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10595 | asciilib_find_slice, ucs1lib_find_slice, |
| 10596 | ucs2lib_find_slice, ucs4lib_find_slice, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10597 | self, (PyObject*)substring, start, end |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10598 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10599 | |
| 10600 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10601 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10602 | if (result == -2) |
| 10603 | return NULL; |
| 10604 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 10605 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10606 | } |
| 10607 | |
| 10608 | static PyObject * |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 10609 | unicode_getitem(PyObject *self, Py_ssize_t index) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10610 | { |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 10611 | Py_UCS4 ch = PyUnicode_ReadChar(self, index); |
| 10612 | if (ch == (Py_UCS4)-1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10613 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10614 | return PyUnicode_FromOrdinal(ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10615 | } |
| 10616 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 10617 | /* Believe it or not, this produces the same value for ASCII strings |
Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 10618 | as bytes_hash(). */ |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 10619 | static Py_hash_t |
Neil Schemenauer | f8c37d1 | 2007-09-07 20:49:04 +0000 | [diff] [blame] | 10620 | unicode_hash(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10621 | { |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 10622 | Py_ssize_t len; |
Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 10623 | Py_uhash_t x; |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 10624 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10625 | if (_PyUnicode_HASH(self) != -1) |
| 10626 | return _PyUnicode_HASH(self); |
| 10627 | if (PyUnicode_READY(self) == -1) |
| 10628 | return -1; |
| 10629 | len = PyUnicode_GET_LENGTH(self); |
| 10630 | |
| 10631 | /* The hash function as a macro, gets expanded three times below. */ |
| 10632 | #define HASH(P) \ |
| 10633 | x = (Py_uhash_t)*P << 7; \ |
| 10634 | while (--len >= 0) \ |
| 10635 | x = (1000003*x) ^ (Py_uhash_t)*P++; |
| 10636 | |
| 10637 | switch (PyUnicode_KIND(self)) { |
| 10638 | case PyUnicode_1BYTE_KIND: { |
| 10639 | const unsigned char *c = PyUnicode_1BYTE_DATA(self); |
| 10640 | HASH(c); |
| 10641 | break; |
| 10642 | } |
| 10643 | case PyUnicode_2BYTE_KIND: { |
| 10644 | const Py_UCS2 *s = PyUnicode_2BYTE_DATA(self); |
| 10645 | HASH(s); |
| 10646 | break; |
| 10647 | } |
| 10648 | default: { |
| 10649 | Py_UCS4 *l; |
| 10650 | assert(PyUnicode_KIND(self) == PyUnicode_4BYTE_KIND && |
| 10651 | "Impossible switch case in unicode_hash"); |
| 10652 | l = PyUnicode_4BYTE_DATA(self); |
| 10653 | HASH(l); |
| 10654 | break; |
| 10655 | } |
| 10656 | } |
| 10657 | x ^= (Py_uhash_t)PyUnicode_GET_LENGTH(self); |
| 10658 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 10659 | if (x == -1) |
| 10660 | x = -2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10661 | _PyUnicode_HASH(self) = x; |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 10662 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10663 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10664 | #undef HASH |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10665 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10666 | PyDoc_STRVAR(index__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10667 | "S.index(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10668 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10669 | 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] | 10670 | |
| 10671 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10672 | unicode_index(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10673 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10674 | Py_ssize_t result; |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10675 | PyUnicodeObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 10676 | Py_ssize_t start; |
| 10677 | Py_ssize_t end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10678 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10679 | if (!stringlib_parse_args_finds_unicode("index", args, &substring, |
| 10680 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10681 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10682 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10683 | if (PyUnicode_READY(self) == -1) |
| 10684 | return NULL; |
| 10685 | if (PyUnicode_READY(substring) == -1) |
| 10686 | return NULL; |
| 10687 | |
| 10688 | result = any_find_slice( |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10689 | asciilib_find_slice, ucs1lib_find_slice, |
| 10690 | ucs2lib_find_slice, ucs4lib_find_slice, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10691 | self, (PyObject*)substring, start, end |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10692 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10693 | |
| 10694 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10695 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10696 | if (result == -2) |
| 10697 | return NULL; |
| 10698 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10699 | if (result < 0) { |
| 10700 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 10701 | return NULL; |
| 10702 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10703 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 10704 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10705 | } |
| 10706 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10707 | PyDoc_STRVAR(islower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10708 | "S.islower() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10709 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10710 | 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] | 10711 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10712 | |
| 10713 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10714 | unicode_islower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10715 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10716 | Py_ssize_t i, length; |
| 10717 | int kind; |
| 10718 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10719 | int cased; |
| 10720 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10721 | if (PyUnicode_READY(self) == -1) |
| 10722 | return NULL; |
| 10723 | length = PyUnicode_GET_LENGTH(self); |
| 10724 | kind = PyUnicode_KIND(self); |
| 10725 | data = PyUnicode_DATA(self); |
| 10726 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10727 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10728 | if (length == 1) |
| 10729 | return PyBool_FromLong( |
| 10730 | Py_UNICODE_ISLOWER(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10731 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10732 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10733 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10734 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10735 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10736 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10737 | for (i = 0; i < length; i++) { |
| 10738 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 10739 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10740 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 10741 | return PyBool_FromLong(0); |
| 10742 | else if (!cased && Py_UNICODE_ISLOWER(ch)) |
| 10743 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10744 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10745 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10746 | } |
| 10747 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10748 | PyDoc_STRVAR(isupper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10749 | "S.isupper() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10750 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 10751 | 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] | 10752 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10753 | |
| 10754 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10755 | unicode_isupper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10756 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10757 | Py_ssize_t i, length; |
| 10758 | int kind; |
| 10759 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10760 | int cased; |
| 10761 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10762 | if (PyUnicode_READY(self) == -1) |
| 10763 | return NULL; |
| 10764 | length = PyUnicode_GET_LENGTH(self); |
| 10765 | kind = PyUnicode_KIND(self); |
| 10766 | data = PyUnicode_DATA(self); |
| 10767 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10768 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10769 | if (length == 1) |
| 10770 | return PyBool_FromLong( |
| 10771 | Py_UNICODE_ISUPPER(PyUnicode_READ(kind, data, 0)) != 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10772 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10773 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10774 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10775 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10776 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10777 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10778 | for (i = 0; i < length; i++) { |
| 10779 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 10780 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10781 | if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 10782 | return PyBool_FromLong(0); |
| 10783 | else if (!cased && Py_UNICODE_ISUPPER(ch)) |
| 10784 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10785 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10786 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10787 | } |
| 10788 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10789 | PyDoc_STRVAR(istitle__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10790 | "S.istitle() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10791 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 10792 | Return True if S is a titlecased string and there is at least one\n\ |
| 10793 | character in S, i.e. upper- and titlecase characters may only\n\ |
| 10794 | follow uncased characters and lowercase characters only cased ones.\n\ |
| 10795 | Return False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10796 | |
| 10797 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10798 | unicode_istitle(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10799 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10800 | Py_ssize_t i, length; |
| 10801 | int kind; |
| 10802 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10803 | int cased, previous_is_cased; |
| 10804 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10805 | if (PyUnicode_READY(self) == -1) |
| 10806 | return NULL; |
| 10807 | length = PyUnicode_GET_LENGTH(self); |
| 10808 | kind = PyUnicode_KIND(self); |
| 10809 | data = PyUnicode_DATA(self); |
| 10810 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10811 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10812 | if (length == 1) { |
| 10813 | Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 10814 | return PyBool_FromLong((Py_UNICODE_ISTITLE(ch) != 0) || |
| 10815 | (Py_UNICODE_ISUPPER(ch) != 0)); |
| 10816 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10817 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10818 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10819 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10820 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10821 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10822 | cased = 0; |
| 10823 | previous_is_cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10824 | for (i = 0; i < length; i++) { |
| 10825 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 10826 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10827 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { |
| 10828 | if (previous_is_cased) |
| 10829 | return PyBool_FromLong(0); |
| 10830 | previous_is_cased = 1; |
| 10831 | cased = 1; |
| 10832 | } |
| 10833 | else if (Py_UNICODE_ISLOWER(ch)) { |
| 10834 | if (!previous_is_cased) |
| 10835 | return PyBool_FromLong(0); |
| 10836 | previous_is_cased = 1; |
| 10837 | cased = 1; |
| 10838 | } |
| 10839 | else |
| 10840 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10841 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10842 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10843 | } |
| 10844 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10845 | PyDoc_STRVAR(isspace__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10846 | "S.isspace() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10847 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 10848 | Return True if all characters in S are whitespace\n\ |
| 10849 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10850 | |
| 10851 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10852 | unicode_isspace(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10853 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10854 | Py_ssize_t i, length; |
| 10855 | int kind; |
| 10856 | void *data; |
| 10857 | |
| 10858 | if (PyUnicode_READY(self) == -1) |
| 10859 | return NULL; |
| 10860 | length = PyUnicode_GET_LENGTH(self); |
| 10861 | kind = PyUnicode_KIND(self); |
| 10862 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10863 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10864 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10865 | if (length == 1) |
| 10866 | return PyBool_FromLong( |
| 10867 | Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10868 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10869 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10870 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10871 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10872 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10873 | for (i = 0; i < length; i++) { |
| 10874 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 10875 | if (!Py_UNICODE_ISSPACE(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10876 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10877 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10878 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10879 | } |
| 10880 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10881 | PyDoc_STRVAR(isalpha__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10882 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10883 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 10884 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10885 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10886 | |
| 10887 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10888 | unicode_isalpha(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10889 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10890 | Py_ssize_t i, length; |
| 10891 | int kind; |
| 10892 | void *data; |
| 10893 | |
| 10894 | if (PyUnicode_READY(self) == -1) |
| 10895 | return NULL; |
| 10896 | length = PyUnicode_GET_LENGTH(self); |
| 10897 | kind = PyUnicode_KIND(self); |
| 10898 | data = PyUnicode_DATA(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10899 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10900 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10901 | if (length == 1) |
| 10902 | return PyBool_FromLong( |
| 10903 | Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, 0))); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10904 | |
| 10905 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10906 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10907 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10908 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10909 | for (i = 0; i < length; i++) { |
| 10910 | if (!Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10911 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10912 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10913 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10914 | } |
| 10915 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10916 | PyDoc_STRVAR(isalnum__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10917 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10918 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 10919 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10920 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10921 | |
| 10922 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10923 | unicode_isalnum(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10924 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10925 | int kind; |
| 10926 | void *data; |
| 10927 | Py_ssize_t len, i; |
| 10928 | |
| 10929 | if (PyUnicode_READY(self) == -1) |
| 10930 | return NULL; |
| 10931 | |
| 10932 | kind = PyUnicode_KIND(self); |
| 10933 | data = PyUnicode_DATA(self); |
| 10934 | len = PyUnicode_GET_LENGTH(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10935 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10936 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10937 | if (len == 1) { |
| 10938 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 10939 | return PyBool_FromLong(Py_UNICODE_ISALNUM(ch)); |
| 10940 | } |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10941 | |
| 10942 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10943 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10944 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10945 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10946 | for (i = 0; i < len; i++) { |
| 10947 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 10948 | if (!Py_UNICODE_ISALNUM(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10949 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10950 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10951 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10952 | } |
| 10953 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10954 | PyDoc_STRVAR(isdecimal__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10955 | "S.isdecimal() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10956 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10957 | 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] | 10958 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10959 | |
| 10960 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10961 | unicode_isdecimal(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10962 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10963 | Py_ssize_t i, length; |
| 10964 | int kind; |
| 10965 | void *data; |
| 10966 | |
| 10967 | if (PyUnicode_READY(self) == -1) |
| 10968 | return NULL; |
| 10969 | length = PyUnicode_GET_LENGTH(self); |
| 10970 | kind = PyUnicode_KIND(self); |
| 10971 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10972 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10973 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10974 | if (length == 1) |
| 10975 | return PyBool_FromLong( |
| 10976 | Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10977 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10978 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10979 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10980 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10981 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10982 | for (i = 0; i < length; i++) { |
| 10983 | if (!Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10984 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10985 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10986 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10987 | } |
| 10988 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10989 | PyDoc_STRVAR(isdigit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10990 | "S.isdigit() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10991 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 10992 | Return True if all characters in S are digits\n\ |
| 10993 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10994 | |
| 10995 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10996 | unicode_isdigit(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10997 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10998 | Py_ssize_t i, length; |
| 10999 | int kind; |
| 11000 | void *data; |
| 11001 | |
| 11002 | if (PyUnicode_READY(self) == -1) |
| 11003 | return NULL; |
| 11004 | length = PyUnicode_GET_LENGTH(self); |
| 11005 | kind = PyUnicode_KIND(self); |
| 11006 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11007 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11008 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11009 | if (length == 1) { |
| 11010 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11011 | return PyBool_FromLong(Py_UNICODE_ISDIGIT(ch)); |
| 11012 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11013 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11014 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11015 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11016 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11017 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11018 | for (i = 0; i < length; i++) { |
| 11019 | if (!Py_UNICODE_ISDIGIT(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11020 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11021 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11022 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11023 | } |
| 11024 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11025 | PyDoc_STRVAR(isnumeric__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11026 | "S.isnumeric() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11027 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11028 | 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] | 11029 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11030 | |
| 11031 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11032 | unicode_isnumeric(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11033 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11034 | Py_ssize_t i, length; |
| 11035 | int kind; |
| 11036 | void *data; |
| 11037 | |
| 11038 | if (PyUnicode_READY(self) == -1) |
| 11039 | return NULL; |
| 11040 | length = PyUnicode_GET_LENGTH(self); |
| 11041 | kind = PyUnicode_KIND(self); |
| 11042 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11043 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11044 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11045 | if (length == 1) |
| 11046 | return PyBool_FromLong( |
| 11047 | Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11048 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11049 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11050 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11051 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11052 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11053 | for (i = 0; i < length; i++) { |
| 11054 | if (!Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11055 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11056 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11057 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11058 | } |
| 11059 | |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11060 | int |
| 11061 | PyUnicode_IsIdentifier(PyObject *self) |
| 11062 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11063 | int kind; |
| 11064 | void *data; |
| 11065 | Py_ssize_t i; |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11066 | Py_UCS4 first; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11067 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11068 | if (PyUnicode_READY(self) == -1) { |
| 11069 | Py_FatalError("identifier not ready"); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11070 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11071 | } |
| 11072 | |
| 11073 | /* Special case for empty strings */ |
| 11074 | if (PyUnicode_GET_LENGTH(self) == 0) |
| 11075 | return 0; |
| 11076 | kind = PyUnicode_KIND(self); |
| 11077 | data = PyUnicode_DATA(self); |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11078 | |
| 11079 | /* PEP 3131 says that the first character must be in |
| 11080 | XID_Start and subsequent characters in XID_Continue, |
| 11081 | and for the ASCII range, the 2.x rules apply (i.e |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11082 | start with letters and underscore, continue with |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11083 | letters, digits, underscore). However, given the current |
| 11084 | definition of XID_Start and XID_Continue, it is sufficient |
| 11085 | to check just for these, except that _ must be allowed |
| 11086 | as starting an identifier. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11087 | first = PyUnicode_READ(kind, data, 0); |
Benjamin Peterson | f413b80 | 2011-08-12 22:17:18 -0500 | [diff] [blame] | 11088 | if (!_PyUnicode_IsXidStart(first) && first != 0x5F /* LOW LINE */) |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11089 | return 0; |
| 11090 | |
Benjamin Peterson | 9c6e6a0 | 2011-09-28 08:09:05 -0400 | [diff] [blame] | 11091 | for (i = 1; i < PyUnicode_GET_LENGTH(self); i++) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11092 | if (!_PyUnicode_IsXidContinue(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11093 | return 0; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11094 | return 1; |
| 11095 | } |
| 11096 | |
| 11097 | PyDoc_STRVAR(isidentifier__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11098 | "S.isidentifier() -> bool\n\ |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11099 | \n\ |
| 11100 | Return True if S is a valid identifier according\n\ |
| 11101 | to the language definition."); |
| 11102 | |
| 11103 | static PyObject* |
| 11104 | unicode_isidentifier(PyObject *self) |
| 11105 | { |
| 11106 | return PyBool_FromLong(PyUnicode_IsIdentifier(self)); |
| 11107 | } |
| 11108 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11109 | PyDoc_STRVAR(isprintable__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11110 | "S.isprintable() -> bool\n\ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11111 | \n\ |
| 11112 | Return True if all characters in S are considered\n\ |
| 11113 | printable in repr() or S is empty, False otherwise."); |
| 11114 | |
| 11115 | static PyObject* |
| 11116 | unicode_isprintable(PyObject *self) |
| 11117 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11118 | Py_ssize_t i, length; |
| 11119 | int kind; |
| 11120 | void *data; |
| 11121 | |
| 11122 | if (PyUnicode_READY(self) == -1) |
| 11123 | return NULL; |
| 11124 | length = PyUnicode_GET_LENGTH(self); |
| 11125 | kind = PyUnicode_KIND(self); |
| 11126 | data = PyUnicode_DATA(self); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11127 | |
| 11128 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11129 | if (length == 1) |
| 11130 | return PyBool_FromLong( |
| 11131 | Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, 0))); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11132 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11133 | for (i = 0; i < length; i++) { |
| 11134 | if (!Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, i))) { |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11135 | Py_RETURN_FALSE; |
| 11136 | } |
| 11137 | } |
| 11138 | Py_RETURN_TRUE; |
| 11139 | } |
| 11140 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11141 | PyDoc_STRVAR(join__doc__, |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 11142 | "S.join(iterable) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11143 | \n\ |
| 11144 | 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] | 11145 | iterable. The separator between elements is S."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11146 | |
| 11147 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11148 | unicode_join(PyObject *self, PyObject *data) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11149 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11150 | return PyUnicode_Join(self, data); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11151 | } |
| 11152 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11153 | static Py_ssize_t |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11154 | unicode_length(PyUnicodeObject *self) |
| 11155 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11156 | if (PyUnicode_READY(self) == -1) |
| 11157 | return -1; |
| 11158 | return PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11159 | } |
| 11160 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11161 | PyDoc_STRVAR(ljust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11162 | "S.ljust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11163 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 11164 | 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] | 11165 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11166 | |
| 11167 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11168 | unicode_ljust(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11169 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11170 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11171 | Py_UCS4 fillchar = ' '; |
| 11172 | |
| 11173 | if (PyUnicode_READY(self) == -1) |
| 11174 | return NULL; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 11175 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11176 | if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11177 | return NULL; |
| 11178 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11179 | if (_PyUnicode_LENGTH(self) >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11180 | Py_INCREF(self); |
| 11181 | return (PyObject*) self; |
| 11182 | } |
| 11183 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11184 | return (PyObject*) pad(self, 0, width - _PyUnicode_LENGTH(self), fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11185 | } |
| 11186 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11187 | PyDoc_STRVAR(lower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11188 | "S.lower() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11189 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11190 | Return a copy of the string S converted to lowercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11191 | |
| 11192 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11193 | unicode_lower(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11194 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11195 | return fixup(self, fixlower); |
| 11196 | } |
| 11197 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11198 | #define LEFTSTRIP 0 |
| 11199 | #define RIGHTSTRIP 1 |
| 11200 | #define BOTHSTRIP 2 |
| 11201 | |
| 11202 | /* Arrays indexed by above */ |
| 11203 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 11204 | |
| 11205 | #define STRIPNAME(i) (stripformat[i]+3) |
| 11206 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11207 | /* externally visible for str.strip(unicode) */ |
| 11208 | PyObject * |
| 11209 | _PyUnicode_XStrip(PyUnicodeObject *self, int striptype, PyObject *sepobj) |
| 11210 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11211 | void *data; |
| 11212 | int kind; |
| 11213 | Py_ssize_t i, j, len; |
| 11214 | BLOOM_MASK sepmask; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11215 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11216 | if (PyUnicode_READY(self) == -1 || PyUnicode_READY(sepobj) == -1) |
| 11217 | return NULL; |
| 11218 | |
| 11219 | kind = PyUnicode_KIND(self); |
| 11220 | data = PyUnicode_DATA(self); |
| 11221 | len = PyUnicode_GET_LENGTH(self); |
| 11222 | sepmask = make_bloom_mask(PyUnicode_KIND(sepobj), |
| 11223 | PyUnicode_DATA(sepobj), |
| 11224 | PyUnicode_GET_LENGTH(sepobj)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11225 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11226 | i = 0; |
| 11227 | if (striptype != RIGHTSTRIP) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11228 | while (i < len && |
| 11229 | BLOOM_MEMBER(sepmask, PyUnicode_READ(kind, data, i), sepobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11230 | i++; |
| 11231 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11232 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11233 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11234 | j = len; |
| 11235 | if (striptype != LEFTSTRIP) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11236 | do { |
| 11237 | j--; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11238 | } while (j >= i && |
| 11239 | BLOOM_MEMBER(sepmask, PyUnicode_READ(kind, data, j), sepobj)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11240 | j++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11241 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11242 | |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11243 | return PyUnicode_Substring((PyObject*)self, i, j); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11244 | } |
| 11245 | |
| 11246 | PyObject* |
| 11247 | PyUnicode_Substring(PyObject *self, Py_ssize_t start, Py_ssize_t end) |
| 11248 | { |
| 11249 | unsigned char *data; |
| 11250 | int kind; |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11251 | Py_ssize_t length; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11252 | |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 11253 | if (PyUnicode_READY(self) == -1) |
| 11254 | return NULL; |
| 11255 | |
| 11256 | end = Py_MIN(end, PyUnicode_GET_LENGTH(self)); |
| 11257 | |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11258 | if (start == 0 && end == PyUnicode_GET_LENGTH(self)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11259 | { |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11260 | if (PyUnicode_CheckExact(self)) { |
| 11261 | Py_INCREF(self); |
| 11262 | return self; |
| 11263 | } |
| 11264 | else |
| 11265 | return PyUnicode_Copy(self); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11266 | } |
| 11267 | |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11268 | length = end - start; |
| 11269 | if (length == 1) |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 11270 | return unicode_getitem(self, start); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11271 | |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 11272 | if (start < 0 || end < 0) { |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11273 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 11274 | return NULL; |
| 11275 | } |
| 11276 | |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 11277 | if (PyUnicode_IS_ASCII(self)) { |
| 11278 | kind = PyUnicode_KIND(self); |
| 11279 | data = PyUnicode_1BYTE_DATA(self); |
| 11280 | return unicode_fromascii(data + start, length); |
| 11281 | } |
| 11282 | else { |
| 11283 | kind = PyUnicode_KIND(self); |
| 11284 | data = PyUnicode_1BYTE_DATA(self); |
| 11285 | return PyUnicode_FromKindAndData(kind, |
| 11286 | data + PyUnicode_KIND_SIZE(kind, start), |
| 11287 | length); |
| 11288 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11289 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11290 | |
| 11291 | static PyObject * |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11292 | do_strip(PyUnicodeObject *self, int striptype) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11293 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11294 | int kind; |
| 11295 | void *data; |
| 11296 | Py_ssize_t len, i, j; |
| 11297 | |
| 11298 | if (PyUnicode_READY(self) == -1) |
| 11299 | return NULL; |
| 11300 | |
| 11301 | kind = PyUnicode_KIND(self); |
| 11302 | data = PyUnicode_DATA(self); |
| 11303 | len = PyUnicode_GET_LENGTH(self); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11304 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11305 | i = 0; |
| 11306 | if (striptype != RIGHTSTRIP) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11307 | while (i < len && Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, i))) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11308 | i++; |
| 11309 | } |
| 11310 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11311 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11312 | j = len; |
| 11313 | if (striptype != LEFTSTRIP) { |
| 11314 | do { |
| 11315 | j--; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11316 | } while (j >= i && Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, j))); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11317 | j++; |
| 11318 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11319 | |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11320 | return PyUnicode_Substring((PyObject*)self, i, j); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11321 | } |
| 11322 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11323 | |
| 11324 | static PyObject * |
| 11325 | do_argstrip(PyUnicodeObject *self, int striptype, PyObject *args) |
| 11326 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11327 | PyObject *sep = NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11328 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11329 | if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) |
| 11330 | return NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11331 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11332 | if (sep != NULL && sep != Py_None) { |
| 11333 | if (PyUnicode_Check(sep)) |
| 11334 | return _PyUnicode_XStrip(self, striptype, sep); |
| 11335 | else { |
| 11336 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11337 | "%s arg must be None or str", |
| 11338 | STRIPNAME(striptype)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11339 | return NULL; |
| 11340 | } |
| 11341 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11342 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11343 | return do_strip(self, striptype); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11344 | } |
| 11345 | |
| 11346 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11347 | PyDoc_STRVAR(strip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11348 | "S.strip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11349 | \n\ |
| 11350 | Return a copy of the string S with leading and trailing\n\ |
| 11351 | whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11352 | 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] | 11353 | |
| 11354 | static PyObject * |
| 11355 | unicode_strip(PyUnicodeObject *self, PyObject *args) |
| 11356 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11357 | if (PyTuple_GET_SIZE(args) == 0) |
| 11358 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 11359 | else |
| 11360 | return do_argstrip(self, BOTHSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11361 | } |
| 11362 | |
| 11363 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11364 | PyDoc_STRVAR(lstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11365 | "S.lstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11366 | \n\ |
| 11367 | Return a copy of the string S with leading whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11368 | 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] | 11369 | |
| 11370 | static PyObject * |
| 11371 | unicode_lstrip(PyUnicodeObject *self, PyObject *args) |
| 11372 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11373 | if (PyTuple_GET_SIZE(args) == 0) |
| 11374 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 11375 | else |
| 11376 | return do_argstrip(self, LEFTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11377 | } |
| 11378 | |
| 11379 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11380 | PyDoc_STRVAR(rstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11381 | "S.rstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11382 | \n\ |
| 11383 | Return a copy of the string S with trailing whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11384 | 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] | 11385 | |
| 11386 | static PyObject * |
| 11387 | unicode_rstrip(PyUnicodeObject *self, PyObject *args) |
| 11388 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11389 | if (PyTuple_GET_SIZE(args) == 0) |
| 11390 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 11391 | else |
| 11392 | return do_argstrip(self, RIGHTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11393 | } |
| 11394 | |
| 11395 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11396 | static PyObject* |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11397 | unicode_repeat(PyUnicodeObject *str, Py_ssize_t len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11398 | { |
| 11399 | PyUnicodeObject *u; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11400 | Py_ssize_t nchars, n; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11401 | |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 11402 | if (len < 1) { |
| 11403 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 11404 | return unicode_empty; |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 11405 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11406 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 11407 | if (len == 1 && PyUnicode_CheckExact(str)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11408 | /* no repeat, return original string */ |
| 11409 | Py_INCREF(str); |
| 11410 | return (PyObject*) str; |
| 11411 | } |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 11412 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11413 | if (PyUnicode_READY(str) == -1) |
| 11414 | return NULL; |
| 11415 | |
Victor Stinner | c759f3e | 2011-10-01 03:09:58 +0200 | [diff] [blame] | 11416 | if (PyUnicode_GET_LENGTH(str) > PY_SSIZE_T_MAX / len) { |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11417 | PyErr_SetString(PyExc_OverflowError, |
| 11418 | "repeated string is too long"); |
| 11419 | return NULL; |
| 11420 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11421 | nchars = len * PyUnicode_GET_LENGTH(str); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11422 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11423 | u = (PyUnicodeObject *)PyUnicode_New(nchars, PyUnicode_MAX_CHAR_VALUE(str)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11424 | if (!u) |
| 11425 | return NULL; |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11426 | assert(PyUnicode_KIND(u) == PyUnicode_KIND(str)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11427 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11428 | if (PyUnicode_GET_LENGTH(str) == 1) { |
| 11429 | const int kind = PyUnicode_KIND(str); |
| 11430 | const Py_UCS4 fill_char = PyUnicode_READ(kind, PyUnicode_DATA(str), 0); |
| 11431 | void *to = PyUnicode_DATA(u); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11432 | if (kind == PyUnicode_1BYTE_KIND) |
| 11433 | memset(to, (unsigned char)fill_char, len); |
| 11434 | else { |
| 11435 | for (n = 0; n < len; ++n) |
| 11436 | PyUnicode_WRITE(kind, to, n, fill_char); |
| 11437 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11438 | } |
| 11439 | else { |
| 11440 | /* number of characters copied this far */ |
| 11441 | Py_ssize_t done = PyUnicode_GET_LENGTH(str); |
| 11442 | const Py_ssize_t char_size = PyUnicode_CHARACTER_SIZE(str); |
| 11443 | char *to = (char *) PyUnicode_DATA(u); |
| 11444 | Py_MEMCPY(to, PyUnicode_DATA(str), |
| 11445 | PyUnicode_GET_LENGTH(str) * char_size); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11446 | while (done < nchars) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11447 | n = (done <= nchars-done) ? done : nchars-done; |
| 11448 | Py_MEMCPY(to + (done * char_size), to, n * char_size); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11449 | done += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11450 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11451 | } |
| 11452 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 11453 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11454 | return (PyObject*) u; |
| 11455 | } |
| 11456 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 11457 | PyObject * |
| 11458 | PyUnicode_Replace(PyObject *obj, |
| 11459 | PyObject *subobj, |
| 11460 | PyObject *replobj, |
| 11461 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11462 | { |
| 11463 | PyObject *self; |
| 11464 | PyObject *str1; |
| 11465 | PyObject *str2; |
| 11466 | PyObject *result; |
| 11467 | |
| 11468 | self = PyUnicode_FromObject(obj); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11469 | if (self == NULL || PyUnicode_READY(self) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11470 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11471 | str1 = PyUnicode_FromObject(subobj); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11472 | if (str1 == NULL || PyUnicode_READY(str1) == -1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11473 | Py_DECREF(self); |
| 11474 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11475 | } |
| 11476 | str2 = PyUnicode_FromObject(replobj); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11477 | if (str2 == NULL || PyUnicode_READY(str2)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11478 | Py_DECREF(self); |
| 11479 | Py_DECREF(str1); |
| 11480 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11481 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11482 | result = replace(self, str1, str2, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11483 | Py_DECREF(self); |
| 11484 | Py_DECREF(str1); |
| 11485 | Py_DECREF(str2); |
| 11486 | return result; |
| 11487 | } |
| 11488 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11489 | PyDoc_STRVAR(replace__doc__, |
Ezio Melotti | c1897e7 | 2010-06-26 18:50:39 +0000 | [diff] [blame] | 11490 | "S.replace(old, new[, count]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11491 | \n\ |
| 11492 | Return a copy of S with all occurrences of substring\n\ |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 11493 | old replaced by new. If the optional argument count is\n\ |
| 11494 | given, only the first count occurrences are replaced."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11495 | |
| 11496 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11497 | unicode_replace(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11498 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11499 | PyObject *str1; |
| 11500 | PyObject *str2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11501 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11502 | PyObject *result; |
| 11503 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11504 | if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11505 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11506 | if (!PyUnicode_READY(self) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11507 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11508 | str1 = PyUnicode_FromObject(str1); |
| 11509 | if (str1 == NULL || PyUnicode_READY(str1) == -1) |
| 11510 | return NULL; |
| 11511 | str2 = PyUnicode_FromObject(str2); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11512 | if (str2 == NULL || PyUnicode_READY(str2) == -1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11513 | Py_DECREF(str1); |
| 11514 | return NULL; |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 11515 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11516 | |
| 11517 | result = replace(self, str1, str2, maxcount); |
| 11518 | |
| 11519 | Py_DECREF(str1); |
| 11520 | Py_DECREF(str2); |
| 11521 | return result; |
| 11522 | } |
| 11523 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 11524 | static PyObject * |
| 11525 | unicode_repr(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11526 | { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11527 | PyObject *repr; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11528 | Py_ssize_t isize; |
| 11529 | Py_ssize_t osize, squote, dquote, i, o; |
| 11530 | Py_UCS4 max, quote; |
| 11531 | int ikind, okind; |
| 11532 | void *idata, *odata; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11533 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11534 | if (PyUnicode_READY(unicode) == -1) |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11535 | return NULL; |
| 11536 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11537 | isize = PyUnicode_GET_LENGTH(unicode); |
| 11538 | idata = PyUnicode_DATA(unicode); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11539 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11540 | /* Compute length of output, quote characters, and |
| 11541 | maximum character */ |
| 11542 | osize = 2; /* quotes */ |
| 11543 | max = 127; |
| 11544 | squote = dquote = 0; |
| 11545 | ikind = PyUnicode_KIND(unicode); |
| 11546 | for (i = 0; i < isize; i++) { |
| 11547 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
| 11548 | switch (ch) { |
| 11549 | case '\'': squote++; osize++; break; |
| 11550 | case '"': dquote++; osize++; break; |
| 11551 | case '\\': case '\t': case '\r': case '\n': |
| 11552 | osize += 2; break; |
| 11553 | default: |
| 11554 | /* Fast-path ASCII */ |
| 11555 | if (ch < ' ' || ch == 0x7f) |
| 11556 | osize += 4; /* \xHH */ |
| 11557 | else if (ch < 0x7f) |
| 11558 | osize++; |
| 11559 | else if (Py_UNICODE_ISPRINTABLE(ch)) { |
| 11560 | osize++; |
| 11561 | max = ch > max ? ch : max; |
| 11562 | } |
| 11563 | else if (ch < 0x100) |
| 11564 | osize += 4; /* \xHH */ |
| 11565 | else if (ch < 0x10000) |
| 11566 | osize += 6; /* \uHHHH */ |
| 11567 | else |
| 11568 | osize += 10; /* \uHHHHHHHH */ |
| 11569 | } |
| 11570 | } |
| 11571 | |
| 11572 | quote = '\''; |
| 11573 | if (squote) { |
| 11574 | if (dquote) |
| 11575 | /* Both squote and dquote present. Use squote, |
| 11576 | and escape them */ |
| 11577 | osize += squote; |
| 11578 | else |
| 11579 | quote = '"'; |
| 11580 | } |
| 11581 | |
| 11582 | repr = PyUnicode_New(osize, max); |
| 11583 | if (repr == NULL) |
| 11584 | return NULL; |
| 11585 | okind = PyUnicode_KIND(repr); |
| 11586 | odata = PyUnicode_DATA(repr); |
| 11587 | |
| 11588 | PyUnicode_WRITE(okind, odata, 0, quote); |
| 11589 | PyUnicode_WRITE(okind, odata, osize-1, quote); |
| 11590 | |
| 11591 | for (i = 0, o = 1; i < isize; i++) { |
| 11592 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11593 | |
| 11594 | /* Escape quotes and backslashes */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11595 | if ((ch == quote) || (ch == '\\')) { |
| 11596 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 11597 | PyUnicode_WRITE(okind, odata, o++, ch); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11598 | continue; |
| 11599 | } |
| 11600 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11601 | /* Map special whitespace to '\t', \n', '\r' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11602 | if (ch == '\t') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11603 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 11604 | PyUnicode_WRITE(okind, odata, o++, 't'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11605 | } |
| 11606 | else if (ch == '\n') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11607 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 11608 | PyUnicode_WRITE(okind, odata, o++, 'n'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11609 | } |
| 11610 | else if (ch == '\r') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11611 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 11612 | PyUnicode_WRITE(okind, odata, o++, 'r'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11613 | } |
| 11614 | |
| 11615 | /* Map non-printable US ASCII to '\xhh' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11616 | else if (ch < ' ' || ch == 0x7F) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11617 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 11618 | PyUnicode_WRITE(okind, odata, o++, 'x'); |
| 11619 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 4) & 0x000F]); |
| 11620 | PyUnicode_WRITE(okind, odata, o++, hexdigits[ch & 0x000F]); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11621 | } |
| 11622 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11623 | /* Copy ASCII characters as-is */ |
| 11624 | else if (ch < 0x7F) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11625 | PyUnicode_WRITE(okind, odata, o++, ch); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11626 | } |
| 11627 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11628 | /* Non-ASCII characters */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11629 | else { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11630 | /* Map Unicode whitespace and control characters |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11631 | (categories Z* and C* except ASCII space) |
| 11632 | */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11633 | if (!Py_UNICODE_ISPRINTABLE(ch)) { |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11634 | /* Map 8-bit characters to '\xhh' */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11635 | if (ch <= 0xff) { |
| 11636 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 11637 | PyUnicode_WRITE(okind, odata, o++, 'x'); |
| 11638 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 4) & 0x000F]); |
| 11639 | PyUnicode_WRITE(okind, odata, o++, hexdigits[ch & 0x000F]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11640 | } |
| 11641 | /* Map 21-bit characters to '\U00xxxxxx' */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11642 | else if (ch >= 0x10000) { |
| 11643 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 11644 | PyUnicode_WRITE(okind, odata, o++, 'U'); |
| 11645 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 28) & 0xF]); |
| 11646 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 24) & 0xF]); |
| 11647 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 20) & 0xF]); |
| 11648 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 16) & 0xF]); |
| 11649 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 12) & 0xF]); |
| 11650 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 8) & 0xF]); |
| 11651 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 4) & 0xF]); |
| 11652 | PyUnicode_WRITE(okind, odata, o++, hexdigits[ch & 0xF]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11653 | } |
| 11654 | /* Map 16-bit characters to '\uxxxx' */ |
| 11655 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11656 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 11657 | PyUnicode_WRITE(okind, odata, o++, 'u'); |
| 11658 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 12) & 0xF]); |
| 11659 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 8) & 0xF]); |
| 11660 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 4) & 0xF]); |
| 11661 | PyUnicode_WRITE(okind, odata, o++, hexdigits[ch & 0xF]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11662 | } |
| 11663 | } |
| 11664 | /* Copy characters as-is */ |
| 11665 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11666 | PyUnicode_WRITE(okind, odata, o++, ch); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11667 | } |
| 11668 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11669 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11670 | /* Closing quote already added at the beginning */ |
Victor Stinner | 05d1189 | 2011-10-06 01:13:58 +0200 | [diff] [blame] | 11671 | assert(_PyUnicode_CheckConsistency(repr, 1)); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11672 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11673 | } |
| 11674 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11675 | PyDoc_STRVAR(rfind__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11676 | "S.rfind(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11677 | \n\ |
| 11678 | Return the highest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 11679 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11680 | arguments start and end are interpreted as in slice notation.\n\ |
| 11681 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11682 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11683 | |
| 11684 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11685 | unicode_rfind(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11686 | { |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11687 | PyUnicodeObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 11688 | Py_ssize_t start; |
| 11689 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11690 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11691 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11692 | if (!stringlib_parse_args_finds_unicode("rfind", args, &substring, |
| 11693 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11694 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11695 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11696 | if (PyUnicode_READY(self) == -1) |
| 11697 | return NULL; |
| 11698 | if (PyUnicode_READY(substring) == -1) |
| 11699 | return NULL; |
| 11700 | |
| 11701 | result = any_find_slice( |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 11702 | asciilib_rfind_slice, ucs1lib_rfind_slice, |
| 11703 | ucs2lib_rfind_slice, ucs4lib_rfind_slice, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11704 | self, (PyObject*)substring, start, end |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11705 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11706 | |
| 11707 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11708 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11709 | if (result == -2) |
| 11710 | return NULL; |
| 11711 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 11712 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11713 | } |
| 11714 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11715 | PyDoc_STRVAR(rindex__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11716 | "S.rindex(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11717 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11718 | 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] | 11719 | |
| 11720 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11721 | unicode_rindex(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11722 | { |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11723 | PyUnicodeObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 11724 | Py_ssize_t start; |
| 11725 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11726 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11727 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11728 | if (!stringlib_parse_args_finds_unicode("rindex", args, &substring, |
| 11729 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11730 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11731 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11732 | if (PyUnicode_READY(self) == -1) |
| 11733 | return NULL; |
| 11734 | if (PyUnicode_READY(substring) == -1) |
| 11735 | return NULL; |
| 11736 | |
| 11737 | result = any_find_slice( |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 11738 | asciilib_rfind_slice, ucs1lib_rfind_slice, |
| 11739 | ucs2lib_rfind_slice, ucs4lib_rfind_slice, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11740 | self, (PyObject*)substring, start, end |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11741 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11742 | |
| 11743 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11744 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11745 | if (result == -2) |
| 11746 | return NULL; |
| 11747 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11748 | if (result < 0) { |
| 11749 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 11750 | return NULL; |
| 11751 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11752 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 11753 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11754 | } |
| 11755 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11756 | PyDoc_STRVAR(rjust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11757 | "S.rjust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11758 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 11759 | 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] | 11760 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11761 | |
| 11762 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11763 | unicode_rjust(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11764 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11765 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11766 | Py_UCS4 fillchar = ' '; |
| 11767 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11768 | 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] | 11769 | return NULL; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 11770 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11771 | if (PyUnicode_READY(self) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11772 | return NULL; |
| 11773 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11774 | if (_PyUnicode_LENGTH(self) >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11775 | Py_INCREF(self); |
| 11776 | return (PyObject*) self; |
| 11777 | } |
| 11778 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11779 | return (PyObject*) pad(self, width - _PyUnicode_LENGTH(self), 0, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11780 | } |
| 11781 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 11782 | PyObject * |
| 11783 | PyUnicode_Split(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11784 | { |
| 11785 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11786 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11787 | s = PyUnicode_FromObject(s); |
| 11788 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11789 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11790 | if (sep != NULL) { |
| 11791 | sep = PyUnicode_FromObject(sep); |
| 11792 | if (sep == NULL) { |
| 11793 | Py_DECREF(s); |
| 11794 | return NULL; |
| 11795 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11796 | } |
| 11797 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11798 | result = split(s, sep, maxsplit); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11799 | |
| 11800 | Py_DECREF(s); |
| 11801 | Py_XDECREF(sep); |
| 11802 | return result; |
| 11803 | } |
| 11804 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11805 | PyDoc_STRVAR(split__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11806 | "S.split([sep[, maxsplit]]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11807 | \n\ |
| 11808 | Return a list of the words in S, using sep as the\n\ |
| 11809 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 11810 | 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] | 11811 | whitespace string is a separator and empty strings are\n\ |
| 11812 | removed from the result."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11813 | |
| 11814 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11815 | unicode_split(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11816 | { |
| 11817 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11818 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11819 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11820 | if (!PyArg_ParseTuple(args, "|On:split", &substring, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11821 | return NULL; |
| 11822 | |
| 11823 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11824 | return split(self, NULL, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11825 | else if (PyUnicode_Check(substring)) |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11826 | return split(self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11827 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11828 | return PyUnicode_Split((PyObject *)self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11829 | } |
| 11830 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11831 | PyObject * |
| 11832 | PyUnicode_Partition(PyObject *str_in, PyObject *sep_in) |
| 11833 | { |
| 11834 | PyObject* str_obj; |
| 11835 | PyObject* sep_obj; |
| 11836 | PyObject* out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11837 | int kind1, kind2, kind; |
| 11838 | void *buf1 = NULL, *buf2 = NULL; |
| 11839 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11840 | |
| 11841 | str_obj = PyUnicode_FromObject(str_in); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11842 | if (!str_obj || PyUnicode_READY(str_obj) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11843 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11844 | sep_obj = PyUnicode_FromObject(sep_in); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11845 | if (!sep_obj || PyUnicode_READY(sep_obj) == -1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11846 | Py_DECREF(str_obj); |
| 11847 | return NULL; |
| 11848 | } |
| 11849 | |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 11850 | kind1 = PyUnicode_KIND(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11851 | kind2 = PyUnicode_KIND(sep_obj); |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 11852 | kind = Py_MAX(kind1, kind2); |
| 11853 | buf1 = PyUnicode_DATA(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11854 | if (kind1 != kind) |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 11855 | buf1 = _PyUnicode_AsKind(str_obj, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11856 | if (!buf1) |
| 11857 | goto onError; |
| 11858 | buf2 = PyUnicode_DATA(sep_obj); |
| 11859 | if (kind2 != kind) |
| 11860 | buf2 = _PyUnicode_AsKind(sep_obj, kind); |
| 11861 | if (!buf2) |
| 11862 | goto onError; |
| 11863 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 11864 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
| 11865 | |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 11866 | switch(PyUnicode_KIND(str_obj)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11867 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 11868 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sep_obj)) |
| 11869 | out = asciilib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 11870 | else |
| 11871 | 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] | 11872 | break; |
| 11873 | case PyUnicode_2BYTE_KIND: |
| 11874 | out = ucs2lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 11875 | break; |
| 11876 | case PyUnicode_4BYTE_KIND: |
| 11877 | out = ucs4lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 11878 | break; |
| 11879 | default: |
| 11880 | assert(0); |
| 11881 | out = 0; |
| 11882 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11883 | |
| 11884 | Py_DECREF(sep_obj); |
| 11885 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11886 | if (kind1 != kind) |
| 11887 | PyMem_Free(buf1); |
| 11888 | if (kind2 != kind) |
| 11889 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11890 | |
| 11891 | return out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11892 | onError: |
| 11893 | Py_DECREF(sep_obj); |
| 11894 | Py_DECREF(str_obj); |
| 11895 | if (kind1 != kind && buf1) |
| 11896 | PyMem_Free(buf1); |
| 11897 | if (kind2 != kind && buf2) |
| 11898 | PyMem_Free(buf2); |
| 11899 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11900 | } |
| 11901 | |
| 11902 | |
| 11903 | PyObject * |
| 11904 | PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in) |
| 11905 | { |
| 11906 | PyObject* str_obj; |
| 11907 | PyObject* sep_obj; |
| 11908 | PyObject* out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11909 | int kind1, kind2, kind; |
| 11910 | void *buf1 = NULL, *buf2 = NULL; |
| 11911 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11912 | |
| 11913 | str_obj = PyUnicode_FromObject(str_in); |
| 11914 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11915 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11916 | sep_obj = PyUnicode_FromObject(sep_in); |
| 11917 | if (!sep_obj) { |
| 11918 | Py_DECREF(str_obj); |
| 11919 | return NULL; |
| 11920 | } |
| 11921 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11922 | kind1 = PyUnicode_KIND(str_in); |
| 11923 | kind2 = PyUnicode_KIND(sep_obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 11924 | kind = Py_MAX(kind1, kind2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11925 | buf1 = PyUnicode_DATA(str_in); |
| 11926 | if (kind1 != kind) |
| 11927 | buf1 = _PyUnicode_AsKind(str_in, kind); |
| 11928 | if (!buf1) |
| 11929 | goto onError; |
| 11930 | buf2 = PyUnicode_DATA(sep_obj); |
| 11931 | if (kind2 != kind) |
| 11932 | buf2 = _PyUnicode_AsKind(sep_obj, kind); |
| 11933 | if (!buf2) |
| 11934 | goto onError; |
| 11935 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 11936 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
| 11937 | |
| 11938 | switch(PyUnicode_KIND(str_in)) { |
| 11939 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 11940 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sep_obj)) |
| 11941 | out = asciilib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 11942 | else |
| 11943 | 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] | 11944 | break; |
| 11945 | case PyUnicode_2BYTE_KIND: |
| 11946 | out = ucs2lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 11947 | break; |
| 11948 | case PyUnicode_4BYTE_KIND: |
| 11949 | out = ucs4lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 11950 | break; |
| 11951 | default: |
| 11952 | assert(0); |
| 11953 | out = 0; |
| 11954 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11955 | |
| 11956 | Py_DECREF(sep_obj); |
| 11957 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11958 | if (kind1 != kind) |
| 11959 | PyMem_Free(buf1); |
| 11960 | if (kind2 != kind) |
| 11961 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11962 | |
| 11963 | return out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11964 | onError: |
| 11965 | Py_DECREF(sep_obj); |
| 11966 | Py_DECREF(str_obj); |
| 11967 | if (kind1 != kind && buf1) |
| 11968 | PyMem_Free(buf1); |
| 11969 | if (kind2 != kind && buf2) |
| 11970 | PyMem_Free(buf2); |
| 11971 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11972 | } |
| 11973 | |
| 11974 | PyDoc_STRVAR(partition__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11975 | "S.partition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11976 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 11977 | 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] | 11978 | 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] | 11979 | found, return S and two empty strings."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11980 | |
| 11981 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11982 | unicode_partition(PyObject *self, PyObject *separator) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11983 | { |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11984 | return PyUnicode_Partition(self, separator); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11985 | } |
| 11986 | |
| 11987 | PyDoc_STRVAR(rpartition__doc__, |
Ezio Melotti | 5b2b242 | 2010-01-25 11:58:28 +0000 | [diff] [blame] | 11988 | "S.rpartition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11989 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 11990 | 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] | 11991 | 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] | 11992 | separator is not found, return two empty strings and S."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11993 | |
| 11994 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11995 | unicode_rpartition(PyObject *self, PyObject *separator) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11996 | { |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11997 | return PyUnicode_RPartition(self, separator); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11998 | } |
| 11999 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12000 | PyObject * |
| 12001 | PyUnicode_RSplit(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12002 | { |
| 12003 | PyObject *result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12004 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12005 | s = PyUnicode_FromObject(s); |
| 12006 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12007 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12008 | if (sep != NULL) { |
| 12009 | sep = PyUnicode_FromObject(sep); |
| 12010 | if (sep == NULL) { |
| 12011 | Py_DECREF(s); |
| 12012 | return NULL; |
| 12013 | } |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12014 | } |
| 12015 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12016 | result = rsplit(s, sep, maxsplit); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12017 | |
| 12018 | Py_DECREF(s); |
| 12019 | Py_XDECREF(sep); |
| 12020 | return result; |
| 12021 | } |
| 12022 | |
| 12023 | PyDoc_STRVAR(rsplit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12024 | "S.rsplit([sep[, maxsplit]]) -> list of strings\n\ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12025 | \n\ |
| 12026 | Return a list of the words in S, using sep as the\n\ |
| 12027 | delimiter string, starting at the end of the string and\n\ |
| 12028 | working to the front. If maxsplit is given, at most maxsplit\n\ |
| 12029 | splits are done. If sep is not specified, any whitespace string\n\ |
| 12030 | is a separator."); |
| 12031 | |
| 12032 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12033 | unicode_rsplit(PyObject *self, PyObject *args) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12034 | { |
| 12035 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12036 | Py_ssize_t maxcount = -1; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12037 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12038 | if (!PyArg_ParseTuple(args, "|On:rsplit", &substring, &maxcount)) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12039 | return NULL; |
| 12040 | |
| 12041 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12042 | return rsplit(self, NULL, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12043 | else if (PyUnicode_Check(substring)) |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12044 | return rsplit(self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12045 | else |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12046 | return PyUnicode_RSplit(self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12047 | } |
| 12048 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12049 | PyDoc_STRVAR(splitlines__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12050 | "S.splitlines([keepends]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12051 | \n\ |
| 12052 | 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] | 12053 | 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] | 12054 | is given and true."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12055 | |
| 12056 | static PyObject* |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12057 | unicode_splitlines(PyUnicodeObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12058 | { |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12059 | static char *kwlist[] = {"keepends", 0}; |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 12060 | int keepends = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12061 | |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12062 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|i:splitlines", |
| 12063 | kwlist, &keepends)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12064 | return NULL; |
| 12065 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 12066 | return PyUnicode_Splitlines((PyObject *)self, keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12067 | } |
| 12068 | |
| 12069 | static |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 12070 | PyObject *unicode_str(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12071 | { |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 12072 | if (PyUnicode_CheckExact(self)) { |
| 12073 | Py_INCREF(self); |
| 12074 | return self; |
| 12075 | } else |
| 12076 | /* Subtype -- return genuine unicode string with the same value. */ |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 12077 | return PyUnicode_Copy(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12078 | } |
| 12079 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12080 | PyDoc_STRVAR(swapcase__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12081 | "S.swapcase() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12082 | \n\ |
| 12083 | 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] | 12084 | and vice versa."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12085 | |
| 12086 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12087 | unicode_swapcase(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12088 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12089 | return fixup(self, fixswapcase); |
| 12090 | } |
| 12091 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12092 | PyDoc_STRVAR(maketrans__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12093 | "str.maketrans(x[, y[, z]]) -> dict (static method)\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12094 | \n\ |
| 12095 | Return a translation table usable for str.translate().\n\ |
| 12096 | If there is only one argument, it must be a dictionary mapping Unicode\n\ |
| 12097 | ordinals (integers) or characters to Unicode ordinals, strings or None.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12098 | Character keys will be then converted to ordinals.\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12099 | If there are two arguments, they must be strings of equal length, and\n\ |
| 12100 | in the resulting dictionary, each character in x will be mapped to the\n\ |
| 12101 | character at the same position in y. If there is a third argument, it\n\ |
| 12102 | must be a string, whose characters will be mapped to None in the result."); |
| 12103 | |
| 12104 | static PyObject* |
| 12105 | unicode_maketrans(PyUnicodeObject *null, PyObject *args) |
| 12106 | { |
| 12107 | PyObject *x, *y = NULL, *z = NULL; |
| 12108 | PyObject *new = NULL, *key, *value; |
| 12109 | Py_ssize_t i = 0; |
| 12110 | int res; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12111 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12112 | if (!PyArg_ParseTuple(args, "O|UU:maketrans", &x, &y, &z)) |
| 12113 | return NULL; |
| 12114 | new = PyDict_New(); |
| 12115 | if (!new) |
| 12116 | return NULL; |
| 12117 | if (y != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12118 | int x_kind, y_kind, z_kind; |
| 12119 | void *x_data, *y_data, *z_data; |
| 12120 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12121 | /* x must be a string too, of equal length */ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12122 | if (!PyUnicode_Check(x)) { |
| 12123 | PyErr_SetString(PyExc_TypeError, "first maketrans argument must " |
| 12124 | "be a string if there is a second argument"); |
| 12125 | goto err; |
| 12126 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12127 | if (PyUnicode_GET_LENGTH(x) != PyUnicode_GET_LENGTH(y)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12128 | PyErr_SetString(PyExc_ValueError, "the first two maketrans " |
| 12129 | "arguments must have equal length"); |
| 12130 | goto err; |
| 12131 | } |
| 12132 | /* 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] | 12133 | x_kind = PyUnicode_KIND(x); |
| 12134 | y_kind = PyUnicode_KIND(y); |
| 12135 | x_data = PyUnicode_DATA(x); |
| 12136 | y_data = PyUnicode_DATA(y); |
| 12137 | for (i = 0; i < PyUnicode_GET_LENGTH(x); i++) { |
| 12138 | key = PyLong_FromLong(PyUnicode_READ(x_kind, x_data, i)); |
| 12139 | value = PyLong_FromLong(PyUnicode_READ(y_kind, y_data, i)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12140 | if (!key || !value) |
| 12141 | goto err; |
| 12142 | res = PyDict_SetItem(new, key, value); |
| 12143 | Py_DECREF(key); |
| 12144 | Py_DECREF(value); |
| 12145 | if (res < 0) |
| 12146 | goto err; |
| 12147 | } |
| 12148 | /* create entries for deleting chars in z */ |
| 12149 | if (z != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12150 | z_kind = PyUnicode_KIND(z); |
| 12151 | z_data = PyUnicode_DATA(z); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12152 | for (i = 0; i < PyUnicode_GET_SIZE(z); i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12153 | key = PyLong_FromLong(PyUnicode_READ(z_kind, z_data, i)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12154 | if (!key) |
| 12155 | goto err; |
| 12156 | res = PyDict_SetItem(new, key, Py_None); |
| 12157 | Py_DECREF(key); |
| 12158 | if (res < 0) |
| 12159 | goto err; |
| 12160 | } |
| 12161 | } |
| 12162 | } else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12163 | int kind; |
| 12164 | void *data; |
| 12165 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12166 | /* x must be a dict */ |
Raymond Hettinger | 3ad0576 | 2009-05-29 22:11:22 +0000 | [diff] [blame] | 12167 | if (!PyDict_CheckExact(x)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12168 | PyErr_SetString(PyExc_TypeError, "if you give only one argument " |
| 12169 | "to maketrans it must be a dict"); |
| 12170 | goto err; |
| 12171 | } |
| 12172 | /* copy entries into the new dict, converting string keys to int keys */ |
| 12173 | while (PyDict_Next(x, &i, &key, &value)) { |
| 12174 | if (PyUnicode_Check(key)) { |
| 12175 | /* convert string keys to integer keys */ |
| 12176 | PyObject *newkey; |
| 12177 | if (PyUnicode_GET_SIZE(key) != 1) { |
| 12178 | PyErr_SetString(PyExc_ValueError, "string keys in translate " |
| 12179 | "table must be of length 1"); |
| 12180 | goto err; |
| 12181 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12182 | kind = PyUnicode_KIND(key); |
| 12183 | data = PyUnicode_DATA(key); |
| 12184 | newkey = PyLong_FromLong(PyUnicode_READ(kind, data, 0)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12185 | if (!newkey) |
| 12186 | goto err; |
| 12187 | res = PyDict_SetItem(new, newkey, value); |
| 12188 | Py_DECREF(newkey); |
| 12189 | if (res < 0) |
| 12190 | goto err; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12191 | } else if (PyLong_Check(key)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12192 | /* just keep integer keys */ |
| 12193 | if (PyDict_SetItem(new, key, value) < 0) |
| 12194 | goto err; |
| 12195 | } else { |
| 12196 | PyErr_SetString(PyExc_TypeError, "keys in translate table must " |
| 12197 | "be strings or integers"); |
| 12198 | goto err; |
| 12199 | } |
| 12200 | } |
| 12201 | } |
| 12202 | return new; |
| 12203 | err: |
| 12204 | Py_DECREF(new); |
| 12205 | return NULL; |
| 12206 | } |
| 12207 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12208 | PyDoc_STRVAR(translate__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12209 | "S.translate(table) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12210 | \n\ |
| 12211 | Return a copy of the string S, where all characters have been mapped\n\ |
| 12212 | through the given translation table, which must be a mapping of\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12213 | Unicode ordinals to Unicode ordinals, strings, or None.\n\ |
Walter Dörwald | 5c1ee17 | 2002-09-04 20:31:32 +0000 | [diff] [blame] | 12214 | Unmapped characters are left untouched. Characters mapped to None\n\ |
| 12215 | are deleted."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12216 | |
| 12217 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12218 | unicode_translate(PyObject *self, PyObject *table) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12219 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12220 | return _PyUnicode_TranslateCharmap(self, table, "ignore"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12221 | } |
| 12222 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12223 | PyDoc_STRVAR(upper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12224 | "S.upper() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12225 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12226 | Return a copy of S converted to uppercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12227 | |
| 12228 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12229 | unicode_upper(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12230 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12231 | return fixup(self, fixupper); |
| 12232 | } |
| 12233 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12234 | PyDoc_STRVAR(zfill__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12235 | "S.zfill(width) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12236 | \n\ |
Benjamin Peterson | 9aa4299 | 2008-09-10 21:57:34 +0000 | [diff] [blame] | 12237 | Pad a numeric string S with zeros on the left, to fill a field\n\ |
| 12238 | of the specified width. The string S is never truncated."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12239 | |
| 12240 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12241 | unicode_zfill(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12242 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12243 | Py_ssize_t fill; |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12244 | PyObject *u; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12245 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12246 | int kind; |
| 12247 | void *data; |
| 12248 | Py_UCS4 chr; |
| 12249 | |
| 12250 | if (PyUnicode_READY(self) == -1) |
| 12251 | return NULL; |
| 12252 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12253 | if (!PyArg_ParseTuple(args, "n:zfill", &width)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12254 | return NULL; |
| 12255 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12256 | if (PyUnicode_GET_LENGTH(self) >= width) { |
Walter Dörwald | 0fe940c | 2002-04-15 18:42:15 +0000 | [diff] [blame] | 12257 | if (PyUnicode_CheckExact(self)) { |
| 12258 | Py_INCREF(self); |
| 12259 | return (PyObject*) self; |
| 12260 | } |
| 12261 | else |
Victor Stinner | 2219e0a | 2011-10-01 01:16:59 +0200 | [diff] [blame] | 12262 | return PyUnicode_Copy((PyObject*)self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12263 | } |
| 12264 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12265 | fill = width - _PyUnicode_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12266 | |
| 12267 | u = pad(self, fill, 0, '0'); |
| 12268 | |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 12269 | if (u == NULL) |
| 12270 | return NULL; |
| 12271 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12272 | kind = PyUnicode_KIND(u); |
| 12273 | data = PyUnicode_DATA(u); |
| 12274 | chr = PyUnicode_READ(kind, data, fill); |
| 12275 | |
| 12276 | if (chr == '+' || chr == '-') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12277 | /* move sign to beginning of string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12278 | PyUnicode_WRITE(kind, data, 0, chr); |
| 12279 | PyUnicode_WRITE(kind, data, fill, '0'); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12280 | } |
| 12281 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12282 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12283 | return (PyObject*) u; |
| 12284 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12285 | |
| 12286 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12287 | static PyObject * |
| 12288 | unicode__decimal2ascii(PyObject *self) |
| 12289 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12290 | return PyUnicode_TransformDecimalAndSpaceToASCII(self); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12291 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12292 | #endif |
| 12293 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12294 | PyDoc_STRVAR(startswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12295 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12296 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 12297 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 12298 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12299 | With optional end, stop comparing S at that position.\n\ |
| 12300 | prefix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12301 | |
| 12302 | static PyObject * |
| 12303 | unicode_startswith(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12304 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12305 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12306 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12307 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12308 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12309 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12310 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12311 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12312 | if (!stringlib_parse_args_finds("startswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12313 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12314 | if (PyTuple_Check(subobj)) { |
| 12315 | Py_ssize_t i; |
| 12316 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 12317 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12318 | PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12319 | if (substring == NULL) |
| 12320 | return NULL; |
| 12321 | result = tailmatch(self, substring, start, end, -1); |
| 12322 | Py_DECREF(substring); |
| 12323 | if (result) { |
| 12324 | Py_RETURN_TRUE; |
| 12325 | } |
| 12326 | } |
| 12327 | /* nothing matched */ |
| 12328 | Py_RETURN_FALSE; |
| 12329 | } |
| 12330 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12331 | if (substring == NULL) { |
| 12332 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 12333 | PyErr_Format(PyExc_TypeError, "startswith first arg must be str or " |
| 12334 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12335 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12336 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12337 | result = tailmatch(self, substring, start, end, -1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12338 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12339 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12340 | } |
| 12341 | |
| 12342 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12343 | PyDoc_STRVAR(endswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12344 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12345 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 12346 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 12347 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12348 | With optional end, stop comparing S at that position.\n\ |
| 12349 | suffix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12350 | |
| 12351 | static PyObject * |
| 12352 | unicode_endswith(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12353 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12354 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12355 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12356 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12357 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12358 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12359 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12360 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12361 | if (!stringlib_parse_args_finds("endswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12362 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12363 | if (PyTuple_Check(subobj)) { |
| 12364 | Py_ssize_t i; |
| 12365 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 12366 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12367 | PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12368 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12369 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12370 | result = tailmatch(self, substring, start, end, +1); |
| 12371 | Py_DECREF(substring); |
| 12372 | if (result) { |
| 12373 | Py_RETURN_TRUE; |
| 12374 | } |
| 12375 | } |
| 12376 | Py_RETURN_FALSE; |
| 12377 | } |
| 12378 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12379 | if (substring == NULL) { |
| 12380 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 12381 | PyErr_Format(PyExc_TypeError, "endswith first arg must be str or " |
| 12382 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12383 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12384 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12385 | result = tailmatch(self, substring, start, end, +1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12386 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12387 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12388 | } |
| 12389 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12390 | #include "stringlib/unicode_format.h" |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12391 | |
| 12392 | PyDoc_STRVAR(format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12393 | "S.format(*args, **kwargs) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12394 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 12395 | Return a formatted version of S, using substitutions from args and kwargs.\n\ |
| 12396 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12397 | |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 12398 | PyDoc_STRVAR(format_map__doc__, |
| 12399 | "S.format_map(mapping) -> str\n\ |
| 12400 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 12401 | Return a formatted version of S, using substitutions from mapping.\n\ |
| 12402 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 12403 | |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 12404 | static PyObject * |
| 12405 | unicode__format__(PyObject* self, PyObject* args) |
| 12406 | { |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12407 | PyObject *format_spec, *out; |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 12408 | |
| 12409 | if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) |
| 12410 | return NULL; |
| 12411 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12412 | out = _PyUnicode_FormatAdvanced(self, format_spec, 0, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12413 | PyUnicode_GET_LENGTH(format_spec)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12414 | return out; |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 12415 | } |
| 12416 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12417 | PyDoc_STRVAR(p_format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12418 | "S.__format__(format_spec) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12419 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 12420 | Return a formatted version of S as described by format_spec."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12421 | |
| 12422 | static PyObject * |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 12423 | unicode__sizeof__(PyUnicodeObject *v) |
| 12424 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12425 | Py_ssize_t size; |
| 12426 | |
| 12427 | /* If it's a compact object, account for base structure + |
| 12428 | character data. */ |
| 12429 | if (PyUnicode_IS_COMPACT_ASCII(v)) |
| 12430 | size = sizeof(PyASCIIObject) + PyUnicode_GET_LENGTH(v) + 1; |
| 12431 | else if (PyUnicode_IS_COMPACT(v)) |
| 12432 | size = sizeof(PyCompactUnicodeObject) + |
| 12433 | (PyUnicode_GET_LENGTH(v) + 1) * PyUnicode_CHARACTER_SIZE(v); |
| 12434 | else { |
| 12435 | /* If it is a two-block object, account for base object, and |
| 12436 | for character block if present. */ |
| 12437 | size = sizeof(PyUnicodeObject); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 12438 | if (_PyUnicode_DATA_ANY(v)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12439 | size += (PyUnicode_GET_LENGTH(v) + 1) * |
| 12440 | PyUnicode_CHARACTER_SIZE(v); |
| 12441 | } |
| 12442 | /* 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] | 12443 | with the data pointer. Check if the data is not shared. */ |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 12444 | if (_PyUnicode_HAS_WSTR_MEMORY(v)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12445 | size += (PyUnicode_WSTR_LENGTH(v) + 1) * sizeof(wchar_t); |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 12446 | if (_PyUnicode_HAS_UTF8_MEMORY(v)) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 12447 | size += PyUnicode_UTF8_LENGTH(v) + 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12448 | |
| 12449 | return PyLong_FromSsize_t(size); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 12450 | } |
| 12451 | |
| 12452 | PyDoc_STRVAR(sizeof__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12453 | "S.__sizeof__() -> size of S in memory, in bytes"); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 12454 | |
| 12455 | static PyObject * |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 12456 | unicode_getnewargs(PyObject *v) |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 12457 | { |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 12458 | PyObject *copy = PyUnicode_Copy(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12459 | if (!copy) |
| 12460 | return NULL; |
| 12461 | return Py_BuildValue("(N)", copy); |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 12462 | } |
| 12463 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12464 | static PyMethodDef unicode_methods[] = { |
| 12465 | |
| 12466 | /* Order is according to common usage: often used methods should |
| 12467 | appear first, since lookup is done sequentially. */ |
| 12468 | |
Benjamin Peterson | 28a4dce | 2010-12-12 01:33:04 +0000 | [diff] [blame] | 12469 | {"encode", (PyCFunction) unicode_encode, METH_VARARGS | METH_KEYWORDS, encode__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12470 | {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, |
| 12471 | {"split", (PyCFunction) unicode_split, METH_VARARGS, split__doc__}, |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12472 | {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS, rsplit__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12473 | {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, |
| 12474 | {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, |
| 12475 | {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, |
| 12476 | {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, |
| 12477 | {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, |
| 12478 | {"expandtabs", (PyCFunction) unicode_expandtabs, METH_VARARGS, expandtabs__doc__}, |
| 12479 | {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12480 | {"partition", (PyCFunction) unicode_partition, METH_O, partition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12481 | {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, |
| 12482 | {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, |
| 12483 | {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12484 | {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12485 | {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, |
| 12486 | {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, |
| 12487 | {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12488 | {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12489 | {"rpartition", (PyCFunction) unicode_rpartition, METH_O, rpartition__doc__}, |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12490 | {"splitlines", (PyCFunction) unicode_splitlines, METH_VARARGS | METH_KEYWORDS, splitlines__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12491 | {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12492 | {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, |
| 12493 | {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, |
| 12494 | {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, |
| 12495 | {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, |
| 12496 | {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, |
| 12497 | {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, |
| 12498 | {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, |
| 12499 | {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, |
| 12500 | {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, |
| 12501 | {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, |
| 12502 | {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, |
| 12503 | {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, |
| 12504 | {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, |
| 12505 | {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 12506 | {"isidentifier", (PyCFunction) unicode_isidentifier, METH_NOARGS, isidentifier__doc__}, |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12507 | {"isprintable", (PyCFunction) unicode_isprintable, METH_NOARGS, isprintable__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12508 | {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, |
Eric Smith | 9cd1e09 | 2007-08-31 18:39:38 +0000 | [diff] [blame] | 12509 | {"format", (PyCFunction) do_string_format, METH_VARARGS | METH_KEYWORDS, format__doc__}, |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 12510 | {"format_map", (PyCFunction) do_string_format_map, METH_O, format_map__doc__}, |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 12511 | {"__format__", (PyCFunction) unicode__format__, METH_VARARGS, p_format__doc__}, |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12512 | {"maketrans", (PyCFunction) unicode_maketrans, |
| 12513 | METH_VARARGS | METH_STATIC, maketrans__doc__}, |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 12514 | {"__sizeof__", (PyCFunction) unicode__sizeof__, METH_NOARGS, sizeof__doc__}, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 12515 | #if 0 |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12516 | {"capwords", (PyCFunction) unicode_capwords, METH_NOARGS, capwords__doc__}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12517 | #endif |
| 12518 | |
| 12519 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12520 | /* These methods are just used for debugging the implementation. */ |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12521 | {"_decimal2ascii", (PyCFunction) unicode__decimal2ascii, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12522 | #endif |
| 12523 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12524 | {"__getnewargs__", (PyCFunction)unicode_getnewargs, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12525 | {NULL, NULL} |
| 12526 | }; |
| 12527 | |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 12528 | static PyObject * |
| 12529 | unicode_mod(PyObject *v, PyObject *w) |
| 12530 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 12531 | if (!PyUnicode_Check(v)) |
| 12532 | Py_RETURN_NOTIMPLEMENTED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12533 | return PyUnicode_Format(v, w); |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 12534 | } |
| 12535 | |
| 12536 | static PyNumberMethods unicode_as_number = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12537 | 0, /*nb_add*/ |
| 12538 | 0, /*nb_subtract*/ |
| 12539 | 0, /*nb_multiply*/ |
| 12540 | unicode_mod, /*nb_remainder*/ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 12541 | }; |
| 12542 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12543 | static PySequenceMethods unicode_as_sequence = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12544 | (lenfunc) unicode_length, /* sq_length */ |
| 12545 | PyUnicode_Concat, /* sq_concat */ |
| 12546 | (ssizeargfunc) unicode_repeat, /* sq_repeat */ |
| 12547 | (ssizeargfunc) unicode_getitem, /* sq_item */ |
| 12548 | 0, /* sq_slice */ |
| 12549 | 0, /* sq_ass_item */ |
| 12550 | 0, /* sq_ass_slice */ |
| 12551 | PyUnicode_Contains, /* sq_contains */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12552 | }; |
| 12553 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12554 | static PyObject* |
| 12555 | unicode_subscript(PyUnicodeObject* self, PyObject* item) |
| 12556 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12557 | if (PyUnicode_READY(self) == -1) |
| 12558 | return NULL; |
| 12559 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 12560 | if (PyIndex_Check(item)) { |
| 12561 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12562 | if (i == -1 && PyErr_Occurred()) |
| 12563 | return NULL; |
| 12564 | if (i < 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12565 | i += PyUnicode_GET_LENGTH(self); |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 12566 | return unicode_getitem((PyObject*)self, i); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12567 | } else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12568 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 12569 | PyObject *result; |
| 12570 | void *src_data, *dest_data; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 12571 | int src_kind, dest_kind; |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 12572 | Py_UCS4 ch, max_char, kind_limit; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12573 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12574 | if (PySlice_GetIndicesEx(item, PyUnicode_GET_LENGTH(self), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12575 | &start, &stop, &step, &slicelength) < 0) { |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12576 | return NULL; |
| 12577 | } |
| 12578 | |
| 12579 | if (slicelength <= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12580 | return PyUnicode_New(0, 0); |
| 12581 | } else if (start == 0 && step == 1 && |
| 12582 | slicelength == PyUnicode_GET_LENGTH(self) && |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 12583 | PyUnicode_CheckExact(self)) { |
| 12584 | Py_INCREF(self); |
| 12585 | return (PyObject *)self; |
| 12586 | } else if (step == 1) { |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 12587 | return PyUnicode_Substring((PyObject*)self, |
| 12588 | start, start + slicelength); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12589 | } |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 12590 | /* General case */ |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 12591 | max_char = 0; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 12592 | src_kind = PyUnicode_KIND(self); |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 12593 | kind_limit = kind_maxchar_limit(src_kind); |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 12594 | src_data = PyUnicode_DATA(self); |
| 12595 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
| 12596 | ch = PyUnicode_READ(src_kind, src_data, cur); |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 12597 | if (ch > max_char) { |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 12598 | max_char = ch; |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 12599 | if (max_char >= kind_limit) |
| 12600 | break; |
| 12601 | } |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 12602 | } |
| 12603 | result = PyUnicode_New(slicelength, max_char); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 12604 | if (result == NULL) |
| 12605 | return NULL; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 12606 | dest_kind = PyUnicode_KIND(result); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 12607 | dest_data = PyUnicode_DATA(result); |
| 12608 | |
| 12609 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 12610 | Py_UCS4 ch = PyUnicode_READ(src_kind, src_data, cur); |
| 12611 | PyUnicode_WRITE(dest_kind, dest_data, i, ch); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 12612 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12613 | assert(_PyUnicode_CheckConsistency(result, 1)); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 12614 | return result; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12615 | } else { |
| 12616 | PyErr_SetString(PyExc_TypeError, "string indices must be integers"); |
| 12617 | return NULL; |
| 12618 | } |
| 12619 | } |
| 12620 | |
| 12621 | static PyMappingMethods unicode_as_mapping = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12622 | (lenfunc)unicode_length, /* mp_length */ |
| 12623 | (binaryfunc)unicode_subscript, /* mp_subscript */ |
| 12624 | (objobjargproc)0, /* mp_ass_subscript */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12625 | }; |
| 12626 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12627 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12628 | /* Helpers for PyUnicode_Format() */ |
| 12629 | |
| 12630 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12631 | 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] | 12632 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12633 | Py_ssize_t argidx = *p_argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12634 | if (argidx < arglen) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12635 | (*p_argidx)++; |
| 12636 | if (arglen < 0) |
| 12637 | return args; |
| 12638 | else |
| 12639 | return PyTuple_GetItem(args, argidx); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12640 | } |
| 12641 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12642 | "not enough arguments for format string"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12643 | return NULL; |
| 12644 | } |
| 12645 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 12646 | /* 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] | 12647 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 12648 | static PyObject * |
| 12649 | formatfloat(PyObject *v, int flags, int prec, int type) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12650 | { |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 12651 | char *p; |
| 12652 | PyObject *result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12653 | double x; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 12654 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12655 | x = PyFloat_AsDouble(v); |
| 12656 | if (x == -1.0 && PyErr_Occurred()) |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 12657 | return NULL; |
| 12658 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12659 | if (prec < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12660 | prec = 6; |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 12661 | |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 12662 | p = PyOS_double_to_string(x, type, prec, |
| 12663 | (flags & F_ALT) ? Py_DTSF_ALT : 0, NULL); |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 12664 | if (p == NULL) |
| 12665 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12666 | result = PyUnicode_DecodeASCII(p, strlen(p), NULL); |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 12667 | PyMem_Free(p); |
| 12668 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12669 | } |
| 12670 | |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 12671 | static PyObject* |
| 12672 | formatlong(PyObject *val, int flags, int prec, int type) |
| 12673 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12674 | char *buf; |
| 12675 | int len; |
| 12676 | PyObject *str; /* temporary string object. */ |
| 12677 | PyObject *result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 12678 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12679 | str = _PyBytes_FormatLong(val, flags, prec, type, &buf, &len); |
| 12680 | if (!str) |
| 12681 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12682 | result = PyUnicode_DecodeASCII(buf, len, NULL); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12683 | Py_DECREF(str); |
| 12684 | return result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 12685 | } |
| 12686 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12687 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12688 | formatchar(Py_UCS4 *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 12689 | size_t buflen, |
| 12690 | PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12691 | { |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 12692 | /* presume that the buffer is at least 3 characters long */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 12693 | if (PyUnicode_Check(v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12694 | if (PyUnicode_GET_LENGTH(v) == 1) { |
| 12695 | buf[0] = PyUnicode_READ_CHAR(v, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12696 | buf[1] = '\0'; |
| 12697 | return 1; |
| 12698 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12699 | goto onError; |
| 12700 | } |
| 12701 | else { |
| 12702 | /* Integer input truncated to a character */ |
| 12703 | long x; |
| 12704 | x = PyLong_AsLong(v); |
| 12705 | if (x == -1 && PyErr_Occurred()) |
| 12706 | goto onError; |
| 12707 | |
| 12708 | if (x < 0 || x > 0x10ffff) { |
| 12709 | PyErr_SetString(PyExc_OverflowError, |
| 12710 | "%c arg not in range(0x110000)"); |
| 12711 | return -1; |
| 12712 | } |
| 12713 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12714 | buf[0] = (Py_UCS4) x; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12715 | buf[1] = '\0'; |
| 12716 | return 1; |
| 12717 | } |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 12718 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12719 | onError: |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 12720 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12721 | "%c requires int or char"); |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 12722 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12723 | } |
| 12724 | |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 12725 | /* fmt%(v1,v2,...) is roughly equivalent to sprintf(fmt, v1, v2, ...) |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 12726 | 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] | 12727 | */ |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 12728 | #define FORMATBUFLEN (size_t)10 |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 12729 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12730 | PyObject * |
| 12731 | PyUnicode_Format(PyObject *format, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12732 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12733 | void *fmt; |
| 12734 | int fmtkind; |
| 12735 | PyObject *result; |
| 12736 | Py_UCS4 *res, *res0; |
| 12737 | Py_UCS4 max; |
| 12738 | int kind; |
| 12739 | Py_ssize_t fmtcnt, fmtpos, rescnt, reslen, arglen, argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12740 | int args_owned = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12741 | PyObject *dict = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12742 | PyUnicodeObject *uformat; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 12743 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12744 | if (format == NULL || args == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12745 | PyErr_BadInternalCall(); |
| 12746 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12747 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12748 | uformat = (PyUnicodeObject*)PyUnicode_FromObject(format); |
| 12749 | if (uformat == NULL || PyUnicode_READY(uformat) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12750 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12751 | fmt = PyUnicode_DATA(uformat); |
| 12752 | fmtkind = PyUnicode_KIND(uformat); |
| 12753 | fmtcnt = PyUnicode_GET_LENGTH(uformat); |
| 12754 | fmtpos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12755 | |
| 12756 | reslen = rescnt = fmtcnt + 100; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12757 | res = res0 = PyMem_Malloc(reslen * sizeof(Py_UCS4)); |
| 12758 | if (res0 == NULL) { |
| 12759 | PyErr_NoMemory(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12760 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12761 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12762 | |
| 12763 | if (PyTuple_Check(args)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12764 | arglen = PyTuple_Size(args); |
| 12765 | argidx = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12766 | } |
| 12767 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12768 | arglen = -1; |
| 12769 | argidx = -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12770 | } |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 12771 | if (Py_TYPE(args)->tp_as_mapping && !PyTuple_Check(args) && |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 12772 | !PyUnicode_Check(args)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12773 | dict = args; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12774 | |
| 12775 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12776 | if (PyUnicode_READ(fmtkind, fmt, fmtpos) != '%') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12777 | if (--rescnt < 0) { |
| 12778 | rescnt = fmtcnt + 100; |
| 12779 | reslen += rescnt; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12780 | res0 = PyMem_Realloc(res0, reslen*sizeof(Py_UCS4)); |
| 12781 | if (res0 == NULL){ |
| 12782 | PyErr_NoMemory(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12783 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12784 | } |
| 12785 | res = res0 + reslen - rescnt; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12786 | --rescnt; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12787 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12788 | *res++ = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12789 | } |
| 12790 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12791 | /* Got a format specifier */ |
| 12792 | int flags = 0; |
| 12793 | Py_ssize_t width = -1; |
| 12794 | int prec = -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12795 | Py_UCS4 c = '\0'; |
| 12796 | Py_UCS4 fill; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12797 | int isnumok; |
| 12798 | PyObject *v = NULL; |
| 12799 | PyObject *temp = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12800 | void *pbuf; |
| 12801 | Py_ssize_t pindex; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12802 | Py_UNICODE sign; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12803 | Py_ssize_t len, len1; |
| 12804 | Py_UCS4 formatbuf[FORMATBUFLEN]; /* For formatchar() */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12805 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12806 | fmtpos++; |
| 12807 | if (PyUnicode_READ(fmtkind, fmt, fmtpos) == '(') { |
| 12808 | Py_ssize_t keystart; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12809 | Py_ssize_t keylen; |
| 12810 | PyObject *key; |
| 12811 | int pcount = 1; |
Christian Heimes | a612dc0 | 2008-02-24 13:08:18 +0000 | [diff] [blame] | 12812 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12813 | if (dict == NULL) { |
| 12814 | PyErr_SetString(PyExc_TypeError, |
| 12815 | "format requires a mapping"); |
| 12816 | goto onError; |
| 12817 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12818 | ++fmtpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12819 | --fmtcnt; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12820 | keystart = fmtpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12821 | /* Skip over balanced parentheses */ |
| 12822 | while (pcount > 0 && --fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12823 | if (PyUnicode_READ(fmtkind, fmt, fmtpos) == ')') |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12824 | --pcount; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12825 | else if (PyUnicode_READ(fmtkind, fmt, fmtpos) == '(') |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12826 | ++pcount; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12827 | fmtpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12828 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12829 | keylen = fmtpos - keystart - 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12830 | if (fmtcnt < 0 || pcount > 0) { |
| 12831 | PyErr_SetString(PyExc_ValueError, |
| 12832 | "incomplete format key"); |
| 12833 | goto onError; |
| 12834 | } |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 12835 | key = PyUnicode_Substring((PyObject*)uformat, |
| 12836 | keystart, keystart + keylen); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12837 | if (key == NULL) |
| 12838 | goto onError; |
| 12839 | if (args_owned) { |
| 12840 | Py_DECREF(args); |
| 12841 | args_owned = 0; |
| 12842 | } |
| 12843 | args = PyObject_GetItem(dict, key); |
| 12844 | Py_DECREF(key); |
| 12845 | if (args == NULL) { |
| 12846 | goto onError; |
| 12847 | } |
| 12848 | args_owned = 1; |
| 12849 | arglen = -1; |
| 12850 | argidx = -2; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12851 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12852 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12853 | switch (c = PyUnicode_READ(fmtkind, fmt, fmtpos++)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12854 | case '-': flags |= F_LJUST; continue; |
| 12855 | case '+': flags |= F_SIGN; continue; |
| 12856 | case ' ': flags |= F_BLANK; continue; |
| 12857 | case '#': flags |= F_ALT; continue; |
| 12858 | case '0': flags |= F_ZERO; continue; |
| 12859 | } |
| 12860 | break; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12861 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12862 | if (c == '*') { |
| 12863 | v = getnextarg(args, arglen, &argidx); |
| 12864 | if (v == NULL) |
| 12865 | goto onError; |
| 12866 | if (!PyLong_Check(v)) { |
| 12867 | PyErr_SetString(PyExc_TypeError, |
| 12868 | "* wants int"); |
| 12869 | goto onError; |
| 12870 | } |
| 12871 | width = PyLong_AsLong(v); |
| 12872 | if (width == -1 && PyErr_Occurred()) |
| 12873 | goto onError; |
| 12874 | if (width < 0) { |
| 12875 | flags |= F_LJUST; |
| 12876 | width = -width; |
| 12877 | } |
| 12878 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12879 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12880 | } |
| 12881 | else if (c >= '0' && c <= '9') { |
| 12882 | width = c - '0'; |
| 12883 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12884 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12885 | if (c < '0' || c > '9') |
| 12886 | break; |
| 12887 | if ((width*10) / 10 != width) { |
| 12888 | PyErr_SetString(PyExc_ValueError, |
| 12889 | "width too big"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12890 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12891 | } |
| 12892 | width = width*10 + (c - '0'); |
| 12893 | } |
| 12894 | } |
| 12895 | if (c == '.') { |
| 12896 | prec = 0; |
| 12897 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12898 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12899 | if (c == '*') { |
| 12900 | v = getnextarg(args, arglen, &argidx); |
| 12901 | if (v == NULL) |
| 12902 | goto onError; |
| 12903 | if (!PyLong_Check(v)) { |
| 12904 | PyErr_SetString(PyExc_TypeError, |
| 12905 | "* wants int"); |
| 12906 | goto onError; |
| 12907 | } |
| 12908 | prec = PyLong_AsLong(v); |
| 12909 | if (prec == -1 && PyErr_Occurred()) |
| 12910 | goto onError; |
| 12911 | if (prec < 0) |
| 12912 | prec = 0; |
| 12913 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12914 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12915 | } |
| 12916 | else if (c >= '0' && c <= '9') { |
| 12917 | prec = c - '0'; |
| 12918 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12919 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12920 | if (c < '0' || c > '9') |
| 12921 | break; |
| 12922 | if ((prec*10) / 10 != prec) { |
| 12923 | PyErr_SetString(PyExc_ValueError, |
| 12924 | "prec too big"); |
| 12925 | goto onError; |
| 12926 | } |
| 12927 | prec = prec*10 + (c - '0'); |
| 12928 | } |
| 12929 | } |
| 12930 | } /* prec */ |
| 12931 | if (fmtcnt >= 0) { |
| 12932 | if (c == 'h' || c == 'l' || c == 'L') { |
| 12933 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12934 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12935 | } |
| 12936 | } |
| 12937 | if (fmtcnt < 0) { |
| 12938 | PyErr_SetString(PyExc_ValueError, |
| 12939 | "incomplete format"); |
| 12940 | goto onError; |
| 12941 | } |
| 12942 | if (c != '%') { |
| 12943 | v = getnextarg(args, arglen, &argidx); |
| 12944 | if (v == NULL) |
| 12945 | goto onError; |
| 12946 | } |
| 12947 | sign = 0; |
| 12948 | fill = ' '; |
| 12949 | switch (c) { |
| 12950 | |
| 12951 | case '%': |
| 12952 | pbuf = formatbuf; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12953 | kind = PyUnicode_4BYTE_KIND; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12954 | /* presume that buffer length is at least 1 */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12955 | PyUnicode_WRITE(kind, pbuf, 0, '%'); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12956 | len = 1; |
| 12957 | break; |
| 12958 | |
| 12959 | case 's': |
| 12960 | case 'r': |
| 12961 | case 'a': |
Victor Stinner | 808fc0a | 2010-03-22 12:50:40 +0000 | [diff] [blame] | 12962 | if (PyUnicode_CheckExact(v) && c == 's') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12963 | temp = v; |
| 12964 | Py_INCREF(temp); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12965 | } |
| 12966 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12967 | if (c == 's') |
| 12968 | temp = PyObject_Str(v); |
| 12969 | else if (c == 'r') |
| 12970 | temp = PyObject_Repr(v); |
| 12971 | else |
| 12972 | temp = PyObject_ASCII(v); |
| 12973 | if (temp == NULL) |
| 12974 | goto onError; |
| 12975 | if (PyUnicode_Check(temp)) |
| 12976 | /* nothing to do */; |
| 12977 | else { |
| 12978 | Py_DECREF(temp); |
| 12979 | PyErr_SetString(PyExc_TypeError, |
| 12980 | "%s argument has non-string str()"); |
| 12981 | goto onError; |
| 12982 | } |
| 12983 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12984 | if (PyUnicode_READY(temp) == -1) { |
| 12985 | Py_CLEAR(temp); |
| 12986 | goto onError; |
| 12987 | } |
| 12988 | pbuf = PyUnicode_DATA(temp); |
| 12989 | kind = PyUnicode_KIND(temp); |
| 12990 | len = PyUnicode_GET_LENGTH(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12991 | if (prec >= 0 && len > prec) |
| 12992 | len = prec; |
| 12993 | break; |
| 12994 | |
| 12995 | case 'i': |
| 12996 | case 'd': |
| 12997 | case 'u': |
| 12998 | case 'o': |
| 12999 | case 'x': |
| 13000 | case 'X': |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13001 | isnumok = 0; |
| 13002 | if (PyNumber_Check(v)) { |
| 13003 | PyObject *iobj=NULL; |
| 13004 | |
| 13005 | if (PyLong_Check(v)) { |
| 13006 | iobj = v; |
| 13007 | Py_INCREF(iobj); |
| 13008 | } |
| 13009 | else { |
| 13010 | iobj = PyNumber_Long(v); |
| 13011 | } |
| 13012 | if (iobj!=NULL) { |
| 13013 | if (PyLong_Check(iobj)) { |
| 13014 | isnumok = 1; |
Senthil Kumaran | 9ebe08d | 2011-07-03 21:03:16 -0700 | [diff] [blame] | 13015 | temp = formatlong(iobj, flags, prec, (c == 'i'? 'd': c)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13016 | Py_DECREF(iobj); |
| 13017 | if (!temp) |
| 13018 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13019 | if (PyUnicode_READY(temp) == -1) { |
| 13020 | Py_CLEAR(temp); |
| 13021 | goto onError; |
| 13022 | } |
| 13023 | pbuf = PyUnicode_DATA(temp); |
| 13024 | kind = PyUnicode_KIND(temp); |
| 13025 | len = PyUnicode_GET_LENGTH(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13026 | sign = 1; |
| 13027 | } |
| 13028 | else { |
| 13029 | Py_DECREF(iobj); |
| 13030 | } |
| 13031 | } |
| 13032 | } |
| 13033 | if (!isnumok) { |
| 13034 | PyErr_Format(PyExc_TypeError, |
| 13035 | "%%%c format: a number is required, " |
| 13036 | "not %.200s", (char)c, Py_TYPE(v)->tp_name); |
| 13037 | goto onError; |
| 13038 | } |
| 13039 | if (flags & F_ZERO) |
| 13040 | fill = '0'; |
| 13041 | break; |
| 13042 | |
| 13043 | case 'e': |
| 13044 | case 'E': |
| 13045 | case 'f': |
| 13046 | case 'F': |
| 13047 | case 'g': |
| 13048 | case 'G': |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13049 | temp = formatfloat(v, flags, prec, c); |
| 13050 | if (!temp) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13051 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13052 | if (PyUnicode_READY(temp) == -1) { |
| 13053 | Py_CLEAR(temp); |
| 13054 | goto onError; |
| 13055 | } |
| 13056 | pbuf = PyUnicode_DATA(temp); |
| 13057 | kind = PyUnicode_KIND(temp); |
| 13058 | len = PyUnicode_GET_LENGTH(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13059 | sign = 1; |
| 13060 | if (flags & F_ZERO) |
| 13061 | fill = '0'; |
| 13062 | break; |
| 13063 | |
| 13064 | case 'c': |
| 13065 | pbuf = formatbuf; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13066 | kind = PyUnicode_4BYTE_KIND; |
Victor Stinner | b9dcffb | 2011-09-29 00:39:24 +0200 | [diff] [blame] | 13067 | len = formatchar(pbuf, Py_ARRAY_LENGTH(formatbuf), v); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13068 | if (len < 0) |
| 13069 | goto onError; |
| 13070 | break; |
| 13071 | |
| 13072 | default: |
| 13073 | PyErr_Format(PyExc_ValueError, |
| 13074 | "unsupported format character '%c' (0x%x) " |
| 13075 | "at index %zd", |
| 13076 | (31<=c && c<=126) ? (char)c : '?', |
| 13077 | (int)c, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13078 | fmtpos - 1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13079 | goto onError; |
| 13080 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13081 | /* pbuf is initialized here. */ |
| 13082 | pindex = 0; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13083 | if (sign) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13084 | if (PyUnicode_READ(kind, pbuf, pindex) == '-' || |
| 13085 | PyUnicode_READ(kind, pbuf, pindex) == '+') { |
| 13086 | sign = PyUnicode_READ(kind, pbuf, pindex++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13087 | len--; |
| 13088 | } |
| 13089 | else if (flags & F_SIGN) |
| 13090 | sign = '+'; |
| 13091 | else if (flags & F_BLANK) |
| 13092 | sign = ' '; |
| 13093 | else |
| 13094 | sign = 0; |
| 13095 | } |
| 13096 | if (width < len) |
| 13097 | width = len; |
| 13098 | if (rescnt - (sign != 0) < width) { |
| 13099 | reslen -= rescnt; |
| 13100 | rescnt = width + fmtcnt + 100; |
| 13101 | reslen += rescnt; |
| 13102 | if (reslen < 0) { |
| 13103 | Py_XDECREF(temp); |
| 13104 | PyErr_NoMemory(); |
| 13105 | goto onError; |
| 13106 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13107 | res0 = PyMem_Realloc(res0, reslen*sizeof(Py_UCS4)); |
| 13108 | if (res0 == 0) { |
| 13109 | PyErr_NoMemory(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13110 | Py_XDECREF(temp); |
| 13111 | goto onError; |
| 13112 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13113 | res = res0 + reslen - rescnt; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13114 | } |
| 13115 | if (sign) { |
| 13116 | if (fill != ' ') |
| 13117 | *res++ = sign; |
| 13118 | rescnt--; |
| 13119 | if (width > len) |
| 13120 | width--; |
| 13121 | } |
| 13122 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13123 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
| 13124 | assert(PyUnicode_READ(kind, pbuf, pindex+1) == c); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13125 | if (fill != ' ') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13126 | *res++ = PyUnicode_READ(kind, pbuf, pindex++); |
| 13127 | *res++ = PyUnicode_READ(kind, pbuf, pindex++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13128 | } |
| 13129 | rescnt -= 2; |
| 13130 | width -= 2; |
| 13131 | if (width < 0) |
| 13132 | width = 0; |
| 13133 | len -= 2; |
| 13134 | } |
| 13135 | if (width > len && !(flags & F_LJUST)) { |
| 13136 | do { |
| 13137 | --rescnt; |
| 13138 | *res++ = fill; |
| 13139 | } while (--width > len); |
| 13140 | } |
| 13141 | if (fill == ' ') { |
| 13142 | if (sign) |
| 13143 | *res++ = sign; |
| 13144 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13145 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
| 13146 | assert(PyUnicode_READ(kind, pbuf, pindex+1) == c); |
| 13147 | *res++ = PyUnicode_READ(kind, pbuf, pindex++); |
| 13148 | *res++ = PyUnicode_READ(kind, pbuf, pindex++); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13149 | } |
| 13150 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13151 | /* Copy all characters, preserving len */ |
| 13152 | len1 = len; |
| 13153 | while (len1--) { |
| 13154 | *res++ = PyUnicode_READ(kind, pbuf, pindex++); |
| 13155 | rescnt--; |
| 13156 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13157 | while (--width >= len) { |
| 13158 | --rescnt; |
| 13159 | *res++ = ' '; |
| 13160 | } |
| 13161 | if (dict && (argidx < arglen) && c != '%') { |
| 13162 | PyErr_SetString(PyExc_TypeError, |
| 13163 | "not all arguments converted during string formatting"); |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 13164 | Py_XDECREF(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13165 | goto onError; |
| 13166 | } |
| 13167 | Py_XDECREF(temp); |
| 13168 | } /* '%' */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13169 | } /* until end */ |
| 13170 | if (argidx < arglen && !dict) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13171 | PyErr_SetString(PyExc_TypeError, |
| 13172 | "not all arguments converted during string formatting"); |
| 13173 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13174 | } |
| 13175 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13176 | |
| 13177 | for (max=0, res = res0; res < res0+reslen-rescnt; res++) |
| 13178 | if (*res > max) |
| 13179 | max = *res; |
| 13180 | result = PyUnicode_New(reslen - rescnt, max); |
| 13181 | if (!result) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13182 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13183 | kind = PyUnicode_KIND(result); |
| 13184 | for (res = res0; res < res0+reslen-rescnt; res++) |
| 13185 | PyUnicode_WRITE(kind, PyUnicode_DATA(result), res-res0, *res); |
| 13186 | PyMem_Free(res0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13187 | if (args_owned) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13188 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13189 | } |
| 13190 | Py_DECREF(uformat); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13191 | assert(_PyUnicode_CheckConsistency(result, 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13192 | return (PyObject *)result; |
| 13193 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13194 | onError: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13195 | PyMem_Free(res0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13196 | Py_DECREF(uformat); |
| 13197 | if (args_owned) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13198 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13199 | } |
| 13200 | return NULL; |
| 13201 | } |
| 13202 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 13203 | static PyObject * |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 13204 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 13205 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13206 | static PyObject * |
| 13207 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 13208 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13209 | PyObject *x = NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13210 | static char *kwlist[] = {"object", "encoding", "errors", 0}; |
| 13211 | char *encoding = NULL; |
| 13212 | char *errors = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13213 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13214 | if (type != &PyUnicode_Type) |
| 13215 | return unicode_subtype_new(type, args, kwds); |
| 13216 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:str", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13217 | kwlist, &x, &encoding, &errors)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13218 | return NULL; |
| 13219 | if (x == NULL) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13220 | return (PyObject *)PyUnicode_New(0, 0); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13221 | if (encoding == NULL && errors == NULL) |
| 13222 | return PyObject_Str(x); |
| 13223 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13224 | return PyUnicode_FromEncodedObject(x, encoding, errors); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13225 | } |
| 13226 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 13227 | static PyObject * |
| 13228 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 13229 | { |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13230 | PyUnicodeObject *unicode, *self; |
| 13231 | Py_ssize_t length, char_size; |
| 13232 | int share_wstr, share_utf8; |
| 13233 | unsigned int kind; |
| 13234 | void *data; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 13235 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13236 | assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13237 | |
| 13238 | unicode = (PyUnicodeObject *)unicode_new(&PyUnicode_Type, args, kwds); |
| 13239 | if (unicode == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13240 | return NULL; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 13241 | assert(_PyUnicode_CHECK(unicode)); |
Victor Stinner | e06e145 | 2011-10-04 20:52:31 +0200 | [diff] [blame] | 13242 | if (PyUnicode_READY(unicode)) |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13243 | return NULL; |
| 13244 | |
| 13245 | self = (PyUnicodeObject *) type->tp_alloc(type, 0); |
| 13246 | if (self == NULL) { |
| 13247 | Py_DECREF(unicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13248 | return NULL; |
| 13249 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13250 | kind = PyUnicode_KIND(unicode); |
| 13251 | length = PyUnicode_GET_LENGTH(unicode); |
| 13252 | |
| 13253 | _PyUnicode_LENGTH(self) = length; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 13254 | #ifdef Py_DEBUG |
| 13255 | _PyUnicode_HASH(self) = -1; |
| 13256 | #else |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13257 | _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 13258 | #endif |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13259 | _PyUnicode_STATE(self).interned = 0; |
| 13260 | _PyUnicode_STATE(self).kind = kind; |
| 13261 | _PyUnicode_STATE(self).compact = 0; |
Victor Stinner | 3cf4637 | 2011-10-03 14:42:15 +0200 | [diff] [blame] | 13262 | _PyUnicode_STATE(self).ascii = _PyUnicode_STATE(unicode).ascii; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13263 | _PyUnicode_STATE(self).ready = 1; |
| 13264 | _PyUnicode_WSTR(self) = NULL; |
| 13265 | _PyUnicode_UTF8_LENGTH(self) = 0; |
| 13266 | _PyUnicode_UTF8(self) = NULL; |
| 13267 | _PyUnicode_WSTR_LENGTH(self) = 0; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 13268 | _PyUnicode_DATA_ANY(self) = NULL; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13269 | |
| 13270 | share_utf8 = 0; |
| 13271 | share_wstr = 0; |
| 13272 | if (kind == PyUnicode_1BYTE_KIND) { |
| 13273 | char_size = 1; |
| 13274 | if (PyUnicode_MAX_CHAR_VALUE(unicode) < 128) |
| 13275 | share_utf8 = 1; |
| 13276 | } |
| 13277 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 13278 | char_size = 2; |
| 13279 | if (sizeof(wchar_t) == 2) |
| 13280 | share_wstr = 1; |
| 13281 | } |
| 13282 | else { |
| 13283 | assert(kind == PyUnicode_4BYTE_KIND); |
| 13284 | char_size = 4; |
| 13285 | if (sizeof(wchar_t) == 4) |
| 13286 | share_wstr = 1; |
| 13287 | } |
| 13288 | |
| 13289 | /* Ensure we won't overflow the length. */ |
| 13290 | if (length > (PY_SSIZE_T_MAX / char_size - 1)) { |
| 13291 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13292 | goto onError; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13293 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13294 | data = PyObject_MALLOC((length + 1) * char_size); |
| 13295 | if (data == NULL) { |
| 13296 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13297 | goto onError; |
| 13298 | } |
| 13299 | |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 13300 | _PyUnicode_DATA_ANY(self) = data; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13301 | if (share_utf8) { |
| 13302 | _PyUnicode_UTF8_LENGTH(self) = length; |
| 13303 | _PyUnicode_UTF8(self) = data; |
| 13304 | } |
| 13305 | if (share_wstr) { |
| 13306 | _PyUnicode_WSTR_LENGTH(self) = length; |
| 13307 | _PyUnicode_WSTR(self) = (wchar_t *)data; |
| 13308 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13309 | |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13310 | Py_MEMCPY(data, PyUnicode_DATA(unicode), |
| 13311 | PyUnicode_KIND_SIZE(kind, length + 1)); |
| 13312 | Py_DECREF(unicode); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13313 | assert(_PyUnicode_CheckConsistency(self, 1)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 13314 | #ifdef Py_DEBUG |
| 13315 | _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode); |
| 13316 | #endif |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13317 | return (PyObject *)self; |
| 13318 | |
| 13319 | onError: |
| 13320 | Py_DECREF(unicode); |
| 13321 | Py_DECREF(self); |
| 13322 | return NULL; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 13323 | } |
| 13324 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 13325 | PyDoc_STRVAR(unicode_doc, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13326 | "str(string[, encoding[, errors]]) -> str\n\ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13327 | \n\ |
Collin Winter | d474ce8 | 2007-08-07 19:42:11 +0000 | [diff] [blame] | 13328 | Create a new string object from the given encoded string.\n\ |
Skip Montanaro | 35b37a5 | 2002-07-26 16:22:46 +0000 | [diff] [blame] | 13329 | encoding defaults to the current default string encoding.\n\ |
| 13330 | errors can be 'strict', 'replace' or 'ignore' and defaults to 'strict'."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13331 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13332 | static PyObject *unicode_iter(PyObject *seq); |
| 13333 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13334 | PyTypeObject PyUnicode_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 13335 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13336 | "str", /* tp_name */ |
| 13337 | sizeof(PyUnicodeObject), /* tp_size */ |
| 13338 | 0, /* tp_itemsize */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13339 | /* Slots */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13340 | (destructor)unicode_dealloc, /* tp_dealloc */ |
| 13341 | 0, /* tp_print */ |
| 13342 | 0, /* tp_getattr */ |
| 13343 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 13344 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13345 | unicode_repr, /* tp_repr */ |
| 13346 | &unicode_as_number, /* tp_as_number */ |
| 13347 | &unicode_as_sequence, /* tp_as_sequence */ |
| 13348 | &unicode_as_mapping, /* tp_as_mapping */ |
| 13349 | (hashfunc) unicode_hash, /* tp_hash*/ |
| 13350 | 0, /* tp_call*/ |
| 13351 | (reprfunc) unicode_str, /* tp_str */ |
| 13352 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 13353 | 0, /* tp_setattro */ |
| 13354 | 0, /* tp_as_buffer */ |
| 13355 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13356 | Py_TPFLAGS_UNICODE_SUBCLASS, /* tp_flags */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13357 | unicode_doc, /* tp_doc */ |
| 13358 | 0, /* tp_traverse */ |
| 13359 | 0, /* tp_clear */ |
| 13360 | PyUnicode_RichCompare, /* tp_richcompare */ |
| 13361 | 0, /* tp_weaklistoffset */ |
| 13362 | unicode_iter, /* tp_iter */ |
| 13363 | 0, /* tp_iternext */ |
| 13364 | unicode_methods, /* tp_methods */ |
| 13365 | 0, /* tp_members */ |
| 13366 | 0, /* tp_getset */ |
| 13367 | &PyBaseObject_Type, /* tp_base */ |
| 13368 | 0, /* tp_dict */ |
| 13369 | 0, /* tp_descr_get */ |
| 13370 | 0, /* tp_descr_set */ |
| 13371 | 0, /* tp_dictoffset */ |
| 13372 | 0, /* tp_init */ |
| 13373 | 0, /* tp_alloc */ |
| 13374 | unicode_new, /* tp_new */ |
| 13375 | PyObject_Del, /* tp_free */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13376 | }; |
| 13377 | |
| 13378 | /* Initialize the Unicode implementation */ |
| 13379 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 13380 | void _PyUnicode_Init(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13381 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 13382 | int i; |
| 13383 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13384 | /* XXX - move this array to unicodectype.c ? */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13385 | Py_UCS2 linebreak[] = { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13386 | 0x000A, /* LINE FEED */ |
| 13387 | 0x000D, /* CARRIAGE RETURN */ |
| 13388 | 0x001C, /* FILE SEPARATOR */ |
| 13389 | 0x001D, /* GROUP SEPARATOR */ |
| 13390 | 0x001E, /* RECORD SEPARATOR */ |
| 13391 | 0x0085, /* NEXT LINE */ |
| 13392 | 0x2028, /* LINE SEPARATOR */ |
| 13393 | 0x2029, /* PARAGRAPH SEPARATOR */ |
| 13394 | }; |
| 13395 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 13396 | /* Init the implementation */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 13397 | unicode_empty = PyUnicode_New(0, 0); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13398 | assert(_PyUnicode_CheckConsistency(unicode_empty, 1)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13399 | if (!unicode_empty) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13400 | Py_FatalError("Can't create empty string"); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13401 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 13402 | for (i = 0; i < 256; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13403 | unicode_latin1[i] = NULL; |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 13404 | if (PyType_Ready(&PyUnicode_Type) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13405 | Py_FatalError("Can't initialize 'unicode'"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13406 | |
| 13407 | /* initialize the linebreak bloom filter */ |
| 13408 | bloom_linebreak = make_bloom_mask( |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13409 | PyUnicode_2BYTE_KIND, linebreak, |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 13410 | Py_ARRAY_LENGTH(linebreak)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13411 | |
| 13412 | PyType_Ready(&EncodingMapType); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13413 | } |
| 13414 | |
| 13415 | /* Finalize the Unicode implementation */ |
| 13416 | |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 13417 | int |
| 13418 | PyUnicode_ClearFreeList(void) |
| 13419 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13420 | return 0; |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 13421 | } |
| 13422 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13423 | void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 13424 | _PyUnicode_Fini(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13425 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 13426 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13427 | |
Guido van Rossum | 4ae8ef8 | 2000-10-03 18:09:04 +0000 | [diff] [blame] | 13428 | Py_XDECREF(unicode_empty); |
| 13429 | unicode_empty = NULL; |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 13430 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 13431 | for (i = 0; i < 256; i++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13432 | if (unicode_latin1[i]) { |
| 13433 | Py_DECREF(unicode_latin1[i]); |
| 13434 | unicode_latin1[i] = NULL; |
| 13435 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 13436 | } |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 13437 | (void)PyUnicode_ClearFreeList(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13438 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 13439 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13440 | void |
| 13441 | PyUnicode_InternInPlace(PyObject **p) |
| 13442 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13443 | register PyUnicodeObject *s = (PyUnicodeObject *)(*p); |
| 13444 | PyObject *t; |
Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 13445 | #ifdef Py_DEBUG |
| 13446 | assert(s != NULL); |
| 13447 | assert(_PyUnicode_CHECK(s)); |
| 13448 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13449 | if (s == NULL || !PyUnicode_Check(s)) |
Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 13450 | return; |
| 13451 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13452 | /* If it's a subclass, we don't really know what putting |
| 13453 | it in the interned dict might do. */ |
| 13454 | if (!PyUnicode_CheckExact(s)) |
| 13455 | return; |
| 13456 | if (PyUnicode_CHECK_INTERNED(s)) |
| 13457 | return; |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 13458 | if (_PyUnicode_READY_REPLACE(p)) { |
Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 13459 | assert(0 && "_PyUnicode_READY_REPLACE fail in PyUnicode_InternInPlace"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13460 | return; |
| 13461 | } |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 13462 | s = (PyUnicodeObject *)(*p); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13463 | if (interned == NULL) { |
| 13464 | interned = PyDict_New(); |
| 13465 | if (interned == NULL) { |
| 13466 | PyErr_Clear(); /* Don't leave an exception */ |
| 13467 | return; |
| 13468 | } |
| 13469 | } |
| 13470 | /* It might be that the GetItem call fails even |
| 13471 | though the key is present in the dictionary, |
| 13472 | namely when this happens during a stack overflow. */ |
| 13473 | Py_ALLOW_RECURSION |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13474 | t = PyDict_GetItem(interned, (PyObject *)s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13475 | Py_END_ALLOW_RECURSION |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 13476 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13477 | if (t) { |
| 13478 | Py_INCREF(t); |
| 13479 | Py_DECREF(*p); |
| 13480 | *p = t; |
| 13481 | return; |
| 13482 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13483 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13484 | PyThreadState_GET()->recursion_critical = 1; |
| 13485 | if (PyDict_SetItem(interned, (PyObject *)s, (PyObject *)s) < 0) { |
| 13486 | PyErr_Clear(); |
| 13487 | PyThreadState_GET()->recursion_critical = 0; |
| 13488 | return; |
| 13489 | } |
| 13490 | PyThreadState_GET()->recursion_critical = 0; |
| 13491 | /* The two references in interned are not counted by refcnt. |
| 13492 | The deallocator will take care of this */ |
| 13493 | Py_REFCNT(s) -= 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13494 | _PyUnicode_STATE(s).interned = SSTATE_INTERNED_MORTAL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13495 | } |
| 13496 | |
| 13497 | void |
| 13498 | PyUnicode_InternImmortal(PyObject **p) |
| 13499 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13500 | PyUnicodeObject *u = (PyUnicodeObject *)*p; |
| 13501 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13502 | PyUnicode_InternInPlace(p); |
| 13503 | if (PyUnicode_CHECK_INTERNED(*p) != SSTATE_INTERNED_IMMORTAL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13504 | _PyUnicode_STATE(u).interned = SSTATE_INTERNED_IMMORTAL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13505 | Py_INCREF(*p); |
| 13506 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13507 | } |
| 13508 | |
| 13509 | PyObject * |
| 13510 | PyUnicode_InternFromString(const char *cp) |
| 13511 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13512 | PyObject *s = PyUnicode_FromString(cp); |
| 13513 | if (s == NULL) |
| 13514 | return NULL; |
| 13515 | PyUnicode_InternInPlace(&s); |
| 13516 | return s; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13517 | } |
| 13518 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 13519 | void |
| 13520 | _Py_ReleaseInternedUnicodeStrings(void) |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13521 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13522 | PyObject *keys; |
| 13523 | PyUnicodeObject *s; |
| 13524 | Py_ssize_t i, n; |
| 13525 | Py_ssize_t immortal_size = 0, mortal_size = 0; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13526 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13527 | if (interned == NULL || !PyDict_Check(interned)) |
| 13528 | return; |
| 13529 | keys = PyDict_Keys(interned); |
| 13530 | if (keys == NULL || !PyList_Check(keys)) { |
| 13531 | PyErr_Clear(); |
| 13532 | return; |
| 13533 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13534 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13535 | /* Since _Py_ReleaseInternedUnicodeStrings() is intended to help a leak |
| 13536 | detector, interned unicode strings are not forcibly deallocated; |
| 13537 | rather, we give them their stolen references back, and then clear |
| 13538 | and DECREF the interned dict. */ |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13539 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13540 | n = PyList_GET_SIZE(keys); |
| 13541 | fprintf(stderr, "releasing %" PY_FORMAT_SIZE_T "d interned strings\n", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13542 | n); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13543 | for (i = 0; i < n; i++) { |
| 13544 | s = (PyUnicodeObject *) PyList_GET_ITEM(keys, i); |
Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 13545 | if (PyUnicode_READY(s) == -1) { |
| 13546 | assert(0 && "could not ready string"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13547 | fprintf(stderr, "could not ready string\n"); |
Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 13548 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13549 | switch (PyUnicode_CHECK_INTERNED(s)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13550 | case SSTATE_NOT_INTERNED: |
| 13551 | /* XXX Shouldn't happen */ |
| 13552 | break; |
| 13553 | case SSTATE_INTERNED_IMMORTAL: |
| 13554 | Py_REFCNT(s) += 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13555 | immortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13556 | break; |
| 13557 | case SSTATE_INTERNED_MORTAL: |
| 13558 | Py_REFCNT(s) += 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13559 | mortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13560 | break; |
| 13561 | default: |
| 13562 | Py_FatalError("Inconsistent interned string state."); |
| 13563 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13564 | _PyUnicode_STATE(s).interned = SSTATE_NOT_INTERNED; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13565 | } |
| 13566 | fprintf(stderr, "total size of all interned strings: " |
| 13567 | "%" PY_FORMAT_SIZE_T "d/%" PY_FORMAT_SIZE_T "d " |
| 13568 | "mortal/immortal\n", mortal_size, immortal_size); |
| 13569 | Py_DECREF(keys); |
| 13570 | PyDict_Clear(interned); |
| 13571 | Py_DECREF(interned); |
| 13572 | interned = NULL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13573 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13574 | |
| 13575 | |
| 13576 | /********************* Unicode Iterator **************************/ |
| 13577 | |
| 13578 | typedef struct { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13579 | PyObject_HEAD |
| 13580 | Py_ssize_t it_index; |
| 13581 | PyUnicodeObject *it_seq; /* Set to NULL when iterator is exhausted */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13582 | } unicodeiterobject; |
| 13583 | |
| 13584 | static void |
| 13585 | unicodeiter_dealloc(unicodeiterobject *it) |
| 13586 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13587 | _PyObject_GC_UNTRACK(it); |
| 13588 | Py_XDECREF(it->it_seq); |
| 13589 | PyObject_GC_Del(it); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13590 | } |
| 13591 | |
| 13592 | static int |
| 13593 | unicodeiter_traverse(unicodeiterobject *it, visitproc visit, void *arg) |
| 13594 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13595 | Py_VISIT(it->it_seq); |
| 13596 | return 0; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13597 | } |
| 13598 | |
| 13599 | static PyObject * |
| 13600 | unicodeiter_next(unicodeiterobject *it) |
| 13601 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13602 | PyUnicodeObject *seq; |
| 13603 | PyObject *item; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13604 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13605 | assert(it != NULL); |
| 13606 | seq = it->it_seq; |
| 13607 | if (seq == NULL) |
| 13608 | return NULL; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 13609 | assert(_PyUnicode_CHECK(seq)); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13610 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13611 | if (it->it_index < PyUnicode_GET_LENGTH(seq)) { |
| 13612 | int kind = PyUnicode_KIND(seq); |
| 13613 | void *data = PyUnicode_DATA(seq); |
| 13614 | Py_UCS4 chr = PyUnicode_READ(kind, data, it->it_index); |
| 13615 | item = PyUnicode_FromOrdinal(chr); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13616 | if (item != NULL) |
| 13617 | ++it->it_index; |
| 13618 | return item; |
| 13619 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13620 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13621 | Py_DECREF(seq); |
| 13622 | it->it_seq = NULL; |
| 13623 | return NULL; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13624 | } |
| 13625 | |
| 13626 | static PyObject * |
| 13627 | unicodeiter_len(unicodeiterobject *it) |
| 13628 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13629 | Py_ssize_t len = 0; |
| 13630 | if (it->it_seq) |
| 13631 | len = PyUnicode_GET_SIZE(it->it_seq) - it->it_index; |
| 13632 | return PyLong_FromSsize_t(len); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13633 | } |
| 13634 | |
| 13635 | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
| 13636 | |
| 13637 | static PyMethodDef unicodeiter_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13638 | {"__length_hint__", (PyCFunction)unicodeiter_len, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13639 | length_hint_doc}, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13640 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13641 | }; |
| 13642 | |
| 13643 | PyTypeObject PyUnicodeIter_Type = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13644 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 13645 | "str_iterator", /* tp_name */ |
| 13646 | sizeof(unicodeiterobject), /* tp_basicsize */ |
| 13647 | 0, /* tp_itemsize */ |
| 13648 | /* methods */ |
| 13649 | (destructor)unicodeiter_dealloc, /* tp_dealloc */ |
| 13650 | 0, /* tp_print */ |
| 13651 | 0, /* tp_getattr */ |
| 13652 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 13653 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13654 | 0, /* tp_repr */ |
| 13655 | 0, /* tp_as_number */ |
| 13656 | 0, /* tp_as_sequence */ |
| 13657 | 0, /* tp_as_mapping */ |
| 13658 | 0, /* tp_hash */ |
| 13659 | 0, /* tp_call */ |
| 13660 | 0, /* tp_str */ |
| 13661 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 13662 | 0, /* tp_setattro */ |
| 13663 | 0, /* tp_as_buffer */ |
| 13664 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 13665 | 0, /* tp_doc */ |
| 13666 | (traverseproc)unicodeiter_traverse, /* tp_traverse */ |
| 13667 | 0, /* tp_clear */ |
| 13668 | 0, /* tp_richcompare */ |
| 13669 | 0, /* tp_weaklistoffset */ |
| 13670 | PyObject_SelfIter, /* tp_iter */ |
| 13671 | (iternextfunc)unicodeiter_next, /* tp_iternext */ |
| 13672 | unicodeiter_methods, /* tp_methods */ |
| 13673 | 0, |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13674 | }; |
| 13675 | |
| 13676 | static PyObject * |
| 13677 | unicode_iter(PyObject *seq) |
| 13678 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13679 | unicodeiterobject *it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13680 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13681 | if (!PyUnicode_Check(seq)) { |
| 13682 | PyErr_BadInternalCall(); |
| 13683 | return NULL; |
| 13684 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13685 | if (PyUnicode_READY(seq) == -1) |
| 13686 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13687 | it = PyObject_GC_New(unicodeiterobject, &PyUnicodeIter_Type); |
| 13688 | if (it == NULL) |
| 13689 | return NULL; |
| 13690 | it->it_index = 0; |
| 13691 | Py_INCREF(seq); |
| 13692 | it->it_seq = (PyUnicodeObject *)seq; |
| 13693 | _PyObject_GC_TRACK(it); |
| 13694 | return (PyObject *)it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13695 | } |
| 13696 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13697 | #define UNIOP(x) Py_UNICODE_##x |
| 13698 | #define UNIOP_t Py_UNICODE |
| 13699 | #include "uniops.h" |
| 13700 | #undef UNIOP |
| 13701 | #undef UNIOP_t |
| 13702 | #define UNIOP(x) Py_UCS4_##x |
| 13703 | #define UNIOP_t Py_UCS4 |
| 13704 | #include "uniops.h" |
| 13705 | #undef UNIOP |
| 13706 | #undef UNIOP_t |
Victor Stinner | 331ea92 | 2010-08-10 16:37:20 +0000 | [diff] [blame] | 13707 | |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 13708 | Py_UNICODE* |
Victor Stinner | 4640860 | 2010-09-03 16:18:00 +0000 | [diff] [blame] | 13709 | PyUnicode_AsUnicodeCopy(PyObject *object) |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 13710 | { |
| 13711 | PyUnicodeObject *unicode = (PyUnicodeObject *)object; |
| 13712 | Py_UNICODE *copy; |
| 13713 | Py_ssize_t size; |
| 13714 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13715 | if (!PyUnicode_Check(unicode)) { |
| 13716 | PyErr_BadArgument(); |
| 13717 | return NULL; |
| 13718 | } |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 13719 | /* Ensure we won't overflow the size. */ |
| 13720 | if (PyUnicode_GET_SIZE(unicode) > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
| 13721 | PyErr_NoMemory(); |
| 13722 | return NULL; |
| 13723 | } |
| 13724 | size = PyUnicode_GET_SIZE(unicode) + 1; /* copy the nul character */ |
| 13725 | size *= sizeof(Py_UNICODE); |
| 13726 | copy = PyMem_Malloc(size); |
| 13727 | if (copy == NULL) { |
| 13728 | PyErr_NoMemory(); |
| 13729 | return NULL; |
| 13730 | } |
| 13731 | memcpy(copy, PyUnicode_AS_UNICODE(unicode), size); |
| 13732 | return copy; |
| 13733 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 13734 | |
Georg Brandl | 66c221e | 2010-10-14 07:04:07 +0000 | [diff] [blame] | 13735 | /* A _string module, to export formatter_parser and formatter_field_name_split |
| 13736 | to the string.Formatter class implemented in Python. */ |
| 13737 | |
| 13738 | static PyMethodDef _string_methods[] = { |
| 13739 | {"formatter_field_name_split", (PyCFunction) formatter_field_name_split, |
| 13740 | METH_O, PyDoc_STR("split the argument as a field name")}, |
| 13741 | {"formatter_parser", (PyCFunction) formatter_parser, |
| 13742 | METH_O, PyDoc_STR("parse the argument as a format string")}, |
| 13743 | {NULL, NULL} |
| 13744 | }; |
| 13745 | |
| 13746 | static struct PyModuleDef _string_module = { |
| 13747 | PyModuleDef_HEAD_INIT, |
| 13748 | "_string", |
| 13749 | PyDoc_STR("string helper module"), |
| 13750 | 0, |
| 13751 | _string_methods, |
| 13752 | NULL, |
| 13753 | NULL, |
| 13754 | NULL, |
| 13755 | NULL |
| 13756 | }; |
| 13757 | |
| 13758 | PyMODINIT_FUNC |
| 13759 | PyInit__string(void) |
| 13760 | { |
| 13761 | return PyModule_Create(&_string_module); |
| 13762 | } |
| 13763 | |
| 13764 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 13765 | #ifdef __cplusplus |
| 13766 | } |
| 13767 | #endif |