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 | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 242 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 243 | static PyObject * |
| 244 | unicode_encode_call_errorhandler(const char *errors, |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 245 | PyObject **errorHandler,const char *encoding, const char *reason, |
| 246 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 247 | Py_ssize_t startpos, Py_ssize_t endpos, Py_ssize_t *newpos); |
| 248 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 249 | static void |
| 250 | raise_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 251 | const char *encoding, |
| 252 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 253 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 254 | const char *reason); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 255 | |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 256 | /* Same for linebreaks */ |
| 257 | static unsigned char ascii_linebreak[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 258 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 259 | /* 0x000A, * LINE FEED */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 260 | /* 0x000B, * LINE TABULATION */ |
| 261 | /* 0x000C, * FORM FEED */ |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 262 | /* 0x000D, * CARRIAGE RETURN */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 263 | 0, 0, 1, 1, 1, 1, 0, 0, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 264 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 265 | /* 0x001C, * FILE SEPARATOR */ |
| 266 | /* 0x001D, * GROUP SEPARATOR */ |
| 267 | /* 0x001E, * RECORD SEPARATOR */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 268 | 0, 0, 0, 0, 1, 1, 1, 0, |
| 269 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 270 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 271 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 272 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 273 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 274 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 275 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 276 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 277 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 278 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 279 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 280 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 281 | 0, 0, 0, 0, 0, 0, 0, 0 |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 282 | }; |
| 283 | |
Ezio Melotti | 48a2f8f | 2011-09-29 00:18:19 +0300 | [diff] [blame] | 284 | /* The max unicode value is always 0x10FFFF while using the PEP-393 API. |
| 285 | 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] | 286 | Py_UNICODE |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 287 | PyUnicode_GetMax(void) |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 288 | { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 289 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 290 | return 0x10FFFF; |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 291 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 292 | /* This is actually an illegal character, so it should |
| 293 | not be passed to unichr. */ |
| 294 | return 0xFFFF; |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 295 | #endif |
| 296 | } |
| 297 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 298 | #ifdef Py_DEBUG |
| 299 | static int |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 300 | /* FIXME: use PyObject* type for op */ |
| 301 | _PyUnicode_CheckConsistency(void *op, int check_content) |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 302 | { |
| 303 | PyASCIIObject *ascii; |
| 304 | unsigned int kind; |
| 305 | |
| 306 | assert(PyUnicode_Check(op)); |
| 307 | |
| 308 | ascii = (PyASCIIObject *)op; |
| 309 | kind = ascii->state.kind; |
| 310 | |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 311 | if (ascii->state.ascii == 1 && ascii->state.compact == 1) { |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 312 | assert(kind == PyUnicode_1BYTE_KIND); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 313 | assert(ascii->state.ready == 1); |
| 314 | } |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 315 | else { |
Victor Stinner | 85041a5 | 2011-10-03 14:42:39 +0200 | [diff] [blame] | 316 | PyCompactUnicodeObject *compact = (PyCompactUnicodeObject *)op; |
Victor Stinner | 7f11ad4 | 2011-10-04 00:00:20 +0200 | [diff] [blame] | 317 | void *data; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 318 | |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 319 | if (ascii->state.compact == 1) { |
| 320 | data = compact + 1; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 321 | assert(kind == PyUnicode_1BYTE_KIND |
| 322 | || kind == PyUnicode_2BYTE_KIND |
| 323 | || kind == PyUnicode_4BYTE_KIND); |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 324 | assert(ascii->state.ascii == 0); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 325 | assert(ascii->state.ready == 1); |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 326 | assert (compact->utf8 != data); |
| 327 | } else { |
| 328 | PyUnicodeObject *unicode = (PyUnicodeObject *)op; |
| 329 | |
| 330 | data = unicode->data.any; |
| 331 | if (kind == PyUnicode_WCHAR_KIND) { |
| 332 | assert(ascii->state.compact == 0); |
| 333 | assert(ascii->state.ascii == 0); |
| 334 | assert(ascii->state.ready == 0); |
| 335 | assert(ascii->wstr != NULL); |
| 336 | assert(data == NULL); |
| 337 | assert(compact->utf8 == NULL); |
| 338 | assert(ascii->state.interned == SSTATE_NOT_INTERNED); |
| 339 | } |
| 340 | else { |
| 341 | assert(kind == PyUnicode_1BYTE_KIND |
| 342 | || kind == PyUnicode_2BYTE_KIND |
| 343 | || kind == PyUnicode_4BYTE_KIND); |
| 344 | assert(ascii->state.compact == 0); |
| 345 | assert(ascii->state.ready == 1); |
| 346 | assert(data != NULL); |
| 347 | if (ascii->state.ascii) { |
| 348 | assert (compact->utf8 == data); |
| 349 | assert (compact->utf8_length == ascii->length); |
| 350 | } |
| 351 | else |
| 352 | assert (compact->utf8 != data); |
| 353 | } |
| 354 | } |
| 355 | if (kind != PyUnicode_WCHAR_KIND) { |
Victor Stinner | 7f11ad4 | 2011-10-04 00:00:20 +0200 | [diff] [blame] | 356 | if ( |
| 357 | #if SIZEOF_WCHAR_T == 2 |
| 358 | kind == PyUnicode_2BYTE_KIND |
| 359 | #else |
| 360 | kind == PyUnicode_4BYTE_KIND |
| 361 | #endif |
| 362 | ) |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 363 | { |
| 364 | assert(ascii->wstr == data); |
| 365 | assert(compact->wstr_length == ascii->length); |
| 366 | } else |
| 367 | assert(ascii->wstr != data); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 368 | } |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 369 | |
| 370 | if (compact->utf8 == NULL) |
| 371 | assert(compact->utf8_length == 0); |
| 372 | if (ascii->wstr == NULL) |
| 373 | assert(compact->wstr_length == 0); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 374 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 375 | /* check that the best kind is used */ |
| 376 | if (check_content && kind != PyUnicode_WCHAR_KIND) |
| 377 | { |
| 378 | Py_ssize_t i; |
| 379 | Py_UCS4 maxchar = 0; |
| 380 | void *data = PyUnicode_DATA(ascii); |
| 381 | for (i=0; i < ascii->length; i++) |
| 382 | { |
| 383 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 384 | if (ch > maxchar) |
| 385 | maxchar = ch; |
| 386 | } |
| 387 | if (kind == PyUnicode_1BYTE_KIND) { |
| 388 | if (ascii->state.ascii == 0) |
| 389 | assert(maxchar >= 128); |
| 390 | else |
| 391 | assert(maxchar < 128); |
| 392 | } |
| 393 | else if (kind == PyUnicode_2BYTE_KIND) |
| 394 | assert(maxchar >= 0x100); |
| 395 | else |
| 396 | assert(maxchar >= 0x10000); |
| 397 | } |
Benjamin Peterson | ccc51c1 | 2011-10-03 19:34:12 -0400 | [diff] [blame] | 398 | return 1; |
| 399 | } |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 400 | #endif |
| 401 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 402 | /* --- Bloom Filters ----------------------------------------------------- */ |
| 403 | |
| 404 | /* stuff to implement simple "bloom filters" for Unicode characters. |
| 405 | to keep things simple, we use a single bitmask, using the least 5 |
| 406 | bits from each unicode characters as the bit index. */ |
| 407 | |
| 408 | /* the linebreak mask is set up by Unicode_Init below */ |
| 409 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 410 | #if LONG_BIT >= 128 |
| 411 | #define BLOOM_WIDTH 128 |
| 412 | #elif LONG_BIT >= 64 |
| 413 | #define BLOOM_WIDTH 64 |
| 414 | #elif LONG_BIT >= 32 |
| 415 | #define BLOOM_WIDTH 32 |
| 416 | #else |
| 417 | #error "LONG_BIT is smaller than 32" |
| 418 | #endif |
| 419 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 420 | #define BLOOM_MASK unsigned long |
| 421 | |
| 422 | static BLOOM_MASK bloom_linebreak; |
| 423 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 424 | #define BLOOM_ADD(mask, ch) ((mask |= (1UL << ((ch) & (BLOOM_WIDTH - 1))))) |
| 425 | #define BLOOM(mask, ch) ((mask & (1UL << ((ch) & (BLOOM_WIDTH - 1))))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 426 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 427 | #define BLOOM_LINEBREAK(ch) \ |
| 428 | ((ch) < 128U ? ascii_linebreak[(ch)] : \ |
| 429 | (BLOOM(bloom_linebreak, (ch)) && Py_UNICODE_ISLINEBREAK(ch))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 430 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 431 | Py_LOCAL_INLINE(BLOOM_MASK) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 432 | make_bloom_mask(int kind, void* ptr, Py_ssize_t len) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 433 | { |
| 434 | /* calculate simple bloom-style bitmask for a given unicode string */ |
| 435 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 436 | BLOOM_MASK mask; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 437 | Py_ssize_t i; |
| 438 | |
| 439 | mask = 0; |
| 440 | for (i = 0; i < len; i++) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 441 | BLOOM_ADD(mask, PyUnicode_READ(kind, ptr, i)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 442 | |
| 443 | return mask; |
| 444 | } |
| 445 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 446 | #define BLOOM_MEMBER(mask, chr, str) \ |
| 447 | (BLOOM(mask, chr) \ |
| 448 | && (PyUnicode_FindChar(str, chr, 0, PyUnicode_GET_LENGTH(str), 1) >= 0)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 449 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 450 | /* --- Unicode Object ----------------------------------------------------- */ |
| 451 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 452 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 453 | fixup(PyObject *self, Py_UCS4 (*fixfct)(PyObject *s)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 454 | |
| 455 | Py_LOCAL_INLINE(char *) findchar(void *s, int kind, |
| 456 | Py_ssize_t size, Py_UCS4 ch, |
| 457 | int direction) |
| 458 | { |
| 459 | /* like wcschr, but doesn't stop at NULL characters */ |
| 460 | Py_ssize_t i; |
| 461 | if (direction == 1) { |
| 462 | for(i = 0; i < size; i++) |
| 463 | if (PyUnicode_READ(kind, s, i) == ch) |
| 464 | return (char*)s + PyUnicode_KIND_SIZE(kind, i); |
| 465 | } |
| 466 | else { |
| 467 | for(i = size-1; i >= 0; i--) |
| 468 | if (PyUnicode_READ(kind, s, i) == ch) |
| 469 | return (char*)s + PyUnicode_KIND_SIZE(kind, i); |
| 470 | } |
| 471 | return NULL; |
| 472 | } |
| 473 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 474 | static PyObject* |
| 475 | resize_compact(PyObject *unicode, Py_ssize_t length) |
| 476 | { |
| 477 | Py_ssize_t char_size; |
| 478 | Py_ssize_t struct_size; |
| 479 | Py_ssize_t new_size; |
| 480 | int share_wstr; |
| 481 | |
| 482 | assert(PyUnicode_IS_READY(unicode)); |
| 483 | char_size = PyUnicode_CHARACTER_SIZE(unicode); |
| 484 | if (PyUnicode_IS_COMPACT_ASCII(unicode)) |
| 485 | struct_size = sizeof(PyASCIIObject); |
| 486 | else |
| 487 | struct_size = sizeof(PyCompactUnicodeObject); |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 488 | share_wstr = _PyUnicode_SHARE_WSTR(unicode); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 489 | |
| 490 | _Py_DEC_REFTOTAL; |
| 491 | _Py_ForgetReference(unicode); |
| 492 | |
| 493 | if (length > ((PY_SSIZE_T_MAX - struct_size) / char_size - 1)) { |
| 494 | PyErr_NoMemory(); |
| 495 | return NULL; |
| 496 | } |
| 497 | new_size = (struct_size + (length + 1) * char_size); |
| 498 | |
| 499 | unicode = (PyObject *)PyObject_REALLOC((char *)unicode, new_size); |
| 500 | if (unicode == NULL) { |
| 501 | PyObject_Del(unicode); |
| 502 | PyErr_NoMemory(); |
| 503 | return NULL; |
| 504 | } |
| 505 | _Py_NewReference(unicode); |
| 506 | _PyUnicode_LENGTH(unicode) = length; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 507 | if (share_wstr) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 508 | _PyUnicode_WSTR(unicode) = PyUnicode_DATA(unicode); |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 509 | if (!PyUnicode_IS_COMPACT_ASCII(unicode)) |
| 510 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 511 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 512 | PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode), |
| 513 | length, 0); |
| 514 | return unicode; |
| 515 | } |
| 516 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 517 | static int |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 518 | resize_inplace(PyUnicodeObject *unicode, Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 519 | { |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 520 | wchar_t *wstr; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 521 | assert(!PyUnicode_IS_COMPACT(unicode)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 522 | assert(Py_REFCNT(unicode) == 1); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 523 | |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 524 | _PyUnicode_DIRTY(unicode); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 525 | |
| 526 | if (PyUnicode_IS_READY(unicode)) { |
| 527 | Py_ssize_t char_size; |
| 528 | Py_ssize_t new_size; |
Victor Stinner | 1c8d0c7 | 2011-10-03 12:11:00 +0200 | [diff] [blame] | 529 | int share_wstr, share_utf8; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 530 | void *data; |
| 531 | |
| 532 | data = _PyUnicode_DATA_ANY(unicode); |
| 533 | assert(data != NULL); |
| 534 | char_size = PyUnicode_CHARACTER_SIZE(unicode); |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 535 | share_wstr = _PyUnicode_SHARE_WSTR(unicode); |
| 536 | share_utf8 = _PyUnicode_SHARE_UTF8(unicode); |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 537 | if (!share_utf8 && _PyUnicode_HAS_UTF8_MEMORY(unicode)) |
| 538 | { |
| 539 | PyObject_DEL(_PyUnicode_UTF8(unicode)); |
| 540 | _PyUnicode_UTF8(unicode) = NULL; |
| 541 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
| 542 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 543 | |
| 544 | if (length > (PY_SSIZE_T_MAX / char_size - 1)) { |
| 545 | PyErr_NoMemory(); |
| 546 | return -1; |
| 547 | } |
| 548 | new_size = (length + 1) * char_size; |
| 549 | |
| 550 | data = (PyObject *)PyObject_REALLOC(data, new_size); |
| 551 | if (data == NULL) { |
| 552 | PyErr_NoMemory(); |
| 553 | return -1; |
| 554 | } |
| 555 | _PyUnicode_DATA_ANY(unicode) = data; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 556 | if (share_wstr) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 557 | _PyUnicode_WSTR(unicode) = data; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 558 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 559 | } |
| 560 | if (share_utf8) { |
Victor Stinner | 1c8d0c7 | 2011-10-03 12:11:00 +0200 | [diff] [blame] | 561 | _PyUnicode_UTF8(unicode) = data; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 562 | _PyUnicode_UTF8_LENGTH(unicode) = length; |
| 563 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 564 | _PyUnicode_LENGTH(unicode) = length; |
| 565 | PyUnicode_WRITE(PyUnicode_KIND(unicode), data, length, 0); |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 566 | if (share_wstr || _PyUnicode_WSTR(unicode) == NULL) { |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 567 | assert(_PyUnicode_CheckConsistency(unicode, 0)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 568 | return 0; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 569 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 570 | } |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 571 | assert(_PyUnicode_WSTR(unicode) != NULL); |
| 572 | |
| 573 | /* check for integer overflow */ |
| 574 | if (length > PY_SSIZE_T_MAX / sizeof(wchar_t) - 1) { |
| 575 | PyErr_NoMemory(); |
| 576 | return -1; |
| 577 | } |
| 578 | wstr = _PyUnicode_WSTR(unicode); |
| 579 | wstr = PyObject_REALLOC(wstr, sizeof(wchar_t) * (length + 1)); |
| 580 | if (!wstr) { |
| 581 | PyErr_NoMemory(); |
| 582 | return -1; |
| 583 | } |
| 584 | _PyUnicode_WSTR(unicode) = wstr; |
| 585 | _PyUnicode_WSTR(unicode)[length] = 0; |
| 586 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 587 | assert(_PyUnicode_CheckConsistency(unicode, 0)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 588 | return 0; |
| 589 | } |
| 590 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 591 | static PyObject* |
| 592 | resize_copy(PyObject *unicode, Py_ssize_t length) |
| 593 | { |
| 594 | Py_ssize_t copy_length; |
| 595 | if (PyUnicode_IS_COMPACT(unicode)) { |
| 596 | PyObject *copy; |
| 597 | assert(PyUnicode_IS_READY(unicode)); |
| 598 | |
| 599 | copy = PyUnicode_New(length, PyUnicode_MAX_CHAR_VALUE(unicode)); |
| 600 | if (copy == NULL) |
| 601 | return NULL; |
| 602 | |
| 603 | copy_length = Py_MIN(length, PyUnicode_GET_LENGTH(unicode)); |
| 604 | if (PyUnicode_CopyCharacters(copy, 0, |
| 605 | unicode, 0, |
| 606 | copy_length) < 0) |
| 607 | { |
| 608 | Py_DECREF(copy); |
| 609 | return NULL; |
| 610 | } |
| 611 | return copy; |
Victor Stinner | 8cfcbed | 2011-10-03 23:19:21 +0200 | [diff] [blame] | 612 | } |
| 613 | else { |
Victor Stinner | 2fd8227 | 2011-10-03 04:06:05 +0200 | [diff] [blame] | 614 | PyUnicodeObject *w; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 615 | assert(_PyUnicode_WSTR(unicode) != NULL); |
| 616 | assert(_PyUnicode_DATA_ANY(unicode) == NULL); |
Victor Stinner | 2fd8227 | 2011-10-03 04:06:05 +0200 | [diff] [blame] | 617 | w = _PyUnicode_New(length); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 618 | if (w == NULL) |
| 619 | return NULL; |
| 620 | copy_length = _PyUnicode_WSTR_LENGTH(unicode); |
| 621 | copy_length = Py_MIN(copy_length, length); |
| 622 | Py_UNICODE_COPY(_PyUnicode_WSTR(w), _PyUnicode_WSTR(unicode), |
| 623 | copy_length); |
| 624 | return (PyObject*)w; |
| 625 | } |
| 626 | } |
| 627 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 628 | /* 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] | 629 | Ux0000 terminated; some code (e.g. new_identifier) |
| 630 | relies on that. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 631 | |
| 632 | XXX This allocator could further be enhanced by assuring that the |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 633 | free list never reduces its size below 1. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 634 | |
| 635 | */ |
| 636 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 637 | #ifdef Py_DEBUG |
| 638 | int unicode_old_new_calls = 0; |
| 639 | #endif |
| 640 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 641 | static PyUnicodeObject * |
| 642 | _PyUnicode_New(Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 643 | { |
| 644 | register PyUnicodeObject *unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 645 | size_t new_size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 646 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 647 | /* Optimization for empty strings */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 648 | if (length == 0 && unicode_empty != NULL) { |
| 649 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 650 | return (PyUnicodeObject*)unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 651 | } |
| 652 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 653 | /* Ensure we won't overflow the size. */ |
| 654 | if (length > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
| 655 | return (PyUnicodeObject *)PyErr_NoMemory(); |
| 656 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 657 | if (length < 0) { |
| 658 | PyErr_SetString(PyExc_SystemError, |
| 659 | "Negative size passed to _PyUnicode_New"); |
| 660 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 661 | } |
| 662 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 663 | #ifdef Py_DEBUG |
| 664 | ++unicode_old_new_calls; |
| 665 | #endif |
| 666 | |
| 667 | unicode = PyObject_New(PyUnicodeObject, &PyUnicode_Type); |
| 668 | if (unicode == NULL) |
| 669 | return NULL; |
| 670 | new_size = sizeof(Py_UNICODE) * ((size_t)length + 1); |
| 671 | _PyUnicode_WSTR(unicode) = (Py_UNICODE*) PyObject_MALLOC(new_size); |
| 672 | if (!_PyUnicode_WSTR(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 673 | PyErr_NoMemory(); |
| 674 | goto onError; |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 675 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 676 | |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 677 | /* Initialize the first element to guard against cases where |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 678 | * the caller fails before initializing str -- unicode_resize() |
| 679 | * reads str[0], and the Keep-Alive optimization can keep memory |
| 680 | * allocated for str alive across a call to unicode_dealloc(unicode). |
| 681 | * We don't want unicode_resize to read uninitialized memory in |
| 682 | * that case. |
| 683 | */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 684 | _PyUnicode_WSTR(unicode)[0] = 0; |
| 685 | _PyUnicode_WSTR(unicode)[length] = 0; |
| 686 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 687 | _PyUnicode_HASH(unicode) = -1; |
| 688 | _PyUnicode_STATE(unicode).interned = 0; |
| 689 | _PyUnicode_STATE(unicode).kind = 0; |
| 690 | _PyUnicode_STATE(unicode).compact = 0; |
| 691 | _PyUnicode_STATE(unicode).ready = 0; |
| 692 | _PyUnicode_STATE(unicode).ascii = 0; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 693 | _PyUnicode_DATA_ANY(unicode) = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 694 | _PyUnicode_LENGTH(unicode) = 0; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 695 | _PyUnicode_UTF8(unicode) = NULL; |
| 696 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 697 | return unicode; |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 698 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 699 | onError: |
Amaury Forgeot d'Arc | 7888d08 | 2008-08-01 01:06:32 +0000 | [diff] [blame] | 700 | /* XXX UNREF/NEWREF interface should be more symmetrical */ |
| 701 | _Py_DEC_REFTOTAL; |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 702 | _Py_ForgetReference((PyObject *)unicode); |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 703 | PyObject_Del(unicode); |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 704 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 705 | } |
| 706 | |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 707 | static const char* |
| 708 | unicode_kind_name(PyObject *unicode) |
| 709 | { |
Victor Stinner | 42dfd71 | 2011-10-03 14:41:45 +0200 | [diff] [blame] | 710 | /* don't check consistency: unicode_kind_name() is called from |
| 711 | _PyUnicode_Dump() */ |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 712 | if (!PyUnicode_IS_COMPACT(unicode)) |
| 713 | { |
| 714 | if (!PyUnicode_IS_READY(unicode)) |
| 715 | return "wstr"; |
| 716 | switch(PyUnicode_KIND(unicode)) |
| 717 | { |
| 718 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 719 | if (PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 720 | return "legacy ascii"; |
| 721 | else |
| 722 | return "legacy latin1"; |
| 723 | case PyUnicode_2BYTE_KIND: |
| 724 | return "legacy UCS2"; |
| 725 | case PyUnicode_4BYTE_KIND: |
| 726 | return "legacy UCS4"; |
| 727 | default: |
| 728 | return "<legacy invalid kind>"; |
| 729 | } |
| 730 | } |
| 731 | assert(PyUnicode_IS_READY(unicode)); |
| 732 | switch(PyUnicode_KIND(unicode)) |
| 733 | { |
| 734 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 735 | if (PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 736 | return "ascii"; |
| 737 | else |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 738 | return "latin1"; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 739 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 740 | return "UCS2"; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 741 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 742 | return "UCS4"; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 743 | default: |
| 744 | return "<invalid compact kind>"; |
| 745 | } |
| 746 | } |
| 747 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 748 | #ifdef Py_DEBUG |
| 749 | int unicode_new_new_calls = 0; |
| 750 | |
| 751 | /* Functions wrapping macros for use in debugger */ |
| 752 | char *_PyUnicode_utf8(void *unicode){ |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 753 | return PyUnicode_UTF8(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 754 | } |
| 755 | |
| 756 | void *_PyUnicode_compact_data(void *unicode) { |
| 757 | return _PyUnicode_COMPACT_DATA(unicode); |
| 758 | } |
| 759 | void *_PyUnicode_data(void *unicode){ |
| 760 | printf("obj %p\n", unicode); |
| 761 | printf("compact %d\n", PyUnicode_IS_COMPACT(unicode)); |
| 762 | printf("compact ascii %d\n", PyUnicode_IS_COMPACT_ASCII(unicode)); |
| 763 | printf("ascii op %p\n", ((void*)((PyASCIIObject*)(unicode) + 1))); |
| 764 | printf("compact op %p\n", ((void*)((PyCompactUnicodeObject*)(unicode) + 1))); |
| 765 | printf("compact data %p\n", _PyUnicode_COMPACT_DATA(unicode)); |
| 766 | return PyUnicode_DATA(unicode); |
| 767 | } |
Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 768 | |
| 769 | void |
| 770 | _PyUnicode_Dump(PyObject *op) |
| 771 | { |
| 772 | PyASCIIObject *ascii = (PyASCIIObject *)op; |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 773 | PyCompactUnicodeObject *compact = (PyCompactUnicodeObject *)op; |
| 774 | PyUnicodeObject *unicode = (PyUnicodeObject *)op; |
| 775 | void *data; |
| 776 | printf("%s: len=%zu, ",unicode_kind_name(op), ascii->length); |
| 777 | if (ascii->state.compact) |
| 778 | data = (compact + 1); |
| 779 | else |
| 780 | data = unicode->data.any; |
| 781 | if (ascii->wstr == data) |
| 782 | printf("shared "); |
| 783 | printf("wstr=%p", ascii->wstr); |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 784 | if (!(ascii->state.ascii == 1 && ascii->state.compact == 1)) { |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 785 | printf(" (%zu), ", compact->wstr_length); |
| 786 | if (!ascii->state.compact && compact->utf8 == unicode->data.any) |
| 787 | printf("shared "); |
| 788 | printf("utf8=%p (%zu)", compact->utf8, compact->utf8_length); |
Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 789 | } |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 790 | printf(", data=%p\n", data); |
Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 791 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 792 | #endif |
| 793 | |
| 794 | PyObject * |
| 795 | PyUnicode_New(Py_ssize_t size, Py_UCS4 maxchar) |
| 796 | { |
| 797 | PyObject *obj; |
| 798 | PyCompactUnicodeObject *unicode; |
| 799 | void *data; |
| 800 | int kind_state; |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 801 | int is_sharing, is_ascii; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 802 | Py_ssize_t char_size; |
| 803 | Py_ssize_t struct_size; |
| 804 | |
| 805 | /* Optimization for empty strings */ |
| 806 | if (size == 0 && unicode_empty != NULL) { |
| 807 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 808 | return unicode_empty; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 809 | } |
| 810 | |
| 811 | #ifdef Py_DEBUG |
| 812 | ++unicode_new_new_calls; |
| 813 | #endif |
| 814 | |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 815 | is_ascii = 0; |
| 816 | is_sharing = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 817 | struct_size = sizeof(PyCompactUnicodeObject); |
| 818 | if (maxchar < 128) { |
| 819 | kind_state = PyUnicode_1BYTE_KIND; |
| 820 | char_size = 1; |
| 821 | is_ascii = 1; |
| 822 | struct_size = sizeof(PyASCIIObject); |
| 823 | } |
| 824 | else if (maxchar < 256) { |
| 825 | kind_state = PyUnicode_1BYTE_KIND; |
| 826 | char_size = 1; |
| 827 | } |
| 828 | else if (maxchar < 65536) { |
| 829 | kind_state = PyUnicode_2BYTE_KIND; |
| 830 | char_size = 2; |
| 831 | if (sizeof(wchar_t) == 2) |
| 832 | is_sharing = 1; |
| 833 | } |
| 834 | else { |
| 835 | kind_state = PyUnicode_4BYTE_KIND; |
| 836 | char_size = 4; |
| 837 | if (sizeof(wchar_t) == 4) |
| 838 | is_sharing = 1; |
| 839 | } |
| 840 | |
| 841 | /* Ensure we won't overflow the size. */ |
| 842 | if (size < 0) { |
| 843 | PyErr_SetString(PyExc_SystemError, |
| 844 | "Negative size passed to PyUnicode_New"); |
| 845 | return NULL; |
| 846 | } |
| 847 | if (size > ((PY_SSIZE_T_MAX - struct_size) / char_size - 1)) |
| 848 | return PyErr_NoMemory(); |
| 849 | |
| 850 | /* Duplicated allocation code from _PyObject_New() instead of a call to |
| 851 | * PyObject_New() so we are able to allocate space for the object and |
| 852 | * it's data buffer. |
| 853 | */ |
| 854 | obj = (PyObject *) PyObject_MALLOC(struct_size + (size + 1) * char_size); |
| 855 | if (obj == NULL) |
| 856 | return PyErr_NoMemory(); |
| 857 | obj = PyObject_INIT(obj, &PyUnicode_Type); |
| 858 | if (obj == NULL) |
| 859 | return NULL; |
| 860 | |
| 861 | unicode = (PyCompactUnicodeObject *)obj; |
| 862 | if (is_ascii) |
| 863 | data = ((PyASCIIObject*)obj) + 1; |
| 864 | else |
| 865 | data = unicode + 1; |
| 866 | _PyUnicode_LENGTH(unicode) = size; |
| 867 | _PyUnicode_HASH(unicode) = -1; |
| 868 | _PyUnicode_STATE(unicode).interned = 0; |
| 869 | _PyUnicode_STATE(unicode).kind = kind_state; |
| 870 | _PyUnicode_STATE(unicode).compact = 1; |
| 871 | _PyUnicode_STATE(unicode).ready = 1; |
| 872 | _PyUnicode_STATE(unicode).ascii = is_ascii; |
| 873 | if (is_ascii) { |
| 874 | ((char*)data)[size] = 0; |
| 875 | _PyUnicode_WSTR(unicode) = NULL; |
| 876 | } |
| 877 | else if (kind_state == PyUnicode_1BYTE_KIND) { |
| 878 | ((char*)data)[size] = 0; |
| 879 | _PyUnicode_WSTR(unicode) = NULL; |
| 880 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 881 | unicode->utf8 = NULL; |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 882 | unicode->utf8_length = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 883 | } |
| 884 | else { |
| 885 | unicode->utf8 = NULL; |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 886 | unicode->utf8_length = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 887 | if (kind_state == PyUnicode_2BYTE_KIND) |
| 888 | ((Py_UCS2*)data)[size] = 0; |
| 889 | else /* kind_state == PyUnicode_4BYTE_KIND */ |
| 890 | ((Py_UCS4*)data)[size] = 0; |
| 891 | if (is_sharing) { |
| 892 | _PyUnicode_WSTR_LENGTH(unicode) = size; |
| 893 | _PyUnicode_WSTR(unicode) = (wchar_t *)data; |
| 894 | } |
| 895 | else { |
| 896 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 897 | _PyUnicode_WSTR(unicode) = NULL; |
| 898 | } |
| 899 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 900 | assert(_PyUnicode_CheckConsistency(unicode, 0)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 901 | return obj; |
| 902 | } |
| 903 | |
| 904 | #if SIZEOF_WCHAR_T == 2 |
| 905 | /* Helper function to convert a 16-bits wchar_t representation to UCS4, this |
| 906 | will decode surrogate pairs, the other conversions are implemented as macros |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 907 | for efficiency. |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 908 | |
| 909 | This function assumes that unicode can hold one more code point than wstr |
| 910 | characters for a terminating null character. */ |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 911 | static void |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 912 | unicode_convert_wchar_to_ucs4(const wchar_t *begin, const wchar_t *end, |
| 913 | PyUnicodeObject *unicode) |
| 914 | { |
| 915 | const wchar_t *iter; |
| 916 | Py_UCS4 *ucs4_out; |
| 917 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 918 | assert(unicode != NULL); |
| 919 | assert(_PyUnicode_CHECK(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 920 | assert(_PyUnicode_KIND(unicode) == PyUnicode_4BYTE_KIND); |
| 921 | ucs4_out = PyUnicode_4BYTE_DATA(unicode); |
| 922 | |
| 923 | for (iter = begin; iter < end; ) { |
| 924 | assert(ucs4_out < (PyUnicode_4BYTE_DATA(unicode) + |
| 925 | _PyUnicode_GET_LENGTH(unicode))); |
| 926 | if (*iter >= 0xD800 && *iter <= 0xDBFF |
| 927 | && (iter+1) < end && iter[1] >= 0xDC00 && iter[1] <= 0xDFFF) |
| 928 | { |
| 929 | *ucs4_out++ = (((iter[0] & 0x3FF)<<10) | (iter[1] & 0x3FF)) + 0x10000; |
| 930 | iter += 2; |
| 931 | } |
| 932 | else { |
| 933 | *ucs4_out++ = *iter; |
| 934 | iter++; |
| 935 | } |
| 936 | } |
| 937 | assert(ucs4_out == (PyUnicode_4BYTE_DATA(unicode) + |
| 938 | _PyUnicode_GET_LENGTH(unicode))); |
| 939 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 940 | } |
| 941 | #endif |
| 942 | |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 943 | static int |
| 944 | _PyUnicode_Dirty(PyObject *unicode) |
| 945 | { |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 946 | assert(_PyUnicode_CHECK(unicode)); |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 947 | if (Py_REFCNT(unicode) != 1) { |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 948 | PyErr_SetString(PyExc_SystemError, |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 949 | "Cannot modify a string having more than 1 reference"); |
| 950 | return -1; |
| 951 | } |
| 952 | _PyUnicode_DIRTY(unicode); |
| 953 | return 0; |
| 954 | } |
| 955 | |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 956 | Py_ssize_t |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 957 | PyUnicode_CopyCharacters(PyObject *to, Py_ssize_t to_start, |
| 958 | PyObject *from, Py_ssize_t from_start, |
| 959 | Py_ssize_t how_many) |
| 960 | { |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 961 | unsigned int from_kind, to_kind; |
| 962 | void *from_data, *to_data; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 963 | |
Victor Stinner | b153615 | 2011-09-30 02:26:10 +0200 | [diff] [blame] | 964 | if (!PyUnicode_Check(from) || !PyUnicode_Check(to)) { |
| 965 | PyErr_BadInternalCall(); |
| 966 | return -1; |
| 967 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 968 | |
| 969 | if (PyUnicode_READY(from)) |
| 970 | return -1; |
| 971 | if (PyUnicode_READY(to)) |
| 972 | return -1; |
| 973 | |
Victor Stinner | ff9e50f | 2011-09-28 22:17:19 +0200 | [diff] [blame] | 974 | how_many = Py_MIN(PyUnicode_GET_LENGTH(from), how_many); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 975 | if (to_start + how_many > PyUnicode_GET_LENGTH(to)) { |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 976 | PyErr_Format(PyExc_SystemError, |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 977 | "Cannot write %zi characters at %zi " |
| 978 | "in a string of %zi characters", |
| 979 | how_many, to_start, PyUnicode_GET_LENGTH(to)); |
| 980 | return -1; |
| 981 | } |
Victor Stinner | f5ca1a2 | 2011-09-28 23:54:59 +0200 | [diff] [blame] | 982 | if (how_many == 0) |
| 983 | return 0; |
| 984 | |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 985 | if (_PyUnicode_Dirty(to)) |
Victor Stinner | f5ca1a2 | 2011-09-28 23:54:59 +0200 | [diff] [blame] | 986 | return -1; |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 987 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 988 | from_kind = PyUnicode_KIND(from); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 989 | from_data = PyUnicode_DATA(from); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 990 | to_kind = PyUnicode_KIND(to); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 991 | to_data = PyUnicode_DATA(to); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 992 | |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 993 | if (from_kind == to_kind |
| 994 | /* deny latin1 => ascii */ |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 995 | && !(!PyUnicode_IS_ASCII(from) && PyUnicode_IS_ASCII(to))) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 996 | { |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 997 | Py_MEMCPY((char*)to_data |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 998 | + PyUnicode_KIND_SIZE(to_kind, to_start), |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 999 | (char*)from_data |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1000 | + PyUnicode_KIND_SIZE(from_kind, from_start), |
| 1001 | PyUnicode_KIND_SIZE(to_kind, how_many)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1002 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1003 | else if (from_kind == PyUnicode_1BYTE_KIND |
| 1004 | && to_kind == PyUnicode_2BYTE_KIND) |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1005 | { |
| 1006 | _PyUnicode_CONVERT_BYTES( |
| 1007 | Py_UCS1, Py_UCS2, |
| 1008 | PyUnicode_1BYTE_DATA(from) + from_start, |
| 1009 | PyUnicode_1BYTE_DATA(from) + from_start + how_many, |
| 1010 | PyUnicode_2BYTE_DATA(to) + to_start |
| 1011 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1012 | } |
Victor Stinner | 157f83f | 2011-09-28 21:41:31 +0200 | [diff] [blame] | 1013 | else if (from_kind == PyUnicode_1BYTE_KIND |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1014 | && to_kind == PyUnicode_4BYTE_KIND) |
| 1015 | { |
| 1016 | _PyUnicode_CONVERT_BYTES( |
| 1017 | Py_UCS1, Py_UCS4, |
| 1018 | PyUnicode_1BYTE_DATA(from) + from_start, |
| 1019 | PyUnicode_1BYTE_DATA(from) + from_start + how_many, |
| 1020 | PyUnicode_4BYTE_DATA(to) + to_start |
| 1021 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1022 | } |
| 1023 | else if (from_kind == PyUnicode_2BYTE_KIND |
| 1024 | && to_kind == PyUnicode_4BYTE_KIND) |
| 1025 | { |
| 1026 | _PyUnicode_CONVERT_BYTES( |
| 1027 | Py_UCS2, Py_UCS4, |
| 1028 | PyUnicode_2BYTE_DATA(from) + from_start, |
| 1029 | PyUnicode_2BYTE_DATA(from) + from_start + how_many, |
| 1030 | PyUnicode_4BYTE_DATA(to) + to_start |
| 1031 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1032 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1033 | else { |
| 1034 | int invalid_kinds; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 1035 | |
| 1036 | /* check if max_char(from substring) <= max_char(to) */ |
| 1037 | if (from_kind > to_kind |
| 1038 | /* latin1 => ascii */ |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1039 | || (!PyUnicode_IS_ASCII(from) && PyUnicode_IS_ASCII(to))) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 1040 | { |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1041 | /* slow path to check for character overflow */ |
| 1042 | const Py_UCS4 to_maxchar = PyUnicode_MAX_CHAR_VALUE(to); |
| 1043 | Py_UCS4 ch, maxchar; |
| 1044 | Py_ssize_t i; |
| 1045 | |
| 1046 | maxchar = 0; |
| 1047 | invalid_kinds = 0; |
| 1048 | for (i=0; i < how_many; i++) { |
| 1049 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
| 1050 | if (ch > maxchar) { |
| 1051 | maxchar = ch; |
| 1052 | if (maxchar > to_maxchar) { |
| 1053 | invalid_kinds = 1; |
| 1054 | break; |
| 1055 | } |
| 1056 | } |
| 1057 | PyUnicode_WRITE(to_kind, to_data, to_start + i, ch); |
| 1058 | } |
| 1059 | } |
| 1060 | else |
| 1061 | invalid_kinds = 1; |
| 1062 | if (invalid_kinds) { |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 1063 | PyErr_Format(PyExc_SystemError, |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 1064 | "Cannot copy %s characters " |
| 1065 | "into a string of %s characters", |
| 1066 | unicode_kind_name(from), |
| 1067 | unicode_kind_name(to)); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1068 | return -1; |
| 1069 | } |
| 1070 | } |
| 1071 | return how_many; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1072 | } |
| 1073 | |
Victor Stinner | 1722216 | 2011-09-28 22:15:37 +0200 | [diff] [blame] | 1074 | /* Find the maximum code point and count the number of surrogate pairs so a |
| 1075 | correct string length can be computed before converting a string to UCS4. |
| 1076 | This function counts single surrogates as a character and not as a pair. |
| 1077 | |
| 1078 | Return 0 on success, or -1 on error. */ |
| 1079 | static int |
| 1080 | find_maxchar_surrogates(const wchar_t *begin, const wchar_t *end, |
| 1081 | Py_UCS4 *maxchar, Py_ssize_t *num_surrogates) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1082 | { |
| 1083 | const wchar_t *iter; |
| 1084 | |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1085 | assert(num_surrogates != NULL && maxchar != NULL); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1086 | *num_surrogates = 0; |
| 1087 | *maxchar = 0; |
| 1088 | |
| 1089 | for (iter = begin; iter < end; ) { |
Victor Stinner | ae86485 | 2011-10-05 14:02:44 +0200 | [diff] [blame] | 1090 | if (*iter > *maxchar) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1091 | *maxchar = *iter; |
Victor Stinner | ae86485 | 2011-10-05 14:02:44 +0200 | [diff] [blame] | 1092 | #if SIZEOF_WCHAR_T != 2 |
| 1093 | if (*maxchar >= 0x10000) |
| 1094 | return 0; |
| 1095 | #endif |
| 1096 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1097 | #if SIZEOF_WCHAR_T == 2 |
| 1098 | if (*iter >= 0xD800 && *iter <= 0xDBFF |
| 1099 | && (iter+1) < end && iter[1] >= 0xDC00 && iter[1] <= 0xDFFF) |
| 1100 | { |
| 1101 | Py_UCS4 surrogate_val; |
| 1102 | surrogate_val = (((iter[0] & 0x3FF)<<10) |
| 1103 | | (iter[1] & 0x3FF)) + 0x10000; |
| 1104 | ++(*num_surrogates); |
| 1105 | if (surrogate_val > *maxchar) |
| 1106 | *maxchar = surrogate_val; |
| 1107 | iter += 2; |
| 1108 | } |
| 1109 | else |
| 1110 | iter++; |
| 1111 | #else |
| 1112 | iter++; |
| 1113 | #endif |
| 1114 | } |
| 1115 | return 0; |
| 1116 | } |
| 1117 | |
| 1118 | #ifdef Py_DEBUG |
| 1119 | int unicode_ready_calls = 0; |
| 1120 | #endif |
| 1121 | |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 1122 | static int |
| 1123 | unicode_ready(PyObject **p_obj, int replace) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1124 | { |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 1125 | PyUnicodeObject *unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1126 | wchar_t *end; |
| 1127 | Py_UCS4 maxchar = 0; |
| 1128 | Py_ssize_t num_surrogates; |
| 1129 | #if SIZEOF_WCHAR_T == 2 |
| 1130 | Py_ssize_t length_wo_surrogates; |
| 1131 | #endif |
| 1132 | |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 1133 | assert(p_obj != NULL); |
| 1134 | unicode = (PyUnicodeObject *)*p_obj; |
| 1135 | |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 1136 | /* _PyUnicode_Ready() is only intended for old-style API usage where |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1137 | strings were created using _PyObject_New() and where no canonical |
| 1138 | representation (the str field) has been set yet aka strings |
| 1139 | which are not yet ready. */ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1140 | assert(_PyUnicode_CHECK(unicode)); |
| 1141 | assert(_PyUnicode_KIND(unicode) == PyUnicode_WCHAR_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1142 | assert(_PyUnicode_WSTR(unicode) != NULL); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1143 | assert(_PyUnicode_DATA_ANY(unicode) == NULL); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1144 | assert(_PyUnicode_UTF8(unicode) == NULL); |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1145 | /* Actually, it should neither be interned nor be anything else: */ |
| 1146 | assert(_PyUnicode_STATE(unicode).interned == SSTATE_NOT_INTERNED); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1147 | |
| 1148 | #ifdef Py_DEBUG |
| 1149 | ++unicode_ready_calls; |
| 1150 | #endif |
| 1151 | |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 1152 | #ifdef Py_DEBUG |
| 1153 | assert(!replace || Py_REFCNT(unicode) == 1); |
| 1154 | #else |
| 1155 | if (replace && Py_REFCNT(unicode) != 1) |
| 1156 | replace = 0; |
| 1157 | #endif |
| 1158 | if (replace) { |
| 1159 | Py_ssize_t len = _PyUnicode_WSTR_LENGTH(unicode); |
| 1160 | wchar_t *wstr = _PyUnicode_WSTR(unicode); |
| 1161 | /* Optimization for empty strings */ |
| 1162 | if (len == 0) { |
| 1163 | Py_INCREF(unicode_empty); |
| 1164 | Py_DECREF(*p_obj); |
| 1165 | *p_obj = unicode_empty; |
| 1166 | return 0; |
| 1167 | } |
| 1168 | if (len == 1 && wstr[0] < 256) { |
| 1169 | PyObject *latin1_char = get_latin1_char((unsigned char)wstr[0]); |
| 1170 | if (latin1_char == NULL) |
| 1171 | return -1; |
| 1172 | Py_DECREF(*p_obj); |
| 1173 | *p_obj = latin1_char; |
| 1174 | return 0; |
| 1175 | } |
| 1176 | } |
| 1177 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1178 | end = _PyUnicode_WSTR(unicode) + _PyUnicode_WSTR_LENGTH(unicode); |
Victor Stinner | 1722216 | 2011-09-28 22:15:37 +0200 | [diff] [blame] | 1179 | if (find_maxchar_surrogates(_PyUnicode_WSTR(unicode), end, |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1180 | &maxchar, &num_surrogates) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1181 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1182 | |
| 1183 | if (maxchar < 256) { |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1184 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC(_PyUnicode_WSTR_LENGTH(unicode) + 1); |
| 1185 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1186 | PyErr_NoMemory(); |
| 1187 | return -1; |
| 1188 | } |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1189 | _PyUnicode_CONVERT_BYTES(wchar_t, unsigned char, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1190 | _PyUnicode_WSTR(unicode), end, |
| 1191 | PyUnicode_1BYTE_DATA(unicode)); |
| 1192 | PyUnicode_1BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1193 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1194 | _PyUnicode_STATE(unicode).kind = PyUnicode_1BYTE_KIND; |
| 1195 | if (maxchar < 128) { |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1196 | _PyUnicode_STATE(unicode).ascii = 1; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1197 | _PyUnicode_UTF8(unicode) = _PyUnicode_DATA_ANY(unicode); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1198 | _PyUnicode_UTF8_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1199 | } |
| 1200 | else { |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1201 | _PyUnicode_STATE(unicode).ascii = 0; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1202 | _PyUnicode_UTF8(unicode) = NULL; |
| 1203 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1204 | } |
| 1205 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1206 | _PyUnicode_WSTR(unicode) = NULL; |
| 1207 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1208 | } |
| 1209 | /* In this case we might have to convert down from 4-byte native |
| 1210 | wchar_t to 2-byte unicode. */ |
| 1211 | else if (maxchar < 65536) { |
| 1212 | assert(num_surrogates == 0 && |
| 1213 | "FindMaxCharAndNumSurrogatePairs() messed up"); |
| 1214 | |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1215 | #if SIZEOF_WCHAR_T == 2 |
| 1216 | /* We can share representations and are done. */ |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1217 | _PyUnicode_DATA_ANY(unicode) = _PyUnicode_WSTR(unicode); |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1218 | PyUnicode_2BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1219 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1220 | _PyUnicode_STATE(unicode).kind = PyUnicode_2BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1221 | _PyUnicode_UTF8(unicode) = NULL; |
| 1222 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1223 | #else |
| 1224 | /* sizeof(wchar_t) == 4 */ |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1225 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC( |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1226 | 2 * (_PyUnicode_WSTR_LENGTH(unicode) + 1)); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1227 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1228 | PyErr_NoMemory(); |
| 1229 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1230 | } |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1231 | _PyUnicode_CONVERT_BYTES(wchar_t, Py_UCS2, |
| 1232 | _PyUnicode_WSTR(unicode), end, |
| 1233 | PyUnicode_2BYTE_DATA(unicode)); |
| 1234 | PyUnicode_2BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1235 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1236 | _PyUnicode_STATE(unicode).kind = PyUnicode_2BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1237 | _PyUnicode_UTF8(unicode) = NULL; |
| 1238 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1239 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1240 | _PyUnicode_WSTR(unicode) = NULL; |
| 1241 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1242 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1243 | } |
| 1244 | /* maxchar exeeds 16 bit, wee need 4 bytes for unicode characters */ |
| 1245 | else { |
| 1246 | #if SIZEOF_WCHAR_T == 2 |
| 1247 | /* in case the native representation is 2-bytes, we need to allocate a |
| 1248 | new normalized 4-byte version. */ |
| 1249 | length_wo_surrogates = _PyUnicode_WSTR_LENGTH(unicode) - num_surrogates; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1250 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC(4 * (length_wo_surrogates + 1)); |
| 1251 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1252 | PyErr_NoMemory(); |
| 1253 | return -1; |
| 1254 | } |
| 1255 | _PyUnicode_LENGTH(unicode) = length_wo_surrogates; |
| 1256 | _PyUnicode_STATE(unicode).kind = PyUnicode_4BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1257 | _PyUnicode_UTF8(unicode) = NULL; |
| 1258 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 126c559 | 2011-10-03 04:17:10 +0200 | [diff] [blame] | 1259 | /* unicode_convert_wchar_to_ucs4() requires a ready string */ |
| 1260 | _PyUnicode_STATE(unicode).ready = 1; |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1261 | unicode_convert_wchar_to_ucs4(_PyUnicode_WSTR(unicode), end, unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1262 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1263 | _PyUnicode_WSTR(unicode) = NULL; |
| 1264 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1265 | #else |
| 1266 | assert(num_surrogates == 0); |
| 1267 | |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1268 | _PyUnicode_DATA_ANY(unicode) = _PyUnicode_WSTR(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1269 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1270 | _PyUnicode_UTF8(unicode) = NULL; |
| 1271 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1272 | _PyUnicode_STATE(unicode).kind = PyUnicode_4BYTE_KIND; |
| 1273 | #endif |
| 1274 | PyUnicode_4BYTE_DATA(unicode)[_PyUnicode_LENGTH(unicode)] = '\0'; |
| 1275 | } |
| 1276 | _PyUnicode_STATE(unicode).ready = 1; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1277 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1278 | return 0; |
| 1279 | } |
| 1280 | |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 1281 | int |
| 1282 | _PyUnicode_ReadyReplace(PyObject **op) |
| 1283 | { |
| 1284 | return unicode_ready(op, 1); |
| 1285 | } |
| 1286 | |
| 1287 | int |
| 1288 | _PyUnicode_Ready(PyObject *op) |
| 1289 | { |
| 1290 | return unicode_ready(&op, 0); |
| 1291 | } |
| 1292 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1293 | static void |
| 1294 | unicode_dealloc(register PyUnicodeObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1295 | { |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1296 | switch (PyUnicode_CHECK_INTERNED(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1297 | case SSTATE_NOT_INTERNED: |
| 1298 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1299 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1300 | case SSTATE_INTERNED_MORTAL: |
| 1301 | /* revive dead object temporarily for DelItem */ |
| 1302 | Py_REFCNT(unicode) = 3; |
| 1303 | if (PyDict_DelItem(interned, (PyObject *)unicode) != 0) |
| 1304 | Py_FatalError( |
| 1305 | "deletion of interned string failed"); |
| 1306 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1307 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1308 | case SSTATE_INTERNED_IMMORTAL: |
| 1309 | Py_FatalError("Immortal interned string died."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1310 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1311 | default: |
| 1312 | Py_FatalError("Inconsistent interned string state."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1313 | } |
| 1314 | |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 1315 | if (_PyUnicode_HAS_WSTR_MEMORY(unicode)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1316 | PyObject_DEL(_PyUnicode_WSTR(unicode)); |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 1317 | if (_PyUnicode_HAS_UTF8_MEMORY(unicode)) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1318 | PyObject_DEL(_PyUnicode_UTF8(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1319 | |
| 1320 | if (PyUnicode_IS_COMPACT(unicode)) { |
| 1321 | Py_TYPE(unicode)->tp_free((PyObject *)unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1322 | } |
| 1323 | else { |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1324 | if (_PyUnicode_DATA_ANY(unicode)) |
| 1325 | PyObject_DEL(_PyUnicode_DATA_ANY(unicode)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1326 | Py_TYPE(unicode)->tp_free((PyObject *)unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1327 | } |
| 1328 | } |
| 1329 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1330 | static int |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1331 | unicode_resizable(PyObject *unicode) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1332 | { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1333 | if (Py_REFCNT(unicode) != 1) |
| 1334 | return 0; |
| 1335 | if (PyUnicode_CHECK_INTERNED(unicode)) |
| 1336 | return 0; |
Benjamin Peterson | 7f3140e | 2011-10-03 19:37:29 -0400 | [diff] [blame] | 1337 | assert(unicode != unicode_empty); |
Victor Stinner | 77bb47b | 2011-10-03 20:06:05 +0200 | [diff] [blame] | 1338 | #ifdef Py_DEBUG |
| 1339 | if (_PyUnicode_KIND(unicode) != PyUnicode_WCHAR_KIND |
| 1340 | && PyUnicode_GET_LENGTH(unicode) == 1) |
| 1341 | { |
| 1342 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, 0); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1343 | if (ch < 256 && unicode_latin1[ch] == unicode) |
| 1344 | return 0; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1345 | } |
Victor Stinner | 77bb47b | 2011-10-03 20:06:05 +0200 | [diff] [blame] | 1346 | #endif |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1347 | return 1; |
| 1348 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1349 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1350 | static int |
| 1351 | unicode_resize(PyObject **p_unicode, Py_ssize_t length) |
| 1352 | { |
| 1353 | PyObject *unicode; |
| 1354 | Py_ssize_t old_length; |
| 1355 | |
| 1356 | assert(p_unicode != NULL); |
| 1357 | unicode = *p_unicode; |
| 1358 | |
| 1359 | assert(unicode != NULL); |
| 1360 | assert(PyUnicode_Check(unicode)); |
| 1361 | assert(0 <= length); |
| 1362 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1363 | if (_PyUnicode_KIND(unicode) == PyUnicode_WCHAR_KIND) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1364 | old_length = PyUnicode_WSTR_LENGTH(unicode); |
| 1365 | else |
| 1366 | old_length = PyUnicode_GET_LENGTH(unicode); |
| 1367 | if (old_length == length) |
| 1368 | return 0; |
| 1369 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1370 | if (!unicode_resizable(unicode)) { |
| 1371 | PyObject *copy = resize_copy(unicode, length); |
| 1372 | if (copy == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1373 | return -1; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1374 | Py_DECREF(*p_unicode); |
| 1375 | *p_unicode = copy; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1376 | return 0; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1377 | } |
| 1378 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1379 | if (PyUnicode_IS_COMPACT(unicode)) { |
| 1380 | *p_unicode = resize_compact(unicode, length); |
| 1381 | if (*p_unicode == NULL) |
| 1382 | return -1; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1383 | assert(_PyUnicode_CheckConsistency(*p_unicode, 0)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1384 | return 0; |
Benjamin Peterson | 4bfce8f | 2011-10-03 19:35:07 -0400 | [diff] [blame] | 1385 | } |
| 1386 | return resize_inplace((PyUnicodeObject*)unicode, length); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1387 | } |
| 1388 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1389 | int |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1390 | PyUnicode_Resize(PyObject **p_unicode, Py_ssize_t length) |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 1391 | { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1392 | PyObject *unicode; |
| 1393 | if (p_unicode == NULL) { |
| 1394 | PyErr_BadInternalCall(); |
| 1395 | return -1; |
| 1396 | } |
| 1397 | unicode = *p_unicode; |
| 1398 | if (unicode == NULL || !PyUnicode_Check(unicode) || length < 0 |
| 1399 | || _PyUnicode_KIND(unicode) != PyUnicode_WCHAR_KIND) |
| 1400 | { |
| 1401 | PyErr_BadInternalCall(); |
| 1402 | return -1; |
| 1403 | } |
| 1404 | return unicode_resize(p_unicode, length); |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 1405 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1406 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1407 | static PyObject* |
| 1408 | get_latin1_char(unsigned char ch) |
| 1409 | { |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1410 | PyObject *unicode = unicode_latin1[ch]; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1411 | if (!unicode) { |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1412 | unicode = PyUnicode_New(1, ch); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1413 | if (!unicode) |
| 1414 | return NULL; |
| 1415 | PyUnicode_1BYTE_DATA(unicode)[0] = ch; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1416 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1417 | unicode_latin1[ch] = unicode; |
| 1418 | } |
| 1419 | Py_INCREF(unicode); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1420 | return unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1421 | } |
| 1422 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1423 | PyObject * |
| 1424 | PyUnicode_FromUnicode(const Py_UNICODE *u, Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1425 | { |
| 1426 | PyUnicodeObject *unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1427 | Py_UCS4 maxchar = 0; |
| 1428 | Py_ssize_t num_surrogates; |
| 1429 | |
| 1430 | if (u == NULL) |
| 1431 | return (PyObject*)_PyUnicode_New(size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1432 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1433 | /* If the Unicode data is known at construction time, we can apply |
| 1434 | some optimizations which share commonly used objects. */ |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1435 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1436 | /* Optimization for empty strings */ |
| 1437 | if (size == 0 && unicode_empty != NULL) { |
| 1438 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1439 | return unicode_empty; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1440 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1441 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1442 | /* Single character Unicode objects in the Latin-1 range are |
| 1443 | shared when using this constructor */ |
| 1444 | if (size == 1 && *u < 256) |
| 1445 | return get_latin1_char((unsigned char)*u); |
| 1446 | |
| 1447 | /* If not empty and not single character, copy the Unicode data |
| 1448 | into the new object */ |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1449 | if (find_maxchar_surrogates(u, u + size, |
| 1450 | &maxchar, &num_surrogates) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1451 | return NULL; |
| 1452 | |
| 1453 | unicode = (PyUnicodeObject *) PyUnicode_New(size - num_surrogates, |
| 1454 | maxchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1455 | if (!unicode) |
| 1456 | return NULL; |
| 1457 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1458 | switch (PyUnicode_KIND(unicode)) { |
| 1459 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1460 | _PyUnicode_CONVERT_BYTES(Py_UNICODE, unsigned char, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1461 | u, u + size, PyUnicode_1BYTE_DATA(unicode)); |
| 1462 | break; |
| 1463 | case PyUnicode_2BYTE_KIND: |
| 1464 | #if Py_UNICODE_SIZE == 2 |
| 1465 | Py_MEMCPY(PyUnicode_2BYTE_DATA(unicode), u, size * 2); |
| 1466 | #else |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1467 | _PyUnicode_CONVERT_BYTES(Py_UNICODE, Py_UCS2, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1468 | u, u + size, PyUnicode_2BYTE_DATA(unicode)); |
| 1469 | #endif |
| 1470 | break; |
| 1471 | case PyUnicode_4BYTE_KIND: |
| 1472 | #if SIZEOF_WCHAR_T == 2 |
| 1473 | /* This is the only case which has to process surrogates, thus |
| 1474 | a simple copy loop is not enough and we need a function. */ |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1475 | unicode_convert_wchar_to_ucs4(u, u + size, unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1476 | #else |
| 1477 | assert(num_surrogates == 0); |
| 1478 | Py_MEMCPY(PyUnicode_4BYTE_DATA(unicode), u, size * 4); |
| 1479 | #endif |
| 1480 | break; |
| 1481 | default: |
| 1482 | assert(0 && "Impossible state"); |
| 1483 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1484 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1485 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1486 | return (PyObject *)unicode; |
| 1487 | } |
| 1488 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1489 | PyObject * |
| 1490 | PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size) |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1491 | { |
| 1492 | PyUnicodeObject *unicode; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1493 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1494 | if (size < 0) { |
| 1495 | PyErr_SetString(PyExc_SystemError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1496 | "Negative size passed to PyUnicode_FromStringAndSize"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1497 | return NULL; |
| 1498 | } |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1499 | |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1500 | /* 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] | 1501 | some optimizations which share commonly used objects. |
| 1502 | Also, this means the input must be UTF-8, so fall back to the |
| 1503 | UTF-8 decoder at the end. */ |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1504 | if (u != NULL) { |
| 1505 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1506 | /* Optimization for empty strings */ |
| 1507 | if (size == 0 && unicode_empty != NULL) { |
| 1508 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1509 | return unicode_empty; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1510 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1511 | |
| 1512 | /* Single characters are shared when using this constructor. |
| 1513 | Restrict to ASCII, since the input must be UTF-8. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1514 | if (size == 1 && Py_CHARMASK(*u) < 128) |
| 1515 | return get_latin1_char(Py_CHARMASK(*u)); |
Martin v. Löwis | 9c12106 | 2007-08-05 20:26:11 +0000 | [diff] [blame] | 1516 | |
| 1517 | return PyUnicode_DecodeUTF8(u, size, NULL); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1518 | } |
| 1519 | |
Walter Dörwald | 5550731 | 2007-05-18 13:12:10 +0000 | [diff] [blame] | 1520 | unicode = _PyUnicode_New(size); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1521 | if (!unicode) |
| 1522 | return NULL; |
| 1523 | |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1524 | return (PyObject *)unicode; |
| 1525 | } |
| 1526 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1527 | PyObject * |
| 1528 | PyUnicode_FromString(const char *u) |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1529 | { |
| 1530 | size_t size = strlen(u); |
| 1531 | if (size > PY_SSIZE_T_MAX) { |
| 1532 | PyErr_SetString(PyExc_OverflowError, "input too long"); |
| 1533 | return NULL; |
| 1534 | } |
| 1535 | |
| 1536 | return PyUnicode_FromStringAndSize(u, size); |
| 1537 | } |
| 1538 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1539 | static PyObject* |
Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame^] | 1540 | unicode_fromascii(const unsigned char* s, Py_ssize_t size) |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1541 | { |
Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame^] | 1542 | PyObject *res; |
| 1543 | #ifdef Py_DEBUG |
| 1544 | const unsigned char *p; |
| 1545 | const unsigned char *end = s + size; |
| 1546 | for (p=s; p < end; p++) { |
| 1547 | assert(*p < 128); |
| 1548 | } |
| 1549 | #endif |
| 1550 | res = PyUnicode_New(size, 127); |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1551 | if (!res) |
| 1552 | return NULL; |
Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame^] | 1553 | memcpy(PyUnicode_1BYTE_DATA(res), s, size); |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1554 | return res; |
| 1555 | } |
| 1556 | |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 1557 | static Py_UCS4 |
| 1558 | kind_maxchar_limit(unsigned int kind) |
| 1559 | { |
| 1560 | switch(kind) { |
| 1561 | case PyUnicode_1BYTE_KIND: |
| 1562 | return 0x80; |
| 1563 | case PyUnicode_2BYTE_KIND: |
| 1564 | return 0x100; |
| 1565 | case PyUnicode_4BYTE_KIND: |
| 1566 | return 0x10000; |
| 1567 | default: |
| 1568 | assert(0 && "invalid kind"); |
| 1569 | return 0x10ffff; |
| 1570 | } |
| 1571 | } |
| 1572 | |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1573 | static PyObject* |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1574 | _PyUnicode_FromUCS1(const unsigned char* u, Py_ssize_t size) |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1575 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1576 | PyObject *res; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1577 | unsigned char max_char = 127; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1578 | Py_ssize_t i; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1579 | |
| 1580 | assert(size >= 0); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1581 | for (i = 0; i < size; i++) { |
| 1582 | if (u[i] & 0x80) { |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1583 | max_char = 255; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1584 | break; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1585 | } |
| 1586 | } |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1587 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1588 | if (!res) |
| 1589 | return NULL; |
| 1590 | memcpy(PyUnicode_1BYTE_DATA(res), u, size); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1591 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1592 | return res; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1593 | } |
| 1594 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1595 | static PyObject* |
| 1596 | _PyUnicode_FromUCS2(const Py_UCS2 *u, Py_ssize_t size) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1597 | { |
| 1598 | PyObject *res; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1599 | Py_UCS2 max_char = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1600 | Py_ssize_t i; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1601 | |
| 1602 | assert(size >= 0); |
| 1603 | for (i = 0; i < size; i++) { |
| 1604 | if (u[i] > max_char) { |
| 1605 | max_char = u[i]; |
| 1606 | if (max_char >= 256) |
| 1607 | break; |
| 1608 | } |
| 1609 | } |
| 1610 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1611 | if (!res) |
| 1612 | return NULL; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1613 | if (max_char >= 256) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1614 | memcpy(PyUnicode_2BYTE_DATA(res), u, sizeof(Py_UCS2)*size); |
| 1615 | else |
| 1616 | for (i = 0; i < size; i++) |
| 1617 | PyUnicode_1BYTE_DATA(res)[i] = (Py_UCS1)u[i]; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1618 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1619 | return res; |
| 1620 | } |
| 1621 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1622 | static PyObject* |
| 1623 | _PyUnicode_FromUCS4(const Py_UCS4 *u, Py_ssize_t size) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1624 | { |
| 1625 | PyObject *res; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1626 | Py_UCS4 max_char = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1627 | Py_ssize_t i; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1628 | |
| 1629 | assert(size >= 0); |
| 1630 | for (i = 0; i < size; i++) { |
| 1631 | if (u[i] > max_char) { |
| 1632 | max_char = u[i]; |
| 1633 | if (max_char >= 0x10000) |
| 1634 | break; |
| 1635 | } |
| 1636 | } |
| 1637 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1638 | if (!res) |
| 1639 | return NULL; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1640 | if (max_char >= 0x10000) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1641 | memcpy(PyUnicode_4BYTE_DATA(res), u, sizeof(Py_UCS4)*size); |
| 1642 | else { |
| 1643 | int kind = PyUnicode_KIND(res); |
| 1644 | void *data = PyUnicode_DATA(res); |
| 1645 | for (i = 0; i < size; i++) |
| 1646 | PyUnicode_WRITE(kind, data, i, u[i]); |
| 1647 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1648 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1649 | return res; |
| 1650 | } |
| 1651 | |
| 1652 | PyObject* |
| 1653 | PyUnicode_FromKindAndData(int kind, const void *buffer, Py_ssize_t size) |
| 1654 | { |
| 1655 | switch(kind) { |
| 1656 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1657 | return _PyUnicode_FromUCS1(buffer, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1658 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1659 | return _PyUnicode_FromUCS2(buffer, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1660 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1661 | return _PyUnicode_FromUCS4(buffer, size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1662 | default: |
| 1663 | assert(0 && "invalid kind"); |
| 1664 | PyErr_SetString(PyExc_SystemError, "invalid kind"); |
| 1665 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1666 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1667 | } |
| 1668 | |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 1669 | PyObject* |
| 1670 | PyUnicode_Copy(PyObject *unicode) |
| 1671 | { |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 1672 | Py_ssize_t size; |
| 1673 | PyObject *copy; |
| 1674 | void *data; |
| 1675 | |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 1676 | if (!PyUnicode_Check(unicode)) { |
| 1677 | PyErr_BadInternalCall(); |
| 1678 | return NULL; |
| 1679 | } |
| 1680 | if (PyUnicode_READY(unicode)) |
| 1681 | return NULL; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 1682 | |
| 1683 | size = PyUnicode_GET_LENGTH(unicode); |
| 1684 | copy = PyUnicode_New(size, PyUnicode_MAX_CHAR_VALUE(unicode)); |
| 1685 | if (!copy) |
| 1686 | return NULL; |
| 1687 | assert(PyUnicode_KIND(copy) == PyUnicode_KIND(unicode)); |
| 1688 | |
| 1689 | data = PyUnicode_DATA(unicode); |
| 1690 | switch (PyUnicode_KIND(unicode)) |
| 1691 | { |
| 1692 | case PyUnicode_1BYTE_KIND: |
| 1693 | memcpy(PyUnicode_1BYTE_DATA(copy), data, size); |
| 1694 | break; |
| 1695 | case PyUnicode_2BYTE_KIND: |
| 1696 | memcpy(PyUnicode_2BYTE_DATA(copy), data, sizeof(Py_UCS2) * size); |
| 1697 | break; |
| 1698 | case PyUnicode_4BYTE_KIND: |
| 1699 | memcpy(PyUnicode_4BYTE_DATA(copy), data, sizeof(Py_UCS4) * size); |
| 1700 | break; |
| 1701 | default: |
| 1702 | assert(0); |
| 1703 | break; |
| 1704 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1705 | assert(_PyUnicode_CheckConsistency(copy, 1)); |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 1706 | return copy; |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 1707 | } |
| 1708 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1709 | |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1710 | /* Widen Unicode objects to larger buffers. Don't write terminating null |
| 1711 | character. Return NULL on error. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1712 | |
| 1713 | void* |
| 1714 | _PyUnicode_AsKind(PyObject *s, unsigned int kind) |
| 1715 | { |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1716 | Py_ssize_t len; |
| 1717 | void *result; |
| 1718 | unsigned int skind; |
| 1719 | |
| 1720 | if (PyUnicode_READY(s)) |
| 1721 | return NULL; |
| 1722 | |
| 1723 | len = PyUnicode_GET_LENGTH(s); |
| 1724 | skind = PyUnicode_KIND(s); |
| 1725 | if (skind >= kind) { |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 1726 | PyErr_SetString(PyExc_SystemError, "invalid widening attempt"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1727 | return NULL; |
| 1728 | } |
| 1729 | switch(kind) { |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1730 | case PyUnicode_2BYTE_KIND: |
| 1731 | result = PyMem_Malloc(len * sizeof(Py_UCS2)); |
| 1732 | if (!result) |
| 1733 | return PyErr_NoMemory(); |
| 1734 | assert(skind == PyUnicode_1BYTE_KIND); |
| 1735 | _PyUnicode_CONVERT_BYTES( |
| 1736 | Py_UCS1, Py_UCS2, |
| 1737 | PyUnicode_1BYTE_DATA(s), |
| 1738 | PyUnicode_1BYTE_DATA(s) + len, |
| 1739 | result); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1740 | return result; |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1741 | case PyUnicode_4BYTE_KIND: |
| 1742 | result = PyMem_Malloc(len * sizeof(Py_UCS4)); |
| 1743 | if (!result) |
| 1744 | return PyErr_NoMemory(); |
| 1745 | if (skind == PyUnicode_2BYTE_KIND) { |
| 1746 | _PyUnicode_CONVERT_BYTES( |
| 1747 | Py_UCS2, Py_UCS4, |
| 1748 | PyUnicode_2BYTE_DATA(s), |
| 1749 | PyUnicode_2BYTE_DATA(s) + len, |
| 1750 | result); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1751 | } |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1752 | else { |
| 1753 | assert(skind == PyUnicode_1BYTE_KIND); |
| 1754 | _PyUnicode_CONVERT_BYTES( |
| 1755 | Py_UCS1, Py_UCS4, |
| 1756 | PyUnicode_1BYTE_DATA(s), |
| 1757 | PyUnicode_1BYTE_DATA(s) + len, |
| 1758 | result); |
| 1759 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1760 | return result; |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1761 | default: |
| 1762 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1763 | } |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 1764 | PyErr_SetString(PyExc_SystemError, "invalid kind"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1765 | return NULL; |
| 1766 | } |
| 1767 | |
| 1768 | static Py_UCS4* |
| 1769 | as_ucs4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, |
| 1770 | int copy_null) |
| 1771 | { |
| 1772 | int kind; |
| 1773 | void *data; |
| 1774 | Py_ssize_t len, targetlen; |
| 1775 | if (PyUnicode_READY(string) == -1) |
| 1776 | return NULL; |
| 1777 | kind = PyUnicode_KIND(string); |
| 1778 | data = PyUnicode_DATA(string); |
| 1779 | len = PyUnicode_GET_LENGTH(string); |
| 1780 | targetlen = len; |
| 1781 | if (copy_null) |
| 1782 | targetlen++; |
| 1783 | if (!target) { |
| 1784 | if (PY_SSIZE_T_MAX / sizeof(Py_UCS4) < targetlen) { |
| 1785 | PyErr_NoMemory(); |
| 1786 | return NULL; |
| 1787 | } |
| 1788 | target = PyMem_Malloc(targetlen * sizeof(Py_UCS4)); |
| 1789 | if (!target) { |
| 1790 | PyErr_NoMemory(); |
| 1791 | return NULL; |
| 1792 | } |
| 1793 | } |
| 1794 | else { |
| 1795 | if (targetsize < targetlen) { |
| 1796 | PyErr_Format(PyExc_SystemError, |
| 1797 | "string is longer than the buffer"); |
| 1798 | if (copy_null && 0 < targetsize) |
| 1799 | target[0] = 0; |
| 1800 | return NULL; |
| 1801 | } |
| 1802 | } |
| 1803 | if (kind != PyUnicode_4BYTE_KIND) { |
| 1804 | Py_ssize_t i; |
| 1805 | for (i = 0; i < len; i++) |
| 1806 | target[i] = PyUnicode_READ(kind, data, i); |
| 1807 | } |
| 1808 | else |
| 1809 | Py_MEMCPY(target, data, len * sizeof(Py_UCS4)); |
| 1810 | if (copy_null) |
| 1811 | target[len] = 0; |
| 1812 | return target; |
| 1813 | } |
| 1814 | |
| 1815 | Py_UCS4* |
| 1816 | PyUnicode_AsUCS4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, |
| 1817 | int copy_null) |
| 1818 | { |
| 1819 | if (target == NULL || targetsize < 1) { |
| 1820 | PyErr_BadInternalCall(); |
| 1821 | return NULL; |
| 1822 | } |
| 1823 | return as_ucs4(string, target, targetsize, copy_null); |
| 1824 | } |
| 1825 | |
| 1826 | Py_UCS4* |
| 1827 | PyUnicode_AsUCS4Copy(PyObject *string) |
| 1828 | { |
| 1829 | return as_ucs4(string, NULL, 0, 1); |
| 1830 | } |
| 1831 | |
| 1832 | #ifdef HAVE_WCHAR_H |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1833 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1834 | PyObject * |
| 1835 | PyUnicode_FromWideChar(register const wchar_t *w, Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1836 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1837 | if (w == NULL) { |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 1838 | if (size == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1839 | return PyUnicode_New(0, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1840 | PyErr_BadInternalCall(); |
| 1841 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1842 | } |
| 1843 | |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 1844 | if (size == -1) { |
| 1845 | size = wcslen(w); |
| 1846 | } |
| 1847 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1848 | return PyUnicode_FromUnicode(w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1849 | } |
| 1850 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1851 | #endif /* HAVE_WCHAR_H */ |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1852 | |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 1853 | static void |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1854 | makefmt(char *fmt, int longflag, int longlongflag, int size_tflag, |
| 1855 | int zeropad, int width, int precision, char c) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 1856 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1857 | *fmt++ = '%'; |
| 1858 | if (width) { |
| 1859 | if (zeropad) |
| 1860 | *fmt++ = '0'; |
| 1861 | fmt += sprintf(fmt, "%d", width); |
| 1862 | } |
| 1863 | if (precision) |
| 1864 | fmt += sprintf(fmt, ".%d", precision); |
| 1865 | if (longflag) |
| 1866 | *fmt++ = 'l'; |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1867 | else if (longlongflag) { |
| 1868 | /* longlongflag should only ever be nonzero on machines with |
| 1869 | HAVE_LONG_LONG defined */ |
| 1870 | #ifdef HAVE_LONG_LONG |
| 1871 | char *f = PY_FORMAT_LONG_LONG; |
| 1872 | while (*f) |
| 1873 | *fmt++ = *f++; |
| 1874 | #else |
| 1875 | /* we shouldn't ever get here */ |
| 1876 | assert(0); |
| 1877 | *fmt++ = 'l'; |
| 1878 | #endif |
| 1879 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1880 | else if (size_tflag) { |
| 1881 | char *f = PY_FORMAT_SIZE_T; |
| 1882 | while (*f) |
| 1883 | *fmt++ = *f++; |
| 1884 | } |
| 1885 | *fmt++ = c; |
| 1886 | *fmt = '\0'; |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 1887 | } |
| 1888 | |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 1889 | /* helper for PyUnicode_FromFormatV() */ |
| 1890 | |
| 1891 | static const char* |
| 1892 | parse_format_flags(const char *f, |
| 1893 | int *p_width, int *p_precision, |
| 1894 | int *p_longflag, int *p_longlongflag, int *p_size_tflag) |
| 1895 | { |
| 1896 | int width, precision, longflag, longlongflag, size_tflag; |
| 1897 | |
| 1898 | /* parse the width.precision part, e.g. "%2.5s" => width=2, precision=5 */ |
| 1899 | f++; |
| 1900 | width = 0; |
| 1901 | while (Py_ISDIGIT((unsigned)*f)) |
| 1902 | width = (width*10) + *f++ - '0'; |
| 1903 | precision = 0; |
| 1904 | if (*f == '.') { |
| 1905 | f++; |
| 1906 | while (Py_ISDIGIT((unsigned)*f)) |
| 1907 | precision = (precision*10) + *f++ - '0'; |
| 1908 | if (*f == '%') { |
| 1909 | /* "%.3%s" => f points to "3" */ |
| 1910 | f--; |
| 1911 | } |
| 1912 | } |
| 1913 | if (*f == '\0') { |
| 1914 | /* bogus format "%.1" => go backward, f points to "1" */ |
| 1915 | f--; |
| 1916 | } |
| 1917 | if (p_width != NULL) |
| 1918 | *p_width = width; |
| 1919 | if (p_precision != NULL) |
| 1920 | *p_precision = precision; |
| 1921 | |
| 1922 | /* Handle %ld, %lu, %lld and %llu. */ |
| 1923 | longflag = 0; |
| 1924 | longlongflag = 0; |
Victor Stinner | e7faec1 | 2011-03-02 00:01:53 +0000 | [diff] [blame] | 1925 | size_tflag = 0; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 1926 | |
| 1927 | if (*f == 'l') { |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 1928 | if (f[1] == 'd' || f[1] == 'u' || f[1] == 'i') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 1929 | longflag = 1; |
| 1930 | ++f; |
| 1931 | } |
| 1932 | #ifdef HAVE_LONG_LONG |
| 1933 | else if (f[1] == 'l' && |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 1934 | (f[2] == 'd' || f[2] == 'u' || f[2] == 'i')) { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 1935 | longlongflag = 1; |
| 1936 | f += 2; |
| 1937 | } |
| 1938 | #endif |
| 1939 | } |
| 1940 | /* handle the size_t flag. */ |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 1941 | 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] | 1942 | size_tflag = 1; |
| 1943 | ++f; |
| 1944 | } |
| 1945 | if (p_longflag != NULL) |
| 1946 | *p_longflag = longflag; |
| 1947 | if (p_longlongflag != NULL) |
| 1948 | *p_longlongflag = longlongflag; |
| 1949 | if (p_size_tflag != NULL) |
| 1950 | *p_size_tflag = size_tflag; |
| 1951 | return f; |
| 1952 | } |
| 1953 | |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1954 | /* maximum number of characters required for output of %ld. 21 characters |
| 1955 | allows for 64-bit integers (in decimal) and an optional sign. */ |
| 1956 | #define MAX_LONG_CHARS 21 |
| 1957 | /* maximum number of characters required for output of %lld. |
| 1958 | We need at most ceil(log10(256)*SIZEOF_LONG_LONG) digits, |
| 1959 | plus 1 for the sign. 53/22 is an upper bound for log10(256). */ |
| 1960 | #define MAX_LONG_LONG_CHARS (2 + (SIZEOF_LONG_LONG*53-1) / 22) |
| 1961 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1962 | PyObject * |
| 1963 | PyUnicode_FromFormatV(const char *format, va_list vargs) |
| 1964 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1965 | va_list count; |
| 1966 | Py_ssize_t callcount = 0; |
| 1967 | PyObject **callresults = NULL; |
| 1968 | PyObject **callresult = NULL; |
| 1969 | Py_ssize_t n = 0; |
| 1970 | int width = 0; |
| 1971 | int precision = 0; |
| 1972 | int zeropad; |
| 1973 | const char* f; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1974 | PyUnicodeObject *string; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1975 | /* used by sprintf */ |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1976 | char fmt[61]; /* should be enough for %0width.precisionlld */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1977 | Py_UCS4 maxchar = 127; /* result is ASCII by default */ |
| 1978 | Py_UCS4 argmaxchar; |
| 1979 | Py_ssize_t numbersize = 0; |
| 1980 | char *numberresults = NULL; |
| 1981 | char *numberresult = NULL; |
| 1982 | Py_ssize_t i; |
| 1983 | int kind; |
| 1984 | void *data; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1985 | |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 1986 | Py_VA_COPY(count, vargs); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 1987 | /* step 1: count the number of %S/%R/%A/%s format specifications |
| 1988 | * (we call PyObject_Str()/PyObject_Repr()/PyObject_ASCII()/ |
| 1989 | * 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] | 1990 | * result in an array) |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 1991 | * also estimate a upper bound for all the number formats in the string, |
| 1992 | * 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] | 1993 | * buffer before putting everything together. */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1994 | for (f = format; *f; f++) { |
| 1995 | if (*f == '%') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 1996 | int longlongflag; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1997 | /* skip width or width.precision (eg. "1.2" of "%1.2f") */ |
| 1998 | f = parse_format_flags(f, &width, NULL, NULL, &longlongflag, NULL); |
| 1999 | if (*f == 's' || *f=='S' || *f=='R' || *f=='A' || *f=='V') |
| 2000 | ++callcount; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2001 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2002 | else if (*f == 'd' || *f=='u' || *f=='i' || *f=='x' || *f=='p') { |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2003 | #ifdef HAVE_LONG_LONG |
| 2004 | if (longlongflag) { |
| 2005 | if (width < MAX_LONG_LONG_CHARS) |
| 2006 | width = MAX_LONG_LONG_CHARS; |
| 2007 | } |
| 2008 | else |
| 2009 | #endif |
| 2010 | /* MAX_LONG_CHARS is enough to hold a 64-bit integer, |
| 2011 | including sign. Decimal takes the most space. This |
| 2012 | isn't enough for octal. If a width is specified we |
| 2013 | need more (which we allocate later). */ |
| 2014 | if (width < MAX_LONG_CHARS) |
| 2015 | width = MAX_LONG_CHARS; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2016 | |
| 2017 | /* account for the size + '\0' to separate numbers |
| 2018 | inside of the numberresults buffer */ |
| 2019 | numbersize += (width + 1); |
| 2020 | } |
| 2021 | } |
| 2022 | else if ((unsigned char)*f > 127) { |
| 2023 | PyErr_Format(PyExc_ValueError, |
| 2024 | "PyUnicode_FromFormatV() expects an ASCII-encoded format " |
| 2025 | "string, got a non-ASCII byte: 0x%02x", |
| 2026 | (unsigned char)*f); |
| 2027 | return NULL; |
| 2028 | } |
| 2029 | } |
| 2030 | /* step 2: allocate memory for the results of |
| 2031 | * PyObject_Str()/PyObject_Repr()/PyUnicode_DecodeUTF8() calls */ |
| 2032 | if (callcount) { |
| 2033 | callresults = PyObject_Malloc(sizeof(PyObject *) * callcount); |
| 2034 | if (!callresults) { |
| 2035 | PyErr_NoMemory(); |
| 2036 | return NULL; |
| 2037 | } |
| 2038 | callresult = callresults; |
| 2039 | } |
| 2040 | /* step 2.5: allocate memory for the results of formating numbers */ |
| 2041 | if (numbersize) { |
| 2042 | numberresults = PyObject_Malloc(numbersize); |
| 2043 | if (!numberresults) { |
| 2044 | PyErr_NoMemory(); |
| 2045 | goto fail; |
| 2046 | } |
| 2047 | numberresult = numberresults; |
| 2048 | } |
| 2049 | |
| 2050 | /* step 3: format numbers and figure out how large a buffer we need */ |
| 2051 | for (f = format; *f; f++) { |
| 2052 | if (*f == '%') { |
| 2053 | const char* p; |
| 2054 | int longflag; |
| 2055 | int longlongflag; |
| 2056 | int size_tflag; |
| 2057 | int numprinted; |
| 2058 | |
| 2059 | p = f; |
| 2060 | zeropad = (f[1] == '0'); |
| 2061 | f = parse_format_flags(f, &width, &precision, |
| 2062 | &longflag, &longlongflag, &size_tflag); |
| 2063 | switch (*f) { |
| 2064 | case 'c': |
| 2065 | { |
| 2066 | Py_UCS4 ordinal = va_arg(count, int); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2067 | maxchar = Py_MAX(maxchar, ordinal); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2068 | n++; |
| 2069 | break; |
| 2070 | } |
| 2071 | case '%': |
| 2072 | n++; |
| 2073 | break; |
| 2074 | case 'i': |
| 2075 | case 'd': |
| 2076 | makefmt(fmt, longflag, longlongflag, size_tflag, zeropad, |
| 2077 | width, precision, *f); |
| 2078 | if (longflag) |
| 2079 | numprinted = sprintf(numberresult, fmt, |
| 2080 | va_arg(count, long)); |
| 2081 | #ifdef HAVE_LONG_LONG |
| 2082 | else if (longlongflag) |
| 2083 | numprinted = sprintf(numberresult, fmt, |
| 2084 | va_arg(count, PY_LONG_LONG)); |
| 2085 | #endif |
| 2086 | else if (size_tflag) |
| 2087 | numprinted = sprintf(numberresult, fmt, |
| 2088 | va_arg(count, Py_ssize_t)); |
| 2089 | else |
| 2090 | numprinted = sprintf(numberresult, fmt, |
| 2091 | va_arg(count, int)); |
| 2092 | n += numprinted; |
| 2093 | /* advance by +1 to skip over the '\0' */ |
| 2094 | numberresult += (numprinted + 1); |
| 2095 | assert(*(numberresult - 1) == '\0'); |
| 2096 | assert(*(numberresult - 2) != '\0'); |
| 2097 | assert(numprinted >= 0); |
| 2098 | assert(numberresult <= numberresults + numbersize); |
| 2099 | break; |
| 2100 | case 'u': |
| 2101 | makefmt(fmt, longflag, longlongflag, size_tflag, zeropad, |
| 2102 | width, precision, 'u'); |
| 2103 | if (longflag) |
| 2104 | numprinted = sprintf(numberresult, fmt, |
| 2105 | va_arg(count, unsigned long)); |
| 2106 | #ifdef HAVE_LONG_LONG |
| 2107 | else if (longlongflag) |
| 2108 | numprinted = sprintf(numberresult, fmt, |
| 2109 | va_arg(count, unsigned PY_LONG_LONG)); |
| 2110 | #endif |
| 2111 | else if (size_tflag) |
| 2112 | numprinted = sprintf(numberresult, fmt, |
| 2113 | va_arg(count, size_t)); |
| 2114 | else |
| 2115 | numprinted = sprintf(numberresult, fmt, |
| 2116 | va_arg(count, unsigned int)); |
| 2117 | n += numprinted; |
| 2118 | numberresult += (numprinted + 1); |
| 2119 | assert(*(numberresult - 1) == '\0'); |
| 2120 | assert(*(numberresult - 2) != '\0'); |
| 2121 | assert(numprinted >= 0); |
| 2122 | assert(numberresult <= numberresults + numbersize); |
| 2123 | break; |
| 2124 | case 'x': |
| 2125 | makefmt(fmt, 0, 0, 0, zeropad, width, precision, 'x'); |
| 2126 | numprinted = sprintf(numberresult, fmt, va_arg(count, int)); |
| 2127 | n += numprinted; |
| 2128 | numberresult += (numprinted + 1); |
| 2129 | assert(*(numberresult - 1) == '\0'); |
| 2130 | assert(*(numberresult - 2) != '\0'); |
| 2131 | assert(numprinted >= 0); |
| 2132 | assert(numberresult <= numberresults + numbersize); |
| 2133 | break; |
| 2134 | case 'p': |
| 2135 | numprinted = sprintf(numberresult, "%p", va_arg(count, void*)); |
| 2136 | /* %p is ill-defined: ensure leading 0x. */ |
| 2137 | if (numberresult[1] == 'X') |
| 2138 | numberresult[1] = 'x'; |
| 2139 | else if (numberresult[1] != 'x') { |
| 2140 | memmove(numberresult + 2, numberresult, |
| 2141 | strlen(numberresult) + 1); |
| 2142 | numberresult[0] = '0'; |
| 2143 | numberresult[1] = 'x'; |
| 2144 | numprinted += 2; |
| 2145 | } |
| 2146 | n += numprinted; |
| 2147 | numberresult += (numprinted + 1); |
| 2148 | assert(*(numberresult - 1) == '\0'); |
| 2149 | assert(*(numberresult - 2) != '\0'); |
| 2150 | assert(numprinted >= 0); |
| 2151 | assert(numberresult <= numberresults + numbersize); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2152 | break; |
| 2153 | case 's': |
| 2154 | { |
| 2155 | /* UTF-8 */ |
Georg Brandl | 780b2a6 | 2009-05-05 09:19:59 +0000 | [diff] [blame] | 2156 | const char *s = va_arg(count, const char*); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2157 | PyObject *str = PyUnicode_DecodeUTF8(s, strlen(s), "replace"); |
| 2158 | if (!str) |
| 2159 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2160 | /* since PyUnicode_DecodeUTF8 returns already flexible |
| 2161 | unicode objects, there is no need to call ready on them */ |
| 2162 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2163 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2164 | n += PyUnicode_GET_LENGTH(str); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2165 | /* Remember the str and switch to the next slot */ |
| 2166 | *callresult++ = str; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2167 | break; |
| 2168 | } |
| 2169 | case 'U': |
| 2170 | { |
| 2171 | PyObject *obj = va_arg(count, PyObject *); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 2172 | assert(obj && _PyUnicode_CHECK(obj)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2173 | if (PyUnicode_READY(obj) == -1) |
| 2174 | goto fail; |
| 2175 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2176 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2177 | n += PyUnicode_GET_LENGTH(obj); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2178 | break; |
| 2179 | } |
| 2180 | case 'V': |
| 2181 | { |
| 2182 | PyObject *obj = va_arg(count, PyObject *); |
| 2183 | const char *str = va_arg(count, const char *); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2184 | PyObject *str_obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2185 | assert(obj || str); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 2186 | assert(!obj || _PyUnicode_CHECK(obj)); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2187 | if (obj) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2188 | if (PyUnicode_READY(obj) == -1) |
| 2189 | goto fail; |
| 2190 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2191 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2192 | n += PyUnicode_GET_LENGTH(obj); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2193 | *callresult++ = NULL; |
| 2194 | } |
| 2195 | else { |
| 2196 | str_obj = PyUnicode_DecodeUTF8(str, strlen(str), "replace"); |
| 2197 | if (!str_obj) |
| 2198 | goto fail; |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 2199 | if (PyUnicode_READY(str_obj)) { |
| 2200 | Py_DECREF(str_obj); |
| 2201 | goto fail; |
| 2202 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2203 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str_obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2204 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2205 | n += PyUnicode_GET_LENGTH(str_obj); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2206 | *callresult++ = str_obj; |
| 2207 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2208 | break; |
| 2209 | } |
| 2210 | case 'S': |
| 2211 | { |
| 2212 | PyObject *obj = va_arg(count, PyObject *); |
| 2213 | PyObject *str; |
| 2214 | assert(obj); |
| 2215 | str = PyObject_Str(obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2216 | if (!str || PyUnicode_READY(str) == -1) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2217 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2218 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2219 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2220 | n += PyUnicode_GET_LENGTH(str); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2221 | /* Remember the str and switch to the next slot */ |
| 2222 | *callresult++ = str; |
| 2223 | break; |
| 2224 | } |
| 2225 | case 'R': |
| 2226 | { |
| 2227 | PyObject *obj = va_arg(count, PyObject *); |
| 2228 | PyObject *repr; |
| 2229 | assert(obj); |
| 2230 | repr = PyObject_Repr(obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2231 | if (!repr || PyUnicode_READY(repr) == -1) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2232 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2233 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(repr); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2234 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2235 | n += PyUnicode_GET_LENGTH(repr); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2236 | /* Remember the repr and switch to the next slot */ |
| 2237 | *callresult++ = repr; |
| 2238 | break; |
| 2239 | } |
| 2240 | case 'A': |
| 2241 | { |
| 2242 | PyObject *obj = va_arg(count, PyObject *); |
| 2243 | PyObject *ascii; |
| 2244 | assert(obj); |
| 2245 | ascii = PyObject_ASCII(obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2246 | if (!ascii || PyUnicode_READY(ascii) == -1) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2247 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2248 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(ascii); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2249 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2250 | n += PyUnicode_GET_LENGTH(ascii); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2251 | /* Remember the repr and switch to the next slot */ |
| 2252 | *callresult++ = ascii; |
| 2253 | break; |
| 2254 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2255 | default: |
| 2256 | /* if we stumble upon an unknown |
| 2257 | formatting code, copy the rest of |
| 2258 | the format string to the output |
| 2259 | string. (we cannot just skip the |
| 2260 | code, since there's no way to know |
| 2261 | what's in the argument list) */ |
| 2262 | n += strlen(p); |
| 2263 | goto expand; |
| 2264 | } |
| 2265 | } else |
| 2266 | n++; |
| 2267 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2268 | expand: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2269 | /* step 4: fill the buffer */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2270 | /* Since we've analyzed how much space we need, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2271 | we don't have to resize the string. |
| 2272 | There can be no errors beyond this point. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2273 | string = (PyUnicodeObject *)PyUnicode_New(n, maxchar); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2274 | if (!string) |
| 2275 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2276 | kind = PyUnicode_KIND(string); |
| 2277 | data = PyUnicode_DATA(string); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2278 | callresult = callresults; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2279 | numberresult = numberresults; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2280 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2281 | for (i = 0, f = format; *f; f++) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2282 | if (*f == '%') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2283 | const char* p; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2284 | |
| 2285 | p = f; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2286 | f = parse_format_flags(f, NULL, NULL, NULL, NULL, NULL); |
| 2287 | /* checking for == because the last argument could be a empty |
| 2288 | string, which causes i to point to end, the assert at the end of |
| 2289 | the loop */ |
| 2290 | assert(i <= PyUnicode_GET_LENGTH(string)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2291 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2292 | switch (*f) { |
| 2293 | case 'c': |
Victor Stinner | 5ed8b2c | 2011-02-21 21:13:44 +0000 | [diff] [blame] | 2294 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2295 | const int ordinal = va_arg(vargs, int); |
| 2296 | PyUnicode_WRITE(kind, data, i++, ordinal); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2297 | break; |
Victor Stinner | 5ed8b2c | 2011-02-21 21:13:44 +0000 | [diff] [blame] | 2298 | } |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2299 | case 'i': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2300 | case 'd': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2301 | case 'u': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2302 | case 'x': |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2303 | case 'p': |
| 2304 | /* unused, since we already have the result */ |
| 2305 | if (*f == 'p') |
| 2306 | (void) va_arg(vargs, void *); |
| 2307 | else |
| 2308 | (void) va_arg(vargs, int); |
| 2309 | /* extract the result from numberresults and append. */ |
| 2310 | for (; *numberresult; ++i, ++numberresult) |
| 2311 | PyUnicode_WRITE(kind, data, i, *numberresult); |
| 2312 | /* skip over the separating '\0' */ |
| 2313 | assert(*numberresult == '\0'); |
| 2314 | numberresult++; |
| 2315 | assert(numberresult <= numberresults + numbersize); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2316 | break; |
| 2317 | case 's': |
| 2318 | { |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2319 | /* unused, since we already have the result */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2320 | Py_ssize_t size; |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2321 | (void) va_arg(vargs, char *); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2322 | size = PyUnicode_GET_LENGTH(*callresult); |
| 2323 | assert(PyUnicode_KIND(*callresult) <= PyUnicode_KIND(string)); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 2324 | if (PyUnicode_CopyCharacters((PyObject*)string, i, |
| 2325 | *callresult, 0, |
| 2326 | size) < 0) |
| 2327 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2328 | i += size; |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2329 | /* We're done with the unicode()/repr() => forget it */ |
| 2330 | Py_DECREF(*callresult); |
| 2331 | /* switch to next unicode()/repr() result */ |
| 2332 | ++callresult; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2333 | break; |
| 2334 | } |
| 2335 | case 'U': |
| 2336 | { |
| 2337 | PyObject *obj = va_arg(vargs, PyObject *); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2338 | Py_ssize_t size; |
| 2339 | assert(PyUnicode_KIND(obj) <= PyUnicode_KIND(string)); |
| 2340 | size = PyUnicode_GET_LENGTH(obj); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 2341 | if (PyUnicode_CopyCharacters((PyObject*)string, i, |
| 2342 | obj, 0, |
| 2343 | size) < 0) |
| 2344 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2345 | i += size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2346 | break; |
| 2347 | } |
| 2348 | case 'V': |
| 2349 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2350 | Py_ssize_t size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2351 | PyObject *obj = va_arg(vargs, PyObject *); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2352 | va_arg(vargs, const char *); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2353 | if (obj) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2354 | size = PyUnicode_GET_LENGTH(obj); |
| 2355 | assert(PyUnicode_KIND(obj) <= PyUnicode_KIND(string)); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 2356 | if (PyUnicode_CopyCharacters((PyObject*)string, i, |
| 2357 | obj, 0, |
| 2358 | size) < 0) |
| 2359 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2360 | i += size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2361 | } else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2362 | size = PyUnicode_GET_LENGTH(*callresult); |
| 2363 | assert(PyUnicode_KIND(*callresult) <= |
| 2364 | PyUnicode_KIND(string)); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 2365 | if (PyUnicode_CopyCharacters((PyObject*)string, i, |
| 2366 | *callresult, |
| 2367 | 0, size) < 0) |
| 2368 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2369 | i += size; |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2370 | Py_DECREF(*callresult); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2371 | } |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2372 | ++callresult; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2373 | break; |
| 2374 | } |
| 2375 | case 'S': |
| 2376 | case 'R': |
Victor Stinner | 9a90900 | 2010-10-18 20:59:24 +0000 | [diff] [blame] | 2377 | case 'A': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2378 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2379 | /* unused, since we already have the result */ |
| 2380 | (void) va_arg(vargs, PyObject *); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2381 | assert(PyUnicode_KIND(*callresult) <= PyUnicode_KIND(string)); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 2382 | if (PyUnicode_CopyCharacters((PyObject*)string, i, |
| 2383 | *callresult, 0, |
| 2384 | PyUnicode_GET_LENGTH(*callresult)) < 0) |
| 2385 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2386 | i += PyUnicode_GET_LENGTH(*callresult); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2387 | /* We're done with the unicode()/repr() => forget it */ |
| 2388 | Py_DECREF(*callresult); |
| 2389 | /* switch to next unicode()/repr() result */ |
| 2390 | ++callresult; |
| 2391 | break; |
| 2392 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2393 | case '%': |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2394 | PyUnicode_WRITE(kind, data, i++, '%'); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2395 | break; |
| 2396 | default: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2397 | for (; *p; ++p, ++i) |
| 2398 | PyUnicode_WRITE(kind, data, i, *p); |
| 2399 | assert(i == PyUnicode_GET_LENGTH(string)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2400 | goto end; |
| 2401 | } |
Victor Stinner | 1205f27 | 2010-09-11 00:54:47 +0000 | [diff] [blame] | 2402 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2403 | else { |
| 2404 | assert(i < PyUnicode_GET_LENGTH(string)); |
| 2405 | PyUnicode_WRITE(kind, data, i++, *f); |
| 2406 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2407 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2408 | assert(i == PyUnicode_GET_LENGTH(string)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2409 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2410 | end: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2411 | if (callresults) |
| 2412 | PyObject_Free(callresults); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2413 | if (numberresults) |
| 2414 | PyObject_Free(numberresults); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2415 | assert(_PyUnicode_CheckConsistency(string, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2416 | return (PyObject *)string; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2417 | fail: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2418 | if (callresults) { |
| 2419 | PyObject **callresult2 = callresults; |
| 2420 | while (callresult2 < callresult) { |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2421 | Py_XDECREF(*callresult2); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2422 | ++callresult2; |
| 2423 | } |
| 2424 | PyObject_Free(callresults); |
| 2425 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2426 | if (numberresults) |
| 2427 | PyObject_Free(numberresults); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2428 | return NULL; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2429 | } |
| 2430 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2431 | PyObject * |
| 2432 | PyUnicode_FromFormat(const char *format, ...) |
| 2433 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2434 | PyObject* ret; |
| 2435 | va_list vargs; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2436 | |
| 2437 | #ifdef HAVE_STDARG_PROTOTYPES |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2438 | va_start(vargs, format); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2439 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2440 | va_start(vargs); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2441 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2442 | ret = PyUnicode_FromFormatV(format, vargs); |
| 2443 | va_end(vargs); |
| 2444 | return ret; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2445 | } |
| 2446 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2447 | #ifdef HAVE_WCHAR_H |
| 2448 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2449 | /* Helper function for PyUnicode_AsWideChar() and PyUnicode_AsWideCharString(): |
| 2450 | convert a Unicode object to a wide character string. |
| 2451 | |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2452 | - 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] | 2453 | character) required to convert the unicode object. Ignore size argument. |
| 2454 | |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2455 | - Otherwise: return the number of wide characters (excluding the null |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2456 | character) written into w. Write at most size wide characters (including |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2457 | the null character). */ |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2458 | static Py_ssize_t |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2459 | unicode_aswidechar(PyUnicodeObject *unicode, |
| 2460 | wchar_t *w, |
| 2461 | Py_ssize_t size) |
| 2462 | { |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2463 | Py_ssize_t res; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2464 | const wchar_t *wstr; |
| 2465 | |
| 2466 | wstr = PyUnicode_AsUnicodeAndSize((PyObject *)unicode, &res); |
| 2467 | if (wstr == NULL) |
| 2468 | return -1; |
| 2469 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2470 | if (w != NULL) { |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2471 | if (size > res) |
| 2472 | size = res + 1; |
| 2473 | else |
| 2474 | res = size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2475 | Py_MEMCPY(w, wstr, size * sizeof(wchar_t)); |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2476 | return res; |
| 2477 | } |
| 2478 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2479 | return res + 1; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2480 | } |
| 2481 | |
| 2482 | Py_ssize_t |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2483 | PyUnicode_AsWideChar(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2484 | wchar_t *w, |
| 2485 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2486 | { |
| 2487 | if (unicode == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2488 | PyErr_BadInternalCall(); |
| 2489 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2490 | } |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2491 | return unicode_aswidechar((PyUnicodeObject*)unicode, w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2492 | } |
| 2493 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2494 | wchar_t* |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 2495 | PyUnicode_AsWideCharString(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2496 | Py_ssize_t *size) |
| 2497 | { |
| 2498 | wchar_t* buffer; |
| 2499 | Py_ssize_t buflen; |
| 2500 | |
| 2501 | if (unicode == NULL) { |
| 2502 | PyErr_BadInternalCall(); |
| 2503 | return NULL; |
| 2504 | } |
| 2505 | |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 2506 | buflen = unicode_aswidechar((PyUnicodeObject *)unicode, NULL, 0); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2507 | if (buflen == -1) |
| 2508 | return NULL; |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2509 | if (PY_SSIZE_T_MAX / sizeof(wchar_t) < buflen) { |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2510 | PyErr_NoMemory(); |
| 2511 | return NULL; |
| 2512 | } |
| 2513 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2514 | buffer = PyMem_MALLOC(buflen * sizeof(wchar_t)); |
| 2515 | if (buffer == NULL) { |
| 2516 | PyErr_NoMemory(); |
| 2517 | return NULL; |
| 2518 | } |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 2519 | buflen = unicode_aswidechar((PyUnicodeObject *)unicode, buffer, buflen); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2520 | if (buflen == -1) |
| 2521 | return NULL; |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2522 | if (size != NULL) |
| 2523 | *size = buflen; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2524 | return buffer; |
| 2525 | } |
| 2526 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2527 | #endif /* HAVE_WCHAR_H */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2528 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2529 | PyObject * |
| 2530 | PyUnicode_FromOrdinal(int ordinal) |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2531 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2532 | PyObject *v; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2533 | if (ordinal < 0 || ordinal > 0x10ffff) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2534 | PyErr_SetString(PyExc_ValueError, |
| 2535 | "chr() arg not in range(0x110000)"); |
| 2536 | return NULL; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2537 | } |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 2538 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2539 | if (ordinal < 256) |
| 2540 | return get_latin1_char(ordinal); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2541 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2542 | v = PyUnicode_New(1, ordinal); |
| 2543 | if (v == NULL) |
| 2544 | return NULL; |
| 2545 | PyUnicode_WRITE(PyUnicode_KIND(v), PyUnicode_DATA(v), 0, ordinal); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2546 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2547 | return v; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2548 | } |
| 2549 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2550 | PyObject * |
| 2551 | PyUnicode_FromObject(register PyObject *obj) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2552 | { |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2553 | /* XXX Perhaps we should make this API an alias of |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2554 | PyObject_Str() instead ?! */ |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2555 | if (PyUnicode_CheckExact(obj)) { |
Victor Stinner | d3a83d5 | 2011-10-01 03:09:33 +0200 | [diff] [blame] | 2556 | if (PyUnicode_READY(obj)) |
| 2557 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2558 | Py_INCREF(obj); |
| 2559 | return obj; |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2560 | } |
| 2561 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2562 | /* For a Unicode subtype that's not a Unicode object, |
| 2563 | return a true Unicode object with the same data. */ |
Victor Stinner | 2219e0a | 2011-10-01 01:16:59 +0200 | [diff] [blame] | 2564 | return PyUnicode_Copy(obj); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2565 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2566 | PyErr_Format(PyExc_TypeError, |
| 2567 | "Can't convert '%.100s' object to str implicitly", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2568 | Py_TYPE(obj)->tp_name); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2569 | return NULL; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2570 | } |
| 2571 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2572 | PyObject * |
| 2573 | PyUnicode_FromEncodedObject(register PyObject *obj, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2574 | const char *encoding, |
| 2575 | const char *errors) |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2576 | { |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2577 | Py_buffer buffer; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2578 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2579 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2580 | if (obj == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2581 | PyErr_BadInternalCall(); |
| 2582 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2583 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2584 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2585 | /* Decoding bytes objects is the most common case and should be fast */ |
| 2586 | if (PyBytes_Check(obj)) { |
| 2587 | if (PyBytes_GET_SIZE(obj) == 0) { |
| 2588 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 2589 | v = unicode_empty; |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2590 | } |
| 2591 | else { |
| 2592 | v = PyUnicode_Decode( |
| 2593 | PyBytes_AS_STRING(obj), PyBytes_GET_SIZE(obj), |
| 2594 | encoding, errors); |
| 2595 | } |
| 2596 | return v; |
| 2597 | } |
| 2598 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2599 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2600 | PyErr_SetString(PyExc_TypeError, |
| 2601 | "decoding str is not supported"); |
| 2602 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2603 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2604 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2605 | /* Retrieve a bytes buffer view through the PEP 3118 buffer interface */ |
| 2606 | if (PyObject_GetBuffer(obj, &buffer, PyBUF_SIMPLE) < 0) { |
| 2607 | PyErr_Format(PyExc_TypeError, |
| 2608 | "coercing to str: need bytes, bytearray " |
| 2609 | "or buffer-like object, %.80s found", |
| 2610 | Py_TYPE(obj)->tp_name); |
| 2611 | return NULL; |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 2612 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2613 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2614 | if (buffer.len == 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2615 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 2616 | v = unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2617 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2618 | else |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2619 | v = PyUnicode_Decode((char*) buffer.buf, buffer.len, encoding, errors); |
Marc-André Lemburg | ad7c98e | 2001-01-17 17:09:53 +0000 | [diff] [blame] | 2620 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2621 | PyBuffer_Release(&buffer); |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2622 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2623 | } |
| 2624 | |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2625 | /* Convert encoding to lower case and replace '_' with '-' in order to |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2626 | catch e.g. UTF_8. Return 0 on error (encoding is longer than lower_len-1), |
| 2627 | 1 on success. */ |
| 2628 | static int |
| 2629 | normalize_encoding(const char *encoding, |
| 2630 | char *lower, |
| 2631 | size_t lower_len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2632 | { |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2633 | const char *e; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2634 | char *l; |
| 2635 | char *l_end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2636 | |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2637 | e = encoding; |
| 2638 | l = lower; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2639 | l_end = &lower[lower_len - 1]; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2640 | while (*e) { |
| 2641 | if (l == l_end) |
| 2642 | return 0; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 2643 | if (Py_ISUPPER(*e)) { |
| 2644 | *l++ = Py_TOLOWER(*e++); |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2645 | } |
| 2646 | else if (*e == '_') { |
| 2647 | *l++ = '-'; |
| 2648 | e++; |
| 2649 | } |
| 2650 | else { |
| 2651 | *l++ = *e++; |
| 2652 | } |
| 2653 | } |
| 2654 | *l = '\0'; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2655 | return 1; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2656 | } |
| 2657 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2658 | PyObject * |
| 2659 | PyUnicode_Decode(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2660 | Py_ssize_t size, |
| 2661 | const char *encoding, |
| 2662 | const char *errors) |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2663 | { |
| 2664 | PyObject *buffer = NULL, *unicode; |
| 2665 | Py_buffer info; |
| 2666 | char lower[11]; /* Enough for any encoding shortcut */ |
| 2667 | |
| 2668 | if (encoding == NULL) |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2669 | return PyUnicode_DecodeUTF8(s, size, errors); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 2670 | |
| 2671 | /* Shortcuts for common default encodings */ |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2672 | if (normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2673 | if ((strcmp(lower, "utf-8") == 0) || |
| 2674 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2675 | return PyUnicode_DecodeUTF8(s, size, errors); |
| 2676 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2677 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2678 | (strcmp(lower, "iso-8859-1") == 0)) |
| 2679 | return PyUnicode_DecodeLatin1(s, size, errors); |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 2680 | #ifdef HAVE_MBCS |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2681 | else if (strcmp(lower, "mbcs") == 0) |
| 2682 | return PyUnicode_DecodeMBCS(s, size, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 2683 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2684 | else if (strcmp(lower, "ascii") == 0) |
| 2685 | return PyUnicode_DecodeASCII(s, size, errors); |
| 2686 | else if (strcmp(lower, "utf-16") == 0) |
| 2687 | return PyUnicode_DecodeUTF16(s, size, errors, 0); |
| 2688 | else if (strcmp(lower, "utf-32") == 0) |
| 2689 | return PyUnicode_DecodeUTF32(s, size, errors, 0); |
| 2690 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2691 | |
| 2692 | /* Decode via the codec registry */ |
Guido van Rossum | be801ac | 2007-10-08 03:32:34 +0000 | [diff] [blame] | 2693 | buffer = NULL; |
Antoine Pitrou | c3b3924 | 2009-01-03 16:59:18 +0000 | [diff] [blame] | 2694 | 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] | 2695 | goto onError; |
Antoine Pitrou | ee58fa4 | 2008-08-19 18:22:14 +0000 | [diff] [blame] | 2696 | buffer = PyMemoryView_FromBuffer(&info); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2697 | if (buffer == NULL) |
| 2698 | goto onError; |
| 2699 | unicode = PyCodec_Decode(buffer, encoding, errors); |
| 2700 | if (unicode == NULL) |
| 2701 | goto onError; |
| 2702 | if (!PyUnicode_Check(unicode)) { |
| 2703 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 2704 | "decoder did not return a str object (type=%.400s)", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2705 | Py_TYPE(unicode)->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2706 | Py_DECREF(unicode); |
| 2707 | goto onError; |
| 2708 | } |
| 2709 | Py_DECREF(buffer); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 2710 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 2711 | if (_PyUnicode_READY_REPLACE(&unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2712 | Py_DECREF(unicode); |
| 2713 | return NULL; |
| 2714 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 2715 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2716 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2717 | return unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2718 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2719 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2720 | Py_XDECREF(buffer); |
| 2721 | return NULL; |
| 2722 | } |
| 2723 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2724 | PyObject * |
| 2725 | PyUnicode_AsDecodedObject(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2726 | const char *encoding, |
| 2727 | const char *errors) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2728 | { |
| 2729 | PyObject *v; |
| 2730 | |
| 2731 | if (!PyUnicode_Check(unicode)) { |
| 2732 | PyErr_BadArgument(); |
| 2733 | goto onError; |
| 2734 | } |
| 2735 | |
| 2736 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2737 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2738 | |
| 2739 | /* Decode via the codec registry */ |
| 2740 | v = PyCodec_Decode(unicode, encoding, errors); |
| 2741 | if (v == NULL) |
| 2742 | goto onError; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2743 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2744 | return v; |
| 2745 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2746 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2747 | return NULL; |
| 2748 | } |
| 2749 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2750 | PyObject * |
| 2751 | PyUnicode_AsDecodedUnicode(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2752 | const char *encoding, |
| 2753 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2754 | { |
| 2755 | PyObject *v; |
| 2756 | |
| 2757 | if (!PyUnicode_Check(unicode)) { |
| 2758 | PyErr_BadArgument(); |
| 2759 | goto onError; |
| 2760 | } |
| 2761 | |
| 2762 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2763 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2764 | |
| 2765 | /* Decode via the codec registry */ |
| 2766 | v = PyCodec_Decode(unicode, encoding, errors); |
| 2767 | if (v == NULL) |
| 2768 | goto onError; |
| 2769 | if (!PyUnicode_Check(v)) { |
| 2770 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 2771 | "decoder did not return a str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2772 | Py_TYPE(v)->tp_name); |
| 2773 | Py_DECREF(v); |
| 2774 | goto onError; |
| 2775 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2776 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2777 | return v; |
| 2778 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2779 | onError: |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2780 | return NULL; |
| 2781 | } |
| 2782 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2783 | PyObject * |
| 2784 | PyUnicode_Encode(const Py_UNICODE *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2785 | Py_ssize_t size, |
| 2786 | const char *encoding, |
| 2787 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2788 | { |
| 2789 | PyObject *v, *unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2790 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2791 | unicode = PyUnicode_FromUnicode(s, size); |
| 2792 | if (unicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2793 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2794 | v = PyUnicode_AsEncodedString(unicode, encoding, errors); |
| 2795 | Py_DECREF(unicode); |
| 2796 | return v; |
| 2797 | } |
| 2798 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2799 | PyObject * |
| 2800 | PyUnicode_AsEncodedObject(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2801 | const char *encoding, |
| 2802 | const char *errors) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2803 | { |
| 2804 | PyObject *v; |
| 2805 | |
| 2806 | if (!PyUnicode_Check(unicode)) { |
| 2807 | PyErr_BadArgument(); |
| 2808 | goto onError; |
| 2809 | } |
| 2810 | |
| 2811 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2812 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2813 | |
| 2814 | /* Encode via the codec registry */ |
| 2815 | v = PyCodec_Encode(unicode, encoding, errors); |
| 2816 | if (v == NULL) |
| 2817 | goto onError; |
| 2818 | return v; |
| 2819 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2820 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2821 | return NULL; |
| 2822 | } |
| 2823 | |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 2824 | PyObject * |
| 2825 | PyUnicode_EncodeFSDefault(PyObject *unicode) |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 2826 | { |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 2827 | #ifdef HAVE_MBCS |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 2828 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
| 2829 | PyUnicode_GET_SIZE(unicode), |
| 2830 | NULL); |
| 2831 | #elif defined(__APPLE__) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2832 | return _PyUnicode_AsUTF8String(unicode, "surrogateescape"); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 2833 | #else |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 2834 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 2835 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 2836 | cannot use it to encode and decode filenames before it is loaded. Load |
| 2837 | the Python codec requires to encode at least its own filename. Use the C |
| 2838 | version of the locale codec until the codec registry is initialized and |
| 2839 | the Python codec is loaded. |
| 2840 | |
| 2841 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 2842 | cannot only rely on it: check also interp->fscodec_initialized for |
| 2843 | subinterpreters. */ |
| 2844 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 2845 | return PyUnicode_AsEncodedString(unicode, |
| 2846 | Py_FileSystemDefaultEncoding, |
| 2847 | "surrogateescape"); |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 2848 | } |
| 2849 | else { |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 2850 | /* locale encoding with surrogateescape */ |
| 2851 | wchar_t *wchar; |
| 2852 | char *bytes; |
| 2853 | PyObject *bytes_obj; |
Victor Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 2854 | size_t error_pos; |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 2855 | |
| 2856 | wchar = PyUnicode_AsWideCharString(unicode, NULL); |
| 2857 | if (wchar == NULL) |
| 2858 | return NULL; |
Victor Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 2859 | bytes = _Py_wchar2char(wchar, &error_pos); |
| 2860 | if (bytes == NULL) { |
| 2861 | if (error_pos != (size_t)-1) { |
| 2862 | char *errmsg = strerror(errno); |
| 2863 | PyObject *exc = NULL; |
| 2864 | if (errmsg == NULL) |
| 2865 | errmsg = "Py_wchar2char() failed"; |
| 2866 | raise_encode_exception(&exc, |
| 2867 | "filesystemencoding", |
| 2868 | PyUnicode_AS_UNICODE(unicode), PyUnicode_GET_SIZE(unicode), |
| 2869 | error_pos, error_pos+1, |
| 2870 | errmsg); |
| 2871 | Py_XDECREF(exc); |
| 2872 | } |
| 2873 | else |
| 2874 | PyErr_NoMemory(); |
| 2875 | PyMem_Free(wchar); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 2876 | return NULL; |
Victor Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 2877 | } |
| 2878 | PyMem_Free(wchar); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 2879 | |
| 2880 | bytes_obj = PyBytes_FromString(bytes); |
| 2881 | PyMem_Free(bytes); |
| 2882 | return bytes_obj; |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 2883 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 2884 | #endif |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 2885 | } |
| 2886 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2887 | PyObject * |
| 2888 | PyUnicode_AsEncodedString(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2889 | const char *encoding, |
| 2890 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2891 | { |
| 2892 | PyObject *v; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2893 | char lower[11]; /* Enough for any encoding shortcut */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2894 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2895 | if (!PyUnicode_Check(unicode)) { |
| 2896 | PyErr_BadArgument(); |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 2897 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2898 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 2899 | |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 2900 | if (encoding == NULL) { |
| 2901 | if (errors == NULL || strcmp(errors, "strict") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2902 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 2903 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2904 | return _PyUnicode_AsUTF8String(unicode, errors); |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 2905 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 2906 | |
| 2907 | /* Shortcuts for common default encodings */ |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2908 | if (normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2909 | if ((strcmp(lower, "utf-8") == 0) || |
| 2910 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 2911 | { |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 2912 | if (errors == NULL || strcmp(errors, "strict") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2913 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 2914 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2915 | return _PyUnicode_AsUTF8String(unicode, errors); |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 2916 | } |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2917 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2918 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2919 | (strcmp(lower, "iso-8859-1") == 0)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2920 | return _PyUnicode_AsLatin1String(unicode, errors); |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 2921 | #ifdef HAVE_MBCS |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2922 | else if (strcmp(lower, "mbcs") == 0) |
| 2923 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
| 2924 | PyUnicode_GET_SIZE(unicode), |
| 2925 | errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 2926 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2927 | else if (strcmp(lower, "ascii") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2928 | return _PyUnicode_AsASCIIString(unicode, errors); |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2929 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2930 | |
| 2931 | /* Encode via the codec registry */ |
| 2932 | v = PyCodec_Encode(unicode, encoding, errors); |
| 2933 | if (v == NULL) |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 2934 | return NULL; |
| 2935 | |
| 2936 | /* The normal path */ |
| 2937 | if (PyBytes_Check(v)) |
| 2938 | return v; |
| 2939 | |
| 2940 | /* 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] | 2941 | if (PyByteArray_Check(v)) { |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 2942 | int error; |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 2943 | PyObject *b; |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 2944 | |
| 2945 | error = PyErr_WarnFormat(PyExc_RuntimeWarning, 1, |
| 2946 | "encoder %s returned bytearray instead of bytes", |
| 2947 | encoding); |
| 2948 | if (error) { |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 2949 | Py_DECREF(v); |
| 2950 | return NULL; |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2951 | } |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2952 | |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 2953 | b = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(v), Py_SIZE(v)); |
| 2954 | Py_DECREF(v); |
| 2955 | return b; |
| 2956 | } |
| 2957 | |
| 2958 | PyErr_Format(PyExc_TypeError, |
| 2959 | "encoder did not return a bytes object (type=%.400s)", |
| 2960 | Py_TYPE(v)->tp_name); |
| 2961 | Py_DECREF(v); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2962 | return NULL; |
| 2963 | } |
| 2964 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2965 | PyObject * |
| 2966 | PyUnicode_AsEncodedUnicode(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2967 | const char *encoding, |
| 2968 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2969 | { |
| 2970 | PyObject *v; |
| 2971 | |
| 2972 | if (!PyUnicode_Check(unicode)) { |
| 2973 | PyErr_BadArgument(); |
| 2974 | goto onError; |
| 2975 | } |
| 2976 | |
| 2977 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2978 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2979 | |
| 2980 | /* Encode via the codec registry */ |
| 2981 | v = PyCodec_Encode(unicode, encoding, errors); |
| 2982 | if (v == NULL) |
| 2983 | goto onError; |
| 2984 | if (!PyUnicode_Check(v)) { |
| 2985 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 2986 | "encoder did not return an str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2987 | Py_TYPE(v)->tp_name); |
| 2988 | Py_DECREF(v); |
| 2989 | goto onError; |
| 2990 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2991 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2992 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2993 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2994 | return NULL; |
| 2995 | } |
| 2996 | |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 2997 | PyObject* |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 2998 | PyUnicode_DecodeFSDefault(const char *s) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 2999 | Py_ssize_t size = (Py_ssize_t)strlen(s); |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3000 | return PyUnicode_DecodeFSDefaultAndSize(s, size); |
| 3001 | } |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3002 | |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3003 | PyObject* |
| 3004 | PyUnicode_DecodeFSDefaultAndSize(const char *s, Py_ssize_t size) |
| 3005 | { |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3006 | #ifdef HAVE_MBCS |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3007 | return PyUnicode_DecodeMBCS(s, size, NULL); |
| 3008 | #elif defined(__APPLE__) |
| 3009 | return PyUnicode_DecodeUTF8(s, size, "surrogateescape"); |
| 3010 | #else |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 3011 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 3012 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 3013 | cannot use it to encode and decode filenames before it is loaded. Load |
| 3014 | the Python codec requires to encode at least its own filename. Use the C |
| 3015 | version of the locale codec until the codec registry is initialized and |
| 3016 | the Python codec is loaded. |
| 3017 | |
| 3018 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 3019 | cannot only rely on it: check also interp->fscodec_initialized for |
| 3020 | subinterpreters. */ |
| 3021 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3022 | return PyUnicode_Decode(s, size, |
| 3023 | Py_FileSystemDefaultEncoding, |
Victor Stinner | b9a20ad | 2010-04-30 16:37:52 +0000 | [diff] [blame] | 3024 | "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3025 | } |
| 3026 | else { |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3027 | /* locale encoding with surrogateescape */ |
| 3028 | wchar_t *wchar; |
| 3029 | PyObject *unicode; |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 3030 | size_t len; |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3031 | |
| 3032 | if (s[size] != '\0' || size != strlen(s)) { |
| 3033 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
| 3034 | return NULL; |
| 3035 | } |
| 3036 | |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 3037 | wchar = _Py_char2wchar(s, &len); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3038 | if (wchar == NULL) |
Victor Stinner | d5af0a5 | 2010-11-08 23:34:29 +0000 | [diff] [blame] | 3039 | return PyErr_NoMemory(); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3040 | |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 3041 | unicode = PyUnicode_FromWideChar(wchar, len); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3042 | PyMem_Free(wchar); |
| 3043 | return unicode; |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3044 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3045 | #endif |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3046 | } |
| 3047 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3048 | |
| 3049 | int |
| 3050 | PyUnicode_FSConverter(PyObject* arg, void* addr) |
| 3051 | { |
| 3052 | PyObject *output = NULL; |
| 3053 | Py_ssize_t size; |
| 3054 | void *data; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3055 | if (arg == NULL) { |
| 3056 | Py_DECREF(*(PyObject**)addr); |
| 3057 | return 1; |
| 3058 | } |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 3059 | if (PyBytes_Check(arg)) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3060 | output = arg; |
| 3061 | Py_INCREF(output); |
| 3062 | } |
| 3063 | else { |
| 3064 | arg = PyUnicode_FromObject(arg); |
| 3065 | if (!arg) |
| 3066 | return 0; |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3067 | output = PyUnicode_EncodeFSDefault(arg); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3068 | Py_DECREF(arg); |
| 3069 | if (!output) |
| 3070 | return 0; |
| 3071 | if (!PyBytes_Check(output)) { |
| 3072 | Py_DECREF(output); |
| 3073 | PyErr_SetString(PyExc_TypeError, "encoder failed to return bytes"); |
| 3074 | return 0; |
| 3075 | } |
| 3076 | } |
Victor Stinner | 0ea2a46 | 2010-04-30 00:22:08 +0000 | [diff] [blame] | 3077 | size = PyBytes_GET_SIZE(output); |
| 3078 | data = PyBytes_AS_STRING(output); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3079 | if (size != strlen(data)) { |
Benjamin Peterson | 7a6b44a | 2011-08-18 13:51:47 -0500 | [diff] [blame] | 3080 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3081 | Py_DECREF(output); |
| 3082 | return 0; |
| 3083 | } |
| 3084 | *(PyObject**)addr = output; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3085 | return Py_CLEANUP_SUPPORTED; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3086 | } |
| 3087 | |
| 3088 | |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3089 | int |
| 3090 | PyUnicode_FSDecoder(PyObject* arg, void* addr) |
| 3091 | { |
| 3092 | PyObject *output = NULL; |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3093 | if (arg == NULL) { |
| 3094 | Py_DECREF(*(PyObject**)addr); |
| 3095 | return 1; |
| 3096 | } |
| 3097 | if (PyUnicode_Check(arg)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3098 | if (PyUnicode_READY(arg)) |
| 3099 | return 0; |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3100 | output = arg; |
| 3101 | Py_INCREF(output); |
| 3102 | } |
| 3103 | else { |
| 3104 | arg = PyBytes_FromObject(arg); |
| 3105 | if (!arg) |
| 3106 | return 0; |
| 3107 | output = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AS_STRING(arg), |
| 3108 | PyBytes_GET_SIZE(arg)); |
| 3109 | Py_DECREF(arg); |
| 3110 | if (!output) |
| 3111 | return 0; |
| 3112 | if (!PyUnicode_Check(output)) { |
| 3113 | Py_DECREF(output); |
| 3114 | PyErr_SetString(PyExc_TypeError, "decoder failed to return unicode"); |
| 3115 | return 0; |
| 3116 | } |
| 3117 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3118 | if (findchar(PyUnicode_DATA(output), PyUnicode_KIND(output), |
| 3119 | PyUnicode_GET_LENGTH(output), 0, 1)) { |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3120 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
| 3121 | Py_DECREF(output); |
| 3122 | return 0; |
| 3123 | } |
| 3124 | *(PyObject**)addr = output; |
| 3125 | return Py_CLEANUP_SUPPORTED; |
| 3126 | } |
| 3127 | |
| 3128 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3129 | char* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3130 | PyUnicode_AsUTF8AndSize(PyObject *unicode, Py_ssize_t *psize) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3131 | { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 3132 | PyObject *bytes; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3133 | PyUnicodeObject *u = (PyUnicodeObject *)unicode; |
| 3134 | |
Neal Norwitz | e0a0a6e | 2007-08-25 01:04:21 +0000 | [diff] [blame] | 3135 | if (!PyUnicode_Check(unicode)) { |
| 3136 | PyErr_BadArgument(); |
| 3137 | return NULL; |
| 3138 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3139 | if (PyUnicode_READY(u) == -1) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3140 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3141 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3142 | if (PyUnicode_UTF8(unicode) == NULL) { |
| 3143 | assert(!PyUnicode_IS_COMPACT_ASCII(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3144 | bytes = _PyUnicode_AsUTF8String(unicode, "strict"); |
| 3145 | if (bytes == NULL) |
| 3146 | return NULL; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3147 | _PyUnicode_UTF8(u) = PyObject_MALLOC(PyBytes_GET_SIZE(bytes) + 1); |
| 3148 | if (_PyUnicode_UTF8(u) == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3149 | Py_DECREF(bytes); |
| 3150 | return NULL; |
| 3151 | } |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3152 | _PyUnicode_UTF8_LENGTH(u) = PyBytes_GET_SIZE(bytes); |
| 3153 | 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] | 3154 | Py_DECREF(bytes); |
| 3155 | } |
| 3156 | |
| 3157 | if (psize) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3158 | *psize = PyUnicode_UTF8_LENGTH(unicode); |
| 3159 | return PyUnicode_UTF8(unicode); |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 3160 | } |
| 3161 | |
| 3162 | char* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3163 | PyUnicode_AsUTF8(PyObject *unicode) |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 3164 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3165 | return PyUnicode_AsUTF8AndSize(unicode, NULL); |
| 3166 | } |
| 3167 | |
| 3168 | #ifdef Py_DEBUG |
| 3169 | int unicode_as_unicode_calls = 0; |
| 3170 | #endif |
| 3171 | |
| 3172 | |
| 3173 | Py_UNICODE * |
| 3174 | PyUnicode_AsUnicodeAndSize(PyObject *unicode, Py_ssize_t *size) |
| 3175 | { |
| 3176 | PyUnicodeObject *u; |
| 3177 | const unsigned char *one_byte; |
| 3178 | #if SIZEOF_WCHAR_T == 4 |
| 3179 | const Py_UCS2 *two_bytes; |
| 3180 | #else |
| 3181 | const Py_UCS4 *four_bytes; |
| 3182 | const Py_UCS4 *ucs4_end; |
| 3183 | Py_ssize_t num_surrogates; |
| 3184 | #endif |
| 3185 | wchar_t *w; |
| 3186 | wchar_t *wchar_end; |
| 3187 | |
| 3188 | if (!PyUnicode_Check(unicode)) { |
| 3189 | PyErr_BadArgument(); |
| 3190 | return NULL; |
| 3191 | } |
| 3192 | u = (PyUnicodeObject*)unicode; |
| 3193 | if (_PyUnicode_WSTR(u) == NULL) { |
| 3194 | /* Non-ASCII compact unicode object */ |
| 3195 | assert(_PyUnicode_KIND(u) != 0); |
| 3196 | assert(PyUnicode_IS_READY(u)); |
| 3197 | |
| 3198 | #ifdef Py_DEBUG |
| 3199 | ++unicode_as_unicode_calls; |
| 3200 | #endif |
| 3201 | |
| 3202 | if (PyUnicode_KIND(u) == PyUnicode_4BYTE_KIND) { |
| 3203 | #if SIZEOF_WCHAR_T == 2 |
| 3204 | four_bytes = PyUnicode_4BYTE_DATA(u); |
| 3205 | ucs4_end = four_bytes + _PyUnicode_LENGTH(u); |
| 3206 | num_surrogates = 0; |
| 3207 | |
| 3208 | for (; four_bytes < ucs4_end; ++four_bytes) { |
| 3209 | if (*four_bytes > 0xFFFF) |
| 3210 | ++num_surrogates; |
| 3211 | } |
| 3212 | |
| 3213 | _PyUnicode_WSTR(u) = (wchar_t *) PyObject_MALLOC( |
| 3214 | sizeof(wchar_t) * (_PyUnicode_LENGTH(u) + 1 + num_surrogates)); |
| 3215 | if (!_PyUnicode_WSTR(u)) { |
| 3216 | PyErr_NoMemory(); |
| 3217 | return NULL; |
| 3218 | } |
| 3219 | _PyUnicode_WSTR_LENGTH(u) = _PyUnicode_LENGTH(u) + num_surrogates; |
| 3220 | |
| 3221 | w = _PyUnicode_WSTR(u); |
| 3222 | wchar_end = w + _PyUnicode_WSTR_LENGTH(u); |
| 3223 | four_bytes = PyUnicode_4BYTE_DATA(u); |
| 3224 | for (; four_bytes < ucs4_end; ++four_bytes, ++w) { |
| 3225 | if (*four_bytes > 0xFFFF) { |
| 3226 | /* encode surrogate pair in this case */ |
| 3227 | *w++ = 0xD800 | ((*four_bytes - 0x10000) >> 10); |
| 3228 | *w = 0xDC00 | ((*four_bytes - 0x10000) & 0x3FF); |
| 3229 | } |
| 3230 | else |
| 3231 | *w = *four_bytes; |
| 3232 | |
| 3233 | if (w > wchar_end) { |
| 3234 | assert(0 && "Miscalculated string end"); |
| 3235 | } |
| 3236 | } |
| 3237 | *w = 0; |
| 3238 | #else |
| 3239 | /* sizeof(wchar_t) == 4 */ |
| 3240 | Py_FatalError("Impossible unicode object state, wstr and str " |
| 3241 | "should share memory already."); |
| 3242 | return NULL; |
| 3243 | #endif |
| 3244 | } |
| 3245 | else { |
| 3246 | _PyUnicode_WSTR(u) = (wchar_t *) PyObject_MALLOC(sizeof(wchar_t) * |
| 3247 | (_PyUnicode_LENGTH(u) + 1)); |
| 3248 | if (!_PyUnicode_WSTR(u)) { |
| 3249 | PyErr_NoMemory(); |
| 3250 | return NULL; |
| 3251 | } |
| 3252 | if (!PyUnicode_IS_COMPACT_ASCII(u)) |
| 3253 | _PyUnicode_WSTR_LENGTH(u) = _PyUnicode_LENGTH(u); |
| 3254 | w = _PyUnicode_WSTR(u); |
| 3255 | wchar_end = w + _PyUnicode_LENGTH(u); |
| 3256 | |
| 3257 | if (PyUnicode_KIND(u) == PyUnicode_1BYTE_KIND) { |
| 3258 | one_byte = PyUnicode_1BYTE_DATA(u); |
| 3259 | for (; w < wchar_end; ++one_byte, ++w) |
| 3260 | *w = *one_byte; |
| 3261 | /* null-terminate the wstr */ |
| 3262 | *w = 0; |
| 3263 | } |
| 3264 | else if (PyUnicode_KIND(u) == PyUnicode_2BYTE_KIND) { |
| 3265 | #if SIZEOF_WCHAR_T == 4 |
| 3266 | two_bytes = PyUnicode_2BYTE_DATA(u); |
| 3267 | for (; w < wchar_end; ++two_bytes, ++w) |
| 3268 | *w = *two_bytes; |
| 3269 | /* null-terminate the wstr */ |
| 3270 | *w = 0; |
| 3271 | #else |
| 3272 | /* sizeof(wchar_t) == 2 */ |
| 3273 | PyObject_FREE(_PyUnicode_WSTR(u)); |
| 3274 | _PyUnicode_WSTR(u) = NULL; |
| 3275 | Py_FatalError("Impossible unicode object state, wstr " |
| 3276 | "and str should share memory already."); |
| 3277 | return NULL; |
| 3278 | #endif |
| 3279 | } |
| 3280 | else { |
| 3281 | assert(0 && "This should never happen."); |
| 3282 | } |
| 3283 | } |
| 3284 | } |
| 3285 | if (size != NULL) |
| 3286 | *size = PyUnicode_WSTR_LENGTH(u); |
| 3287 | return _PyUnicode_WSTR(u); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3288 | } |
| 3289 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3290 | Py_UNICODE * |
| 3291 | PyUnicode_AsUnicode(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3292 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3293 | return PyUnicode_AsUnicodeAndSize(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3294 | } |
| 3295 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3296 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3297 | Py_ssize_t |
| 3298 | PyUnicode_GetSize(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3299 | { |
| 3300 | if (!PyUnicode_Check(unicode)) { |
| 3301 | PyErr_BadArgument(); |
| 3302 | goto onError; |
| 3303 | } |
| 3304 | return PyUnicode_GET_SIZE(unicode); |
| 3305 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3306 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3307 | return -1; |
| 3308 | } |
| 3309 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3310 | Py_ssize_t |
| 3311 | PyUnicode_GetLength(PyObject *unicode) |
| 3312 | { |
Victor Stinner | 5a706cf | 2011-10-02 00:36:53 +0200 | [diff] [blame] | 3313 | if (!PyUnicode_Check(unicode) || PyUnicode_READY(unicode) == -1) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3314 | PyErr_BadArgument(); |
| 3315 | return -1; |
| 3316 | } |
| 3317 | |
| 3318 | return PyUnicode_GET_LENGTH(unicode); |
| 3319 | } |
| 3320 | |
| 3321 | Py_UCS4 |
| 3322 | PyUnicode_ReadChar(PyObject *unicode, Py_ssize_t index) |
| 3323 | { |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 3324 | if (!PyUnicode_Check(unicode) || PyUnicode_READY(unicode) == -1) { |
| 3325 | PyErr_BadArgument(); |
| 3326 | return (Py_UCS4)-1; |
| 3327 | } |
| 3328 | if (index < 0 || index >= _PyUnicode_LENGTH(unicode)) { |
| 3329 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3330 | return (Py_UCS4)-1; |
| 3331 | } |
| 3332 | return PyUnicode_READ_CHAR(unicode, index); |
| 3333 | } |
| 3334 | |
| 3335 | int |
| 3336 | PyUnicode_WriteChar(PyObject *unicode, Py_ssize_t index, Py_UCS4 ch) |
| 3337 | { |
| 3338 | if (!PyUnicode_Check(unicode) || !PyUnicode_IS_COMPACT(unicode)) { |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 3339 | PyErr_BadArgument(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3340 | return -1; |
| 3341 | } |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 3342 | if (index < 0 || index >= _PyUnicode_LENGTH(unicode)) { |
| 3343 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 3344 | return -1; |
| 3345 | } |
| 3346 | if (_PyUnicode_Dirty(unicode)) |
| 3347 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3348 | PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode), |
| 3349 | index, ch); |
| 3350 | return 0; |
| 3351 | } |
| 3352 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3353 | const char * |
| 3354 | PyUnicode_GetDefaultEncoding(void) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3355 | { |
Victor Stinner | 42cb462 | 2010-09-01 19:39:01 +0000 | [diff] [blame] | 3356 | return "utf-8"; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3357 | } |
| 3358 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 3359 | /* create or adjust a UnicodeDecodeError */ |
| 3360 | static void |
| 3361 | make_decode_exception(PyObject **exceptionObject, |
| 3362 | const char *encoding, |
| 3363 | const char *input, Py_ssize_t length, |
| 3364 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 3365 | const char *reason) |
| 3366 | { |
| 3367 | if (*exceptionObject == NULL) { |
| 3368 | *exceptionObject = PyUnicodeDecodeError_Create( |
| 3369 | encoding, input, length, startpos, endpos, reason); |
| 3370 | } |
| 3371 | else { |
| 3372 | if (PyUnicodeDecodeError_SetStart(*exceptionObject, startpos)) |
| 3373 | goto onError; |
| 3374 | if (PyUnicodeDecodeError_SetEnd(*exceptionObject, endpos)) |
| 3375 | goto onError; |
| 3376 | if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) |
| 3377 | goto onError; |
| 3378 | } |
| 3379 | return; |
| 3380 | |
| 3381 | onError: |
| 3382 | Py_DECREF(*exceptionObject); |
| 3383 | *exceptionObject = NULL; |
| 3384 | } |
| 3385 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3386 | /* error handling callback helper: |
| 3387 | build arguments, call the callback and check the arguments, |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 3388 | if no exception occurred, copy the replacement to the output |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3389 | and adjust various state variables. |
| 3390 | return 0 on success, -1 on error |
| 3391 | */ |
| 3392 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3393 | static int |
| 3394 | unicode_decode_call_errorhandler(const char *errors, PyObject **errorHandler, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3395 | const char *encoding, const char *reason, |
| 3396 | const char **input, const char **inend, Py_ssize_t *startinpos, |
| 3397 | Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
| 3398 | PyUnicodeObject **output, Py_ssize_t *outpos, Py_UNICODE **outptr) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3399 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3400 | 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] | 3401 | |
| 3402 | PyObject *restuple = NULL; |
| 3403 | PyObject *repunicode = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3404 | Py_ssize_t outsize = PyUnicode_GET_SIZE(*output); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3405 | Py_ssize_t insize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3406 | Py_ssize_t requiredsize; |
| 3407 | Py_ssize_t newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3408 | const Py_UNICODE *repptr; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3409 | PyObject *inputobj = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3410 | Py_ssize_t repsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3411 | int res = -1; |
| 3412 | |
| 3413 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3414 | *errorHandler = PyCodec_LookupError(errors); |
| 3415 | if (*errorHandler == NULL) |
| 3416 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3417 | } |
| 3418 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 3419 | make_decode_exception(exceptionObject, |
| 3420 | encoding, |
| 3421 | *input, *inend - *input, |
| 3422 | *startinpos, *endinpos, |
| 3423 | reason); |
| 3424 | if (*exceptionObject == NULL) |
| 3425 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3426 | |
| 3427 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 3428 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3429 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3430 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 3431 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3432 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3433 | } |
| 3434 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3435 | goto onError; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3436 | |
| 3437 | /* Copy back the bytes variables, which might have been modified by the |
| 3438 | callback */ |
| 3439 | inputobj = PyUnicodeDecodeError_GetObject(*exceptionObject); |
| 3440 | if (!inputobj) |
| 3441 | goto onError; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3442 | if (!PyBytes_Check(inputobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3443 | PyErr_Format(PyExc_TypeError, "exception attribute object must be bytes"); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3444 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3445 | *input = PyBytes_AS_STRING(inputobj); |
| 3446 | insize = PyBytes_GET_SIZE(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3447 | *inend = *input + insize; |
Walter Dörwald | 36f938f | 2007-08-10 10:11:43 +0000 | [diff] [blame] | 3448 | /* we can DECREF safely, as the exception has another reference, |
| 3449 | so the object won't go away. */ |
| 3450 | Py_DECREF(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3451 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3452 | if (newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3453 | newpos = insize+newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 3454 | if (newpos<0 || newpos>insize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3455 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", newpos); |
| 3456 | goto onError; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 3457 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3458 | |
| 3459 | /* need more space? (at least enough for what we |
| 3460 | have+the replacement+the rest of the string (starting |
| 3461 | at the new input position), so we won't have to check space |
| 3462 | when there are no errors in the rest of the string) */ |
| 3463 | repptr = PyUnicode_AS_UNICODE(repunicode); |
| 3464 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 3465 | requiredsize = *outpos + repsize + insize-newpos; |
| 3466 | if (requiredsize > outsize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3467 | if (requiredsize<2*outsize) |
| 3468 | requiredsize = 2*outsize; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 3469 | if (PyUnicode_Resize((PyObject**)output, requiredsize) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3470 | goto onError; |
| 3471 | *outptr = PyUnicode_AS_UNICODE(*output) + *outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3472 | } |
| 3473 | *endinpos = newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3474 | *inptr = *input + newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3475 | Py_UNICODE_COPY(*outptr, repptr, repsize); |
| 3476 | *outptr += repsize; |
| 3477 | *outpos += repsize; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3478 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3479 | /* we made it! */ |
| 3480 | res = 0; |
| 3481 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3482 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3483 | Py_XDECREF(restuple); |
| 3484 | return res; |
| 3485 | } |
| 3486 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3487 | /* --- UTF-7 Codec -------------------------------------------------------- */ |
| 3488 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3489 | /* See RFC2152 for details. We encode conservatively and decode liberally. */ |
| 3490 | |
| 3491 | /* Three simple macros defining base-64. */ |
| 3492 | |
| 3493 | /* Is c a base-64 character? */ |
| 3494 | |
| 3495 | #define IS_BASE64(c) \ |
| 3496 | (((c) >= 'A' && (c) <= 'Z') || \ |
| 3497 | ((c) >= 'a' && (c) <= 'z') || \ |
| 3498 | ((c) >= '0' && (c) <= '9') || \ |
| 3499 | (c) == '+' || (c) == '/') |
| 3500 | |
| 3501 | /* given that c is a base-64 character, what is its base-64 value? */ |
| 3502 | |
| 3503 | #define FROM_BASE64(c) \ |
| 3504 | (((c) >= 'A' && (c) <= 'Z') ? (c) - 'A' : \ |
| 3505 | ((c) >= 'a' && (c) <= 'z') ? (c) - 'a' + 26 : \ |
| 3506 | ((c) >= '0' && (c) <= '9') ? (c) - '0' + 52 : \ |
| 3507 | (c) == '+' ? 62 : 63) |
| 3508 | |
| 3509 | /* What is the base-64 character of the bottom 6 bits of n? */ |
| 3510 | |
| 3511 | #define TO_BASE64(n) \ |
| 3512 | ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) |
| 3513 | |
| 3514 | /* DECODE_DIRECT: this byte encountered in a UTF-7 string should be |
| 3515 | * decoded as itself. We are permissive on decoding; the only ASCII |
| 3516 | * byte not decoding to itself is the + which begins a base64 |
| 3517 | * string. */ |
| 3518 | |
| 3519 | #define DECODE_DIRECT(c) \ |
| 3520 | ((c) <= 127 && (c) != '+') |
| 3521 | |
| 3522 | /* The UTF-7 encoder treats ASCII characters differently according to |
| 3523 | * whether they are Set D, Set O, Whitespace, or special (i.e. none of |
| 3524 | * the above). See RFC2152. This array identifies these different |
| 3525 | * sets: |
| 3526 | * 0 : "Set D" |
| 3527 | * alphanumeric and '(),-./:? |
| 3528 | * 1 : "Set O" |
| 3529 | * !"#$%&*;<=>@[]^_`{|} |
| 3530 | * 2 : "whitespace" |
| 3531 | * ht nl cr sp |
| 3532 | * 3 : special (must be base64 encoded) |
| 3533 | * everything else (i.e. +\~ and non-printing codes 0-8 11-12 14-31 127) |
| 3534 | */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3535 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3536 | static |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3537 | char utf7_category[128] = { |
| 3538 | /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */ |
| 3539 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 3, 3, 2, 3, 3, |
| 3540 | /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */ |
| 3541 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 3542 | /* sp ! " # $ % & ' ( ) * + , - . / */ |
| 3543 | 2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 3, 0, 0, 0, 0, |
| 3544 | /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */ |
| 3545 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, |
| 3546 | /* @ A B C D E F G H I J K L M N O */ |
| 3547 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 3548 | /* P Q R S T U V W X Y Z [ \ ] ^ _ */ |
| 3549 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 1, 1, 1, |
| 3550 | /* ` a b c d e f g h i j k l m n o */ |
| 3551 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 3552 | /* p q r s t u v w x y z { | } ~ del */ |
| 3553 | 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] | 3554 | }; |
| 3555 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3556 | /* ENCODE_DIRECT: this character should be encoded as itself. The |
| 3557 | * answer depends on whether we are encoding set O as itself, and also |
| 3558 | * on whether we are encoding whitespace as itself. RFC2152 makes it |
| 3559 | * clear that the answers to these questions vary between |
| 3560 | * applications, so this code needs to be flexible. */ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 3561 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3562 | #define ENCODE_DIRECT(c, directO, directWS) \ |
| 3563 | ((c) < 128 && (c) > 0 && \ |
| 3564 | ((utf7_category[(c)] == 0) || \ |
| 3565 | (directWS && (utf7_category[(c)] == 2)) || \ |
| 3566 | (directO && (utf7_category[(c)] == 1)))) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3567 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3568 | PyObject * |
| 3569 | PyUnicode_DecodeUTF7(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3570 | Py_ssize_t size, |
| 3571 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3572 | { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3573 | return PyUnicode_DecodeUTF7Stateful(s, size, errors, NULL); |
| 3574 | } |
| 3575 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3576 | /* The decoder. The only state we preserve is our read position, |
| 3577 | * i.e. how many characters we have consumed. So if we end in the |
| 3578 | * middle of a shift sequence we have to back off the read position |
| 3579 | * and the output to the beginning of the sequence, otherwise we lose |
| 3580 | * all the shift state (seen bits, number of bits seen, high |
| 3581 | * surrogate). */ |
| 3582 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3583 | PyObject * |
| 3584 | PyUnicode_DecodeUTF7Stateful(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3585 | Py_ssize_t size, |
| 3586 | const char *errors, |
| 3587 | Py_ssize_t *consumed) |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3588 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3589 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3590 | Py_ssize_t startinpos; |
| 3591 | Py_ssize_t endinpos; |
| 3592 | Py_ssize_t outpos; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3593 | const char *e; |
| 3594 | PyUnicodeObject *unicode; |
| 3595 | Py_UNICODE *p; |
| 3596 | const char *errmsg = ""; |
| 3597 | int inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3598 | Py_UNICODE *shiftOutStart; |
| 3599 | unsigned int base64bits = 0; |
| 3600 | unsigned long base64buffer = 0; |
| 3601 | Py_UNICODE surrogate = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3602 | PyObject *errorHandler = NULL; |
| 3603 | PyObject *exc = NULL; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3604 | |
| 3605 | unicode = _PyUnicode_New(size); |
| 3606 | if (!unicode) |
| 3607 | return NULL; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3608 | if (size == 0) { |
| 3609 | if (consumed) |
| 3610 | *consumed = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3611 | return (PyObject *)unicode; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3612 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3613 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3614 | p = PyUnicode_AS_UNICODE(unicode); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3615 | shiftOutStart = p; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3616 | e = s + size; |
| 3617 | |
| 3618 | while (s < e) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3619 | Py_UNICODE ch; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3620 | restart: |
Antoine Pitrou | 5ffd9e9 | 2008-07-25 18:05:24 +0000 | [diff] [blame] | 3621 | ch = (unsigned char) *s; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3622 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3623 | if (inShift) { /* in a base-64 section */ |
| 3624 | if (IS_BASE64(ch)) { /* consume a base-64 character */ |
| 3625 | base64buffer = (base64buffer << 6) | FROM_BASE64(ch); |
| 3626 | base64bits += 6; |
| 3627 | s++; |
| 3628 | if (base64bits >= 16) { |
| 3629 | /* we have enough bits for a UTF-16 value */ |
| 3630 | Py_UNICODE outCh = (Py_UNICODE) |
| 3631 | (base64buffer >> (base64bits-16)); |
| 3632 | base64bits -= 16; |
| 3633 | base64buffer &= (1 << base64bits) - 1; /* clear high bits */ |
| 3634 | if (surrogate) { |
| 3635 | /* expecting a second surrogate */ |
| 3636 | if (outCh >= 0xDC00 && outCh <= 0xDFFF) { |
| 3637 | #ifdef Py_UNICODE_WIDE |
| 3638 | *p++ = (((surrogate & 0x3FF)<<10) |
| 3639 | | (outCh & 0x3FF)) + 0x10000; |
| 3640 | #else |
| 3641 | *p++ = surrogate; |
| 3642 | *p++ = outCh; |
| 3643 | #endif |
| 3644 | surrogate = 0; |
| 3645 | } |
| 3646 | else { |
| 3647 | surrogate = 0; |
| 3648 | errmsg = "second surrogate missing"; |
| 3649 | goto utf7Error; |
| 3650 | } |
| 3651 | } |
| 3652 | else if (outCh >= 0xD800 && outCh <= 0xDBFF) { |
| 3653 | /* first surrogate */ |
| 3654 | surrogate = outCh; |
| 3655 | } |
| 3656 | else if (outCh >= 0xDC00 && outCh <= 0xDFFF) { |
| 3657 | errmsg = "unexpected second surrogate"; |
| 3658 | goto utf7Error; |
| 3659 | } |
| 3660 | else { |
| 3661 | *p++ = outCh; |
| 3662 | } |
| 3663 | } |
| 3664 | } |
| 3665 | else { /* now leaving a base-64 section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3666 | inShift = 0; |
| 3667 | s++; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3668 | if (surrogate) { |
| 3669 | errmsg = "second surrogate missing at end of shift sequence"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3670 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3671 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3672 | if (base64bits > 0) { /* left-over bits */ |
| 3673 | if (base64bits >= 6) { |
| 3674 | /* We've seen at least one base-64 character */ |
| 3675 | errmsg = "partial character in shift sequence"; |
| 3676 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3677 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3678 | else { |
| 3679 | /* Some bits remain; they should be zero */ |
| 3680 | if (base64buffer != 0) { |
| 3681 | errmsg = "non-zero padding bits in shift sequence"; |
| 3682 | goto utf7Error; |
| 3683 | } |
| 3684 | } |
| 3685 | } |
| 3686 | if (ch != '-') { |
| 3687 | /* '-' is absorbed; other terminating |
| 3688 | characters are preserved */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3689 | *p++ = ch; |
| 3690 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3691 | } |
| 3692 | } |
| 3693 | else if ( ch == '+' ) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3694 | startinpos = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3695 | s++; /* consume '+' */ |
| 3696 | if (s < e && *s == '-') { /* '+-' encodes '+' */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3697 | s++; |
| 3698 | *p++ = '+'; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3699 | } |
| 3700 | else { /* begin base64-encoded section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3701 | inShift = 1; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3702 | shiftOutStart = p; |
| 3703 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3704 | } |
| 3705 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3706 | else if (DECODE_DIRECT(ch)) { /* character decodes as itself */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3707 | *p++ = ch; |
| 3708 | s++; |
| 3709 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3710 | else { |
| 3711 | startinpos = s-starts; |
| 3712 | s++; |
| 3713 | errmsg = "unexpected special character"; |
| 3714 | goto utf7Error; |
| 3715 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3716 | continue; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3717 | utf7Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3718 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 3719 | endinpos = s-starts; |
| 3720 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3721 | errors, &errorHandler, |
| 3722 | "utf7", errmsg, |
| 3723 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 3724 | &unicode, &outpos, &p)) |
| 3725 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3726 | } |
| 3727 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3728 | /* end of string */ |
| 3729 | |
| 3730 | if (inShift && !consumed) { /* in shift sequence, no more to follow */ |
| 3731 | /* if we're in an inconsistent state, that's an error */ |
| 3732 | if (surrogate || |
| 3733 | (base64bits >= 6) || |
| 3734 | (base64bits > 0 && base64buffer != 0)) { |
| 3735 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 3736 | endinpos = size; |
| 3737 | if (unicode_decode_call_errorhandler( |
| 3738 | errors, &errorHandler, |
| 3739 | "utf7", "unterminated shift sequence", |
| 3740 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 3741 | &unicode, &outpos, &p)) |
| 3742 | goto onError; |
| 3743 | if (s < e) |
| 3744 | goto restart; |
| 3745 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3746 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3747 | |
| 3748 | /* return state */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3749 | if (consumed) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3750 | if (inShift) { |
| 3751 | p = shiftOutStart; /* back off output */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3752 | *consumed = startinpos; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3753 | } |
| 3754 | else { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3755 | *consumed = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3756 | } |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3757 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3758 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 3759 | if (PyUnicode_Resize((PyObject**)&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3760 | goto onError; |
| 3761 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3762 | Py_XDECREF(errorHandler); |
| 3763 | Py_XDECREF(exc); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 3764 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 3765 | if (_PyUnicode_READY_REPLACE(&unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3766 | Py_DECREF(unicode); |
| 3767 | return NULL; |
| 3768 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 3769 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 3770 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3771 | return (PyObject *)unicode; |
| 3772 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3773 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3774 | Py_XDECREF(errorHandler); |
| 3775 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3776 | Py_DECREF(unicode); |
| 3777 | return NULL; |
| 3778 | } |
| 3779 | |
| 3780 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3781 | PyObject * |
| 3782 | PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3783 | Py_ssize_t size, |
| 3784 | int base64SetO, |
| 3785 | int base64WhiteSpace, |
| 3786 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3787 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3788 | PyObject *v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3789 | /* It might be possible to tighten this worst case */ |
Alexandre Vassalotti | e85bd98 | 2009-07-21 00:39:03 +0000 | [diff] [blame] | 3790 | Py_ssize_t allocated = 8 * size; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3791 | int inShift = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3792 | Py_ssize_t i = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3793 | unsigned int base64bits = 0; |
| 3794 | unsigned long base64buffer = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3795 | char * out; |
| 3796 | char * start; |
| 3797 | |
| 3798 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3799 | return PyBytes_FromStringAndSize(NULL, 0); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3800 | |
Alexandre Vassalotti | e85bd98 | 2009-07-21 00:39:03 +0000 | [diff] [blame] | 3801 | if (allocated / 8 != size) |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3802 | return PyErr_NoMemory(); |
| 3803 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3804 | v = PyBytes_FromStringAndSize(NULL, allocated); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3805 | if (v == NULL) |
| 3806 | return NULL; |
| 3807 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3808 | start = out = PyBytes_AS_STRING(v); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3809 | for (;i < size; ++i) { |
| 3810 | Py_UNICODE ch = s[i]; |
| 3811 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3812 | if (inShift) { |
| 3813 | if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 3814 | /* shifting out */ |
| 3815 | if (base64bits) { /* output remaining bits */ |
| 3816 | *out++ = TO_BASE64(base64buffer << (6-base64bits)); |
| 3817 | base64buffer = 0; |
| 3818 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3819 | } |
| 3820 | inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3821 | /* Characters not in the BASE64 set implicitly unshift the sequence |
| 3822 | so no '-' is required, except if the character is itself a '-' */ |
| 3823 | if (IS_BASE64(ch) || ch == '-') { |
| 3824 | *out++ = '-'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3825 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3826 | *out++ = (char) ch; |
| 3827 | } |
| 3828 | else { |
| 3829 | goto encode_char; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3830 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3831 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3832 | else { /* not in a shift sequence */ |
| 3833 | if (ch == '+') { |
| 3834 | *out++ = '+'; |
| 3835 | *out++ = '-'; |
| 3836 | } |
| 3837 | else if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 3838 | *out++ = (char) ch; |
| 3839 | } |
| 3840 | else { |
| 3841 | *out++ = '+'; |
| 3842 | inShift = 1; |
| 3843 | goto encode_char; |
| 3844 | } |
| 3845 | } |
| 3846 | continue; |
| 3847 | encode_char: |
| 3848 | #ifdef Py_UNICODE_WIDE |
| 3849 | if (ch >= 0x10000) { |
| 3850 | /* code first surrogate */ |
| 3851 | base64bits += 16; |
| 3852 | base64buffer = (base64buffer << 16) | 0xd800 | ((ch-0x10000) >> 10); |
| 3853 | while (base64bits >= 6) { |
| 3854 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 3855 | base64bits -= 6; |
| 3856 | } |
| 3857 | /* prepare second surrogate */ |
| 3858 | ch = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 3859 | } |
| 3860 | #endif |
| 3861 | base64bits += 16; |
| 3862 | base64buffer = (base64buffer << 16) | ch; |
| 3863 | while (base64bits >= 6) { |
| 3864 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 3865 | base64bits -= 6; |
| 3866 | } |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 3867 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3868 | if (base64bits) |
| 3869 | *out++= TO_BASE64(base64buffer << (6-base64bits) ); |
| 3870 | if (inShift) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3871 | *out++ = '-'; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3872 | if (_PyBytes_Resize(&v, out - start) < 0) |
| 3873 | return NULL; |
| 3874 | return v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3875 | } |
| 3876 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3877 | #undef IS_BASE64 |
| 3878 | #undef FROM_BASE64 |
| 3879 | #undef TO_BASE64 |
| 3880 | #undef DECODE_DIRECT |
| 3881 | #undef ENCODE_DIRECT |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3882 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3883 | /* --- UTF-8 Codec -------------------------------------------------------- */ |
| 3884 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3885 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3886 | char utf8_code_length[256] = { |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 3887 | /* Map UTF-8 encoded prefix byte to sequence length. Zero means |
| 3888 | illegal prefix. See RFC 3629 for details */ |
| 3889 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 00-0F */ |
| 3890 | 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] | 3891 | 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] | 3892 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 3893 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 3894 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 3895 | 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] | 3896 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 70-7F */ |
| 3897 | 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] | 3898 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 3899 | 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] | 3900 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* B0-BF */ |
| 3901 | 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* C0-C1 + C2-CF */ |
| 3902 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* D0-DF */ |
| 3903 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* E0-EF */ |
| 3904 | 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] | 3905 | }; |
| 3906 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3907 | PyObject * |
| 3908 | PyUnicode_DecodeUTF8(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3909 | Py_ssize_t size, |
| 3910 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3911 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3912 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
| 3913 | } |
| 3914 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3915 | /* Mask to check or force alignment of a pointer to C 'long' boundaries */ |
| 3916 | #define LONG_PTR_MASK (size_t) (SIZEOF_LONG - 1) |
| 3917 | |
| 3918 | /* Mask to quickly check whether a C 'long' contains a |
| 3919 | non-ASCII, UTF8-encoded char. */ |
| 3920 | #if (SIZEOF_LONG == 8) |
| 3921 | # define ASCII_CHAR_MASK 0x8080808080808080L |
| 3922 | #elif (SIZEOF_LONG == 4) |
| 3923 | # define ASCII_CHAR_MASK 0x80808080L |
| 3924 | #else |
| 3925 | # error C 'long' size should be either 4 or 8! |
| 3926 | #endif |
| 3927 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3928 | /* Scans a UTF-8 string and returns the maximum character to be expected, |
| 3929 | the size of the decoded unicode string and if any major errors were |
| 3930 | encountered. |
| 3931 | |
| 3932 | This function does check basic UTF-8 sanity, it does however NOT CHECK |
| 3933 | if the string contains surrogates, and if all continuation bytes are |
| 3934 | within the correct ranges, these checks are performed in |
| 3935 | PyUnicode_DecodeUTF8Stateful. |
| 3936 | |
| 3937 | If it sets has_errors to 1, it means the value of unicode_size and max_char |
| 3938 | will be bogus and you should not rely on useful information in them. |
| 3939 | */ |
| 3940 | static Py_UCS4 |
| 3941 | utf8_max_char_size_and_has_errors(const char *s, Py_ssize_t string_size, |
| 3942 | Py_ssize_t *unicode_size, Py_ssize_t* consumed, |
| 3943 | int *has_errors) |
| 3944 | { |
| 3945 | Py_ssize_t n; |
| 3946 | Py_ssize_t char_count = 0; |
| 3947 | Py_UCS4 max_char = 127, new_max; |
| 3948 | Py_UCS4 upper_bound; |
| 3949 | const unsigned char *p = (const unsigned char *)s; |
| 3950 | const unsigned char *end = p + string_size; |
| 3951 | const unsigned char *aligned_end = (const unsigned char *) ((size_t) end & ~LONG_PTR_MASK); |
| 3952 | int err = 0; |
| 3953 | |
| 3954 | for (; p < end && !err; ++p, ++char_count) { |
| 3955 | /* Only check value if it's not a ASCII char... */ |
| 3956 | if (*p < 0x80) { |
| 3957 | /* Fast path, see below in PyUnicode_DecodeUTF8Stateful for |
| 3958 | an explanation. */ |
| 3959 | if (!((size_t) p & LONG_PTR_MASK)) { |
| 3960 | /* Help register allocation */ |
| 3961 | register const unsigned char *_p = p; |
| 3962 | while (_p < aligned_end) { |
| 3963 | unsigned long value = *(unsigned long *) _p; |
| 3964 | if (value & ASCII_CHAR_MASK) |
| 3965 | break; |
| 3966 | _p += SIZEOF_LONG; |
| 3967 | char_count += SIZEOF_LONG; |
| 3968 | } |
| 3969 | p = _p; |
| 3970 | if (p == end) |
| 3971 | break; |
| 3972 | } |
| 3973 | } |
| 3974 | if (*p >= 0x80) { |
| 3975 | n = utf8_code_length[*p]; |
| 3976 | new_max = max_char; |
| 3977 | switch (n) { |
| 3978 | /* invalid start byte */ |
| 3979 | case 0: |
| 3980 | err = 1; |
| 3981 | break; |
| 3982 | case 2: |
| 3983 | /* Code points between 0x00FF and 0x07FF inclusive. |
| 3984 | Approximate the upper bound of the code point, |
| 3985 | if this flips over 255 we can be sure it will be more |
| 3986 | than 255 and the string will need 2 bytes per code coint, |
| 3987 | if it stays under or equal to 255, we can be sure 1 byte |
| 3988 | is enough. |
| 3989 | ((*p & 0b00011111) << 6) | 0b00111111 */ |
| 3990 | upper_bound = ((*p & 0x1F) << 6) | 0x3F; |
| 3991 | if (max_char < upper_bound) |
| 3992 | new_max = upper_bound; |
| 3993 | /* Ensure we track at least that we left ASCII space. */ |
| 3994 | if (new_max < 128) |
| 3995 | new_max = 128; |
| 3996 | break; |
| 3997 | case 3: |
| 3998 | /* Between 0x0FFF and 0xFFFF inclusive, so values are |
| 3999 | always > 255 and <= 65535 and will always need 2 bytes. */ |
| 4000 | if (max_char < 65535) |
| 4001 | new_max = 65535; |
| 4002 | break; |
| 4003 | case 4: |
| 4004 | /* Code point will be above 0xFFFF for sure in this case. */ |
| 4005 | new_max = 65537; |
| 4006 | break; |
| 4007 | /* Internal error, this should be caught by the first if */ |
| 4008 | case 1: |
| 4009 | default: |
| 4010 | assert(0 && "Impossible case in utf8_max_char_and_size"); |
| 4011 | err = 1; |
| 4012 | } |
| 4013 | /* Instead of number of overall bytes for this code point, |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 4014 | n contains the number of following bytes: */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4015 | --n; |
| 4016 | /* Check if the follow up chars are all valid continuation bytes */ |
| 4017 | if (n >= 1) { |
| 4018 | const unsigned char *cont; |
| 4019 | if ((p + n) >= end) { |
| 4020 | if (consumed == 0) |
| 4021 | /* incomplete data, non-incremental decoding */ |
| 4022 | err = 1; |
| 4023 | break; |
| 4024 | } |
| 4025 | for (cont = p + 1; cont < (p + n); ++cont) { |
| 4026 | if ((*cont & 0xc0) != 0x80) { |
| 4027 | err = 1; |
| 4028 | break; |
| 4029 | } |
| 4030 | } |
| 4031 | p += n; |
| 4032 | } |
| 4033 | else |
| 4034 | err = 1; |
| 4035 | max_char = new_max; |
| 4036 | } |
| 4037 | } |
| 4038 | |
| 4039 | if (unicode_size) |
| 4040 | *unicode_size = char_count; |
| 4041 | if (has_errors) |
| 4042 | *has_errors = err; |
| 4043 | return max_char; |
| 4044 | } |
| 4045 | |
| 4046 | /* Similar to PyUnicode_WRITE but can also write into wstr field |
| 4047 | of the legacy unicode representation */ |
| 4048 | #define WRITE_FLEXIBLE_OR_WSTR(kind, buf, index, value) \ |
| 4049 | do { \ |
| 4050 | const int k_ = (kind); \ |
| 4051 | if (k_ == PyUnicode_WCHAR_KIND) \ |
| 4052 | ((Py_UNICODE *)(buf))[(index)] = (Py_UNICODE)(value); \ |
| 4053 | else if (k_ == PyUnicode_1BYTE_KIND) \ |
| 4054 | ((unsigned char *)(buf))[(index)] = (unsigned char)(value); \ |
| 4055 | else if (k_ == PyUnicode_2BYTE_KIND) \ |
| 4056 | ((Py_UCS2 *)(buf))[(index)] = (Py_UCS2)(value); \ |
| 4057 | else \ |
| 4058 | ((Py_UCS4 *)(buf))[(index)] = (Py_UCS4)(value); \ |
| 4059 | } while (0) |
| 4060 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4061 | PyObject * |
| 4062 | PyUnicode_DecodeUTF8Stateful(const char *s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4063 | Py_ssize_t size, |
| 4064 | const char *errors, |
| 4065 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4066 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4067 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4068 | int n; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4069 | int k; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4070 | Py_ssize_t startinpos; |
| 4071 | Py_ssize_t endinpos; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4072 | const char *e, *aligned_end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4073 | PyUnicodeObject *unicode; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4074 | const char *errmsg = ""; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4075 | PyObject *errorHandler = NULL; |
| 4076 | PyObject *exc = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4077 | Py_UCS4 maxchar = 0; |
| 4078 | Py_ssize_t unicode_size; |
| 4079 | Py_ssize_t i; |
| 4080 | int kind; |
| 4081 | void *data; |
| 4082 | int has_errors; |
| 4083 | Py_UNICODE *error_outptr; |
| 4084 | #if SIZEOF_WCHAR_T == 2 |
| 4085 | Py_ssize_t wchar_offset = 0; |
| 4086 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4087 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4088 | if (size == 0) { |
| 4089 | if (consumed) |
| 4090 | *consumed = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4091 | return (PyObject *)PyUnicode_New(0, 0); |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4092 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4093 | maxchar = utf8_max_char_size_and_has_errors(s, size, &unicode_size, |
| 4094 | consumed, &has_errors); |
| 4095 | if (has_errors) { |
| 4096 | unicode = _PyUnicode_New(size); |
| 4097 | if (!unicode) |
| 4098 | return NULL; |
| 4099 | kind = PyUnicode_WCHAR_KIND; |
| 4100 | data = PyUnicode_AS_UNICODE(unicode); |
| 4101 | assert(data != NULL); |
| 4102 | } |
| 4103 | else { |
| 4104 | unicode = (PyUnicodeObject *)PyUnicode_New(unicode_size, maxchar); |
| 4105 | if (!unicode) |
| 4106 | return NULL; |
| 4107 | /* When the string is ASCII only, just use memcpy and return. |
| 4108 | unicode_size may be != size if there is an incomplete UTF-8 |
| 4109 | sequence at the end of the ASCII block. */ |
| 4110 | if (maxchar < 128 && size == unicode_size) { |
| 4111 | Py_MEMCPY(PyUnicode_1BYTE_DATA(unicode), s, unicode_size); |
| 4112 | return (PyObject *)unicode; |
| 4113 | } |
| 4114 | kind = PyUnicode_KIND(unicode); |
| 4115 | data = PyUnicode_DATA(unicode); |
| 4116 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4117 | /* Unpack UTF-8 encoded data */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4118 | i = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4119 | e = s + size; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4120 | aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4121 | |
| 4122 | while (s < e) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 4123 | Py_UCS4 ch = (unsigned char)*s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4124 | |
| 4125 | if (ch < 0x80) { |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4126 | /* Fast path for runs of ASCII characters. Given that common UTF-8 |
| 4127 | input will consist of an overwhelming majority of ASCII |
| 4128 | characters, we try to optimize for this case by checking |
| 4129 | as many characters as a C 'long' can contain. |
| 4130 | First, check if we can do an aligned read, as most CPUs have |
| 4131 | a penalty for unaligned reads. |
| 4132 | */ |
| 4133 | if (!((size_t) s & LONG_PTR_MASK)) { |
| 4134 | /* Help register allocation */ |
| 4135 | register const char *_s = s; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4136 | register Py_ssize_t _i = i; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4137 | while (_s < aligned_end) { |
| 4138 | /* Read a whole long at a time (either 4 or 8 bytes), |
| 4139 | and do a fast unrolled copy if it only contains ASCII |
| 4140 | characters. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4141 | unsigned long value = *(unsigned long *) _s; |
| 4142 | if (value & ASCII_CHAR_MASK) |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4143 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4144 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+0, _s[0]); |
| 4145 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+1, _s[1]); |
| 4146 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+2, _s[2]); |
| 4147 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+3, _s[3]); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4148 | #if (SIZEOF_LONG == 8) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4149 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+4, _s[4]); |
| 4150 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+5, _s[5]); |
| 4151 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+6, _s[6]); |
| 4152 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+7, _s[7]); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4153 | #endif |
| 4154 | _s += SIZEOF_LONG; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4155 | _i += SIZEOF_LONG; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4156 | } |
| 4157 | s = _s; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4158 | i = _i; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4159 | if (s == e) |
| 4160 | break; |
| 4161 | ch = (unsigned char)*s; |
| 4162 | } |
| 4163 | } |
| 4164 | |
| 4165 | if (ch < 0x80) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4166 | WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4167 | s++; |
| 4168 | continue; |
| 4169 | } |
| 4170 | |
| 4171 | n = utf8_code_length[ch]; |
| 4172 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4173 | if (s + n > e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4174 | if (consumed) |
| 4175 | break; |
| 4176 | else { |
| 4177 | errmsg = "unexpected end of data"; |
| 4178 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4179 | endinpos = startinpos+1; |
| 4180 | for (k=1; (k < size-startinpos) && ((s[k]&0xC0) == 0x80); k++) |
| 4181 | endinpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4182 | goto utf8Error; |
| 4183 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4184 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4185 | |
| 4186 | switch (n) { |
| 4187 | |
| 4188 | case 0: |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4189 | errmsg = "invalid start byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4190 | startinpos = s-starts; |
| 4191 | endinpos = startinpos+1; |
| 4192 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4193 | |
| 4194 | case 1: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4195 | errmsg = "internal error"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4196 | startinpos = s-starts; |
| 4197 | endinpos = startinpos+1; |
| 4198 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4199 | |
| 4200 | case 2: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4201 | if ((s[1] & 0xc0) != 0x80) { |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4202 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4203 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4204 | endinpos = startinpos + 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4205 | goto utf8Error; |
| 4206 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4207 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4208 | assert ((ch > 0x007F) && (ch <= 0x07FF)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4209 | WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4210 | break; |
| 4211 | |
| 4212 | case 3: |
Ezio Melotti | 9bf2b3a | 2010-07-03 04:52:19 +0000 | [diff] [blame] | 4213 | /* Decoding UTF-8 sequences in range \xed\xa0\x80-\xed\xbf\xbf |
| 4214 | will result in surrogates in range d800-dfff. Surrogates are |
| 4215 | not valid UTF-8 so they are rejected. |
| 4216 | See http://www.unicode.org/versions/Unicode5.2.0/ch03.pdf |
| 4217 | (table 3-7) and http://www.rfc-editor.org/rfc/rfc3629.txt */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4218 | if ((s[1] & 0xc0) != 0x80 || |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4219 | (s[2] & 0xc0) != 0x80 || |
| 4220 | ((unsigned char)s[0] == 0xE0 && |
| 4221 | (unsigned char)s[1] < 0xA0) || |
| 4222 | ((unsigned char)s[0] == 0xED && |
| 4223 | (unsigned char)s[1] > 0x9F)) { |
| 4224 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4225 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4226 | endinpos = startinpos + 1; |
| 4227 | |
| 4228 | /* if s[1] first two bits are 1 and 0, then the invalid |
| 4229 | continuation byte is s[2], so increment endinpos by 1, |
| 4230 | if not, s[1] is invalid and endinpos doesn't need to |
| 4231 | be incremented. */ |
| 4232 | if ((s[1] & 0xC0) == 0x80) |
| 4233 | endinpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4234 | goto utf8Error; |
| 4235 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4236 | ch = ((s[0] & 0x0f) << 12) + ((s[1] & 0x3f) << 6) + (s[2] & 0x3f); |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4237 | assert ((ch > 0x07FF) && (ch <= 0xFFFF)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4238 | WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, ch); |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 4239 | break; |
| 4240 | |
| 4241 | case 4: |
| 4242 | if ((s[1] & 0xc0) != 0x80 || |
| 4243 | (s[2] & 0xc0) != 0x80 || |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4244 | (s[3] & 0xc0) != 0x80 || |
| 4245 | ((unsigned char)s[0] == 0xF0 && |
| 4246 | (unsigned char)s[1] < 0x90) || |
| 4247 | ((unsigned char)s[0] == 0xF4 && |
| 4248 | (unsigned char)s[1] > 0x8F)) { |
| 4249 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4250 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4251 | endinpos = startinpos + 1; |
| 4252 | if ((s[1] & 0xC0) == 0x80) { |
| 4253 | endinpos++; |
| 4254 | if ((s[2] & 0xC0) == 0x80) |
| 4255 | endinpos++; |
| 4256 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4257 | goto utf8Error; |
| 4258 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 4259 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4260 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
| 4261 | assert ((ch > 0xFFFF) && (ch <= 0x10ffff)); |
| 4262 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4263 | /* If the string is flexible or we have native UCS-4, write |
| 4264 | directly.. */ |
| 4265 | if (sizeof(Py_UNICODE) > 2 || kind != PyUnicode_WCHAR_KIND) |
| 4266 | WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, ch); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4267 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4268 | else { |
| 4269 | /* compute and append the two surrogates: */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4270 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4271 | /* translate from 10000..10FFFF to 0..FFFF */ |
| 4272 | ch -= 0x10000; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4273 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4274 | /* high surrogate = top 10 bits added to D800 */ |
| 4275 | WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, |
| 4276 | (Py_UNICODE)(0xD800 + (ch >> 10))); |
| 4277 | |
| 4278 | /* low surrogate = bottom 10 bits added to DC00 */ |
| 4279 | WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, |
| 4280 | (Py_UNICODE)(0xDC00 + (ch & 0x03FF))); |
| 4281 | } |
| 4282 | #if SIZEOF_WCHAR_T == 2 |
| 4283 | wchar_offset++; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 4284 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4285 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4286 | } |
| 4287 | s += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4288 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4289 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4290 | utf8Error: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4291 | /* If this is not yet a resizable string, make it one.. */ |
| 4292 | if (kind != PyUnicode_WCHAR_KIND) { |
| 4293 | const Py_UNICODE *u; |
| 4294 | PyUnicodeObject *new_unicode = _PyUnicode_New(size); |
| 4295 | if (!new_unicode) |
| 4296 | goto onError; |
| 4297 | u = PyUnicode_AsUnicode((PyObject *)unicode); |
| 4298 | if (!u) |
| 4299 | goto onError; |
| 4300 | #if SIZEOF_WCHAR_T == 2 |
| 4301 | i += wchar_offset; |
| 4302 | #endif |
| 4303 | Py_UNICODE_COPY(PyUnicode_AS_UNICODE(new_unicode), u, i); |
| 4304 | Py_DECREF(unicode); |
| 4305 | unicode = new_unicode; |
| 4306 | kind = 0; |
| 4307 | data = PyUnicode_AS_UNICODE(new_unicode); |
| 4308 | assert(data != NULL); |
| 4309 | } |
| 4310 | error_outptr = PyUnicode_AS_UNICODE(unicode) + i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4311 | if (unicode_decode_call_errorhandler( |
| 4312 | errors, &errorHandler, |
| 4313 | "utf8", errmsg, |
| 4314 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4315 | &unicode, &i, &error_outptr)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4316 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4317 | /* Update data because unicode_decode_call_errorhandler might have |
| 4318 | re-created or resized the unicode object. */ |
| 4319 | data = PyUnicode_AS_UNICODE(unicode); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4320 | aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4321 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4322 | /* Ensure the unicode_size calculation above was correct: */ |
| 4323 | assert(kind == PyUnicode_WCHAR_KIND || i == unicode_size); |
| 4324 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4325 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4326 | *consumed = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4327 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4328 | /* Adjust length and ready string when it contained errors and |
| 4329 | is of the old resizable kind. */ |
| 4330 | if (kind == PyUnicode_WCHAR_KIND) { |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 4331 | if (PyUnicode_Resize((PyObject**)&unicode, i) < 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4332 | goto onError; |
| 4333 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4334 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4335 | Py_XDECREF(errorHandler); |
| 4336 | Py_XDECREF(exc); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 4337 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 4338 | if (_PyUnicode_READY_REPLACE(&unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4339 | Py_DECREF(unicode); |
| 4340 | return NULL; |
| 4341 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 4342 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 4343 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4344 | return (PyObject *)unicode; |
| 4345 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4346 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4347 | Py_XDECREF(errorHandler); |
| 4348 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4349 | Py_DECREF(unicode); |
| 4350 | return NULL; |
| 4351 | } |
| 4352 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4353 | #undef WRITE_FLEXIBLE_OR_WSTR |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4354 | |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4355 | #ifdef __APPLE__ |
| 4356 | |
| 4357 | /* Simplified UTF-8 decoder using surrogateescape error handler, |
| 4358 | used to decode the command line arguments on Mac OS X. */ |
| 4359 | |
| 4360 | wchar_t* |
| 4361 | _Py_DecodeUTF8_surrogateescape(const char *s, Py_ssize_t size) |
| 4362 | { |
| 4363 | int n; |
| 4364 | const char *e; |
| 4365 | wchar_t *unicode, *p; |
| 4366 | |
| 4367 | /* Note: size will always be longer than the resulting Unicode |
| 4368 | character count */ |
| 4369 | if (PY_SSIZE_T_MAX / sizeof(wchar_t) < (size + 1)) { |
| 4370 | PyErr_NoMemory(); |
| 4371 | return NULL; |
| 4372 | } |
| 4373 | unicode = PyMem_Malloc((size + 1) * sizeof(wchar_t)); |
| 4374 | if (!unicode) |
| 4375 | return NULL; |
| 4376 | |
| 4377 | /* Unpack UTF-8 encoded data */ |
| 4378 | p = unicode; |
| 4379 | e = s + size; |
| 4380 | while (s < e) { |
| 4381 | Py_UCS4 ch = (unsigned char)*s; |
| 4382 | |
| 4383 | if (ch < 0x80) { |
| 4384 | *p++ = (wchar_t)ch; |
| 4385 | s++; |
| 4386 | continue; |
| 4387 | } |
| 4388 | |
| 4389 | n = utf8_code_length[ch]; |
| 4390 | if (s + n > e) { |
| 4391 | goto surrogateescape; |
| 4392 | } |
| 4393 | |
| 4394 | switch (n) { |
| 4395 | case 0: |
| 4396 | case 1: |
| 4397 | goto surrogateescape; |
| 4398 | |
| 4399 | case 2: |
| 4400 | if ((s[1] & 0xc0) != 0x80) |
| 4401 | goto surrogateescape; |
| 4402 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
| 4403 | assert ((ch > 0x007F) && (ch <= 0x07FF)); |
| 4404 | *p++ = (wchar_t)ch; |
| 4405 | break; |
| 4406 | |
| 4407 | case 3: |
| 4408 | /* Decoding UTF-8 sequences in range \xed\xa0\x80-\xed\xbf\xbf |
| 4409 | will result in surrogates in range d800-dfff. Surrogates are |
| 4410 | not valid UTF-8 so they are rejected. |
| 4411 | See http://www.unicode.org/versions/Unicode5.2.0/ch03.pdf |
| 4412 | (table 3-7) and http://www.rfc-editor.org/rfc/rfc3629.txt */ |
| 4413 | if ((s[1] & 0xc0) != 0x80 || |
| 4414 | (s[2] & 0xc0) != 0x80 || |
| 4415 | ((unsigned char)s[0] == 0xE0 && |
| 4416 | (unsigned char)s[1] < 0xA0) || |
| 4417 | ((unsigned char)s[0] == 0xED && |
| 4418 | (unsigned char)s[1] > 0x9F)) { |
| 4419 | |
| 4420 | goto surrogateescape; |
| 4421 | } |
| 4422 | ch = ((s[0] & 0x0f) << 12) + ((s[1] & 0x3f) << 6) + (s[2] & 0x3f); |
| 4423 | assert ((ch > 0x07FF) && (ch <= 0xFFFF)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4424 | *p++ = (wchar_t)ch; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4425 | break; |
| 4426 | |
| 4427 | case 4: |
| 4428 | if ((s[1] & 0xc0) != 0x80 || |
| 4429 | (s[2] & 0xc0) != 0x80 || |
| 4430 | (s[3] & 0xc0) != 0x80 || |
| 4431 | ((unsigned char)s[0] == 0xF0 && |
| 4432 | (unsigned char)s[1] < 0x90) || |
| 4433 | ((unsigned char)s[0] == 0xF4 && |
| 4434 | (unsigned char)s[1] > 0x8F)) { |
| 4435 | goto surrogateescape; |
| 4436 | } |
| 4437 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
| 4438 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
| 4439 | assert ((ch > 0xFFFF) && (ch <= 0x10ffff)); |
| 4440 | |
| 4441 | #if SIZEOF_WCHAR_T == 4 |
| 4442 | *p++ = (wchar_t)ch; |
| 4443 | #else |
| 4444 | /* compute and append the two surrogates: */ |
| 4445 | |
| 4446 | /* translate from 10000..10FFFF to 0..FFFF */ |
| 4447 | ch -= 0x10000; |
| 4448 | |
| 4449 | /* high surrogate = top 10 bits added to D800 */ |
| 4450 | *p++ = (wchar_t)(0xD800 + (ch >> 10)); |
| 4451 | |
| 4452 | /* low surrogate = bottom 10 bits added to DC00 */ |
| 4453 | *p++ = (wchar_t)(0xDC00 + (ch & 0x03FF)); |
| 4454 | #endif |
| 4455 | break; |
| 4456 | } |
| 4457 | s += n; |
| 4458 | continue; |
| 4459 | |
| 4460 | surrogateescape: |
| 4461 | *p++ = 0xDC00 + ch; |
| 4462 | s++; |
| 4463 | } |
| 4464 | *p = L'\0'; |
| 4465 | return unicode; |
| 4466 | } |
| 4467 | |
| 4468 | #endif /* __APPLE__ */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4469 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4470 | /* Primary internal function which creates utf8 encoded bytes objects. |
| 4471 | |
| 4472 | Allocation strategy: if the string is short, convert into a stack buffer |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4473 | and allocate exactly as much space needed at the end. Else allocate the |
| 4474 | maximum possible needed (4 result bytes per Unicode character), and return |
| 4475 | the excess memory at the end. |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4476 | */ |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 4477 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4478 | _PyUnicode_AsUTF8String(PyObject *obj, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4479 | { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4480 | #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] | 4481 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4482 | Py_ssize_t i; /* index into s of next input byte */ |
| 4483 | PyObject *result; /* result string object */ |
| 4484 | char *p; /* next free byte in output buffer */ |
| 4485 | Py_ssize_t nallocated; /* number of result bytes allocated */ |
| 4486 | Py_ssize_t nneeded; /* number of result bytes needed */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4487 | char stackbuf[MAX_SHORT_UNICHARS * 4]; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4488 | PyObject *errorHandler = NULL; |
| 4489 | PyObject *exc = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4490 | int kind; |
| 4491 | void *data; |
| 4492 | Py_ssize_t size; |
| 4493 | PyUnicodeObject *unicode = (PyUnicodeObject *)obj; |
| 4494 | #if SIZEOF_WCHAR_T == 2 |
| 4495 | Py_ssize_t wchar_offset = 0; |
| 4496 | #endif |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 4497 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4498 | if (!PyUnicode_Check(unicode)) { |
| 4499 | PyErr_BadArgument(); |
| 4500 | return NULL; |
| 4501 | } |
| 4502 | |
| 4503 | if (PyUnicode_READY(unicode) == -1) |
| 4504 | return NULL; |
| 4505 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 4506 | if (PyUnicode_UTF8(unicode)) |
| 4507 | return PyBytes_FromStringAndSize(PyUnicode_UTF8(unicode), |
| 4508 | PyUnicode_UTF8_LENGTH(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4509 | |
| 4510 | kind = PyUnicode_KIND(unicode); |
| 4511 | data = PyUnicode_DATA(unicode); |
| 4512 | size = PyUnicode_GET_LENGTH(unicode); |
| 4513 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4514 | assert(size >= 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4515 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4516 | if (size <= MAX_SHORT_UNICHARS) { |
| 4517 | /* Write into the stack buffer; nallocated can't overflow. |
| 4518 | * At the end, we'll allocate exactly as much heap space as it |
| 4519 | * turns out we need. |
| 4520 | */ |
| 4521 | nallocated = Py_SAFE_DOWNCAST(sizeof(stackbuf), size_t, int); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4522 | result = NULL; /* will allocate after we're done */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4523 | p = stackbuf; |
| 4524 | } |
| 4525 | else { |
| 4526 | /* Overallocate on the heap, and give the excess back at the end. */ |
| 4527 | nallocated = size * 4; |
| 4528 | if (nallocated / 4 != size) /* overflow! */ |
| 4529 | return PyErr_NoMemory(); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4530 | result = PyBytes_FromStringAndSize(NULL, nallocated); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4531 | if (result == NULL) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4532 | return NULL; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4533 | p = PyBytes_AS_STRING(result); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4534 | } |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4535 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4536 | for (i = 0; i < size;) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4537 | Py_UCS4 ch = PyUnicode_READ(kind, data, i++); |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 4538 | |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4539 | if (ch < 0x80) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4540 | /* Encode ASCII */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4541 | *p++ = (char) ch; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 4542 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4543 | else if (ch < 0x0800) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4544 | /* Encode Latin-1 */ |
Marc-André Lemburg | dc724d6 | 2002-02-06 18:20:19 +0000 | [diff] [blame] | 4545 | *p++ = (char)(0xc0 | (ch >> 6)); |
| 4546 | *p++ = (char)(0x80 | (ch & 0x3f)); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4547 | } else if (0xD800 <= ch && ch <= 0xDFFF) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4548 | Py_ssize_t newpos; |
| 4549 | PyObject *rep; |
| 4550 | Py_ssize_t repsize, k, startpos; |
| 4551 | startpos = i-1; |
| 4552 | #if SIZEOF_WCHAR_T == 2 |
| 4553 | startpos += wchar_offset; |
Victor Stinner | 445a623 | 2010-04-22 20:01:57 +0000 | [diff] [blame] | 4554 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4555 | rep = unicode_encode_call_errorhandler( |
| 4556 | errors, &errorHandler, "utf-8", "surrogates not allowed", |
| 4557 | PyUnicode_AS_UNICODE(unicode), PyUnicode_GET_SIZE(unicode), |
| 4558 | &exc, startpos, startpos+1, &newpos); |
| 4559 | if (!rep) |
| 4560 | goto error; |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4561 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4562 | if (PyBytes_Check(rep)) |
| 4563 | repsize = PyBytes_GET_SIZE(rep); |
| 4564 | else |
| 4565 | repsize = PyUnicode_GET_SIZE(rep); |
| 4566 | |
| 4567 | if (repsize > 4) { |
| 4568 | Py_ssize_t offset; |
| 4569 | |
| 4570 | if (result == NULL) |
| 4571 | offset = p - stackbuf; |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4572 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4573 | offset = p - PyBytes_AS_STRING(result); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4574 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4575 | if (nallocated > PY_SSIZE_T_MAX - repsize + 4) { |
| 4576 | /* integer overflow */ |
| 4577 | PyErr_NoMemory(); |
| 4578 | goto error; |
| 4579 | } |
| 4580 | nallocated += repsize - 4; |
| 4581 | if (result != NULL) { |
| 4582 | if (_PyBytes_Resize(&result, nallocated) < 0) |
| 4583 | goto error; |
| 4584 | } else { |
| 4585 | result = PyBytes_FromStringAndSize(NULL, nallocated); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4586 | if (result == NULL) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4587 | goto error; |
| 4588 | Py_MEMCPY(PyBytes_AS_STRING(result), stackbuf, offset); |
| 4589 | } |
| 4590 | p = PyBytes_AS_STRING(result) + offset; |
| 4591 | } |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4592 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4593 | if (PyBytes_Check(rep)) { |
| 4594 | char *prep = PyBytes_AS_STRING(rep); |
| 4595 | for(k = repsize; k > 0; k--) |
| 4596 | *p++ = *prep++; |
| 4597 | } else /* rep is unicode */ { |
| 4598 | const Py_UNICODE *prep = PyUnicode_AS_UNICODE(rep); |
| 4599 | Py_UNICODE c; |
| 4600 | |
| 4601 | for(k=0; k<repsize; k++) { |
| 4602 | c = prep[k]; |
| 4603 | if (0x80 <= c) { |
| 4604 | raise_encode_exception(&exc, "utf-8", |
| 4605 | PyUnicode_AS_UNICODE(unicode), |
| 4606 | size, i-1, i, |
| 4607 | "surrogates not allowed"); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4608 | goto error; |
| 4609 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4610 | *p++ = (char)prep[k]; |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4611 | } |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4612 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4613 | Py_DECREF(rep); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4614 | } else if (ch < 0x10000) { |
| 4615 | *p++ = (char)(0xe0 | (ch >> 12)); |
| 4616 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 4617 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 4618 | } else /* ch >= 0x10000 */ { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4619 | /* Encode UCS4 Unicode ordinals */ |
| 4620 | *p++ = (char)(0xf0 | (ch >> 18)); |
| 4621 | *p++ = (char)(0x80 | ((ch >> 12) & 0x3f)); |
| 4622 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 4623 | *p++ = (char)(0x80 | (ch & 0x3f)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4624 | #if SIZEOF_WCHAR_T == 2 |
| 4625 | wchar_offset++; |
| 4626 | #endif |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4627 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4628 | } |
Tim Peters | 0eca65c | 2002-04-21 17:28:06 +0000 | [diff] [blame] | 4629 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4630 | if (result == NULL) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4631 | /* This was stack allocated. */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4632 | nneeded = p - stackbuf; |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4633 | assert(nneeded <= nallocated); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4634 | result = PyBytes_FromStringAndSize(stackbuf, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4635 | } |
| 4636 | else { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 4637 | /* Cut back to size actually needed. */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4638 | nneeded = p - PyBytes_AS_STRING(result); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4639 | assert(nneeded <= nallocated); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4640 | _PyBytes_Resize(&result, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4641 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4642 | |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4643 | Py_XDECREF(errorHandler); |
| 4644 | Py_XDECREF(exc); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4645 | return result; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4646 | error: |
| 4647 | Py_XDECREF(errorHandler); |
| 4648 | Py_XDECREF(exc); |
| 4649 | Py_XDECREF(result); |
| 4650 | return NULL; |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4651 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4652 | #undef MAX_SHORT_UNICHARS |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4653 | } |
| 4654 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4655 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4656 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
| 4657 | Py_ssize_t size, |
| 4658 | const char *errors) |
| 4659 | { |
| 4660 | PyObject *v, *unicode; |
| 4661 | |
| 4662 | unicode = PyUnicode_FromUnicode(s, size); |
| 4663 | if (unicode == NULL) |
| 4664 | return NULL; |
| 4665 | v = _PyUnicode_AsUTF8String(unicode, errors); |
| 4666 | Py_DECREF(unicode); |
| 4667 | return v; |
| 4668 | } |
| 4669 | |
| 4670 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4671 | PyUnicode_AsUTF8String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4672 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4673 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4674 | } |
| 4675 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4676 | /* --- UTF-32 Codec ------------------------------------------------------- */ |
| 4677 | |
| 4678 | PyObject * |
| 4679 | PyUnicode_DecodeUTF32(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4680 | Py_ssize_t size, |
| 4681 | const char *errors, |
| 4682 | int *byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4683 | { |
| 4684 | return PyUnicode_DecodeUTF32Stateful(s, size, errors, byteorder, NULL); |
| 4685 | } |
| 4686 | |
| 4687 | PyObject * |
| 4688 | PyUnicode_DecodeUTF32Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4689 | Py_ssize_t size, |
| 4690 | const char *errors, |
| 4691 | int *byteorder, |
| 4692 | Py_ssize_t *consumed) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4693 | { |
| 4694 | const char *starts = s; |
| 4695 | Py_ssize_t startinpos; |
| 4696 | Py_ssize_t endinpos; |
| 4697 | Py_ssize_t outpos; |
| 4698 | PyUnicodeObject *unicode; |
| 4699 | Py_UNICODE *p; |
| 4700 | #ifndef Py_UNICODE_WIDE |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 4701 | int pairs = 0; |
Mark Dickinson | 7db923c | 2010-06-12 09:10:14 +0000 | [diff] [blame] | 4702 | const unsigned char *qq; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4703 | #else |
| 4704 | const int pairs = 0; |
| 4705 | #endif |
Mark Dickinson | 7db923c | 2010-06-12 09:10:14 +0000 | [diff] [blame] | 4706 | const unsigned char *q, *e; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4707 | int bo = 0; /* assume native ordering by default */ |
| 4708 | const char *errmsg = ""; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4709 | /* Offsets from q for retrieving bytes in the right order. */ |
| 4710 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 4711 | int iorder[] = {0, 1, 2, 3}; |
| 4712 | #else |
| 4713 | int iorder[] = {3, 2, 1, 0}; |
| 4714 | #endif |
| 4715 | PyObject *errorHandler = NULL; |
| 4716 | PyObject *exc = NULL; |
Victor Stinner | 313a120 | 2010-06-11 23:56:51 +0000 | [diff] [blame] | 4717 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4718 | q = (unsigned char *)s; |
| 4719 | e = q + size; |
| 4720 | |
| 4721 | if (byteorder) |
| 4722 | bo = *byteorder; |
| 4723 | |
| 4724 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 4725 | byte order setting accordingly. In native mode, the leading BOM |
| 4726 | mark is skipped, in all other modes, it is copied to the output |
| 4727 | stream as-is (giving a ZWNBSP character). */ |
| 4728 | if (bo == 0) { |
| 4729 | if (size >= 4) { |
| 4730 | const Py_UCS4 bom = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4731 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4732 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4733 | if (bom == 0x0000FEFF) { |
| 4734 | q += 4; |
| 4735 | bo = -1; |
| 4736 | } |
| 4737 | else if (bom == 0xFFFE0000) { |
| 4738 | q += 4; |
| 4739 | bo = 1; |
| 4740 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4741 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4742 | if (bom == 0x0000FEFF) { |
| 4743 | q += 4; |
| 4744 | bo = 1; |
| 4745 | } |
| 4746 | else if (bom == 0xFFFE0000) { |
| 4747 | q += 4; |
| 4748 | bo = -1; |
| 4749 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4750 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4751 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4752 | } |
| 4753 | |
| 4754 | if (bo == -1) { |
| 4755 | /* force LE */ |
| 4756 | iorder[0] = 0; |
| 4757 | iorder[1] = 1; |
| 4758 | iorder[2] = 2; |
| 4759 | iorder[3] = 3; |
| 4760 | } |
| 4761 | else if (bo == 1) { |
| 4762 | /* force BE */ |
| 4763 | iorder[0] = 3; |
| 4764 | iorder[1] = 2; |
| 4765 | iorder[2] = 1; |
| 4766 | iorder[3] = 0; |
| 4767 | } |
| 4768 | |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 4769 | /* On narrow builds we split characters outside the BMP into two |
| 4770 | codepoints => count how much extra space we need. */ |
| 4771 | #ifndef Py_UNICODE_WIDE |
| 4772 | for (qq = q; qq < e; qq += 4) |
| 4773 | if (qq[iorder[2]] != 0 || qq[iorder[3]] != 0) |
| 4774 | pairs++; |
| 4775 | #endif |
| 4776 | |
| 4777 | /* This might be one to much, because of a BOM */ |
| 4778 | unicode = _PyUnicode_New((size+3)/4+pairs); |
| 4779 | if (!unicode) |
| 4780 | return NULL; |
| 4781 | if (size == 0) |
| 4782 | return (PyObject *)unicode; |
| 4783 | |
| 4784 | /* Unpack UTF-32 encoded data */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4785 | p = PyUnicode_AS_UNICODE(unicode); |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 4786 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4787 | while (q < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4788 | Py_UCS4 ch; |
| 4789 | /* remaining bytes at the end? (size should be divisible by 4) */ |
| 4790 | if (e-q<4) { |
| 4791 | if (consumed) |
| 4792 | break; |
| 4793 | errmsg = "truncated data"; |
| 4794 | startinpos = ((const char *)q)-starts; |
| 4795 | endinpos = ((const char *)e)-starts; |
| 4796 | goto utf32Error; |
| 4797 | /* The remaining input chars are ignored if the callback |
| 4798 | chooses to skip the input */ |
| 4799 | } |
| 4800 | ch = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
| 4801 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4802 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4803 | if (ch >= 0x110000) |
| 4804 | { |
| 4805 | errmsg = "codepoint not in range(0x110000)"; |
| 4806 | startinpos = ((const char *)q)-starts; |
| 4807 | endinpos = startinpos+4; |
| 4808 | goto utf32Error; |
| 4809 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4810 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4811 | if (ch >= 0x10000) |
| 4812 | { |
| 4813 | *p++ = 0xD800 | ((ch-0x10000) >> 10); |
| 4814 | *p++ = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 4815 | } |
| 4816 | else |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4817 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4818 | *p++ = ch; |
| 4819 | q += 4; |
| 4820 | continue; |
| 4821 | utf32Error: |
| 4822 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 4823 | if (unicode_decode_call_errorhandler( |
| 4824 | errors, &errorHandler, |
| 4825 | "utf32", errmsg, |
| 4826 | &starts, (const char **)&e, &startinpos, &endinpos, &exc, (const char **)&q, |
| 4827 | &unicode, &outpos, &p)) |
| 4828 | goto onError; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4829 | } |
| 4830 | |
| 4831 | if (byteorder) |
| 4832 | *byteorder = bo; |
| 4833 | |
| 4834 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4835 | *consumed = (const char *)q-starts; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4836 | |
| 4837 | /* Adjust length */ |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 4838 | if (PyUnicode_Resize((PyObject**)&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4839 | goto onError; |
| 4840 | |
| 4841 | Py_XDECREF(errorHandler); |
| 4842 | Py_XDECREF(exc); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 4843 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 4844 | if (_PyUnicode_READY_REPLACE(&unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4845 | Py_DECREF(unicode); |
| 4846 | return NULL; |
| 4847 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 4848 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 4849 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4850 | return (PyObject *)unicode; |
| 4851 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4852 | onError: |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4853 | Py_DECREF(unicode); |
| 4854 | Py_XDECREF(errorHandler); |
| 4855 | Py_XDECREF(exc); |
| 4856 | return NULL; |
| 4857 | } |
| 4858 | |
| 4859 | PyObject * |
| 4860 | PyUnicode_EncodeUTF32(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4861 | Py_ssize_t size, |
| 4862 | const char *errors, |
| 4863 | int byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4864 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4865 | PyObject *v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4866 | unsigned char *p; |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4867 | Py_ssize_t nsize, bytesize; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4868 | #ifndef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4869 | Py_ssize_t i, pairs; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4870 | #else |
| 4871 | const int pairs = 0; |
| 4872 | #endif |
| 4873 | /* Offsets from p for storing byte pairs in the right order. */ |
| 4874 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 4875 | int iorder[] = {0, 1, 2, 3}; |
| 4876 | #else |
| 4877 | int iorder[] = {3, 2, 1, 0}; |
| 4878 | #endif |
| 4879 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4880 | #define STORECHAR(CH) \ |
| 4881 | do { \ |
| 4882 | p[iorder[3]] = ((CH) >> 24) & 0xff; \ |
| 4883 | p[iorder[2]] = ((CH) >> 16) & 0xff; \ |
| 4884 | p[iorder[1]] = ((CH) >> 8) & 0xff; \ |
| 4885 | p[iorder[0]] = (CH) & 0xff; \ |
| 4886 | p += 4; \ |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4887 | } while(0) |
| 4888 | |
| 4889 | /* In narrow builds we can output surrogate pairs as one codepoint, |
| 4890 | so we need less space. */ |
| 4891 | #ifndef Py_UNICODE_WIDE |
| 4892 | for (i = pairs = 0; i < size-1; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4893 | if (0xD800 <= s[i] && s[i] <= 0xDBFF && |
| 4894 | 0xDC00 <= s[i+1] && s[i+1] <= 0xDFFF) |
| 4895 | pairs++; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4896 | #endif |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4897 | nsize = (size - pairs + (byteorder == 0)); |
| 4898 | bytesize = nsize * 4; |
| 4899 | if (bytesize / 4 != nsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4900 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4901 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4902 | if (v == NULL) |
| 4903 | return NULL; |
| 4904 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4905 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4906 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4907 | STORECHAR(0xFEFF); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4908 | if (size == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4909 | goto done; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4910 | |
| 4911 | if (byteorder == -1) { |
| 4912 | /* force LE */ |
| 4913 | iorder[0] = 0; |
| 4914 | iorder[1] = 1; |
| 4915 | iorder[2] = 2; |
| 4916 | iorder[3] = 3; |
| 4917 | } |
| 4918 | else if (byteorder == 1) { |
| 4919 | /* force BE */ |
| 4920 | iorder[0] = 3; |
| 4921 | iorder[1] = 2; |
| 4922 | iorder[2] = 1; |
| 4923 | iorder[3] = 0; |
| 4924 | } |
| 4925 | |
| 4926 | while (size-- > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4927 | Py_UCS4 ch = *s++; |
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 (0xD800 <= ch && ch <= 0xDBFF && size > 0) { |
| 4930 | Py_UCS4 ch2 = *s; |
| 4931 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
| 4932 | ch = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
| 4933 | s++; |
| 4934 | size--; |
| 4935 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4936 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4937 | #endif |
| 4938 | STORECHAR(ch); |
| 4939 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4940 | |
| 4941 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4942 | return v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4943 | #undef STORECHAR |
| 4944 | } |
| 4945 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4946 | PyObject * |
| 4947 | PyUnicode_AsUTF32String(PyObject *unicode) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4948 | { |
| 4949 | if (!PyUnicode_Check(unicode)) { |
| 4950 | PyErr_BadArgument(); |
| 4951 | return NULL; |
| 4952 | } |
| 4953 | return PyUnicode_EncodeUTF32(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4954 | PyUnicode_GET_SIZE(unicode), |
| 4955 | NULL, |
| 4956 | 0); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4957 | } |
| 4958 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4959 | /* --- UTF-16 Codec ------------------------------------------------------- */ |
| 4960 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4961 | PyObject * |
| 4962 | PyUnicode_DecodeUTF16(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4963 | Py_ssize_t size, |
| 4964 | const char *errors, |
| 4965 | int *byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4966 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4967 | return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL); |
| 4968 | } |
| 4969 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4970 | /* Two masks for fast checking of whether a C 'long' may contain |
| 4971 | UTF16-encoded surrogate characters. This is an efficient heuristic, |
| 4972 | assuming that non-surrogate characters with a code point >= 0x8000 are |
| 4973 | rare in most input. |
| 4974 | FAST_CHAR_MASK is used when the input is in native byte ordering, |
| 4975 | SWAPPED_FAST_CHAR_MASK when the input is in byteswapped ordering. |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4976 | */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4977 | #if (SIZEOF_LONG == 8) |
| 4978 | # define FAST_CHAR_MASK 0x8000800080008000L |
| 4979 | # define SWAPPED_FAST_CHAR_MASK 0x0080008000800080L |
| 4980 | #elif (SIZEOF_LONG == 4) |
| 4981 | # define FAST_CHAR_MASK 0x80008000L |
| 4982 | # define SWAPPED_FAST_CHAR_MASK 0x00800080L |
| 4983 | #else |
| 4984 | # error C 'long' size should be either 4 or 8! |
| 4985 | #endif |
| 4986 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4987 | PyObject * |
| 4988 | PyUnicode_DecodeUTF16Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4989 | Py_ssize_t size, |
| 4990 | const char *errors, |
| 4991 | int *byteorder, |
| 4992 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4993 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4994 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4995 | Py_ssize_t startinpos; |
| 4996 | Py_ssize_t endinpos; |
| 4997 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4998 | PyUnicodeObject *unicode; |
| 4999 | Py_UNICODE *p; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5000 | const unsigned char *q, *e, *aligned_end; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5001 | int bo = 0; /* assume native ordering by default */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5002 | int native_ordering = 0; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 5003 | const char *errmsg = ""; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5004 | /* Offsets from q for retrieving byte pairs in the right order. */ |
| 5005 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5006 | int ihi = 1, ilo = 0; |
| 5007 | #else |
| 5008 | int ihi = 0, ilo = 1; |
| 5009 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5010 | PyObject *errorHandler = NULL; |
| 5011 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5012 | |
| 5013 | /* Note: size will always be longer than the resulting Unicode |
| 5014 | character count */ |
| 5015 | unicode = _PyUnicode_New(size); |
| 5016 | if (!unicode) |
| 5017 | return NULL; |
| 5018 | if (size == 0) |
| 5019 | return (PyObject *)unicode; |
| 5020 | |
| 5021 | /* Unpack UTF-16 encoded data */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5022 | p = PyUnicode_AS_UNICODE(unicode); |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5023 | q = (unsigned char *)s; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5024 | e = q + size - 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5025 | |
| 5026 | if (byteorder) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5027 | bo = *byteorder; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5028 | |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5029 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 5030 | byte order setting accordingly. In native mode, the leading BOM |
| 5031 | mark is skipped, in all other modes, it is copied to the output |
| 5032 | stream as-is (giving a ZWNBSP character). */ |
| 5033 | if (bo == 0) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5034 | if (size >= 2) { |
| 5035 | const Py_UNICODE bom = (q[ihi] << 8) | q[ilo]; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5036 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5037 | if (bom == 0xFEFF) { |
| 5038 | q += 2; |
| 5039 | bo = -1; |
| 5040 | } |
| 5041 | else if (bom == 0xFFFE) { |
| 5042 | q += 2; |
| 5043 | bo = 1; |
| 5044 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5045 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5046 | if (bom == 0xFEFF) { |
| 5047 | q += 2; |
| 5048 | bo = 1; |
| 5049 | } |
| 5050 | else if (bom == 0xFFFE) { |
| 5051 | q += 2; |
| 5052 | bo = -1; |
| 5053 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5054 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5055 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5056 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5057 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5058 | if (bo == -1) { |
| 5059 | /* force LE */ |
| 5060 | ihi = 1; |
| 5061 | ilo = 0; |
| 5062 | } |
| 5063 | else if (bo == 1) { |
| 5064 | /* force BE */ |
| 5065 | ihi = 0; |
| 5066 | ilo = 1; |
| 5067 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5068 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5069 | native_ordering = ilo < ihi; |
| 5070 | #else |
| 5071 | native_ordering = ilo > ihi; |
| 5072 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5073 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5074 | aligned_end = (const unsigned char *) ((size_t) e & ~LONG_PTR_MASK); |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5075 | while (q < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5076 | Py_UNICODE ch; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5077 | /* First check for possible aligned read of a C 'long'. Unaligned |
| 5078 | reads are more expensive, better to defer to another iteration. */ |
| 5079 | if (!((size_t) q & LONG_PTR_MASK)) { |
| 5080 | /* Fast path for runs of non-surrogate chars. */ |
| 5081 | register const unsigned char *_q = q; |
| 5082 | Py_UNICODE *_p = p; |
| 5083 | if (native_ordering) { |
| 5084 | /* Native ordering is simple: as long as the input cannot |
| 5085 | possibly contain a surrogate char, do an unrolled copy |
| 5086 | of several 16-bit code points to the target object. |
| 5087 | The non-surrogate check is done on several input bytes |
| 5088 | at a time (as many as a C 'long' can contain). */ |
| 5089 | while (_q < aligned_end) { |
| 5090 | unsigned long data = * (unsigned long *) _q; |
| 5091 | if (data & FAST_CHAR_MASK) |
| 5092 | break; |
| 5093 | _p[0] = ((unsigned short *) _q)[0]; |
| 5094 | _p[1] = ((unsigned short *) _q)[1]; |
| 5095 | #if (SIZEOF_LONG == 8) |
| 5096 | _p[2] = ((unsigned short *) _q)[2]; |
| 5097 | _p[3] = ((unsigned short *) _q)[3]; |
| 5098 | #endif |
| 5099 | _q += SIZEOF_LONG; |
| 5100 | _p += SIZEOF_LONG / 2; |
| 5101 | } |
| 5102 | } |
| 5103 | else { |
| 5104 | /* Byteswapped ordering is similar, but we must decompose |
| 5105 | the copy bytewise, and take care of zero'ing out the |
| 5106 | upper bytes if the target object is in 32-bit units |
| 5107 | (that is, in UCS-4 builds). */ |
| 5108 | while (_q < aligned_end) { |
| 5109 | unsigned long data = * (unsigned long *) _q; |
| 5110 | if (data & SWAPPED_FAST_CHAR_MASK) |
| 5111 | break; |
| 5112 | /* Zero upper bytes in UCS-4 builds */ |
| 5113 | #if (Py_UNICODE_SIZE > 2) |
| 5114 | _p[0] = 0; |
| 5115 | _p[1] = 0; |
| 5116 | #if (SIZEOF_LONG == 8) |
| 5117 | _p[2] = 0; |
| 5118 | _p[3] = 0; |
| 5119 | #endif |
| 5120 | #endif |
Antoine Pitrou | d6e8de1 | 2009-01-11 23:56:55 +0000 | [diff] [blame] | 5121 | /* Issue #4916; UCS-4 builds on big endian machines must |
| 5122 | fill the two last bytes of each 4-byte unit. */ |
| 5123 | #if (!defined(BYTEORDER_IS_LITTLE_ENDIAN) && Py_UNICODE_SIZE > 2) |
| 5124 | # define OFF 2 |
| 5125 | #else |
| 5126 | # define OFF 0 |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5127 | #endif |
Antoine Pitrou | d6e8de1 | 2009-01-11 23:56:55 +0000 | [diff] [blame] | 5128 | ((unsigned char *) _p)[OFF + 1] = _q[0]; |
| 5129 | ((unsigned char *) _p)[OFF + 0] = _q[1]; |
| 5130 | ((unsigned char *) _p)[OFF + 1 + Py_UNICODE_SIZE] = _q[2]; |
| 5131 | ((unsigned char *) _p)[OFF + 0 + Py_UNICODE_SIZE] = _q[3]; |
| 5132 | #if (SIZEOF_LONG == 8) |
| 5133 | ((unsigned char *) _p)[OFF + 1 + 2 * Py_UNICODE_SIZE] = _q[4]; |
| 5134 | ((unsigned char *) _p)[OFF + 0 + 2 * Py_UNICODE_SIZE] = _q[5]; |
| 5135 | ((unsigned char *) _p)[OFF + 1 + 3 * Py_UNICODE_SIZE] = _q[6]; |
| 5136 | ((unsigned char *) _p)[OFF + 0 + 3 * Py_UNICODE_SIZE] = _q[7]; |
| 5137 | #endif |
| 5138 | #undef OFF |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5139 | _q += SIZEOF_LONG; |
| 5140 | _p += SIZEOF_LONG / 2; |
| 5141 | } |
| 5142 | } |
| 5143 | p = _p; |
| 5144 | q = _q; |
| 5145 | if (q >= e) |
| 5146 | break; |
| 5147 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5148 | ch = (q[ihi] << 8) | q[ilo]; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5149 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5150 | q += 2; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5151 | |
| 5152 | if (ch < 0xD800 || ch > 0xDFFF) { |
| 5153 | *p++ = ch; |
| 5154 | continue; |
| 5155 | } |
| 5156 | |
| 5157 | /* UTF-16 code pair: */ |
| 5158 | if (q > e) { |
| 5159 | errmsg = "unexpected end of data"; |
| 5160 | startinpos = (((const char *)q) - 2) - starts; |
| 5161 | endinpos = ((const char *)e) + 1 - starts; |
| 5162 | goto utf16Error; |
| 5163 | } |
| 5164 | if (0xD800 <= ch && ch <= 0xDBFF) { |
| 5165 | Py_UNICODE ch2 = (q[ihi] << 8) | q[ilo]; |
| 5166 | q += 2; |
| 5167 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 5168 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5169 | *p++ = ch; |
| 5170 | *p++ = ch2; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5171 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5172 | *p++ = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5173 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5174 | continue; |
| 5175 | } |
| 5176 | else { |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5177 | errmsg = "illegal UTF-16 surrogate"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5178 | startinpos = (((const char *)q)-4)-starts; |
| 5179 | endinpos = startinpos+2; |
| 5180 | goto utf16Error; |
| 5181 | } |
| 5182 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5183 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5184 | errmsg = "illegal encoding"; |
| 5185 | startinpos = (((const char *)q)-2)-starts; |
| 5186 | endinpos = startinpos+2; |
| 5187 | /* Fall through to report the error */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5188 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5189 | utf16Error: |
| 5190 | outpos = p - PyUnicode_AS_UNICODE(unicode); |
| 5191 | if (unicode_decode_call_errorhandler( |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5192 | errors, |
| 5193 | &errorHandler, |
| 5194 | "utf16", errmsg, |
| 5195 | &starts, |
| 5196 | (const char **)&e, |
| 5197 | &startinpos, |
| 5198 | &endinpos, |
| 5199 | &exc, |
| 5200 | (const char **)&q, |
| 5201 | &unicode, |
| 5202 | &outpos, |
| 5203 | &p)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5204 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5205 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5206 | /* remaining byte at the end? (size should be even) */ |
| 5207 | if (e == q) { |
| 5208 | if (!consumed) { |
| 5209 | errmsg = "truncated data"; |
| 5210 | startinpos = ((const char *)q) - starts; |
| 5211 | endinpos = ((const char *)e) + 1 - starts; |
| 5212 | outpos = p - PyUnicode_AS_UNICODE(unicode); |
| 5213 | if (unicode_decode_call_errorhandler( |
| 5214 | errors, |
| 5215 | &errorHandler, |
| 5216 | "utf16", errmsg, |
| 5217 | &starts, |
| 5218 | (const char **)&e, |
| 5219 | &startinpos, |
| 5220 | &endinpos, |
| 5221 | &exc, |
| 5222 | (const char **)&q, |
| 5223 | &unicode, |
| 5224 | &outpos, |
| 5225 | &p)) |
| 5226 | goto onError; |
| 5227 | /* The remaining input chars are ignored if the callback |
| 5228 | chooses to skip the input */ |
| 5229 | } |
| 5230 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5231 | |
| 5232 | if (byteorder) |
| 5233 | *byteorder = bo; |
| 5234 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5235 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5236 | *consumed = (const char *)q-starts; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5237 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5238 | /* Adjust length */ |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 5239 | if (PyUnicode_Resize((PyObject**)&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5240 | goto onError; |
| 5241 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5242 | Py_XDECREF(errorHandler); |
| 5243 | Py_XDECREF(exc); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 5244 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 5245 | if (_PyUnicode_READY_REPLACE(&unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5246 | Py_DECREF(unicode); |
| 5247 | return NULL; |
| 5248 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 5249 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 5250 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5251 | return (PyObject *)unicode; |
| 5252 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5253 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5254 | Py_DECREF(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5255 | Py_XDECREF(errorHandler); |
| 5256 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5257 | return NULL; |
| 5258 | } |
| 5259 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5260 | #undef FAST_CHAR_MASK |
| 5261 | #undef SWAPPED_FAST_CHAR_MASK |
| 5262 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5263 | PyObject * |
| 5264 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5265 | Py_ssize_t size, |
| 5266 | const char *errors, |
| 5267 | int byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5268 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5269 | PyObject *v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5270 | unsigned char *p; |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5271 | Py_ssize_t nsize, bytesize; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 5272 | #ifdef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5273 | Py_ssize_t i, pairs; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 5274 | #else |
| 5275 | const int pairs = 0; |
| 5276 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5277 | /* Offsets from p for storing byte pairs in the right order. */ |
| 5278 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5279 | int ihi = 1, ilo = 0; |
| 5280 | #else |
| 5281 | int ihi = 0, ilo = 1; |
| 5282 | #endif |
| 5283 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5284 | #define STORECHAR(CH) \ |
| 5285 | do { \ |
| 5286 | p[ihi] = ((CH) >> 8) & 0xff; \ |
| 5287 | p[ilo] = (CH) & 0xff; \ |
| 5288 | p += 2; \ |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5289 | } while(0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5290 | |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 5291 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5292 | for (i = pairs = 0; i < size; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5293 | if (s[i] >= 0x10000) |
| 5294 | pairs++; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 5295 | #endif |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5296 | /* 2 * (size + pairs + (byteorder == 0)) */ |
| 5297 | if (size > PY_SSIZE_T_MAX || |
| 5298 | size > PY_SSIZE_T_MAX - pairs - (byteorder == 0)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5299 | return PyErr_NoMemory(); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5300 | nsize = size + pairs + (byteorder == 0); |
| 5301 | bytesize = nsize * 2; |
| 5302 | if (bytesize / 2 != nsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5303 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5304 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5305 | if (v == NULL) |
| 5306 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5307 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5308 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5309 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5310 | STORECHAR(0xFEFF); |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 5311 | if (size == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5312 | goto done; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5313 | |
| 5314 | if (byteorder == -1) { |
| 5315 | /* force LE */ |
| 5316 | ihi = 1; |
| 5317 | ilo = 0; |
| 5318 | } |
| 5319 | else if (byteorder == 1) { |
| 5320 | /* force BE */ |
| 5321 | ihi = 0; |
| 5322 | ilo = 1; |
| 5323 | } |
| 5324 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5325 | while (size-- > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5326 | Py_UNICODE ch = *s++; |
| 5327 | Py_UNICODE ch2 = 0; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 5328 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5329 | if (ch >= 0x10000) { |
| 5330 | ch2 = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 5331 | ch = 0xD800 | ((ch-0x10000) >> 10); |
| 5332 | } |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 5333 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5334 | STORECHAR(ch); |
| 5335 | if (ch2) |
| 5336 | STORECHAR(ch2); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5337 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5338 | |
| 5339 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5340 | return v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5341 | #undef STORECHAR |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5342 | } |
| 5343 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5344 | PyObject * |
| 5345 | PyUnicode_AsUTF16String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5346 | { |
| 5347 | if (!PyUnicode_Check(unicode)) { |
| 5348 | PyErr_BadArgument(); |
| 5349 | return NULL; |
| 5350 | } |
| 5351 | return PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5352 | PyUnicode_GET_SIZE(unicode), |
| 5353 | NULL, |
| 5354 | 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5355 | } |
| 5356 | |
| 5357 | /* --- Unicode Escape Codec ----------------------------------------------- */ |
| 5358 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5359 | /* Helper function for PyUnicode_DecodeUnicodeEscape, determines |
| 5360 | if all the escapes in the string make it still a valid ASCII string. |
| 5361 | Returns -1 if any escapes were found which cause the string to |
| 5362 | pop out of ASCII range. Otherwise returns the length of the |
| 5363 | required buffer to hold the string. |
| 5364 | */ |
| 5365 | Py_ssize_t |
| 5366 | length_of_escaped_ascii_string(const char *s, Py_ssize_t size) |
| 5367 | { |
| 5368 | const unsigned char *p = (const unsigned char *)s; |
| 5369 | const unsigned char *end = p + size; |
| 5370 | Py_ssize_t length = 0; |
| 5371 | |
| 5372 | if (size < 0) |
| 5373 | return -1; |
| 5374 | |
| 5375 | for (; p < end; ++p) { |
| 5376 | if (*p > 127) { |
| 5377 | /* Non-ASCII */ |
| 5378 | return -1; |
| 5379 | } |
| 5380 | else if (*p != '\\') { |
| 5381 | /* Normal character */ |
| 5382 | ++length; |
| 5383 | } |
| 5384 | else { |
| 5385 | /* Backslash-escape, check next char */ |
| 5386 | ++p; |
| 5387 | /* Escape sequence reaches till end of string or |
| 5388 | non-ASCII follow-up. */ |
| 5389 | if (p >= end || *p > 127) |
| 5390 | return -1; |
| 5391 | switch (*p) { |
| 5392 | case '\n': |
| 5393 | /* backslash + \n result in zero characters */ |
| 5394 | break; |
| 5395 | case '\\': case '\'': case '\"': |
| 5396 | case 'b': case 'f': case 't': |
| 5397 | case 'n': case 'r': case 'v': case 'a': |
| 5398 | ++length; |
| 5399 | break; |
| 5400 | case '0': case '1': case '2': case '3': |
| 5401 | case '4': case '5': case '6': case '7': |
| 5402 | case 'x': case 'u': case 'U': case 'N': |
| 5403 | /* these do not guarantee ASCII characters */ |
| 5404 | return -1; |
| 5405 | default: |
| 5406 | /* count the backslash + the other character */ |
| 5407 | length += 2; |
| 5408 | } |
| 5409 | } |
| 5410 | } |
| 5411 | return length; |
| 5412 | } |
| 5413 | |
| 5414 | /* Similar to PyUnicode_WRITE but either write into wstr field |
| 5415 | or treat string as ASCII. */ |
| 5416 | #define WRITE_ASCII_OR_WSTR(kind, buf, index, value) \ |
| 5417 | do { \ |
| 5418 | if ((kind) != PyUnicode_WCHAR_KIND) \ |
| 5419 | ((unsigned char *)(buf))[(index)] = (unsigned char)(value); \ |
| 5420 | else \ |
| 5421 | ((Py_UNICODE *)(buf))[(index)] = (Py_UNICODE)(value); \ |
| 5422 | } while (0) |
| 5423 | |
| 5424 | #define WRITE_WSTR(buf, index, value) \ |
| 5425 | assert(kind == PyUnicode_WCHAR_KIND), \ |
| 5426 | ((Py_UNICODE *)(buf))[(index)] = (Py_UNICODE)(value) |
| 5427 | |
| 5428 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 5429 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; |
Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 5430 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5431 | PyObject * |
| 5432 | PyUnicode_DecodeUnicodeEscape(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5433 | Py_ssize_t size, |
Victor Stinner | c17f540 | 2011-09-29 00:16:58 +0200 | [diff] [blame] | 5434 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5435 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5436 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5437 | Py_ssize_t startinpos; |
| 5438 | Py_ssize_t endinpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5439 | int j; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5440 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5441 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5442 | const char *end; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5443 | char* message; |
| 5444 | Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5445 | PyObject *errorHandler = NULL; |
| 5446 | PyObject *exc = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5447 | Py_ssize_t ascii_length; |
| 5448 | Py_ssize_t i; |
| 5449 | int kind; |
| 5450 | void *data; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5451 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5452 | ascii_length = length_of_escaped_ascii_string(s, size); |
| 5453 | |
| 5454 | /* After length_of_escaped_ascii_string() there are two alternatives, |
| 5455 | either the string is pure ASCII with named escapes like \n, etc. |
| 5456 | and we determined it's exact size (common case) |
| 5457 | or it contains \x, \u, ... escape sequences. then we create a |
| 5458 | legacy wchar string and resize it at the end of this function. */ |
| 5459 | if (ascii_length >= 0) { |
| 5460 | v = (PyUnicodeObject *)PyUnicode_New(ascii_length, 127); |
| 5461 | if (!v) |
| 5462 | goto onError; |
| 5463 | assert(PyUnicode_KIND(v) == PyUnicode_1BYTE_KIND); |
| 5464 | kind = PyUnicode_1BYTE_KIND; |
| 5465 | data = PyUnicode_DATA(v); |
| 5466 | } |
| 5467 | else { |
| 5468 | /* Escaped strings will always be longer than the resulting |
| 5469 | Unicode string, so we start with size here and then reduce the |
| 5470 | length after conversion to the true value. |
| 5471 | (but if the error callback returns a long replacement string |
| 5472 | we'll have to allocate more space) */ |
| 5473 | v = _PyUnicode_New(size); |
| 5474 | if (!v) |
| 5475 | goto onError; |
| 5476 | kind = PyUnicode_WCHAR_KIND; |
| 5477 | data = PyUnicode_AS_UNICODE(v); |
| 5478 | } |
| 5479 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5480 | if (size == 0) |
| 5481 | return (PyObject *)v; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5482 | i = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5483 | end = s + size; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5484 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5485 | while (s < end) { |
| 5486 | unsigned char c; |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 5487 | Py_UNICODE x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5488 | int digits; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5489 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5490 | if (kind == PyUnicode_WCHAR_KIND) { |
| 5491 | assert(i < _PyUnicode_WSTR_LENGTH(v)); |
| 5492 | } |
| 5493 | else { |
| 5494 | /* The only case in which i == ascii_length is a backslash |
| 5495 | followed by a newline. */ |
| 5496 | assert(i <= ascii_length); |
| 5497 | } |
| 5498 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5499 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 5500 | if (*s != '\\') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5501 | WRITE_ASCII_OR_WSTR(kind, data, i++, (unsigned char) *s++); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5502 | continue; |
| 5503 | } |
| 5504 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5505 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5506 | /* \ - Escapes */ |
| 5507 | s++; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5508 | c = *s++; |
| 5509 | if (s > end) |
| 5510 | c = '\0'; /* Invalid after \ */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5511 | |
| 5512 | if (kind == PyUnicode_WCHAR_KIND) { |
| 5513 | assert(i < _PyUnicode_WSTR_LENGTH(v)); |
| 5514 | } |
| 5515 | else { |
| 5516 | /* The only case in which i == ascii_length is a backslash |
| 5517 | followed by a newline. */ |
| 5518 | assert(i < ascii_length || (i == ascii_length && c == '\n')); |
| 5519 | } |
| 5520 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5521 | switch (c) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5522 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5523 | /* \x escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5524 | case '\n': break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5525 | case '\\': WRITE_ASCII_OR_WSTR(kind, data, i++, '\\'); break; |
| 5526 | case '\'': WRITE_ASCII_OR_WSTR(kind, data, i++, '\''); break; |
| 5527 | case '\"': WRITE_ASCII_OR_WSTR(kind, data, i++, '\"'); break; |
| 5528 | case 'b': WRITE_ASCII_OR_WSTR(kind, data, i++, '\b'); break; |
| 5529 | /* FF */ |
| 5530 | case 'f': WRITE_ASCII_OR_WSTR(kind, data, i++, '\014'); break; |
| 5531 | case 't': WRITE_ASCII_OR_WSTR(kind, data, i++, '\t'); break; |
| 5532 | case 'n': WRITE_ASCII_OR_WSTR(kind, data, i++, '\n'); break; |
| 5533 | case 'r': WRITE_ASCII_OR_WSTR(kind, data, i++, '\r'); break; |
| 5534 | /* VT */ |
| 5535 | case 'v': WRITE_ASCII_OR_WSTR(kind, data, i++, '\013'); break; |
| 5536 | /* BEL, not classic C */ |
| 5537 | case 'a': WRITE_ASCII_OR_WSTR(kind, data, i++, '\007'); break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5538 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5539 | /* \OOO (octal) escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5540 | case '0': case '1': case '2': case '3': |
| 5541 | case '4': case '5': case '6': case '7': |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5542 | x = s[-1] - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5543 | if (s < end && '0' <= *s && *s <= '7') { |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5544 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5545 | if (s < end && '0' <= *s && *s <= '7') |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5546 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5547 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5548 | WRITE_WSTR(data, i++, x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5549 | break; |
| 5550 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5551 | /* hex escapes */ |
| 5552 | /* \xXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5553 | case 'x': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5554 | digits = 2; |
| 5555 | message = "truncated \\xXX escape"; |
| 5556 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5557 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5558 | /* \uXXXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5559 | case 'u': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5560 | digits = 4; |
| 5561 | message = "truncated \\uXXXX escape"; |
| 5562 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5563 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5564 | /* \UXXXXXXXX */ |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5565 | case 'U': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5566 | digits = 8; |
| 5567 | message = "truncated \\UXXXXXXXX escape"; |
| 5568 | hexescape: |
| 5569 | chr = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5570 | p = PyUnicode_AS_UNICODE(v) + i; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5571 | if (s+digits>end) { |
| 5572 | endinpos = size; |
| 5573 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5574 | errors, &errorHandler, |
| 5575 | "unicodeescape", "end of string in escape sequence", |
| 5576 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5577 | &v, &i, &p)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5578 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5579 | data = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5580 | goto nextByte; |
| 5581 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5582 | for (j = 0; j < digits; ++j) { |
| 5583 | c = (unsigned char) s[j]; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 5584 | if (!Py_ISXDIGIT(c)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5585 | endinpos = (s+j+1)-starts; |
| 5586 | p = PyUnicode_AS_UNICODE(v) + i; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5587 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5588 | errors, &errorHandler, |
| 5589 | "unicodeescape", message, |
| 5590 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5591 | &v, &i, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5592 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5593 | data = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5594 | goto nextByte; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5595 | } |
| 5596 | chr = (chr<<4) & ~0xF; |
| 5597 | if (c >= '0' && c <= '9') |
| 5598 | chr += c - '0'; |
| 5599 | else if (c >= 'a' && c <= 'f') |
| 5600 | chr += 10 + c - 'a'; |
| 5601 | else |
| 5602 | chr += 10 + c - 'A'; |
| 5603 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5604 | s += j; |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 5605 | if (chr == 0xffffffff && PyErr_Occurred()) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5606 | /* _decoding_error will have already written into the |
| 5607 | target buffer. */ |
| 5608 | break; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5609 | store: |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5610 | /* when we get here, chr is a 32-bit unicode character */ |
| 5611 | if (chr <= 0xffff) |
| 5612 | /* UCS-2 character */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5613 | WRITE_WSTR(data, i++, chr); |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5614 | else if (chr <= 0x10ffff) { |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 5615 | /* UCS-4 character. Either store directly, or as |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5616 | surrogate pair. */ |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 5617 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5618 | WRITE_WSTR(data, i++, chr); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5619 | #else |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5620 | chr -= 0x10000L; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5621 | WRITE_WSTR(data, i++, 0xD800 + (Py_UNICODE) (chr >> 10)); |
| 5622 | WRITE_WSTR(data, i++, 0xDC00 + (Py_UNICODE) (chr & 0x03FF)); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5623 | #endif |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5624 | } else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5625 | endinpos = s-starts; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5626 | p = PyUnicode_AS_UNICODE(v) + i; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5627 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5628 | errors, &errorHandler, |
| 5629 | "unicodeescape", "illegal Unicode character", |
| 5630 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5631 | &v, &i, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5632 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5633 | data = PyUnicode_AS_UNICODE(v); |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5634 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5635 | break; |
| 5636 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5637 | /* \N{name} */ |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5638 | case 'N': |
| 5639 | message = "malformed \\N character escape"; |
| 5640 | if (ucnhash_CAPI == NULL) { |
| 5641 | /* load the unicode data module */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5642 | ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCapsule_Import( |
| 5643 | PyUnicodeData_CAPSULE_NAME, 1); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5644 | if (ucnhash_CAPI == NULL) |
| 5645 | goto ucnhashError; |
| 5646 | } |
| 5647 | if (*s == '{') { |
| 5648 | const char *start = s+1; |
| 5649 | /* look for the closing brace */ |
| 5650 | while (*s != '}' && s < end) |
| 5651 | s++; |
| 5652 | if (s > start && s < end && *s == '}') { |
| 5653 | /* found a name. look it up in the unicode database */ |
| 5654 | message = "unknown Unicode character name"; |
| 5655 | s++; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5656 | if (ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), |
| 5657 | &chr)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5658 | goto store; |
| 5659 | } |
| 5660 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5661 | endinpos = s-starts; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5662 | p = PyUnicode_AS_UNICODE(v) + i; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5663 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5664 | errors, &errorHandler, |
| 5665 | "unicodeescape", message, |
| 5666 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5667 | &v, &i, &p)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5668 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5669 | data = PyUnicode_AS_UNICODE(v); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5670 | break; |
| 5671 | |
| 5672 | default: |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5673 | if (s > end) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5674 | assert(kind == PyUnicode_WCHAR_KIND); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5675 | message = "\\ at end of string"; |
| 5676 | s--; |
| 5677 | endinpos = s-starts; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5678 | p = PyUnicode_AS_UNICODE(v) + i; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5679 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5680 | errors, &errorHandler, |
| 5681 | "unicodeescape", message, |
| 5682 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5683 | &v, &i, &p)) |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5684 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5685 | data = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5686 | } |
| 5687 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5688 | WRITE_ASCII_OR_WSTR(kind, data, i++, '\\'); |
| 5689 | WRITE_ASCII_OR_WSTR(kind, data, i++, (unsigned char)s[-1]); |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5690 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5691 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5692 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5693 | nextByte: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5694 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5695 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5696 | /* Ensure the length prediction worked in case of ASCII strings */ |
| 5697 | assert(kind == PyUnicode_WCHAR_KIND || i == ascii_length); |
| 5698 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 5699 | if (kind == PyUnicode_WCHAR_KIND) |
| 5700 | { |
| 5701 | if (PyUnicode_Resize((PyObject**)&v, i) < 0) |
| 5702 | goto onError; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 5703 | } |
Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 5704 | Py_XDECREF(errorHandler); |
| 5705 | Py_XDECREF(exc); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 5706 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 5707 | if (_PyUnicode_READY_REPLACE(&v)) { |
| 5708 | Py_DECREF(v); |
| 5709 | return NULL; |
| 5710 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 5711 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 5712 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5713 | return (PyObject *)v; |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5714 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5715 | ucnhashError: |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 5716 | PyErr_SetString( |
| 5717 | PyExc_UnicodeError, |
| 5718 | "\\N escapes not supported (can't load unicodedata module)" |
| 5719 | ); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 5720 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5721 | Py_XDECREF(errorHandler); |
| 5722 | Py_XDECREF(exc); |
Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 5723 | return NULL; |
| 5724 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5725 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5726 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5727 | Py_XDECREF(errorHandler); |
| 5728 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5729 | return NULL; |
| 5730 | } |
| 5731 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5732 | #undef WRITE_ASCII_OR_WSTR |
| 5733 | #undef WRITE_WSTR |
| 5734 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5735 | /* Return a Unicode-Escape string version of the Unicode object. |
| 5736 | |
| 5737 | If quotes is true, the string is enclosed in u"" or u'' quotes as |
| 5738 | appropriate. |
| 5739 | |
| 5740 | */ |
| 5741 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5742 | static const char *hexdigits = "0123456789abcdef"; |
| 5743 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5744 | PyObject * |
| 5745 | PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5746 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5747 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5748 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5749 | char *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5750 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5751 | #ifdef Py_UNICODE_WIDE |
| 5752 | const Py_ssize_t expandsize = 10; |
| 5753 | #else |
| 5754 | const Py_ssize_t expandsize = 6; |
| 5755 | #endif |
| 5756 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5757 | /* XXX(nnorwitz): rather than over-allocating, it would be |
| 5758 | better to choose a different scheme. Perhaps scan the |
| 5759 | first N-chars of the string and allocate based on that size. |
| 5760 | */ |
| 5761 | /* Initial allocation is based on the longest-possible unichr |
| 5762 | escape. |
| 5763 | |
| 5764 | In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source |
| 5765 | unichr, so in this case it's the longest unichr escape. In |
| 5766 | narrow (UTF-16) builds this is five chars per source unichr |
| 5767 | since there are two unichrs in the surrogate pair, so in narrow |
| 5768 | (UTF-16) builds it's not the longest unichr escape. |
| 5769 | |
| 5770 | In wide or narrow builds '\uxxxx' is 6 chars per source unichr, |
| 5771 | so in the narrow (UTF-16) build case it's the longest unichr |
| 5772 | escape. |
| 5773 | */ |
| 5774 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5775 | if (size == 0) |
| 5776 | return PyBytes_FromStringAndSize(NULL, 0); |
| 5777 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5778 | if (size > (PY_SSIZE_T_MAX - 2 - 1) / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5779 | return PyErr_NoMemory(); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5780 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5781 | repr = PyBytes_FromStringAndSize(NULL, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5782 | 2 |
| 5783 | + expandsize*size |
| 5784 | + 1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5785 | if (repr == NULL) |
| 5786 | return NULL; |
| 5787 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5788 | p = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5789 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5790 | while (size-- > 0) { |
| 5791 | Py_UNICODE ch = *s++; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5792 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5793 | /* Escape backslashes */ |
| 5794 | if (ch == '\\') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5795 | *p++ = '\\'; |
| 5796 | *p++ = (char) ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5797 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5798 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5799 | |
Guido van Rossum | 0d42e0c | 2001-07-20 16:36:21 +0000 | [diff] [blame] | 5800 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5801 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 5802 | else if (ch >= 0x10000) { |
| 5803 | *p++ = '\\'; |
| 5804 | *p++ = 'U'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5805 | *p++ = hexdigits[(ch >> 28) & 0x0000000F]; |
| 5806 | *p++ = hexdigits[(ch >> 24) & 0x0000000F]; |
| 5807 | *p++ = hexdigits[(ch >> 20) & 0x0000000F]; |
| 5808 | *p++ = hexdigits[(ch >> 16) & 0x0000000F]; |
| 5809 | *p++ = hexdigits[(ch >> 12) & 0x0000000F]; |
| 5810 | *p++ = hexdigits[(ch >> 8) & 0x0000000F]; |
| 5811 | *p++ = hexdigits[(ch >> 4) & 0x0000000F]; |
| 5812 | *p++ = hexdigits[ch & 0x0000000F]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5813 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5814 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5815 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5816 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
| 5817 | else if (ch >= 0xD800 && ch < 0xDC00) { |
| 5818 | Py_UNICODE ch2; |
| 5819 | Py_UCS4 ucs; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5820 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5821 | ch2 = *s++; |
| 5822 | size--; |
Georg Brandl | 78eef3de | 2010-08-01 20:51:02 +0000 | [diff] [blame] | 5823 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5824 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 5825 | *p++ = '\\'; |
| 5826 | *p++ = 'U'; |
| 5827 | *p++ = hexdigits[(ucs >> 28) & 0x0000000F]; |
| 5828 | *p++ = hexdigits[(ucs >> 24) & 0x0000000F]; |
| 5829 | *p++ = hexdigits[(ucs >> 20) & 0x0000000F]; |
| 5830 | *p++ = hexdigits[(ucs >> 16) & 0x0000000F]; |
| 5831 | *p++ = hexdigits[(ucs >> 12) & 0x0000000F]; |
| 5832 | *p++ = hexdigits[(ucs >> 8) & 0x0000000F]; |
| 5833 | *p++ = hexdigits[(ucs >> 4) & 0x0000000F]; |
| 5834 | *p++ = hexdigits[ucs & 0x0000000F]; |
| 5835 | continue; |
| 5836 | } |
| 5837 | /* Fall through: isolated surrogates are copied as-is */ |
| 5838 | s--; |
| 5839 | size++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5840 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5841 | #endif |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 5842 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5843 | /* Map 16-bit characters to '\uxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 5844 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5845 | *p++ = '\\'; |
| 5846 | *p++ = 'u'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5847 | *p++ = hexdigits[(ch >> 12) & 0x000F]; |
| 5848 | *p++ = hexdigits[(ch >> 8) & 0x000F]; |
| 5849 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 5850 | *p++ = hexdigits[ch & 0x000F]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5851 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5852 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 5853 | /* Map special whitespace to '\t', \n', '\r' */ |
| 5854 | else if (ch == '\t') { |
| 5855 | *p++ = '\\'; |
| 5856 | *p++ = 't'; |
| 5857 | } |
| 5858 | else if (ch == '\n') { |
| 5859 | *p++ = '\\'; |
| 5860 | *p++ = 'n'; |
| 5861 | } |
| 5862 | else if (ch == '\r') { |
| 5863 | *p++ = '\\'; |
| 5864 | *p++ = 'r'; |
| 5865 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5866 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 5867 | /* Map non-printable US ASCII to '\xhh' */ |
Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 5868 | else if (ch < ' ' || ch >= 0x7F) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5869 | *p++ = '\\'; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 5870 | *p++ = 'x'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5871 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 5872 | *p++ = hexdigits[ch & 0x000F]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5873 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5874 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5875 | /* Copy everything else as-is */ |
| 5876 | else |
| 5877 | *p++ = (char) ch; |
| 5878 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5879 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5880 | assert(p - PyBytes_AS_STRING(repr) > 0); |
| 5881 | if (_PyBytes_Resize(&repr, p - PyBytes_AS_STRING(repr)) < 0) |
| 5882 | return NULL; |
| 5883 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5884 | } |
| 5885 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5886 | PyObject * |
| 5887 | PyUnicode_AsUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5888 | { |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 5889 | PyObject *s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5890 | if (!PyUnicode_Check(unicode)) { |
| 5891 | PyErr_BadArgument(); |
| 5892 | return NULL; |
| 5893 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5894 | s = PyUnicode_EncodeUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 5895 | PyUnicode_GET_SIZE(unicode)); |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 5896 | return s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5897 | } |
| 5898 | |
| 5899 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ |
| 5900 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5901 | PyObject * |
| 5902 | PyUnicode_DecodeRawUnicodeEscape(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5903 | Py_ssize_t size, |
| 5904 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5905 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5906 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5907 | Py_ssize_t startinpos; |
| 5908 | Py_ssize_t endinpos; |
| 5909 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5910 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5911 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5912 | const char *end; |
| 5913 | const char *bs; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5914 | PyObject *errorHandler = NULL; |
| 5915 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5916 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5917 | /* Escaped strings will always be longer than the resulting |
| 5918 | 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] | 5919 | length after conversion to the true value. (But decoding error |
| 5920 | handler might have to resize the string) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5921 | v = _PyUnicode_New(size); |
| 5922 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5923 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5924 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5925 | return (PyObject *)v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5926 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5927 | end = s + size; |
| 5928 | while (s < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5929 | unsigned char c; |
| 5930 | Py_UCS4 x; |
| 5931 | int i; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5932 | int count; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5933 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5934 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 5935 | if (*s != '\\') { |
| 5936 | *p++ = (unsigned char)*s++; |
| 5937 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5938 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5939 | startinpos = s-starts; |
| 5940 | |
| 5941 | /* \u-escapes are only interpreted iff the number of leading |
| 5942 | backslashes if odd */ |
| 5943 | bs = s; |
| 5944 | for (;s < end;) { |
| 5945 | if (*s != '\\') |
| 5946 | break; |
| 5947 | *p++ = (unsigned char)*s++; |
| 5948 | } |
| 5949 | if (((s - bs) & 1) == 0 || |
| 5950 | s >= end || |
| 5951 | (*s != 'u' && *s != 'U')) { |
| 5952 | continue; |
| 5953 | } |
| 5954 | p--; |
| 5955 | count = *s=='u' ? 4 : 8; |
| 5956 | s++; |
| 5957 | |
| 5958 | /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ |
| 5959 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 5960 | for (x = 0, i = 0; i < count; ++i, ++s) { |
| 5961 | c = (unsigned char)*s; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 5962 | if (!Py_ISXDIGIT(c)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5963 | endinpos = s-starts; |
| 5964 | if (unicode_decode_call_errorhandler( |
| 5965 | errors, &errorHandler, |
| 5966 | "rawunicodeescape", "truncated \\uXXXX", |
| 5967 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 5968 | &v, &outpos, &p)) |
| 5969 | goto onError; |
| 5970 | goto nextByte; |
| 5971 | } |
| 5972 | x = (x<<4) & ~0xF; |
| 5973 | if (c >= '0' && c <= '9') |
| 5974 | x += c - '0'; |
| 5975 | else if (c >= 'a' && c <= 'f') |
| 5976 | x += 10 + c - 'a'; |
| 5977 | else |
| 5978 | x += 10 + c - 'A'; |
| 5979 | } |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 5980 | if (x <= 0xffff) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5981 | /* UCS-2 character */ |
| 5982 | *p++ = (Py_UNICODE) x; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 5983 | else if (x <= 0x10ffff) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5984 | /* UCS-4 character. Either store directly, or as |
| 5985 | surrogate pair. */ |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 5986 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5987 | *p++ = (Py_UNICODE) x; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 5988 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5989 | x -= 0x10000L; |
| 5990 | *p++ = 0xD800 + (Py_UNICODE) (x >> 10); |
| 5991 | *p++ = 0xDC00 + (Py_UNICODE) (x & 0x03FF); |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 5992 | #endif |
| 5993 | } else { |
| 5994 | endinpos = s-starts; |
| 5995 | outpos = p-PyUnicode_AS_UNICODE(v); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5996 | if (unicode_decode_call_errorhandler( |
| 5997 | errors, &errorHandler, |
| 5998 | "rawunicodeescape", "\\Uxxxxxxxx out of range", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5999 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 6000 | &v, &outpos, &p)) |
| 6001 | goto onError; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6002 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6003 | nextByte: |
| 6004 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6005 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 6006 | if (PyUnicode_Resize((PyObject**)&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6007 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6008 | Py_XDECREF(errorHandler); |
| 6009 | Py_XDECREF(exc); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 6010 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 6011 | if (_PyUnicode_READY_REPLACE(&v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6012 | Py_DECREF(v); |
| 6013 | return NULL; |
| 6014 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 6015 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 6016 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6017 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6018 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6019 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6020 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6021 | Py_XDECREF(errorHandler); |
| 6022 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6023 | return NULL; |
| 6024 | } |
| 6025 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6026 | PyObject * |
| 6027 | PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6028 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6029 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6030 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6031 | char *p; |
| 6032 | char *q; |
| 6033 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6034 | #ifdef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 6035 | const Py_ssize_t expandsize = 10; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6036 | #else |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 6037 | const Py_ssize_t expandsize = 6; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6038 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6039 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 6040 | if (size > PY_SSIZE_T_MAX / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6041 | return PyErr_NoMemory(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6042 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6043 | repr = PyBytes_FromStringAndSize(NULL, expandsize * size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6044 | if (repr == NULL) |
| 6045 | return NULL; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 6046 | if (size == 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6047 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6048 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6049 | p = q = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6050 | while (size-- > 0) { |
| 6051 | Py_UNICODE ch = *s++; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6052 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6053 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 6054 | if (ch >= 0x10000) { |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6055 | *p++ = '\\'; |
| 6056 | *p++ = 'U'; |
Walter Dörwald | db5d33e | 2007-05-12 11:13:47 +0000 | [diff] [blame] | 6057 | *p++ = hexdigits[(ch >> 28) & 0xf]; |
| 6058 | *p++ = hexdigits[(ch >> 24) & 0xf]; |
| 6059 | *p++ = hexdigits[(ch >> 20) & 0xf]; |
| 6060 | *p++ = hexdigits[(ch >> 16) & 0xf]; |
| 6061 | *p++ = hexdigits[(ch >> 12) & 0xf]; |
| 6062 | *p++ = hexdigits[(ch >> 8) & 0xf]; |
| 6063 | *p++ = hexdigits[(ch >> 4) & 0xf]; |
| 6064 | *p++ = hexdigits[ch & 15]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6065 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6066 | else |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 6067 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6068 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
| 6069 | if (ch >= 0xD800 && ch < 0xDC00) { |
| 6070 | Py_UNICODE ch2; |
| 6071 | Py_UCS4 ucs; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 6072 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6073 | ch2 = *s++; |
| 6074 | size--; |
Georg Brandl | 78eef3de | 2010-08-01 20:51:02 +0000 | [diff] [blame] | 6075 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6076 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 6077 | *p++ = '\\'; |
| 6078 | *p++ = 'U'; |
| 6079 | *p++ = hexdigits[(ucs >> 28) & 0xf]; |
| 6080 | *p++ = hexdigits[(ucs >> 24) & 0xf]; |
| 6081 | *p++ = hexdigits[(ucs >> 20) & 0xf]; |
| 6082 | *p++ = hexdigits[(ucs >> 16) & 0xf]; |
| 6083 | *p++ = hexdigits[(ucs >> 12) & 0xf]; |
| 6084 | *p++ = hexdigits[(ucs >> 8) & 0xf]; |
| 6085 | *p++ = hexdigits[(ucs >> 4) & 0xf]; |
| 6086 | *p++ = hexdigits[ucs & 0xf]; |
| 6087 | continue; |
| 6088 | } |
| 6089 | /* Fall through: isolated surrogates are copied as-is */ |
| 6090 | s--; |
| 6091 | size++; |
| 6092 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6093 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6094 | /* Map 16-bit characters to '\uxxxx' */ |
| 6095 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6096 | *p++ = '\\'; |
| 6097 | *p++ = 'u'; |
Walter Dörwald | db5d33e | 2007-05-12 11:13:47 +0000 | [diff] [blame] | 6098 | *p++ = hexdigits[(ch >> 12) & 0xf]; |
| 6099 | *p++ = hexdigits[(ch >> 8) & 0xf]; |
| 6100 | *p++ = hexdigits[(ch >> 4) & 0xf]; |
| 6101 | *p++ = hexdigits[ch & 15]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6102 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6103 | /* Copy everything else as-is */ |
| 6104 | else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6105 | *p++ = (char) ch; |
| 6106 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6107 | size = p - q; |
| 6108 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6109 | assert(size > 0); |
| 6110 | if (_PyBytes_Resize(&repr, size) < 0) |
| 6111 | return NULL; |
| 6112 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6113 | } |
| 6114 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6115 | PyObject * |
| 6116 | PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6117 | { |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 6118 | PyObject *s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6119 | if (!PyUnicode_Check(unicode)) { |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 6120 | PyErr_BadArgument(); |
| 6121 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6122 | } |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 6123 | s = PyUnicode_EncodeRawUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 6124 | PyUnicode_GET_SIZE(unicode)); |
| 6125 | |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 6126 | return s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6127 | } |
| 6128 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6129 | /* --- Unicode Internal Codec ------------------------------------------- */ |
| 6130 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6131 | PyObject * |
| 6132 | _PyUnicode_DecodeUnicodeInternal(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6133 | Py_ssize_t size, |
| 6134 | const char *errors) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6135 | { |
| 6136 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6137 | Py_ssize_t startinpos; |
| 6138 | Py_ssize_t endinpos; |
| 6139 | Py_ssize_t outpos; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6140 | PyUnicodeObject *v; |
| 6141 | Py_UNICODE *p; |
| 6142 | const char *end; |
| 6143 | const char *reason; |
| 6144 | PyObject *errorHandler = NULL; |
| 6145 | PyObject *exc = NULL; |
| 6146 | |
Neal Norwitz | d43069c | 2006-01-08 01:12:10 +0000 | [diff] [blame] | 6147 | #ifdef Py_UNICODE_WIDE |
| 6148 | Py_UNICODE unimax = PyUnicode_GetMax(); |
| 6149 | #endif |
| 6150 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6151 | /* XXX overflow detection missing */ |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6152 | v = _PyUnicode_New((size+Py_UNICODE_SIZE-1)/ Py_UNICODE_SIZE); |
| 6153 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6154 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6155 | /* Intentionally PyUnicode_GET_SIZE instead of PyUnicode_GET_LENGTH |
| 6156 | as string was created with the old API. */ |
| 6157 | if (PyUnicode_GET_SIZE(v) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6158 | return (PyObject *)v; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6159 | p = PyUnicode_AS_UNICODE(v); |
| 6160 | end = s + size; |
| 6161 | |
| 6162 | while (s < end) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6163 | memcpy(p, s, sizeof(Py_UNICODE)); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6164 | /* We have to sanity check the raw data, otherwise doom looms for |
| 6165 | some malformed UCS-4 data. */ |
| 6166 | if ( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6167 | #ifdef Py_UNICODE_WIDE |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6168 | *p > unimax || *p < 0 || |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6169 | #endif |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6170 | end-s < Py_UNICODE_SIZE |
| 6171 | ) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6172 | { |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6173 | startinpos = s - starts; |
| 6174 | if (end-s < Py_UNICODE_SIZE) { |
| 6175 | endinpos = end-starts; |
| 6176 | reason = "truncated input"; |
| 6177 | } |
| 6178 | else { |
| 6179 | endinpos = s - starts + Py_UNICODE_SIZE; |
| 6180 | reason = "illegal code point (> 0x10FFFF)"; |
| 6181 | } |
| 6182 | outpos = p - PyUnicode_AS_UNICODE(v); |
| 6183 | if (unicode_decode_call_errorhandler( |
| 6184 | errors, &errorHandler, |
| 6185 | "unicode_internal", reason, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 6186 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 6187 | &v, &outpos, &p)) { |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6188 | goto onError; |
| 6189 | } |
| 6190 | } |
| 6191 | else { |
| 6192 | p++; |
| 6193 | s += Py_UNICODE_SIZE; |
| 6194 | } |
| 6195 | } |
| 6196 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 6197 | if (PyUnicode_Resize((PyObject**)&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6198 | goto onError; |
| 6199 | Py_XDECREF(errorHandler); |
| 6200 | Py_XDECREF(exc); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 6201 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 6202 | if (_PyUnicode_READY_REPLACE(&v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6203 | Py_DECREF(v); |
| 6204 | return NULL; |
| 6205 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 6206 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 6207 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6208 | return (PyObject *)v; |
| 6209 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6210 | onError: |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6211 | Py_XDECREF(v); |
| 6212 | Py_XDECREF(errorHandler); |
| 6213 | Py_XDECREF(exc); |
| 6214 | return NULL; |
| 6215 | } |
| 6216 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6217 | /* --- Latin-1 Codec ------------------------------------------------------ */ |
| 6218 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6219 | PyObject * |
| 6220 | PyUnicode_DecodeLatin1(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6221 | Py_ssize_t size, |
| 6222 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6223 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6224 | /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 6225 | return _PyUnicode_FromUCS1((unsigned char*)s, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6226 | } |
| 6227 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6228 | /* create or adjust a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6229 | static void |
| 6230 | make_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6231 | const char *encoding, |
| 6232 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 6233 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6234 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6235 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6236 | if (*exceptionObject == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6237 | *exceptionObject = PyUnicodeEncodeError_Create( |
| 6238 | encoding, unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6239 | } |
| 6240 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6241 | if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 6242 | goto onError; |
| 6243 | if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) |
| 6244 | goto onError; |
| 6245 | if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) |
| 6246 | goto onError; |
| 6247 | return; |
| 6248 | onError: |
| 6249 | Py_DECREF(*exceptionObject); |
| 6250 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6251 | } |
| 6252 | } |
| 6253 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6254 | /* raises a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6255 | static void |
| 6256 | raise_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6257 | const char *encoding, |
| 6258 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 6259 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6260 | const char *reason) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6261 | { |
| 6262 | make_encode_exception(exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6263 | encoding, unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6264 | if (*exceptionObject != NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6265 | PyCodec_StrictErrors(*exceptionObject); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6266 | } |
| 6267 | |
| 6268 | /* error handling callback helper: |
| 6269 | build arguments, call the callback and check the arguments, |
| 6270 | put the result into newpos and return the replacement string, which |
| 6271 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6272 | static PyObject * |
| 6273 | unicode_encode_call_errorhandler(const char *errors, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6274 | PyObject **errorHandler, |
| 6275 | const char *encoding, const char *reason, |
| 6276 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 6277 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6278 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6279 | { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6280 | 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] | 6281 | |
| 6282 | PyObject *restuple; |
| 6283 | PyObject *resunicode; |
| 6284 | |
| 6285 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6286 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6287 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6288 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6289 | } |
| 6290 | |
| 6291 | make_encode_exception(exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6292 | encoding, unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6293 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6294 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6295 | |
| 6296 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6297 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6298 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6299 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6300 | if (!PyTuple_Check(restuple)) { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6301 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6302 | Py_DECREF(restuple); |
| 6303 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6304 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6305 | if (!PyArg_ParseTuple(restuple, argparse, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6306 | &resunicode, newpos)) { |
| 6307 | Py_DECREF(restuple); |
| 6308 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6309 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6310 | if (!PyUnicode_Check(resunicode) && !PyBytes_Check(resunicode)) { |
| 6311 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
| 6312 | Py_DECREF(restuple); |
| 6313 | return NULL; |
| 6314 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6315 | if (*newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6316 | *newpos = size+*newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 6317 | if (*newpos<0 || *newpos>size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6318 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 6319 | Py_DECREF(restuple); |
| 6320 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 6321 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6322 | Py_INCREF(resunicode); |
| 6323 | Py_DECREF(restuple); |
| 6324 | return resunicode; |
| 6325 | } |
| 6326 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6327 | static PyObject * |
| 6328 | unicode_encode_ucs1(const Py_UNICODE *p, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6329 | Py_ssize_t size, |
| 6330 | const char *errors, |
| 6331 | int limit) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6332 | { |
| 6333 | /* output object */ |
| 6334 | PyObject *res; |
| 6335 | /* pointers to the beginning and end+1 of input */ |
| 6336 | const Py_UNICODE *startp = p; |
| 6337 | const Py_UNICODE *endp = p + size; |
| 6338 | /* pointer to the beginning of the unencodable characters */ |
| 6339 | /* const Py_UNICODE *badp = NULL; */ |
| 6340 | /* pointer into the output */ |
| 6341 | char *str; |
| 6342 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6343 | Py_ssize_t ressize; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6344 | const char *encoding = (limit == 256) ? "latin-1" : "ascii"; |
| 6345 | 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] | 6346 | PyObject *errorHandler = NULL; |
| 6347 | PyObject *exc = NULL; |
| 6348 | /* the following variable is used for caching string comparisons |
| 6349 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 6350 | int known_errorHandler = -1; |
| 6351 | |
| 6352 | /* allocate enough for a simple encoding without |
| 6353 | replacements, if we need more, we'll resize */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6354 | if (size == 0) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 6355 | return PyBytes_FromStringAndSize(NULL, 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6356 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6357 | if (res == NULL) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6358 | return NULL; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6359 | str = PyBytes_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6360 | ressize = size; |
| 6361 | |
| 6362 | while (p<endp) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6363 | Py_UNICODE c = *p; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6364 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6365 | /* can we encode this? */ |
| 6366 | if (c<limit) { |
| 6367 | /* no overflow check, because we know that the space is enough */ |
| 6368 | *str++ = (char)c; |
| 6369 | ++p; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6370 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6371 | else { |
| 6372 | Py_ssize_t unicodepos = p-startp; |
| 6373 | Py_ssize_t requiredsize; |
| 6374 | PyObject *repunicode; |
| 6375 | Py_ssize_t repsize; |
| 6376 | Py_ssize_t newpos; |
| 6377 | Py_ssize_t respos; |
| 6378 | Py_UNICODE *uni2; |
| 6379 | /* startpos for collecting unencodable chars */ |
| 6380 | const Py_UNICODE *collstart = p; |
| 6381 | const Py_UNICODE *collend = p; |
| 6382 | /* find all unecodable characters */ |
| 6383 | while ((collend < endp) && ((*collend)>=limit)) |
| 6384 | ++collend; |
| 6385 | /* cache callback name lookup (if not done yet, i.e. it's the first error) */ |
| 6386 | if (known_errorHandler==-1) { |
| 6387 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 6388 | known_errorHandler = 1; |
| 6389 | else if (!strcmp(errors, "replace")) |
| 6390 | known_errorHandler = 2; |
| 6391 | else if (!strcmp(errors, "ignore")) |
| 6392 | known_errorHandler = 3; |
| 6393 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 6394 | known_errorHandler = 4; |
| 6395 | else |
| 6396 | known_errorHandler = 0; |
| 6397 | } |
| 6398 | switch (known_errorHandler) { |
| 6399 | case 1: /* strict */ |
| 6400 | raise_encode_exception(&exc, encoding, startp, size, collstart-startp, collend-startp, reason); |
| 6401 | goto onError; |
| 6402 | case 2: /* replace */ |
| 6403 | while (collstart++<collend) |
| 6404 | *str++ = '?'; /* fall through */ |
| 6405 | case 3: /* ignore */ |
| 6406 | p = collend; |
| 6407 | break; |
| 6408 | case 4: /* xmlcharrefreplace */ |
| 6409 | respos = str - PyBytes_AS_STRING(res); |
| 6410 | /* determine replacement size (temporarily (mis)uses p) */ |
| 6411 | for (p = collstart, repsize = 0; p < collend; ++p) { |
| 6412 | if (*p<10) |
| 6413 | repsize += 2+1+1; |
| 6414 | else if (*p<100) |
| 6415 | repsize += 2+2+1; |
| 6416 | else if (*p<1000) |
| 6417 | repsize += 2+3+1; |
| 6418 | else if (*p<10000) |
| 6419 | repsize += 2+4+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 6420 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6421 | else |
| 6422 | repsize += 2+5+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 6423 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6424 | else if (*p<100000) |
| 6425 | repsize += 2+5+1; |
| 6426 | else if (*p<1000000) |
| 6427 | repsize += 2+6+1; |
| 6428 | else |
| 6429 | repsize += 2+7+1; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 6430 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6431 | } |
| 6432 | requiredsize = respos+repsize+(endp-collend); |
| 6433 | if (requiredsize > ressize) { |
| 6434 | if (requiredsize<2*ressize) |
| 6435 | requiredsize = 2*ressize; |
| 6436 | if (_PyBytes_Resize(&res, requiredsize)) |
| 6437 | goto onError; |
| 6438 | str = PyBytes_AS_STRING(res) + respos; |
| 6439 | ressize = requiredsize; |
| 6440 | } |
| 6441 | /* generate replacement (temporarily (mis)uses p) */ |
| 6442 | for (p = collstart; p < collend; ++p) { |
| 6443 | str += sprintf(str, "&#%d;", (int)*p); |
| 6444 | } |
| 6445 | p = collend; |
| 6446 | break; |
| 6447 | default: |
| 6448 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 6449 | encoding, reason, startp, size, &exc, |
| 6450 | collstart-startp, collend-startp, &newpos); |
| 6451 | if (repunicode == NULL) |
| 6452 | goto onError; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6453 | if (PyBytes_Check(repunicode)) { |
| 6454 | /* Directly copy bytes result to output. */ |
| 6455 | repsize = PyBytes_Size(repunicode); |
| 6456 | if (repsize > 1) { |
| 6457 | /* Make room for all additional bytes. */ |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 6458 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6459 | if (_PyBytes_Resize(&res, ressize+repsize-1)) { |
| 6460 | Py_DECREF(repunicode); |
| 6461 | goto onError; |
| 6462 | } |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 6463 | str = PyBytes_AS_STRING(res) + respos; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6464 | ressize += repsize-1; |
| 6465 | } |
| 6466 | memcpy(str, PyBytes_AsString(repunicode), repsize); |
| 6467 | str += repsize; |
| 6468 | p = startp + newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6469 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6470 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6471 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6472 | /* need more space? (at least enough for what we |
| 6473 | have+the replacement+the rest of the string, so |
| 6474 | we won't have to check space for encodable characters) */ |
| 6475 | respos = str - PyBytes_AS_STRING(res); |
| 6476 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 6477 | requiredsize = respos+repsize+(endp-collend); |
| 6478 | if (requiredsize > ressize) { |
| 6479 | if (requiredsize<2*ressize) |
| 6480 | requiredsize = 2*ressize; |
| 6481 | if (_PyBytes_Resize(&res, requiredsize)) { |
| 6482 | Py_DECREF(repunicode); |
| 6483 | goto onError; |
| 6484 | } |
| 6485 | str = PyBytes_AS_STRING(res) + respos; |
| 6486 | ressize = requiredsize; |
| 6487 | } |
| 6488 | /* check if there is anything unencodable in the replacement |
| 6489 | and copy it to the output */ |
| 6490 | for (uni2 = PyUnicode_AS_UNICODE(repunicode);repsize-->0; ++uni2, ++str) { |
| 6491 | c = *uni2; |
| 6492 | if (c >= limit) { |
| 6493 | raise_encode_exception(&exc, encoding, startp, size, |
| 6494 | unicodepos, unicodepos+1, reason); |
| 6495 | Py_DECREF(repunicode); |
| 6496 | goto onError; |
| 6497 | } |
| 6498 | *str = (char)c; |
| 6499 | } |
| 6500 | p = startp + newpos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6501 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6502 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6503 | } |
| 6504 | } |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6505 | /* Resize if we allocated to much */ |
| 6506 | size = str - PyBytes_AS_STRING(res); |
| 6507 | if (size < ressize) { /* If this falls res will be NULL */ |
Alexandre Vassalotti | bad1b92 | 2008-12-27 09:49:09 +0000 | [diff] [blame] | 6508 | assert(size >= 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6509 | if (_PyBytes_Resize(&res, size) < 0) |
| 6510 | goto onError; |
| 6511 | } |
| 6512 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6513 | Py_XDECREF(errorHandler); |
| 6514 | Py_XDECREF(exc); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6515 | return res; |
| 6516 | |
| 6517 | onError: |
| 6518 | Py_XDECREF(res); |
| 6519 | Py_XDECREF(errorHandler); |
| 6520 | Py_XDECREF(exc); |
| 6521 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6522 | } |
| 6523 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6524 | PyObject * |
| 6525 | PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6526 | Py_ssize_t size, |
| 6527 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6528 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6529 | return unicode_encode_ucs1(p, size, errors, 256); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6530 | } |
| 6531 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6532 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6533 | _PyUnicode_AsLatin1String(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6534 | { |
| 6535 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6536 | PyErr_BadArgument(); |
| 6537 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6538 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6539 | if (PyUnicode_READY(unicode) == -1) |
| 6540 | return NULL; |
| 6541 | /* Fast path: if it is a one-byte string, construct |
| 6542 | bytes object directly. */ |
| 6543 | if (PyUnicode_KIND(unicode) == PyUnicode_1BYTE_KIND) |
| 6544 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 6545 | PyUnicode_GET_LENGTH(unicode)); |
| 6546 | /* Non-Latin-1 characters present. Defer to above function to |
| 6547 | raise the exception. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6548 | return PyUnicode_EncodeLatin1(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6549 | PyUnicode_GET_SIZE(unicode), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6550 | errors); |
| 6551 | } |
| 6552 | |
| 6553 | PyObject* |
| 6554 | PyUnicode_AsLatin1String(PyObject *unicode) |
| 6555 | { |
| 6556 | return _PyUnicode_AsLatin1String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6557 | } |
| 6558 | |
| 6559 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ |
| 6560 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6561 | PyObject * |
| 6562 | PyUnicode_DecodeASCII(const char *s, |
| 6563 | Py_ssize_t size, |
| 6564 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6565 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6566 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6567 | PyUnicodeObject *v; |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6568 | Py_UNICODE *u; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6569 | Py_ssize_t startinpos; |
| 6570 | Py_ssize_t endinpos; |
| 6571 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6572 | const char *e; |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6573 | int has_error; |
| 6574 | const unsigned char *p = (const unsigned char *)s; |
| 6575 | const unsigned char *end = p + size; |
| 6576 | 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] | 6577 | PyObject *errorHandler = NULL; |
| 6578 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6579 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6580 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6581 | if (size == 1 && (unsigned char)s[0] < 128) |
| 6582 | return get_latin1_char((unsigned char)s[0]); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6583 | |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6584 | has_error = 0; |
| 6585 | while (p < end && !has_error) { |
| 6586 | /* Fast path, see below in PyUnicode_DecodeUTF8Stateful for |
| 6587 | an explanation. */ |
| 6588 | if (!((size_t) p & LONG_PTR_MASK)) { |
| 6589 | /* Help register allocation */ |
| 6590 | register const unsigned char *_p = p; |
| 6591 | while (_p < aligned_end) { |
| 6592 | unsigned long value = *(unsigned long *) _p; |
| 6593 | if (value & ASCII_CHAR_MASK) { |
| 6594 | has_error = 1; |
| 6595 | break; |
| 6596 | } |
| 6597 | _p += SIZEOF_LONG; |
| 6598 | } |
| 6599 | if (_p == end) |
| 6600 | break; |
| 6601 | if (has_error) |
| 6602 | break; |
| 6603 | p = _p; |
| 6604 | } |
| 6605 | if (*p & 0x80) { |
| 6606 | has_error = 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6607 | break; |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6608 | } |
| 6609 | else { |
| 6610 | ++p; |
| 6611 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 6612 | } |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6613 | if (!has_error) |
| 6614 | return unicode_fromascii((const unsigned char *)s, size); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6615 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6616 | v = _PyUnicode_New(size); |
| 6617 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6618 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6619 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6620 | return (PyObject *)v; |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6621 | u = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6622 | e = s + size; |
| 6623 | while (s < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6624 | register unsigned char c = (unsigned char)*s; |
| 6625 | if (c < 128) { |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6626 | *u++ = c; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6627 | ++s; |
| 6628 | } |
| 6629 | else { |
| 6630 | startinpos = s-starts; |
| 6631 | endinpos = startinpos + 1; |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6632 | outpos = u - (Py_UNICODE *)PyUnicode_AS_UNICODE(v); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6633 | if (unicode_decode_call_errorhandler( |
| 6634 | errors, &errorHandler, |
| 6635 | "ascii", "ordinal not in range(128)", |
| 6636 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6637 | &v, &outpos, &u)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6638 | goto onError; |
| 6639 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6640 | } |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6641 | if (u - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
| 6642 | if (PyUnicode_Resize((PyObject**)&v, u - PyUnicode_AS_UNICODE(v)) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6643 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6644 | Py_XDECREF(errorHandler); |
| 6645 | Py_XDECREF(exc); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 6646 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 6647 | if (_PyUnicode_READY_REPLACE(&v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6648 | Py_DECREF(v); |
| 6649 | return NULL; |
| 6650 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 6651 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 6652 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6653 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6654 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6655 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6656 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6657 | Py_XDECREF(errorHandler); |
| 6658 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6659 | return NULL; |
| 6660 | } |
| 6661 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6662 | PyObject * |
| 6663 | PyUnicode_EncodeASCII(const Py_UNICODE *p, |
| 6664 | Py_ssize_t size, |
| 6665 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6666 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6667 | return unicode_encode_ucs1(p, size, errors, 128); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6668 | } |
| 6669 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6670 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6671 | _PyUnicode_AsASCIIString(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6672 | { |
| 6673 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6674 | PyErr_BadArgument(); |
| 6675 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6676 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6677 | if (PyUnicode_READY(unicode) == -1) |
| 6678 | return NULL; |
| 6679 | /* Fast path: if it is an ASCII-only string, construct bytes object |
| 6680 | directly. Else defer to above function to raise the exception. */ |
| 6681 | if (PyUnicode_MAX_CHAR_VALUE(unicode) < 128) |
| 6682 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 6683 | PyUnicode_GET_LENGTH(unicode)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6684 | return PyUnicode_EncodeASCII(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6685 | PyUnicode_GET_SIZE(unicode), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6686 | errors); |
| 6687 | } |
| 6688 | |
| 6689 | PyObject * |
| 6690 | PyUnicode_AsASCIIString(PyObject *unicode) |
| 6691 | { |
| 6692 | return _PyUnicode_AsASCIIString(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6693 | } |
| 6694 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 6695 | #ifdef HAVE_MBCS |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6696 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6697 | /* --- MBCS codecs for Windows -------------------------------------------- */ |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6698 | |
Hirokazu Yamamoto | 3530246 | 2009-03-21 13:23:27 +0000 | [diff] [blame] | 6699 | #if SIZEOF_INT < SIZEOF_SIZE_T |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6700 | #define NEED_RETRY |
| 6701 | #endif |
| 6702 | |
| 6703 | /* XXX This code is limited to "true" double-byte encodings, as |
| 6704 | a) it assumes an incomplete character consists of a single byte, and |
| 6705 | b) IsDBCSLeadByte (probably) does not work for non-DBCS multi-byte |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6706 | encodings, see IsDBCSLeadByteEx documentation. */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6707 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6708 | static int |
| 6709 | is_dbcs_lead_byte(const char *s, int offset) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6710 | { |
| 6711 | const char *curr = s + offset; |
| 6712 | |
| 6713 | if (IsDBCSLeadByte(*curr)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6714 | const char *prev = CharPrev(s, curr); |
| 6715 | return (prev == curr) || !IsDBCSLeadByte(*prev) || (curr - prev == 2); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6716 | } |
| 6717 | return 0; |
| 6718 | } |
| 6719 | |
| 6720 | /* |
| 6721 | * Decode MBCS string into unicode object. If 'final' is set, converts |
| 6722 | * trailing lead-byte too. Returns consumed size if succeed, -1 otherwise. |
| 6723 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6724 | static int |
| 6725 | decode_mbcs(PyUnicodeObject **v, |
| 6726 | const char *s, /* MBCS string */ |
| 6727 | int size, /* sizeof MBCS string */ |
| 6728 | int final, |
| 6729 | const char *errors) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6730 | { |
| 6731 | Py_UNICODE *p; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6732 | Py_ssize_t n; |
| 6733 | DWORD usize; |
| 6734 | DWORD flags; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6735 | |
| 6736 | assert(size >= 0); |
| 6737 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6738 | /* check and handle 'errors' arg */ |
| 6739 | if (errors==NULL || strcmp(errors, "strict")==0) |
| 6740 | flags = MB_ERR_INVALID_CHARS; |
| 6741 | else if (strcmp(errors, "ignore")==0) |
| 6742 | flags = 0; |
| 6743 | else { |
| 6744 | PyErr_Format(PyExc_ValueError, |
| 6745 | "mbcs encoding does not support errors='%s'", |
| 6746 | errors); |
| 6747 | return -1; |
| 6748 | } |
| 6749 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6750 | /* Skip trailing lead-byte unless 'final' is set */ |
| 6751 | if (!final && size >= 1 && is_dbcs_lead_byte(s, size - 1)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6752 | --size; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6753 | |
| 6754 | /* First get the size of the result */ |
| 6755 | if (size > 0) { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6756 | usize = MultiByteToWideChar(CP_ACP, flags, s, size, NULL, 0); |
| 6757 | if (usize==0) |
| 6758 | goto mbcs_decode_error; |
| 6759 | } else |
| 6760 | usize = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6761 | |
| 6762 | if (*v == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6763 | /* Create unicode object */ |
| 6764 | *v = _PyUnicode_New(usize); |
| 6765 | if (*v == NULL) |
| 6766 | return -1; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6767 | n = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6768 | } |
| 6769 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6770 | /* Extend unicode object */ |
| 6771 | n = PyUnicode_GET_SIZE(*v); |
Victor Stinner | 2fd8227 | 2011-10-03 04:06:05 +0200 | [diff] [blame] | 6772 | if (PyUnicode_Resize((PyObject**)v, n + usize) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6773 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6774 | } |
| 6775 | |
| 6776 | /* Do the conversion */ |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6777 | if (usize > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6778 | p = PyUnicode_AS_UNICODE(*v) + n; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6779 | if (0 == MultiByteToWideChar(CP_ACP, flags, s, size, p, usize)) { |
| 6780 | goto mbcs_decode_error; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6781 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6782 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6783 | return size; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6784 | |
| 6785 | mbcs_decode_error: |
| 6786 | /* If the last error was ERROR_NO_UNICODE_TRANSLATION, then |
| 6787 | we raise a UnicodeDecodeError - else it is a 'generic' |
| 6788 | windows error |
| 6789 | */ |
| 6790 | if (GetLastError()==ERROR_NO_UNICODE_TRANSLATION) { |
| 6791 | /* Ideally, we should get reason from FormatMessage - this |
| 6792 | is the Windows 2000 English version of the message |
| 6793 | */ |
| 6794 | PyObject *exc = NULL; |
| 6795 | const char *reason = "No mapping for the Unicode character exists " |
| 6796 | "in the target multi-byte code page."; |
| 6797 | make_decode_exception(&exc, "mbcs", s, size, 0, 0, reason); |
| 6798 | if (exc != NULL) { |
| 6799 | PyCodec_StrictErrors(exc); |
| 6800 | Py_DECREF(exc); |
| 6801 | } |
| 6802 | } else { |
| 6803 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 6804 | } |
| 6805 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6806 | } |
| 6807 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6808 | PyObject * |
| 6809 | PyUnicode_DecodeMBCSStateful(const char *s, |
| 6810 | Py_ssize_t size, |
| 6811 | const char *errors, |
| 6812 | Py_ssize_t *consumed) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6813 | { |
| 6814 | PyUnicodeObject *v = NULL; |
| 6815 | int done; |
| 6816 | |
| 6817 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6818 | *consumed = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6819 | |
| 6820 | #ifdef NEED_RETRY |
| 6821 | retry: |
| 6822 | if (size > INT_MAX) |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6823 | done = decode_mbcs(&v, s, INT_MAX, 0, errors); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6824 | else |
| 6825 | #endif |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6826 | done = decode_mbcs(&v, s, (int)size, !consumed, errors); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6827 | |
| 6828 | if (done < 0) { |
| 6829 | Py_XDECREF(v); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6830 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6831 | } |
| 6832 | |
| 6833 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6834 | *consumed += done; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6835 | |
| 6836 | #ifdef NEED_RETRY |
| 6837 | if (size > INT_MAX) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6838 | s += done; |
| 6839 | size -= done; |
| 6840 | goto retry; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6841 | } |
| 6842 | #endif |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 6843 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 6844 | if (_PyUnicode_READY_REPLACE(&v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6845 | Py_DECREF(v); |
| 6846 | return NULL; |
| 6847 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 6848 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 6849 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6850 | return (PyObject *)v; |
| 6851 | } |
| 6852 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6853 | PyObject * |
| 6854 | PyUnicode_DecodeMBCS(const char *s, |
| 6855 | Py_ssize_t size, |
| 6856 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6857 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6858 | return PyUnicode_DecodeMBCSStateful(s, size, errors, NULL); |
| 6859 | } |
| 6860 | |
| 6861 | /* |
| 6862 | * Convert unicode into string object (MBCS). |
| 6863 | * Returns 0 if succeed, -1 otherwise. |
| 6864 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6865 | static int |
| 6866 | encode_mbcs(PyObject **repr, |
| 6867 | const Py_UNICODE *p, /* unicode */ |
| 6868 | int size, /* size of unicode */ |
| 6869 | const char* errors) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6870 | { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6871 | BOOL usedDefaultChar = FALSE; |
| 6872 | BOOL *pusedDefaultChar; |
| 6873 | int mbcssize; |
| 6874 | Py_ssize_t n; |
| 6875 | PyObject *exc = NULL; |
| 6876 | DWORD flags; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6877 | |
| 6878 | assert(size >= 0); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6879 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6880 | /* check and handle 'errors' arg */ |
| 6881 | if (errors==NULL || strcmp(errors, "strict")==0) { |
| 6882 | flags = WC_NO_BEST_FIT_CHARS; |
| 6883 | pusedDefaultChar = &usedDefaultChar; |
| 6884 | } else if (strcmp(errors, "replace")==0) { |
| 6885 | flags = 0; |
| 6886 | pusedDefaultChar = NULL; |
| 6887 | } else { |
| 6888 | PyErr_Format(PyExc_ValueError, |
| 6889 | "mbcs encoding does not support errors='%s'", |
| 6890 | errors); |
| 6891 | return -1; |
| 6892 | } |
| 6893 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6894 | /* First get the size of the result */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6895 | if (size > 0) { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6896 | mbcssize = WideCharToMultiByte(CP_ACP, flags, p, size, NULL, 0, |
| 6897 | NULL, pusedDefaultChar); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6898 | if (mbcssize == 0) { |
| 6899 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 6900 | return -1; |
| 6901 | } |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6902 | /* If we used a default char, then we failed! */ |
| 6903 | if (pusedDefaultChar && *pusedDefaultChar) |
| 6904 | goto mbcs_encode_error; |
| 6905 | } else { |
| 6906 | mbcssize = 0; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6907 | } |
| 6908 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6909 | if (*repr == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6910 | /* Create string object */ |
| 6911 | *repr = PyBytes_FromStringAndSize(NULL, mbcssize); |
| 6912 | if (*repr == NULL) |
| 6913 | return -1; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6914 | n = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6915 | } |
| 6916 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6917 | /* Extend string object */ |
| 6918 | n = PyBytes_Size(*repr); |
| 6919 | if (_PyBytes_Resize(repr, n + mbcssize) < 0) |
| 6920 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6921 | } |
| 6922 | |
| 6923 | /* Do the conversion */ |
| 6924 | if (size > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6925 | char *s = PyBytes_AS_STRING(*repr) + n; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6926 | if (0 == WideCharToMultiByte(CP_ACP, flags, p, size, s, mbcssize, |
| 6927 | NULL, pusedDefaultChar)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6928 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 6929 | return -1; |
| 6930 | } |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6931 | if (pusedDefaultChar && *pusedDefaultChar) |
| 6932 | goto mbcs_encode_error; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6933 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6934 | return 0; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6935 | |
| 6936 | mbcs_encode_error: |
| 6937 | raise_encode_exception(&exc, "mbcs", p, size, 0, 0, "invalid character"); |
| 6938 | Py_XDECREF(exc); |
| 6939 | return -1; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6940 | } |
| 6941 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6942 | PyObject * |
| 6943 | PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
| 6944 | Py_ssize_t size, |
| 6945 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6946 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6947 | PyObject *repr = NULL; |
| 6948 | int ret; |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 6949 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6950 | #ifdef NEED_RETRY |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6951 | retry: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6952 | if (size > INT_MAX) |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6953 | ret = encode_mbcs(&repr, p, INT_MAX, errors); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6954 | else |
| 6955 | #endif |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6956 | ret = encode_mbcs(&repr, p, (int)size, errors); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6957 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6958 | if (ret < 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6959 | Py_XDECREF(repr); |
| 6960 | return NULL; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6961 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6962 | |
| 6963 | #ifdef NEED_RETRY |
| 6964 | if (size > INT_MAX) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6965 | p += INT_MAX; |
| 6966 | size -= INT_MAX; |
| 6967 | goto retry; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6968 | } |
| 6969 | #endif |
| 6970 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6971 | return repr; |
| 6972 | } |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6973 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6974 | PyObject * |
| 6975 | PyUnicode_AsMBCSString(PyObject *unicode) |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 6976 | { |
| 6977 | if (!PyUnicode_Check(unicode)) { |
| 6978 | PyErr_BadArgument(); |
| 6979 | return NULL; |
| 6980 | } |
| 6981 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6982 | PyUnicode_GET_SIZE(unicode), |
| 6983 | NULL); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 6984 | } |
| 6985 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6986 | #undef NEED_RETRY |
| 6987 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 6988 | #endif /* HAVE_MBCS */ |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6989 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6990 | /* --- Character Mapping Codec -------------------------------------------- */ |
| 6991 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6992 | PyObject * |
| 6993 | PyUnicode_DecodeCharmap(const char *s, |
| 6994 | Py_ssize_t size, |
| 6995 | PyObject *mapping, |
| 6996 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6997 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6998 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6999 | Py_ssize_t startinpos; |
| 7000 | Py_ssize_t endinpos; |
| 7001 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7002 | const char *e; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7003 | PyUnicodeObject *v; |
| 7004 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7005 | Py_ssize_t extrachars = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7006 | PyObject *errorHandler = NULL; |
| 7007 | PyObject *exc = NULL; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7008 | Py_UNICODE *mapstring = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7009 | Py_ssize_t maplen = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7010 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7011 | /* Default to Latin-1 */ |
| 7012 | if (mapping == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7013 | return PyUnicode_DecodeLatin1(s, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7014 | |
| 7015 | v = _PyUnicode_New(size); |
| 7016 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7017 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7018 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7019 | return (PyObject *)v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7020 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7021 | e = s + size; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7022 | if (PyUnicode_CheckExact(mapping)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7023 | mapstring = PyUnicode_AS_UNICODE(mapping); |
| 7024 | maplen = PyUnicode_GET_SIZE(mapping); |
| 7025 | while (s < e) { |
| 7026 | unsigned char ch = *s; |
| 7027 | Py_UNICODE x = 0xfffe; /* illegal value */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7028 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7029 | if (ch < maplen) |
| 7030 | x = mapstring[ch]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7031 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7032 | if (x == 0xfffe) { |
| 7033 | /* undefined mapping */ |
| 7034 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 7035 | startinpos = s-starts; |
| 7036 | endinpos = startinpos+1; |
| 7037 | if (unicode_decode_call_errorhandler( |
| 7038 | errors, &errorHandler, |
| 7039 | "charmap", "character maps to <undefined>", |
| 7040 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 7041 | &v, &outpos, &p)) { |
| 7042 | goto onError; |
| 7043 | } |
| 7044 | continue; |
| 7045 | } |
| 7046 | *p++ = x; |
| 7047 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7048 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7049 | } |
| 7050 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7051 | while (s < e) { |
| 7052 | unsigned char ch = *s; |
| 7053 | PyObject *w, *x; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7054 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7055 | /* Get mapping (char ordinal -> integer, Unicode char or None) */ |
| 7056 | w = PyLong_FromLong((long)ch); |
| 7057 | if (w == NULL) |
| 7058 | goto onError; |
| 7059 | x = PyObject_GetItem(mapping, w); |
| 7060 | Py_DECREF(w); |
| 7061 | if (x == NULL) { |
| 7062 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 7063 | /* No mapping found means: mapping is undefined. */ |
| 7064 | PyErr_Clear(); |
| 7065 | x = Py_None; |
| 7066 | Py_INCREF(x); |
| 7067 | } else |
| 7068 | goto onError; |
| 7069 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7070 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7071 | /* Apply mapping */ |
| 7072 | if (PyLong_Check(x)) { |
| 7073 | long value = PyLong_AS_LONG(x); |
| 7074 | if (value < 0 || value > 65535) { |
| 7075 | PyErr_SetString(PyExc_TypeError, |
| 7076 | "character mapping must be in range(65536)"); |
| 7077 | Py_DECREF(x); |
| 7078 | goto onError; |
| 7079 | } |
| 7080 | *p++ = (Py_UNICODE)value; |
| 7081 | } |
| 7082 | else if (x == Py_None) { |
| 7083 | /* undefined mapping */ |
| 7084 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 7085 | startinpos = s-starts; |
| 7086 | endinpos = startinpos+1; |
| 7087 | if (unicode_decode_call_errorhandler( |
| 7088 | errors, &errorHandler, |
| 7089 | "charmap", "character maps to <undefined>", |
| 7090 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 7091 | &v, &outpos, &p)) { |
| 7092 | Py_DECREF(x); |
| 7093 | goto onError; |
| 7094 | } |
| 7095 | Py_DECREF(x); |
| 7096 | continue; |
| 7097 | } |
| 7098 | else if (PyUnicode_Check(x)) { |
| 7099 | Py_ssize_t targetsize = PyUnicode_GET_SIZE(x); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7100 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7101 | if (targetsize == 1) |
| 7102 | /* 1-1 mapping */ |
| 7103 | *p++ = *PyUnicode_AS_UNICODE(x); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7104 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7105 | else if (targetsize > 1) { |
| 7106 | /* 1-n mapping */ |
| 7107 | if (targetsize > extrachars) { |
| 7108 | /* resize first */ |
| 7109 | Py_ssize_t oldpos = p - PyUnicode_AS_UNICODE(v); |
| 7110 | Py_ssize_t needed = (targetsize - extrachars) + \ |
| 7111 | (targetsize << 2); |
| 7112 | extrachars += needed; |
| 7113 | /* XXX overflow detection missing */ |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 7114 | if (PyUnicode_Resize((PyObject**)&v, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7115 | PyUnicode_GET_SIZE(v) + needed) < 0) { |
| 7116 | Py_DECREF(x); |
| 7117 | goto onError; |
| 7118 | } |
| 7119 | p = PyUnicode_AS_UNICODE(v) + oldpos; |
| 7120 | } |
| 7121 | Py_UNICODE_COPY(p, |
| 7122 | PyUnicode_AS_UNICODE(x), |
| 7123 | targetsize); |
| 7124 | p += targetsize; |
| 7125 | extrachars -= targetsize; |
| 7126 | } |
| 7127 | /* 1-0 mapping: skip the character */ |
| 7128 | } |
| 7129 | else { |
| 7130 | /* wrong return value */ |
| 7131 | PyErr_SetString(PyExc_TypeError, |
| 7132 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7133 | Py_DECREF(x); |
| 7134 | goto onError; |
| 7135 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7136 | Py_DECREF(x); |
| 7137 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7138 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7139 | } |
| 7140 | if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 7141 | if (PyUnicode_Resize((PyObject**)&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7142 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7143 | Py_XDECREF(errorHandler); |
| 7144 | Py_XDECREF(exc); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 7145 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 7146 | if (_PyUnicode_READY_REPLACE(&v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7147 | Py_DECREF(v); |
| 7148 | return NULL; |
| 7149 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 7150 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 7151 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7152 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7153 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7154 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7155 | Py_XDECREF(errorHandler); |
| 7156 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7157 | Py_XDECREF(v); |
| 7158 | return NULL; |
| 7159 | } |
| 7160 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7161 | /* Charmap encoding: the lookup table */ |
| 7162 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7163 | struct encoding_map { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7164 | PyObject_HEAD |
| 7165 | unsigned char level1[32]; |
| 7166 | int count2, count3; |
| 7167 | unsigned char level23[1]; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7168 | }; |
| 7169 | |
| 7170 | static PyObject* |
| 7171 | encoding_map_size(PyObject *obj, PyObject* args) |
| 7172 | { |
| 7173 | struct encoding_map *map = (struct encoding_map*)obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7174 | return PyLong_FromLong(sizeof(*map) - 1 + 16*map->count2 + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7175 | 128*map->count3); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7176 | } |
| 7177 | |
| 7178 | static PyMethodDef encoding_map_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7179 | {"size", encoding_map_size, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7180 | PyDoc_STR("Return the size (in bytes) of this object") }, |
| 7181 | { 0 } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7182 | }; |
| 7183 | |
| 7184 | static void |
| 7185 | encoding_map_dealloc(PyObject* o) |
| 7186 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7187 | PyObject_FREE(o); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7188 | } |
| 7189 | |
| 7190 | static PyTypeObject EncodingMapType = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7191 | PyVarObject_HEAD_INIT(NULL, 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7192 | "EncodingMap", /*tp_name*/ |
| 7193 | sizeof(struct encoding_map), /*tp_basicsize*/ |
| 7194 | 0, /*tp_itemsize*/ |
| 7195 | /* methods */ |
| 7196 | encoding_map_dealloc, /*tp_dealloc*/ |
| 7197 | 0, /*tp_print*/ |
| 7198 | 0, /*tp_getattr*/ |
| 7199 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 7200 | 0, /*tp_reserved*/ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7201 | 0, /*tp_repr*/ |
| 7202 | 0, /*tp_as_number*/ |
| 7203 | 0, /*tp_as_sequence*/ |
| 7204 | 0, /*tp_as_mapping*/ |
| 7205 | 0, /*tp_hash*/ |
| 7206 | 0, /*tp_call*/ |
| 7207 | 0, /*tp_str*/ |
| 7208 | 0, /*tp_getattro*/ |
| 7209 | 0, /*tp_setattro*/ |
| 7210 | 0, /*tp_as_buffer*/ |
| 7211 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 7212 | 0, /*tp_doc*/ |
| 7213 | 0, /*tp_traverse*/ |
| 7214 | 0, /*tp_clear*/ |
| 7215 | 0, /*tp_richcompare*/ |
| 7216 | 0, /*tp_weaklistoffset*/ |
| 7217 | 0, /*tp_iter*/ |
| 7218 | 0, /*tp_iternext*/ |
| 7219 | encoding_map_methods, /*tp_methods*/ |
| 7220 | 0, /*tp_members*/ |
| 7221 | 0, /*tp_getset*/ |
| 7222 | 0, /*tp_base*/ |
| 7223 | 0, /*tp_dict*/ |
| 7224 | 0, /*tp_descr_get*/ |
| 7225 | 0, /*tp_descr_set*/ |
| 7226 | 0, /*tp_dictoffset*/ |
| 7227 | 0, /*tp_init*/ |
| 7228 | 0, /*tp_alloc*/ |
| 7229 | 0, /*tp_new*/ |
| 7230 | 0, /*tp_free*/ |
| 7231 | 0, /*tp_is_gc*/ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7232 | }; |
| 7233 | |
| 7234 | PyObject* |
| 7235 | PyUnicode_BuildEncodingMap(PyObject* string) |
| 7236 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7237 | PyObject *result; |
| 7238 | struct encoding_map *mresult; |
| 7239 | int i; |
| 7240 | int need_dict = 0; |
| 7241 | unsigned char level1[32]; |
| 7242 | unsigned char level2[512]; |
| 7243 | unsigned char *mlevel1, *mlevel2, *mlevel3; |
| 7244 | int count2 = 0, count3 = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7245 | int kind; |
| 7246 | void *data; |
| 7247 | Py_UCS4 ch; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7248 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7249 | if (!PyUnicode_Check(string) || PyUnicode_GET_LENGTH(string) != 256) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7250 | PyErr_BadArgument(); |
| 7251 | return NULL; |
| 7252 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7253 | kind = PyUnicode_KIND(string); |
| 7254 | data = PyUnicode_DATA(string); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7255 | memset(level1, 0xFF, sizeof level1); |
| 7256 | memset(level2, 0xFF, sizeof level2); |
| 7257 | |
| 7258 | /* If there isn't a one-to-one mapping of NULL to \0, |
| 7259 | or if there are non-BMP characters, we need to use |
| 7260 | a mapping dictionary. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7261 | if (PyUnicode_READ(kind, data, 0) != 0) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7262 | need_dict = 1; |
| 7263 | for (i = 1; i < 256; i++) { |
| 7264 | int l1, l2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7265 | ch = PyUnicode_READ(kind, data, i); |
| 7266 | if (ch == 0 || ch > 0xFFFF) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7267 | need_dict = 1; |
| 7268 | break; |
| 7269 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7270 | if (ch == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7271 | /* unmapped character */ |
| 7272 | continue; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7273 | l1 = ch >> 11; |
| 7274 | l2 = ch >> 7; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7275 | if (level1[l1] == 0xFF) |
| 7276 | level1[l1] = count2++; |
| 7277 | if (level2[l2] == 0xFF) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7278 | level2[l2] = count3++; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7279 | } |
| 7280 | |
| 7281 | if (count2 >= 0xFF || count3 >= 0xFF) |
| 7282 | need_dict = 1; |
| 7283 | |
| 7284 | if (need_dict) { |
| 7285 | PyObject *result = PyDict_New(); |
| 7286 | PyObject *key, *value; |
| 7287 | if (!result) |
| 7288 | return NULL; |
| 7289 | for (i = 0; i < 256; i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7290 | key = PyLong_FromLong(PyUnicode_READ(kind, data, i)); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7291 | value = PyLong_FromLong(i); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7292 | if (!key || !value) |
| 7293 | goto failed1; |
| 7294 | if (PyDict_SetItem(result, key, value) == -1) |
| 7295 | goto failed1; |
| 7296 | Py_DECREF(key); |
| 7297 | Py_DECREF(value); |
| 7298 | } |
| 7299 | return result; |
| 7300 | failed1: |
| 7301 | Py_XDECREF(key); |
| 7302 | Py_XDECREF(value); |
| 7303 | Py_DECREF(result); |
| 7304 | return NULL; |
| 7305 | } |
| 7306 | |
| 7307 | /* Create a three-level trie */ |
| 7308 | result = PyObject_MALLOC(sizeof(struct encoding_map) + |
| 7309 | 16*count2 + 128*count3 - 1); |
| 7310 | if (!result) |
| 7311 | return PyErr_NoMemory(); |
| 7312 | PyObject_Init(result, &EncodingMapType); |
| 7313 | mresult = (struct encoding_map*)result; |
| 7314 | mresult->count2 = count2; |
| 7315 | mresult->count3 = count3; |
| 7316 | mlevel1 = mresult->level1; |
| 7317 | mlevel2 = mresult->level23; |
| 7318 | mlevel3 = mresult->level23 + 16*count2; |
| 7319 | memcpy(mlevel1, level1, 32); |
| 7320 | memset(mlevel2, 0xFF, 16*count2); |
| 7321 | memset(mlevel3, 0, 128*count3); |
| 7322 | count3 = 0; |
| 7323 | for (i = 1; i < 256; i++) { |
| 7324 | int o1, o2, o3, i2, i3; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7325 | if (PyUnicode_READ(kind, data, i) == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7326 | /* unmapped character */ |
| 7327 | continue; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7328 | o1 = PyUnicode_READ(kind, data, i)>>11; |
| 7329 | o2 = (PyUnicode_READ(kind, data, i)>>7) & 0xF; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7330 | i2 = 16*mlevel1[o1] + o2; |
| 7331 | if (mlevel2[i2] == 0xFF) |
| 7332 | mlevel2[i2] = count3++; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7333 | o3 = PyUnicode_READ(kind, data, i) & 0x7F; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7334 | i3 = 128*mlevel2[i2] + o3; |
| 7335 | mlevel3[i3] = i; |
| 7336 | } |
| 7337 | return result; |
| 7338 | } |
| 7339 | |
| 7340 | static int |
| 7341 | encoding_map_lookup(Py_UNICODE c, PyObject *mapping) |
| 7342 | { |
| 7343 | struct encoding_map *map = (struct encoding_map*)mapping; |
| 7344 | int l1 = c>>11; |
| 7345 | int l2 = (c>>7) & 0xF; |
| 7346 | int l3 = c & 0x7F; |
| 7347 | int i; |
| 7348 | |
| 7349 | #ifdef Py_UNICODE_WIDE |
| 7350 | if (c > 0xFFFF) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7351 | return -1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7352 | } |
| 7353 | #endif |
| 7354 | if (c == 0) |
| 7355 | return 0; |
| 7356 | /* level 1*/ |
| 7357 | i = map->level1[l1]; |
| 7358 | if (i == 0xFF) { |
| 7359 | return -1; |
| 7360 | } |
| 7361 | /* level 2*/ |
| 7362 | i = map->level23[16*i+l2]; |
| 7363 | if (i == 0xFF) { |
| 7364 | return -1; |
| 7365 | } |
| 7366 | /* level 3 */ |
| 7367 | i = map->level23[16*map->count2 + 128*i + l3]; |
| 7368 | if (i == 0) { |
| 7369 | return -1; |
| 7370 | } |
| 7371 | return i; |
| 7372 | } |
| 7373 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7374 | /* Lookup the character ch in the mapping. If the character |
| 7375 | can't be found, Py_None is returned (or NULL, if another |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 7376 | error occurred). */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7377 | static PyObject * |
| 7378 | charmapencode_lookup(Py_UNICODE c, PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7379 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7380 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7381 | PyObject *x; |
| 7382 | |
| 7383 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7384 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7385 | x = PyObject_GetItem(mapping, w); |
| 7386 | Py_DECREF(w); |
| 7387 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7388 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 7389 | /* No mapping found means: mapping is undefined. */ |
| 7390 | PyErr_Clear(); |
| 7391 | x = Py_None; |
| 7392 | Py_INCREF(x); |
| 7393 | return x; |
| 7394 | } else |
| 7395 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7396 | } |
Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 7397 | else if (x == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7398 | return x; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7399 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7400 | long value = PyLong_AS_LONG(x); |
| 7401 | if (value < 0 || value > 255) { |
| 7402 | PyErr_SetString(PyExc_TypeError, |
| 7403 | "character mapping must be in range(256)"); |
| 7404 | Py_DECREF(x); |
| 7405 | return NULL; |
| 7406 | } |
| 7407 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7408 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7409 | else if (PyBytes_Check(x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7410 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7411 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7412 | /* wrong return value */ |
| 7413 | PyErr_Format(PyExc_TypeError, |
| 7414 | "character mapping must return integer, bytes or None, not %.400s", |
| 7415 | x->ob_type->tp_name); |
| 7416 | Py_DECREF(x); |
| 7417 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7418 | } |
| 7419 | } |
| 7420 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7421 | static int |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 7422 | charmapencode_resize(PyObject **outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7423 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7424 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
| 7425 | /* exponentially overallocate to minimize reallocations */ |
| 7426 | if (requiredsize < 2*outsize) |
| 7427 | requiredsize = 2*outsize; |
| 7428 | if (_PyBytes_Resize(outobj, requiredsize)) |
| 7429 | return -1; |
| 7430 | return 0; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7431 | } |
| 7432 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7433 | typedef enum charmapencode_result { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7434 | enc_SUCCESS, enc_FAILED, enc_EXCEPTION |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7435 | } charmapencode_result; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7436 | /* 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] | 7437 | various state variables. Resize the output bytes object if not enough |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7438 | space is available. Return a new reference to the object that |
| 7439 | was put in the output buffer, or Py_None, if the mapping was undefined |
| 7440 | (in which case no character was written) or NULL, if a |
Andrew M. Kuchling | 8294de5 | 2005-11-02 16:36:12 +0000 | [diff] [blame] | 7441 | reallocation error occurred. The caller must decref the result */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7442 | static charmapencode_result |
| 7443 | charmapencode_output(Py_UNICODE c, PyObject *mapping, |
| 7444 | PyObject **outobj, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7445 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7446 | PyObject *rep; |
| 7447 | char *outstart; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7448 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7449 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 7450 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7451 | int res = encoding_map_lookup(c, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7452 | Py_ssize_t requiredsize = *outpos+1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7453 | if (res == -1) |
| 7454 | return enc_FAILED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7455 | if (outsize<requiredsize) |
| 7456 | if (charmapencode_resize(outobj, outpos, requiredsize)) |
| 7457 | return enc_EXCEPTION; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7458 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7459 | outstart[(*outpos)++] = (char)res; |
| 7460 | return enc_SUCCESS; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7461 | } |
| 7462 | |
| 7463 | rep = charmapencode_lookup(c, mapping); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7464 | if (rep==NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7465 | return enc_EXCEPTION; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7466 | else if (rep==Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7467 | Py_DECREF(rep); |
| 7468 | return enc_FAILED; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7469 | } else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7470 | if (PyLong_Check(rep)) { |
| 7471 | Py_ssize_t requiredsize = *outpos+1; |
| 7472 | if (outsize<requiredsize) |
| 7473 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 7474 | Py_DECREF(rep); |
| 7475 | return enc_EXCEPTION; |
| 7476 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7477 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7478 | outstart[(*outpos)++] = (char)PyLong_AS_LONG(rep); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7479 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7480 | else { |
| 7481 | const char *repchars = PyBytes_AS_STRING(rep); |
| 7482 | Py_ssize_t repsize = PyBytes_GET_SIZE(rep); |
| 7483 | Py_ssize_t requiredsize = *outpos+repsize; |
| 7484 | if (outsize<requiredsize) |
| 7485 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 7486 | Py_DECREF(rep); |
| 7487 | return enc_EXCEPTION; |
| 7488 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7489 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7490 | memcpy(outstart + *outpos, repchars, repsize); |
| 7491 | *outpos += repsize; |
| 7492 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7493 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7494 | Py_DECREF(rep); |
| 7495 | return enc_SUCCESS; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7496 | } |
| 7497 | |
| 7498 | /* handle an error in PyUnicode_EncodeCharmap |
| 7499 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7500 | static int |
| 7501 | charmap_encoding_error( |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7502 | 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] | 7503 | PyObject **exceptionObject, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 7504 | int *known_errorHandler, PyObject **errorHandler, const char *errors, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 7505 | PyObject **res, Py_ssize_t *respos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7506 | { |
| 7507 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7508 | Py_ssize_t repsize; |
| 7509 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7510 | Py_UNICODE *uni2; |
| 7511 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7512 | Py_ssize_t collstartpos = *inpos; |
| 7513 | Py_ssize_t collendpos = *inpos+1; |
| 7514 | Py_ssize_t collpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7515 | char *encoding = "charmap"; |
| 7516 | char *reason = "character maps to <undefined>"; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7517 | charmapencode_result x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7518 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7519 | /* find all unencodable characters */ |
| 7520 | while (collendpos < size) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7521 | PyObject *rep; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 7522 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7523 | int res = encoding_map_lookup(p[collendpos], mapping); |
| 7524 | if (res != -1) |
| 7525 | break; |
| 7526 | ++collendpos; |
| 7527 | continue; |
| 7528 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7529 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7530 | rep = charmapencode_lookup(p[collendpos], mapping); |
| 7531 | if (rep==NULL) |
| 7532 | return -1; |
| 7533 | else if (rep!=Py_None) { |
| 7534 | Py_DECREF(rep); |
| 7535 | break; |
| 7536 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7537 | Py_DECREF(rep); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7538 | ++collendpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7539 | } |
| 7540 | /* cache callback name lookup |
| 7541 | * (if not done yet, i.e. it's the first error) */ |
| 7542 | if (*known_errorHandler==-1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7543 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 7544 | *known_errorHandler = 1; |
| 7545 | else if (!strcmp(errors, "replace")) |
| 7546 | *known_errorHandler = 2; |
| 7547 | else if (!strcmp(errors, "ignore")) |
| 7548 | *known_errorHandler = 3; |
| 7549 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 7550 | *known_errorHandler = 4; |
| 7551 | else |
| 7552 | *known_errorHandler = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7553 | } |
| 7554 | switch (*known_errorHandler) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7555 | case 1: /* strict */ |
| 7556 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 7557 | return -1; |
| 7558 | case 2: /* replace */ |
| 7559 | for (collpos = collstartpos; collpos<collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7560 | x = charmapencode_output('?', mapping, res, respos); |
| 7561 | if (x==enc_EXCEPTION) { |
| 7562 | return -1; |
| 7563 | } |
| 7564 | else if (x==enc_FAILED) { |
| 7565 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 7566 | return -1; |
| 7567 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7568 | } |
| 7569 | /* fall through */ |
| 7570 | case 3: /* ignore */ |
| 7571 | *inpos = collendpos; |
| 7572 | break; |
| 7573 | case 4: /* xmlcharrefreplace */ |
| 7574 | /* generate replacement (temporarily (mis)uses p) */ |
| 7575 | for (collpos = collstartpos; collpos < collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7576 | char buffer[2+29+1+1]; |
| 7577 | char *cp; |
| 7578 | sprintf(buffer, "&#%d;", (int)p[collpos]); |
| 7579 | for (cp = buffer; *cp; ++cp) { |
| 7580 | x = charmapencode_output(*cp, mapping, res, respos); |
| 7581 | if (x==enc_EXCEPTION) |
| 7582 | return -1; |
| 7583 | else if (x==enc_FAILED) { |
| 7584 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 7585 | return -1; |
| 7586 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7587 | } |
| 7588 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7589 | *inpos = collendpos; |
| 7590 | break; |
| 7591 | default: |
| 7592 | repunicode = unicode_encode_call_errorhandler(errors, errorHandler, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7593 | encoding, reason, p, size, exceptionObject, |
| 7594 | collstartpos, collendpos, &newpos); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7595 | if (repunicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7596 | return -1; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 7597 | if (PyBytes_Check(repunicode)) { |
| 7598 | /* Directly copy bytes result to output. */ |
| 7599 | Py_ssize_t outsize = PyBytes_Size(*res); |
| 7600 | Py_ssize_t requiredsize; |
| 7601 | repsize = PyBytes_Size(repunicode); |
| 7602 | requiredsize = *respos + repsize; |
| 7603 | if (requiredsize > outsize) |
| 7604 | /* Make room for all additional bytes. */ |
| 7605 | if (charmapencode_resize(res, respos, requiredsize)) { |
| 7606 | Py_DECREF(repunicode); |
| 7607 | return -1; |
| 7608 | } |
| 7609 | memcpy(PyBytes_AsString(*res) + *respos, |
| 7610 | PyBytes_AsString(repunicode), repsize); |
| 7611 | *respos += repsize; |
| 7612 | *inpos = newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 7613 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 7614 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 7615 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7616 | /* generate replacement */ |
| 7617 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 7618 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7619 | x = charmapencode_output(*uni2, mapping, res, respos); |
| 7620 | if (x==enc_EXCEPTION) { |
| 7621 | return -1; |
| 7622 | } |
| 7623 | else if (x==enc_FAILED) { |
| 7624 | Py_DECREF(repunicode); |
| 7625 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 7626 | return -1; |
| 7627 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7628 | } |
| 7629 | *inpos = newpos; |
| 7630 | Py_DECREF(repunicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7631 | } |
| 7632 | return 0; |
| 7633 | } |
| 7634 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7635 | PyObject * |
| 7636 | PyUnicode_EncodeCharmap(const Py_UNICODE *p, |
| 7637 | Py_ssize_t size, |
| 7638 | PyObject *mapping, |
| 7639 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7640 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7641 | /* output object */ |
| 7642 | PyObject *res = NULL; |
| 7643 | /* current input position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7644 | Py_ssize_t inpos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7645 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7646 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7647 | PyObject *errorHandler = NULL; |
| 7648 | PyObject *exc = NULL; |
| 7649 | /* the following variable is used for caching string comparisons |
| 7650 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 7651 | * 3=ignore, 4=xmlcharrefreplace */ |
| 7652 | int known_errorHandler = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7653 | |
| 7654 | /* Default to Latin-1 */ |
| 7655 | if (mapping == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7656 | return PyUnicode_EncodeLatin1(p, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7657 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7658 | /* allocate enough for a simple encoding without |
| 7659 | replacements, if we need more, we'll resize */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7660 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7661 | if (res == NULL) |
| 7662 | goto onError; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 7663 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7664 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7665 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7666 | while (inpos<size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7667 | /* try to encode it */ |
| 7668 | charmapencode_result x = charmapencode_output(p[inpos], mapping, &res, &respos); |
| 7669 | if (x==enc_EXCEPTION) /* error */ |
| 7670 | goto onError; |
| 7671 | if (x==enc_FAILED) { /* unencodable character */ |
| 7672 | if (charmap_encoding_error(p, size, &inpos, mapping, |
| 7673 | &exc, |
| 7674 | &known_errorHandler, &errorHandler, errors, |
| 7675 | &res, &respos)) { |
| 7676 | goto onError; |
| 7677 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7678 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7679 | else |
| 7680 | /* done with this character => adjust input position */ |
| 7681 | ++inpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7682 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7683 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7684 | /* Resize if we allocated to much */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7685 | if (respos<PyBytes_GET_SIZE(res)) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 7686 | if (_PyBytes_Resize(&res, respos) < 0) |
| 7687 | goto onError; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 7688 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7689 | Py_XDECREF(exc); |
| 7690 | Py_XDECREF(errorHandler); |
| 7691 | return res; |
| 7692 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7693 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7694 | Py_XDECREF(res); |
| 7695 | Py_XDECREF(exc); |
| 7696 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7697 | return NULL; |
| 7698 | } |
| 7699 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7700 | PyObject * |
| 7701 | PyUnicode_AsCharmapString(PyObject *unicode, |
| 7702 | PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7703 | { |
| 7704 | if (!PyUnicode_Check(unicode) || mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7705 | PyErr_BadArgument(); |
| 7706 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7707 | } |
| 7708 | return PyUnicode_EncodeCharmap(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7709 | PyUnicode_GET_SIZE(unicode), |
| 7710 | mapping, |
| 7711 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7712 | } |
| 7713 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7714 | /* create or adjust a UnicodeTranslateError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7715 | static void |
| 7716 | make_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7717 | PyObject *unicode, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7718 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 7719 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7720 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7721 | if (*exceptionObject == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7722 | *exceptionObject = _PyUnicodeTranslateError_Create( |
| 7723 | unicode, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7724 | } |
| 7725 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7726 | if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
| 7727 | goto onError; |
| 7728 | if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) |
| 7729 | goto onError; |
| 7730 | if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) |
| 7731 | goto onError; |
| 7732 | return; |
| 7733 | onError: |
| 7734 | Py_DECREF(*exceptionObject); |
| 7735 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7736 | } |
| 7737 | } |
| 7738 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7739 | /* raises a UnicodeTranslateError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7740 | static void |
| 7741 | raise_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7742 | PyObject *unicode, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7743 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 7744 | const char *reason) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7745 | { |
| 7746 | make_translate_exception(exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7747 | unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7748 | if (*exceptionObject != NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7749 | PyCodec_StrictErrors(*exceptionObject); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7750 | } |
| 7751 | |
| 7752 | /* error handling callback helper: |
| 7753 | build arguments, call the callback and check the arguments, |
| 7754 | put the result into newpos and return the replacement string, which |
| 7755 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7756 | static PyObject * |
| 7757 | unicode_translate_call_errorhandler(const char *errors, |
| 7758 | PyObject **errorHandler, |
| 7759 | const char *reason, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7760 | PyObject *unicode, PyObject **exceptionObject, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7761 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 7762 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7763 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 7764 | 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] | 7765 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7766 | Py_ssize_t i_newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7767 | PyObject *restuple; |
| 7768 | PyObject *resunicode; |
| 7769 | |
| 7770 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7771 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7772 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7773 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7774 | } |
| 7775 | |
| 7776 | make_translate_exception(exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7777 | unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7778 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7779 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7780 | |
| 7781 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7782 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7783 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7784 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7785 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 7786 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7787 | Py_DECREF(restuple); |
| 7788 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7789 | } |
| 7790 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7791 | &resunicode, &i_newpos)) { |
| 7792 | Py_DECREF(restuple); |
| 7793 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7794 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7795 | if (i_newpos<0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7796 | *newpos = PyUnicode_GET_LENGTH(unicode)+i_newpos; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7797 | else |
| 7798 | *newpos = i_newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7799 | if (*newpos<0 || *newpos>PyUnicode_GET_LENGTH(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7800 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 7801 | Py_DECREF(restuple); |
| 7802 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 7803 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7804 | Py_INCREF(resunicode); |
| 7805 | Py_DECREF(restuple); |
| 7806 | return resunicode; |
| 7807 | } |
| 7808 | |
| 7809 | /* Lookup the character ch in the mapping and put the result in result, |
| 7810 | which must be decrefed by the caller. |
| 7811 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7812 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7813 | charmaptranslate_lookup(Py_UCS4 c, PyObject *mapping, PyObject **result) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7814 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7815 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7816 | PyObject *x; |
| 7817 | |
| 7818 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7819 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7820 | x = PyObject_GetItem(mapping, w); |
| 7821 | Py_DECREF(w); |
| 7822 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7823 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 7824 | /* No mapping found means: use 1:1 mapping. */ |
| 7825 | PyErr_Clear(); |
| 7826 | *result = NULL; |
| 7827 | return 0; |
| 7828 | } else |
| 7829 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7830 | } |
| 7831 | else if (x == Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7832 | *result = x; |
| 7833 | return 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7834 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7835 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7836 | long value = PyLong_AS_LONG(x); |
| 7837 | long max = PyUnicode_GetMax(); |
| 7838 | if (value < 0 || value > max) { |
| 7839 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5a2f7e60 | 2007-10-24 21:13:09 +0000 | [diff] [blame] | 7840 | "character mapping must be in range(0x%x)", max+1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7841 | Py_DECREF(x); |
| 7842 | return -1; |
| 7843 | } |
| 7844 | *result = x; |
| 7845 | return 0; |
| 7846 | } |
| 7847 | else if (PyUnicode_Check(x)) { |
| 7848 | *result = x; |
| 7849 | return 0; |
| 7850 | } |
| 7851 | else { |
| 7852 | /* wrong return value */ |
| 7853 | PyErr_SetString(PyExc_TypeError, |
| 7854 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7855 | Py_DECREF(x); |
| 7856 | return -1; |
| 7857 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7858 | } |
| 7859 | /* ensure that *outobj is at least requiredsize characters long, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7860 | if not reallocate and adjust various state variables. |
| 7861 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7862 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7863 | charmaptranslate_makespace(Py_UCS4 **outobj, Py_ssize_t *psize, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7864 | Py_ssize_t requiredsize) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7865 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7866 | Py_ssize_t oldsize = *psize; |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 7867 | if (requiredsize > oldsize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7868 | /* exponentially overallocate to minimize reallocations */ |
| 7869 | if (requiredsize < 2 * oldsize) |
| 7870 | requiredsize = 2 * oldsize; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7871 | *outobj = PyMem_Realloc(*outobj, requiredsize * sizeof(Py_UCS4)); |
| 7872 | if (*outobj == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7873 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7874 | *psize = requiredsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7875 | } |
| 7876 | return 0; |
| 7877 | } |
| 7878 | /* lookup the character, put the result in the output string and adjust |
| 7879 | various state variables. Return a new reference to the object that |
| 7880 | was put in the output buffer in *result, or Py_None, if the mapping was |
| 7881 | undefined (in which case no character was written). |
| 7882 | The called must decref result. |
| 7883 | Return 0 on success, -1 on error. */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7884 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7885 | charmaptranslate_output(PyObject *input, Py_ssize_t ipos, |
| 7886 | PyObject *mapping, Py_UCS4 **output, |
| 7887 | Py_ssize_t *osize, Py_ssize_t *opos, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7888 | PyObject **res) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7889 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7890 | Py_UCS4 curinp = PyUnicode_READ_CHAR(input, ipos); |
| 7891 | if (charmaptranslate_lookup(curinp, mapping, res)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7892 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7893 | if (*res==NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7894 | /* not found => default to 1:1 mapping */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7895 | (*output)[(*opos)++] = curinp; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7896 | } |
| 7897 | else if (*res==Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7898 | ; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7899 | else if (PyLong_Check(*res)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7900 | /* 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] | 7901 | (*output)[(*opos)++] = (Py_UCS4)PyLong_AS_LONG(*res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7902 | } |
| 7903 | else if (PyUnicode_Check(*res)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7904 | Py_ssize_t repsize; |
| 7905 | if (PyUnicode_READY(*res) == -1) |
| 7906 | return -1; |
| 7907 | repsize = PyUnicode_GET_LENGTH(*res); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7908 | if (repsize==1) { |
| 7909 | /* 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] | 7910 | (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7911 | } |
| 7912 | else if (repsize!=0) { |
| 7913 | /* more than one character */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7914 | Py_ssize_t requiredsize = *opos + |
| 7915 | (PyUnicode_GET_LENGTH(input) - ipos) + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7916 | repsize - 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7917 | Py_ssize_t i; |
| 7918 | if (charmaptranslate_makespace(output, osize, requiredsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7919 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7920 | for(i = 0; i < repsize; i++) |
| 7921 | (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, i); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7922 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7923 | } |
| 7924 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7925 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7926 | return 0; |
| 7927 | } |
| 7928 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7929 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7930 | _PyUnicode_TranslateCharmap(PyObject *input, |
| 7931 | PyObject *mapping, |
| 7932 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7933 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7934 | /* input object */ |
| 7935 | char *idata; |
| 7936 | Py_ssize_t size, i; |
| 7937 | int kind; |
| 7938 | /* output buffer */ |
| 7939 | Py_UCS4 *output = NULL; |
| 7940 | Py_ssize_t osize; |
| 7941 | PyObject *res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7942 | /* current output position */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7943 | Py_ssize_t opos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7944 | char *reason = "character maps to <undefined>"; |
| 7945 | PyObject *errorHandler = NULL; |
| 7946 | PyObject *exc = NULL; |
| 7947 | /* the following variable is used for caching string comparisons |
| 7948 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 7949 | * 3=ignore, 4=xmlcharrefreplace */ |
| 7950 | int known_errorHandler = -1; |
| 7951 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7952 | if (mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7953 | PyErr_BadArgument(); |
| 7954 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7955 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7956 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7957 | if (PyUnicode_READY(input) == -1) |
| 7958 | return NULL; |
| 7959 | idata = (char*)PyUnicode_DATA(input); |
| 7960 | kind = PyUnicode_KIND(input); |
| 7961 | size = PyUnicode_GET_LENGTH(input); |
| 7962 | i = 0; |
| 7963 | |
| 7964 | if (size == 0) { |
| 7965 | Py_INCREF(input); |
| 7966 | return input; |
| 7967 | } |
| 7968 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7969 | /* allocate enough for a simple 1:1 translation without |
| 7970 | replacements, if we need more, we'll resize */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7971 | osize = size; |
| 7972 | output = PyMem_Malloc(osize * sizeof(Py_UCS4)); |
| 7973 | opos = 0; |
| 7974 | if (output == NULL) { |
| 7975 | PyErr_NoMemory(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7976 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7977 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7978 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7979 | while (i<size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7980 | /* try to encode it */ |
| 7981 | PyObject *x = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7982 | if (charmaptranslate_output(input, i, mapping, |
| 7983 | &output, &osize, &opos, &x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7984 | Py_XDECREF(x); |
| 7985 | goto onError; |
| 7986 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7987 | Py_XDECREF(x); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7988 | if (x!=Py_None) /* it worked => adjust input pointer */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7989 | ++i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7990 | else { /* untranslatable character */ |
| 7991 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
| 7992 | Py_ssize_t repsize; |
| 7993 | Py_ssize_t newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7994 | Py_ssize_t uni2; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7995 | /* startpos for collecting untranslatable chars */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7996 | Py_ssize_t collstart = i; |
| 7997 | Py_ssize_t collend = i+1; |
| 7998 | Py_ssize_t coll; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7999 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8000 | /* find all untranslatable characters */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8001 | while (collend < size) { |
| 8002 | if (charmaptranslate_lookup(PyUnicode_READ(kind,idata, collend), mapping, &x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8003 | goto onError; |
| 8004 | Py_XDECREF(x); |
| 8005 | if (x!=Py_None) |
| 8006 | break; |
| 8007 | ++collend; |
| 8008 | } |
| 8009 | /* cache callback name lookup |
| 8010 | * (if not done yet, i.e. it's the first error) */ |
| 8011 | if (known_errorHandler==-1) { |
| 8012 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 8013 | known_errorHandler = 1; |
| 8014 | else if (!strcmp(errors, "replace")) |
| 8015 | known_errorHandler = 2; |
| 8016 | else if (!strcmp(errors, "ignore")) |
| 8017 | known_errorHandler = 3; |
| 8018 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 8019 | known_errorHandler = 4; |
| 8020 | else |
| 8021 | known_errorHandler = 0; |
| 8022 | } |
| 8023 | switch (known_errorHandler) { |
| 8024 | case 1: /* strict */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8025 | raise_translate_exception(&exc, input, collstart, |
| 8026 | collend, reason); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8027 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8028 | case 2: /* replace */ |
| 8029 | /* 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] | 8030 | for (coll = collstart; coll<collend; coll++) |
| 8031 | output[opos++] = '?'; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8032 | /* fall through */ |
| 8033 | case 3: /* ignore */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8034 | i = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8035 | break; |
| 8036 | case 4: /* xmlcharrefreplace */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8037 | /* generate replacement (temporarily (mis)uses i) */ |
| 8038 | for (i = collstart; i < collend; ++i) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8039 | char buffer[2+29+1+1]; |
| 8040 | char *cp; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8041 | sprintf(buffer, "&#%d;", PyUnicode_READ(kind, idata, i)); |
| 8042 | if (charmaptranslate_makespace(&output, &osize, |
| 8043 | opos+strlen(buffer)+(size-collend))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8044 | goto onError; |
| 8045 | for (cp = buffer; *cp; ++cp) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8046 | output[opos++] = *cp; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8047 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8048 | i = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8049 | break; |
| 8050 | default: |
| 8051 | repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8052 | reason, input, &exc, |
| 8053 | collstart, collend, &newpos); |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 8054 | if (repunicode == NULL || _PyUnicode_READY_REPLACE(&repunicode)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8055 | goto onError; |
| 8056 | /* generate replacement */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8057 | repsize = PyUnicode_GET_LENGTH(repunicode); |
| 8058 | if (charmaptranslate_makespace(&output, &osize, |
| 8059 | opos+repsize+(size-collend))) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8060 | Py_DECREF(repunicode); |
| 8061 | goto onError; |
| 8062 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8063 | for (uni2 = 0; repsize-->0; ++uni2) |
| 8064 | output[opos++] = PyUnicode_READ_CHAR(repunicode, uni2); |
| 8065 | i = newpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8066 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8067 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8068 | } |
| 8069 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8070 | res = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, output, opos); |
| 8071 | if (!res) |
| 8072 | goto onError; |
| 8073 | PyMem_Free(output); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8074 | Py_XDECREF(exc); |
| 8075 | Py_XDECREF(errorHandler); |
| 8076 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8077 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8078 | onError: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8079 | PyMem_Free(output); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8080 | Py_XDECREF(exc); |
| 8081 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8082 | return NULL; |
| 8083 | } |
| 8084 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8085 | /* Deprecated. Use PyUnicode_Translate instead. */ |
| 8086 | PyObject * |
| 8087 | PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
| 8088 | Py_ssize_t size, |
| 8089 | PyObject *mapping, |
| 8090 | const char *errors) |
| 8091 | { |
| 8092 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 8093 | if (!unicode) |
| 8094 | return NULL; |
| 8095 | return _PyUnicode_TranslateCharmap(unicode, mapping, errors); |
| 8096 | } |
| 8097 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8098 | PyObject * |
| 8099 | PyUnicode_Translate(PyObject *str, |
| 8100 | PyObject *mapping, |
| 8101 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8102 | { |
| 8103 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8104 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8105 | str = PyUnicode_FromObject(str); |
| 8106 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8107 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8108 | result = _PyUnicode_TranslateCharmap(str, mapping, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8109 | Py_DECREF(str); |
| 8110 | return result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8111 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8112 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8113 | Py_XDECREF(str); |
| 8114 | return NULL; |
| 8115 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8116 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8117 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8118 | fix_decimal_and_space_to_ascii(PyObject *self) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8119 | { |
| 8120 | /* No need to call PyUnicode_READY(self) because this function is only |
| 8121 | called as a callback from fixup() which does it already. */ |
| 8122 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 8123 | const int kind = PyUnicode_KIND(self); |
| 8124 | void *data = PyUnicode_DATA(self); |
| 8125 | Py_UCS4 maxchar = 0, ch, fixed; |
| 8126 | Py_ssize_t i; |
| 8127 | |
| 8128 | for (i = 0; i < len; ++i) { |
| 8129 | ch = PyUnicode_READ(kind, data, i); |
| 8130 | fixed = 0; |
| 8131 | if (ch > 127) { |
| 8132 | if (Py_UNICODE_ISSPACE(ch)) |
| 8133 | fixed = ' '; |
| 8134 | else { |
| 8135 | const int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8136 | if (decimal >= 0) |
| 8137 | fixed = '0' + decimal; |
| 8138 | } |
| 8139 | if (fixed != 0) { |
| 8140 | if (fixed > maxchar) |
| 8141 | maxchar = fixed; |
| 8142 | PyUnicode_WRITE(kind, data, i, fixed); |
| 8143 | } |
| 8144 | else if (ch > maxchar) |
| 8145 | maxchar = ch; |
| 8146 | } |
| 8147 | else if (ch > maxchar) |
| 8148 | maxchar = ch; |
| 8149 | } |
| 8150 | |
| 8151 | return maxchar; |
| 8152 | } |
| 8153 | |
| 8154 | PyObject * |
| 8155 | _PyUnicode_TransformDecimalAndSpaceToASCII(PyObject *unicode) |
| 8156 | { |
| 8157 | if (!PyUnicode_Check(unicode)) { |
| 8158 | PyErr_BadInternalCall(); |
| 8159 | return NULL; |
| 8160 | } |
| 8161 | if (PyUnicode_READY(unicode) == -1) |
| 8162 | return NULL; |
| 8163 | if (PyUnicode_MAX_CHAR_VALUE(unicode) <= 127) { |
| 8164 | /* If the string is already ASCII, just return the same string */ |
| 8165 | Py_INCREF(unicode); |
| 8166 | return unicode; |
| 8167 | } |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8168 | return fixup(unicode, fix_decimal_and_space_to_ascii); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8169 | } |
| 8170 | |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8171 | PyObject * |
| 8172 | PyUnicode_TransformDecimalToASCII(Py_UNICODE *s, |
| 8173 | Py_ssize_t length) |
| 8174 | { |
| 8175 | PyObject *result; |
| 8176 | Py_UNICODE *p; /* write pointer into result */ |
| 8177 | Py_ssize_t i; |
| 8178 | /* Copy to a new string */ |
| 8179 | result = (PyObject *)_PyUnicode_New(length); |
| 8180 | Py_UNICODE_COPY(PyUnicode_AS_UNICODE(result), s, length); |
| 8181 | if (result == NULL) |
| 8182 | return result; |
| 8183 | p = PyUnicode_AS_UNICODE(result); |
| 8184 | /* Iterate over code points */ |
| 8185 | for (i = 0; i < length; i++) { |
| 8186 | Py_UNICODE ch =s[i]; |
| 8187 | if (ch > 127) { |
| 8188 | int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8189 | if (decimal >= 0) |
| 8190 | p[i] = '0' + decimal; |
| 8191 | } |
| 8192 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 8193 | #ifndef DONT_MAKE_RESULT_READY |
| 8194 | if (_PyUnicode_READY_REPLACE(&result)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8195 | Py_DECREF(result); |
| 8196 | return NULL; |
| 8197 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 8198 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 8199 | assert(_PyUnicode_CheckConsistency(result, 1)); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8200 | return result; |
| 8201 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8202 | /* --- Decimal Encoder ---------------------------------------------------- */ |
| 8203 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8204 | int |
| 8205 | PyUnicode_EncodeDecimal(Py_UNICODE *s, |
| 8206 | Py_ssize_t length, |
| 8207 | char *output, |
| 8208 | const char *errors) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8209 | { |
| 8210 | Py_UNICODE *p, *end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8211 | PyObject *errorHandler = NULL; |
| 8212 | PyObject *exc = NULL; |
| 8213 | const char *encoding = "decimal"; |
| 8214 | const char *reason = "invalid decimal Unicode string"; |
| 8215 | /* the following variable is used for caching string comparisons |
| 8216 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 8217 | int known_errorHandler = -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8218 | |
| 8219 | if (output == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8220 | PyErr_BadArgument(); |
| 8221 | return -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8222 | } |
| 8223 | |
| 8224 | p = s; |
| 8225 | end = s + length; |
| 8226 | while (p < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8227 | register Py_UNICODE ch = *p; |
| 8228 | int decimal; |
| 8229 | PyObject *repunicode; |
| 8230 | Py_ssize_t repsize; |
| 8231 | Py_ssize_t newpos; |
| 8232 | Py_UNICODE *uni2; |
| 8233 | Py_UNICODE *collstart; |
| 8234 | Py_UNICODE *collend; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8235 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8236 | if (Py_UNICODE_ISSPACE(ch)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8237 | *output++ = ' '; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8238 | ++p; |
| 8239 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8240 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8241 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 8242 | if (decimal >= 0) { |
| 8243 | *output++ = '0' + decimal; |
| 8244 | ++p; |
| 8245 | continue; |
| 8246 | } |
| 8247 | if (0 < ch && ch < 256) { |
| 8248 | *output++ = (char)ch; |
| 8249 | ++p; |
| 8250 | continue; |
| 8251 | } |
| 8252 | /* All other characters are considered unencodable */ |
| 8253 | collstart = p; |
| 8254 | collend = p+1; |
| 8255 | while (collend < end) { |
| 8256 | if ((0 < *collend && *collend < 256) || |
| 8257 | !Py_UNICODE_ISSPACE(*collend) || |
| 8258 | Py_UNICODE_TODECIMAL(*collend)) |
| 8259 | break; |
| 8260 | } |
| 8261 | /* cache callback name lookup |
| 8262 | * (if not done yet, i.e. it's the first error) */ |
| 8263 | if (known_errorHandler==-1) { |
| 8264 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 8265 | known_errorHandler = 1; |
| 8266 | else if (!strcmp(errors, "replace")) |
| 8267 | known_errorHandler = 2; |
| 8268 | else if (!strcmp(errors, "ignore")) |
| 8269 | known_errorHandler = 3; |
| 8270 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 8271 | known_errorHandler = 4; |
| 8272 | else |
| 8273 | known_errorHandler = 0; |
| 8274 | } |
| 8275 | switch (known_errorHandler) { |
| 8276 | case 1: /* strict */ |
| 8277 | raise_encode_exception(&exc, encoding, s, length, collstart-s, collend-s, reason); |
| 8278 | goto onError; |
| 8279 | case 2: /* replace */ |
| 8280 | for (p = collstart; p < collend; ++p) |
| 8281 | *output++ = '?'; |
| 8282 | /* fall through */ |
| 8283 | case 3: /* ignore */ |
| 8284 | p = collend; |
| 8285 | break; |
| 8286 | case 4: /* xmlcharrefreplace */ |
| 8287 | /* generate replacement (temporarily (mis)uses p) */ |
| 8288 | for (p = collstart; p < collend; ++p) |
| 8289 | output += sprintf(output, "&#%d;", (int)*p); |
| 8290 | p = collend; |
| 8291 | break; |
| 8292 | default: |
| 8293 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 8294 | encoding, reason, s, length, &exc, |
| 8295 | collstart-s, collend-s, &newpos); |
| 8296 | if (repunicode == NULL) |
| 8297 | goto onError; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 8298 | if (!PyUnicode_Check(repunicode)) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8299 | /* Byte results not supported, since they have no decimal property. */ |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 8300 | PyErr_SetString(PyExc_TypeError, "error handler should return unicode"); |
| 8301 | Py_DECREF(repunicode); |
| 8302 | goto onError; |
| 8303 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8304 | /* generate replacement */ |
| 8305 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 8306 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
| 8307 | Py_UNICODE ch = *uni2; |
| 8308 | if (Py_UNICODE_ISSPACE(ch)) |
| 8309 | *output++ = ' '; |
| 8310 | else { |
| 8311 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 8312 | if (decimal >= 0) |
| 8313 | *output++ = '0' + decimal; |
| 8314 | else if (0 < ch && ch < 256) |
| 8315 | *output++ = (char)ch; |
| 8316 | else { |
| 8317 | Py_DECREF(repunicode); |
| 8318 | raise_encode_exception(&exc, encoding, |
| 8319 | s, length, collstart-s, collend-s, reason); |
| 8320 | goto onError; |
| 8321 | } |
| 8322 | } |
| 8323 | } |
| 8324 | p = s + newpos; |
| 8325 | Py_DECREF(repunicode); |
| 8326 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8327 | } |
| 8328 | /* 0-terminate the output string */ |
| 8329 | *output++ = '\0'; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8330 | Py_XDECREF(exc); |
| 8331 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8332 | return 0; |
| 8333 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8334 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8335 | Py_XDECREF(exc); |
| 8336 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8337 | return -1; |
| 8338 | } |
| 8339 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8340 | /* --- Helpers ------------------------------------------------------------ */ |
| 8341 | |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 8342 | #include "stringlib/asciilib.h" |
| 8343 | #include "stringlib/fastsearch.h" |
| 8344 | #include "stringlib/partition.h" |
| 8345 | #include "stringlib/split.h" |
| 8346 | #include "stringlib/count.h" |
| 8347 | #include "stringlib/find.h" |
| 8348 | #include "stringlib/localeutil.h" |
| 8349 | #include "stringlib/undef.h" |
| 8350 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8351 | #include "stringlib/ucs1lib.h" |
| 8352 | #include "stringlib/fastsearch.h" |
| 8353 | #include "stringlib/partition.h" |
| 8354 | #include "stringlib/split.h" |
| 8355 | #include "stringlib/count.h" |
| 8356 | #include "stringlib/find.h" |
| 8357 | #include "stringlib/localeutil.h" |
| 8358 | #include "stringlib/undef.h" |
| 8359 | |
| 8360 | #include "stringlib/ucs2lib.h" |
| 8361 | #include "stringlib/fastsearch.h" |
| 8362 | #include "stringlib/partition.h" |
| 8363 | #include "stringlib/split.h" |
| 8364 | #include "stringlib/count.h" |
| 8365 | #include "stringlib/find.h" |
| 8366 | #include "stringlib/localeutil.h" |
| 8367 | #include "stringlib/undef.h" |
| 8368 | |
| 8369 | #include "stringlib/ucs4lib.h" |
| 8370 | #include "stringlib/fastsearch.h" |
| 8371 | #include "stringlib/partition.h" |
| 8372 | #include "stringlib/split.h" |
| 8373 | #include "stringlib/count.h" |
| 8374 | #include "stringlib/find.h" |
| 8375 | #include "stringlib/localeutil.h" |
| 8376 | #include "stringlib/undef.h" |
| 8377 | |
| 8378 | static Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 8379 | any_find_slice(Py_ssize_t Py_LOCAL_CALLBACK(ascii)(const Py_UCS1*, Py_ssize_t, |
| 8380 | const Py_UCS1*, Py_ssize_t, |
| 8381 | Py_ssize_t, Py_ssize_t), |
| 8382 | 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] | 8383 | const Py_UCS1*, Py_ssize_t, |
| 8384 | Py_ssize_t, Py_ssize_t), |
| 8385 | Py_ssize_t Py_LOCAL_CALLBACK(ucs2)(const Py_UCS2*, Py_ssize_t, |
| 8386 | const Py_UCS2*, Py_ssize_t, |
| 8387 | Py_ssize_t, Py_ssize_t), |
| 8388 | Py_ssize_t Py_LOCAL_CALLBACK(ucs4)(const Py_UCS4*, Py_ssize_t, |
| 8389 | const Py_UCS4*, Py_ssize_t, |
| 8390 | Py_ssize_t, Py_ssize_t), |
| 8391 | PyObject* s1, PyObject* s2, |
| 8392 | Py_ssize_t start, |
| 8393 | Py_ssize_t end) |
| 8394 | { |
| 8395 | int kind1, kind2, kind; |
| 8396 | void *buf1, *buf2; |
| 8397 | Py_ssize_t len1, len2, result; |
| 8398 | |
| 8399 | kind1 = PyUnicode_KIND(s1); |
| 8400 | kind2 = PyUnicode_KIND(s2); |
| 8401 | kind = kind1 > kind2 ? kind1 : kind2; |
| 8402 | buf1 = PyUnicode_DATA(s1); |
| 8403 | buf2 = PyUnicode_DATA(s2); |
| 8404 | if (kind1 != kind) |
| 8405 | buf1 = _PyUnicode_AsKind(s1, kind); |
| 8406 | if (!buf1) |
| 8407 | return -2; |
| 8408 | if (kind2 != kind) |
| 8409 | buf2 = _PyUnicode_AsKind(s2, kind); |
| 8410 | if (!buf2) { |
| 8411 | if (kind1 != kind) PyMem_Free(buf1); |
| 8412 | return -2; |
| 8413 | } |
| 8414 | len1 = PyUnicode_GET_LENGTH(s1); |
| 8415 | len2 = PyUnicode_GET_LENGTH(s2); |
| 8416 | |
| 8417 | switch(kind) { |
| 8418 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 8419 | if (PyUnicode_IS_ASCII(s1) && PyUnicode_IS_ASCII(s2)) |
| 8420 | result = ascii(buf1, len1, buf2, len2, start, end); |
| 8421 | else |
| 8422 | result = ucs1(buf1, len1, buf2, len2, start, end); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8423 | break; |
| 8424 | case PyUnicode_2BYTE_KIND: |
| 8425 | result = ucs2(buf1, len1, buf2, len2, start, end); |
| 8426 | break; |
| 8427 | case PyUnicode_4BYTE_KIND: |
| 8428 | result = ucs4(buf1, len1, buf2, len2, start, end); |
| 8429 | break; |
| 8430 | default: |
| 8431 | assert(0); result = -2; |
| 8432 | } |
| 8433 | |
| 8434 | if (kind1 != kind) |
| 8435 | PyMem_Free(buf1); |
| 8436 | if (kind2 != kind) |
| 8437 | PyMem_Free(buf2); |
| 8438 | |
| 8439 | return result; |
| 8440 | } |
| 8441 | |
| 8442 | Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 8443 | _PyUnicode_InsertThousandsGrouping(PyObject *unicode, int kind, void *data, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8444 | Py_ssize_t n_buffer, |
| 8445 | void *digits, Py_ssize_t n_digits, |
| 8446 | Py_ssize_t min_width, |
| 8447 | const char *grouping, |
| 8448 | const char *thousands_sep) |
| 8449 | { |
| 8450 | switch(kind) { |
| 8451 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 8452 | if (unicode != NULL && PyUnicode_IS_ASCII(unicode)) |
| 8453 | return _PyUnicode_ascii_InsertThousandsGrouping( |
| 8454 | (Py_UCS1*)data, n_buffer, (Py_UCS1*)digits, n_digits, |
| 8455 | min_width, grouping, thousands_sep); |
| 8456 | else |
| 8457 | return _PyUnicode_ucs1_InsertThousandsGrouping( |
| 8458 | (Py_UCS1*)data, n_buffer, (Py_UCS1*)digits, n_digits, |
| 8459 | min_width, grouping, thousands_sep); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8460 | case PyUnicode_2BYTE_KIND: |
| 8461 | return _PyUnicode_ucs2_InsertThousandsGrouping( |
| 8462 | (Py_UCS2*)data, n_buffer, (Py_UCS2*)digits, n_digits, |
| 8463 | min_width, grouping, thousands_sep); |
| 8464 | case PyUnicode_4BYTE_KIND: |
| 8465 | return _PyUnicode_ucs4_InsertThousandsGrouping( |
| 8466 | (Py_UCS4*)data, n_buffer, (Py_UCS4*)digits, n_digits, |
| 8467 | min_width, grouping, thousands_sep); |
| 8468 | } |
| 8469 | assert(0); |
| 8470 | return -1; |
| 8471 | } |
| 8472 | |
| 8473 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 8474 | #include "stringlib/unicodedefs.h" |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8475 | #include "stringlib/fastsearch.h" |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 8476 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8477 | #include "stringlib/count.h" |
| 8478 | #include "stringlib/find.h" |
Eric Smith | 5807c41 | 2008-05-11 21:00:57 +0000 | [diff] [blame] | 8479 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8480 | /* helper macro to fixup start/end slice values */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 8481 | #define ADJUST_INDICES(start, end, len) \ |
| 8482 | if (end > len) \ |
| 8483 | end = len; \ |
| 8484 | else if (end < 0) { \ |
| 8485 | end += len; \ |
| 8486 | if (end < 0) \ |
| 8487 | end = 0; \ |
| 8488 | } \ |
| 8489 | if (start < 0) { \ |
| 8490 | start += len; \ |
| 8491 | if (start < 0) \ |
| 8492 | start = 0; \ |
| 8493 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8494 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8495 | Py_ssize_t |
| 8496 | PyUnicode_Count(PyObject *str, |
| 8497 | PyObject *substr, |
| 8498 | Py_ssize_t start, |
| 8499 | Py_ssize_t end) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8500 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8501 | Py_ssize_t result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8502 | PyUnicodeObject* str_obj; |
| 8503 | PyUnicodeObject* sub_obj; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8504 | int kind1, kind2, kind; |
| 8505 | void *buf1 = NULL, *buf2 = NULL; |
| 8506 | Py_ssize_t len1, len2; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8507 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8508 | str_obj = (PyUnicodeObject*) PyUnicode_FromObject(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8509 | if (!str_obj || PyUnicode_READY(str_obj) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8510 | return -1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8511 | sub_obj = (PyUnicodeObject*) PyUnicode_FromObject(substr); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 8512 | if (!sub_obj || PyUnicode_READY(sub_obj) == -1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8513 | Py_DECREF(str_obj); |
| 8514 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8515 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8516 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8517 | kind1 = PyUnicode_KIND(str_obj); |
| 8518 | kind2 = PyUnicode_KIND(sub_obj); |
| 8519 | kind = kind1 > kind2 ? kind1 : kind2; |
| 8520 | buf1 = PyUnicode_DATA(str_obj); |
| 8521 | if (kind1 != kind) |
| 8522 | buf1 = _PyUnicode_AsKind((PyObject*)str_obj, kind); |
| 8523 | if (!buf1) |
| 8524 | goto onError; |
| 8525 | buf2 = PyUnicode_DATA(sub_obj); |
| 8526 | if (kind2 != kind) |
| 8527 | buf2 = _PyUnicode_AsKind((PyObject*)sub_obj, kind); |
| 8528 | if (!buf2) |
| 8529 | goto onError; |
| 8530 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 8531 | len2 = PyUnicode_GET_LENGTH(sub_obj); |
| 8532 | |
| 8533 | ADJUST_INDICES(start, end, len1); |
| 8534 | switch(kind) { |
| 8535 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 8536 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sub_obj)) |
| 8537 | result = asciilib_count( |
| 8538 | ((Py_UCS1*)buf1) + start, end - start, |
| 8539 | buf2, len2, PY_SSIZE_T_MAX |
| 8540 | ); |
| 8541 | else |
| 8542 | result = ucs1lib_count( |
| 8543 | ((Py_UCS1*)buf1) + start, end - start, |
| 8544 | buf2, len2, PY_SSIZE_T_MAX |
| 8545 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8546 | break; |
| 8547 | case PyUnicode_2BYTE_KIND: |
| 8548 | result = ucs2lib_count( |
| 8549 | ((Py_UCS2*)buf1) + start, end - start, |
| 8550 | buf2, len2, PY_SSIZE_T_MAX |
| 8551 | ); |
| 8552 | break; |
| 8553 | case PyUnicode_4BYTE_KIND: |
| 8554 | result = ucs4lib_count( |
| 8555 | ((Py_UCS4*)buf1) + start, end - start, |
| 8556 | buf2, len2, PY_SSIZE_T_MAX |
| 8557 | ); |
| 8558 | break; |
| 8559 | default: |
| 8560 | assert(0); result = 0; |
| 8561 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8562 | |
| 8563 | Py_DECREF(sub_obj); |
| 8564 | Py_DECREF(str_obj); |
| 8565 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8566 | if (kind1 != kind) |
| 8567 | PyMem_Free(buf1); |
| 8568 | if (kind2 != kind) |
| 8569 | PyMem_Free(buf2); |
| 8570 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8571 | return result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8572 | onError: |
| 8573 | Py_DECREF(sub_obj); |
| 8574 | Py_DECREF(str_obj); |
| 8575 | if (kind1 != kind && buf1) |
| 8576 | PyMem_Free(buf1); |
| 8577 | if (kind2 != kind && buf2) |
| 8578 | PyMem_Free(buf2); |
| 8579 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8580 | } |
| 8581 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8582 | Py_ssize_t |
| 8583 | PyUnicode_Find(PyObject *str, |
| 8584 | PyObject *sub, |
| 8585 | Py_ssize_t start, |
| 8586 | Py_ssize_t end, |
| 8587 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8588 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8589 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8590 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8591 | str = PyUnicode_FromObject(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8592 | if (!str || PyUnicode_READY(str) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8593 | return -2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8594 | sub = PyUnicode_FromObject(sub); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8595 | if (!sub || PyUnicode_READY(sub) == -1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8596 | Py_DECREF(str); |
| 8597 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8598 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8599 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8600 | if (direction > 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8601 | result = any_find_slice( |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 8602 | asciilib_find_slice, ucs1lib_find_slice, |
| 8603 | ucs2lib_find_slice, ucs4lib_find_slice, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8604 | str, sub, start, end |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8605 | ); |
| 8606 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8607 | result = any_find_slice( |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 8608 | asciilib_find_slice, ucs1lib_rfind_slice, |
| 8609 | ucs2lib_rfind_slice, ucs4lib_rfind_slice, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8610 | str, sub, start, end |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8611 | ); |
| 8612 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8613 | Py_DECREF(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8614 | Py_DECREF(sub); |
| 8615 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8616 | return result; |
| 8617 | } |
| 8618 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8619 | Py_ssize_t |
| 8620 | PyUnicode_FindChar(PyObject *str, Py_UCS4 ch, |
| 8621 | Py_ssize_t start, Py_ssize_t end, |
| 8622 | int direction) |
| 8623 | { |
| 8624 | char *result; |
| 8625 | int kind; |
| 8626 | if (PyUnicode_READY(str) == -1) |
| 8627 | return -2; |
Victor Stinner | 267aa24 | 2011-10-02 01:08:37 +0200 | [diff] [blame] | 8628 | if (start < 0 || end < 0) { |
| 8629 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 8630 | return -2; |
| 8631 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8632 | if (end > PyUnicode_GET_LENGTH(str)) |
| 8633 | end = PyUnicode_GET_LENGTH(str); |
| 8634 | kind = PyUnicode_KIND(str); |
| 8635 | result = findchar(PyUnicode_1BYTE_DATA(str) |
| 8636 | + PyUnicode_KIND_SIZE(kind, start), |
| 8637 | kind, |
| 8638 | end-start, ch, direction); |
| 8639 | if (!result) |
| 8640 | return -1; |
| 8641 | return (result-(char*)PyUnicode_DATA(str)) >> (kind-1); |
| 8642 | } |
| 8643 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8644 | static int |
| 8645 | tailmatch(PyUnicodeObject *self, |
| 8646 | PyUnicodeObject *substring, |
| 8647 | Py_ssize_t start, |
| 8648 | Py_ssize_t end, |
| 8649 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8650 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8651 | int kind_self; |
| 8652 | int kind_sub; |
| 8653 | void *data_self; |
| 8654 | void *data_sub; |
| 8655 | Py_ssize_t offset; |
| 8656 | Py_ssize_t i; |
| 8657 | Py_ssize_t end_sub; |
| 8658 | |
| 8659 | if (PyUnicode_READY(self) == -1 || |
| 8660 | PyUnicode_READY(substring) == -1) |
| 8661 | return 0; |
| 8662 | |
| 8663 | if (PyUnicode_GET_LENGTH(substring) == 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8664 | return 1; |
| 8665 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8666 | ADJUST_INDICES(start, end, PyUnicode_GET_LENGTH(self)); |
| 8667 | end -= PyUnicode_GET_LENGTH(substring); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8668 | if (end < start) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8669 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8670 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8671 | kind_self = PyUnicode_KIND(self); |
| 8672 | data_self = PyUnicode_DATA(self); |
| 8673 | kind_sub = PyUnicode_KIND(substring); |
| 8674 | data_sub = PyUnicode_DATA(substring); |
| 8675 | end_sub = PyUnicode_GET_LENGTH(substring) - 1; |
| 8676 | |
| 8677 | if (direction > 0) |
| 8678 | offset = end; |
| 8679 | else |
| 8680 | offset = start; |
| 8681 | |
| 8682 | if (PyUnicode_READ(kind_self, data_self, offset) == |
| 8683 | PyUnicode_READ(kind_sub, data_sub, 0) && |
| 8684 | PyUnicode_READ(kind_self, data_self, offset + end_sub) == |
| 8685 | PyUnicode_READ(kind_sub, data_sub, end_sub)) { |
| 8686 | /* If both are of the same kind, memcmp is sufficient */ |
| 8687 | if (kind_self == kind_sub) { |
| 8688 | return ! memcmp((char *)data_self + |
| 8689 | (offset * PyUnicode_CHARACTER_SIZE(substring)), |
| 8690 | data_sub, |
| 8691 | PyUnicode_GET_LENGTH(substring) * |
| 8692 | PyUnicode_CHARACTER_SIZE(substring)); |
| 8693 | } |
| 8694 | /* otherwise we have to compare each character by first accesing it */ |
| 8695 | else { |
| 8696 | /* We do not need to compare 0 and len(substring)-1 because |
| 8697 | the if statement above ensured already that they are equal |
| 8698 | when we end up here. */ |
| 8699 | // TODO: honor direction and do a forward or backwards search |
| 8700 | for (i = 1; i < end_sub; ++i) { |
| 8701 | if (PyUnicode_READ(kind_self, data_self, offset + i) != |
| 8702 | PyUnicode_READ(kind_sub, data_sub, i)) |
| 8703 | return 0; |
| 8704 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8705 | return 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8706 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8707 | } |
| 8708 | |
| 8709 | return 0; |
| 8710 | } |
| 8711 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8712 | Py_ssize_t |
| 8713 | PyUnicode_Tailmatch(PyObject *str, |
| 8714 | PyObject *substr, |
| 8715 | Py_ssize_t start, |
| 8716 | Py_ssize_t end, |
| 8717 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8718 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8719 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8720 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8721 | str = PyUnicode_FromObject(str); |
| 8722 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8723 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8724 | substr = PyUnicode_FromObject(substr); |
| 8725 | if (substr == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8726 | Py_DECREF(str); |
| 8727 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8728 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8729 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8730 | result = tailmatch((PyUnicodeObject *)str, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8731 | (PyUnicodeObject *)substr, |
| 8732 | start, end, direction); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8733 | Py_DECREF(str); |
| 8734 | Py_DECREF(substr); |
| 8735 | return result; |
| 8736 | } |
| 8737 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8738 | /* Apply fixfct filter to the Unicode object self and return a |
| 8739 | reference to the modified object */ |
| 8740 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8741 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8742 | fixup(PyObject *self, |
| 8743 | Py_UCS4 (*fixfct)(PyObject *s)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8744 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8745 | PyObject *u; |
| 8746 | Py_UCS4 maxchar_old, maxchar_new = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8747 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8748 | if (PyUnicode_READY(self) == -1) |
| 8749 | return NULL; |
| 8750 | maxchar_old = PyUnicode_MAX_CHAR_VALUE(self); |
| 8751 | u = PyUnicode_New(PyUnicode_GET_LENGTH(self), |
| 8752 | maxchar_old); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8753 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8754 | return NULL; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8755 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8756 | Py_MEMCPY(PyUnicode_1BYTE_DATA(u), PyUnicode_1BYTE_DATA(self), |
| 8757 | PyUnicode_GET_LENGTH(u) * PyUnicode_CHARACTER_SIZE(u)); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8758 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8759 | /* fix functions return the new maximum character in a string, |
| 8760 | if the kind of the resulting unicode object does not change, |
| 8761 | everything is fine. Otherwise we need to change the string kind |
| 8762 | and re-run the fix function. */ |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8763 | maxchar_new = fixfct(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8764 | if (maxchar_new == 0) |
| 8765 | /* do nothing, keep maxchar_new at 0 which means no changes. */; |
| 8766 | else if (maxchar_new <= 127) |
| 8767 | maxchar_new = 127; |
| 8768 | else if (maxchar_new <= 255) |
| 8769 | maxchar_new = 255; |
| 8770 | else if (maxchar_new <= 65535) |
| 8771 | maxchar_new = 65535; |
| 8772 | else |
| 8773 | maxchar_new = 1114111; /* 0x10ffff */ |
| 8774 | |
| 8775 | if (!maxchar_new && PyUnicode_CheckExact(self)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8776 | /* fixfct should return TRUE if it modified the buffer. If |
| 8777 | FALSE, return a reference to the original buffer instead |
| 8778 | (to save space, not time) */ |
| 8779 | Py_INCREF(self); |
| 8780 | Py_DECREF(u); |
| 8781 | return (PyObject*) self; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8782 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8783 | else if (maxchar_new == maxchar_old) { |
| 8784 | return u; |
| 8785 | } |
| 8786 | else { |
| 8787 | /* In case the maximum character changed, we need to |
| 8788 | convert the string to the new category. */ |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 8789 | PyObject *v = PyUnicode_New(PyUnicode_GET_LENGTH(self), maxchar_new); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8790 | if (v == NULL) { |
| 8791 | Py_DECREF(u); |
| 8792 | return NULL; |
| 8793 | } |
| 8794 | if (maxchar_new > maxchar_old) { |
| 8795 | /* If the maxchar increased so that the kind changed, not all |
| 8796 | characters are representable anymore and we need to fix the |
| 8797 | string again. This only happens in very few cases. */ |
Victor Stinner | 157f83f | 2011-09-28 21:41:31 +0200 | [diff] [blame] | 8798 | if (PyUnicode_CopyCharacters(v, 0, |
| 8799 | (PyObject*)self, 0, |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 8800 | PyUnicode_GET_LENGTH(self)) < 0) |
| 8801 | { |
| 8802 | Py_DECREF(u); |
| 8803 | return NULL; |
| 8804 | } |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8805 | maxchar_old = fixfct(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8806 | assert(maxchar_old > 0 && maxchar_old <= maxchar_new); |
| 8807 | } |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 8808 | else { |
Victor Stinner | 157f83f | 2011-09-28 21:41:31 +0200 | [diff] [blame] | 8809 | if (PyUnicode_CopyCharacters(v, 0, |
| 8810 | u, 0, |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 8811 | PyUnicode_GET_LENGTH(self)) < 0) |
| 8812 | { |
| 8813 | Py_DECREF(u); |
| 8814 | return NULL; |
| 8815 | } |
| 8816 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8817 | |
| 8818 | Py_DECREF(u); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 8819 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8820 | return v; |
| 8821 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8822 | } |
| 8823 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8824 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8825 | fixupper(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8826 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8827 | /* No need to call PyUnicode_READY(self) because this function is only |
| 8828 | called as a callback from fixup() which does it already. */ |
| 8829 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 8830 | const int kind = PyUnicode_KIND(self); |
| 8831 | void *data = PyUnicode_DATA(self); |
| 8832 | int touched = 0; |
| 8833 | Py_UCS4 maxchar = 0; |
| 8834 | Py_ssize_t i; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8835 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8836 | for (i = 0; i < len; ++i) { |
| 8837 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 8838 | const Py_UCS4 up = Py_UNICODE_TOUPPER(ch); |
| 8839 | if (up != ch) { |
| 8840 | if (up > maxchar) |
| 8841 | maxchar = up; |
| 8842 | PyUnicode_WRITE(kind, data, i, up); |
| 8843 | touched = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8844 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8845 | else if (ch > maxchar) |
| 8846 | maxchar = ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8847 | } |
| 8848 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8849 | if (touched) |
| 8850 | return maxchar; |
| 8851 | else |
| 8852 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8853 | } |
| 8854 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8855 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8856 | fixlower(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8857 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8858 | /* No need to call PyUnicode_READY(self) because fixup() which does it. */ |
| 8859 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 8860 | const int kind = PyUnicode_KIND(self); |
| 8861 | void *data = PyUnicode_DATA(self); |
| 8862 | int touched = 0; |
| 8863 | Py_UCS4 maxchar = 0; |
| 8864 | Py_ssize_t i; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8865 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8866 | for(i = 0; i < len; ++i) { |
| 8867 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 8868 | const Py_UCS4 lo = Py_UNICODE_TOLOWER(ch); |
| 8869 | if (lo != ch) { |
| 8870 | if (lo > maxchar) |
| 8871 | maxchar = lo; |
| 8872 | PyUnicode_WRITE(kind, data, i, lo); |
| 8873 | touched = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8874 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8875 | else if (ch > maxchar) |
| 8876 | maxchar = ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8877 | } |
| 8878 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8879 | if (touched) |
| 8880 | return maxchar; |
| 8881 | else |
| 8882 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8883 | } |
| 8884 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8885 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8886 | fixswapcase(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8887 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8888 | /* No need to call PyUnicode_READY(self) because fixup() which does it. */ |
| 8889 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 8890 | const int kind = PyUnicode_KIND(self); |
| 8891 | void *data = PyUnicode_DATA(self); |
| 8892 | int touched = 0; |
| 8893 | Py_UCS4 maxchar = 0; |
| 8894 | Py_ssize_t i; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8895 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8896 | for(i = 0; i < len; ++i) { |
| 8897 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 8898 | Py_UCS4 nu = 0; |
| 8899 | |
| 8900 | if (Py_UNICODE_ISUPPER(ch)) |
| 8901 | nu = Py_UNICODE_TOLOWER(ch); |
| 8902 | else if (Py_UNICODE_ISLOWER(ch)) |
| 8903 | nu = Py_UNICODE_TOUPPER(ch); |
| 8904 | |
| 8905 | if (nu != 0) { |
| 8906 | if (nu > maxchar) |
| 8907 | maxchar = nu; |
| 8908 | PyUnicode_WRITE(kind, data, i, nu); |
| 8909 | touched = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8910 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8911 | else if (ch > maxchar) |
| 8912 | maxchar = ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8913 | } |
| 8914 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8915 | if (touched) |
| 8916 | return maxchar; |
| 8917 | else |
| 8918 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8919 | } |
| 8920 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8921 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8922 | fixcapitalize(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8923 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8924 | /* No need to call PyUnicode_READY(self) because fixup() which does it. */ |
| 8925 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 8926 | const int kind = PyUnicode_KIND(self); |
| 8927 | void *data = PyUnicode_DATA(self); |
| 8928 | int touched = 0; |
| 8929 | Py_UCS4 maxchar = 0; |
| 8930 | Py_ssize_t i = 0; |
| 8931 | Py_UCS4 ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8932 | |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 8933 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8934 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8935 | |
| 8936 | ch = PyUnicode_READ(kind, data, i); |
| 8937 | if (!Py_UNICODE_ISUPPER(ch)) { |
| 8938 | maxchar = Py_UNICODE_TOUPPER(ch); |
| 8939 | PyUnicode_WRITE(kind, data, i, maxchar); |
| 8940 | touched = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8941 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8942 | ++i; |
| 8943 | for(; i < len; ++i) { |
| 8944 | ch = PyUnicode_READ(kind, data, i); |
| 8945 | if (!Py_UNICODE_ISLOWER(ch)) { |
| 8946 | const Py_UCS4 lo = Py_UNICODE_TOLOWER(ch); |
| 8947 | if (lo > maxchar) |
| 8948 | maxchar = lo; |
| 8949 | PyUnicode_WRITE(kind, data, i, lo); |
| 8950 | touched = 1; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 8951 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8952 | else if (ch > maxchar) |
| 8953 | maxchar = ch; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 8954 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8955 | |
| 8956 | if (touched) |
| 8957 | return maxchar; |
| 8958 | else |
| 8959 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8960 | } |
| 8961 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8962 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8963 | fixtitle(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8964 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8965 | /* No need to call PyUnicode_READY(self) because fixup() which does it. */ |
| 8966 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 8967 | const int kind = PyUnicode_KIND(self); |
| 8968 | void *data = PyUnicode_DATA(self); |
| 8969 | Py_UCS4 maxchar = 0; |
| 8970 | Py_ssize_t i = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8971 | int previous_is_cased; |
| 8972 | |
| 8973 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8974 | if (len == 1) { |
| 8975 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 8976 | const Py_UCS4 ti = Py_UNICODE_TOTITLE(ch); |
| 8977 | if (ti != ch) { |
| 8978 | PyUnicode_WRITE(kind, data, i, ti); |
| 8979 | return ti; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8980 | } |
| 8981 | else |
| 8982 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8983 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8984 | previous_is_cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8985 | for(; i < len; ++i) { |
| 8986 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 8987 | Py_UCS4 nu; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8988 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8989 | if (previous_is_cased) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8990 | nu = Py_UNICODE_TOLOWER(ch); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8991 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8992 | nu = Py_UNICODE_TOTITLE(ch); |
| 8993 | |
| 8994 | if (nu > maxchar) |
| 8995 | maxchar = nu; |
| 8996 | PyUnicode_WRITE(kind, data, i, nu); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8997 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8998 | if (Py_UNICODE_ISLOWER(ch) || |
| 8999 | Py_UNICODE_ISUPPER(ch) || |
| 9000 | Py_UNICODE_ISTITLE(ch)) |
| 9001 | previous_is_cased = 1; |
| 9002 | else |
| 9003 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9004 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9005 | return maxchar; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9006 | } |
| 9007 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9008 | PyObject * |
| 9009 | PyUnicode_Join(PyObject *separator, PyObject *seq) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9010 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9011 | PyObject *sep = NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9012 | Py_ssize_t seplen = 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9013 | PyObject *res = NULL; /* the result */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9014 | PyObject *fseq; /* PySequence_Fast(seq) */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9015 | Py_ssize_t seqlen; /* len(fseq) -- number of items in sequence */ |
| 9016 | PyObject **items; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9017 | PyObject *item; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9018 | Py_ssize_t sz, i, res_offset; |
| 9019 | Py_UCS4 maxchar = 0; |
| 9020 | Py_UCS4 item_maxchar; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9021 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9022 | fseq = PySequence_Fast(seq, ""); |
| 9023 | if (fseq == NULL) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9024 | return NULL; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9025 | } |
| 9026 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9027 | /* NOTE: the following code can't call back into Python code, |
| 9028 | * so we are sure that fseq won't be mutated. |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 9029 | */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9030 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9031 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 9032 | /* If empty sequence, return u"". */ |
| 9033 | if (seqlen == 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9034 | res = PyUnicode_New(0, 0); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9035 | goto Done; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9036 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9037 | items = PySequence_Fast_ITEMS(fseq); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9038 | /* If singleton sequence with an exact Unicode, return that. */ |
| 9039 | if (seqlen == 1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9040 | item = items[0]; |
| 9041 | if (PyUnicode_CheckExact(item)) { |
| 9042 | Py_INCREF(item); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9043 | res = item; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9044 | goto Done; |
| 9045 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9046 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9047 | else { |
| 9048 | /* Set up sep and seplen */ |
| 9049 | if (separator == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9050 | /* fall back to a blank space separator */ |
| 9051 | sep = PyUnicode_FromOrdinal(' '); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 9052 | if (!sep) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9053 | goto onError; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9054 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9055 | else { |
| 9056 | if (!PyUnicode_Check(separator)) { |
| 9057 | PyErr_Format(PyExc_TypeError, |
| 9058 | "separator: expected str instance," |
| 9059 | " %.80s found", |
| 9060 | Py_TYPE(separator)->tp_name); |
| 9061 | goto onError; |
| 9062 | } |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 9063 | if (PyUnicode_READY(separator)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9064 | goto onError; |
| 9065 | sep = separator; |
| 9066 | seplen = PyUnicode_GET_LENGTH(separator); |
| 9067 | maxchar = PyUnicode_MAX_CHAR_VALUE(separator); |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 9068 | /* inc refcount to keep this code path symmetric with the |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9069 | above case of a blank separator */ |
| 9070 | Py_INCREF(sep); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9071 | } |
| 9072 | } |
| 9073 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9074 | /* There are at least two things to join, or else we have a subclass |
| 9075 | * of str in the sequence. |
| 9076 | * Do a pre-pass to figure out the total amount of space we'll |
| 9077 | * need (sz), and see whether all argument are strings. |
| 9078 | */ |
| 9079 | sz = 0; |
| 9080 | for (i = 0; i < seqlen; i++) { |
| 9081 | const Py_ssize_t old_sz = sz; |
| 9082 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9083 | if (!PyUnicode_Check(item)) { |
| 9084 | PyErr_Format(PyExc_TypeError, |
| 9085 | "sequence item %zd: expected str instance," |
| 9086 | " %.80s found", |
| 9087 | i, Py_TYPE(item)->tp_name); |
| 9088 | goto onError; |
| 9089 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9090 | if (PyUnicode_READY(item) == -1) |
| 9091 | goto onError; |
| 9092 | sz += PyUnicode_GET_LENGTH(item); |
| 9093 | item_maxchar = PyUnicode_MAX_CHAR_VALUE(item); |
| 9094 | if (item_maxchar > maxchar) |
| 9095 | maxchar = item_maxchar; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9096 | if (i != 0) |
| 9097 | sz += seplen; |
| 9098 | if (sz < old_sz || sz > PY_SSIZE_T_MAX) { |
| 9099 | PyErr_SetString(PyExc_OverflowError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9100 | "join() result is too long for a Python string"); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9101 | goto onError; |
| 9102 | } |
| 9103 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9104 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9105 | res = PyUnicode_New(sz, maxchar); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9106 | if (res == NULL) |
| 9107 | goto onError; |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 9108 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9109 | /* Catenate everything. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9110 | for (i = 0, res_offset = 0; i < seqlen; ++i) { |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 9111 | Py_ssize_t itemlen, copied; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9112 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9113 | /* Copy item, and maybe the separator. */ |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 9114 | if (i && seplen != 0) { |
| 9115 | copied = PyUnicode_CopyCharacters(res, res_offset, |
| 9116 | sep, 0, seplen); |
| 9117 | if (copied < 0) |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9118 | goto onError; |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 9119 | #ifdef Py_DEBUG |
| 9120 | res_offset += copied; |
| 9121 | #else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9122 | res_offset += seplen; |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 9123 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9124 | } |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 9125 | itemlen = PyUnicode_GET_LENGTH(item); |
| 9126 | if (itemlen != 0) { |
| 9127 | copied = PyUnicode_CopyCharacters(res, res_offset, |
| 9128 | item, 0, itemlen); |
| 9129 | if (copied < 0) |
| 9130 | goto onError; |
| 9131 | #ifdef Py_DEBUG |
| 9132 | res_offset += copied; |
| 9133 | #else |
| 9134 | res_offset += itemlen; |
| 9135 | #endif |
| 9136 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9137 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9138 | assert(res_offset == PyUnicode_GET_LENGTH(res)); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9139 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9140 | Done: |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9141 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9142 | Py_XDECREF(sep); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 9143 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9144 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9145 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9146 | onError: |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9147 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9148 | Py_XDECREF(sep); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9149 | Py_XDECREF(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9150 | return NULL; |
| 9151 | } |
| 9152 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9153 | #define FILL(kind, data, value, start, length) \ |
| 9154 | do { \ |
| 9155 | Py_ssize_t i_ = 0; \ |
| 9156 | assert(kind != PyUnicode_WCHAR_KIND); \ |
| 9157 | switch ((kind)) { \ |
| 9158 | case PyUnicode_1BYTE_KIND: { \ |
| 9159 | unsigned char * to_ = (unsigned char *)((data)) + (start); \ |
| 9160 | memset(to_, (unsigned char)value, length); \ |
| 9161 | break; \ |
| 9162 | } \ |
| 9163 | case PyUnicode_2BYTE_KIND: { \ |
| 9164 | Py_UCS2 * to_ = (Py_UCS2 *)((data)) + (start); \ |
| 9165 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 9166 | break; \ |
| 9167 | } \ |
| 9168 | default: { \ |
| 9169 | Py_UCS4 * to_ = (Py_UCS4 *)((data)) + (start); \ |
| 9170 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 9171 | break; \ |
| 9172 | } \ |
| 9173 | } \ |
| 9174 | } while (0) |
| 9175 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9176 | static PyObject * |
| 9177 | pad(PyObject *self, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9178 | Py_ssize_t left, |
| 9179 | Py_ssize_t right, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9180 | Py_UCS4 fill) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9181 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9182 | PyObject *u; |
| 9183 | Py_UCS4 maxchar; |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9184 | int kind; |
| 9185 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9186 | |
| 9187 | if (left < 0) |
| 9188 | left = 0; |
| 9189 | if (right < 0) |
| 9190 | right = 0; |
| 9191 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 9192 | if (left == 0 && right == 0 && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9193 | Py_INCREF(self); |
| 9194 | return self; |
| 9195 | } |
| 9196 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9197 | if (left > PY_SSIZE_T_MAX - _PyUnicode_LENGTH(self) || |
| 9198 | right > PY_SSIZE_T_MAX - (left + _PyUnicode_LENGTH(self))) { |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 9199 | PyErr_SetString(PyExc_OverflowError, "padded string is too long"); |
| 9200 | return NULL; |
| 9201 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9202 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
| 9203 | if (fill > maxchar) |
| 9204 | maxchar = fill; |
| 9205 | u = PyUnicode_New(left + _PyUnicode_LENGTH(self) + right, maxchar); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9206 | if (!u) |
| 9207 | return NULL; |
| 9208 | |
| 9209 | kind = PyUnicode_KIND(u); |
| 9210 | data = PyUnicode_DATA(u); |
| 9211 | if (left) |
| 9212 | FILL(kind, data, fill, 0, left); |
| 9213 | if (right) |
| 9214 | FILL(kind, data, fill, left + _PyUnicode_LENGTH(self), right); |
Victor Stinner | 157f83f | 2011-09-28 21:41:31 +0200 | [diff] [blame] | 9215 | if (PyUnicode_CopyCharacters(u, left, |
| 9216 | (PyObject*)self, 0, |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9217 | _PyUnicode_LENGTH(self)) < 0) |
| 9218 | { |
| 9219 | Py_DECREF(u); |
| 9220 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9221 | } |
| 9222 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 9223 | assert(_PyUnicode_CheckConsistency(u, 1)); |
| 9224 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9225 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9226 | #undef FILL |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9227 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9228 | PyObject * |
| 9229 | PyUnicode_Splitlines(PyObject *string, int keepends) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9230 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9231 | PyObject *list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9232 | |
| 9233 | string = PyUnicode_FromObject(string); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9234 | if (string == NULL || PyUnicode_READY(string) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9235 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9236 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9237 | switch(PyUnicode_KIND(string)) { |
| 9238 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9239 | if (PyUnicode_IS_ASCII(string)) |
| 9240 | list = asciilib_splitlines( |
| 9241 | (PyObject*) string, PyUnicode_1BYTE_DATA(string), |
| 9242 | PyUnicode_GET_LENGTH(string), keepends); |
| 9243 | else |
| 9244 | list = ucs1lib_splitlines( |
| 9245 | (PyObject*) string, PyUnicode_1BYTE_DATA(string), |
| 9246 | PyUnicode_GET_LENGTH(string), keepends); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9247 | break; |
| 9248 | case PyUnicode_2BYTE_KIND: |
| 9249 | list = ucs2lib_splitlines( |
| 9250 | (PyObject*) string, PyUnicode_2BYTE_DATA(string), |
| 9251 | PyUnicode_GET_LENGTH(string), keepends); |
| 9252 | break; |
| 9253 | case PyUnicode_4BYTE_KIND: |
| 9254 | list = ucs4lib_splitlines( |
| 9255 | (PyObject*) string, PyUnicode_4BYTE_DATA(string), |
| 9256 | PyUnicode_GET_LENGTH(string), keepends); |
| 9257 | break; |
| 9258 | default: |
| 9259 | assert(0); |
| 9260 | list = 0; |
| 9261 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9262 | Py_DECREF(string); |
| 9263 | return list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9264 | } |
| 9265 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9266 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9267 | split(PyObject *self, |
| 9268 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9269 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9270 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9271 | int kind1, kind2, kind; |
| 9272 | void *buf1, *buf2; |
| 9273 | Py_ssize_t len1, len2; |
| 9274 | PyObject* out; |
| 9275 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9276 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9277 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9278 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9279 | if (PyUnicode_READY(self) == -1) |
| 9280 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9281 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9282 | if (substring == NULL) |
| 9283 | switch(PyUnicode_KIND(self)) { |
| 9284 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9285 | if (PyUnicode_IS_ASCII(self)) |
| 9286 | return asciilib_split_whitespace( |
| 9287 | (PyObject*) self, PyUnicode_1BYTE_DATA(self), |
| 9288 | PyUnicode_GET_LENGTH(self), maxcount |
| 9289 | ); |
| 9290 | else |
| 9291 | return ucs1lib_split_whitespace( |
| 9292 | (PyObject*) self, PyUnicode_1BYTE_DATA(self), |
| 9293 | PyUnicode_GET_LENGTH(self), maxcount |
| 9294 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9295 | case PyUnicode_2BYTE_KIND: |
| 9296 | return ucs2lib_split_whitespace( |
| 9297 | (PyObject*) self, PyUnicode_2BYTE_DATA(self), |
| 9298 | PyUnicode_GET_LENGTH(self), maxcount |
| 9299 | ); |
| 9300 | case PyUnicode_4BYTE_KIND: |
| 9301 | return ucs4lib_split_whitespace( |
| 9302 | (PyObject*) self, PyUnicode_4BYTE_DATA(self), |
| 9303 | PyUnicode_GET_LENGTH(self), maxcount |
| 9304 | ); |
| 9305 | default: |
| 9306 | assert(0); |
| 9307 | return NULL; |
| 9308 | } |
| 9309 | |
| 9310 | if (PyUnicode_READY(substring) == -1) |
| 9311 | return NULL; |
| 9312 | |
| 9313 | kind1 = PyUnicode_KIND(self); |
| 9314 | kind2 = PyUnicode_KIND(substring); |
| 9315 | kind = kind1 > kind2 ? kind1 : kind2; |
| 9316 | buf1 = PyUnicode_DATA(self); |
| 9317 | buf2 = PyUnicode_DATA(substring); |
| 9318 | if (kind1 != kind) |
| 9319 | buf1 = _PyUnicode_AsKind((PyObject*)self, kind); |
| 9320 | if (!buf1) |
| 9321 | return NULL; |
| 9322 | if (kind2 != kind) |
| 9323 | buf2 = _PyUnicode_AsKind((PyObject*)substring, kind); |
| 9324 | if (!buf2) { |
| 9325 | if (kind1 != kind) PyMem_Free(buf1); |
| 9326 | return NULL; |
| 9327 | } |
| 9328 | len1 = PyUnicode_GET_LENGTH(self); |
| 9329 | len2 = PyUnicode_GET_LENGTH(substring); |
| 9330 | |
| 9331 | switch(kind) { |
| 9332 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9333 | if (PyUnicode_IS_ASCII(self) && PyUnicode_IS_ASCII(substring)) |
| 9334 | out = asciilib_split( |
| 9335 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
| 9336 | else |
| 9337 | out = ucs1lib_split( |
| 9338 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9339 | break; |
| 9340 | case PyUnicode_2BYTE_KIND: |
| 9341 | out = ucs2lib_split( |
| 9342 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
| 9343 | break; |
| 9344 | case PyUnicode_4BYTE_KIND: |
| 9345 | out = ucs4lib_split( |
| 9346 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
| 9347 | break; |
| 9348 | default: |
| 9349 | out = NULL; |
| 9350 | } |
| 9351 | if (kind1 != kind) |
| 9352 | PyMem_Free(buf1); |
| 9353 | if (kind2 != kind) |
| 9354 | PyMem_Free(buf2); |
| 9355 | return out; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9356 | } |
| 9357 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9358 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9359 | rsplit(PyObject *self, |
| 9360 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9361 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9362 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9363 | int kind1, kind2, kind; |
| 9364 | void *buf1, *buf2; |
| 9365 | Py_ssize_t len1, len2; |
| 9366 | PyObject* out; |
| 9367 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9368 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9369 | maxcount = PY_SSIZE_T_MAX; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9370 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9371 | if (PyUnicode_READY(self) == -1) |
| 9372 | return NULL; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9373 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9374 | if (substring == NULL) |
| 9375 | switch(PyUnicode_KIND(self)) { |
| 9376 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9377 | if (PyUnicode_IS_ASCII(self)) |
| 9378 | return asciilib_rsplit_whitespace( |
| 9379 | (PyObject*) self, PyUnicode_1BYTE_DATA(self), |
| 9380 | PyUnicode_GET_LENGTH(self), maxcount |
| 9381 | ); |
| 9382 | else |
| 9383 | return ucs1lib_rsplit_whitespace( |
| 9384 | (PyObject*) self, PyUnicode_1BYTE_DATA(self), |
| 9385 | PyUnicode_GET_LENGTH(self), maxcount |
| 9386 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9387 | case PyUnicode_2BYTE_KIND: |
| 9388 | return ucs2lib_rsplit_whitespace( |
| 9389 | (PyObject*) self, PyUnicode_2BYTE_DATA(self), |
| 9390 | PyUnicode_GET_LENGTH(self), maxcount |
| 9391 | ); |
| 9392 | case PyUnicode_4BYTE_KIND: |
| 9393 | return ucs4lib_rsplit_whitespace( |
| 9394 | (PyObject*) self, PyUnicode_4BYTE_DATA(self), |
| 9395 | PyUnicode_GET_LENGTH(self), maxcount |
| 9396 | ); |
| 9397 | default: |
| 9398 | assert(0); |
| 9399 | return NULL; |
| 9400 | } |
| 9401 | |
| 9402 | if (PyUnicode_READY(substring) == -1) |
| 9403 | return NULL; |
| 9404 | |
| 9405 | kind1 = PyUnicode_KIND(self); |
| 9406 | kind2 = PyUnicode_KIND(substring); |
| 9407 | kind = kind1 > kind2 ? kind1 : kind2; |
| 9408 | buf1 = PyUnicode_DATA(self); |
| 9409 | buf2 = PyUnicode_DATA(substring); |
| 9410 | if (kind1 != kind) |
| 9411 | buf1 = _PyUnicode_AsKind((PyObject*)self, kind); |
| 9412 | if (!buf1) |
| 9413 | return NULL; |
| 9414 | if (kind2 != kind) |
| 9415 | buf2 = _PyUnicode_AsKind((PyObject*)substring, kind); |
| 9416 | if (!buf2) { |
| 9417 | if (kind1 != kind) PyMem_Free(buf1); |
| 9418 | return NULL; |
| 9419 | } |
| 9420 | len1 = PyUnicode_GET_LENGTH(self); |
| 9421 | len2 = PyUnicode_GET_LENGTH(substring); |
| 9422 | |
| 9423 | switch(kind) { |
| 9424 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9425 | if (PyUnicode_IS_ASCII(self) && PyUnicode_IS_ASCII(substring)) |
| 9426 | out = asciilib_rsplit( |
| 9427 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
| 9428 | else |
| 9429 | out = ucs1lib_rsplit( |
| 9430 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9431 | break; |
| 9432 | case PyUnicode_2BYTE_KIND: |
| 9433 | out = ucs2lib_rsplit( |
| 9434 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
| 9435 | break; |
| 9436 | case PyUnicode_4BYTE_KIND: |
| 9437 | out = ucs4lib_rsplit( |
| 9438 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
| 9439 | break; |
| 9440 | default: |
| 9441 | out = NULL; |
| 9442 | } |
| 9443 | if (kind1 != kind) |
| 9444 | PyMem_Free(buf1); |
| 9445 | if (kind2 != kind) |
| 9446 | PyMem_Free(buf2); |
| 9447 | return out; |
| 9448 | } |
| 9449 | |
| 9450 | static Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9451 | anylib_find(int kind, PyObject *str1, void *buf1, Py_ssize_t len1, |
| 9452 | 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] | 9453 | { |
| 9454 | switch(kind) { |
| 9455 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9456 | if (PyUnicode_IS_ASCII(str1) && PyUnicode_IS_ASCII(str2)) |
| 9457 | return asciilib_find(buf1, len1, buf2, len2, offset); |
| 9458 | else |
| 9459 | return ucs1lib_find(buf1, len1, buf2, len2, offset); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9460 | case PyUnicode_2BYTE_KIND: |
| 9461 | return ucs2lib_find(buf1, len1, buf2, len2, offset); |
| 9462 | case PyUnicode_4BYTE_KIND: |
| 9463 | return ucs4lib_find(buf1, len1, buf2, len2, offset); |
| 9464 | } |
| 9465 | assert(0); |
| 9466 | return -1; |
| 9467 | } |
| 9468 | |
| 9469 | static Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9470 | anylib_count(int kind, PyObject *sstr, void* sbuf, Py_ssize_t slen, |
| 9471 | 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] | 9472 | { |
| 9473 | switch(kind) { |
| 9474 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9475 | if (PyUnicode_IS_ASCII(sstr) && PyUnicode_IS_ASCII(str1)) |
| 9476 | return asciilib_count(sbuf, slen, buf1, len1, maxcount); |
| 9477 | else |
| 9478 | return ucs1lib_count(sbuf, slen, buf1, len1, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9479 | case PyUnicode_2BYTE_KIND: |
| 9480 | return ucs2lib_count(sbuf, slen, buf1, len1, maxcount); |
| 9481 | case PyUnicode_4BYTE_KIND: |
| 9482 | return ucs4lib_count(sbuf, slen, buf1, len1, maxcount); |
| 9483 | } |
| 9484 | assert(0); |
| 9485 | return 0; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9486 | } |
| 9487 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9488 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9489 | replace(PyObject *self, PyObject *str1, |
| 9490 | PyObject *str2, Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9491 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9492 | PyObject *u; |
| 9493 | char *sbuf = PyUnicode_DATA(self); |
| 9494 | char *buf1 = PyUnicode_DATA(str1); |
| 9495 | char *buf2 = PyUnicode_DATA(str2); |
| 9496 | int srelease = 0, release1 = 0, release2 = 0; |
| 9497 | int skind = PyUnicode_KIND(self); |
| 9498 | int kind1 = PyUnicode_KIND(str1); |
| 9499 | int kind2 = PyUnicode_KIND(str2); |
| 9500 | Py_ssize_t slen = PyUnicode_GET_LENGTH(self); |
| 9501 | Py_ssize_t len1 = PyUnicode_GET_LENGTH(str1); |
| 9502 | Py_ssize_t len2 = PyUnicode_GET_LENGTH(str2); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9503 | |
| 9504 | if (maxcount < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9505 | maxcount = PY_SSIZE_T_MAX; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9506 | else if (maxcount == 0 || slen == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9507 | goto nothing; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9508 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9509 | if (skind < kind1) |
| 9510 | /* substring too wide to be present */ |
| 9511 | goto nothing; |
| 9512 | |
| 9513 | if (len1 == len2) { |
Antoine Pitrou | cbfdee3 | 2010-01-13 08:58:08 +0000 | [diff] [blame] | 9514 | Py_ssize_t i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9515 | /* same length */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9516 | if (len1 == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9517 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9518 | if (len1 == 1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9519 | /* replace characters */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9520 | Py_UCS4 u1, u2, maxchar; |
| 9521 | int mayshrink, rkind; |
| 9522 | u1 = PyUnicode_READ_CHAR(str1, 0); |
| 9523 | if (!findchar(sbuf, PyUnicode_KIND(self), |
| 9524 | slen, u1, 1)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9525 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9526 | u2 = PyUnicode_READ_CHAR(str2, 0); |
| 9527 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
| 9528 | /* Replacing u1 with u2 may cause a maxchar reduction in the |
| 9529 | result string. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9530 | if (u2 > maxchar) { |
| 9531 | maxchar = u2; |
| 9532 | mayshrink = 0; |
| 9533 | } |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 9534 | else |
| 9535 | mayshrink = maxchar > 127; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9536 | u = PyUnicode_New(slen, maxchar); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9537 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9538 | goto error; |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9539 | if (PyUnicode_CopyCharacters(u, 0, |
| 9540 | (PyObject*)self, 0, slen) < 0) |
| 9541 | { |
| 9542 | Py_DECREF(u); |
| 9543 | return NULL; |
| 9544 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9545 | rkind = PyUnicode_KIND(u); |
| 9546 | for (i = 0; i < PyUnicode_GET_LENGTH(u); i++) |
| 9547 | if (PyUnicode_READ(rkind, PyUnicode_DATA(u), i) == u1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9548 | if (--maxcount < 0) |
| 9549 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9550 | PyUnicode_WRITE(rkind, PyUnicode_DATA(u), i, u2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9551 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9552 | if (mayshrink) { |
| 9553 | PyObject *tmp = u; |
| 9554 | u = PyUnicode_FromKindAndData(rkind, PyUnicode_DATA(tmp), |
| 9555 | PyUnicode_GET_LENGTH(tmp)); |
| 9556 | Py_DECREF(tmp); |
| 9557 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9558 | } else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9559 | int rkind = skind; |
| 9560 | char *res; |
| 9561 | if (kind1 < rkind) { |
| 9562 | /* widen substring */ |
| 9563 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 9564 | if (!buf1) goto error; |
| 9565 | release1 = 1; |
| 9566 | } |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9567 | i = anylib_find(rkind, self, sbuf, slen, str1, buf1, len1, 0); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9568 | if (i < 0) |
| 9569 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9570 | if (rkind > kind2) { |
| 9571 | /* widen replacement */ |
| 9572 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 9573 | if (!buf2) goto error; |
| 9574 | release2 = 1; |
| 9575 | } |
| 9576 | else if (rkind < kind2) { |
| 9577 | /* widen self and buf1 */ |
| 9578 | rkind = kind2; |
| 9579 | if (release1) PyMem_Free(buf1); |
| 9580 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 9581 | if (!sbuf) goto error; |
| 9582 | srelease = 1; |
| 9583 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 9584 | if (!buf1) goto error; |
| 9585 | release1 = 1; |
| 9586 | } |
| 9587 | res = PyMem_Malloc(PyUnicode_KIND_SIZE(rkind, slen)); |
| 9588 | if (!res) { |
| 9589 | PyErr_NoMemory(); |
| 9590 | goto error; |
| 9591 | } |
| 9592 | memcpy(res, sbuf, PyUnicode_KIND_SIZE(rkind, slen)); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9593 | /* change everything in-place, starting with this one */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9594 | memcpy(res + PyUnicode_KIND_SIZE(rkind, i), |
| 9595 | buf2, |
| 9596 | PyUnicode_KIND_SIZE(rkind, len2)); |
| 9597 | i += len1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9598 | |
| 9599 | while ( --maxcount > 0) { |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9600 | i = anylib_find(rkind, self, |
| 9601 | sbuf+PyUnicode_KIND_SIZE(rkind, i), slen-i, |
| 9602 | str1, buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9603 | if (i == -1) |
| 9604 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9605 | memcpy(res + PyUnicode_KIND_SIZE(rkind, i), |
| 9606 | buf2, |
| 9607 | PyUnicode_KIND_SIZE(rkind, len2)); |
| 9608 | i += len1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9609 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9610 | |
| 9611 | u = PyUnicode_FromKindAndData(rkind, res, slen); |
| 9612 | PyMem_Free(res); |
| 9613 | if (!u) goto error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9614 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9615 | } else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9616 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9617 | Py_ssize_t n, i, j, ires; |
| 9618 | Py_ssize_t product, new_size; |
| 9619 | int rkind = skind; |
| 9620 | char *res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9621 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9622 | if (kind1 < rkind) { |
| 9623 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 9624 | if (!buf1) goto error; |
| 9625 | release1 = 1; |
| 9626 | } |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9627 | n = anylib_count(rkind, self, sbuf, slen, str1, buf1, len1, maxcount); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9628 | if (n == 0) |
| 9629 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9630 | if (kind2 < rkind) { |
| 9631 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 9632 | if (!buf2) goto error; |
| 9633 | release2 = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9634 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9635 | else if (kind2 > rkind) { |
| 9636 | rkind = kind2; |
| 9637 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 9638 | if (!sbuf) goto error; |
| 9639 | srelease = 1; |
| 9640 | if (release1) PyMem_Free(buf1); |
| 9641 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 9642 | if (!buf1) goto error; |
| 9643 | release1 = 1; |
| 9644 | } |
| 9645 | /* new_size = PyUnicode_GET_LENGTH(self) + n * (PyUnicode_GET_LENGTH(str2) - |
| 9646 | PyUnicode_GET_LENGTH(str1))); */ |
| 9647 | product = n * (len2-len1); |
| 9648 | if ((product / (len2-len1)) != n) { |
| 9649 | PyErr_SetString(PyExc_OverflowError, |
| 9650 | "replace string is too long"); |
| 9651 | goto error; |
| 9652 | } |
| 9653 | new_size = slen + product; |
| 9654 | if (new_size < 0 || new_size > (PY_SSIZE_T_MAX >> (rkind-1))) { |
| 9655 | PyErr_SetString(PyExc_OverflowError, |
| 9656 | "replace string is too long"); |
| 9657 | goto error; |
| 9658 | } |
| 9659 | res = PyMem_Malloc(PyUnicode_KIND_SIZE(rkind, new_size)); |
| 9660 | if (!res) |
| 9661 | goto error; |
| 9662 | ires = i = 0; |
| 9663 | if (len1 > 0) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9664 | while (n-- > 0) { |
| 9665 | /* look for next match */ |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9666 | j = anylib_find(rkind, self, |
| 9667 | sbuf + PyUnicode_KIND_SIZE(rkind, i), slen-i, |
| 9668 | str1, buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9669 | if (j == -1) |
| 9670 | break; |
| 9671 | else if (j > i) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9672 | /* copy unchanged part [i:j] */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9673 | memcpy(res + PyUnicode_KIND_SIZE(rkind, ires), |
| 9674 | sbuf + PyUnicode_KIND_SIZE(rkind, i), |
| 9675 | PyUnicode_KIND_SIZE(rkind, j-i)); |
| 9676 | ires += j - i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9677 | } |
| 9678 | /* copy substitution string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9679 | if (len2 > 0) { |
| 9680 | memcpy(res + PyUnicode_KIND_SIZE(rkind, ires), |
| 9681 | buf2, |
| 9682 | PyUnicode_KIND_SIZE(rkind, len2)); |
| 9683 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9684 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9685 | i = j + len1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9686 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9687 | if (i < slen) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9688 | /* copy tail [i:] */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9689 | memcpy(res + PyUnicode_KIND_SIZE(rkind, ires), |
| 9690 | sbuf + PyUnicode_KIND_SIZE(rkind, i), |
| 9691 | PyUnicode_KIND_SIZE(rkind, slen-i)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9692 | } else { |
| 9693 | /* interleave */ |
| 9694 | while (n > 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9695 | memcpy(res + PyUnicode_KIND_SIZE(rkind, ires), |
| 9696 | buf2, |
| 9697 | PyUnicode_KIND_SIZE(rkind, len2)); |
| 9698 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9699 | if (--n <= 0) |
| 9700 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9701 | memcpy(res + PyUnicode_KIND_SIZE(rkind, ires), |
| 9702 | sbuf + PyUnicode_KIND_SIZE(rkind, i), |
| 9703 | PyUnicode_KIND_SIZE(rkind, 1)); |
| 9704 | ires++; |
| 9705 | i++; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9706 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9707 | memcpy(res + PyUnicode_KIND_SIZE(rkind, ires), |
| 9708 | sbuf + PyUnicode_KIND_SIZE(rkind, i), |
| 9709 | PyUnicode_KIND_SIZE(rkind, slen-i)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9710 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9711 | u = PyUnicode_FromKindAndData(rkind, res, new_size); |
Martin v. Löwis | 0b1d348 | 2011-10-01 16:35:40 +0200 | [diff] [blame] | 9712 | PyMem_Free(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9713 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9714 | if (srelease) |
| 9715 | PyMem_FREE(sbuf); |
| 9716 | if (release1) |
| 9717 | PyMem_FREE(buf1); |
| 9718 | if (release2) |
| 9719 | PyMem_FREE(buf2); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 9720 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9721 | return u; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9722 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9723 | nothing: |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9724 | /* nothing to replace; return original string (when possible) */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9725 | if (srelease) |
| 9726 | PyMem_FREE(sbuf); |
| 9727 | if (release1) |
| 9728 | PyMem_FREE(buf1); |
| 9729 | if (release2) |
| 9730 | PyMem_FREE(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9731 | if (PyUnicode_CheckExact(self)) { |
| 9732 | Py_INCREF(self); |
| 9733 | return (PyObject *) self; |
| 9734 | } |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 9735 | return PyUnicode_Copy(self); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9736 | error: |
| 9737 | if (srelease && sbuf) |
| 9738 | PyMem_FREE(sbuf); |
| 9739 | if (release1 && buf1) |
| 9740 | PyMem_FREE(buf1); |
| 9741 | if (release2 && buf2) |
| 9742 | PyMem_FREE(buf2); |
| 9743 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9744 | } |
| 9745 | |
| 9746 | /* --- Unicode Object Methods --------------------------------------------- */ |
| 9747 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9748 | PyDoc_STRVAR(title__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9749 | "S.title() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9750 | \n\ |
| 9751 | 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] | 9752 | characters, all remaining cased characters have lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9753 | |
| 9754 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9755 | unicode_title(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9756 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9757 | return fixup(self, fixtitle); |
| 9758 | } |
| 9759 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9760 | PyDoc_STRVAR(capitalize__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9761 | "S.capitalize() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9762 | \n\ |
| 9763 | 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] | 9764 | have upper case and the rest lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9765 | |
| 9766 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9767 | unicode_capitalize(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9768 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9769 | return fixup(self, fixcapitalize); |
| 9770 | } |
| 9771 | |
| 9772 | #if 0 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9773 | PyDoc_STRVAR(capwords__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9774 | "S.capwords() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9775 | \n\ |
| 9776 | 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] | 9777 | normalized whitespace (all whitespace strings are replaced by ' ')."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9778 | |
| 9779 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 9780 | unicode_capwords(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9781 | { |
| 9782 | PyObject *list; |
| 9783 | PyObject *item; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9784 | Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9785 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9786 | /* Split into words */ |
| 9787 | list = split(self, NULL, -1); |
| 9788 | if (!list) |
| 9789 | return NULL; |
| 9790 | |
| 9791 | /* Capitalize each word */ |
| 9792 | for (i = 0; i < PyList_GET_SIZE(list); i++) { |
| 9793 | item = fixup((PyUnicodeObject *)PyList_GET_ITEM(list, i), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9794 | fixcapitalize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9795 | if (item == NULL) |
| 9796 | goto onError; |
| 9797 | Py_DECREF(PyList_GET_ITEM(list, i)); |
| 9798 | PyList_SET_ITEM(list, i, item); |
| 9799 | } |
| 9800 | |
| 9801 | /* Join the words to form a new string */ |
| 9802 | item = PyUnicode_Join(NULL, list); |
| 9803 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9804 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9805 | Py_DECREF(list); |
| 9806 | return (PyObject *)item; |
| 9807 | } |
| 9808 | #endif |
| 9809 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 9810 | /* Argument converter. Coerces to a single unicode character */ |
| 9811 | |
| 9812 | static int |
| 9813 | convert_uc(PyObject *obj, void *addr) |
| 9814 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9815 | Py_UCS4 *fillcharloc = (Py_UCS4 *)addr; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9816 | PyObject *uniobj; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 9817 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9818 | uniobj = PyUnicode_FromObject(obj); |
| 9819 | if (uniobj == NULL) { |
| 9820 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9821 | "The fill character cannot be converted to Unicode"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9822 | return 0; |
| 9823 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9824 | if (PyUnicode_GET_LENGTH(uniobj) != 1) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9825 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9826 | "The fill character must be exactly one character long"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9827 | Py_DECREF(uniobj); |
| 9828 | return 0; |
| 9829 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9830 | *fillcharloc = PyUnicode_READ_CHAR(uniobj, 0); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9831 | Py_DECREF(uniobj); |
| 9832 | return 1; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 9833 | } |
| 9834 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9835 | PyDoc_STRVAR(center__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9836 | "S.center(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9837 | \n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 9838 | Return S centered in a string of length width. Padding is\n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 9839 | done using the specified fill character (default is a space)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9840 | |
| 9841 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9842 | unicode_center(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9843 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9844 | Py_ssize_t marg, left; |
| 9845 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9846 | Py_UCS4 fillchar = ' '; |
| 9847 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 9848 | 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] | 9849 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9850 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 9851 | if (PyUnicode_READY(self) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9852 | return NULL; |
| 9853 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9854 | if (_PyUnicode_LENGTH(self) >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9855 | Py_INCREF(self); |
| 9856 | return (PyObject*) self; |
| 9857 | } |
| 9858 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9859 | marg = width - _PyUnicode_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9860 | left = marg / 2 + (marg & width & 1); |
| 9861 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9862 | return pad(self, left, marg - left, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9863 | } |
| 9864 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 9865 | #if 0 |
| 9866 | |
| 9867 | /* This code should go into some future Unicode collation support |
| 9868 | module. The basic comparison should compare ordinals on a naive |
Georg Brandl | c6c3178 | 2009-06-08 13:41:29 +0000 | [diff] [blame] | 9869 | basis (this is what Java does and thus Jython too). */ |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 9870 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9871 | /* speedy UTF-16 code point order comparison */ |
| 9872 | /* gleaned from: */ |
| 9873 | /* http://www-4.ibm.com/software/developer/library/utf16.html?dwzone=unicode */ |
| 9874 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 9875 | static short utf16Fixup[32] = |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9876 | { |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9877 | 0, 0, 0, 0, 0, 0, 0, 0, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9878 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 9879 | 0, 0, 0, 0, 0, 0, 0, 0, |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 9880 | 0, 0, 0, 0x2000, -0x800, -0x800, -0x800, -0x800 |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9881 | }; |
| 9882 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9883 | static int |
| 9884 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 9885 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9886 | Py_ssize_t len1, len2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9887 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9888 | Py_UNICODE *s1 = str1->str; |
| 9889 | Py_UNICODE *s2 = str2->str; |
| 9890 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9891 | len1 = str1->_base._base.length; |
| 9892 | len2 = str2->_base._base.length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9893 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9894 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9895 | Py_UNICODE c1, c2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9896 | |
| 9897 | c1 = *s1++; |
| 9898 | c2 = *s2++; |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 9899 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9900 | if (c1 > (1<<11) * 26) |
| 9901 | c1 += utf16Fixup[c1>>11]; |
| 9902 | if (c2 > (1<<11) * 26) |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9903 | c2 += utf16Fixup[c2>>11]; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9904 | /* now c1 and c2 are in UTF-32-compatible order */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 9905 | |
| 9906 | if (c1 != c2) |
| 9907 | return (c1 < c2) ? -1 : 1; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9908 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9909 | len1--; len2--; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9910 | } |
| 9911 | |
| 9912 | return (len1 < len2) ? -1 : (len1 != len2); |
| 9913 | } |
| 9914 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 9915 | #else |
| 9916 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9917 | /* This function assumes that str1 and str2 are readied by the caller. */ |
| 9918 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 9919 | static int |
| 9920 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 9921 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9922 | int kind1, kind2; |
| 9923 | void *data1, *data2; |
| 9924 | Py_ssize_t len1, len2, i; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 9925 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9926 | kind1 = PyUnicode_KIND(str1); |
| 9927 | kind2 = PyUnicode_KIND(str2); |
| 9928 | data1 = PyUnicode_DATA(str1); |
| 9929 | data2 = PyUnicode_DATA(str2); |
| 9930 | len1 = PyUnicode_GET_LENGTH(str1); |
| 9931 | len2 = PyUnicode_GET_LENGTH(str2); |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 9932 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9933 | for (i = 0; i < len1 && i < len2; ++i) { |
| 9934 | Py_UCS4 c1, c2; |
| 9935 | c1 = PyUnicode_READ(kind1, data1, i); |
| 9936 | c2 = PyUnicode_READ(kind2, data2, i); |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 9937 | |
| 9938 | if (c1 != c2) |
| 9939 | return (c1 < c2) ? -1 : 1; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 9940 | } |
| 9941 | |
| 9942 | return (len1 < len2) ? -1 : (len1 != len2); |
| 9943 | } |
| 9944 | |
| 9945 | #endif |
| 9946 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9947 | int |
| 9948 | PyUnicode_Compare(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9949 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9950 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 9951 | if (PyUnicode_READY(left) == -1 || |
| 9952 | PyUnicode_READY(right) == -1) |
| 9953 | return -1; |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 9954 | return unicode_compare((PyUnicodeObject *)left, |
| 9955 | (PyUnicodeObject *)right); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9956 | } |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 9957 | PyErr_Format(PyExc_TypeError, |
| 9958 | "Can't compare %.100s and %.100s", |
| 9959 | left->ob_type->tp_name, |
| 9960 | right->ob_type->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9961 | return -1; |
| 9962 | } |
| 9963 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9964 | int |
| 9965 | PyUnicode_CompareWithASCIIString(PyObject* uni, const char* str) |
| 9966 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9967 | Py_ssize_t i; |
| 9968 | int kind; |
| 9969 | void *data; |
| 9970 | Py_UCS4 chr; |
| 9971 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 9972 | assert(_PyUnicode_CHECK(uni)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9973 | if (PyUnicode_READY(uni) == -1) |
| 9974 | return -1; |
| 9975 | kind = PyUnicode_KIND(uni); |
| 9976 | data = PyUnicode_DATA(uni); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9977 | /* Compare Unicode string and source character set string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9978 | for (i = 0; (chr = PyUnicode_READ(kind, data, i)) && str[i]; i++) |
| 9979 | if (chr != str[i]) |
| 9980 | return (chr < (unsigned char)(str[i])) ? -1 : 1; |
Benjamin Peterson | 8667a9b | 2010-01-09 21:45:28 +0000 | [diff] [blame] | 9981 | /* This check keeps Python strings that end in '\0' from comparing equal |
| 9982 | to C strings identical up to that point. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9983 | if (PyUnicode_GET_LENGTH(uni) != i || chr) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9984 | return 1; /* uni is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9985 | if (str[i]) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9986 | return -1; /* str is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9987 | return 0; |
| 9988 | } |
| 9989 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 9990 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9991 | #define TEST_COND(cond) \ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9992 | ((cond) ? Py_True : Py_False) |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 9993 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9994 | PyObject * |
| 9995 | PyUnicode_RichCompare(PyObject *left, PyObject *right, int op) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 9996 | { |
| 9997 | int result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9998 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 9999 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 10000 | PyObject *v; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10001 | if (PyUnicode_READY(left) == -1 || |
| 10002 | PyUnicode_READY(right) == -1) |
| 10003 | return NULL; |
| 10004 | if (PyUnicode_GET_LENGTH(left) != PyUnicode_GET_LENGTH(right) || |
| 10005 | PyUnicode_KIND(left) != PyUnicode_KIND(right)) { |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10006 | if (op == Py_EQ) { |
| 10007 | Py_INCREF(Py_False); |
| 10008 | return Py_False; |
| 10009 | } |
| 10010 | if (op == Py_NE) { |
| 10011 | Py_INCREF(Py_True); |
| 10012 | return Py_True; |
| 10013 | } |
| 10014 | } |
| 10015 | if (left == right) |
| 10016 | result = 0; |
| 10017 | else |
| 10018 | result = unicode_compare((PyUnicodeObject *)left, |
| 10019 | (PyUnicodeObject *)right); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10020 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10021 | /* Convert the return value to a Boolean */ |
| 10022 | switch (op) { |
| 10023 | case Py_EQ: |
| 10024 | v = TEST_COND(result == 0); |
| 10025 | break; |
| 10026 | case Py_NE: |
| 10027 | v = TEST_COND(result != 0); |
| 10028 | break; |
| 10029 | case Py_LE: |
| 10030 | v = TEST_COND(result <= 0); |
| 10031 | break; |
| 10032 | case Py_GE: |
| 10033 | v = TEST_COND(result >= 0); |
| 10034 | break; |
| 10035 | case Py_LT: |
| 10036 | v = TEST_COND(result == -1); |
| 10037 | break; |
| 10038 | case Py_GT: |
| 10039 | v = TEST_COND(result == 1); |
| 10040 | break; |
| 10041 | default: |
| 10042 | PyErr_BadArgument(); |
| 10043 | return NULL; |
| 10044 | } |
| 10045 | Py_INCREF(v); |
| 10046 | return v; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10047 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10048 | |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 10049 | Py_RETURN_NOTIMPLEMENTED; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10050 | } |
| 10051 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10052 | int |
| 10053 | PyUnicode_Contains(PyObject *container, PyObject *element) |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10054 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10055 | PyObject *str, *sub; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10056 | int kind1, kind2, kind; |
| 10057 | void *buf1, *buf2; |
| 10058 | Py_ssize_t len1, len2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10059 | int result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10060 | |
| 10061 | /* Coerce the two arguments */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10062 | sub = PyUnicode_FromObject(element); |
| 10063 | if (!sub) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10064 | PyErr_Format(PyExc_TypeError, |
| 10065 | "'in <string>' requires string as left operand, not %s", |
| 10066 | element->ob_type->tp_name); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10067 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10068 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10069 | if (PyUnicode_READY(sub) == -1) |
| 10070 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10071 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10072 | str = PyUnicode_FromObject(container); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 10073 | if (!str || PyUnicode_READY(str) == -1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10074 | Py_DECREF(sub); |
| 10075 | return -1; |
| 10076 | } |
| 10077 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10078 | kind1 = PyUnicode_KIND(str); |
| 10079 | kind2 = PyUnicode_KIND(sub); |
| 10080 | kind = kind1 > kind2 ? kind1 : kind2; |
| 10081 | buf1 = PyUnicode_DATA(str); |
| 10082 | buf2 = PyUnicode_DATA(sub); |
| 10083 | if (kind1 != kind) |
| 10084 | buf1 = _PyUnicode_AsKind((PyObject*)str, kind); |
| 10085 | if (!buf1) { |
| 10086 | Py_DECREF(sub); |
| 10087 | return -1; |
| 10088 | } |
| 10089 | if (kind2 != kind) |
| 10090 | buf2 = _PyUnicode_AsKind((PyObject*)sub, kind); |
| 10091 | if (!buf2) { |
| 10092 | Py_DECREF(sub); |
| 10093 | if (kind1 != kind) PyMem_Free(buf1); |
| 10094 | return -1; |
| 10095 | } |
| 10096 | len1 = PyUnicode_GET_LENGTH(str); |
| 10097 | len2 = PyUnicode_GET_LENGTH(sub); |
| 10098 | |
| 10099 | switch(kind) { |
| 10100 | case PyUnicode_1BYTE_KIND: |
| 10101 | result = ucs1lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10102 | break; |
| 10103 | case PyUnicode_2BYTE_KIND: |
| 10104 | result = ucs2lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10105 | break; |
| 10106 | case PyUnicode_4BYTE_KIND: |
| 10107 | result = ucs4lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10108 | break; |
| 10109 | default: |
| 10110 | result = -1; |
| 10111 | assert(0); |
| 10112 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10113 | |
| 10114 | Py_DECREF(str); |
| 10115 | Py_DECREF(sub); |
| 10116 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10117 | if (kind1 != kind) |
| 10118 | PyMem_Free(buf1); |
| 10119 | if (kind2 != kind) |
| 10120 | PyMem_Free(buf2); |
| 10121 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10122 | return result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10123 | } |
| 10124 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10125 | /* Concat to string or Unicode object giving a new Unicode object. */ |
| 10126 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10127 | PyObject * |
| 10128 | PyUnicode_Concat(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10129 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10130 | PyObject *u = NULL, *v = NULL, *w; |
| 10131 | Py_UCS4 maxchar; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10132 | |
| 10133 | /* Coerce the two arguments */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10134 | u = PyUnicode_FromObject(left); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10135 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10136 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10137 | v = PyUnicode_FromObject(right); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10138 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10139 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10140 | |
| 10141 | /* Shortcuts */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 10142 | if (v == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10143 | Py_DECREF(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10144 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10145 | } |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 10146 | if (u == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10147 | Py_DECREF(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10148 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10149 | } |
| 10150 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10151 | maxchar = PyUnicode_MAX_CHAR_VALUE(u); |
Victor Stinner | ff9e50f | 2011-09-28 22:17:19 +0200 | [diff] [blame] | 10152 | maxchar = Py_MAX(maxchar, PyUnicode_MAX_CHAR_VALUE(v)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10153 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10154 | /* Concat the two Unicode strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10155 | w = PyUnicode_New( |
| 10156 | PyUnicode_GET_LENGTH(u) + PyUnicode_GET_LENGTH(v), |
| 10157 | maxchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10158 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10159 | goto onError; |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 10160 | if (PyUnicode_CopyCharacters(w, 0, u, 0, PyUnicode_GET_LENGTH(u)) < 0) |
| 10161 | goto onError; |
Victor Stinner | 157f83f | 2011-09-28 21:41:31 +0200 | [diff] [blame] | 10162 | if (PyUnicode_CopyCharacters(w, PyUnicode_GET_LENGTH(u), |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 10163 | v, 0, |
| 10164 | PyUnicode_GET_LENGTH(v)) < 0) |
| 10165 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10166 | Py_DECREF(u); |
| 10167 | Py_DECREF(v); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10168 | assert(_PyUnicode_CheckConsistency(w, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10169 | return w; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10170 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10171 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10172 | Py_XDECREF(u); |
| 10173 | Py_XDECREF(v); |
| 10174 | return NULL; |
| 10175 | } |
| 10176 | |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10177 | static void |
| 10178 | unicode_append_inplace(PyObject **p_left, PyObject *right) |
| 10179 | { |
| 10180 | Py_ssize_t left_len, right_len, new_len; |
| 10181 | #ifdef Py_DEBUG |
| 10182 | Py_ssize_t copied; |
| 10183 | #endif |
| 10184 | |
| 10185 | assert(PyUnicode_IS_READY(*p_left)); |
| 10186 | assert(PyUnicode_IS_READY(right)); |
| 10187 | |
| 10188 | left_len = PyUnicode_GET_LENGTH(*p_left); |
| 10189 | right_len = PyUnicode_GET_LENGTH(right); |
| 10190 | if (left_len > PY_SSIZE_T_MAX - right_len) { |
| 10191 | PyErr_SetString(PyExc_OverflowError, |
| 10192 | "strings are too large to concat"); |
| 10193 | goto error; |
| 10194 | } |
| 10195 | new_len = left_len + right_len; |
| 10196 | |
| 10197 | /* Now we own the last reference to 'left', so we can resize it |
| 10198 | * in-place. |
| 10199 | */ |
| 10200 | if (unicode_resize(p_left, new_len) != 0) { |
| 10201 | /* XXX if _PyUnicode_Resize() fails, 'left' has been |
| 10202 | * deallocated so it cannot be put back into |
| 10203 | * 'variable'. The MemoryError is raised when there |
| 10204 | * is no value in 'variable', which might (very |
| 10205 | * remotely) be a cause of incompatibilities. |
| 10206 | */ |
| 10207 | goto error; |
| 10208 | } |
| 10209 | /* copy 'right' into the newly allocated area of 'left' */ |
| 10210 | #ifdef Py_DEBUG |
| 10211 | copied = PyUnicode_CopyCharacters(*p_left, left_len, |
| 10212 | right, 0, |
| 10213 | right_len); |
| 10214 | assert(0 <= copied); |
| 10215 | #else |
| 10216 | PyUnicode_CopyCharacters(*p_left, left_len, right, 0, right_len); |
| 10217 | #endif |
| 10218 | return; |
| 10219 | |
| 10220 | error: |
| 10221 | Py_DECREF(*p_left); |
| 10222 | *p_left = NULL; |
| 10223 | } |
| 10224 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10225 | void |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10226 | PyUnicode_Append(PyObject **p_left, PyObject *right) |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10227 | { |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10228 | PyObject *left, *res; |
| 10229 | |
| 10230 | if (p_left == NULL) { |
| 10231 | if (!PyErr_Occurred()) |
| 10232 | PyErr_BadInternalCall(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10233 | return; |
| 10234 | } |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10235 | left = *p_left; |
| 10236 | if (right == NULL || !PyUnicode_Check(left)) { |
| 10237 | if (!PyErr_Occurred()) |
| 10238 | PyErr_BadInternalCall(); |
| 10239 | goto error; |
| 10240 | } |
| 10241 | |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 10242 | if (PyUnicode_READY(left)) |
| 10243 | goto error; |
| 10244 | if (PyUnicode_READY(right)) |
| 10245 | goto error; |
| 10246 | |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10247 | if (PyUnicode_CheckExact(left) && left != unicode_empty |
| 10248 | && PyUnicode_CheckExact(right) && right != unicode_empty |
| 10249 | && unicode_resizable(left) |
| 10250 | && (_PyUnicode_KIND(right) <= _PyUnicode_KIND(left) |
| 10251 | || _PyUnicode_WSTR(left) != NULL)) |
| 10252 | { |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10253 | /* Don't resize for ascii += latin1. Convert ascii to latin1 requires |
| 10254 | to change the structure size, but characters are stored just after |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 10255 | the structure, and so it requires to move all characters which is |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10256 | not so different than duplicating the string. */ |
| 10257 | if (!(PyUnicode_IS_ASCII(left) && !PyUnicode_IS_ASCII(right))) |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10258 | { |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10259 | unicode_append_inplace(p_left, right); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10260 | if (p_left != NULL) |
| 10261 | assert(_PyUnicode_CheckConsistency(*p_left, 1)); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10262 | return; |
| 10263 | } |
| 10264 | } |
| 10265 | |
| 10266 | res = PyUnicode_Concat(left, right); |
| 10267 | if (res == NULL) |
| 10268 | goto error; |
| 10269 | Py_DECREF(left); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10270 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10271 | *p_left = res; |
| 10272 | return; |
| 10273 | |
| 10274 | error: |
| 10275 | Py_DECREF(*p_left); |
| 10276 | *p_left = NULL; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10277 | } |
| 10278 | |
| 10279 | void |
| 10280 | PyUnicode_AppendAndDel(PyObject **pleft, PyObject *right) |
| 10281 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10282 | PyUnicode_Append(pleft, right); |
| 10283 | Py_XDECREF(right); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10284 | } |
| 10285 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10286 | PyDoc_STRVAR(count__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10287 | "S.count(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10288 | \n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10289 | Return the number of non-overlapping occurrences of substring sub in\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 10290 | 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] | 10291 | interpreted as in slice notation."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10292 | |
| 10293 | static PyObject * |
| 10294 | unicode_count(PyUnicodeObject *self, PyObject *args) |
| 10295 | { |
| 10296 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10297 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10298 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10299 | PyObject *result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10300 | int kind1, kind2, kind; |
| 10301 | void *buf1, *buf2; |
| 10302 | Py_ssize_t len1, len2, iresult; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10303 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10304 | if (!stringlib_parse_args_finds_unicode("count", args, &substring, |
| 10305 | &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10306 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 10307 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10308 | kind1 = PyUnicode_KIND(self); |
| 10309 | kind2 = PyUnicode_KIND(substring); |
| 10310 | kind = kind1 > kind2 ? kind1 : kind2; |
| 10311 | buf1 = PyUnicode_DATA(self); |
| 10312 | buf2 = PyUnicode_DATA(substring); |
| 10313 | if (kind1 != kind) |
| 10314 | buf1 = _PyUnicode_AsKind((PyObject*)self, kind); |
| 10315 | if (!buf1) { |
| 10316 | Py_DECREF(substring); |
| 10317 | return NULL; |
| 10318 | } |
| 10319 | if (kind2 != kind) |
| 10320 | buf2 = _PyUnicode_AsKind((PyObject*)substring, kind); |
| 10321 | if (!buf2) { |
| 10322 | Py_DECREF(substring); |
| 10323 | if (kind1 != kind) PyMem_Free(buf1); |
| 10324 | return NULL; |
| 10325 | } |
| 10326 | len1 = PyUnicode_GET_LENGTH(self); |
| 10327 | len2 = PyUnicode_GET_LENGTH(substring); |
| 10328 | |
| 10329 | ADJUST_INDICES(start, end, len1); |
| 10330 | switch(kind) { |
| 10331 | case PyUnicode_1BYTE_KIND: |
| 10332 | iresult = ucs1lib_count( |
| 10333 | ((Py_UCS1*)buf1) + start, end - start, |
| 10334 | buf2, len2, PY_SSIZE_T_MAX |
| 10335 | ); |
| 10336 | break; |
| 10337 | case PyUnicode_2BYTE_KIND: |
| 10338 | iresult = ucs2lib_count( |
| 10339 | ((Py_UCS2*)buf1) + start, end - start, |
| 10340 | buf2, len2, PY_SSIZE_T_MAX |
| 10341 | ); |
| 10342 | break; |
| 10343 | case PyUnicode_4BYTE_KIND: |
| 10344 | iresult = ucs4lib_count( |
| 10345 | ((Py_UCS4*)buf1) + start, end - start, |
| 10346 | buf2, len2, PY_SSIZE_T_MAX |
| 10347 | ); |
| 10348 | break; |
| 10349 | default: |
| 10350 | assert(0); iresult = 0; |
| 10351 | } |
| 10352 | |
| 10353 | result = PyLong_FromSsize_t(iresult); |
| 10354 | |
| 10355 | if (kind1 != kind) |
| 10356 | PyMem_Free(buf1); |
| 10357 | if (kind2 != kind) |
| 10358 | PyMem_Free(buf2); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10359 | |
| 10360 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10361 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10362 | return result; |
| 10363 | } |
| 10364 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10365 | PyDoc_STRVAR(encode__doc__, |
Victor Stinner | c911bbf | 2010-11-07 19:04:46 +0000 | [diff] [blame] | 10366 | "S.encode(encoding='utf-8', errors='strict') -> bytes\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10367 | \n\ |
Victor Stinner | e14e212 | 2010-11-07 18:41:46 +0000 | [diff] [blame] | 10368 | Encode S using the codec registered for encoding. Default encoding\n\ |
| 10369 | 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] | 10370 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 10371 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 10372 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 10373 | codecs.register_error that can handle UnicodeEncodeErrors."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10374 | |
| 10375 | static PyObject * |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 10376 | unicode_encode(PyUnicodeObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10377 | { |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 10378 | static char *kwlist[] = {"encoding", "errors", 0}; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10379 | char *encoding = NULL; |
| 10380 | char *errors = NULL; |
Guido van Rossum | 35d9428 | 2007-08-27 18:20:11 +0000 | [diff] [blame] | 10381 | |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 10382 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:encode", |
| 10383 | kwlist, &encoding, &errors)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10384 | return NULL; |
Georg Brandl | 3b9406b | 2010-12-03 07:54:09 +0000 | [diff] [blame] | 10385 | return PyUnicode_AsEncodedString((PyObject *)self, encoding, errors); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 10386 | } |
| 10387 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10388 | PyDoc_STRVAR(expandtabs__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10389 | "S.expandtabs([tabsize]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10390 | \n\ |
| 10391 | 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] | 10392 | 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] | 10393 | |
| 10394 | static PyObject* |
| 10395 | unicode_expandtabs(PyUnicodeObject *self, PyObject *args) |
| 10396 | { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10397 | Py_ssize_t i, j, line_pos, src_len, incr; |
| 10398 | Py_UCS4 ch; |
| 10399 | PyObject *u; |
| 10400 | void *src_data, *dest_data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10401 | int tabsize = 8; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10402 | int kind; |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 10403 | int found; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10404 | |
| 10405 | if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10406 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10407 | |
Antoine Pitrou | 2242522 | 2011-10-04 19:10:51 +0200 | [diff] [blame] | 10408 | if (PyUnicode_READY(self) == -1) |
| 10409 | return NULL; |
| 10410 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 10411 | /* First pass: determine size of output string */ |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10412 | src_len = PyUnicode_GET_LENGTH(self); |
| 10413 | i = j = line_pos = 0; |
| 10414 | kind = PyUnicode_KIND(self); |
| 10415 | src_data = PyUnicode_DATA(self); |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 10416 | found = 0; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10417 | for (; i < src_len; i++) { |
| 10418 | ch = PyUnicode_READ(kind, src_data, i); |
| 10419 | if (ch == '\t') { |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 10420 | found = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10421 | if (tabsize > 0) { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10422 | incr = tabsize - (line_pos % tabsize); /* cannot overflow */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10423 | if (j > PY_SSIZE_T_MAX - incr) |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10424 | goto overflow; |
| 10425 | line_pos += incr; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10426 | j += incr; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10427 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10428 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10429 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10430 | if (j > PY_SSIZE_T_MAX - 1) |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10431 | goto overflow; |
| 10432 | line_pos++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10433 | j++; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10434 | if (ch == '\n' || ch == '\r') |
| 10435 | line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10436 | } |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10437 | } |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 10438 | if (!found && PyUnicode_CheckExact(self)) { |
| 10439 | Py_INCREF((PyObject *) self); |
| 10440 | return (PyObject *) self; |
| 10441 | } |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 10442 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10443 | /* Second pass: create output string and fill it */ |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10444 | u = PyUnicode_New(j, PyUnicode_MAX_CHAR_VALUE(self)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10445 | if (!u) |
| 10446 | return NULL; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10447 | dest_data = PyUnicode_DATA(u); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10448 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10449 | i = j = line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10450 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10451 | for (; i < src_len; i++) { |
| 10452 | ch = PyUnicode_READ(kind, src_data, i); |
| 10453 | if (ch == '\t') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10454 | if (tabsize > 0) { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10455 | incr = tabsize - (line_pos % tabsize); |
| 10456 | line_pos += incr; |
| 10457 | while (incr--) { |
| 10458 | PyUnicode_WRITE(kind, dest_data, j, ' '); |
| 10459 | j++; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10460 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10461 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10462 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10463 | else { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10464 | line_pos++; |
| 10465 | PyUnicode_WRITE(kind, dest_data, j, ch); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10466 | j++; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10467 | if (ch == '\n' || ch == '\r') |
| 10468 | line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10469 | } |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10470 | } |
| 10471 | assert (j == PyUnicode_GET_LENGTH(u)); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 10472 | #ifndef DONT_MAKE_RESULT_READY |
| 10473 | if (_PyUnicode_READY_REPLACE(&u)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10474 | Py_DECREF(u); |
| 10475 | return NULL; |
| 10476 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 10477 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10478 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10479 | return (PyObject*) u; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10480 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10481 | overflow: |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10482 | PyErr_SetString(PyExc_OverflowError, "new string is too long"); |
| 10483 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10484 | } |
| 10485 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10486 | PyDoc_STRVAR(find__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10487 | "S.find(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10488 | \n\ |
| 10489 | Return the lowest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 10490 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10491 | arguments start and end are interpreted as in slice notation.\n\ |
| 10492 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10493 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10494 | |
| 10495 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10496 | unicode_find(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10497 | { |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10498 | PyUnicodeObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 10499 | Py_ssize_t start; |
| 10500 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10501 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10502 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10503 | if (!stringlib_parse_args_finds_unicode("find", args, &substring, |
| 10504 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10505 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10506 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10507 | if (PyUnicode_READY(self) == -1) |
| 10508 | return NULL; |
| 10509 | if (PyUnicode_READY(substring) == -1) |
| 10510 | return NULL; |
| 10511 | |
| 10512 | result = any_find_slice( |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10513 | asciilib_find_slice, ucs1lib_find_slice, |
| 10514 | ucs2lib_find_slice, ucs4lib_find_slice, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10515 | self, (PyObject*)substring, start, end |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10516 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10517 | |
| 10518 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10519 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10520 | if (result == -2) |
| 10521 | return NULL; |
| 10522 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 10523 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10524 | } |
| 10525 | |
| 10526 | static PyObject * |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 10527 | unicode_getitem(PyObject *self, Py_ssize_t index) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10528 | { |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 10529 | Py_UCS4 ch = PyUnicode_ReadChar(self, index); |
| 10530 | if (ch == (Py_UCS4)-1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10531 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10532 | return PyUnicode_FromOrdinal(ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10533 | } |
| 10534 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 10535 | /* Believe it or not, this produces the same value for ASCII strings |
Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 10536 | as bytes_hash(). */ |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 10537 | static Py_hash_t |
Neil Schemenauer | f8c37d1 | 2007-09-07 20:49:04 +0000 | [diff] [blame] | 10538 | unicode_hash(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10539 | { |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 10540 | Py_ssize_t len; |
Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 10541 | Py_uhash_t x; |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 10542 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10543 | if (_PyUnicode_HASH(self) != -1) |
| 10544 | return _PyUnicode_HASH(self); |
| 10545 | if (PyUnicode_READY(self) == -1) |
| 10546 | return -1; |
| 10547 | len = PyUnicode_GET_LENGTH(self); |
| 10548 | |
| 10549 | /* The hash function as a macro, gets expanded three times below. */ |
| 10550 | #define HASH(P) \ |
| 10551 | x = (Py_uhash_t)*P << 7; \ |
| 10552 | while (--len >= 0) \ |
| 10553 | x = (1000003*x) ^ (Py_uhash_t)*P++; |
| 10554 | |
| 10555 | switch (PyUnicode_KIND(self)) { |
| 10556 | case PyUnicode_1BYTE_KIND: { |
| 10557 | const unsigned char *c = PyUnicode_1BYTE_DATA(self); |
| 10558 | HASH(c); |
| 10559 | break; |
| 10560 | } |
| 10561 | case PyUnicode_2BYTE_KIND: { |
| 10562 | const Py_UCS2 *s = PyUnicode_2BYTE_DATA(self); |
| 10563 | HASH(s); |
| 10564 | break; |
| 10565 | } |
| 10566 | default: { |
| 10567 | Py_UCS4 *l; |
| 10568 | assert(PyUnicode_KIND(self) == PyUnicode_4BYTE_KIND && |
| 10569 | "Impossible switch case in unicode_hash"); |
| 10570 | l = PyUnicode_4BYTE_DATA(self); |
| 10571 | HASH(l); |
| 10572 | break; |
| 10573 | } |
| 10574 | } |
| 10575 | x ^= (Py_uhash_t)PyUnicode_GET_LENGTH(self); |
| 10576 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 10577 | if (x == -1) |
| 10578 | x = -2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10579 | _PyUnicode_HASH(self) = x; |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 10580 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10581 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10582 | #undef HASH |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10583 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10584 | PyDoc_STRVAR(index__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10585 | "S.index(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10586 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10587 | 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] | 10588 | |
| 10589 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10590 | unicode_index(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10591 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10592 | Py_ssize_t result; |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10593 | PyUnicodeObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 10594 | Py_ssize_t start; |
| 10595 | Py_ssize_t end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10596 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10597 | if (!stringlib_parse_args_finds_unicode("index", args, &substring, |
| 10598 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10599 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10600 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10601 | if (PyUnicode_READY(self) == -1) |
| 10602 | return NULL; |
| 10603 | if (PyUnicode_READY(substring) == -1) |
| 10604 | return NULL; |
| 10605 | |
| 10606 | result = any_find_slice( |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10607 | asciilib_find_slice, ucs1lib_find_slice, |
| 10608 | ucs2lib_find_slice, ucs4lib_find_slice, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10609 | self, (PyObject*)substring, start, end |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10610 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10611 | |
| 10612 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10613 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10614 | if (result == -2) |
| 10615 | return NULL; |
| 10616 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10617 | if (result < 0) { |
| 10618 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 10619 | return NULL; |
| 10620 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10621 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 10622 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10623 | } |
| 10624 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10625 | PyDoc_STRVAR(islower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10626 | "S.islower() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10627 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10628 | 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] | 10629 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10630 | |
| 10631 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10632 | unicode_islower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10633 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10634 | Py_ssize_t i, length; |
| 10635 | int kind; |
| 10636 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10637 | int cased; |
| 10638 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10639 | if (PyUnicode_READY(self) == -1) |
| 10640 | return NULL; |
| 10641 | length = PyUnicode_GET_LENGTH(self); |
| 10642 | kind = PyUnicode_KIND(self); |
| 10643 | data = PyUnicode_DATA(self); |
| 10644 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10645 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10646 | if (length == 1) |
| 10647 | return PyBool_FromLong( |
| 10648 | Py_UNICODE_ISLOWER(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10649 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10650 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10651 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10652 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10653 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10654 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10655 | for (i = 0; i < length; i++) { |
| 10656 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 10657 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10658 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 10659 | return PyBool_FromLong(0); |
| 10660 | else if (!cased && Py_UNICODE_ISLOWER(ch)) |
| 10661 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10662 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10663 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10664 | } |
| 10665 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10666 | PyDoc_STRVAR(isupper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10667 | "S.isupper() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10668 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 10669 | 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] | 10670 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10671 | |
| 10672 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10673 | unicode_isupper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10674 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10675 | Py_ssize_t i, length; |
| 10676 | int kind; |
| 10677 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10678 | int cased; |
| 10679 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10680 | if (PyUnicode_READY(self) == -1) |
| 10681 | return NULL; |
| 10682 | length = PyUnicode_GET_LENGTH(self); |
| 10683 | kind = PyUnicode_KIND(self); |
| 10684 | data = PyUnicode_DATA(self); |
| 10685 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10686 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10687 | if (length == 1) |
| 10688 | return PyBool_FromLong( |
| 10689 | Py_UNICODE_ISUPPER(PyUnicode_READ(kind, data, 0)) != 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10690 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10691 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10692 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10693 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10694 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10695 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10696 | for (i = 0; i < length; i++) { |
| 10697 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 10698 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10699 | if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 10700 | return PyBool_FromLong(0); |
| 10701 | else if (!cased && Py_UNICODE_ISUPPER(ch)) |
| 10702 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10703 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10704 | return PyBool_FromLong(cased); |
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(istitle__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10708 | "S.istitle() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10709 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 10710 | Return True if S is a titlecased string and there is at least one\n\ |
| 10711 | character in S, i.e. upper- and titlecase characters may only\n\ |
| 10712 | follow uncased characters and lowercase characters only cased ones.\n\ |
| 10713 | Return False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10714 | |
| 10715 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10716 | unicode_istitle(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10717 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10718 | Py_ssize_t i, length; |
| 10719 | int kind; |
| 10720 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10721 | int cased, previous_is_cased; |
| 10722 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10723 | if (PyUnicode_READY(self) == -1) |
| 10724 | return NULL; |
| 10725 | length = PyUnicode_GET_LENGTH(self); |
| 10726 | kind = PyUnicode_KIND(self); |
| 10727 | data = PyUnicode_DATA(self); |
| 10728 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10729 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10730 | if (length == 1) { |
| 10731 | Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 10732 | return PyBool_FromLong((Py_UNICODE_ISTITLE(ch) != 0) || |
| 10733 | (Py_UNICODE_ISUPPER(ch) != 0)); |
| 10734 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10735 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10736 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10737 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10738 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10739 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10740 | cased = 0; |
| 10741 | previous_is_cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10742 | for (i = 0; i < length; i++) { |
| 10743 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 10744 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10745 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { |
| 10746 | if (previous_is_cased) |
| 10747 | return PyBool_FromLong(0); |
| 10748 | previous_is_cased = 1; |
| 10749 | cased = 1; |
| 10750 | } |
| 10751 | else if (Py_UNICODE_ISLOWER(ch)) { |
| 10752 | if (!previous_is_cased) |
| 10753 | return PyBool_FromLong(0); |
| 10754 | previous_is_cased = 1; |
| 10755 | cased = 1; |
| 10756 | } |
| 10757 | else |
| 10758 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10759 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10760 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10761 | } |
| 10762 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10763 | PyDoc_STRVAR(isspace__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10764 | "S.isspace() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10765 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 10766 | Return True if all characters in S are whitespace\n\ |
| 10767 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10768 | |
| 10769 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10770 | unicode_isspace(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10771 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10772 | Py_ssize_t i, length; |
| 10773 | int kind; |
| 10774 | void *data; |
| 10775 | |
| 10776 | if (PyUnicode_READY(self) == -1) |
| 10777 | return NULL; |
| 10778 | length = PyUnicode_GET_LENGTH(self); |
| 10779 | kind = PyUnicode_KIND(self); |
| 10780 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10781 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10782 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10783 | if (length == 1) |
| 10784 | return PyBool_FromLong( |
| 10785 | Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10786 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10787 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10788 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10789 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10790 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10791 | for (i = 0; i < length; i++) { |
| 10792 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 10793 | if (!Py_UNICODE_ISSPACE(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10794 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10795 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10796 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10797 | } |
| 10798 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10799 | PyDoc_STRVAR(isalpha__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10800 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10801 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 10802 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10803 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10804 | |
| 10805 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10806 | unicode_isalpha(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10807 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10808 | Py_ssize_t i, length; |
| 10809 | int kind; |
| 10810 | void *data; |
| 10811 | |
| 10812 | if (PyUnicode_READY(self) == -1) |
| 10813 | return NULL; |
| 10814 | length = PyUnicode_GET_LENGTH(self); |
| 10815 | kind = PyUnicode_KIND(self); |
| 10816 | data = PyUnicode_DATA(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10817 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10818 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10819 | if (length == 1) |
| 10820 | return PyBool_FromLong( |
| 10821 | Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, 0))); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10822 | |
| 10823 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10824 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10825 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10826 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10827 | for (i = 0; i < length; i++) { |
| 10828 | if (!Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10829 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10830 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10831 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10832 | } |
| 10833 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10834 | PyDoc_STRVAR(isalnum__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10835 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10836 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 10837 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10838 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10839 | |
| 10840 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10841 | unicode_isalnum(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10842 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10843 | int kind; |
| 10844 | void *data; |
| 10845 | Py_ssize_t len, i; |
| 10846 | |
| 10847 | if (PyUnicode_READY(self) == -1) |
| 10848 | return NULL; |
| 10849 | |
| 10850 | kind = PyUnicode_KIND(self); |
| 10851 | data = PyUnicode_DATA(self); |
| 10852 | len = PyUnicode_GET_LENGTH(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10853 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10854 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10855 | if (len == 1) { |
| 10856 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 10857 | return PyBool_FromLong(Py_UNICODE_ISALNUM(ch)); |
| 10858 | } |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10859 | |
| 10860 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10861 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10862 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10863 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10864 | for (i = 0; i < len; i++) { |
| 10865 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 10866 | if (!Py_UNICODE_ISALNUM(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10867 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10868 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10869 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10870 | } |
| 10871 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10872 | PyDoc_STRVAR(isdecimal__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10873 | "S.isdecimal() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10874 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10875 | 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] | 10876 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10877 | |
| 10878 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10879 | unicode_isdecimal(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10880 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10881 | Py_ssize_t i, length; |
| 10882 | int kind; |
| 10883 | void *data; |
| 10884 | |
| 10885 | if (PyUnicode_READY(self) == -1) |
| 10886 | return NULL; |
| 10887 | length = PyUnicode_GET_LENGTH(self); |
| 10888 | kind = PyUnicode_KIND(self); |
| 10889 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10890 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10891 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10892 | if (length == 1) |
| 10893 | return PyBool_FromLong( |
| 10894 | Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10895 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10896 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10897 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10898 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10899 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10900 | for (i = 0; i < length; i++) { |
| 10901 | if (!Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10902 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10903 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10904 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10905 | } |
| 10906 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10907 | PyDoc_STRVAR(isdigit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10908 | "S.isdigit() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10909 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 10910 | Return True if all characters in S are digits\n\ |
| 10911 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10912 | |
| 10913 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10914 | unicode_isdigit(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10915 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10916 | Py_ssize_t i, length; |
| 10917 | int kind; |
| 10918 | void *data; |
| 10919 | |
| 10920 | if (PyUnicode_READY(self) == -1) |
| 10921 | return NULL; |
| 10922 | length = PyUnicode_GET_LENGTH(self); |
| 10923 | kind = PyUnicode_KIND(self); |
| 10924 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10925 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10926 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10927 | if (length == 1) { |
| 10928 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 10929 | return PyBool_FromLong(Py_UNICODE_ISDIGIT(ch)); |
| 10930 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10931 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10932 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10933 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10934 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10935 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10936 | for (i = 0; i < length; i++) { |
| 10937 | if (!Py_UNICODE_ISDIGIT(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10938 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10939 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10940 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10941 | } |
| 10942 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10943 | PyDoc_STRVAR(isnumeric__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10944 | "S.isnumeric() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10945 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10946 | 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] | 10947 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10948 | |
| 10949 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10950 | unicode_isnumeric(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10951 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10952 | Py_ssize_t i, length; |
| 10953 | int kind; |
| 10954 | void *data; |
| 10955 | |
| 10956 | if (PyUnicode_READY(self) == -1) |
| 10957 | return NULL; |
| 10958 | length = PyUnicode_GET_LENGTH(self); |
| 10959 | kind = PyUnicode_KIND(self); |
| 10960 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10961 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10962 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10963 | if (length == 1) |
| 10964 | return PyBool_FromLong( |
| 10965 | Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10966 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10967 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10968 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10969 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10970 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10971 | for (i = 0; i < length; i++) { |
| 10972 | if (!Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10973 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10974 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10975 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10976 | } |
| 10977 | |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 10978 | int |
| 10979 | PyUnicode_IsIdentifier(PyObject *self) |
| 10980 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10981 | int kind; |
| 10982 | void *data; |
| 10983 | Py_ssize_t i; |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 10984 | Py_UCS4 first; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 10985 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10986 | if (PyUnicode_READY(self) == -1) { |
| 10987 | Py_FatalError("identifier not ready"); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10988 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10989 | } |
| 10990 | |
| 10991 | /* Special case for empty strings */ |
| 10992 | if (PyUnicode_GET_LENGTH(self) == 0) |
| 10993 | return 0; |
| 10994 | kind = PyUnicode_KIND(self); |
| 10995 | data = PyUnicode_DATA(self); |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 10996 | |
| 10997 | /* PEP 3131 says that the first character must be in |
| 10998 | XID_Start and subsequent characters in XID_Continue, |
| 10999 | and for the ASCII range, the 2.x rules apply (i.e |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11000 | start with letters and underscore, continue with |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11001 | letters, digits, underscore). However, given the current |
| 11002 | definition of XID_Start and XID_Continue, it is sufficient |
| 11003 | to check just for these, except that _ must be allowed |
| 11004 | as starting an identifier. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11005 | first = PyUnicode_READ(kind, data, 0); |
Benjamin Peterson | f413b80 | 2011-08-12 22:17:18 -0500 | [diff] [blame] | 11006 | if (!_PyUnicode_IsXidStart(first) && first != 0x5F /* LOW LINE */) |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11007 | return 0; |
| 11008 | |
Benjamin Peterson | 9c6e6a0 | 2011-09-28 08:09:05 -0400 | [diff] [blame] | 11009 | for (i = 1; i < PyUnicode_GET_LENGTH(self); i++) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11010 | if (!_PyUnicode_IsXidContinue(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11011 | return 0; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11012 | return 1; |
| 11013 | } |
| 11014 | |
| 11015 | PyDoc_STRVAR(isidentifier__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11016 | "S.isidentifier() -> bool\n\ |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11017 | \n\ |
| 11018 | Return True if S is a valid identifier according\n\ |
| 11019 | to the language definition."); |
| 11020 | |
| 11021 | static PyObject* |
| 11022 | unicode_isidentifier(PyObject *self) |
| 11023 | { |
| 11024 | return PyBool_FromLong(PyUnicode_IsIdentifier(self)); |
| 11025 | } |
| 11026 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11027 | PyDoc_STRVAR(isprintable__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11028 | "S.isprintable() -> bool\n\ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11029 | \n\ |
| 11030 | Return True if all characters in S are considered\n\ |
| 11031 | printable in repr() or S is empty, False otherwise."); |
| 11032 | |
| 11033 | static PyObject* |
| 11034 | unicode_isprintable(PyObject *self) |
| 11035 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11036 | Py_ssize_t i, length; |
| 11037 | int kind; |
| 11038 | void *data; |
| 11039 | |
| 11040 | if (PyUnicode_READY(self) == -1) |
| 11041 | return NULL; |
| 11042 | length = PyUnicode_GET_LENGTH(self); |
| 11043 | kind = PyUnicode_KIND(self); |
| 11044 | data = PyUnicode_DATA(self); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11045 | |
| 11046 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11047 | if (length == 1) |
| 11048 | return PyBool_FromLong( |
| 11049 | Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, 0))); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11050 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11051 | for (i = 0; i < length; i++) { |
| 11052 | if (!Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, i))) { |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11053 | Py_RETURN_FALSE; |
| 11054 | } |
| 11055 | } |
| 11056 | Py_RETURN_TRUE; |
| 11057 | } |
| 11058 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11059 | PyDoc_STRVAR(join__doc__, |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 11060 | "S.join(iterable) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11061 | \n\ |
| 11062 | 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] | 11063 | iterable. The separator between elements is S."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11064 | |
| 11065 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11066 | unicode_join(PyObject *self, PyObject *data) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11067 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11068 | return PyUnicode_Join(self, data); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11069 | } |
| 11070 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11071 | static Py_ssize_t |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11072 | unicode_length(PyUnicodeObject *self) |
| 11073 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11074 | if (PyUnicode_READY(self) == -1) |
| 11075 | return -1; |
| 11076 | return PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11077 | } |
| 11078 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11079 | PyDoc_STRVAR(ljust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11080 | "S.ljust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11081 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 11082 | 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] | 11083 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11084 | |
| 11085 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11086 | unicode_ljust(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11087 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11088 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11089 | Py_UCS4 fillchar = ' '; |
| 11090 | |
| 11091 | if (PyUnicode_READY(self) == -1) |
| 11092 | return NULL; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 11093 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11094 | if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11095 | return NULL; |
| 11096 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11097 | if (_PyUnicode_LENGTH(self) >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11098 | Py_INCREF(self); |
| 11099 | return (PyObject*) self; |
| 11100 | } |
| 11101 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11102 | return (PyObject*) pad(self, 0, width - _PyUnicode_LENGTH(self), fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11103 | } |
| 11104 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11105 | PyDoc_STRVAR(lower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11106 | "S.lower() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11107 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11108 | Return a copy of the string S converted to lowercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11109 | |
| 11110 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11111 | unicode_lower(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11112 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11113 | return fixup(self, fixlower); |
| 11114 | } |
| 11115 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11116 | #define LEFTSTRIP 0 |
| 11117 | #define RIGHTSTRIP 1 |
| 11118 | #define BOTHSTRIP 2 |
| 11119 | |
| 11120 | /* Arrays indexed by above */ |
| 11121 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 11122 | |
| 11123 | #define STRIPNAME(i) (stripformat[i]+3) |
| 11124 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11125 | /* externally visible for str.strip(unicode) */ |
| 11126 | PyObject * |
| 11127 | _PyUnicode_XStrip(PyUnicodeObject *self, int striptype, PyObject *sepobj) |
| 11128 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11129 | void *data; |
| 11130 | int kind; |
| 11131 | Py_ssize_t i, j, len; |
| 11132 | BLOOM_MASK sepmask; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11133 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11134 | if (PyUnicode_READY(self) == -1 || PyUnicode_READY(sepobj) == -1) |
| 11135 | return NULL; |
| 11136 | |
| 11137 | kind = PyUnicode_KIND(self); |
| 11138 | data = PyUnicode_DATA(self); |
| 11139 | len = PyUnicode_GET_LENGTH(self); |
| 11140 | sepmask = make_bloom_mask(PyUnicode_KIND(sepobj), |
| 11141 | PyUnicode_DATA(sepobj), |
| 11142 | PyUnicode_GET_LENGTH(sepobj)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11143 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11144 | i = 0; |
| 11145 | if (striptype != RIGHTSTRIP) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11146 | while (i < len && |
| 11147 | BLOOM_MEMBER(sepmask, PyUnicode_READ(kind, data, i), sepobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11148 | i++; |
| 11149 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11150 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11151 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11152 | j = len; |
| 11153 | if (striptype != LEFTSTRIP) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11154 | do { |
| 11155 | j--; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11156 | } while (j >= i && |
| 11157 | BLOOM_MEMBER(sepmask, PyUnicode_READ(kind, data, j), sepobj)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11158 | j++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11159 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11160 | |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11161 | return PyUnicode_Substring((PyObject*)self, i, j); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11162 | } |
| 11163 | |
| 11164 | PyObject* |
| 11165 | PyUnicode_Substring(PyObject *self, Py_ssize_t start, Py_ssize_t end) |
| 11166 | { |
| 11167 | unsigned char *data; |
| 11168 | int kind; |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11169 | Py_ssize_t length; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11170 | |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 11171 | if (PyUnicode_READY(self) == -1) |
| 11172 | return NULL; |
| 11173 | |
| 11174 | end = Py_MIN(end, PyUnicode_GET_LENGTH(self)); |
| 11175 | |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11176 | if (start == 0 && end == PyUnicode_GET_LENGTH(self)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11177 | { |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11178 | if (PyUnicode_CheckExact(self)) { |
| 11179 | Py_INCREF(self); |
| 11180 | return self; |
| 11181 | } |
| 11182 | else |
| 11183 | return PyUnicode_Copy(self); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11184 | } |
| 11185 | |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11186 | length = end - start; |
| 11187 | if (length == 1) |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 11188 | return unicode_getitem(self, start); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11189 | |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 11190 | if (start < 0 || end < 0) { |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11191 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 11192 | return NULL; |
| 11193 | } |
| 11194 | |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 11195 | if (PyUnicode_IS_ASCII(self)) { |
| 11196 | kind = PyUnicode_KIND(self); |
| 11197 | data = PyUnicode_1BYTE_DATA(self); |
| 11198 | return unicode_fromascii(data + start, length); |
| 11199 | } |
| 11200 | else { |
| 11201 | kind = PyUnicode_KIND(self); |
| 11202 | data = PyUnicode_1BYTE_DATA(self); |
| 11203 | return PyUnicode_FromKindAndData(kind, |
| 11204 | data + PyUnicode_KIND_SIZE(kind, start), |
| 11205 | length); |
| 11206 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11207 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11208 | |
| 11209 | static PyObject * |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11210 | do_strip(PyUnicodeObject *self, int striptype) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11211 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11212 | int kind; |
| 11213 | void *data; |
| 11214 | Py_ssize_t len, i, j; |
| 11215 | |
| 11216 | if (PyUnicode_READY(self) == -1) |
| 11217 | return NULL; |
| 11218 | |
| 11219 | kind = PyUnicode_KIND(self); |
| 11220 | data = PyUnicode_DATA(self); |
| 11221 | len = PyUnicode_GET_LENGTH(self); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11222 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11223 | i = 0; |
| 11224 | if (striptype != RIGHTSTRIP) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11225 | while (i < len && Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, i))) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11226 | i++; |
| 11227 | } |
| 11228 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11229 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11230 | j = len; |
| 11231 | if (striptype != LEFTSTRIP) { |
| 11232 | do { |
| 11233 | j--; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11234 | } while (j >= i && Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, j))); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11235 | j++; |
| 11236 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11237 | |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11238 | return PyUnicode_Substring((PyObject*)self, i, j); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11239 | } |
| 11240 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11241 | |
| 11242 | static PyObject * |
| 11243 | do_argstrip(PyUnicodeObject *self, int striptype, PyObject *args) |
| 11244 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11245 | PyObject *sep = NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11246 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11247 | if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) |
| 11248 | return NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11249 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11250 | if (sep != NULL && sep != Py_None) { |
| 11251 | if (PyUnicode_Check(sep)) |
| 11252 | return _PyUnicode_XStrip(self, striptype, sep); |
| 11253 | else { |
| 11254 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11255 | "%s arg must be None or str", |
| 11256 | STRIPNAME(striptype)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11257 | return NULL; |
| 11258 | } |
| 11259 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11260 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11261 | return do_strip(self, striptype); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11262 | } |
| 11263 | |
| 11264 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11265 | PyDoc_STRVAR(strip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11266 | "S.strip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11267 | \n\ |
| 11268 | Return a copy of the string S with leading and trailing\n\ |
| 11269 | whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11270 | 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] | 11271 | |
| 11272 | static PyObject * |
| 11273 | unicode_strip(PyUnicodeObject *self, PyObject *args) |
| 11274 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11275 | if (PyTuple_GET_SIZE(args) == 0) |
| 11276 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 11277 | else |
| 11278 | return do_argstrip(self, BOTHSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11279 | } |
| 11280 | |
| 11281 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11282 | PyDoc_STRVAR(lstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11283 | "S.lstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11284 | \n\ |
| 11285 | Return a copy of the string S with leading whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11286 | 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] | 11287 | |
| 11288 | static PyObject * |
| 11289 | unicode_lstrip(PyUnicodeObject *self, PyObject *args) |
| 11290 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11291 | if (PyTuple_GET_SIZE(args) == 0) |
| 11292 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 11293 | else |
| 11294 | return do_argstrip(self, LEFTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11295 | } |
| 11296 | |
| 11297 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11298 | PyDoc_STRVAR(rstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11299 | "S.rstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11300 | \n\ |
| 11301 | Return a copy of the string S with trailing whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11302 | 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] | 11303 | |
| 11304 | static PyObject * |
| 11305 | unicode_rstrip(PyUnicodeObject *self, PyObject *args) |
| 11306 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11307 | if (PyTuple_GET_SIZE(args) == 0) |
| 11308 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 11309 | else |
| 11310 | return do_argstrip(self, RIGHTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11311 | } |
| 11312 | |
| 11313 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11314 | static PyObject* |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11315 | unicode_repeat(PyUnicodeObject *str, Py_ssize_t len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11316 | { |
| 11317 | PyUnicodeObject *u; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11318 | Py_ssize_t nchars, n; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11319 | |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 11320 | if (len < 1) { |
| 11321 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 11322 | return unicode_empty; |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 11323 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11324 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 11325 | if (len == 1 && PyUnicode_CheckExact(str)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11326 | /* no repeat, return original string */ |
| 11327 | Py_INCREF(str); |
| 11328 | return (PyObject*) str; |
| 11329 | } |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 11330 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11331 | if (PyUnicode_READY(str) == -1) |
| 11332 | return NULL; |
| 11333 | |
Victor Stinner | c759f3e | 2011-10-01 03:09:58 +0200 | [diff] [blame] | 11334 | if (PyUnicode_GET_LENGTH(str) > PY_SSIZE_T_MAX / len) { |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11335 | PyErr_SetString(PyExc_OverflowError, |
| 11336 | "repeated string is too long"); |
| 11337 | return NULL; |
| 11338 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11339 | nchars = len * PyUnicode_GET_LENGTH(str); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11340 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11341 | u = (PyUnicodeObject *)PyUnicode_New(nchars, PyUnicode_MAX_CHAR_VALUE(str)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11342 | if (!u) |
| 11343 | return NULL; |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11344 | assert(PyUnicode_KIND(u) == PyUnicode_KIND(str)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11345 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11346 | if (PyUnicode_GET_LENGTH(str) == 1) { |
| 11347 | const int kind = PyUnicode_KIND(str); |
| 11348 | const Py_UCS4 fill_char = PyUnicode_READ(kind, PyUnicode_DATA(str), 0); |
| 11349 | void *to = PyUnicode_DATA(u); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11350 | if (kind == PyUnicode_1BYTE_KIND) |
| 11351 | memset(to, (unsigned char)fill_char, len); |
| 11352 | else { |
| 11353 | for (n = 0; n < len; ++n) |
| 11354 | PyUnicode_WRITE(kind, to, n, fill_char); |
| 11355 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11356 | } |
| 11357 | else { |
| 11358 | /* number of characters copied this far */ |
| 11359 | Py_ssize_t done = PyUnicode_GET_LENGTH(str); |
| 11360 | const Py_ssize_t char_size = PyUnicode_CHARACTER_SIZE(str); |
| 11361 | char *to = (char *) PyUnicode_DATA(u); |
| 11362 | Py_MEMCPY(to, PyUnicode_DATA(str), |
| 11363 | PyUnicode_GET_LENGTH(str) * char_size); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11364 | while (done < nchars) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11365 | n = (done <= nchars-done) ? done : nchars-done; |
| 11366 | Py_MEMCPY(to + (done * char_size), to, n * char_size); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11367 | done += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11368 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11369 | } |
| 11370 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 11371 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11372 | return (PyObject*) u; |
| 11373 | } |
| 11374 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 11375 | PyObject * |
| 11376 | PyUnicode_Replace(PyObject *obj, |
| 11377 | PyObject *subobj, |
| 11378 | PyObject *replobj, |
| 11379 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11380 | { |
| 11381 | PyObject *self; |
| 11382 | PyObject *str1; |
| 11383 | PyObject *str2; |
| 11384 | PyObject *result; |
| 11385 | |
| 11386 | self = PyUnicode_FromObject(obj); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11387 | if (self == NULL || PyUnicode_READY(self) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11388 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11389 | str1 = PyUnicode_FromObject(subobj); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11390 | if (str1 == NULL || PyUnicode_READY(str1) == -1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11391 | Py_DECREF(self); |
| 11392 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11393 | } |
| 11394 | str2 = PyUnicode_FromObject(replobj); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11395 | if (str2 == NULL || PyUnicode_READY(str2)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11396 | Py_DECREF(self); |
| 11397 | Py_DECREF(str1); |
| 11398 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11399 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11400 | result = replace(self, str1, str2, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11401 | Py_DECREF(self); |
| 11402 | Py_DECREF(str1); |
| 11403 | Py_DECREF(str2); |
| 11404 | return result; |
| 11405 | } |
| 11406 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11407 | PyDoc_STRVAR(replace__doc__, |
Ezio Melotti | c1897e7 | 2010-06-26 18:50:39 +0000 | [diff] [blame] | 11408 | "S.replace(old, new[, count]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11409 | \n\ |
| 11410 | Return a copy of S with all occurrences of substring\n\ |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 11411 | old replaced by new. If the optional argument count is\n\ |
| 11412 | given, only the first count occurrences are replaced."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11413 | |
| 11414 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11415 | unicode_replace(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11416 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11417 | PyObject *str1; |
| 11418 | PyObject *str2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11419 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11420 | PyObject *result; |
| 11421 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11422 | if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11423 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11424 | if (!PyUnicode_READY(self) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11425 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11426 | str1 = PyUnicode_FromObject(str1); |
| 11427 | if (str1 == NULL || PyUnicode_READY(str1) == -1) |
| 11428 | return NULL; |
| 11429 | str2 = PyUnicode_FromObject(str2); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11430 | if (str2 == NULL || PyUnicode_READY(str2) == -1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11431 | Py_DECREF(str1); |
| 11432 | return NULL; |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 11433 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11434 | |
| 11435 | result = replace(self, str1, str2, maxcount); |
| 11436 | |
| 11437 | Py_DECREF(str1); |
| 11438 | Py_DECREF(str2); |
| 11439 | return result; |
| 11440 | } |
| 11441 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 11442 | static PyObject * |
| 11443 | unicode_repr(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11444 | { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11445 | PyObject *repr; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11446 | Py_ssize_t isize; |
| 11447 | Py_ssize_t osize, squote, dquote, i, o; |
| 11448 | Py_UCS4 max, quote; |
| 11449 | int ikind, okind; |
| 11450 | void *idata, *odata; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11451 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11452 | if (PyUnicode_READY(unicode) == -1) |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11453 | return NULL; |
| 11454 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11455 | isize = PyUnicode_GET_LENGTH(unicode); |
| 11456 | idata = PyUnicode_DATA(unicode); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11457 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11458 | /* Compute length of output, quote characters, and |
| 11459 | maximum character */ |
| 11460 | osize = 2; /* quotes */ |
| 11461 | max = 127; |
| 11462 | squote = dquote = 0; |
| 11463 | ikind = PyUnicode_KIND(unicode); |
| 11464 | for (i = 0; i < isize; i++) { |
| 11465 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
| 11466 | switch (ch) { |
| 11467 | case '\'': squote++; osize++; break; |
| 11468 | case '"': dquote++; osize++; break; |
| 11469 | case '\\': case '\t': case '\r': case '\n': |
| 11470 | osize += 2; break; |
| 11471 | default: |
| 11472 | /* Fast-path ASCII */ |
| 11473 | if (ch < ' ' || ch == 0x7f) |
| 11474 | osize += 4; /* \xHH */ |
| 11475 | else if (ch < 0x7f) |
| 11476 | osize++; |
| 11477 | else if (Py_UNICODE_ISPRINTABLE(ch)) { |
| 11478 | osize++; |
| 11479 | max = ch > max ? ch : max; |
| 11480 | } |
| 11481 | else if (ch < 0x100) |
| 11482 | osize += 4; /* \xHH */ |
| 11483 | else if (ch < 0x10000) |
| 11484 | osize += 6; /* \uHHHH */ |
| 11485 | else |
| 11486 | osize += 10; /* \uHHHHHHHH */ |
| 11487 | } |
| 11488 | } |
| 11489 | |
| 11490 | quote = '\''; |
| 11491 | if (squote) { |
| 11492 | if (dquote) |
| 11493 | /* Both squote and dquote present. Use squote, |
| 11494 | and escape them */ |
| 11495 | osize += squote; |
| 11496 | else |
| 11497 | quote = '"'; |
| 11498 | } |
| 11499 | |
| 11500 | repr = PyUnicode_New(osize, max); |
| 11501 | if (repr == NULL) |
| 11502 | return NULL; |
| 11503 | okind = PyUnicode_KIND(repr); |
| 11504 | odata = PyUnicode_DATA(repr); |
| 11505 | |
| 11506 | PyUnicode_WRITE(okind, odata, 0, quote); |
| 11507 | PyUnicode_WRITE(okind, odata, osize-1, quote); |
| 11508 | |
| 11509 | for (i = 0, o = 1; i < isize; i++) { |
| 11510 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11511 | |
| 11512 | /* Escape quotes and backslashes */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11513 | if ((ch == quote) || (ch == '\\')) { |
| 11514 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 11515 | PyUnicode_WRITE(okind, odata, o++, ch); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11516 | continue; |
| 11517 | } |
| 11518 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11519 | /* Map special whitespace to '\t', \n', '\r' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11520 | if (ch == '\t') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11521 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 11522 | PyUnicode_WRITE(okind, odata, o++, 't'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11523 | } |
| 11524 | else if (ch == '\n') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11525 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 11526 | PyUnicode_WRITE(okind, odata, o++, 'n'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11527 | } |
| 11528 | else if (ch == '\r') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11529 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 11530 | PyUnicode_WRITE(okind, odata, o++, 'r'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11531 | } |
| 11532 | |
| 11533 | /* Map non-printable US ASCII to '\xhh' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11534 | else if (ch < ' ' || ch == 0x7F) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11535 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 11536 | PyUnicode_WRITE(okind, odata, o++, 'x'); |
| 11537 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 4) & 0x000F]); |
| 11538 | PyUnicode_WRITE(okind, odata, o++, hexdigits[ch & 0x000F]); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11539 | } |
| 11540 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11541 | /* Copy ASCII characters as-is */ |
| 11542 | else if (ch < 0x7F) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11543 | PyUnicode_WRITE(okind, odata, o++, ch); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11544 | } |
| 11545 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11546 | /* Non-ASCII characters */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11547 | else { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11548 | /* Map Unicode whitespace and control characters |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11549 | (categories Z* and C* except ASCII space) |
| 11550 | */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11551 | if (!Py_UNICODE_ISPRINTABLE(ch)) { |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11552 | /* Map 8-bit characters to '\xhh' */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11553 | if (ch <= 0xff) { |
| 11554 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 11555 | PyUnicode_WRITE(okind, odata, o++, 'x'); |
| 11556 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 4) & 0x000F]); |
| 11557 | PyUnicode_WRITE(okind, odata, o++, hexdigits[ch & 0x000F]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11558 | } |
| 11559 | /* Map 21-bit characters to '\U00xxxxxx' */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11560 | else if (ch >= 0x10000) { |
| 11561 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 11562 | PyUnicode_WRITE(okind, odata, o++, 'U'); |
| 11563 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 28) & 0xF]); |
| 11564 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 24) & 0xF]); |
| 11565 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 20) & 0xF]); |
| 11566 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 16) & 0xF]); |
| 11567 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 12) & 0xF]); |
| 11568 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 8) & 0xF]); |
| 11569 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 4) & 0xF]); |
| 11570 | PyUnicode_WRITE(okind, odata, o++, hexdigits[ch & 0xF]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11571 | } |
| 11572 | /* Map 16-bit characters to '\uxxxx' */ |
| 11573 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11574 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 11575 | PyUnicode_WRITE(okind, odata, o++, 'u'); |
| 11576 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 12) & 0xF]); |
| 11577 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 8) & 0xF]); |
| 11578 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 4) & 0xF]); |
| 11579 | PyUnicode_WRITE(okind, odata, o++, hexdigits[ch & 0xF]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11580 | } |
| 11581 | } |
| 11582 | /* Copy characters as-is */ |
| 11583 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11584 | PyUnicode_WRITE(okind, odata, o++, ch); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11585 | } |
| 11586 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11587 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11588 | /* Closing quote already added at the beginning */ |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 11589 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11590 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11591 | } |
| 11592 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11593 | PyDoc_STRVAR(rfind__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11594 | "S.rfind(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11595 | \n\ |
| 11596 | Return the highest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 11597 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11598 | arguments start and end are interpreted as in slice notation.\n\ |
| 11599 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11600 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11601 | |
| 11602 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11603 | unicode_rfind(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11604 | { |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11605 | PyUnicodeObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 11606 | Py_ssize_t start; |
| 11607 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11608 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11609 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11610 | if (!stringlib_parse_args_finds_unicode("rfind", args, &substring, |
| 11611 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11612 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11613 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11614 | if (PyUnicode_READY(self) == -1) |
| 11615 | return NULL; |
| 11616 | if (PyUnicode_READY(substring) == -1) |
| 11617 | return NULL; |
| 11618 | |
| 11619 | result = any_find_slice( |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 11620 | asciilib_rfind_slice, ucs1lib_rfind_slice, |
| 11621 | ucs2lib_rfind_slice, ucs4lib_rfind_slice, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11622 | self, (PyObject*)substring, start, end |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11623 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11624 | |
| 11625 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11626 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11627 | if (result == -2) |
| 11628 | return NULL; |
| 11629 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 11630 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11631 | } |
| 11632 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11633 | PyDoc_STRVAR(rindex__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11634 | "S.rindex(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11635 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11636 | 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] | 11637 | |
| 11638 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11639 | unicode_rindex(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11640 | { |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11641 | PyUnicodeObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 11642 | Py_ssize_t start; |
| 11643 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11644 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11645 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11646 | if (!stringlib_parse_args_finds_unicode("rindex", args, &substring, |
| 11647 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11648 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11649 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11650 | if (PyUnicode_READY(self) == -1) |
| 11651 | return NULL; |
| 11652 | if (PyUnicode_READY(substring) == -1) |
| 11653 | return NULL; |
| 11654 | |
| 11655 | result = any_find_slice( |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 11656 | asciilib_rfind_slice, ucs1lib_rfind_slice, |
| 11657 | ucs2lib_rfind_slice, ucs4lib_rfind_slice, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11658 | self, (PyObject*)substring, start, end |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11659 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11660 | |
| 11661 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11662 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11663 | if (result == -2) |
| 11664 | return NULL; |
| 11665 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11666 | if (result < 0) { |
| 11667 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 11668 | return NULL; |
| 11669 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11670 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 11671 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11672 | } |
| 11673 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11674 | PyDoc_STRVAR(rjust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11675 | "S.rjust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11676 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 11677 | 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] | 11678 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11679 | |
| 11680 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11681 | unicode_rjust(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11682 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11683 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11684 | Py_UCS4 fillchar = ' '; |
| 11685 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11686 | 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] | 11687 | return NULL; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 11688 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11689 | if (PyUnicode_READY(self) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11690 | return NULL; |
| 11691 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11692 | if (_PyUnicode_LENGTH(self) >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11693 | Py_INCREF(self); |
| 11694 | return (PyObject*) self; |
| 11695 | } |
| 11696 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11697 | return (PyObject*) pad(self, width - _PyUnicode_LENGTH(self), 0, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11698 | } |
| 11699 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 11700 | PyObject * |
| 11701 | PyUnicode_Split(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11702 | { |
| 11703 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11704 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11705 | s = PyUnicode_FromObject(s); |
| 11706 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11707 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11708 | if (sep != NULL) { |
| 11709 | sep = PyUnicode_FromObject(sep); |
| 11710 | if (sep == NULL) { |
| 11711 | Py_DECREF(s); |
| 11712 | return NULL; |
| 11713 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11714 | } |
| 11715 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11716 | result = split(s, sep, maxsplit); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11717 | |
| 11718 | Py_DECREF(s); |
| 11719 | Py_XDECREF(sep); |
| 11720 | return result; |
| 11721 | } |
| 11722 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11723 | PyDoc_STRVAR(split__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11724 | "S.split([sep[, maxsplit]]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11725 | \n\ |
| 11726 | Return a list of the words in S, using sep as the\n\ |
| 11727 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 11728 | 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] | 11729 | whitespace string is a separator and empty strings are\n\ |
| 11730 | removed from the result."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11731 | |
| 11732 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11733 | unicode_split(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11734 | { |
| 11735 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11736 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11737 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11738 | if (!PyArg_ParseTuple(args, "|On:split", &substring, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11739 | return NULL; |
| 11740 | |
| 11741 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11742 | return split(self, NULL, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11743 | else if (PyUnicode_Check(substring)) |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11744 | return split(self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11745 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11746 | return PyUnicode_Split((PyObject *)self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11747 | } |
| 11748 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11749 | PyObject * |
| 11750 | PyUnicode_Partition(PyObject *str_in, PyObject *sep_in) |
| 11751 | { |
| 11752 | PyObject* str_obj; |
| 11753 | PyObject* sep_obj; |
| 11754 | PyObject* out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11755 | int kind1, kind2, kind; |
| 11756 | void *buf1 = NULL, *buf2 = NULL; |
| 11757 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11758 | |
| 11759 | str_obj = PyUnicode_FromObject(str_in); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11760 | if (!str_obj || PyUnicode_READY(str_obj) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11761 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11762 | sep_obj = PyUnicode_FromObject(sep_in); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11763 | if (!sep_obj || PyUnicode_READY(sep_obj) == -1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11764 | Py_DECREF(str_obj); |
| 11765 | return NULL; |
| 11766 | } |
| 11767 | |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 11768 | kind1 = PyUnicode_KIND(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11769 | kind2 = PyUnicode_KIND(sep_obj); |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 11770 | kind = Py_MAX(kind1, kind2); |
| 11771 | buf1 = PyUnicode_DATA(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11772 | if (kind1 != kind) |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 11773 | buf1 = _PyUnicode_AsKind(str_obj, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11774 | if (!buf1) |
| 11775 | goto onError; |
| 11776 | buf2 = PyUnicode_DATA(sep_obj); |
| 11777 | if (kind2 != kind) |
| 11778 | buf2 = _PyUnicode_AsKind(sep_obj, kind); |
| 11779 | if (!buf2) |
| 11780 | goto onError; |
| 11781 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 11782 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
| 11783 | |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 11784 | switch(PyUnicode_KIND(str_obj)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11785 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 11786 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sep_obj)) |
| 11787 | out = asciilib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 11788 | else |
| 11789 | 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] | 11790 | break; |
| 11791 | case PyUnicode_2BYTE_KIND: |
| 11792 | out = ucs2lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 11793 | break; |
| 11794 | case PyUnicode_4BYTE_KIND: |
| 11795 | out = ucs4lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 11796 | break; |
| 11797 | default: |
| 11798 | assert(0); |
| 11799 | out = 0; |
| 11800 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11801 | |
| 11802 | Py_DECREF(sep_obj); |
| 11803 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11804 | if (kind1 != kind) |
| 11805 | PyMem_Free(buf1); |
| 11806 | if (kind2 != kind) |
| 11807 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11808 | |
| 11809 | return out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11810 | onError: |
| 11811 | Py_DECREF(sep_obj); |
| 11812 | Py_DECREF(str_obj); |
| 11813 | if (kind1 != kind && buf1) |
| 11814 | PyMem_Free(buf1); |
| 11815 | if (kind2 != kind && buf2) |
| 11816 | PyMem_Free(buf2); |
| 11817 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11818 | } |
| 11819 | |
| 11820 | |
| 11821 | PyObject * |
| 11822 | PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in) |
| 11823 | { |
| 11824 | PyObject* str_obj; |
| 11825 | PyObject* sep_obj; |
| 11826 | PyObject* out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11827 | int kind1, kind2, kind; |
| 11828 | void *buf1 = NULL, *buf2 = NULL; |
| 11829 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11830 | |
| 11831 | str_obj = PyUnicode_FromObject(str_in); |
| 11832 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11833 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11834 | sep_obj = PyUnicode_FromObject(sep_in); |
| 11835 | if (!sep_obj) { |
| 11836 | Py_DECREF(str_obj); |
| 11837 | return NULL; |
| 11838 | } |
| 11839 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11840 | kind1 = PyUnicode_KIND(str_in); |
| 11841 | kind2 = PyUnicode_KIND(sep_obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 11842 | kind = Py_MAX(kind1, kind2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11843 | buf1 = PyUnicode_DATA(str_in); |
| 11844 | if (kind1 != kind) |
| 11845 | buf1 = _PyUnicode_AsKind(str_in, kind); |
| 11846 | if (!buf1) |
| 11847 | goto onError; |
| 11848 | buf2 = PyUnicode_DATA(sep_obj); |
| 11849 | if (kind2 != kind) |
| 11850 | buf2 = _PyUnicode_AsKind(sep_obj, kind); |
| 11851 | if (!buf2) |
| 11852 | goto onError; |
| 11853 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 11854 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
| 11855 | |
| 11856 | switch(PyUnicode_KIND(str_in)) { |
| 11857 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 11858 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sep_obj)) |
| 11859 | out = asciilib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 11860 | else |
| 11861 | 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] | 11862 | break; |
| 11863 | case PyUnicode_2BYTE_KIND: |
| 11864 | out = ucs2lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 11865 | break; |
| 11866 | case PyUnicode_4BYTE_KIND: |
| 11867 | out = ucs4lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 11868 | break; |
| 11869 | default: |
| 11870 | assert(0); |
| 11871 | out = 0; |
| 11872 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11873 | |
| 11874 | Py_DECREF(sep_obj); |
| 11875 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11876 | if (kind1 != kind) |
| 11877 | PyMem_Free(buf1); |
| 11878 | if (kind2 != kind) |
| 11879 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11880 | |
| 11881 | return out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11882 | onError: |
| 11883 | Py_DECREF(sep_obj); |
| 11884 | Py_DECREF(str_obj); |
| 11885 | if (kind1 != kind && buf1) |
| 11886 | PyMem_Free(buf1); |
| 11887 | if (kind2 != kind && buf2) |
| 11888 | PyMem_Free(buf2); |
| 11889 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11890 | } |
| 11891 | |
| 11892 | PyDoc_STRVAR(partition__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11893 | "S.partition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11894 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 11895 | 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] | 11896 | 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] | 11897 | found, return S and two empty strings."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11898 | |
| 11899 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11900 | unicode_partition(PyObject *self, PyObject *separator) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11901 | { |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11902 | return PyUnicode_Partition(self, separator); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11903 | } |
| 11904 | |
| 11905 | PyDoc_STRVAR(rpartition__doc__, |
Ezio Melotti | 5b2b242 | 2010-01-25 11:58:28 +0000 | [diff] [blame] | 11906 | "S.rpartition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11907 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 11908 | 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] | 11909 | 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] | 11910 | separator is not found, return two empty strings and S."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11911 | |
| 11912 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11913 | unicode_rpartition(PyObject *self, PyObject *separator) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11914 | { |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11915 | return PyUnicode_RPartition(self, separator); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11916 | } |
| 11917 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 11918 | PyObject * |
| 11919 | PyUnicode_RSplit(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 11920 | { |
| 11921 | PyObject *result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11922 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 11923 | s = PyUnicode_FromObject(s); |
| 11924 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11925 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11926 | if (sep != NULL) { |
| 11927 | sep = PyUnicode_FromObject(sep); |
| 11928 | if (sep == NULL) { |
| 11929 | Py_DECREF(s); |
| 11930 | return NULL; |
| 11931 | } |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 11932 | } |
| 11933 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11934 | result = rsplit(s, sep, maxsplit); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 11935 | |
| 11936 | Py_DECREF(s); |
| 11937 | Py_XDECREF(sep); |
| 11938 | return result; |
| 11939 | } |
| 11940 | |
| 11941 | PyDoc_STRVAR(rsplit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11942 | "S.rsplit([sep[, maxsplit]]) -> list of strings\n\ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 11943 | \n\ |
| 11944 | Return a list of the words in S, using sep as the\n\ |
| 11945 | delimiter string, starting at the end of the string and\n\ |
| 11946 | working to the front. If maxsplit is given, at most maxsplit\n\ |
| 11947 | splits are done. If sep is not specified, any whitespace string\n\ |
| 11948 | is a separator."); |
| 11949 | |
| 11950 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11951 | unicode_rsplit(PyObject *self, PyObject *args) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 11952 | { |
| 11953 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11954 | Py_ssize_t maxcount = -1; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 11955 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11956 | if (!PyArg_ParseTuple(args, "|On:rsplit", &substring, &maxcount)) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 11957 | return NULL; |
| 11958 | |
| 11959 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11960 | return rsplit(self, NULL, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 11961 | else if (PyUnicode_Check(substring)) |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11962 | return rsplit(self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 11963 | else |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11964 | return PyUnicode_RSplit(self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 11965 | } |
| 11966 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11967 | PyDoc_STRVAR(splitlines__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11968 | "S.splitlines([keepends]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11969 | \n\ |
| 11970 | 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] | 11971 | 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] | 11972 | is given and true."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11973 | |
| 11974 | static PyObject* |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 11975 | unicode_splitlines(PyUnicodeObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11976 | { |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 11977 | static char *kwlist[] = {"keepends", 0}; |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 11978 | int keepends = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11979 | |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 11980 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|i:splitlines", |
| 11981 | kwlist, &keepends)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11982 | return NULL; |
| 11983 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 11984 | return PyUnicode_Splitlines((PyObject *)self, keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11985 | } |
| 11986 | |
| 11987 | static |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 11988 | PyObject *unicode_str(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11989 | { |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 11990 | if (PyUnicode_CheckExact(self)) { |
| 11991 | Py_INCREF(self); |
| 11992 | return self; |
| 11993 | } else |
| 11994 | /* Subtype -- return genuine unicode string with the same value. */ |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 11995 | return PyUnicode_Copy(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11996 | } |
| 11997 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11998 | PyDoc_STRVAR(swapcase__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11999 | "S.swapcase() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12000 | \n\ |
| 12001 | 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] | 12002 | and vice versa."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12003 | |
| 12004 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12005 | unicode_swapcase(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12006 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12007 | return fixup(self, fixswapcase); |
| 12008 | } |
| 12009 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12010 | PyDoc_STRVAR(maketrans__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12011 | "str.maketrans(x[, y[, z]]) -> dict (static method)\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12012 | \n\ |
| 12013 | Return a translation table usable for str.translate().\n\ |
| 12014 | If there is only one argument, it must be a dictionary mapping Unicode\n\ |
| 12015 | ordinals (integers) or characters to Unicode ordinals, strings or None.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12016 | Character keys will be then converted to ordinals.\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12017 | If there are two arguments, they must be strings of equal length, and\n\ |
| 12018 | in the resulting dictionary, each character in x will be mapped to the\n\ |
| 12019 | character at the same position in y. If there is a third argument, it\n\ |
| 12020 | must be a string, whose characters will be mapped to None in the result."); |
| 12021 | |
| 12022 | static PyObject* |
| 12023 | unicode_maketrans(PyUnicodeObject *null, PyObject *args) |
| 12024 | { |
| 12025 | PyObject *x, *y = NULL, *z = NULL; |
| 12026 | PyObject *new = NULL, *key, *value; |
| 12027 | Py_ssize_t i = 0; |
| 12028 | int res; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12029 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12030 | if (!PyArg_ParseTuple(args, "O|UU:maketrans", &x, &y, &z)) |
| 12031 | return NULL; |
| 12032 | new = PyDict_New(); |
| 12033 | if (!new) |
| 12034 | return NULL; |
| 12035 | if (y != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12036 | int x_kind, y_kind, z_kind; |
| 12037 | void *x_data, *y_data, *z_data; |
| 12038 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12039 | /* x must be a string too, of equal length */ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12040 | if (!PyUnicode_Check(x)) { |
| 12041 | PyErr_SetString(PyExc_TypeError, "first maketrans argument must " |
| 12042 | "be a string if there is a second argument"); |
| 12043 | goto err; |
| 12044 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12045 | if (PyUnicode_GET_LENGTH(x) != PyUnicode_GET_LENGTH(y)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12046 | PyErr_SetString(PyExc_ValueError, "the first two maketrans " |
| 12047 | "arguments must have equal length"); |
| 12048 | goto err; |
| 12049 | } |
| 12050 | /* 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] | 12051 | x_kind = PyUnicode_KIND(x); |
| 12052 | y_kind = PyUnicode_KIND(y); |
| 12053 | x_data = PyUnicode_DATA(x); |
| 12054 | y_data = PyUnicode_DATA(y); |
| 12055 | for (i = 0; i < PyUnicode_GET_LENGTH(x); i++) { |
| 12056 | key = PyLong_FromLong(PyUnicode_READ(x_kind, x_data, i)); |
| 12057 | value = PyLong_FromLong(PyUnicode_READ(y_kind, y_data, i)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12058 | if (!key || !value) |
| 12059 | goto err; |
| 12060 | res = PyDict_SetItem(new, key, value); |
| 12061 | Py_DECREF(key); |
| 12062 | Py_DECREF(value); |
| 12063 | if (res < 0) |
| 12064 | goto err; |
| 12065 | } |
| 12066 | /* create entries for deleting chars in z */ |
| 12067 | if (z != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12068 | z_kind = PyUnicode_KIND(z); |
| 12069 | z_data = PyUnicode_DATA(z); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12070 | for (i = 0; i < PyUnicode_GET_SIZE(z); i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12071 | key = PyLong_FromLong(PyUnicode_READ(z_kind, z_data, i)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12072 | if (!key) |
| 12073 | goto err; |
| 12074 | res = PyDict_SetItem(new, key, Py_None); |
| 12075 | Py_DECREF(key); |
| 12076 | if (res < 0) |
| 12077 | goto err; |
| 12078 | } |
| 12079 | } |
| 12080 | } else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12081 | int kind; |
| 12082 | void *data; |
| 12083 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12084 | /* x must be a dict */ |
Raymond Hettinger | 3ad0576 | 2009-05-29 22:11:22 +0000 | [diff] [blame] | 12085 | if (!PyDict_CheckExact(x)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12086 | PyErr_SetString(PyExc_TypeError, "if you give only one argument " |
| 12087 | "to maketrans it must be a dict"); |
| 12088 | goto err; |
| 12089 | } |
| 12090 | /* copy entries into the new dict, converting string keys to int keys */ |
| 12091 | while (PyDict_Next(x, &i, &key, &value)) { |
| 12092 | if (PyUnicode_Check(key)) { |
| 12093 | /* convert string keys to integer keys */ |
| 12094 | PyObject *newkey; |
| 12095 | if (PyUnicode_GET_SIZE(key) != 1) { |
| 12096 | PyErr_SetString(PyExc_ValueError, "string keys in translate " |
| 12097 | "table must be of length 1"); |
| 12098 | goto err; |
| 12099 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12100 | kind = PyUnicode_KIND(key); |
| 12101 | data = PyUnicode_DATA(key); |
| 12102 | newkey = PyLong_FromLong(PyUnicode_READ(kind, data, 0)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12103 | if (!newkey) |
| 12104 | goto err; |
| 12105 | res = PyDict_SetItem(new, newkey, value); |
| 12106 | Py_DECREF(newkey); |
| 12107 | if (res < 0) |
| 12108 | goto err; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12109 | } else if (PyLong_Check(key)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12110 | /* just keep integer keys */ |
| 12111 | if (PyDict_SetItem(new, key, value) < 0) |
| 12112 | goto err; |
| 12113 | } else { |
| 12114 | PyErr_SetString(PyExc_TypeError, "keys in translate table must " |
| 12115 | "be strings or integers"); |
| 12116 | goto err; |
| 12117 | } |
| 12118 | } |
| 12119 | } |
| 12120 | return new; |
| 12121 | err: |
| 12122 | Py_DECREF(new); |
| 12123 | return NULL; |
| 12124 | } |
| 12125 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12126 | PyDoc_STRVAR(translate__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12127 | "S.translate(table) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12128 | \n\ |
| 12129 | Return a copy of the string S, where all characters have been mapped\n\ |
| 12130 | through the given translation table, which must be a mapping of\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12131 | Unicode ordinals to Unicode ordinals, strings, or None.\n\ |
Walter Dörwald | 5c1ee17 | 2002-09-04 20:31:32 +0000 | [diff] [blame] | 12132 | Unmapped characters are left untouched. Characters mapped to None\n\ |
| 12133 | are deleted."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12134 | |
| 12135 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12136 | unicode_translate(PyObject *self, PyObject *table) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12137 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12138 | return _PyUnicode_TranslateCharmap(self, table, "ignore"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12139 | } |
| 12140 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12141 | PyDoc_STRVAR(upper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12142 | "S.upper() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12143 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12144 | Return a copy of S converted to uppercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12145 | |
| 12146 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12147 | unicode_upper(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12148 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12149 | return fixup(self, fixupper); |
| 12150 | } |
| 12151 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12152 | PyDoc_STRVAR(zfill__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12153 | "S.zfill(width) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12154 | \n\ |
Benjamin Peterson | 9aa4299 | 2008-09-10 21:57:34 +0000 | [diff] [blame] | 12155 | Pad a numeric string S with zeros on the left, to fill a field\n\ |
| 12156 | of the specified width. The string S is never truncated."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12157 | |
| 12158 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12159 | unicode_zfill(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12160 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12161 | Py_ssize_t fill; |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12162 | PyObject *u; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12163 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12164 | int kind; |
| 12165 | void *data; |
| 12166 | Py_UCS4 chr; |
| 12167 | |
| 12168 | if (PyUnicode_READY(self) == -1) |
| 12169 | return NULL; |
| 12170 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12171 | if (!PyArg_ParseTuple(args, "n:zfill", &width)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12172 | return NULL; |
| 12173 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12174 | if (PyUnicode_GET_LENGTH(self) >= width) { |
Walter Dörwald | 0fe940c | 2002-04-15 18:42:15 +0000 | [diff] [blame] | 12175 | if (PyUnicode_CheckExact(self)) { |
| 12176 | Py_INCREF(self); |
| 12177 | return (PyObject*) self; |
| 12178 | } |
| 12179 | else |
Victor Stinner | 2219e0a | 2011-10-01 01:16:59 +0200 | [diff] [blame] | 12180 | return PyUnicode_Copy((PyObject*)self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12181 | } |
| 12182 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12183 | fill = width - _PyUnicode_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12184 | |
| 12185 | u = pad(self, fill, 0, '0'); |
| 12186 | |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 12187 | if (u == NULL) |
| 12188 | return NULL; |
| 12189 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12190 | kind = PyUnicode_KIND(u); |
| 12191 | data = PyUnicode_DATA(u); |
| 12192 | chr = PyUnicode_READ(kind, data, fill); |
| 12193 | |
| 12194 | if (chr == '+' || chr == '-') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12195 | /* move sign to beginning of string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12196 | PyUnicode_WRITE(kind, data, 0, chr); |
| 12197 | PyUnicode_WRITE(kind, data, fill, '0'); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12198 | } |
| 12199 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12200 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12201 | return (PyObject*) u; |
| 12202 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12203 | |
| 12204 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12205 | static PyObject * |
| 12206 | unicode__decimal2ascii(PyObject *self) |
| 12207 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12208 | return PyUnicode_TransformDecimalAndSpaceToASCII(self); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12209 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12210 | #endif |
| 12211 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12212 | PyDoc_STRVAR(startswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12213 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12214 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 12215 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 12216 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12217 | With optional end, stop comparing S at that position.\n\ |
| 12218 | prefix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12219 | |
| 12220 | static PyObject * |
| 12221 | unicode_startswith(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12222 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12223 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12224 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12225 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12226 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12227 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12228 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12229 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12230 | if (!stringlib_parse_args_finds("startswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12231 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12232 | if (PyTuple_Check(subobj)) { |
| 12233 | Py_ssize_t i; |
| 12234 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 12235 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12236 | PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12237 | if (substring == NULL) |
| 12238 | return NULL; |
| 12239 | result = tailmatch(self, substring, start, end, -1); |
| 12240 | Py_DECREF(substring); |
| 12241 | if (result) { |
| 12242 | Py_RETURN_TRUE; |
| 12243 | } |
| 12244 | } |
| 12245 | /* nothing matched */ |
| 12246 | Py_RETURN_FALSE; |
| 12247 | } |
| 12248 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12249 | if (substring == NULL) { |
| 12250 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 12251 | PyErr_Format(PyExc_TypeError, "startswith first arg must be str or " |
| 12252 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12253 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12254 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12255 | result = tailmatch(self, substring, start, end, -1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12256 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12257 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12258 | } |
| 12259 | |
| 12260 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12261 | PyDoc_STRVAR(endswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12262 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12263 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 12264 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 12265 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12266 | With optional end, stop comparing S at that position.\n\ |
| 12267 | suffix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12268 | |
| 12269 | static PyObject * |
| 12270 | unicode_endswith(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12271 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12272 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12273 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12274 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12275 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12276 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12277 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12278 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12279 | if (!stringlib_parse_args_finds("endswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12280 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12281 | if (PyTuple_Check(subobj)) { |
| 12282 | Py_ssize_t i; |
| 12283 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 12284 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12285 | PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12286 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12287 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12288 | result = tailmatch(self, substring, start, end, +1); |
| 12289 | Py_DECREF(substring); |
| 12290 | if (result) { |
| 12291 | Py_RETURN_TRUE; |
| 12292 | } |
| 12293 | } |
| 12294 | Py_RETURN_FALSE; |
| 12295 | } |
| 12296 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12297 | if (substring == NULL) { |
| 12298 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 12299 | PyErr_Format(PyExc_TypeError, "endswith first arg must be str or " |
| 12300 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12301 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12302 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12303 | result = tailmatch(self, substring, start, end, +1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12304 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12305 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12306 | } |
| 12307 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12308 | #include "stringlib/unicode_format.h" |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12309 | |
| 12310 | PyDoc_STRVAR(format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12311 | "S.format(*args, **kwargs) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12312 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 12313 | Return a formatted version of S, using substitutions from args and kwargs.\n\ |
| 12314 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12315 | |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 12316 | PyDoc_STRVAR(format_map__doc__, |
| 12317 | "S.format_map(mapping) -> str\n\ |
| 12318 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 12319 | Return a formatted version of S, using substitutions from mapping.\n\ |
| 12320 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 12321 | |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 12322 | static PyObject * |
| 12323 | unicode__format__(PyObject* self, PyObject* args) |
| 12324 | { |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12325 | PyObject *format_spec, *out; |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 12326 | |
| 12327 | if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) |
| 12328 | return NULL; |
| 12329 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12330 | out = _PyUnicode_FormatAdvanced(self, format_spec, 0, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12331 | PyUnicode_GET_LENGTH(format_spec)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12332 | if (out != NULL) |
| 12333 | assert(_PyUnicode_CheckConsistency(out, 1)); |
| 12334 | return out; |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 12335 | } |
| 12336 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12337 | PyDoc_STRVAR(p_format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12338 | "S.__format__(format_spec) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12339 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 12340 | Return a formatted version of S as described by format_spec."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12341 | |
| 12342 | static PyObject * |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 12343 | unicode__sizeof__(PyUnicodeObject *v) |
| 12344 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12345 | Py_ssize_t size; |
| 12346 | |
| 12347 | /* If it's a compact object, account for base structure + |
| 12348 | character data. */ |
| 12349 | if (PyUnicode_IS_COMPACT_ASCII(v)) |
| 12350 | size = sizeof(PyASCIIObject) + PyUnicode_GET_LENGTH(v) + 1; |
| 12351 | else if (PyUnicode_IS_COMPACT(v)) |
| 12352 | size = sizeof(PyCompactUnicodeObject) + |
| 12353 | (PyUnicode_GET_LENGTH(v) + 1) * PyUnicode_CHARACTER_SIZE(v); |
| 12354 | else { |
| 12355 | /* If it is a two-block object, account for base object, and |
| 12356 | for character block if present. */ |
| 12357 | size = sizeof(PyUnicodeObject); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 12358 | if (_PyUnicode_DATA_ANY(v)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12359 | size += (PyUnicode_GET_LENGTH(v) + 1) * |
| 12360 | PyUnicode_CHARACTER_SIZE(v); |
| 12361 | } |
| 12362 | /* 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] | 12363 | with the data pointer. Check if the data is not shared. */ |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 12364 | if (_PyUnicode_HAS_WSTR_MEMORY(v)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12365 | size += (PyUnicode_WSTR_LENGTH(v) + 1) * sizeof(wchar_t); |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 12366 | if (_PyUnicode_HAS_UTF8_MEMORY(v)) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 12367 | size += PyUnicode_UTF8_LENGTH(v) + 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12368 | |
| 12369 | return PyLong_FromSsize_t(size); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 12370 | } |
| 12371 | |
| 12372 | PyDoc_STRVAR(sizeof__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12373 | "S.__sizeof__() -> size of S in memory, in bytes"); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 12374 | |
| 12375 | static PyObject * |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 12376 | unicode_getnewargs(PyObject *v) |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 12377 | { |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 12378 | PyObject *copy = PyUnicode_Copy(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12379 | if (!copy) |
| 12380 | return NULL; |
| 12381 | return Py_BuildValue("(N)", copy); |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 12382 | } |
| 12383 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12384 | static PyMethodDef unicode_methods[] = { |
| 12385 | |
| 12386 | /* Order is according to common usage: often used methods should |
| 12387 | appear first, since lookup is done sequentially. */ |
| 12388 | |
Benjamin Peterson | 28a4dce | 2010-12-12 01:33:04 +0000 | [diff] [blame] | 12389 | {"encode", (PyCFunction) unicode_encode, METH_VARARGS | METH_KEYWORDS, encode__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12390 | {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, |
| 12391 | {"split", (PyCFunction) unicode_split, METH_VARARGS, split__doc__}, |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12392 | {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS, rsplit__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12393 | {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, |
| 12394 | {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, |
| 12395 | {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, |
| 12396 | {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, |
| 12397 | {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, |
| 12398 | {"expandtabs", (PyCFunction) unicode_expandtabs, METH_VARARGS, expandtabs__doc__}, |
| 12399 | {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12400 | {"partition", (PyCFunction) unicode_partition, METH_O, partition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12401 | {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, |
| 12402 | {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, |
| 12403 | {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12404 | {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12405 | {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, |
| 12406 | {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, |
| 12407 | {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12408 | {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12409 | {"rpartition", (PyCFunction) unicode_rpartition, METH_O, rpartition__doc__}, |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12410 | {"splitlines", (PyCFunction) unicode_splitlines, METH_VARARGS | METH_KEYWORDS, splitlines__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12411 | {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12412 | {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, |
| 12413 | {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, |
| 12414 | {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, |
| 12415 | {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, |
| 12416 | {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, |
| 12417 | {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, |
| 12418 | {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, |
| 12419 | {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, |
| 12420 | {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, |
| 12421 | {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, |
| 12422 | {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, |
| 12423 | {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, |
| 12424 | {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, |
| 12425 | {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 12426 | {"isidentifier", (PyCFunction) unicode_isidentifier, METH_NOARGS, isidentifier__doc__}, |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12427 | {"isprintable", (PyCFunction) unicode_isprintable, METH_NOARGS, isprintable__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12428 | {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, |
Eric Smith | 9cd1e09 | 2007-08-31 18:39:38 +0000 | [diff] [blame] | 12429 | {"format", (PyCFunction) do_string_format, METH_VARARGS | METH_KEYWORDS, format__doc__}, |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 12430 | {"format_map", (PyCFunction) do_string_format_map, METH_O, format_map__doc__}, |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 12431 | {"__format__", (PyCFunction) unicode__format__, METH_VARARGS, p_format__doc__}, |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12432 | {"maketrans", (PyCFunction) unicode_maketrans, |
| 12433 | METH_VARARGS | METH_STATIC, maketrans__doc__}, |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 12434 | {"__sizeof__", (PyCFunction) unicode__sizeof__, METH_NOARGS, sizeof__doc__}, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 12435 | #if 0 |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12436 | {"capwords", (PyCFunction) unicode_capwords, METH_NOARGS, capwords__doc__}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12437 | #endif |
| 12438 | |
| 12439 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12440 | /* These methods are just used for debugging the implementation. */ |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12441 | {"_decimal2ascii", (PyCFunction) unicode__decimal2ascii, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12442 | #endif |
| 12443 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12444 | {"__getnewargs__", (PyCFunction)unicode_getnewargs, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12445 | {NULL, NULL} |
| 12446 | }; |
| 12447 | |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 12448 | static PyObject * |
| 12449 | unicode_mod(PyObject *v, PyObject *w) |
| 12450 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 12451 | if (!PyUnicode_Check(v)) |
| 12452 | Py_RETURN_NOTIMPLEMENTED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12453 | return PyUnicode_Format(v, w); |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 12454 | } |
| 12455 | |
| 12456 | static PyNumberMethods unicode_as_number = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12457 | 0, /*nb_add*/ |
| 12458 | 0, /*nb_subtract*/ |
| 12459 | 0, /*nb_multiply*/ |
| 12460 | unicode_mod, /*nb_remainder*/ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 12461 | }; |
| 12462 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12463 | static PySequenceMethods unicode_as_sequence = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12464 | (lenfunc) unicode_length, /* sq_length */ |
| 12465 | PyUnicode_Concat, /* sq_concat */ |
| 12466 | (ssizeargfunc) unicode_repeat, /* sq_repeat */ |
| 12467 | (ssizeargfunc) unicode_getitem, /* sq_item */ |
| 12468 | 0, /* sq_slice */ |
| 12469 | 0, /* sq_ass_item */ |
| 12470 | 0, /* sq_ass_slice */ |
| 12471 | PyUnicode_Contains, /* sq_contains */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12472 | }; |
| 12473 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12474 | static PyObject* |
| 12475 | unicode_subscript(PyUnicodeObject* self, PyObject* item) |
| 12476 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12477 | if (PyUnicode_READY(self) == -1) |
| 12478 | return NULL; |
| 12479 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 12480 | if (PyIndex_Check(item)) { |
| 12481 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12482 | if (i == -1 && PyErr_Occurred()) |
| 12483 | return NULL; |
| 12484 | if (i < 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12485 | i += PyUnicode_GET_LENGTH(self); |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 12486 | return unicode_getitem((PyObject*)self, i); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12487 | } else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12488 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 12489 | PyObject *result; |
| 12490 | void *src_data, *dest_data; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 12491 | int src_kind, dest_kind; |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 12492 | Py_UCS4 ch, max_char, kind_limit; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12493 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12494 | if (PySlice_GetIndicesEx(item, PyUnicode_GET_LENGTH(self), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12495 | &start, &stop, &step, &slicelength) < 0) { |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12496 | return NULL; |
| 12497 | } |
| 12498 | |
| 12499 | if (slicelength <= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12500 | return PyUnicode_New(0, 0); |
| 12501 | } else if (start == 0 && step == 1 && |
| 12502 | slicelength == PyUnicode_GET_LENGTH(self) && |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 12503 | PyUnicode_CheckExact(self)) { |
| 12504 | Py_INCREF(self); |
| 12505 | return (PyObject *)self; |
| 12506 | } else if (step == 1) { |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 12507 | return PyUnicode_Substring((PyObject*)self, |
| 12508 | start, start + slicelength); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12509 | } |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 12510 | /* General case */ |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 12511 | max_char = 0; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 12512 | src_kind = PyUnicode_KIND(self); |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 12513 | kind_limit = kind_maxchar_limit(src_kind); |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 12514 | src_data = PyUnicode_DATA(self); |
| 12515 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
| 12516 | ch = PyUnicode_READ(src_kind, src_data, cur); |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 12517 | if (ch > max_char) { |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 12518 | max_char = ch; |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 12519 | if (max_char >= kind_limit) |
| 12520 | break; |
| 12521 | } |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 12522 | } |
| 12523 | result = PyUnicode_New(slicelength, max_char); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 12524 | if (result == NULL) |
| 12525 | return NULL; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 12526 | dest_kind = PyUnicode_KIND(result); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 12527 | dest_data = PyUnicode_DATA(result); |
| 12528 | |
| 12529 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 12530 | Py_UCS4 ch = PyUnicode_READ(src_kind, src_data, cur); |
| 12531 | PyUnicode_WRITE(dest_kind, dest_data, i, ch); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 12532 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12533 | assert(_PyUnicode_CheckConsistency(result, 1)); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 12534 | return result; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12535 | } else { |
| 12536 | PyErr_SetString(PyExc_TypeError, "string indices must be integers"); |
| 12537 | return NULL; |
| 12538 | } |
| 12539 | } |
| 12540 | |
| 12541 | static PyMappingMethods unicode_as_mapping = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12542 | (lenfunc)unicode_length, /* mp_length */ |
| 12543 | (binaryfunc)unicode_subscript, /* mp_subscript */ |
| 12544 | (objobjargproc)0, /* mp_ass_subscript */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12545 | }; |
| 12546 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12547 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12548 | /* Helpers for PyUnicode_Format() */ |
| 12549 | |
| 12550 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12551 | 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] | 12552 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12553 | Py_ssize_t argidx = *p_argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12554 | if (argidx < arglen) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12555 | (*p_argidx)++; |
| 12556 | if (arglen < 0) |
| 12557 | return args; |
| 12558 | else |
| 12559 | return PyTuple_GetItem(args, argidx); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12560 | } |
| 12561 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12562 | "not enough arguments for format string"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12563 | return NULL; |
| 12564 | } |
| 12565 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 12566 | /* 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] | 12567 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 12568 | static PyObject * |
| 12569 | formatfloat(PyObject *v, int flags, int prec, int type) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12570 | { |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 12571 | char *p; |
| 12572 | PyObject *result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12573 | double x; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 12574 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12575 | x = PyFloat_AsDouble(v); |
| 12576 | if (x == -1.0 && PyErr_Occurred()) |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 12577 | return NULL; |
| 12578 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12579 | if (prec < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12580 | prec = 6; |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 12581 | |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 12582 | p = PyOS_double_to_string(x, type, prec, |
| 12583 | (flags & F_ALT) ? Py_DTSF_ALT : 0, NULL); |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 12584 | if (p == NULL) |
| 12585 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12586 | result = PyUnicode_DecodeASCII(p, strlen(p), NULL); |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 12587 | PyMem_Free(p); |
| 12588 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12589 | } |
| 12590 | |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 12591 | static PyObject* |
| 12592 | formatlong(PyObject *val, int flags, int prec, int type) |
| 12593 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12594 | char *buf; |
| 12595 | int len; |
| 12596 | PyObject *str; /* temporary string object. */ |
| 12597 | PyObject *result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 12598 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12599 | str = _PyBytes_FormatLong(val, flags, prec, type, &buf, &len); |
| 12600 | if (!str) |
| 12601 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12602 | result = PyUnicode_DecodeASCII(buf, len, NULL); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12603 | Py_DECREF(str); |
| 12604 | return result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 12605 | } |
| 12606 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12607 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12608 | formatchar(Py_UCS4 *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 12609 | size_t buflen, |
| 12610 | PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12611 | { |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 12612 | /* presume that the buffer is at least 3 characters long */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 12613 | if (PyUnicode_Check(v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12614 | if (PyUnicode_GET_LENGTH(v) == 1) { |
| 12615 | buf[0] = PyUnicode_READ_CHAR(v, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12616 | buf[1] = '\0'; |
| 12617 | return 1; |
| 12618 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12619 | goto onError; |
| 12620 | } |
| 12621 | else { |
| 12622 | /* Integer input truncated to a character */ |
| 12623 | long x; |
| 12624 | x = PyLong_AsLong(v); |
| 12625 | if (x == -1 && PyErr_Occurred()) |
| 12626 | goto onError; |
| 12627 | |
| 12628 | if (x < 0 || x > 0x10ffff) { |
| 12629 | PyErr_SetString(PyExc_OverflowError, |
| 12630 | "%c arg not in range(0x110000)"); |
| 12631 | return -1; |
| 12632 | } |
| 12633 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12634 | buf[0] = (Py_UCS4) x; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12635 | buf[1] = '\0'; |
| 12636 | return 1; |
| 12637 | } |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 12638 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12639 | onError: |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 12640 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12641 | "%c requires int or char"); |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 12642 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12643 | } |
| 12644 | |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 12645 | /* fmt%(v1,v2,...) is roughly equivalent to sprintf(fmt, v1, v2, ...) |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 12646 | 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] | 12647 | */ |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 12648 | #define FORMATBUFLEN (size_t)10 |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 12649 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12650 | PyObject * |
| 12651 | PyUnicode_Format(PyObject *format, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12652 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12653 | void *fmt; |
| 12654 | int fmtkind; |
| 12655 | PyObject *result; |
| 12656 | Py_UCS4 *res, *res0; |
| 12657 | Py_UCS4 max; |
| 12658 | int kind; |
| 12659 | Py_ssize_t fmtcnt, fmtpos, rescnt, reslen, arglen, argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12660 | int args_owned = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12661 | PyObject *dict = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12662 | PyUnicodeObject *uformat; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 12663 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12664 | if (format == NULL || args == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12665 | PyErr_BadInternalCall(); |
| 12666 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12667 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12668 | uformat = (PyUnicodeObject*)PyUnicode_FromObject(format); |
| 12669 | if (uformat == NULL || PyUnicode_READY(uformat) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12670 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12671 | fmt = PyUnicode_DATA(uformat); |
| 12672 | fmtkind = PyUnicode_KIND(uformat); |
| 12673 | fmtcnt = PyUnicode_GET_LENGTH(uformat); |
| 12674 | fmtpos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12675 | |
| 12676 | reslen = rescnt = fmtcnt + 100; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12677 | res = res0 = PyMem_Malloc(reslen * sizeof(Py_UCS4)); |
| 12678 | if (res0 == NULL) { |
| 12679 | PyErr_NoMemory(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12680 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12681 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12682 | |
| 12683 | if (PyTuple_Check(args)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12684 | arglen = PyTuple_Size(args); |
| 12685 | argidx = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12686 | } |
| 12687 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12688 | arglen = -1; |
| 12689 | argidx = -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12690 | } |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 12691 | if (Py_TYPE(args)->tp_as_mapping && !PyTuple_Check(args) && |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 12692 | !PyUnicode_Check(args)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12693 | dict = args; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12694 | |
| 12695 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12696 | if (PyUnicode_READ(fmtkind, fmt, fmtpos) != '%') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12697 | if (--rescnt < 0) { |
| 12698 | rescnt = fmtcnt + 100; |
| 12699 | reslen += rescnt; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12700 | res0 = PyMem_Realloc(res0, reslen*sizeof(Py_UCS4)); |
| 12701 | if (res0 == NULL){ |
| 12702 | PyErr_NoMemory(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12703 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12704 | } |
| 12705 | res = res0 + reslen - rescnt; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12706 | --rescnt; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12707 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12708 | *res++ = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12709 | } |
| 12710 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12711 | /* Got a format specifier */ |
| 12712 | int flags = 0; |
| 12713 | Py_ssize_t width = -1; |
| 12714 | int prec = -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12715 | Py_UCS4 c = '\0'; |
| 12716 | Py_UCS4 fill; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12717 | int isnumok; |
| 12718 | PyObject *v = NULL; |
| 12719 | PyObject *temp = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12720 | void *pbuf; |
| 12721 | Py_ssize_t pindex; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12722 | Py_UNICODE sign; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12723 | Py_ssize_t len, len1; |
| 12724 | Py_UCS4 formatbuf[FORMATBUFLEN]; /* For formatchar() */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12725 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12726 | fmtpos++; |
| 12727 | if (PyUnicode_READ(fmtkind, fmt, fmtpos) == '(') { |
| 12728 | Py_ssize_t keystart; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12729 | Py_ssize_t keylen; |
| 12730 | PyObject *key; |
| 12731 | int pcount = 1; |
Christian Heimes | a612dc0 | 2008-02-24 13:08:18 +0000 | [diff] [blame] | 12732 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12733 | if (dict == NULL) { |
| 12734 | PyErr_SetString(PyExc_TypeError, |
| 12735 | "format requires a mapping"); |
| 12736 | goto onError; |
| 12737 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12738 | ++fmtpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12739 | --fmtcnt; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12740 | keystart = fmtpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12741 | /* Skip over balanced parentheses */ |
| 12742 | while (pcount > 0 && --fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12743 | if (PyUnicode_READ(fmtkind, fmt, fmtpos) == ')') |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12744 | --pcount; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12745 | else if (PyUnicode_READ(fmtkind, fmt, fmtpos) == '(') |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12746 | ++pcount; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12747 | fmtpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12748 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12749 | keylen = fmtpos - keystart - 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12750 | if (fmtcnt < 0 || pcount > 0) { |
| 12751 | PyErr_SetString(PyExc_ValueError, |
| 12752 | "incomplete format key"); |
| 12753 | goto onError; |
| 12754 | } |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 12755 | key = PyUnicode_Substring((PyObject*)uformat, |
| 12756 | keystart, keystart + keylen); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12757 | if (key == NULL) |
| 12758 | goto onError; |
| 12759 | if (args_owned) { |
| 12760 | Py_DECREF(args); |
| 12761 | args_owned = 0; |
| 12762 | } |
| 12763 | args = PyObject_GetItem(dict, key); |
| 12764 | Py_DECREF(key); |
| 12765 | if (args == NULL) { |
| 12766 | goto onError; |
| 12767 | } |
| 12768 | args_owned = 1; |
| 12769 | arglen = -1; |
| 12770 | argidx = -2; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12771 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12772 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12773 | switch (c = PyUnicode_READ(fmtkind, fmt, fmtpos++)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12774 | case '-': flags |= F_LJUST; continue; |
| 12775 | case '+': flags |= F_SIGN; continue; |
| 12776 | case ' ': flags |= F_BLANK; continue; |
| 12777 | case '#': flags |= F_ALT; continue; |
| 12778 | case '0': flags |= F_ZERO; continue; |
| 12779 | } |
| 12780 | break; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12781 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12782 | if (c == '*') { |
| 12783 | v = getnextarg(args, arglen, &argidx); |
| 12784 | if (v == NULL) |
| 12785 | goto onError; |
| 12786 | if (!PyLong_Check(v)) { |
| 12787 | PyErr_SetString(PyExc_TypeError, |
| 12788 | "* wants int"); |
| 12789 | goto onError; |
| 12790 | } |
| 12791 | width = PyLong_AsLong(v); |
| 12792 | if (width == -1 && PyErr_Occurred()) |
| 12793 | goto onError; |
| 12794 | if (width < 0) { |
| 12795 | flags |= F_LJUST; |
| 12796 | width = -width; |
| 12797 | } |
| 12798 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12799 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12800 | } |
| 12801 | else if (c >= '0' && c <= '9') { |
| 12802 | width = c - '0'; |
| 12803 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12804 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12805 | if (c < '0' || c > '9') |
| 12806 | break; |
| 12807 | if ((width*10) / 10 != width) { |
| 12808 | PyErr_SetString(PyExc_ValueError, |
| 12809 | "width too big"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12810 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12811 | } |
| 12812 | width = width*10 + (c - '0'); |
| 12813 | } |
| 12814 | } |
| 12815 | if (c == '.') { |
| 12816 | prec = 0; |
| 12817 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12818 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12819 | if (c == '*') { |
| 12820 | v = getnextarg(args, arglen, &argidx); |
| 12821 | if (v == NULL) |
| 12822 | goto onError; |
| 12823 | if (!PyLong_Check(v)) { |
| 12824 | PyErr_SetString(PyExc_TypeError, |
| 12825 | "* wants int"); |
| 12826 | goto onError; |
| 12827 | } |
| 12828 | prec = PyLong_AsLong(v); |
| 12829 | if (prec == -1 && PyErr_Occurred()) |
| 12830 | goto onError; |
| 12831 | if (prec < 0) |
| 12832 | prec = 0; |
| 12833 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12834 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12835 | } |
| 12836 | else if (c >= '0' && c <= '9') { |
| 12837 | prec = c - '0'; |
| 12838 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12839 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12840 | if (c < '0' || c > '9') |
| 12841 | break; |
| 12842 | if ((prec*10) / 10 != prec) { |
| 12843 | PyErr_SetString(PyExc_ValueError, |
| 12844 | "prec too big"); |
| 12845 | goto onError; |
| 12846 | } |
| 12847 | prec = prec*10 + (c - '0'); |
| 12848 | } |
| 12849 | } |
| 12850 | } /* prec */ |
| 12851 | if (fmtcnt >= 0) { |
| 12852 | if (c == 'h' || c == 'l' || c == 'L') { |
| 12853 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12854 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12855 | } |
| 12856 | } |
| 12857 | if (fmtcnt < 0) { |
| 12858 | PyErr_SetString(PyExc_ValueError, |
| 12859 | "incomplete format"); |
| 12860 | goto onError; |
| 12861 | } |
| 12862 | if (c != '%') { |
| 12863 | v = getnextarg(args, arglen, &argidx); |
| 12864 | if (v == NULL) |
| 12865 | goto onError; |
| 12866 | } |
| 12867 | sign = 0; |
| 12868 | fill = ' '; |
| 12869 | switch (c) { |
| 12870 | |
| 12871 | case '%': |
| 12872 | pbuf = formatbuf; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12873 | kind = PyUnicode_4BYTE_KIND; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12874 | /* presume that buffer length is at least 1 */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12875 | PyUnicode_WRITE(kind, pbuf, 0, '%'); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12876 | len = 1; |
| 12877 | break; |
| 12878 | |
| 12879 | case 's': |
| 12880 | case 'r': |
| 12881 | case 'a': |
Victor Stinner | 808fc0a | 2010-03-22 12:50:40 +0000 | [diff] [blame] | 12882 | if (PyUnicode_CheckExact(v) && c == 's') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12883 | temp = v; |
| 12884 | Py_INCREF(temp); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12885 | } |
| 12886 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12887 | if (c == 's') |
| 12888 | temp = PyObject_Str(v); |
| 12889 | else if (c == 'r') |
| 12890 | temp = PyObject_Repr(v); |
| 12891 | else |
| 12892 | temp = PyObject_ASCII(v); |
| 12893 | if (temp == NULL) |
| 12894 | goto onError; |
| 12895 | if (PyUnicode_Check(temp)) |
| 12896 | /* nothing to do */; |
| 12897 | else { |
| 12898 | Py_DECREF(temp); |
| 12899 | PyErr_SetString(PyExc_TypeError, |
| 12900 | "%s argument has non-string str()"); |
| 12901 | goto onError; |
| 12902 | } |
| 12903 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12904 | if (PyUnicode_READY(temp) == -1) { |
| 12905 | Py_CLEAR(temp); |
| 12906 | goto onError; |
| 12907 | } |
| 12908 | pbuf = PyUnicode_DATA(temp); |
| 12909 | kind = PyUnicode_KIND(temp); |
| 12910 | len = PyUnicode_GET_LENGTH(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12911 | if (prec >= 0 && len > prec) |
| 12912 | len = prec; |
| 12913 | break; |
| 12914 | |
| 12915 | case 'i': |
| 12916 | case 'd': |
| 12917 | case 'u': |
| 12918 | case 'o': |
| 12919 | case 'x': |
| 12920 | case 'X': |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12921 | isnumok = 0; |
| 12922 | if (PyNumber_Check(v)) { |
| 12923 | PyObject *iobj=NULL; |
| 12924 | |
| 12925 | if (PyLong_Check(v)) { |
| 12926 | iobj = v; |
| 12927 | Py_INCREF(iobj); |
| 12928 | } |
| 12929 | else { |
| 12930 | iobj = PyNumber_Long(v); |
| 12931 | } |
| 12932 | if (iobj!=NULL) { |
| 12933 | if (PyLong_Check(iobj)) { |
| 12934 | isnumok = 1; |
Senthil Kumaran | 9ebe08d | 2011-07-03 21:03:16 -0700 | [diff] [blame] | 12935 | temp = formatlong(iobj, flags, prec, (c == 'i'? 'd': c)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12936 | Py_DECREF(iobj); |
| 12937 | if (!temp) |
| 12938 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12939 | if (PyUnicode_READY(temp) == -1) { |
| 12940 | Py_CLEAR(temp); |
| 12941 | goto onError; |
| 12942 | } |
| 12943 | pbuf = PyUnicode_DATA(temp); |
| 12944 | kind = PyUnicode_KIND(temp); |
| 12945 | len = PyUnicode_GET_LENGTH(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12946 | sign = 1; |
| 12947 | } |
| 12948 | else { |
| 12949 | Py_DECREF(iobj); |
| 12950 | } |
| 12951 | } |
| 12952 | } |
| 12953 | if (!isnumok) { |
| 12954 | PyErr_Format(PyExc_TypeError, |
| 12955 | "%%%c format: a number is required, " |
| 12956 | "not %.200s", (char)c, Py_TYPE(v)->tp_name); |
| 12957 | goto onError; |
| 12958 | } |
| 12959 | if (flags & F_ZERO) |
| 12960 | fill = '0'; |
| 12961 | break; |
| 12962 | |
| 12963 | case 'e': |
| 12964 | case 'E': |
| 12965 | case 'f': |
| 12966 | case 'F': |
| 12967 | case 'g': |
| 12968 | case 'G': |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 12969 | temp = formatfloat(v, flags, prec, c); |
| 12970 | if (!temp) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12971 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12972 | if (PyUnicode_READY(temp) == -1) { |
| 12973 | Py_CLEAR(temp); |
| 12974 | goto onError; |
| 12975 | } |
| 12976 | pbuf = PyUnicode_DATA(temp); |
| 12977 | kind = PyUnicode_KIND(temp); |
| 12978 | len = PyUnicode_GET_LENGTH(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12979 | sign = 1; |
| 12980 | if (flags & F_ZERO) |
| 12981 | fill = '0'; |
| 12982 | break; |
| 12983 | |
| 12984 | case 'c': |
| 12985 | pbuf = formatbuf; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12986 | kind = PyUnicode_4BYTE_KIND; |
Victor Stinner | b9dcffb | 2011-09-29 00:39:24 +0200 | [diff] [blame] | 12987 | len = formatchar(pbuf, Py_ARRAY_LENGTH(formatbuf), v); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12988 | if (len < 0) |
| 12989 | goto onError; |
| 12990 | break; |
| 12991 | |
| 12992 | default: |
| 12993 | PyErr_Format(PyExc_ValueError, |
| 12994 | "unsupported format character '%c' (0x%x) " |
| 12995 | "at index %zd", |
| 12996 | (31<=c && c<=126) ? (char)c : '?', |
| 12997 | (int)c, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12998 | fmtpos - 1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12999 | goto onError; |
| 13000 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13001 | /* pbuf is initialized here. */ |
| 13002 | pindex = 0; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13003 | if (sign) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13004 | if (PyUnicode_READ(kind, pbuf, pindex) == '-' || |
| 13005 | PyUnicode_READ(kind, pbuf, pindex) == '+') { |
| 13006 | sign = PyUnicode_READ(kind, pbuf, pindex++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13007 | len--; |
| 13008 | } |
| 13009 | else if (flags & F_SIGN) |
| 13010 | sign = '+'; |
| 13011 | else if (flags & F_BLANK) |
| 13012 | sign = ' '; |
| 13013 | else |
| 13014 | sign = 0; |
| 13015 | } |
| 13016 | if (width < len) |
| 13017 | width = len; |
| 13018 | if (rescnt - (sign != 0) < width) { |
| 13019 | reslen -= rescnt; |
| 13020 | rescnt = width + fmtcnt + 100; |
| 13021 | reslen += rescnt; |
| 13022 | if (reslen < 0) { |
| 13023 | Py_XDECREF(temp); |
| 13024 | PyErr_NoMemory(); |
| 13025 | goto onError; |
| 13026 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13027 | res0 = PyMem_Realloc(res0, reslen*sizeof(Py_UCS4)); |
| 13028 | if (res0 == 0) { |
| 13029 | PyErr_NoMemory(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13030 | Py_XDECREF(temp); |
| 13031 | goto onError; |
| 13032 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13033 | res = res0 + reslen - rescnt; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13034 | } |
| 13035 | if (sign) { |
| 13036 | if (fill != ' ') |
| 13037 | *res++ = sign; |
| 13038 | rescnt--; |
| 13039 | if (width > len) |
| 13040 | width--; |
| 13041 | } |
| 13042 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13043 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
| 13044 | assert(PyUnicode_READ(kind, pbuf, pindex+1) == c); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13045 | if (fill != ' ') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13046 | *res++ = PyUnicode_READ(kind, pbuf, pindex++); |
| 13047 | *res++ = PyUnicode_READ(kind, pbuf, pindex++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13048 | } |
| 13049 | rescnt -= 2; |
| 13050 | width -= 2; |
| 13051 | if (width < 0) |
| 13052 | width = 0; |
| 13053 | len -= 2; |
| 13054 | } |
| 13055 | if (width > len && !(flags & F_LJUST)) { |
| 13056 | do { |
| 13057 | --rescnt; |
| 13058 | *res++ = fill; |
| 13059 | } while (--width > len); |
| 13060 | } |
| 13061 | if (fill == ' ') { |
| 13062 | if (sign) |
| 13063 | *res++ = sign; |
| 13064 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13065 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
| 13066 | assert(PyUnicode_READ(kind, pbuf, pindex+1) == c); |
| 13067 | *res++ = PyUnicode_READ(kind, pbuf, pindex++); |
| 13068 | *res++ = PyUnicode_READ(kind, pbuf, pindex++); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13069 | } |
| 13070 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13071 | /* Copy all characters, preserving len */ |
| 13072 | len1 = len; |
| 13073 | while (len1--) { |
| 13074 | *res++ = PyUnicode_READ(kind, pbuf, pindex++); |
| 13075 | rescnt--; |
| 13076 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13077 | while (--width >= len) { |
| 13078 | --rescnt; |
| 13079 | *res++ = ' '; |
| 13080 | } |
| 13081 | if (dict && (argidx < arglen) && c != '%') { |
| 13082 | PyErr_SetString(PyExc_TypeError, |
| 13083 | "not all arguments converted during string formatting"); |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 13084 | Py_XDECREF(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13085 | goto onError; |
| 13086 | } |
| 13087 | Py_XDECREF(temp); |
| 13088 | } /* '%' */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13089 | } /* until end */ |
| 13090 | if (argidx < arglen && !dict) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13091 | PyErr_SetString(PyExc_TypeError, |
| 13092 | "not all arguments converted during string formatting"); |
| 13093 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13094 | } |
| 13095 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13096 | |
| 13097 | for (max=0, res = res0; res < res0+reslen-rescnt; res++) |
| 13098 | if (*res > max) |
| 13099 | max = *res; |
| 13100 | result = PyUnicode_New(reslen - rescnt, max); |
| 13101 | if (!result) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13102 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13103 | kind = PyUnicode_KIND(result); |
| 13104 | for (res = res0; res < res0+reslen-rescnt; res++) |
| 13105 | PyUnicode_WRITE(kind, PyUnicode_DATA(result), res-res0, *res); |
| 13106 | PyMem_Free(res0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13107 | if (args_owned) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13108 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13109 | } |
| 13110 | Py_DECREF(uformat); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13111 | assert(_PyUnicode_CheckConsistency(result, 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13112 | return (PyObject *)result; |
| 13113 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13114 | onError: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13115 | PyMem_Free(res0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13116 | Py_DECREF(uformat); |
| 13117 | if (args_owned) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13118 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13119 | } |
| 13120 | return NULL; |
| 13121 | } |
| 13122 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 13123 | static PyObject * |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 13124 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 13125 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13126 | static PyObject * |
| 13127 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 13128 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13129 | PyObject *x = NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13130 | static char *kwlist[] = {"object", "encoding", "errors", 0}; |
| 13131 | char *encoding = NULL; |
| 13132 | char *errors = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13133 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13134 | if (type != &PyUnicode_Type) |
| 13135 | return unicode_subtype_new(type, args, kwds); |
| 13136 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:str", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13137 | kwlist, &x, &encoding, &errors)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13138 | return NULL; |
| 13139 | if (x == NULL) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13140 | return (PyObject *)PyUnicode_New(0, 0); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13141 | if (encoding == NULL && errors == NULL) |
| 13142 | return PyObject_Str(x); |
| 13143 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13144 | return PyUnicode_FromEncodedObject(x, encoding, errors); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13145 | } |
| 13146 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 13147 | static PyObject * |
| 13148 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 13149 | { |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13150 | PyUnicodeObject *unicode, *self; |
| 13151 | Py_ssize_t length, char_size; |
| 13152 | int share_wstr, share_utf8; |
| 13153 | unsigned int kind; |
| 13154 | void *data; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 13155 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13156 | assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13157 | |
| 13158 | unicode = (PyUnicodeObject *)unicode_new(&PyUnicode_Type, args, kwds); |
| 13159 | if (unicode == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13160 | return NULL; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 13161 | assert(_PyUnicode_CHECK(unicode)); |
Victor Stinner | e06e145 | 2011-10-04 20:52:31 +0200 | [diff] [blame] | 13162 | if (PyUnicode_READY(unicode)) |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13163 | return NULL; |
| 13164 | |
| 13165 | self = (PyUnicodeObject *) type->tp_alloc(type, 0); |
| 13166 | if (self == NULL) { |
| 13167 | Py_DECREF(unicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13168 | return NULL; |
| 13169 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13170 | kind = PyUnicode_KIND(unicode); |
| 13171 | length = PyUnicode_GET_LENGTH(unicode); |
| 13172 | |
| 13173 | _PyUnicode_LENGTH(self) = length; |
| 13174 | _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode); |
| 13175 | _PyUnicode_STATE(self).interned = 0; |
| 13176 | _PyUnicode_STATE(self).kind = kind; |
| 13177 | _PyUnicode_STATE(self).compact = 0; |
Victor Stinner | 3cf4637 | 2011-10-03 14:42:15 +0200 | [diff] [blame] | 13178 | _PyUnicode_STATE(self).ascii = _PyUnicode_STATE(unicode).ascii; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13179 | _PyUnicode_STATE(self).ready = 1; |
| 13180 | _PyUnicode_WSTR(self) = NULL; |
| 13181 | _PyUnicode_UTF8_LENGTH(self) = 0; |
| 13182 | _PyUnicode_UTF8(self) = NULL; |
| 13183 | _PyUnicode_WSTR_LENGTH(self) = 0; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 13184 | _PyUnicode_DATA_ANY(self) = NULL; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13185 | |
| 13186 | share_utf8 = 0; |
| 13187 | share_wstr = 0; |
| 13188 | if (kind == PyUnicode_1BYTE_KIND) { |
| 13189 | char_size = 1; |
| 13190 | if (PyUnicode_MAX_CHAR_VALUE(unicode) < 128) |
| 13191 | share_utf8 = 1; |
| 13192 | } |
| 13193 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 13194 | char_size = 2; |
| 13195 | if (sizeof(wchar_t) == 2) |
| 13196 | share_wstr = 1; |
| 13197 | } |
| 13198 | else { |
| 13199 | assert(kind == PyUnicode_4BYTE_KIND); |
| 13200 | char_size = 4; |
| 13201 | if (sizeof(wchar_t) == 4) |
| 13202 | share_wstr = 1; |
| 13203 | } |
| 13204 | |
| 13205 | /* Ensure we won't overflow the length. */ |
| 13206 | if (length > (PY_SSIZE_T_MAX / char_size - 1)) { |
| 13207 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13208 | goto onError; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13209 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13210 | data = PyObject_MALLOC((length + 1) * char_size); |
| 13211 | if (data == NULL) { |
| 13212 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13213 | goto onError; |
| 13214 | } |
| 13215 | |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 13216 | _PyUnicode_DATA_ANY(self) = data; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13217 | if (share_utf8) { |
| 13218 | _PyUnicode_UTF8_LENGTH(self) = length; |
| 13219 | _PyUnicode_UTF8(self) = data; |
| 13220 | } |
| 13221 | if (share_wstr) { |
| 13222 | _PyUnicode_WSTR_LENGTH(self) = length; |
| 13223 | _PyUnicode_WSTR(self) = (wchar_t *)data; |
| 13224 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13225 | |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13226 | Py_MEMCPY(data, PyUnicode_DATA(unicode), |
| 13227 | PyUnicode_KIND_SIZE(kind, length + 1)); |
| 13228 | Py_DECREF(unicode); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13229 | assert(_PyUnicode_CheckConsistency(self, 1)); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13230 | return (PyObject *)self; |
| 13231 | |
| 13232 | onError: |
| 13233 | Py_DECREF(unicode); |
| 13234 | Py_DECREF(self); |
| 13235 | return NULL; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 13236 | } |
| 13237 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 13238 | PyDoc_STRVAR(unicode_doc, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13239 | "str(string[, encoding[, errors]]) -> str\n\ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13240 | \n\ |
Collin Winter | d474ce8 | 2007-08-07 19:42:11 +0000 | [diff] [blame] | 13241 | Create a new string object from the given encoded string.\n\ |
Skip Montanaro | 35b37a5 | 2002-07-26 16:22:46 +0000 | [diff] [blame] | 13242 | encoding defaults to the current default string encoding.\n\ |
| 13243 | errors can be 'strict', 'replace' or 'ignore' and defaults to 'strict'."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13244 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13245 | static PyObject *unicode_iter(PyObject *seq); |
| 13246 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13247 | PyTypeObject PyUnicode_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 13248 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13249 | "str", /* tp_name */ |
| 13250 | sizeof(PyUnicodeObject), /* tp_size */ |
| 13251 | 0, /* tp_itemsize */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13252 | /* Slots */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13253 | (destructor)unicode_dealloc, /* tp_dealloc */ |
| 13254 | 0, /* tp_print */ |
| 13255 | 0, /* tp_getattr */ |
| 13256 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 13257 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13258 | unicode_repr, /* tp_repr */ |
| 13259 | &unicode_as_number, /* tp_as_number */ |
| 13260 | &unicode_as_sequence, /* tp_as_sequence */ |
| 13261 | &unicode_as_mapping, /* tp_as_mapping */ |
| 13262 | (hashfunc) unicode_hash, /* tp_hash*/ |
| 13263 | 0, /* tp_call*/ |
| 13264 | (reprfunc) unicode_str, /* tp_str */ |
| 13265 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 13266 | 0, /* tp_setattro */ |
| 13267 | 0, /* tp_as_buffer */ |
| 13268 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13269 | Py_TPFLAGS_UNICODE_SUBCLASS, /* tp_flags */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13270 | unicode_doc, /* tp_doc */ |
| 13271 | 0, /* tp_traverse */ |
| 13272 | 0, /* tp_clear */ |
| 13273 | PyUnicode_RichCompare, /* tp_richcompare */ |
| 13274 | 0, /* tp_weaklistoffset */ |
| 13275 | unicode_iter, /* tp_iter */ |
| 13276 | 0, /* tp_iternext */ |
| 13277 | unicode_methods, /* tp_methods */ |
| 13278 | 0, /* tp_members */ |
| 13279 | 0, /* tp_getset */ |
| 13280 | &PyBaseObject_Type, /* tp_base */ |
| 13281 | 0, /* tp_dict */ |
| 13282 | 0, /* tp_descr_get */ |
| 13283 | 0, /* tp_descr_set */ |
| 13284 | 0, /* tp_dictoffset */ |
| 13285 | 0, /* tp_init */ |
| 13286 | 0, /* tp_alloc */ |
| 13287 | unicode_new, /* tp_new */ |
| 13288 | PyObject_Del, /* tp_free */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13289 | }; |
| 13290 | |
| 13291 | /* Initialize the Unicode implementation */ |
| 13292 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 13293 | void _PyUnicode_Init(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13294 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 13295 | int i; |
| 13296 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13297 | /* XXX - move this array to unicodectype.c ? */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13298 | Py_UCS2 linebreak[] = { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13299 | 0x000A, /* LINE FEED */ |
| 13300 | 0x000D, /* CARRIAGE RETURN */ |
| 13301 | 0x001C, /* FILE SEPARATOR */ |
| 13302 | 0x001D, /* GROUP SEPARATOR */ |
| 13303 | 0x001E, /* RECORD SEPARATOR */ |
| 13304 | 0x0085, /* NEXT LINE */ |
| 13305 | 0x2028, /* LINE SEPARATOR */ |
| 13306 | 0x2029, /* PARAGRAPH SEPARATOR */ |
| 13307 | }; |
| 13308 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 13309 | /* Init the implementation */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 13310 | unicode_empty = PyUnicode_New(0, 0); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13311 | assert(_PyUnicode_CheckConsistency(unicode_empty, 1)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13312 | if (!unicode_empty) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13313 | Py_FatalError("Can't create empty string"); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13314 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 13315 | for (i = 0; i < 256; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13316 | unicode_latin1[i] = NULL; |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 13317 | if (PyType_Ready(&PyUnicode_Type) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13318 | Py_FatalError("Can't initialize 'unicode'"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13319 | |
| 13320 | /* initialize the linebreak bloom filter */ |
| 13321 | bloom_linebreak = make_bloom_mask( |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13322 | PyUnicode_2BYTE_KIND, linebreak, |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 13323 | Py_ARRAY_LENGTH(linebreak)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13324 | |
| 13325 | PyType_Ready(&EncodingMapType); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13326 | } |
| 13327 | |
| 13328 | /* Finalize the Unicode implementation */ |
| 13329 | |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 13330 | int |
| 13331 | PyUnicode_ClearFreeList(void) |
| 13332 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13333 | return 0; |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 13334 | } |
| 13335 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13336 | void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 13337 | _PyUnicode_Fini(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13338 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 13339 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13340 | |
Guido van Rossum | 4ae8ef8 | 2000-10-03 18:09:04 +0000 | [diff] [blame] | 13341 | Py_XDECREF(unicode_empty); |
| 13342 | unicode_empty = NULL; |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 13343 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 13344 | for (i = 0; i < 256; i++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13345 | if (unicode_latin1[i]) { |
| 13346 | Py_DECREF(unicode_latin1[i]); |
| 13347 | unicode_latin1[i] = NULL; |
| 13348 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 13349 | } |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 13350 | (void)PyUnicode_ClearFreeList(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13351 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 13352 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13353 | void |
| 13354 | PyUnicode_InternInPlace(PyObject **p) |
| 13355 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13356 | register PyUnicodeObject *s = (PyUnicodeObject *)(*p); |
| 13357 | PyObject *t; |
Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 13358 | #ifdef Py_DEBUG |
| 13359 | assert(s != NULL); |
| 13360 | assert(_PyUnicode_CHECK(s)); |
| 13361 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13362 | if (s == NULL || !PyUnicode_Check(s)) |
Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 13363 | return; |
| 13364 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13365 | /* If it's a subclass, we don't really know what putting |
| 13366 | it in the interned dict might do. */ |
| 13367 | if (!PyUnicode_CheckExact(s)) |
| 13368 | return; |
| 13369 | if (PyUnicode_CHECK_INTERNED(s)) |
| 13370 | return; |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 13371 | if (_PyUnicode_READY_REPLACE(p)) { |
Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 13372 | assert(0 && "_PyUnicode_READY_REPLACE fail in PyUnicode_InternInPlace"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13373 | return; |
| 13374 | } |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 13375 | s = (PyUnicodeObject *)(*p); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13376 | if (interned == NULL) { |
| 13377 | interned = PyDict_New(); |
| 13378 | if (interned == NULL) { |
| 13379 | PyErr_Clear(); /* Don't leave an exception */ |
| 13380 | return; |
| 13381 | } |
| 13382 | } |
| 13383 | /* It might be that the GetItem call fails even |
| 13384 | though the key is present in the dictionary, |
| 13385 | namely when this happens during a stack overflow. */ |
| 13386 | Py_ALLOW_RECURSION |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13387 | t = PyDict_GetItem(interned, (PyObject *)s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13388 | Py_END_ALLOW_RECURSION |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 13389 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13390 | if (t) { |
| 13391 | Py_INCREF(t); |
| 13392 | Py_DECREF(*p); |
| 13393 | *p = t; |
| 13394 | return; |
| 13395 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13396 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13397 | PyThreadState_GET()->recursion_critical = 1; |
| 13398 | if (PyDict_SetItem(interned, (PyObject *)s, (PyObject *)s) < 0) { |
| 13399 | PyErr_Clear(); |
| 13400 | PyThreadState_GET()->recursion_critical = 0; |
| 13401 | return; |
| 13402 | } |
| 13403 | PyThreadState_GET()->recursion_critical = 0; |
| 13404 | /* The two references in interned are not counted by refcnt. |
| 13405 | The deallocator will take care of this */ |
| 13406 | Py_REFCNT(s) -= 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13407 | _PyUnicode_STATE(s).interned = SSTATE_INTERNED_MORTAL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13408 | } |
| 13409 | |
| 13410 | void |
| 13411 | PyUnicode_InternImmortal(PyObject **p) |
| 13412 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13413 | PyUnicodeObject *u = (PyUnicodeObject *)*p; |
| 13414 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13415 | PyUnicode_InternInPlace(p); |
| 13416 | if (PyUnicode_CHECK_INTERNED(*p) != SSTATE_INTERNED_IMMORTAL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13417 | _PyUnicode_STATE(u).interned = SSTATE_INTERNED_IMMORTAL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13418 | Py_INCREF(*p); |
| 13419 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13420 | } |
| 13421 | |
| 13422 | PyObject * |
| 13423 | PyUnicode_InternFromString(const char *cp) |
| 13424 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13425 | PyObject *s = PyUnicode_FromString(cp); |
| 13426 | if (s == NULL) |
| 13427 | return NULL; |
| 13428 | PyUnicode_InternInPlace(&s); |
| 13429 | return s; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13430 | } |
| 13431 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 13432 | void |
| 13433 | _Py_ReleaseInternedUnicodeStrings(void) |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13434 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13435 | PyObject *keys; |
| 13436 | PyUnicodeObject *s; |
| 13437 | Py_ssize_t i, n; |
| 13438 | Py_ssize_t immortal_size = 0, mortal_size = 0; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13439 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13440 | if (interned == NULL || !PyDict_Check(interned)) |
| 13441 | return; |
| 13442 | keys = PyDict_Keys(interned); |
| 13443 | if (keys == NULL || !PyList_Check(keys)) { |
| 13444 | PyErr_Clear(); |
| 13445 | return; |
| 13446 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13447 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13448 | /* Since _Py_ReleaseInternedUnicodeStrings() is intended to help a leak |
| 13449 | detector, interned unicode strings are not forcibly deallocated; |
| 13450 | rather, we give them their stolen references back, and then clear |
| 13451 | and DECREF the interned dict. */ |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13452 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13453 | n = PyList_GET_SIZE(keys); |
| 13454 | fprintf(stderr, "releasing %" PY_FORMAT_SIZE_T "d interned strings\n", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13455 | n); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13456 | for (i = 0; i < n; i++) { |
| 13457 | s = (PyUnicodeObject *) PyList_GET_ITEM(keys, i); |
Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 13458 | if (PyUnicode_READY(s) == -1) { |
| 13459 | assert(0 && "could not ready string"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13460 | fprintf(stderr, "could not ready string\n"); |
Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 13461 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13462 | switch (PyUnicode_CHECK_INTERNED(s)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13463 | case SSTATE_NOT_INTERNED: |
| 13464 | /* XXX Shouldn't happen */ |
| 13465 | break; |
| 13466 | case SSTATE_INTERNED_IMMORTAL: |
| 13467 | Py_REFCNT(s) += 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13468 | immortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13469 | break; |
| 13470 | case SSTATE_INTERNED_MORTAL: |
| 13471 | Py_REFCNT(s) += 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13472 | mortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13473 | break; |
| 13474 | default: |
| 13475 | Py_FatalError("Inconsistent interned string state."); |
| 13476 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13477 | _PyUnicode_STATE(s).interned = SSTATE_NOT_INTERNED; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13478 | } |
| 13479 | fprintf(stderr, "total size of all interned strings: " |
| 13480 | "%" PY_FORMAT_SIZE_T "d/%" PY_FORMAT_SIZE_T "d " |
| 13481 | "mortal/immortal\n", mortal_size, immortal_size); |
| 13482 | Py_DECREF(keys); |
| 13483 | PyDict_Clear(interned); |
| 13484 | Py_DECREF(interned); |
| 13485 | interned = NULL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13486 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13487 | |
| 13488 | |
| 13489 | /********************* Unicode Iterator **************************/ |
| 13490 | |
| 13491 | typedef struct { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13492 | PyObject_HEAD |
| 13493 | Py_ssize_t it_index; |
| 13494 | PyUnicodeObject *it_seq; /* Set to NULL when iterator is exhausted */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13495 | } unicodeiterobject; |
| 13496 | |
| 13497 | static void |
| 13498 | unicodeiter_dealloc(unicodeiterobject *it) |
| 13499 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13500 | _PyObject_GC_UNTRACK(it); |
| 13501 | Py_XDECREF(it->it_seq); |
| 13502 | PyObject_GC_Del(it); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13503 | } |
| 13504 | |
| 13505 | static int |
| 13506 | unicodeiter_traverse(unicodeiterobject *it, visitproc visit, void *arg) |
| 13507 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13508 | Py_VISIT(it->it_seq); |
| 13509 | return 0; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13510 | } |
| 13511 | |
| 13512 | static PyObject * |
| 13513 | unicodeiter_next(unicodeiterobject *it) |
| 13514 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13515 | PyUnicodeObject *seq; |
| 13516 | PyObject *item; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13517 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13518 | assert(it != NULL); |
| 13519 | seq = it->it_seq; |
| 13520 | if (seq == NULL) |
| 13521 | return NULL; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 13522 | assert(_PyUnicode_CHECK(seq)); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13523 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13524 | if (it->it_index < PyUnicode_GET_LENGTH(seq)) { |
| 13525 | int kind = PyUnicode_KIND(seq); |
| 13526 | void *data = PyUnicode_DATA(seq); |
| 13527 | Py_UCS4 chr = PyUnicode_READ(kind, data, it->it_index); |
| 13528 | item = PyUnicode_FromOrdinal(chr); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13529 | if (item != NULL) |
| 13530 | ++it->it_index; |
| 13531 | return item; |
| 13532 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13533 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13534 | Py_DECREF(seq); |
| 13535 | it->it_seq = NULL; |
| 13536 | return NULL; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13537 | } |
| 13538 | |
| 13539 | static PyObject * |
| 13540 | unicodeiter_len(unicodeiterobject *it) |
| 13541 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13542 | Py_ssize_t len = 0; |
| 13543 | if (it->it_seq) |
| 13544 | len = PyUnicode_GET_SIZE(it->it_seq) - it->it_index; |
| 13545 | return PyLong_FromSsize_t(len); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13546 | } |
| 13547 | |
| 13548 | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
| 13549 | |
| 13550 | static PyMethodDef unicodeiter_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13551 | {"__length_hint__", (PyCFunction)unicodeiter_len, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13552 | length_hint_doc}, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13553 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13554 | }; |
| 13555 | |
| 13556 | PyTypeObject PyUnicodeIter_Type = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13557 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 13558 | "str_iterator", /* tp_name */ |
| 13559 | sizeof(unicodeiterobject), /* tp_basicsize */ |
| 13560 | 0, /* tp_itemsize */ |
| 13561 | /* methods */ |
| 13562 | (destructor)unicodeiter_dealloc, /* tp_dealloc */ |
| 13563 | 0, /* tp_print */ |
| 13564 | 0, /* tp_getattr */ |
| 13565 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 13566 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13567 | 0, /* tp_repr */ |
| 13568 | 0, /* tp_as_number */ |
| 13569 | 0, /* tp_as_sequence */ |
| 13570 | 0, /* tp_as_mapping */ |
| 13571 | 0, /* tp_hash */ |
| 13572 | 0, /* tp_call */ |
| 13573 | 0, /* tp_str */ |
| 13574 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 13575 | 0, /* tp_setattro */ |
| 13576 | 0, /* tp_as_buffer */ |
| 13577 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 13578 | 0, /* tp_doc */ |
| 13579 | (traverseproc)unicodeiter_traverse, /* tp_traverse */ |
| 13580 | 0, /* tp_clear */ |
| 13581 | 0, /* tp_richcompare */ |
| 13582 | 0, /* tp_weaklistoffset */ |
| 13583 | PyObject_SelfIter, /* tp_iter */ |
| 13584 | (iternextfunc)unicodeiter_next, /* tp_iternext */ |
| 13585 | unicodeiter_methods, /* tp_methods */ |
| 13586 | 0, |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13587 | }; |
| 13588 | |
| 13589 | static PyObject * |
| 13590 | unicode_iter(PyObject *seq) |
| 13591 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13592 | unicodeiterobject *it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13593 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13594 | if (!PyUnicode_Check(seq)) { |
| 13595 | PyErr_BadInternalCall(); |
| 13596 | return NULL; |
| 13597 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13598 | if (PyUnicode_READY(seq) == -1) |
| 13599 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13600 | it = PyObject_GC_New(unicodeiterobject, &PyUnicodeIter_Type); |
| 13601 | if (it == NULL) |
| 13602 | return NULL; |
| 13603 | it->it_index = 0; |
| 13604 | Py_INCREF(seq); |
| 13605 | it->it_seq = (PyUnicodeObject *)seq; |
| 13606 | _PyObject_GC_TRACK(it); |
| 13607 | return (PyObject *)it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13608 | } |
| 13609 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13610 | #define UNIOP(x) Py_UNICODE_##x |
| 13611 | #define UNIOP_t Py_UNICODE |
| 13612 | #include "uniops.h" |
| 13613 | #undef UNIOP |
| 13614 | #undef UNIOP_t |
| 13615 | #define UNIOP(x) Py_UCS4_##x |
| 13616 | #define UNIOP_t Py_UCS4 |
| 13617 | #include "uniops.h" |
| 13618 | #undef UNIOP |
| 13619 | #undef UNIOP_t |
Victor Stinner | 331ea92 | 2010-08-10 16:37:20 +0000 | [diff] [blame] | 13620 | |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 13621 | Py_UNICODE* |
Victor Stinner | 4640860 | 2010-09-03 16:18:00 +0000 | [diff] [blame] | 13622 | PyUnicode_AsUnicodeCopy(PyObject *object) |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 13623 | { |
| 13624 | PyUnicodeObject *unicode = (PyUnicodeObject *)object; |
| 13625 | Py_UNICODE *copy; |
| 13626 | Py_ssize_t size; |
| 13627 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13628 | if (!PyUnicode_Check(unicode)) { |
| 13629 | PyErr_BadArgument(); |
| 13630 | return NULL; |
| 13631 | } |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 13632 | /* Ensure we won't overflow the size. */ |
| 13633 | if (PyUnicode_GET_SIZE(unicode) > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
| 13634 | PyErr_NoMemory(); |
| 13635 | return NULL; |
| 13636 | } |
| 13637 | size = PyUnicode_GET_SIZE(unicode) + 1; /* copy the nul character */ |
| 13638 | size *= sizeof(Py_UNICODE); |
| 13639 | copy = PyMem_Malloc(size); |
| 13640 | if (copy == NULL) { |
| 13641 | PyErr_NoMemory(); |
| 13642 | return NULL; |
| 13643 | } |
| 13644 | memcpy(copy, PyUnicode_AS_UNICODE(unicode), size); |
| 13645 | return copy; |
| 13646 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 13647 | |
Georg Brandl | 66c221e | 2010-10-14 07:04:07 +0000 | [diff] [blame] | 13648 | /* A _string module, to export formatter_parser and formatter_field_name_split |
| 13649 | to the string.Formatter class implemented in Python. */ |
| 13650 | |
| 13651 | static PyMethodDef _string_methods[] = { |
| 13652 | {"formatter_field_name_split", (PyCFunction) formatter_field_name_split, |
| 13653 | METH_O, PyDoc_STR("split the argument as a field name")}, |
| 13654 | {"formatter_parser", (PyCFunction) formatter_parser, |
| 13655 | METH_O, PyDoc_STR("parse the argument as a format string")}, |
| 13656 | {NULL, NULL} |
| 13657 | }; |
| 13658 | |
| 13659 | static struct PyModuleDef _string_module = { |
| 13660 | PyModuleDef_HEAD_INIT, |
| 13661 | "_string", |
| 13662 | PyDoc_STR("string helper module"), |
| 13663 | 0, |
| 13664 | _string_methods, |
| 13665 | NULL, |
| 13666 | NULL, |
| 13667 | NULL, |
| 13668 | NULL |
| 13669 | }; |
| 13670 | |
| 13671 | PyMODINIT_FUNC |
| 13672 | PyInit__string(void) |
| 13673 | { |
| 13674 | return PyModule_Create(&_string_module); |
| 13675 | } |
| 13676 | |
| 13677 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 13678 | #ifdef __cplusplus |
| 13679 | } |
| 13680 | #endif |