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" |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 44 | #include "bytes_methods.h" |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 45 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 46 | #ifdef MS_WINDOWS |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 47 | #include <windows.h> |
| 48 | #endif |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 49 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 50 | /* Endianness switches; defaults to little endian */ |
| 51 | |
| 52 | #ifdef WORDS_BIGENDIAN |
| 53 | # define BYTEORDER_IS_BIG_ENDIAN |
| 54 | #else |
| 55 | # define BYTEORDER_IS_LITTLE_ENDIAN |
| 56 | #endif |
| 57 | |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 58 | /* --- Globals ------------------------------------------------------------ |
| 59 | |
| 60 | The globals are initialized by the _PyUnicode_Init() API and should |
| 61 | not be used before calling that API. |
| 62 | |
| 63 | */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 64 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 65 | |
| 66 | #ifdef __cplusplus |
| 67 | extern "C" { |
| 68 | #endif |
| 69 | |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 70 | /* Maximum code point of Unicode 6.0: 0x10ffff (1,114,111) */ |
| 71 | #define MAX_UNICODE 0x10ffff |
| 72 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 73 | #ifdef Py_DEBUG |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 74 | # define _PyUnicode_CHECK(op) _PyUnicode_CheckConsistency(op, 0) |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 75 | #else |
| 76 | # define _PyUnicode_CHECK(op) PyUnicode_Check(op) |
| 77 | #endif |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 78 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 79 | #define _PyUnicode_UTF8(op) \ |
| 80 | (((PyCompactUnicodeObject*)(op))->utf8) |
| 81 | #define PyUnicode_UTF8(op) \ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 82 | (assert(_PyUnicode_CHECK(op)), \ |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 83 | assert(PyUnicode_IS_READY(op)), \ |
| 84 | PyUnicode_IS_COMPACT_ASCII(op) ? \ |
| 85 | ((char*)((PyASCIIObject*)(op) + 1)) : \ |
| 86 | _PyUnicode_UTF8(op)) |
Victor Stinner | bc8b81b | 2011-09-29 19:31:34 +0200 | [diff] [blame] | 87 | #define _PyUnicode_UTF8_LENGTH(op) \ |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 88 | (((PyCompactUnicodeObject*)(op))->utf8_length) |
| 89 | #define PyUnicode_UTF8_LENGTH(op) \ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 90 | (assert(_PyUnicode_CHECK(op)), \ |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 91 | assert(PyUnicode_IS_READY(op)), \ |
| 92 | PyUnicode_IS_COMPACT_ASCII(op) ? \ |
| 93 | ((PyASCIIObject*)(op))->length : \ |
| 94 | _PyUnicode_UTF8_LENGTH(op)) |
Victor Stinner | a5f9163 | 2011-10-04 01:07:11 +0200 | [diff] [blame] | 95 | #define _PyUnicode_WSTR(op) \ |
| 96 | (((PyASCIIObject*)(op))->wstr) |
| 97 | #define _PyUnicode_WSTR_LENGTH(op) \ |
| 98 | (((PyCompactUnicodeObject*)(op))->wstr_length) |
| 99 | #define _PyUnicode_LENGTH(op) \ |
| 100 | (((PyASCIIObject *)(op))->length) |
| 101 | #define _PyUnicode_STATE(op) \ |
| 102 | (((PyASCIIObject *)(op))->state) |
| 103 | #define _PyUnicode_HASH(op) \ |
| 104 | (((PyASCIIObject *)(op))->hash) |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 105 | #define _PyUnicode_KIND(op) \ |
| 106 | (assert(_PyUnicode_CHECK(op)), \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 107 | ((PyASCIIObject *)(op))->state.kind) |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 108 | #define _PyUnicode_GET_LENGTH(op) \ |
| 109 | (assert(_PyUnicode_CHECK(op)), \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 110 | ((PyASCIIObject *)(op))->length) |
Victor Stinner | a5f9163 | 2011-10-04 01:07:11 +0200 | [diff] [blame] | 111 | #define _PyUnicode_DATA_ANY(op) \ |
| 112 | (((PyUnicodeObject*)(op))->data.any) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 113 | |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 114 | /* Optimized version of Py_MAX() to compute the maximum character: |
| 115 | use it when your are computing the second argument of PyUnicode_New() */ |
| 116 | #define MAX_MAXCHAR(maxchar1, maxchar2) \ |
| 117 | ((maxchar1) | (maxchar2)) |
| 118 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 119 | #undef PyUnicode_READY |
| 120 | #define PyUnicode_READY(op) \ |
| 121 | (assert(_PyUnicode_CHECK(op)), \ |
| 122 | (PyUnicode_IS_READY(op) ? \ |
Victor Stinner | a5f9163 | 2011-10-04 01:07:11 +0200 | [diff] [blame] | 123 | 0 : \ |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 124 | _PyUnicode_Ready(op))) |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 125 | |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 126 | #define _PyUnicode_SHARE_UTF8(op) \ |
| 127 | (assert(_PyUnicode_CHECK(op)), \ |
| 128 | assert(!PyUnicode_IS_COMPACT_ASCII(op)), \ |
| 129 | (_PyUnicode_UTF8(op) == PyUnicode_DATA(op))) |
| 130 | #define _PyUnicode_SHARE_WSTR(op) \ |
| 131 | (assert(_PyUnicode_CHECK(op)), \ |
| 132 | (_PyUnicode_WSTR(unicode) == PyUnicode_DATA(op))) |
| 133 | |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 134 | /* true if the Unicode object has an allocated UTF-8 memory block |
| 135 | (not shared with other data) */ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 136 | #define _PyUnicode_HAS_UTF8_MEMORY(op) \ |
| 137 | (assert(_PyUnicode_CHECK(op)), \ |
| 138 | (!PyUnicode_IS_COMPACT_ASCII(op) \ |
| 139 | && _PyUnicode_UTF8(op) \ |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 140 | && _PyUnicode_UTF8(op) != PyUnicode_DATA(op))) |
| 141 | |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 142 | /* true if the Unicode object has an allocated wstr memory block |
| 143 | (not shared with other data) */ |
| 144 | #define _PyUnicode_HAS_WSTR_MEMORY(op) \ |
| 145 | (assert(_PyUnicode_CHECK(op)), \ |
| 146 | (_PyUnicode_WSTR(op) && \ |
| 147 | (!PyUnicode_IS_READY(op) || \ |
| 148 | _PyUnicode_WSTR(op) != PyUnicode_DATA(op)))) |
| 149 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 150 | /* Generic helper macro to convert characters of different types. |
| 151 | from_type and to_type have to be valid type names, begin and end |
| 152 | are pointers to the source characters which should be of type |
| 153 | "from_type *". to is a pointer of type "to_type *" and points to the |
| 154 | buffer where the result characters are written to. */ |
| 155 | #define _PyUnicode_CONVERT_BYTES(from_type, to_type, begin, end, to) \ |
| 156 | do { \ |
Antoine Pitrou | e459a08 | 2011-10-11 20:58:41 +0200 | [diff] [blame] | 157 | to_type *_to = (to_type *) to; \ |
| 158 | const from_type *_iter = (begin); \ |
| 159 | const from_type *_end = (end); \ |
| 160 | Py_ssize_t n = (_end) - (_iter); \ |
| 161 | const from_type *_unrolled_end = \ |
| 162 | _iter + (n & ~ (Py_ssize_t) 3); \ |
| 163 | while (_iter < (_unrolled_end)) { \ |
| 164 | _to[0] = (to_type) _iter[0]; \ |
| 165 | _to[1] = (to_type) _iter[1]; \ |
| 166 | _to[2] = (to_type) _iter[2]; \ |
| 167 | _to[3] = (to_type) _iter[3]; \ |
| 168 | _iter += 4; _to += 4; \ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 169 | } \ |
Antoine Pitrou | e459a08 | 2011-10-11 20:58:41 +0200 | [diff] [blame] | 170 | while (_iter < (_end)) \ |
| 171 | *_to++ = (to_type) *_iter++; \ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 172 | } while (0) |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 173 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 174 | /* This dictionary holds all interned unicode strings. Note that references |
| 175 | to strings in this dictionary are *not* counted in the string's ob_refcnt. |
| 176 | When the interned string reaches a refcnt of 0 the string deallocation |
| 177 | function will delete the reference from this dictionary. |
| 178 | |
| 179 | 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] | 180 | 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] | 181 | */ |
| 182 | static PyObject *interned; |
| 183 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 184 | /* The empty Unicode object is shared to improve performance. */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 185 | static PyObject *unicode_empty; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 186 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 187 | /* List of static strings. */ |
| 188 | static _Py_Identifier *static_strings; |
| 189 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 190 | /* Single character Unicode strings in the Latin-1 range are being |
| 191 | shared as well. */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 192 | static PyObject *unicode_latin1[256]; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 193 | |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 194 | /* Fast detection of the most frequent whitespace characters */ |
| 195 | const unsigned char _Py_ascii_whitespace[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 196 | 0, 0, 0, 0, 0, 0, 0, 0, |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 197 | /* case 0x0009: * CHARACTER TABULATION */ |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 198 | /* case 0x000A: * LINE FEED */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 199 | /* case 0x000B: * LINE TABULATION */ |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 200 | /* case 0x000C: * FORM FEED */ |
| 201 | /* case 0x000D: * CARRIAGE RETURN */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 202 | 0, 1, 1, 1, 1, 1, 0, 0, |
| 203 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 204 | /* case 0x001C: * FILE SEPARATOR */ |
| 205 | /* case 0x001D: * GROUP SEPARATOR */ |
| 206 | /* case 0x001E: * RECORD SEPARATOR */ |
| 207 | /* case 0x001F: * UNIT SEPARATOR */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 208 | 0, 0, 0, 0, 1, 1, 1, 1, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 209 | /* case 0x0020: * SPACE */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 210 | 1, 0, 0, 0, 0, 0, 0, 0, |
| 211 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 212 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 213 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 214 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 215 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 216 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 217 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 218 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 219 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 220 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 221 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 222 | 0, 0, 0, 0, 0, 0, 0, 0 |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 223 | }; |
| 224 | |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 225 | /* forward */ |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 226 | static PyUnicodeObject *_PyUnicode_New(Py_ssize_t length); |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 227 | static PyObject* get_latin1_char(unsigned char ch); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 228 | static void copy_characters( |
| 229 | PyObject *to, Py_ssize_t to_start, |
| 230 | PyObject *from, Py_ssize_t from_start, |
| 231 | Py_ssize_t how_many); |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 232 | static int unicode_modifiable(PyObject *unicode); |
| 233 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 234 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 235 | static PyObject * |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 236 | unicode_fromascii(const unsigned char *s, Py_ssize_t size); |
| 237 | static PyObject * |
| 238 | _PyUnicode_FromUCS1(const unsigned char *s, Py_ssize_t size); |
| 239 | static PyObject * |
| 240 | _PyUnicode_FromUCS2(const Py_UCS2 *s, Py_ssize_t size); |
| 241 | static PyObject * |
| 242 | _PyUnicode_FromUCS4(const Py_UCS4 *s, Py_ssize_t size); |
| 243 | |
| 244 | static PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 245 | unicode_encode_call_errorhandler(const char *errors, |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 246 | PyObject **errorHandler,const char *encoding, const char *reason, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 247 | PyObject *unicode, PyObject **exceptionObject, |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 248 | Py_ssize_t startpos, Py_ssize_t endpos, Py_ssize_t *newpos); |
| 249 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 250 | static void |
| 251 | raise_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 252 | const char *encoding, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 253 | PyObject *unicode, |
| 254 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 255 | const char *reason); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 256 | |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 257 | /* Same for linebreaks */ |
| 258 | static unsigned char ascii_linebreak[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 259 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 260 | /* 0x000A, * LINE FEED */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 261 | /* 0x000B, * LINE TABULATION */ |
| 262 | /* 0x000C, * FORM FEED */ |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 263 | /* 0x000D, * CARRIAGE RETURN */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 264 | 0, 0, 1, 1, 1, 1, 0, 0, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 265 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 266 | /* 0x001C, * FILE SEPARATOR */ |
| 267 | /* 0x001D, * GROUP SEPARATOR */ |
| 268 | /* 0x001E, * RECORD SEPARATOR */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 269 | 0, 0, 0, 0, 1, 1, 1, 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, |
| 273 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 274 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 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, |
| 282 | 0, 0, 0, 0, 0, 0, 0, 0 |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 283 | }; |
| 284 | |
Ezio Melotti | 48a2f8f | 2011-09-29 00:18:19 +0300 | [diff] [blame] | 285 | /* The max unicode value is always 0x10FFFF while using the PEP-393 API. |
| 286 | 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] | 287 | Py_UNICODE |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 288 | PyUnicode_GetMax(void) |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 289 | { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 290 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 291 | return 0x10FFFF; |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 292 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 293 | /* This is actually an illegal character, so it should |
| 294 | not be passed to unichr. */ |
| 295 | return 0xFFFF; |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 296 | #endif |
| 297 | } |
| 298 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 299 | #ifdef Py_DEBUG |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 300 | int |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 301 | _PyUnicode_CheckConsistency(PyObject *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); |
Victor Stinner | e30c0a1 | 2011-11-04 20:54:05 +0100 | [diff] [blame] | 327 | } |
| 328 | else { |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 329 | PyUnicodeObject *unicode = (PyUnicodeObject *)op; |
| 330 | |
| 331 | data = unicode->data.any; |
| 332 | if (kind == PyUnicode_WCHAR_KIND) { |
Victor Stinner | e30c0a1 | 2011-11-04 20:54:05 +0100 | [diff] [blame] | 333 | assert(ascii->length == 0); |
| 334 | assert(ascii->hash == -1); |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 335 | assert(ascii->state.compact == 0); |
| 336 | assert(ascii->state.ascii == 0); |
| 337 | assert(ascii->state.ready == 0); |
Victor Stinner | e30c0a1 | 2011-11-04 20:54:05 +0100 | [diff] [blame] | 338 | assert(ascii->state.interned == SSTATE_NOT_INTERNED); |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 339 | assert(ascii->wstr != NULL); |
| 340 | assert(data == NULL); |
| 341 | assert(compact->utf8 == NULL); |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 342 | } |
| 343 | else { |
| 344 | assert(kind == PyUnicode_1BYTE_KIND |
| 345 | || kind == PyUnicode_2BYTE_KIND |
| 346 | || kind == PyUnicode_4BYTE_KIND); |
| 347 | assert(ascii->state.compact == 0); |
| 348 | assert(ascii->state.ready == 1); |
| 349 | assert(data != NULL); |
| 350 | if (ascii->state.ascii) { |
| 351 | assert (compact->utf8 == data); |
| 352 | assert (compact->utf8_length == ascii->length); |
| 353 | } |
| 354 | else |
| 355 | assert (compact->utf8 != data); |
| 356 | } |
| 357 | } |
| 358 | if (kind != PyUnicode_WCHAR_KIND) { |
Victor Stinner | 7f11ad4 | 2011-10-04 00:00:20 +0200 | [diff] [blame] | 359 | if ( |
| 360 | #if SIZEOF_WCHAR_T == 2 |
| 361 | kind == PyUnicode_2BYTE_KIND |
| 362 | #else |
| 363 | kind == PyUnicode_4BYTE_KIND |
| 364 | #endif |
| 365 | ) |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 366 | { |
| 367 | assert(ascii->wstr == data); |
| 368 | assert(compact->wstr_length == ascii->length); |
| 369 | } else |
| 370 | assert(ascii->wstr != data); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 371 | } |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 372 | |
| 373 | if (compact->utf8 == NULL) |
| 374 | assert(compact->utf8_length == 0); |
| 375 | if (ascii->wstr == NULL) |
| 376 | assert(compact->wstr_length == 0); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 377 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 378 | /* check that the best kind is used */ |
| 379 | if (check_content && kind != PyUnicode_WCHAR_KIND) |
| 380 | { |
| 381 | Py_ssize_t i; |
| 382 | Py_UCS4 maxchar = 0; |
Victor Stinner | 718fbf0 | 2012-04-26 00:39:37 +0200 | [diff] [blame] | 383 | void *data; |
| 384 | Py_UCS4 ch; |
| 385 | |
| 386 | data = PyUnicode_DATA(ascii); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 387 | for (i=0; i < ascii->length; i++) |
| 388 | { |
Victor Stinner | 718fbf0 | 2012-04-26 00:39:37 +0200 | [diff] [blame] | 389 | ch = PyUnicode_READ(kind, data, i); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 390 | if (ch > maxchar) |
| 391 | maxchar = ch; |
| 392 | } |
| 393 | if (kind == PyUnicode_1BYTE_KIND) { |
Victor Stinner | 77faf69 | 2011-11-20 18:56:05 +0100 | [diff] [blame] | 394 | if (ascii->state.ascii == 0) { |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 395 | assert(maxchar >= 128); |
Victor Stinner | 77faf69 | 2011-11-20 18:56:05 +0100 | [diff] [blame] | 396 | assert(maxchar <= 255); |
| 397 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 398 | else |
| 399 | assert(maxchar < 128); |
| 400 | } |
Victor Stinner | 77faf69 | 2011-11-20 18:56:05 +0100 | [diff] [blame] | 401 | else if (kind == PyUnicode_2BYTE_KIND) { |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 402 | assert(maxchar >= 0x100); |
Victor Stinner | 77faf69 | 2011-11-20 18:56:05 +0100 | [diff] [blame] | 403 | assert(maxchar <= 0xFFFF); |
| 404 | } |
| 405 | else { |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 406 | assert(maxchar >= 0x10000); |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 407 | assert(maxchar <= MAX_UNICODE); |
Victor Stinner | 77faf69 | 2011-11-20 18:56:05 +0100 | [diff] [blame] | 408 | } |
Victor Stinner | 718fbf0 | 2012-04-26 00:39:37 +0200 | [diff] [blame] | 409 | assert(PyUnicode_READ(kind, data, ascii->length) == 0); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 410 | } |
Benjamin Peterson | ccc51c1 | 2011-10-03 19:34:12 -0400 | [diff] [blame] | 411 | return 1; |
| 412 | } |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 413 | #endif |
| 414 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 415 | static PyObject* |
| 416 | unicode_result_wchar(PyObject *unicode) |
| 417 | { |
| 418 | #ifndef Py_DEBUG |
| 419 | Py_ssize_t len; |
| 420 | |
| 421 | assert(Py_REFCNT(unicode) == 1); |
| 422 | |
| 423 | len = _PyUnicode_WSTR_LENGTH(unicode); |
| 424 | if (len == 0) { |
| 425 | Py_INCREF(unicode_empty); |
| 426 | Py_DECREF(unicode); |
| 427 | return unicode_empty; |
| 428 | } |
| 429 | |
| 430 | if (len == 1) { |
| 431 | wchar_t ch = _PyUnicode_WSTR(unicode)[0]; |
| 432 | if (ch < 256) { |
| 433 | PyObject *latin1_char = get_latin1_char((unsigned char)ch); |
| 434 | Py_DECREF(unicode); |
| 435 | return latin1_char; |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | if (_PyUnicode_Ready(unicode) < 0) { |
| 440 | Py_XDECREF(unicode); |
| 441 | return NULL; |
| 442 | } |
| 443 | #else |
| 444 | /* don't make the result ready in debug mode to ensure that the caller |
| 445 | makes the string ready before using it */ |
| 446 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
| 447 | #endif |
| 448 | return unicode; |
| 449 | } |
| 450 | |
| 451 | static PyObject* |
| 452 | unicode_result_ready(PyObject *unicode) |
| 453 | { |
| 454 | Py_ssize_t length; |
| 455 | |
| 456 | length = PyUnicode_GET_LENGTH(unicode); |
| 457 | if (length == 0) { |
| 458 | if (unicode != unicode_empty) { |
| 459 | Py_INCREF(unicode_empty); |
| 460 | Py_DECREF(unicode); |
| 461 | } |
| 462 | return unicode_empty; |
| 463 | } |
| 464 | |
| 465 | if (length == 1) { |
| 466 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, 0); |
| 467 | if (ch < 256) { |
| 468 | PyObject *latin1_char = unicode_latin1[ch]; |
| 469 | if (latin1_char != NULL) { |
| 470 | if (unicode != latin1_char) { |
| 471 | Py_INCREF(latin1_char); |
| 472 | Py_DECREF(unicode); |
| 473 | } |
| 474 | return latin1_char; |
| 475 | } |
| 476 | else { |
| 477 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
| 478 | Py_INCREF(unicode); |
| 479 | unicode_latin1[ch] = unicode; |
| 480 | return unicode; |
| 481 | } |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
| 486 | return unicode; |
| 487 | } |
| 488 | |
| 489 | static PyObject* |
| 490 | unicode_result(PyObject *unicode) |
| 491 | { |
| 492 | assert(_PyUnicode_CHECK(unicode)); |
| 493 | if (PyUnicode_IS_READY(unicode)) |
| 494 | return unicode_result_ready(unicode); |
| 495 | else |
| 496 | return unicode_result_wchar(unicode); |
| 497 | } |
| 498 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 499 | static PyObject* |
| 500 | unicode_result_unchanged(PyObject *unicode) |
| 501 | { |
| 502 | if (PyUnicode_CheckExact(unicode)) { |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 503 | if (PyUnicode_READY(unicode) == -1) |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 504 | return NULL; |
| 505 | Py_INCREF(unicode); |
| 506 | return unicode; |
| 507 | } |
| 508 | else |
| 509 | /* Subtype -- return genuine unicode string with the same value. */ |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 510 | return _PyUnicode_Copy(unicode); |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 511 | } |
| 512 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 513 | #ifdef HAVE_MBCS |
| 514 | static OSVERSIONINFOEX winver; |
| 515 | #endif |
| 516 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 517 | /* --- Bloom Filters ----------------------------------------------------- */ |
| 518 | |
| 519 | /* stuff to implement simple "bloom filters" for Unicode characters. |
| 520 | to keep things simple, we use a single bitmask, using the least 5 |
| 521 | bits from each unicode characters as the bit index. */ |
| 522 | |
| 523 | /* the linebreak mask is set up by Unicode_Init below */ |
| 524 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 525 | #if LONG_BIT >= 128 |
| 526 | #define BLOOM_WIDTH 128 |
| 527 | #elif LONG_BIT >= 64 |
| 528 | #define BLOOM_WIDTH 64 |
| 529 | #elif LONG_BIT >= 32 |
| 530 | #define BLOOM_WIDTH 32 |
| 531 | #else |
| 532 | #error "LONG_BIT is smaller than 32" |
| 533 | #endif |
| 534 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 535 | #define BLOOM_MASK unsigned long |
| 536 | |
| 537 | static BLOOM_MASK bloom_linebreak; |
| 538 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 539 | #define BLOOM_ADD(mask, ch) ((mask |= (1UL << ((ch) & (BLOOM_WIDTH - 1))))) |
| 540 | #define BLOOM(mask, ch) ((mask & (1UL << ((ch) & (BLOOM_WIDTH - 1))))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 541 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 542 | #define BLOOM_LINEBREAK(ch) \ |
| 543 | ((ch) < 128U ? ascii_linebreak[(ch)] : \ |
| 544 | (BLOOM(bloom_linebreak, (ch)) && Py_UNICODE_ISLINEBREAK(ch))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 545 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 546 | Py_LOCAL_INLINE(BLOOM_MASK) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 547 | make_bloom_mask(int kind, void* ptr, Py_ssize_t len) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 548 | { |
| 549 | /* calculate simple bloom-style bitmask for a given unicode string */ |
| 550 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 551 | BLOOM_MASK mask; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 552 | Py_ssize_t i; |
| 553 | |
| 554 | mask = 0; |
| 555 | for (i = 0; i < len; i++) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 556 | BLOOM_ADD(mask, PyUnicode_READ(kind, ptr, i)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 557 | |
| 558 | return mask; |
| 559 | } |
| 560 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 561 | #define BLOOM_MEMBER(mask, chr, str) \ |
| 562 | (BLOOM(mask, chr) \ |
| 563 | && (PyUnicode_FindChar(str, chr, 0, PyUnicode_GET_LENGTH(str), 1) >= 0)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 564 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 565 | /* Compilation of templated routines */ |
| 566 | |
| 567 | #include "stringlib/asciilib.h" |
| 568 | #include "stringlib/fastsearch.h" |
| 569 | #include "stringlib/partition.h" |
| 570 | #include "stringlib/split.h" |
| 571 | #include "stringlib/count.h" |
| 572 | #include "stringlib/find.h" |
| 573 | #include "stringlib/find_max_char.h" |
| 574 | #include "stringlib/localeutil.h" |
| 575 | #include "stringlib/undef.h" |
| 576 | |
| 577 | #include "stringlib/ucs1lib.h" |
| 578 | #include "stringlib/fastsearch.h" |
| 579 | #include "stringlib/partition.h" |
| 580 | #include "stringlib/split.h" |
| 581 | #include "stringlib/count.h" |
| 582 | #include "stringlib/find.h" |
| 583 | #include "stringlib/find_max_char.h" |
| 584 | #include "stringlib/localeutil.h" |
| 585 | #include "stringlib/undef.h" |
| 586 | |
| 587 | #include "stringlib/ucs2lib.h" |
| 588 | #include "stringlib/fastsearch.h" |
| 589 | #include "stringlib/partition.h" |
| 590 | #include "stringlib/split.h" |
| 591 | #include "stringlib/count.h" |
| 592 | #include "stringlib/find.h" |
| 593 | #include "stringlib/find_max_char.h" |
| 594 | #include "stringlib/localeutil.h" |
| 595 | #include "stringlib/undef.h" |
| 596 | |
| 597 | #include "stringlib/ucs4lib.h" |
| 598 | #include "stringlib/fastsearch.h" |
| 599 | #include "stringlib/partition.h" |
| 600 | #include "stringlib/split.h" |
| 601 | #include "stringlib/count.h" |
| 602 | #include "stringlib/find.h" |
| 603 | #include "stringlib/find_max_char.h" |
| 604 | #include "stringlib/localeutil.h" |
| 605 | #include "stringlib/undef.h" |
| 606 | |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 607 | #include "stringlib/unicodedefs.h" |
| 608 | #include "stringlib/fastsearch.h" |
| 609 | #include "stringlib/count.h" |
| 610 | #include "stringlib/find.h" |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 611 | #include "stringlib/undef.h" |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 612 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 613 | /* --- Unicode Object ----------------------------------------------------- */ |
| 614 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 615 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 616 | fixup(PyObject *self, Py_UCS4 (*fixfct)(PyObject *s)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 617 | |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 618 | Py_LOCAL_INLINE(Py_ssize_t) findchar(void *s, int kind, |
| 619 | Py_ssize_t size, Py_UCS4 ch, |
| 620 | int direction) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 621 | { |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 622 | int mode = (direction == 1) ? FAST_SEARCH : FAST_RSEARCH; |
| 623 | |
| 624 | switch (kind) { |
| 625 | case PyUnicode_1BYTE_KIND: |
| 626 | { |
| 627 | Py_UCS1 ch1 = (Py_UCS1) ch; |
| 628 | if (ch1 == ch) |
| 629 | return ucs1lib_fastsearch((Py_UCS1 *) s, size, &ch1, 1, 0, mode); |
| 630 | else |
| 631 | return -1; |
| 632 | } |
| 633 | case PyUnicode_2BYTE_KIND: |
| 634 | { |
| 635 | Py_UCS2 ch2 = (Py_UCS2) ch; |
| 636 | if (ch2 == ch) |
| 637 | return ucs2lib_fastsearch((Py_UCS2 *) s, size, &ch2, 1, 0, mode); |
| 638 | else |
| 639 | return -1; |
| 640 | } |
| 641 | case PyUnicode_4BYTE_KIND: |
| 642 | return ucs4lib_fastsearch((Py_UCS4 *) s, size, &ch, 1, 0, mode); |
| 643 | default: |
| 644 | assert(0); |
| 645 | return -1; |
Victor Stinner | 9e7a1bc | 2011-10-13 00:18:12 +0200 | [diff] [blame] | 646 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 647 | } |
| 648 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 649 | static PyObject* |
| 650 | resize_compact(PyObject *unicode, Py_ssize_t length) |
| 651 | { |
| 652 | Py_ssize_t char_size; |
| 653 | Py_ssize_t struct_size; |
| 654 | Py_ssize_t new_size; |
| 655 | int share_wstr; |
Victor Stinner | 84def37 | 2011-12-11 20:04:56 +0100 | [diff] [blame] | 656 | PyObject *new_unicode; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 657 | assert(PyUnicode_IS_READY(unicode)); |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 658 | assert(PyUnicode_IS_COMPACT(unicode)); |
| 659 | |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 660 | char_size = PyUnicode_KIND(unicode); |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 661 | if (PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 662 | struct_size = sizeof(PyASCIIObject); |
| 663 | else |
| 664 | struct_size = sizeof(PyCompactUnicodeObject); |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 665 | share_wstr = _PyUnicode_SHARE_WSTR(unicode); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 666 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 667 | if (length > ((PY_SSIZE_T_MAX - struct_size) / char_size - 1)) { |
| 668 | PyErr_NoMemory(); |
| 669 | return NULL; |
| 670 | } |
| 671 | new_size = (struct_size + (length + 1) * char_size); |
| 672 | |
Victor Stinner | 84def37 | 2011-12-11 20:04:56 +0100 | [diff] [blame] | 673 | _Py_DEC_REFTOTAL; |
| 674 | _Py_ForgetReference(unicode); |
| 675 | |
| 676 | new_unicode = (PyObject *)PyObject_REALLOC((char *)unicode, new_size); |
| 677 | if (new_unicode == NULL) { |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 678 | _Py_NewReference(unicode); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 679 | PyErr_NoMemory(); |
| 680 | return NULL; |
| 681 | } |
Victor Stinner | 84def37 | 2011-12-11 20:04:56 +0100 | [diff] [blame] | 682 | unicode = new_unicode; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 683 | _Py_NewReference(unicode); |
Victor Stinner | 84def37 | 2011-12-11 20:04:56 +0100 | [diff] [blame] | 684 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 685 | _PyUnicode_LENGTH(unicode) = length; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 686 | if (share_wstr) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 687 | _PyUnicode_WSTR(unicode) = PyUnicode_DATA(unicode); |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 688 | if (!PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 689 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 690 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 691 | PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode), |
| 692 | length, 0); |
| 693 | return unicode; |
| 694 | } |
| 695 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 696 | static int |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 697 | resize_inplace(PyObject *unicode, Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 698 | { |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 699 | wchar_t *wstr; |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 700 | Py_ssize_t new_size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 701 | assert(!PyUnicode_IS_COMPACT(unicode)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 702 | assert(Py_REFCNT(unicode) == 1); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 703 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 704 | if (PyUnicode_IS_READY(unicode)) { |
| 705 | Py_ssize_t char_size; |
Victor Stinner | 1c8d0c7 | 2011-10-03 12:11:00 +0200 | [diff] [blame] | 706 | int share_wstr, share_utf8; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 707 | void *data; |
| 708 | |
| 709 | data = _PyUnicode_DATA_ANY(unicode); |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 710 | char_size = PyUnicode_KIND(unicode); |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 711 | share_wstr = _PyUnicode_SHARE_WSTR(unicode); |
| 712 | share_utf8 = _PyUnicode_SHARE_UTF8(unicode); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 713 | |
| 714 | if (length > (PY_SSIZE_T_MAX / char_size - 1)) { |
| 715 | PyErr_NoMemory(); |
| 716 | return -1; |
| 717 | } |
| 718 | new_size = (length + 1) * char_size; |
| 719 | |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 720 | if (!share_utf8 && _PyUnicode_HAS_UTF8_MEMORY(unicode)) |
| 721 | { |
| 722 | PyObject_DEL(_PyUnicode_UTF8(unicode)); |
| 723 | _PyUnicode_UTF8(unicode) = NULL; |
| 724 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
| 725 | } |
| 726 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 727 | data = (PyObject *)PyObject_REALLOC(data, new_size); |
| 728 | if (data == NULL) { |
| 729 | PyErr_NoMemory(); |
| 730 | return -1; |
| 731 | } |
| 732 | _PyUnicode_DATA_ANY(unicode) = data; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 733 | if (share_wstr) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 734 | _PyUnicode_WSTR(unicode) = data; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 735 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 736 | } |
| 737 | if (share_utf8) { |
Victor Stinner | 1c8d0c7 | 2011-10-03 12:11:00 +0200 | [diff] [blame] | 738 | _PyUnicode_UTF8(unicode) = data; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 739 | _PyUnicode_UTF8_LENGTH(unicode) = length; |
| 740 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 741 | _PyUnicode_LENGTH(unicode) = length; |
| 742 | PyUnicode_WRITE(PyUnicode_KIND(unicode), data, length, 0); |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 743 | if (share_wstr || _PyUnicode_WSTR(unicode) == NULL) { |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 744 | assert(_PyUnicode_CheckConsistency(unicode, 0)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 745 | return 0; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 746 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 747 | } |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 748 | assert(_PyUnicode_WSTR(unicode) != NULL); |
| 749 | |
| 750 | /* check for integer overflow */ |
| 751 | if (length > PY_SSIZE_T_MAX / sizeof(wchar_t) - 1) { |
| 752 | PyErr_NoMemory(); |
| 753 | return -1; |
| 754 | } |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 755 | new_size = sizeof(wchar_t) * (length + 1); |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 756 | wstr = _PyUnicode_WSTR(unicode); |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 757 | wstr = PyObject_REALLOC(wstr, new_size); |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 758 | if (!wstr) { |
| 759 | PyErr_NoMemory(); |
| 760 | return -1; |
| 761 | } |
| 762 | _PyUnicode_WSTR(unicode) = wstr; |
| 763 | _PyUnicode_WSTR(unicode)[length] = 0; |
| 764 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 765 | assert(_PyUnicode_CheckConsistency(unicode, 0)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 766 | return 0; |
| 767 | } |
| 768 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 769 | static PyObject* |
| 770 | resize_copy(PyObject *unicode, Py_ssize_t length) |
| 771 | { |
| 772 | Py_ssize_t copy_length; |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 773 | if (_PyUnicode_KIND(unicode) != PyUnicode_WCHAR_KIND) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 774 | PyObject *copy; |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 775 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 776 | if (PyUnicode_READY(unicode) == -1) |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 777 | return NULL; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 778 | |
| 779 | copy = PyUnicode_New(length, PyUnicode_MAX_CHAR_VALUE(unicode)); |
| 780 | if (copy == NULL) |
| 781 | return NULL; |
| 782 | |
| 783 | copy_length = Py_MIN(length, PyUnicode_GET_LENGTH(unicode)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 784 | copy_characters(copy, 0, unicode, 0, copy_length); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 785 | return copy; |
Victor Stinner | 8cfcbed | 2011-10-03 23:19:21 +0200 | [diff] [blame] | 786 | } |
| 787 | else { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 788 | PyObject *w; |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 789 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 790 | w = (PyObject*)_PyUnicode_New(length); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 791 | if (w == NULL) |
| 792 | return NULL; |
| 793 | copy_length = _PyUnicode_WSTR_LENGTH(unicode); |
| 794 | copy_length = Py_MIN(copy_length, length); |
| 795 | Py_UNICODE_COPY(_PyUnicode_WSTR(w), _PyUnicode_WSTR(unicode), |
| 796 | copy_length); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 797 | return w; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 798 | } |
| 799 | } |
| 800 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 801 | /* 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] | 802 | Ux0000 terminated; some code (e.g. new_identifier) |
| 803 | relies on that. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 804 | |
| 805 | XXX This allocator could further be enhanced by assuring that the |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 806 | free list never reduces its size below 1. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 807 | |
| 808 | */ |
| 809 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 810 | #ifdef Py_DEBUG |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 811 | static int unicode_old_new_calls = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 812 | #endif |
| 813 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 814 | static PyUnicodeObject * |
| 815 | _PyUnicode_New(Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 816 | { |
| 817 | register PyUnicodeObject *unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 818 | size_t new_size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 819 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 820 | /* Optimization for empty strings */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 821 | if (length == 0 && unicode_empty != NULL) { |
| 822 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 823 | return (PyUnicodeObject*)unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 824 | } |
| 825 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 826 | /* Ensure we won't overflow the size. */ |
| 827 | if (length > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
| 828 | return (PyUnicodeObject *)PyErr_NoMemory(); |
| 829 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 830 | if (length < 0) { |
| 831 | PyErr_SetString(PyExc_SystemError, |
| 832 | "Negative size passed to _PyUnicode_New"); |
| 833 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 834 | } |
| 835 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 836 | #ifdef Py_DEBUG |
| 837 | ++unicode_old_new_calls; |
| 838 | #endif |
| 839 | |
| 840 | unicode = PyObject_New(PyUnicodeObject, &PyUnicode_Type); |
| 841 | if (unicode == NULL) |
| 842 | return NULL; |
| 843 | new_size = sizeof(Py_UNICODE) * ((size_t)length + 1); |
| 844 | _PyUnicode_WSTR(unicode) = (Py_UNICODE*) PyObject_MALLOC(new_size); |
| 845 | if (!_PyUnicode_WSTR(unicode)) { |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 846 | Py_DECREF(unicode); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 847 | PyErr_NoMemory(); |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 848 | return NULL; |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 849 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 850 | |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 851 | /* Initialize the first element to guard against cases where |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 852 | * the caller fails before initializing str -- unicode_resize() |
| 853 | * reads str[0], and the Keep-Alive optimization can keep memory |
| 854 | * allocated for str alive across a call to unicode_dealloc(unicode). |
| 855 | * We don't want unicode_resize to read uninitialized memory in |
| 856 | * that case. |
| 857 | */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 858 | _PyUnicode_WSTR(unicode)[0] = 0; |
| 859 | _PyUnicode_WSTR(unicode)[length] = 0; |
| 860 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 861 | _PyUnicode_HASH(unicode) = -1; |
| 862 | _PyUnicode_STATE(unicode).interned = 0; |
| 863 | _PyUnicode_STATE(unicode).kind = 0; |
| 864 | _PyUnicode_STATE(unicode).compact = 0; |
| 865 | _PyUnicode_STATE(unicode).ready = 0; |
| 866 | _PyUnicode_STATE(unicode).ascii = 0; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 867 | _PyUnicode_DATA_ANY(unicode) = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 868 | _PyUnicode_LENGTH(unicode) = 0; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 869 | _PyUnicode_UTF8(unicode) = NULL; |
| 870 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 871 | assert(_PyUnicode_CheckConsistency((PyObject *)unicode, 0)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 872 | return unicode; |
| 873 | } |
| 874 | |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 875 | static const char* |
| 876 | unicode_kind_name(PyObject *unicode) |
| 877 | { |
Victor Stinner | 42dfd71 | 2011-10-03 14:41:45 +0200 | [diff] [blame] | 878 | /* don't check consistency: unicode_kind_name() is called from |
| 879 | _PyUnicode_Dump() */ |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 880 | if (!PyUnicode_IS_COMPACT(unicode)) |
| 881 | { |
| 882 | if (!PyUnicode_IS_READY(unicode)) |
| 883 | return "wstr"; |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 884 | switch (PyUnicode_KIND(unicode)) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 885 | { |
| 886 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 887 | if (PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 888 | return "legacy ascii"; |
| 889 | else |
| 890 | return "legacy latin1"; |
| 891 | case PyUnicode_2BYTE_KIND: |
| 892 | return "legacy UCS2"; |
| 893 | case PyUnicode_4BYTE_KIND: |
| 894 | return "legacy UCS4"; |
| 895 | default: |
| 896 | return "<legacy invalid kind>"; |
| 897 | } |
| 898 | } |
| 899 | assert(PyUnicode_IS_READY(unicode)); |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 900 | switch (PyUnicode_KIND(unicode)) { |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 901 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 902 | if (PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 903 | return "ascii"; |
| 904 | else |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 905 | return "latin1"; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 906 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 907 | return "UCS2"; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 908 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 909 | return "UCS4"; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 910 | default: |
| 911 | return "<invalid compact kind>"; |
| 912 | } |
| 913 | } |
| 914 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 915 | #ifdef Py_DEBUG |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 916 | static int unicode_new_new_calls = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 917 | |
| 918 | /* Functions wrapping macros for use in debugger */ |
| 919 | char *_PyUnicode_utf8(void *unicode){ |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 920 | return PyUnicode_UTF8(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 921 | } |
| 922 | |
| 923 | void *_PyUnicode_compact_data(void *unicode) { |
| 924 | return _PyUnicode_COMPACT_DATA(unicode); |
| 925 | } |
| 926 | void *_PyUnicode_data(void *unicode){ |
| 927 | printf("obj %p\n", unicode); |
| 928 | printf("compact %d\n", PyUnicode_IS_COMPACT(unicode)); |
| 929 | printf("compact ascii %d\n", PyUnicode_IS_COMPACT_ASCII(unicode)); |
| 930 | printf("ascii op %p\n", ((void*)((PyASCIIObject*)(unicode) + 1))); |
| 931 | printf("compact op %p\n", ((void*)((PyCompactUnicodeObject*)(unicode) + 1))); |
| 932 | printf("compact data %p\n", _PyUnicode_COMPACT_DATA(unicode)); |
| 933 | return PyUnicode_DATA(unicode); |
| 934 | } |
Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 935 | |
| 936 | void |
| 937 | _PyUnicode_Dump(PyObject *op) |
| 938 | { |
| 939 | PyASCIIObject *ascii = (PyASCIIObject *)op; |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 940 | PyCompactUnicodeObject *compact = (PyCompactUnicodeObject *)op; |
| 941 | PyUnicodeObject *unicode = (PyUnicodeObject *)op; |
| 942 | void *data; |
Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 943 | |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 944 | if (ascii->state.compact) |
Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 945 | { |
| 946 | if (ascii->state.ascii) |
| 947 | data = (ascii + 1); |
| 948 | else |
| 949 | data = (compact + 1); |
| 950 | } |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 951 | else |
| 952 | data = unicode->data.any; |
Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 953 | printf("%s: len=%zu, ",unicode_kind_name(op), ascii->length); |
| 954 | |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 955 | if (ascii->wstr == data) |
| 956 | printf("shared "); |
| 957 | printf("wstr=%p", ascii->wstr); |
Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 958 | |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 959 | if (!(ascii->state.ascii == 1 && ascii->state.compact == 1)) { |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 960 | printf(" (%zu), ", compact->wstr_length); |
| 961 | if (!ascii->state.compact && compact->utf8 == unicode->data.any) |
| 962 | printf("shared "); |
| 963 | printf("utf8=%p (%zu)", compact->utf8, compact->utf8_length); |
Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 964 | } |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 965 | printf(", data=%p\n", data); |
Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 966 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 967 | #endif |
| 968 | |
| 969 | PyObject * |
| 970 | PyUnicode_New(Py_ssize_t size, Py_UCS4 maxchar) |
| 971 | { |
| 972 | PyObject *obj; |
| 973 | PyCompactUnicodeObject *unicode; |
| 974 | void *data; |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 975 | enum PyUnicode_Kind kind; |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 976 | int is_sharing, is_ascii; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 977 | Py_ssize_t char_size; |
| 978 | Py_ssize_t struct_size; |
| 979 | |
| 980 | /* Optimization for empty strings */ |
| 981 | if (size == 0 && unicode_empty != NULL) { |
| 982 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 983 | return unicode_empty; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 984 | } |
| 985 | |
| 986 | #ifdef Py_DEBUG |
| 987 | ++unicode_new_new_calls; |
| 988 | #endif |
| 989 | |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 990 | is_ascii = 0; |
| 991 | is_sharing = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 992 | struct_size = sizeof(PyCompactUnicodeObject); |
| 993 | if (maxchar < 128) { |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 994 | kind = PyUnicode_1BYTE_KIND; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 995 | char_size = 1; |
| 996 | is_ascii = 1; |
| 997 | struct_size = sizeof(PyASCIIObject); |
| 998 | } |
| 999 | else if (maxchar < 256) { |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1000 | kind = PyUnicode_1BYTE_KIND; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1001 | char_size = 1; |
| 1002 | } |
| 1003 | else if (maxchar < 65536) { |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1004 | kind = PyUnicode_2BYTE_KIND; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1005 | char_size = 2; |
| 1006 | if (sizeof(wchar_t) == 2) |
| 1007 | is_sharing = 1; |
| 1008 | } |
| 1009 | else { |
Victor Stinner | c9590ad | 2012-03-04 01:34:37 +0100 | [diff] [blame] | 1010 | if (maxchar > MAX_UNICODE) { |
| 1011 | PyErr_SetString(PyExc_SystemError, |
| 1012 | "invalid maximum character passed to PyUnicode_New"); |
| 1013 | return NULL; |
| 1014 | } |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1015 | kind = PyUnicode_4BYTE_KIND; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1016 | char_size = 4; |
| 1017 | if (sizeof(wchar_t) == 4) |
| 1018 | is_sharing = 1; |
| 1019 | } |
| 1020 | |
| 1021 | /* Ensure we won't overflow the size. */ |
| 1022 | if (size < 0) { |
| 1023 | PyErr_SetString(PyExc_SystemError, |
| 1024 | "Negative size passed to PyUnicode_New"); |
| 1025 | return NULL; |
| 1026 | } |
| 1027 | if (size > ((PY_SSIZE_T_MAX - struct_size) / char_size - 1)) |
| 1028 | return PyErr_NoMemory(); |
| 1029 | |
| 1030 | /* Duplicated allocation code from _PyObject_New() instead of a call to |
| 1031 | * PyObject_New() so we are able to allocate space for the object and |
| 1032 | * it's data buffer. |
| 1033 | */ |
| 1034 | obj = (PyObject *) PyObject_MALLOC(struct_size + (size + 1) * char_size); |
| 1035 | if (obj == NULL) |
| 1036 | return PyErr_NoMemory(); |
| 1037 | obj = PyObject_INIT(obj, &PyUnicode_Type); |
| 1038 | if (obj == NULL) |
| 1039 | return NULL; |
| 1040 | |
| 1041 | unicode = (PyCompactUnicodeObject *)obj; |
| 1042 | if (is_ascii) |
| 1043 | data = ((PyASCIIObject*)obj) + 1; |
| 1044 | else |
| 1045 | data = unicode + 1; |
| 1046 | _PyUnicode_LENGTH(unicode) = size; |
| 1047 | _PyUnicode_HASH(unicode) = -1; |
| 1048 | _PyUnicode_STATE(unicode).interned = 0; |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1049 | _PyUnicode_STATE(unicode).kind = kind; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1050 | _PyUnicode_STATE(unicode).compact = 1; |
| 1051 | _PyUnicode_STATE(unicode).ready = 1; |
| 1052 | _PyUnicode_STATE(unicode).ascii = is_ascii; |
| 1053 | if (is_ascii) { |
| 1054 | ((char*)data)[size] = 0; |
| 1055 | _PyUnicode_WSTR(unicode) = NULL; |
| 1056 | } |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1057 | else if (kind == PyUnicode_1BYTE_KIND) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1058 | ((char*)data)[size] = 0; |
| 1059 | _PyUnicode_WSTR(unicode) = NULL; |
| 1060 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1061 | unicode->utf8 = NULL; |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 1062 | unicode->utf8_length = 0; |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1063 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1064 | else { |
| 1065 | unicode->utf8 = NULL; |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 1066 | unicode->utf8_length = 0; |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1067 | if (kind == PyUnicode_2BYTE_KIND) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1068 | ((Py_UCS2*)data)[size] = 0; |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1069 | else /* kind == PyUnicode_4BYTE_KIND */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1070 | ((Py_UCS4*)data)[size] = 0; |
| 1071 | if (is_sharing) { |
| 1072 | _PyUnicode_WSTR_LENGTH(unicode) = size; |
| 1073 | _PyUnicode_WSTR(unicode) = (wchar_t *)data; |
| 1074 | } |
| 1075 | else { |
| 1076 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1077 | _PyUnicode_WSTR(unicode) = NULL; |
| 1078 | } |
| 1079 | } |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1080 | #ifdef Py_DEBUG |
| 1081 | /* Fill the data with invalid characters to detect bugs earlier. |
| 1082 | _PyUnicode_CheckConsistency(str, 1) detects invalid characters, |
| 1083 | at least for ASCII and UCS-4 strings. U+00FF is invalid in ASCII |
| 1084 | and U+FFFFFFFF is an invalid character in Unicode 6.0. */ |
| 1085 | memset(data, 0xff, size * kind); |
| 1086 | #endif |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 1087 | assert(_PyUnicode_CheckConsistency((PyObject*)unicode, 0)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1088 | return obj; |
| 1089 | } |
| 1090 | |
| 1091 | #if SIZEOF_WCHAR_T == 2 |
| 1092 | /* Helper function to convert a 16-bits wchar_t representation to UCS4, this |
| 1093 | will decode surrogate pairs, the other conversions are implemented as macros |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 1094 | for efficiency. |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1095 | |
| 1096 | This function assumes that unicode can hold one more code point than wstr |
| 1097 | characters for a terminating null character. */ |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1098 | static void |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1099 | unicode_convert_wchar_to_ucs4(const wchar_t *begin, const wchar_t *end, |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1100 | PyObject *unicode) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1101 | { |
| 1102 | const wchar_t *iter; |
| 1103 | Py_UCS4 *ucs4_out; |
| 1104 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1105 | assert(unicode != NULL); |
| 1106 | assert(_PyUnicode_CHECK(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1107 | assert(_PyUnicode_KIND(unicode) == PyUnicode_4BYTE_KIND); |
| 1108 | ucs4_out = PyUnicode_4BYTE_DATA(unicode); |
| 1109 | |
| 1110 | for (iter = begin; iter < end; ) { |
| 1111 | assert(ucs4_out < (PyUnicode_4BYTE_DATA(unicode) + |
| 1112 | _PyUnicode_GET_LENGTH(unicode))); |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 1113 | if (Py_UNICODE_IS_HIGH_SURROGATE(iter[0]) |
| 1114 | && (iter+1) < end |
| 1115 | && Py_UNICODE_IS_LOW_SURROGATE(iter[1])) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1116 | { |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 1117 | *ucs4_out++ = Py_UNICODE_JOIN_SURROGATES(iter[0], iter[1]); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1118 | iter += 2; |
| 1119 | } |
| 1120 | else { |
| 1121 | *ucs4_out++ = *iter; |
| 1122 | iter++; |
| 1123 | } |
| 1124 | } |
| 1125 | assert(ucs4_out == (PyUnicode_4BYTE_DATA(unicode) + |
| 1126 | _PyUnicode_GET_LENGTH(unicode))); |
| 1127 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1128 | } |
| 1129 | #endif |
| 1130 | |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1131 | static int |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1132 | unicode_check_modifiable(PyObject *unicode) |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1133 | { |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1134 | if (!unicode_modifiable(unicode)) { |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 1135 | PyErr_SetString(PyExc_SystemError, |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1136 | "Cannot modify a string currently used"); |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1137 | return -1; |
| 1138 | } |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1139 | return 0; |
| 1140 | } |
| 1141 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1142 | static int |
| 1143 | _copy_characters(PyObject *to, Py_ssize_t to_start, |
| 1144 | PyObject *from, Py_ssize_t from_start, |
| 1145 | Py_ssize_t how_many, int check_maxchar) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1146 | { |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1147 | unsigned int from_kind, to_kind; |
| 1148 | void *from_data, *to_data; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1149 | int fast; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1150 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1151 | assert(PyUnicode_Check(from)); |
| 1152 | assert(PyUnicode_Check(to)); |
| 1153 | assert(PyUnicode_IS_READY(from)); |
| 1154 | assert(PyUnicode_IS_READY(to)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1155 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1156 | assert(PyUnicode_GET_LENGTH(from) >= how_many); |
| 1157 | assert(to_start + how_many <= PyUnicode_GET_LENGTH(to)); |
| 1158 | assert(0 <= how_many); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1159 | |
Victor Stinner | f5ca1a2 | 2011-09-28 23:54:59 +0200 | [diff] [blame] | 1160 | if (how_many == 0) |
| 1161 | return 0; |
| 1162 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1163 | from_kind = PyUnicode_KIND(from); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1164 | from_data = PyUnicode_DATA(from); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1165 | to_kind = PyUnicode_KIND(to); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1166 | to_data = PyUnicode_DATA(to); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1167 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1168 | #ifdef Py_DEBUG |
| 1169 | if (!check_maxchar |
| 1170 | && (from_kind > to_kind |
| 1171 | || (!PyUnicode_IS_ASCII(from) && PyUnicode_IS_ASCII(to)))) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 1172 | { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1173 | const Py_UCS4 to_maxchar = PyUnicode_MAX_CHAR_VALUE(to); |
| 1174 | Py_UCS4 ch; |
| 1175 | Py_ssize_t i; |
| 1176 | for (i=0; i < how_many; i++) { |
| 1177 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
| 1178 | assert(ch <= to_maxchar); |
| 1179 | } |
| 1180 | } |
| 1181 | #endif |
| 1182 | fast = (from_kind == to_kind); |
| 1183 | if (check_maxchar |
| 1184 | && (!PyUnicode_IS_ASCII(from) && PyUnicode_IS_ASCII(to))) |
| 1185 | { |
| 1186 | /* deny latin1 => ascii */ |
| 1187 | fast = 0; |
| 1188 | } |
| 1189 | |
| 1190 | if (fast) { |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 1191 | Py_MEMCPY((char*)to_data + to_kind * to_start, |
| 1192 | (char*)from_data + from_kind * from_start, |
| 1193 | to_kind * how_many); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1194 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1195 | else if (from_kind == PyUnicode_1BYTE_KIND |
| 1196 | && to_kind == PyUnicode_2BYTE_KIND) |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1197 | { |
| 1198 | _PyUnicode_CONVERT_BYTES( |
| 1199 | Py_UCS1, Py_UCS2, |
| 1200 | PyUnicode_1BYTE_DATA(from) + from_start, |
| 1201 | PyUnicode_1BYTE_DATA(from) + from_start + how_many, |
| 1202 | PyUnicode_2BYTE_DATA(to) + to_start |
| 1203 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1204 | } |
Victor Stinner | 157f83f | 2011-09-28 21:41:31 +0200 | [diff] [blame] | 1205 | else if (from_kind == PyUnicode_1BYTE_KIND |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1206 | && to_kind == PyUnicode_4BYTE_KIND) |
| 1207 | { |
| 1208 | _PyUnicode_CONVERT_BYTES( |
| 1209 | Py_UCS1, Py_UCS4, |
| 1210 | PyUnicode_1BYTE_DATA(from) + from_start, |
| 1211 | PyUnicode_1BYTE_DATA(from) + from_start + how_many, |
| 1212 | PyUnicode_4BYTE_DATA(to) + to_start |
| 1213 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1214 | } |
| 1215 | else if (from_kind == PyUnicode_2BYTE_KIND |
| 1216 | && to_kind == PyUnicode_4BYTE_KIND) |
| 1217 | { |
| 1218 | _PyUnicode_CONVERT_BYTES( |
| 1219 | Py_UCS2, Py_UCS4, |
| 1220 | PyUnicode_2BYTE_DATA(from) + from_start, |
| 1221 | PyUnicode_2BYTE_DATA(from) + from_start + how_many, |
| 1222 | PyUnicode_4BYTE_DATA(to) + to_start |
| 1223 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1224 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1225 | else { |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 1226 | /* check if max_char(from substring) <= max_char(to) */ |
| 1227 | if (from_kind > to_kind |
| 1228 | /* latin1 => ascii */ |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1229 | || (!PyUnicode_IS_ASCII(from) && PyUnicode_IS_ASCII(to))) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 1230 | { |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1231 | /* slow path to check for character overflow */ |
| 1232 | const Py_UCS4 to_maxchar = PyUnicode_MAX_CHAR_VALUE(to); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1233 | Py_UCS4 ch; |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1234 | Py_ssize_t i; |
| 1235 | |
Victor Stinner | 56c161a | 2011-10-06 02:47:11 +0200 | [diff] [blame] | 1236 | #ifdef Py_DEBUG |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1237 | for (i=0; i < how_many; i++) { |
| 1238 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
Victor Stinner | 56c161a | 2011-10-06 02:47:11 +0200 | [diff] [blame] | 1239 | assert(ch <= to_maxchar); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1240 | PyUnicode_WRITE(to_kind, to_data, to_start + i, ch); |
| 1241 | } |
Victor Stinner | 56c161a | 2011-10-06 02:47:11 +0200 | [diff] [blame] | 1242 | #else |
| 1243 | if (!check_maxchar) { |
| 1244 | for (i=0; i < how_many; i++) { |
| 1245 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
| 1246 | PyUnicode_WRITE(to_kind, to_data, to_start + i, ch); |
| 1247 | } |
| 1248 | } |
| 1249 | else { |
| 1250 | for (i=0; i < how_many; i++) { |
| 1251 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
| 1252 | if (ch > to_maxchar) |
| 1253 | return 1; |
| 1254 | PyUnicode_WRITE(to_kind, to_data, to_start + i, ch); |
| 1255 | } |
| 1256 | } |
| 1257 | #endif |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1258 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1259 | else { |
Victor Stinner | 56c161a | 2011-10-06 02:47:11 +0200 | [diff] [blame] | 1260 | assert(0 && "inconsistent state"); |
| 1261 | return 1; |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1262 | } |
| 1263 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1264 | return 0; |
| 1265 | } |
| 1266 | |
| 1267 | static void |
| 1268 | copy_characters(PyObject *to, Py_ssize_t to_start, |
| 1269 | PyObject *from, Py_ssize_t from_start, |
| 1270 | Py_ssize_t how_many) |
| 1271 | { |
| 1272 | (void)_copy_characters(to, to_start, from, from_start, how_many, 0); |
| 1273 | } |
| 1274 | |
| 1275 | Py_ssize_t |
| 1276 | PyUnicode_CopyCharacters(PyObject *to, Py_ssize_t to_start, |
| 1277 | PyObject *from, Py_ssize_t from_start, |
| 1278 | Py_ssize_t how_many) |
| 1279 | { |
| 1280 | int err; |
| 1281 | |
| 1282 | if (!PyUnicode_Check(from) || !PyUnicode_Check(to)) { |
| 1283 | PyErr_BadInternalCall(); |
| 1284 | return -1; |
| 1285 | } |
| 1286 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 1287 | if (PyUnicode_READY(from) == -1) |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1288 | return -1; |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 1289 | if (PyUnicode_READY(to) == -1) |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1290 | return -1; |
| 1291 | |
| 1292 | how_many = Py_MIN(PyUnicode_GET_LENGTH(from), how_many); |
| 1293 | if (to_start + how_many > PyUnicode_GET_LENGTH(to)) { |
| 1294 | PyErr_Format(PyExc_SystemError, |
| 1295 | "Cannot write %zi characters at %zi " |
| 1296 | "in a string of %zi characters", |
| 1297 | how_many, to_start, PyUnicode_GET_LENGTH(to)); |
| 1298 | return -1; |
| 1299 | } |
| 1300 | |
| 1301 | if (how_many == 0) |
| 1302 | return 0; |
| 1303 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1304 | if (unicode_check_modifiable(to)) |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1305 | return -1; |
| 1306 | |
| 1307 | err = _copy_characters(to, to_start, from, from_start, how_many, 1); |
| 1308 | if (err) { |
| 1309 | PyErr_Format(PyExc_SystemError, |
| 1310 | "Cannot copy %s characters " |
| 1311 | "into a string of %s characters", |
| 1312 | unicode_kind_name(from), |
| 1313 | unicode_kind_name(to)); |
| 1314 | return -1; |
| 1315 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1316 | return how_many; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1317 | } |
| 1318 | |
Victor Stinner | 1722216 | 2011-09-28 22:15:37 +0200 | [diff] [blame] | 1319 | /* Find the maximum code point and count the number of surrogate pairs so a |
| 1320 | correct string length can be computed before converting a string to UCS4. |
| 1321 | This function counts single surrogates as a character and not as a pair. |
| 1322 | |
| 1323 | Return 0 on success, or -1 on error. */ |
| 1324 | static int |
| 1325 | find_maxchar_surrogates(const wchar_t *begin, const wchar_t *end, |
| 1326 | Py_UCS4 *maxchar, Py_ssize_t *num_surrogates) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1327 | { |
| 1328 | const wchar_t *iter; |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1329 | Py_UCS4 ch; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1330 | |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1331 | assert(num_surrogates != NULL && maxchar != NULL); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1332 | *num_surrogates = 0; |
| 1333 | *maxchar = 0; |
| 1334 | |
| 1335 | for (iter = begin; iter < end; ) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1336 | #if SIZEOF_WCHAR_T == 2 |
Victor Stinner | ca4f207 | 2011-11-22 03:38:40 +0100 | [diff] [blame] | 1337 | if (Py_UNICODE_IS_HIGH_SURROGATE(iter[0]) |
| 1338 | && (iter+1) < end |
| 1339 | && Py_UNICODE_IS_LOW_SURROGATE(iter[1])) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1340 | { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1341 | ch = Py_UNICODE_JOIN_SURROGATES(iter[0], iter[1]); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1342 | ++(*num_surrogates); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1343 | iter += 2; |
| 1344 | } |
| 1345 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1346 | #endif |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1347 | { |
| 1348 | ch = *iter; |
| 1349 | iter++; |
| 1350 | } |
| 1351 | if (ch > *maxchar) { |
| 1352 | *maxchar = ch; |
| 1353 | if (*maxchar > MAX_UNICODE) { |
| 1354 | PyErr_Format(PyExc_ValueError, |
| 1355 | "character U+%x is not in range [U+0000; U+10ffff]", |
| 1356 | ch); |
| 1357 | return -1; |
| 1358 | } |
| 1359 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1360 | } |
| 1361 | return 0; |
| 1362 | } |
| 1363 | |
| 1364 | #ifdef Py_DEBUG |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 1365 | static int unicode_ready_calls = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1366 | #endif |
| 1367 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1368 | int |
| 1369 | _PyUnicode_Ready(PyObject *unicode) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1370 | { |
| 1371 | wchar_t *end; |
| 1372 | Py_UCS4 maxchar = 0; |
| 1373 | Py_ssize_t num_surrogates; |
| 1374 | #if SIZEOF_WCHAR_T == 2 |
| 1375 | Py_ssize_t length_wo_surrogates; |
| 1376 | #endif |
| 1377 | |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 1378 | /* _PyUnicode_Ready() is only intended for old-style API usage where |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1379 | strings were created using _PyObject_New() and where no canonical |
| 1380 | representation (the str field) has been set yet aka strings |
| 1381 | which are not yet ready. */ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1382 | assert(_PyUnicode_CHECK(unicode)); |
| 1383 | assert(_PyUnicode_KIND(unicode) == PyUnicode_WCHAR_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1384 | assert(_PyUnicode_WSTR(unicode) != NULL); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1385 | assert(_PyUnicode_DATA_ANY(unicode) == NULL); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1386 | assert(_PyUnicode_UTF8(unicode) == NULL); |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1387 | /* Actually, it should neither be interned nor be anything else: */ |
| 1388 | assert(_PyUnicode_STATE(unicode).interned == SSTATE_NOT_INTERNED); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1389 | |
| 1390 | #ifdef Py_DEBUG |
| 1391 | ++unicode_ready_calls; |
| 1392 | #endif |
| 1393 | |
| 1394 | end = _PyUnicode_WSTR(unicode) + _PyUnicode_WSTR_LENGTH(unicode); |
Victor Stinner | 1722216 | 2011-09-28 22:15:37 +0200 | [diff] [blame] | 1395 | if (find_maxchar_surrogates(_PyUnicode_WSTR(unicode), end, |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1396 | &maxchar, &num_surrogates) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1397 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1398 | |
| 1399 | if (maxchar < 256) { |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1400 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC(_PyUnicode_WSTR_LENGTH(unicode) + 1); |
| 1401 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1402 | PyErr_NoMemory(); |
| 1403 | return -1; |
| 1404 | } |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1405 | _PyUnicode_CONVERT_BYTES(wchar_t, unsigned char, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1406 | _PyUnicode_WSTR(unicode), end, |
| 1407 | PyUnicode_1BYTE_DATA(unicode)); |
| 1408 | PyUnicode_1BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1409 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1410 | _PyUnicode_STATE(unicode).kind = PyUnicode_1BYTE_KIND; |
| 1411 | if (maxchar < 128) { |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1412 | _PyUnicode_STATE(unicode).ascii = 1; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1413 | _PyUnicode_UTF8(unicode) = _PyUnicode_DATA_ANY(unicode); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1414 | _PyUnicode_UTF8_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1415 | } |
| 1416 | else { |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1417 | _PyUnicode_STATE(unicode).ascii = 0; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1418 | _PyUnicode_UTF8(unicode) = NULL; |
| 1419 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1420 | } |
| 1421 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1422 | _PyUnicode_WSTR(unicode) = NULL; |
| 1423 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1424 | } |
| 1425 | /* In this case we might have to convert down from 4-byte native |
| 1426 | wchar_t to 2-byte unicode. */ |
| 1427 | else if (maxchar < 65536) { |
| 1428 | assert(num_surrogates == 0 && |
| 1429 | "FindMaxCharAndNumSurrogatePairs() messed up"); |
| 1430 | |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1431 | #if SIZEOF_WCHAR_T == 2 |
| 1432 | /* We can share representations and are done. */ |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1433 | _PyUnicode_DATA_ANY(unicode) = _PyUnicode_WSTR(unicode); |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1434 | PyUnicode_2BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1435 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1436 | _PyUnicode_STATE(unicode).kind = PyUnicode_2BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1437 | _PyUnicode_UTF8(unicode) = NULL; |
| 1438 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1439 | #else |
| 1440 | /* sizeof(wchar_t) == 4 */ |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1441 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC( |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1442 | 2 * (_PyUnicode_WSTR_LENGTH(unicode) + 1)); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1443 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1444 | PyErr_NoMemory(); |
| 1445 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1446 | } |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1447 | _PyUnicode_CONVERT_BYTES(wchar_t, Py_UCS2, |
| 1448 | _PyUnicode_WSTR(unicode), end, |
| 1449 | PyUnicode_2BYTE_DATA(unicode)); |
| 1450 | PyUnicode_2BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1451 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1452 | _PyUnicode_STATE(unicode).kind = PyUnicode_2BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1453 | _PyUnicode_UTF8(unicode) = NULL; |
| 1454 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1455 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1456 | _PyUnicode_WSTR(unicode) = NULL; |
| 1457 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1458 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1459 | } |
| 1460 | /* maxchar exeeds 16 bit, wee need 4 bytes for unicode characters */ |
| 1461 | else { |
| 1462 | #if SIZEOF_WCHAR_T == 2 |
| 1463 | /* in case the native representation is 2-bytes, we need to allocate a |
| 1464 | new normalized 4-byte version. */ |
| 1465 | length_wo_surrogates = _PyUnicode_WSTR_LENGTH(unicode) - num_surrogates; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1466 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC(4 * (length_wo_surrogates + 1)); |
| 1467 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1468 | PyErr_NoMemory(); |
| 1469 | return -1; |
| 1470 | } |
| 1471 | _PyUnicode_LENGTH(unicode) = length_wo_surrogates; |
| 1472 | _PyUnicode_STATE(unicode).kind = PyUnicode_4BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1473 | _PyUnicode_UTF8(unicode) = NULL; |
| 1474 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 126c559 | 2011-10-03 04:17:10 +0200 | [diff] [blame] | 1475 | /* unicode_convert_wchar_to_ucs4() requires a ready string */ |
| 1476 | _PyUnicode_STATE(unicode).ready = 1; |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1477 | unicode_convert_wchar_to_ucs4(_PyUnicode_WSTR(unicode), end, unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1478 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1479 | _PyUnicode_WSTR(unicode) = NULL; |
| 1480 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1481 | #else |
| 1482 | assert(num_surrogates == 0); |
| 1483 | |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1484 | _PyUnicode_DATA_ANY(unicode) = _PyUnicode_WSTR(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1485 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1486 | _PyUnicode_UTF8(unicode) = NULL; |
| 1487 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1488 | _PyUnicode_STATE(unicode).kind = PyUnicode_4BYTE_KIND; |
| 1489 | #endif |
| 1490 | PyUnicode_4BYTE_DATA(unicode)[_PyUnicode_LENGTH(unicode)] = '\0'; |
| 1491 | } |
| 1492 | _PyUnicode_STATE(unicode).ready = 1; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1493 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1494 | return 0; |
| 1495 | } |
| 1496 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1497 | static void |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1498 | unicode_dealloc(register PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1499 | { |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1500 | switch (PyUnicode_CHECK_INTERNED(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1501 | case SSTATE_NOT_INTERNED: |
| 1502 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1503 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1504 | case SSTATE_INTERNED_MORTAL: |
| 1505 | /* revive dead object temporarily for DelItem */ |
| 1506 | Py_REFCNT(unicode) = 3; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 1507 | if (PyDict_DelItem(interned, unicode) != 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1508 | Py_FatalError( |
| 1509 | "deletion of interned string failed"); |
| 1510 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1511 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1512 | case SSTATE_INTERNED_IMMORTAL: |
| 1513 | Py_FatalError("Immortal interned string died."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1514 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1515 | default: |
| 1516 | Py_FatalError("Inconsistent interned string state."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1517 | } |
| 1518 | |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 1519 | if (_PyUnicode_HAS_WSTR_MEMORY(unicode)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1520 | PyObject_DEL(_PyUnicode_WSTR(unicode)); |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 1521 | if (_PyUnicode_HAS_UTF8_MEMORY(unicode)) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1522 | PyObject_DEL(_PyUnicode_UTF8(unicode)); |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1523 | if (!PyUnicode_IS_COMPACT(unicode) && _PyUnicode_DATA_ANY(unicode)) |
| 1524 | PyObject_DEL(_PyUnicode_DATA_ANY(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1525 | |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1526 | Py_TYPE(unicode)->tp_free(unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1527 | } |
| 1528 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1529 | #ifdef Py_DEBUG |
| 1530 | static int |
| 1531 | unicode_is_singleton(PyObject *unicode) |
| 1532 | { |
| 1533 | PyASCIIObject *ascii = (PyASCIIObject *)unicode; |
| 1534 | if (unicode == unicode_empty) |
| 1535 | return 1; |
| 1536 | if (ascii->state.kind != PyUnicode_WCHAR_KIND && ascii->length == 1) |
| 1537 | { |
| 1538 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, 0); |
| 1539 | if (ch < 256 && unicode_latin1[ch] == unicode) |
| 1540 | return 1; |
| 1541 | } |
| 1542 | return 0; |
| 1543 | } |
| 1544 | #endif |
| 1545 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1546 | static int |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1547 | unicode_modifiable(PyObject *unicode) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1548 | { |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1549 | assert(_PyUnicode_CHECK(unicode)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1550 | if (Py_REFCNT(unicode) != 1) |
| 1551 | return 0; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1552 | if (_PyUnicode_HASH(unicode) != -1) |
| 1553 | return 0; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1554 | if (PyUnicode_CHECK_INTERNED(unicode)) |
| 1555 | return 0; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1556 | if (!PyUnicode_CheckExact(unicode)) |
| 1557 | return 0; |
Victor Stinner | 77bb47b | 2011-10-03 20:06:05 +0200 | [diff] [blame] | 1558 | #ifdef Py_DEBUG |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1559 | /* singleton refcount is greater than 1 */ |
| 1560 | assert(!unicode_is_singleton(unicode)); |
Victor Stinner | 77bb47b | 2011-10-03 20:06:05 +0200 | [diff] [blame] | 1561 | #endif |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1562 | return 1; |
| 1563 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1564 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1565 | static int |
| 1566 | unicode_resize(PyObject **p_unicode, Py_ssize_t length) |
| 1567 | { |
| 1568 | PyObject *unicode; |
| 1569 | Py_ssize_t old_length; |
| 1570 | |
| 1571 | assert(p_unicode != NULL); |
| 1572 | unicode = *p_unicode; |
| 1573 | |
| 1574 | assert(unicode != NULL); |
| 1575 | assert(PyUnicode_Check(unicode)); |
| 1576 | assert(0 <= length); |
| 1577 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1578 | if (_PyUnicode_KIND(unicode) == PyUnicode_WCHAR_KIND) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1579 | old_length = PyUnicode_WSTR_LENGTH(unicode); |
| 1580 | else |
| 1581 | old_length = PyUnicode_GET_LENGTH(unicode); |
| 1582 | if (old_length == length) |
| 1583 | return 0; |
| 1584 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1585 | if (length == 0) { |
| 1586 | Py_DECREF(*p_unicode); |
| 1587 | *p_unicode = unicode_empty; |
| 1588 | Py_INCREF(*p_unicode); |
| 1589 | return 0; |
| 1590 | } |
| 1591 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1592 | if (!unicode_modifiable(unicode)) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1593 | PyObject *copy = resize_copy(unicode, length); |
| 1594 | if (copy == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1595 | return -1; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1596 | Py_DECREF(*p_unicode); |
| 1597 | *p_unicode = copy; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1598 | return 0; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1599 | } |
| 1600 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1601 | if (PyUnicode_IS_COMPACT(unicode)) { |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1602 | PyObject *new_unicode = resize_compact(unicode, length); |
| 1603 | if (new_unicode == NULL) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1604 | return -1; |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1605 | *p_unicode = new_unicode; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1606 | assert(_PyUnicode_CheckConsistency(*p_unicode, 0)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1607 | return 0; |
Benjamin Peterson | 4bfce8f | 2011-10-03 19:35:07 -0400 | [diff] [blame] | 1608 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1609 | return resize_inplace(unicode, length); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1610 | } |
| 1611 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1612 | int |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1613 | PyUnicode_Resize(PyObject **p_unicode, Py_ssize_t length) |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 1614 | { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1615 | PyObject *unicode; |
| 1616 | if (p_unicode == NULL) { |
| 1617 | PyErr_BadInternalCall(); |
| 1618 | return -1; |
| 1619 | } |
| 1620 | unicode = *p_unicode; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1621 | if (unicode == NULL || !PyUnicode_Check(unicode) || length < 0) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1622 | { |
| 1623 | PyErr_BadInternalCall(); |
| 1624 | return -1; |
| 1625 | } |
| 1626 | return unicode_resize(p_unicode, length); |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 1627 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1628 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1629 | static int |
Victor Stinner | 0a045ef | 2011-11-09 00:02:42 +0100 | [diff] [blame] | 1630 | unicode_widen(PyObject **p_unicode, unsigned int maxchar) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1631 | { |
| 1632 | PyObject *result; |
| 1633 | assert(PyUnicode_IS_READY(*p_unicode)); |
| 1634 | if (maxchar <= PyUnicode_MAX_CHAR_VALUE(*p_unicode)) |
| 1635 | return 0; |
| 1636 | result = PyUnicode_New(PyUnicode_GET_LENGTH(*p_unicode), |
| 1637 | maxchar); |
| 1638 | if (result == NULL) |
| 1639 | return -1; |
| 1640 | PyUnicode_CopyCharacters(result, 0, *p_unicode, 0, |
| 1641 | PyUnicode_GET_LENGTH(*p_unicode)); |
| 1642 | Py_DECREF(*p_unicode); |
| 1643 | *p_unicode = result; |
| 1644 | return 0; |
| 1645 | } |
| 1646 | |
| 1647 | static int |
| 1648 | unicode_putchar(PyObject **p_unicode, Py_ssize_t *pos, |
| 1649 | Py_UCS4 ch) |
| 1650 | { |
Victor Stinner | 15e9ed2 | 2012-02-22 13:36:20 +0100 | [diff] [blame] | 1651 | assert(ch <= MAX_UNICODE); |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1652 | if (unicode_widen(p_unicode, ch) < 0) |
| 1653 | return -1; |
| 1654 | PyUnicode_WRITE(PyUnicode_KIND(*p_unicode), |
| 1655 | PyUnicode_DATA(*p_unicode), |
| 1656 | (*pos)++, ch); |
| 1657 | return 0; |
| 1658 | } |
| 1659 | |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1660 | /* Copy a ASCII or latin1 char* string into a Python Unicode string. |
| 1661 | Return the length of the input string. |
| 1662 | |
Victor Stinner | b429d3b | 2012-02-22 21:22:20 +0100 | [diff] [blame] | 1663 | WARNING: The function doesn't copy the terminating null character and |
| 1664 | doesn't check the maximum character (may write a latin1 character in an |
| 1665 | ASCII string). */ |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1666 | static Py_ssize_t |
| 1667 | unicode_write_cstr(PyObject *unicode, Py_ssize_t index, const char *str) |
| 1668 | { |
| 1669 | enum PyUnicode_Kind kind = PyUnicode_KIND(unicode); |
| 1670 | void *data = PyUnicode_DATA(unicode); |
| 1671 | |
| 1672 | switch (kind) { |
| 1673 | case PyUnicode_1BYTE_KIND: { |
| 1674 | Py_ssize_t len = strlen(str); |
| 1675 | assert(index + len <= PyUnicode_GET_LENGTH(unicode)); |
Antoine Pitrou | ba6bafc | 2012-02-22 16:41:50 +0100 | [diff] [blame] | 1676 | memcpy((char *) data + index, str, len); |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1677 | return len; |
| 1678 | } |
| 1679 | case PyUnicode_2BYTE_KIND: { |
| 1680 | Py_UCS2 *start = (Py_UCS2 *)data + index; |
| 1681 | Py_UCS2 *ucs2 = start; |
| 1682 | assert(index <= PyUnicode_GET_LENGTH(unicode)); |
| 1683 | |
| 1684 | for (; *str; ++ucs2, ++str) |
| 1685 | *ucs2 = (Py_UCS2)*str; |
| 1686 | |
| 1687 | assert((ucs2 - start) <= PyUnicode_GET_LENGTH(unicode)); |
| 1688 | return ucs2 - start; |
| 1689 | } |
| 1690 | default: { |
| 1691 | Py_UCS4 *start = (Py_UCS4 *)data + index; |
| 1692 | Py_UCS4 *ucs4 = start; |
| 1693 | assert(kind == PyUnicode_4BYTE_KIND); |
| 1694 | assert(index <= PyUnicode_GET_LENGTH(unicode)); |
| 1695 | |
| 1696 | for (; *str; ++ucs4, ++str) |
| 1697 | *ucs4 = (Py_UCS4)*str; |
| 1698 | |
| 1699 | assert((ucs4 - start) <= PyUnicode_GET_LENGTH(unicode)); |
| 1700 | return ucs4 - start; |
| 1701 | } |
| 1702 | } |
| 1703 | } |
| 1704 | |
| 1705 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1706 | static PyObject* |
| 1707 | get_latin1_char(unsigned char ch) |
| 1708 | { |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1709 | PyObject *unicode = unicode_latin1[ch]; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1710 | if (!unicode) { |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1711 | unicode = PyUnicode_New(1, ch); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1712 | if (!unicode) |
| 1713 | return NULL; |
| 1714 | PyUnicode_1BYTE_DATA(unicode)[0] = ch; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1715 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1716 | unicode_latin1[ch] = unicode; |
| 1717 | } |
| 1718 | Py_INCREF(unicode); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1719 | return unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1720 | } |
| 1721 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1722 | PyObject * |
| 1723 | PyUnicode_FromUnicode(const Py_UNICODE *u, Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1724 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1725 | PyObject *unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1726 | Py_UCS4 maxchar = 0; |
| 1727 | Py_ssize_t num_surrogates; |
| 1728 | |
| 1729 | if (u == NULL) |
| 1730 | return (PyObject*)_PyUnicode_New(size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1731 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1732 | /* If the Unicode data is known at construction time, we can apply |
| 1733 | some optimizations which share commonly used objects. */ |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1734 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1735 | /* Optimization for empty strings */ |
| 1736 | if (size == 0 && unicode_empty != NULL) { |
| 1737 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1738 | return unicode_empty; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1739 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1740 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1741 | /* Single character Unicode objects in the Latin-1 range are |
| 1742 | shared when using this constructor */ |
| 1743 | if (size == 1 && *u < 256) |
| 1744 | return get_latin1_char((unsigned char)*u); |
| 1745 | |
| 1746 | /* If not empty and not single character, copy the Unicode data |
| 1747 | into the new object */ |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1748 | if (find_maxchar_surrogates(u, u + size, |
| 1749 | &maxchar, &num_surrogates) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1750 | return NULL; |
| 1751 | |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1752 | unicode = PyUnicode_New(size - num_surrogates, maxchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1753 | if (!unicode) |
| 1754 | return NULL; |
| 1755 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1756 | switch (PyUnicode_KIND(unicode)) { |
| 1757 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1758 | _PyUnicode_CONVERT_BYTES(Py_UNICODE, unsigned char, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1759 | u, u + size, PyUnicode_1BYTE_DATA(unicode)); |
| 1760 | break; |
| 1761 | case PyUnicode_2BYTE_KIND: |
| 1762 | #if Py_UNICODE_SIZE == 2 |
| 1763 | Py_MEMCPY(PyUnicode_2BYTE_DATA(unicode), u, size * 2); |
| 1764 | #else |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1765 | _PyUnicode_CONVERT_BYTES(Py_UNICODE, Py_UCS2, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1766 | u, u + size, PyUnicode_2BYTE_DATA(unicode)); |
| 1767 | #endif |
| 1768 | break; |
| 1769 | case PyUnicode_4BYTE_KIND: |
| 1770 | #if SIZEOF_WCHAR_T == 2 |
| 1771 | /* This is the only case which has to process surrogates, thus |
| 1772 | a simple copy loop is not enough and we need a function. */ |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1773 | unicode_convert_wchar_to_ucs4(u, u + size, unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1774 | #else |
| 1775 | assert(num_surrogates == 0); |
| 1776 | Py_MEMCPY(PyUnicode_4BYTE_DATA(unicode), u, size * 4); |
| 1777 | #endif |
| 1778 | break; |
| 1779 | default: |
| 1780 | assert(0 && "Impossible state"); |
| 1781 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1782 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1783 | return unicode_result(unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1784 | } |
| 1785 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1786 | PyObject * |
| 1787 | PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size) |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1788 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1789 | if (size < 0) { |
| 1790 | PyErr_SetString(PyExc_SystemError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1791 | "Negative size passed to PyUnicode_FromStringAndSize"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1792 | return NULL; |
| 1793 | } |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 1794 | if (u != NULL) |
| 1795 | return PyUnicode_DecodeUTF8Stateful(u, size, NULL, NULL); |
| 1796 | else |
| 1797 | return (PyObject *)_PyUnicode_New(size); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1798 | } |
| 1799 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1800 | PyObject * |
| 1801 | PyUnicode_FromString(const char *u) |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1802 | { |
| 1803 | size_t size = strlen(u); |
| 1804 | if (size > PY_SSIZE_T_MAX) { |
| 1805 | PyErr_SetString(PyExc_OverflowError, "input too long"); |
| 1806 | return NULL; |
| 1807 | } |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 1808 | return PyUnicode_DecodeUTF8Stateful(u, (Py_ssize_t)size, NULL, NULL); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1809 | } |
| 1810 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1811 | PyObject * |
| 1812 | _PyUnicode_FromId(_Py_Identifier *id) |
| 1813 | { |
| 1814 | if (!id->object) { |
Victor Stinner | d1cd99b | 2012-02-07 23:05:55 +0100 | [diff] [blame] | 1815 | id->object = PyUnicode_DecodeUTF8Stateful(id->string, |
| 1816 | strlen(id->string), |
| 1817 | NULL, NULL); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1818 | if (!id->object) |
| 1819 | return NULL; |
| 1820 | PyUnicode_InternInPlace(&id->object); |
| 1821 | assert(!id->next); |
| 1822 | id->next = static_strings; |
| 1823 | static_strings = id; |
| 1824 | } |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1825 | return id->object; |
| 1826 | } |
| 1827 | |
| 1828 | void |
| 1829 | _PyUnicode_ClearStaticStrings() |
| 1830 | { |
| 1831 | _Py_Identifier *i; |
| 1832 | for (i = static_strings; i; i = i->next) { |
| 1833 | Py_DECREF(i->object); |
| 1834 | i->object = NULL; |
| 1835 | i->next = NULL; |
| 1836 | } |
| 1837 | } |
| 1838 | |
Benjamin Peterson | 0df5429 | 2012-03-26 14:50:32 -0400 | [diff] [blame] | 1839 | /* Internal function, doesn't check maximum character */ |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1840 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1841 | static PyObject* |
Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 1842 | unicode_fromascii(const unsigned char* s, Py_ssize_t size) |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1843 | { |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 1844 | PyObject *unicode; |
Victor Stinner | e6b2d44 | 2011-12-11 21:54:30 +0100 | [diff] [blame] | 1845 | if (size == 1) { |
Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 1846 | #ifdef Py_DEBUG |
Victor Stinner | e6b2d44 | 2011-12-11 21:54:30 +0100 | [diff] [blame] | 1847 | assert(s[0] < 128); |
Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 1848 | #endif |
Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 1849 | return get_latin1_char(s[0]); |
Victor Stinner | e6b2d44 | 2011-12-11 21:54:30 +0100 | [diff] [blame] | 1850 | } |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 1851 | unicode = PyUnicode_New(size, 127); |
| 1852 | if (!unicode) |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1853 | return NULL; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 1854 | memcpy(PyUnicode_1BYTE_DATA(unicode), s, size); |
| 1855 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
| 1856 | return unicode; |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1857 | } |
| 1858 | |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 1859 | static Py_UCS4 |
| 1860 | kind_maxchar_limit(unsigned int kind) |
| 1861 | { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 1862 | switch (kind) { |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 1863 | case PyUnicode_1BYTE_KIND: |
| 1864 | return 0x80; |
| 1865 | case PyUnicode_2BYTE_KIND: |
| 1866 | return 0x100; |
| 1867 | case PyUnicode_4BYTE_KIND: |
| 1868 | return 0x10000; |
| 1869 | default: |
| 1870 | assert(0 && "invalid kind"); |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1871 | return MAX_UNICODE; |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 1872 | } |
| 1873 | } |
| 1874 | |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 1875 | Py_LOCAL_INLINE(Py_UCS4) |
| 1876 | align_maxchar(Py_UCS4 maxchar) |
| 1877 | { |
| 1878 | if (maxchar <= 127) |
| 1879 | return 127; |
| 1880 | else if (maxchar <= 255) |
| 1881 | return 255; |
| 1882 | else if (maxchar <= 65535) |
| 1883 | return 65535; |
| 1884 | else |
| 1885 | return MAX_UNICODE; |
| 1886 | } |
| 1887 | |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1888 | static PyObject* |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1889 | _PyUnicode_FromUCS1(const unsigned char* u, Py_ssize_t size) |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1890 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1891 | PyObject *res; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1892 | unsigned char max_char; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1893 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1894 | if (size == 0) { |
| 1895 | Py_INCREF(unicode_empty); |
| 1896 | return unicode_empty; |
| 1897 | } |
| 1898 | assert(size > 0); |
Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 1899 | if (size == 1) |
| 1900 | return get_latin1_char(u[0]); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1901 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1902 | max_char = ucs1lib_find_max_char(u, u + size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1903 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1904 | if (!res) |
| 1905 | return NULL; |
| 1906 | memcpy(PyUnicode_1BYTE_DATA(res), u, size); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1907 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1908 | return res; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1909 | } |
| 1910 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1911 | static PyObject* |
| 1912 | _PyUnicode_FromUCS2(const Py_UCS2 *u, Py_ssize_t size) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1913 | { |
| 1914 | PyObject *res; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1915 | Py_UCS2 max_char; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1916 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1917 | if (size == 0) { |
| 1918 | Py_INCREF(unicode_empty); |
| 1919 | return unicode_empty; |
| 1920 | } |
| 1921 | assert(size > 0); |
Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 1922 | if (size == 1) { |
| 1923 | Py_UCS4 ch = u[0]; |
| 1924 | if (ch < 256) |
| 1925 | return get_latin1_char((unsigned char)ch); |
| 1926 | |
| 1927 | res = PyUnicode_New(1, ch); |
| 1928 | if (res == NULL) |
| 1929 | return NULL; |
| 1930 | PyUnicode_WRITE(PyUnicode_KIND(res), PyUnicode_DATA(res), 0, ch); |
| 1931 | assert(_PyUnicode_CheckConsistency(res, 1)); |
| 1932 | return res; |
| 1933 | } |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1934 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1935 | max_char = ucs2lib_find_max_char(u, u + size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1936 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1937 | if (!res) |
| 1938 | return NULL; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1939 | if (max_char >= 256) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1940 | memcpy(PyUnicode_2BYTE_DATA(res), u, sizeof(Py_UCS2)*size); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1941 | else { |
| 1942 | _PyUnicode_CONVERT_BYTES( |
| 1943 | Py_UCS2, Py_UCS1, u, u + size, PyUnicode_1BYTE_DATA(res)); |
| 1944 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1945 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1946 | return res; |
| 1947 | } |
| 1948 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1949 | static PyObject* |
| 1950 | _PyUnicode_FromUCS4(const Py_UCS4 *u, Py_ssize_t size) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1951 | { |
| 1952 | PyObject *res; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1953 | Py_UCS4 max_char; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1954 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1955 | if (size == 0) { |
| 1956 | Py_INCREF(unicode_empty); |
| 1957 | return unicode_empty; |
| 1958 | } |
| 1959 | assert(size > 0); |
Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 1960 | if (size == 1) { |
| 1961 | Py_UCS4 ch = u[0]; |
| 1962 | if (ch < 256) |
| 1963 | return get_latin1_char((unsigned char)ch); |
| 1964 | |
| 1965 | res = PyUnicode_New(1, ch); |
| 1966 | if (res == NULL) |
| 1967 | return NULL; |
| 1968 | PyUnicode_WRITE(PyUnicode_KIND(res), PyUnicode_DATA(res), 0, ch); |
| 1969 | assert(_PyUnicode_CheckConsistency(res, 1)); |
| 1970 | return res; |
| 1971 | } |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1972 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1973 | max_char = ucs4lib_find_max_char(u, u + size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1974 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1975 | if (!res) |
| 1976 | return NULL; |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 1977 | if (max_char < 256) |
| 1978 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS1, u, u + size, |
| 1979 | PyUnicode_1BYTE_DATA(res)); |
| 1980 | else if (max_char < 0x10000) |
| 1981 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS2, u, u + size, |
| 1982 | PyUnicode_2BYTE_DATA(res)); |
| 1983 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1984 | memcpy(PyUnicode_4BYTE_DATA(res), u, sizeof(Py_UCS4)*size); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1985 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1986 | return res; |
| 1987 | } |
| 1988 | |
| 1989 | PyObject* |
| 1990 | PyUnicode_FromKindAndData(int kind, const void *buffer, Py_ssize_t size) |
| 1991 | { |
Victor Stinner | cfed46e | 2011-11-22 01:29:14 +0100 | [diff] [blame] | 1992 | if (size < 0) { |
| 1993 | PyErr_SetString(PyExc_ValueError, "size must be positive"); |
| 1994 | return NULL; |
| 1995 | } |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 1996 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1997 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1998 | return _PyUnicode_FromUCS1(buffer, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1999 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 2000 | return _PyUnicode_FromUCS2(buffer, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2001 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 2002 | return _PyUnicode_FromUCS4(buffer, size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 2003 | default: |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 2004 | PyErr_SetString(PyExc_SystemError, "invalid kind"); |
| 2005 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2006 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2007 | } |
| 2008 | |
Victor Stinner | ece58de | 2012-04-23 23:36:38 +0200 | [diff] [blame] | 2009 | Py_UCS4 |
| 2010 | _PyUnicode_FindMaxChar(PyObject *unicode, Py_ssize_t start, Py_ssize_t end) |
| 2011 | { |
| 2012 | enum PyUnicode_Kind kind; |
| 2013 | void *startptr, *endptr; |
| 2014 | |
| 2015 | assert(PyUnicode_IS_READY(unicode)); |
| 2016 | assert(0 <= start); |
| 2017 | assert(end <= PyUnicode_GET_LENGTH(unicode)); |
| 2018 | assert(start <= end); |
| 2019 | |
| 2020 | if (start == 0 && end == PyUnicode_GET_LENGTH(unicode)) |
| 2021 | return PyUnicode_MAX_CHAR_VALUE(unicode); |
| 2022 | |
| 2023 | if (start == end) |
| 2024 | return 127; |
| 2025 | |
Victor Stinner | 94d558b | 2012-04-27 22:26:58 +0200 | [diff] [blame] | 2026 | if (PyUnicode_IS_ASCII(unicode)) |
| 2027 | return 127; |
| 2028 | |
Victor Stinner | ece58de | 2012-04-23 23:36:38 +0200 | [diff] [blame] | 2029 | kind = PyUnicode_KIND(unicode); |
Benjamin Peterson | f3b7d86 | 2012-04-23 18:07:01 -0400 | [diff] [blame] | 2030 | startptr = PyUnicode_DATA(unicode); |
Benjamin Peterson | b9f4c9d | 2012-04-23 21:45:40 -0400 | [diff] [blame] | 2031 | endptr = (char *)startptr + end * kind; |
| 2032 | startptr = (char *)startptr + start * kind; |
Benjamin Peterson | 2844a7a | 2012-04-23 18:00:25 -0400 | [diff] [blame] | 2033 | switch(kind) { |
| 2034 | case PyUnicode_1BYTE_KIND: |
| 2035 | return ucs1lib_find_max_char(startptr, endptr); |
| 2036 | case PyUnicode_2BYTE_KIND: |
| 2037 | return ucs2lib_find_max_char(startptr, endptr); |
| 2038 | case PyUnicode_4BYTE_KIND: |
| 2039 | return ucs4lib_find_max_char(startptr, endptr); |
Victor Stinner | ece58de | 2012-04-23 23:36:38 +0200 | [diff] [blame] | 2040 | default: |
Benjamin Peterson | 2844a7a | 2012-04-23 18:00:25 -0400 | [diff] [blame] | 2041 | assert(0); |
| 2042 | return 0; |
Victor Stinner | ece58de | 2012-04-23 23:36:38 +0200 | [diff] [blame] | 2043 | } |
| 2044 | } |
| 2045 | |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2046 | /* Ensure that a string uses the most efficient storage, if it is not the |
| 2047 | case: create a new string with of the right kind. Write NULL into *p_unicode |
| 2048 | on error. */ |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 2049 | static void |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2050 | unicode_adjust_maxchar(PyObject **p_unicode) |
| 2051 | { |
| 2052 | PyObject *unicode, *copy; |
| 2053 | Py_UCS4 max_char; |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2054 | Py_ssize_t len; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2055 | unsigned int kind; |
| 2056 | |
| 2057 | assert(p_unicode != NULL); |
| 2058 | unicode = *p_unicode; |
| 2059 | assert(PyUnicode_IS_READY(unicode)); |
| 2060 | if (PyUnicode_IS_ASCII(unicode)) |
| 2061 | return; |
| 2062 | |
| 2063 | len = PyUnicode_GET_LENGTH(unicode); |
| 2064 | kind = PyUnicode_KIND(unicode); |
| 2065 | if (kind == PyUnicode_1BYTE_KIND) { |
| 2066 | const Py_UCS1 *u = PyUnicode_1BYTE_DATA(unicode); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2067 | max_char = ucs1lib_find_max_char(u, u + len); |
| 2068 | if (max_char >= 128) |
| 2069 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2070 | } |
| 2071 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 2072 | const Py_UCS2 *u = PyUnicode_2BYTE_DATA(unicode); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2073 | max_char = ucs2lib_find_max_char(u, u + len); |
| 2074 | if (max_char >= 256) |
| 2075 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2076 | } |
| 2077 | else { |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2078 | const Py_UCS4 *u = PyUnicode_4BYTE_DATA(unicode); |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2079 | assert(kind == PyUnicode_4BYTE_KIND); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2080 | max_char = ucs4lib_find_max_char(u, u + len); |
| 2081 | if (max_char >= 0x10000) |
| 2082 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2083 | } |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2084 | copy = PyUnicode_New(len, max_char); |
| 2085 | copy_characters(copy, 0, unicode, 0, len); |
| 2086 | Py_DECREF(unicode); |
| 2087 | *p_unicode = copy; |
| 2088 | } |
| 2089 | |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2090 | PyObject* |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 2091 | _PyUnicode_Copy(PyObject *unicode) |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2092 | { |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 2093 | Py_ssize_t length; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2094 | PyObject *copy; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2095 | |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2096 | if (!PyUnicode_Check(unicode)) { |
| 2097 | PyErr_BadInternalCall(); |
| 2098 | return NULL; |
| 2099 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 2100 | if (PyUnicode_READY(unicode) == -1) |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2101 | return NULL; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2102 | |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 2103 | length = PyUnicode_GET_LENGTH(unicode); |
| 2104 | copy = PyUnicode_New(length, PyUnicode_MAX_CHAR_VALUE(unicode)); |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2105 | if (!copy) |
| 2106 | return NULL; |
| 2107 | assert(PyUnicode_KIND(copy) == PyUnicode_KIND(unicode)); |
| 2108 | |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 2109 | Py_MEMCPY(PyUnicode_DATA(copy), PyUnicode_DATA(unicode), |
| 2110 | length * PyUnicode_KIND(unicode)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2111 | assert(_PyUnicode_CheckConsistency(copy, 1)); |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2112 | return copy; |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2113 | } |
| 2114 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2115 | |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2116 | /* Widen Unicode objects to larger buffers. Don't write terminating null |
| 2117 | character. Return NULL on error. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2118 | |
| 2119 | void* |
| 2120 | _PyUnicode_AsKind(PyObject *s, unsigned int kind) |
| 2121 | { |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2122 | Py_ssize_t len; |
| 2123 | void *result; |
| 2124 | unsigned int skind; |
| 2125 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 2126 | if (PyUnicode_READY(s) == -1) |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2127 | return NULL; |
| 2128 | |
| 2129 | len = PyUnicode_GET_LENGTH(s); |
| 2130 | skind = PyUnicode_KIND(s); |
| 2131 | if (skind >= kind) { |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 2132 | PyErr_SetString(PyExc_SystemError, "invalid widening attempt"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2133 | return NULL; |
| 2134 | } |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 2135 | switch (kind) { |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2136 | case PyUnicode_2BYTE_KIND: |
| 2137 | result = PyMem_Malloc(len * sizeof(Py_UCS2)); |
| 2138 | if (!result) |
| 2139 | return PyErr_NoMemory(); |
| 2140 | assert(skind == PyUnicode_1BYTE_KIND); |
| 2141 | _PyUnicode_CONVERT_BYTES( |
| 2142 | Py_UCS1, Py_UCS2, |
| 2143 | PyUnicode_1BYTE_DATA(s), |
| 2144 | PyUnicode_1BYTE_DATA(s) + len, |
| 2145 | result); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2146 | return result; |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2147 | case PyUnicode_4BYTE_KIND: |
| 2148 | result = PyMem_Malloc(len * sizeof(Py_UCS4)); |
| 2149 | if (!result) |
| 2150 | return PyErr_NoMemory(); |
| 2151 | if (skind == PyUnicode_2BYTE_KIND) { |
| 2152 | _PyUnicode_CONVERT_BYTES( |
| 2153 | Py_UCS2, Py_UCS4, |
| 2154 | PyUnicode_2BYTE_DATA(s), |
| 2155 | PyUnicode_2BYTE_DATA(s) + len, |
| 2156 | result); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2157 | } |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2158 | else { |
| 2159 | assert(skind == PyUnicode_1BYTE_KIND); |
| 2160 | _PyUnicode_CONVERT_BYTES( |
| 2161 | Py_UCS1, Py_UCS4, |
| 2162 | PyUnicode_1BYTE_DATA(s), |
| 2163 | PyUnicode_1BYTE_DATA(s) + len, |
| 2164 | result); |
| 2165 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2166 | return result; |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2167 | default: |
| 2168 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2169 | } |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 2170 | PyErr_SetString(PyExc_SystemError, "invalid kind"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2171 | return NULL; |
| 2172 | } |
| 2173 | |
| 2174 | static Py_UCS4* |
| 2175 | as_ucs4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, |
| 2176 | int copy_null) |
| 2177 | { |
| 2178 | int kind; |
| 2179 | void *data; |
| 2180 | Py_ssize_t len, targetlen; |
| 2181 | if (PyUnicode_READY(string) == -1) |
| 2182 | return NULL; |
| 2183 | kind = PyUnicode_KIND(string); |
| 2184 | data = PyUnicode_DATA(string); |
| 2185 | len = PyUnicode_GET_LENGTH(string); |
| 2186 | targetlen = len; |
| 2187 | if (copy_null) |
| 2188 | targetlen++; |
| 2189 | if (!target) { |
| 2190 | if (PY_SSIZE_T_MAX / sizeof(Py_UCS4) < targetlen) { |
| 2191 | PyErr_NoMemory(); |
| 2192 | return NULL; |
| 2193 | } |
| 2194 | target = PyMem_Malloc(targetlen * sizeof(Py_UCS4)); |
| 2195 | if (!target) { |
| 2196 | PyErr_NoMemory(); |
| 2197 | return NULL; |
| 2198 | } |
| 2199 | } |
| 2200 | else { |
| 2201 | if (targetsize < targetlen) { |
| 2202 | PyErr_Format(PyExc_SystemError, |
| 2203 | "string is longer than the buffer"); |
| 2204 | if (copy_null && 0 < targetsize) |
| 2205 | target[0] = 0; |
| 2206 | return NULL; |
| 2207 | } |
| 2208 | } |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2209 | if (kind == PyUnicode_1BYTE_KIND) { |
| 2210 | Py_UCS1 *start = (Py_UCS1 *) data; |
| 2211 | _PyUnicode_CONVERT_BYTES(Py_UCS1, Py_UCS4, start, start + len, target); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2212 | } |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2213 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 2214 | Py_UCS2 *start = (Py_UCS2 *) data; |
| 2215 | _PyUnicode_CONVERT_BYTES(Py_UCS2, Py_UCS4, start, start + len, target); |
| 2216 | } |
| 2217 | else { |
| 2218 | assert(kind == PyUnicode_4BYTE_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2219 | Py_MEMCPY(target, data, len * sizeof(Py_UCS4)); |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2220 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2221 | if (copy_null) |
| 2222 | target[len] = 0; |
| 2223 | return target; |
| 2224 | } |
| 2225 | |
| 2226 | Py_UCS4* |
| 2227 | PyUnicode_AsUCS4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, |
| 2228 | int copy_null) |
| 2229 | { |
Antoine Pitrou | de20b0b | 2011-11-10 21:47:38 +0100 | [diff] [blame] | 2230 | if (target == NULL || targetsize < 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2231 | PyErr_BadInternalCall(); |
| 2232 | return NULL; |
| 2233 | } |
| 2234 | return as_ucs4(string, target, targetsize, copy_null); |
| 2235 | } |
| 2236 | |
| 2237 | Py_UCS4* |
| 2238 | PyUnicode_AsUCS4Copy(PyObject *string) |
| 2239 | { |
| 2240 | return as_ucs4(string, NULL, 0, 1); |
| 2241 | } |
| 2242 | |
| 2243 | #ifdef HAVE_WCHAR_H |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 2244 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2245 | PyObject * |
| 2246 | PyUnicode_FromWideChar(register const wchar_t *w, Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2247 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2248 | if (w == NULL) { |
Victor Stinner | 382955f | 2011-12-11 21:44:00 +0100 | [diff] [blame] | 2249 | if (size == 0) { |
| 2250 | Py_INCREF(unicode_empty); |
| 2251 | return unicode_empty; |
| 2252 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2253 | PyErr_BadInternalCall(); |
| 2254 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2255 | } |
| 2256 | |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 2257 | if (size == -1) { |
| 2258 | size = wcslen(w); |
| 2259 | } |
| 2260 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2261 | return PyUnicode_FromUnicode(w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2262 | } |
| 2263 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2264 | #endif /* HAVE_WCHAR_H */ |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 2265 | |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2266 | static void |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2267 | makefmt(char *fmt, int longflag, int longlongflag, int size_tflag, |
| 2268 | int zeropad, int width, int precision, char c) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2269 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2270 | *fmt++ = '%'; |
| 2271 | if (width) { |
| 2272 | if (zeropad) |
| 2273 | *fmt++ = '0'; |
| 2274 | fmt += sprintf(fmt, "%d", width); |
| 2275 | } |
| 2276 | if (precision) |
| 2277 | fmt += sprintf(fmt, ".%d", precision); |
| 2278 | if (longflag) |
| 2279 | *fmt++ = 'l'; |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2280 | else if (longlongflag) { |
| 2281 | /* longlongflag should only ever be nonzero on machines with |
| 2282 | HAVE_LONG_LONG defined */ |
| 2283 | #ifdef HAVE_LONG_LONG |
| 2284 | char *f = PY_FORMAT_LONG_LONG; |
| 2285 | while (*f) |
| 2286 | *fmt++ = *f++; |
| 2287 | #else |
| 2288 | /* we shouldn't ever get here */ |
| 2289 | assert(0); |
| 2290 | *fmt++ = 'l'; |
| 2291 | #endif |
| 2292 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2293 | else if (size_tflag) { |
| 2294 | char *f = PY_FORMAT_SIZE_T; |
| 2295 | while (*f) |
| 2296 | *fmt++ = *f++; |
| 2297 | } |
| 2298 | *fmt++ = c; |
| 2299 | *fmt = '\0'; |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2300 | } |
| 2301 | |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2302 | /* helper for PyUnicode_FromFormatV() */ |
| 2303 | |
| 2304 | static const char* |
| 2305 | parse_format_flags(const char *f, |
| 2306 | int *p_width, int *p_precision, |
| 2307 | int *p_longflag, int *p_longlongflag, int *p_size_tflag) |
| 2308 | { |
| 2309 | int width, precision, longflag, longlongflag, size_tflag; |
| 2310 | |
| 2311 | /* parse the width.precision part, e.g. "%2.5s" => width=2, precision=5 */ |
| 2312 | f++; |
| 2313 | width = 0; |
| 2314 | while (Py_ISDIGIT((unsigned)*f)) |
| 2315 | width = (width*10) + *f++ - '0'; |
| 2316 | precision = 0; |
| 2317 | if (*f == '.') { |
| 2318 | f++; |
| 2319 | while (Py_ISDIGIT((unsigned)*f)) |
| 2320 | precision = (precision*10) + *f++ - '0'; |
| 2321 | if (*f == '%') { |
| 2322 | /* "%.3%s" => f points to "3" */ |
| 2323 | f--; |
| 2324 | } |
| 2325 | } |
| 2326 | if (*f == '\0') { |
| 2327 | /* bogus format "%.1" => go backward, f points to "1" */ |
| 2328 | f--; |
| 2329 | } |
| 2330 | if (p_width != NULL) |
| 2331 | *p_width = width; |
| 2332 | if (p_precision != NULL) |
| 2333 | *p_precision = precision; |
| 2334 | |
| 2335 | /* Handle %ld, %lu, %lld and %llu. */ |
| 2336 | longflag = 0; |
| 2337 | longlongflag = 0; |
Victor Stinner | e7faec1 | 2011-03-02 00:01:53 +0000 | [diff] [blame] | 2338 | size_tflag = 0; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2339 | |
| 2340 | if (*f == 'l') { |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2341 | if (f[1] == 'd' || f[1] == 'u' || f[1] == 'i') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2342 | longflag = 1; |
| 2343 | ++f; |
| 2344 | } |
| 2345 | #ifdef HAVE_LONG_LONG |
| 2346 | else if (f[1] == 'l' && |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2347 | (f[2] == 'd' || f[2] == 'u' || f[2] == 'i')) { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2348 | longlongflag = 1; |
| 2349 | f += 2; |
| 2350 | } |
| 2351 | #endif |
| 2352 | } |
| 2353 | /* handle the size_t flag. */ |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2354 | 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] | 2355 | size_tflag = 1; |
| 2356 | ++f; |
| 2357 | } |
| 2358 | if (p_longflag != NULL) |
| 2359 | *p_longflag = longflag; |
| 2360 | if (p_longlongflag != NULL) |
| 2361 | *p_longlongflag = longlongflag; |
| 2362 | if (p_size_tflag != NULL) |
| 2363 | *p_size_tflag = size_tflag; |
| 2364 | return f; |
| 2365 | } |
| 2366 | |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2367 | /* maximum number of characters required for output of %ld. 21 characters |
| 2368 | allows for 64-bit integers (in decimal) and an optional sign. */ |
| 2369 | #define MAX_LONG_CHARS 21 |
| 2370 | /* maximum number of characters required for output of %lld. |
| 2371 | We need at most ceil(log10(256)*SIZEOF_LONG_LONG) digits, |
| 2372 | plus 1 for the sign. 53/22 is an upper bound for log10(256). */ |
| 2373 | #define MAX_LONG_LONG_CHARS (2 + (SIZEOF_LONG_LONG*53-1) / 22) |
| 2374 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2375 | PyObject * |
| 2376 | PyUnicode_FromFormatV(const char *format, va_list vargs) |
| 2377 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2378 | va_list count; |
| 2379 | Py_ssize_t callcount = 0; |
| 2380 | PyObject **callresults = NULL; |
| 2381 | PyObject **callresult = NULL; |
| 2382 | Py_ssize_t n = 0; |
| 2383 | int width = 0; |
| 2384 | int precision = 0; |
| 2385 | int zeropad; |
| 2386 | const char* f; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2387 | PyObject *string; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2388 | /* used by sprintf */ |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2389 | char fmt[61]; /* should be enough for %0width.precisionlld */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2390 | Py_UCS4 maxchar = 127; /* result is ASCII by default */ |
| 2391 | Py_UCS4 argmaxchar; |
| 2392 | Py_ssize_t numbersize = 0; |
| 2393 | char *numberresults = NULL; |
| 2394 | char *numberresult = NULL; |
| 2395 | Py_ssize_t i; |
| 2396 | int kind; |
| 2397 | void *data; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2398 | |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 2399 | Py_VA_COPY(count, vargs); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2400 | /* step 1: count the number of %S/%R/%A/%s format specifications |
| 2401 | * (we call PyObject_Str()/PyObject_Repr()/PyObject_ASCII()/ |
| 2402 | * 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] | 2403 | * result in an array) |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 2404 | * also estimate a upper bound for all the number formats in the string, |
| 2405 | * 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] | 2406 | * buffer before putting everything together. */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2407 | for (f = format; *f; f++) { |
| 2408 | if (*f == '%') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2409 | int longlongflag; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2410 | /* skip width or width.precision (eg. "1.2" of "%1.2f") */ |
| 2411 | f = parse_format_flags(f, &width, NULL, NULL, &longlongflag, NULL); |
| 2412 | if (*f == 's' || *f=='S' || *f=='R' || *f=='A' || *f=='V') |
| 2413 | ++callcount; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2414 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2415 | else if (*f == 'd' || *f=='u' || *f=='i' || *f=='x' || *f=='p') { |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2416 | #ifdef HAVE_LONG_LONG |
| 2417 | if (longlongflag) { |
| 2418 | if (width < MAX_LONG_LONG_CHARS) |
| 2419 | width = MAX_LONG_LONG_CHARS; |
| 2420 | } |
| 2421 | else |
| 2422 | #endif |
| 2423 | /* MAX_LONG_CHARS is enough to hold a 64-bit integer, |
| 2424 | including sign. Decimal takes the most space. This |
| 2425 | isn't enough for octal. If a width is specified we |
| 2426 | need more (which we allocate later). */ |
| 2427 | if (width < MAX_LONG_CHARS) |
| 2428 | width = MAX_LONG_CHARS; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2429 | |
| 2430 | /* account for the size + '\0' to separate numbers |
| 2431 | inside of the numberresults buffer */ |
| 2432 | numbersize += (width + 1); |
| 2433 | } |
| 2434 | } |
| 2435 | else if ((unsigned char)*f > 127) { |
| 2436 | PyErr_Format(PyExc_ValueError, |
| 2437 | "PyUnicode_FromFormatV() expects an ASCII-encoded format " |
| 2438 | "string, got a non-ASCII byte: 0x%02x", |
| 2439 | (unsigned char)*f); |
| 2440 | return NULL; |
| 2441 | } |
| 2442 | } |
| 2443 | /* step 2: allocate memory for the results of |
| 2444 | * PyObject_Str()/PyObject_Repr()/PyUnicode_DecodeUTF8() calls */ |
| 2445 | if (callcount) { |
| 2446 | callresults = PyObject_Malloc(sizeof(PyObject *) * callcount); |
| 2447 | if (!callresults) { |
| 2448 | PyErr_NoMemory(); |
| 2449 | return NULL; |
| 2450 | } |
| 2451 | callresult = callresults; |
| 2452 | } |
| 2453 | /* step 2.5: allocate memory for the results of formating numbers */ |
| 2454 | if (numbersize) { |
| 2455 | numberresults = PyObject_Malloc(numbersize); |
| 2456 | if (!numberresults) { |
| 2457 | PyErr_NoMemory(); |
| 2458 | goto fail; |
| 2459 | } |
| 2460 | numberresult = numberresults; |
| 2461 | } |
| 2462 | |
| 2463 | /* step 3: format numbers and figure out how large a buffer we need */ |
| 2464 | for (f = format; *f; f++) { |
| 2465 | if (*f == '%') { |
| 2466 | const char* p; |
| 2467 | int longflag; |
| 2468 | int longlongflag; |
| 2469 | int size_tflag; |
| 2470 | int numprinted; |
| 2471 | |
| 2472 | p = f; |
| 2473 | zeropad = (f[1] == '0'); |
| 2474 | f = parse_format_flags(f, &width, &precision, |
| 2475 | &longflag, &longlongflag, &size_tflag); |
| 2476 | switch (*f) { |
| 2477 | case 'c': |
| 2478 | { |
| 2479 | Py_UCS4 ordinal = va_arg(count, int); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 2480 | maxchar = MAX_MAXCHAR(maxchar, ordinal); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2481 | n++; |
| 2482 | break; |
| 2483 | } |
| 2484 | case '%': |
| 2485 | n++; |
| 2486 | break; |
| 2487 | case 'i': |
| 2488 | case 'd': |
| 2489 | makefmt(fmt, longflag, longlongflag, size_tflag, zeropad, |
| 2490 | width, precision, *f); |
| 2491 | if (longflag) |
| 2492 | numprinted = sprintf(numberresult, fmt, |
| 2493 | va_arg(count, long)); |
| 2494 | #ifdef HAVE_LONG_LONG |
| 2495 | else if (longlongflag) |
| 2496 | numprinted = sprintf(numberresult, fmt, |
| 2497 | va_arg(count, PY_LONG_LONG)); |
| 2498 | #endif |
| 2499 | else if (size_tflag) |
| 2500 | numprinted = sprintf(numberresult, fmt, |
| 2501 | va_arg(count, Py_ssize_t)); |
| 2502 | else |
| 2503 | numprinted = sprintf(numberresult, fmt, |
| 2504 | va_arg(count, int)); |
| 2505 | n += numprinted; |
| 2506 | /* advance by +1 to skip over the '\0' */ |
| 2507 | numberresult += (numprinted + 1); |
| 2508 | assert(*(numberresult - 1) == '\0'); |
| 2509 | assert(*(numberresult - 2) != '\0'); |
| 2510 | assert(numprinted >= 0); |
| 2511 | assert(numberresult <= numberresults + numbersize); |
| 2512 | break; |
| 2513 | case 'u': |
| 2514 | makefmt(fmt, longflag, longlongflag, size_tflag, zeropad, |
| 2515 | width, precision, 'u'); |
| 2516 | if (longflag) |
| 2517 | numprinted = sprintf(numberresult, fmt, |
| 2518 | va_arg(count, unsigned long)); |
| 2519 | #ifdef HAVE_LONG_LONG |
| 2520 | else if (longlongflag) |
| 2521 | numprinted = sprintf(numberresult, fmt, |
| 2522 | va_arg(count, unsigned PY_LONG_LONG)); |
| 2523 | #endif |
| 2524 | else if (size_tflag) |
| 2525 | numprinted = sprintf(numberresult, fmt, |
| 2526 | va_arg(count, size_t)); |
| 2527 | else |
| 2528 | numprinted = sprintf(numberresult, fmt, |
| 2529 | va_arg(count, unsigned int)); |
| 2530 | n += numprinted; |
| 2531 | numberresult += (numprinted + 1); |
| 2532 | assert(*(numberresult - 1) == '\0'); |
| 2533 | assert(*(numberresult - 2) != '\0'); |
| 2534 | assert(numprinted >= 0); |
| 2535 | assert(numberresult <= numberresults + numbersize); |
| 2536 | break; |
| 2537 | case 'x': |
| 2538 | makefmt(fmt, 0, 0, 0, zeropad, width, precision, 'x'); |
| 2539 | numprinted = sprintf(numberresult, fmt, va_arg(count, int)); |
| 2540 | n += numprinted; |
| 2541 | numberresult += (numprinted + 1); |
| 2542 | assert(*(numberresult - 1) == '\0'); |
| 2543 | assert(*(numberresult - 2) != '\0'); |
| 2544 | assert(numprinted >= 0); |
| 2545 | assert(numberresult <= numberresults + numbersize); |
| 2546 | break; |
| 2547 | case 'p': |
| 2548 | numprinted = sprintf(numberresult, "%p", va_arg(count, void*)); |
| 2549 | /* %p is ill-defined: ensure leading 0x. */ |
| 2550 | if (numberresult[1] == 'X') |
| 2551 | numberresult[1] = 'x'; |
| 2552 | else if (numberresult[1] != 'x') { |
| 2553 | memmove(numberresult + 2, numberresult, |
| 2554 | strlen(numberresult) + 1); |
| 2555 | numberresult[0] = '0'; |
| 2556 | numberresult[1] = 'x'; |
| 2557 | numprinted += 2; |
| 2558 | } |
| 2559 | n += numprinted; |
| 2560 | numberresult += (numprinted + 1); |
| 2561 | assert(*(numberresult - 1) == '\0'); |
| 2562 | assert(*(numberresult - 2) != '\0'); |
| 2563 | assert(numprinted >= 0); |
| 2564 | assert(numberresult <= numberresults + numbersize); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2565 | break; |
| 2566 | case 's': |
| 2567 | { |
| 2568 | /* UTF-8 */ |
Georg Brandl | 780b2a6 | 2009-05-05 09:19:59 +0000 | [diff] [blame] | 2569 | const char *s = va_arg(count, const char*); |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 2570 | PyObject *str = PyUnicode_DecodeUTF8Stateful(s, strlen(s), "replace", NULL); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2571 | if (!str) |
| 2572 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2573 | /* since PyUnicode_DecodeUTF8 returns already flexible |
| 2574 | unicode objects, there is no need to call ready on them */ |
| 2575 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 2576 | maxchar = MAX_MAXCHAR(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2577 | n += PyUnicode_GET_LENGTH(str); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2578 | /* Remember the str and switch to the next slot */ |
| 2579 | *callresult++ = str; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2580 | break; |
| 2581 | } |
| 2582 | case 'U': |
| 2583 | { |
| 2584 | PyObject *obj = va_arg(count, PyObject *); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 2585 | assert(obj && _PyUnicode_CHECK(obj)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2586 | if (PyUnicode_READY(obj) == -1) |
| 2587 | goto fail; |
| 2588 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(obj); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 2589 | maxchar = MAX_MAXCHAR(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2590 | n += PyUnicode_GET_LENGTH(obj); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2591 | break; |
| 2592 | } |
| 2593 | case 'V': |
| 2594 | { |
| 2595 | PyObject *obj = va_arg(count, PyObject *); |
| 2596 | const char *str = va_arg(count, const char *); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2597 | PyObject *str_obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2598 | assert(obj || str); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 2599 | assert(!obj || _PyUnicode_CHECK(obj)); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2600 | if (obj) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2601 | if (PyUnicode_READY(obj) == -1) |
| 2602 | goto fail; |
| 2603 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(obj); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 2604 | maxchar = MAX_MAXCHAR(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2605 | n += PyUnicode_GET_LENGTH(obj); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2606 | *callresult++ = NULL; |
| 2607 | } |
| 2608 | else { |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 2609 | str_obj = PyUnicode_DecodeUTF8Stateful(str, strlen(str), "replace", NULL); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2610 | if (!str_obj) |
| 2611 | goto fail; |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 2612 | if (PyUnicode_READY(str_obj) == -1) { |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 2613 | Py_DECREF(str_obj); |
| 2614 | goto fail; |
| 2615 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2616 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str_obj); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 2617 | maxchar = MAX_MAXCHAR(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2618 | n += PyUnicode_GET_LENGTH(str_obj); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2619 | *callresult++ = str_obj; |
| 2620 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2621 | break; |
| 2622 | } |
| 2623 | case 'S': |
| 2624 | { |
| 2625 | PyObject *obj = va_arg(count, PyObject *); |
| 2626 | PyObject *str; |
| 2627 | assert(obj); |
| 2628 | str = PyObject_Str(obj); |
Benjamin Peterson | c8d8b88 | 2012-01-14 13:37:31 -0500 | [diff] [blame] | 2629 | if (!str) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2630 | goto fail; |
Benjamin Peterson | c8d8b88 | 2012-01-14 13:37:31 -0500 | [diff] [blame] | 2631 | if (PyUnicode_READY(str) == -1) { |
| 2632 | Py_DECREF(str); |
| 2633 | goto fail; |
| 2634 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2635 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 2636 | maxchar = MAX_MAXCHAR(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2637 | n += PyUnicode_GET_LENGTH(str); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2638 | /* Remember the str and switch to the next slot */ |
| 2639 | *callresult++ = str; |
| 2640 | break; |
| 2641 | } |
| 2642 | case 'R': |
| 2643 | { |
| 2644 | PyObject *obj = va_arg(count, PyObject *); |
| 2645 | PyObject *repr; |
| 2646 | assert(obj); |
| 2647 | repr = PyObject_Repr(obj); |
Benjamin Peterson | c8d8b88 | 2012-01-14 13:37:31 -0500 | [diff] [blame] | 2648 | if (!repr) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2649 | goto fail; |
Benjamin Peterson | c8d8b88 | 2012-01-14 13:37:31 -0500 | [diff] [blame] | 2650 | if (PyUnicode_READY(repr) == -1) { |
| 2651 | Py_DECREF(repr); |
| 2652 | goto fail; |
| 2653 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2654 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(repr); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 2655 | maxchar = MAX_MAXCHAR(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2656 | n += PyUnicode_GET_LENGTH(repr); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2657 | /* Remember the repr and switch to the next slot */ |
| 2658 | *callresult++ = repr; |
| 2659 | break; |
| 2660 | } |
| 2661 | case 'A': |
| 2662 | { |
| 2663 | PyObject *obj = va_arg(count, PyObject *); |
| 2664 | PyObject *ascii; |
| 2665 | assert(obj); |
| 2666 | ascii = PyObject_ASCII(obj); |
Benjamin Peterson | c8d8b88 | 2012-01-14 13:37:31 -0500 | [diff] [blame] | 2667 | if (!ascii) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2668 | goto fail; |
Benjamin Peterson | c8d8b88 | 2012-01-14 13:37:31 -0500 | [diff] [blame] | 2669 | if (PyUnicode_READY(ascii) == -1) { |
| 2670 | Py_DECREF(ascii); |
| 2671 | goto fail; |
| 2672 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2673 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(ascii); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 2674 | maxchar = MAX_MAXCHAR(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2675 | n += PyUnicode_GET_LENGTH(ascii); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2676 | /* Remember the repr and switch to the next slot */ |
| 2677 | *callresult++ = ascii; |
| 2678 | break; |
| 2679 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2680 | default: |
| 2681 | /* if we stumble upon an unknown |
| 2682 | formatting code, copy the rest of |
| 2683 | the format string to the output |
| 2684 | string. (we cannot just skip the |
| 2685 | code, since there's no way to know |
| 2686 | what's in the argument list) */ |
| 2687 | n += strlen(p); |
| 2688 | goto expand; |
| 2689 | } |
| 2690 | } else |
| 2691 | n++; |
| 2692 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2693 | expand: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2694 | /* step 4: fill the buffer */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2695 | /* Since we've analyzed how much space we need, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2696 | we don't have to resize the string. |
| 2697 | There can be no errors beyond this point. */ |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2698 | string = PyUnicode_New(n, maxchar); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2699 | if (!string) |
| 2700 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2701 | kind = PyUnicode_KIND(string); |
| 2702 | data = PyUnicode_DATA(string); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2703 | callresult = callresults; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2704 | numberresult = numberresults; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2705 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2706 | for (i = 0, f = format; *f; f++) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2707 | if (*f == '%') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2708 | const char* p; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2709 | |
| 2710 | p = f; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2711 | f = parse_format_flags(f, NULL, NULL, NULL, NULL, NULL); |
| 2712 | /* checking for == because the last argument could be a empty |
| 2713 | string, which causes i to point to end, the assert at the end of |
| 2714 | the loop */ |
| 2715 | assert(i <= PyUnicode_GET_LENGTH(string)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2716 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2717 | switch (*f) { |
| 2718 | case 'c': |
Victor Stinner | 5ed8b2c | 2011-02-21 21:13:44 +0000 | [diff] [blame] | 2719 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2720 | const int ordinal = va_arg(vargs, int); |
| 2721 | PyUnicode_WRITE(kind, data, i++, ordinal); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2722 | break; |
Victor Stinner | 5ed8b2c | 2011-02-21 21:13:44 +0000 | [diff] [blame] | 2723 | } |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2724 | case 'i': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2725 | case 'd': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2726 | case 'u': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2727 | case 'x': |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2728 | case 'p': |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 2729 | { |
| 2730 | Py_ssize_t written; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2731 | /* unused, since we already have the result */ |
| 2732 | if (*f == 'p') |
| 2733 | (void) va_arg(vargs, void *); |
| 2734 | else |
| 2735 | (void) va_arg(vargs, int); |
| 2736 | /* extract the result from numberresults and append. */ |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 2737 | written = unicode_write_cstr(string, i, numberresult); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2738 | /* skip over the separating '\0' */ |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 2739 | i += written; |
| 2740 | numberresult += written; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2741 | assert(*numberresult == '\0'); |
| 2742 | numberresult++; |
| 2743 | assert(numberresult <= numberresults + numbersize); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2744 | break; |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 2745 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2746 | case 's': |
| 2747 | { |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2748 | /* unused, since we already have the result */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2749 | Py_ssize_t size; |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2750 | (void) va_arg(vargs, char *); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2751 | size = PyUnicode_GET_LENGTH(*callresult); |
| 2752 | assert(PyUnicode_KIND(*callresult) <= PyUnicode_KIND(string)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2753 | copy_characters(string, i, *callresult, 0, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2754 | i += size; |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2755 | /* We're done with the unicode()/repr() => forget it */ |
| 2756 | Py_DECREF(*callresult); |
| 2757 | /* switch to next unicode()/repr() result */ |
| 2758 | ++callresult; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2759 | break; |
| 2760 | } |
| 2761 | case 'U': |
| 2762 | { |
| 2763 | PyObject *obj = va_arg(vargs, PyObject *); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2764 | Py_ssize_t size; |
| 2765 | assert(PyUnicode_KIND(obj) <= PyUnicode_KIND(string)); |
| 2766 | size = PyUnicode_GET_LENGTH(obj); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2767 | copy_characters(string, i, obj, 0, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2768 | i += size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2769 | break; |
| 2770 | } |
| 2771 | case 'V': |
| 2772 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2773 | Py_ssize_t size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2774 | PyObject *obj = va_arg(vargs, PyObject *); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2775 | va_arg(vargs, const char *); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2776 | if (obj) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2777 | size = PyUnicode_GET_LENGTH(obj); |
| 2778 | assert(PyUnicode_KIND(obj) <= PyUnicode_KIND(string)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2779 | copy_characters(string, i, obj, 0, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2780 | i += size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2781 | } else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2782 | size = PyUnicode_GET_LENGTH(*callresult); |
| 2783 | assert(PyUnicode_KIND(*callresult) <= |
| 2784 | PyUnicode_KIND(string)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2785 | copy_characters(string, i, *callresult, 0, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2786 | i += size; |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2787 | Py_DECREF(*callresult); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2788 | } |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2789 | ++callresult; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2790 | break; |
| 2791 | } |
| 2792 | case 'S': |
| 2793 | case 'R': |
Victor Stinner | 9a90900 | 2010-10-18 20:59:24 +0000 | [diff] [blame] | 2794 | case 'A': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2795 | { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2796 | Py_ssize_t size = PyUnicode_GET_LENGTH(*callresult); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2797 | /* unused, since we already have the result */ |
| 2798 | (void) va_arg(vargs, PyObject *); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2799 | assert(PyUnicode_KIND(*callresult) <= PyUnicode_KIND(string)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2800 | copy_characters(string, i, *callresult, 0, size); |
| 2801 | i += size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2802 | /* We're done with the unicode()/repr() => forget it */ |
| 2803 | Py_DECREF(*callresult); |
| 2804 | /* switch to next unicode()/repr() result */ |
| 2805 | ++callresult; |
| 2806 | break; |
| 2807 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2808 | case '%': |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2809 | PyUnicode_WRITE(kind, data, i++, '%'); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2810 | break; |
| 2811 | default: |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 2812 | i += unicode_write_cstr(string, i, p); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2813 | assert(i == PyUnicode_GET_LENGTH(string)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2814 | goto end; |
| 2815 | } |
Victor Stinner | 1205f27 | 2010-09-11 00:54:47 +0000 | [diff] [blame] | 2816 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2817 | else { |
| 2818 | assert(i < PyUnicode_GET_LENGTH(string)); |
| 2819 | PyUnicode_WRITE(kind, data, i++, *f); |
| 2820 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2821 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2822 | assert(i == PyUnicode_GET_LENGTH(string)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2823 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2824 | end: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2825 | if (callresults) |
| 2826 | PyObject_Free(callresults); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2827 | if (numberresults) |
| 2828 | PyObject_Free(numberresults); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 2829 | return unicode_result(string); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2830 | fail: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2831 | if (callresults) { |
| 2832 | PyObject **callresult2 = callresults; |
| 2833 | while (callresult2 < callresult) { |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2834 | Py_XDECREF(*callresult2); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2835 | ++callresult2; |
| 2836 | } |
| 2837 | PyObject_Free(callresults); |
| 2838 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2839 | if (numberresults) |
| 2840 | PyObject_Free(numberresults); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2841 | return NULL; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2842 | } |
| 2843 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2844 | PyObject * |
| 2845 | PyUnicode_FromFormat(const char *format, ...) |
| 2846 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2847 | PyObject* ret; |
| 2848 | va_list vargs; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2849 | |
| 2850 | #ifdef HAVE_STDARG_PROTOTYPES |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2851 | va_start(vargs, format); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2852 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2853 | va_start(vargs); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2854 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2855 | ret = PyUnicode_FromFormatV(format, vargs); |
| 2856 | va_end(vargs); |
| 2857 | return ret; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2858 | } |
| 2859 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2860 | #ifdef HAVE_WCHAR_H |
| 2861 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2862 | /* Helper function for PyUnicode_AsWideChar() and PyUnicode_AsWideCharString(): |
| 2863 | convert a Unicode object to a wide character string. |
| 2864 | |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2865 | - 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] | 2866 | character) required to convert the unicode object. Ignore size argument. |
| 2867 | |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2868 | - Otherwise: return the number of wide characters (excluding the null |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2869 | character) written into w. Write at most size wide characters (including |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2870 | the null character). */ |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2871 | static Py_ssize_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2872 | unicode_aswidechar(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2873 | wchar_t *w, |
| 2874 | Py_ssize_t size) |
| 2875 | { |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2876 | Py_ssize_t res; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2877 | const wchar_t *wstr; |
| 2878 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2879 | wstr = PyUnicode_AsUnicodeAndSize(unicode, &res); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2880 | if (wstr == NULL) |
| 2881 | return -1; |
| 2882 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2883 | if (w != NULL) { |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2884 | if (size > res) |
| 2885 | size = res + 1; |
| 2886 | else |
| 2887 | res = size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2888 | Py_MEMCPY(w, wstr, size * sizeof(wchar_t)); |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2889 | return res; |
| 2890 | } |
| 2891 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2892 | return res + 1; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2893 | } |
| 2894 | |
| 2895 | Py_ssize_t |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2896 | PyUnicode_AsWideChar(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2897 | wchar_t *w, |
| 2898 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2899 | { |
| 2900 | if (unicode == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2901 | PyErr_BadInternalCall(); |
| 2902 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2903 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2904 | return unicode_aswidechar(unicode, w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2905 | } |
| 2906 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2907 | wchar_t* |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 2908 | PyUnicode_AsWideCharString(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2909 | Py_ssize_t *size) |
| 2910 | { |
| 2911 | wchar_t* buffer; |
| 2912 | Py_ssize_t buflen; |
| 2913 | |
| 2914 | if (unicode == NULL) { |
| 2915 | PyErr_BadInternalCall(); |
| 2916 | return NULL; |
| 2917 | } |
| 2918 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2919 | buflen = unicode_aswidechar(unicode, NULL, 0); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2920 | if (buflen == -1) |
| 2921 | return NULL; |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2922 | if (PY_SSIZE_T_MAX / sizeof(wchar_t) < buflen) { |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2923 | PyErr_NoMemory(); |
| 2924 | return NULL; |
| 2925 | } |
| 2926 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2927 | buffer = PyMem_MALLOC(buflen * sizeof(wchar_t)); |
| 2928 | if (buffer == NULL) { |
| 2929 | PyErr_NoMemory(); |
| 2930 | return NULL; |
| 2931 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2932 | buflen = unicode_aswidechar(unicode, buffer, buflen); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2933 | if (buflen == -1) |
| 2934 | return NULL; |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2935 | if (size != NULL) |
| 2936 | *size = buflen; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2937 | return buffer; |
| 2938 | } |
| 2939 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2940 | #endif /* HAVE_WCHAR_H */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2941 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2942 | PyObject * |
| 2943 | PyUnicode_FromOrdinal(int ordinal) |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2944 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2945 | PyObject *v; |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 2946 | if (ordinal < 0 || ordinal > MAX_UNICODE) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2947 | PyErr_SetString(PyExc_ValueError, |
| 2948 | "chr() arg not in range(0x110000)"); |
| 2949 | return NULL; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2950 | } |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 2951 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2952 | if (ordinal < 256) |
| 2953 | return get_latin1_char(ordinal); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2954 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2955 | v = PyUnicode_New(1, ordinal); |
| 2956 | if (v == NULL) |
| 2957 | return NULL; |
| 2958 | PyUnicode_WRITE(PyUnicode_KIND(v), PyUnicode_DATA(v), 0, ordinal); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2959 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2960 | return v; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2961 | } |
| 2962 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2963 | PyObject * |
| 2964 | PyUnicode_FromObject(register PyObject *obj) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2965 | { |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2966 | /* XXX Perhaps we should make this API an alias of |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2967 | PyObject_Str() instead ?! */ |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2968 | if (PyUnicode_CheckExact(obj)) { |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 2969 | if (PyUnicode_READY(obj) == -1) |
Victor Stinner | d3a83d5 | 2011-10-01 03:09:33 +0200 | [diff] [blame] | 2970 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2971 | Py_INCREF(obj); |
| 2972 | return obj; |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2973 | } |
| 2974 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2975 | /* For a Unicode subtype that's not a Unicode object, |
| 2976 | return a true Unicode object with the same data. */ |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 2977 | return _PyUnicode_Copy(obj); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2978 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2979 | PyErr_Format(PyExc_TypeError, |
| 2980 | "Can't convert '%.100s' object to str implicitly", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2981 | Py_TYPE(obj)->tp_name); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2982 | return NULL; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2983 | } |
| 2984 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2985 | PyObject * |
| 2986 | PyUnicode_FromEncodedObject(register PyObject *obj, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2987 | const char *encoding, |
| 2988 | const char *errors) |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2989 | { |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2990 | Py_buffer buffer; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2991 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2992 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2993 | if (obj == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2994 | PyErr_BadInternalCall(); |
| 2995 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2996 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2997 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2998 | /* Decoding bytes objects is the most common case and should be fast */ |
| 2999 | if (PyBytes_Check(obj)) { |
| 3000 | if (PyBytes_GET_SIZE(obj) == 0) { |
| 3001 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 3002 | v = unicode_empty; |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 3003 | } |
| 3004 | else { |
| 3005 | v = PyUnicode_Decode( |
| 3006 | PyBytes_AS_STRING(obj), PyBytes_GET_SIZE(obj), |
| 3007 | encoding, errors); |
| 3008 | } |
| 3009 | return v; |
| 3010 | } |
| 3011 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 3012 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3013 | PyErr_SetString(PyExc_TypeError, |
| 3014 | "decoding str is not supported"); |
| 3015 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 3016 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 3017 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 3018 | /* Retrieve a bytes buffer view through the PEP 3118 buffer interface */ |
| 3019 | if (PyObject_GetBuffer(obj, &buffer, PyBUF_SIMPLE) < 0) { |
| 3020 | PyErr_Format(PyExc_TypeError, |
| 3021 | "coercing to str: need bytes, bytearray " |
| 3022 | "or buffer-like object, %.80s found", |
| 3023 | Py_TYPE(obj)->tp_name); |
| 3024 | return NULL; |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 3025 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3026 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 3027 | if (buffer.len == 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3028 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 3029 | v = unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3030 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3031 | else |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 3032 | v = PyUnicode_Decode((char*) buffer.buf, buffer.len, encoding, errors); |
Marc-André Lemburg | ad7c98e | 2001-01-17 17:09:53 +0000 | [diff] [blame] | 3033 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 3034 | PyBuffer_Release(&buffer); |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 3035 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3036 | } |
| 3037 | |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3038 | /* Convert encoding to lower case and replace '_' with '-' in order to |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3039 | catch e.g. UTF_8. Return 0 on error (encoding is longer than lower_len-1), |
| 3040 | 1 on success. */ |
| 3041 | static int |
| 3042 | normalize_encoding(const char *encoding, |
| 3043 | char *lower, |
| 3044 | size_t lower_len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3045 | { |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 3046 | const char *e; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3047 | char *l; |
| 3048 | char *l_end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3049 | |
Benjamin Peterson | 7a6debe | 2011-10-15 09:25:28 -0400 | [diff] [blame] | 3050 | if (encoding == NULL) { |
| 3051 | strcpy(lower, "utf-8"); |
| 3052 | return 1; |
| 3053 | } |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 3054 | e = encoding; |
| 3055 | l = lower; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3056 | l_end = &lower[lower_len - 1]; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3057 | while (*e) { |
| 3058 | if (l == l_end) |
| 3059 | return 0; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 3060 | if (Py_ISUPPER(*e)) { |
| 3061 | *l++ = Py_TOLOWER(*e++); |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 3062 | } |
| 3063 | else if (*e == '_') { |
| 3064 | *l++ = '-'; |
| 3065 | e++; |
| 3066 | } |
| 3067 | else { |
| 3068 | *l++ = *e++; |
| 3069 | } |
| 3070 | } |
| 3071 | *l = '\0'; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3072 | return 1; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3073 | } |
| 3074 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3075 | PyObject * |
| 3076 | PyUnicode_Decode(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3077 | Py_ssize_t size, |
| 3078 | const char *encoding, |
| 3079 | const char *errors) |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3080 | { |
| 3081 | PyObject *buffer = NULL, *unicode; |
| 3082 | Py_buffer info; |
| 3083 | char lower[11]; /* Enough for any encoding shortcut */ |
| 3084 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3085 | /* Shortcuts for common default encodings */ |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3086 | if (normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3087 | if ((strcmp(lower, "utf-8") == 0) || |
| 3088 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 3089 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3090 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3091 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3092 | (strcmp(lower, "iso-8859-1") == 0)) |
| 3093 | return PyUnicode_DecodeLatin1(s, size, errors); |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3094 | #ifdef HAVE_MBCS |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3095 | else if (strcmp(lower, "mbcs") == 0) |
| 3096 | return PyUnicode_DecodeMBCS(s, size, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 3097 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3098 | else if (strcmp(lower, "ascii") == 0) |
| 3099 | return PyUnicode_DecodeASCII(s, size, errors); |
| 3100 | else if (strcmp(lower, "utf-16") == 0) |
| 3101 | return PyUnicode_DecodeUTF16(s, size, errors, 0); |
| 3102 | else if (strcmp(lower, "utf-32") == 0) |
| 3103 | return PyUnicode_DecodeUTF32(s, size, errors, 0); |
| 3104 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3105 | |
| 3106 | /* Decode via the codec registry */ |
Guido van Rossum | be801ac | 2007-10-08 03:32:34 +0000 | [diff] [blame] | 3107 | buffer = NULL; |
Antoine Pitrou | c3b3924 | 2009-01-03 16:59:18 +0000 | [diff] [blame] | 3108 | 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] | 3109 | goto onError; |
Antoine Pitrou | ee58fa4 | 2008-08-19 18:22:14 +0000 | [diff] [blame] | 3110 | buffer = PyMemoryView_FromBuffer(&info); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3111 | if (buffer == NULL) |
| 3112 | goto onError; |
| 3113 | unicode = PyCodec_Decode(buffer, encoding, errors); |
| 3114 | if (unicode == NULL) |
| 3115 | goto onError; |
| 3116 | if (!PyUnicode_Check(unicode)) { |
| 3117 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3118 | "decoder did not return a str object (type=%.400s)", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 3119 | Py_TYPE(unicode)->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3120 | Py_DECREF(unicode); |
| 3121 | goto onError; |
| 3122 | } |
| 3123 | Py_DECREF(buffer); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 3124 | return unicode_result(unicode); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3125 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3126 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3127 | Py_XDECREF(buffer); |
| 3128 | return NULL; |
| 3129 | } |
| 3130 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3131 | PyObject * |
| 3132 | PyUnicode_AsDecodedObject(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3133 | const char *encoding, |
| 3134 | const char *errors) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3135 | { |
| 3136 | PyObject *v; |
| 3137 | |
| 3138 | if (!PyUnicode_Check(unicode)) { |
| 3139 | PyErr_BadArgument(); |
| 3140 | goto onError; |
| 3141 | } |
| 3142 | |
| 3143 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3144 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3145 | |
| 3146 | /* Decode via the codec registry */ |
| 3147 | v = PyCodec_Decode(unicode, encoding, errors); |
| 3148 | if (v == NULL) |
| 3149 | goto onError; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 3150 | return unicode_result(v); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3151 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3152 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3153 | return NULL; |
| 3154 | } |
| 3155 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3156 | PyObject * |
| 3157 | PyUnicode_AsDecodedUnicode(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3158 | const char *encoding, |
| 3159 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3160 | { |
| 3161 | PyObject *v; |
| 3162 | |
| 3163 | if (!PyUnicode_Check(unicode)) { |
| 3164 | PyErr_BadArgument(); |
| 3165 | goto onError; |
| 3166 | } |
| 3167 | |
| 3168 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3169 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3170 | |
| 3171 | /* Decode via the codec registry */ |
| 3172 | v = PyCodec_Decode(unicode, encoding, errors); |
| 3173 | if (v == NULL) |
| 3174 | goto onError; |
| 3175 | if (!PyUnicode_Check(v)) { |
| 3176 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3177 | "decoder did not return a str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3178 | Py_TYPE(v)->tp_name); |
| 3179 | Py_DECREF(v); |
| 3180 | goto onError; |
| 3181 | } |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 3182 | return unicode_result(v); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3183 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3184 | onError: |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3185 | return NULL; |
| 3186 | } |
| 3187 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3188 | PyObject * |
| 3189 | PyUnicode_Encode(const Py_UNICODE *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3190 | Py_ssize_t size, |
| 3191 | const char *encoding, |
| 3192 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3193 | { |
| 3194 | PyObject *v, *unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3195 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3196 | unicode = PyUnicode_FromUnicode(s, size); |
| 3197 | if (unicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3198 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3199 | v = PyUnicode_AsEncodedString(unicode, encoding, errors); |
| 3200 | Py_DECREF(unicode); |
| 3201 | return v; |
| 3202 | } |
| 3203 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3204 | PyObject * |
| 3205 | PyUnicode_AsEncodedObject(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3206 | const char *encoding, |
| 3207 | const char *errors) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3208 | { |
| 3209 | PyObject *v; |
| 3210 | |
| 3211 | if (!PyUnicode_Check(unicode)) { |
| 3212 | PyErr_BadArgument(); |
| 3213 | goto onError; |
| 3214 | } |
| 3215 | |
| 3216 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3217 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3218 | |
| 3219 | /* Encode via the codec registry */ |
| 3220 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3221 | if (v == NULL) |
| 3222 | goto onError; |
| 3223 | return v; |
| 3224 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3225 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3226 | return NULL; |
| 3227 | } |
| 3228 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3229 | static size_t |
| 3230 | wcstombs_errorpos(const wchar_t *wstr) |
| 3231 | { |
| 3232 | size_t len; |
| 3233 | #if SIZEOF_WCHAR_T == 2 |
| 3234 | wchar_t buf[3]; |
| 3235 | #else |
| 3236 | wchar_t buf[2]; |
| 3237 | #endif |
| 3238 | char outbuf[MB_LEN_MAX]; |
| 3239 | const wchar_t *start, *previous; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3240 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3241 | #if SIZEOF_WCHAR_T == 2 |
| 3242 | buf[2] = 0; |
| 3243 | #else |
| 3244 | buf[1] = 0; |
| 3245 | #endif |
| 3246 | start = wstr; |
| 3247 | while (*wstr != L'\0') |
| 3248 | { |
| 3249 | previous = wstr; |
| 3250 | #if SIZEOF_WCHAR_T == 2 |
| 3251 | if (Py_UNICODE_IS_HIGH_SURROGATE(wstr[0]) |
| 3252 | && Py_UNICODE_IS_LOW_SURROGATE(wstr[1])) |
| 3253 | { |
| 3254 | buf[0] = wstr[0]; |
| 3255 | buf[1] = wstr[1]; |
| 3256 | wstr += 2; |
| 3257 | } |
| 3258 | else { |
| 3259 | buf[0] = *wstr; |
| 3260 | buf[1] = 0; |
| 3261 | wstr++; |
| 3262 | } |
| 3263 | #else |
| 3264 | buf[0] = *wstr; |
| 3265 | wstr++; |
| 3266 | #endif |
| 3267 | len = wcstombs(outbuf, buf, sizeof(outbuf)); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3268 | if (len == (size_t)-1) |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3269 | return previous - start; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3270 | } |
| 3271 | |
| 3272 | /* failed to find the unencodable character */ |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3273 | return 0; |
| 3274 | } |
| 3275 | |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3276 | static int |
| 3277 | locale_error_handler(const char *errors, int *surrogateescape) |
| 3278 | { |
| 3279 | if (errors == NULL) { |
| 3280 | *surrogateescape = 0; |
| 3281 | return 0; |
| 3282 | } |
| 3283 | |
| 3284 | if (strcmp(errors, "strict") == 0) { |
| 3285 | *surrogateescape = 0; |
| 3286 | return 0; |
| 3287 | } |
| 3288 | if (strcmp(errors, "surrogateescape") == 0) { |
| 3289 | *surrogateescape = 1; |
| 3290 | return 0; |
| 3291 | } |
| 3292 | PyErr_Format(PyExc_ValueError, |
| 3293 | "only 'strict' and 'surrogateescape' error handlers " |
| 3294 | "are supported, not '%s'", |
| 3295 | errors); |
| 3296 | return -1; |
| 3297 | } |
| 3298 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3299 | PyObject * |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3300 | PyUnicode_EncodeLocale(PyObject *unicode, const char *errors) |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3301 | { |
| 3302 | Py_ssize_t wlen, wlen2; |
| 3303 | wchar_t *wstr; |
| 3304 | PyObject *bytes = NULL; |
| 3305 | char *errmsg; |
Victor Stinner | 1f33f2b | 2011-12-17 04:45:09 +0100 | [diff] [blame] | 3306 | PyObject *reason; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3307 | PyObject *exc; |
| 3308 | size_t error_pos; |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3309 | int surrogateescape; |
| 3310 | |
| 3311 | if (locale_error_handler(errors, &surrogateescape) < 0) |
| 3312 | return NULL; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3313 | |
| 3314 | wstr = PyUnicode_AsWideCharString(unicode, &wlen); |
| 3315 | if (wstr == NULL) |
| 3316 | return NULL; |
| 3317 | |
| 3318 | wlen2 = wcslen(wstr); |
| 3319 | if (wlen2 != wlen) { |
| 3320 | PyMem_Free(wstr); |
| 3321 | PyErr_SetString(PyExc_TypeError, "embedded null character"); |
| 3322 | return NULL; |
| 3323 | } |
| 3324 | |
| 3325 | if (surrogateescape) { |
| 3326 | /* locale encoding with surrogateescape */ |
| 3327 | char *str; |
| 3328 | |
| 3329 | str = _Py_wchar2char(wstr, &error_pos); |
| 3330 | if (str == NULL) { |
| 3331 | if (error_pos == (size_t)-1) { |
| 3332 | PyErr_NoMemory(); |
| 3333 | PyMem_Free(wstr); |
| 3334 | return NULL; |
| 3335 | } |
| 3336 | else { |
| 3337 | goto encode_error; |
| 3338 | } |
| 3339 | } |
| 3340 | PyMem_Free(wstr); |
| 3341 | |
| 3342 | bytes = PyBytes_FromString(str); |
| 3343 | PyMem_Free(str); |
| 3344 | } |
| 3345 | else { |
| 3346 | size_t len, len2; |
| 3347 | |
| 3348 | len = wcstombs(NULL, wstr, 0); |
| 3349 | if (len == (size_t)-1) { |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3350 | error_pos = (size_t)-1; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3351 | goto encode_error; |
| 3352 | } |
| 3353 | |
| 3354 | bytes = PyBytes_FromStringAndSize(NULL, len); |
| 3355 | if (bytes == NULL) { |
| 3356 | PyMem_Free(wstr); |
| 3357 | return NULL; |
| 3358 | } |
| 3359 | |
| 3360 | len2 = wcstombs(PyBytes_AS_STRING(bytes), wstr, len+1); |
| 3361 | if (len2 == (size_t)-1 || len2 > len) { |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3362 | error_pos = (size_t)-1; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3363 | goto encode_error; |
| 3364 | } |
| 3365 | PyMem_Free(wstr); |
| 3366 | } |
| 3367 | return bytes; |
| 3368 | |
| 3369 | encode_error: |
| 3370 | errmsg = strerror(errno); |
| 3371 | assert(errmsg != NULL); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3372 | |
| 3373 | if (error_pos == (size_t)-1) |
| 3374 | error_pos = wcstombs_errorpos(wstr); |
| 3375 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3376 | PyMem_Free(wstr); |
| 3377 | Py_XDECREF(bytes); |
| 3378 | |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3379 | if (errmsg != NULL) { |
| 3380 | size_t errlen; |
| 3381 | wstr = _Py_char2wchar(errmsg, &errlen); |
| 3382 | if (wstr != NULL) { |
| 3383 | reason = PyUnicode_FromWideChar(wstr, errlen); |
| 3384 | PyMem_Free(wstr); |
| 3385 | } else |
| 3386 | errmsg = NULL; |
| 3387 | } |
| 3388 | if (errmsg == NULL) |
Victor Stinner | 1f33f2b | 2011-12-17 04:45:09 +0100 | [diff] [blame] | 3389 | reason = PyUnicode_FromString( |
| 3390 | "wcstombs() encountered an unencodable " |
| 3391 | "wide character"); |
| 3392 | if (reason == NULL) |
| 3393 | return NULL; |
| 3394 | |
| 3395 | exc = PyObject_CallFunction(PyExc_UnicodeEncodeError, "sOnnO", |
| 3396 | "locale", unicode, |
| 3397 | (Py_ssize_t)error_pos, |
| 3398 | (Py_ssize_t)(error_pos+1), |
| 3399 | reason); |
| 3400 | Py_DECREF(reason); |
| 3401 | if (exc != NULL) { |
| 3402 | PyCodec_StrictErrors(exc); |
| 3403 | Py_XDECREF(exc); |
| 3404 | } |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3405 | return NULL; |
| 3406 | } |
| 3407 | |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3408 | PyObject * |
| 3409 | PyUnicode_EncodeFSDefault(PyObject *unicode) |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3410 | { |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3411 | #ifdef HAVE_MBCS |
Victor Stinner | ac931b1 | 2011-11-20 18:27:03 +0100 | [diff] [blame] | 3412 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, NULL); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3413 | #elif defined(__APPLE__) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3414 | return _PyUnicode_AsUTF8String(unicode, "surrogateescape"); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3415 | #else |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 3416 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 3417 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 3418 | cannot use it to encode and decode filenames before it is loaded. Load |
| 3419 | the Python codec requires to encode at least its own filename. Use the C |
| 3420 | version of the locale codec until the codec registry is initialized and |
| 3421 | the Python codec is loaded. |
| 3422 | |
| 3423 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 3424 | cannot only rely on it: check also interp->fscodec_initialized for |
| 3425 | subinterpreters. */ |
| 3426 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3427 | return PyUnicode_AsEncodedString(unicode, |
| 3428 | Py_FileSystemDefaultEncoding, |
| 3429 | "surrogateescape"); |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 3430 | } |
| 3431 | else { |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3432 | return PyUnicode_EncodeLocale(unicode, "surrogateescape"); |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 3433 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3434 | #endif |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3435 | } |
| 3436 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3437 | PyObject * |
| 3438 | PyUnicode_AsEncodedString(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3439 | const char *encoding, |
| 3440 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3441 | { |
| 3442 | PyObject *v; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3443 | char lower[11]; /* Enough for any encoding shortcut */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3444 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3445 | if (!PyUnicode_Check(unicode)) { |
| 3446 | PyErr_BadArgument(); |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3447 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3448 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3449 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3450 | /* Shortcuts for common default encodings */ |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3451 | if (normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3452 | if ((strcmp(lower, "utf-8") == 0) || |
| 3453 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 3454 | { |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3455 | if (errors == NULL || strcmp(errors, "strict") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3456 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3457 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3458 | return _PyUnicode_AsUTF8String(unicode, errors); |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 3459 | } |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3460 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3461 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3462 | (strcmp(lower, "iso-8859-1") == 0)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3463 | return _PyUnicode_AsLatin1String(unicode, errors); |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3464 | #ifdef HAVE_MBCS |
Victor Stinner | ac931b1 | 2011-11-20 18:27:03 +0100 | [diff] [blame] | 3465 | else if (strcmp(lower, "mbcs") == 0) |
| 3466 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 3467 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3468 | else if (strcmp(lower, "ascii") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3469 | return _PyUnicode_AsASCIIString(unicode, errors); |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3470 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3471 | |
| 3472 | /* Encode via the codec registry */ |
| 3473 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3474 | if (v == NULL) |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3475 | return NULL; |
| 3476 | |
| 3477 | /* The normal path */ |
| 3478 | if (PyBytes_Check(v)) |
| 3479 | return v; |
| 3480 | |
| 3481 | /* 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] | 3482 | if (PyByteArray_Check(v)) { |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 3483 | int error; |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3484 | PyObject *b; |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 3485 | |
| 3486 | error = PyErr_WarnFormat(PyExc_RuntimeWarning, 1, |
| 3487 | "encoder %s returned bytearray instead of bytes", |
| 3488 | encoding); |
| 3489 | if (error) { |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3490 | Py_DECREF(v); |
| 3491 | return NULL; |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3492 | } |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3493 | |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3494 | b = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(v), Py_SIZE(v)); |
| 3495 | Py_DECREF(v); |
| 3496 | return b; |
| 3497 | } |
| 3498 | |
| 3499 | PyErr_Format(PyExc_TypeError, |
| 3500 | "encoder did not return a bytes object (type=%.400s)", |
| 3501 | Py_TYPE(v)->tp_name); |
| 3502 | Py_DECREF(v); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3503 | return NULL; |
| 3504 | } |
| 3505 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3506 | PyObject * |
| 3507 | PyUnicode_AsEncodedUnicode(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3508 | const char *encoding, |
| 3509 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3510 | { |
| 3511 | PyObject *v; |
| 3512 | |
| 3513 | if (!PyUnicode_Check(unicode)) { |
| 3514 | PyErr_BadArgument(); |
| 3515 | goto onError; |
| 3516 | } |
| 3517 | |
| 3518 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3519 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3520 | |
| 3521 | /* Encode via the codec registry */ |
| 3522 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3523 | if (v == NULL) |
| 3524 | goto onError; |
| 3525 | if (!PyUnicode_Check(v)) { |
| 3526 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3527 | "encoder did not return an str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3528 | Py_TYPE(v)->tp_name); |
| 3529 | Py_DECREF(v); |
| 3530 | goto onError; |
| 3531 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3532 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3533 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3534 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3535 | return NULL; |
| 3536 | } |
| 3537 | |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3538 | static size_t |
| 3539 | mbstowcs_errorpos(const char *str, size_t len) |
| 3540 | { |
| 3541 | #ifdef HAVE_MBRTOWC |
| 3542 | const char *start = str; |
| 3543 | mbstate_t mbs; |
| 3544 | size_t converted; |
| 3545 | wchar_t ch; |
| 3546 | |
| 3547 | memset(&mbs, 0, sizeof mbs); |
| 3548 | while (len) |
| 3549 | { |
| 3550 | converted = mbrtowc(&ch, (char*)str, len, &mbs); |
| 3551 | if (converted == 0) |
| 3552 | /* Reached end of string */ |
| 3553 | break; |
| 3554 | if (converted == (size_t)-1 || converted == (size_t)-2) { |
| 3555 | /* Conversion error or incomplete character */ |
| 3556 | return str - start; |
| 3557 | } |
| 3558 | else { |
| 3559 | str += converted; |
| 3560 | len -= converted; |
| 3561 | } |
| 3562 | } |
| 3563 | /* failed to find the undecodable byte sequence */ |
| 3564 | return 0; |
| 3565 | #endif |
| 3566 | return 0; |
| 3567 | } |
| 3568 | |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3569 | PyObject* |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3570 | PyUnicode_DecodeLocaleAndSize(const char *str, Py_ssize_t len, |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3571 | const char *errors) |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3572 | { |
| 3573 | wchar_t smallbuf[256]; |
| 3574 | size_t smallbuf_len = Py_ARRAY_LENGTH(smallbuf); |
| 3575 | wchar_t *wstr; |
| 3576 | size_t wlen, wlen2; |
| 3577 | PyObject *unicode; |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3578 | int surrogateescape; |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3579 | size_t error_pos; |
| 3580 | char *errmsg; |
| 3581 | PyObject *reason, *exc; |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3582 | |
| 3583 | if (locale_error_handler(errors, &surrogateescape) < 0) |
| 3584 | return NULL; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3585 | |
| 3586 | if (str[len] != '\0' || len != strlen(str)) { |
| 3587 | PyErr_SetString(PyExc_TypeError, "embedded null character"); |
| 3588 | return NULL; |
| 3589 | } |
| 3590 | |
| 3591 | if (surrogateescape) |
| 3592 | { |
| 3593 | wstr = _Py_char2wchar(str, &wlen); |
| 3594 | if (wstr == NULL) { |
| 3595 | if (wlen == (size_t)-1) |
| 3596 | PyErr_NoMemory(); |
| 3597 | else |
| 3598 | PyErr_SetFromErrno(PyExc_OSError); |
| 3599 | return NULL; |
| 3600 | } |
| 3601 | |
| 3602 | unicode = PyUnicode_FromWideChar(wstr, wlen); |
| 3603 | PyMem_Free(wstr); |
| 3604 | } |
| 3605 | else { |
| 3606 | #ifndef HAVE_BROKEN_MBSTOWCS |
| 3607 | wlen = mbstowcs(NULL, str, 0); |
| 3608 | #else |
| 3609 | wlen = len; |
| 3610 | #endif |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3611 | if (wlen == (size_t)-1) |
| 3612 | goto decode_error; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3613 | if (wlen+1 <= smallbuf_len) { |
| 3614 | wstr = smallbuf; |
| 3615 | } |
| 3616 | else { |
| 3617 | if (wlen > PY_SSIZE_T_MAX / sizeof(wchar_t) - 1) |
| 3618 | return PyErr_NoMemory(); |
| 3619 | |
| 3620 | wstr = PyMem_Malloc((wlen+1) * sizeof(wchar_t)); |
| 3621 | if (!wstr) |
| 3622 | return PyErr_NoMemory(); |
| 3623 | } |
| 3624 | |
| 3625 | /* This shouldn't fail now */ |
| 3626 | wlen2 = mbstowcs(wstr, str, wlen+1); |
| 3627 | if (wlen2 == (size_t)-1) { |
| 3628 | if (wstr != smallbuf) |
| 3629 | PyMem_Free(wstr); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3630 | goto decode_error; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3631 | } |
| 3632 | #ifdef HAVE_BROKEN_MBSTOWCS |
| 3633 | assert(wlen2 == wlen); |
| 3634 | #endif |
| 3635 | unicode = PyUnicode_FromWideChar(wstr, wlen2); |
| 3636 | if (wstr != smallbuf) |
| 3637 | PyMem_Free(wstr); |
| 3638 | } |
| 3639 | return unicode; |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3640 | |
| 3641 | decode_error: |
| 3642 | errmsg = strerror(errno); |
| 3643 | assert(errmsg != NULL); |
| 3644 | |
| 3645 | error_pos = mbstowcs_errorpos(str, len); |
| 3646 | if (errmsg != NULL) { |
| 3647 | size_t errlen; |
| 3648 | wstr = _Py_char2wchar(errmsg, &errlen); |
| 3649 | if (wstr != NULL) { |
| 3650 | reason = PyUnicode_FromWideChar(wstr, errlen); |
| 3651 | PyMem_Free(wstr); |
| 3652 | } else |
| 3653 | errmsg = NULL; |
| 3654 | } |
| 3655 | if (errmsg == NULL) |
| 3656 | reason = PyUnicode_FromString( |
| 3657 | "mbstowcs() encountered an invalid multibyte sequence"); |
| 3658 | if (reason == NULL) |
| 3659 | return NULL; |
| 3660 | |
| 3661 | exc = PyObject_CallFunction(PyExc_UnicodeDecodeError, "sy#nnO", |
| 3662 | "locale", str, len, |
| 3663 | (Py_ssize_t)error_pos, |
| 3664 | (Py_ssize_t)(error_pos+1), |
| 3665 | reason); |
| 3666 | Py_DECREF(reason); |
| 3667 | if (exc != NULL) { |
| 3668 | PyCodec_StrictErrors(exc); |
| 3669 | Py_XDECREF(exc); |
| 3670 | } |
| 3671 | return NULL; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3672 | } |
| 3673 | |
| 3674 | PyObject* |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3675 | PyUnicode_DecodeLocale(const char *str, const char *errors) |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3676 | { |
| 3677 | Py_ssize_t size = (Py_ssize_t)strlen(str); |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3678 | return PyUnicode_DecodeLocaleAndSize(str, size, errors); |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3679 | } |
| 3680 | |
| 3681 | |
| 3682 | PyObject* |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3683 | PyUnicode_DecodeFSDefault(const char *s) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3684 | Py_ssize_t size = (Py_ssize_t)strlen(s); |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3685 | return PyUnicode_DecodeFSDefaultAndSize(s, size); |
| 3686 | } |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3687 | |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3688 | PyObject* |
| 3689 | PyUnicode_DecodeFSDefaultAndSize(const char *s, Py_ssize_t size) |
| 3690 | { |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3691 | #ifdef HAVE_MBCS |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3692 | return PyUnicode_DecodeMBCS(s, size, NULL); |
| 3693 | #elif defined(__APPLE__) |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 3694 | return PyUnicode_DecodeUTF8Stateful(s, size, "surrogateescape", NULL); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3695 | #else |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 3696 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 3697 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 3698 | cannot use it to encode and decode filenames before it is loaded. Load |
| 3699 | the Python codec requires to encode at least its own filename. Use the C |
| 3700 | version of the locale codec until the codec registry is initialized and |
| 3701 | the Python codec is loaded. |
| 3702 | |
| 3703 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 3704 | cannot only rely on it: check also interp->fscodec_initialized for |
| 3705 | subinterpreters. */ |
| 3706 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3707 | return PyUnicode_Decode(s, size, |
| 3708 | Py_FileSystemDefaultEncoding, |
Victor Stinner | b9a20ad | 2010-04-30 16:37:52 +0000 | [diff] [blame] | 3709 | "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3710 | } |
| 3711 | else { |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3712 | return PyUnicode_DecodeLocaleAndSize(s, size, "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3713 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3714 | #endif |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3715 | } |
| 3716 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3717 | |
| 3718 | int |
Antoine Pitrou | 1334884 | 2012-01-29 18:36:34 +0100 | [diff] [blame] | 3719 | _PyUnicode_HasNULChars(PyObject* s) |
| 3720 | { |
| 3721 | static PyObject *nul = NULL; |
| 3722 | |
| 3723 | if (nul == NULL) |
| 3724 | nul = PyUnicode_FromStringAndSize("\0", 1); |
| 3725 | if (nul == NULL) |
| 3726 | return -1; |
| 3727 | return PyUnicode_Contains(s, nul); |
| 3728 | } |
| 3729 | |
| 3730 | |
| 3731 | int |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3732 | PyUnicode_FSConverter(PyObject* arg, void* addr) |
| 3733 | { |
| 3734 | PyObject *output = NULL; |
| 3735 | Py_ssize_t size; |
| 3736 | void *data; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3737 | if (arg == NULL) { |
| 3738 | Py_DECREF(*(PyObject**)addr); |
| 3739 | return 1; |
| 3740 | } |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 3741 | if (PyBytes_Check(arg)) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3742 | output = arg; |
| 3743 | Py_INCREF(output); |
| 3744 | } |
| 3745 | else { |
| 3746 | arg = PyUnicode_FromObject(arg); |
| 3747 | if (!arg) |
| 3748 | return 0; |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3749 | output = PyUnicode_EncodeFSDefault(arg); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3750 | Py_DECREF(arg); |
| 3751 | if (!output) |
| 3752 | return 0; |
| 3753 | if (!PyBytes_Check(output)) { |
| 3754 | Py_DECREF(output); |
| 3755 | PyErr_SetString(PyExc_TypeError, "encoder failed to return bytes"); |
| 3756 | return 0; |
| 3757 | } |
| 3758 | } |
Victor Stinner | 0ea2a46 | 2010-04-30 00:22:08 +0000 | [diff] [blame] | 3759 | size = PyBytes_GET_SIZE(output); |
| 3760 | data = PyBytes_AS_STRING(output); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3761 | if (size != strlen(data)) { |
Benjamin Peterson | 7a6b44a | 2011-08-18 13:51:47 -0500 | [diff] [blame] | 3762 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3763 | Py_DECREF(output); |
| 3764 | return 0; |
| 3765 | } |
| 3766 | *(PyObject**)addr = output; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3767 | return Py_CLEANUP_SUPPORTED; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3768 | } |
| 3769 | |
| 3770 | |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3771 | int |
| 3772 | PyUnicode_FSDecoder(PyObject* arg, void* addr) |
| 3773 | { |
| 3774 | PyObject *output = NULL; |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3775 | if (arg == NULL) { |
| 3776 | Py_DECREF(*(PyObject**)addr); |
| 3777 | return 1; |
| 3778 | } |
| 3779 | if (PyUnicode_Check(arg)) { |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 3780 | if (PyUnicode_READY(arg) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3781 | return 0; |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3782 | output = arg; |
| 3783 | Py_INCREF(output); |
| 3784 | } |
| 3785 | else { |
| 3786 | arg = PyBytes_FromObject(arg); |
| 3787 | if (!arg) |
| 3788 | return 0; |
| 3789 | output = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AS_STRING(arg), |
| 3790 | PyBytes_GET_SIZE(arg)); |
| 3791 | Py_DECREF(arg); |
| 3792 | if (!output) |
| 3793 | return 0; |
| 3794 | if (!PyUnicode_Check(output)) { |
| 3795 | Py_DECREF(output); |
| 3796 | PyErr_SetString(PyExc_TypeError, "decoder failed to return unicode"); |
| 3797 | return 0; |
| 3798 | } |
| 3799 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 3800 | if (PyUnicode_READY(output) == -1) { |
Victor Stinner | 065836e | 2011-10-27 01:56:33 +0200 | [diff] [blame] | 3801 | Py_DECREF(output); |
| 3802 | return 0; |
| 3803 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3804 | if (findchar(PyUnicode_DATA(output), PyUnicode_KIND(output), |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 3805 | PyUnicode_GET_LENGTH(output), 0, 1) >= 0) { |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3806 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
| 3807 | Py_DECREF(output); |
| 3808 | return 0; |
| 3809 | } |
| 3810 | *(PyObject**)addr = output; |
| 3811 | return Py_CLEANUP_SUPPORTED; |
| 3812 | } |
| 3813 | |
| 3814 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3815 | char* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3816 | PyUnicode_AsUTF8AndSize(PyObject *unicode, Py_ssize_t *psize) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3817 | { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 3818 | PyObject *bytes; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3819 | |
Neal Norwitz | e0a0a6e | 2007-08-25 01:04:21 +0000 | [diff] [blame] | 3820 | if (!PyUnicode_Check(unicode)) { |
| 3821 | PyErr_BadArgument(); |
| 3822 | return NULL; |
| 3823 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3824 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3825 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3826 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3827 | if (PyUnicode_UTF8(unicode) == NULL) { |
| 3828 | assert(!PyUnicode_IS_COMPACT_ASCII(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3829 | bytes = _PyUnicode_AsUTF8String(unicode, "strict"); |
| 3830 | if (bytes == NULL) |
| 3831 | return NULL; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3832 | _PyUnicode_UTF8(unicode) = PyObject_MALLOC(PyBytes_GET_SIZE(bytes) + 1); |
| 3833 | if (_PyUnicode_UTF8(unicode) == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3834 | Py_DECREF(bytes); |
| 3835 | return NULL; |
| 3836 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3837 | _PyUnicode_UTF8_LENGTH(unicode) = PyBytes_GET_SIZE(bytes); |
| 3838 | Py_MEMCPY(_PyUnicode_UTF8(unicode), |
| 3839 | PyBytes_AS_STRING(bytes), |
| 3840 | _PyUnicode_UTF8_LENGTH(unicode) + 1); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3841 | Py_DECREF(bytes); |
| 3842 | } |
| 3843 | |
| 3844 | if (psize) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3845 | *psize = PyUnicode_UTF8_LENGTH(unicode); |
| 3846 | return PyUnicode_UTF8(unicode); |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 3847 | } |
| 3848 | |
| 3849 | char* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3850 | PyUnicode_AsUTF8(PyObject *unicode) |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 3851 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3852 | return PyUnicode_AsUTF8AndSize(unicode, NULL); |
| 3853 | } |
| 3854 | |
| 3855 | #ifdef Py_DEBUG |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 3856 | static int unicode_as_unicode_calls = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3857 | #endif |
| 3858 | |
| 3859 | |
| 3860 | Py_UNICODE * |
| 3861 | PyUnicode_AsUnicodeAndSize(PyObject *unicode, Py_ssize_t *size) |
| 3862 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3863 | const unsigned char *one_byte; |
| 3864 | #if SIZEOF_WCHAR_T == 4 |
| 3865 | const Py_UCS2 *two_bytes; |
| 3866 | #else |
| 3867 | const Py_UCS4 *four_bytes; |
| 3868 | const Py_UCS4 *ucs4_end; |
| 3869 | Py_ssize_t num_surrogates; |
| 3870 | #endif |
| 3871 | wchar_t *w; |
| 3872 | wchar_t *wchar_end; |
| 3873 | |
| 3874 | if (!PyUnicode_Check(unicode)) { |
| 3875 | PyErr_BadArgument(); |
| 3876 | return NULL; |
| 3877 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3878 | if (_PyUnicode_WSTR(unicode) == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3879 | /* Non-ASCII compact unicode object */ |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3880 | assert(_PyUnicode_KIND(unicode) != 0); |
| 3881 | assert(PyUnicode_IS_READY(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3882 | |
| 3883 | #ifdef Py_DEBUG |
| 3884 | ++unicode_as_unicode_calls; |
| 3885 | #endif |
| 3886 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3887 | if (PyUnicode_KIND(unicode) == PyUnicode_4BYTE_KIND) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3888 | #if SIZEOF_WCHAR_T == 2 |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3889 | four_bytes = PyUnicode_4BYTE_DATA(unicode); |
| 3890 | ucs4_end = four_bytes + _PyUnicode_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3891 | num_surrogates = 0; |
| 3892 | |
| 3893 | for (; four_bytes < ucs4_end; ++four_bytes) { |
| 3894 | if (*four_bytes > 0xFFFF) |
| 3895 | ++num_surrogates; |
| 3896 | } |
| 3897 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3898 | _PyUnicode_WSTR(unicode) = (wchar_t *) PyObject_MALLOC( |
| 3899 | sizeof(wchar_t) * (_PyUnicode_LENGTH(unicode) + 1 + num_surrogates)); |
| 3900 | if (!_PyUnicode_WSTR(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3901 | PyErr_NoMemory(); |
| 3902 | return NULL; |
| 3903 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3904 | _PyUnicode_WSTR_LENGTH(unicode) = _PyUnicode_LENGTH(unicode) + num_surrogates; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3905 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3906 | w = _PyUnicode_WSTR(unicode); |
| 3907 | wchar_end = w + _PyUnicode_WSTR_LENGTH(unicode); |
| 3908 | four_bytes = PyUnicode_4BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3909 | for (; four_bytes < ucs4_end; ++four_bytes, ++w) { |
| 3910 | if (*four_bytes > 0xFFFF) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 3911 | assert(*four_bytes <= MAX_UNICODE); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3912 | /* encode surrogate pair in this case */ |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 3913 | *w++ = Py_UNICODE_HIGH_SURROGATE(*four_bytes); |
| 3914 | *w = Py_UNICODE_LOW_SURROGATE(*four_bytes); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3915 | } |
| 3916 | else |
| 3917 | *w = *four_bytes; |
| 3918 | |
| 3919 | if (w > wchar_end) { |
| 3920 | assert(0 && "Miscalculated string end"); |
| 3921 | } |
| 3922 | } |
| 3923 | *w = 0; |
| 3924 | #else |
| 3925 | /* sizeof(wchar_t) == 4 */ |
| 3926 | Py_FatalError("Impossible unicode object state, wstr and str " |
| 3927 | "should share memory already."); |
| 3928 | return NULL; |
| 3929 | #endif |
| 3930 | } |
| 3931 | else { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3932 | _PyUnicode_WSTR(unicode) = (wchar_t *) PyObject_MALLOC(sizeof(wchar_t) * |
| 3933 | (_PyUnicode_LENGTH(unicode) + 1)); |
| 3934 | if (!_PyUnicode_WSTR(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3935 | PyErr_NoMemory(); |
| 3936 | return NULL; |
| 3937 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3938 | if (!PyUnicode_IS_COMPACT_ASCII(unicode)) |
| 3939 | _PyUnicode_WSTR_LENGTH(unicode) = _PyUnicode_LENGTH(unicode); |
| 3940 | w = _PyUnicode_WSTR(unicode); |
| 3941 | wchar_end = w + _PyUnicode_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3942 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3943 | if (PyUnicode_KIND(unicode) == PyUnicode_1BYTE_KIND) { |
| 3944 | one_byte = PyUnicode_1BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3945 | for (; w < wchar_end; ++one_byte, ++w) |
| 3946 | *w = *one_byte; |
| 3947 | /* null-terminate the wstr */ |
| 3948 | *w = 0; |
| 3949 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3950 | else if (PyUnicode_KIND(unicode) == PyUnicode_2BYTE_KIND) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3951 | #if SIZEOF_WCHAR_T == 4 |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3952 | two_bytes = PyUnicode_2BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3953 | for (; w < wchar_end; ++two_bytes, ++w) |
| 3954 | *w = *two_bytes; |
| 3955 | /* null-terminate the wstr */ |
| 3956 | *w = 0; |
| 3957 | #else |
| 3958 | /* sizeof(wchar_t) == 2 */ |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3959 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 3960 | _PyUnicode_WSTR(unicode) = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3961 | Py_FatalError("Impossible unicode object state, wstr " |
| 3962 | "and str should share memory already."); |
| 3963 | return NULL; |
| 3964 | #endif |
| 3965 | } |
| 3966 | else { |
| 3967 | assert(0 && "This should never happen."); |
| 3968 | } |
| 3969 | } |
| 3970 | } |
| 3971 | if (size != NULL) |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3972 | *size = PyUnicode_WSTR_LENGTH(unicode); |
| 3973 | return _PyUnicode_WSTR(unicode); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3974 | } |
| 3975 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3976 | Py_UNICODE * |
| 3977 | PyUnicode_AsUnicode(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3978 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3979 | return PyUnicode_AsUnicodeAndSize(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3980 | } |
| 3981 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3982 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3983 | Py_ssize_t |
| 3984 | PyUnicode_GetSize(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3985 | { |
| 3986 | if (!PyUnicode_Check(unicode)) { |
| 3987 | PyErr_BadArgument(); |
| 3988 | goto onError; |
| 3989 | } |
| 3990 | return PyUnicode_GET_SIZE(unicode); |
| 3991 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3992 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3993 | return -1; |
| 3994 | } |
| 3995 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3996 | Py_ssize_t |
| 3997 | PyUnicode_GetLength(PyObject *unicode) |
| 3998 | { |
Victor Stinner | 5a706cf | 2011-10-02 00:36:53 +0200 | [diff] [blame] | 3999 | if (!PyUnicode_Check(unicode) || PyUnicode_READY(unicode) == -1) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4000 | PyErr_BadArgument(); |
| 4001 | return -1; |
| 4002 | } |
| 4003 | |
| 4004 | return PyUnicode_GET_LENGTH(unicode); |
| 4005 | } |
| 4006 | |
| 4007 | Py_UCS4 |
| 4008 | PyUnicode_ReadChar(PyObject *unicode, Py_ssize_t index) |
| 4009 | { |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 4010 | if (!PyUnicode_Check(unicode) || PyUnicode_READY(unicode) == -1) { |
| 4011 | PyErr_BadArgument(); |
| 4012 | return (Py_UCS4)-1; |
| 4013 | } |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 4014 | if (index < 0 || index >= PyUnicode_GET_LENGTH(unicode)) { |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 4015 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4016 | return (Py_UCS4)-1; |
| 4017 | } |
| 4018 | return PyUnicode_READ_CHAR(unicode, index); |
| 4019 | } |
| 4020 | |
| 4021 | int |
| 4022 | PyUnicode_WriteChar(PyObject *unicode, Py_ssize_t index, Py_UCS4 ch) |
| 4023 | { |
| 4024 | if (!PyUnicode_Check(unicode) || !PyUnicode_IS_COMPACT(unicode)) { |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 4025 | PyErr_BadArgument(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4026 | return -1; |
| 4027 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 4028 | assert(PyUnicode_IS_READY(unicode)); |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 4029 | if (index < 0 || index >= PyUnicode_GET_LENGTH(unicode)) { |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 4030 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 4031 | return -1; |
| 4032 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 4033 | if (unicode_check_modifiable(unicode)) |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 4034 | return -1; |
Victor Stinner | c9590ad | 2012-03-04 01:34:37 +0100 | [diff] [blame] | 4035 | if (ch > PyUnicode_MAX_CHAR_VALUE(unicode)) { |
| 4036 | PyErr_SetString(PyExc_ValueError, "character out of range"); |
| 4037 | return -1; |
| 4038 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4039 | PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode), |
| 4040 | index, ch); |
| 4041 | return 0; |
| 4042 | } |
| 4043 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4044 | const char * |
| 4045 | PyUnicode_GetDefaultEncoding(void) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 4046 | { |
Victor Stinner | 42cb462 | 2010-09-01 19:39:01 +0000 | [diff] [blame] | 4047 | return "utf-8"; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 4048 | } |
| 4049 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4050 | /* create or adjust a UnicodeDecodeError */ |
| 4051 | static void |
| 4052 | make_decode_exception(PyObject **exceptionObject, |
| 4053 | const char *encoding, |
| 4054 | const char *input, Py_ssize_t length, |
| 4055 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 4056 | const char *reason) |
| 4057 | { |
| 4058 | if (*exceptionObject == NULL) { |
| 4059 | *exceptionObject = PyUnicodeDecodeError_Create( |
| 4060 | encoding, input, length, startpos, endpos, reason); |
| 4061 | } |
| 4062 | else { |
| 4063 | if (PyUnicodeDecodeError_SetStart(*exceptionObject, startpos)) |
| 4064 | goto onError; |
| 4065 | if (PyUnicodeDecodeError_SetEnd(*exceptionObject, endpos)) |
| 4066 | goto onError; |
| 4067 | if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) |
| 4068 | goto onError; |
| 4069 | } |
| 4070 | return; |
| 4071 | |
| 4072 | onError: |
| 4073 | Py_DECREF(*exceptionObject); |
| 4074 | *exceptionObject = NULL; |
| 4075 | } |
| 4076 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4077 | /* error handling callback helper: |
| 4078 | build arguments, call the callback and check the arguments, |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 4079 | if no exception occurred, copy the replacement to the output |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4080 | and adjust various state variables. |
| 4081 | return 0 on success, -1 on error |
| 4082 | */ |
| 4083 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4084 | static int |
| 4085 | unicode_decode_call_errorhandler(const char *errors, PyObject **errorHandler, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4086 | const char *encoding, const char *reason, |
| 4087 | const char **input, const char **inend, Py_ssize_t *startinpos, |
| 4088 | Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4089 | PyObject **output, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4090 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 4091 | 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] | 4092 | |
| 4093 | PyObject *restuple = NULL; |
| 4094 | PyObject *repunicode = NULL; |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 4095 | Py_ssize_t outsize; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4096 | Py_ssize_t insize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4097 | Py_ssize_t requiredsize; |
| 4098 | Py_ssize_t newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4099 | PyObject *inputobj = NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4100 | int res = -1; |
| 4101 | |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 4102 | if (_PyUnicode_KIND(*output) != PyUnicode_WCHAR_KIND) |
| 4103 | outsize = PyUnicode_GET_LENGTH(*output); |
| 4104 | else |
| 4105 | outsize = _PyUnicode_WSTR_LENGTH(*output); |
| 4106 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4107 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4108 | *errorHandler = PyCodec_LookupError(errors); |
| 4109 | if (*errorHandler == NULL) |
| 4110 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4111 | } |
| 4112 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4113 | make_decode_exception(exceptionObject, |
| 4114 | encoding, |
| 4115 | *input, *inend - *input, |
| 4116 | *startinpos, *endinpos, |
| 4117 | reason); |
| 4118 | if (*exceptionObject == NULL) |
| 4119 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4120 | |
| 4121 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 4122 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4123 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4124 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 4125 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4126 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4127 | } |
| 4128 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4129 | goto onError; |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 4130 | if (PyUnicode_READY(repunicode) == -1) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4131 | goto onError; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4132 | |
| 4133 | /* Copy back the bytes variables, which might have been modified by the |
| 4134 | callback */ |
| 4135 | inputobj = PyUnicodeDecodeError_GetObject(*exceptionObject); |
| 4136 | if (!inputobj) |
| 4137 | goto onError; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4138 | if (!PyBytes_Check(inputobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4139 | PyErr_Format(PyExc_TypeError, "exception attribute object must be bytes"); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4140 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4141 | *input = PyBytes_AS_STRING(inputobj); |
| 4142 | insize = PyBytes_GET_SIZE(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4143 | *inend = *input + insize; |
Walter Dörwald | 36f938f | 2007-08-10 10:11:43 +0000 | [diff] [blame] | 4144 | /* we can DECREF safely, as the exception has another reference, |
| 4145 | so the object won't go away. */ |
| 4146 | Py_DECREF(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4147 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4148 | if (newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4149 | newpos = insize+newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4150 | if (newpos<0 || newpos>insize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4151 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", newpos); |
| 4152 | goto onError; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4153 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4154 | |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 4155 | if (_PyUnicode_KIND(*output) != PyUnicode_WCHAR_KIND) { |
| 4156 | /* need more space? (at least enough for what we |
| 4157 | have+the replacement+the rest of the string (starting |
| 4158 | at the new input position), so we won't have to check space |
| 4159 | when there are no errors in the rest of the string) */ |
| 4160 | Py_ssize_t replen = PyUnicode_GET_LENGTH(repunicode); |
| 4161 | requiredsize = *outpos + replen + insize-newpos; |
| 4162 | if (requiredsize > outsize) { |
| 4163 | if (requiredsize<2*outsize) |
| 4164 | requiredsize = 2*outsize; |
| 4165 | if (unicode_resize(output, requiredsize) < 0) |
| 4166 | goto onError; |
| 4167 | } |
| 4168 | if (unicode_widen(output, PyUnicode_MAX_CHAR_VALUE(repunicode)) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4169 | goto onError; |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 4170 | copy_characters(*output, *outpos, repunicode, 0, replen); |
| 4171 | *outpos += replen; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4172 | } |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 4173 | else { |
| 4174 | wchar_t *repwstr; |
| 4175 | Py_ssize_t repwlen; |
| 4176 | repwstr = PyUnicode_AsUnicodeAndSize(repunicode, &repwlen); |
| 4177 | if (repwstr == NULL) |
| 4178 | goto onError; |
| 4179 | /* need more space? (at least enough for what we |
| 4180 | have+the replacement+the rest of the string (starting |
| 4181 | at the new input position), so we won't have to check space |
| 4182 | when there are no errors in the rest of the string) */ |
| 4183 | requiredsize = *outpos + repwlen + insize-newpos; |
| 4184 | if (requiredsize > outsize) { |
| 4185 | if (requiredsize < 2*outsize) |
| 4186 | requiredsize = 2*outsize; |
| 4187 | if (unicode_resize(output, requiredsize) < 0) |
| 4188 | goto onError; |
| 4189 | } |
| 4190 | wcsncpy(_PyUnicode_WSTR(*output) + *outpos, repwstr, repwlen); |
| 4191 | *outpos += repwlen; |
| 4192 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4193 | *endinpos = newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4194 | *inptr = *input + newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4195 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4196 | /* we made it! */ |
| 4197 | res = 0; |
| 4198 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4199 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4200 | Py_XDECREF(restuple); |
| 4201 | return res; |
| 4202 | } |
| 4203 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4204 | /* --- UTF-7 Codec -------------------------------------------------------- */ |
| 4205 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4206 | /* See RFC2152 for details. We encode conservatively and decode liberally. */ |
| 4207 | |
| 4208 | /* Three simple macros defining base-64. */ |
| 4209 | |
| 4210 | /* Is c a base-64 character? */ |
| 4211 | |
| 4212 | #define IS_BASE64(c) \ |
| 4213 | (((c) >= 'A' && (c) <= 'Z') || \ |
| 4214 | ((c) >= 'a' && (c) <= 'z') || \ |
| 4215 | ((c) >= '0' && (c) <= '9') || \ |
| 4216 | (c) == '+' || (c) == '/') |
| 4217 | |
| 4218 | /* given that c is a base-64 character, what is its base-64 value? */ |
| 4219 | |
| 4220 | #define FROM_BASE64(c) \ |
| 4221 | (((c) >= 'A' && (c) <= 'Z') ? (c) - 'A' : \ |
| 4222 | ((c) >= 'a' && (c) <= 'z') ? (c) - 'a' + 26 : \ |
| 4223 | ((c) >= '0' && (c) <= '9') ? (c) - '0' + 52 : \ |
| 4224 | (c) == '+' ? 62 : 63) |
| 4225 | |
| 4226 | /* What is the base-64 character of the bottom 6 bits of n? */ |
| 4227 | |
| 4228 | #define TO_BASE64(n) \ |
| 4229 | ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) |
| 4230 | |
| 4231 | /* DECODE_DIRECT: this byte encountered in a UTF-7 string should be |
| 4232 | * decoded as itself. We are permissive on decoding; the only ASCII |
| 4233 | * byte not decoding to itself is the + which begins a base64 |
| 4234 | * string. */ |
| 4235 | |
| 4236 | #define DECODE_DIRECT(c) \ |
| 4237 | ((c) <= 127 && (c) != '+') |
| 4238 | |
| 4239 | /* The UTF-7 encoder treats ASCII characters differently according to |
| 4240 | * whether they are Set D, Set O, Whitespace, or special (i.e. none of |
| 4241 | * the above). See RFC2152. This array identifies these different |
| 4242 | * sets: |
| 4243 | * 0 : "Set D" |
| 4244 | * alphanumeric and '(),-./:? |
| 4245 | * 1 : "Set O" |
| 4246 | * !"#$%&*;<=>@[]^_`{|} |
| 4247 | * 2 : "whitespace" |
| 4248 | * ht nl cr sp |
| 4249 | * 3 : special (must be base64 encoded) |
| 4250 | * everything else (i.e. +\~ and non-printing codes 0-8 11-12 14-31 127) |
| 4251 | */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4252 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4253 | static |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4254 | char utf7_category[128] = { |
| 4255 | /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */ |
| 4256 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 3, 3, 2, 3, 3, |
| 4257 | /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */ |
| 4258 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 4259 | /* sp ! " # $ % & ' ( ) * + , - . / */ |
| 4260 | 2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 3, 0, 0, 0, 0, |
| 4261 | /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */ |
| 4262 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, |
| 4263 | /* @ A B C D E F G H I J K L M N O */ |
| 4264 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4265 | /* P Q R S T U V W X Y Z [ \ ] ^ _ */ |
| 4266 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 1, 1, 1, |
| 4267 | /* ` a b c d e f g h i j k l m n o */ |
| 4268 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4269 | /* p q r s t u v w x y z { | } ~ del */ |
| 4270 | 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] | 4271 | }; |
| 4272 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4273 | /* ENCODE_DIRECT: this character should be encoded as itself. The |
| 4274 | * answer depends on whether we are encoding set O as itself, and also |
| 4275 | * on whether we are encoding whitespace as itself. RFC2152 makes it |
| 4276 | * clear that the answers to these questions vary between |
| 4277 | * applications, so this code needs to be flexible. */ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 4278 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4279 | #define ENCODE_DIRECT(c, directO, directWS) \ |
| 4280 | ((c) < 128 && (c) > 0 && \ |
| 4281 | ((utf7_category[(c)] == 0) || \ |
| 4282 | (directWS && (utf7_category[(c)] == 2)) || \ |
| 4283 | (directO && (utf7_category[(c)] == 1)))) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4284 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4285 | PyObject * |
| 4286 | PyUnicode_DecodeUTF7(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4287 | Py_ssize_t size, |
| 4288 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4289 | { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4290 | return PyUnicode_DecodeUTF7Stateful(s, size, errors, NULL); |
| 4291 | } |
| 4292 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4293 | /* The decoder. The only state we preserve is our read position, |
| 4294 | * i.e. how many characters we have consumed. So if we end in the |
| 4295 | * middle of a shift sequence we have to back off the read position |
| 4296 | * and the output to the beginning of the sequence, otherwise we lose |
| 4297 | * all the shift state (seen bits, number of bits seen, high |
| 4298 | * surrogate). */ |
| 4299 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4300 | PyObject * |
| 4301 | PyUnicode_DecodeUTF7Stateful(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4302 | Py_ssize_t size, |
| 4303 | const char *errors, |
| 4304 | Py_ssize_t *consumed) |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4305 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4306 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4307 | Py_ssize_t startinpos; |
| 4308 | Py_ssize_t endinpos; |
| 4309 | Py_ssize_t outpos; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4310 | const char *e; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4311 | PyObject *unicode; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4312 | const char *errmsg = ""; |
| 4313 | int inShift = 0; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4314 | Py_ssize_t shiftOutStart; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4315 | unsigned int base64bits = 0; |
| 4316 | unsigned long base64buffer = 0; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 4317 | Py_UCS4 surrogate = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4318 | PyObject *errorHandler = NULL; |
| 4319 | PyObject *exc = NULL; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4320 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4321 | /* Start off assuming it's all ASCII. Widen later as necessary. */ |
| 4322 | unicode = PyUnicode_New(size, 127); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4323 | if (!unicode) |
| 4324 | return NULL; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4325 | if (size == 0) { |
| 4326 | if (consumed) |
| 4327 | *consumed = 0; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4328 | return unicode; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4329 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4330 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4331 | shiftOutStart = outpos = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4332 | e = s + size; |
| 4333 | |
| 4334 | while (s < e) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4335 | Py_UCS4 ch; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4336 | restart: |
Antoine Pitrou | 5ffd9e9 | 2008-07-25 18:05:24 +0000 | [diff] [blame] | 4337 | ch = (unsigned char) *s; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4338 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4339 | if (inShift) { /* in a base-64 section */ |
| 4340 | if (IS_BASE64(ch)) { /* consume a base-64 character */ |
| 4341 | base64buffer = (base64buffer << 6) | FROM_BASE64(ch); |
| 4342 | base64bits += 6; |
| 4343 | s++; |
| 4344 | if (base64bits >= 16) { |
| 4345 | /* we have enough bits for a UTF-16 value */ |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 4346 | Py_UCS4 outCh = (Py_UCS4)(base64buffer >> (base64bits-16)); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4347 | base64bits -= 16; |
| 4348 | base64buffer &= (1 << base64bits) - 1; /* clear high bits */ |
| 4349 | if (surrogate) { |
| 4350 | /* expecting a second surrogate */ |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 4351 | if (Py_UNICODE_IS_LOW_SURROGATE(outCh)) { |
| 4352 | Py_UCS4 ch2 = Py_UNICODE_JOIN_SURROGATES(surrogate, outCh); |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4353 | if (unicode_putchar(&unicode, &outpos, ch2) < 0) |
| 4354 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4355 | surrogate = 0; |
Antoine Pitrou | 5418ee0 | 2011-11-15 01:42:21 +0100 | [diff] [blame] | 4356 | continue; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4357 | } |
| 4358 | else { |
Antoine Pitrou | 78edf75 | 2011-11-15 01:44:16 +0100 | [diff] [blame] | 4359 | if (unicode_putchar(&unicode, &outpos, surrogate) < 0) |
| 4360 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4361 | surrogate = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4362 | } |
| 4363 | } |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 4364 | if (Py_UNICODE_IS_HIGH_SURROGATE(outCh)) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4365 | /* first surrogate */ |
| 4366 | surrogate = outCh; |
| 4367 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4368 | else { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4369 | if (unicode_putchar(&unicode, &outpos, outCh) < 0) |
| 4370 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4371 | } |
| 4372 | } |
| 4373 | } |
| 4374 | else { /* now leaving a base-64 section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4375 | inShift = 0; |
| 4376 | s++; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4377 | if (surrogate) { |
Antoine Pitrou | 78edf75 | 2011-11-15 01:44:16 +0100 | [diff] [blame] | 4378 | if (unicode_putchar(&unicode, &outpos, surrogate) < 0) |
| 4379 | goto onError; |
Antoine Pitrou | 5418ee0 | 2011-11-15 01:42:21 +0100 | [diff] [blame] | 4380 | surrogate = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4381 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4382 | if (base64bits > 0) { /* left-over bits */ |
| 4383 | if (base64bits >= 6) { |
| 4384 | /* We've seen at least one base-64 character */ |
| 4385 | errmsg = "partial character in shift sequence"; |
| 4386 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4387 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4388 | else { |
| 4389 | /* Some bits remain; they should be zero */ |
| 4390 | if (base64buffer != 0) { |
| 4391 | errmsg = "non-zero padding bits in shift sequence"; |
| 4392 | goto utf7Error; |
| 4393 | } |
| 4394 | } |
| 4395 | } |
| 4396 | if (ch != '-') { |
| 4397 | /* '-' is absorbed; other terminating |
| 4398 | characters are preserved */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4399 | if (unicode_putchar(&unicode, &outpos, ch) < 0) |
| 4400 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4401 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4402 | } |
| 4403 | } |
| 4404 | else if ( ch == '+' ) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4405 | startinpos = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4406 | s++; /* consume '+' */ |
| 4407 | if (s < e && *s == '-') { /* '+-' encodes '+' */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4408 | s++; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4409 | if (unicode_putchar(&unicode, &outpos, '+') < 0) |
| 4410 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4411 | } |
| 4412 | else { /* begin base64-encoded section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4413 | inShift = 1; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4414 | shiftOutStart = outpos; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4415 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4416 | } |
| 4417 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4418 | else if (DECODE_DIRECT(ch)) { /* character decodes as itself */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4419 | if (unicode_putchar(&unicode, &outpos, ch) < 0) |
| 4420 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4421 | s++; |
| 4422 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4423 | else { |
| 4424 | startinpos = s-starts; |
| 4425 | s++; |
| 4426 | errmsg = "unexpected special character"; |
| 4427 | goto utf7Error; |
| 4428 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4429 | continue; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4430 | utf7Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4431 | endinpos = s-starts; |
| 4432 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4433 | errors, &errorHandler, |
| 4434 | "utf7", errmsg, |
| 4435 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4436 | &unicode, &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4437 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4438 | } |
| 4439 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4440 | /* end of string */ |
| 4441 | |
| 4442 | if (inShift && !consumed) { /* in shift sequence, no more to follow */ |
| 4443 | /* if we're in an inconsistent state, that's an error */ |
| 4444 | if (surrogate || |
| 4445 | (base64bits >= 6) || |
| 4446 | (base64bits > 0 && base64buffer != 0)) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4447 | endinpos = size; |
| 4448 | if (unicode_decode_call_errorhandler( |
| 4449 | errors, &errorHandler, |
| 4450 | "utf7", "unterminated shift sequence", |
| 4451 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4452 | &unicode, &outpos)) |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4453 | goto onError; |
| 4454 | if (s < e) |
| 4455 | goto restart; |
| 4456 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4457 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4458 | |
| 4459 | /* return state */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4460 | if (consumed) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4461 | if (inShift) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4462 | outpos = shiftOutStart; /* back off output */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4463 | *consumed = startinpos; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4464 | } |
| 4465 | else { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4466 | *consumed = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4467 | } |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4468 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4469 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4470 | if (unicode_resize(&unicode, outpos) < 0) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4471 | goto onError; |
| 4472 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4473 | Py_XDECREF(errorHandler); |
| 4474 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 4475 | return unicode_result(unicode); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4476 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4477 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4478 | Py_XDECREF(errorHandler); |
| 4479 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4480 | Py_DECREF(unicode); |
| 4481 | return NULL; |
| 4482 | } |
| 4483 | |
| 4484 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4485 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4486 | _PyUnicode_EncodeUTF7(PyObject *str, |
| 4487 | int base64SetO, |
| 4488 | int base64WhiteSpace, |
| 4489 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4490 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4491 | int kind; |
| 4492 | void *data; |
| 4493 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4494 | PyObject *v; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4495 | Py_ssize_t allocated; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4496 | int inShift = 0; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4497 | Py_ssize_t i; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4498 | unsigned int base64bits = 0; |
| 4499 | unsigned long base64buffer = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4500 | char * out; |
| 4501 | char * start; |
| 4502 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 4503 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4504 | return NULL; |
| 4505 | kind = PyUnicode_KIND(str); |
| 4506 | data = PyUnicode_DATA(str); |
| 4507 | len = PyUnicode_GET_LENGTH(str); |
| 4508 | |
| 4509 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4510 | return PyBytes_FromStringAndSize(NULL, 0); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4511 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4512 | /* It might be possible to tighten this worst case */ |
| 4513 | allocated = 8 * len; |
| 4514 | if (allocated / 8 != len) |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4515 | return PyErr_NoMemory(); |
| 4516 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4517 | v = PyBytes_FromStringAndSize(NULL, allocated); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4518 | if (v == NULL) |
| 4519 | return NULL; |
| 4520 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4521 | start = out = PyBytes_AS_STRING(v); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4522 | for (i = 0; i < len; ++i) { |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 4523 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4524 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4525 | if (inShift) { |
| 4526 | if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 4527 | /* shifting out */ |
| 4528 | if (base64bits) { /* output remaining bits */ |
| 4529 | *out++ = TO_BASE64(base64buffer << (6-base64bits)); |
| 4530 | base64buffer = 0; |
| 4531 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4532 | } |
| 4533 | inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4534 | /* Characters not in the BASE64 set implicitly unshift the sequence |
| 4535 | so no '-' is required, except if the character is itself a '-' */ |
| 4536 | if (IS_BASE64(ch) || ch == '-') { |
| 4537 | *out++ = '-'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4538 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4539 | *out++ = (char) ch; |
| 4540 | } |
| 4541 | else { |
| 4542 | goto encode_char; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4543 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4544 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4545 | else { /* not in a shift sequence */ |
| 4546 | if (ch == '+') { |
| 4547 | *out++ = '+'; |
| 4548 | *out++ = '-'; |
| 4549 | } |
| 4550 | else if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 4551 | *out++ = (char) ch; |
| 4552 | } |
| 4553 | else { |
| 4554 | *out++ = '+'; |
| 4555 | inShift = 1; |
| 4556 | goto encode_char; |
| 4557 | } |
| 4558 | } |
| 4559 | continue; |
| 4560 | encode_char: |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4561 | if (ch >= 0x10000) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 4562 | assert(ch <= MAX_UNICODE); |
Victor Stinner | 0d3721d | 2011-11-22 03:27:53 +0100 | [diff] [blame] | 4563 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4564 | /* code first surrogate */ |
| 4565 | base64bits += 16; |
| 4566 | base64buffer = (base64buffer << 16) | 0xd800 | ((ch-0x10000) >> 10); |
| 4567 | while (base64bits >= 6) { |
| 4568 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 4569 | base64bits -= 6; |
| 4570 | } |
| 4571 | /* prepare second surrogate */ |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 4572 | ch = Py_UNICODE_LOW_SURROGATE(ch); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4573 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4574 | base64bits += 16; |
| 4575 | base64buffer = (base64buffer << 16) | ch; |
| 4576 | while (base64bits >= 6) { |
| 4577 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 4578 | base64bits -= 6; |
| 4579 | } |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 4580 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4581 | if (base64bits) |
| 4582 | *out++= TO_BASE64(base64buffer << (6-base64bits) ); |
| 4583 | if (inShift) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4584 | *out++ = '-'; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4585 | if (_PyBytes_Resize(&v, out - start) < 0) |
| 4586 | return NULL; |
| 4587 | return v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4588 | } |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4589 | PyObject * |
| 4590 | PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
| 4591 | Py_ssize_t size, |
| 4592 | int base64SetO, |
| 4593 | int base64WhiteSpace, |
| 4594 | const char *errors) |
| 4595 | { |
| 4596 | PyObject *result; |
| 4597 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 4598 | if (tmp == NULL) |
| 4599 | return NULL; |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 4600 | result = _PyUnicode_EncodeUTF7(tmp, base64SetO, |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4601 | base64WhiteSpace, errors); |
| 4602 | Py_DECREF(tmp); |
| 4603 | return result; |
| 4604 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4605 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4606 | #undef IS_BASE64 |
| 4607 | #undef FROM_BASE64 |
| 4608 | #undef TO_BASE64 |
| 4609 | #undef DECODE_DIRECT |
| 4610 | #undef ENCODE_DIRECT |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4611 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4612 | /* --- UTF-8 Codec -------------------------------------------------------- */ |
| 4613 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4614 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4615 | char utf8_code_length[256] = { |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4616 | /* Map UTF-8 encoded prefix byte to sequence length. Zero means |
| 4617 | illegal prefix. See RFC 3629 for details */ |
| 4618 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 00-0F */ |
| 4619 | 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] | 4620 | 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] | 4621 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 4622 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 4623 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 4624 | 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] | 4625 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 70-7F */ |
| 4626 | 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] | 4627 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4628 | 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] | 4629 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* B0-BF */ |
| 4630 | 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* C0-C1 + C2-CF */ |
| 4631 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* D0-DF */ |
| 4632 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* E0-EF */ |
| 4633 | 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] | 4634 | }; |
| 4635 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4636 | PyObject * |
| 4637 | PyUnicode_DecodeUTF8(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4638 | Py_ssize_t size, |
| 4639 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4640 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4641 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
| 4642 | } |
| 4643 | |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4644 | #include "stringlib/ucs1lib.h" |
| 4645 | #include "stringlib/codecs.h" |
| 4646 | #include "stringlib/undef.h" |
| 4647 | |
| 4648 | #include "stringlib/ucs2lib.h" |
| 4649 | #include "stringlib/codecs.h" |
| 4650 | #include "stringlib/undef.h" |
| 4651 | |
| 4652 | #include "stringlib/ucs4lib.h" |
| 4653 | #include "stringlib/codecs.h" |
| 4654 | #include "stringlib/undef.h" |
| 4655 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4656 | /* Mask to check or force alignment of a pointer to C 'long' boundaries */ |
| 4657 | #define LONG_PTR_MASK (size_t) (SIZEOF_LONG - 1) |
| 4658 | |
| 4659 | /* Mask to quickly check whether a C 'long' contains a |
| 4660 | non-ASCII, UTF8-encoded char. */ |
| 4661 | #if (SIZEOF_LONG == 8) |
| 4662 | # define ASCII_CHAR_MASK 0x8080808080808080L |
| 4663 | #elif (SIZEOF_LONG == 4) |
| 4664 | # define ASCII_CHAR_MASK 0x80808080L |
| 4665 | #else |
| 4666 | # error C 'long' size should be either 4 or 8! |
| 4667 | #endif |
| 4668 | |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4669 | /* Scans a UTF-8 string and returns the maximum character to be expected |
| 4670 | and the size of the decoded unicode string. |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4671 | |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4672 | This function doesn't check for errors, these checks are performed in |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4673 | PyUnicode_DecodeUTF8Stateful. |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4674 | */ |
| 4675 | static Py_UCS4 |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 4676 | utf8_scanner(const unsigned char *p, Py_ssize_t string_size, Py_ssize_t *unicode_size) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4677 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4678 | Py_ssize_t char_count = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4679 | const unsigned char *end = p + string_size; |
| 4680 | const unsigned char *aligned_end = (const unsigned char *) ((size_t) end & ~LONG_PTR_MASK); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4681 | |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4682 | assert(unicode_size != NULL); |
| 4683 | |
| 4684 | /* By having a cascade of independent loops which fallback onto each |
| 4685 | other, we minimize the amount of work done in the average loop |
| 4686 | iteration, and we also maximize the CPU's ability to predict |
| 4687 | branches correctly (because a given condition will have always the |
| 4688 | same boolean outcome except perhaps in the last iteration of the |
| 4689 | corresponding loop). |
| 4690 | In the general case this brings us rather close to decoding |
| 4691 | performance pre-PEP 393, despite the two-pass decoding. |
| 4692 | |
| 4693 | Note that the pure ASCII loop is not duplicated once a non-ASCII |
| 4694 | character has been encountered. It is actually a pessimization (by |
| 4695 | a significant factor) to use this loop on text with many non-ASCII |
| 4696 | characters, and it is important to avoid bad performance on valid |
| 4697 | utf-8 data (invalid utf-8 being a different can of worms). |
| 4698 | */ |
| 4699 | |
| 4700 | /* ASCII */ |
| 4701 | for (; p < end; ++p) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4702 | /* Only check value if it's not a ASCII char... */ |
| 4703 | if (*p < 0x80) { |
| 4704 | /* Fast path, see below in PyUnicode_DecodeUTF8Stateful for |
| 4705 | an explanation. */ |
| 4706 | if (!((size_t) p & LONG_PTR_MASK)) { |
| 4707 | /* Help register allocation */ |
| 4708 | register const unsigned char *_p = p; |
| 4709 | while (_p < aligned_end) { |
| 4710 | unsigned long value = *(unsigned long *) _p; |
| 4711 | if (value & ASCII_CHAR_MASK) |
| 4712 | break; |
| 4713 | _p += SIZEOF_LONG; |
| 4714 | char_count += SIZEOF_LONG; |
| 4715 | } |
| 4716 | p = _p; |
| 4717 | if (p == end) |
| 4718 | break; |
| 4719 | } |
| 4720 | } |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4721 | if (*p < 0x80) |
| 4722 | ++char_count; |
| 4723 | else |
| 4724 | goto _ucs1loop; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4725 | } |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4726 | *unicode_size = char_count; |
| 4727 | return 127; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4728 | |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4729 | _ucs1loop: |
| 4730 | for (; p < end; ++p) { |
| 4731 | if (*p < 0xc4) |
| 4732 | char_count += ((*p & 0xc0) != 0x80); |
| 4733 | else |
| 4734 | goto _ucs2loop; |
| 4735 | } |
| 4736 | *unicode_size = char_count; |
| 4737 | return 255; |
| 4738 | |
| 4739 | _ucs2loop: |
| 4740 | for (; p < end; ++p) { |
| 4741 | if (*p < 0xf0) |
| 4742 | char_count += ((*p & 0xc0) != 0x80); |
| 4743 | else |
| 4744 | goto _ucs4loop; |
| 4745 | } |
| 4746 | *unicode_size = char_count; |
| 4747 | return 65535; |
| 4748 | |
| 4749 | _ucs4loop: |
| 4750 | for (; p < end; ++p) { |
| 4751 | char_count += ((*p & 0xc0) != 0x80); |
| 4752 | } |
| 4753 | *unicode_size = char_count; |
| 4754 | return 65537; |
| 4755 | } |
| 4756 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4757 | /* Similar to PyUnicode_WRITE but may attempt to widen and resize the string |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4758 | in case of errors. Implicit parameters: unicode, kind, data, onError. |
| 4759 | Potential resizing overallocates, so the result needs to shrink at the end. |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4760 | */ |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4761 | #define WRITE_MAYBE_FAIL(index, value) \ |
| 4762 | do { \ |
| 4763 | Py_ssize_t pos = index; \ |
| 4764 | if (pos > PyUnicode_GET_LENGTH(unicode) && \ |
| 4765 | unicode_resize(&unicode, pos + pos/8) < 0) \ |
| 4766 | goto onError; \ |
| 4767 | if (unicode_putchar(&unicode, &pos, value) < 0) \ |
| 4768 | goto onError; \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4769 | } while (0) |
| 4770 | |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 4771 | static PyObject * |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4772 | decode_utf8_errors(const char *starts, |
| 4773 | Py_ssize_t size, |
| 4774 | const char *errors, |
| 4775 | Py_ssize_t *consumed, |
| 4776 | const char *s, |
| 4777 | PyObject *unicode, |
| 4778 | Py_ssize_t i) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4779 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4780 | int n; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4781 | int k; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4782 | Py_ssize_t startinpos; |
| 4783 | Py_ssize_t endinpos; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4784 | const char *e = starts + size; |
| 4785 | const char *aligned_end; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4786 | const char *errmsg = ""; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4787 | PyObject *errorHandler = NULL; |
| 4788 | PyObject *exc = NULL; |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4789 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4790 | aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4791 | |
| 4792 | while (s < e) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 4793 | Py_UCS4 ch = (unsigned char)*s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4794 | |
| 4795 | if (ch < 0x80) { |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4796 | /* Fast path for runs of ASCII characters. Given that common UTF-8 |
| 4797 | input will consist of an overwhelming majority of ASCII |
| 4798 | characters, we try to optimize for this case by checking |
| 4799 | as many characters as a C 'long' can contain. |
| 4800 | First, check if we can do an aligned read, as most CPUs have |
| 4801 | a penalty for unaligned reads. |
| 4802 | */ |
| 4803 | if (!((size_t) s & LONG_PTR_MASK)) { |
| 4804 | /* Help register allocation */ |
| 4805 | register const char *_s = s; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4806 | register Py_ssize_t _i = i; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4807 | while (_s < aligned_end) { |
| 4808 | /* Read a whole long at a time (either 4 or 8 bytes), |
| 4809 | and do a fast unrolled copy if it only contains ASCII |
| 4810 | characters. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4811 | unsigned long value = *(unsigned long *) _s; |
| 4812 | if (value & ASCII_CHAR_MASK) |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4813 | break; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4814 | WRITE_MAYBE_FAIL(_i+0, _s[0]); |
| 4815 | WRITE_MAYBE_FAIL(_i+1, _s[1]); |
| 4816 | WRITE_MAYBE_FAIL(_i+2, _s[2]); |
| 4817 | WRITE_MAYBE_FAIL(_i+3, _s[3]); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4818 | #if (SIZEOF_LONG == 8) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4819 | WRITE_MAYBE_FAIL(_i+4, _s[4]); |
| 4820 | WRITE_MAYBE_FAIL(_i+5, _s[5]); |
| 4821 | WRITE_MAYBE_FAIL(_i+6, _s[6]); |
| 4822 | WRITE_MAYBE_FAIL(_i+7, _s[7]); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4823 | #endif |
| 4824 | _s += SIZEOF_LONG; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4825 | _i += SIZEOF_LONG; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4826 | } |
| 4827 | s = _s; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4828 | i = _i; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4829 | if (s == e) |
| 4830 | break; |
| 4831 | ch = (unsigned char)*s; |
| 4832 | } |
| 4833 | } |
| 4834 | |
| 4835 | if (ch < 0x80) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4836 | WRITE_MAYBE_FAIL(i++, ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4837 | s++; |
| 4838 | continue; |
| 4839 | } |
| 4840 | |
| 4841 | n = utf8_code_length[ch]; |
| 4842 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4843 | if (s + n > e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4844 | if (consumed) |
| 4845 | break; |
| 4846 | else { |
| 4847 | errmsg = "unexpected end of data"; |
| 4848 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4849 | endinpos = startinpos+1; |
| 4850 | for (k=1; (k < size-startinpos) && ((s[k]&0xC0) == 0x80); k++) |
| 4851 | endinpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4852 | goto utf8Error; |
| 4853 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4854 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4855 | |
| 4856 | switch (n) { |
| 4857 | |
| 4858 | case 0: |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4859 | errmsg = "invalid start byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4860 | startinpos = s-starts; |
| 4861 | endinpos = startinpos+1; |
| 4862 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4863 | |
| 4864 | case 1: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4865 | errmsg = "internal error"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4866 | startinpos = s-starts; |
| 4867 | endinpos = startinpos+1; |
| 4868 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4869 | |
| 4870 | case 2: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4871 | if ((s[1] & 0xc0) != 0x80) { |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4872 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4873 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4874 | endinpos = startinpos + 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4875 | goto utf8Error; |
| 4876 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4877 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4878 | assert ((ch > 0x007F) && (ch <= 0x07FF)); |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4879 | WRITE_MAYBE_FAIL(i++, ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4880 | break; |
| 4881 | |
| 4882 | case 3: |
Ezio Melotti | 9bf2b3a | 2010-07-03 04:52:19 +0000 | [diff] [blame] | 4883 | /* Decoding UTF-8 sequences in range \xed\xa0\x80-\xed\xbf\xbf |
| 4884 | will result in surrogates in range d800-dfff. Surrogates are |
| 4885 | not valid UTF-8 so they are rejected. |
| 4886 | See http://www.unicode.org/versions/Unicode5.2.0/ch03.pdf |
| 4887 | (table 3-7) and http://www.rfc-editor.org/rfc/rfc3629.txt */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4888 | if ((s[1] & 0xc0) != 0x80 || |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4889 | (s[2] & 0xc0) != 0x80 || |
| 4890 | ((unsigned char)s[0] == 0xE0 && |
| 4891 | (unsigned char)s[1] < 0xA0) || |
| 4892 | ((unsigned char)s[0] == 0xED && |
| 4893 | (unsigned char)s[1] > 0x9F)) { |
| 4894 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4895 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4896 | endinpos = startinpos + 1; |
| 4897 | |
| 4898 | /* if s[1] first two bits are 1 and 0, then the invalid |
| 4899 | continuation byte is s[2], so increment endinpos by 1, |
| 4900 | if not, s[1] is invalid and endinpos doesn't need to |
| 4901 | be incremented. */ |
| 4902 | if ((s[1] & 0xC0) == 0x80) |
| 4903 | endinpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4904 | goto utf8Error; |
| 4905 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4906 | ch = ((s[0] & 0x0f) << 12) + ((s[1] & 0x3f) << 6) + (s[2] & 0x3f); |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4907 | assert ((ch > 0x07FF) && (ch <= 0xFFFF)); |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4908 | WRITE_MAYBE_FAIL(i++, ch); |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 4909 | break; |
| 4910 | |
| 4911 | case 4: |
| 4912 | if ((s[1] & 0xc0) != 0x80 || |
| 4913 | (s[2] & 0xc0) != 0x80 || |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4914 | (s[3] & 0xc0) != 0x80 || |
| 4915 | ((unsigned char)s[0] == 0xF0 && |
| 4916 | (unsigned char)s[1] < 0x90) || |
| 4917 | ((unsigned char)s[0] == 0xF4 && |
| 4918 | (unsigned char)s[1] > 0x8F)) { |
| 4919 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4920 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4921 | endinpos = startinpos + 1; |
| 4922 | if ((s[1] & 0xC0) == 0x80) { |
| 4923 | endinpos++; |
| 4924 | if ((s[2] & 0xC0) == 0x80) |
| 4925 | endinpos++; |
| 4926 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4927 | goto utf8Error; |
| 4928 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 4929 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4930 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 4931 | assert ((ch > 0xFFFF) && (ch <= MAX_UNICODE)); |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4932 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4933 | WRITE_MAYBE_FAIL(i++, ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4934 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4935 | } |
| 4936 | s += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4937 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4938 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4939 | utf8Error: |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4940 | if (unicode_decode_call_errorhandler( |
| 4941 | errors, &errorHandler, |
Victor Stinner | cbe0134 | 2012-02-14 01:17:45 +0100 | [diff] [blame] | 4942 | "utf-8", errmsg, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4943 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4944 | &unicode, &i)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4945 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4946 | /* Update data because unicode_decode_call_errorhandler might have |
| 4947 | re-created or resized the unicode object. */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4948 | aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4949 | } |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4950 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4951 | *consumed = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4952 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4953 | /* Adjust length and ready string when it contained errors and |
| 4954 | is of the old resizable kind. */ |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4955 | if (unicode_resize(&unicode, i) < 0) |
| 4956 | goto onError; |
| 4957 | unicode_adjust_maxchar(&unicode); |
| 4958 | if (unicode == NULL) |
| 4959 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4960 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4961 | Py_XDECREF(errorHandler); |
| 4962 | Py_XDECREF(exc); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 4963 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4964 | return unicode; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4965 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4966 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4967 | Py_XDECREF(errorHandler); |
| 4968 | Py_XDECREF(exc); |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4969 | Py_XDECREF(unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4970 | return NULL; |
| 4971 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4972 | #undef WRITE_MAYBE_FAIL |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4973 | |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4974 | PyObject * |
| 4975 | PyUnicode_DecodeUTF8Stateful(const char *s, |
| 4976 | Py_ssize_t size, |
| 4977 | const char *errors, |
| 4978 | Py_ssize_t *consumed) |
| 4979 | { |
| 4980 | Py_UCS4 maxchar = 0; |
| 4981 | Py_ssize_t unicode_size; |
| 4982 | int has_errors = 0; |
| 4983 | PyObject *unicode; |
| 4984 | int kind; |
| 4985 | void *data; |
| 4986 | const char *starts = s; |
| 4987 | const char *e; |
| 4988 | Py_ssize_t i; |
| 4989 | |
| 4990 | if (size == 0) { |
| 4991 | if (consumed) |
| 4992 | *consumed = 0; |
Victor Stinner | 382955f | 2011-12-11 21:44:00 +0100 | [diff] [blame] | 4993 | Py_INCREF(unicode_empty); |
| 4994 | return unicode_empty; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4995 | } |
| 4996 | |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 4997 | maxchar = utf8_scanner((const unsigned char *)s, size, &unicode_size); |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4998 | |
| 4999 | /* When the string is ASCII only, just use memcpy and return. |
| 5000 | unicode_size may be != size if there is an incomplete UTF-8 |
| 5001 | sequence at the end of the ASCII block. */ |
| 5002 | if (maxchar < 128 && size == unicode_size) { |
| 5003 | if (consumed) |
| 5004 | *consumed = size; |
Victor Stinner | ab87021 | 2011-12-17 22:39:43 +0100 | [diff] [blame] | 5005 | return unicode_fromascii((const unsigned char *)s, size); |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 5006 | } |
| 5007 | |
| 5008 | unicode = PyUnicode_New(unicode_size, maxchar); |
| 5009 | if (!unicode) |
| 5010 | return NULL; |
| 5011 | kind = PyUnicode_KIND(unicode); |
| 5012 | data = PyUnicode_DATA(unicode); |
| 5013 | |
| 5014 | /* Unpack UTF-8 encoded data */ |
| 5015 | i = 0; |
| 5016 | e = starts + size; |
| 5017 | switch (kind) { |
| 5018 | case PyUnicode_1BYTE_KIND: |
| 5019 | has_errors = ucs1lib_utf8_try_decode(s, e, (Py_UCS1 *) data, &s, &i); |
| 5020 | break; |
| 5021 | case PyUnicode_2BYTE_KIND: |
| 5022 | has_errors = ucs2lib_utf8_try_decode(s, e, (Py_UCS2 *) data, &s, &i); |
| 5023 | break; |
| 5024 | case PyUnicode_4BYTE_KIND: |
| 5025 | has_errors = ucs4lib_utf8_try_decode(s, e, (Py_UCS4 *) data, &s, &i); |
| 5026 | break; |
| 5027 | } |
| 5028 | if (!has_errors) { |
| 5029 | /* Ensure the unicode size calculation was correct */ |
| 5030 | assert(i == unicode_size); |
| 5031 | assert(s == e); |
| 5032 | if (consumed) |
| 5033 | *consumed = size; |
| 5034 | return unicode; |
| 5035 | } |
| 5036 | |
| 5037 | /* In case of errors, maxchar and size computation might be incorrect; |
| 5038 | code below refits and resizes as necessary. */ |
| 5039 | return decode_utf8_errors(starts, size, errors, consumed, s, unicode, i); |
| 5040 | } |
| 5041 | |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 5042 | #ifdef __APPLE__ |
| 5043 | |
| 5044 | /* Simplified UTF-8 decoder using surrogateescape error handler, |
| 5045 | used to decode the command line arguments on Mac OS X. */ |
| 5046 | |
| 5047 | wchar_t* |
| 5048 | _Py_DecodeUTF8_surrogateescape(const char *s, Py_ssize_t size) |
| 5049 | { |
| 5050 | int n; |
| 5051 | const char *e; |
| 5052 | wchar_t *unicode, *p; |
| 5053 | |
| 5054 | /* Note: size will always be longer than the resulting Unicode |
| 5055 | character count */ |
| 5056 | if (PY_SSIZE_T_MAX / sizeof(wchar_t) < (size + 1)) { |
| 5057 | PyErr_NoMemory(); |
| 5058 | return NULL; |
| 5059 | } |
| 5060 | unicode = PyMem_Malloc((size + 1) * sizeof(wchar_t)); |
| 5061 | if (!unicode) |
| 5062 | return NULL; |
| 5063 | |
| 5064 | /* Unpack UTF-8 encoded data */ |
| 5065 | p = unicode; |
| 5066 | e = s + size; |
| 5067 | while (s < e) { |
| 5068 | Py_UCS4 ch = (unsigned char)*s; |
| 5069 | |
| 5070 | if (ch < 0x80) { |
| 5071 | *p++ = (wchar_t)ch; |
| 5072 | s++; |
| 5073 | continue; |
| 5074 | } |
| 5075 | |
| 5076 | n = utf8_code_length[ch]; |
| 5077 | if (s + n > e) { |
| 5078 | goto surrogateescape; |
| 5079 | } |
| 5080 | |
| 5081 | switch (n) { |
| 5082 | case 0: |
| 5083 | case 1: |
| 5084 | goto surrogateescape; |
| 5085 | |
| 5086 | case 2: |
| 5087 | if ((s[1] & 0xc0) != 0x80) |
| 5088 | goto surrogateescape; |
| 5089 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
| 5090 | assert ((ch > 0x007F) && (ch <= 0x07FF)); |
| 5091 | *p++ = (wchar_t)ch; |
| 5092 | break; |
| 5093 | |
| 5094 | case 3: |
| 5095 | /* Decoding UTF-8 sequences in range \xed\xa0\x80-\xed\xbf\xbf |
| 5096 | will result in surrogates in range d800-dfff. Surrogates are |
| 5097 | not valid UTF-8 so they are rejected. |
| 5098 | See http://www.unicode.org/versions/Unicode5.2.0/ch03.pdf |
| 5099 | (table 3-7) and http://www.rfc-editor.org/rfc/rfc3629.txt */ |
| 5100 | if ((s[1] & 0xc0) != 0x80 || |
| 5101 | (s[2] & 0xc0) != 0x80 || |
| 5102 | ((unsigned char)s[0] == 0xE0 && |
| 5103 | (unsigned char)s[1] < 0xA0) || |
| 5104 | ((unsigned char)s[0] == 0xED && |
| 5105 | (unsigned char)s[1] > 0x9F)) { |
| 5106 | |
| 5107 | goto surrogateescape; |
| 5108 | } |
| 5109 | ch = ((s[0] & 0x0f) << 12) + ((s[1] & 0x3f) << 6) + (s[2] & 0x3f); |
| 5110 | assert ((ch > 0x07FF) && (ch <= 0xFFFF)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5111 | *p++ = (wchar_t)ch; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 5112 | break; |
| 5113 | |
| 5114 | case 4: |
| 5115 | if ((s[1] & 0xc0) != 0x80 || |
| 5116 | (s[2] & 0xc0) != 0x80 || |
| 5117 | (s[3] & 0xc0) != 0x80 || |
| 5118 | ((unsigned char)s[0] == 0xF0 && |
| 5119 | (unsigned char)s[1] < 0x90) || |
| 5120 | ((unsigned char)s[0] == 0xF4 && |
| 5121 | (unsigned char)s[1] > 0x8F)) { |
| 5122 | goto surrogateescape; |
| 5123 | } |
| 5124 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
| 5125 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 5126 | assert ((ch > 0xFFFF) && (ch <= MAX_UNICODE)); |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 5127 | |
| 5128 | #if SIZEOF_WCHAR_T == 4 |
| 5129 | *p++ = (wchar_t)ch; |
| 5130 | #else |
| 5131 | /* compute and append the two surrogates: */ |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 5132 | *p++ = (wchar_t)Py_UNICODE_HIGH_SURROGATE(ch); |
| 5133 | *p++ = (wchar_t)Py_UNICODE_LOW_SURROGATE(ch); |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 5134 | #endif |
| 5135 | break; |
| 5136 | } |
| 5137 | s += n; |
| 5138 | continue; |
| 5139 | |
| 5140 | surrogateescape: |
| 5141 | *p++ = 0xDC00 + ch; |
| 5142 | s++; |
| 5143 | } |
| 5144 | *p = L'\0'; |
| 5145 | return unicode; |
| 5146 | } |
| 5147 | |
| 5148 | #endif /* __APPLE__ */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5149 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5150 | /* Primary internal function which creates utf8 encoded bytes objects. |
| 5151 | |
| 5152 | Allocation strategy: if the string is short, convert into a stack buffer |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 5153 | and allocate exactly as much space needed at the end. Else allocate the |
| 5154 | maximum possible needed (4 result bytes per Unicode character), and return |
| 5155 | the excess memory at the end. |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 5156 | */ |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 5157 | PyObject * |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5158 | _PyUnicode_AsUTF8String(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5159 | { |
Victor Stinner | 6099a03 | 2011-12-18 14:22:26 +0100 | [diff] [blame] | 5160 | enum PyUnicode_Kind kind; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5161 | void *data; |
| 5162 | Py_ssize_t size; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 5163 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5164 | if (!PyUnicode_Check(unicode)) { |
| 5165 | PyErr_BadArgument(); |
| 5166 | return NULL; |
| 5167 | } |
| 5168 | |
| 5169 | if (PyUnicode_READY(unicode) == -1) |
| 5170 | return NULL; |
| 5171 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 5172 | if (PyUnicode_UTF8(unicode)) |
| 5173 | return PyBytes_FromStringAndSize(PyUnicode_UTF8(unicode), |
| 5174 | PyUnicode_UTF8_LENGTH(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5175 | |
| 5176 | kind = PyUnicode_KIND(unicode); |
| 5177 | data = PyUnicode_DATA(unicode); |
| 5178 | size = PyUnicode_GET_LENGTH(unicode); |
| 5179 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 5180 | switch (kind) { |
Victor Stinner | 6099a03 | 2011-12-18 14:22:26 +0100 | [diff] [blame] | 5181 | default: |
| 5182 | assert(0); |
| 5183 | case PyUnicode_1BYTE_KIND: |
| 5184 | /* the string cannot be ASCII, or PyUnicode_UTF8() would be set */ |
| 5185 | assert(!PyUnicode_IS_ASCII(unicode)); |
| 5186 | return ucs1lib_utf8_encoder(unicode, data, size, errors); |
| 5187 | case PyUnicode_2BYTE_KIND: |
| 5188 | return ucs2lib_utf8_encoder(unicode, data, size, errors); |
| 5189 | case PyUnicode_4BYTE_KIND: |
| 5190 | return ucs4lib_utf8_encoder(unicode, data, size, errors); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 5191 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5192 | } |
| 5193 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5194 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5195 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
| 5196 | Py_ssize_t size, |
| 5197 | const char *errors) |
| 5198 | { |
| 5199 | PyObject *v, *unicode; |
| 5200 | |
| 5201 | unicode = PyUnicode_FromUnicode(s, size); |
| 5202 | if (unicode == NULL) |
| 5203 | return NULL; |
| 5204 | v = _PyUnicode_AsUTF8String(unicode, errors); |
| 5205 | Py_DECREF(unicode); |
| 5206 | return v; |
| 5207 | } |
| 5208 | |
| 5209 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5210 | PyUnicode_AsUTF8String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5211 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5212 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5213 | } |
| 5214 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5215 | /* --- UTF-32 Codec ------------------------------------------------------- */ |
| 5216 | |
| 5217 | PyObject * |
| 5218 | PyUnicode_DecodeUTF32(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5219 | Py_ssize_t size, |
| 5220 | const char *errors, |
| 5221 | int *byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5222 | { |
| 5223 | return PyUnicode_DecodeUTF32Stateful(s, size, errors, byteorder, NULL); |
| 5224 | } |
| 5225 | |
| 5226 | PyObject * |
| 5227 | PyUnicode_DecodeUTF32Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5228 | Py_ssize_t size, |
| 5229 | const char *errors, |
| 5230 | int *byteorder, |
| 5231 | Py_ssize_t *consumed) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5232 | { |
| 5233 | const char *starts = s; |
| 5234 | Py_ssize_t startinpos; |
| 5235 | Py_ssize_t endinpos; |
| 5236 | Py_ssize_t outpos; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5237 | PyObject *unicode; |
Mark Dickinson | 7db923c | 2010-06-12 09:10:14 +0000 | [diff] [blame] | 5238 | const unsigned char *q, *e; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5239 | int bo = 0; /* assume native ordering by default */ |
| 5240 | const char *errmsg = ""; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5241 | /* Offsets from q for retrieving bytes in the right order. */ |
| 5242 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5243 | int iorder[] = {0, 1, 2, 3}; |
| 5244 | #else |
| 5245 | int iorder[] = {3, 2, 1, 0}; |
| 5246 | #endif |
| 5247 | PyObject *errorHandler = NULL; |
| 5248 | PyObject *exc = NULL; |
Victor Stinner | 313a120 | 2010-06-11 23:56:51 +0000 | [diff] [blame] | 5249 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5250 | q = (unsigned char *)s; |
| 5251 | e = q + size; |
| 5252 | |
| 5253 | if (byteorder) |
| 5254 | bo = *byteorder; |
| 5255 | |
| 5256 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 5257 | byte order setting accordingly. In native mode, the leading BOM |
| 5258 | mark is skipped, in all other modes, it is copied to the output |
| 5259 | stream as-is (giving a ZWNBSP character). */ |
| 5260 | if (bo == 0) { |
| 5261 | if (size >= 4) { |
| 5262 | const Py_UCS4 bom = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5263 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5264 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5265 | if (bom == 0x0000FEFF) { |
| 5266 | q += 4; |
| 5267 | bo = -1; |
| 5268 | } |
| 5269 | else if (bom == 0xFFFE0000) { |
| 5270 | q += 4; |
| 5271 | bo = 1; |
| 5272 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5273 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5274 | if (bom == 0x0000FEFF) { |
| 5275 | q += 4; |
| 5276 | bo = 1; |
| 5277 | } |
| 5278 | else if (bom == 0xFFFE0000) { |
| 5279 | q += 4; |
| 5280 | bo = -1; |
| 5281 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5282 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5283 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5284 | } |
| 5285 | |
| 5286 | if (bo == -1) { |
| 5287 | /* force LE */ |
| 5288 | iorder[0] = 0; |
| 5289 | iorder[1] = 1; |
| 5290 | iorder[2] = 2; |
| 5291 | iorder[3] = 3; |
| 5292 | } |
| 5293 | else if (bo == 1) { |
| 5294 | /* force BE */ |
| 5295 | iorder[0] = 3; |
| 5296 | iorder[1] = 2; |
| 5297 | iorder[2] = 1; |
| 5298 | iorder[3] = 0; |
| 5299 | } |
| 5300 | |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 5301 | /* This might be one to much, because of a BOM */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5302 | unicode = PyUnicode_New((size+3)/4, 127); |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 5303 | if (!unicode) |
| 5304 | return NULL; |
| 5305 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5306 | return unicode; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5307 | outpos = 0; |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 5308 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5309 | while (q < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5310 | Py_UCS4 ch; |
| 5311 | /* remaining bytes at the end? (size should be divisible by 4) */ |
| 5312 | if (e-q<4) { |
| 5313 | if (consumed) |
| 5314 | break; |
| 5315 | errmsg = "truncated data"; |
| 5316 | startinpos = ((const char *)q)-starts; |
| 5317 | endinpos = ((const char *)e)-starts; |
| 5318 | goto utf32Error; |
| 5319 | /* The remaining input chars are ignored if the callback |
| 5320 | chooses to skip the input */ |
| 5321 | } |
| 5322 | ch = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
| 5323 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5324 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5325 | if (ch >= 0x110000) |
| 5326 | { |
| 5327 | errmsg = "codepoint not in range(0x110000)"; |
| 5328 | startinpos = ((const char *)q)-starts; |
| 5329 | endinpos = startinpos+4; |
| 5330 | goto utf32Error; |
| 5331 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5332 | if (unicode_putchar(&unicode, &outpos, ch) < 0) |
| 5333 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5334 | q += 4; |
| 5335 | continue; |
| 5336 | utf32Error: |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5337 | if (unicode_decode_call_errorhandler( |
| 5338 | errors, &errorHandler, |
| 5339 | "utf32", errmsg, |
| 5340 | &starts, (const char **)&e, &startinpos, &endinpos, &exc, (const char **)&q, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5341 | &unicode, &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5342 | goto onError; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5343 | } |
| 5344 | |
| 5345 | if (byteorder) |
| 5346 | *byteorder = bo; |
| 5347 | |
| 5348 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5349 | *consumed = (const char *)q-starts; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5350 | |
| 5351 | /* Adjust length */ |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 5352 | if (unicode_resize(&unicode, outpos) < 0) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5353 | goto onError; |
| 5354 | |
| 5355 | Py_XDECREF(errorHandler); |
| 5356 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 5357 | return unicode_result(unicode); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5358 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5359 | onError: |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5360 | Py_DECREF(unicode); |
| 5361 | Py_XDECREF(errorHandler); |
| 5362 | Py_XDECREF(exc); |
| 5363 | return NULL; |
| 5364 | } |
| 5365 | |
| 5366 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5367 | _PyUnicode_EncodeUTF32(PyObject *str, |
| 5368 | const char *errors, |
| 5369 | int byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5370 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5371 | int kind; |
| 5372 | void *data; |
| 5373 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5374 | PyObject *v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5375 | unsigned char *p; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5376 | Py_ssize_t nsize, bytesize, i; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5377 | /* Offsets from p for storing byte pairs in the right order. */ |
| 5378 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5379 | int iorder[] = {0, 1, 2, 3}; |
| 5380 | #else |
| 5381 | int iorder[] = {3, 2, 1, 0}; |
| 5382 | #endif |
| 5383 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5384 | #define STORECHAR(CH) \ |
| 5385 | do { \ |
| 5386 | p[iorder[3]] = ((CH) >> 24) & 0xff; \ |
| 5387 | p[iorder[2]] = ((CH) >> 16) & 0xff; \ |
| 5388 | p[iorder[1]] = ((CH) >> 8) & 0xff; \ |
| 5389 | p[iorder[0]] = (CH) & 0xff; \ |
| 5390 | p += 4; \ |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5391 | } while(0) |
| 5392 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5393 | if (!PyUnicode_Check(str)) { |
| 5394 | PyErr_BadArgument(); |
| 5395 | return NULL; |
| 5396 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 5397 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5398 | return NULL; |
| 5399 | kind = PyUnicode_KIND(str); |
| 5400 | data = PyUnicode_DATA(str); |
| 5401 | len = PyUnicode_GET_LENGTH(str); |
| 5402 | |
| 5403 | nsize = len + (byteorder == 0); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5404 | bytesize = nsize * 4; |
| 5405 | if (bytesize / 4 != nsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5406 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5407 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5408 | if (v == NULL) |
| 5409 | return NULL; |
| 5410 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5411 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5412 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5413 | STORECHAR(0xFEFF); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5414 | if (len == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5415 | goto done; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5416 | |
| 5417 | if (byteorder == -1) { |
| 5418 | /* force LE */ |
| 5419 | iorder[0] = 0; |
| 5420 | iorder[1] = 1; |
| 5421 | iorder[2] = 2; |
| 5422 | iorder[3] = 3; |
| 5423 | } |
| 5424 | else if (byteorder == 1) { |
| 5425 | /* force BE */ |
| 5426 | iorder[0] = 3; |
| 5427 | iorder[1] = 2; |
| 5428 | iorder[2] = 1; |
| 5429 | iorder[3] = 0; |
| 5430 | } |
| 5431 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5432 | for (i = 0; i < len; i++) |
| 5433 | STORECHAR(PyUnicode_READ(kind, data, i)); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5434 | |
| 5435 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5436 | return v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5437 | #undef STORECHAR |
| 5438 | } |
| 5439 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5440 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5441 | PyUnicode_EncodeUTF32(const Py_UNICODE *s, |
| 5442 | Py_ssize_t size, |
| 5443 | const char *errors, |
| 5444 | int byteorder) |
| 5445 | { |
| 5446 | PyObject *result; |
| 5447 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 5448 | if (tmp == NULL) |
| 5449 | return NULL; |
| 5450 | result = _PyUnicode_EncodeUTF32(tmp, errors, byteorder); |
| 5451 | Py_DECREF(tmp); |
| 5452 | return result; |
| 5453 | } |
| 5454 | |
| 5455 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5456 | PyUnicode_AsUTF32String(PyObject *unicode) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5457 | { |
Victor Stinner | b960b34 | 2011-11-20 19:12:52 +0100 | [diff] [blame] | 5458 | return _PyUnicode_EncodeUTF32(unicode, NULL, 0); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5459 | } |
| 5460 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5461 | /* --- UTF-16 Codec ------------------------------------------------------- */ |
| 5462 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5463 | PyObject * |
| 5464 | PyUnicode_DecodeUTF16(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5465 | Py_ssize_t size, |
| 5466 | const char *errors, |
| 5467 | int *byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5468 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5469 | return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL); |
| 5470 | } |
| 5471 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5472 | /* Two masks for fast checking of whether a C 'long' may contain |
| 5473 | UTF16-encoded surrogate characters. This is an efficient heuristic, |
| 5474 | assuming that non-surrogate characters with a code point >= 0x8000 are |
| 5475 | rare in most input. |
| 5476 | FAST_CHAR_MASK is used when the input is in native byte ordering, |
| 5477 | SWAPPED_FAST_CHAR_MASK when the input is in byteswapped ordering. |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5478 | */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5479 | #if (SIZEOF_LONG == 8) |
| 5480 | # define FAST_CHAR_MASK 0x8000800080008000L |
| 5481 | # define SWAPPED_FAST_CHAR_MASK 0x0080008000800080L |
Victor Stinner | afb5205 | 2012-04-05 22:54:49 +0200 | [diff] [blame] | 5482 | # define STRIPPED_MASK 0x00FF00FF00FF00FFL |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5483 | #elif (SIZEOF_LONG == 4) |
| 5484 | # define FAST_CHAR_MASK 0x80008000L |
| 5485 | # define SWAPPED_FAST_CHAR_MASK 0x00800080L |
Victor Stinner | afb5205 | 2012-04-05 22:54:49 +0200 | [diff] [blame] | 5486 | # define STRIPPED_MASK 0x00FF00FFL |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5487 | #else |
| 5488 | # error C 'long' size should be either 4 or 8! |
| 5489 | #endif |
| 5490 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5491 | PyObject * |
| 5492 | PyUnicode_DecodeUTF16Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5493 | Py_ssize_t size, |
| 5494 | const char *errors, |
| 5495 | int *byteorder, |
| 5496 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5497 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5498 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5499 | Py_ssize_t startinpos; |
| 5500 | Py_ssize_t endinpos; |
| 5501 | Py_ssize_t outpos; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5502 | PyObject *unicode; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5503 | const unsigned char *q, *e, *aligned_end; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5504 | int bo = 0; /* assume native ordering by default */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5505 | int native_ordering = 0; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 5506 | const char *errmsg = ""; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5507 | /* Offsets from q for retrieving byte pairs in the right order. */ |
| 5508 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5509 | int ihi = 1, ilo = 0; |
| 5510 | #else |
| 5511 | int ihi = 0, ilo = 1; |
| 5512 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5513 | PyObject *errorHandler = NULL; |
| 5514 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5515 | |
| 5516 | /* Note: size will always be longer than the resulting Unicode |
| 5517 | character count */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5518 | unicode = PyUnicode_New(size, 127); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5519 | if (!unicode) |
| 5520 | return NULL; |
| 5521 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5522 | return unicode; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5523 | outpos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5524 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5525 | q = (unsigned char *)s; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5526 | e = q + size - 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5527 | |
| 5528 | if (byteorder) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5529 | bo = *byteorder; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5530 | |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5531 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 5532 | byte order setting accordingly. In native mode, the leading BOM |
| 5533 | mark is skipped, in all other modes, it is copied to the output |
| 5534 | stream as-is (giving a ZWNBSP character). */ |
| 5535 | if (bo == 0) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5536 | if (size >= 2) { |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 5537 | const Py_UCS4 bom = (q[ihi] << 8) | q[ilo]; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5538 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5539 | if (bom == 0xFEFF) { |
| 5540 | q += 2; |
| 5541 | bo = -1; |
| 5542 | } |
| 5543 | else if (bom == 0xFFFE) { |
| 5544 | q += 2; |
| 5545 | bo = 1; |
| 5546 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5547 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5548 | if (bom == 0xFEFF) { |
| 5549 | q += 2; |
| 5550 | bo = 1; |
| 5551 | } |
| 5552 | else if (bom == 0xFFFE) { |
| 5553 | q += 2; |
| 5554 | bo = -1; |
| 5555 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5556 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5557 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5558 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5559 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5560 | if (bo == -1) { |
| 5561 | /* force LE */ |
| 5562 | ihi = 1; |
| 5563 | ilo = 0; |
| 5564 | } |
| 5565 | else if (bo == 1) { |
| 5566 | /* force BE */ |
| 5567 | ihi = 0; |
| 5568 | ilo = 1; |
| 5569 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5570 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5571 | native_ordering = ilo < ihi; |
| 5572 | #else |
| 5573 | native_ordering = ilo > ihi; |
| 5574 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5575 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5576 | aligned_end = (const unsigned char *) ((size_t) e & ~LONG_PTR_MASK); |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5577 | while (q < e) { |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 5578 | Py_UCS4 ch; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5579 | /* First check for possible aligned read of a C 'long'. Unaligned |
| 5580 | reads are more expensive, better to defer to another iteration. */ |
| 5581 | if (!((size_t) q & LONG_PTR_MASK)) { |
| 5582 | /* Fast path for runs of non-surrogate chars. */ |
| 5583 | register const unsigned char *_q = q; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5584 | int kind = PyUnicode_KIND(unicode); |
| 5585 | void *data = PyUnicode_DATA(unicode); |
| 5586 | while (_q < aligned_end) { |
Victor Stinner | afb5205 | 2012-04-05 22:54:49 +0200 | [diff] [blame] | 5587 | unsigned long block = * (unsigned long *) _q; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5588 | Py_UCS4 maxch; |
| 5589 | if (native_ordering) { |
| 5590 | /* Can use buffer directly */ |
Victor Stinner | afb5205 | 2012-04-05 22:54:49 +0200 | [diff] [blame] | 5591 | if (block & FAST_CHAR_MASK) |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5592 | break; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5593 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5594 | else { |
| 5595 | /* Need to byte-swap */ |
Victor Stinner | afb5205 | 2012-04-05 22:54:49 +0200 | [diff] [blame] | 5596 | if (block & SWAPPED_FAST_CHAR_MASK) |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5597 | break; |
Victor Stinner | afb5205 | 2012-04-05 22:54:49 +0200 | [diff] [blame] | 5598 | block = ((block >> 8) & STRIPPED_MASK) | |
| 5599 | ((block & STRIPPED_MASK) << 8); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5600 | } |
Victor Stinner | afb5205 | 2012-04-05 22:54:49 +0200 | [diff] [blame] | 5601 | maxch = (Py_UCS2)(block & 0xFFFF); |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5602 | #if SIZEOF_LONG == 8 |
Victor Stinner | afb5205 | 2012-04-05 22:54:49 +0200 | [diff] [blame] | 5603 | ch = (Py_UCS2)((block >> 16) & 0xFFFF); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 5604 | maxch = MAX_MAXCHAR(maxch, ch); |
Victor Stinner | afb5205 | 2012-04-05 22:54:49 +0200 | [diff] [blame] | 5605 | ch = (Py_UCS2)((block >> 32) & 0xFFFF); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 5606 | maxch = MAX_MAXCHAR(maxch, ch); |
Victor Stinner | afb5205 | 2012-04-05 22:54:49 +0200 | [diff] [blame] | 5607 | ch = (Py_UCS2)(block >> 48); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 5608 | maxch = MAX_MAXCHAR(maxch, ch); |
Victor Stinner | afb5205 | 2012-04-05 22:54:49 +0200 | [diff] [blame] | 5609 | #else |
| 5610 | ch = (Py_UCS2)(block >> 16); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 5611 | maxch = MAX_MAXCHAR(maxch, ch); |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5612 | #endif |
| 5613 | if (maxch > PyUnicode_MAX_CHAR_VALUE(unicode)) { |
| 5614 | if (unicode_widen(&unicode, maxch) < 0) |
| 5615 | goto onError; |
| 5616 | kind = PyUnicode_KIND(unicode); |
| 5617 | data = PyUnicode_DATA(unicode); |
| 5618 | } |
Victor Stinner | afb5205 | 2012-04-05 22:54:49 +0200 | [diff] [blame] | 5619 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5620 | PyUnicode_WRITE(kind, data, outpos++, (Py_UCS2)(block & 0xFFFF)); |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5621 | #if SIZEOF_LONG == 8 |
Victor Stinner | afb5205 | 2012-04-05 22:54:49 +0200 | [diff] [blame] | 5622 | PyUnicode_WRITE(kind, data, outpos++, (Py_UCS2)((block >> 16) & 0xFFFF)); |
| 5623 | PyUnicode_WRITE(kind, data, outpos++, (Py_UCS2)((block >> 32) & 0xFFFF)); |
| 5624 | PyUnicode_WRITE(kind, data, outpos++, (Py_UCS2)((block >> 48))); |
| 5625 | #else |
| 5626 | PyUnicode_WRITE(kind, data, outpos++, (Py_UCS2)(block >> 16)); |
| 5627 | #endif |
| 5628 | #else |
| 5629 | #if SIZEOF_LONG == 8 |
| 5630 | PyUnicode_WRITE(kind, data, outpos++, (Py_UCS2)((block >> 48))); |
| 5631 | PyUnicode_WRITE(kind, data, outpos++, (Py_UCS2)((block >> 32) & 0xFFFF)); |
| 5632 | PyUnicode_WRITE(kind, data, outpos++, (Py_UCS2)((block >> 16) & 0xFFFF)); |
| 5633 | #else |
| 5634 | PyUnicode_WRITE(kind, data, outpos++, (Py_UCS2)(block >> 16)); |
| 5635 | #endif |
| 5636 | PyUnicode_WRITE(kind, data, outpos++, (Py_UCS2)(block & 0xFFFF)); |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5637 | #endif |
| 5638 | _q += SIZEOF_LONG; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5639 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5640 | q = _q; |
| 5641 | if (q >= e) |
| 5642 | break; |
| 5643 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5644 | ch = (q[ihi] << 8) | q[ilo]; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5645 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5646 | q += 2; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5647 | |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 5648 | if (!Py_UNICODE_IS_SURROGATE(ch)) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5649 | if (unicode_putchar(&unicode, &outpos, ch) < 0) |
| 5650 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5651 | continue; |
| 5652 | } |
| 5653 | |
| 5654 | /* UTF-16 code pair: */ |
| 5655 | if (q > e) { |
| 5656 | errmsg = "unexpected end of data"; |
| 5657 | startinpos = (((const char *)q) - 2) - starts; |
| 5658 | endinpos = ((const char *)e) + 1 - starts; |
| 5659 | goto utf16Error; |
| 5660 | } |
Victor Stinner | 2e9cfad | 2011-11-20 18:40:27 +0100 | [diff] [blame] | 5661 | if (Py_UNICODE_IS_HIGH_SURROGATE(ch)) { |
| 5662 | Py_UCS4 ch2 = (q[ihi] << 8) | q[ilo]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5663 | q += 2; |
Victor Stinner | 2e9cfad | 2011-11-20 18:40:27 +0100 | [diff] [blame] | 5664 | if (Py_UNICODE_IS_LOW_SURROGATE(ch2)) { |
Victor Stinner | 62aa4d0 | 2011-11-09 00:03:45 +0100 | [diff] [blame] | 5665 | if (unicode_putchar(&unicode, &outpos, |
Victor Stinner | 2e9cfad | 2011-11-20 18:40:27 +0100 | [diff] [blame] | 5666 | Py_UNICODE_JOIN_SURROGATES(ch, ch2)) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5667 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5668 | continue; |
| 5669 | } |
| 5670 | else { |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5671 | errmsg = "illegal UTF-16 surrogate"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5672 | startinpos = (((const char *)q)-4)-starts; |
| 5673 | endinpos = startinpos+2; |
| 5674 | goto utf16Error; |
| 5675 | } |
| 5676 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5677 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5678 | errmsg = "illegal encoding"; |
| 5679 | startinpos = (((const char *)q)-2)-starts; |
| 5680 | endinpos = startinpos+2; |
| 5681 | /* Fall through to report the error */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5682 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5683 | utf16Error: |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5684 | if (unicode_decode_call_errorhandler( |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5685 | errors, |
| 5686 | &errorHandler, |
| 5687 | "utf16", errmsg, |
| 5688 | &starts, |
| 5689 | (const char **)&e, |
| 5690 | &startinpos, |
| 5691 | &endinpos, |
| 5692 | &exc, |
| 5693 | (const char **)&q, |
| 5694 | &unicode, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5695 | &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5696 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5697 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5698 | /* remaining byte at the end? (size should be even) */ |
| 5699 | if (e == q) { |
| 5700 | if (!consumed) { |
| 5701 | errmsg = "truncated data"; |
| 5702 | startinpos = ((const char *)q) - starts; |
| 5703 | endinpos = ((const char *)e) + 1 - starts; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5704 | if (unicode_decode_call_errorhandler( |
| 5705 | errors, |
| 5706 | &errorHandler, |
| 5707 | "utf16", errmsg, |
| 5708 | &starts, |
| 5709 | (const char **)&e, |
| 5710 | &startinpos, |
| 5711 | &endinpos, |
| 5712 | &exc, |
| 5713 | (const char **)&q, |
| 5714 | &unicode, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5715 | &outpos)) |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5716 | goto onError; |
| 5717 | /* The remaining input chars are ignored if the callback |
| 5718 | chooses to skip the input */ |
| 5719 | } |
| 5720 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5721 | |
| 5722 | if (byteorder) |
| 5723 | *byteorder = bo; |
| 5724 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5725 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5726 | *consumed = (const char *)q-starts; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5727 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5728 | /* Adjust length */ |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 5729 | if (unicode_resize(&unicode, outpos) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5730 | goto onError; |
| 5731 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5732 | Py_XDECREF(errorHandler); |
| 5733 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 5734 | return unicode_result(unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5735 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5736 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5737 | Py_DECREF(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5738 | Py_XDECREF(errorHandler); |
| 5739 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5740 | return NULL; |
| 5741 | } |
| 5742 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5743 | #undef FAST_CHAR_MASK |
| 5744 | #undef SWAPPED_FAST_CHAR_MASK |
| 5745 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5746 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5747 | _PyUnicode_EncodeUTF16(PyObject *str, |
| 5748 | const char *errors, |
| 5749 | int byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5750 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5751 | int kind; |
| 5752 | void *data; |
| 5753 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5754 | PyObject *v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5755 | unsigned char *p; |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5756 | Py_ssize_t nsize, bytesize; |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5757 | Py_ssize_t i, pairs; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5758 | /* Offsets from p for storing byte pairs in the right order. */ |
| 5759 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5760 | int ihi = 1, ilo = 0; |
| 5761 | #else |
| 5762 | int ihi = 0, ilo = 1; |
| 5763 | #endif |
| 5764 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5765 | #define STORECHAR(CH) \ |
| 5766 | do { \ |
| 5767 | p[ihi] = ((CH) >> 8) & 0xff; \ |
| 5768 | p[ilo] = (CH) & 0xff; \ |
| 5769 | p += 2; \ |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5770 | } while(0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5771 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5772 | if (!PyUnicode_Check(str)) { |
| 5773 | PyErr_BadArgument(); |
| 5774 | return NULL; |
| 5775 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 5776 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5777 | return NULL; |
| 5778 | kind = PyUnicode_KIND(str); |
| 5779 | data = PyUnicode_DATA(str); |
| 5780 | len = PyUnicode_GET_LENGTH(str); |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 5781 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5782 | pairs = 0; |
| 5783 | if (kind == PyUnicode_4BYTE_KIND) |
| 5784 | for (i = 0; i < len; i++) |
| 5785 | if (PyUnicode_READ(kind, data, i) >= 0x10000) |
| 5786 | pairs++; |
| 5787 | /* 2 * (len + pairs + (byteorder == 0)) */ |
| 5788 | if (len > PY_SSIZE_T_MAX - pairs - (byteorder == 0)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5789 | return PyErr_NoMemory(); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5790 | nsize = len + pairs + (byteorder == 0); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5791 | bytesize = nsize * 2; |
| 5792 | if (bytesize / 2 != nsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5793 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5794 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5795 | if (v == NULL) |
| 5796 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5797 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5798 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5799 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5800 | STORECHAR(0xFEFF); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5801 | if (len == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5802 | goto done; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5803 | |
| 5804 | if (byteorder == -1) { |
| 5805 | /* force LE */ |
| 5806 | ihi = 1; |
| 5807 | ilo = 0; |
| 5808 | } |
| 5809 | else if (byteorder == 1) { |
| 5810 | /* force BE */ |
| 5811 | ihi = 0; |
| 5812 | ilo = 1; |
| 5813 | } |
| 5814 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5815 | for (i = 0; i < len; i++) { |
| 5816 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 5817 | Py_UCS4 ch2 = 0; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5818 | if (ch >= 0x10000) { |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 5819 | ch2 = Py_UNICODE_LOW_SURROGATE(ch); |
| 5820 | ch = Py_UNICODE_HIGH_SURROGATE(ch); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5821 | } |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5822 | STORECHAR(ch); |
| 5823 | if (ch2) |
| 5824 | STORECHAR(ch2); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5825 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5826 | |
| 5827 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5828 | return v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5829 | #undef STORECHAR |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5830 | } |
| 5831 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5832 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5833 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
| 5834 | Py_ssize_t size, |
| 5835 | const char *errors, |
| 5836 | int byteorder) |
| 5837 | { |
| 5838 | PyObject *result; |
| 5839 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 5840 | if (tmp == NULL) |
| 5841 | return NULL; |
| 5842 | result = _PyUnicode_EncodeUTF16(tmp, errors, byteorder); |
| 5843 | Py_DECREF(tmp); |
| 5844 | return result; |
| 5845 | } |
| 5846 | |
| 5847 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5848 | PyUnicode_AsUTF16String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5849 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5850 | return _PyUnicode_EncodeUTF16(unicode, NULL, 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5851 | } |
| 5852 | |
| 5853 | /* --- Unicode Escape Codec ----------------------------------------------- */ |
| 5854 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5855 | /* Helper function for PyUnicode_DecodeUnicodeEscape, determines |
| 5856 | if all the escapes in the string make it still a valid ASCII string. |
| 5857 | Returns -1 if any escapes were found which cause the string to |
| 5858 | pop out of ASCII range. Otherwise returns the length of the |
| 5859 | required buffer to hold the string. |
| 5860 | */ |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 5861 | static Py_ssize_t |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5862 | length_of_escaped_ascii_string(const char *s, Py_ssize_t size) |
| 5863 | { |
| 5864 | const unsigned char *p = (const unsigned char *)s; |
| 5865 | const unsigned char *end = p + size; |
| 5866 | Py_ssize_t length = 0; |
| 5867 | |
| 5868 | if (size < 0) |
| 5869 | return -1; |
| 5870 | |
| 5871 | for (; p < end; ++p) { |
| 5872 | if (*p > 127) { |
| 5873 | /* Non-ASCII */ |
| 5874 | return -1; |
| 5875 | } |
| 5876 | else if (*p != '\\') { |
| 5877 | /* Normal character */ |
| 5878 | ++length; |
| 5879 | } |
| 5880 | else { |
| 5881 | /* Backslash-escape, check next char */ |
| 5882 | ++p; |
| 5883 | /* Escape sequence reaches till end of string or |
| 5884 | non-ASCII follow-up. */ |
| 5885 | if (p >= end || *p > 127) |
| 5886 | return -1; |
| 5887 | switch (*p) { |
| 5888 | case '\n': |
| 5889 | /* backslash + \n result in zero characters */ |
| 5890 | break; |
| 5891 | case '\\': case '\'': case '\"': |
| 5892 | case 'b': case 'f': case 't': |
| 5893 | case 'n': case 'r': case 'v': case 'a': |
| 5894 | ++length; |
| 5895 | break; |
| 5896 | case '0': case '1': case '2': case '3': |
| 5897 | case '4': case '5': case '6': case '7': |
| 5898 | case 'x': case 'u': case 'U': case 'N': |
| 5899 | /* these do not guarantee ASCII characters */ |
| 5900 | return -1; |
| 5901 | default: |
| 5902 | /* count the backslash + the other character */ |
| 5903 | length += 2; |
| 5904 | } |
| 5905 | } |
| 5906 | } |
| 5907 | return length; |
| 5908 | } |
| 5909 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 5910 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; |
Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 5911 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5912 | PyObject * |
| 5913 | PyUnicode_DecodeUnicodeEscape(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5914 | Py_ssize_t size, |
Victor Stinner | c17f540 | 2011-09-29 00:16:58 +0200 | [diff] [blame] | 5915 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5916 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5917 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5918 | Py_ssize_t startinpos; |
| 5919 | Py_ssize_t endinpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5920 | int j; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5921 | PyObject *v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5922 | const char *end; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5923 | char* message; |
| 5924 | Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5925 | PyObject *errorHandler = NULL; |
| 5926 | PyObject *exc = NULL; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5927 | Py_ssize_t len; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5928 | Py_ssize_t i; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5929 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5930 | len = length_of_escaped_ascii_string(s, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5931 | |
| 5932 | /* After length_of_escaped_ascii_string() there are two alternatives, |
| 5933 | either the string is pure ASCII with named escapes like \n, etc. |
| 5934 | and we determined it's exact size (common case) |
| 5935 | or it contains \x, \u, ... escape sequences. then we create a |
| 5936 | legacy wchar string and resize it at the end of this function. */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5937 | if (len >= 0) { |
| 5938 | v = PyUnicode_New(len, 127); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5939 | if (!v) |
| 5940 | goto onError; |
| 5941 | assert(PyUnicode_KIND(v) == PyUnicode_1BYTE_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5942 | } |
| 5943 | else { |
| 5944 | /* Escaped strings will always be longer than the resulting |
| 5945 | Unicode string, so we start with size here and then reduce the |
| 5946 | length after conversion to the true value. |
| 5947 | (but if the error callback returns a long replacement string |
| 5948 | we'll have to allocate more space) */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5949 | v = PyUnicode_New(size, 127); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5950 | if (!v) |
| 5951 | goto onError; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5952 | len = size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5953 | } |
| 5954 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5955 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5956 | return v; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5957 | i = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5958 | end = s + size; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5959 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5960 | while (s < end) { |
| 5961 | unsigned char c; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 5962 | Py_UCS4 x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5963 | int digits; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5964 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5965 | /* The only case in which i == ascii_length is a backslash |
| 5966 | followed by a newline. */ |
| 5967 | assert(i <= len); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5968 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5969 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 5970 | if (*s != '\\') { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5971 | if (unicode_putchar(&v, &i, (unsigned char) *s++) < 0) |
| 5972 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5973 | continue; |
| 5974 | } |
| 5975 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5976 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5977 | /* \ - Escapes */ |
| 5978 | s++; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5979 | c = *s++; |
| 5980 | if (s > end) |
| 5981 | c = '\0'; /* Invalid after \ */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5982 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5983 | /* The only case in which i == ascii_length is a backslash |
| 5984 | followed by a newline. */ |
| 5985 | assert(i < len || (i == len && c == '\n')); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5986 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5987 | switch (c) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5988 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5989 | /* \x escapes */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5990 | #define WRITECHAR(ch) \ |
| 5991 | do { \ |
| 5992 | if (unicode_putchar(&v, &i, ch) < 0) \ |
| 5993 | goto onError; \ |
| 5994 | }while(0) |
| 5995 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5996 | case '\n': break; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5997 | case '\\': WRITECHAR('\\'); break; |
| 5998 | case '\'': WRITECHAR('\''); break; |
| 5999 | case '\"': WRITECHAR('\"'); break; |
| 6000 | case 'b': WRITECHAR('\b'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6001 | /* FF */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6002 | case 'f': WRITECHAR('\014'); break; |
| 6003 | case 't': WRITECHAR('\t'); break; |
| 6004 | case 'n': WRITECHAR('\n'); break; |
| 6005 | case 'r': WRITECHAR('\r'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6006 | /* VT */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6007 | case 'v': WRITECHAR('\013'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6008 | /* BEL, not classic C */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6009 | case 'a': WRITECHAR('\007'); break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6010 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6011 | /* \OOO (octal) escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6012 | case '0': case '1': case '2': case '3': |
| 6013 | case '4': case '5': case '6': case '7': |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 6014 | x = s[-1] - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 6015 | if (s < end && '0' <= *s && *s <= '7') { |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 6016 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 6017 | if (s < end && '0' <= *s && *s <= '7') |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 6018 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6019 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6020 | WRITECHAR(x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6021 | break; |
| 6022 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6023 | /* hex escapes */ |
| 6024 | /* \xXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6025 | case 'x': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 6026 | digits = 2; |
| 6027 | message = "truncated \\xXX escape"; |
| 6028 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6029 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6030 | /* \uXXXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6031 | case 'u': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 6032 | digits = 4; |
| 6033 | message = "truncated \\uXXXX escape"; |
| 6034 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6035 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6036 | /* \UXXXXXXXX */ |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 6037 | case 'U': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 6038 | digits = 8; |
| 6039 | message = "truncated \\UXXXXXXXX escape"; |
| 6040 | hexescape: |
| 6041 | chr = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6042 | if (s+digits>end) { |
| 6043 | endinpos = size; |
| 6044 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6045 | errors, &errorHandler, |
| 6046 | "unicodeescape", "end of string in escape sequence", |
| 6047 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6048 | &v, &i)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6049 | goto onError; |
| 6050 | goto nextByte; |
| 6051 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6052 | for (j = 0; j < digits; ++j) { |
| 6053 | c = (unsigned char) s[j]; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 6054 | if (!Py_ISXDIGIT(c)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6055 | endinpos = (s+j+1)-starts; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6056 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6057 | errors, &errorHandler, |
| 6058 | "unicodeescape", message, |
| 6059 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6060 | &v, &i)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 6061 | goto onError; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6062 | len = PyUnicode_GET_LENGTH(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6063 | goto nextByte; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 6064 | } |
| 6065 | chr = (chr<<4) & ~0xF; |
| 6066 | if (c >= '0' && c <= '9') |
| 6067 | chr += c - '0'; |
| 6068 | else if (c >= 'a' && c <= 'f') |
| 6069 | chr += 10 + c - 'a'; |
| 6070 | else |
| 6071 | chr += 10 + c - 'A'; |
| 6072 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6073 | s += j; |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 6074 | if (chr == 0xffffffff && PyErr_Occurred()) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6075 | /* _decoding_error will have already written into the |
| 6076 | target buffer. */ |
| 6077 | break; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 6078 | store: |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 6079 | /* when we get here, chr is a 32-bit unicode character */ |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 6080 | if (chr <= MAX_UNICODE) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6081 | WRITECHAR(chr); |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 6082 | } else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6083 | endinpos = s-starts; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6084 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6085 | errors, &errorHandler, |
| 6086 | "unicodeescape", "illegal Unicode character", |
| 6087 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6088 | &v, &i)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 6089 | goto onError; |
| 6090 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 6091 | break; |
| 6092 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6093 | /* \N{name} */ |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 6094 | case 'N': |
| 6095 | message = "malformed \\N character escape"; |
| 6096 | if (ucnhash_CAPI == NULL) { |
| 6097 | /* load the unicode data module */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6098 | ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCapsule_Import( |
| 6099 | PyUnicodeData_CAPSULE_NAME, 1); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 6100 | if (ucnhash_CAPI == NULL) |
| 6101 | goto ucnhashError; |
| 6102 | } |
| 6103 | if (*s == '{') { |
| 6104 | const char *start = s+1; |
| 6105 | /* look for the closing brace */ |
| 6106 | while (*s != '}' && s < end) |
| 6107 | s++; |
| 6108 | if (s > start && s < end && *s == '}') { |
| 6109 | /* found a name. look it up in the unicode database */ |
| 6110 | message = "unknown Unicode character name"; |
| 6111 | s++; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6112 | if (ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), |
Ezio Melotti | 931b8aa | 2011-10-21 21:57:36 +0300 | [diff] [blame] | 6113 | &chr, 0)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 6114 | goto store; |
| 6115 | } |
| 6116 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6117 | endinpos = s-starts; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6118 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6119 | errors, &errorHandler, |
| 6120 | "unicodeescape", message, |
| 6121 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6122 | &v, &i)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 6123 | goto onError; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 6124 | break; |
| 6125 | |
| 6126 | default: |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 6127 | if (s > end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6128 | message = "\\ at end of string"; |
| 6129 | s--; |
| 6130 | endinpos = s-starts; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6131 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6132 | errors, &errorHandler, |
| 6133 | "unicodeescape", message, |
| 6134 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6135 | &v, &i)) |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 6136 | goto onError; |
| 6137 | } |
| 6138 | else { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6139 | WRITECHAR('\\'); |
| 6140 | WRITECHAR(s[-1]); |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 6141 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 6142 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6143 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6144 | nextByte: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6145 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6146 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6147 | #undef WRITECHAR |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6148 | |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 6149 | if (unicode_resize(&v, i) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6150 | goto onError; |
Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 6151 | Py_XDECREF(errorHandler); |
| 6152 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 6153 | return unicode_result(v); |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 6154 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6155 | ucnhashError: |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 6156 | PyErr_SetString( |
| 6157 | PyExc_UnicodeError, |
| 6158 | "\\N escapes not supported (can't load unicodedata module)" |
| 6159 | ); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 6160 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6161 | Py_XDECREF(errorHandler); |
| 6162 | Py_XDECREF(exc); |
Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 6163 | return NULL; |
| 6164 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6165 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6166 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6167 | Py_XDECREF(errorHandler); |
| 6168 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6169 | return NULL; |
| 6170 | } |
| 6171 | |
| 6172 | /* Return a Unicode-Escape string version of the Unicode object. |
| 6173 | |
| 6174 | If quotes is true, the string is enclosed in u"" or u'' quotes as |
| 6175 | appropriate. |
| 6176 | |
| 6177 | */ |
| 6178 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6179 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6180 | PyUnicode_AsUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6181 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6182 | Py_ssize_t i, len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6183 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6184 | char *p; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6185 | int kind; |
| 6186 | void *data; |
| 6187 | Py_ssize_t expandsize = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6188 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6189 | /* Initial allocation is based on the longest-possible unichr |
| 6190 | escape. |
| 6191 | |
| 6192 | In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source |
| 6193 | unichr, so in this case it's the longest unichr escape. In |
| 6194 | narrow (UTF-16) builds this is five chars per source unichr |
| 6195 | since there are two unichrs in the surrogate pair, so in narrow |
| 6196 | (UTF-16) builds it's not the longest unichr escape. |
| 6197 | |
| 6198 | In wide or narrow builds '\uxxxx' is 6 chars per source unichr, |
| 6199 | so in the narrow (UTF-16) build case it's the longest unichr |
| 6200 | escape. |
| 6201 | */ |
| 6202 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6203 | if (!PyUnicode_Check(unicode)) { |
| 6204 | PyErr_BadArgument(); |
| 6205 | return NULL; |
| 6206 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6207 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6208 | return NULL; |
| 6209 | len = PyUnicode_GET_LENGTH(unicode); |
| 6210 | kind = PyUnicode_KIND(unicode); |
| 6211 | data = PyUnicode_DATA(unicode); |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 6212 | switch (kind) { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6213 | case PyUnicode_1BYTE_KIND: expandsize = 4; break; |
| 6214 | case PyUnicode_2BYTE_KIND: expandsize = 6; break; |
| 6215 | case PyUnicode_4BYTE_KIND: expandsize = 10; break; |
| 6216 | } |
| 6217 | |
| 6218 | if (len == 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6219 | return PyBytes_FromStringAndSize(NULL, 0); |
| 6220 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6221 | if (len > (PY_SSIZE_T_MAX - 2 - 1) / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6222 | return PyErr_NoMemory(); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 6223 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6224 | repr = PyBytes_FromStringAndSize(NULL, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6225 | 2 |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6226 | + expandsize*len |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6227 | + 1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6228 | if (repr == NULL) |
| 6229 | return NULL; |
| 6230 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6231 | p = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6232 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6233 | for (i = 0; i < len; i++) { |
Victor Stinner | 3326cb6 | 2011-11-10 20:15:25 +0100 | [diff] [blame] | 6234 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 6235 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 6236 | /* Escape backslashes */ |
| 6237 | if (ch == '\\') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6238 | *p++ = '\\'; |
| 6239 | *p++ = (char) ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 6240 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6241 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 6242 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 6243 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 6244 | else if (ch >= 0x10000) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 6245 | assert(ch <= MAX_UNICODE); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 6246 | *p++ = '\\'; |
| 6247 | *p++ = 'U'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6248 | *p++ = Py_hexdigits[(ch >> 28) & 0x0000000F]; |
| 6249 | *p++ = Py_hexdigits[(ch >> 24) & 0x0000000F]; |
| 6250 | *p++ = Py_hexdigits[(ch >> 20) & 0x0000000F]; |
| 6251 | *p++ = Py_hexdigits[(ch >> 16) & 0x0000000F]; |
| 6252 | *p++ = Py_hexdigits[(ch >> 12) & 0x0000000F]; |
| 6253 | *p++ = Py_hexdigits[(ch >> 8) & 0x0000000F]; |
| 6254 | *p++ = Py_hexdigits[(ch >> 4) & 0x0000000F]; |
| 6255 | *p++ = Py_hexdigits[ch & 0x0000000F]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6256 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 6257 | } |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 6258 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6259 | /* Map 16-bit characters to '\uxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 6260 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6261 | *p++ = '\\'; |
| 6262 | *p++ = 'u'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6263 | *p++ = Py_hexdigits[(ch >> 12) & 0x000F]; |
| 6264 | *p++ = Py_hexdigits[(ch >> 8) & 0x000F]; |
| 6265 | *p++ = Py_hexdigits[(ch >> 4) & 0x000F]; |
| 6266 | *p++ = Py_hexdigits[ch & 0x000F]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6267 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 6268 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 6269 | /* Map special whitespace to '\t', \n', '\r' */ |
| 6270 | else if (ch == '\t') { |
| 6271 | *p++ = '\\'; |
| 6272 | *p++ = 't'; |
| 6273 | } |
| 6274 | else if (ch == '\n') { |
| 6275 | *p++ = '\\'; |
| 6276 | *p++ = 'n'; |
| 6277 | } |
| 6278 | else if (ch == '\r') { |
| 6279 | *p++ = '\\'; |
| 6280 | *p++ = 'r'; |
| 6281 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 6282 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 6283 | /* Map non-printable US ASCII to '\xhh' */ |
Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 6284 | else if (ch < ' ' || ch >= 0x7F) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6285 | *p++ = '\\'; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 6286 | *p++ = 'x'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6287 | *p++ = Py_hexdigits[(ch >> 4) & 0x000F]; |
| 6288 | *p++ = Py_hexdigits[ch & 0x000F]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6289 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 6290 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6291 | /* Copy everything else as-is */ |
| 6292 | else |
| 6293 | *p++ = (char) ch; |
| 6294 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6295 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6296 | assert(p - PyBytes_AS_STRING(repr) > 0); |
| 6297 | if (_PyBytes_Resize(&repr, p - PyBytes_AS_STRING(repr)) < 0) |
| 6298 | return NULL; |
| 6299 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6300 | } |
| 6301 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6302 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6303 | PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, |
| 6304 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6305 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6306 | PyObject *result; |
| 6307 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 6308 | if (tmp == NULL) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6309 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6310 | result = PyUnicode_AsUnicodeEscapeString(tmp); |
| 6311 | Py_DECREF(tmp); |
| 6312 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6313 | } |
| 6314 | |
| 6315 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ |
| 6316 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6317 | PyObject * |
| 6318 | PyUnicode_DecodeRawUnicodeEscape(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6319 | Py_ssize_t size, |
| 6320 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6321 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6322 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6323 | Py_ssize_t startinpos; |
| 6324 | Py_ssize_t endinpos; |
| 6325 | Py_ssize_t outpos; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6326 | PyObject *v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6327 | const char *end; |
| 6328 | const char *bs; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6329 | PyObject *errorHandler = NULL; |
| 6330 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6331 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6332 | /* Escaped strings will always be longer than the resulting |
| 6333 | 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] | 6334 | length after conversion to the true value. (But decoding error |
| 6335 | handler might have to resize the string) */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6336 | v = PyUnicode_New(size, 127); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6337 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6338 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6339 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6340 | return v; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6341 | outpos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6342 | end = s + size; |
| 6343 | while (s < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6344 | unsigned char c; |
| 6345 | Py_UCS4 x; |
| 6346 | int i; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6347 | int count; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6348 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6349 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 6350 | if (*s != '\\') { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6351 | if (unicode_putchar(&v, &outpos, (unsigned char)*s++) < 0) |
| 6352 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6353 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6354 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6355 | startinpos = s-starts; |
| 6356 | |
| 6357 | /* \u-escapes are only interpreted iff the number of leading |
| 6358 | backslashes if odd */ |
| 6359 | bs = s; |
| 6360 | for (;s < end;) { |
| 6361 | if (*s != '\\') |
| 6362 | break; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6363 | if (unicode_putchar(&v, &outpos, (unsigned char)*s++) < 0) |
| 6364 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6365 | } |
| 6366 | if (((s - bs) & 1) == 0 || |
| 6367 | s >= end || |
| 6368 | (*s != 'u' && *s != 'U')) { |
| 6369 | continue; |
| 6370 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6371 | outpos--; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6372 | count = *s=='u' ? 4 : 8; |
| 6373 | s++; |
| 6374 | |
| 6375 | /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6376 | for (x = 0, i = 0; i < count; ++i, ++s) { |
| 6377 | c = (unsigned char)*s; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 6378 | if (!Py_ISXDIGIT(c)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6379 | endinpos = s-starts; |
| 6380 | if (unicode_decode_call_errorhandler( |
| 6381 | errors, &errorHandler, |
| 6382 | "rawunicodeescape", "truncated \\uXXXX", |
| 6383 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6384 | &v, &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6385 | goto onError; |
| 6386 | goto nextByte; |
| 6387 | } |
| 6388 | x = (x<<4) & ~0xF; |
| 6389 | if (c >= '0' && c <= '9') |
| 6390 | x += c - '0'; |
| 6391 | else if (c >= 'a' && c <= 'f') |
| 6392 | x += 10 + c - 'a'; |
| 6393 | else |
| 6394 | x += 10 + c - 'A'; |
| 6395 | } |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 6396 | if (x <= MAX_UNICODE) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6397 | if (unicode_putchar(&v, &outpos, x) < 0) |
| 6398 | goto onError; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 6399 | } else { |
| 6400 | endinpos = s-starts; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6401 | if (unicode_decode_call_errorhandler( |
| 6402 | errors, &errorHandler, |
| 6403 | "rawunicodeescape", "\\Uxxxxxxxx out of range", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6404 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6405 | &v, &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6406 | goto onError; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6407 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6408 | nextByte: |
| 6409 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6410 | } |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 6411 | if (unicode_resize(&v, outpos) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6412 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6413 | Py_XDECREF(errorHandler); |
| 6414 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 6415 | return unicode_result(v); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6416 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6417 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6418 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6419 | Py_XDECREF(errorHandler); |
| 6420 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6421 | return NULL; |
| 6422 | } |
| 6423 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6424 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6425 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6426 | PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6427 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6428 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6429 | char *p; |
| 6430 | char *q; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6431 | Py_ssize_t expandsize, pos; |
| 6432 | int kind; |
| 6433 | void *data; |
| 6434 | Py_ssize_t len; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6435 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6436 | if (!PyUnicode_Check(unicode)) { |
| 6437 | PyErr_BadArgument(); |
| 6438 | return NULL; |
| 6439 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6440 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6441 | return NULL; |
| 6442 | kind = PyUnicode_KIND(unicode); |
| 6443 | data = PyUnicode_DATA(unicode); |
| 6444 | len = PyUnicode_GET_LENGTH(unicode); |
Benjamin Peterson | 1518e87 | 2011-11-23 10:44:52 -0600 | [diff] [blame] | 6445 | /* 4 byte characters can take up 10 bytes, 2 byte characters can take up 6 |
| 6446 | bytes, and 1 byte characters 4. */ |
| 6447 | expandsize = kind * 2 + 2; |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 6448 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6449 | if (len > PY_SSIZE_T_MAX / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6450 | return PyErr_NoMemory(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6451 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6452 | repr = PyBytes_FromStringAndSize(NULL, expandsize * len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6453 | if (repr == NULL) |
| 6454 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6455 | if (len == 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6456 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6457 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6458 | p = q = PyBytes_AS_STRING(repr); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6459 | for (pos = 0; pos < len; pos++) { |
| 6460 | Py_UCS4 ch = PyUnicode_READ(kind, data, pos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6461 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 6462 | if (ch >= 0x10000) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 6463 | assert(ch <= MAX_UNICODE); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6464 | *p++ = '\\'; |
| 6465 | *p++ = 'U'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6466 | *p++ = Py_hexdigits[(ch >> 28) & 0xf]; |
| 6467 | *p++ = Py_hexdigits[(ch >> 24) & 0xf]; |
| 6468 | *p++ = Py_hexdigits[(ch >> 20) & 0xf]; |
| 6469 | *p++ = Py_hexdigits[(ch >> 16) & 0xf]; |
| 6470 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 6471 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 6472 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 6473 | *p++ = Py_hexdigits[ch & 15]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6474 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6475 | /* Map 16-bit characters to '\uxxxx' */ |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6476 | else if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6477 | *p++ = '\\'; |
| 6478 | *p++ = 'u'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6479 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 6480 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 6481 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 6482 | *p++ = Py_hexdigits[ch & 15]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6483 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6484 | /* Copy everything else as-is */ |
| 6485 | else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6486 | *p++ = (char) ch; |
| 6487 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6488 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6489 | assert(p > q); |
| 6490 | if (_PyBytes_Resize(&repr, p - q) < 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6491 | return NULL; |
| 6492 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6493 | } |
| 6494 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6495 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6496 | PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
| 6497 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6498 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6499 | PyObject *result; |
| 6500 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 6501 | if (tmp == NULL) |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 6502 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6503 | result = PyUnicode_AsRawUnicodeEscapeString(tmp); |
| 6504 | Py_DECREF(tmp); |
| 6505 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6506 | } |
| 6507 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6508 | /* --- Unicode Internal Codec ------------------------------------------- */ |
| 6509 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6510 | PyObject * |
| 6511 | _PyUnicode_DecodeUnicodeInternal(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6512 | Py_ssize_t size, |
| 6513 | const char *errors) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6514 | { |
| 6515 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6516 | Py_ssize_t startinpos; |
| 6517 | Py_ssize_t endinpos; |
| 6518 | Py_ssize_t outpos; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6519 | PyObject *v; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6520 | const char *end; |
| 6521 | const char *reason; |
| 6522 | PyObject *errorHandler = NULL; |
| 6523 | PyObject *exc = NULL; |
| 6524 | |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6525 | if (PyErr_WarnEx(PyExc_DeprecationWarning, |
Ezio Melotti | 11060a4 | 2011-11-16 09:39:10 +0200 | [diff] [blame] | 6526 | "unicode_internal codec has been deprecated", |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6527 | 1)) |
| 6528 | return NULL; |
| 6529 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6530 | /* XXX overflow detection missing */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6531 | v = PyUnicode_New((size+Py_UNICODE_SIZE-1)/ Py_UNICODE_SIZE, 127); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6532 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6533 | goto onError; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6534 | if (PyUnicode_GET_LENGTH(v) == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6535 | return v; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6536 | outpos = 0; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6537 | end = s + size; |
| 6538 | |
| 6539 | while (s < end) { |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6540 | Py_UNICODE uch; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6541 | Py_UCS4 ch; |
| 6542 | /* We copy the raw representation one byte at a time because the |
| 6543 | pointer may be unaligned (see test_codeccallbacks). */ |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6544 | ((char *) &uch)[0] = s[0]; |
| 6545 | ((char *) &uch)[1] = s[1]; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6546 | #ifdef Py_UNICODE_WIDE |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6547 | ((char *) &uch)[2] = s[2]; |
| 6548 | ((char *) &uch)[3] = s[3]; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6549 | #endif |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6550 | ch = uch; |
| 6551 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6552 | /* We have to sanity check the raw data, otherwise doom looms for |
| 6553 | some malformed UCS-4 data. */ |
| 6554 | if ( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6555 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6556 | ch > 0x10ffff || |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6557 | #endif |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6558 | end-s < Py_UNICODE_SIZE |
| 6559 | ) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6560 | { |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6561 | startinpos = s - starts; |
| 6562 | if (end-s < Py_UNICODE_SIZE) { |
| 6563 | endinpos = end-starts; |
| 6564 | reason = "truncated input"; |
| 6565 | } |
| 6566 | else { |
| 6567 | endinpos = s - starts + Py_UNICODE_SIZE; |
| 6568 | reason = "illegal code point (> 0x10FFFF)"; |
| 6569 | } |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6570 | if (unicode_decode_call_errorhandler( |
| 6571 | errors, &errorHandler, |
| 6572 | "unicode_internal", reason, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 6573 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6574 | &v, &outpos)) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6575 | goto onError; |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6576 | continue; |
| 6577 | } |
| 6578 | |
| 6579 | s += Py_UNICODE_SIZE; |
| 6580 | #ifndef Py_UNICODE_WIDE |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 6581 | if (Py_UNICODE_IS_HIGH_SURROGATE(ch) && s < end) |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6582 | { |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6583 | Py_UNICODE uch2; |
| 6584 | ((char *) &uch2)[0] = s[0]; |
| 6585 | ((char *) &uch2)[1] = s[1]; |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 6586 | if (Py_UNICODE_IS_LOW_SURROGATE(uch2)) |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6587 | { |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 6588 | ch = Py_UNICODE_JOIN_SURROGATES(uch, uch2); |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6589 | s += Py_UNICODE_SIZE; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6590 | } |
| 6591 | } |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6592 | #endif |
| 6593 | |
| 6594 | if (unicode_putchar(&v, &outpos, ch) < 0) |
| 6595 | goto onError; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6596 | } |
| 6597 | |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 6598 | if (unicode_resize(&v, outpos) < 0) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6599 | goto onError; |
| 6600 | Py_XDECREF(errorHandler); |
| 6601 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 6602 | return unicode_result(v); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6603 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6604 | onError: |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6605 | Py_XDECREF(v); |
| 6606 | Py_XDECREF(errorHandler); |
| 6607 | Py_XDECREF(exc); |
| 6608 | return NULL; |
| 6609 | } |
| 6610 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6611 | /* --- Latin-1 Codec ------------------------------------------------------ */ |
| 6612 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6613 | PyObject * |
| 6614 | PyUnicode_DecodeLatin1(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6615 | Py_ssize_t size, |
| 6616 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6617 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6618 | /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 6619 | return _PyUnicode_FromUCS1((unsigned char*)s, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6620 | } |
| 6621 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6622 | /* create or adjust a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6623 | static void |
| 6624 | make_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6625 | const char *encoding, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6626 | PyObject *unicode, |
| 6627 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6628 | const char *reason) |
| 6629 | { |
| 6630 | if (*exceptionObject == NULL) { |
| 6631 | *exceptionObject = PyObject_CallFunction( |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6632 | PyExc_UnicodeEncodeError, "sOnns", |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6633 | encoding, unicode, startpos, endpos, reason); |
| 6634 | } |
| 6635 | else { |
| 6636 | if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 6637 | goto onError; |
| 6638 | if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) |
| 6639 | goto onError; |
| 6640 | if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) |
| 6641 | goto onError; |
| 6642 | return; |
| 6643 | onError: |
| 6644 | Py_DECREF(*exceptionObject); |
| 6645 | *exceptionObject = NULL; |
| 6646 | } |
| 6647 | } |
| 6648 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6649 | /* raises a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6650 | static void |
| 6651 | raise_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6652 | const char *encoding, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6653 | PyObject *unicode, |
| 6654 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6655 | const char *reason) |
| 6656 | { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6657 | make_encode_exception(exceptionObject, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6658 | encoding, unicode, startpos, endpos, reason); |
| 6659 | if (*exceptionObject != NULL) |
| 6660 | PyCodec_StrictErrors(*exceptionObject); |
| 6661 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6662 | |
| 6663 | /* error handling callback helper: |
| 6664 | build arguments, call the callback and check the arguments, |
| 6665 | put the result into newpos and return the replacement string, which |
| 6666 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6667 | static PyObject * |
| 6668 | unicode_encode_call_errorhandler(const char *errors, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6669 | PyObject **errorHandler, |
| 6670 | const char *encoding, const char *reason, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6671 | PyObject *unicode, PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6672 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6673 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6674 | { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6675 | static char *argparse = "On;encoding error handler must return (str/bytes, int) tuple"; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6676 | Py_ssize_t len; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6677 | PyObject *restuple; |
| 6678 | PyObject *resunicode; |
| 6679 | |
| 6680 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6681 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6682 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6683 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6684 | } |
| 6685 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6686 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6687 | return NULL; |
| 6688 | len = PyUnicode_GET_LENGTH(unicode); |
| 6689 | |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6690 | make_encode_exception(exceptionObject, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6691 | encoding, unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6692 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6693 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6694 | |
| 6695 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6696 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6697 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6698 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6699 | if (!PyTuple_Check(restuple)) { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6700 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6701 | Py_DECREF(restuple); |
| 6702 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6703 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6704 | if (!PyArg_ParseTuple(restuple, argparse, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6705 | &resunicode, newpos)) { |
| 6706 | Py_DECREF(restuple); |
| 6707 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6708 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6709 | if (!PyUnicode_Check(resunicode) && !PyBytes_Check(resunicode)) { |
| 6710 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
| 6711 | Py_DECREF(restuple); |
| 6712 | return NULL; |
| 6713 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6714 | if (*newpos<0) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6715 | *newpos = len + *newpos; |
| 6716 | if (*newpos<0 || *newpos>len) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6717 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 6718 | Py_DECREF(restuple); |
| 6719 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 6720 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6721 | Py_INCREF(resunicode); |
| 6722 | Py_DECREF(restuple); |
| 6723 | return resunicode; |
| 6724 | } |
| 6725 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6726 | static PyObject * |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6727 | unicode_encode_ucs1(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6728 | const char *errors, |
Victor Stinner | fcd9653 | 2011-11-04 00:28:50 +0100 | [diff] [blame] | 6729 | unsigned int limit) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6730 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6731 | /* input state */ |
| 6732 | Py_ssize_t pos=0, size; |
| 6733 | int kind; |
| 6734 | void *data; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6735 | /* output object */ |
| 6736 | PyObject *res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6737 | /* pointer into the output */ |
| 6738 | char *str; |
| 6739 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6740 | Py_ssize_t ressize; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6741 | const char *encoding = (limit == 256) ? "latin-1" : "ascii"; |
| 6742 | 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] | 6743 | PyObject *errorHandler = NULL; |
| 6744 | PyObject *exc = NULL; |
| 6745 | /* the following variable is used for caching string comparisons |
| 6746 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 6747 | int known_errorHandler = -1; |
| 6748 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6749 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6750 | return NULL; |
| 6751 | size = PyUnicode_GET_LENGTH(unicode); |
| 6752 | kind = PyUnicode_KIND(unicode); |
| 6753 | data = PyUnicode_DATA(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6754 | /* allocate enough for a simple encoding without |
| 6755 | replacements, if we need more, we'll resize */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6756 | if (size == 0) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 6757 | return PyBytes_FromStringAndSize(NULL, 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6758 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6759 | if (res == NULL) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6760 | return NULL; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6761 | str = PyBytes_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6762 | ressize = size; |
| 6763 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6764 | while (pos < size) { |
| 6765 | Py_UCS4 c = PyUnicode_READ(kind, data, pos); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6766 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6767 | /* can we encode this? */ |
| 6768 | if (c<limit) { |
| 6769 | /* no overflow check, because we know that the space is enough */ |
| 6770 | *str++ = (char)c; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6771 | ++pos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6772 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6773 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6774 | Py_ssize_t requiredsize; |
| 6775 | PyObject *repunicode; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6776 | Py_ssize_t repsize, newpos, respos, i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6777 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6778 | Py_ssize_t collstart = pos; |
| 6779 | Py_ssize_t collend = pos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6780 | /* find all unecodable characters */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6781 | while ((collend < size) && (PyUnicode_READ(kind, data, collend)>=limit)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6782 | ++collend; |
| 6783 | /* cache callback name lookup (if not done yet, i.e. it's the first error) */ |
| 6784 | if (known_errorHandler==-1) { |
| 6785 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 6786 | known_errorHandler = 1; |
| 6787 | else if (!strcmp(errors, "replace")) |
| 6788 | known_errorHandler = 2; |
| 6789 | else if (!strcmp(errors, "ignore")) |
| 6790 | known_errorHandler = 3; |
| 6791 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 6792 | known_errorHandler = 4; |
| 6793 | else |
| 6794 | known_errorHandler = 0; |
| 6795 | } |
| 6796 | switch (known_errorHandler) { |
| 6797 | case 1: /* strict */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6798 | raise_encode_exception(&exc, encoding, unicode, collstart, collend, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6799 | goto onError; |
| 6800 | case 2: /* replace */ |
| 6801 | while (collstart++<collend) |
| 6802 | *str++ = '?'; /* fall through */ |
| 6803 | case 3: /* ignore */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6804 | pos = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6805 | break; |
| 6806 | case 4: /* xmlcharrefreplace */ |
| 6807 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6808 | /* determine replacement size */ |
| 6809 | for (i = collstart, repsize = 0; i < collend; ++i) { |
| 6810 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 6811 | if (ch < 10) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6812 | repsize += 2+1+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6813 | else if (ch < 100) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6814 | repsize += 2+2+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6815 | else if (ch < 1000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6816 | repsize += 2+3+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6817 | else if (ch < 10000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6818 | repsize += 2+4+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6819 | else if (ch < 100000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6820 | repsize += 2+5+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6821 | else if (ch < 1000000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6822 | repsize += 2+6+1; |
Victor Stinner | 0d3721d | 2011-11-22 03:27:53 +0100 | [diff] [blame] | 6823 | else { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 6824 | assert(ch <= MAX_UNICODE); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6825 | repsize += 2+7+1; |
Victor Stinner | 0d3721d | 2011-11-22 03:27:53 +0100 | [diff] [blame] | 6826 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6827 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6828 | requiredsize = respos+repsize+(size-collend); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6829 | if (requiredsize > ressize) { |
| 6830 | if (requiredsize<2*ressize) |
| 6831 | requiredsize = 2*ressize; |
| 6832 | if (_PyBytes_Resize(&res, requiredsize)) |
| 6833 | goto onError; |
| 6834 | str = PyBytes_AS_STRING(res) + respos; |
| 6835 | ressize = requiredsize; |
| 6836 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6837 | /* generate replacement */ |
| 6838 | for (i = collstart; i < collend; ++i) { |
| 6839 | str += sprintf(str, "&#%d;", PyUnicode_READ(kind, data, i)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6840 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6841 | pos = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6842 | break; |
| 6843 | default: |
| 6844 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6845 | encoding, reason, unicode, &exc, |
| 6846 | collstart, collend, &newpos); |
| 6847 | if (repunicode == NULL || (PyUnicode_Check(repunicode) && |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6848 | PyUnicode_READY(repunicode) == -1)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6849 | goto onError; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6850 | if (PyBytes_Check(repunicode)) { |
| 6851 | /* Directly copy bytes result to output. */ |
| 6852 | repsize = PyBytes_Size(repunicode); |
| 6853 | if (repsize > 1) { |
| 6854 | /* Make room for all additional bytes. */ |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 6855 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6856 | if (_PyBytes_Resize(&res, ressize+repsize-1)) { |
| 6857 | Py_DECREF(repunicode); |
| 6858 | goto onError; |
| 6859 | } |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 6860 | str = PyBytes_AS_STRING(res) + respos; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6861 | ressize += repsize-1; |
| 6862 | } |
| 6863 | memcpy(str, PyBytes_AsString(repunicode), repsize); |
| 6864 | str += repsize; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6865 | pos = newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6866 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6867 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6868 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6869 | /* need more space? (at least enough for what we |
| 6870 | have+the replacement+the rest of the string, so |
| 6871 | we won't have to check space for encodable characters) */ |
| 6872 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6873 | repsize = PyUnicode_GET_LENGTH(repunicode); |
| 6874 | requiredsize = respos+repsize+(size-collend); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6875 | if (requiredsize > ressize) { |
| 6876 | if (requiredsize<2*ressize) |
| 6877 | requiredsize = 2*ressize; |
| 6878 | if (_PyBytes_Resize(&res, requiredsize)) { |
| 6879 | Py_DECREF(repunicode); |
| 6880 | goto onError; |
| 6881 | } |
| 6882 | str = PyBytes_AS_STRING(res) + respos; |
| 6883 | ressize = requiredsize; |
| 6884 | } |
| 6885 | /* check if there is anything unencodable in the replacement |
| 6886 | and copy it to the output */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6887 | for (i = 0; repsize-->0; ++i, ++str) { |
| 6888 | c = PyUnicode_READ_CHAR(repunicode, i); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6889 | if (c >= limit) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6890 | raise_encode_exception(&exc, encoding, unicode, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6891 | pos, pos+1, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6892 | Py_DECREF(repunicode); |
| 6893 | goto onError; |
| 6894 | } |
| 6895 | *str = (char)c; |
| 6896 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6897 | pos = newpos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6898 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6899 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6900 | } |
| 6901 | } |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6902 | /* Resize if we allocated to much */ |
| 6903 | size = str - PyBytes_AS_STRING(res); |
| 6904 | if (size < ressize) { /* If this falls res will be NULL */ |
Alexandre Vassalotti | bad1b92 | 2008-12-27 09:49:09 +0000 | [diff] [blame] | 6905 | assert(size >= 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6906 | if (_PyBytes_Resize(&res, size) < 0) |
| 6907 | goto onError; |
| 6908 | } |
| 6909 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6910 | Py_XDECREF(errorHandler); |
| 6911 | Py_XDECREF(exc); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6912 | return res; |
| 6913 | |
| 6914 | onError: |
| 6915 | Py_XDECREF(res); |
| 6916 | Py_XDECREF(errorHandler); |
| 6917 | Py_XDECREF(exc); |
| 6918 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6919 | } |
| 6920 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6921 | /* Deprecated */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6922 | PyObject * |
| 6923 | PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6924 | Py_ssize_t size, |
| 6925 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6926 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6927 | PyObject *result; |
| 6928 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 6929 | if (unicode == NULL) |
| 6930 | return NULL; |
| 6931 | result = unicode_encode_ucs1(unicode, errors, 256); |
| 6932 | Py_DECREF(unicode); |
| 6933 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6934 | } |
| 6935 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6936 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6937 | _PyUnicode_AsLatin1String(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6938 | { |
| 6939 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6940 | PyErr_BadArgument(); |
| 6941 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6942 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6943 | if (PyUnicode_READY(unicode) == -1) |
| 6944 | return NULL; |
| 6945 | /* Fast path: if it is a one-byte string, construct |
| 6946 | bytes object directly. */ |
| 6947 | if (PyUnicode_KIND(unicode) == PyUnicode_1BYTE_KIND) |
| 6948 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 6949 | PyUnicode_GET_LENGTH(unicode)); |
| 6950 | /* Non-Latin-1 characters present. Defer to above function to |
| 6951 | raise the exception. */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6952 | return unicode_encode_ucs1(unicode, errors, 256); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6953 | } |
| 6954 | |
| 6955 | PyObject* |
| 6956 | PyUnicode_AsLatin1String(PyObject *unicode) |
| 6957 | { |
| 6958 | return _PyUnicode_AsLatin1String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6959 | } |
| 6960 | |
| 6961 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ |
| 6962 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6963 | PyObject * |
| 6964 | PyUnicode_DecodeASCII(const char *s, |
| 6965 | Py_ssize_t size, |
| 6966 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6967 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6968 | const char *starts = s; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6969 | PyObject *v; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6970 | int kind; |
| 6971 | void *data; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6972 | Py_ssize_t startinpos; |
| 6973 | Py_ssize_t endinpos; |
| 6974 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6975 | const char *e; |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6976 | int has_error; |
| 6977 | const unsigned char *p = (const unsigned char *)s; |
| 6978 | const unsigned char *end = p + size; |
| 6979 | 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] | 6980 | PyObject *errorHandler = NULL; |
| 6981 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6982 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 6983 | if (size == 0) { |
| 6984 | Py_INCREF(unicode_empty); |
| 6985 | return unicode_empty; |
| 6986 | } |
| 6987 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6988 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6989 | if (size == 1 && (unsigned char)s[0] < 128) |
| 6990 | return get_latin1_char((unsigned char)s[0]); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6991 | |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6992 | has_error = 0; |
| 6993 | while (p < end && !has_error) { |
| 6994 | /* Fast path, see below in PyUnicode_DecodeUTF8Stateful for |
| 6995 | an explanation. */ |
| 6996 | if (!((size_t) p & LONG_PTR_MASK)) { |
| 6997 | /* Help register allocation */ |
| 6998 | register const unsigned char *_p = p; |
| 6999 | while (_p < aligned_end) { |
| 7000 | unsigned long value = *(unsigned long *) _p; |
| 7001 | if (value & ASCII_CHAR_MASK) { |
| 7002 | has_error = 1; |
| 7003 | break; |
| 7004 | } |
| 7005 | _p += SIZEOF_LONG; |
| 7006 | } |
| 7007 | if (_p == end) |
| 7008 | break; |
| 7009 | if (has_error) |
| 7010 | break; |
| 7011 | p = _p; |
| 7012 | } |
| 7013 | if (*p & 0x80) { |
| 7014 | has_error = 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7015 | break; |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 7016 | } |
| 7017 | else { |
| 7018 | ++p; |
| 7019 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 7020 | } |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 7021 | if (!has_error) |
| 7022 | return unicode_fromascii((const unsigned char *)s, size); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7023 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7024 | v = PyUnicode_New(size, 127); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7025 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7026 | goto onError; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7027 | kind = PyUnicode_KIND(v); |
| 7028 | data = PyUnicode_DATA(v); |
| 7029 | outpos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7030 | e = s + size; |
| 7031 | while (s < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7032 | register unsigned char c = (unsigned char)*s; |
| 7033 | if (c < 128) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7034 | PyUnicode_WRITE(kind, data, outpos++, c); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7035 | ++s; |
| 7036 | } |
| 7037 | else { |
| 7038 | startinpos = s-starts; |
| 7039 | endinpos = startinpos + 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7040 | if (unicode_decode_call_errorhandler( |
| 7041 | errors, &errorHandler, |
| 7042 | "ascii", "ordinal not in range(128)", |
| 7043 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7044 | &v, &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7045 | goto onError; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7046 | kind = PyUnicode_KIND(v); |
| 7047 | data = PyUnicode_DATA(v); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7048 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7049 | } |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 7050 | if (unicode_resize(&v, outpos) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7051 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7052 | Py_XDECREF(errorHandler); |
| 7053 | Py_XDECREF(exc); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 7054 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 7055 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7056 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7057 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7058 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7059 | Py_XDECREF(errorHandler); |
| 7060 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7061 | return NULL; |
| 7062 | } |
| 7063 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7064 | /* Deprecated */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7065 | PyObject * |
| 7066 | PyUnicode_EncodeASCII(const Py_UNICODE *p, |
| 7067 | Py_ssize_t size, |
| 7068 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7069 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7070 | PyObject *result; |
| 7071 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 7072 | if (unicode == NULL) |
| 7073 | return NULL; |
| 7074 | result = unicode_encode_ucs1(unicode, errors, 128); |
| 7075 | Py_DECREF(unicode); |
| 7076 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7077 | } |
| 7078 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7079 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7080 | _PyUnicode_AsASCIIString(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7081 | { |
| 7082 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7083 | PyErr_BadArgument(); |
| 7084 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7085 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7086 | if (PyUnicode_READY(unicode) == -1) |
| 7087 | return NULL; |
| 7088 | /* Fast path: if it is an ASCII-only string, construct bytes object |
| 7089 | directly. Else defer to above function to raise the exception. */ |
| 7090 | if (PyUnicode_MAX_CHAR_VALUE(unicode) < 128) |
| 7091 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 7092 | PyUnicode_GET_LENGTH(unicode)); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7093 | return unicode_encode_ucs1(unicode, errors, 128); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7094 | } |
| 7095 | |
| 7096 | PyObject * |
| 7097 | PyUnicode_AsASCIIString(PyObject *unicode) |
| 7098 | { |
| 7099 | return _PyUnicode_AsASCIIString(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7100 | } |
| 7101 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 7102 | #ifdef HAVE_MBCS |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 7103 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7104 | /* --- MBCS codecs for Windows -------------------------------------------- */ |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 7105 | |
Hirokazu Yamamoto | 3530246 | 2009-03-21 13:23:27 +0000 | [diff] [blame] | 7106 | #if SIZEOF_INT < SIZEOF_SIZE_T |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7107 | #define NEED_RETRY |
| 7108 | #endif |
| 7109 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7110 | #ifndef WC_ERR_INVALID_CHARS |
| 7111 | # define WC_ERR_INVALID_CHARS 0x0080 |
| 7112 | #endif |
| 7113 | |
| 7114 | static char* |
| 7115 | code_page_name(UINT code_page, PyObject **obj) |
| 7116 | { |
| 7117 | *obj = NULL; |
| 7118 | if (code_page == CP_ACP) |
| 7119 | return "mbcs"; |
| 7120 | if (code_page == CP_UTF7) |
| 7121 | return "CP_UTF7"; |
| 7122 | if (code_page == CP_UTF8) |
| 7123 | return "CP_UTF8"; |
| 7124 | |
| 7125 | *obj = PyBytes_FromFormat("cp%u", code_page); |
| 7126 | if (*obj == NULL) |
| 7127 | return NULL; |
| 7128 | return PyBytes_AS_STRING(*obj); |
| 7129 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7130 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7131 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7132 | is_dbcs_lead_byte(UINT code_page, const char *s, int offset) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7133 | { |
| 7134 | const char *curr = s + offset; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7135 | const char *prev; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7136 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7137 | if (!IsDBCSLeadByteEx(code_page, *curr)) |
| 7138 | return 0; |
| 7139 | |
| 7140 | prev = CharPrevExA(code_page, s, curr, 0); |
| 7141 | if (prev == curr) |
| 7142 | return 1; |
| 7143 | /* FIXME: This code is limited to "true" double-byte encodings, |
| 7144 | as it assumes an incomplete character consists of a single |
| 7145 | byte. */ |
| 7146 | if (curr - prev == 2) |
| 7147 | return 1; |
| 7148 | if (!IsDBCSLeadByteEx(code_page, *prev)) |
| 7149 | return 1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7150 | return 0; |
| 7151 | } |
| 7152 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7153 | static DWORD |
| 7154 | decode_code_page_flags(UINT code_page) |
| 7155 | { |
| 7156 | if (code_page == CP_UTF7) { |
| 7157 | /* The CP_UTF7 decoder only supports flags=0 */ |
| 7158 | return 0; |
| 7159 | } |
| 7160 | else |
| 7161 | return MB_ERR_INVALID_CHARS; |
| 7162 | } |
| 7163 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7164 | /* |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7165 | * Decode a byte string from a Windows code page into unicode object in strict |
| 7166 | * mode. |
| 7167 | * |
| 7168 | * Returns consumed size if succeed, returns -2 on decode error, or raise a |
| 7169 | * WindowsError and returns -1 on other error. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7170 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7171 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7172 | decode_code_page_strict(UINT code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7173 | PyObject **v, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7174 | const char *in, |
| 7175 | int insize) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7176 | { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7177 | const DWORD flags = decode_code_page_flags(code_page); |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 7178 | wchar_t *out; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7179 | DWORD outsize; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7180 | |
| 7181 | /* First get the size of the result */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7182 | assert(insize > 0); |
| 7183 | outsize = MultiByteToWideChar(code_page, flags, in, insize, NULL, 0); |
| 7184 | if (outsize <= 0) |
| 7185 | goto error; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7186 | |
| 7187 | if (*v == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7188 | /* Create unicode object */ |
Victor Stinner | ab59594 | 2011-12-17 04:59:06 +0100 | [diff] [blame] | 7189 | /* FIXME: don't use _PyUnicode_New(), but allocate a wchar_t* buffer */ |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7190 | *v = (PyObject*)_PyUnicode_New(outsize); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7191 | if (*v == NULL) |
| 7192 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7193 | out = PyUnicode_AS_UNICODE(*v); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7194 | } |
| 7195 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7196 | /* Extend unicode object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7197 | Py_ssize_t n = PyUnicode_GET_SIZE(*v); |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 7198 | if (unicode_resize(v, n + outsize) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7199 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7200 | out = PyUnicode_AS_UNICODE(*v) + n; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7201 | } |
| 7202 | |
| 7203 | /* Do the conversion */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7204 | outsize = MultiByteToWideChar(code_page, flags, in, insize, out, outsize); |
| 7205 | if (outsize <= 0) |
| 7206 | goto error; |
| 7207 | return insize; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7208 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7209 | error: |
| 7210 | if (GetLastError() == ERROR_NO_UNICODE_TRANSLATION) |
| 7211 | return -2; |
| 7212 | PyErr_SetFromWindowsErr(0); |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7213 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7214 | } |
| 7215 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7216 | /* |
| 7217 | * Decode a byte string from a code page into unicode object with an error |
| 7218 | * handler. |
| 7219 | * |
| 7220 | * Returns consumed size if succeed, or raise a WindowsError or |
| 7221 | * UnicodeDecodeError exception and returns -1 on error. |
| 7222 | */ |
| 7223 | static int |
| 7224 | decode_code_page_errors(UINT code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7225 | PyObject **v, |
| 7226 | const char *in, const int size, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7227 | const char *errors) |
| 7228 | { |
| 7229 | const char *startin = in; |
| 7230 | const char *endin = in + size; |
| 7231 | const DWORD flags = decode_code_page_flags(code_page); |
| 7232 | /* Ideally, we should get reason from FormatMessage. This is the Windows |
| 7233 | 2000 English version of the message. */ |
| 7234 | const char *reason = "No mapping for the Unicode character exists " |
| 7235 | "in the target code page."; |
| 7236 | /* each step cannot decode more than 1 character, but a character can be |
| 7237 | represented as a surrogate pair */ |
| 7238 | wchar_t buffer[2], *startout, *out; |
| 7239 | int insize, outsize; |
| 7240 | PyObject *errorHandler = NULL; |
| 7241 | PyObject *exc = NULL; |
| 7242 | PyObject *encoding_obj = NULL; |
| 7243 | char *encoding; |
| 7244 | DWORD err; |
| 7245 | int ret = -1; |
| 7246 | |
| 7247 | assert(size > 0); |
| 7248 | |
| 7249 | encoding = code_page_name(code_page, &encoding_obj); |
| 7250 | if (encoding == NULL) |
| 7251 | return -1; |
| 7252 | |
| 7253 | if (errors == NULL || strcmp(errors, "strict") == 0) { |
| 7254 | /* The last error was ERROR_NO_UNICODE_TRANSLATION, then we raise a |
| 7255 | UnicodeDecodeError. */ |
| 7256 | make_decode_exception(&exc, encoding, in, size, 0, 0, reason); |
| 7257 | if (exc != NULL) { |
| 7258 | PyCodec_StrictErrors(exc); |
| 7259 | Py_CLEAR(exc); |
| 7260 | } |
| 7261 | goto error; |
| 7262 | } |
| 7263 | |
| 7264 | if (*v == NULL) { |
| 7265 | /* Create unicode object */ |
| 7266 | if (size > PY_SSIZE_T_MAX / (Py_ssize_t)Py_ARRAY_LENGTH(buffer)) { |
| 7267 | PyErr_NoMemory(); |
| 7268 | goto error; |
| 7269 | } |
Victor Stinner | ab59594 | 2011-12-17 04:59:06 +0100 | [diff] [blame] | 7270 | /* FIXME: don't use _PyUnicode_New(), but allocate a wchar_t* buffer */ |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7271 | *v = (PyObject*)_PyUnicode_New(size * Py_ARRAY_LENGTH(buffer)); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7272 | if (*v == NULL) |
| 7273 | goto error; |
| 7274 | startout = PyUnicode_AS_UNICODE(*v); |
| 7275 | } |
| 7276 | else { |
| 7277 | /* Extend unicode object */ |
| 7278 | Py_ssize_t n = PyUnicode_GET_SIZE(*v); |
| 7279 | if (size > (PY_SSIZE_T_MAX - n) / (Py_ssize_t)Py_ARRAY_LENGTH(buffer)) { |
| 7280 | PyErr_NoMemory(); |
| 7281 | goto error; |
| 7282 | } |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 7283 | if (unicode_resize(v, n + size * Py_ARRAY_LENGTH(buffer)) < 0) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7284 | goto error; |
| 7285 | startout = PyUnicode_AS_UNICODE(*v) + n; |
| 7286 | } |
| 7287 | |
| 7288 | /* Decode the byte string character per character */ |
| 7289 | out = startout; |
| 7290 | while (in < endin) |
| 7291 | { |
| 7292 | /* Decode a character */ |
| 7293 | insize = 1; |
| 7294 | do |
| 7295 | { |
| 7296 | outsize = MultiByteToWideChar(code_page, flags, |
| 7297 | in, insize, |
| 7298 | buffer, Py_ARRAY_LENGTH(buffer)); |
| 7299 | if (outsize > 0) |
| 7300 | break; |
| 7301 | err = GetLastError(); |
| 7302 | if (err != ERROR_NO_UNICODE_TRANSLATION |
| 7303 | && err != ERROR_INSUFFICIENT_BUFFER) |
| 7304 | { |
| 7305 | PyErr_SetFromWindowsErr(0); |
| 7306 | goto error; |
| 7307 | } |
| 7308 | insize++; |
| 7309 | } |
| 7310 | /* 4=maximum length of a UTF-8 sequence */ |
| 7311 | while (insize <= 4 && (in + insize) <= endin); |
| 7312 | |
| 7313 | if (outsize <= 0) { |
| 7314 | Py_ssize_t startinpos, endinpos, outpos; |
| 7315 | |
| 7316 | startinpos = in - startin; |
| 7317 | endinpos = startinpos + 1; |
| 7318 | outpos = out - PyUnicode_AS_UNICODE(*v); |
| 7319 | if (unicode_decode_call_errorhandler( |
| 7320 | errors, &errorHandler, |
| 7321 | encoding, reason, |
| 7322 | &startin, &endin, &startinpos, &endinpos, &exc, &in, |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 7323 | v, &outpos)) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7324 | { |
| 7325 | goto error; |
| 7326 | } |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 7327 | out = PyUnicode_AS_UNICODE(*v) + outpos; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7328 | } |
| 7329 | else { |
| 7330 | in += insize; |
| 7331 | memcpy(out, buffer, outsize * sizeof(wchar_t)); |
| 7332 | out += outsize; |
| 7333 | } |
| 7334 | } |
| 7335 | |
| 7336 | /* write a NUL character at the end */ |
| 7337 | *out = 0; |
| 7338 | |
| 7339 | /* Extend unicode object */ |
| 7340 | outsize = out - startout; |
| 7341 | assert(outsize <= PyUnicode_WSTR_LENGTH(*v)); |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 7342 | if (unicode_resize(v, outsize) < 0) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7343 | goto error; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7344 | ret = size; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7345 | |
| 7346 | error: |
| 7347 | Py_XDECREF(encoding_obj); |
| 7348 | Py_XDECREF(errorHandler); |
| 7349 | Py_XDECREF(exc); |
| 7350 | return ret; |
| 7351 | } |
| 7352 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7353 | static PyObject * |
| 7354 | decode_code_page_stateful(int code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7355 | const char *s, Py_ssize_t size, |
| 7356 | const char *errors, Py_ssize_t *consumed) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7357 | { |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7358 | PyObject *v = NULL; |
| 7359 | int chunk_size, final, converted, done; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7360 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7361 | if (code_page < 0) { |
| 7362 | PyErr_SetString(PyExc_ValueError, "invalid code page number"); |
| 7363 | return NULL; |
| 7364 | } |
| 7365 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7366 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7367 | *consumed = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7368 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7369 | do |
| 7370 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7371 | #ifdef NEED_RETRY |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7372 | if (size > INT_MAX) { |
| 7373 | chunk_size = INT_MAX; |
| 7374 | final = 0; |
| 7375 | done = 0; |
| 7376 | } |
| 7377 | else |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7378 | #endif |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7379 | { |
| 7380 | chunk_size = (int)size; |
| 7381 | final = (consumed == NULL); |
| 7382 | done = 1; |
| 7383 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7384 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7385 | /* Skip trailing lead-byte unless 'final' is set */ |
| 7386 | if (!final && is_dbcs_lead_byte(code_page, s, chunk_size - 1)) |
| 7387 | --chunk_size; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7388 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7389 | if (chunk_size == 0 && done) { |
| 7390 | if (v != NULL) |
| 7391 | break; |
| 7392 | Py_INCREF(unicode_empty); |
| 7393 | return unicode_empty; |
| 7394 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7395 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7396 | |
| 7397 | converted = decode_code_page_strict(code_page, &v, |
| 7398 | s, chunk_size); |
| 7399 | if (converted == -2) |
| 7400 | converted = decode_code_page_errors(code_page, &v, |
| 7401 | s, chunk_size, |
| 7402 | errors); |
| 7403 | assert(converted != 0); |
| 7404 | |
| 7405 | if (converted < 0) { |
| 7406 | Py_XDECREF(v); |
| 7407 | return NULL; |
| 7408 | } |
| 7409 | |
| 7410 | if (consumed) |
| 7411 | *consumed += converted; |
| 7412 | |
| 7413 | s += converted; |
| 7414 | size -= converted; |
| 7415 | } while (!done); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7416 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 7417 | return unicode_result(v); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7418 | } |
| 7419 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7420 | PyObject * |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7421 | PyUnicode_DecodeCodePageStateful(int code_page, |
| 7422 | const char *s, |
| 7423 | Py_ssize_t size, |
| 7424 | const char *errors, |
| 7425 | Py_ssize_t *consumed) |
| 7426 | { |
| 7427 | return decode_code_page_stateful(code_page, s, size, errors, consumed); |
| 7428 | } |
| 7429 | |
| 7430 | PyObject * |
| 7431 | PyUnicode_DecodeMBCSStateful(const char *s, |
| 7432 | Py_ssize_t size, |
| 7433 | const char *errors, |
| 7434 | Py_ssize_t *consumed) |
| 7435 | { |
| 7436 | return decode_code_page_stateful(CP_ACP, s, size, errors, consumed); |
| 7437 | } |
| 7438 | |
| 7439 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7440 | PyUnicode_DecodeMBCS(const char *s, |
| 7441 | Py_ssize_t size, |
| 7442 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7443 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7444 | return PyUnicode_DecodeMBCSStateful(s, size, errors, NULL); |
| 7445 | } |
| 7446 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7447 | static DWORD |
| 7448 | encode_code_page_flags(UINT code_page, const char *errors) |
| 7449 | { |
| 7450 | if (code_page == CP_UTF8) { |
| 7451 | if (winver.dwMajorVersion >= 6) |
| 7452 | /* CP_UTF8 supports WC_ERR_INVALID_CHARS on Windows Vista |
| 7453 | and later */ |
| 7454 | return WC_ERR_INVALID_CHARS; |
| 7455 | else |
| 7456 | /* CP_UTF8 only supports flags=0 on Windows older than Vista */ |
| 7457 | return 0; |
| 7458 | } |
| 7459 | else if (code_page == CP_UTF7) { |
| 7460 | /* CP_UTF7 only supports flags=0 */ |
| 7461 | return 0; |
| 7462 | } |
| 7463 | else { |
| 7464 | if (errors != NULL && strcmp(errors, "replace") == 0) |
| 7465 | return 0; |
| 7466 | else |
| 7467 | return WC_NO_BEST_FIT_CHARS; |
| 7468 | } |
| 7469 | } |
| 7470 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7471 | /* |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7472 | * Encode a Unicode string to a Windows code page into a byte string in strict |
| 7473 | * mode. |
| 7474 | * |
| 7475 | * Returns consumed characters if succeed, returns -2 on encode error, or raise |
| 7476 | * a WindowsError and returns -1 on other error. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7477 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7478 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7479 | encode_code_page_strict(UINT code_page, PyObject **outbytes, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7480 | PyObject *unicode, Py_ssize_t offset, int len, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7481 | const char* errors) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7482 | { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7483 | BOOL usedDefaultChar = FALSE; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7484 | BOOL *pusedDefaultChar = &usedDefaultChar; |
| 7485 | int outsize; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7486 | PyObject *exc = NULL; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 7487 | wchar_t *p; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7488 | Py_ssize_t size; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7489 | const DWORD flags = encode_code_page_flags(code_page, NULL); |
| 7490 | char *out; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7491 | /* Create a substring so that we can get the UTF-16 representation |
| 7492 | of just the slice under consideration. */ |
| 7493 | PyObject *substring; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7494 | |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7495 | assert(len > 0); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7496 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7497 | if (code_page != CP_UTF8 && code_page != CP_UTF7) |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7498 | pusedDefaultChar = &usedDefaultChar; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7499 | else |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7500 | pusedDefaultChar = NULL; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7501 | |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7502 | substring = PyUnicode_Substring(unicode, offset, offset+len); |
| 7503 | if (substring == NULL) |
| 7504 | return -1; |
| 7505 | p = PyUnicode_AsUnicodeAndSize(substring, &size); |
| 7506 | if (p == NULL) { |
| 7507 | Py_DECREF(substring); |
| 7508 | return -1; |
| 7509 | } |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7510 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7511 | /* First get the size of the result */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7512 | outsize = WideCharToMultiByte(code_page, flags, |
| 7513 | p, size, |
| 7514 | NULL, 0, |
| 7515 | NULL, pusedDefaultChar); |
| 7516 | if (outsize <= 0) |
| 7517 | goto error; |
| 7518 | /* If we used a default char, then we failed! */ |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7519 | if (pusedDefaultChar && *pusedDefaultChar) { |
| 7520 | Py_DECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7521 | return -2; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7522 | } |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7523 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7524 | if (*outbytes == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7525 | /* Create string object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7526 | *outbytes = PyBytes_FromStringAndSize(NULL, outsize); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7527 | if (*outbytes == NULL) { |
| 7528 | Py_DECREF(substring); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7529 | return -1; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7530 | } |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7531 | out = PyBytes_AS_STRING(*outbytes); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7532 | } |
| 7533 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7534 | /* Extend string object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7535 | const Py_ssize_t n = PyBytes_Size(*outbytes); |
| 7536 | if (outsize > PY_SSIZE_T_MAX - n) { |
| 7537 | PyErr_NoMemory(); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7538 | Py_DECREF(substring); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7539 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7540 | } |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7541 | if (_PyBytes_Resize(outbytes, n + outsize) < 0) { |
| 7542 | Py_DECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7543 | return -1; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7544 | } |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7545 | out = PyBytes_AS_STRING(*outbytes) + n; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7546 | } |
| 7547 | |
| 7548 | /* Do the conversion */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7549 | outsize = WideCharToMultiByte(code_page, flags, |
| 7550 | p, size, |
| 7551 | out, outsize, |
| 7552 | NULL, pusedDefaultChar); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7553 | Py_CLEAR(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7554 | if (outsize <= 0) |
| 7555 | goto error; |
| 7556 | if (pusedDefaultChar && *pusedDefaultChar) |
| 7557 | return -2; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7558 | return 0; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7559 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7560 | error: |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7561 | Py_XDECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7562 | if (GetLastError() == ERROR_NO_UNICODE_TRANSLATION) |
| 7563 | return -2; |
| 7564 | PyErr_SetFromWindowsErr(0); |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7565 | return -1; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7566 | } |
| 7567 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7568 | /* |
| 7569 | * Encode a Unicode string to a Windows code page into a byte string using a |
| 7570 | * error handler. |
| 7571 | * |
| 7572 | * Returns consumed characters if succeed, or raise a WindowsError and returns |
| 7573 | * -1 on other error. |
| 7574 | */ |
| 7575 | static int |
| 7576 | encode_code_page_errors(UINT code_page, PyObject **outbytes, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7577 | PyObject *unicode, Py_ssize_t unicode_offset, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7578 | Py_ssize_t insize, const char* errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7579 | { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7580 | const DWORD flags = encode_code_page_flags(code_page, errors); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7581 | Py_ssize_t pos = unicode_offset; |
| 7582 | Py_ssize_t endin = unicode_offset + insize; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7583 | /* Ideally, we should get reason from FormatMessage. This is the Windows |
| 7584 | 2000 English version of the message. */ |
| 7585 | const char *reason = "invalid character"; |
| 7586 | /* 4=maximum length of a UTF-8 sequence */ |
| 7587 | char buffer[4]; |
| 7588 | BOOL usedDefaultChar = FALSE, *pusedDefaultChar; |
| 7589 | Py_ssize_t outsize; |
| 7590 | char *out; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7591 | PyObject *errorHandler = NULL; |
| 7592 | PyObject *exc = NULL; |
| 7593 | PyObject *encoding_obj = NULL; |
| 7594 | char *encoding; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7595 | Py_ssize_t newpos, newoutsize; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7596 | PyObject *rep; |
| 7597 | int ret = -1; |
| 7598 | |
| 7599 | assert(insize > 0); |
| 7600 | |
| 7601 | encoding = code_page_name(code_page, &encoding_obj); |
| 7602 | if (encoding == NULL) |
| 7603 | return -1; |
| 7604 | |
| 7605 | if (errors == NULL || strcmp(errors, "strict") == 0) { |
| 7606 | /* The last error was ERROR_NO_UNICODE_TRANSLATION, |
| 7607 | then we raise a UnicodeEncodeError. */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 7608 | make_encode_exception(&exc, encoding, unicode, 0, 0, reason); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7609 | if (exc != NULL) { |
| 7610 | PyCodec_StrictErrors(exc); |
| 7611 | Py_DECREF(exc); |
| 7612 | } |
| 7613 | Py_XDECREF(encoding_obj); |
| 7614 | return -1; |
| 7615 | } |
| 7616 | |
| 7617 | if (code_page != CP_UTF8 && code_page != CP_UTF7) |
| 7618 | pusedDefaultChar = &usedDefaultChar; |
| 7619 | else |
| 7620 | pusedDefaultChar = NULL; |
| 7621 | |
| 7622 | if (Py_ARRAY_LENGTH(buffer) > PY_SSIZE_T_MAX / insize) { |
| 7623 | PyErr_NoMemory(); |
| 7624 | goto error; |
| 7625 | } |
| 7626 | outsize = insize * Py_ARRAY_LENGTH(buffer); |
| 7627 | |
| 7628 | if (*outbytes == NULL) { |
| 7629 | /* Create string object */ |
| 7630 | *outbytes = PyBytes_FromStringAndSize(NULL, outsize); |
| 7631 | if (*outbytes == NULL) |
| 7632 | goto error; |
| 7633 | out = PyBytes_AS_STRING(*outbytes); |
| 7634 | } |
| 7635 | else { |
| 7636 | /* Extend string object */ |
| 7637 | Py_ssize_t n = PyBytes_Size(*outbytes); |
| 7638 | if (n > PY_SSIZE_T_MAX - outsize) { |
| 7639 | PyErr_NoMemory(); |
| 7640 | goto error; |
| 7641 | } |
| 7642 | if (_PyBytes_Resize(outbytes, n + outsize) < 0) |
| 7643 | goto error; |
| 7644 | out = PyBytes_AS_STRING(*outbytes) + n; |
| 7645 | } |
| 7646 | |
| 7647 | /* Encode the string character per character */ |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7648 | while (pos < endin) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7649 | { |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7650 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, pos); |
| 7651 | wchar_t chars[2]; |
| 7652 | int charsize; |
| 7653 | if (ch < 0x10000) { |
| 7654 | chars[0] = (wchar_t)ch; |
| 7655 | charsize = 1; |
| 7656 | } |
| 7657 | else { |
| 7658 | ch -= 0x10000; |
| 7659 | chars[0] = 0xd800 + (ch >> 10); |
| 7660 | chars[1] = 0xdc00 + (ch & 0x3ff); |
| 7661 | charsize = 2; |
| 7662 | } |
| 7663 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7664 | outsize = WideCharToMultiByte(code_page, flags, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7665 | chars, charsize, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7666 | buffer, Py_ARRAY_LENGTH(buffer), |
| 7667 | NULL, pusedDefaultChar); |
| 7668 | if (outsize > 0) { |
| 7669 | if (pusedDefaultChar == NULL || !(*pusedDefaultChar)) |
| 7670 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7671 | pos++; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7672 | memcpy(out, buffer, outsize); |
| 7673 | out += outsize; |
| 7674 | continue; |
| 7675 | } |
| 7676 | } |
| 7677 | else if (GetLastError() != ERROR_NO_UNICODE_TRANSLATION) { |
| 7678 | PyErr_SetFromWindowsErr(0); |
| 7679 | goto error; |
| 7680 | } |
| 7681 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7682 | rep = unicode_encode_call_errorhandler( |
| 7683 | errors, &errorHandler, encoding, reason, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7684 | unicode, &exc, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7685 | pos, pos + 1, &newpos); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7686 | if (rep == NULL) |
| 7687 | goto error; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7688 | pos = newpos; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7689 | |
| 7690 | if (PyBytes_Check(rep)) { |
| 7691 | outsize = PyBytes_GET_SIZE(rep); |
| 7692 | if (outsize != 1) { |
| 7693 | Py_ssize_t offset = out - PyBytes_AS_STRING(*outbytes); |
| 7694 | newoutsize = PyBytes_GET_SIZE(*outbytes) + (outsize - 1); |
| 7695 | if (_PyBytes_Resize(outbytes, newoutsize) < 0) { |
| 7696 | Py_DECREF(rep); |
| 7697 | goto error; |
| 7698 | } |
| 7699 | out = PyBytes_AS_STRING(*outbytes) + offset; |
| 7700 | } |
| 7701 | memcpy(out, PyBytes_AS_STRING(rep), outsize); |
| 7702 | out += outsize; |
| 7703 | } |
| 7704 | else { |
| 7705 | Py_ssize_t i; |
| 7706 | enum PyUnicode_Kind kind; |
| 7707 | void *data; |
| 7708 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7709 | if (PyUnicode_READY(rep) == -1) { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7710 | Py_DECREF(rep); |
| 7711 | goto error; |
| 7712 | } |
| 7713 | |
| 7714 | outsize = PyUnicode_GET_LENGTH(rep); |
| 7715 | if (outsize != 1) { |
| 7716 | Py_ssize_t offset = out - PyBytes_AS_STRING(*outbytes); |
| 7717 | newoutsize = PyBytes_GET_SIZE(*outbytes) + (outsize - 1); |
| 7718 | if (_PyBytes_Resize(outbytes, newoutsize) < 0) { |
| 7719 | Py_DECREF(rep); |
| 7720 | goto error; |
| 7721 | } |
| 7722 | out = PyBytes_AS_STRING(*outbytes) + offset; |
| 7723 | } |
| 7724 | kind = PyUnicode_KIND(rep); |
| 7725 | data = PyUnicode_DATA(rep); |
| 7726 | for (i=0; i < outsize; i++) { |
| 7727 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 7728 | if (ch > 127) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 7729 | raise_encode_exception(&exc, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7730 | encoding, unicode, |
| 7731 | pos, pos + 1, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7732 | "unable to encode error handler result to ASCII"); |
| 7733 | Py_DECREF(rep); |
| 7734 | goto error; |
| 7735 | } |
| 7736 | *out = (unsigned char)ch; |
| 7737 | out++; |
| 7738 | } |
| 7739 | } |
| 7740 | Py_DECREF(rep); |
| 7741 | } |
| 7742 | /* write a NUL byte */ |
| 7743 | *out = 0; |
| 7744 | outsize = out - PyBytes_AS_STRING(*outbytes); |
| 7745 | assert(outsize <= PyBytes_GET_SIZE(*outbytes)); |
| 7746 | if (_PyBytes_Resize(outbytes, outsize) < 0) |
| 7747 | goto error; |
| 7748 | ret = 0; |
| 7749 | |
| 7750 | error: |
| 7751 | Py_XDECREF(encoding_obj); |
| 7752 | Py_XDECREF(errorHandler); |
| 7753 | Py_XDECREF(exc); |
| 7754 | return ret; |
| 7755 | } |
| 7756 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7757 | static PyObject * |
| 7758 | encode_code_page(int code_page, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7759 | PyObject *unicode, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7760 | const char *errors) |
| 7761 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7762 | Py_ssize_t len; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7763 | PyObject *outbytes = NULL; |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7764 | Py_ssize_t offset; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7765 | int chunk_len, ret, done; |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7766 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7767 | if (PyUnicode_READY(unicode) == -1) |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7768 | return NULL; |
| 7769 | len = PyUnicode_GET_LENGTH(unicode); |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 7770 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7771 | if (code_page < 0) { |
| 7772 | PyErr_SetString(PyExc_ValueError, "invalid code page number"); |
| 7773 | return NULL; |
| 7774 | } |
| 7775 | |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7776 | if (len == 0) |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7777 | return PyBytes_FromStringAndSize(NULL, 0); |
| 7778 | |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7779 | offset = 0; |
| 7780 | do |
| 7781 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7782 | #ifdef NEED_RETRY |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7783 | /* UTF-16 encoding may double the size, so use only INT_MAX/2 |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7784 | chunks. */ |
| 7785 | if (len > INT_MAX/2) { |
| 7786 | chunk_len = INT_MAX/2; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7787 | done = 0; |
| 7788 | } |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7789 | else |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7790 | #endif |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7791 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7792 | chunk_len = (int)len; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7793 | done = 1; |
| 7794 | } |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7795 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7796 | ret = encode_code_page_strict(code_page, &outbytes, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7797 | unicode, offset, chunk_len, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7798 | errors); |
| 7799 | if (ret == -2) |
| 7800 | ret = encode_code_page_errors(code_page, &outbytes, |
| 7801 | unicode, offset, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7802 | chunk_len, errors); |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7803 | if (ret < 0) { |
| 7804 | Py_XDECREF(outbytes); |
| 7805 | return NULL; |
| 7806 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7807 | |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7808 | offset += chunk_len; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7809 | len -= chunk_len; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7810 | } while (!done); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7811 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7812 | return outbytes; |
| 7813 | } |
| 7814 | |
| 7815 | PyObject * |
| 7816 | PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
| 7817 | Py_ssize_t size, |
| 7818 | const char *errors) |
| 7819 | { |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7820 | PyObject *unicode, *res; |
| 7821 | unicode = PyUnicode_FromUnicode(p, size); |
| 7822 | if (unicode == NULL) |
| 7823 | return NULL; |
| 7824 | res = encode_code_page(CP_ACP, unicode, errors); |
| 7825 | Py_DECREF(unicode); |
| 7826 | return res; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7827 | } |
| 7828 | |
| 7829 | PyObject * |
| 7830 | PyUnicode_EncodeCodePage(int code_page, |
| 7831 | PyObject *unicode, |
| 7832 | const char *errors) |
| 7833 | { |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7834 | return encode_code_page(code_page, unicode, errors); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7835 | } |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 7836 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7837 | PyObject * |
| 7838 | PyUnicode_AsMBCSString(PyObject *unicode) |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 7839 | { |
| 7840 | if (!PyUnicode_Check(unicode)) { |
| 7841 | PyErr_BadArgument(); |
| 7842 | return NULL; |
| 7843 | } |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7844 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, NULL); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 7845 | } |
| 7846 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7847 | #undef NEED_RETRY |
| 7848 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 7849 | #endif /* HAVE_MBCS */ |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7850 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7851 | /* --- Character Mapping Codec -------------------------------------------- */ |
| 7852 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7853 | PyObject * |
| 7854 | PyUnicode_DecodeCharmap(const char *s, |
| 7855 | Py_ssize_t size, |
| 7856 | PyObject *mapping, |
| 7857 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7858 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7859 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7860 | Py_ssize_t startinpos; |
| 7861 | Py_ssize_t endinpos; |
| 7862 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7863 | const char *e; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 7864 | PyObject *v; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7865 | Py_ssize_t extrachars = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7866 | PyObject *errorHandler = NULL; |
| 7867 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7868 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7869 | /* Default to Latin-1 */ |
| 7870 | if (mapping == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7871 | return PyUnicode_DecodeLatin1(s, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7872 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7873 | v = PyUnicode_New(size, 127); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7874 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7875 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7876 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 7877 | return v; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7878 | outpos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7879 | e = s + size; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7880 | if (PyUnicode_CheckExact(mapping)) { |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7881 | Py_ssize_t maplen; |
| 7882 | enum PyUnicode_Kind kind; |
| 7883 | void *data; |
| 7884 | Py_UCS4 x; |
| 7885 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7886 | if (PyUnicode_READY(mapping) == -1) |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7887 | return NULL; |
| 7888 | |
| 7889 | maplen = PyUnicode_GET_LENGTH(mapping); |
| 7890 | data = PyUnicode_DATA(mapping); |
| 7891 | kind = PyUnicode_KIND(mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7892 | while (s < e) { |
| 7893 | unsigned char ch = *s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7894 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7895 | if (ch < maplen) |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7896 | x = PyUnicode_READ(kind, data, ch); |
| 7897 | else |
| 7898 | x = 0xfffe; /* invalid value */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7899 | |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7900 | if (x == 0xfffe) |
| 7901 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7902 | /* undefined mapping */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7903 | startinpos = s-starts; |
| 7904 | endinpos = startinpos+1; |
| 7905 | if (unicode_decode_call_errorhandler( |
| 7906 | errors, &errorHandler, |
| 7907 | "charmap", "character maps to <undefined>", |
| 7908 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7909 | &v, &outpos)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7910 | goto onError; |
| 7911 | } |
| 7912 | continue; |
| 7913 | } |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7914 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7915 | if (unicode_putchar(&v, &outpos, x) < 0) |
| 7916 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7917 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7918 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7919 | } |
| 7920 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7921 | while (s < e) { |
| 7922 | unsigned char ch = *s; |
| 7923 | PyObject *w, *x; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7924 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7925 | /* Get mapping (char ordinal -> integer, Unicode char or None) */ |
| 7926 | w = PyLong_FromLong((long)ch); |
| 7927 | if (w == NULL) |
| 7928 | goto onError; |
| 7929 | x = PyObject_GetItem(mapping, w); |
| 7930 | Py_DECREF(w); |
| 7931 | if (x == NULL) { |
| 7932 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 7933 | /* No mapping found means: mapping is undefined. */ |
| 7934 | PyErr_Clear(); |
| 7935 | x = Py_None; |
| 7936 | Py_INCREF(x); |
| 7937 | } else |
| 7938 | goto onError; |
| 7939 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7940 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7941 | /* Apply mapping */ |
| 7942 | if (PyLong_Check(x)) { |
| 7943 | long value = PyLong_AS_LONG(x); |
| 7944 | if (value < 0 || value > 65535) { |
| 7945 | PyErr_SetString(PyExc_TypeError, |
| 7946 | "character mapping must be in range(65536)"); |
| 7947 | Py_DECREF(x); |
| 7948 | goto onError; |
| 7949 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7950 | if (unicode_putchar(&v, &outpos, value) < 0) |
| 7951 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7952 | } |
| 7953 | else if (x == Py_None) { |
| 7954 | /* undefined mapping */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7955 | startinpos = s-starts; |
| 7956 | endinpos = startinpos+1; |
| 7957 | if (unicode_decode_call_errorhandler( |
| 7958 | errors, &errorHandler, |
| 7959 | "charmap", "character maps to <undefined>", |
| 7960 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7961 | &v, &outpos)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7962 | Py_DECREF(x); |
| 7963 | goto onError; |
| 7964 | } |
| 7965 | Py_DECREF(x); |
| 7966 | continue; |
| 7967 | } |
| 7968 | else if (PyUnicode_Check(x)) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7969 | Py_ssize_t targetsize; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7970 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7971 | if (PyUnicode_READY(x) == -1) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7972 | goto onError; |
| 7973 | targetsize = PyUnicode_GET_LENGTH(x); |
| 7974 | |
| 7975 | if (targetsize == 1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7976 | /* 1-1 mapping */ |
Victor Stinner | 62aa4d0 | 2011-11-09 00:03:45 +0100 | [diff] [blame] | 7977 | if (unicode_putchar(&v, &outpos, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7978 | PyUnicode_READ_CHAR(x, 0)) < 0) |
| 7979 | goto onError; |
| 7980 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7981 | else if (targetsize > 1) { |
| 7982 | /* 1-n mapping */ |
| 7983 | if (targetsize > extrachars) { |
| 7984 | /* resize first */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7985 | Py_ssize_t needed = (targetsize - extrachars) + \ |
| 7986 | (targetsize << 2); |
| 7987 | extrachars += needed; |
| 7988 | /* XXX overflow detection missing */ |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 7989 | if (unicode_resize(&v, |
| 7990 | PyUnicode_GET_LENGTH(v) + needed) < 0) |
| 7991 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7992 | Py_DECREF(x); |
| 7993 | goto onError; |
| 7994 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7995 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7996 | if (unicode_widen(&v, PyUnicode_MAX_CHAR_VALUE(x)) < 0) |
| 7997 | goto onError; |
| 7998 | PyUnicode_CopyCharacters(v, outpos, x, 0, targetsize); |
| 7999 | outpos += targetsize; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8000 | extrachars -= targetsize; |
| 8001 | } |
| 8002 | /* 1-0 mapping: skip the character */ |
| 8003 | } |
| 8004 | else { |
| 8005 | /* wrong return value */ |
| 8006 | PyErr_SetString(PyExc_TypeError, |
| 8007 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8008 | Py_DECREF(x); |
| 8009 | goto onError; |
| 8010 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8011 | Py_DECREF(x); |
| 8012 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8013 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8014 | } |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 8015 | if (unicode_resize(&v, outpos) < 0) |
Antoine Pitrou | a8f63c0 | 2011-11-08 18:37:16 +0100 | [diff] [blame] | 8016 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8017 | Py_XDECREF(errorHandler); |
| 8018 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 8019 | return unicode_result(v); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8020 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8021 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8022 | Py_XDECREF(errorHandler); |
| 8023 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8024 | Py_XDECREF(v); |
| 8025 | return NULL; |
| 8026 | } |
| 8027 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8028 | /* Charmap encoding: the lookup table */ |
| 8029 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8030 | struct encoding_map { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8031 | PyObject_HEAD |
| 8032 | unsigned char level1[32]; |
| 8033 | int count2, count3; |
| 8034 | unsigned char level23[1]; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8035 | }; |
| 8036 | |
| 8037 | static PyObject* |
| 8038 | encoding_map_size(PyObject *obj, PyObject* args) |
| 8039 | { |
| 8040 | struct encoding_map *map = (struct encoding_map*)obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8041 | return PyLong_FromLong(sizeof(*map) - 1 + 16*map->count2 + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8042 | 128*map->count3); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8043 | } |
| 8044 | |
| 8045 | static PyMethodDef encoding_map_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8046 | {"size", encoding_map_size, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8047 | PyDoc_STR("Return the size (in bytes) of this object") }, |
| 8048 | { 0 } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8049 | }; |
| 8050 | |
| 8051 | static void |
| 8052 | encoding_map_dealloc(PyObject* o) |
| 8053 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8054 | PyObject_FREE(o); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8055 | } |
| 8056 | |
| 8057 | static PyTypeObject EncodingMapType = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8058 | PyVarObject_HEAD_INIT(NULL, 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8059 | "EncodingMap", /*tp_name*/ |
| 8060 | sizeof(struct encoding_map), /*tp_basicsize*/ |
| 8061 | 0, /*tp_itemsize*/ |
| 8062 | /* methods */ |
| 8063 | encoding_map_dealloc, /*tp_dealloc*/ |
| 8064 | 0, /*tp_print*/ |
| 8065 | 0, /*tp_getattr*/ |
| 8066 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 8067 | 0, /*tp_reserved*/ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8068 | 0, /*tp_repr*/ |
| 8069 | 0, /*tp_as_number*/ |
| 8070 | 0, /*tp_as_sequence*/ |
| 8071 | 0, /*tp_as_mapping*/ |
| 8072 | 0, /*tp_hash*/ |
| 8073 | 0, /*tp_call*/ |
| 8074 | 0, /*tp_str*/ |
| 8075 | 0, /*tp_getattro*/ |
| 8076 | 0, /*tp_setattro*/ |
| 8077 | 0, /*tp_as_buffer*/ |
| 8078 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 8079 | 0, /*tp_doc*/ |
| 8080 | 0, /*tp_traverse*/ |
| 8081 | 0, /*tp_clear*/ |
| 8082 | 0, /*tp_richcompare*/ |
| 8083 | 0, /*tp_weaklistoffset*/ |
| 8084 | 0, /*tp_iter*/ |
| 8085 | 0, /*tp_iternext*/ |
| 8086 | encoding_map_methods, /*tp_methods*/ |
| 8087 | 0, /*tp_members*/ |
| 8088 | 0, /*tp_getset*/ |
| 8089 | 0, /*tp_base*/ |
| 8090 | 0, /*tp_dict*/ |
| 8091 | 0, /*tp_descr_get*/ |
| 8092 | 0, /*tp_descr_set*/ |
| 8093 | 0, /*tp_dictoffset*/ |
| 8094 | 0, /*tp_init*/ |
| 8095 | 0, /*tp_alloc*/ |
| 8096 | 0, /*tp_new*/ |
| 8097 | 0, /*tp_free*/ |
| 8098 | 0, /*tp_is_gc*/ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8099 | }; |
| 8100 | |
| 8101 | PyObject* |
| 8102 | PyUnicode_BuildEncodingMap(PyObject* string) |
| 8103 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8104 | PyObject *result; |
| 8105 | struct encoding_map *mresult; |
| 8106 | int i; |
| 8107 | int need_dict = 0; |
| 8108 | unsigned char level1[32]; |
| 8109 | unsigned char level2[512]; |
| 8110 | unsigned char *mlevel1, *mlevel2, *mlevel3; |
| 8111 | int count2 = 0, count3 = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8112 | int kind; |
| 8113 | void *data; |
| 8114 | Py_UCS4 ch; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8115 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8116 | if (!PyUnicode_Check(string) || PyUnicode_GET_LENGTH(string) != 256) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8117 | PyErr_BadArgument(); |
| 8118 | return NULL; |
| 8119 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8120 | kind = PyUnicode_KIND(string); |
| 8121 | data = PyUnicode_DATA(string); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8122 | memset(level1, 0xFF, sizeof level1); |
| 8123 | memset(level2, 0xFF, sizeof level2); |
| 8124 | |
| 8125 | /* If there isn't a one-to-one mapping of NULL to \0, |
| 8126 | or if there are non-BMP characters, we need to use |
| 8127 | a mapping dictionary. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8128 | if (PyUnicode_READ(kind, data, 0) != 0) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8129 | need_dict = 1; |
| 8130 | for (i = 1; i < 256; i++) { |
| 8131 | int l1, l2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8132 | ch = PyUnicode_READ(kind, data, i); |
| 8133 | if (ch == 0 || ch > 0xFFFF) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8134 | need_dict = 1; |
| 8135 | break; |
| 8136 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8137 | if (ch == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8138 | /* unmapped character */ |
| 8139 | continue; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8140 | l1 = ch >> 11; |
| 8141 | l2 = ch >> 7; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8142 | if (level1[l1] == 0xFF) |
| 8143 | level1[l1] = count2++; |
| 8144 | if (level2[l2] == 0xFF) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8145 | level2[l2] = count3++; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8146 | } |
| 8147 | |
| 8148 | if (count2 >= 0xFF || count3 >= 0xFF) |
| 8149 | need_dict = 1; |
| 8150 | |
| 8151 | if (need_dict) { |
| 8152 | PyObject *result = PyDict_New(); |
| 8153 | PyObject *key, *value; |
| 8154 | if (!result) |
| 8155 | return NULL; |
| 8156 | for (i = 0; i < 256; i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8157 | key = PyLong_FromLong(PyUnicode_READ(kind, data, i)); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8158 | value = PyLong_FromLong(i); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8159 | if (!key || !value) |
| 8160 | goto failed1; |
| 8161 | if (PyDict_SetItem(result, key, value) == -1) |
| 8162 | goto failed1; |
| 8163 | Py_DECREF(key); |
| 8164 | Py_DECREF(value); |
| 8165 | } |
| 8166 | return result; |
| 8167 | failed1: |
| 8168 | Py_XDECREF(key); |
| 8169 | Py_XDECREF(value); |
| 8170 | Py_DECREF(result); |
| 8171 | return NULL; |
| 8172 | } |
| 8173 | |
| 8174 | /* Create a three-level trie */ |
| 8175 | result = PyObject_MALLOC(sizeof(struct encoding_map) + |
| 8176 | 16*count2 + 128*count3 - 1); |
| 8177 | if (!result) |
| 8178 | return PyErr_NoMemory(); |
| 8179 | PyObject_Init(result, &EncodingMapType); |
| 8180 | mresult = (struct encoding_map*)result; |
| 8181 | mresult->count2 = count2; |
| 8182 | mresult->count3 = count3; |
| 8183 | mlevel1 = mresult->level1; |
| 8184 | mlevel2 = mresult->level23; |
| 8185 | mlevel3 = mresult->level23 + 16*count2; |
| 8186 | memcpy(mlevel1, level1, 32); |
| 8187 | memset(mlevel2, 0xFF, 16*count2); |
| 8188 | memset(mlevel3, 0, 128*count3); |
| 8189 | count3 = 0; |
| 8190 | for (i = 1; i < 256; i++) { |
| 8191 | int o1, o2, o3, i2, i3; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8192 | if (PyUnicode_READ(kind, data, i) == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8193 | /* unmapped character */ |
| 8194 | continue; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8195 | o1 = PyUnicode_READ(kind, data, i)>>11; |
| 8196 | o2 = (PyUnicode_READ(kind, data, i)>>7) & 0xF; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8197 | i2 = 16*mlevel1[o1] + o2; |
| 8198 | if (mlevel2[i2] == 0xFF) |
| 8199 | mlevel2[i2] = count3++; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8200 | o3 = PyUnicode_READ(kind, data, i) & 0x7F; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8201 | i3 = 128*mlevel2[i2] + o3; |
| 8202 | mlevel3[i3] = i; |
| 8203 | } |
| 8204 | return result; |
| 8205 | } |
| 8206 | |
| 8207 | static int |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 8208 | encoding_map_lookup(Py_UCS4 c, PyObject *mapping) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8209 | { |
| 8210 | struct encoding_map *map = (struct encoding_map*)mapping; |
| 8211 | int l1 = c>>11; |
| 8212 | int l2 = (c>>7) & 0xF; |
| 8213 | int l3 = c & 0x7F; |
| 8214 | int i; |
| 8215 | |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 8216 | if (c > 0xFFFF) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8217 | return -1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8218 | if (c == 0) |
| 8219 | return 0; |
| 8220 | /* level 1*/ |
| 8221 | i = map->level1[l1]; |
| 8222 | if (i == 0xFF) { |
| 8223 | return -1; |
| 8224 | } |
| 8225 | /* level 2*/ |
| 8226 | i = map->level23[16*i+l2]; |
| 8227 | if (i == 0xFF) { |
| 8228 | return -1; |
| 8229 | } |
| 8230 | /* level 3 */ |
| 8231 | i = map->level23[16*map->count2 + 128*i + l3]; |
| 8232 | if (i == 0) { |
| 8233 | return -1; |
| 8234 | } |
| 8235 | return i; |
| 8236 | } |
| 8237 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8238 | /* Lookup the character ch in the mapping. If the character |
| 8239 | can't be found, Py_None is returned (or NULL, if another |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 8240 | error occurred). */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8241 | static PyObject * |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 8242 | charmapencode_lookup(Py_UCS4 c, PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8243 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8244 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8245 | PyObject *x; |
| 8246 | |
| 8247 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8248 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8249 | x = PyObject_GetItem(mapping, w); |
| 8250 | Py_DECREF(w); |
| 8251 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8252 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 8253 | /* No mapping found means: mapping is undefined. */ |
| 8254 | PyErr_Clear(); |
| 8255 | x = Py_None; |
| 8256 | Py_INCREF(x); |
| 8257 | return x; |
| 8258 | } else |
| 8259 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8260 | } |
Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 8261 | else if (x == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8262 | return x; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8263 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8264 | long value = PyLong_AS_LONG(x); |
| 8265 | if (value < 0 || value > 255) { |
| 8266 | PyErr_SetString(PyExc_TypeError, |
| 8267 | "character mapping must be in range(256)"); |
| 8268 | Py_DECREF(x); |
| 8269 | return NULL; |
| 8270 | } |
| 8271 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8272 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8273 | else if (PyBytes_Check(x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8274 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8275 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8276 | /* wrong return value */ |
| 8277 | PyErr_Format(PyExc_TypeError, |
| 8278 | "character mapping must return integer, bytes or None, not %.400s", |
| 8279 | x->ob_type->tp_name); |
| 8280 | Py_DECREF(x); |
| 8281 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8282 | } |
| 8283 | } |
| 8284 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8285 | static int |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 8286 | charmapencode_resize(PyObject **outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8287 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8288 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
| 8289 | /* exponentially overallocate to minimize reallocations */ |
| 8290 | if (requiredsize < 2*outsize) |
| 8291 | requiredsize = 2*outsize; |
| 8292 | if (_PyBytes_Resize(outobj, requiredsize)) |
| 8293 | return -1; |
| 8294 | return 0; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8295 | } |
| 8296 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8297 | typedef enum charmapencode_result { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8298 | enc_SUCCESS, enc_FAILED, enc_EXCEPTION |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8299 | } charmapencode_result; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8300 | /* 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] | 8301 | various state variables. Resize the output bytes object if not enough |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8302 | space is available. Return a new reference to the object that |
| 8303 | was put in the output buffer, or Py_None, if the mapping was undefined |
| 8304 | (in which case no character was written) or NULL, if a |
Andrew M. Kuchling | 8294de5 | 2005-11-02 16:36:12 +0000 | [diff] [blame] | 8305 | reallocation error occurred. The caller must decref the result */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8306 | static charmapencode_result |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 8307 | charmapencode_output(Py_UCS4 c, PyObject *mapping, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8308 | PyObject **outobj, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8309 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8310 | PyObject *rep; |
| 8311 | char *outstart; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8312 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8313 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 8314 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8315 | int res = encoding_map_lookup(c, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8316 | Py_ssize_t requiredsize = *outpos+1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8317 | if (res == -1) |
| 8318 | return enc_FAILED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8319 | if (outsize<requiredsize) |
| 8320 | if (charmapencode_resize(outobj, outpos, requiredsize)) |
| 8321 | return enc_EXCEPTION; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8322 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8323 | outstart[(*outpos)++] = (char)res; |
| 8324 | return enc_SUCCESS; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8325 | } |
| 8326 | |
| 8327 | rep = charmapencode_lookup(c, mapping); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8328 | if (rep==NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8329 | return enc_EXCEPTION; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8330 | else if (rep==Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8331 | Py_DECREF(rep); |
| 8332 | return enc_FAILED; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8333 | } else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8334 | if (PyLong_Check(rep)) { |
| 8335 | Py_ssize_t requiredsize = *outpos+1; |
| 8336 | if (outsize<requiredsize) |
| 8337 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 8338 | Py_DECREF(rep); |
| 8339 | return enc_EXCEPTION; |
| 8340 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8341 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8342 | outstart[(*outpos)++] = (char)PyLong_AS_LONG(rep); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8343 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8344 | else { |
| 8345 | const char *repchars = PyBytes_AS_STRING(rep); |
| 8346 | Py_ssize_t repsize = PyBytes_GET_SIZE(rep); |
| 8347 | Py_ssize_t requiredsize = *outpos+repsize; |
| 8348 | if (outsize<requiredsize) |
| 8349 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 8350 | Py_DECREF(rep); |
| 8351 | return enc_EXCEPTION; |
| 8352 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8353 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8354 | memcpy(outstart + *outpos, repchars, repsize); |
| 8355 | *outpos += repsize; |
| 8356 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8357 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8358 | Py_DECREF(rep); |
| 8359 | return enc_SUCCESS; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8360 | } |
| 8361 | |
| 8362 | /* handle an error in PyUnicode_EncodeCharmap |
| 8363 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8364 | static int |
| 8365 | charmap_encoding_error( |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8366 | PyObject *unicode, Py_ssize_t *inpos, PyObject *mapping, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8367 | PyObject **exceptionObject, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 8368 | int *known_errorHandler, PyObject **errorHandler, const char *errors, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 8369 | PyObject **res, Py_ssize_t *respos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8370 | { |
| 8371 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8372 | Py_ssize_t size, repsize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8373 | Py_ssize_t newpos; |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8374 | enum PyUnicode_Kind kind; |
| 8375 | void *data; |
| 8376 | Py_ssize_t index; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8377 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8378 | Py_ssize_t collstartpos = *inpos; |
| 8379 | Py_ssize_t collendpos = *inpos+1; |
| 8380 | Py_ssize_t collpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8381 | char *encoding = "charmap"; |
| 8382 | char *reason = "character maps to <undefined>"; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8383 | charmapencode_result x; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8384 | Py_UCS4 ch; |
Brian Curtin | 2787ea4 | 2011-11-02 15:09:37 -0500 | [diff] [blame] | 8385 | int val; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8386 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 8387 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8388 | return -1; |
| 8389 | size = PyUnicode_GET_LENGTH(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8390 | /* find all unencodable characters */ |
| 8391 | while (collendpos < size) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8392 | PyObject *rep; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 8393 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8394 | ch = PyUnicode_READ_CHAR(unicode, collendpos); |
Brian Curtin | 2787ea4 | 2011-11-02 15:09:37 -0500 | [diff] [blame] | 8395 | val = encoding_map_lookup(ch, mapping); |
| 8396 | if (val != -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8397 | break; |
| 8398 | ++collendpos; |
| 8399 | continue; |
| 8400 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8401 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8402 | ch = PyUnicode_READ_CHAR(unicode, collendpos); |
| 8403 | rep = charmapencode_lookup(ch, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8404 | if (rep==NULL) |
| 8405 | return -1; |
| 8406 | else if (rep!=Py_None) { |
| 8407 | Py_DECREF(rep); |
| 8408 | break; |
| 8409 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8410 | Py_DECREF(rep); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8411 | ++collendpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8412 | } |
| 8413 | /* cache callback name lookup |
| 8414 | * (if not done yet, i.e. it's the first error) */ |
| 8415 | if (*known_errorHandler==-1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8416 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 8417 | *known_errorHandler = 1; |
| 8418 | else if (!strcmp(errors, "replace")) |
| 8419 | *known_errorHandler = 2; |
| 8420 | else if (!strcmp(errors, "ignore")) |
| 8421 | *known_errorHandler = 3; |
| 8422 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 8423 | *known_errorHandler = 4; |
| 8424 | else |
| 8425 | *known_errorHandler = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8426 | } |
| 8427 | switch (*known_errorHandler) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8428 | case 1: /* strict */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8429 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8430 | return -1; |
| 8431 | case 2: /* replace */ |
| 8432 | for (collpos = collstartpos; collpos<collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8433 | x = charmapencode_output('?', mapping, res, respos); |
| 8434 | if (x==enc_EXCEPTION) { |
| 8435 | return -1; |
| 8436 | } |
| 8437 | else if (x==enc_FAILED) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8438 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8439 | return -1; |
| 8440 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8441 | } |
| 8442 | /* fall through */ |
| 8443 | case 3: /* ignore */ |
| 8444 | *inpos = collendpos; |
| 8445 | break; |
| 8446 | case 4: /* xmlcharrefreplace */ |
| 8447 | /* generate replacement (temporarily (mis)uses p) */ |
| 8448 | for (collpos = collstartpos; collpos < collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8449 | char buffer[2+29+1+1]; |
| 8450 | char *cp; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8451 | sprintf(buffer, "&#%d;", (int)PyUnicode_READ_CHAR(unicode, collpos)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8452 | for (cp = buffer; *cp; ++cp) { |
| 8453 | x = charmapencode_output(*cp, mapping, res, respos); |
| 8454 | if (x==enc_EXCEPTION) |
| 8455 | return -1; |
| 8456 | else if (x==enc_FAILED) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8457 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8458 | return -1; |
| 8459 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8460 | } |
| 8461 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8462 | *inpos = collendpos; |
| 8463 | break; |
| 8464 | default: |
| 8465 | repunicode = unicode_encode_call_errorhandler(errors, errorHandler, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8466 | encoding, reason, unicode, exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8467 | collstartpos, collendpos, &newpos); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8468 | if (repunicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8469 | return -1; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8470 | if (PyBytes_Check(repunicode)) { |
| 8471 | /* Directly copy bytes result to output. */ |
| 8472 | Py_ssize_t outsize = PyBytes_Size(*res); |
| 8473 | Py_ssize_t requiredsize; |
| 8474 | repsize = PyBytes_Size(repunicode); |
| 8475 | requiredsize = *respos + repsize; |
| 8476 | if (requiredsize > outsize) |
| 8477 | /* Make room for all additional bytes. */ |
| 8478 | if (charmapencode_resize(res, respos, requiredsize)) { |
| 8479 | Py_DECREF(repunicode); |
| 8480 | return -1; |
| 8481 | } |
| 8482 | memcpy(PyBytes_AsString(*res) + *respos, |
| 8483 | PyBytes_AsString(repunicode), repsize); |
| 8484 | *respos += repsize; |
| 8485 | *inpos = newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 8486 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8487 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 8488 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8489 | /* generate replacement */ |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 8490 | if (PyUnicode_READY(repunicode) == -1) { |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8491 | Py_DECREF(repunicode); |
| 8492 | return -1; |
| 8493 | } |
Victor Stinner | 9e30aa5 | 2011-11-21 02:49:52 +0100 | [diff] [blame] | 8494 | repsize = PyUnicode_GET_LENGTH(repunicode); |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8495 | data = PyUnicode_DATA(repunicode); |
| 8496 | kind = PyUnicode_KIND(repunicode); |
| 8497 | for (index = 0; index < repsize; index++) { |
| 8498 | Py_UCS4 repch = PyUnicode_READ(kind, data, index); |
| 8499 | x = charmapencode_output(repch, mapping, res, respos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8500 | if (x==enc_EXCEPTION) { |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8501 | Py_DECREF(repunicode); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8502 | return -1; |
| 8503 | } |
| 8504 | else if (x==enc_FAILED) { |
| 8505 | Py_DECREF(repunicode); |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8506 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8507 | return -1; |
| 8508 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8509 | } |
| 8510 | *inpos = newpos; |
| 8511 | Py_DECREF(repunicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8512 | } |
| 8513 | return 0; |
| 8514 | } |
| 8515 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8516 | PyObject * |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8517 | _PyUnicode_EncodeCharmap(PyObject *unicode, |
| 8518 | PyObject *mapping, |
| 8519 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8520 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8521 | /* output object */ |
| 8522 | PyObject *res = NULL; |
| 8523 | /* current input position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8524 | Py_ssize_t inpos = 0; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8525 | Py_ssize_t size; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8526 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8527 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8528 | PyObject *errorHandler = NULL; |
| 8529 | PyObject *exc = NULL; |
| 8530 | /* the following variable is used for caching string comparisons |
| 8531 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 8532 | * 3=ignore, 4=xmlcharrefreplace */ |
| 8533 | int known_errorHandler = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8534 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 8535 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8536 | return NULL; |
| 8537 | size = PyUnicode_GET_LENGTH(unicode); |
| 8538 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8539 | /* Default to Latin-1 */ |
| 8540 | if (mapping == NULL) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8541 | return unicode_encode_ucs1(unicode, errors, 256); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8542 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8543 | /* allocate enough for a simple encoding without |
| 8544 | replacements, if we need more, we'll resize */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8545 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8546 | if (res == NULL) |
| 8547 | goto onError; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 8548 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8549 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8550 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8551 | while (inpos<size) { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8552 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, inpos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8553 | /* try to encode it */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8554 | charmapencode_result x = charmapencode_output(ch, mapping, &res, &respos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8555 | if (x==enc_EXCEPTION) /* error */ |
| 8556 | goto onError; |
| 8557 | if (x==enc_FAILED) { /* unencodable character */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8558 | if (charmap_encoding_error(unicode, &inpos, mapping, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8559 | &exc, |
| 8560 | &known_errorHandler, &errorHandler, errors, |
| 8561 | &res, &respos)) { |
| 8562 | goto onError; |
| 8563 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8564 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8565 | else |
| 8566 | /* done with this character => adjust input position */ |
| 8567 | ++inpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8568 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8569 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8570 | /* Resize if we allocated to much */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8571 | if (respos<PyBytes_GET_SIZE(res)) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 8572 | if (_PyBytes_Resize(&res, respos) < 0) |
| 8573 | goto onError; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 8574 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8575 | Py_XDECREF(exc); |
| 8576 | Py_XDECREF(errorHandler); |
| 8577 | return res; |
| 8578 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8579 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8580 | Py_XDECREF(res); |
| 8581 | Py_XDECREF(exc); |
| 8582 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8583 | return NULL; |
| 8584 | } |
| 8585 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8586 | /* Deprecated */ |
| 8587 | PyObject * |
| 8588 | PyUnicode_EncodeCharmap(const Py_UNICODE *p, |
| 8589 | Py_ssize_t size, |
| 8590 | PyObject *mapping, |
| 8591 | const char *errors) |
| 8592 | { |
| 8593 | PyObject *result; |
| 8594 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 8595 | if (unicode == NULL) |
| 8596 | return NULL; |
| 8597 | result = _PyUnicode_EncodeCharmap(unicode, mapping, errors); |
| 8598 | Py_DECREF(unicode); |
Victor Stinner | fc026c9 | 2011-11-04 00:24:51 +0100 | [diff] [blame] | 8599 | return result; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8600 | } |
| 8601 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8602 | PyObject * |
| 8603 | PyUnicode_AsCharmapString(PyObject *unicode, |
| 8604 | PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8605 | { |
| 8606 | if (!PyUnicode_Check(unicode) || mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8607 | PyErr_BadArgument(); |
| 8608 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8609 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8610 | return _PyUnicode_EncodeCharmap(unicode, mapping, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8611 | } |
| 8612 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8613 | /* create or adjust a UnicodeTranslateError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8614 | static void |
| 8615 | make_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8616 | PyObject *unicode, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8617 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 8618 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8619 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8620 | if (*exceptionObject == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8621 | *exceptionObject = _PyUnicodeTranslateError_Create( |
| 8622 | unicode, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8623 | } |
| 8624 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8625 | if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
| 8626 | goto onError; |
| 8627 | if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) |
| 8628 | goto onError; |
| 8629 | if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) |
| 8630 | goto onError; |
| 8631 | return; |
| 8632 | onError: |
| 8633 | Py_DECREF(*exceptionObject); |
| 8634 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8635 | } |
| 8636 | } |
| 8637 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8638 | /* raises a UnicodeTranslateError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8639 | static void |
| 8640 | raise_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8641 | PyObject *unicode, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8642 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 8643 | const char *reason) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8644 | { |
| 8645 | make_translate_exception(exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8646 | unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8647 | if (*exceptionObject != NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8648 | PyCodec_StrictErrors(*exceptionObject); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8649 | } |
| 8650 | |
| 8651 | /* error handling callback helper: |
| 8652 | build arguments, call the callback and check the arguments, |
| 8653 | put the result into newpos and return the replacement string, which |
| 8654 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8655 | static PyObject * |
| 8656 | unicode_translate_call_errorhandler(const char *errors, |
| 8657 | PyObject **errorHandler, |
| 8658 | const char *reason, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8659 | PyObject *unicode, PyObject **exceptionObject, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8660 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 8661 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8662 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 8663 | 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] | 8664 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8665 | Py_ssize_t i_newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8666 | PyObject *restuple; |
| 8667 | PyObject *resunicode; |
| 8668 | |
| 8669 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8670 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8671 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8672 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8673 | } |
| 8674 | |
| 8675 | make_translate_exception(exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8676 | unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8677 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8678 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8679 | |
| 8680 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8681 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8682 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8683 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8684 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 8685 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8686 | Py_DECREF(restuple); |
| 8687 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8688 | } |
| 8689 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8690 | &resunicode, &i_newpos)) { |
| 8691 | Py_DECREF(restuple); |
| 8692 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8693 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8694 | if (i_newpos<0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8695 | *newpos = PyUnicode_GET_LENGTH(unicode)+i_newpos; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8696 | else |
| 8697 | *newpos = i_newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8698 | if (*newpos<0 || *newpos>PyUnicode_GET_LENGTH(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8699 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 8700 | Py_DECREF(restuple); |
| 8701 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 8702 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8703 | Py_INCREF(resunicode); |
| 8704 | Py_DECREF(restuple); |
| 8705 | return resunicode; |
| 8706 | } |
| 8707 | |
| 8708 | /* Lookup the character ch in the mapping and put the result in result, |
| 8709 | which must be decrefed by the caller. |
| 8710 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8711 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8712 | charmaptranslate_lookup(Py_UCS4 c, PyObject *mapping, PyObject **result) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8713 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8714 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8715 | PyObject *x; |
| 8716 | |
| 8717 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8718 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8719 | x = PyObject_GetItem(mapping, w); |
| 8720 | Py_DECREF(w); |
| 8721 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8722 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 8723 | /* No mapping found means: use 1:1 mapping. */ |
| 8724 | PyErr_Clear(); |
| 8725 | *result = NULL; |
| 8726 | return 0; |
| 8727 | } else |
| 8728 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8729 | } |
| 8730 | else if (x == Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8731 | *result = x; |
| 8732 | return 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8733 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8734 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8735 | long value = PyLong_AS_LONG(x); |
| 8736 | long max = PyUnicode_GetMax(); |
| 8737 | if (value < 0 || value > max) { |
| 8738 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5a2f7e60 | 2007-10-24 21:13:09 +0000 | [diff] [blame] | 8739 | "character mapping must be in range(0x%x)", max+1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8740 | Py_DECREF(x); |
| 8741 | return -1; |
| 8742 | } |
| 8743 | *result = x; |
| 8744 | return 0; |
| 8745 | } |
| 8746 | else if (PyUnicode_Check(x)) { |
| 8747 | *result = x; |
| 8748 | return 0; |
| 8749 | } |
| 8750 | else { |
| 8751 | /* wrong return value */ |
| 8752 | PyErr_SetString(PyExc_TypeError, |
| 8753 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8754 | Py_DECREF(x); |
| 8755 | return -1; |
| 8756 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8757 | } |
| 8758 | /* ensure that *outobj is at least requiredsize characters long, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8759 | if not reallocate and adjust various state variables. |
| 8760 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8761 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8762 | charmaptranslate_makespace(Py_UCS4 **outobj, Py_ssize_t *psize, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8763 | Py_ssize_t requiredsize) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8764 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8765 | Py_ssize_t oldsize = *psize; |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 8766 | if (requiredsize > oldsize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8767 | /* exponentially overallocate to minimize reallocations */ |
| 8768 | if (requiredsize < 2 * oldsize) |
| 8769 | requiredsize = 2 * oldsize; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8770 | *outobj = PyMem_Realloc(*outobj, requiredsize * sizeof(Py_UCS4)); |
| 8771 | if (*outobj == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8772 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8773 | *psize = requiredsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8774 | } |
| 8775 | return 0; |
| 8776 | } |
| 8777 | /* lookup the character, put the result in the output string and adjust |
| 8778 | various state variables. Return a new reference to the object that |
| 8779 | was put in the output buffer in *result, or Py_None, if the mapping was |
| 8780 | undefined (in which case no character was written). |
| 8781 | The called must decref result. |
| 8782 | Return 0 on success, -1 on error. */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8783 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8784 | charmaptranslate_output(PyObject *input, Py_ssize_t ipos, |
| 8785 | PyObject *mapping, Py_UCS4 **output, |
| 8786 | Py_ssize_t *osize, Py_ssize_t *opos, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8787 | PyObject **res) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8788 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8789 | Py_UCS4 curinp = PyUnicode_READ_CHAR(input, ipos); |
| 8790 | if (charmaptranslate_lookup(curinp, mapping, res)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8791 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8792 | if (*res==NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8793 | /* not found => default to 1:1 mapping */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8794 | (*output)[(*opos)++] = curinp; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8795 | } |
| 8796 | else if (*res==Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8797 | ; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8798 | else if (PyLong_Check(*res)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8799 | /* 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] | 8800 | (*output)[(*opos)++] = (Py_UCS4)PyLong_AS_LONG(*res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8801 | } |
| 8802 | else if (PyUnicode_Check(*res)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8803 | Py_ssize_t repsize; |
| 8804 | if (PyUnicode_READY(*res) == -1) |
| 8805 | return -1; |
| 8806 | repsize = PyUnicode_GET_LENGTH(*res); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8807 | if (repsize==1) { |
| 8808 | /* 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] | 8809 | (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8810 | } |
| 8811 | else if (repsize!=0) { |
| 8812 | /* more than one character */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8813 | Py_ssize_t requiredsize = *opos + |
| 8814 | (PyUnicode_GET_LENGTH(input) - ipos) + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8815 | repsize - 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8816 | Py_ssize_t i; |
| 8817 | if (charmaptranslate_makespace(output, osize, requiredsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8818 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8819 | for(i = 0; i < repsize; i++) |
| 8820 | (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, i); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8821 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8822 | } |
| 8823 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8824 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8825 | return 0; |
| 8826 | } |
| 8827 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8828 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8829 | _PyUnicode_TranslateCharmap(PyObject *input, |
| 8830 | PyObject *mapping, |
| 8831 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8832 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8833 | /* input object */ |
| 8834 | char *idata; |
| 8835 | Py_ssize_t size, i; |
| 8836 | int kind; |
| 8837 | /* output buffer */ |
| 8838 | Py_UCS4 *output = NULL; |
| 8839 | Py_ssize_t osize; |
| 8840 | PyObject *res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8841 | /* current output position */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8842 | Py_ssize_t opos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8843 | char *reason = "character maps to <undefined>"; |
| 8844 | PyObject *errorHandler = NULL; |
| 8845 | PyObject *exc = NULL; |
| 8846 | /* the following variable is used for caching string comparisons |
| 8847 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 8848 | * 3=ignore, 4=xmlcharrefreplace */ |
| 8849 | int known_errorHandler = -1; |
| 8850 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8851 | if (mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8852 | PyErr_BadArgument(); |
| 8853 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8854 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8855 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8856 | if (PyUnicode_READY(input) == -1) |
| 8857 | return NULL; |
| 8858 | idata = (char*)PyUnicode_DATA(input); |
| 8859 | kind = PyUnicode_KIND(input); |
| 8860 | size = PyUnicode_GET_LENGTH(input); |
| 8861 | i = 0; |
| 8862 | |
| 8863 | if (size == 0) { |
| 8864 | Py_INCREF(input); |
| 8865 | return input; |
| 8866 | } |
| 8867 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8868 | /* allocate enough for a simple 1:1 translation without |
| 8869 | replacements, if we need more, we'll resize */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8870 | osize = size; |
| 8871 | output = PyMem_Malloc(osize * sizeof(Py_UCS4)); |
| 8872 | opos = 0; |
| 8873 | if (output == NULL) { |
| 8874 | PyErr_NoMemory(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8875 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8876 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8877 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8878 | while (i<size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8879 | /* try to encode it */ |
| 8880 | PyObject *x = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8881 | if (charmaptranslate_output(input, i, mapping, |
| 8882 | &output, &osize, &opos, &x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8883 | Py_XDECREF(x); |
| 8884 | goto onError; |
| 8885 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8886 | Py_XDECREF(x); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8887 | if (x!=Py_None) /* it worked => adjust input pointer */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8888 | ++i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8889 | else { /* untranslatable character */ |
| 8890 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
| 8891 | Py_ssize_t repsize; |
| 8892 | Py_ssize_t newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8893 | Py_ssize_t uni2; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8894 | /* startpos for collecting untranslatable chars */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8895 | Py_ssize_t collstart = i; |
| 8896 | Py_ssize_t collend = i+1; |
| 8897 | Py_ssize_t coll; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8898 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8899 | /* find all untranslatable characters */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8900 | while (collend < size) { |
| 8901 | if (charmaptranslate_lookup(PyUnicode_READ(kind,idata, collend), mapping, &x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8902 | goto onError; |
| 8903 | Py_XDECREF(x); |
| 8904 | if (x!=Py_None) |
| 8905 | break; |
| 8906 | ++collend; |
| 8907 | } |
| 8908 | /* cache callback name lookup |
| 8909 | * (if not done yet, i.e. it's the first error) */ |
| 8910 | if (known_errorHandler==-1) { |
| 8911 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 8912 | known_errorHandler = 1; |
| 8913 | else if (!strcmp(errors, "replace")) |
| 8914 | known_errorHandler = 2; |
| 8915 | else if (!strcmp(errors, "ignore")) |
| 8916 | known_errorHandler = 3; |
| 8917 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 8918 | known_errorHandler = 4; |
| 8919 | else |
| 8920 | known_errorHandler = 0; |
| 8921 | } |
| 8922 | switch (known_errorHandler) { |
| 8923 | case 1: /* strict */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8924 | raise_translate_exception(&exc, input, collstart, |
| 8925 | collend, reason); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8926 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8927 | case 2: /* replace */ |
| 8928 | /* 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] | 8929 | for (coll = collstart; coll<collend; coll++) |
| 8930 | output[opos++] = '?'; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8931 | /* fall through */ |
| 8932 | case 3: /* ignore */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8933 | i = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8934 | break; |
| 8935 | case 4: /* xmlcharrefreplace */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8936 | /* generate replacement (temporarily (mis)uses i) */ |
| 8937 | for (i = collstart; i < collend; ++i) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8938 | char buffer[2+29+1+1]; |
| 8939 | char *cp; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8940 | sprintf(buffer, "&#%d;", PyUnicode_READ(kind, idata, i)); |
| 8941 | if (charmaptranslate_makespace(&output, &osize, |
| 8942 | opos+strlen(buffer)+(size-collend))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8943 | goto onError; |
| 8944 | for (cp = buffer; *cp; ++cp) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8945 | output[opos++] = *cp; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8946 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8947 | i = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8948 | break; |
| 8949 | default: |
| 8950 | repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8951 | reason, input, &exc, |
| 8952 | collstart, collend, &newpos); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 8953 | if (repunicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8954 | goto onError; |
Benjamin Peterson | 9ca3ffa | 2012-01-01 16:04:29 -0600 | [diff] [blame] | 8955 | if (PyUnicode_READY(repunicode) == -1) { |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 8956 | Py_DECREF(repunicode); |
| 8957 | goto onError; |
| 8958 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8959 | /* generate replacement */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8960 | repsize = PyUnicode_GET_LENGTH(repunicode); |
| 8961 | if (charmaptranslate_makespace(&output, &osize, |
| 8962 | opos+repsize+(size-collend))) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8963 | Py_DECREF(repunicode); |
| 8964 | goto onError; |
| 8965 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8966 | for (uni2 = 0; repsize-->0; ++uni2) |
| 8967 | output[opos++] = PyUnicode_READ_CHAR(repunicode, uni2); |
| 8968 | i = newpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8969 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8970 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8971 | } |
| 8972 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8973 | res = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, output, opos); |
| 8974 | if (!res) |
| 8975 | goto onError; |
| 8976 | PyMem_Free(output); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8977 | Py_XDECREF(exc); |
| 8978 | Py_XDECREF(errorHandler); |
| 8979 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8980 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8981 | onError: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8982 | PyMem_Free(output); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8983 | Py_XDECREF(exc); |
| 8984 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8985 | return NULL; |
| 8986 | } |
| 8987 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8988 | /* Deprecated. Use PyUnicode_Translate instead. */ |
| 8989 | PyObject * |
| 8990 | PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
| 8991 | Py_ssize_t size, |
| 8992 | PyObject *mapping, |
| 8993 | const char *errors) |
| 8994 | { |
| 8995 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 8996 | if (!unicode) |
| 8997 | return NULL; |
| 8998 | return _PyUnicode_TranslateCharmap(unicode, mapping, errors); |
| 8999 | } |
| 9000 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9001 | PyObject * |
| 9002 | PyUnicode_Translate(PyObject *str, |
| 9003 | PyObject *mapping, |
| 9004 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9005 | { |
| 9006 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9007 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9008 | str = PyUnicode_FromObject(str); |
| 9009 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9010 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9011 | result = _PyUnicode_TranslateCharmap(str, mapping, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9012 | Py_DECREF(str); |
| 9013 | return result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9014 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9015 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9016 | Py_XDECREF(str); |
| 9017 | return NULL; |
| 9018 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9019 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9020 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9021 | fix_decimal_and_space_to_ascii(PyObject *self) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9022 | { |
| 9023 | /* No need to call PyUnicode_READY(self) because this function is only |
| 9024 | called as a callback from fixup() which does it already. */ |
| 9025 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 9026 | const int kind = PyUnicode_KIND(self); |
| 9027 | void *data = PyUnicode_DATA(self); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9028 | Py_UCS4 maxchar = 127, ch, fixed; |
Benjamin Peterson | 821e4cf | 2012-01-12 15:40:18 -0500 | [diff] [blame] | 9029 | int modified = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9030 | Py_ssize_t i; |
| 9031 | |
| 9032 | for (i = 0; i < len; ++i) { |
| 9033 | ch = PyUnicode_READ(kind, data, i); |
| 9034 | fixed = 0; |
| 9035 | if (ch > 127) { |
| 9036 | if (Py_UNICODE_ISSPACE(ch)) |
| 9037 | fixed = ' '; |
| 9038 | else { |
| 9039 | const int decimal = Py_UNICODE_TODECIMAL(ch); |
| 9040 | if (decimal >= 0) |
| 9041 | fixed = '0' + decimal; |
| 9042 | } |
| 9043 | if (fixed != 0) { |
Benjamin Peterson | 821e4cf | 2012-01-12 15:40:18 -0500 | [diff] [blame] | 9044 | modified = 1; |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9045 | maxchar = MAX_MAXCHAR(maxchar, fixed); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9046 | PyUnicode_WRITE(kind, data, i, fixed); |
| 9047 | } |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9048 | else |
| 9049 | maxchar = MAX_MAXCHAR(maxchar, ch); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9050 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9051 | } |
| 9052 | |
Benjamin Peterson | 821e4cf | 2012-01-12 15:40:18 -0500 | [diff] [blame] | 9053 | return (modified) ? maxchar : 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9054 | } |
| 9055 | |
| 9056 | PyObject * |
| 9057 | _PyUnicode_TransformDecimalAndSpaceToASCII(PyObject *unicode) |
| 9058 | { |
| 9059 | if (!PyUnicode_Check(unicode)) { |
| 9060 | PyErr_BadInternalCall(); |
| 9061 | return NULL; |
| 9062 | } |
| 9063 | if (PyUnicode_READY(unicode) == -1) |
| 9064 | return NULL; |
| 9065 | if (PyUnicode_MAX_CHAR_VALUE(unicode) <= 127) { |
| 9066 | /* If the string is already ASCII, just return the same string */ |
| 9067 | Py_INCREF(unicode); |
| 9068 | return unicode; |
| 9069 | } |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9070 | return fixup(unicode, fix_decimal_and_space_to_ascii); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9071 | } |
| 9072 | |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 9073 | PyObject * |
| 9074 | PyUnicode_TransformDecimalToASCII(Py_UNICODE *s, |
| 9075 | Py_ssize_t length) |
| 9076 | { |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 9077 | PyObject *decimal; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 9078 | Py_ssize_t i; |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 9079 | Py_UCS4 maxchar; |
| 9080 | enum PyUnicode_Kind kind; |
| 9081 | void *data; |
| 9082 | |
Victor Stinner | 99d7ad0 | 2012-02-22 13:37:39 +0100 | [diff] [blame] | 9083 | maxchar = 127; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 9084 | for (i = 0; i < length; i++) { |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 9085 | Py_UNICODE ch = s[i]; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 9086 | if (ch > 127) { |
| 9087 | int decimal = Py_UNICODE_TODECIMAL(ch); |
| 9088 | if (decimal >= 0) |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 9089 | ch = '0' + decimal; |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9090 | maxchar = MAX_MAXCHAR(maxchar, ch); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 9091 | } |
| 9092 | } |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 9093 | |
| 9094 | /* Copy to a new string */ |
| 9095 | decimal = PyUnicode_New(length, maxchar); |
| 9096 | if (decimal == NULL) |
| 9097 | return decimal; |
| 9098 | kind = PyUnicode_KIND(decimal); |
| 9099 | data = PyUnicode_DATA(decimal); |
| 9100 | /* Iterate over code points */ |
| 9101 | for (i = 0; i < length; i++) { |
| 9102 | Py_UNICODE ch = s[i]; |
| 9103 | if (ch > 127) { |
| 9104 | int decimal = Py_UNICODE_TODECIMAL(ch); |
| 9105 | if (decimal >= 0) |
| 9106 | ch = '0' + decimal; |
| 9107 | } |
| 9108 | PyUnicode_WRITE(kind, data, i, ch); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9109 | } |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 9110 | return unicode_result(decimal); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 9111 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 9112 | /* --- Decimal Encoder ---------------------------------------------------- */ |
| 9113 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9114 | int |
| 9115 | PyUnicode_EncodeDecimal(Py_UNICODE *s, |
| 9116 | Py_ssize_t length, |
| 9117 | char *output, |
| 9118 | const char *errors) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 9119 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 9120 | PyObject *unicode; |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 9121 | Py_ssize_t i; |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 9122 | enum PyUnicode_Kind kind; |
| 9123 | void *data; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 9124 | |
| 9125 | if (output == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9126 | PyErr_BadArgument(); |
| 9127 | return -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 9128 | } |
| 9129 | |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 9130 | unicode = PyUnicode_FromUnicode(s, length); |
| 9131 | if (unicode == NULL) |
| 9132 | return -1; |
| 9133 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 9134 | if (PyUnicode_READY(unicode) == -1) { |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 9135 | Py_DECREF(unicode); |
| 9136 | return -1; |
| 9137 | } |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 9138 | kind = PyUnicode_KIND(unicode); |
| 9139 | data = PyUnicode_DATA(unicode); |
| 9140 | |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 9141 | for (i=0; i < length; ) { |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 9142 | PyObject *exc; |
| 9143 | Py_UCS4 ch; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9144 | int decimal; |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 9145 | Py_ssize_t startpos; |
| 9146 | |
| 9147 | ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9148 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9149 | if (Py_UNICODE_ISSPACE(ch)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9150 | *output++ = ' '; |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 9151 | i++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9152 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9153 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9154 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 9155 | if (decimal >= 0) { |
| 9156 | *output++ = '0' + decimal; |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 9157 | i++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9158 | continue; |
| 9159 | } |
| 9160 | if (0 < ch && ch < 256) { |
| 9161 | *output++ = (char)ch; |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 9162 | i++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9163 | continue; |
| 9164 | } |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 9165 | |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 9166 | startpos = i; |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 9167 | exc = NULL; |
| 9168 | raise_encode_exception(&exc, "decimal", unicode, |
| 9169 | startpos, startpos+1, |
| 9170 | "invalid decimal Unicode string"); |
| 9171 | Py_XDECREF(exc); |
| 9172 | Py_DECREF(unicode); |
| 9173 | return -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 9174 | } |
| 9175 | /* 0-terminate the output string */ |
| 9176 | *output++ = '\0'; |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 9177 | Py_DECREF(unicode); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 9178 | return 0; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 9179 | } |
| 9180 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9181 | /* --- Helpers ------------------------------------------------------------ */ |
| 9182 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9183 | static Py_ssize_t |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 9184 | any_find_slice(int direction, PyObject* s1, PyObject* s2, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9185 | Py_ssize_t start, |
| 9186 | Py_ssize_t end) |
| 9187 | { |
| 9188 | int kind1, kind2, kind; |
| 9189 | void *buf1, *buf2; |
| 9190 | Py_ssize_t len1, len2, result; |
| 9191 | |
| 9192 | kind1 = PyUnicode_KIND(s1); |
| 9193 | kind2 = PyUnicode_KIND(s2); |
| 9194 | kind = kind1 > kind2 ? kind1 : kind2; |
| 9195 | buf1 = PyUnicode_DATA(s1); |
| 9196 | buf2 = PyUnicode_DATA(s2); |
| 9197 | if (kind1 != kind) |
| 9198 | buf1 = _PyUnicode_AsKind(s1, kind); |
| 9199 | if (!buf1) |
| 9200 | return -2; |
| 9201 | if (kind2 != kind) |
| 9202 | buf2 = _PyUnicode_AsKind(s2, kind); |
| 9203 | if (!buf2) { |
| 9204 | if (kind1 != kind) PyMem_Free(buf1); |
| 9205 | return -2; |
| 9206 | } |
| 9207 | len1 = PyUnicode_GET_LENGTH(s1); |
| 9208 | len2 = PyUnicode_GET_LENGTH(s2); |
| 9209 | |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 9210 | if (direction > 0) { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9211 | switch (kind) { |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 9212 | case PyUnicode_1BYTE_KIND: |
| 9213 | if (PyUnicode_IS_ASCII(s1) && PyUnicode_IS_ASCII(s2)) |
| 9214 | result = asciilib_find_slice(buf1, len1, buf2, len2, start, end); |
| 9215 | else |
| 9216 | result = ucs1lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 9217 | break; |
| 9218 | case PyUnicode_2BYTE_KIND: |
| 9219 | result = ucs2lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 9220 | break; |
| 9221 | case PyUnicode_4BYTE_KIND: |
| 9222 | result = ucs4lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 9223 | break; |
| 9224 | default: |
| 9225 | assert(0); result = -2; |
| 9226 | } |
| 9227 | } |
| 9228 | else { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9229 | switch (kind) { |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 9230 | case PyUnicode_1BYTE_KIND: |
| 9231 | if (PyUnicode_IS_ASCII(s1) && PyUnicode_IS_ASCII(s2)) |
| 9232 | result = asciilib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 9233 | else |
| 9234 | result = ucs1lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 9235 | break; |
| 9236 | case PyUnicode_2BYTE_KIND: |
| 9237 | result = ucs2lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 9238 | break; |
| 9239 | case PyUnicode_4BYTE_KIND: |
| 9240 | result = ucs4lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 9241 | break; |
| 9242 | default: |
| 9243 | assert(0); result = -2; |
| 9244 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9245 | } |
| 9246 | |
| 9247 | if (kind1 != kind) |
| 9248 | PyMem_Free(buf1); |
| 9249 | if (kind2 != kind) |
| 9250 | PyMem_Free(buf2); |
| 9251 | |
| 9252 | return result; |
| 9253 | } |
| 9254 | |
| 9255 | Py_ssize_t |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9256 | _PyUnicode_InsertThousandsGrouping( |
| 9257 | PyObject *unicode, Py_ssize_t index, |
| 9258 | Py_ssize_t n_buffer, |
| 9259 | void *digits, Py_ssize_t n_digits, |
| 9260 | Py_ssize_t min_width, |
| 9261 | const char *grouping, PyObject *thousands_sep, |
| 9262 | Py_UCS4 *maxchar) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9263 | { |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9264 | unsigned int kind, thousands_sep_kind; |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9265 | char *data, *thousands_sep_data; |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9266 | Py_ssize_t thousands_sep_len; |
| 9267 | Py_ssize_t len; |
| 9268 | |
| 9269 | if (unicode != NULL) { |
| 9270 | kind = PyUnicode_KIND(unicode); |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9271 | data = (char *) PyUnicode_DATA(unicode) + index * kind; |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9272 | } |
| 9273 | else { |
| 9274 | kind = PyUnicode_1BYTE_KIND; |
| 9275 | data = NULL; |
| 9276 | } |
| 9277 | thousands_sep_kind = PyUnicode_KIND(thousands_sep); |
| 9278 | thousands_sep_data = PyUnicode_DATA(thousands_sep); |
| 9279 | thousands_sep_len = PyUnicode_GET_LENGTH(thousands_sep); |
| 9280 | if (unicode != NULL && thousands_sep_kind != kind) { |
Victor Stinner | 90f50d4 | 2012-02-24 01:44:47 +0100 | [diff] [blame] | 9281 | if (thousands_sep_kind < kind) { |
| 9282 | thousands_sep_data = _PyUnicode_AsKind(thousands_sep, kind); |
| 9283 | if (!thousands_sep_data) |
| 9284 | return -1; |
| 9285 | } |
| 9286 | else { |
| 9287 | data = _PyUnicode_AsKind(unicode, thousands_sep_kind); |
| 9288 | if (!data) |
| 9289 | return -1; |
| 9290 | } |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9291 | } |
| 9292 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9293 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9294 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9295 | if (unicode != NULL && PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9296 | len = asciilib_InsertThousandsGrouping( |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9297 | (Py_UCS1 *) data, n_buffer, (Py_UCS1 *) digits, n_digits, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9298 | min_width, grouping, |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9299 | (Py_UCS1 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9300 | else |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9301 | len = ucs1lib_InsertThousandsGrouping( |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9302 | (Py_UCS1*)data, n_buffer, (Py_UCS1*)digits, n_digits, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9303 | min_width, grouping, |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9304 | (Py_UCS1 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9305 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9306 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9307 | len = ucs2lib_InsertThousandsGrouping( |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9308 | (Py_UCS2 *) data, n_buffer, (Py_UCS2 *) digits, n_digits, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9309 | min_width, grouping, |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9310 | (Py_UCS2 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9311 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9312 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9313 | len = ucs4lib_InsertThousandsGrouping( |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9314 | (Py_UCS4 *) data, n_buffer, (Py_UCS4 *) digits, n_digits, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9315 | min_width, grouping, |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9316 | (Py_UCS4 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9317 | break; |
| 9318 | default: |
| 9319 | assert(0); |
| 9320 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9321 | } |
Victor Stinner | 90f50d4 | 2012-02-24 01:44:47 +0100 | [diff] [blame] | 9322 | if (unicode != NULL && thousands_sep_kind != kind) { |
| 9323 | if (thousands_sep_kind < kind) |
| 9324 | PyMem_Free(thousands_sep_data); |
| 9325 | else |
| 9326 | PyMem_Free(data); |
| 9327 | } |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9328 | if (unicode == NULL) { |
| 9329 | *maxchar = 127; |
| 9330 | if (len != n_digits) { |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9331 | *maxchar = MAX_MAXCHAR(*maxchar, |
| 9332 | PyUnicode_MAX_CHAR_VALUE(thousands_sep)); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9333 | } |
| 9334 | } |
| 9335 | return len; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9336 | } |
| 9337 | |
| 9338 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9339 | /* helper macro to fixup start/end slice values */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9340 | #define ADJUST_INDICES(start, end, len) \ |
| 9341 | if (end > len) \ |
| 9342 | end = len; \ |
| 9343 | else if (end < 0) { \ |
| 9344 | end += len; \ |
| 9345 | if (end < 0) \ |
| 9346 | end = 0; \ |
| 9347 | } \ |
| 9348 | if (start < 0) { \ |
| 9349 | start += len; \ |
| 9350 | if (start < 0) \ |
| 9351 | start = 0; \ |
| 9352 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9353 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9354 | Py_ssize_t |
| 9355 | PyUnicode_Count(PyObject *str, |
| 9356 | PyObject *substr, |
| 9357 | Py_ssize_t start, |
| 9358 | Py_ssize_t end) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9359 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9360 | Py_ssize_t result; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9361 | PyObject* str_obj; |
| 9362 | PyObject* sub_obj; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9363 | int kind1, kind2, kind; |
| 9364 | void *buf1 = NULL, *buf2 = NULL; |
| 9365 | Py_ssize_t len1, len2; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9366 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9367 | str_obj = PyUnicode_FromObject(str); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9368 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9369 | return -1; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9370 | sub_obj = PyUnicode_FromObject(substr); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9371 | if (!sub_obj) { |
| 9372 | Py_DECREF(str_obj); |
| 9373 | return -1; |
| 9374 | } |
Benjamin Peterson | 4c13a4a | 2012-01-02 09:07:38 -0600 | [diff] [blame] | 9375 | if (PyUnicode_READY(sub_obj) == -1 || PyUnicode_READY(str_obj) == -1) { |
Benjamin Peterson | 5e458f5 | 2012-01-02 10:12:13 -0600 | [diff] [blame] | 9376 | Py_DECREF(sub_obj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9377 | Py_DECREF(str_obj); |
| 9378 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9379 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9380 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9381 | kind1 = PyUnicode_KIND(str_obj); |
| 9382 | kind2 = PyUnicode_KIND(sub_obj); |
| 9383 | kind = kind1 > kind2 ? kind1 : kind2; |
| 9384 | buf1 = PyUnicode_DATA(str_obj); |
| 9385 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9386 | buf1 = _PyUnicode_AsKind(str_obj, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9387 | if (!buf1) |
| 9388 | goto onError; |
| 9389 | buf2 = PyUnicode_DATA(sub_obj); |
| 9390 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9391 | buf2 = _PyUnicode_AsKind(sub_obj, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9392 | if (!buf2) |
| 9393 | goto onError; |
| 9394 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 9395 | len2 = PyUnicode_GET_LENGTH(sub_obj); |
| 9396 | |
| 9397 | ADJUST_INDICES(start, end, len1); |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9398 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9399 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9400 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sub_obj)) |
| 9401 | result = asciilib_count( |
| 9402 | ((Py_UCS1*)buf1) + start, end - start, |
| 9403 | buf2, len2, PY_SSIZE_T_MAX |
| 9404 | ); |
| 9405 | else |
| 9406 | result = ucs1lib_count( |
| 9407 | ((Py_UCS1*)buf1) + start, end - start, |
| 9408 | buf2, len2, PY_SSIZE_T_MAX |
| 9409 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9410 | break; |
| 9411 | case PyUnicode_2BYTE_KIND: |
| 9412 | result = ucs2lib_count( |
| 9413 | ((Py_UCS2*)buf1) + start, end - start, |
| 9414 | buf2, len2, PY_SSIZE_T_MAX |
| 9415 | ); |
| 9416 | break; |
| 9417 | case PyUnicode_4BYTE_KIND: |
| 9418 | result = ucs4lib_count( |
| 9419 | ((Py_UCS4*)buf1) + start, end - start, |
| 9420 | buf2, len2, PY_SSIZE_T_MAX |
| 9421 | ); |
| 9422 | break; |
| 9423 | default: |
| 9424 | assert(0); result = 0; |
| 9425 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9426 | |
| 9427 | Py_DECREF(sub_obj); |
| 9428 | Py_DECREF(str_obj); |
| 9429 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9430 | if (kind1 != kind) |
| 9431 | PyMem_Free(buf1); |
| 9432 | if (kind2 != kind) |
| 9433 | PyMem_Free(buf2); |
| 9434 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9435 | return result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9436 | onError: |
| 9437 | Py_DECREF(sub_obj); |
| 9438 | Py_DECREF(str_obj); |
| 9439 | if (kind1 != kind && buf1) |
| 9440 | PyMem_Free(buf1); |
| 9441 | if (kind2 != kind && buf2) |
| 9442 | PyMem_Free(buf2); |
| 9443 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9444 | } |
| 9445 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9446 | Py_ssize_t |
| 9447 | PyUnicode_Find(PyObject *str, |
| 9448 | PyObject *sub, |
| 9449 | Py_ssize_t start, |
| 9450 | Py_ssize_t end, |
| 9451 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9452 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9453 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9454 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9455 | str = PyUnicode_FromObject(str); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9456 | if (!str) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9457 | return -2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9458 | sub = PyUnicode_FromObject(sub); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9459 | if (!sub) { |
| 9460 | Py_DECREF(str); |
| 9461 | return -2; |
| 9462 | } |
| 9463 | if (PyUnicode_READY(sub) == -1 || PyUnicode_READY(str) == -1) { |
| 9464 | Py_DECREF(sub); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9465 | Py_DECREF(str); |
| 9466 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9467 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9468 | |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 9469 | result = any_find_slice(direction, |
| 9470 | str, sub, start, end |
| 9471 | ); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9472 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9473 | Py_DECREF(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9474 | Py_DECREF(sub); |
| 9475 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9476 | return result; |
| 9477 | } |
| 9478 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9479 | Py_ssize_t |
| 9480 | PyUnicode_FindChar(PyObject *str, Py_UCS4 ch, |
| 9481 | Py_ssize_t start, Py_ssize_t end, |
| 9482 | int direction) |
| 9483 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9484 | int kind; |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 9485 | Py_ssize_t result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9486 | if (PyUnicode_READY(str) == -1) |
| 9487 | return -2; |
Victor Stinner | 267aa24 | 2011-10-02 01:08:37 +0200 | [diff] [blame] | 9488 | if (start < 0 || end < 0) { |
| 9489 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 9490 | return -2; |
| 9491 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9492 | if (end > PyUnicode_GET_LENGTH(str)) |
| 9493 | end = PyUnicode_GET_LENGTH(str); |
| 9494 | kind = PyUnicode_KIND(str); |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 9495 | result = findchar(PyUnicode_1BYTE_DATA(str) + kind*start, |
| 9496 | kind, end-start, ch, direction); |
| 9497 | if (result == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9498 | return -1; |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 9499 | else |
| 9500 | return start + result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9501 | } |
| 9502 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9503 | static int |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9504 | tailmatch(PyObject *self, |
| 9505 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9506 | Py_ssize_t start, |
| 9507 | Py_ssize_t end, |
| 9508 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9509 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9510 | int kind_self; |
| 9511 | int kind_sub; |
| 9512 | void *data_self; |
| 9513 | void *data_sub; |
| 9514 | Py_ssize_t offset; |
| 9515 | Py_ssize_t i; |
| 9516 | Py_ssize_t end_sub; |
| 9517 | |
| 9518 | if (PyUnicode_READY(self) == -1 || |
| 9519 | PyUnicode_READY(substring) == -1) |
| 9520 | return 0; |
| 9521 | |
| 9522 | if (PyUnicode_GET_LENGTH(substring) == 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9523 | return 1; |
| 9524 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9525 | ADJUST_INDICES(start, end, PyUnicode_GET_LENGTH(self)); |
| 9526 | end -= PyUnicode_GET_LENGTH(substring); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9527 | if (end < start) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9528 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9529 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9530 | kind_self = PyUnicode_KIND(self); |
| 9531 | data_self = PyUnicode_DATA(self); |
| 9532 | kind_sub = PyUnicode_KIND(substring); |
| 9533 | data_sub = PyUnicode_DATA(substring); |
| 9534 | end_sub = PyUnicode_GET_LENGTH(substring) - 1; |
| 9535 | |
| 9536 | if (direction > 0) |
| 9537 | offset = end; |
| 9538 | else |
| 9539 | offset = start; |
| 9540 | |
| 9541 | if (PyUnicode_READ(kind_self, data_self, offset) == |
| 9542 | PyUnicode_READ(kind_sub, data_sub, 0) && |
| 9543 | PyUnicode_READ(kind_self, data_self, offset + end_sub) == |
| 9544 | PyUnicode_READ(kind_sub, data_sub, end_sub)) { |
| 9545 | /* If both are of the same kind, memcmp is sufficient */ |
| 9546 | if (kind_self == kind_sub) { |
| 9547 | return ! memcmp((char *)data_self + |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9548 | (offset * PyUnicode_KIND(substring)), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9549 | data_sub, |
| 9550 | PyUnicode_GET_LENGTH(substring) * |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9551 | PyUnicode_KIND(substring)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9552 | } |
| 9553 | /* otherwise we have to compare each character by first accesing it */ |
| 9554 | else { |
| 9555 | /* We do not need to compare 0 and len(substring)-1 because |
| 9556 | the if statement above ensured already that they are equal |
| 9557 | when we end up here. */ |
| 9558 | // TODO: honor direction and do a forward or backwards search |
| 9559 | for (i = 1; i < end_sub; ++i) { |
| 9560 | if (PyUnicode_READ(kind_self, data_self, offset + i) != |
| 9561 | PyUnicode_READ(kind_sub, data_sub, i)) |
| 9562 | return 0; |
| 9563 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9564 | return 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9565 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9566 | } |
| 9567 | |
| 9568 | return 0; |
| 9569 | } |
| 9570 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9571 | Py_ssize_t |
| 9572 | PyUnicode_Tailmatch(PyObject *str, |
| 9573 | PyObject *substr, |
| 9574 | Py_ssize_t start, |
| 9575 | Py_ssize_t end, |
| 9576 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9577 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9578 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9579 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9580 | str = PyUnicode_FromObject(str); |
| 9581 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9582 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9583 | substr = PyUnicode_FromObject(substr); |
| 9584 | if (substr == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9585 | Py_DECREF(str); |
| 9586 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9587 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9588 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9589 | result = tailmatch(str, substr, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9590 | start, end, direction); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9591 | Py_DECREF(str); |
| 9592 | Py_DECREF(substr); |
| 9593 | return result; |
| 9594 | } |
| 9595 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9596 | /* Apply fixfct filter to the Unicode object self and return a |
| 9597 | reference to the modified object */ |
| 9598 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9599 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9600 | fixup(PyObject *self, |
| 9601 | Py_UCS4 (*fixfct)(PyObject *s)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9602 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9603 | PyObject *u; |
| 9604 | Py_UCS4 maxchar_old, maxchar_new = 0; |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9605 | PyObject *v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9606 | |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 9607 | u = _PyUnicode_Copy(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9608 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9609 | return NULL; |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 9610 | maxchar_old = PyUnicode_MAX_CHAR_VALUE(u); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9611 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9612 | /* fix functions return the new maximum character in a string, |
| 9613 | if the kind of the resulting unicode object does not change, |
| 9614 | everything is fine. Otherwise we need to change the string kind |
| 9615 | and re-run the fix function. */ |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9616 | maxchar_new = fixfct(u); |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9617 | |
| 9618 | if (maxchar_new == 0) { |
| 9619 | /* no changes */; |
| 9620 | if (PyUnicode_CheckExact(self)) { |
| 9621 | Py_DECREF(u); |
| 9622 | Py_INCREF(self); |
| 9623 | return self; |
| 9624 | } |
| 9625 | else |
| 9626 | return u; |
| 9627 | } |
| 9628 | |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9629 | maxchar_new = align_maxchar(maxchar_new); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9630 | |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9631 | if (maxchar_new == maxchar_old) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9632 | return u; |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9633 | |
| 9634 | /* In case the maximum character changed, we need to |
| 9635 | convert the string to the new category. */ |
| 9636 | v = PyUnicode_New(PyUnicode_GET_LENGTH(self), maxchar_new); |
| 9637 | if (v == NULL) { |
| 9638 | Py_DECREF(u); |
| 9639 | return NULL; |
| 9640 | } |
| 9641 | if (maxchar_new > maxchar_old) { |
| 9642 | /* If the maxchar increased so that the kind changed, not all |
| 9643 | characters are representable anymore and we need to fix the |
| 9644 | string again. This only happens in very few cases. */ |
| 9645 | copy_characters(v, 0, self, 0, PyUnicode_GET_LENGTH(self)); |
| 9646 | maxchar_old = fixfct(v); |
| 9647 | assert(maxchar_old > 0 && maxchar_old <= maxchar_new); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9648 | } |
| 9649 | else { |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9650 | copy_characters(v, 0, u, 0, PyUnicode_GET_LENGTH(self)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9651 | } |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9652 | Py_DECREF(u); |
| 9653 | assert(_PyUnicode_CheckConsistency(v, 1)); |
| 9654 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9655 | } |
| 9656 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9657 | static PyObject * |
| 9658 | ascii_upper_or_lower(PyObject *self, int lower) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9659 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9660 | Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 9661 | char *resdata, *data = PyUnicode_DATA(self); |
| 9662 | PyObject *res; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9663 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9664 | res = PyUnicode_New(len, 127); |
| 9665 | if (res == NULL) |
| 9666 | return NULL; |
| 9667 | resdata = PyUnicode_DATA(res); |
| 9668 | if (lower) |
| 9669 | _Py_bytes_lower(resdata, data, len); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9670 | else |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9671 | _Py_bytes_upper(resdata, data, len); |
| 9672 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9673 | } |
| 9674 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9675 | static Py_UCS4 |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9676 | handle_capital_sigma(int kind, void *data, Py_ssize_t length, Py_ssize_t i) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9677 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9678 | Py_ssize_t j; |
| 9679 | int final_sigma; |
| 9680 | Py_UCS4 c; |
| 9681 | /* U+03A3 is in the Final_Sigma context when, it is found like this: |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9682 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9683 | \p{cased}\p{case-ignorable}*U+03A3!(\p{case-ignorable}*\p{cased}) |
| 9684 | |
| 9685 | where ! is a negation and \p{xxx} is a character with property xxx. |
| 9686 | */ |
| 9687 | for (j = i - 1; j >= 0; j--) { |
| 9688 | c = PyUnicode_READ(kind, data, j); |
| 9689 | if (!_PyUnicode_IsCaseIgnorable(c)) |
| 9690 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9691 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9692 | final_sigma = j >= 0 && _PyUnicode_IsCased(c); |
| 9693 | if (final_sigma) { |
| 9694 | for (j = i + 1; j < length; j++) { |
| 9695 | c = PyUnicode_READ(kind, data, j); |
| 9696 | if (!_PyUnicode_IsCaseIgnorable(c)) |
| 9697 | break; |
| 9698 | } |
| 9699 | final_sigma = j == length || !_PyUnicode_IsCased(c); |
| 9700 | } |
| 9701 | return (final_sigma) ? 0x3C2 : 0x3C3; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9702 | } |
| 9703 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9704 | static int |
| 9705 | lower_ucs4(int kind, void *data, Py_ssize_t length, Py_ssize_t i, |
| 9706 | Py_UCS4 c, Py_UCS4 *mapped) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9707 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9708 | /* Obscure special case. */ |
| 9709 | if (c == 0x3A3) { |
| 9710 | mapped[0] = handle_capital_sigma(kind, data, length, i); |
| 9711 | return 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9712 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9713 | return _PyUnicode_ToLowerFull(c, mapped); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9714 | } |
| 9715 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9716 | static Py_ssize_t |
| 9717 | do_capitalize(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9718 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9719 | Py_ssize_t i, k = 0; |
| 9720 | int n_res, j; |
| 9721 | Py_UCS4 c, mapped[3]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9722 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9723 | c = PyUnicode_READ(kind, data, 0); |
| 9724 | n_res = _PyUnicode_ToUpperFull(c, mapped); |
| 9725 | for (j = 0; j < n_res; j++) { |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9726 | *maxchar = MAX_MAXCHAR(*maxchar, mapped[j]); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9727 | res[k++] = mapped[j]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9728 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9729 | for (i = 1; i < length; i++) { |
| 9730 | c = PyUnicode_READ(kind, data, i); |
| 9731 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9732 | for (j = 0; j < n_res; j++) { |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9733 | *maxchar = MAX_MAXCHAR(*maxchar, mapped[j]); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9734 | res[k++] = mapped[j]; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9735 | } |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9736 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9737 | return k; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9738 | } |
| 9739 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9740 | static Py_ssize_t |
| 9741 | do_swapcase(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) { |
| 9742 | Py_ssize_t i, k = 0; |
| 9743 | |
| 9744 | for (i = 0; i < length; i++) { |
| 9745 | Py_UCS4 c = PyUnicode_READ(kind, data, i), mapped[3]; |
| 9746 | int n_res, j; |
| 9747 | if (Py_UNICODE_ISUPPER(c)) { |
| 9748 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9749 | } |
| 9750 | else if (Py_UNICODE_ISLOWER(c)) { |
| 9751 | n_res = _PyUnicode_ToUpperFull(c, mapped); |
| 9752 | } |
| 9753 | else { |
| 9754 | n_res = 1; |
| 9755 | mapped[0] = c; |
| 9756 | } |
| 9757 | for (j = 0; j < n_res; j++) { |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9758 | *maxchar = MAX_MAXCHAR(*maxchar, mapped[j]); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9759 | res[k++] = mapped[j]; |
| 9760 | } |
| 9761 | } |
| 9762 | return k; |
| 9763 | } |
| 9764 | |
| 9765 | static Py_ssize_t |
| 9766 | do_upper_or_lower(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, |
| 9767 | Py_UCS4 *maxchar, int lower) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9768 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9769 | Py_ssize_t i, k = 0; |
| 9770 | |
| 9771 | for (i = 0; i < length; i++) { |
| 9772 | Py_UCS4 c = PyUnicode_READ(kind, data, i), mapped[3]; |
| 9773 | int n_res, j; |
| 9774 | if (lower) |
| 9775 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9776 | else |
| 9777 | n_res = _PyUnicode_ToUpperFull(c, mapped); |
| 9778 | for (j = 0; j < n_res; j++) { |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9779 | *maxchar = MAX_MAXCHAR(*maxchar, mapped[j]); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9780 | res[k++] = mapped[j]; |
| 9781 | } |
| 9782 | } |
| 9783 | return k; |
| 9784 | } |
| 9785 | |
| 9786 | static Py_ssize_t |
| 9787 | do_upper(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9788 | { |
| 9789 | return do_upper_or_lower(kind, data, length, res, maxchar, 0); |
| 9790 | } |
| 9791 | |
| 9792 | static Py_ssize_t |
| 9793 | do_lower(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9794 | { |
| 9795 | return do_upper_or_lower(kind, data, length, res, maxchar, 1); |
| 9796 | } |
| 9797 | |
Benjamin Peterson | e51757f | 2012-01-12 21:10:29 -0500 | [diff] [blame] | 9798 | static Py_ssize_t |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 9799 | do_casefold(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9800 | { |
| 9801 | Py_ssize_t i, k = 0; |
| 9802 | |
| 9803 | for (i = 0; i < length; i++) { |
| 9804 | Py_UCS4 c = PyUnicode_READ(kind, data, i); |
| 9805 | Py_UCS4 mapped[3]; |
| 9806 | int j, n_res = _PyUnicode_ToFoldedFull(c, mapped); |
| 9807 | for (j = 0; j < n_res; j++) { |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9808 | *maxchar = MAX_MAXCHAR(*maxchar, mapped[j]); |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 9809 | res[k++] = mapped[j]; |
| 9810 | } |
| 9811 | } |
| 9812 | return k; |
| 9813 | } |
| 9814 | |
| 9815 | static Py_ssize_t |
Benjamin Peterson | e51757f | 2012-01-12 21:10:29 -0500 | [diff] [blame] | 9816 | do_title(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9817 | { |
| 9818 | Py_ssize_t i, k = 0; |
| 9819 | int previous_is_cased; |
| 9820 | |
| 9821 | previous_is_cased = 0; |
| 9822 | for (i = 0; i < length; i++) { |
| 9823 | const Py_UCS4 c = PyUnicode_READ(kind, data, i); |
| 9824 | Py_UCS4 mapped[3]; |
| 9825 | int n_res, j; |
| 9826 | |
| 9827 | if (previous_is_cased) |
| 9828 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9829 | else |
| 9830 | n_res = _PyUnicode_ToTitleFull(c, mapped); |
| 9831 | |
| 9832 | for (j = 0; j < n_res; j++) { |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9833 | *maxchar = MAX_MAXCHAR(*maxchar, mapped[j]); |
Benjamin Peterson | e51757f | 2012-01-12 21:10:29 -0500 | [diff] [blame] | 9834 | res[k++] = mapped[j]; |
| 9835 | } |
| 9836 | |
| 9837 | previous_is_cased = _PyUnicode_IsCased(c); |
| 9838 | } |
| 9839 | return k; |
| 9840 | } |
| 9841 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9842 | static PyObject * |
| 9843 | case_operation(PyObject *self, |
| 9844 | Py_ssize_t (*perform)(int, void *, Py_ssize_t, Py_UCS4 *, Py_UCS4 *)) |
| 9845 | { |
| 9846 | PyObject *res = NULL; |
| 9847 | Py_ssize_t length, newlength = 0; |
| 9848 | int kind, outkind; |
| 9849 | void *data, *outdata; |
| 9850 | Py_UCS4 maxchar = 0, *tmp, *tmpend; |
| 9851 | |
Benjamin Peterson | eea4846 | 2012-01-16 14:28:50 -0500 | [diff] [blame] | 9852 | assert(PyUnicode_IS_READY(self)); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9853 | |
| 9854 | kind = PyUnicode_KIND(self); |
| 9855 | data = PyUnicode_DATA(self); |
| 9856 | length = PyUnicode_GET_LENGTH(self); |
| 9857 | tmp = PyMem_MALLOC(sizeof(Py_UCS4) * 3 * length); |
| 9858 | if (tmp == NULL) |
| 9859 | return PyErr_NoMemory(); |
| 9860 | newlength = perform(kind, data, length, tmp, &maxchar); |
| 9861 | res = PyUnicode_New(newlength, maxchar); |
| 9862 | if (res == NULL) |
| 9863 | goto leave; |
| 9864 | tmpend = tmp + newlength; |
| 9865 | outdata = PyUnicode_DATA(res); |
| 9866 | outkind = PyUnicode_KIND(res); |
| 9867 | switch (outkind) { |
| 9868 | case PyUnicode_1BYTE_KIND: |
| 9869 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS1, tmp, tmpend, outdata); |
| 9870 | break; |
| 9871 | case PyUnicode_2BYTE_KIND: |
| 9872 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS2, tmp, tmpend, outdata); |
| 9873 | break; |
| 9874 | case PyUnicode_4BYTE_KIND: |
| 9875 | memcpy(outdata, tmp, sizeof(Py_UCS4) * newlength); |
| 9876 | break; |
| 9877 | default: |
| 9878 | assert(0); |
| 9879 | break; |
| 9880 | } |
| 9881 | leave: |
| 9882 | PyMem_FREE(tmp); |
| 9883 | return res; |
| 9884 | } |
| 9885 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9886 | PyObject * |
| 9887 | PyUnicode_Join(PyObject *separator, PyObject *seq) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9888 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9889 | PyObject *sep = NULL; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9890 | Py_ssize_t seplen; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9891 | PyObject *res = NULL; /* the result */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9892 | PyObject *fseq; /* PySequence_Fast(seq) */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9893 | Py_ssize_t seqlen; /* len(fseq) -- number of items in sequence */ |
| 9894 | PyObject **items; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9895 | PyObject *item; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9896 | Py_ssize_t sz, i, res_offset; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9897 | Py_UCS4 maxchar; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9898 | Py_UCS4 item_maxchar; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9899 | int use_memcpy; |
| 9900 | unsigned char *res_data = NULL, *sep_data = NULL; |
| 9901 | PyObject *last_obj; |
| 9902 | unsigned int kind = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9903 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9904 | fseq = PySequence_Fast(seq, ""); |
| 9905 | if (fseq == NULL) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9906 | return NULL; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9907 | } |
| 9908 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9909 | /* NOTE: the following code can't call back into Python code, |
| 9910 | * so we are sure that fseq won't be mutated. |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 9911 | */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9912 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9913 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 9914 | /* If empty sequence, return u"". */ |
| 9915 | if (seqlen == 0) { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9916 | Py_DECREF(fseq); |
| 9917 | Py_INCREF(unicode_empty); |
| 9918 | res = unicode_empty; |
| 9919 | return res; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9920 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9921 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9922 | /* If singleton sequence with an exact Unicode, return that. */ |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9923 | last_obj = NULL; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9924 | items = PySequence_Fast_ITEMS(fseq); |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9925 | if (seqlen == 1) { |
| 9926 | if (PyUnicode_CheckExact(items[0])) { |
| 9927 | res = items[0]; |
| 9928 | Py_INCREF(res); |
| 9929 | Py_DECREF(fseq); |
| 9930 | return res; |
| 9931 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9932 | seplen = 0; |
Victor Stinner | c6f0df7 | 2011-10-06 15:58:54 +0200 | [diff] [blame] | 9933 | maxchar = 0; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9934 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9935 | else { |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9936 | /* Set up sep and seplen */ |
| 9937 | if (separator == NULL) { |
| 9938 | /* fall back to a blank space separator */ |
| 9939 | sep = PyUnicode_FromOrdinal(' '); |
| 9940 | if (!sep) |
| 9941 | goto onError; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9942 | seplen = 1; |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9943 | maxchar = 32; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9944 | } |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9945 | else { |
| 9946 | if (!PyUnicode_Check(separator)) { |
| 9947 | PyErr_Format(PyExc_TypeError, |
| 9948 | "separator: expected str instance," |
| 9949 | " %.80s found", |
| 9950 | Py_TYPE(separator)->tp_name); |
| 9951 | goto onError; |
| 9952 | } |
| 9953 | if (PyUnicode_READY(separator)) |
| 9954 | goto onError; |
| 9955 | sep = separator; |
| 9956 | seplen = PyUnicode_GET_LENGTH(separator); |
| 9957 | maxchar = PyUnicode_MAX_CHAR_VALUE(separator); |
| 9958 | /* inc refcount to keep this code path symmetric with the |
| 9959 | above case of a blank separator */ |
| 9960 | Py_INCREF(sep); |
| 9961 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9962 | last_obj = sep; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9963 | } |
| 9964 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9965 | /* There are at least two things to join, or else we have a subclass |
| 9966 | * of str in the sequence. |
| 9967 | * Do a pre-pass to figure out the total amount of space we'll |
| 9968 | * need (sz), and see whether all argument are strings. |
| 9969 | */ |
| 9970 | sz = 0; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9971 | #ifdef Py_DEBUG |
| 9972 | use_memcpy = 0; |
| 9973 | #else |
| 9974 | use_memcpy = 1; |
| 9975 | #endif |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9976 | for (i = 0; i < seqlen; i++) { |
| 9977 | const Py_ssize_t old_sz = sz; |
| 9978 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9979 | if (!PyUnicode_Check(item)) { |
| 9980 | PyErr_Format(PyExc_TypeError, |
| 9981 | "sequence item %zd: expected str instance," |
| 9982 | " %.80s found", |
| 9983 | i, Py_TYPE(item)->tp_name); |
| 9984 | goto onError; |
| 9985 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9986 | if (PyUnicode_READY(item) == -1) |
| 9987 | goto onError; |
| 9988 | sz += PyUnicode_GET_LENGTH(item); |
| 9989 | item_maxchar = PyUnicode_MAX_CHAR_VALUE(item); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9990 | maxchar = MAX_MAXCHAR(maxchar, item_maxchar); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9991 | if (i != 0) |
| 9992 | sz += seplen; |
| 9993 | if (sz < old_sz || sz > PY_SSIZE_T_MAX) { |
| 9994 | PyErr_SetString(PyExc_OverflowError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9995 | "join() result is too long for a Python string"); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9996 | goto onError; |
| 9997 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9998 | if (use_memcpy && last_obj != NULL) { |
| 9999 | if (PyUnicode_KIND(last_obj) != PyUnicode_KIND(item)) |
| 10000 | use_memcpy = 0; |
| 10001 | } |
| 10002 | last_obj = item; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 10003 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 10004 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10005 | res = PyUnicode_New(sz, maxchar); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 10006 | if (res == NULL) |
| 10007 | goto onError; |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 10008 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 10009 | /* Catenate everything. */ |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 10010 | #ifdef Py_DEBUG |
| 10011 | use_memcpy = 0; |
| 10012 | #else |
| 10013 | if (use_memcpy) { |
| 10014 | res_data = PyUnicode_1BYTE_DATA(res); |
| 10015 | kind = PyUnicode_KIND(res); |
| 10016 | if (seplen != 0) |
| 10017 | sep_data = PyUnicode_1BYTE_DATA(sep); |
| 10018 | } |
| 10019 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10020 | for (i = 0, res_offset = 0; i < seqlen; ++i) { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 10021 | Py_ssize_t itemlen; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 10022 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10023 | /* Copy item, and maybe the separator. */ |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 10024 | if (i && seplen != 0) { |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 10025 | if (use_memcpy) { |
| 10026 | Py_MEMCPY(res_data, |
| 10027 | sep_data, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10028 | kind * seplen); |
| 10029 | res_data += kind * seplen; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 10030 | } |
| 10031 | else { |
| 10032 | copy_characters(res, res_offset, sep, 0, seplen); |
| 10033 | res_offset += seplen; |
| 10034 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10035 | } |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 10036 | itemlen = PyUnicode_GET_LENGTH(item); |
| 10037 | if (itemlen != 0) { |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 10038 | if (use_memcpy) { |
| 10039 | Py_MEMCPY(res_data, |
| 10040 | PyUnicode_DATA(item), |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10041 | kind * itemlen); |
| 10042 | res_data += kind * itemlen; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 10043 | } |
| 10044 | else { |
| 10045 | copy_characters(res, res_offset, item, 0, itemlen); |
| 10046 | res_offset += itemlen; |
| 10047 | } |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 10048 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 10049 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 10050 | if (use_memcpy) |
| 10051 | assert(res_data == PyUnicode_1BYTE_DATA(res) |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10052 | + kind * PyUnicode_GET_LENGTH(res)); |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 10053 | else |
| 10054 | assert(res_offset == PyUnicode_GET_LENGTH(res)); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 10055 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 10056 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10057 | Py_XDECREF(sep); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10058 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10059 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10060 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10061 | onError: |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 10062 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10063 | Py_XDECREF(sep); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 10064 | Py_XDECREF(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10065 | return NULL; |
| 10066 | } |
| 10067 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10068 | #define FILL(kind, data, value, start, length) \ |
| 10069 | do { \ |
| 10070 | Py_ssize_t i_ = 0; \ |
| 10071 | assert(kind != PyUnicode_WCHAR_KIND); \ |
| 10072 | switch ((kind)) { \ |
| 10073 | case PyUnicode_1BYTE_KIND: { \ |
| 10074 | unsigned char * to_ = (unsigned char *)((data)) + (start); \ |
| 10075 | memset(to_, (unsigned char)value, length); \ |
| 10076 | break; \ |
| 10077 | } \ |
| 10078 | case PyUnicode_2BYTE_KIND: { \ |
| 10079 | Py_UCS2 * to_ = (Py_UCS2 *)((data)) + (start); \ |
| 10080 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 10081 | break; \ |
| 10082 | } \ |
Benjamin Peterson | e157cf1 | 2012-01-01 15:56:20 -0600 | [diff] [blame] | 10083 | case PyUnicode_4BYTE_KIND: { \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10084 | Py_UCS4 * to_ = (Py_UCS4 *)((data)) + (start); \ |
| 10085 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 10086 | break; \ |
Benjamin Peterson | e157cf1 | 2012-01-01 15:56:20 -0600 | [diff] [blame] | 10087 | default: assert(0); \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10088 | } \ |
| 10089 | } \ |
| 10090 | } while (0) |
| 10091 | |
Victor Stinner | 3fe5531 | 2012-01-04 00:33:50 +0100 | [diff] [blame] | 10092 | Py_ssize_t |
| 10093 | PyUnicode_Fill(PyObject *unicode, Py_ssize_t start, Py_ssize_t length, |
| 10094 | Py_UCS4 fill_char) |
| 10095 | { |
| 10096 | Py_ssize_t maxlen; |
| 10097 | enum PyUnicode_Kind kind; |
| 10098 | void *data; |
| 10099 | |
| 10100 | if (!PyUnicode_Check(unicode)) { |
| 10101 | PyErr_BadInternalCall(); |
| 10102 | return -1; |
| 10103 | } |
| 10104 | if (PyUnicode_READY(unicode) == -1) |
| 10105 | return -1; |
| 10106 | if (unicode_check_modifiable(unicode)) |
| 10107 | return -1; |
| 10108 | |
| 10109 | if (fill_char > PyUnicode_MAX_CHAR_VALUE(unicode)) { |
| 10110 | PyErr_SetString(PyExc_ValueError, |
| 10111 | "fill character is bigger than " |
| 10112 | "the string maximum character"); |
| 10113 | return -1; |
| 10114 | } |
| 10115 | |
| 10116 | maxlen = PyUnicode_GET_LENGTH(unicode) - start; |
| 10117 | length = Py_MIN(maxlen, length); |
| 10118 | if (length <= 0) |
| 10119 | return 0; |
| 10120 | |
| 10121 | kind = PyUnicode_KIND(unicode); |
| 10122 | data = PyUnicode_DATA(unicode); |
| 10123 | FILL(kind, data, fill_char, start, length); |
| 10124 | return length; |
| 10125 | } |
| 10126 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10127 | static PyObject * |
| 10128 | pad(PyObject *self, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10129 | Py_ssize_t left, |
| 10130 | Py_ssize_t right, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10131 | Py_UCS4 fill) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10132 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10133 | PyObject *u; |
| 10134 | Py_UCS4 maxchar; |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 10135 | int kind; |
| 10136 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10137 | |
| 10138 | if (left < 0) |
| 10139 | left = 0; |
| 10140 | if (right < 0) |
| 10141 | right = 0; |
| 10142 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10143 | if (left == 0 && right == 0) |
| 10144 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10145 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10146 | if (left > PY_SSIZE_T_MAX - _PyUnicode_LENGTH(self) || |
| 10147 | right > PY_SSIZE_T_MAX - (left + _PyUnicode_LENGTH(self))) { |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 10148 | PyErr_SetString(PyExc_OverflowError, "padded string is too long"); |
| 10149 | return NULL; |
| 10150 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10151 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 10152 | maxchar = MAX_MAXCHAR(maxchar, fill); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10153 | u = PyUnicode_New(left + _PyUnicode_LENGTH(self) + right, maxchar); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 10154 | if (!u) |
| 10155 | return NULL; |
| 10156 | |
| 10157 | kind = PyUnicode_KIND(u); |
| 10158 | data = PyUnicode_DATA(u); |
| 10159 | if (left) |
| 10160 | FILL(kind, data, fill, 0, left); |
| 10161 | if (right) |
| 10162 | FILL(kind, data, fill, left + _PyUnicode_LENGTH(self), right); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 10163 | copy_characters(u, left, self, 0, _PyUnicode_LENGTH(self)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10164 | assert(_PyUnicode_CheckConsistency(u, 1)); |
| 10165 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10166 | } |
| 10167 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10168 | PyObject * |
| 10169 | PyUnicode_Splitlines(PyObject *string, int keepends) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10170 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10171 | PyObject *list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10172 | |
| 10173 | string = PyUnicode_FromObject(string); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 10174 | if (string == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10175 | return NULL; |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 10176 | if (PyUnicode_READY(string) == -1) { |
| 10177 | Py_DECREF(string); |
| 10178 | return NULL; |
| 10179 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10180 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10181 | switch (PyUnicode_KIND(string)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10182 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10183 | if (PyUnicode_IS_ASCII(string)) |
| 10184 | list = asciilib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10185 | string, PyUnicode_1BYTE_DATA(string), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10186 | PyUnicode_GET_LENGTH(string), keepends); |
| 10187 | else |
| 10188 | list = ucs1lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10189 | string, PyUnicode_1BYTE_DATA(string), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10190 | PyUnicode_GET_LENGTH(string), keepends); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10191 | break; |
| 10192 | case PyUnicode_2BYTE_KIND: |
| 10193 | list = ucs2lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10194 | string, PyUnicode_2BYTE_DATA(string), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10195 | PyUnicode_GET_LENGTH(string), keepends); |
| 10196 | break; |
| 10197 | case PyUnicode_4BYTE_KIND: |
| 10198 | list = ucs4lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10199 | string, PyUnicode_4BYTE_DATA(string), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10200 | PyUnicode_GET_LENGTH(string), keepends); |
| 10201 | break; |
| 10202 | default: |
| 10203 | assert(0); |
| 10204 | list = 0; |
| 10205 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10206 | Py_DECREF(string); |
| 10207 | return list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10208 | } |
| 10209 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10210 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10211 | split(PyObject *self, |
| 10212 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10213 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10214 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10215 | int kind1, kind2, kind; |
| 10216 | void *buf1, *buf2; |
| 10217 | Py_ssize_t len1, len2; |
| 10218 | PyObject* out; |
| 10219 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10220 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10221 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10222 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10223 | if (PyUnicode_READY(self) == -1) |
| 10224 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10225 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10226 | if (substring == NULL) |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10227 | switch (PyUnicode_KIND(self)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10228 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10229 | if (PyUnicode_IS_ASCII(self)) |
| 10230 | return asciilib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10231 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10232 | PyUnicode_GET_LENGTH(self), maxcount |
| 10233 | ); |
| 10234 | else |
| 10235 | return ucs1lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10236 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10237 | PyUnicode_GET_LENGTH(self), maxcount |
| 10238 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10239 | case PyUnicode_2BYTE_KIND: |
| 10240 | return ucs2lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10241 | self, PyUnicode_2BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10242 | PyUnicode_GET_LENGTH(self), maxcount |
| 10243 | ); |
| 10244 | case PyUnicode_4BYTE_KIND: |
| 10245 | return ucs4lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10246 | self, PyUnicode_4BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10247 | PyUnicode_GET_LENGTH(self), maxcount |
| 10248 | ); |
| 10249 | default: |
| 10250 | assert(0); |
| 10251 | return NULL; |
| 10252 | } |
| 10253 | |
| 10254 | if (PyUnicode_READY(substring) == -1) |
| 10255 | return NULL; |
| 10256 | |
| 10257 | kind1 = PyUnicode_KIND(self); |
| 10258 | kind2 = PyUnicode_KIND(substring); |
| 10259 | kind = kind1 > kind2 ? kind1 : kind2; |
| 10260 | buf1 = PyUnicode_DATA(self); |
| 10261 | buf2 = PyUnicode_DATA(substring); |
| 10262 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10263 | buf1 = _PyUnicode_AsKind(self, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10264 | if (!buf1) |
| 10265 | return NULL; |
| 10266 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10267 | buf2 = _PyUnicode_AsKind(substring, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10268 | if (!buf2) { |
| 10269 | if (kind1 != kind) PyMem_Free(buf1); |
| 10270 | return NULL; |
| 10271 | } |
| 10272 | len1 = PyUnicode_GET_LENGTH(self); |
| 10273 | len2 = PyUnicode_GET_LENGTH(substring); |
| 10274 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10275 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10276 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10277 | if (PyUnicode_IS_ASCII(self) && PyUnicode_IS_ASCII(substring)) |
| 10278 | out = asciilib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10279 | self, buf1, len1, buf2, len2, maxcount); |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10280 | else |
| 10281 | out = ucs1lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10282 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10283 | break; |
| 10284 | case PyUnicode_2BYTE_KIND: |
| 10285 | out = ucs2lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10286 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10287 | break; |
| 10288 | case PyUnicode_4BYTE_KIND: |
| 10289 | out = ucs4lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10290 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10291 | break; |
| 10292 | default: |
| 10293 | out = NULL; |
| 10294 | } |
| 10295 | if (kind1 != kind) |
| 10296 | PyMem_Free(buf1); |
| 10297 | if (kind2 != kind) |
| 10298 | PyMem_Free(buf2); |
| 10299 | return out; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10300 | } |
| 10301 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10302 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10303 | rsplit(PyObject *self, |
| 10304 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10305 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10306 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10307 | int kind1, kind2, kind; |
| 10308 | void *buf1, *buf2; |
| 10309 | Py_ssize_t len1, len2; |
| 10310 | PyObject* out; |
| 10311 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10312 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10313 | maxcount = PY_SSIZE_T_MAX; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10314 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10315 | if (PyUnicode_READY(self) == -1) |
| 10316 | return NULL; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10317 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10318 | if (substring == NULL) |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10319 | switch (PyUnicode_KIND(self)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10320 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10321 | if (PyUnicode_IS_ASCII(self)) |
| 10322 | return asciilib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10323 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10324 | PyUnicode_GET_LENGTH(self), maxcount |
| 10325 | ); |
| 10326 | else |
| 10327 | return ucs1lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10328 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10329 | PyUnicode_GET_LENGTH(self), maxcount |
| 10330 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10331 | case PyUnicode_2BYTE_KIND: |
| 10332 | return ucs2lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10333 | self, PyUnicode_2BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10334 | PyUnicode_GET_LENGTH(self), maxcount |
| 10335 | ); |
| 10336 | case PyUnicode_4BYTE_KIND: |
| 10337 | return ucs4lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10338 | self, PyUnicode_4BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10339 | PyUnicode_GET_LENGTH(self), maxcount |
| 10340 | ); |
| 10341 | default: |
| 10342 | assert(0); |
| 10343 | return NULL; |
| 10344 | } |
| 10345 | |
| 10346 | if (PyUnicode_READY(substring) == -1) |
| 10347 | return NULL; |
| 10348 | |
| 10349 | kind1 = PyUnicode_KIND(self); |
| 10350 | kind2 = PyUnicode_KIND(substring); |
| 10351 | kind = kind1 > kind2 ? kind1 : kind2; |
| 10352 | buf1 = PyUnicode_DATA(self); |
| 10353 | buf2 = PyUnicode_DATA(substring); |
| 10354 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10355 | buf1 = _PyUnicode_AsKind(self, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10356 | if (!buf1) |
| 10357 | return NULL; |
| 10358 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10359 | buf2 = _PyUnicode_AsKind(substring, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10360 | if (!buf2) { |
| 10361 | if (kind1 != kind) PyMem_Free(buf1); |
| 10362 | return NULL; |
| 10363 | } |
| 10364 | len1 = PyUnicode_GET_LENGTH(self); |
| 10365 | len2 = PyUnicode_GET_LENGTH(substring); |
| 10366 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10367 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10368 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10369 | if (PyUnicode_IS_ASCII(self) && PyUnicode_IS_ASCII(substring)) |
| 10370 | out = asciilib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10371 | self, buf1, len1, buf2, len2, maxcount); |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10372 | else |
| 10373 | out = ucs1lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10374 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10375 | break; |
| 10376 | case PyUnicode_2BYTE_KIND: |
| 10377 | out = ucs2lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10378 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10379 | break; |
| 10380 | case PyUnicode_4BYTE_KIND: |
| 10381 | out = ucs4lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10382 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10383 | break; |
| 10384 | default: |
| 10385 | out = NULL; |
| 10386 | } |
| 10387 | if (kind1 != kind) |
| 10388 | PyMem_Free(buf1); |
| 10389 | if (kind2 != kind) |
| 10390 | PyMem_Free(buf2); |
| 10391 | return out; |
| 10392 | } |
| 10393 | |
| 10394 | static Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10395 | anylib_find(int kind, PyObject *str1, void *buf1, Py_ssize_t len1, |
| 10396 | 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] | 10397 | { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10398 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10399 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10400 | if (PyUnicode_IS_ASCII(str1) && PyUnicode_IS_ASCII(str2)) |
| 10401 | return asciilib_find(buf1, len1, buf2, len2, offset); |
| 10402 | else |
| 10403 | return ucs1lib_find(buf1, len1, buf2, len2, offset); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10404 | case PyUnicode_2BYTE_KIND: |
| 10405 | return ucs2lib_find(buf1, len1, buf2, len2, offset); |
| 10406 | case PyUnicode_4BYTE_KIND: |
| 10407 | return ucs4lib_find(buf1, len1, buf2, len2, offset); |
| 10408 | } |
| 10409 | assert(0); |
| 10410 | return -1; |
| 10411 | } |
| 10412 | |
| 10413 | static Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10414 | anylib_count(int kind, PyObject *sstr, void* sbuf, Py_ssize_t slen, |
| 10415 | 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] | 10416 | { |
Benjamin Peterson | c0b95d1 | 2011-12-20 17:24:05 -0600 | [diff] [blame] | 10417 | switch (kind) { |
| 10418 | case PyUnicode_1BYTE_KIND: |
| 10419 | if (PyUnicode_IS_ASCII(sstr) && PyUnicode_IS_ASCII(str1)) |
| 10420 | return asciilib_count(sbuf, slen, buf1, len1, maxcount); |
| 10421 | else |
| 10422 | return ucs1lib_count(sbuf, slen, buf1, len1, maxcount); |
| 10423 | case PyUnicode_2BYTE_KIND: |
| 10424 | return ucs2lib_count(sbuf, slen, buf1, len1, maxcount); |
| 10425 | case PyUnicode_4BYTE_KIND: |
| 10426 | return ucs4lib_count(sbuf, slen, buf1, len1, maxcount); |
| 10427 | } |
| 10428 | assert(0); |
| 10429 | return 0; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10430 | } |
| 10431 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10432 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10433 | replace(PyObject *self, PyObject *str1, |
| 10434 | PyObject *str2, Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10435 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10436 | PyObject *u; |
| 10437 | char *sbuf = PyUnicode_DATA(self); |
| 10438 | char *buf1 = PyUnicode_DATA(str1); |
| 10439 | char *buf2 = PyUnicode_DATA(str2); |
| 10440 | int srelease = 0, release1 = 0, release2 = 0; |
| 10441 | int skind = PyUnicode_KIND(self); |
| 10442 | int kind1 = PyUnicode_KIND(str1); |
| 10443 | int kind2 = PyUnicode_KIND(str2); |
| 10444 | Py_ssize_t slen = PyUnicode_GET_LENGTH(self); |
| 10445 | Py_ssize_t len1 = PyUnicode_GET_LENGTH(str1); |
| 10446 | Py_ssize_t len2 = PyUnicode_GET_LENGTH(str2); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10447 | int mayshrink; |
| 10448 | Py_UCS4 maxchar, maxchar_str2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10449 | |
| 10450 | if (maxcount < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10451 | maxcount = PY_SSIZE_T_MAX; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10452 | else if (maxcount == 0 || slen == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10453 | goto nothing; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10454 | |
Victor Stinner | 59de0ee | 2011-10-07 10:01:28 +0200 | [diff] [blame] | 10455 | if (str1 == str2) |
| 10456 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10457 | if (skind < kind1) |
| 10458 | /* substring too wide to be present */ |
| 10459 | goto nothing; |
| 10460 | |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10461 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
| 10462 | maxchar_str2 = PyUnicode_MAX_CHAR_VALUE(str2); |
| 10463 | /* Replacing str1 with str2 may cause a maxchar reduction in the |
| 10464 | result string. */ |
| 10465 | mayshrink = (maxchar_str2 < maxchar); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 10466 | maxchar = MAX_MAXCHAR(maxchar, maxchar_str2); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10467 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10468 | if (len1 == len2) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10469 | /* same length */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10470 | if (len1 == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10471 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10472 | if (len1 == 1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10473 | /* replace characters */ |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10474 | Py_UCS4 u1, u2; |
| 10475 | int rkind; |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10476 | Py_ssize_t index, pos; |
| 10477 | char *src; |
| 10478 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10479 | u1 = PyUnicode_READ_CHAR(str1, 0); |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10480 | pos = findchar(sbuf, PyUnicode_KIND(self), slen, u1, 1); |
| 10481 | if (pos < 0) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10482 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10483 | u2 = PyUnicode_READ_CHAR(str2, 0); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10484 | u = PyUnicode_New(slen, maxchar); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10485 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10486 | goto error; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 10487 | copy_characters(u, 0, self, 0, slen); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10488 | rkind = PyUnicode_KIND(u); |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10489 | |
| 10490 | PyUnicode_WRITE(rkind, PyUnicode_DATA(u), pos, u2); |
| 10491 | index = 0; |
| 10492 | src = sbuf; |
| 10493 | while (--maxcount) |
| 10494 | { |
| 10495 | pos++; |
| 10496 | src += pos * PyUnicode_KIND(self); |
| 10497 | slen -= pos; |
| 10498 | index += pos; |
| 10499 | pos = findchar(src, PyUnicode_KIND(self), slen, u1, 1); |
| 10500 | if (pos < 0) |
| 10501 | break; |
| 10502 | PyUnicode_WRITE(rkind, PyUnicode_DATA(u), index + pos, u2); |
| 10503 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10504 | } |
| 10505 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10506 | int rkind = skind; |
| 10507 | char *res; |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10508 | Py_ssize_t i; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10509 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10510 | if (kind1 < rkind) { |
| 10511 | /* widen substring */ |
| 10512 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10513 | if (!buf1) goto error; |
| 10514 | release1 = 1; |
| 10515 | } |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10516 | i = anylib_find(rkind, self, sbuf, slen, str1, buf1, len1, 0); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10517 | if (i < 0) |
| 10518 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10519 | if (rkind > kind2) { |
| 10520 | /* widen replacement */ |
| 10521 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 10522 | if (!buf2) goto error; |
| 10523 | release2 = 1; |
| 10524 | } |
| 10525 | else if (rkind < kind2) { |
| 10526 | /* widen self and buf1 */ |
| 10527 | rkind = kind2; |
| 10528 | if (release1) PyMem_Free(buf1); |
| 10529 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 10530 | if (!sbuf) goto error; |
| 10531 | srelease = 1; |
| 10532 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10533 | if (!buf1) goto error; |
| 10534 | release1 = 1; |
| 10535 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10536 | u = PyUnicode_New(slen, maxchar); |
| 10537 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10538 | goto error; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10539 | assert(PyUnicode_KIND(u) == rkind); |
| 10540 | res = PyUnicode_DATA(u); |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10541 | |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10542 | memcpy(res, sbuf, rkind * slen); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10543 | /* change everything in-place, starting with this one */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10544 | memcpy(res + rkind * i, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10545 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10546 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10547 | i += len1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10548 | |
| 10549 | while ( --maxcount > 0) { |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10550 | i = anylib_find(rkind, self, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10551 | sbuf+rkind*i, slen-i, |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10552 | str1, buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10553 | if (i == -1) |
| 10554 | break; |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10555 | memcpy(res + rkind * i, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10556 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10557 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10558 | i += len1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10559 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10560 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10561 | } |
| 10562 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10563 | Py_ssize_t n, i, j, ires; |
| 10564 | Py_ssize_t product, new_size; |
| 10565 | int rkind = skind; |
| 10566 | char *res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10567 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10568 | if (kind1 < rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10569 | /* widen substring */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10570 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10571 | if (!buf1) goto error; |
| 10572 | release1 = 1; |
| 10573 | } |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10574 | n = anylib_count(rkind, self, sbuf, slen, str1, buf1, len1, maxcount); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10575 | if (n == 0) |
| 10576 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10577 | if (kind2 < rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10578 | /* widen replacement */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10579 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 10580 | if (!buf2) goto error; |
| 10581 | release2 = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10582 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10583 | else if (kind2 > rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10584 | /* widen self and buf1 */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10585 | rkind = kind2; |
| 10586 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 10587 | if (!sbuf) goto error; |
| 10588 | srelease = 1; |
| 10589 | if (release1) PyMem_Free(buf1); |
| 10590 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10591 | if (!buf1) goto error; |
| 10592 | release1 = 1; |
| 10593 | } |
| 10594 | /* new_size = PyUnicode_GET_LENGTH(self) + n * (PyUnicode_GET_LENGTH(str2) - |
| 10595 | PyUnicode_GET_LENGTH(str1))); */ |
| 10596 | product = n * (len2-len1); |
| 10597 | if ((product / (len2-len1)) != n) { |
| 10598 | PyErr_SetString(PyExc_OverflowError, |
| 10599 | "replace string is too long"); |
| 10600 | goto error; |
| 10601 | } |
| 10602 | new_size = slen + product; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10603 | if (new_size == 0) { |
| 10604 | Py_INCREF(unicode_empty); |
| 10605 | u = unicode_empty; |
| 10606 | goto done; |
| 10607 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10608 | if (new_size < 0 || new_size > (PY_SSIZE_T_MAX >> (rkind-1))) { |
| 10609 | PyErr_SetString(PyExc_OverflowError, |
| 10610 | "replace string is too long"); |
| 10611 | goto error; |
| 10612 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10613 | u = PyUnicode_New(new_size, maxchar); |
| 10614 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10615 | goto error; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10616 | assert(PyUnicode_KIND(u) == rkind); |
| 10617 | res = PyUnicode_DATA(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10618 | ires = i = 0; |
| 10619 | if (len1 > 0) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10620 | while (n-- > 0) { |
| 10621 | /* look for next match */ |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10622 | j = anylib_find(rkind, self, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10623 | sbuf + rkind * i, slen-i, |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10624 | str1, buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10625 | if (j == -1) |
| 10626 | break; |
| 10627 | else if (j > i) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10628 | /* copy unchanged part [i:j] */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10629 | memcpy(res + rkind * ires, |
| 10630 | sbuf + rkind * i, |
| 10631 | rkind * (j-i)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10632 | ires += j - i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10633 | } |
| 10634 | /* copy substitution string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10635 | if (len2 > 0) { |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10636 | memcpy(res + rkind * ires, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10637 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10638 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10639 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10640 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10641 | i = j + len1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10642 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10643 | if (i < slen) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10644 | /* copy tail [i:] */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10645 | memcpy(res + rkind * ires, |
| 10646 | sbuf + rkind * i, |
| 10647 | rkind * (slen-i)); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10648 | } |
| 10649 | else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10650 | /* interleave */ |
| 10651 | while (n > 0) { |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10652 | memcpy(res + rkind * ires, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10653 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10654 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10655 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10656 | if (--n <= 0) |
| 10657 | break; |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10658 | memcpy(res + rkind * ires, |
| 10659 | sbuf + rkind * i, |
| 10660 | rkind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10661 | ires++; |
| 10662 | i++; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10663 | } |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10664 | memcpy(res + rkind * ires, |
| 10665 | sbuf + rkind * i, |
| 10666 | rkind * (slen-i)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10667 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10668 | } |
| 10669 | |
| 10670 | if (mayshrink) { |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10671 | unicode_adjust_maxchar(&u); |
| 10672 | if (u == NULL) |
| 10673 | goto error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10674 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10675 | |
| 10676 | done: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10677 | if (srelease) |
| 10678 | PyMem_FREE(sbuf); |
| 10679 | if (release1) |
| 10680 | PyMem_FREE(buf1); |
| 10681 | if (release2) |
| 10682 | PyMem_FREE(buf2); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10683 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10684 | return u; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10685 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10686 | nothing: |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10687 | /* nothing to replace; return original string (when possible) */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10688 | if (srelease) |
| 10689 | PyMem_FREE(sbuf); |
| 10690 | if (release1) |
| 10691 | PyMem_FREE(buf1); |
| 10692 | if (release2) |
| 10693 | PyMem_FREE(buf2); |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10694 | return unicode_result_unchanged(self); |
| 10695 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10696 | error: |
| 10697 | if (srelease && sbuf) |
| 10698 | PyMem_FREE(sbuf); |
| 10699 | if (release1 && buf1) |
| 10700 | PyMem_FREE(buf1); |
| 10701 | if (release2 && buf2) |
| 10702 | PyMem_FREE(buf2); |
| 10703 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10704 | } |
| 10705 | |
| 10706 | /* --- Unicode Object Methods --------------------------------------------- */ |
| 10707 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10708 | PyDoc_STRVAR(title__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10709 | "S.title() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10710 | \n\ |
| 10711 | 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] | 10712 | characters, all remaining cased characters have lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10713 | |
| 10714 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10715 | unicode_title(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10716 | { |
Benjamin Peterson | eea4846 | 2012-01-16 14:28:50 -0500 | [diff] [blame] | 10717 | if (PyUnicode_READY(self) == -1) |
| 10718 | return NULL; |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 10719 | return case_operation(self, do_title); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10720 | } |
| 10721 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10722 | PyDoc_STRVAR(capitalize__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10723 | "S.capitalize() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10724 | \n\ |
| 10725 | 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] | 10726 | have upper case and the rest lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10727 | |
| 10728 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10729 | unicode_capitalize(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10730 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 10731 | if (PyUnicode_READY(self) == -1) |
| 10732 | return NULL; |
| 10733 | if (PyUnicode_GET_LENGTH(self) == 0) |
| 10734 | return unicode_result_unchanged(self); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 10735 | return case_operation(self, do_capitalize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10736 | } |
| 10737 | |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 10738 | PyDoc_STRVAR(casefold__doc__, |
| 10739 | "S.casefold() -> str\n\ |
| 10740 | \n\ |
| 10741 | Return a version of S suitable for caseless comparisons."); |
| 10742 | |
| 10743 | static PyObject * |
| 10744 | unicode_casefold(PyObject *self) |
| 10745 | { |
| 10746 | if (PyUnicode_READY(self) == -1) |
| 10747 | return NULL; |
| 10748 | if (PyUnicode_IS_ASCII(self)) |
| 10749 | return ascii_upper_or_lower(self, 1); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 10750 | return case_operation(self, do_casefold); |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 10751 | } |
| 10752 | |
| 10753 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10754 | /* Argument converter. Coerces to a single unicode character */ |
| 10755 | |
| 10756 | static int |
| 10757 | convert_uc(PyObject *obj, void *addr) |
| 10758 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10759 | Py_UCS4 *fillcharloc = (Py_UCS4 *)addr; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10760 | PyObject *uniobj; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10761 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10762 | uniobj = PyUnicode_FromObject(obj); |
| 10763 | if (uniobj == NULL) { |
| 10764 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10765 | "The fill character cannot be converted to Unicode"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10766 | return 0; |
| 10767 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10768 | if (PyUnicode_GET_LENGTH(uniobj) != 1) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10769 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10770 | "The fill character must be exactly one character long"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10771 | Py_DECREF(uniobj); |
| 10772 | return 0; |
| 10773 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10774 | *fillcharloc = PyUnicode_READ_CHAR(uniobj, 0); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10775 | Py_DECREF(uniobj); |
| 10776 | return 1; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10777 | } |
| 10778 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10779 | PyDoc_STRVAR(center__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10780 | "S.center(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10781 | \n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 10782 | Return S centered in a string of length width. Padding is\n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10783 | done using the specified fill character (default is a space)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10784 | |
| 10785 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10786 | unicode_center(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10787 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10788 | Py_ssize_t marg, left; |
| 10789 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10790 | Py_UCS4 fillchar = ' '; |
| 10791 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 10792 | 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] | 10793 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10794 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 10795 | if (PyUnicode_READY(self) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10796 | return NULL; |
| 10797 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10798 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 10799 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10800 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10801 | marg = width - PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10802 | left = marg / 2 + (marg & width & 1); |
| 10803 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10804 | return pad(self, left, marg - left, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10805 | } |
| 10806 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10807 | /* This function assumes that str1 and str2 are readied by the caller. */ |
| 10808 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10809 | static int |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10810 | unicode_compare(PyObject *str1, PyObject *str2) |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10811 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10812 | int kind1, kind2; |
| 10813 | void *data1, *data2; |
| 10814 | Py_ssize_t len1, len2, i; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10815 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10816 | kind1 = PyUnicode_KIND(str1); |
| 10817 | kind2 = PyUnicode_KIND(str2); |
| 10818 | data1 = PyUnicode_DATA(str1); |
| 10819 | data2 = PyUnicode_DATA(str2); |
| 10820 | len1 = PyUnicode_GET_LENGTH(str1); |
| 10821 | len2 = PyUnicode_GET_LENGTH(str2); |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10822 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10823 | for (i = 0; i < len1 && i < len2; ++i) { |
| 10824 | Py_UCS4 c1, c2; |
| 10825 | c1 = PyUnicode_READ(kind1, data1, i); |
| 10826 | c2 = PyUnicode_READ(kind2, data2, i); |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 10827 | |
| 10828 | if (c1 != c2) |
| 10829 | return (c1 < c2) ? -1 : 1; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10830 | } |
| 10831 | |
| 10832 | return (len1 < len2) ? -1 : (len1 != len2); |
| 10833 | } |
| 10834 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10835 | int |
| 10836 | PyUnicode_Compare(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10837 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10838 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 10839 | if (PyUnicode_READY(left) == -1 || |
| 10840 | PyUnicode_READY(right) == -1) |
| 10841 | return -1; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10842 | return unicode_compare(left, right); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10843 | } |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 10844 | PyErr_Format(PyExc_TypeError, |
| 10845 | "Can't compare %.100s and %.100s", |
| 10846 | left->ob_type->tp_name, |
| 10847 | right->ob_type->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10848 | return -1; |
| 10849 | } |
| 10850 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10851 | int |
| 10852 | PyUnicode_CompareWithASCIIString(PyObject* uni, const char* str) |
| 10853 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10854 | Py_ssize_t i; |
| 10855 | int kind; |
| 10856 | void *data; |
| 10857 | Py_UCS4 chr; |
| 10858 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 10859 | assert(_PyUnicode_CHECK(uni)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10860 | if (PyUnicode_READY(uni) == -1) |
| 10861 | return -1; |
| 10862 | kind = PyUnicode_KIND(uni); |
| 10863 | data = PyUnicode_DATA(uni); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10864 | /* Compare Unicode string and source character set string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10865 | for (i = 0; (chr = PyUnicode_READ(kind, data, i)) && str[i]; i++) |
| 10866 | if (chr != str[i]) |
| 10867 | return (chr < (unsigned char)(str[i])) ? -1 : 1; |
Benjamin Peterson | 8667a9b | 2010-01-09 21:45:28 +0000 | [diff] [blame] | 10868 | /* This check keeps Python strings that end in '\0' from comparing equal |
| 10869 | to C strings identical up to that point. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10870 | if (PyUnicode_GET_LENGTH(uni) != i || chr) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10871 | return 1; /* uni is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10872 | if (str[i]) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10873 | return -1; /* str is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10874 | return 0; |
| 10875 | } |
| 10876 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10877 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10878 | #define TEST_COND(cond) \ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10879 | ((cond) ? Py_True : Py_False) |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10880 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10881 | PyObject * |
| 10882 | PyUnicode_RichCompare(PyObject *left, PyObject *right, int op) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10883 | { |
| 10884 | int result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10885 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10886 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 10887 | PyObject *v; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10888 | if (PyUnicode_READY(left) == -1 || |
| 10889 | PyUnicode_READY(right) == -1) |
| 10890 | return NULL; |
| 10891 | if (PyUnicode_GET_LENGTH(left) != PyUnicode_GET_LENGTH(right) || |
| 10892 | PyUnicode_KIND(left) != PyUnicode_KIND(right)) { |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10893 | if (op == Py_EQ) { |
| 10894 | Py_INCREF(Py_False); |
| 10895 | return Py_False; |
| 10896 | } |
| 10897 | if (op == Py_NE) { |
| 10898 | Py_INCREF(Py_True); |
| 10899 | return Py_True; |
| 10900 | } |
| 10901 | } |
| 10902 | if (left == right) |
| 10903 | result = 0; |
| 10904 | else |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10905 | result = unicode_compare(left, right); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10906 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10907 | /* Convert the return value to a Boolean */ |
| 10908 | switch (op) { |
| 10909 | case Py_EQ: |
| 10910 | v = TEST_COND(result == 0); |
| 10911 | break; |
| 10912 | case Py_NE: |
| 10913 | v = TEST_COND(result != 0); |
| 10914 | break; |
| 10915 | case Py_LE: |
| 10916 | v = TEST_COND(result <= 0); |
| 10917 | break; |
| 10918 | case Py_GE: |
| 10919 | v = TEST_COND(result >= 0); |
| 10920 | break; |
| 10921 | case Py_LT: |
| 10922 | v = TEST_COND(result == -1); |
| 10923 | break; |
| 10924 | case Py_GT: |
| 10925 | v = TEST_COND(result == 1); |
| 10926 | break; |
| 10927 | default: |
| 10928 | PyErr_BadArgument(); |
| 10929 | return NULL; |
| 10930 | } |
| 10931 | Py_INCREF(v); |
| 10932 | return v; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10933 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10934 | |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 10935 | Py_RETURN_NOTIMPLEMENTED; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10936 | } |
| 10937 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10938 | int |
| 10939 | PyUnicode_Contains(PyObject *container, PyObject *element) |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10940 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10941 | PyObject *str, *sub; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10942 | int kind1, kind2, kind; |
| 10943 | void *buf1, *buf2; |
| 10944 | Py_ssize_t len1, len2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10945 | int result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10946 | |
| 10947 | /* Coerce the two arguments */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10948 | sub = PyUnicode_FromObject(element); |
| 10949 | if (!sub) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10950 | PyErr_Format(PyExc_TypeError, |
| 10951 | "'in <string>' requires string as left operand, not %s", |
| 10952 | element->ob_type->tp_name); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10953 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10954 | } |
| 10955 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10956 | str = PyUnicode_FromObject(container); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 10957 | if (!str) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10958 | Py_DECREF(sub); |
| 10959 | return -1; |
| 10960 | } |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 10961 | if (PyUnicode_READY(sub) == -1 || PyUnicode_READY(str) == -1) { |
| 10962 | Py_DECREF(sub); |
| 10963 | Py_DECREF(str); |
| 10964 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10965 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10966 | kind1 = PyUnicode_KIND(str); |
| 10967 | kind2 = PyUnicode_KIND(sub); |
| 10968 | kind = kind1 > kind2 ? kind1 : kind2; |
| 10969 | buf1 = PyUnicode_DATA(str); |
| 10970 | buf2 = PyUnicode_DATA(sub); |
| 10971 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10972 | buf1 = _PyUnicode_AsKind(str, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10973 | if (!buf1) { |
| 10974 | Py_DECREF(sub); |
| 10975 | return -1; |
| 10976 | } |
| 10977 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10978 | buf2 = _PyUnicode_AsKind(sub, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10979 | if (!buf2) { |
| 10980 | Py_DECREF(sub); |
| 10981 | if (kind1 != kind) PyMem_Free(buf1); |
| 10982 | return -1; |
| 10983 | } |
| 10984 | len1 = PyUnicode_GET_LENGTH(str); |
| 10985 | len2 = PyUnicode_GET_LENGTH(sub); |
| 10986 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10987 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10988 | case PyUnicode_1BYTE_KIND: |
| 10989 | result = ucs1lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10990 | break; |
| 10991 | case PyUnicode_2BYTE_KIND: |
| 10992 | result = ucs2lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10993 | break; |
| 10994 | case PyUnicode_4BYTE_KIND: |
| 10995 | result = ucs4lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10996 | break; |
| 10997 | default: |
| 10998 | result = -1; |
| 10999 | assert(0); |
| 11000 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11001 | |
| 11002 | Py_DECREF(str); |
| 11003 | Py_DECREF(sub); |
| 11004 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11005 | if (kind1 != kind) |
| 11006 | PyMem_Free(buf1); |
| 11007 | if (kind2 != kind) |
| 11008 | PyMem_Free(buf2); |
| 11009 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 11010 | return result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 11011 | } |
| 11012 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11013 | /* Concat to string or Unicode object giving a new Unicode object. */ |
| 11014 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 11015 | PyObject * |
| 11016 | PyUnicode_Concat(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11017 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11018 | PyObject *u = NULL, *v = NULL, *w; |
Victor Stinner | 127226b | 2011-10-13 01:12:34 +0200 | [diff] [blame] | 11019 | Py_UCS4 maxchar, maxchar2; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11020 | Py_ssize_t u_len, v_len, new_len; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11021 | |
| 11022 | /* Coerce the two arguments */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11023 | u = PyUnicode_FromObject(left); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11024 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11025 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11026 | v = PyUnicode_FromObject(right); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11027 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11028 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11029 | |
| 11030 | /* Shortcuts */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 11031 | if (v == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11032 | Py_DECREF(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11033 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11034 | } |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 11035 | if (u == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11036 | Py_DECREF(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11037 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11038 | } |
| 11039 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11040 | u_len = PyUnicode_GET_LENGTH(u); |
| 11041 | v_len = PyUnicode_GET_LENGTH(v); |
| 11042 | if (u_len > PY_SSIZE_T_MAX - v_len) { |
| 11043 | PyErr_SetString(PyExc_OverflowError, |
| 11044 | "strings are too large to concat"); |
| 11045 | goto onError; |
| 11046 | } |
| 11047 | new_len = u_len + v_len; |
| 11048 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11049 | maxchar = PyUnicode_MAX_CHAR_VALUE(u); |
Victor Stinner | 127226b | 2011-10-13 01:12:34 +0200 | [diff] [blame] | 11050 | maxchar2 = PyUnicode_MAX_CHAR_VALUE(v); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 11051 | maxchar = MAX_MAXCHAR(maxchar, maxchar2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11052 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11053 | /* Concat the two Unicode strings */ |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11054 | w = PyUnicode_New(new_len, maxchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11055 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11056 | goto onError; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11057 | copy_characters(w, 0, u, 0, u_len); |
| 11058 | copy_characters(w, u_len, v, 0, v_len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11059 | Py_DECREF(u); |
| 11060 | Py_DECREF(v); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 11061 | assert(_PyUnicode_CheckConsistency(w, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11062 | return w; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11063 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11064 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11065 | Py_XDECREF(u); |
| 11066 | Py_XDECREF(v); |
| 11067 | return NULL; |
| 11068 | } |
| 11069 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 11070 | void |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11071 | PyUnicode_Append(PyObject **p_left, PyObject *right) |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 11072 | { |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11073 | PyObject *left, *res; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11074 | Py_UCS4 maxchar, maxchar2; |
| 11075 | Py_ssize_t left_len, right_len, new_len; |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11076 | |
| 11077 | if (p_left == NULL) { |
| 11078 | if (!PyErr_Occurred()) |
| 11079 | PyErr_BadInternalCall(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11080 | return; |
| 11081 | } |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11082 | left = *p_left; |
| 11083 | if (right == NULL || !PyUnicode_Check(left)) { |
| 11084 | if (!PyErr_Occurred()) |
| 11085 | PyErr_BadInternalCall(); |
| 11086 | goto error; |
| 11087 | } |
| 11088 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 11089 | if (PyUnicode_READY(left) == -1) |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 11090 | goto error; |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 11091 | if (PyUnicode_READY(right) == -1) |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 11092 | goto error; |
| 11093 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11094 | /* Shortcuts */ |
| 11095 | if (left == unicode_empty) { |
| 11096 | Py_DECREF(left); |
| 11097 | Py_INCREF(right); |
| 11098 | *p_left = right; |
| 11099 | return; |
| 11100 | } |
| 11101 | if (right == unicode_empty) |
| 11102 | return; |
| 11103 | |
| 11104 | left_len = PyUnicode_GET_LENGTH(left); |
| 11105 | right_len = PyUnicode_GET_LENGTH(right); |
| 11106 | if (left_len > PY_SSIZE_T_MAX - right_len) { |
| 11107 | PyErr_SetString(PyExc_OverflowError, |
| 11108 | "strings are too large to concat"); |
| 11109 | goto error; |
| 11110 | } |
| 11111 | new_len = left_len + right_len; |
| 11112 | |
| 11113 | if (unicode_modifiable(left) |
| 11114 | && PyUnicode_CheckExact(right) |
| 11115 | && PyUnicode_KIND(right) <= PyUnicode_KIND(left) |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 11116 | /* Don't resize for ascii += latin1. Convert ascii to latin1 requires |
| 11117 | to change the structure size, but characters are stored just after |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 11118 | the structure, and so it requires to move all characters which is |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 11119 | not so different than duplicating the string. */ |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11120 | && !(PyUnicode_IS_ASCII(left) && !PyUnicode_IS_ASCII(right))) |
| 11121 | { |
| 11122 | /* append inplace */ |
| 11123 | if (unicode_resize(p_left, new_len) != 0) { |
| 11124 | /* XXX if _PyUnicode_Resize() fails, 'left' has been |
| 11125 | * deallocated so it cannot be put back into |
| 11126 | * 'variable'. The MemoryError is raised when there |
| 11127 | * is no value in 'variable', which might (very |
| 11128 | * remotely) be a cause of incompatibilities. |
| 11129 | */ |
| 11130 | goto error; |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11131 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11132 | /* copy 'right' into the newly allocated area of 'left' */ |
| 11133 | copy_characters(*p_left, left_len, right, 0, right_len); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11134 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11135 | else { |
| 11136 | maxchar = PyUnicode_MAX_CHAR_VALUE(left); |
| 11137 | maxchar2 = PyUnicode_MAX_CHAR_VALUE(right); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 11138 | maxchar = MAX_MAXCHAR(maxchar, maxchar2); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11139 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11140 | /* Concat the two Unicode strings */ |
| 11141 | res = PyUnicode_New(new_len, maxchar); |
| 11142 | if (res == NULL) |
| 11143 | goto error; |
| 11144 | copy_characters(res, 0, left, 0, left_len); |
| 11145 | copy_characters(res, left_len, right, 0, right_len); |
| 11146 | Py_DECREF(left); |
| 11147 | *p_left = res; |
| 11148 | } |
| 11149 | assert(_PyUnicode_CheckConsistency(*p_left, 1)); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11150 | return; |
| 11151 | |
| 11152 | error: |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11153 | Py_CLEAR(*p_left); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 11154 | } |
| 11155 | |
| 11156 | void |
| 11157 | PyUnicode_AppendAndDel(PyObject **pleft, PyObject *right) |
| 11158 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11159 | PyUnicode_Append(pleft, right); |
| 11160 | Py_XDECREF(right); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 11161 | } |
| 11162 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11163 | PyDoc_STRVAR(count__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11164 | "S.count(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11165 | \n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11166 | Return the number of non-overlapping occurrences of substring sub in\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11167 | 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] | 11168 | interpreted as in slice notation."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11169 | |
| 11170 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11171 | unicode_count(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11172 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11173 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11174 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11175 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11176 | PyObject *result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11177 | int kind1, kind2, kind; |
| 11178 | void *buf1, *buf2; |
| 11179 | Py_ssize_t len1, len2, iresult; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11180 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11181 | if (!stringlib_parse_args_finds_unicode("count", args, &substring, |
| 11182 | &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11183 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11184 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11185 | kind1 = PyUnicode_KIND(self); |
| 11186 | kind2 = PyUnicode_KIND(substring); |
| 11187 | kind = kind1 > kind2 ? kind1 : kind2; |
| 11188 | buf1 = PyUnicode_DATA(self); |
| 11189 | buf2 = PyUnicode_DATA(substring); |
| 11190 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11191 | buf1 = _PyUnicode_AsKind(self, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11192 | if (!buf1) { |
| 11193 | Py_DECREF(substring); |
| 11194 | return NULL; |
| 11195 | } |
| 11196 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11197 | buf2 = _PyUnicode_AsKind(substring, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11198 | if (!buf2) { |
| 11199 | Py_DECREF(substring); |
| 11200 | if (kind1 != kind) PyMem_Free(buf1); |
| 11201 | return NULL; |
| 11202 | } |
| 11203 | len1 = PyUnicode_GET_LENGTH(self); |
| 11204 | len2 = PyUnicode_GET_LENGTH(substring); |
| 11205 | |
| 11206 | ADJUST_INDICES(start, end, len1); |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 11207 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11208 | case PyUnicode_1BYTE_KIND: |
| 11209 | iresult = ucs1lib_count( |
| 11210 | ((Py_UCS1*)buf1) + start, end - start, |
| 11211 | buf2, len2, PY_SSIZE_T_MAX |
| 11212 | ); |
| 11213 | break; |
| 11214 | case PyUnicode_2BYTE_KIND: |
| 11215 | iresult = ucs2lib_count( |
| 11216 | ((Py_UCS2*)buf1) + start, end - start, |
| 11217 | buf2, len2, PY_SSIZE_T_MAX |
| 11218 | ); |
| 11219 | break; |
| 11220 | case PyUnicode_4BYTE_KIND: |
| 11221 | iresult = ucs4lib_count( |
| 11222 | ((Py_UCS4*)buf1) + start, end - start, |
| 11223 | buf2, len2, PY_SSIZE_T_MAX |
| 11224 | ); |
| 11225 | break; |
| 11226 | default: |
| 11227 | assert(0); iresult = 0; |
| 11228 | } |
| 11229 | |
| 11230 | result = PyLong_FromSsize_t(iresult); |
| 11231 | |
| 11232 | if (kind1 != kind) |
| 11233 | PyMem_Free(buf1); |
| 11234 | if (kind2 != kind) |
| 11235 | PyMem_Free(buf2); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11236 | |
| 11237 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11238 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11239 | return result; |
| 11240 | } |
| 11241 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11242 | PyDoc_STRVAR(encode__doc__, |
Victor Stinner | c911bbf | 2010-11-07 19:04:46 +0000 | [diff] [blame] | 11243 | "S.encode(encoding='utf-8', errors='strict') -> bytes\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11244 | \n\ |
Victor Stinner | e14e212 | 2010-11-07 18:41:46 +0000 | [diff] [blame] | 11245 | Encode S using the codec registered for encoding. Default encoding\n\ |
| 11246 | 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] | 11247 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 11248 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 11249 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 11250 | codecs.register_error that can handle UnicodeEncodeErrors."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11251 | |
| 11252 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11253 | unicode_encode(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11254 | { |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 11255 | static char *kwlist[] = {"encoding", "errors", 0}; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11256 | char *encoding = NULL; |
| 11257 | char *errors = NULL; |
Guido van Rossum | 35d9428 | 2007-08-27 18:20:11 +0000 | [diff] [blame] | 11258 | |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 11259 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:encode", |
| 11260 | kwlist, &encoding, &errors)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11261 | return NULL; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11262 | return PyUnicode_AsEncodedString(self, encoding, errors); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 11263 | } |
| 11264 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11265 | PyDoc_STRVAR(expandtabs__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11266 | "S.expandtabs([tabsize]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11267 | \n\ |
| 11268 | 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] | 11269 | 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] | 11270 | |
| 11271 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11272 | unicode_expandtabs(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11273 | { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11274 | Py_ssize_t i, j, line_pos, src_len, incr; |
| 11275 | Py_UCS4 ch; |
| 11276 | PyObject *u; |
| 11277 | void *src_data, *dest_data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11278 | int tabsize = 8; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11279 | int kind; |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 11280 | int found; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11281 | |
| 11282 | if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11283 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11284 | |
Antoine Pitrou | 2242522 | 2011-10-04 19:10:51 +0200 | [diff] [blame] | 11285 | if (PyUnicode_READY(self) == -1) |
| 11286 | return NULL; |
| 11287 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 11288 | /* First pass: determine size of output string */ |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11289 | src_len = PyUnicode_GET_LENGTH(self); |
| 11290 | i = j = line_pos = 0; |
| 11291 | kind = PyUnicode_KIND(self); |
| 11292 | src_data = PyUnicode_DATA(self); |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 11293 | found = 0; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11294 | for (; i < src_len; i++) { |
| 11295 | ch = PyUnicode_READ(kind, src_data, i); |
| 11296 | if (ch == '\t') { |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 11297 | found = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11298 | if (tabsize > 0) { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11299 | incr = tabsize - (line_pos % tabsize); /* cannot overflow */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11300 | if (j > PY_SSIZE_T_MAX - incr) |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11301 | goto overflow; |
| 11302 | line_pos += incr; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11303 | j += incr; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11304 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11305 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11306 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11307 | if (j > PY_SSIZE_T_MAX - 1) |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11308 | goto overflow; |
| 11309 | line_pos++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11310 | j++; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11311 | if (ch == '\n' || ch == '\r') |
| 11312 | line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11313 | } |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11314 | } |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11315 | if (!found) |
| 11316 | return unicode_result_unchanged(self); |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 11317 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11318 | /* Second pass: create output string and fill it */ |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11319 | u = PyUnicode_New(j, PyUnicode_MAX_CHAR_VALUE(self)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11320 | if (!u) |
| 11321 | return NULL; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11322 | dest_data = PyUnicode_DATA(u); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11323 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11324 | i = j = line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11325 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11326 | for (; i < src_len; i++) { |
| 11327 | ch = PyUnicode_READ(kind, src_data, i); |
| 11328 | if (ch == '\t') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11329 | if (tabsize > 0) { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11330 | incr = tabsize - (line_pos % tabsize); |
| 11331 | line_pos += incr; |
Victor Stinner | da79e63 | 2012-02-22 13:37:04 +0100 | [diff] [blame] | 11332 | FILL(kind, dest_data, ' ', j, incr); |
| 11333 | j += incr; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11334 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11335 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11336 | else { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11337 | line_pos++; |
| 11338 | PyUnicode_WRITE(kind, dest_data, j, ch); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11339 | j++; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11340 | if (ch == '\n' || ch == '\r') |
| 11341 | line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11342 | } |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11343 | } |
| 11344 | assert (j == PyUnicode_GET_LENGTH(u)); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 11345 | return unicode_result(u); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11346 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11347 | overflow: |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11348 | PyErr_SetString(PyExc_OverflowError, "new string is too long"); |
| 11349 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11350 | } |
| 11351 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11352 | PyDoc_STRVAR(find__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11353 | "S.find(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11354 | \n\ |
| 11355 | Return the lowest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 11356 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11357 | arguments start and end are interpreted as in slice notation.\n\ |
| 11358 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11359 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11360 | |
| 11361 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11362 | unicode_find(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11363 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11364 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 11365 | Py_ssize_t start; |
| 11366 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11367 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11368 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11369 | if (!stringlib_parse_args_finds_unicode("find", args, &substring, |
| 11370 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11371 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11372 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11373 | if (PyUnicode_READY(self) == -1) |
| 11374 | return NULL; |
| 11375 | if (PyUnicode_READY(substring) == -1) |
| 11376 | return NULL; |
| 11377 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11378 | result = any_find_slice(1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11379 | |
| 11380 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11381 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11382 | if (result == -2) |
| 11383 | return NULL; |
| 11384 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 11385 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11386 | } |
| 11387 | |
| 11388 | static PyObject * |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 11389 | unicode_getitem(PyObject *self, Py_ssize_t index) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11390 | { |
Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 11391 | void *data; |
| 11392 | enum PyUnicode_Kind kind; |
| 11393 | Py_UCS4 ch; |
| 11394 | PyObject *res; |
| 11395 | |
| 11396 | if (!PyUnicode_Check(self) || PyUnicode_READY(self) == -1) { |
| 11397 | PyErr_BadArgument(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11398 | return NULL; |
Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 11399 | } |
| 11400 | if (index < 0 || index >= PyUnicode_GET_LENGTH(self)) { |
| 11401 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 11402 | return NULL; |
| 11403 | } |
| 11404 | kind = PyUnicode_KIND(self); |
| 11405 | data = PyUnicode_DATA(self); |
| 11406 | ch = PyUnicode_READ(kind, data, index); |
| 11407 | if (ch < 256) |
| 11408 | return get_latin1_char(ch); |
| 11409 | |
| 11410 | res = PyUnicode_New(1, ch); |
| 11411 | if (res == NULL) |
| 11412 | return NULL; |
| 11413 | kind = PyUnicode_KIND(res); |
| 11414 | data = PyUnicode_DATA(res); |
| 11415 | PyUnicode_WRITE(kind, data, 0, ch); |
| 11416 | assert(_PyUnicode_CheckConsistency(res, 1)); |
| 11417 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11418 | } |
| 11419 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11420 | /* Believe it or not, this produces the same value for ASCII strings |
Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 11421 | as bytes_hash(). */ |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 11422 | static Py_hash_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11423 | unicode_hash(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11424 | { |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11425 | Py_ssize_t len; |
Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 11426 | Py_uhash_t x; |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11427 | |
Benjamin Peterson | f6622c8 | 2012-04-09 14:53:07 -0400 | [diff] [blame] | 11428 | #ifdef Py_DEBUG |
Benjamin Peterson | 69e9727 | 2012-02-21 11:08:50 -0500 | [diff] [blame] | 11429 | assert(_Py_HashSecret_Initialized); |
Benjamin Peterson | f6622c8 | 2012-04-09 14:53:07 -0400 | [diff] [blame] | 11430 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11431 | if (_PyUnicode_HASH(self) != -1) |
| 11432 | return _PyUnicode_HASH(self); |
| 11433 | if (PyUnicode_READY(self) == -1) |
| 11434 | return -1; |
| 11435 | len = PyUnicode_GET_LENGTH(self); |
Georg Brandl | 16fa2a1 | 2012-02-21 00:50:13 +0100 | [diff] [blame] | 11436 | /* |
| 11437 | We make the hash of the empty string be 0, rather than using |
| 11438 | (prefix ^ suffix), since this slightly obfuscates the hash secret |
| 11439 | */ |
| 11440 | if (len == 0) { |
| 11441 | _PyUnicode_HASH(self) = 0; |
| 11442 | return 0; |
| 11443 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11444 | |
| 11445 | /* The hash function as a macro, gets expanded three times below. */ |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 11446 | #define HASH(P) \ |
| 11447 | x ^= (Py_uhash_t) *P << 7; \ |
| 11448 | while (--len >= 0) \ |
| 11449 | x = (_PyHASH_MULTIPLIER * x) ^ (Py_uhash_t) *P++; \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11450 | |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 11451 | x = (Py_uhash_t) _Py_HashSecret.prefix; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11452 | switch (PyUnicode_KIND(self)) { |
| 11453 | case PyUnicode_1BYTE_KIND: { |
| 11454 | const unsigned char *c = PyUnicode_1BYTE_DATA(self); |
| 11455 | HASH(c); |
| 11456 | break; |
| 11457 | } |
| 11458 | case PyUnicode_2BYTE_KIND: { |
| 11459 | const Py_UCS2 *s = PyUnicode_2BYTE_DATA(self); |
| 11460 | HASH(s); |
| 11461 | break; |
| 11462 | } |
| 11463 | default: { |
| 11464 | Py_UCS4 *l; |
| 11465 | assert(PyUnicode_KIND(self) == PyUnicode_4BYTE_KIND && |
| 11466 | "Impossible switch case in unicode_hash"); |
| 11467 | l = PyUnicode_4BYTE_DATA(self); |
| 11468 | HASH(l); |
| 11469 | break; |
| 11470 | } |
| 11471 | } |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 11472 | x ^= (Py_uhash_t) PyUnicode_GET_LENGTH(self); |
| 11473 | x ^= (Py_uhash_t) _Py_HashSecret.suffix; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11474 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11475 | if (x == -1) |
| 11476 | x = -2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11477 | _PyUnicode_HASH(self) = x; |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11478 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11479 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11480 | #undef HASH |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11481 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11482 | PyDoc_STRVAR(index__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11483 | "S.index(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11484 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11485 | 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] | 11486 | |
| 11487 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11488 | unicode_index(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11489 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11490 | Py_ssize_t result; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11491 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 11492 | Py_ssize_t start; |
| 11493 | Py_ssize_t end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11494 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11495 | if (!stringlib_parse_args_finds_unicode("index", args, &substring, |
| 11496 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11497 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11498 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11499 | if (PyUnicode_READY(self) == -1) |
| 11500 | return NULL; |
| 11501 | if (PyUnicode_READY(substring) == -1) |
| 11502 | return NULL; |
| 11503 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11504 | result = any_find_slice(1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11505 | |
| 11506 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11507 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11508 | if (result == -2) |
| 11509 | return NULL; |
| 11510 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11511 | if (result < 0) { |
| 11512 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 11513 | return NULL; |
| 11514 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11515 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 11516 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11517 | } |
| 11518 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11519 | PyDoc_STRVAR(islower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11520 | "S.islower() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11521 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11522 | 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] | 11523 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11524 | |
| 11525 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11526 | unicode_islower(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11527 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11528 | Py_ssize_t i, length; |
| 11529 | int kind; |
| 11530 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11531 | int cased; |
| 11532 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11533 | if (PyUnicode_READY(self) == -1) |
| 11534 | return NULL; |
| 11535 | length = PyUnicode_GET_LENGTH(self); |
| 11536 | kind = PyUnicode_KIND(self); |
| 11537 | data = PyUnicode_DATA(self); |
| 11538 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11539 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11540 | if (length == 1) |
| 11541 | return PyBool_FromLong( |
| 11542 | Py_UNICODE_ISLOWER(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11543 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11544 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11545 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11546 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11547 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11548 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11549 | for (i = 0; i < length; i++) { |
| 11550 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11551 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11552 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 11553 | return PyBool_FromLong(0); |
| 11554 | else if (!cased && Py_UNICODE_ISLOWER(ch)) |
| 11555 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11556 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11557 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11558 | } |
| 11559 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11560 | PyDoc_STRVAR(isupper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11561 | "S.isupper() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11562 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11563 | 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] | 11564 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11565 | |
| 11566 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11567 | unicode_isupper(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11568 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11569 | Py_ssize_t i, length; |
| 11570 | int kind; |
| 11571 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11572 | int cased; |
| 11573 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11574 | if (PyUnicode_READY(self) == -1) |
| 11575 | return NULL; |
| 11576 | length = PyUnicode_GET_LENGTH(self); |
| 11577 | kind = PyUnicode_KIND(self); |
| 11578 | data = PyUnicode_DATA(self); |
| 11579 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11580 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11581 | if (length == 1) |
| 11582 | return PyBool_FromLong( |
| 11583 | Py_UNICODE_ISUPPER(PyUnicode_READ(kind, data, 0)) != 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11584 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11585 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11586 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11587 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11588 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11589 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11590 | for (i = 0; i < length; i++) { |
| 11591 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11592 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11593 | if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 11594 | return PyBool_FromLong(0); |
| 11595 | else if (!cased && Py_UNICODE_ISUPPER(ch)) |
| 11596 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11597 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11598 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11599 | } |
| 11600 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11601 | PyDoc_STRVAR(istitle__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11602 | "S.istitle() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11603 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11604 | Return True if S is a titlecased string and there is at least one\n\ |
| 11605 | character in S, i.e. upper- and titlecase characters may only\n\ |
| 11606 | follow uncased characters and lowercase characters only cased ones.\n\ |
| 11607 | Return False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11608 | |
| 11609 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11610 | unicode_istitle(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11611 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11612 | Py_ssize_t i, length; |
| 11613 | int kind; |
| 11614 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11615 | int cased, previous_is_cased; |
| 11616 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11617 | if (PyUnicode_READY(self) == -1) |
| 11618 | return NULL; |
| 11619 | length = PyUnicode_GET_LENGTH(self); |
| 11620 | kind = PyUnicode_KIND(self); |
| 11621 | data = PyUnicode_DATA(self); |
| 11622 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11623 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11624 | if (length == 1) { |
| 11625 | Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11626 | return PyBool_FromLong((Py_UNICODE_ISTITLE(ch) != 0) || |
| 11627 | (Py_UNICODE_ISUPPER(ch) != 0)); |
| 11628 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11629 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11630 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11631 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11632 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11633 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11634 | cased = 0; |
| 11635 | previous_is_cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11636 | for (i = 0; i < length; i++) { |
| 11637 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11638 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11639 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { |
| 11640 | if (previous_is_cased) |
| 11641 | return PyBool_FromLong(0); |
| 11642 | previous_is_cased = 1; |
| 11643 | cased = 1; |
| 11644 | } |
| 11645 | else if (Py_UNICODE_ISLOWER(ch)) { |
| 11646 | if (!previous_is_cased) |
| 11647 | return PyBool_FromLong(0); |
| 11648 | previous_is_cased = 1; |
| 11649 | cased = 1; |
| 11650 | } |
| 11651 | else |
| 11652 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11653 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11654 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11655 | } |
| 11656 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11657 | PyDoc_STRVAR(isspace__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11658 | "S.isspace() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11659 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11660 | Return True if all characters in S are whitespace\n\ |
| 11661 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11662 | |
| 11663 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11664 | unicode_isspace(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11665 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11666 | Py_ssize_t i, length; |
| 11667 | int kind; |
| 11668 | void *data; |
| 11669 | |
| 11670 | if (PyUnicode_READY(self) == -1) |
| 11671 | return NULL; |
| 11672 | length = PyUnicode_GET_LENGTH(self); |
| 11673 | kind = PyUnicode_KIND(self); |
| 11674 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11675 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11676 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11677 | if (length == 1) |
| 11678 | return PyBool_FromLong( |
| 11679 | Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11680 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11681 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11682 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11683 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11684 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11685 | for (i = 0; i < length; i++) { |
| 11686 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11687 | if (!Py_UNICODE_ISSPACE(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11688 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11689 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11690 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11691 | } |
| 11692 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11693 | PyDoc_STRVAR(isalpha__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11694 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11695 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11696 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11697 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11698 | |
| 11699 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11700 | unicode_isalpha(PyObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11701 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11702 | Py_ssize_t i, length; |
| 11703 | int kind; |
| 11704 | void *data; |
| 11705 | |
| 11706 | if (PyUnicode_READY(self) == -1) |
| 11707 | return NULL; |
| 11708 | length = PyUnicode_GET_LENGTH(self); |
| 11709 | kind = PyUnicode_KIND(self); |
| 11710 | data = PyUnicode_DATA(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11711 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11712 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11713 | if (length == 1) |
| 11714 | return PyBool_FromLong( |
| 11715 | Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, 0))); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11716 | |
| 11717 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11718 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11719 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11720 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11721 | for (i = 0; i < length; i++) { |
| 11722 | if (!Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11723 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11724 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11725 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11726 | } |
| 11727 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11728 | PyDoc_STRVAR(isalnum__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11729 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11730 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11731 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11732 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11733 | |
| 11734 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11735 | unicode_isalnum(PyObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11736 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11737 | int kind; |
| 11738 | void *data; |
| 11739 | Py_ssize_t len, i; |
| 11740 | |
| 11741 | if (PyUnicode_READY(self) == -1) |
| 11742 | return NULL; |
| 11743 | |
| 11744 | kind = PyUnicode_KIND(self); |
| 11745 | data = PyUnicode_DATA(self); |
| 11746 | len = PyUnicode_GET_LENGTH(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11747 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11748 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11749 | if (len == 1) { |
| 11750 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11751 | return PyBool_FromLong(Py_UNICODE_ISALNUM(ch)); |
| 11752 | } |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11753 | |
| 11754 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11755 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11756 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11757 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11758 | for (i = 0; i < len; i++) { |
| 11759 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11760 | if (!Py_UNICODE_ISALNUM(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11761 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11762 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11763 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11764 | } |
| 11765 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11766 | PyDoc_STRVAR(isdecimal__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11767 | "S.isdecimal() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11768 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11769 | 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] | 11770 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11771 | |
| 11772 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11773 | unicode_isdecimal(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11774 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11775 | Py_ssize_t i, length; |
| 11776 | int kind; |
| 11777 | void *data; |
| 11778 | |
| 11779 | if (PyUnicode_READY(self) == -1) |
| 11780 | return NULL; |
| 11781 | length = PyUnicode_GET_LENGTH(self); |
| 11782 | kind = PyUnicode_KIND(self); |
| 11783 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11784 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11785 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11786 | if (length == 1) |
| 11787 | return PyBool_FromLong( |
| 11788 | Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11789 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11790 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11791 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11792 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11793 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11794 | for (i = 0; i < length; i++) { |
| 11795 | if (!Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11796 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11797 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11798 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11799 | } |
| 11800 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11801 | PyDoc_STRVAR(isdigit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11802 | "S.isdigit() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11803 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11804 | Return True if all characters in S are digits\n\ |
| 11805 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11806 | |
| 11807 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11808 | unicode_isdigit(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11809 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11810 | Py_ssize_t i, length; |
| 11811 | int kind; |
| 11812 | void *data; |
| 11813 | |
| 11814 | if (PyUnicode_READY(self) == -1) |
| 11815 | return NULL; |
| 11816 | length = PyUnicode_GET_LENGTH(self); |
| 11817 | kind = PyUnicode_KIND(self); |
| 11818 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11819 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11820 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11821 | if (length == 1) { |
| 11822 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11823 | return PyBool_FromLong(Py_UNICODE_ISDIGIT(ch)); |
| 11824 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11825 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11826 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11827 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11828 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11829 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11830 | for (i = 0; i < length; i++) { |
| 11831 | if (!Py_UNICODE_ISDIGIT(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11832 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11833 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11834 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11835 | } |
| 11836 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11837 | PyDoc_STRVAR(isnumeric__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11838 | "S.isnumeric() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11839 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11840 | 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] | 11841 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11842 | |
| 11843 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11844 | unicode_isnumeric(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11845 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11846 | Py_ssize_t i, length; |
| 11847 | int kind; |
| 11848 | void *data; |
| 11849 | |
| 11850 | if (PyUnicode_READY(self) == -1) |
| 11851 | return NULL; |
| 11852 | length = PyUnicode_GET_LENGTH(self); |
| 11853 | kind = PyUnicode_KIND(self); |
| 11854 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11855 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11856 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11857 | if (length == 1) |
| 11858 | return PyBool_FromLong( |
| 11859 | Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11860 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11861 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11862 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11863 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11864 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11865 | for (i = 0; i < length; i++) { |
| 11866 | if (!Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11867 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11868 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11869 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11870 | } |
| 11871 | |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11872 | int |
| 11873 | PyUnicode_IsIdentifier(PyObject *self) |
| 11874 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11875 | int kind; |
| 11876 | void *data; |
| 11877 | Py_ssize_t i; |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11878 | Py_UCS4 first; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11879 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11880 | if (PyUnicode_READY(self) == -1) { |
| 11881 | Py_FatalError("identifier not ready"); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11882 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11883 | } |
| 11884 | |
| 11885 | /* Special case for empty strings */ |
| 11886 | if (PyUnicode_GET_LENGTH(self) == 0) |
| 11887 | return 0; |
| 11888 | kind = PyUnicode_KIND(self); |
| 11889 | data = PyUnicode_DATA(self); |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11890 | |
| 11891 | /* PEP 3131 says that the first character must be in |
| 11892 | XID_Start and subsequent characters in XID_Continue, |
| 11893 | and for the ASCII range, the 2.x rules apply (i.e |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11894 | start with letters and underscore, continue with |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11895 | letters, digits, underscore). However, given the current |
| 11896 | definition of XID_Start and XID_Continue, it is sufficient |
| 11897 | to check just for these, except that _ must be allowed |
| 11898 | as starting an identifier. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11899 | first = PyUnicode_READ(kind, data, 0); |
Benjamin Peterson | f413b80 | 2011-08-12 22:17:18 -0500 | [diff] [blame] | 11900 | if (!_PyUnicode_IsXidStart(first) && first != 0x5F /* LOW LINE */) |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11901 | return 0; |
| 11902 | |
Benjamin Peterson | 9c6e6a0 | 2011-09-28 08:09:05 -0400 | [diff] [blame] | 11903 | for (i = 1; i < PyUnicode_GET_LENGTH(self); i++) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11904 | if (!_PyUnicode_IsXidContinue(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11905 | return 0; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11906 | return 1; |
| 11907 | } |
| 11908 | |
| 11909 | PyDoc_STRVAR(isidentifier__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11910 | "S.isidentifier() -> bool\n\ |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11911 | \n\ |
| 11912 | Return True if S is a valid identifier according\n\ |
| 11913 | to the language definition."); |
| 11914 | |
| 11915 | static PyObject* |
| 11916 | unicode_isidentifier(PyObject *self) |
| 11917 | { |
| 11918 | return PyBool_FromLong(PyUnicode_IsIdentifier(self)); |
| 11919 | } |
| 11920 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11921 | PyDoc_STRVAR(isprintable__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11922 | "S.isprintable() -> bool\n\ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11923 | \n\ |
| 11924 | Return True if all characters in S are considered\n\ |
| 11925 | printable in repr() or S is empty, False otherwise."); |
| 11926 | |
| 11927 | static PyObject* |
| 11928 | unicode_isprintable(PyObject *self) |
| 11929 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11930 | Py_ssize_t i, length; |
| 11931 | int kind; |
| 11932 | void *data; |
| 11933 | |
| 11934 | if (PyUnicode_READY(self) == -1) |
| 11935 | return NULL; |
| 11936 | length = PyUnicode_GET_LENGTH(self); |
| 11937 | kind = PyUnicode_KIND(self); |
| 11938 | data = PyUnicode_DATA(self); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11939 | |
| 11940 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11941 | if (length == 1) |
| 11942 | return PyBool_FromLong( |
| 11943 | Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, 0))); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11944 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11945 | for (i = 0; i < length; i++) { |
| 11946 | if (!Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, i))) { |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11947 | Py_RETURN_FALSE; |
| 11948 | } |
| 11949 | } |
| 11950 | Py_RETURN_TRUE; |
| 11951 | } |
| 11952 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11953 | PyDoc_STRVAR(join__doc__, |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 11954 | "S.join(iterable) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11955 | \n\ |
| 11956 | 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] | 11957 | iterable. The separator between elements is S."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11958 | |
| 11959 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11960 | unicode_join(PyObject *self, PyObject *data) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11961 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11962 | return PyUnicode_Join(self, data); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11963 | } |
| 11964 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11965 | static Py_ssize_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11966 | unicode_length(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11967 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11968 | if (PyUnicode_READY(self) == -1) |
| 11969 | return -1; |
| 11970 | return PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11971 | } |
| 11972 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11973 | PyDoc_STRVAR(ljust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11974 | "S.ljust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11975 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 11976 | 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] | 11977 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11978 | |
| 11979 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11980 | unicode_ljust(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11981 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11982 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11983 | Py_UCS4 fillchar = ' '; |
| 11984 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11985 | if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11986 | return NULL; |
| 11987 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 11988 | if (PyUnicode_READY(self) == -1) |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11989 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11990 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11991 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 11992 | return unicode_result_unchanged(self); |
| 11993 | |
| 11994 | return pad(self, 0, width - PyUnicode_GET_LENGTH(self), fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11995 | } |
| 11996 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11997 | PyDoc_STRVAR(lower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11998 | "S.lower() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11999 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12000 | Return a copy of the string S converted to lowercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12001 | |
| 12002 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12003 | unicode_lower(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12004 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 12005 | if (PyUnicode_READY(self) == -1) |
| 12006 | return NULL; |
| 12007 | if (PyUnicode_IS_ASCII(self)) |
| 12008 | return ascii_upper_or_lower(self, 1); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 12009 | return case_operation(self, do_lower); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12010 | } |
| 12011 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12012 | #define LEFTSTRIP 0 |
| 12013 | #define RIGHTSTRIP 1 |
| 12014 | #define BOTHSTRIP 2 |
| 12015 | |
| 12016 | /* Arrays indexed by above */ |
| 12017 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 12018 | |
| 12019 | #define STRIPNAME(i) (stripformat[i]+3) |
| 12020 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12021 | /* externally visible for str.strip(unicode) */ |
| 12022 | PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12023 | _PyUnicode_XStrip(PyObject *self, int striptype, PyObject *sepobj) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12024 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12025 | void *data; |
| 12026 | int kind; |
| 12027 | Py_ssize_t i, j, len; |
| 12028 | BLOOM_MASK sepmask; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12029 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12030 | if (PyUnicode_READY(self) == -1 || PyUnicode_READY(sepobj) == -1) |
| 12031 | return NULL; |
| 12032 | |
| 12033 | kind = PyUnicode_KIND(self); |
| 12034 | data = PyUnicode_DATA(self); |
| 12035 | len = PyUnicode_GET_LENGTH(self); |
| 12036 | sepmask = make_bloom_mask(PyUnicode_KIND(sepobj), |
| 12037 | PyUnicode_DATA(sepobj), |
| 12038 | PyUnicode_GET_LENGTH(sepobj)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12039 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12040 | i = 0; |
| 12041 | if (striptype != RIGHTSTRIP) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12042 | while (i < len && |
| 12043 | BLOOM_MEMBER(sepmask, PyUnicode_READ(kind, data, i), sepobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12044 | i++; |
| 12045 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12046 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12047 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12048 | j = len; |
| 12049 | if (striptype != LEFTSTRIP) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12050 | do { |
| 12051 | j--; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12052 | } while (j >= i && |
| 12053 | BLOOM_MEMBER(sepmask, PyUnicode_READ(kind, data, j), sepobj)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12054 | j++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12055 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12056 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12057 | return PyUnicode_Substring(self, i, j); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12058 | } |
| 12059 | |
| 12060 | PyObject* |
| 12061 | PyUnicode_Substring(PyObject *self, Py_ssize_t start, Py_ssize_t end) |
| 12062 | { |
| 12063 | unsigned char *data; |
| 12064 | int kind; |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 12065 | Py_ssize_t length; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12066 | |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 12067 | if (PyUnicode_READY(self) == -1) |
| 12068 | return NULL; |
| 12069 | |
Victor Stinner | 684d5fd | 2012-05-03 02:32:34 +0200 | [diff] [blame] | 12070 | length = PyUnicode_GET_LENGTH(self); |
| 12071 | end = Py_MIN(end, length); |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 12072 | |
Victor Stinner | 684d5fd | 2012-05-03 02:32:34 +0200 | [diff] [blame] | 12073 | if (start == 0 && end == length) |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12074 | return unicode_result_unchanged(self); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12075 | |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 12076 | if (start < 0 || end < 0) { |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 12077 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 12078 | return NULL; |
| 12079 | } |
Victor Stinner | 684d5fd | 2012-05-03 02:32:34 +0200 | [diff] [blame] | 12080 | if (start >= length || end < start) { |
Victor Stinner | 3a7f7977 | 2012-05-03 03:36:40 +0200 | [diff] [blame^] | 12081 | Py_INCREF(unicode_empty); |
| 12082 | return unicode_empty; |
Victor Stinner | 684d5fd | 2012-05-03 02:32:34 +0200 | [diff] [blame] | 12083 | } |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 12084 | |
Victor Stinner | 684d5fd | 2012-05-03 02:32:34 +0200 | [diff] [blame] | 12085 | length = end - start; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 12086 | if (PyUnicode_IS_ASCII(self)) { |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 12087 | data = PyUnicode_1BYTE_DATA(self); |
| 12088 | return unicode_fromascii(data + start, length); |
| 12089 | } |
| 12090 | else { |
| 12091 | kind = PyUnicode_KIND(self); |
| 12092 | data = PyUnicode_1BYTE_DATA(self); |
| 12093 | return PyUnicode_FromKindAndData(kind, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 12094 | data + kind * start, |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 12095 | length); |
| 12096 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12097 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12098 | |
| 12099 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12100 | do_strip(PyObject *self, int striptype) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12101 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12102 | int kind; |
| 12103 | void *data; |
| 12104 | Py_ssize_t len, i, j; |
| 12105 | |
| 12106 | if (PyUnicode_READY(self) == -1) |
| 12107 | return NULL; |
| 12108 | |
| 12109 | kind = PyUnicode_KIND(self); |
| 12110 | data = PyUnicode_DATA(self); |
| 12111 | len = PyUnicode_GET_LENGTH(self); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12112 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12113 | i = 0; |
| 12114 | if (striptype != RIGHTSTRIP) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12115 | while (i < len && Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, i))) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12116 | i++; |
| 12117 | } |
| 12118 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12119 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12120 | j = len; |
| 12121 | if (striptype != LEFTSTRIP) { |
| 12122 | do { |
| 12123 | j--; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12124 | } while (j >= i && Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, j))); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12125 | j++; |
| 12126 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12127 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12128 | return PyUnicode_Substring(self, i, j); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12129 | } |
| 12130 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12131 | |
| 12132 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12133 | do_argstrip(PyObject *self, int striptype, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12134 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12135 | PyObject *sep = NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12136 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12137 | if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) |
| 12138 | return NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12139 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12140 | if (sep != NULL && sep != Py_None) { |
| 12141 | if (PyUnicode_Check(sep)) |
| 12142 | return _PyUnicode_XStrip(self, striptype, sep); |
| 12143 | else { |
| 12144 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12145 | "%s arg must be None or str", |
| 12146 | STRIPNAME(striptype)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12147 | return NULL; |
| 12148 | } |
| 12149 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12150 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12151 | return do_strip(self, striptype); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12152 | } |
| 12153 | |
| 12154 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12155 | PyDoc_STRVAR(strip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12156 | "S.strip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12157 | \n\ |
| 12158 | Return a copy of the string S with leading and trailing\n\ |
| 12159 | whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12160 | 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] | 12161 | |
| 12162 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12163 | unicode_strip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12164 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12165 | if (PyTuple_GET_SIZE(args) == 0) |
| 12166 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 12167 | else |
| 12168 | return do_argstrip(self, BOTHSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12169 | } |
| 12170 | |
| 12171 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12172 | PyDoc_STRVAR(lstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12173 | "S.lstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12174 | \n\ |
| 12175 | Return a copy of the string S with leading whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12176 | 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] | 12177 | |
| 12178 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12179 | unicode_lstrip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12180 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12181 | if (PyTuple_GET_SIZE(args) == 0) |
| 12182 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 12183 | else |
| 12184 | return do_argstrip(self, LEFTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12185 | } |
| 12186 | |
| 12187 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12188 | PyDoc_STRVAR(rstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12189 | "S.rstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12190 | \n\ |
| 12191 | Return a copy of the string S with trailing whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12192 | 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] | 12193 | |
| 12194 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12195 | unicode_rstrip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12196 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12197 | if (PyTuple_GET_SIZE(args) == 0) |
| 12198 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 12199 | else |
| 12200 | return do_argstrip(self, RIGHTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12201 | } |
| 12202 | |
| 12203 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12204 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12205 | unicode_repeat(PyObject *str, Py_ssize_t len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12206 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12207 | PyObject *u; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12208 | Py_ssize_t nchars, n; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12209 | |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 12210 | if (len < 1) { |
| 12211 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 12212 | return unicode_empty; |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 12213 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12214 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12215 | /* no repeat, return original string */ |
| 12216 | if (len == 1) |
| 12217 | return unicode_result_unchanged(str); |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 12218 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 12219 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12220 | return NULL; |
| 12221 | |
Victor Stinner | c759f3e | 2011-10-01 03:09:58 +0200 | [diff] [blame] | 12222 | if (PyUnicode_GET_LENGTH(str) > PY_SSIZE_T_MAX / len) { |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 12223 | PyErr_SetString(PyExc_OverflowError, |
| 12224 | "repeated string is too long"); |
| 12225 | return NULL; |
| 12226 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12227 | nchars = len * PyUnicode_GET_LENGTH(str); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 12228 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12229 | u = PyUnicode_New(nchars, PyUnicode_MAX_CHAR_VALUE(str)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12230 | if (!u) |
| 12231 | return NULL; |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 12232 | assert(PyUnicode_KIND(u) == PyUnicode_KIND(str)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12233 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12234 | if (PyUnicode_GET_LENGTH(str) == 1) { |
| 12235 | const int kind = PyUnicode_KIND(str); |
| 12236 | const Py_UCS4 fill_char = PyUnicode_READ(kind, PyUnicode_DATA(str), 0); |
Victor Stinner | 73f53b5 | 2011-12-18 03:26:31 +0100 | [diff] [blame] | 12237 | if (kind == PyUnicode_1BYTE_KIND) { |
| 12238 | void *to = PyUnicode_DATA(u); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 12239 | memset(to, (unsigned char)fill_char, len); |
Victor Stinner | 73f53b5 | 2011-12-18 03:26:31 +0100 | [diff] [blame] | 12240 | } |
| 12241 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 12242 | Py_UCS2 *ucs2 = PyUnicode_2BYTE_DATA(u); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 12243 | for (n = 0; n < len; ++n) |
Victor Stinner | 73f53b5 | 2011-12-18 03:26:31 +0100 | [diff] [blame] | 12244 | ucs2[n] = fill_char; |
| 12245 | } else { |
| 12246 | Py_UCS4 *ucs4 = PyUnicode_4BYTE_DATA(u); |
| 12247 | assert(kind == PyUnicode_4BYTE_KIND); |
| 12248 | for (n = 0; n < len; ++n) |
| 12249 | ucs4[n] = fill_char; |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 12250 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12251 | } |
| 12252 | else { |
| 12253 | /* number of characters copied this far */ |
| 12254 | Py_ssize_t done = PyUnicode_GET_LENGTH(str); |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 12255 | const Py_ssize_t char_size = PyUnicode_KIND(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12256 | char *to = (char *) PyUnicode_DATA(u); |
| 12257 | Py_MEMCPY(to, PyUnicode_DATA(str), |
| 12258 | PyUnicode_GET_LENGTH(str) * char_size); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12259 | while (done < nchars) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12260 | n = (done <= nchars-done) ? done : nchars-done; |
| 12261 | Py_MEMCPY(to + (done * char_size), to, n * char_size); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12262 | done += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12263 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12264 | } |
| 12265 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12266 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12267 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12268 | } |
| 12269 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12270 | PyObject * |
| 12271 | PyUnicode_Replace(PyObject *obj, |
| 12272 | PyObject *subobj, |
| 12273 | PyObject *replobj, |
| 12274 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12275 | { |
| 12276 | PyObject *self; |
| 12277 | PyObject *str1; |
| 12278 | PyObject *str2; |
| 12279 | PyObject *result; |
| 12280 | |
| 12281 | self = PyUnicode_FromObject(obj); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12282 | if (self == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12283 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12284 | str1 = PyUnicode_FromObject(subobj); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12285 | if (str1 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12286 | Py_DECREF(self); |
| 12287 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12288 | } |
| 12289 | str2 = PyUnicode_FromObject(replobj); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12290 | if (str2 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12291 | Py_DECREF(self); |
| 12292 | Py_DECREF(str1); |
| 12293 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12294 | } |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12295 | if (PyUnicode_READY(self) == -1 || |
| 12296 | PyUnicode_READY(str1) == -1 || |
| 12297 | PyUnicode_READY(str2) == -1) |
| 12298 | result = NULL; |
| 12299 | else |
| 12300 | result = replace(self, str1, str2, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12301 | Py_DECREF(self); |
| 12302 | Py_DECREF(str1); |
| 12303 | Py_DECREF(str2); |
| 12304 | return result; |
| 12305 | } |
| 12306 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12307 | PyDoc_STRVAR(replace__doc__, |
Ezio Melotti | c1897e7 | 2010-06-26 18:50:39 +0000 | [diff] [blame] | 12308 | "S.replace(old, new[, count]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12309 | \n\ |
| 12310 | Return a copy of S with all occurrences of substring\n\ |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 12311 | old replaced by new. If the optional argument count is\n\ |
| 12312 | given, only the first count occurrences are replaced."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12313 | |
| 12314 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12315 | unicode_replace(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12316 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12317 | PyObject *str1; |
| 12318 | PyObject *str2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12319 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12320 | PyObject *result; |
| 12321 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12322 | if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12323 | return NULL; |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12324 | if (PyUnicode_READY(self) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12325 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12326 | str1 = PyUnicode_FromObject(str1); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12327 | if (str1 == NULL) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12328 | return NULL; |
| 12329 | str2 = PyUnicode_FromObject(str2); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12330 | if (str2 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12331 | Py_DECREF(str1); |
| 12332 | return NULL; |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 12333 | } |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12334 | if (PyUnicode_READY(str1) == -1 || PyUnicode_READY(str2) == -1) |
| 12335 | result = NULL; |
| 12336 | else |
| 12337 | result = replace(self, str1, str2, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12338 | |
| 12339 | Py_DECREF(str1); |
| 12340 | Py_DECREF(str2); |
| 12341 | return result; |
| 12342 | } |
| 12343 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12344 | static PyObject * |
| 12345 | unicode_repr(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12346 | { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12347 | PyObject *repr; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12348 | Py_ssize_t isize; |
| 12349 | Py_ssize_t osize, squote, dquote, i, o; |
| 12350 | Py_UCS4 max, quote; |
| 12351 | int ikind, okind; |
| 12352 | void *idata, *odata; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12353 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12354 | if (PyUnicode_READY(unicode) == -1) |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12355 | return NULL; |
| 12356 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12357 | isize = PyUnicode_GET_LENGTH(unicode); |
| 12358 | idata = PyUnicode_DATA(unicode); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12359 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12360 | /* Compute length of output, quote characters, and |
| 12361 | maximum character */ |
| 12362 | osize = 2; /* quotes */ |
| 12363 | max = 127; |
| 12364 | squote = dquote = 0; |
| 12365 | ikind = PyUnicode_KIND(unicode); |
| 12366 | for (i = 0; i < isize; i++) { |
| 12367 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
| 12368 | switch (ch) { |
| 12369 | case '\'': squote++; osize++; break; |
| 12370 | case '"': dquote++; osize++; break; |
| 12371 | case '\\': case '\t': case '\r': case '\n': |
| 12372 | osize += 2; break; |
| 12373 | default: |
| 12374 | /* Fast-path ASCII */ |
| 12375 | if (ch < ' ' || ch == 0x7f) |
| 12376 | osize += 4; /* \xHH */ |
| 12377 | else if (ch < 0x7f) |
| 12378 | osize++; |
| 12379 | else if (Py_UNICODE_ISPRINTABLE(ch)) { |
| 12380 | osize++; |
| 12381 | max = ch > max ? ch : max; |
| 12382 | } |
| 12383 | else if (ch < 0x100) |
| 12384 | osize += 4; /* \xHH */ |
| 12385 | else if (ch < 0x10000) |
| 12386 | osize += 6; /* \uHHHH */ |
| 12387 | else |
| 12388 | osize += 10; /* \uHHHHHHHH */ |
| 12389 | } |
| 12390 | } |
| 12391 | |
| 12392 | quote = '\''; |
| 12393 | if (squote) { |
| 12394 | if (dquote) |
| 12395 | /* Both squote and dquote present. Use squote, |
| 12396 | and escape them */ |
| 12397 | osize += squote; |
| 12398 | else |
| 12399 | quote = '"'; |
| 12400 | } |
| 12401 | |
| 12402 | repr = PyUnicode_New(osize, max); |
| 12403 | if (repr == NULL) |
| 12404 | return NULL; |
| 12405 | okind = PyUnicode_KIND(repr); |
| 12406 | odata = PyUnicode_DATA(repr); |
| 12407 | |
| 12408 | PyUnicode_WRITE(okind, odata, 0, quote); |
| 12409 | PyUnicode_WRITE(okind, odata, osize-1, quote); |
| 12410 | |
| 12411 | for (i = 0, o = 1; i < isize; i++) { |
| 12412 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12413 | |
| 12414 | /* Escape quotes and backslashes */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12415 | if ((ch == quote) || (ch == '\\')) { |
| 12416 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12417 | PyUnicode_WRITE(okind, odata, o++, ch); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12418 | continue; |
| 12419 | } |
| 12420 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12421 | /* Map special whitespace to '\t', \n', '\r' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12422 | if (ch == '\t') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12423 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12424 | PyUnicode_WRITE(okind, odata, o++, 't'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12425 | } |
| 12426 | else if (ch == '\n') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12427 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12428 | PyUnicode_WRITE(okind, odata, o++, 'n'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12429 | } |
| 12430 | else if (ch == '\r') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12431 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12432 | PyUnicode_WRITE(okind, odata, o++, 'r'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12433 | } |
| 12434 | |
| 12435 | /* Map non-printable US ASCII to '\xhh' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12436 | else if (ch < ' ' || ch == 0x7F) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12437 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12438 | PyUnicode_WRITE(okind, odata, o++, 'x'); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 12439 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0x000F]); |
| 12440 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0x000F]); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12441 | } |
| 12442 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12443 | /* Copy ASCII characters as-is */ |
| 12444 | else if (ch < 0x7F) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12445 | PyUnicode_WRITE(okind, odata, o++, ch); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12446 | } |
| 12447 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12448 | /* Non-ASCII characters */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12449 | else { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12450 | /* Map Unicode whitespace and control characters |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12451 | (categories Z* and C* except ASCII space) |
| 12452 | */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12453 | if (!Py_UNICODE_ISPRINTABLE(ch)) { |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12454 | /* Map 8-bit characters to '\xhh' */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12455 | if (ch <= 0xff) { |
| 12456 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12457 | PyUnicode_WRITE(okind, odata, o++, 'x'); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 12458 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0x000F]); |
| 12459 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0x000F]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12460 | } |
| 12461 | /* Map 21-bit characters to '\U00xxxxxx' */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12462 | else if (ch >= 0x10000) { |
| 12463 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12464 | PyUnicode_WRITE(okind, odata, o++, 'U'); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 12465 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 28) & 0xF]); |
| 12466 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 24) & 0xF]); |
| 12467 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 20) & 0xF]); |
| 12468 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 16) & 0xF]); |
| 12469 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 12) & 0xF]); |
| 12470 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 8) & 0xF]); |
| 12471 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0xF]); |
| 12472 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0xF]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12473 | } |
| 12474 | /* Map 16-bit characters to '\uxxxx' */ |
| 12475 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12476 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12477 | PyUnicode_WRITE(okind, odata, o++, 'u'); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 12478 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 12) & 0xF]); |
| 12479 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 8) & 0xF]); |
| 12480 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0xF]); |
| 12481 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0xF]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12482 | } |
| 12483 | } |
| 12484 | /* Copy characters as-is */ |
| 12485 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12486 | PyUnicode_WRITE(okind, odata, o++, ch); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12487 | } |
| 12488 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12489 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12490 | /* Closing quote already added at the beginning */ |
Victor Stinner | 05d1189 | 2011-10-06 01:13:58 +0200 | [diff] [blame] | 12491 | assert(_PyUnicode_CheckConsistency(repr, 1)); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12492 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12493 | } |
| 12494 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12495 | PyDoc_STRVAR(rfind__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12496 | "S.rfind(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12497 | \n\ |
| 12498 | Return the highest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 12499 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12500 | arguments start and end are interpreted as in slice notation.\n\ |
| 12501 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12502 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12503 | |
| 12504 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12505 | unicode_rfind(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12506 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12507 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 12508 | Py_ssize_t start; |
| 12509 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12510 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12511 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12512 | if (!stringlib_parse_args_finds_unicode("rfind", args, &substring, |
| 12513 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12514 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12515 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12516 | if (PyUnicode_READY(self) == -1) |
| 12517 | return NULL; |
| 12518 | if (PyUnicode_READY(substring) == -1) |
| 12519 | return NULL; |
| 12520 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12521 | result = any_find_slice(-1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12522 | |
| 12523 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12524 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12525 | if (result == -2) |
| 12526 | return NULL; |
| 12527 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12528 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12529 | } |
| 12530 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12531 | PyDoc_STRVAR(rindex__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12532 | "S.rindex(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12533 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12534 | 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] | 12535 | |
| 12536 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12537 | unicode_rindex(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12538 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12539 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 12540 | Py_ssize_t start; |
| 12541 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12542 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12543 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12544 | if (!stringlib_parse_args_finds_unicode("rindex", args, &substring, |
| 12545 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12546 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12547 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12548 | if (PyUnicode_READY(self) == -1) |
| 12549 | return NULL; |
| 12550 | if (PyUnicode_READY(substring) == -1) |
| 12551 | return NULL; |
| 12552 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12553 | result = any_find_slice(-1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12554 | |
| 12555 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12556 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12557 | if (result == -2) |
| 12558 | return NULL; |
| 12559 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12560 | if (result < 0) { |
| 12561 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 12562 | return NULL; |
| 12563 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12564 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12565 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12566 | } |
| 12567 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12568 | PyDoc_STRVAR(rjust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12569 | "S.rjust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12570 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 12571 | 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] | 12572 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12573 | |
| 12574 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12575 | unicode_rjust(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12576 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12577 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12578 | Py_UCS4 fillchar = ' '; |
| 12579 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 12580 | 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] | 12581 | return NULL; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 12582 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 12583 | if (PyUnicode_READY(self) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12584 | return NULL; |
| 12585 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12586 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 12587 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12588 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12589 | return pad(self, width - PyUnicode_GET_LENGTH(self), 0, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12590 | } |
| 12591 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12592 | PyObject * |
| 12593 | PyUnicode_Split(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12594 | { |
| 12595 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 12596 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12597 | s = PyUnicode_FromObject(s); |
| 12598 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12599 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12600 | if (sep != NULL) { |
| 12601 | sep = PyUnicode_FromObject(sep); |
| 12602 | if (sep == NULL) { |
| 12603 | Py_DECREF(s); |
| 12604 | return NULL; |
| 12605 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12606 | } |
| 12607 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12608 | result = split(s, sep, maxsplit); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12609 | |
| 12610 | Py_DECREF(s); |
| 12611 | Py_XDECREF(sep); |
| 12612 | return result; |
| 12613 | } |
| 12614 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12615 | PyDoc_STRVAR(split__doc__, |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12616 | "S.split(sep=None, maxsplit=-1) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12617 | \n\ |
| 12618 | Return a list of the words in S, using sep as the\n\ |
| 12619 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 12620 | 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] | 12621 | whitespace string is a separator and empty strings are\n\ |
| 12622 | removed from the result."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12623 | |
| 12624 | static PyObject* |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12625 | unicode_split(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12626 | { |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12627 | static char *kwlist[] = {"sep", "maxsplit", 0}; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12628 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12629 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12630 | |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12631 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|On:split", |
| 12632 | kwlist, &substring, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12633 | return NULL; |
| 12634 | |
| 12635 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12636 | return split(self, NULL, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12637 | else if (PyUnicode_Check(substring)) |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12638 | return split(self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12639 | else |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12640 | return PyUnicode_Split(self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12641 | } |
| 12642 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12643 | PyObject * |
| 12644 | PyUnicode_Partition(PyObject *str_in, PyObject *sep_in) |
| 12645 | { |
| 12646 | PyObject* str_obj; |
| 12647 | PyObject* sep_obj; |
| 12648 | PyObject* out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12649 | int kind1, kind2, kind; |
| 12650 | void *buf1 = NULL, *buf2 = NULL; |
| 12651 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12652 | |
| 12653 | str_obj = PyUnicode_FromObject(str_in); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12654 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12655 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12656 | sep_obj = PyUnicode_FromObject(sep_in); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12657 | if (!sep_obj) { |
| 12658 | Py_DECREF(str_obj); |
| 12659 | return NULL; |
| 12660 | } |
| 12661 | if (PyUnicode_READY(sep_obj) == -1 || PyUnicode_READY(str_obj) == -1) { |
| 12662 | Py_DECREF(sep_obj); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12663 | Py_DECREF(str_obj); |
| 12664 | return NULL; |
| 12665 | } |
| 12666 | |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12667 | kind1 = PyUnicode_KIND(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12668 | kind2 = PyUnicode_KIND(sep_obj); |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12669 | kind = Py_MAX(kind1, kind2); |
| 12670 | buf1 = PyUnicode_DATA(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12671 | if (kind1 != kind) |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12672 | buf1 = _PyUnicode_AsKind(str_obj, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12673 | if (!buf1) |
| 12674 | goto onError; |
| 12675 | buf2 = PyUnicode_DATA(sep_obj); |
| 12676 | if (kind2 != kind) |
| 12677 | buf2 = _PyUnicode_AsKind(sep_obj, kind); |
| 12678 | if (!buf2) |
| 12679 | goto onError; |
| 12680 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 12681 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
| 12682 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 12683 | switch (PyUnicode_KIND(str_obj)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12684 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 12685 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sep_obj)) |
| 12686 | out = asciilib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12687 | else |
| 12688 | 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] | 12689 | break; |
| 12690 | case PyUnicode_2BYTE_KIND: |
| 12691 | out = ucs2lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12692 | break; |
| 12693 | case PyUnicode_4BYTE_KIND: |
| 12694 | out = ucs4lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12695 | break; |
| 12696 | default: |
| 12697 | assert(0); |
| 12698 | out = 0; |
| 12699 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12700 | |
| 12701 | Py_DECREF(sep_obj); |
| 12702 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12703 | if (kind1 != kind) |
| 12704 | PyMem_Free(buf1); |
| 12705 | if (kind2 != kind) |
| 12706 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12707 | |
| 12708 | return out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12709 | onError: |
| 12710 | Py_DECREF(sep_obj); |
| 12711 | Py_DECREF(str_obj); |
| 12712 | if (kind1 != kind && buf1) |
| 12713 | PyMem_Free(buf1); |
| 12714 | if (kind2 != kind && buf2) |
| 12715 | PyMem_Free(buf2); |
| 12716 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12717 | } |
| 12718 | |
| 12719 | |
| 12720 | PyObject * |
| 12721 | PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in) |
| 12722 | { |
| 12723 | PyObject* str_obj; |
| 12724 | PyObject* sep_obj; |
| 12725 | PyObject* out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12726 | int kind1, kind2, kind; |
| 12727 | void *buf1 = NULL, *buf2 = NULL; |
| 12728 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12729 | |
| 12730 | str_obj = PyUnicode_FromObject(str_in); |
| 12731 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12732 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12733 | sep_obj = PyUnicode_FromObject(sep_in); |
| 12734 | if (!sep_obj) { |
| 12735 | Py_DECREF(str_obj); |
| 12736 | return NULL; |
| 12737 | } |
| 12738 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12739 | kind1 = PyUnicode_KIND(str_in); |
| 12740 | kind2 = PyUnicode_KIND(sep_obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 12741 | kind = Py_MAX(kind1, kind2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12742 | buf1 = PyUnicode_DATA(str_in); |
| 12743 | if (kind1 != kind) |
| 12744 | buf1 = _PyUnicode_AsKind(str_in, kind); |
| 12745 | if (!buf1) |
| 12746 | goto onError; |
| 12747 | buf2 = PyUnicode_DATA(sep_obj); |
| 12748 | if (kind2 != kind) |
| 12749 | buf2 = _PyUnicode_AsKind(sep_obj, kind); |
| 12750 | if (!buf2) |
| 12751 | goto onError; |
| 12752 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 12753 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
| 12754 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 12755 | switch (PyUnicode_KIND(str_in)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12756 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 12757 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sep_obj)) |
| 12758 | out = asciilib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12759 | else |
| 12760 | 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] | 12761 | break; |
| 12762 | case PyUnicode_2BYTE_KIND: |
| 12763 | out = ucs2lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12764 | break; |
| 12765 | case PyUnicode_4BYTE_KIND: |
| 12766 | out = ucs4lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12767 | break; |
| 12768 | default: |
| 12769 | assert(0); |
| 12770 | out = 0; |
| 12771 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12772 | |
| 12773 | Py_DECREF(sep_obj); |
| 12774 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12775 | if (kind1 != kind) |
| 12776 | PyMem_Free(buf1); |
| 12777 | if (kind2 != kind) |
| 12778 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12779 | |
| 12780 | return out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12781 | onError: |
| 12782 | Py_DECREF(sep_obj); |
| 12783 | Py_DECREF(str_obj); |
| 12784 | if (kind1 != kind && buf1) |
| 12785 | PyMem_Free(buf1); |
| 12786 | if (kind2 != kind && buf2) |
| 12787 | PyMem_Free(buf2); |
| 12788 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12789 | } |
| 12790 | |
| 12791 | PyDoc_STRVAR(partition__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12792 | "S.partition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12793 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 12794 | 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] | 12795 | 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] | 12796 | found, return S and two empty strings."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12797 | |
| 12798 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12799 | unicode_partition(PyObject *self, PyObject *separator) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12800 | { |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12801 | return PyUnicode_Partition(self, separator); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12802 | } |
| 12803 | |
| 12804 | PyDoc_STRVAR(rpartition__doc__, |
Ezio Melotti | 5b2b242 | 2010-01-25 11:58:28 +0000 | [diff] [blame] | 12805 | "S.rpartition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12806 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 12807 | 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] | 12808 | 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] | 12809 | separator is not found, return two empty strings and S."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12810 | |
| 12811 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12812 | unicode_rpartition(PyObject *self, PyObject *separator) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12813 | { |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12814 | return PyUnicode_RPartition(self, separator); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12815 | } |
| 12816 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12817 | PyObject * |
| 12818 | PyUnicode_RSplit(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12819 | { |
| 12820 | PyObject *result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12821 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12822 | s = PyUnicode_FromObject(s); |
| 12823 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12824 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12825 | if (sep != NULL) { |
| 12826 | sep = PyUnicode_FromObject(sep); |
| 12827 | if (sep == NULL) { |
| 12828 | Py_DECREF(s); |
| 12829 | return NULL; |
| 12830 | } |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12831 | } |
| 12832 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12833 | result = rsplit(s, sep, maxsplit); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12834 | |
| 12835 | Py_DECREF(s); |
| 12836 | Py_XDECREF(sep); |
| 12837 | return result; |
| 12838 | } |
| 12839 | |
| 12840 | PyDoc_STRVAR(rsplit__doc__, |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12841 | "S.rsplit(sep=None, maxsplit=-1) -> list of strings\n\ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12842 | \n\ |
| 12843 | Return a list of the words in S, using sep as the\n\ |
| 12844 | delimiter string, starting at the end of the string and\n\ |
| 12845 | working to the front. If maxsplit is given, at most maxsplit\n\ |
| 12846 | splits are done. If sep is not specified, any whitespace string\n\ |
| 12847 | is a separator."); |
| 12848 | |
| 12849 | static PyObject* |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12850 | unicode_rsplit(PyObject *self, PyObject *args, PyObject *kwds) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12851 | { |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12852 | static char *kwlist[] = {"sep", "maxsplit", 0}; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12853 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12854 | Py_ssize_t maxcount = -1; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12855 | |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12856 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|On:rsplit", |
| 12857 | kwlist, &substring, &maxcount)) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12858 | return NULL; |
| 12859 | |
| 12860 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12861 | return rsplit(self, NULL, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12862 | else if (PyUnicode_Check(substring)) |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12863 | return rsplit(self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12864 | else |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12865 | return PyUnicode_RSplit(self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12866 | } |
| 12867 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12868 | PyDoc_STRVAR(splitlines__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12869 | "S.splitlines([keepends]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12870 | \n\ |
| 12871 | 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] | 12872 | 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] | 12873 | is given and true."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12874 | |
| 12875 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12876 | unicode_splitlines(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12877 | { |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12878 | static char *kwlist[] = {"keepends", 0}; |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 12879 | int keepends = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12880 | |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12881 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|i:splitlines", |
| 12882 | kwlist, &keepends)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12883 | return NULL; |
| 12884 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12885 | return PyUnicode_Splitlines(self, keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12886 | } |
| 12887 | |
| 12888 | static |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 12889 | PyObject *unicode_str(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12890 | { |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12891 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12892 | } |
| 12893 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12894 | PyDoc_STRVAR(swapcase__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12895 | "S.swapcase() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12896 | \n\ |
| 12897 | 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] | 12898 | and vice versa."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12899 | |
| 12900 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12901 | unicode_swapcase(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12902 | { |
Benjamin Peterson | eea4846 | 2012-01-16 14:28:50 -0500 | [diff] [blame] | 12903 | if (PyUnicode_READY(self) == -1) |
| 12904 | return NULL; |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 12905 | return case_operation(self, do_swapcase); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12906 | } |
| 12907 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12908 | PyDoc_STRVAR(maketrans__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12909 | "str.maketrans(x[, y[, z]]) -> dict (static method)\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12910 | \n\ |
| 12911 | Return a translation table usable for str.translate().\n\ |
| 12912 | If there is only one argument, it must be a dictionary mapping Unicode\n\ |
| 12913 | ordinals (integers) or characters to Unicode ordinals, strings or None.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12914 | Character keys will be then converted to ordinals.\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12915 | If there are two arguments, they must be strings of equal length, and\n\ |
| 12916 | in the resulting dictionary, each character in x will be mapped to the\n\ |
| 12917 | character at the same position in y. If there is a third argument, it\n\ |
| 12918 | must be a string, whose characters will be mapped to None in the result."); |
| 12919 | |
| 12920 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12921 | unicode_maketrans(PyObject *null, PyObject *args) |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12922 | { |
| 12923 | PyObject *x, *y = NULL, *z = NULL; |
| 12924 | PyObject *new = NULL, *key, *value; |
| 12925 | Py_ssize_t i = 0; |
| 12926 | int res; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12927 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12928 | if (!PyArg_ParseTuple(args, "O|UU:maketrans", &x, &y, &z)) |
| 12929 | return NULL; |
| 12930 | new = PyDict_New(); |
| 12931 | if (!new) |
| 12932 | return NULL; |
| 12933 | if (y != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12934 | int x_kind, y_kind, z_kind; |
| 12935 | void *x_data, *y_data, *z_data; |
| 12936 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12937 | /* x must be a string too, of equal length */ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12938 | if (!PyUnicode_Check(x)) { |
| 12939 | PyErr_SetString(PyExc_TypeError, "first maketrans argument must " |
| 12940 | "be a string if there is a second argument"); |
| 12941 | goto err; |
| 12942 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12943 | if (PyUnicode_GET_LENGTH(x) != PyUnicode_GET_LENGTH(y)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12944 | PyErr_SetString(PyExc_ValueError, "the first two maketrans " |
| 12945 | "arguments must have equal length"); |
| 12946 | goto err; |
| 12947 | } |
| 12948 | /* 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] | 12949 | x_kind = PyUnicode_KIND(x); |
| 12950 | y_kind = PyUnicode_KIND(y); |
| 12951 | x_data = PyUnicode_DATA(x); |
| 12952 | y_data = PyUnicode_DATA(y); |
| 12953 | for (i = 0; i < PyUnicode_GET_LENGTH(x); i++) { |
| 12954 | key = PyLong_FromLong(PyUnicode_READ(x_kind, x_data, i)); |
Benjamin Peterson | 53aa1d7 | 2011-12-20 13:29:45 -0600 | [diff] [blame] | 12955 | if (!key) |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12956 | goto err; |
Benjamin Peterson | 822c790 | 2011-12-20 13:32:50 -0600 | [diff] [blame] | 12957 | value = PyLong_FromLong(PyUnicode_READ(y_kind, y_data, i)); |
Benjamin Peterson | 53aa1d7 | 2011-12-20 13:29:45 -0600 | [diff] [blame] | 12958 | if (!value) { |
| 12959 | Py_DECREF(key); |
| 12960 | goto err; |
| 12961 | } |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12962 | res = PyDict_SetItem(new, key, value); |
| 12963 | Py_DECREF(key); |
| 12964 | Py_DECREF(value); |
| 12965 | if (res < 0) |
| 12966 | goto err; |
| 12967 | } |
| 12968 | /* create entries for deleting chars in z */ |
| 12969 | if (z != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12970 | z_kind = PyUnicode_KIND(z); |
| 12971 | z_data = PyUnicode_DATA(z); |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 12972 | for (i = 0; i < PyUnicode_GET_LENGTH(z); i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12973 | key = PyLong_FromLong(PyUnicode_READ(z_kind, z_data, i)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12974 | if (!key) |
| 12975 | goto err; |
| 12976 | res = PyDict_SetItem(new, key, Py_None); |
| 12977 | Py_DECREF(key); |
| 12978 | if (res < 0) |
| 12979 | goto err; |
| 12980 | } |
| 12981 | } |
| 12982 | } else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12983 | int kind; |
| 12984 | void *data; |
| 12985 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12986 | /* x must be a dict */ |
Raymond Hettinger | 3ad0576 | 2009-05-29 22:11:22 +0000 | [diff] [blame] | 12987 | if (!PyDict_CheckExact(x)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12988 | PyErr_SetString(PyExc_TypeError, "if you give only one argument " |
| 12989 | "to maketrans it must be a dict"); |
| 12990 | goto err; |
| 12991 | } |
| 12992 | /* copy entries into the new dict, converting string keys to int keys */ |
| 12993 | while (PyDict_Next(x, &i, &key, &value)) { |
| 12994 | if (PyUnicode_Check(key)) { |
| 12995 | /* convert string keys to integer keys */ |
| 12996 | PyObject *newkey; |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 12997 | if (PyUnicode_GET_LENGTH(key) != 1) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12998 | PyErr_SetString(PyExc_ValueError, "string keys in translate " |
| 12999 | "table must be of length 1"); |
| 13000 | goto err; |
| 13001 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13002 | kind = PyUnicode_KIND(key); |
| 13003 | data = PyUnicode_DATA(key); |
| 13004 | newkey = PyLong_FromLong(PyUnicode_READ(kind, data, 0)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 13005 | if (!newkey) |
| 13006 | goto err; |
| 13007 | res = PyDict_SetItem(new, newkey, value); |
| 13008 | Py_DECREF(newkey); |
| 13009 | if (res < 0) |
| 13010 | goto err; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 13011 | } else if (PyLong_Check(key)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 13012 | /* just keep integer keys */ |
| 13013 | if (PyDict_SetItem(new, key, value) < 0) |
| 13014 | goto err; |
| 13015 | } else { |
| 13016 | PyErr_SetString(PyExc_TypeError, "keys in translate table must " |
| 13017 | "be strings or integers"); |
| 13018 | goto err; |
| 13019 | } |
| 13020 | } |
| 13021 | } |
| 13022 | return new; |
| 13023 | err: |
| 13024 | Py_DECREF(new); |
| 13025 | return NULL; |
| 13026 | } |
| 13027 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 13028 | PyDoc_STRVAR(translate__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13029 | "S.translate(table) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13030 | \n\ |
| 13031 | Return a copy of the string S, where all characters have been mapped\n\ |
| 13032 | through the given translation table, which must be a mapping of\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 13033 | Unicode ordinals to Unicode ordinals, strings, or None.\n\ |
Walter Dörwald | 5c1ee17 | 2002-09-04 20:31:32 +0000 | [diff] [blame] | 13034 | Unmapped characters are left untouched. Characters mapped to None\n\ |
| 13035 | are deleted."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13036 | |
| 13037 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13038 | unicode_translate(PyObject *self, PyObject *table) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13039 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13040 | return _PyUnicode_TranslateCharmap(self, table, "ignore"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13041 | } |
| 13042 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 13043 | PyDoc_STRVAR(upper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13044 | "S.upper() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13045 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 13046 | Return a copy of S converted to uppercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13047 | |
| 13048 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 13049 | unicode_upper(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13050 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 13051 | if (PyUnicode_READY(self) == -1) |
| 13052 | return NULL; |
| 13053 | if (PyUnicode_IS_ASCII(self)) |
| 13054 | return ascii_upper_or_lower(self, 0); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 13055 | return case_operation(self, do_upper); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13056 | } |
| 13057 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 13058 | PyDoc_STRVAR(zfill__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13059 | "S.zfill(width) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13060 | \n\ |
Benjamin Peterson | 9aa4299 | 2008-09-10 21:57:34 +0000 | [diff] [blame] | 13061 | Pad a numeric string S with zeros on the left, to fill a field\n\ |
| 13062 | of the specified width. The string S is never truncated."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13063 | |
| 13064 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 13065 | unicode_zfill(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13066 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13067 | Py_ssize_t fill; |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 13068 | PyObject *u; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13069 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13070 | int kind; |
| 13071 | void *data; |
| 13072 | Py_UCS4 chr; |
| 13073 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13074 | if (!PyArg_ParseTuple(args, "n:zfill", &width)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13075 | return NULL; |
| 13076 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 13077 | if (PyUnicode_READY(self) == -1) |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 13078 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13079 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 13080 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 13081 | return unicode_result_unchanged(self); |
| 13082 | |
| 13083 | fill = width - PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13084 | |
| 13085 | u = pad(self, fill, 0, '0'); |
| 13086 | |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 13087 | if (u == NULL) |
| 13088 | return NULL; |
| 13089 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13090 | kind = PyUnicode_KIND(u); |
| 13091 | data = PyUnicode_DATA(u); |
| 13092 | chr = PyUnicode_READ(kind, data, fill); |
| 13093 | |
| 13094 | if (chr == '+' || chr == '-') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13095 | /* move sign to beginning of string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13096 | PyUnicode_WRITE(kind, data, 0, chr); |
| 13097 | PyUnicode_WRITE(kind, data, fill, '0'); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13098 | } |
| 13099 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13100 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 13101 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13102 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13103 | |
| 13104 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 13105 | static PyObject * |
| 13106 | unicode__decimal2ascii(PyObject *self) |
| 13107 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13108 | return PyUnicode_TransformDecimalAndSpaceToASCII(self); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 13109 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13110 | #endif |
| 13111 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 13112 | PyDoc_STRVAR(startswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13113 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13114 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 13115 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 13116 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13117 | With optional end, stop comparing S at that position.\n\ |
| 13118 | prefix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13119 | |
| 13120 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13121 | unicode_startswith(PyObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13122 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13123 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13124 | PyObject *subobj; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13125 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13126 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 13127 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13128 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13129 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 13130 | if (!stringlib_parse_args_finds("startswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13131 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13132 | if (PyTuple_Check(subobj)) { |
| 13133 | Py_ssize_t i; |
| 13134 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13135 | substring = PyUnicode_FromObject(PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13136 | if (substring == NULL) |
| 13137 | return NULL; |
| 13138 | result = tailmatch(self, substring, start, end, -1); |
| 13139 | Py_DECREF(substring); |
| 13140 | if (result) { |
| 13141 | Py_RETURN_TRUE; |
| 13142 | } |
| 13143 | } |
| 13144 | /* nothing matched */ |
| 13145 | Py_RETURN_FALSE; |
| 13146 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13147 | substring = PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 13148 | if (substring == NULL) { |
| 13149 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 13150 | PyErr_Format(PyExc_TypeError, "startswith first arg must be str or " |
| 13151 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13152 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 13153 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13154 | result = tailmatch(self, substring, start, end, -1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13155 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13156 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13157 | } |
| 13158 | |
| 13159 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 13160 | PyDoc_STRVAR(endswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13161 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13162 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 13163 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 13164 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13165 | With optional end, stop comparing S at that position.\n\ |
| 13166 | suffix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13167 | |
| 13168 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13169 | unicode_endswith(PyObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13170 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13171 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13172 | PyObject *subobj; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13173 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13174 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 13175 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13176 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13177 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 13178 | if (!stringlib_parse_args_finds("endswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13179 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13180 | if (PyTuple_Check(subobj)) { |
| 13181 | Py_ssize_t i; |
| 13182 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13183 | substring = PyUnicode_FromObject( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13184 | PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13185 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13186 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13187 | result = tailmatch(self, substring, start, end, +1); |
| 13188 | Py_DECREF(substring); |
| 13189 | if (result) { |
| 13190 | Py_RETURN_TRUE; |
| 13191 | } |
| 13192 | } |
| 13193 | Py_RETURN_FALSE; |
| 13194 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13195 | substring = PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 13196 | if (substring == NULL) { |
| 13197 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 13198 | PyErr_Format(PyExc_TypeError, "endswith first arg must be str or " |
| 13199 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13200 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 13201 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13202 | result = tailmatch(self, substring, start, end, +1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13203 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13204 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13205 | } |
| 13206 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13207 | #include "stringlib/unicode_format.h" |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13208 | |
| 13209 | PyDoc_STRVAR(format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13210 | "S.format(*args, **kwargs) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13211 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 13212 | Return a formatted version of S, using substitutions from args and kwargs.\n\ |
| 13213 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13214 | |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 13215 | PyDoc_STRVAR(format_map__doc__, |
| 13216 | "S.format_map(mapping) -> str\n\ |
| 13217 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 13218 | Return a formatted version of S, using substitutions from mapping.\n\ |
| 13219 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 13220 | |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 13221 | static PyObject * |
| 13222 | unicode__format__(PyObject* self, PyObject* args) |
| 13223 | { |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13224 | PyObject *format_spec, *out; |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 13225 | |
| 13226 | if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) |
| 13227 | return NULL; |
| 13228 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13229 | out = _PyUnicode_FormatAdvanced(self, format_spec, 0, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13230 | PyUnicode_GET_LENGTH(format_spec)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13231 | return out; |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 13232 | } |
| 13233 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13234 | PyDoc_STRVAR(p_format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13235 | "S.__format__(format_spec) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13236 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 13237 | Return a formatted version of S as described by format_spec."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13238 | |
| 13239 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13240 | unicode__sizeof__(PyObject *v) |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13241 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13242 | Py_ssize_t size; |
| 13243 | |
| 13244 | /* If it's a compact object, account for base structure + |
| 13245 | character data. */ |
| 13246 | if (PyUnicode_IS_COMPACT_ASCII(v)) |
| 13247 | size = sizeof(PyASCIIObject) + PyUnicode_GET_LENGTH(v) + 1; |
| 13248 | else if (PyUnicode_IS_COMPACT(v)) |
| 13249 | size = sizeof(PyCompactUnicodeObject) + |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 13250 | (PyUnicode_GET_LENGTH(v) + 1) * PyUnicode_KIND(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13251 | else { |
| 13252 | /* If it is a two-block object, account for base object, and |
| 13253 | for character block if present. */ |
| 13254 | size = sizeof(PyUnicodeObject); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 13255 | if (_PyUnicode_DATA_ANY(v)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13256 | size += (PyUnicode_GET_LENGTH(v) + 1) * |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 13257 | PyUnicode_KIND(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13258 | } |
| 13259 | /* 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] | 13260 | with the data pointer. Check if the data is not shared. */ |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 13261 | if (_PyUnicode_HAS_WSTR_MEMORY(v)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13262 | size += (PyUnicode_WSTR_LENGTH(v) + 1) * sizeof(wchar_t); |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 13263 | if (_PyUnicode_HAS_UTF8_MEMORY(v)) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 13264 | size += PyUnicode_UTF8_LENGTH(v) + 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13265 | |
| 13266 | return PyLong_FromSsize_t(size); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13267 | } |
| 13268 | |
| 13269 | PyDoc_STRVAR(sizeof__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13270 | "S.__sizeof__() -> size of S in memory, in bytes"); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13271 | |
| 13272 | static PyObject * |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 13273 | unicode_getnewargs(PyObject *v) |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 13274 | { |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 13275 | PyObject *copy = _PyUnicode_Copy(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13276 | if (!copy) |
| 13277 | return NULL; |
| 13278 | return Py_BuildValue("(N)", copy); |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 13279 | } |
| 13280 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13281 | static PyMethodDef unicode_methods[] = { |
Benjamin Peterson | 28a4dce | 2010-12-12 01:33:04 +0000 | [diff] [blame] | 13282 | {"encode", (PyCFunction) unicode_encode, METH_VARARGS | METH_KEYWORDS, encode__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13283 | {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 13284 | {"split", (PyCFunction) unicode_split, METH_VARARGS | METH_KEYWORDS, split__doc__}, |
| 13285 | {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS | METH_KEYWORDS, rsplit__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13286 | {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, |
| 13287 | {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 13288 | {"casefold", (PyCFunction) unicode_casefold, METH_NOARGS, casefold__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13289 | {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, |
| 13290 | {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, |
| 13291 | {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, |
| 13292 | {"expandtabs", (PyCFunction) unicode_expandtabs, METH_VARARGS, expandtabs__doc__}, |
| 13293 | {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13294 | {"partition", (PyCFunction) unicode_partition, METH_O, partition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13295 | {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, |
| 13296 | {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, |
| 13297 | {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 13298 | {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13299 | {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, |
| 13300 | {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, |
| 13301 | {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 13302 | {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13303 | {"rpartition", (PyCFunction) unicode_rpartition, METH_O, rpartition__doc__}, |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 13304 | {"splitlines", (PyCFunction) unicode_splitlines, METH_VARARGS | METH_KEYWORDS, splitlines__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 13305 | {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13306 | {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, |
| 13307 | {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, |
| 13308 | {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, |
| 13309 | {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, |
| 13310 | {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, |
| 13311 | {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, |
| 13312 | {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, |
| 13313 | {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, |
| 13314 | {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, |
| 13315 | {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, |
| 13316 | {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, |
| 13317 | {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, |
| 13318 | {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, |
| 13319 | {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 13320 | {"isidentifier", (PyCFunction) unicode_isidentifier, METH_NOARGS, isidentifier__doc__}, |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 13321 | {"isprintable", (PyCFunction) unicode_isprintable, METH_NOARGS, isprintable__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13322 | {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, |
Eric Smith | 9cd1e09 | 2007-08-31 18:39:38 +0000 | [diff] [blame] | 13323 | {"format", (PyCFunction) do_string_format, METH_VARARGS | METH_KEYWORDS, format__doc__}, |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 13324 | {"format_map", (PyCFunction) do_string_format_map, METH_O, format_map__doc__}, |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 13325 | {"__format__", (PyCFunction) unicode__format__, METH_VARARGS, p_format__doc__}, |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 13326 | {"maketrans", (PyCFunction) unicode_maketrans, |
| 13327 | METH_VARARGS | METH_STATIC, maketrans__doc__}, |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13328 | {"__sizeof__", (PyCFunction) unicode__sizeof__, METH_NOARGS, sizeof__doc__}, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 13329 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 13330 | /* These methods are just used for debugging the implementation. */ |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 13331 | {"_decimal2ascii", (PyCFunction) unicode__decimal2ascii, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13332 | #endif |
| 13333 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13334 | {"__getnewargs__", (PyCFunction)unicode_getnewargs, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13335 | {NULL, NULL} |
| 13336 | }; |
| 13337 | |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 13338 | static PyObject * |
| 13339 | unicode_mod(PyObject *v, PyObject *w) |
| 13340 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 13341 | if (!PyUnicode_Check(v)) |
| 13342 | Py_RETURN_NOTIMPLEMENTED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13343 | return PyUnicode_Format(v, w); |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 13344 | } |
| 13345 | |
| 13346 | static PyNumberMethods unicode_as_number = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13347 | 0, /*nb_add*/ |
| 13348 | 0, /*nb_subtract*/ |
| 13349 | 0, /*nb_multiply*/ |
| 13350 | unicode_mod, /*nb_remainder*/ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 13351 | }; |
| 13352 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13353 | static PySequenceMethods unicode_as_sequence = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13354 | (lenfunc) unicode_length, /* sq_length */ |
| 13355 | PyUnicode_Concat, /* sq_concat */ |
| 13356 | (ssizeargfunc) unicode_repeat, /* sq_repeat */ |
| 13357 | (ssizeargfunc) unicode_getitem, /* sq_item */ |
| 13358 | 0, /* sq_slice */ |
| 13359 | 0, /* sq_ass_item */ |
| 13360 | 0, /* sq_ass_slice */ |
| 13361 | PyUnicode_Contains, /* sq_contains */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13362 | }; |
| 13363 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13364 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13365 | unicode_subscript(PyObject* self, PyObject* item) |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13366 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13367 | if (PyUnicode_READY(self) == -1) |
| 13368 | return NULL; |
| 13369 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 13370 | if (PyIndex_Check(item)) { |
| 13371 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13372 | if (i == -1 && PyErr_Occurred()) |
| 13373 | return NULL; |
| 13374 | if (i < 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13375 | i += PyUnicode_GET_LENGTH(self); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13376 | return unicode_getitem(self, i); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13377 | } else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13378 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13379 | PyObject *result; |
| 13380 | void *src_data, *dest_data; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13381 | int src_kind, dest_kind; |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 13382 | Py_UCS4 ch, max_char, kind_limit; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13383 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13384 | if (PySlice_GetIndicesEx(item, PyUnicode_GET_LENGTH(self), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13385 | &start, &stop, &step, &slicelength) < 0) { |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13386 | return NULL; |
| 13387 | } |
| 13388 | |
| 13389 | if (slicelength <= 0) { |
Victor Stinner | 382955f | 2011-12-11 21:44:00 +0100 | [diff] [blame] | 13390 | Py_INCREF(unicode_empty); |
| 13391 | return unicode_empty; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13392 | } else if (start == 0 && step == 1 && |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 13393 | slicelength == PyUnicode_GET_LENGTH(self)) { |
| 13394 | return unicode_result_unchanged(self); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 13395 | } else if (step == 1) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13396 | return PyUnicode_Substring(self, |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 13397 | start, start + slicelength); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13398 | } |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13399 | /* General case */ |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13400 | src_kind = PyUnicode_KIND(self); |
| 13401 | src_data = PyUnicode_DATA(self); |
Victor Stinner | 55c9911 | 2011-10-13 01:17:06 +0200 | [diff] [blame] | 13402 | if (!PyUnicode_IS_ASCII(self)) { |
| 13403 | kind_limit = kind_maxchar_limit(src_kind); |
| 13404 | max_char = 0; |
| 13405 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
| 13406 | ch = PyUnicode_READ(src_kind, src_data, cur); |
| 13407 | if (ch > max_char) { |
| 13408 | max_char = ch; |
| 13409 | if (max_char >= kind_limit) |
| 13410 | break; |
| 13411 | } |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 13412 | } |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13413 | } |
Victor Stinner | 55c9911 | 2011-10-13 01:17:06 +0200 | [diff] [blame] | 13414 | else |
| 13415 | max_char = 127; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13416 | result = PyUnicode_New(slicelength, max_char); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13417 | if (result == NULL) |
| 13418 | return NULL; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13419 | dest_kind = PyUnicode_KIND(result); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13420 | dest_data = PyUnicode_DATA(result); |
| 13421 | |
| 13422 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13423 | Py_UCS4 ch = PyUnicode_READ(src_kind, src_data, cur); |
| 13424 | PyUnicode_WRITE(dest_kind, dest_data, i, ch); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13425 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13426 | assert(_PyUnicode_CheckConsistency(result, 1)); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13427 | return result; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13428 | } else { |
| 13429 | PyErr_SetString(PyExc_TypeError, "string indices must be integers"); |
| 13430 | return NULL; |
| 13431 | } |
| 13432 | } |
| 13433 | |
| 13434 | static PyMappingMethods unicode_as_mapping = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13435 | (lenfunc)unicode_length, /* mp_length */ |
| 13436 | (binaryfunc)unicode_subscript, /* mp_subscript */ |
| 13437 | (objobjargproc)0, /* mp_ass_subscript */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13438 | }; |
| 13439 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13440 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13441 | /* Helpers for PyUnicode_Format() */ |
| 13442 | |
| 13443 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13444 | 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] | 13445 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13446 | Py_ssize_t argidx = *p_argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13447 | if (argidx < arglen) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13448 | (*p_argidx)++; |
| 13449 | if (arglen < 0) |
| 13450 | return args; |
| 13451 | else |
| 13452 | return PyTuple_GetItem(args, argidx); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13453 | } |
| 13454 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13455 | "not enough arguments for format string"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13456 | return NULL; |
| 13457 | } |
| 13458 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13459 | /* 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] | 13460 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13461 | static PyObject * |
| 13462 | formatfloat(PyObject *v, int flags, int prec, int type) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13463 | { |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13464 | char *p; |
| 13465 | PyObject *result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13466 | double x; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 13467 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13468 | x = PyFloat_AsDouble(v); |
| 13469 | if (x == -1.0 && PyErr_Occurred()) |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13470 | return NULL; |
| 13471 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13472 | if (prec < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13473 | prec = 6; |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 13474 | |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 13475 | p = PyOS_double_to_string(x, type, prec, |
| 13476 | (flags & F_ALT) ? Py_DTSF_ALT : 0, NULL); |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13477 | if (p == NULL) |
| 13478 | return NULL; |
Victor Stinner | b5c3ea3 | 2012-05-02 00:29:36 +0200 | [diff] [blame] | 13479 | result = unicode_fromascii((unsigned char*)p, strlen(p)); |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 13480 | PyMem_Free(p); |
| 13481 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13482 | } |
| 13483 | |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13484 | /* formatlong() emulates the format codes d, u, o, x and X, and |
| 13485 | * the F_ALT flag, for Python's long (unbounded) ints. It's not used for |
| 13486 | * Python's regular ints. |
| 13487 | * Return value: a new PyUnicodeObject*, or NULL if error. |
| 13488 | * The output string is of the form |
| 13489 | * "-"? ("0x" | "0X")? digit+ |
| 13490 | * "0x"/"0X" are present only for x and X conversions, with F_ALT |
| 13491 | * set in flags. The case of hex digits will be correct, |
| 13492 | * There will be at least prec digits, zero-filled on the left if |
| 13493 | * necessary to get that many. |
| 13494 | * val object to be converted |
| 13495 | * flags bitmask of format flags; only F_ALT is looked at |
| 13496 | * prec minimum number of digits; 0-fill on left if needed |
| 13497 | * type a character in [duoxX]; u acts the same as d |
| 13498 | * |
| 13499 | * CAUTION: o, x and X conversions on regular ints can never |
| 13500 | * produce a '-' sign, but can for Python's unbounded ints. |
| 13501 | */ |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13502 | static PyObject* |
| 13503 | formatlong(PyObject *val, int flags, int prec, int type) |
| 13504 | { |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13505 | PyObject *result = NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13506 | char *buf; |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13507 | Py_ssize_t i; |
| 13508 | int sign; /* 1 if '-', else 0 */ |
| 13509 | int len; /* number of characters */ |
| 13510 | Py_ssize_t llen; |
| 13511 | int numdigits; /* len == numnondigits + numdigits */ |
| 13512 | int numnondigits = 0; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13513 | |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13514 | /* Avoid exceeding SSIZE_T_MAX */ |
| 13515 | if (prec > INT_MAX-3) { |
| 13516 | PyErr_SetString(PyExc_OverflowError, |
| 13517 | "precision too large"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13518 | return NULL; |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13519 | } |
| 13520 | |
| 13521 | assert(PyLong_Check(val)); |
| 13522 | |
| 13523 | switch (type) { |
| 13524 | case 'd': |
| 13525 | case 'u': |
| 13526 | /* Special-case boolean: we want 0/1 */ |
Victor Stinner | b11d91d | 2012-04-28 00:25:34 +0200 | [diff] [blame] | 13527 | if (PyBool_Check(val)) |
| 13528 | result = PyNumber_ToBase(val, 10); |
| 13529 | else |
| 13530 | result = Py_TYPE(val)->tp_str(val); |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13531 | break; |
| 13532 | case 'o': |
| 13533 | numnondigits = 2; |
| 13534 | result = PyNumber_ToBase(val, 8); |
| 13535 | break; |
| 13536 | case 'x': |
| 13537 | case 'X': |
| 13538 | numnondigits = 2; |
| 13539 | result = PyNumber_ToBase(val, 16); |
| 13540 | break; |
| 13541 | default: |
| 13542 | assert(!"'type' not in [duoxX]"); |
| 13543 | } |
| 13544 | if (!result) |
| 13545 | return NULL; |
| 13546 | |
| 13547 | assert(unicode_modifiable(result)); |
| 13548 | assert(PyUnicode_IS_READY(result)); |
| 13549 | assert(PyUnicode_IS_ASCII(result)); |
| 13550 | |
| 13551 | /* To modify the string in-place, there can only be one reference. */ |
| 13552 | if (Py_REFCNT(result) != 1) { |
| 13553 | PyErr_BadInternalCall(); |
| 13554 | return NULL; |
| 13555 | } |
| 13556 | buf = PyUnicode_DATA(result); |
| 13557 | llen = PyUnicode_GET_LENGTH(result); |
| 13558 | if (llen > INT_MAX) { |
| 13559 | PyErr_SetString(PyExc_ValueError, |
| 13560 | "string too large in _PyBytes_FormatLong"); |
| 13561 | return NULL; |
| 13562 | } |
| 13563 | len = (int)llen; |
| 13564 | sign = buf[0] == '-'; |
| 13565 | numnondigits += sign; |
| 13566 | numdigits = len - numnondigits; |
| 13567 | assert(numdigits > 0); |
| 13568 | |
| 13569 | /* Get rid of base marker unless F_ALT */ |
| 13570 | if (((flags & F_ALT) == 0 && |
| 13571 | (type == 'o' || type == 'x' || type == 'X'))) { |
| 13572 | assert(buf[sign] == '0'); |
| 13573 | assert(buf[sign+1] == 'x' || buf[sign+1] == 'X' || |
| 13574 | buf[sign+1] == 'o'); |
| 13575 | numnondigits -= 2; |
| 13576 | buf += 2; |
| 13577 | len -= 2; |
| 13578 | if (sign) |
| 13579 | buf[0] = '-'; |
| 13580 | assert(len == numnondigits + numdigits); |
| 13581 | assert(numdigits > 0); |
| 13582 | } |
| 13583 | |
| 13584 | /* Fill with leading zeroes to meet minimum width. */ |
| 13585 | if (prec > numdigits) { |
| 13586 | PyObject *r1 = PyBytes_FromStringAndSize(NULL, |
| 13587 | numnondigits + prec); |
| 13588 | char *b1; |
| 13589 | if (!r1) { |
| 13590 | Py_DECREF(result); |
| 13591 | return NULL; |
| 13592 | } |
| 13593 | b1 = PyBytes_AS_STRING(r1); |
| 13594 | for (i = 0; i < numnondigits; ++i) |
| 13595 | *b1++ = *buf++; |
| 13596 | for (i = 0; i < prec - numdigits; i++) |
| 13597 | *b1++ = '0'; |
| 13598 | for (i = 0; i < numdigits; i++) |
| 13599 | *b1++ = *buf++; |
| 13600 | *b1 = '\0'; |
| 13601 | Py_DECREF(result); |
| 13602 | result = r1; |
| 13603 | buf = PyBytes_AS_STRING(result); |
| 13604 | len = numnondigits + prec; |
| 13605 | } |
| 13606 | |
| 13607 | /* Fix up case for hex conversions. */ |
| 13608 | if (type == 'X') { |
| 13609 | /* Need to convert all lower case letters to upper case. |
| 13610 | and need to convert 0x to 0X (and -0x to -0X). */ |
| 13611 | for (i = 0; i < len; i++) |
| 13612 | if (buf[i] >= 'a' && buf[i] <= 'x') |
| 13613 | buf[i] -= 'a'-'A'; |
| 13614 | } |
| 13615 | if (!PyUnicode_Check(result) || len != PyUnicode_GET_LENGTH(result)) { |
| 13616 | PyObject *unicode; |
| 13617 | unicode = unicode_fromascii((unsigned char *)buf, len); |
| 13618 | Py_DECREF(result); |
| 13619 | result = unicode; |
| 13620 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13621 | return result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13622 | } |
| 13623 | |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13624 | static Py_UCS4 |
| 13625 | formatchar(PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13626 | { |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 13627 | /* presume that the buffer is at least 3 characters long */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 13628 | if (PyUnicode_Check(v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13629 | if (PyUnicode_GET_LENGTH(v) == 1) { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13630 | return PyUnicode_READ_CHAR(v, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13631 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13632 | goto onError; |
| 13633 | } |
| 13634 | else { |
| 13635 | /* Integer input truncated to a character */ |
| 13636 | long x; |
| 13637 | x = PyLong_AsLong(v); |
| 13638 | if (x == -1 && PyErr_Occurred()) |
| 13639 | goto onError; |
| 13640 | |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 13641 | if (x < 0 || x > MAX_UNICODE) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13642 | PyErr_SetString(PyExc_OverflowError, |
| 13643 | "%c arg not in range(0x110000)"); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13644 | return (Py_UCS4) -1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13645 | } |
| 13646 | |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13647 | return (Py_UCS4) x; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13648 | } |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 13649 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13650 | onError: |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 13651 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13652 | "%c requires int or char"); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13653 | return (Py_UCS4) -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13654 | } |
| 13655 | |
Antoine Pitrou | 978b9d2 | 2011-10-07 12:35:48 +0200 | [diff] [blame] | 13656 | static int |
| 13657 | repeat_accumulate(_PyAccu *acc, PyObject *obj, Py_ssize_t count) |
| 13658 | { |
| 13659 | int r; |
| 13660 | assert(count > 0); |
| 13661 | assert(PyUnicode_Check(obj)); |
| 13662 | if (count > 5) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13663 | PyObject *repeated = unicode_repeat(obj, count); |
Antoine Pitrou | 978b9d2 | 2011-10-07 12:35:48 +0200 | [diff] [blame] | 13664 | if (repeated == NULL) |
| 13665 | return -1; |
| 13666 | r = _PyAccu_Accumulate(acc, repeated); |
| 13667 | Py_DECREF(repeated); |
| 13668 | return r; |
| 13669 | } |
| 13670 | else { |
| 13671 | do { |
| 13672 | if (_PyAccu_Accumulate(acc, obj)) |
| 13673 | return -1; |
| 13674 | } while (--count); |
| 13675 | return 0; |
| 13676 | } |
| 13677 | } |
| 13678 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 13679 | PyObject * |
| 13680 | PyUnicode_Format(PyObject *format, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13681 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13682 | void *fmt; |
| 13683 | int fmtkind; |
| 13684 | PyObject *result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13685 | int kind; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13686 | int r; |
| 13687 | Py_ssize_t fmtcnt, fmtpos, arglen, argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13688 | int args_owned = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13689 | PyObject *dict = NULL; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13690 | PyObject *temp = NULL; |
| 13691 | PyObject *second = NULL; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13692 | PyObject *uformat; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13693 | _PyAccu acc; |
| 13694 | static PyObject *plus, *minus, *blank, *zero, *percent; |
| 13695 | |
| 13696 | if (!plus && !(plus = get_latin1_char('+'))) |
| 13697 | return NULL; |
| 13698 | if (!minus && !(minus = get_latin1_char('-'))) |
| 13699 | return NULL; |
| 13700 | if (!blank && !(blank = get_latin1_char(' '))) |
| 13701 | return NULL; |
| 13702 | if (!zero && !(zero = get_latin1_char('0'))) |
| 13703 | return NULL; |
| 13704 | if (!percent && !(percent = get_latin1_char('%'))) |
| 13705 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 13706 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13707 | if (format == NULL || args == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13708 | PyErr_BadInternalCall(); |
| 13709 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13710 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13711 | uformat = PyUnicode_FromObject(format); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 13712 | if (uformat == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13713 | return NULL; |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 13714 | if (PyUnicode_READY(uformat) == -1) |
| 13715 | Py_DECREF(uformat); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13716 | if (_PyAccu_Init(&acc)) |
| 13717 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13718 | fmt = PyUnicode_DATA(uformat); |
| 13719 | fmtkind = PyUnicode_KIND(uformat); |
| 13720 | fmtcnt = PyUnicode_GET_LENGTH(uformat); |
| 13721 | fmtpos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13722 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13723 | if (PyTuple_Check(args)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13724 | arglen = PyTuple_Size(args); |
| 13725 | argidx = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13726 | } |
| 13727 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13728 | arglen = -1; |
| 13729 | argidx = -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13730 | } |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 13731 | if (Py_TYPE(args)->tp_as_mapping && !PyTuple_Check(args) && |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 13732 | !PyUnicode_Check(args)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13733 | dict = args; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13734 | |
| 13735 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13736 | if (PyUnicode_READ(fmtkind, fmt, fmtpos) != '%') { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13737 | PyObject *nonfmt; |
| 13738 | Py_ssize_t nonfmtpos; |
| 13739 | nonfmtpos = fmtpos++; |
| 13740 | while (fmtcnt >= 0 && |
| 13741 | PyUnicode_READ(fmtkind, fmt, fmtpos) != '%') { |
| 13742 | fmtpos++; |
| 13743 | fmtcnt--; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13744 | } |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 13745 | nonfmt = PyUnicode_Substring(uformat, nonfmtpos, fmtpos); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13746 | if (nonfmt == NULL) |
| 13747 | goto onError; |
| 13748 | r = _PyAccu_Accumulate(&acc, nonfmt); |
| 13749 | Py_DECREF(nonfmt); |
| 13750 | if (r) |
| 13751 | goto onError; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13752 | } |
| 13753 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13754 | /* Got a format specifier */ |
| 13755 | int flags = 0; |
| 13756 | Py_ssize_t width = -1; |
| 13757 | int prec = -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13758 | Py_UCS4 c = '\0'; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13759 | Py_UCS4 fill, sign; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13760 | int isnumok; |
| 13761 | PyObject *v = NULL; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13762 | void *pbuf = NULL; |
| 13763 | Py_ssize_t pindex, len; |
| 13764 | PyObject *signobj = NULL, *fillobj = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13765 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13766 | fmtpos++; |
Victor Stinner | 438106b | 2012-05-02 00:41:57 +0200 | [diff] [blame] | 13767 | c = PyUnicode_READ(fmtkind, fmt, fmtpos); |
| 13768 | if (c == '(') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13769 | Py_ssize_t keystart; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13770 | Py_ssize_t keylen; |
| 13771 | PyObject *key; |
| 13772 | int pcount = 1; |
Christian Heimes | a612dc0 | 2008-02-24 13:08:18 +0000 | [diff] [blame] | 13773 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13774 | if (dict == NULL) { |
| 13775 | PyErr_SetString(PyExc_TypeError, |
| 13776 | "format requires a mapping"); |
| 13777 | goto onError; |
| 13778 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13779 | ++fmtpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13780 | --fmtcnt; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13781 | keystart = fmtpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13782 | /* Skip over balanced parentheses */ |
| 13783 | while (pcount > 0 && --fmtcnt >= 0) { |
Victor Stinner | bff7c96 | 2012-05-03 01:44:59 +0200 | [diff] [blame] | 13784 | c = PyUnicode_READ(fmtkind, fmt, fmtpos); |
| 13785 | if (c == ')') |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13786 | --pcount; |
Victor Stinner | bff7c96 | 2012-05-03 01:44:59 +0200 | [diff] [blame] | 13787 | else if (c == '(') |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13788 | ++pcount; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13789 | fmtpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13790 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13791 | keylen = fmtpos - keystart - 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13792 | if (fmtcnt < 0 || pcount > 0) { |
| 13793 | PyErr_SetString(PyExc_ValueError, |
| 13794 | "incomplete format key"); |
| 13795 | goto onError; |
| 13796 | } |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 13797 | key = PyUnicode_Substring(uformat, |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 13798 | keystart, keystart + keylen); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13799 | if (key == NULL) |
| 13800 | goto onError; |
| 13801 | if (args_owned) { |
| 13802 | Py_DECREF(args); |
| 13803 | args_owned = 0; |
| 13804 | } |
| 13805 | args = PyObject_GetItem(dict, key); |
| 13806 | Py_DECREF(key); |
| 13807 | if (args == NULL) { |
| 13808 | goto onError; |
| 13809 | } |
| 13810 | args_owned = 1; |
| 13811 | arglen = -1; |
| 13812 | argidx = -2; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13813 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13814 | while (--fmtcnt >= 0) { |
Victor Stinner | 438106b | 2012-05-02 00:41:57 +0200 | [diff] [blame] | 13815 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
| 13816 | switch (c) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13817 | case '-': flags |= F_LJUST; continue; |
| 13818 | case '+': flags |= F_SIGN; continue; |
| 13819 | case ' ': flags |= F_BLANK; continue; |
| 13820 | case '#': flags |= F_ALT; continue; |
| 13821 | case '0': flags |= F_ZERO; continue; |
| 13822 | } |
| 13823 | break; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13824 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13825 | if (c == '*') { |
| 13826 | v = getnextarg(args, arglen, &argidx); |
| 13827 | if (v == NULL) |
| 13828 | goto onError; |
| 13829 | if (!PyLong_Check(v)) { |
| 13830 | PyErr_SetString(PyExc_TypeError, |
| 13831 | "* wants int"); |
| 13832 | goto onError; |
| 13833 | } |
| 13834 | width = PyLong_AsLong(v); |
| 13835 | if (width == -1 && PyErr_Occurred()) |
| 13836 | goto onError; |
| 13837 | if (width < 0) { |
| 13838 | flags |= F_LJUST; |
| 13839 | width = -width; |
| 13840 | } |
| 13841 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13842 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13843 | } |
| 13844 | else if (c >= '0' && c <= '9') { |
| 13845 | width = c - '0'; |
| 13846 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13847 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13848 | if (c < '0' || c > '9') |
| 13849 | break; |
| 13850 | if ((width*10) / 10 != width) { |
| 13851 | PyErr_SetString(PyExc_ValueError, |
| 13852 | "width too big"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13853 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13854 | } |
| 13855 | width = width*10 + (c - '0'); |
| 13856 | } |
| 13857 | } |
| 13858 | if (c == '.') { |
| 13859 | prec = 0; |
| 13860 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13861 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13862 | if (c == '*') { |
| 13863 | v = getnextarg(args, arglen, &argidx); |
| 13864 | if (v == NULL) |
| 13865 | goto onError; |
| 13866 | if (!PyLong_Check(v)) { |
| 13867 | PyErr_SetString(PyExc_TypeError, |
| 13868 | "* wants int"); |
| 13869 | goto onError; |
| 13870 | } |
| 13871 | prec = PyLong_AsLong(v); |
| 13872 | if (prec == -1 && PyErr_Occurred()) |
| 13873 | goto onError; |
| 13874 | if (prec < 0) |
| 13875 | prec = 0; |
| 13876 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13877 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13878 | } |
| 13879 | else if (c >= '0' && c <= '9') { |
| 13880 | prec = c - '0'; |
| 13881 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13882 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13883 | if (c < '0' || c > '9') |
| 13884 | break; |
| 13885 | if ((prec*10) / 10 != prec) { |
| 13886 | PyErr_SetString(PyExc_ValueError, |
| 13887 | "prec too big"); |
| 13888 | goto onError; |
| 13889 | } |
| 13890 | prec = prec*10 + (c - '0'); |
| 13891 | } |
| 13892 | } |
| 13893 | } /* prec */ |
| 13894 | if (fmtcnt >= 0) { |
| 13895 | if (c == 'h' || c == 'l' || c == 'L') { |
| 13896 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13897 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13898 | } |
| 13899 | } |
| 13900 | if (fmtcnt < 0) { |
| 13901 | PyErr_SetString(PyExc_ValueError, |
| 13902 | "incomplete format"); |
| 13903 | goto onError; |
| 13904 | } |
Victor Stinner | aff3cc6 | 2012-04-30 05:19:21 +0200 | [diff] [blame] | 13905 | |
| 13906 | if (c == '%') { |
| 13907 | _PyAccu_Accumulate(&acc, percent); |
| 13908 | continue; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13909 | } |
Victor Stinner | aff3cc6 | 2012-04-30 05:19:21 +0200 | [diff] [blame] | 13910 | |
| 13911 | |
| 13912 | v = getnextarg(args, arglen, &argidx); |
| 13913 | if (v == NULL) |
| 13914 | goto onError; |
| 13915 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13916 | sign = 0; |
| 13917 | fill = ' '; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13918 | fillobj = blank; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13919 | switch (c) { |
| 13920 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13921 | case 's': |
| 13922 | case 'r': |
| 13923 | case 'a': |
Victor Stinner | 808fc0a | 2010-03-22 12:50:40 +0000 | [diff] [blame] | 13924 | if (PyUnicode_CheckExact(v) && c == 's') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13925 | temp = v; |
| 13926 | Py_INCREF(temp); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13927 | } |
| 13928 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13929 | if (c == 's') |
| 13930 | temp = PyObject_Str(v); |
| 13931 | else if (c == 'r') |
| 13932 | temp = PyObject_Repr(v); |
| 13933 | else |
| 13934 | temp = PyObject_ASCII(v); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13935 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13936 | break; |
| 13937 | |
| 13938 | case 'i': |
| 13939 | case 'd': |
| 13940 | case 'u': |
| 13941 | case 'o': |
| 13942 | case 'x': |
| 13943 | case 'X': |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13944 | isnumok = 0; |
| 13945 | if (PyNumber_Check(v)) { |
| 13946 | PyObject *iobj=NULL; |
| 13947 | |
| 13948 | if (PyLong_Check(v)) { |
| 13949 | iobj = v; |
| 13950 | Py_INCREF(iobj); |
| 13951 | } |
| 13952 | else { |
| 13953 | iobj = PyNumber_Long(v); |
| 13954 | } |
| 13955 | if (iobj!=NULL) { |
| 13956 | if (PyLong_Check(iobj)) { |
| 13957 | isnumok = 1; |
Victor Stinner | aff3cc6 | 2012-04-30 05:19:21 +0200 | [diff] [blame] | 13958 | sign = 1; |
Senthil Kumaran | 9ebe08d | 2011-07-03 21:03:16 -0700 | [diff] [blame] | 13959 | temp = formatlong(iobj, flags, prec, (c == 'i'? 'd': c)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13960 | Py_DECREF(iobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13961 | } |
| 13962 | else { |
| 13963 | Py_DECREF(iobj); |
| 13964 | } |
| 13965 | } |
| 13966 | } |
| 13967 | if (!isnumok) { |
| 13968 | PyErr_Format(PyExc_TypeError, |
| 13969 | "%%%c format: a number is required, " |
| 13970 | "not %.200s", (char)c, Py_TYPE(v)->tp_name); |
| 13971 | goto onError; |
| 13972 | } |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13973 | if (flags & F_ZERO) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13974 | fill = '0'; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13975 | fillobj = zero; |
| 13976 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13977 | break; |
| 13978 | |
| 13979 | case 'e': |
| 13980 | case 'E': |
| 13981 | case 'f': |
| 13982 | case 'F': |
| 13983 | case 'g': |
| 13984 | case 'G': |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13985 | sign = 1; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13986 | if (flags & F_ZERO) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13987 | fill = '0'; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13988 | fillobj = zero; |
| 13989 | } |
Victor Stinner | aff3cc6 | 2012-04-30 05:19:21 +0200 | [diff] [blame] | 13990 | temp = formatfloat(v, flags, prec, c); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13991 | break; |
| 13992 | |
| 13993 | case 'c': |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13994 | { |
| 13995 | Py_UCS4 ch = formatchar(v); |
| 13996 | if (ch == (Py_UCS4) -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13997 | goto onError; |
Victor Stinner | b5c3ea3 | 2012-05-02 00:29:36 +0200 | [diff] [blame] | 13998 | temp = PyUnicode_FromOrdinal(ch); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13999 | break; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14000 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14001 | |
| 14002 | default: |
| 14003 | PyErr_Format(PyExc_ValueError, |
| 14004 | "unsupported format character '%c' (0x%x) " |
| 14005 | "at index %zd", |
| 14006 | (31<=c && c<=126) ? (char)c : '?', |
| 14007 | (int)c, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14008 | fmtpos - 1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14009 | goto onError; |
| 14010 | } |
Victor Stinner | aff3cc6 | 2012-04-30 05:19:21 +0200 | [diff] [blame] | 14011 | if (temp == NULL) |
| 14012 | goto onError; |
| 14013 | assert (PyUnicode_Check(temp)); |
| 14014 | if (PyUnicode_READY(temp) == -1) { |
| 14015 | Py_CLEAR(temp); |
| 14016 | goto onError; |
| 14017 | } |
| 14018 | kind = PyUnicode_KIND(temp); |
| 14019 | pbuf = PyUnicode_DATA(temp); |
| 14020 | len = PyUnicode_GET_LENGTH(temp); |
| 14021 | |
| 14022 | if (c == 's' || c == 'r' || c == 'a') { |
| 14023 | if (prec >= 0 && len > prec) |
| 14024 | len = prec; |
| 14025 | } |
| 14026 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14027 | /* pbuf is initialized here. */ |
| 14028 | pindex = 0; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14029 | if (sign) { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14030 | if (PyUnicode_READ(kind, pbuf, pindex) == '-') { |
| 14031 | signobj = minus; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14032 | len--; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14033 | pindex++; |
| 14034 | } |
| 14035 | else if (PyUnicode_READ(kind, pbuf, pindex) == '+') { |
| 14036 | signobj = plus; |
| 14037 | len--; |
| 14038 | pindex++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14039 | } |
| 14040 | else if (flags & F_SIGN) |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14041 | signobj = plus; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14042 | else if (flags & F_BLANK) |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14043 | signobj = blank; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14044 | else |
| 14045 | sign = 0; |
| 14046 | } |
| 14047 | if (width < len) |
| 14048 | width = len; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14049 | if (sign) { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14050 | if (fill != ' ') { |
| 14051 | assert(signobj != NULL); |
| 14052 | if (_PyAccu_Accumulate(&acc, signobj)) |
| 14053 | goto onError; |
| 14054 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14055 | if (width > len) |
| 14056 | width--; |
| 14057 | } |
| 14058 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14059 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14060 | assert(PyUnicode_READ(kind, pbuf, pindex + 1) == c); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14061 | if (fill != ' ') { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14062 | second = get_latin1_char( |
| 14063 | PyUnicode_READ(kind, pbuf, pindex + 1)); |
| 14064 | pindex += 2; |
| 14065 | if (second == NULL || |
| 14066 | _PyAccu_Accumulate(&acc, zero) || |
| 14067 | _PyAccu_Accumulate(&acc, second)) |
| 14068 | goto onError; |
| 14069 | Py_CLEAR(second); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14070 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14071 | width -= 2; |
| 14072 | if (width < 0) |
| 14073 | width = 0; |
| 14074 | len -= 2; |
| 14075 | } |
| 14076 | if (width > len && !(flags & F_LJUST)) { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14077 | assert(fillobj != NULL); |
Antoine Pitrou | 978b9d2 | 2011-10-07 12:35:48 +0200 | [diff] [blame] | 14078 | if (repeat_accumulate(&acc, fillobj, width - len)) |
| 14079 | goto onError; |
| 14080 | width = len; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14081 | } |
| 14082 | if (fill == ' ') { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14083 | if (sign) { |
| 14084 | assert(signobj != NULL); |
| 14085 | if (_PyAccu_Accumulate(&acc, signobj)) |
| 14086 | goto onError; |
| 14087 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14088 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14089 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
| 14090 | assert(PyUnicode_READ(kind, pbuf, pindex+1) == c); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14091 | second = get_latin1_char( |
| 14092 | PyUnicode_READ(kind, pbuf, pindex + 1)); |
| 14093 | pindex += 2; |
| 14094 | if (second == NULL || |
| 14095 | _PyAccu_Accumulate(&acc, zero) || |
| 14096 | _PyAccu_Accumulate(&acc, second)) |
| 14097 | goto onError; |
| 14098 | Py_CLEAR(second); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14099 | } |
| 14100 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14101 | /* Copy all characters, preserving len */ |
Victor Stinner | b80e46e | 2012-04-30 05:21:52 +0200 | [diff] [blame] | 14102 | if (pindex == 0 && len == PyUnicode_GET_LENGTH(temp)) { |
| 14103 | r = _PyAccu_Accumulate(&acc, temp); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14104 | } |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14105 | else { |
Victor Stinner | b80e46e | 2012-04-30 05:21:52 +0200 | [diff] [blame] | 14106 | v = PyUnicode_Substring(temp, pindex, pindex + len); |
| 14107 | if (v == NULL) |
| 14108 | goto onError; |
| 14109 | r = _PyAccu_Accumulate(&acc, v); |
| 14110 | Py_DECREF(v); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14111 | } |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14112 | if (r) |
| 14113 | goto onError; |
Antoine Pitrou | 978b9d2 | 2011-10-07 12:35:48 +0200 | [diff] [blame] | 14114 | if (width > len && repeat_accumulate(&acc, blank, width - len)) |
| 14115 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14116 | if (dict && (argidx < arglen) && c != '%') { |
| 14117 | PyErr_SetString(PyExc_TypeError, |
| 14118 | "not all arguments converted during string formatting"); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14119 | goto onError; |
| 14120 | } |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14121 | Py_CLEAR(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14122 | } /* '%' */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14123 | } /* until end */ |
| 14124 | if (argidx < arglen && !dict) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14125 | PyErr_SetString(PyExc_TypeError, |
| 14126 | "not all arguments converted during string formatting"); |
| 14127 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14128 | } |
| 14129 | |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14130 | result = _PyAccu_Finish(&acc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14131 | if (args_owned) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14132 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14133 | } |
| 14134 | Py_DECREF(uformat); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14135 | Py_XDECREF(temp); |
| 14136 | Py_XDECREF(second); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 14137 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14138 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14139 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14140 | Py_DECREF(uformat); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14141 | Py_XDECREF(temp); |
| 14142 | Py_XDECREF(second); |
| 14143 | _PyAccu_Destroy(&acc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14144 | if (args_owned) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14145 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14146 | } |
| 14147 | return NULL; |
| 14148 | } |
| 14149 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 14150 | static PyObject * |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 14151 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 14152 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14153 | static PyObject * |
| 14154 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 14155 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14156 | PyObject *x = NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14157 | static char *kwlist[] = {"object", "encoding", "errors", 0}; |
| 14158 | char *encoding = NULL; |
| 14159 | char *errors = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14160 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14161 | if (type != &PyUnicode_Type) |
| 14162 | return unicode_subtype_new(type, args, kwds); |
| 14163 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:str", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14164 | kwlist, &x, &encoding, &errors)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14165 | return NULL; |
Victor Stinner | 382955f | 2011-12-11 21:44:00 +0100 | [diff] [blame] | 14166 | if (x == NULL) { |
| 14167 | Py_INCREF(unicode_empty); |
| 14168 | return unicode_empty; |
| 14169 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14170 | if (encoding == NULL && errors == NULL) |
| 14171 | return PyObject_Str(x); |
| 14172 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14173 | return PyUnicode_FromEncodedObject(x, encoding, errors); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14174 | } |
| 14175 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 14176 | static PyObject * |
| 14177 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 14178 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14179 | PyObject *unicode, *self; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14180 | Py_ssize_t length, char_size; |
| 14181 | int share_wstr, share_utf8; |
| 14182 | unsigned int kind; |
| 14183 | void *data; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 14184 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14185 | assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14186 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14187 | unicode = unicode_new(&PyUnicode_Type, args, kwds); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14188 | if (unicode == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14189 | return NULL; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 14190 | assert(_PyUnicode_CHECK(unicode)); |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 14191 | if (PyUnicode_READY(unicode) == -1) { |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 14192 | Py_DECREF(unicode); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14193 | return NULL; |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 14194 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14195 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14196 | self = type->tp_alloc(type, 0); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14197 | if (self == NULL) { |
| 14198 | Py_DECREF(unicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14199 | return NULL; |
| 14200 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14201 | kind = PyUnicode_KIND(unicode); |
| 14202 | length = PyUnicode_GET_LENGTH(unicode); |
| 14203 | |
| 14204 | _PyUnicode_LENGTH(self) = length; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 14205 | #ifdef Py_DEBUG |
| 14206 | _PyUnicode_HASH(self) = -1; |
| 14207 | #else |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14208 | _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 14209 | #endif |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14210 | _PyUnicode_STATE(self).interned = 0; |
| 14211 | _PyUnicode_STATE(self).kind = kind; |
| 14212 | _PyUnicode_STATE(self).compact = 0; |
Victor Stinner | 3cf4637 | 2011-10-03 14:42:15 +0200 | [diff] [blame] | 14213 | _PyUnicode_STATE(self).ascii = _PyUnicode_STATE(unicode).ascii; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14214 | _PyUnicode_STATE(self).ready = 1; |
| 14215 | _PyUnicode_WSTR(self) = NULL; |
| 14216 | _PyUnicode_UTF8_LENGTH(self) = 0; |
| 14217 | _PyUnicode_UTF8(self) = NULL; |
| 14218 | _PyUnicode_WSTR_LENGTH(self) = 0; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 14219 | _PyUnicode_DATA_ANY(self) = NULL; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14220 | |
| 14221 | share_utf8 = 0; |
| 14222 | share_wstr = 0; |
| 14223 | if (kind == PyUnicode_1BYTE_KIND) { |
| 14224 | char_size = 1; |
| 14225 | if (PyUnicode_MAX_CHAR_VALUE(unicode) < 128) |
| 14226 | share_utf8 = 1; |
| 14227 | } |
| 14228 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 14229 | char_size = 2; |
| 14230 | if (sizeof(wchar_t) == 2) |
| 14231 | share_wstr = 1; |
| 14232 | } |
| 14233 | else { |
| 14234 | assert(kind == PyUnicode_4BYTE_KIND); |
| 14235 | char_size = 4; |
| 14236 | if (sizeof(wchar_t) == 4) |
| 14237 | share_wstr = 1; |
| 14238 | } |
| 14239 | |
| 14240 | /* Ensure we won't overflow the length. */ |
| 14241 | if (length > (PY_SSIZE_T_MAX / char_size - 1)) { |
| 14242 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14243 | goto onError; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14244 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14245 | data = PyObject_MALLOC((length + 1) * char_size); |
| 14246 | if (data == NULL) { |
| 14247 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14248 | goto onError; |
| 14249 | } |
| 14250 | |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 14251 | _PyUnicode_DATA_ANY(self) = data; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14252 | if (share_utf8) { |
| 14253 | _PyUnicode_UTF8_LENGTH(self) = length; |
| 14254 | _PyUnicode_UTF8(self) = data; |
| 14255 | } |
| 14256 | if (share_wstr) { |
| 14257 | _PyUnicode_WSTR_LENGTH(self) = length; |
| 14258 | _PyUnicode_WSTR(self) = (wchar_t *)data; |
| 14259 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14260 | |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14261 | Py_MEMCPY(data, PyUnicode_DATA(unicode), |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 14262 | kind * (length + 1)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 14263 | assert(_PyUnicode_CheckConsistency(self, 1)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 14264 | #ifdef Py_DEBUG |
| 14265 | _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode); |
| 14266 | #endif |
Victor Stinner | dd18d3a | 2011-10-22 11:08:10 +0200 | [diff] [blame] | 14267 | Py_DECREF(unicode); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 14268 | return self; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14269 | |
| 14270 | onError: |
| 14271 | Py_DECREF(unicode); |
| 14272 | Py_DECREF(self); |
| 14273 | return NULL; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 14274 | } |
| 14275 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 14276 | PyDoc_STRVAR(unicode_doc, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14277 | "str(string[, encoding[, errors]]) -> str\n\ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14278 | \n\ |
Collin Winter | d474ce8 | 2007-08-07 19:42:11 +0000 | [diff] [blame] | 14279 | Create a new string object from the given encoded string.\n\ |
Skip Montanaro | 35b37a5 | 2002-07-26 16:22:46 +0000 | [diff] [blame] | 14280 | encoding defaults to the current default string encoding.\n\ |
| 14281 | errors can be 'strict', 'replace' or 'ignore' and defaults to 'strict'."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14282 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14283 | static PyObject *unicode_iter(PyObject *seq); |
| 14284 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14285 | PyTypeObject PyUnicode_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 14286 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14287 | "str", /* tp_name */ |
| 14288 | sizeof(PyUnicodeObject), /* tp_size */ |
| 14289 | 0, /* tp_itemsize */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14290 | /* Slots */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14291 | (destructor)unicode_dealloc, /* tp_dealloc */ |
| 14292 | 0, /* tp_print */ |
| 14293 | 0, /* tp_getattr */ |
| 14294 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 14295 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14296 | unicode_repr, /* tp_repr */ |
| 14297 | &unicode_as_number, /* tp_as_number */ |
| 14298 | &unicode_as_sequence, /* tp_as_sequence */ |
| 14299 | &unicode_as_mapping, /* tp_as_mapping */ |
| 14300 | (hashfunc) unicode_hash, /* tp_hash*/ |
| 14301 | 0, /* tp_call*/ |
| 14302 | (reprfunc) unicode_str, /* tp_str */ |
| 14303 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 14304 | 0, /* tp_setattro */ |
| 14305 | 0, /* tp_as_buffer */ |
| 14306 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14307 | Py_TPFLAGS_UNICODE_SUBCLASS, /* tp_flags */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14308 | unicode_doc, /* tp_doc */ |
| 14309 | 0, /* tp_traverse */ |
| 14310 | 0, /* tp_clear */ |
| 14311 | PyUnicode_RichCompare, /* tp_richcompare */ |
| 14312 | 0, /* tp_weaklistoffset */ |
| 14313 | unicode_iter, /* tp_iter */ |
| 14314 | 0, /* tp_iternext */ |
| 14315 | unicode_methods, /* tp_methods */ |
| 14316 | 0, /* tp_members */ |
| 14317 | 0, /* tp_getset */ |
| 14318 | &PyBaseObject_Type, /* tp_base */ |
| 14319 | 0, /* tp_dict */ |
| 14320 | 0, /* tp_descr_get */ |
| 14321 | 0, /* tp_descr_set */ |
| 14322 | 0, /* tp_dictoffset */ |
| 14323 | 0, /* tp_init */ |
| 14324 | 0, /* tp_alloc */ |
| 14325 | unicode_new, /* tp_new */ |
| 14326 | PyObject_Del, /* tp_free */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14327 | }; |
| 14328 | |
| 14329 | /* Initialize the Unicode implementation */ |
| 14330 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 14331 | int _PyUnicode_Init(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14332 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 14333 | int i; |
| 14334 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14335 | /* XXX - move this array to unicodectype.c ? */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14336 | Py_UCS2 linebreak[] = { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14337 | 0x000A, /* LINE FEED */ |
| 14338 | 0x000D, /* CARRIAGE RETURN */ |
| 14339 | 0x001C, /* FILE SEPARATOR */ |
| 14340 | 0x001D, /* GROUP SEPARATOR */ |
| 14341 | 0x001E, /* RECORD SEPARATOR */ |
| 14342 | 0x0085, /* NEXT LINE */ |
| 14343 | 0x2028, /* LINE SEPARATOR */ |
| 14344 | 0x2029, /* PARAGRAPH SEPARATOR */ |
| 14345 | }; |
| 14346 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 14347 | /* Init the implementation */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 14348 | unicode_empty = PyUnicode_New(0, 0); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 14349 | if (!unicode_empty) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14350 | Py_FatalError("Can't create empty string"); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 14351 | assert(_PyUnicode_CheckConsistency(unicode_empty, 1)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 14352 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 14353 | for (i = 0; i < 256; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14354 | unicode_latin1[i] = NULL; |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 14355 | if (PyType_Ready(&PyUnicode_Type) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14356 | Py_FatalError("Can't initialize 'unicode'"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14357 | |
| 14358 | /* initialize the linebreak bloom filter */ |
| 14359 | bloom_linebreak = make_bloom_mask( |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14360 | PyUnicode_2BYTE_KIND, linebreak, |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 14361 | Py_ARRAY_LENGTH(linebreak)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 14362 | |
| 14363 | PyType_Ready(&EncodingMapType); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 14364 | |
| 14365 | #ifdef HAVE_MBCS |
| 14366 | winver.dwOSVersionInfoSize = sizeof(winver); |
| 14367 | if (!GetVersionEx((OSVERSIONINFO*)&winver)) { |
| 14368 | PyErr_SetFromWindowsErr(0); |
| 14369 | return -1; |
| 14370 | } |
| 14371 | #endif |
| 14372 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14373 | } |
| 14374 | |
| 14375 | /* Finalize the Unicode implementation */ |
| 14376 | |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 14377 | int |
| 14378 | PyUnicode_ClearFreeList(void) |
| 14379 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14380 | return 0; |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 14381 | } |
| 14382 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14383 | void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 14384 | _PyUnicode_Fini(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14385 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 14386 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14387 | |
Guido van Rossum | 4ae8ef8 | 2000-10-03 18:09:04 +0000 | [diff] [blame] | 14388 | Py_XDECREF(unicode_empty); |
| 14389 | unicode_empty = NULL; |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 14390 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 14391 | for (i = 0; i < 256; i++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14392 | if (unicode_latin1[i]) { |
| 14393 | Py_DECREF(unicode_latin1[i]); |
| 14394 | unicode_latin1[i] = NULL; |
| 14395 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 14396 | } |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 14397 | _PyUnicode_ClearStaticStrings(); |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 14398 | (void)PyUnicode_ClearFreeList(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14399 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 14400 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14401 | void |
| 14402 | PyUnicode_InternInPlace(PyObject **p) |
| 14403 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14404 | register PyObject *s = *p; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14405 | PyObject *t; |
Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 14406 | #ifdef Py_DEBUG |
| 14407 | assert(s != NULL); |
| 14408 | assert(_PyUnicode_CHECK(s)); |
| 14409 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14410 | if (s == NULL || !PyUnicode_Check(s)) |
Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 14411 | return; |
| 14412 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14413 | /* If it's a subclass, we don't really know what putting |
| 14414 | it in the interned dict might do. */ |
| 14415 | if (!PyUnicode_CheckExact(s)) |
| 14416 | return; |
| 14417 | if (PyUnicode_CHECK_INTERNED(s)) |
| 14418 | return; |
| 14419 | if (interned == NULL) { |
| 14420 | interned = PyDict_New(); |
| 14421 | if (interned == NULL) { |
| 14422 | PyErr_Clear(); /* Don't leave an exception */ |
| 14423 | return; |
| 14424 | } |
| 14425 | } |
| 14426 | /* It might be that the GetItem call fails even |
| 14427 | though the key is present in the dictionary, |
| 14428 | namely when this happens during a stack overflow. */ |
| 14429 | Py_ALLOW_RECURSION |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 14430 | t = PyDict_GetItem(interned, s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14431 | Py_END_ALLOW_RECURSION |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 14432 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14433 | if (t) { |
| 14434 | Py_INCREF(t); |
| 14435 | Py_DECREF(*p); |
| 14436 | *p = t; |
| 14437 | return; |
| 14438 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14439 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14440 | PyThreadState_GET()->recursion_critical = 1; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 14441 | if (PyDict_SetItem(interned, s, s) < 0) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14442 | PyErr_Clear(); |
| 14443 | PyThreadState_GET()->recursion_critical = 0; |
| 14444 | return; |
| 14445 | } |
| 14446 | PyThreadState_GET()->recursion_critical = 0; |
| 14447 | /* The two references in interned are not counted by refcnt. |
| 14448 | The deallocator will take care of this */ |
| 14449 | Py_REFCNT(s) -= 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14450 | _PyUnicode_STATE(s).interned = SSTATE_INTERNED_MORTAL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14451 | } |
| 14452 | |
| 14453 | void |
| 14454 | PyUnicode_InternImmortal(PyObject **p) |
| 14455 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14456 | PyUnicode_InternInPlace(p); |
| 14457 | if (PyUnicode_CHECK_INTERNED(*p) != SSTATE_INTERNED_IMMORTAL) { |
Victor Stinner | af9e4b8 | 2011-10-23 20:07:00 +0200 | [diff] [blame] | 14458 | _PyUnicode_STATE(*p).interned = SSTATE_INTERNED_IMMORTAL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14459 | Py_INCREF(*p); |
| 14460 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14461 | } |
| 14462 | |
| 14463 | PyObject * |
| 14464 | PyUnicode_InternFromString(const char *cp) |
| 14465 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14466 | PyObject *s = PyUnicode_FromString(cp); |
| 14467 | if (s == NULL) |
| 14468 | return NULL; |
| 14469 | PyUnicode_InternInPlace(&s); |
| 14470 | return s; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14471 | } |
| 14472 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 14473 | void |
| 14474 | _Py_ReleaseInternedUnicodeStrings(void) |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14475 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14476 | PyObject *keys; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14477 | PyObject *s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14478 | Py_ssize_t i, n; |
| 14479 | Py_ssize_t immortal_size = 0, mortal_size = 0; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14480 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14481 | if (interned == NULL || !PyDict_Check(interned)) |
| 14482 | return; |
| 14483 | keys = PyDict_Keys(interned); |
| 14484 | if (keys == NULL || !PyList_Check(keys)) { |
| 14485 | PyErr_Clear(); |
| 14486 | return; |
| 14487 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14488 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14489 | /* Since _Py_ReleaseInternedUnicodeStrings() is intended to help a leak |
| 14490 | detector, interned unicode strings are not forcibly deallocated; |
| 14491 | rather, we give them their stolen references back, and then clear |
| 14492 | and DECREF the interned dict. */ |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14493 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14494 | n = PyList_GET_SIZE(keys); |
| 14495 | fprintf(stderr, "releasing %" PY_FORMAT_SIZE_T "d interned strings\n", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14496 | n); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14497 | for (i = 0; i < n; i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14498 | s = PyList_GET_ITEM(keys, i); |
Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 14499 | if (PyUnicode_READY(s) == -1) { |
| 14500 | assert(0 && "could not ready string"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14501 | fprintf(stderr, "could not ready string\n"); |
Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 14502 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14503 | switch (PyUnicode_CHECK_INTERNED(s)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14504 | case SSTATE_NOT_INTERNED: |
| 14505 | /* XXX Shouldn't happen */ |
| 14506 | break; |
| 14507 | case SSTATE_INTERNED_IMMORTAL: |
| 14508 | Py_REFCNT(s) += 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14509 | immortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14510 | break; |
| 14511 | case SSTATE_INTERNED_MORTAL: |
| 14512 | Py_REFCNT(s) += 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14513 | mortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14514 | break; |
| 14515 | default: |
| 14516 | Py_FatalError("Inconsistent interned string state."); |
| 14517 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14518 | _PyUnicode_STATE(s).interned = SSTATE_NOT_INTERNED; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14519 | } |
| 14520 | fprintf(stderr, "total size of all interned strings: " |
| 14521 | "%" PY_FORMAT_SIZE_T "d/%" PY_FORMAT_SIZE_T "d " |
| 14522 | "mortal/immortal\n", mortal_size, immortal_size); |
| 14523 | Py_DECREF(keys); |
| 14524 | PyDict_Clear(interned); |
| 14525 | Py_DECREF(interned); |
| 14526 | interned = NULL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14527 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14528 | |
| 14529 | |
| 14530 | /********************* Unicode Iterator **************************/ |
| 14531 | |
| 14532 | typedef struct { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14533 | PyObject_HEAD |
| 14534 | Py_ssize_t it_index; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14535 | PyObject *it_seq; /* Set to NULL when iterator is exhausted */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14536 | } unicodeiterobject; |
| 14537 | |
| 14538 | static void |
| 14539 | unicodeiter_dealloc(unicodeiterobject *it) |
| 14540 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14541 | _PyObject_GC_UNTRACK(it); |
| 14542 | Py_XDECREF(it->it_seq); |
| 14543 | PyObject_GC_Del(it); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14544 | } |
| 14545 | |
| 14546 | static int |
| 14547 | unicodeiter_traverse(unicodeiterobject *it, visitproc visit, void *arg) |
| 14548 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14549 | Py_VISIT(it->it_seq); |
| 14550 | return 0; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14551 | } |
| 14552 | |
| 14553 | static PyObject * |
| 14554 | unicodeiter_next(unicodeiterobject *it) |
| 14555 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14556 | PyObject *seq, *item; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14557 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14558 | assert(it != NULL); |
| 14559 | seq = it->it_seq; |
| 14560 | if (seq == NULL) |
| 14561 | return NULL; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 14562 | assert(_PyUnicode_CHECK(seq)); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14563 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14564 | if (it->it_index < PyUnicode_GET_LENGTH(seq)) { |
| 14565 | int kind = PyUnicode_KIND(seq); |
| 14566 | void *data = PyUnicode_DATA(seq); |
| 14567 | Py_UCS4 chr = PyUnicode_READ(kind, data, it->it_index); |
| 14568 | item = PyUnicode_FromOrdinal(chr); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14569 | if (item != NULL) |
| 14570 | ++it->it_index; |
| 14571 | return item; |
| 14572 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14573 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14574 | Py_DECREF(seq); |
| 14575 | it->it_seq = NULL; |
| 14576 | return NULL; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14577 | } |
| 14578 | |
| 14579 | static PyObject * |
| 14580 | unicodeiter_len(unicodeiterobject *it) |
| 14581 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14582 | Py_ssize_t len = 0; |
| 14583 | if (it->it_seq) |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 14584 | len = PyUnicode_GET_LENGTH(it->it_seq) - it->it_index; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14585 | return PyLong_FromSsize_t(len); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14586 | } |
| 14587 | |
| 14588 | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
| 14589 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 14590 | static PyObject * |
| 14591 | unicodeiter_reduce(unicodeiterobject *it) |
| 14592 | { |
| 14593 | if (it->it_seq != NULL) { |
Antoine Pitrou | a701388 | 2012-04-05 00:04:20 +0200 | [diff] [blame] | 14594 | return Py_BuildValue("N(O)n", _PyObject_GetBuiltin("iter"), |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 14595 | it->it_seq, it->it_index); |
| 14596 | } else { |
| 14597 | PyObject *u = PyUnicode_FromUnicode(NULL, 0); |
| 14598 | if (u == NULL) |
| 14599 | return NULL; |
Antoine Pitrou | a701388 | 2012-04-05 00:04:20 +0200 | [diff] [blame] | 14600 | return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), u); |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 14601 | } |
| 14602 | } |
| 14603 | |
| 14604 | PyDoc_STRVAR(reduce_doc, "Return state information for pickling."); |
| 14605 | |
| 14606 | static PyObject * |
| 14607 | unicodeiter_setstate(unicodeiterobject *it, PyObject *state) |
| 14608 | { |
| 14609 | Py_ssize_t index = PyLong_AsSsize_t(state); |
| 14610 | if (index == -1 && PyErr_Occurred()) |
| 14611 | return NULL; |
| 14612 | if (index < 0) |
| 14613 | index = 0; |
| 14614 | it->it_index = index; |
| 14615 | Py_RETURN_NONE; |
| 14616 | } |
| 14617 | |
| 14618 | PyDoc_STRVAR(setstate_doc, "Set state information for unpickling."); |
| 14619 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14620 | static PyMethodDef unicodeiter_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14621 | {"__length_hint__", (PyCFunction)unicodeiter_len, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14622 | length_hint_doc}, |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 14623 | {"__reduce__", (PyCFunction)unicodeiter_reduce, METH_NOARGS, |
| 14624 | reduce_doc}, |
| 14625 | {"__setstate__", (PyCFunction)unicodeiter_setstate, METH_O, |
| 14626 | setstate_doc}, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14627 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14628 | }; |
| 14629 | |
| 14630 | PyTypeObject PyUnicodeIter_Type = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14631 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 14632 | "str_iterator", /* tp_name */ |
| 14633 | sizeof(unicodeiterobject), /* tp_basicsize */ |
| 14634 | 0, /* tp_itemsize */ |
| 14635 | /* methods */ |
| 14636 | (destructor)unicodeiter_dealloc, /* tp_dealloc */ |
| 14637 | 0, /* tp_print */ |
| 14638 | 0, /* tp_getattr */ |
| 14639 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 14640 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14641 | 0, /* tp_repr */ |
| 14642 | 0, /* tp_as_number */ |
| 14643 | 0, /* tp_as_sequence */ |
| 14644 | 0, /* tp_as_mapping */ |
| 14645 | 0, /* tp_hash */ |
| 14646 | 0, /* tp_call */ |
| 14647 | 0, /* tp_str */ |
| 14648 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 14649 | 0, /* tp_setattro */ |
| 14650 | 0, /* tp_as_buffer */ |
| 14651 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 14652 | 0, /* tp_doc */ |
| 14653 | (traverseproc)unicodeiter_traverse, /* tp_traverse */ |
| 14654 | 0, /* tp_clear */ |
| 14655 | 0, /* tp_richcompare */ |
| 14656 | 0, /* tp_weaklistoffset */ |
| 14657 | PyObject_SelfIter, /* tp_iter */ |
| 14658 | (iternextfunc)unicodeiter_next, /* tp_iternext */ |
| 14659 | unicodeiter_methods, /* tp_methods */ |
| 14660 | 0, |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14661 | }; |
| 14662 | |
| 14663 | static PyObject * |
| 14664 | unicode_iter(PyObject *seq) |
| 14665 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14666 | unicodeiterobject *it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14667 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14668 | if (!PyUnicode_Check(seq)) { |
| 14669 | PyErr_BadInternalCall(); |
| 14670 | return NULL; |
| 14671 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14672 | if (PyUnicode_READY(seq) == -1) |
| 14673 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14674 | it = PyObject_GC_New(unicodeiterobject, &PyUnicodeIter_Type); |
| 14675 | if (it == NULL) |
| 14676 | return NULL; |
| 14677 | it->it_index = 0; |
| 14678 | Py_INCREF(seq); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14679 | it->it_seq = seq; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14680 | _PyObject_GC_TRACK(it); |
| 14681 | return (PyObject *)it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14682 | } |
| 14683 | |
Martin v. Löwis | 0d3072e | 2011-10-31 08:40:56 +0100 | [diff] [blame] | 14684 | |
| 14685 | size_t |
| 14686 | Py_UNICODE_strlen(const Py_UNICODE *u) |
| 14687 | { |
| 14688 | int res = 0; |
| 14689 | while(*u++) |
| 14690 | res++; |
| 14691 | return res; |
| 14692 | } |
| 14693 | |
| 14694 | Py_UNICODE* |
| 14695 | Py_UNICODE_strcpy(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 14696 | { |
| 14697 | Py_UNICODE *u = s1; |
| 14698 | while ((*u++ = *s2++)); |
| 14699 | return s1; |
| 14700 | } |
| 14701 | |
| 14702 | Py_UNICODE* |
| 14703 | Py_UNICODE_strncpy(Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 14704 | { |
| 14705 | Py_UNICODE *u = s1; |
| 14706 | while ((*u++ = *s2++)) |
| 14707 | if (n-- == 0) |
| 14708 | break; |
| 14709 | return s1; |
| 14710 | } |
| 14711 | |
| 14712 | Py_UNICODE* |
| 14713 | Py_UNICODE_strcat(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 14714 | { |
| 14715 | Py_UNICODE *u1 = s1; |
| 14716 | u1 += Py_UNICODE_strlen(u1); |
| 14717 | Py_UNICODE_strcpy(u1, s2); |
| 14718 | return s1; |
| 14719 | } |
| 14720 | |
| 14721 | int |
| 14722 | Py_UNICODE_strcmp(const Py_UNICODE *s1, const Py_UNICODE *s2) |
| 14723 | { |
| 14724 | while (*s1 && *s2 && *s1 == *s2) |
| 14725 | s1++, s2++; |
| 14726 | if (*s1 && *s2) |
| 14727 | return (*s1 < *s2) ? -1 : +1; |
| 14728 | if (*s1) |
| 14729 | return 1; |
| 14730 | if (*s2) |
| 14731 | return -1; |
| 14732 | return 0; |
| 14733 | } |
| 14734 | |
| 14735 | int |
| 14736 | Py_UNICODE_strncmp(const Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 14737 | { |
| 14738 | register Py_UNICODE u1, u2; |
| 14739 | for (; n != 0; n--) { |
| 14740 | u1 = *s1; |
| 14741 | u2 = *s2; |
| 14742 | if (u1 != u2) |
| 14743 | return (u1 < u2) ? -1 : +1; |
| 14744 | if (u1 == '\0') |
| 14745 | return 0; |
| 14746 | s1++; |
| 14747 | s2++; |
| 14748 | } |
| 14749 | return 0; |
| 14750 | } |
| 14751 | |
| 14752 | Py_UNICODE* |
| 14753 | Py_UNICODE_strchr(const Py_UNICODE *s, Py_UNICODE c) |
| 14754 | { |
| 14755 | const Py_UNICODE *p; |
| 14756 | for (p = s; *p; p++) |
| 14757 | if (*p == c) |
| 14758 | return (Py_UNICODE*)p; |
| 14759 | return NULL; |
| 14760 | } |
| 14761 | |
| 14762 | Py_UNICODE* |
| 14763 | Py_UNICODE_strrchr(const Py_UNICODE *s, Py_UNICODE c) |
| 14764 | { |
| 14765 | const Py_UNICODE *p; |
| 14766 | p = s + Py_UNICODE_strlen(s); |
| 14767 | while (p != s) { |
| 14768 | p--; |
| 14769 | if (*p == c) |
| 14770 | return (Py_UNICODE*)p; |
| 14771 | } |
| 14772 | return NULL; |
| 14773 | } |
Victor Stinner | 331ea92 | 2010-08-10 16:37:20 +0000 | [diff] [blame] | 14774 | |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14775 | Py_UNICODE* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14776 | PyUnicode_AsUnicodeCopy(PyObject *unicode) |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14777 | { |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 14778 | Py_UNICODE *u, *copy; |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14779 | Py_ssize_t len, size; |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14780 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14781 | if (!PyUnicode_Check(unicode)) { |
| 14782 | PyErr_BadArgument(); |
| 14783 | return NULL; |
| 14784 | } |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14785 | u = PyUnicode_AsUnicodeAndSize(unicode, &len); |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 14786 | if (u == NULL) |
| 14787 | return NULL; |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14788 | /* Ensure we won't overflow the size. */ |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14789 | if (len > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14790 | PyErr_NoMemory(); |
| 14791 | return NULL; |
| 14792 | } |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14793 | size = len + 1; /* copy the null character */ |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14794 | size *= sizeof(Py_UNICODE); |
| 14795 | copy = PyMem_Malloc(size); |
| 14796 | if (copy == NULL) { |
| 14797 | PyErr_NoMemory(); |
| 14798 | return NULL; |
| 14799 | } |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 14800 | memcpy(copy, u, size); |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14801 | return copy; |
| 14802 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 14803 | |
Georg Brandl | 66c221e | 2010-10-14 07:04:07 +0000 | [diff] [blame] | 14804 | /* A _string module, to export formatter_parser and formatter_field_name_split |
| 14805 | to the string.Formatter class implemented in Python. */ |
| 14806 | |
| 14807 | static PyMethodDef _string_methods[] = { |
| 14808 | {"formatter_field_name_split", (PyCFunction) formatter_field_name_split, |
| 14809 | METH_O, PyDoc_STR("split the argument as a field name")}, |
| 14810 | {"formatter_parser", (PyCFunction) formatter_parser, |
| 14811 | METH_O, PyDoc_STR("parse the argument as a format string")}, |
| 14812 | {NULL, NULL} |
| 14813 | }; |
| 14814 | |
| 14815 | static struct PyModuleDef _string_module = { |
| 14816 | PyModuleDef_HEAD_INIT, |
| 14817 | "_string", |
| 14818 | PyDoc_STR("string helper module"), |
| 14819 | 0, |
| 14820 | _string_methods, |
| 14821 | NULL, |
| 14822 | NULL, |
| 14823 | NULL, |
| 14824 | NULL |
| 14825 | }; |
| 14826 | |
| 14827 | PyMODINIT_FUNC |
| 14828 | PyInit__string(void) |
| 14829 | { |
| 14830 | return PyModule_Create(&_string_module); |
| 14831 | } |
| 14832 | |
| 14833 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 14834 | #ifdef __cplusplus |
| 14835 | } |
| 14836 | #endif |