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 | 7989157 | 2012-05-03 13:43:07 +0200 | [diff] [blame] | 657 | assert(unicode_modifiable(unicode)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 658 | assert(PyUnicode_IS_READY(unicode)); |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 659 | assert(PyUnicode_IS_COMPACT(unicode)); |
| 660 | |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 661 | char_size = PyUnicode_KIND(unicode); |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 662 | if (PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 663 | struct_size = sizeof(PyASCIIObject); |
| 664 | else |
| 665 | struct_size = sizeof(PyCompactUnicodeObject); |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 666 | share_wstr = _PyUnicode_SHARE_WSTR(unicode); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 667 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 668 | if (length > ((PY_SSIZE_T_MAX - struct_size) / char_size - 1)) { |
| 669 | PyErr_NoMemory(); |
| 670 | return NULL; |
| 671 | } |
| 672 | new_size = (struct_size + (length + 1) * char_size); |
| 673 | |
Victor Stinner | 84def37 | 2011-12-11 20:04:56 +0100 | [diff] [blame] | 674 | _Py_DEC_REFTOTAL; |
| 675 | _Py_ForgetReference(unicode); |
| 676 | |
| 677 | new_unicode = (PyObject *)PyObject_REALLOC((char *)unicode, new_size); |
| 678 | if (new_unicode == NULL) { |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 679 | _Py_NewReference(unicode); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 680 | PyErr_NoMemory(); |
| 681 | return NULL; |
| 682 | } |
Victor Stinner | 84def37 | 2011-12-11 20:04:56 +0100 | [diff] [blame] | 683 | unicode = new_unicode; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 684 | _Py_NewReference(unicode); |
Victor Stinner | 84def37 | 2011-12-11 20:04:56 +0100 | [diff] [blame] | 685 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 686 | _PyUnicode_LENGTH(unicode) = length; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 687 | if (share_wstr) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 688 | _PyUnicode_WSTR(unicode) = PyUnicode_DATA(unicode); |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 689 | if (!PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 690 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 691 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 692 | PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode), |
| 693 | length, 0); |
Victor Stinner | 7989157 | 2012-05-03 13:43:07 +0200 | [diff] [blame] | 694 | assert(_PyUnicode_CheckConsistency(unicode, 0)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 695 | return unicode; |
| 696 | } |
| 697 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 698 | static int |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 699 | resize_inplace(PyObject *unicode, Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 700 | { |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 701 | wchar_t *wstr; |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 702 | Py_ssize_t new_size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 703 | assert(!PyUnicode_IS_COMPACT(unicode)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 704 | assert(Py_REFCNT(unicode) == 1); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 705 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 706 | if (PyUnicode_IS_READY(unicode)) { |
| 707 | Py_ssize_t char_size; |
Victor Stinner | 1c8d0c7 | 2011-10-03 12:11:00 +0200 | [diff] [blame] | 708 | int share_wstr, share_utf8; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 709 | void *data; |
| 710 | |
| 711 | data = _PyUnicode_DATA_ANY(unicode); |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 712 | char_size = PyUnicode_KIND(unicode); |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 713 | share_wstr = _PyUnicode_SHARE_WSTR(unicode); |
| 714 | share_utf8 = _PyUnicode_SHARE_UTF8(unicode); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 715 | |
| 716 | if (length > (PY_SSIZE_T_MAX / char_size - 1)) { |
| 717 | PyErr_NoMemory(); |
| 718 | return -1; |
| 719 | } |
| 720 | new_size = (length + 1) * char_size; |
| 721 | |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 722 | if (!share_utf8 && _PyUnicode_HAS_UTF8_MEMORY(unicode)) |
| 723 | { |
| 724 | PyObject_DEL(_PyUnicode_UTF8(unicode)); |
| 725 | _PyUnicode_UTF8(unicode) = NULL; |
| 726 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
| 727 | } |
| 728 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 729 | data = (PyObject *)PyObject_REALLOC(data, new_size); |
| 730 | if (data == NULL) { |
| 731 | PyErr_NoMemory(); |
| 732 | return -1; |
| 733 | } |
| 734 | _PyUnicode_DATA_ANY(unicode) = data; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 735 | if (share_wstr) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 736 | _PyUnicode_WSTR(unicode) = data; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 737 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 738 | } |
| 739 | if (share_utf8) { |
Victor Stinner | 1c8d0c7 | 2011-10-03 12:11:00 +0200 | [diff] [blame] | 740 | _PyUnicode_UTF8(unicode) = data; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 741 | _PyUnicode_UTF8_LENGTH(unicode) = length; |
| 742 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 743 | _PyUnicode_LENGTH(unicode) = length; |
| 744 | PyUnicode_WRITE(PyUnicode_KIND(unicode), data, length, 0); |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 745 | if (share_wstr || _PyUnicode_WSTR(unicode) == NULL) { |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 746 | assert(_PyUnicode_CheckConsistency(unicode, 0)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 747 | return 0; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 748 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 749 | } |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 750 | assert(_PyUnicode_WSTR(unicode) != NULL); |
| 751 | |
| 752 | /* check for integer overflow */ |
| 753 | if (length > PY_SSIZE_T_MAX / sizeof(wchar_t) - 1) { |
| 754 | PyErr_NoMemory(); |
| 755 | return -1; |
| 756 | } |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 757 | new_size = sizeof(wchar_t) * (length + 1); |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 758 | wstr = _PyUnicode_WSTR(unicode); |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 759 | wstr = PyObject_REALLOC(wstr, new_size); |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 760 | if (!wstr) { |
| 761 | PyErr_NoMemory(); |
| 762 | return -1; |
| 763 | } |
| 764 | _PyUnicode_WSTR(unicode) = wstr; |
| 765 | _PyUnicode_WSTR(unicode)[length] = 0; |
| 766 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 767 | assert(_PyUnicode_CheckConsistency(unicode, 0)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 768 | return 0; |
| 769 | } |
| 770 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 771 | static PyObject* |
| 772 | resize_copy(PyObject *unicode, Py_ssize_t length) |
| 773 | { |
| 774 | Py_ssize_t copy_length; |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 775 | if (_PyUnicode_KIND(unicode) != PyUnicode_WCHAR_KIND) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 776 | PyObject *copy; |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 777 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 778 | if (PyUnicode_READY(unicode) == -1) |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 779 | return NULL; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 780 | |
| 781 | copy = PyUnicode_New(length, PyUnicode_MAX_CHAR_VALUE(unicode)); |
| 782 | if (copy == NULL) |
| 783 | return NULL; |
| 784 | |
| 785 | copy_length = Py_MIN(length, PyUnicode_GET_LENGTH(unicode)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 786 | copy_characters(copy, 0, unicode, 0, copy_length); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 787 | return copy; |
Victor Stinner | 8cfcbed | 2011-10-03 23:19:21 +0200 | [diff] [blame] | 788 | } |
| 789 | else { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 790 | PyObject *w; |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 791 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 792 | w = (PyObject*)_PyUnicode_New(length); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 793 | if (w == NULL) |
| 794 | return NULL; |
| 795 | copy_length = _PyUnicode_WSTR_LENGTH(unicode); |
| 796 | copy_length = Py_MIN(copy_length, length); |
| 797 | Py_UNICODE_COPY(_PyUnicode_WSTR(w), _PyUnicode_WSTR(unicode), |
| 798 | copy_length); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 799 | return w; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 800 | } |
| 801 | } |
| 802 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 803 | /* 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] | 804 | Ux0000 terminated; some code (e.g. new_identifier) |
| 805 | relies on that. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 806 | |
| 807 | XXX This allocator could further be enhanced by assuring that the |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 808 | free list never reduces its size below 1. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 809 | |
| 810 | */ |
| 811 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 812 | #ifdef Py_DEBUG |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 813 | static int unicode_old_new_calls = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 814 | #endif |
| 815 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 816 | static PyUnicodeObject * |
| 817 | _PyUnicode_New(Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 818 | { |
| 819 | register PyUnicodeObject *unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 820 | size_t new_size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 821 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 822 | /* Optimization for empty strings */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 823 | if (length == 0 && unicode_empty != NULL) { |
| 824 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 825 | return (PyUnicodeObject*)unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 826 | } |
| 827 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 828 | /* Ensure we won't overflow the size. */ |
| 829 | if (length > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
| 830 | return (PyUnicodeObject *)PyErr_NoMemory(); |
| 831 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 832 | if (length < 0) { |
| 833 | PyErr_SetString(PyExc_SystemError, |
| 834 | "Negative size passed to _PyUnicode_New"); |
| 835 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 836 | } |
| 837 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 838 | #ifdef Py_DEBUG |
| 839 | ++unicode_old_new_calls; |
| 840 | #endif |
| 841 | |
| 842 | unicode = PyObject_New(PyUnicodeObject, &PyUnicode_Type); |
| 843 | if (unicode == NULL) |
| 844 | return NULL; |
| 845 | new_size = sizeof(Py_UNICODE) * ((size_t)length + 1); |
| 846 | _PyUnicode_WSTR(unicode) = (Py_UNICODE*) PyObject_MALLOC(new_size); |
| 847 | if (!_PyUnicode_WSTR(unicode)) { |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 848 | Py_DECREF(unicode); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 849 | PyErr_NoMemory(); |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 850 | return NULL; |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 851 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 852 | |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 853 | /* Initialize the first element to guard against cases where |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 854 | * the caller fails before initializing str -- unicode_resize() |
| 855 | * reads str[0], and the Keep-Alive optimization can keep memory |
| 856 | * allocated for str alive across a call to unicode_dealloc(unicode). |
| 857 | * We don't want unicode_resize to read uninitialized memory in |
| 858 | * that case. |
| 859 | */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 860 | _PyUnicode_WSTR(unicode)[0] = 0; |
| 861 | _PyUnicode_WSTR(unicode)[length] = 0; |
| 862 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 863 | _PyUnicode_HASH(unicode) = -1; |
| 864 | _PyUnicode_STATE(unicode).interned = 0; |
| 865 | _PyUnicode_STATE(unicode).kind = 0; |
| 866 | _PyUnicode_STATE(unicode).compact = 0; |
| 867 | _PyUnicode_STATE(unicode).ready = 0; |
| 868 | _PyUnicode_STATE(unicode).ascii = 0; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 869 | _PyUnicode_DATA_ANY(unicode) = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 870 | _PyUnicode_LENGTH(unicode) = 0; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 871 | _PyUnicode_UTF8(unicode) = NULL; |
| 872 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 873 | assert(_PyUnicode_CheckConsistency((PyObject *)unicode, 0)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 874 | return unicode; |
| 875 | } |
| 876 | |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 877 | static const char* |
| 878 | unicode_kind_name(PyObject *unicode) |
| 879 | { |
Victor Stinner | 42dfd71 | 2011-10-03 14:41:45 +0200 | [diff] [blame] | 880 | /* don't check consistency: unicode_kind_name() is called from |
| 881 | _PyUnicode_Dump() */ |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 882 | if (!PyUnicode_IS_COMPACT(unicode)) |
| 883 | { |
| 884 | if (!PyUnicode_IS_READY(unicode)) |
| 885 | return "wstr"; |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 886 | switch (PyUnicode_KIND(unicode)) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 887 | { |
| 888 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 889 | if (PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 890 | return "legacy ascii"; |
| 891 | else |
| 892 | return "legacy latin1"; |
| 893 | case PyUnicode_2BYTE_KIND: |
| 894 | return "legacy UCS2"; |
| 895 | case PyUnicode_4BYTE_KIND: |
| 896 | return "legacy UCS4"; |
| 897 | default: |
| 898 | return "<legacy invalid kind>"; |
| 899 | } |
| 900 | } |
| 901 | assert(PyUnicode_IS_READY(unicode)); |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 902 | switch (PyUnicode_KIND(unicode)) { |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 903 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 904 | if (PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 905 | return "ascii"; |
| 906 | else |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 907 | return "latin1"; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 908 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 909 | return "UCS2"; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 910 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 911 | return "UCS4"; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 912 | default: |
| 913 | return "<invalid compact kind>"; |
| 914 | } |
| 915 | } |
| 916 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 917 | #ifdef Py_DEBUG |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 918 | static int unicode_new_new_calls = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 919 | |
| 920 | /* Functions wrapping macros for use in debugger */ |
| 921 | char *_PyUnicode_utf8(void *unicode){ |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 922 | return PyUnicode_UTF8(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 923 | } |
| 924 | |
| 925 | void *_PyUnicode_compact_data(void *unicode) { |
| 926 | return _PyUnicode_COMPACT_DATA(unicode); |
| 927 | } |
| 928 | void *_PyUnicode_data(void *unicode){ |
| 929 | printf("obj %p\n", unicode); |
| 930 | printf("compact %d\n", PyUnicode_IS_COMPACT(unicode)); |
| 931 | printf("compact ascii %d\n", PyUnicode_IS_COMPACT_ASCII(unicode)); |
| 932 | printf("ascii op %p\n", ((void*)((PyASCIIObject*)(unicode) + 1))); |
| 933 | printf("compact op %p\n", ((void*)((PyCompactUnicodeObject*)(unicode) + 1))); |
| 934 | printf("compact data %p\n", _PyUnicode_COMPACT_DATA(unicode)); |
| 935 | return PyUnicode_DATA(unicode); |
| 936 | } |
Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 937 | |
| 938 | void |
| 939 | _PyUnicode_Dump(PyObject *op) |
| 940 | { |
| 941 | PyASCIIObject *ascii = (PyASCIIObject *)op; |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 942 | PyCompactUnicodeObject *compact = (PyCompactUnicodeObject *)op; |
| 943 | PyUnicodeObject *unicode = (PyUnicodeObject *)op; |
| 944 | void *data; |
Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 945 | |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 946 | if (ascii->state.compact) |
Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 947 | { |
| 948 | if (ascii->state.ascii) |
| 949 | data = (ascii + 1); |
| 950 | else |
| 951 | data = (compact + 1); |
| 952 | } |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 953 | else |
| 954 | data = unicode->data.any; |
Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 955 | printf("%s: len=%zu, ",unicode_kind_name(op), ascii->length); |
| 956 | |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 957 | if (ascii->wstr == data) |
| 958 | printf("shared "); |
| 959 | printf("wstr=%p", ascii->wstr); |
Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 960 | |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 961 | if (!(ascii->state.ascii == 1 && ascii->state.compact == 1)) { |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 962 | printf(" (%zu), ", compact->wstr_length); |
| 963 | if (!ascii->state.compact && compact->utf8 == unicode->data.any) |
| 964 | printf("shared "); |
| 965 | printf("utf8=%p (%zu)", compact->utf8, compact->utf8_length); |
Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 966 | } |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 967 | printf(", data=%p\n", data); |
Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 968 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 969 | #endif |
| 970 | |
| 971 | PyObject * |
| 972 | PyUnicode_New(Py_ssize_t size, Py_UCS4 maxchar) |
| 973 | { |
| 974 | PyObject *obj; |
| 975 | PyCompactUnicodeObject *unicode; |
| 976 | void *data; |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 977 | enum PyUnicode_Kind kind; |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 978 | int is_sharing, is_ascii; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 979 | Py_ssize_t char_size; |
| 980 | Py_ssize_t struct_size; |
| 981 | |
| 982 | /* Optimization for empty strings */ |
| 983 | if (size == 0 && unicode_empty != NULL) { |
| 984 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 985 | return unicode_empty; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 986 | } |
| 987 | |
| 988 | #ifdef Py_DEBUG |
| 989 | ++unicode_new_new_calls; |
| 990 | #endif |
| 991 | |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 992 | is_ascii = 0; |
| 993 | is_sharing = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 994 | struct_size = sizeof(PyCompactUnicodeObject); |
| 995 | if (maxchar < 128) { |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 996 | kind = PyUnicode_1BYTE_KIND; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 997 | char_size = 1; |
| 998 | is_ascii = 1; |
| 999 | struct_size = sizeof(PyASCIIObject); |
| 1000 | } |
| 1001 | else if (maxchar < 256) { |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1002 | kind = PyUnicode_1BYTE_KIND; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1003 | char_size = 1; |
| 1004 | } |
| 1005 | else if (maxchar < 65536) { |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1006 | kind = PyUnicode_2BYTE_KIND; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1007 | char_size = 2; |
| 1008 | if (sizeof(wchar_t) == 2) |
| 1009 | is_sharing = 1; |
| 1010 | } |
| 1011 | else { |
Victor Stinner | c9590ad | 2012-03-04 01:34:37 +0100 | [diff] [blame] | 1012 | if (maxchar > MAX_UNICODE) { |
| 1013 | PyErr_SetString(PyExc_SystemError, |
| 1014 | "invalid maximum character passed to PyUnicode_New"); |
| 1015 | return NULL; |
| 1016 | } |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1017 | kind = PyUnicode_4BYTE_KIND; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1018 | char_size = 4; |
| 1019 | if (sizeof(wchar_t) == 4) |
| 1020 | is_sharing = 1; |
| 1021 | } |
| 1022 | |
| 1023 | /* Ensure we won't overflow the size. */ |
| 1024 | if (size < 0) { |
| 1025 | PyErr_SetString(PyExc_SystemError, |
| 1026 | "Negative size passed to PyUnicode_New"); |
| 1027 | return NULL; |
| 1028 | } |
| 1029 | if (size > ((PY_SSIZE_T_MAX - struct_size) / char_size - 1)) |
| 1030 | return PyErr_NoMemory(); |
| 1031 | |
| 1032 | /* Duplicated allocation code from _PyObject_New() instead of a call to |
| 1033 | * PyObject_New() so we are able to allocate space for the object and |
| 1034 | * it's data buffer. |
| 1035 | */ |
| 1036 | obj = (PyObject *) PyObject_MALLOC(struct_size + (size + 1) * char_size); |
| 1037 | if (obj == NULL) |
| 1038 | return PyErr_NoMemory(); |
| 1039 | obj = PyObject_INIT(obj, &PyUnicode_Type); |
| 1040 | if (obj == NULL) |
| 1041 | return NULL; |
| 1042 | |
| 1043 | unicode = (PyCompactUnicodeObject *)obj; |
| 1044 | if (is_ascii) |
| 1045 | data = ((PyASCIIObject*)obj) + 1; |
| 1046 | else |
| 1047 | data = unicode + 1; |
| 1048 | _PyUnicode_LENGTH(unicode) = size; |
| 1049 | _PyUnicode_HASH(unicode) = -1; |
| 1050 | _PyUnicode_STATE(unicode).interned = 0; |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1051 | _PyUnicode_STATE(unicode).kind = kind; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1052 | _PyUnicode_STATE(unicode).compact = 1; |
| 1053 | _PyUnicode_STATE(unicode).ready = 1; |
| 1054 | _PyUnicode_STATE(unicode).ascii = is_ascii; |
| 1055 | if (is_ascii) { |
| 1056 | ((char*)data)[size] = 0; |
| 1057 | _PyUnicode_WSTR(unicode) = NULL; |
| 1058 | } |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1059 | else if (kind == PyUnicode_1BYTE_KIND) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1060 | ((char*)data)[size] = 0; |
| 1061 | _PyUnicode_WSTR(unicode) = NULL; |
| 1062 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1063 | unicode->utf8 = NULL; |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 1064 | unicode->utf8_length = 0; |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1065 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1066 | else { |
| 1067 | unicode->utf8 = NULL; |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 1068 | unicode->utf8_length = 0; |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1069 | if (kind == PyUnicode_2BYTE_KIND) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1070 | ((Py_UCS2*)data)[size] = 0; |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1071 | else /* kind == PyUnicode_4BYTE_KIND */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1072 | ((Py_UCS4*)data)[size] = 0; |
| 1073 | if (is_sharing) { |
| 1074 | _PyUnicode_WSTR_LENGTH(unicode) = size; |
| 1075 | _PyUnicode_WSTR(unicode) = (wchar_t *)data; |
| 1076 | } |
| 1077 | else { |
| 1078 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1079 | _PyUnicode_WSTR(unicode) = NULL; |
| 1080 | } |
| 1081 | } |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1082 | #ifdef Py_DEBUG |
| 1083 | /* Fill the data with invalid characters to detect bugs earlier. |
| 1084 | _PyUnicode_CheckConsistency(str, 1) detects invalid characters, |
| 1085 | at least for ASCII and UCS-4 strings. U+00FF is invalid in ASCII |
| 1086 | and U+FFFFFFFF is an invalid character in Unicode 6.0. */ |
| 1087 | memset(data, 0xff, size * kind); |
| 1088 | #endif |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 1089 | assert(_PyUnicode_CheckConsistency((PyObject*)unicode, 0)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1090 | return obj; |
| 1091 | } |
| 1092 | |
| 1093 | #if SIZEOF_WCHAR_T == 2 |
| 1094 | /* Helper function to convert a 16-bits wchar_t representation to UCS4, this |
| 1095 | will decode surrogate pairs, the other conversions are implemented as macros |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 1096 | for efficiency. |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1097 | |
| 1098 | This function assumes that unicode can hold one more code point than wstr |
| 1099 | characters for a terminating null character. */ |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1100 | static void |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1101 | 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] | 1102 | PyObject *unicode) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1103 | { |
| 1104 | const wchar_t *iter; |
| 1105 | Py_UCS4 *ucs4_out; |
| 1106 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1107 | assert(unicode != NULL); |
| 1108 | assert(_PyUnicode_CHECK(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1109 | assert(_PyUnicode_KIND(unicode) == PyUnicode_4BYTE_KIND); |
| 1110 | ucs4_out = PyUnicode_4BYTE_DATA(unicode); |
| 1111 | |
| 1112 | for (iter = begin; iter < end; ) { |
| 1113 | assert(ucs4_out < (PyUnicode_4BYTE_DATA(unicode) + |
| 1114 | _PyUnicode_GET_LENGTH(unicode))); |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 1115 | if (Py_UNICODE_IS_HIGH_SURROGATE(iter[0]) |
| 1116 | && (iter+1) < end |
| 1117 | && Py_UNICODE_IS_LOW_SURROGATE(iter[1])) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1118 | { |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 1119 | *ucs4_out++ = Py_UNICODE_JOIN_SURROGATES(iter[0], iter[1]); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1120 | iter += 2; |
| 1121 | } |
| 1122 | else { |
| 1123 | *ucs4_out++ = *iter; |
| 1124 | iter++; |
| 1125 | } |
| 1126 | } |
| 1127 | assert(ucs4_out == (PyUnicode_4BYTE_DATA(unicode) + |
| 1128 | _PyUnicode_GET_LENGTH(unicode))); |
| 1129 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1130 | } |
| 1131 | #endif |
| 1132 | |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1133 | static int |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1134 | unicode_check_modifiable(PyObject *unicode) |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1135 | { |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1136 | if (!unicode_modifiable(unicode)) { |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 1137 | PyErr_SetString(PyExc_SystemError, |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1138 | "Cannot modify a string currently used"); |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1139 | return -1; |
| 1140 | } |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1141 | return 0; |
| 1142 | } |
| 1143 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1144 | static int |
| 1145 | _copy_characters(PyObject *to, Py_ssize_t to_start, |
| 1146 | PyObject *from, Py_ssize_t from_start, |
| 1147 | Py_ssize_t how_many, int check_maxchar) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1148 | { |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1149 | unsigned int from_kind, to_kind; |
| 1150 | void *from_data, *to_data; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1151 | int fast; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1152 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1153 | assert(PyUnicode_Check(from)); |
| 1154 | assert(PyUnicode_Check(to)); |
| 1155 | assert(PyUnicode_IS_READY(from)); |
| 1156 | assert(PyUnicode_IS_READY(to)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1157 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1158 | assert(PyUnicode_GET_LENGTH(from) >= how_many); |
| 1159 | assert(to_start + how_many <= PyUnicode_GET_LENGTH(to)); |
| 1160 | assert(0 <= how_many); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1161 | |
Victor Stinner | f5ca1a2 | 2011-09-28 23:54:59 +0200 | [diff] [blame] | 1162 | if (how_many == 0) |
| 1163 | return 0; |
| 1164 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1165 | from_kind = PyUnicode_KIND(from); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1166 | from_data = PyUnicode_DATA(from); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1167 | to_kind = PyUnicode_KIND(to); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1168 | to_data = PyUnicode_DATA(to); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1169 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1170 | #ifdef Py_DEBUG |
| 1171 | if (!check_maxchar |
| 1172 | && (from_kind > to_kind |
| 1173 | || (!PyUnicode_IS_ASCII(from) && PyUnicode_IS_ASCII(to)))) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 1174 | { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1175 | const Py_UCS4 to_maxchar = PyUnicode_MAX_CHAR_VALUE(to); |
| 1176 | Py_UCS4 ch; |
| 1177 | Py_ssize_t i; |
| 1178 | for (i=0; i < how_many; i++) { |
| 1179 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
| 1180 | assert(ch <= to_maxchar); |
| 1181 | } |
| 1182 | } |
| 1183 | #endif |
| 1184 | fast = (from_kind == to_kind); |
| 1185 | if (check_maxchar |
| 1186 | && (!PyUnicode_IS_ASCII(from) && PyUnicode_IS_ASCII(to))) |
| 1187 | { |
| 1188 | /* deny latin1 => ascii */ |
| 1189 | fast = 0; |
| 1190 | } |
| 1191 | |
| 1192 | if (fast) { |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 1193 | Py_MEMCPY((char*)to_data + to_kind * to_start, |
| 1194 | (char*)from_data + from_kind * from_start, |
| 1195 | to_kind * how_many); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1196 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1197 | else if (from_kind == PyUnicode_1BYTE_KIND |
| 1198 | && to_kind == PyUnicode_2BYTE_KIND) |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1199 | { |
| 1200 | _PyUnicode_CONVERT_BYTES( |
| 1201 | Py_UCS1, Py_UCS2, |
| 1202 | PyUnicode_1BYTE_DATA(from) + from_start, |
| 1203 | PyUnicode_1BYTE_DATA(from) + from_start + how_many, |
| 1204 | PyUnicode_2BYTE_DATA(to) + to_start |
| 1205 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1206 | } |
Victor Stinner | 157f83f | 2011-09-28 21:41:31 +0200 | [diff] [blame] | 1207 | else if (from_kind == PyUnicode_1BYTE_KIND |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1208 | && to_kind == PyUnicode_4BYTE_KIND) |
| 1209 | { |
| 1210 | _PyUnicode_CONVERT_BYTES( |
| 1211 | Py_UCS1, Py_UCS4, |
| 1212 | PyUnicode_1BYTE_DATA(from) + from_start, |
| 1213 | PyUnicode_1BYTE_DATA(from) + from_start + how_many, |
| 1214 | PyUnicode_4BYTE_DATA(to) + to_start |
| 1215 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1216 | } |
| 1217 | else if (from_kind == PyUnicode_2BYTE_KIND |
| 1218 | && to_kind == PyUnicode_4BYTE_KIND) |
| 1219 | { |
| 1220 | _PyUnicode_CONVERT_BYTES( |
| 1221 | Py_UCS2, Py_UCS4, |
| 1222 | PyUnicode_2BYTE_DATA(from) + from_start, |
| 1223 | PyUnicode_2BYTE_DATA(from) + from_start + how_many, |
| 1224 | PyUnicode_4BYTE_DATA(to) + to_start |
| 1225 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1226 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1227 | else { |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 1228 | /* check if max_char(from substring) <= max_char(to) */ |
| 1229 | if (from_kind > to_kind |
| 1230 | /* latin1 => ascii */ |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1231 | || (!PyUnicode_IS_ASCII(from) && PyUnicode_IS_ASCII(to))) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 1232 | { |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1233 | /* slow path to check for character overflow */ |
| 1234 | const Py_UCS4 to_maxchar = PyUnicode_MAX_CHAR_VALUE(to); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1235 | Py_UCS4 ch; |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1236 | Py_ssize_t i; |
| 1237 | |
Victor Stinner | 56c161a | 2011-10-06 02:47:11 +0200 | [diff] [blame] | 1238 | #ifdef Py_DEBUG |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1239 | for (i=0; i < how_many; i++) { |
| 1240 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
Victor Stinner | 56c161a | 2011-10-06 02:47:11 +0200 | [diff] [blame] | 1241 | assert(ch <= to_maxchar); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1242 | PyUnicode_WRITE(to_kind, to_data, to_start + i, ch); |
| 1243 | } |
Victor Stinner | 56c161a | 2011-10-06 02:47:11 +0200 | [diff] [blame] | 1244 | #else |
| 1245 | if (!check_maxchar) { |
| 1246 | for (i=0; i < how_many; i++) { |
| 1247 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
| 1248 | PyUnicode_WRITE(to_kind, to_data, to_start + i, ch); |
| 1249 | } |
| 1250 | } |
| 1251 | else { |
| 1252 | for (i=0; i < how_many; i++) { |
| 1253 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
| 1254 | if (ch > to_maxchar) |
| 1255 | return 1; |
| 1256 | PyUnicode_WRITE(to_kind, to_data, to_start + i, ch); |
| 1257 | } |
| 1258 | } |
| 1259 | #endif |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1260 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1261 | else { |
Victor Stinner | 56c161a | 2011-10-06 02:47:11 +0200 | [diff] [blame] | 1262 | assert(0 && "inconsistent state"); |
| 1263 | return 1; |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1264 | } |
| 1265 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1266 | return 0; |
| 1267 | } |
| 1268 | |
| 1269 | static void |
| 1270 | copy_characters(PyObject *to, Py_ssize_t to_start, |
| 1271 | PyObject *from, Py_ssize_t from_start, |
| 1272 | Py_ssize_t how_many) |
| 1273 | { |
| 1274 | (void)_copy_characters(to, to_start, from, from_start, how_many, 0); |
| 1275 | } |
| 1276 | |
| 1277 | Py_ssize_t |
| 1278 | PyUnicode_CopyCharacters(PyObject *to, Py_ssize_t to_start, |
| 1279 | PyObject *from, Py_ssize_t from_start, |
| 1280 | Py_ssize_t how_many) |
| 1281 | { |
| 1282 | int err; |
| 1283 | |
| 1284 | if (!PyUnicode_Check(from) || !PyUnicode_Check(to)) { |
| 1285 | PyErr_BadInternalCall(); |
| 1286 | return -1; |
| 1287 | } |
| 1288 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 1289 | if (PyUnicode_READY(from) == -1) |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1290 | return -1; |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 1291 | if (PyUnicode_READY(to) == -1) |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1292 | return -1; |
| 1293 | |
| 1294 | how_many = Py_MIN(PyUnicode_GET_LENGTH(from), how_many); |
| 1295 | if (to_start + how_many > PyUnicode_GET_LENGTH(to)) { |
| 1296 | PyErr_Format(PyExc_SystemError, |
| 1297 | "Cannot write %zi characters at %zi " |
| 1298 | "in a string of %zi characters", |
| 1299 | how_many, to_start, PyUnicode_GET_LENGTH(to)); |
| 1300 | return -1; |
| 1301 | } |
| 1302 | |
| 1303 | if (how_many == 0) |
| 1304 | return 0; |
| 1305 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1306 | if (unicode_check_modifiable(to)) |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1307 | return -1; |
| 1308 | |
| 1309 | err = _copy_characters(to, to_start, from, from_start, how_many, 1); |
| 1310 | if (err) { |
| 1311 | PyErr_Format(PyExc_SystemError, |
| 1312 | "Cannot copy %s characters " |
| 1313 | "into a string of %s characters", |
| 1314 | unicode_kind_name(from), |
| 1315 | unicode_kind_name(to)); |
| 1316 | return -1; |
| 1317 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1318 | return how_many; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1319 | } |
| 1320 | |
Victor Stinner | 1722216 | 2011-09-28 22:15:37 +0200 | [diff] [blame] | 1321 | /* Find the maximum code point and count the number of surrogate pairs so a |
| 1322 | correct string length can be computed before converting a string to UCS4. |
| 1323 | This function counts single surrogates as a character and not as a pair. |
| 1324 | |
| 1325 | Return 0 on success, or -1 on error. */ |
| 1326 | static int |
| 1327 | find_maxchar_surrogates(const wchar_t *begin, const wchar_t *end, |
| 1328 | Py_UCS4 *maxchar, Py_ssize_t *num_surrogates) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1329 | { |
| 1330 | const wchar_t *iter; |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1331 | Py_UCS4 ch; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1332 | |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1333 | assert(num_surrogates != NULL && maxchar != NULL); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1334 | *num_surrogates = 0; |
| 1335 | *maxchar = 0; |
| 1336 | |
| 1337 | for (iter = begin; iter < end; ) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1338 | #if SIZEOF_WCHAR_T == 2 |
Victor Stinner | ca4f207 | 2011-11-22 03:38:40 +0100 | [diff] [blame] | 1339 | if (Py_UNICODE_IS_HIGH_SURROGATE(iter[0]) |
| 1340 | && (iter+1) < end |
| 1341 | && Py_UNICODE_IS_LOW_SURROGATE(iter[1])) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1342 | { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1343 | ch = Py_UNICODE_JOIN_SURROGATES(iter[0], iter[1]); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1344 | ++(*num_surrogates); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1345 | iter += 2; |
| 1346 | } |
| 1347 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1348 | #endif |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1349 | { |
| 1350 | ch = *iter; |
| 1351 | iter++; |
| 1352 | } |
| 1353 | if (ch > *maxchar) { |
| 1354 | *maxchar = ch; |
| 1355 | if (*maxchar > MAX_UNICODE) { |
| 1356 | PyErr_Format(PyExc_ValueError, |
| 1357 | "character U+%x is not in range [U+0000; U+10ffff]", |
| 1358 | ch); |
| 1359 | return -1; |
| 1360 | } |
| 1361 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1362 | } |
| 1363 | return 0; |
| 1364 | } |
| 1365 | |
| 1366 | #ifdef Py_DEBUG |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 1367 | static int unicode_ready_calls = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1368 | #endif |
| 1369 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1370 | int |
| 1371 | _PyUnicode_Ready(PyObject *unicode) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1372 | { |
| 1373 | wchar_t *end; |
| 1374 | Py_UCS4 maxchar = 0; |
| 1375 | Py_ssize_t num_surrogates; |
| 1376 | #if SIZEOF_WCHAR_T == 2 |
| 1377 | Py_ssize_t length_wo_surrogates; |
| 1378 | #endif |
| 1379 | |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 1380 | /* _PyUnicode_Ready() is only intended for old-style API usage where |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1381 | strings were created using _PyObject_New() and where no canonical |
| 1382 | representation (the str field) has been set yet aka strings |
| 1383 | which are not yet ready. */ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1384 | assert(_PyUnicode_CHECK(unicode)); |
| 1385 | assert(_PyUnicode_KIND(unicode) == PyUnicode_WCHAR_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1386 | assert(_PyUnicode_WSTR(unicode) != NULL); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1387 | assert(_PyUnicode_DATA_ANY(unicode) == NULL); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1388 | assert(_PyUnicode_UTF8(unicode) == NULL); |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1389 | /* Actually, it should neither be interned nor be anything else: */ |
| 1390 | assert(_PyUnicode_STATE(unicode).interned == SSTATE_NOT_INTERNED); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1391 | |
| 1392 | #ifdef Py_DEBUG |
| 1393 | ++unicode_ready_calls; |
| 1394 | #endif |
| 1395 | |
| 1396 | end = _PyUnicode_WSTR(unicode) + _PyUnicode_WSTR_LENGTH(unicode); |
Victor Stinner | 1722216 | 2011-09-28 22:15:37 +0200 | [diff] [blame] | 1397 | if (find_maxchar_surrogates(_PyUnicode_WSTR(unicode), end, |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1398 | &maxchar, &num_surrogates) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1399 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1400 | |
| 1401 | if (maxchar < 256) { |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1402 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC(_PyUnicode_WSTR_LENGTH(unicode) + 1); |
| 1403 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1404 | PyErr_NoMemory(); |
| 1405 | return -1; |
| 1406 | } |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1407 | _PyUnicode_CONVERT_BYTES(wchar_t, unsigned char, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1408 | _PyUnicode_WSTR(unicode), end, |
| 1409 | PyUnicode_1BYTE_DATA(unicode)); |
| 1410 | PyUnicode_1BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1411 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1412 | _PyUnicode_STATE(unicode).kind = PyUnicode_1BYTE_KIND; |
| 1413 | if (maxchar < 128) { |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1414 | _PyUnicode_STATE(unicode).ascii = 1; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1415 | _PyUnicode_UTF8(unicode) = _PyUnicode_DATA_ANY(unicode); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1416 | _PyUnicode_UTF8_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1417 | } |
| 1418 | else { |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1419 | _PyUnicode_STATE(unicode).ascii = 0; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1420 | _PyUnicode_UTF8(unicode) = NULL; |
| 1421 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1422 | } |
| 1423 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1424 | _PyUnicode_WSTR(unicode) = NULL; |
| 1425 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1426 | } |
| 1427 | /* In this case we might have to convert down from 4-byte native |
| 1428 | wchar_t to 2-byte unicode. */ |
| 1429 | else if (maxchar < 65536) { |
| 1430 | assert(num_surrogates == 0 && |
| 1431 | "FindMaxCharAndNumSurrogatePairs() messed up"); |
| 1432 | |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1433 | #if SIZEOF_WCHAR_T == 2 |
| 1434 | /* We can share representations and are done. */ |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1435 | _PyUnicode_DATA_ANY(unicode) = _PyUnicode_WSTR(unicode); |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1436 | PyUnicode_2BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1437 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1438 | _PyUnicode_STATE(unicode).kind = PyUnicode_2BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1439 | _PyUnicode_UTF8(unicode) = NULL; |
| 1440 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1441 | #else |
| 1442 | /* sizeof(wchar_t) == 4 */ |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1443 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC( |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1444 | 2 * (_PyUnicode_WSTR_LENGTH(unicode) + 1)); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1445 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1446 | PyErr_NoMemory(); |
| 1447 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1448 | } |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1449 | _PyUnicode_CONVERT_BYTES(wchar_t, Py_UCS2, |
| 1450 | _PyUnicode_WSTR(unicode), end, |
| 1451 | PyUnicode_2BYTE_DATA(unicode)); |
| 1452 | PyUnicode_2BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1453 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1454 | _PyUnicode_STATE(unicode).kind = PyUnicode_2BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1455 | _PyUnicode_UTF8(unicode) = NULL; |
| 1456 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1457 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1458 | _PyUnicode_WSTR(unicode) = NULL; |
| 1459 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1460 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1461 | } |
| 1462 | /* maxchar exeeds 16 bit, wee need 4 bytes for unicode characters */ |
| 1463 | else { |
| 1464 | #if SIZEOF_WCHAR_T == 2 |
| 1465 | /* in case the native representation is 2-bytes, we need to allocate a |
| 1466 | new normalized 4-byte version. */ |
| 1467 | length_wo_surrogates = _PyUnicode_WSTR_LENGTH(unicode) - num_surrogates; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1468 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC(4 * (length_wo_surrogates + 1)); |
| 1469 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1470 | PyErr_NoMemory(); |
| 1471 | return -1; |
| 1472 | } |
| 1473 | _PyUnicode_LENGTH(unicode) = length_wo_surrogates; |
| 1474 | _PyUnicode_STATE(unicode).kind = PyUnicode_4BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1475 | _PyUnicode_UTF8(unicode) = NULL; |
| 1476 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 126c559 | 2011-10-03 04:17:10 +0200 | [diff] [blame] | 1477 | /* unicode_convert_wchar_to_ucs4() requires a ready string */ |
| 1478 | _PyUnicode_STATE(unicode).ready = 1; |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1479 | unicode_convert_wchar_to_ucs4(_PyUnicode_WSTR(unicode), end, unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1480 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1481 | _PyUnicode_WSTR(unicode) = NULL; |
| 1482 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1483 | #else |
| 1484 | assert(num_surrogates == 0); |
| 1485 | |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1486 | _PyUnicode_DATA_ANY(unicode) = _PyUnicode_WSTR(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1487 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1488 | _PyUnicode_UTF8(unicode) = NULL; |
| 1489 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1490 | _PyUnicode_STATE(unicode).kind = PyUnicode_4BYTE_KIND; |
| 1491 | #endif |
| 1492 | PyUnicode_4BYTE_DATA(unicode)[_PyUnicode_LENGTH(unicode)] = '\0'; |
| 1493 | } |
| 1494 | _PyUnicode_STATE(unicode).ready = 1; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1495 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1496 | return 0; |
| 1497 | } |
| 1498 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1499 | static void |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1500 | unicode_dealloc(register PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1501 | { |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1502 | switch (PyUnicode_CHECK_INTERNED(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1503 | case SSTATE_NOT_INTERNED: |
| 1504 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1505 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1506 | case SSTATE_INTERNED_MORTAL: |
| 1507 | /* revive dead object temporarily for DelItem */ |
| 1508 | Py_REFCNT(unicode) = 3; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 1509 | if (PyDict_DelItem(interned, unicode) != 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1510 | Py_FatalError( |
| 1511 | "deletion of interned string failed"); |
| 1512 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1513 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1514 | case SSTATE_INTERNED_IMMORTAL: |
| 1515 | Py_FatalError("Immortal interned string died."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1516 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1517 | default: |
| 1518 | Py_FatalError("Inconsistent interned string state."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1519 | } |
| 1520 | |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 1521 | if (_PyUnicode_HAS_WSTR_MEMORY(unicode)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1522 | PyObject_DEL(_PyUnicode_WSTR(unicode)); |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 1523 | if (_PyUnicode_HAS_UTF8_MEMORY(unicode)) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1524 | PyObject_DEL(_PyUnicode_UTF8(unicode)); |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1525 | if (!PyUnicode_IS_COMPACT(unicode) && _PyUnicode_DATA_ANY(unicode)) |
| 1526 | PyObject_DEL(_PyUnicode_DATA_ANY(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1527 | |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1528 | Py_TYPE(unicode)->tp_free(unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1529 | } |
| 1530 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1531 | #ifdef Py_DEBUG |
| 1532 | static int |
| 1533 | unicode_is_singleton(PyObject *unicode) |
| 1534 | { |
| 1535 | PyASCIIObject *ascii = (PyASCIIObject *)unicode; |
| 1536 | if (unicode == unicode_empty) |
| 1537 | return 1; |
| 1538 | if (ascii->state.kind != PyUnicode_WCHAR_KIND && ascii->length == 1) |
| 1539 | { |
| 1540 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, 0); |
| 1541 | if (ch < 256 && unicode_latin1[ch] == unicode) |
| 1542 | return 1; |
| 1543 | } |
| 1544 | return 0; |
| 1545 | } |
| 1546 | #endif |
| 1547 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1548 | static int |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1549 | unicode_modifiable(PyObject *unicode) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1550 | { |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1551 | assert(_PyUnicode_CHECK(unicode)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1552 | if (Py_REFCNT(unicode) != 1) |
| 1553 | return 0; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1554 | if (_PyUnicode_HASH(unicode) != -1) |
| 1555 | return 0; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1556 | if (PyUnicode_CHECK_INTERNED(unicode)) |
| 1557 | return 0; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1558 | if (!PyUnicode_CheckExact(unicode)) |
| 1559 | return 0; |
Victor Stinner | 77bb47b | 2011-10-03 20:06:05 +0200 | [diff] [blame] | 1560 | #ifdef Py_DEBUG |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1561 | /* singleton refcount is greater than 1 */ |
| 1562 | assert(!unicode_is_singleton(unicode)); |
Victor Stinner | 77bb47b | 2011-10-03 20:06:05 +0200 | [diff] [blame] | 1563 | #endif |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1564 | return 1; |
| 1565 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1566 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1567 | static int |
| 1568 | unicode_resize(PyObject **p_unicode, Py_ssize_t length) |
| 1569 | { |
| 1570 | PyObject *unicode; |
| 1571 | Py_ssize_t old_length; |
| 1572 | |
| 1573 | assert(p_unicode != NULL); |
| 1574 | unicode = *p_unicode; |
| 1575 | |
| 1576 | assert(unicode != NULL); |
| 1577 | assert(PyUnicode_Check(unicode)); |
| 1578 | assert(0 <= length); |
| 1579 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1580 | if (_PyUnicode_KIND(unicode) == PyUnicode_WCHAR_KIND) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1581 | old_length = PyUnicode_WSTR_LENGTH(unicode); |
| 1582 | else |
| 1583 | old_length = PyUnicode_GET_LENGTH(unicode); |
| 1584 | if (old_length == length) |
| 1585 | return 0; |
| 1586 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1587 | if (length == 0) { |
| 1588 | Py_DECREF(*p_unicode); |
| 1589 | *p_unicode = unicode_empty; |
| 1590 | Py_INCREF(*p_unicode); |
| 1591 | return 0; |
| 1592 | } |
| 1593 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1594 | if (!unicode_modifiable(unicode)) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1595 | PyObject *copy = resize_copy(unicode, length); |
| 1596 | if (copy == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1597 | return -1; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1598 | Py_DECREF(*p_unicode); |
| 1599 | *p_unicode = copy; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1600 | return 0; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1601 | } |
| 1602 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1603 | if (PyUnicode_IS_COMPACT(unicode)) { |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1604 | PyObject *new_unicode = resize_compact(unicode, length); |
| 1605 | if (new_unicode == NULL) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1606 | return -1; |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1607 | *p_unicode = new_unicode; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1608 | return 0; |
Benjamin Peterson | 4bfce8f | 2011-10-03 19:35:07 -0400 | [diff] [blame] | 1609 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1610 | return resize_inplace(unicode, length); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1611 | } |
| 1612 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1613 | int |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1614 | PyUnicode_Resize(PyObject **p_unicode, Py_ssize_t length) |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 1615 | { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1616 | PyObject *unicode; |
| 1617 | if (p_unicode == NULL) { |
| 1618 | PyErr_BadInternalCall(); |
| 1619 | return -1; |
| 1620 | } |
| 1621 | unicode = *p_unicode; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1622 | if (unicode == NULL || !PyUnicode_Check(unicode) || length < 0) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1623 | { |
| 1624 | PyErr_BadInternalCall(); |
| 1625 | return -1; |
| 1626 | } |
| 1627 | return unicode_resize(p_unicode, length); |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 1628 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1629 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1630 | static int |
Victor Stinner | 1b487b4 | 2012-05-03 12:29:04 +0200 | [diff] [blame] | 1631 | unicode_widen(PyObject **p_unicode, Py_ssize_t length, |
| 1632 | unsigned int maxchar) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1633 | { |
| 1634 | PyObject *result; |
| 1635 | assert(PyUnicode_IS_READY(*p_unicode)); |
Victor Stinner | 1b487b4 | 2012-05-03 12:29:04 +0200 | [diff] [blame] | 1636 | assert(length <= PyUnicode_GET_LENGTH(*p_unicode)); |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1637 | if (maxchar <= PyUnicode_MAX_CHAR_VALUE(*p_unicode)) |
| 1638 | return 0; |
| 1639 | result = PyUnicode_New(PyUnicode_GET_LENGTH(*p_unicode), |
| 1640 | maxchar); |
| 1641 | if (result == NULL) |
| 1642 | return -1; |
Victor Stinner | 1b487b4 | 2012-05-03 12:29:04 +0200 | [diff] [blame] | 1643 | PyUnicode_CopyCharacters(result, 0, *p_unicode, 0, length); |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1644 | Py_DECREF(*p_unicode); |
| 1645 | *p_unicode = result; |
| 1646 | return 0; |
| 1647 | } |
| 1648 | |
| 1649 | static int |
| 1650 | unicode_putchar(PyObject **p_unicode, Py_ssize_t *pos, |
| 1651 | Py_UCS4 ch) |
| 1652 | { |
Victor Stinner | 15e9ed2 | 2012-02-22 13:36:20 +0100 | [diff] [blame] | 1653 | assert(ch <= MAX_UNICODE); |
Victor Stinner | 1b487b4 | 2012-05-03 12:29:04 +0200 | [diff] [blame] | 1654 | if (unicode_widen(p_unicode, *pos, ch) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1655 | return -1; |
| 1656 | PyUnicode_WRITE(PyUnicode_KIND(*p_unicode), |
| 1657 | PyUnicode_DATA(*p_unicode), |
| 1658 | (*pos)++, ch); |
| 1659 | return 0; |
| 1660 | } |
| 1661 | |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1662 | /* Copy a ASCII or latin1 char* string into a Python Unicode string. |
| 1663 | Return the length of the input string. |
| 1664 | |
Victor Stinner | b429d3b | 2012-02-22 21:22:20 +0100 | [diff] [blame] | 1665 | WARNING: The function doesn't copy the terminating null character and |
| 1666 | doesn't check the maximum character (may write a latin1 character in an |
| 1667 | ASCII string). */ |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1668 | static Py_ssize_t |
| 1669 | unicode_write_cstr(PyObject *unicode, Py_ssize_t index, const char *str) |
| 1670 | { |
| 1671 | enum PyUnicode_Kind kind = PyUnicode_KIND(unicode); |
| 1672 | void *data = PyUnicode_DATA(unicode); |
| 1673 | |
| 1674 | switch (kind) { |
| 1675 | case PyUnicode_1BYTE_KIND: { |
| 1676 | Py_ssize_t len = strlen(str); |
| 1677 | assert(index + len <= PyUnicode_GET_LENGTH(unicode)); |
Antoine Pitrou | ba6bafc | 2012-02-22 16:41:50 +0100 | [diff] [blame] | 1678 | memcpy((char *) data + index, str, len); |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1679 | return len; |
| 1680 | } |
| 1681 | case PyUnicode_2BYTE_KIND: { |
| 1682 | Py_UCS2 *start = (Py_UCS2 *)data + index; |
| 1683 | Py_UCS2 *ucs2 = start; |
| 1684 | assert(index <= PyUnicode_GET_LENGTH(unicode)); |
| 1685 | |
| 1686 | for (; *str; ++ucs2, ++str) |
| 1687 | *ucs2 = (Py_UCS2)*str; |
| 1688 | |
| 1689 | assert((ucs2 - start) <= PyUnicode_GET_LENGTH(unicode)); |
| 1690 | return ucs2 - start; |
| 1691 | } |
| 1692 | default: { |
| 1693 | Py_UCS4 *start = (Py_UCS4 *)data + index; |
| 1694 | Py_UCS4 *ucs4 = start; |
| 1695 | assert(kind == PyUnicode_4BYTE_KIND); |
| 1696 | assert(index <= PyUnicode_GET_LENGTH(unicode)); |
| 1697 | |
| 1698 | for (; *str; ++ucs4, ++str) |
| 1699 | *ucs4 = (Py_UCS4)*str; |
| 1700 | |
| 1701 | assert((ucs4 - start) <= PyUnicode_GET_LENGTH(unicode)); |
| 1702 | return ucs4 - start; |
| 1703 | } |
| 1704 | } |
| 1705 | } |
| 1706 | |
| 1707 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1708 | static PyObject* |
| 1709 | get_latin1_char(unsigned char ch) |
| 1710 | { |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1711 | PyObject *unicode = unicode_latin1[ch]; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1712 | if (!unicode) { |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1713 | unicode = PyUnicode_New(1, ch); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1714 | if (!unicode) |
| 1715 | return NULL; |
| 1716 | PyUnicode_1BYTE_DATA(unicode)[0] = ch; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1717 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1718 | unicode_latin1[ch] = unicode; |
| 1719 | } |
| 1720 | Py_INCREF(unicode); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1721 | return unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1722 | } |
| 1723 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1724 | PyObject * |
| 1725 | PyUnicode_FromUnicode(const Py_UNICODE *u, Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1726 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1727 | PyObject *unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1728 | Py_UCS4 maxchar = 0; |
| 1729 | Py_ssize_t num_surrogates; |
| 1730 | |
| 1731 | if (u == NULL) |
| 1732 | return (PyObject*)_PyUnicode_New(size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1733 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1734 | /* If the Unicode data is known at construction time, we can apply |
| 1735 | some optimizations which share commonly used objects. */ |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1736 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1737 | /* Optimization for empty strings */ |
| 1738 | if (size == 0 && unicode_empty != NULL) { |
| 1739 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1740 | return unicode_empty; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1741 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1742 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1743 | /* Single character Unicode objects in the Latin-1 range are |
| 1744 | shared when using this constructor */ |
| 1745 | if (size == 1 && *u < 256) |
| 1746 | return get_latin1_char((unsigned char)*u); |
| 1747 | |
| 1748 | /* If not empty and not single character, copy the Unicode data |
| 1749 | into the new object */ |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1750 | if (find_maxchar_surrogates(u, u + size, |
| 1751 | &maxchar, &num_surrogates) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1752 | return NULL; |
| 1753 | |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1754 | unicode = PyUnicode_New(size - num_surrogates, maxchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1755 | if (!unicode) |
| 1756 | return NULL; |
| 1757 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1758 | switch (PyUnicode_KIND(unicode)) { |
| 1759 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1760 | _PyUnicode_CONVERT_BYTES(Py_UNICODE, unsigned char, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1761 | u, u + size, PyUnicode_1BYTE_DATA(unicode)); |
| 1762 | break; |
| 1763 | case PyUnicode_2BYTE_KIND: |
| 1764 | #if Py_UNICODE_SIZE == 2 |
| 1765 | Py_MEMCPY(PyUnicode_2BYTE_DATA(unicode), u, size * 2); |
| 1766 | #else |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1767 | _PyUnicode_CONVERT_BYTES(Py_UNICODE, Py_UCS2, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1768 | u, u + size, PyUnicode_2BYTE_DATA(unicode)); |
| 1769 | #endif |
| 1770 | break; |
| 1771 | case PyUnicode_4BYTE_KIND: |
| 1772 | #if SIZEOF_WCHAR_T == 2 |
| 1773 | /* This is the only case which has to process surrogates, thus |
| 1774 | a simple copy loop is not enough and we need a function. */ |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1775 | unicode_convert_wchar_to_ucs4(u, u + size, unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1776 | #else |
| 1777 | assert(num_surrogates == 0); |
| 1778 | Py_MEMCPY(PyUnicode_4BYTE_DATA(unicode), u, size * 4); |
| 1779 | #endif |
| 1780 | break; |
| 1781 | default: |
| 1782 | assert(0 && "Impossible state"); |
| 1783 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1784 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1785 | return unicode_result(unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1786 | } |
| 1787 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1788 | PyObject * |
| 1789 | PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size) |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1790 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1791 | if (size < 0) { |
| 1792 | PyErr_SetString(PyExc_SystemError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1793 | "Negative size passed to PyUnicode_FromStringAndSize"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1794 | return NULL; |
| 1795 | } |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 1796 | if (u != NULL) |
| 1797 | return PyUnicode_DecodeUTF8Stateful(u, size, NULL, NULL); |
| 1798 | else |
| 1799 | return (PyObject *)_PyUnicode_New(size); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1800 | } |
| 1801 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1802 | PyObject * |
| 1803 | PyUnicode_FromString(const char *u) |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1804 | { |
| 1805 | size_t size = strlen(u); |
| 1806 | if (size > PY_SSIZE_T_MAX) { |
| 1807 | PyErr_SetString(PyExc_OverflowError, "input too long"); |
| 1808 | return NULL; |
| 1809 | } |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 1810 | return PyUnicode_DecodeUTF8Stateful(u, (Py_ssize_t)size, NULL, NULL); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1811 | } |
| 1812 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1813 | PyObject * |
| 1814 | _PyUnicode_FromId(_Py_Identifier *id) |
| 1815 | { |
| 1816 | if (!id->object) { |
Victor Stinner | d1cd99b | 2012-02-07 23:05:55 +0100 | [diff] [blame] | 1817 | id->object = PyUnicode_DecodeUTF8Stateful(id->string, |
| 1818 | strlen(id->string), |
| 1819 | NULL, NULL); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1820 | if (!id->object) |
| 1821 | return NULL; |
| 1822 | PyUnicode_InternInPlace(&id->object); |
| 1823 | assert(!id->next); |
| 1824 | id->next = static_strings; |
| 1825 | static_strings = id; |
| 1826 | } |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1827 | return id->object; |
| 1828 | } |
| 1829 | |
| 1830 | void |
| 1831 | _PyUnicode_ClearStaticStrings() |
| 1832 | { |
| 1833 | _Py_Identifier *i; |
| 1834 | for (i = static_strings; i; i = i->next) { |
| 1835 | Py_DECREF(i->object); |
| 1836 | i->object = NULL; |
| 1837 | i->next = NULL; |
| 1838 | } |
| 1839 | } |
| 1840 | |
Benjamin Peterson | 0df5429 | 2012-03-26 14:50:32 -0400 | [diff] [blame] | 1841 | /* Internal function, doesn't check maximum character */ |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1842 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1843 | static PyObject* |
Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 1844 | unicode_fromascii(const unsigned char* s, Py_ssize_t size) |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1845 | { |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 1846 | PyObject *unicode; |
Victor Stinner | e6b2d44 | 2011-12-11 21:54:30 +0100 | [diff] [blame] | 1847 | if (size == 1) { |
Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 1848 | #ifdef Py_DEBUG |
Victor Stinner | e6b2d44 | 2011-12-11 21:54:30 +0100 | [diff] [blame] | 1849 | assert(s[0] < 128); |
Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 1850 | #endif |
Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 1851 | return get_latin1_char(s[0]); |
Victor Stinner | e6b2d44 | 2011-12-11 21:54:30 +0100 | [diff] [blame] | 1852 | } |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 1853 | unicode = PyUnicode_New(size, 127); |
| 1854 | if (!unicode) |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1855 | return NULL; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 1856 | memcpy(PyUnicode_1BYTE_DATA(unicode), s, size); |
| 1857 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
| 1858 | return unicode; |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1859 | } |
| 1860 | |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 1861 | static Py_UCS4 |
| 1862 | kind_maxchar_limit(unsigned int kind) |
| 1863 | { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 1864 | switch (kind) { |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 1865 | case PyUnicode_1BYTE_KIND: |
| 1866 | return 0x80; |
| 1867 | case PyUnicode_2BYTE_KIND: |
| 1868 | return 0x100; |
| 1869 | case PyUnicode_4BYTE_KIND: |
| 1870 | return 0x10000; |
| 1871 | default: |
| 1872 | assert(0 && "invalid kind"); |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1873 | return MAX_UNICODE; |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 1874 | } |
| 1875 | } |
| 1876 | |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 1877 | Py_LOCAL_INLINE(Py_UCS4) |
| 1878 | align_maxchar(Py_UCS4 maxchar) |
| 1879 | { |
| 1880 | if (maxchar <= 127) |
| 1881 | return 127; |
| 1882 | else if (maxchar <= 255) |
| 1883 | return 255; |
| 1884 | else if (maxchar <= 65535) |
| 1885 | return 65535; |
| 1886 | else |
| 1887 | return MAX_UNICODE; |
| 1888 | } |
| 1889 | |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1890 | static PyObject* |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1891 | _PyUnicode_FromUCS1(const unsigned char* u, Py_ssize_t size) |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1892 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1893 | PyObject *res; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1894 | unsigned char max_char; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1895 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1896 | if (size == 0) { |
| 1897 | Py_INCREF(unicode_empty); |
| 1898 | return unicode_empty; |
| 1899 | } |
| 1900 | assert(size > 0); |
Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 1901 | if (size == 1) |
| 1902 | return get_latin1_char(u[0]); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1903 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1904 | max_char = ucs1lib_find_max_char(u, u + size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1905 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1906 | if (!res) |
| 1907 | return NULL; |
| 1908 | memcpy(PyUnicode_1BYTE_DATA(res), u, size); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1909 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1910 | return res; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1911 | } |
| 1912 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1913 | static PyObject* |
| 1914 | _PyUnicode_FromUCS2(const Py_UCS2 *u, Py_ssize_t size) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1915 | { |
| 1916 | PyObject *res; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1917 | Py_UCS2 max_char; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1918 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1919 | if (size == 0) { |
| 1920 | Py_INCREF(unicode_empty); |
| 1921 | return unicode_empty; |
| 1922 | } |
| 1923 | assert(size > 0); |
Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 1924 | if (size == 1) { |
| 1925 | Py_UCS4 ch = u[0]; |
| 1926 | if (ch < 256) |
| 1927 | return get_latin1_char((unsigned char)ch); |
| 1928 | |
| 1929 | res = PyUnicode_New(1, ch); |
| 1930 | if (res == NULL) |
| 1931 | return NULL; |
| 1932 | PyUnicode_WRITE(PyUnicode_KIND(res), PyUnicode_DATA(res), 0, ch); |
| 1933 | assert(_PyUnicode_CheckConsistency(res, 1)); |
| 1934 | return res; |
| 1935 | } |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1936 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1937 | max_char = ucs2lib_find_max_char(u, u + size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1938 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1939 | if (!res) |
| 1940 | return NULL; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1941 | if (max_char >= 256) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1942 | memcpy(PyUnicode_2BYTE_DATA(res), u, sizeof(Py_UCS2)*size); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1943 | else { |
| 1944 | _PyUnicode_CONVERT_BYTES( |
| 1945 | Py_UCS2, Py_UCS1, u, u + size, PyUnicode_1BYTE_DATA(res)); |
| 1946 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1947 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1948 | return res; |
| 1949 | } |
| 1950 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1951 | static PyObject* |
| 1952 | _PyUnicode_FromUCS4(const Py_UCS4 *u, Py_ssize_t size) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1953 | { |
| 1954 | PyObject *res; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1955 | Py_UCS4 max_char; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1956 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1957 | if (size == 0) { |
| 1958 | Py_INCREF(unicode_empty); |
| 1959 | return unicode_empty; |
| 1960 | } |
| 1961 | assert(size > 0); |
Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 1962 | if (size == 1) { |
| 1963 | Py_UCS4 ch = u[0]; |
| 1964 | if (ch < 256) |
| 1965 | return get_latin1_char((unsigned char)ch); |
| 1966 | |
| 1967 | res = PyUnicode_New(1, ch); |
| 1968 | if (res == NULL) |
| 1969 | return NULL; |
| 1970 | PyUnicode_WRITE(PyUnicode_KIND(res), PyUnicode_DATA(res), 0, ch); |
| 1971 | assert(_PyUnicode_CheckConsistency(res, 1)); |
| 1972 | return res; |
| 1973 | } |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1974 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1975 | max_char = ucs4lib_find_max_char(u, u + size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1976 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1977 | if (!res) |
| 1978 | return NULL; |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 1979 | if (max_char < 256) |
| 1980 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS1, u, u + size, |
| 1981 | PyUnicode_1BYTE_DATA(res)); |
| 1982 | else if (max_char < 0x10000) |
| 1983 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS2, u, u + size, |
| 1984 | PyUnicode_2BYTE_DATA(res)); |
| 1985 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1986 | memcpy(PyUnicode_4BYTE_DATA(res), u, sizeof(Py_UCS4)*size); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1987 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1988 | return res; |
| 1989 | } |
| 1990 | |
| 1991 | PyObject* |
| 1992 | PyUnicode_FromKindAndData(int kind, const void *buffer, Py_ssize_t size) |
| 1993 | { |
Victor Stinner | cfed46e | 2011-11-22 01:29:14 +0100 | [diff] [blame] | 1994 | if (size < 0) { |
| 1995 | PyErr_SetString(PyExc_ValueError, "size must be positive"); |
| 1996 | return NULL; |
| 1997 | } |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 1998 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1999 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 2000 | return _PyUnicode_FromUCS1(buffer, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2001 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 2002 | return _PyUnicode_FromUCS2(buffer, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2003 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 2004 | return _PyUnicode_FromUCS4(buffer, size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 2005 | default: |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 2006 | PyErr_SetString(PyExc_SystemError, "invalid kind"); |
| 2007 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2008 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2009 | } |
| 2010 | |
Victor Stinner | ece58de | 2012-04-23 23:36:38 +0200 | [diff] [blame] | 2011 | Py_UCS4 |
| 2012 | _PyUnicode_FindMaxChar(PyObject *unicode, Py_ssize_t start, Py_ssize_t end) |
| 2013 | { |
| 2014 | enum PyUnicode_Kind kind; |
| 2015 | void *startptr, *endptr; |
| 2016 | |
| 2017 | assert(PyUnicode_IS_READY(unicode)); |
| 2018 | assert(0 <= start); |
| 2019 | assert(end <= PyUnicode_GET_LENGTH(unicode)); |
| 2020 | assert(start <= end); |
| 2021 | |
| 2022 | if (start == 0 && end == PyUnicode_GET_LENGTH(unicode)) |
| 2023 | return PyUnicode_MAX_CHAR_VALUE(unicode); |
| 2024 | |
| 2025 | if (start == end) |
| 2026 | return 127; |
| 2027 | |
Victor Stinner | 94d558b | 2012-04-27 22:26:58 +0200 | [diff] [blame] | 2028 | if (PyUnicode_IS_ASCII(unicode)) |
| 2029 | return 127; |
| 2030 | |
Victor Stinner | ece58de | 2012-04-23 23:36:38 +0200 | [diff] [blame] | 2031 | kind = PyUnicode_KIND(unicode); |
Benjamin Peterson | f3b7d86 | 2012-04-23 18:07:01 -0400 | [diff] [blame] | 2032 | startptr = PyUnicode_DATA(unicode); |
Benjamin Peterson | b9f4c9d | 2012-04-23 21:45:40 -0400 | [diff] [blame] | 2033 | endptr = (char *)startptr + end * kind; |
| 2034 | startptr = (char *)startptr + start * kind; |
Benjamin Peterson | 2844a7a | 2012-04-23 18:00:25 -0400 | [diff] [blame] | 2035 | switch(kind) { |
| 2036 | case PyUnicode_1BYTE_KIND: |
| 2037 | return ucs1lib_find_max_char(startptr, endptr); |
| 2038 | case PyUnicode_2BYTE_KIND: |
| 2039 | return ucs2lib_find_max_char(startptr, endptr); |
| 2040 | case PyUnicode_4BYTE_KIND: |
| 2041 | return ucs4lib_find_max_char(startptr, endptr); |
Victor Stinner | ece58de | 2012-04-23 23:36:38 +0200 | [diff] [blame] | 2042 | default: |
Benjamin Peterson | 2844a7a | 2012-04-23 18:00:25 -0400 | [diff] [blame] | 2043 | assert(0); |
| 2044 | return 0; |
Victor Stinner | ece58de | 2012-04-23 23:36:38 +0200 | [diff] [blame] | 2045 | } |
| 2046 | } |
| 2047 | |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2048 | /* Ensure that a string uses the most efficient storage, if it is not the |
| 2049 | case: create a new string with of the right kind. Write NULL into *p_unicode |
| 2050 | on error. */ |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 2051 | static void |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2052 | unicode_adjust_maxchar(PyObject **p_unicode) |
| 2053 | { |
| 2054 | PyObject *unicode, *copy; |
| 2055 | Py_UCS4 max_char; |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2056 | Py_ssize_t len; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2057 | unsigned int kind; |
| 2058 | |
| 2059 | assert(p_unicode != NULL); |
| 2060 | unicode = *p_unicode; |
| 2061 | assert(PyUnicode_IS_READY(unicode)); |
| 2062 | if (PyUnicode_IS_ASCII(unicode)) |
| 2063 | return; |
| 2064 | |
| 2065 | len = PyUnicode_GET_LENGTH(unicode); |
| 2066 | kind = PyUnicode_KIND(unicode); |
| 2067 | if (kind == PyUnicode_1BYTE_KIND) { |
| 2068 | const Py_UCS1 *u = PyUnicode_1BYTE_DATA(unicode); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2069 | max_char = ucs1lib_find_max_char(u, u + len); |
| 2070 | if (max_char >= 128) |
| 2071 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2072 | } |
| 2073 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 2074 | const Py_UCS2 *u = PyUnicode_2BYTE_DATA(unicode); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2075 | max_char = ucs2lib_find_max_char(u, u + len); |
| 2076 | if (max_char >= 256) |
| 2077 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2078 | } |
| 2079 | else { |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2080 | const Py_UCS4 *u = PyUnicode_4BYTE_DATA(unicode); |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2081 | assert(kind == PyUnicode_4BYTE_KIND); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2082 | max_char = ucs4lib_find_max_char(u, u + len); |
| 2083 | if (max_char >= 0x10000) |
| 2084 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2085 | } |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2086 | copy = PyUnicode_New(len, max_char); |
| 2087 | copy_characters(copy, 0, unicode, 0, len); |
| 2088 | Py_DECREF(unicode); |
| 2089 | *p_unicode = copy; |
| 2090 | } |
| 2091 | |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2092 | PyObject* |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 2093 | _PyUnicode_Copy(PyObject *unicode) |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2094 | { |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 2095 | Py_ssize_t length; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2096 | PyObject *copy; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2097 | |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2098 | if (!PyUnicode_Check(unicode)) { |
| 2099 | PyErr_BadInternalCall(); |
| 2100 | return NULL; |
| 2101 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 2102 | if (PyUnicode_READY(unicode) == -1) |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2103 | return NULL; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2104 | |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 2105 | length = PyUnicode_GET_LENGTH(unicode); |
| 2106 | copy = PyUnicode_New(length, PyUnicode_MAX_CHAR_VALUE(unicode)); |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2107 | if (!copy) |
| 2108 | return NULL; |
| 2109 | assert(PyUnicode_KIND(copy) == PyUnicode_KIND(unicode)); |
| 2110 | |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 2111 | Py_MEMCPY(PyUnicode_DATA(copy), PyUnicode_DATA(unicode), |
| 2112 | length * PyUnicode_KIND(unicode)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2113 | assert(_PyUnicode_CheckConsistency(copy, 1)); |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2114 | return copy; |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2115 | } |
| 2116 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2117 | |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2118 | /* Widen Unicode objects to larger buffers. Don't write terminating null |
| 2119 | character. Return NULL on error. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2120 | |
| 2121 | void* |
| 2122 | _PyUnicode_AsKind(PyObject *s, unsigned int kind) |
| 2123 | { |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2124 | Py_ssize_t len; |
| 2125 | void *result; |
| 2126 | unsigned int skind; |
| 2127 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 2128 | if (PyUnicode_READY(s) == -1) |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2129 | return NULL; |
| 2130 | |
| 2131 | len = PyUnicode_GET_LENGTH(s); |
| 2132 | skind = PyUnicode_KIND(s); |
| 2133 | if (skind >= kind) { |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 2134 | PyErr_SetString(PyExc_SystemError, "invalid widening attempt"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2135 | return NULL; |
| 2136 | } |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 2137 | switch (kind) { |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2138 | case PyUnicode_2BYTE_KIND: |
| 2139 | result = PyMem_Malloc(len * sizeof(Py_UCS2)); |
| 2140 | if (!result) |
| 2141 | return PyErr_NoMemory(); |
| 2142 | assert(skind == PyUnicode_1BYTE_KIND); |
| 2143 | _PyUnicode_CONVERT_BYTES( |
| 2144 | Py_UCS1, Py_UCS2, |
| 2145 | PyUnicode_1BYTE_DATA(s), |
| 2146 | PyUnicode_1BYTE_DATA(s) + len, |
| 2147 | result); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2148 | return result; |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2149 | case PyUnicode_4BYTE_KIND: |
| 2150 | result = PyMem_Malloc(len * sizeof(Py_UCS4)); |
| 2151 | if (!result) |
| 2152 | return PyErr_NoMemory(); |
| 2153 | if (skind == PyUnicode_2BYTE_KIND) { |
| 2154 | _PyUnicode_CONVERT_BYTES( |
| 2155 | Py_UCS2, Py_UCS4, |
| 2156 | PyUnicode_2BYTE_DATA(s), |
| 2157 | PyUnicode_2BYTE_DATA(s) + len, |
| 2158 | result); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2159 | } |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2160 | else { |
| 2161 | assert(skind == PyUnicode_1BYTE_KIND); |
| 2162 | _PyUnicode_CONVERT_BYTES( |
| 2163 | Py_UCS1, Py_UCS4, |
| 2164 | PyUnicode_1BYTE_DATA(s), |
| 2165 | PyUnicode_1BYTE_DATA(s) + len, |
| 2166 | result); |
| 2167 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2168 | return result; |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2169 | default: |
| 2170 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2171 | } |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 2172 | PyErr_SetString(PyExc_SystemError, "invalid kind"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2173 | return NULL; |
| 2174 | } |
| 2175 | |
| 2176 | static Py_UCS4* |
| 2177 | as_ucs4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, |
| 2178 | int copy_null) |
| 2179 | { |
| 2180 | int kind; |
| 2181 | void *data; |
| 2182 | Py_ssize_t len, targetlen; |
| 2183 | if (PyUnicode_READY(string) == -1) |
| 2184 | return NULL; |
| 2185 | kind = PyUnicode_KIND(string); |
| 2186 | data = PyUnicode_DATA(string); |
| 2187 | len = PyUnicode_GET_LENGTH(string); |
| 2188 | targetlen = len; |
| 2189 | if (copy_null) |
| 2190 | targetlen++; |
| 2191 | if (!target) { |
| 2192 | if (PY_SSIZE_T_MAX / sizeof(Py_UCS4) < targetlen) { |
| 2193 | PyErr_NoMemory(); |
| 2194 | return NULL; |
| 2195 | } |
| 2196 | target = PyMem_Malloc(targetlen * sizeof(Py_UCS4)); |
| 2197 | if (!target) { |
| 2198 | PyErr_NoMemory(); |
| 2199 | return NULL; |
| 2200 | } |
| 2201 | } |
| 2202 | else { |
| 2203 | if (targetsize < targetlen) { |
| 2204 | PyErr_Format(PyExc_SystemError, |
| 2205 | "string is longer than the buffer"); |
| 2206 | if (copy_null && 0 < targetsize) |
| 2207 | target[0] = 0; |
| 2208 | return NULL; |
| 2209 | } |
| 2210 | } |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2211 | if (kind == PyUnicode_1BYTE_KIND) { |
| 2212 | Py_UCS1 *start = (Py_UCS1 *) data; |
| 2213 | _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] | 2214 | } |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2215 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 2216 | Py_UCS2 *start = (Py_UCS2 *) data; |
| 2217 | _PyUnicode_CONVERT_BYTES(Py_UCS2, Py_UCS4, start, start + len, target); |
| 2218 | } |
| 2219 | else { |
| 2220 | assert(kind == PyUnicode_4BYTE_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2221 | Py_MEMCPY(target, data, len * sizeof(Py_UCS4)); |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2222 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2223 | if (copy_null) |
| 2224 | target[len] = 0; |
| 2225 | return target; |
| 2226 | } |
| 2227 | |
| 2228 | Py_UCS4* |
| 2229 | PyUnicode_AsUCS4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, |
| 2230 | int copy_null) |
| 2231 | { |
Antoine Pitrou | de20b0b | 2011-11-10 21:47:38 +0100 | [diff] [blame] | 2232 | if (target == NULL || targetsize < 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2233 | PyErr_BadInternalCall(); |
| 2234 | return NULL; |
| 2235 | } |
| 2236 | return as_ucs4(string, target, targetsize, copy_null); |
| 2237 | } |
| 2238 | |
| 2239 | Py_UCS4* |
| 2240 | PyUnicode_AsUCS4Copy(PyObject *string) |
| 2241 | { |
| 2242 | return as_ucs4(string, NULL, 0, 1); |
| 2243 | } |
| 2244 | |
| 2245 | #ifdef HAVE_WCHAR_H |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 2246 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2247 | PyObject * |
| 2248 | PyUnicode_FromWideChar(register const wchar_t *w, Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2249 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2250 | if (w == NULL) { |
Victor Stinner | 382955f | 2011-12-11 21:44:00 +0100 | [diff] [blame] | 2251 | if (size == 0) { |
| 2252 | Py_INCREF(unicode_empty); |
| 2253 | return unicode_empty; |
| 2254 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2255 | PyErr_BadInternalCall(); |
| 2256 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2257 | } |
| 2258 | |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 2259 | if (size == -1) { |
| 2260 | size = wcslen(w); |
| 2261 | } |
| 2262 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2263 | return PyUnicode_FromUnicode(w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2264 | } |
| 2265 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2266 | #endif /* HAVE_WCHAR_H */ |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 2267 | |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2268 | static void |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2269 | makefmt(char *fmt, int longflag, int longlongflag, int size_tflag, |
| 2270 | int zeropad, int width, int precision, char c) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2271 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2272 | *fmt++ = '%'; |
| 2273 | if (width) { |
| 2274 | if (zeropad) |
| 2275 | *fmt++ = '0'; |
| 2276 | fmt += sprintf(fmt, "%d", width); |
| 2277 | } |
| 2278 | if (precision) |
| 2279 | fmt += sprintf(fmt, ".%d", precision); |
| 2280 | if (longflag) |
| 2281 | *fmt++ = 'l'; |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2282 | else if (longlongflag) { |
| 2283 | /* longlongflag should only ever be nonzero on machines with |
| 2284 | HAVE_LONG_LONG defined */ |
| 2285 | #ifdef HAVE_LONG_LONG |
| 2286 | char *f = PY_FORMAT_LONG_LONG; |
| 2287 | while (*f) |
| 2288 | *fmt++ = *f++; |
| 2289 | #else |
| 2290 | /* we shouldn't ever get here */ |
| 2291 | assert(0); |
| 2292 | *fmt++ = 'l'; |
| 2293 | #endif |
| 2294 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2295 | else if (size_tflag) { |
| 2296 | char *f = PY_FORMAT_SIZE_T; |
| 2297 | while (*f) |
| 2298 | *fmt++ = *f++; |
| 2299 | } |
| 2300 | *fmt++ = c; |
| 2301 | *fmt = '\0'; |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2302 | } |
| 2303 | |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2304 | /* helper for PyUnicode_FromFormatV() */ |
| 2305 | |
| 2306 | static const char* |
| 2307 | parse_format_flags(const char *f, |
| 2308 | int *p_width, int *p_precision, |
| 2309 | int *p_longflag, int *p_longlongflag, int *p_size_tflag) |
| 2310 | { |
| 2311 | int width, precision, longflag, longlongflag, size_tflag; |
| 2312 | |
| 2313 | /* parse the width.precision part, e.g. "%2.5s" => width=2, precision=5 */ |
| 2314 | f++; |
| 2315 | width = 0; |
| 2316 | while (Py_ISDIGIT((unsigned)*f)) |
| 2317 | width = (width*10) + *f++ - '0'; |
| 2318 | precision = 0; |
| 2319 | if (*f == '.') { |
| 2320 | f++; |
| 2321 | while (Py_ISDIGIT((unsigned)*f)) |
| 2322 | precision = (precision*10) + *f++ - '0'; |
| 2323 | if (*f == '%') { |
| 2324 | /* "%.3%s" => f points to "3" */ |
| 2325 | f--; |
| 2326 | } |
| 2327 | } |
| 2328 | if (*f == '\0') { |
| 2329 | /* bogus format "%.1" => go backward, f points to "1" */ |
| 2330 | f--; |
| 2331 | } |
| 2332 | if (p_width != NULL) |
| 2333 | *p_width = width; |
| 2334 | if (p_precision != NULL) |
| 2335 | *p_precision = precision; |
| 2336 | |
| 2337 | /* Handle %ld, %lu, %lld and %llu. */ |
| 2338 | longflag = 0; |
| 2339 | longlongflag = 0; |
Victor Stinner | e7faec1 | 2011-03-02 00:01:53 +0000 | [diff] [blame] | 2340 | size_tflag = 0; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2341 | |
| 2342 | if (*f == 'l') { |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2343 | if (f[1] == 'd' || f[1] == 'u' || f[1] == 'i') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2344 | longflag = 1; |
| 2345 | ++f; |
| 2346 | } |
| 2347 | #ifdef HAVE_LONG_LONG |
| 2348 | else if (f[1] == 'l' && |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2349 | (f[2] == 'd' || f[2] == 'u' || f[2] == 'i')) { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2350 | longlongflag = 1; |
| 2351 | f += 2; |
| 2352 | } |
| 2353 | #endif |
| 2354 | } |
| 2355 | /* handle the size_t flag. */ |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2356 | 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] | 2357 | size_tflag = 1; |
| 2358 | ++f; |
| 2359 | } |
| 2360 | if (p_longflag != NULL) |
| 2361 | *p_longflag = longflag; |
| 2362 | if (p_longlongflag != NULL) |
| 2363 | *p_longlongflag = longlongflag; |
| 2364 | if (p_size_tflag != NULL) |
| 2365 | *p_size_tflag = size_tflag; |
| 2366 | return f; |
| 2367 | } |
| 2368 | |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2369 | /* maximum number of characters required for output of %ld. 21 characters |
| 2370 | allows for 64-bit integers (in decimal) and an optional sign. */ |
| 2371 | #define MAX_LONG_CHARS 21 |
| 2372 | /* maximum number of characters required for output of %lld. |
| 2373 | We need at most ceil(log10(256)*SIZEOF_LONG_LONG) digits, |
| 2374 | plus 1 for the sign. 53/22 is an upper bound for log10(256). */ |
| 2375 | #define MAX_LONG_LONG_CHARS (2 + (SIZEOF_LONG_LONG*53-1) / 22) |
| 2376 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2377 | PyObject * |
| 2378 | PyUnicode_FromFormatV(const char *format, va_list vargs) |
| 2379 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2380 | va_list count; |
| 2381 | Py_ssize_t callcount = 0; |
| 2382 | PyObject **callresults = NULL; |
| 2383 | PyObject **callresult = NULL; |
| 2384 | Py_ssize_t n = 0; |
| 2385 | int width = 0; |
| 2386 | int precision = 0; |
| 2387 | int zeropad; |
| 2388 | const char* f; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2389 | PyObject *string; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2390 | /* used by sprintf */ |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2391 | char fmt[61]; /* should be enough for %0width.precisionlld */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2392 | Py_UCS4 maxchar = 127; /* result is ASCII by default */ |
| 2393 | Py_UCS4 argmaxchar; |
| 2394 | Py_ssize_t numbersize = 0; |
| 2395 | char *numberresults = NULL; |
| 2396 | char *numberresult = NULL; |
| 2397 | Py_ssize_t i; |
| 2398 | int kind; |
| 2399 | void *data; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2400 | |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 2401 | Py_VA_COPY(count, vargs); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2402 | /* step 1: count the number of %S/%R/%A/%s format specifications |
| 2403 | * (we call PyObject_Str()/PyObject_Repr()/PyObject_ASCII()/ |
| 2404 | * 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] | 2405 | * result in an array) |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 2406 | * also estimate a upper bound for all the number formats in the string, |
| 2407 | * 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] | 2408 | * buffer before putting everything together. */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2409 | for (f = format; *f; f++) { |
| 2410 | if (*f == '%') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2411 | int longlongflag; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2412 | /* skip width or width.precision (eg. "1.2" of "%1.2f") */ |
| 2413 | f = parse_format_flags(f, &width, NULL, NULL, &longlongflag, NULL); |
| 2414 | if (*f == 's' || *f=='S' || *f=='R' || *f=='A' || *f=='V') |
| 2415 | ++callcount; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2416 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2417 | else if (*f == 'd' || *f=='u' || *f=='i' || *f=='x' || *f=='p') { |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2418 | #ifdef HAVE_LONG_LONG |
| 2419 | if (longlongflag) { |
| 2420 | if (width < MAX_LONG_LONG_CHARS) |
| 2421 | width = MAX_LONG_LONG_CHARS; |
| 2422 | } |
| 2423 | else |
| 2424 | #endif |
| 2425 | /* MAX_LONG_CHARS is enough to hold a 64-bit integer, |
| 2426 | including sign. Decimal takes the most space. This |
| 2427 | isn't enough for octal. If a width is specified we |
| 2428 | need more (which we allocate later). */ |
| 2429 | if (width < MAX_LONG_CHARS) |
| 2430 | width = MAX_LONG_CHARS; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2431 | |
| 2432 | /* account for the size + '\0' to separate numbers |
| 2433 | inside of the numberresults buffer */ |
| 2434 | numbersize += (width + 1); |
| 2435 | } |
| 2436 | } |
| 2437 | else if ((unsigned char)*f > 127) { |
| 2438 | PyErr_Format(PyExc_ValueError, |
| 2439 | "PyUnicode_FromFormatV() expects an ASCII-encoded format " |
| 2440 | "string, got a non-ASCII byte: 0x%02x", |
| 2441 | (unsigned char)*f); |
| 2442 | return NULL; |
| 2443 | } |
| 2444 | } |
| 2445 | /* step 2: allocate memory for the results of |
| 2446 | * PyObject_Str()/PyObject_Repr()/PyUnicode_DecodeUTF8() calls */ |
| 2447 | if (callcount) { |
| 2448 | callresults = PyObject_Malloc(sizeof(PyObject *) * callcount); |
| 2449 | if (!callresults) { |
| 2450 | PyErr_NoMemory(); |
| 2451 | return NULL; |
| 2452 | } |
| 2453 | callresult = callresults; |
| 2454 | } |
| 2455 | /* step 2.5: allocate memory for the results of formating numbers */ |
| 2456 | if (numbersize) { |
| 2457 | numberresults = PyObject_Malloc(numbersize); |
| 2458 | if (!numberresults) { |
| 2459 | PyErr_NoMemory(); |
| 2460 | goto fail; |
| 2461 | } |
| 2462 | numberresult = numberresults; |
| 2463 | } |
| 2464 | |
| 2465 | /* step 3: format numbers and figure out how large a buffer we need */ |
| 2466 | for (f = format; *f; f++) { |
| 2467 | if (*f == '%') { |
| 2468 | const char* p; |
| 2469 | int longflag; |
| 2470 | int longlongflag; |
| 2471 | int size_tflag; |
| 2472 | int numprinted; |
| 2473 | |
| 2474 | p = f; |
| 2475 | zeropad = (f[1] == '0'); |
| 2476 | f = parse_format_flags(f, &width, &precision, |
| 2477 | &longflag, &longlongflag, &size_tflag); |
| 2478 | switch (*f) { |
| 2479 | case 'c': |
| 2480 | { |
| 2481 | Py_UCS4 ordinal = va_arg(count, int); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 2482 | maxchar = MAX_MAXCHAR(maxchar, ordinal); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2483 | n++; |
| 2484 | break; |
| 2485 | } |
| 2486 | case '%': |
| 2487 | n++; |
| 2488 | break; |
| 2489 | case 'i': |
| 2490 | case 'd': |
| 2491 | makefmt(fmt, longflag, longlongflag, size_tflag, zeropad, |
| 2492 | width, precision, *f); |
| 2493 | if (longflag) |
| 2494 | numprinted = sprintf(numberresult, fmt, |
| 2495 | va_arg(count, long)); |
| 2496 | #ifdef HAVE_LONG_LONG |
| 2497 | else if (longlongflag) |
| 2498 | numprinted = sprintf(numberresult, fmt, |
| 2499 | va_arg(count, PY_LONG_LONG)); |
| 2500 | #endif |
| 2501 | else if (size_tflag) |
| 2502 | numprinted = sprintf(numberresult, fmt, |
| 2503 | va_arg(count, Py_ssize_t)); |
| 2504 | else |
| 2505 | numprinted = sprintf(numberresult, fmt, |
| 2506 | va_arg(count, int)); |
| 2507 | n += numprinted; |
| 2508 | /* advance by +1 to skip over the '\0' */ |
| 2509 | numberresult += (numprinted + 1); |
| 2510 | assert(*(numberresult - 1) == '\0'); |
| 2511 | assert(*(numberresult - 2) != '\0'); |
| 2512 | assert(numprinted >= 0); |
| 2513 | assert(numberresult <= numberresults + numbersize); |
| 2514 | break; |
| 2515 | case 'u': |
| 2516 | makefmt(fmt, longflag, longlongflag, size_tflag, zeropad, |
| 2517 | width, precision, 'u'); |
| 2518 | if (longflag) |
| 2519 | numprinted = sprintf(numberresult, fmt, |
| 2520 | va_arg(count, unsigned long)); |
| 2521 | #ifdef HAVE_LONG_LONG |
| 2522 | else if (longlongflag) |
| 2523 | numprinted = sprintf(numberresult, fmt, |
| 2524 | va_arg(count, unsigned PY_LONG_LONG)); |
| 2525 | #endif |
| 2526 | else if (size_tflag) |
| 2527 | numprinted = sprintf(numberresult, fmt, |
| 2528 | va_arg(count, size_t)); |
| 2529 | else |
| 2530 | numprinted = sprintf(numberresult, fmt, |
| 2531 | va_arg(count, unsigned int)); |
| 2532 | n += numprinted; |
| 2533 | numberresult += (numprinted + 1); |
| 2534 | assert(*(numberresult - 1) == '\0'); |
| 2535 | assert(*(numberresult - 2) != '\0'); |
| 2536 | assert(numprinted >= 0); |
| 2537 | assert(numberresult <= numberresults + numbersize); |
| 2538 | break; |
| 2539 | case 'x': |
| 2540 | makefmt(fmt, 0, 0, 0, zeropad, width, precision, 'x'); |
| 2541 | numprinted = sprintf(numberresult, fmt, va_arg(count, int)); |
| 2542 | n += numprinted; |
| 2543 | numberresult += (numprinted + 1); |
| 2544 | assert(*(numberresult - 1) == '\0'); |
| 2545 | assert(*(numberresult - 2) != '\0'); |
| 2546 | assert(numprinted >= 0); |
| 2547 | assert(numberresult <= numberresults + numbersize); |
| 2548 | break; |
| 2549 | case 'p': |
| 2550 | numprinted = sprintf(numberresult, "%p", va_arg(count, void*)); |
| 2551 | /* %p is ill-defined: ensure leading 0x. */ |
| 2552 | if (numberresult[1] == 'X') |
| 2553 | numberresult[1] = 'x'; |
| 2554 | else if (numberresult[1] != 'x') { |
| 2555 | memmove(numberresult + 2, numberresult, |
| 2556 | strlen(numberresult) + 1); |
| 2557 | numberresult[0] = '0'; |
| 2558 | numberresult[1] = 'x'; |
| 2559 | numprinted += 2; |
| 2560 | } |
| 2561 | n += numprinted; |
| 2562 | numberresult += (numprinted + 1); |
| 2563 | assert(*(numberresult - 1) == '\0'); |
| 2564 | assert(*(numberresult - 2) != '\0'); |
| 2565 | assert(numprinted >= 0); |
| 2566 | assert(numberresult <= numberresults + numbersize); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2567 | break; |
| 2568 | case 's': |
| 2569 | { |
| 2570 | /* UTF-8 */ |
Georg Brandl | 780b2a6 | 2009-05-05 09:19:59 +0000 | [diff] [blame] | 2571 | const char *s = va_arg(count, const char*); |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 2572 | PyObject *str = PyUnicode_DecodeUTF8Stateful(s, strlen(s), "replace", NULL); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2573 | if (!str) |
| 2574 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2575 | /* since PyUnicode_DecodeUTF8 returns already flexible |
| 2576 | unicode objects, there is no need to call ready on them */ |
| 2577 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 2578 | maxchar = MAX_MAXCHAR(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2579 | n += PyUnicode_GET_LENGTH(str); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2580 | /* Remember the str and switch to the next slot */ |
| 2581 | *callresult++ = str; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2582 | break; |
| 2583 | } |
| 2584 | case 'U': |
| 2585 | { |
| 2586 | PyObject *obj = va_arg(count, PyObject *); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 2587 | assert(obj && _PyUnicode_CHECK(obj)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2588 | if (PyUnicode_READY(obj) == -1) |
| 2589 | goto fail; |
| 2590 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(obj); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 2591 | maxchar = MAX_MAXCHAR(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2592 | n += PyUnicode_GET_LENGTH(obj); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2593 | break; |
| 2594 | } |
| 2595 | case 'V': |
| 2596 | { |
| 2597 | PyObject *obj = va_arg(count, PyObject *); |
| 2598 | const char *str = va_arg(count, const char *); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2599 | PyObject *str_obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2600 | assert(obj || str); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 2601 | assert(!obj || _PyUnicode_CHECK(obj)); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2602 | if (obj) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2603 | if (PyUnicode_READY(obj) == -1) |
| 2604 | goto fail; |
| 2605 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(obj); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 2606 | maxchar = MAX_MAXCHAR(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2607 | n += PyUnicode_GET_LENGTH(obj); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2608 | *callresult++ = NULL; |
| 2609 | } |
| 2610 | else { |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 2611 | str_obj = PyUnicode_DecodeUTF8Stateful(str, strlen(str), "replace", NULL); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2612 | if (!str_obj) |
| 2613 | goto fail; |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 2614 | if (PyUnicode_READY(str_obj) == -1) { |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 2615 | Py_DECREF(str_obj); |
| 2616 | goto fail; |
| 2617 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2618 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str_obj); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 2619 | maxchar = MAX_MAXCHAR(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2620 | n += PyUnicode_GET_LENGTH(str_obj); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2621 | *callresult++ = str_obj; |
| 2622 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2623 | break; |
| 2624 | } |
| 2625 | case 'S': |
| 2626 | { |
| 2627 | PyObject *obj = va_arg(count, PyObject *); |
| 2628 | PyObject *str; |
| 2629 | assert(obj); |
| 2630 | str = PyObject_Str(obj); |
Benjamin Peterson | c8d8b88 | 2012-01-14 13:37:31 -0500 | [diff] [blame] | 2631 | if (!str) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2632 | goto fail; |
Benjamin Peterson | c8d8b88 | 2012-01-14 13:37:31 -0500 | [diff] [blame] | 2633 | if (PyUnicode_READY(str) == -1) { |
| 2634 | Py_DECREF(str); |
| 2635 | goto fail; |
| 2636 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2637 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 2638 | maxchar = MAX_MAXCHAR(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2639 | n += PyUnicode_GET_LENGTH(str); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2640 | /* Remember the str and switch to the next slot */ |
| 2641 | *callresult++ = str; |
| 2642 | break; |
| 2643 | } |
| 2644 | case 'R': |
| 2645 | { |
| 2646 | PyObject *obj = va_arg(count, PyObject *); |
| 2647 | PyObject *repr; |
| 2648 | assert(obj); |
| 2649 | repr = PyObject_Repr(obj); |
Benjamin Peterson | c8d8b88 | 2012-01-14 13:37:31 -0500 | [diff] [blame] | 2650 | if (!repr) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2651 | goto fail; |
Benjamin Peterson | c8d8b88 | 2012-01-14 13:37:31 -0500 | [diff] [blame] | 2652 | if (PyUnicode_READY(repr) == -1) { |
| 2653 | Py_DECREF(repr); |
| 2654 | goto fail; |
| 2655 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2656 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(repr); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 2657 | maxchar = MAX_MAXCHAR(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2658 | n += PyUnicode_GET_LENGTH(repr); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2659 | /* Remember the repr and switch to the next slot */ |
| 2660 | *callresult++ = repr; |
| 2661 | break; |
| 2662 | } |
| 2663 | case 'A': |
| 2664 | { |
| 2665 | PyObject *obj = va_arg(count, PyObject *); |
| 2666 | PyObject *ascii; |
| 2667 | assert(obj); |
| 2668 | ascii = PyObject_ASCII(obj); |
Benjamin Peterson | c8d8b88 | 2012-01-14 13:37:31 -0500 | [diff] [blame] | 2669 | if (!ascii) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2670 | goto fail; |
Benjamin Peterson | c8d8b88 | 2012-01-14 13:37:31 -0500 | [diff] [blame] | 2671 | if (PyUnicode_READY(ascii) == -1) { |
| 2672 | Py_DECREF(ascii); |
| 2673 | goto fail; |
| 2674 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2675 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(ascii); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 2676 | maxchar = MAX_MAXCHAR(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2677 | n += PyUnicode_GET_LENGTH(ascii); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2678 | /* Remember the repr and switch to the next slot */ |
| 2679 | *callresult++ = ascii; |
| 2680 | break; |
| 2681 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2682 | default: |
| 2683 | /* if we stumble upon an unknown |
| 2684 | formatting code, copy the rest of |
| 2685 | the format string to the output |
| 2686 | string. (we cannot just skip the |
| 2687 | code, since there's no way to know |
| 2688 | what's in the argument list) */ |
| 2689 | n += strlen(p); |
| 2690 | goto expand; |
| 2691 | } |
| 2692 | } else |
| 2693 | n++; |
| 2694 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2695 | expand: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2696 | /* step 4: fill the buffer */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2697 | /* Since we've analyzed how much space we need, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2698 | we don't have to resize the string. |
| 2699 | There can be no errors beyond this point. */ |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2700 | string = PyUnicode_New(n, maxchar); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2701 | if (!string) |
| 2702 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2703 | kind = PyUnicode_KIND(string); |
| 2704 | data = PyUnicode_DATA(string); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2705 | callresult = callresults; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2706 | numberresult = numberresults; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2707 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2708 | for (i = 0, f = format; *f; f++) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2709 | if (*f == '%') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2710 | const char* p; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2711 | |
| 2712 | p = f; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2713 | f = parse_format_flags(f, NULL, NULL, NULL, NULL, NULL); |
| 2714 | /* checking for == because the last argument could be a empty |
| 2715 | string, which causes i to point to end, the assert at the end of |
| 2716 | the loop */ |
| 2717 | assert(i <= PyUnicode_GET_LENGTH(string)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2718 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2719 | switch (*f) { |
| 2720 | case 'c': |
Victor Stinner | 5ed8b2c | 2011-02-21 21:13:44 +0000 | [diff] [blame] | 2721 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2722 | const int ordinal = va_arg(vargs, int); |
| 2723 | PyUnicode_WRITE(kind, data, i++, ordinal); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2724 | break; |
Victor Stinner | 5ed8b2c | 2011-02-21 21:13:44 +0000 | [diff] [blame] | 2725 | } |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2726 | case 'i': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2727 | case 'd': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2728 | case 'u': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2729 | case 'x': |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2730 | case 'p': |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 2731 | { |
| 2732 | Py_ssize_t written; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2733 | /* unused, since we already have the result */ |
| 2734 | if (*f == 'p') |
| 2735 | (void) va_arg(vargs, void *); |
| 2736 | else |
| 2737 | (void) va_arg(vargs, int); |
| 2738 | /* extract the result from numberresults and append. */ |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 2739 | written = unicode_write_cstr(string, i, numberresult); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2740 | /* skip over the separating '\0' */ |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 2741 | i += written; |
| 2742 | numberresult += written; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2743 | assert(*numberresult == '\0'); |
| 2744 | numberresult++; |
| 2745 | assert(numberresult <= numberresults + numbersize); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2746 | break; |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 2747 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2748 | case 's': |
| 2749 | { |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2750 | /* unused, since we already have the result */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2751 | Py_ssize_t size; |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2752 | (void) va_arg(vargs, char *); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2753 | size = PyUnicode_GET_LENGTH(*callresult); |
| 2754 | assert(PyUnicode_KIND(*callresult) <= PyUnicode_KIND(string)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2755 | copy_characters(string, i, *callresult, 0, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2756 | i += size; |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2757 | /* We're done with the unicode()/repr() => forget it */ |
| 2758 | Py_DECREF(*callresult); |
| 2759 | /* switch to next unicode()/repr() result */ |
| 2760 | ++callresult; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2761 | break; |
| 2762 | } |
| 2763 | case 'U': |
| 2764 | { |
| 2765 | PyObject *obj = va_arg(vargs, PyObject *); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2766 | Py_ssize_t size; |
| 2767 | assert(PyUnicode_KIND(obj) <= PyUnicode_KIND(string)); |
| 2768 | size = PyUnicode_GET_LENGTH(obj); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2769 | copy_characters(string, i, obj, 0, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2770 | i += size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2771 | break; |
| 2772 | } |
| 2773 | case 'V': |
| 2774 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2775 | Py_ssize_t size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2776 | PyObject *obj = va_arg(vargs, PyObject *); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2777 | va_arg(vargs, const char *); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2778 | if (obj) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2779 | size = PyUnicode_GET_LENGTH(obj); |
| 2780 | assert(PyUnicode_KIND(obj) <= PyUnicode_KIND(string)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2781 | copy_characters(string, i, obj, 0, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2782 | i += size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2783 | } else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2784 | size = PyUnicode_GET_LENGTH(*callresult); |
| 2785 | assert(PyUnicode_KIND(*callresult) <= |
| 2786 | PyUnicode_KIND(string)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2787 | copy_characters(string, i, *callresult, 0, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2788 | i += size; |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2789 | Py_DECREF(*callresult); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2790 | } |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2791 | ++callresult; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2792 | break; |
| 2793 | } |
| 2794 | case 'S': |
| 2795 | case 'R': |
Victor Stinner | 9a90900 | 2010-10-18 20:59:24 +0000 | [diff] [blame] | 2796 | case 'A': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2797 | { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2798 | Py_ssize_t size = PyUnicode_GET_LENGTH(*callresult); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2799 | /* unused, since we already have the result */ |
| 2800 | (void) va_arg(vargs, PyObject *); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2801 | assert(PyUnicode_KIND(*callresult) <= PyUnicode_KIND(string)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2802 | copy_characters(string, i, *callresult, 0, size); |
| 2803 | i += size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2804 | /* We're done with the unicode()/repr() => forget it */ |
| 2805 | Py_DECREF(*callresult); |
| 2806 | /* switch to next unicode()/repr() result */ |
| 2807 | ++callresult; |
| 2808 | break; |
| 2809 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2810 | case '%': |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2811 | PyUnicode_WRITE(kind, data, i++, '%'); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2812 | break; |
| 2813 | default: |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 2814 | i += unicode_write_cstr(string, i, p); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2815 | assert(i == PyUnicode_GET_LENGTH(string)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2816 | goto end; |
| 2817 | } |
Victor Stinner | 1205f27 | 2010-09-11 00:54:47 +0000 | [diff] [blame] | 2818 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2819 | else { |
| 2820 | assert(i < PyUnicode_GET_LENGTH(string)); |
| 2821 | PyUnicode_WRITE(kind, data, i++, *f); |
| 2822 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2823 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2824 | assert(i == PyUnicode_GET_LENGTH(string)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2825 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2826 | end: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2827 | if (callresults) |
| 2828 | PyObject_Free(callresults); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2829 | if (numberresults) |
| 2830 | PyObject_Free(numberresults); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 2831 | return unicode_result(string); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2832 | fail: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2833 | if (callresults) { |
| 2834 | PyObject **callresult2 = callresults; |
| 2835 | while (callresult2 < callresult) { |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2836 | Py_XDECREF(*callresult2); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2837 | ++callresult2; |
| 2838 | } |
| 2839 | PyObject_Free(callresults); |
| 2840 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2841 | if (numberresults) |
| 2842 | PyObject_Free(numberresults); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2843 | return NULL; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2844 | } |
| 2845 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2846 | PyObject * |
| 2847 | PyUnicode_FromFormat(const char *format, ...) |
| 2848 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2849 | PyObject* ret; |
| 2850 | va_list vargs; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2851 | |
| 2852 | #ifdef HAVE_STDARG_PROTOTYPES |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2853 | va_start(vargs, format); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2854 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2855 | va_start(vargs); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2856 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2857 | ret = PyUnicode_FromFormatV(format, vargs); |
| 2858 | va_end(vargs); |
| 2859 | return ret; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2860 | } |
| 2861 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2862 | #ifdef HAVE_WCHAR_H |
| 2863 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2864 | /* Helper function for PyUnicode_AsWideChar() and PyUnicode_AsWideCharString(): |
| 2865 | convert a Unicode object to a wide character string. |
| 2866 | |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2867 | - 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] | 2868 | character) required to convert the unicode object. Ignore size argument. |
| 2869 | |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2870 | - Otherwise: return the number of wide characters (excluding the null |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2871 | character) written into w. Write at most size wide characters (including |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2872 | the null character). */ |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2873 | static Py_ssize_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2874 | unicode_aswidechar(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2875 | wchar_t *w, |
| 2876 | Py_ssize_t size) |
| 2877 | { |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2878 | Py_ssize_t res; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2879 | const wchar_t *wstr; |
| 2880 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2881 | wstr = PyUnicode_AsUnicodeAndSize(unicode, &res); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2882 | if (wstr == NULL) |
| 2883 | return -1; |
| 2884 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2885 | if (w != NULL) { |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2886 | if (size > res) |
| 2887 | size = res + 1; |
| 2888 | else |
| 2889 | res = size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2890 | Py_MEMCPY(w, wstr, size * sizeof(wchar_t)); |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2891 | return res; |
| 2892 | } |
| 2893 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2894 | return res + 1; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2895 | } |
| 2896 | |
| 2897 | Py_ssize_t |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2898 | PyUnicode_AsWideChar(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2899 | wchar_t *w, |
| 2900 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2901 | { |
| 2902 | if (unicode == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2903 | PyErr_BadInternalCall(); |
| 2904 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2905 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2906 | return unicode_aswidechar(unicode, w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2907 | } |
| 2908 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2909 | wchar_t* |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 2910 | PyUnicode_AsWideCharString(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2911 | Py_ssize_t *size) |
| 2912 | { |
| 2913 | wchar_t* buffer; |
| 2914 | Py_ssize_t buflen; |
| 2915 | |
| 2916 | if (unicode == NULL) { |
| 2917 | PyErr_BadInternalCall(); |
| 2918 | return NULL; |
| 2919 | } |
| 2920 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2921 | buflen = unicode_aswidechar(unicode, NULL, 0); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2922 | if (buflen == -1) |
| 2923 | return NULL; |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2924 | if (PY_SSIZE_T_MAX / sizeof(wchar_t) < buflen) { |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2925 | PyErr_NoMemory(); |
| 2926 | return NULL; |
| 2927 | } |
| 2928 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2929 | buffer = PyMem_MALLOC(buflen * sizeof(wchar_t)); |
| 2930 | if (buffer == NULL) { |
| 2931 | PyErr_NoMemory(); |
| 2932 | return NULL; |
| 2933 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2934 | buflen = unicode_aswidechar(unicode, buffer, buflen); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2935 | if (buflen == -1) |
| 2936 | return NULL; |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2937 | if (size != NULL) |
| 2938 | *size = buflen; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2939 | return buffer; |
| 2940 | } |
| 2941 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2942 | #endif /* HAVE_WCHAR_H */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2943 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2944 | PyObject * |
| 2945 | PyUnicode_FromOrdinal(int ordinal) |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2946 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2947 | PyObject *v; |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 2948 | if (ordinal < 0 || ordinal > MAX_UNICODE) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2949 | PyErr_SetString(PyExc_ValueError, |
| 2950 | "chr() arg not in range(0x110000)"); |
| 2951 | return NULL; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2952 | } |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 2953 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2954 | if (ordinal < 256) |
| 2955 | return get_latin1_char(ordinal); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2956 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2957 | v = PyUnicode_New(1, ordinal); |
| 2958 | if (v == NULL) |
| 2959 | return NULL; |
| 2960 | PyUnicode_WRITE(PyUnicode_KIND(v), PyUnicode_DATA(v), 0, ordinal); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2961 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2962 | return v; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2963 | } |
| 2964 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2965 | PyObject * |
| 2966 | PyUnicode_FromObject(register PyObject *obj) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2967 | { |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2968 | /* XXX Perhaps we should make this API an alias of |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2969 | PyObject_Str() instead ?! */ |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2970 | if (PyUnicode_CheckExact(obj)) { |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 2971 | if (PyUnicode_READY(obj) == -1) |
Victor Stinner | d3a83d5 | 2011-10-01 03:09:33 +0200 | [diff] [blame] | 2972 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2973 | Py_INCREF(obj); |
| 2974 | return obj; |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2975 | } |
| 2976 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2977 | /* For a Unicode subtype that's not a Unicode object, |
| 2978 | return a true Unicode object with the same data. */ |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 2979 | return _PyUnicode_Copy(obj); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2980 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2981 | PyErr_Format(PyExc_TypeError, |
| 2982 | "Can't convert '%.100s' object to str implicitly", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2983 | Py_TYPE(obj)->tp_name); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2984 | return NULL; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2985 | } |
| 2986 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2987 | PyObject * |
| 2988 | PyUnicode_FromEncodedObject(register PyObject *obj, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2989 | const char *encoding, |
| 2990 | const char *errors) |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2991 | { |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2992 | Py_buffer buffer; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2993 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2994 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2995 | if (obj == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2996 | PyErr_BadInternalCall(); |
| 2997 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2998 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2999 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 3000 | /* Decoding bytes objects is the most common case and should be fast */ |
| 3001 | if (PyBytes_Check(obj)) { |
| 3002 | if (PyBytes_GET_SIZE(obj) == 0) { |
| 3003 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 3004 | v = unicode_empty; |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 3005 | } |
| 3006 | else { |
| 3007 | v = PyUnicode_Decode( |
| 3008 | PyBytes_AS_STRING(obj), PyBytes_GET_SIZE(obj), |
| 3009 | encoding, errors); |
| 3010 | } |
| 3011 | return v; |
| 3012 | } |
| 3013 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 3014 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3015 | PyErr_SetString(PyExc_TypeError, |
| 3016 | "decoding str is not supported"); |
| 3017 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 3018 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 3019 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 3020 | /* Retrieve a bytes buffer view through the PEP 3118 buffer interface */ |
| 3021 | if (PyObject_GetBuffer(obj, &buffer, PyBUF_SIMPLE) < 0) { |
| 3022 | PyErr_Format(PyExc_TypeError, |
| 3023 | "coercing to str: need bytes, bytearray " |
| 3024 | "or buffer-like object, %.80s found", |
| 3025 | Py_TYPE(obj)->tp_name); |
| 3026 | return NULL; |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 3027 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3028 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 3029 | if (buffer.len == 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3030 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 3031 | v = unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3032 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3033 | else |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 3034 | v = PyUnicode_Decode((char*) buffer.buf, buffer.len, encoding, errors); |
Marc-André Lemburg | ad7c98e | 2001-01-17 17:09:53 +0000 | [diff] [blame] | 3035 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 3036 | PyBuffer_Release(&buffer); |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 3037 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3038 | } |
| 3039 | |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3040 | /* Convert encoding to lower case and replace '_' with '-' in order to |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3041 | catch e.g. UTF_8. Return 0 on error (encoding is longer than lower_len-1), |
| 3042 | 1 on success. */ |
| 3043 | static int |
| 3044 | normalize_encoding(const char *encoding, |
| 3045 | char *lower, |
| 3046 | size_t lower_len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3047 | { |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 3048 | const char *e; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3049 | char *l; |
| 3050 | char *l_end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3051 | |
Benjamin Peterson | 7a6debe | 2011-10-15 09:25:28 -0400 | [diff] [blame] | 3052 | if (encoding == NULL) { |
| 3053 | strcpy(lower, "utf-8"); |
| 3054 | return 1; |
| 3055 | } |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 3056 | e = encoding; |
| 3057 | l = lower; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3058 | l_end = &lower[lower_len - 1]; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3059 | while (*e) { |
| 3060 | if (l == l_end) |
| 3061 | return 0; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 3062 | if (Py_ISUPPER(*e)) { |
| 3063 | *l++ = Py_TOLOWER(*e++); |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 3064 | } |
| 3065 | else if (*e == '_') { |
| 3066 | *l++ = '-'; |
| 3067 | e++; |
| 3068 | } |
| 3069 | else { |
| 3070 | *l++ = *e++; |
| 3071 | } |
| 3072 | } |
| 3073 | *l = '\0'; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3074 | return 1; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3075 | } |
| 3076 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3077 | PyObject * |
| 3078 | PyUnicode_Decode(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3079 | Py_ssize_t size, |
| 3080 | const char *encoding, |
| 3081 | const char *errors) |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3082 | { |
| 3083 | PyObject *buffer = NULL, *unicode; |
| 3084 | Py_buffer info; |
| 3085 | char lower[11]; /* Enough for any encoding shortcut */ |
| 3086 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3087 | /* Shortcuts for common default encodings */ |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3088 | if (normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3089 | if ((strcmp(lower, "utf-8") == 0) || |
| 3090 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 3091 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3092 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3093 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3094 | (strcmp(lower, "iso-8859-1") == 0)) |
| 3095 | return PyUnicode_DecodeLatin1(s, size, errors); |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3096 | #ifdef HAVE_MBCS |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3097 | else if (strcmp(lower, "mbcs") == 0) |
| 3098 | return PyUnicode_DecodeMBCS(s, size, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 3099 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3100 | else if (strcmp(lower, "ascii") == 0) |
| 3101 | return PyUnicode_DecodeASCII(s, size, errors); |
| 3102 | else if (strcmp(lower, "utf-16") == 0) |
| 3103 | return PyUnicode_DecodeUTF16(s, size, errors, 0); |
| 3104 | else if (strcmp(lower, "utf-32") == 0) |
| 3105 | return PyUnicode_DecodeUTF32(s, size, errors, 0); |
| 3106 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3107 | |
| 3108 | /* Decode via the codec registry */ |
Guido van Rossum | be801ac | 2007-10-08 03:32:34 +0000 | [diff] [blame] | 3109 | buffer = NULL; |
Antoine Pitrou | c3b3924 | 2009-01-03 16:59:18 +0000 | [diff] [blame] | 3110 | 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] | 3111 | goto onError; |
Antoine Pitrou | ee58fa4 | 2008-08-19 18:22:14 +0000 | [diff] [blame] | 3112 | buffer = PyMemoryView_FromBuffer(&info); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3113 | if (buffer == NULL) |
| 3114 | goto onError; |
| 3115 | unicode = PyCodec_Decode(buffer, encoding, errors); |
| 3116 | if (unicode == NULL) |
| 3117 | goto onError; |
| 3118 | if (!PyUnicode_Check(unicode)) { |
| 3119 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3120 | "decoder did not return a str object (type=%.400s)", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 3121 | Py_TYPE(unicode)->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3122 | Py_DECREF(unicode); |
| 3123 | goto onError; |
| 3124 | } |
| 3125 | Py_DECREF(buffer); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 3126 | return unicode_result(unicode); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3127 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3128 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3129 | Py_XDECREF(buffer); |
| 3130 | return NULL; |
| 3131 | } |
| 3132 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3133 | PyObject * |
| 3134 | PyUnicode_AsDecodedObject(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3135 | const char *encoding, |
| 3136 | const char *errors) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3137 | { |
| 3138 | PyObject *v; |
| 3139 | |
| 3140 | if (!PyUnicode_Check(unicode)) { |
| 3141 | PyErr_BadArgument(); |
| 3142 | goto onError; |
| 3143 | } |
| 3144 | |
| 3145 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3146 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3147 | |
| 3148 | /* Decode via the codec registry */ |
| 3149 | v = PyCodec_Decode(unicode, encoding, errors); |
| 3150 | if (v == NULL) |
| 3151 | goto onError; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 3152 | return unicode_result(v); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3153 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3154 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3155 | return NULL; |
| 3156 | } |
| 3157 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3158 | PyObject * |
| 3159 | PyUnicode_AsDecodedUnicode(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3160 | const char *encoding, |
| 3161 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3162 | { |
| 3163 | PyObject *v; |
| 3164 | |
| 3165 | if (!PyUnicode_Check(unicode)) { |
| 3166 | PyErr_BadArgument(); |
| 3167 | goto onError; |
| 3168 | } |
| 3169 | |
| 3170 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3171 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3172 | |
| 3173 | /* Decode via the codec registry */ |
| 3174 | v = PyCodec_Decode(unicode, encoding, errors); |
| 3175 | if (v == NULL) |
| 3176 | goto onError; |
| 3177 | if (!PyUnicode_Check(v)) { |
| 3178 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3179 | "decoder did not return a str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3180 | Py_TYPE(v)->tp_name); |
| 3181 | Py_DECREF(v); |
| 3182 | goto onError; |
| 3183 | } |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 3184 | return unicode_result(v); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3185 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3186 | onError: |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3187 | return NULL; |
| 3188 | } |
| 3189 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3190 | PyObject * |
| 3191 | PyUnicode_Encode(const Py_UNICODE *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3192 | Py_ssize_t size, |
| 3193 | const char *encoding, |
| 3194 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3195 | { |
| 3196 | PyObject *v, *unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3197 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3198 | unicode = PyUnicode_FromUnicode(s, size); |
| 3199 | if (unicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3200 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3201 | v = PyUnicode_AsEncodedString(unicode, encoding, errors); |
| 3202 | Py_DECREF(unicode); |
| 3203 | return v; |
| 3204 | } |
| 3205 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3206 | PyObject * |
| 3207 | PyUnicode_AsEncodedObject(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3208 | const char *encoding, |
| 3209 | const char *errors) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3210 | { |
| 3211 | PyObject *v; |
| 3212 | |
| 3213 | if (!PyUnicode_Check(unicode)) { |
| 3214 | PyErr_BadArgument(); |
| 3215 | goto onError; |
| 3216 | } |
| 3217 | |
| 3218 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3219 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3220 | |
| 3221 | /* Encode via the codec registry */ |
| 3222 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3223 | if (v == NULL) |
| 3224 | goto onError; |
| 3225 | return v; |
| 3226 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3227 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3228 | return NULL; |
| 3229 | } |
| 3230 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3231 | static size_t |
| 3232 | wcstombs_errorpos(const wchar_t *wstr) |
| 3233 | { |
| 3234 | size_t len; |
| 3235 | #if SIZEOF_WCHAR_T == 2 |
| 3236 | wchar_t buf[3]; |
| 3237 | #else |
| 3238 | wchar_t buf[2]; |
| 3239 | #endif |
| 3240 | char outbuf[MB_LEN_MAX]; |
| 3241 | const wchar_t *start, *previous; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3242 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3243 | #if SIZEOF_WCHAR_T == 2 |
| 3244 | buf[2] = 0; |
| 3245 | #else |
| 3246 | buf[1] = 0; |
| 3247 | #endif |
| 3248 | start = wstr; |
| 3249 | while (*wstr != L'\0') |
| 3250 | { |
| 3251 | previous = wstr; |
| 3252 | #if SIZEOF_WCHAR_T == 2 |
| 3253 | if (Py_UNICODE_IS_HIGH_SURROGATE(wstr[0]) |
| 3254 | && Py_UNICODE_IS_LOW_SURROGATE(wstr[1])) |
| 3255 | { |
| 3256 | buf[0] = wstr[0]; |
| 3257 | buf[1] = wstr[1]; |
| 3258 | wstr += 2; |
| 3259 | } |
| 3260 | else { |
| 3261 | buf[0] = *wstr; |
| 3262 | buf[1] = 0; |
| 3263 | wstr++; |
| 3264 | } |
| 3265 | #else |
| 3266 | buf[0] = *wstr; |
| 3267 | wstr++; |
| 3268 | #endif |
| 3269 | len = wcstombs(outbuf, buf, sizeof(outbuf)); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3270 | if (len == (size_t)-1) |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3271 | return previous - start; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3272 | } |
| 3273 | |
| 3274 | /* failed to find the unencodable character */ |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3275 | return 0; |
| 3276 | } |
| 3277 | |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3278 | static int |
| 3279 | locale_error_handler(const char *errors, int *surrogateescape) |
| 3280 | { |
| 3281 | if (errors == NULL) { |
| 3282 | *surrogateescape = 0; |
| 3283 | return 0; |
| 3284 | } |
| 3285 | |
| 3286 | if (strcmp(errors, "strict") == 0) { |
| 3287 | *surrogateescape = 0; |
| 3288 | return 0; |
| 3289 | } |
| 3290 | if (strcmp(errors, "surrogateescape") == 0) { |
| 3291 | *surrogateescape = 1; |
| 3292 | return 0; |
| 3293 | } |
| 3294 | PyErr_Format(PyExc_ValueError, |
| 3295 | "only 'strict' and 'surrogateescape' error handlers " |
| 3296 | "are supported, not '%s'", |
| 3297 | errors); |
| 3298 | return -1; |
| 3299 | } |
| 3300 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3301 | PyObject * |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3302 | PyUnicode_EncodeLocale(PyObject *unicode, const char *errors) |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3303 | { |
| 3304 | Py_ssize_t wlen, wlen2; |
| 3305 | wchar_t *wstr; |
| 3306 | PyObject *bytes = NULL; |
| 3307 | char *errmsg; |
Victor Stinner | 1f33f2b | 2011-12-17 04:45:09 +0100 | [diff] [blame] | 3308 | PyObject *reason; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3309 | PyObject *exc; |
| 3310 | size_t error_pos; |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3311 | int surrogateescape; |
| 3312 | |
| 3313 | if (locale_error_handler(errors, &surrogateescape) < 0) |
| 3314 | return NULL; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3315 | |
| 3316 | wstr = PyUnicode_AsWideCharString(unicode, &wlen); |
| 3317 | if (wstr == NULL) |
| 3318 | return NULL; |
| 3319 | |
| 3320 | wlen2 = wcslen(wstr); |
| 3321 | if (wlen2 != wlen) { |
| 3322 | PyMem_Free(wstr); |
| 3323 | PyErr_SetString(PyExc_TypeError, "embedded null character"); |
| 3324 | return NULL; |
| 3325 | } |
| 3326 | |
| 3327 | if (surrogateescape) { |
| 3328 | /* locale encoding with surrogateescape */ |
| 3329 | char *str; |
| 3330 | |
| 3331 | str = _Py_wchar2char(wstr, &error_pos); |
| 3332 | if (str == NULL) { |
| 3333 | if (error_pos == (size_t)-1) { |
| 3334 | PyErr_NoMemory(); |
| 3335 | PyMem_Free(wstr); |
| 3336 | return NULL; |
| 3337 | } |
| 3338 | else { |
| 3339 | goto encode_error; |
| 3340 | } |
| 3341 | } |
| 3342 | PyMem_Free(wstr); |
| 3343 | |
| 3344 | bytes = PyBytes_FromString(str); |
| 3345 | PyMem_Free(str); |
| 3346 | } |
| 3347 | else { |
| 3348 | size_t len, len2; |
| 3349 | |
| 3350 | len = wcstombs(NULL, wstr, 0); |
| 3351 | if (len == (size_t)-1) { |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3352 | error_pos = (size_t)-1; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3353 | goto encode_error; |
| 3354 | } |
| 3355 | |
| 3356 | bytes = PyBytes_FromStringAndSize(NULL, len); |
| 3357 | if (bytes == NULL) { |
| 3358 | PyMem_Free(wstr); |
| 3359 | return NULL; |
| 3360 | } |
| 3361 | |
| 3362 | len2 = wcstombs(PyBytes_AS_STRING(bytes), wstr, len+1); |
| 3363 | if (len2 == (size_t)-1 || len2 > len) { |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3364 | error_pos = (size_t)-1; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3365 | goto encode_error; |
| 3366 | } |
| 3367 | PyMem_Free(wstr); |
| 3368 | } |
| 3369 | return bytes; |
| 3370 | |
| 3371 | encode_error: |
| 3372 | errmsg = strerror(errno); |
| 3373 | assert(errmsg != NULL); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3374 | |
| 3375 | if (error_pos == (size_t)-1) |
| 3376 | error_pos = wcstombs_errorpos(wstr); |
| 3377 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3378 | PyMem_Free(wstr); |
| 3379 | Py_XDECREF(bytes); |
| 3380 | |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3381 | if (errmsg != NULL) { |
| 3382 | size_t errlen; |
| 3383 | wstr = _Py_char2wchar(errmsg, &errlen); |
| 3384 | if (wstr != NULL) { |
| 3385 | reason = PyUnicode_FromWideChar(wstr, errlen); |
| 3386 | PyMem_Free(wstr); |
| 3387 | } else |
| 3388 | errmsg = NULL; |
| 3389 | } |
| 3390 | if (errmsg == NULL) |
Victor Stinner | 1f33f2b | 2011-12-17 04:45:09 +0100 | [diff] [blame] | 3391 | reason = PyUnicode_FromString( |
| 3392 | "wcstombs() encountered an unencodable " |
| 3393 | "wide character"); |
| 3394 | if (reason == NULL) |
| 3395 | return NULL; |
| 3396 | |
| 3397 | exc = PyObject_CallFunction(PyExc_UnicodeEncodeError, "sOnnO", |
| 3398 | "locale", unicode, |
| 3399 | (Py_ssize_t)error_pos, |
| 3400 | (Py_ssize_t)(error_pos+1), |
| 3401 | reason); |
| 3402 | Py_DECREF(reason); |
| 3403 | if (exc != NULL) { |
| 3404 | PyCodec_StrictErrors(exc); |
| 3405 | Py_XDECREF(exc); |
| 3406 | } |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3407 | return NULL; |
| 3408 | } |
| 3409 | |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3410 | PyObject * |
| 3411 | PyUnicode_EncodeFSDefault(PyObject *unicode) |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3412 | { |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3413 | #ifdef HAVE_MBCS |
Victor Stinner | ac931b1 | 2011-11-20 18:27:03 +0100 | [diff] [blame] | 3414 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, NULL); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3415 | #elif defined(__APPLE__) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3416 | return _PyUnicode_AsUTF8String(unicode, "surrogateescape"); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3417 | #else |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 3418 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 3419 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 3420 | cannot use it to encode and decode filenames before it is loaded. Load |
| 3421 | the Python codec requires to encode at least its own filename. Use the C |
| 3422 | version of the locale codec until the codec registry is initialized and |
| 3423 | the Python codec is loaded. |
| 3424 | |
| 3425 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 3426 | cannot only rely on it: check also interp->fscodec_initialized for |
| 3427 | subinterpreters. */ |
| 3428 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3429 | return PyUnicode_AsEncodedString(unicode, |
| 3430 | Py_FileSystemDefaultEncoding, |
| 3431 | "surrogateescape"); |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 3432 | } |
| 3433 | else { |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3434 | return PyUnicode_EncodeLocale(unicode, "surrogateescape"); |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 3435 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3436 | #endif |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3437 | } |
| 3438 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3439 | PyObject * |
| 3440 | PyUnicode_AsEncodedString(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3441 | const char *encoding, |
| 3442 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3443 | { |
| 3444 | PyObject *v; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3445 | char lower[11]; /* Enough for any encoding shortcut */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3446 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3447 | if (!PyUnicode_Check(unicode)) { |
| 3448 | PyErr_BadArgument(); |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3449 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3450 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3451 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3452 | /* Shortcuts for common default encodings */ |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3453 | if (normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3454 | if ((strcmp(lower, "utf-8") == 0) || |
| 3455 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 3456 | { |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3457 | if (errors == NULL || strcmp(errors, "strict") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3458 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3459 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3460 | return _PyUnicode_AsUTF8String(unicode, errors); |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 3461 | } |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3462 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3463 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3464 | (strcmp(lower, "iso-8859-1") == 0)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3465 | return _PyUnicode_AsLatin1String(unicode, errors); |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3466 | #ifdef HAVE_MBCS |
Victor Stinner | ac931b1 | 2011-11-20 18:27:03 +0100 | [diff] [blame] | 3467 | else if (strcmp(lower, "mbcs") == 0) |
| 3468 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 3469 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3470 | else if (strcmp(lower, "ascii") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3471 | return _PyUnicode_AsASCIIString(unicode, errors); |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3472 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3473 | |
| 3474 | /* Encode via the codec registry */ |
| 3475 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3476 | if (v == NULL) |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3477 | return NULL; |
| 3478 | |
| 3479 | /* The normal path */ |
| 3480 | if (PyBytes_Check(v)) |
| 3481 | return v; |
| 3482 | |
| 3483 | /* 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] | 3484 | if (PyByteArray_Check(v)) { |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 3485 | int error; |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3486 | PyObject *b; |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 3487 | |
| 3488 | error = PyErr_WarnFormat(PyExc_RuntimeWarning, 1, |
| 3489 | "encoder %s returned bytearray instead of bytes", |
| 3490 | encoding); |
| 3491 | if (error) { |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3492 | Py_DECREF(v); |
| 3493 | return NULL; |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3494 | } |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3495 | |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3496 | b = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(v), Py_SIZE(v)); |
| 3497 | Py_DECREF(v); |
| 3498 | return b; |
| 3499 | } |
| 3500 | |
| 3501 | PyErr_Format(PyExc_TypeError, |
| 3502 | "encoder did not return a bytes object (type=%.400s)", |
| 3503 | Py_TYPE(v)->tp_name); |
| 3504 | Py_DECREF(v); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3505 | return NULL; |
| 3506 | } |
| 3507 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3508 | PyObject * |
| 3509 | PyUnicode_AsEncodedUnicode(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3510 | const char *encoding, |
| 3511 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3512 | { |
| 3513 | PyObject *v; |
| 3514 | |
| 3515 | if (!PyUnicode_Check(unicode)) { |
| 3516 | PyErr_BadArgument(); |
| 3517 | goto onError; |
| 3518 | } |
| 3519 | |
| 3520 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3521 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3522 | |
| 3523 | /* Encode via the codec registry */ |
| 3524 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3525 | if (v == NULL) |
| 3526 | goto onError; |
| 3527 | if (!PyUnicode_Check(v)) { |
| 3528 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3529 | "encoder did not return an str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3530 | Py_TYPE(v)->tp_name); |
| 3531 | Py_DECREF(v); |
| 3532 | goto onError; |
| 3533 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3534 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3535 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3536 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3537 | return NULL; |
| 3538 | } |
| 3539 | |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3540 | static size_t |
| 3541 | mbstowcs_errorpos(const char *str, size_t len) |
| 3542 | { |
| 3543 | #ifdef HAVE_MBRTOWC |
| 3544 | const char *start = str; |
| 3545 | mbstate_t mbs; |
| 3546 | size_t converted; |
| 3547 | wchar_t ch; |
| 3548 | |
| 3549 | memset(&mbs, 0, sizeof mbs); |
| 3550 | while (len) |
| 3551 | { |
| 3552 | converted = mbrtowc(&ch, (char*)str, len, &mbs); |
| 3553 | if (converted == 0) |
| 3554 | /* Reached end of string */ |
| 3555 | break; |
| 3556 | if (converted == (size_t)-1 || converted == (size_t)-2) { |
| 3557 | /* Conversion error or incomplete character */ |
| 3558 | return str - start; |
| 3559 | } |
| 3560 | else { |
| 3561 | str += converted; |
| 3562 | len -= converted; |
| 3563 | } |
| 3564 | } |
| 3565 | /* failed to find the undecodable byte sequence */ |
| 3566 | return 0; |
| 3567 | #endif |
| 3568 | return 0; |
| 3569 | } |
| 3570 | |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3571 | PyObject* |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3572 | PyUnicode_DecodeLocaleAndSize(const char *str, Py_ssize_t len, |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3573 | const char *errors) |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3574 | { |
| 3575 | wchar_t smallbuf[256]; |
| 3576 | size_t smallbuf_len = Py_ARRAY_LENGTH(smallbuf); |
| 3577 | wchar_t *wstr; |
| 3578 | size_t wlen, wlen2; |
| 3579 | PyObject *unicode; |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3580 | int surrogateescape; |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3581 | size_t error_pos; |
| 3582 | char *errmsg; |
| 3583 | PyObject *reason, *exc; |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3584 | |
| 3585 | if (locale_error_handler(errors, &surrogateescape) < 0) |
| 3586 | return NULL; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3587 | |
| 3588 | if (str[len] != '\0' || len != strlen(str)) { |
| 3589 | PyErr_SetString(PyExc_TypeError, "embedded null character"); |
| 3590 | return NULL; |
| 3591 | } |
| 3592 | |
| 3593 | if (surrogateescape) |
| 3594 | { |
| 3595 | wstr = _Py_char2wchar(str, &wlen); |
| 3596 | if (wstr == NULL) { |
| 3597 | if (wlen == (size_t)-1) |
| 3598 | PyErr_NoMemory(); |
| 3599 | else |
| 3600 | PyErr_SetFromErrno(PyExc_OSError); |
| 3601 | return NULL; |
| 3602 | } |
| 3603 | |
| 3604 | unicode = PyUnicode_FromWideChar(wstr, wlen); |
| 3605 | PyMem_Free(wstr); |
| 3606 | } |
| 3607 | else { |
| 3608 | #ifndef HAVE_BROKEN_MBSTOWCS |
| 3609 | wlen = mbstowcs(NULL, str, 0); |
| 3610 | #else |
| 3611 | wlen = len; |
| 3612 | #endif |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3613 | if (wlen == (size_t)-1) |
| 3614 | goto decode_error; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3615 | if (wlen+1 <= smallbuf_len) { |
| 3616 | wstr = smallbuf; |
| 3617 | } |
| 3618 | else { |
| 3619 | if (wlen > PY_SSIZE_T_MAX / sizeof(wchar_t) - 1) |
| 3620 | return PyErr_NoMemory(); |
| 3621 | |
| 3622 | wstr = PyMem_Malloc((wlen+1) * sizeof(wchar_t)); |
| 3623 | if (!wstr) |
| 3624 | return PyErr_NoMemory(); |
| 3625 | } |
| 3626 | |
| 3627 | /* This shouldn't fail now */ |
| 3628 | wlen2 = mbstowcs(wstr, str, wlen+1); |
| 3629 | if (wlen2 == (size_t)-1) { |
| 3630 | if (wstr != smallbuf) |
| 3631 | PyMem_Free(wstr); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3632 | goto decode_error; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3633 | } |
| 3634 | #ifdef HAVE_BROKEN_MBSTOWCS |
| 3635 | assert(wlen2 == wlen); |
| 3636 | #endif |
| 3637 | unicode = PyUnicode_FromWideChar(wstr, wlen2); |
| 3638 | if (wstr != smallbuf) |
| 3639 | PyMem_Free(wstr); |
| 3640 | } |
| 3641 | return unicode; |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3642 | |
| 3643 | decode_error: |
| 3644 | errmsg = strerror(errno); |
| 3645 | assert(errmsg != NULL); |
| 3646 | |
| 3647 | error_pos = mbstowcs_errorpos(str, len); |
| 3648 | if (errmsg != NULL) { |
| 3649 | size_t errlen; |
| 3650 | wstr = _Py_char2wchar(errmsg, &errlen); |
| 3651 | if (wstr != NULL) { |
| 3652 | reason = PyUnicode_FromWideChar(wstr, errlen); |
| 3653 | PyMem_Free(wstr); |
| 3654 | } else |
| 3655 | errmsg = NULL; |
| 3656 | } |
| 3657 | if (errmsg == NULL) |
| 3658 | reason = PyUnicode_FromString( |
| 3659 | "mbstowcs() encountered an invalid multibyte sequence"); |
| 3660 | if (reason == NULL) |
| 3661 | return NULL; |
| 3662 | |
| 3663 | exc = PyObject_CallFunction(PyExc_UnicodeDecodeError, "sy#nnO", |
| 3664 | "locale", str, len, |
| 3665 | (Py_ssize_t)error_pos, |
| 3666 | (Py_ssize_t)(error_pos+1), |
| 3667 | reason); |
| 3668 | Py_DECREF(reason); |
| 3669 | if (exc != NULL) { |
| 3670 | PyCodec_StrictErrors(exc); |
| 3671 | Py_XDECREF(exc); |
| 3672 | } |
| 3673 | return NULL; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3674 | } |
| 3675 | |
| 3676 | PyObject* |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3677 | PyUnicode_DecodeLocale(const char *str, const char *errors) |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3678 | { |
| 3679 | Py_ssize_t size = (Py_ssize_t)strlen(str); |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3680 | return PyUnicode_DecodeLocaleAndSize(str, size, errors); |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3681 | } |
| 3682 | |
| 3683 | |
| 3684 | PyObject* |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3685 | PyUnicode_DecodeFSDefault(const char *s) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3686 | Py_ssize_t size = (Py_ssize_t)strlen(s); |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3687 | return PyUnicode_DecodeFSDefaultAndSize(s, size); |
| 3688 | } |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3689 | |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3690 | PyObject* |
| 3691 | PyUnicode_DecodeFSDefaultAndSize(const char *s, Py_ssize_t size) |
| 3692 | { |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3693 | #ifdef HAVE_MBCS |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3694 | return PyUnicode_DecodeMBCS(s, size, NULL); |
| 3695 | #elif defined(__APPLE__) |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 3696 | return PyUnicode_DecodeUTF8Stateful(s, size, "surrogateescape", NULL); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3697 | #else |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 3698 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 3699 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 3700 | cannot use it to encode and decode filenames before it is loaded. Load |
| 3701 | the Python codec requires to encode at least its own filename. Use the C |
| 3702 | version of the locale codec until the codec registry is initialized and |
| 3703 | the Python codec is loaded. |
| 3704 | |
| 3705 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 3706 | cannot only rely on it: check also interp->fscodec_initialized for |
| 3707 | subinterpreters. */ |
| 3708 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3709 | return PyUnicode_Decode(s, size, |
| 3710 | Py_FileSystemDefaultEncoding, |
Victor Stinner | b9a20ad | 2010-04-30 16:37:52 +0000 | [diff] [blame] | 3711 | "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3712 | } |
| 3713 | else { |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3714 | return PyUnicode_DecodeLocaleAndSize(s, size, "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3715 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3716 | #endif |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3717 | } |
| 3718 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3719 | |
| 3720 | int |
Antoine Pitrou | 1334884 | 2012-01-29 18:36:34 +0100 | [diff] [blame] | 3721 | _PyUnicode_HasNULChars(PyObject* s) |
| 3722 | { |
| 3723 | static PyObject *nul = NULL; |
| 3724 | |
| 3725 | if (nul == NULL) |
| 3726 | nul = PyUnicode_FromStringAndSize("\0", 1); |
| 3727 | if (nul == NULL) |
| 3728 | return -1; |
| 3729 | return PyUnicode_Contains(s, nul); |
| 3730 | } |
| 3731 | |
| 3732 | |
| 3733 | int |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3734 | PyUnicode_FSConverter(PyObject* arg, void* addr) |
| 3735 | { |
| 3736 | PyObject *output = NULL; |
| 3737 | Py_ssize_t size; |
| 3738 | void *data; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3739 | if (arg == NULL) { |
| 3740 | Py_DECREF(*(PyObject**)addr); |
| 3741 | return 1; |
| 3742 | } |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 3743 | if (PyBytes_Check(arg)) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3744 | output = arg; |
| 3745 | Py_INCREF(output); |
| 3746 | } |
| 3747 | else { |
| 3748 | arg = PyUnicode_FromObject(arg); |
| 3749 | if (!arg) |
| 3750 | return 0; |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3751 | output = PyUnicode_EncodeFSDefault(arg); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3752 | Py_DECREF(arg); |
| 3753 | if (!output) |
| 3754 | return 0; |
| 3755 | if (!PyBytes_Check(output)) { |
| 3756 | Py_DECREF(output); |
| 3757 | PyErr_SetString(PyExc_TypeError, "encoder failed to return bytes"); |
| 3758 | return 0; |
| 3759 | } |
| 3760 | } |
Victor Stinner | 0ea2a46 | 2010-04-30 00:22:08 +0000 | [diff] [blame] | 3761 | size = PyBytes_GET_SIZE(output); |
| 3762 | data = PyBytes_AS_STRING(output); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3763 | if (size != strlen(data)) { |
Benjamin Peterson | 7a6b44a | 2011-08-18 13:51:47 -0500 | [diff] [blame] | 3764 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3765 | Py_DECREF(output); |
| 3766 | return 0; |
| 3767 | } |
| 3768 | *(PyObject**)addr = output; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3769 | return Py_CLEANUP_SUPPORTED; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3770 | } |
| 3771 | |
| 3772 | |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3773 | int |
| 3774 | PyUnicode_FSDecoder(PyObject* arg, void* addr) |
| 3775 | { |
| 3776 | PyObject *output = NULL; |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3777 | if (arg == NULL) { |
| 3778 | Py_DECREF(*(PyObject**)addr); |
| 3779 | return 1; |
| 3780 | } |
| 3781 | if (PyUnicode_Check(arg)) { |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 3782 | if (PyUnicode_READY(arg) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3783 | return 0; |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3784 | output = arg; |
| 3785 | Py_INCREF(output); |
| 3786 | } |
| 3787 | else { |
| 3788 | arg = PyBytes_FromObject(arg); |
| 3789 | if (!arg) |
| 3790 | return 0; |
| 3791 | output = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AS_STRING(arg), |
| 3792 | PyBytes_GET_SIZE(arg)); |
| 3793 | Py_DECREF(arg); |
| 3794 | if (!output) |
| 3795 | return 0; |
| 3796 | if (!PyUnicode_Check(output)) { |
| 3797 | Py_DECREF(output); |
| 3798 | PyErr_SetString(PyExc_TypeError, "decoder failed to return unicode"); |
| 3799 | return 0; |
| 3800 | } |
| 3801 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 3802 | if (PyUnicode_READY(output) == -1) { |
Victor Stinner | 065836e | 2011-10-27 01:56:33 +0200 | [diff] [blame] | 3803 | Py_DECREF(output); |
| 3804 | return 0; |
| 3805 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3806 | if (findchar(PyUnicode_DATA(output), PyUnicode_KIND(output), |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 3807 | PyUnicode_GET_LENGTH(output), 0, 1) >= 0) { |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3808 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
| 3809 | Py_DECREF(output); |
| 3810 | return 0; |
| 3811 | } |
| 3812 | *(PyObject**)addr = output; |
| 3813 | return Py_CLEANUP_SUPPORTED; |
| 3814 | } |
| 3815 | |
| 3816 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3817 | char* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3818 | PyUnicode_AsUTF8AndSize(PyObject *unicode, Py_ssize_t *psize) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3819 | { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 3820 | PyObject *bytes; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3821 | |
Neal Norwitz | e0a0a6e | 2007-08-25 01:04:21 +0000 | [diff] [blame] | 3822 | if (!PyUnicode_Check(unicode)) { |
| 3823 | PyErr_BadArgument(); |
| 3824 | return NULL; |
| 3825 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3826 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3827 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3828 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3829 | if (PyUnicode_UTF8(unicode) == NULL) { |
| 3830 | assert(!PyUnicode_IS_COMPACT_ASCII(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3831 | bytes = _PyUnicode_AsUTF8String(unicode, "strict"); |
| 3832 | if (bytes == NULL) |
| 3833 | return NULL; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3834 | _PyUnicode_UTF8(unicode) = PyObject_MALLOC(PyBytes_GET_SIZE(bytes) + 1); |
| 3835 | if (_PyUnicode_UTF8(unicode) == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3836 | Py_DECREF(bytes); |
| 3837 | return NULL; |
| 3838 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3839 | _PyUnicode_UTF8_LENGTH(unicode) = PyBytes_GET_SIZE(bytes); |
| 3840 | Py_MEMCPY(_PyUnicode_UTF8(unicode), |
| 3841 | PyBytes_AS_STRING(bytes), |
| 3842 | _PyUnicode_UTF8_LENGTH(unicode) + 1); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3843 | Py_DECREF(bytes); |
| 3844 | } |
| 3845 | |
| 3846 | if (psize) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3847 | *psize = PyUnicode_UTF8_LENGTH(unicode); |
| 3848 | return PyUnicode_UTF8(unicode); |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 3849 | } |
| 3850 | |
| 3851 | char* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3852 | PyUnicode_AsUTF8(PyObject *unicode) |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 3853 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3854 | return PyUnicode_AsUTF8AndSize(unicode, NULL); |
| 3855 | } |
| 3856 | |
| 3857 | #ifdef Py_DEBUG |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 3858 | static int unicode_as_unicode_calls = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3859 | #endif |
| 3860 | |
| 3861 | |
| 3862 | Py_UNICODE * |
| 3863 | PyUnicode_AsUnicodeAndSize(PyObject *unicode, Py_ssize_t *size) |
| 3864 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3865 | const unsigned char *one_byte; |
| 3866 | #if SIZEOF_WCHAR_T == 4 |
| 3867 | const Py_UCS2 *two_bytes; |
| 3868 | #else |
| 3869 | const Py_UCS4 *four_bytes; |
| 3870 | const Py_UCS4 *ucs4_end; |
| 3871 | Py_ssize_t num_surrogates; |
| 3872 | #endif |
| 3873 | wchar_t *w; |
| 3874 | wchar_t *wchar_end; |
| 3875 | |
| 3876 | if (!PyUnicode_Check(unicode)) { |
| 3877 | PyErr_BadArgument(); |
| 3878 | return NULL; |
| 3879 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3880 | if (_PyUnicode_WSTR(unicode) == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3881 | /* Non-ASCII compact unicode object */ |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3882 | assert(_PyUnicode_KIND(unicode) != 0); |
| 3883 | assert(PyUnicode_IS_READY(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3884 | |
| 3885 | #ifdef Py_DEBUG |
| 3886 | ++unicode_as_unicode_calls; |
| 3887 | #endif |
| 3888 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3889 | if (PyUnicode_KIND(unicode) == PyUnicode_4BYTE_KIND) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3890 | #if SIZEOF_WCHAR_T == 2 |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3891 | four_bytes = PyUnicode_4BYTE_DATA(unicode); |
| 3892 | ucs4_end = four_bytes + _PyUnicode_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3893 | num_surrogates = 0; |
| 3894 | |
| 3895 | for (; four_bytes < ucs4_end; ++four_bytes) { |
| 3896 | if (*four_bytes > 0xFFFF) |
| 3897 | ++num_surrogates; |
| 3898 | } |
| 3899 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3900 | _PyUnicode_WSTR(unicode) = (wchar_t *) PyObject_MALLOC( |
| 3901 | sizeof(wchar_t) * (_PyUnicode_LENGTH(unicode) + 1 + num_surrogates)); |
| 3902 | if (!_PyUnicode_WSTR(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3903 | PyErr_NoMemory(); |
| 3904 | return NULL; |
| 3905 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3906 | _PyUnicode_WSTR_LENGTH(unicode) = _PyUnicode_LENGTH(unicode) + num_surrogates; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3907 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3908 | w = _PyUnicode_WSTR(unicode); |
| 3909 | wchar_end = w + _PyUnicode_WSTR_LENGTH(unicode); |
| 3910 | four_bytes = PyUnicode_4BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3911 | for (; four_bytes < ucs4_end; ++four_bytes, ++w) { |
| 3912 | if (*four_bytes > 0xFFFF) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 3913 | assert(*four_bytes <= MAX_UNICODE); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3914 | /* encode surrogate pair in this case */ |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 3915 | *w++ = Py_UNICODE_HIGH_SURROGATE(*four_bytes); |
| 3916 | *w = Py_UNICODE_LOW_SURROGATE(*four_bytes); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3917 | } |
| 3918 | else |
| 3919 | *w = *four_bytes; |
| 3920 | |
| 3921 | if (w > wchar_end) { |
| 3922 | assert(0 && "Miscalculated string end"); |
| 3923 | } |
| 3924 | } |
| 3925 | *w = 0; |
| 3926 | #else |
| 3927 | /* sizeof(wchar_t) == 4 */ |
| 3928 | Py_FatalError("Impossible unicode object state, wstr and str " |
| 3929 | "should share memory already."); |
| 3930 | return NULL; |
| 3931 | #endif |
| 3932 | } |
| 3933 | else { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3934 | _PyUnicode_WSTR(unicode) = (wchar_t *) PyObject_MALLOC(sizeof(wchar_t) * |
| 3935 | (_PyUnicode_LENGTH(unicode) + 1)); |
| 3936 | if (!_PyUnicode_WSTR(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3937 | PyErr_NoMemory(); |
| 3938 | return NULL; |
| 3939 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3940 | if (!PyUnicode_IS_COMPACT_ASCII(unicode)) |
| 3941 | _PyUnicode_WSTR_LENGTH(unicode) = _PyUnicode_LENGTH(unicode); |
| 3942 | w = _PyUnicode_WSTR(unicode); |
| 3943 | wchar_end = w + _PyUnicode_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3944 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3945 | if (PyUnicode_KIND(unicode) == PyUnicode_1BYTE_KIND) { |
| 3946 | one_byte = PyUnicode_1BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3947 | for (; w < wchar_end; ++one_byte, ++w) |
| 3948 | *w = *one_byte; |
| 3949 | /* null-terminate the wstr */ |
| 3950 | *w = 0; |
| 3951 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3952 | else if (PyUnicode_KIND(unicode) == PyUnicode_2BYTE_KIND) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3953 | #if SIZEOF_WCHAR_T == 4 |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3954 | two_bytes = PyUnicode_2BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3955 | for (; w < wchar_end; ++two_bytes, ++w) |
| 3956 | *w = *two_bytes; |
| 3957 | /* null-terminate the wstr */ |
| 3958 | *w = 0; |
| 3959 | #else |
| 3960 | /* sizeof(wchar_t) == 2 */ |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3961 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 3962 | _PyUnicode_WSTR(unicode) = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3963 | Py_FatalError("Impossible unicode object state, wstr " |
| 3964 | "and str should share memory already."); |
| 3965 | return NULL; |
| 3966 | #endif |
| 3967 | } |
| 3968 | else { |
| 3969 | assert(0 && "This should never happen."); |
| 3970 | } |
| 3971 | } |
| 3972 | } |
| 3973 | if (size != NULL) |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3974 | *size = PyUnicode_WSTR_LENGTH(unicode); |
| 3975 | return _PyUnicode_WSTR(unicode); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3976 | } |
| 3977 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3978 | Py_UNICODE * |
| 3979 | PyUnicode_AsUnicode(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3980 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3981 | return PyUnicode_AsUnicodeAndSize(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3982 | } |
| 3983 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3984 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3985 | Py_ssize_t |
| 3986 | PyUnicode_GetSize(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3987 | { |
| 3988 | if (!PyUnicode_Check(unicode)) { |
| 3989 | PyErr_BadArgument(); |
| 3990 | goto onError; |
| 3991 | } |
| 3992 | return PyUnicode_GET_SIZE(unicode); |
| 3993 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3994 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3995 | return -1; |
| 3996 | } |
| 3997 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3998 | Py_ssize_t |
| 3999 | PyUnicode_GetLength(PyObject *unicode) |
| 4000 | { |
Victor Stinner | 5a706cf | 2011-10-02 00:36:53 +0200 | [diff] [blame] | 4001 | if (!PyUnicode_Check(unicode) || PyUnicode_READY(unicode) == -1) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4002 | PyErr_BadArgument(); |
| 4003 | return -1; |
| 4004 | } |
| 4005 | |
| 4006 | return PyUnicode_GET_LENGTH(unicode); |
| 4007 | } |
| 4008 | |
| 4009 | Py_UCS4 |
| 4010 | PyUnicode_ReadChar(PyObject *unicode, Py_ssize_t index) |
| 4011 | { |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 4012 | if (!PyUnicode_Check(unicode) || PyUnicode_READY(unicode) == -1) { |
| 4013 | PyErr_BadArgument(); |
| 4014 | return (Py_UCS4)-1; |
| 4015 | } |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 4016 | if (index < 0 || index >= PyUnicode_GET_LENGTH(unicode)) { |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 4017 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4018 | return (Py_UCS4)-1; |
| 4019 | } |
| 4020 | return PyUnicode_READ_CHAR(unicode, index); |
| 4021 | } |
| 4022 | |
| 4023 | int |
| 4024 | PyUnicode_WriteChar(PyObject *unicode, Py_ssize_t index, Py_UCS4 ch) |
| 4025 | { |
| 4026 | if (!PyUnicode_Check(unicode) || !PyUnicode_IS_COMPACT(unicode)) { |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 4027 | PyErr_BadArgument(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4028 | return -1; |
| 4029 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 4030 | assert(PyUnicode_IS_READY(unicode)); |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 4031 | if (index < 0 || index >= PyUnicode_GET_LENGTH(unicode)) { |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 4032 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 4033 | return -1; |
| 4034 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 4035 | if (unicode_check_modifiable(unicode)) |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 4036 | return -1; |
Victor Stinner | c9590ad | 2012-03-04 01:34:37 +0100 | [diff] [blame] | 4037 | if (ch > PyUnicode_MAX_CHAR_VALUE(unicode)) { |
| 4038 | PyErr_SetString(PyExc_ValueError, "character out of range"); |
| 4039 | return -1; |
| 4040 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4041 | PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode), |
| 4042 | index, ch); |
| 4043 | return 0; |
| 4044 | } |
| 4045 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4046 | const char * |
| 4047 | PyUnicode_GetDefaultEncoding(void) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 4048 | { |
Victor Stinner | 42cb462 | 2010-09-01 19:39:01 +0000 | [diff] [blame] | 4049 | return "utf-8"; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 4050 | } |
| 4051 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4052 | /* create or adjust a UnicodeDecodeError */ |
| 4053 | static void |
| 4054 | make_decode_exception(PyObject **exceptionObject, |
| 4055 | const char *encoding, |
| 4056 | const char *input, Py_ssize_t length, |
| 4057 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 4058 | const char *reason) |
| 4059 | { |
| 4060 | if (*exceptionObject == NULL) { |
| 4061 | *exceptionObject = PyUnicodeDecodeError_Create( |
| 4062 | encoding, input, length, startpos, endpos, reason); |
| 4063 | } |
| 4064 | else { |
| 4065 | if (PyUnicodeDecodeError_SetStart(*exceptionObject, startpos)) |
| 4066 | goto onError; |
| 4067 | if (PyUnicodeDecodeError_SetEnd(*exceptionObject, endpos)) |
| 4068 | goto onError; |
| 4069 | if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) |
| 4070 | goto onError; |
| 4071 | } |
| 4072 | return; |
| 4073 | |
| 4074 | onError: |
| 4075 | Py_DECREF(*exceptionObject); |
| 4076 | *exceptionObject = NULL; |
| 4077 | } |
| 4078 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4079 | /* error handling callback helper: |
| 4080 | build arguments, call the callback and check the arguments, |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 4081 | if no exception occurred, copy the replacement to the output |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4082 | and adjust various state variables. |
| 4083 | return 0 on success, -1 on error |
| 4084 | */ |
| 4085 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4086 | static int |
| 4087 | unicode_decode_call_errorhandler(const char *errors, PyObject **errorHandler, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4088 | const char *encoding, const char *reason, |
| 4089 | const char **input, const char **inend, Py_ssize_t *startinpos, |
| 4090 | Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4091 | PyObject **output, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4092 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 4093 | 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] | 4094 | |
| 4095 | PyObject *restuple = NULL; |
| 4096 | PyObject *repunicode = NULL; |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 4097 | Py_ssize_t outsize; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4098 | Py_ssize_t insize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4099 | Py_ssize_t requiredsize; |
| 4100 | Py_ssize_t newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4101 | PyObject *inputobj = NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4102 | int res = -1; |
| 4103 | |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 4104 | if (_PyUnicode_KIND(*output) != PyUnicode_WCHAR_KIND) |
| 4105 | outsize = PyUnicode_GET_LENGTH(*output); |
| 4106 | else |
| 4107 | outsize = _PyUnicode_WSTR_LENGTH(*output); |
| 4108 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4109 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4110 | *errorHandler = PyCodec_LookupError(errors); |
| 4111 | if (*errorHandler == NULL) |
| 4112 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4113 | } |
| 4114 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4115 | make_decode_exception(exceptionObject, |
| 4116 | encoding, |
| 4117 | *input, *inend - *input, |
| 4118 | *startinpos, *endinpos, |
| 4119 | reason); |
| 4120 | if (*exceptionObject == NULL) |
| 4121 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4122 | |
| 4123 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 4124 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4125 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4126 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 4127 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4128 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4129 | } |
| 4130 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4131 | goto onError; |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 4132 | if (PyUnicode_READY(repunicode) == -1) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4133 | goto onError; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4134 | |
| 4135 | /* Copy back the bytes variables, which might have been modified by the |
| 4136 | callback */ |
| 4137 | inputobj = PyUnicodeDecodeError_GetObject(*exceptionObject); |
| 4138 | if (!inputobj) |
| 4139 | goto onError; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4140 | if (!PyBytes_Check(inputobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4141 | PyErr_Format(PyExc_TypeError, "exception attribute object must be bytes"); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4142 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4143 | *input = PyBytes_AS_STRING(inputobj); |
| 4144 | insize = PyBytes_GET_SIZE(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4145 | *inend = *input + insize; |
Walter Dörwald | 36f938f | 2007-08-10 10:11:43 +0000 | [diff] [blame] | 4146 | /* we can DECREF safely, as the exception has another reference, |
| 4147 | so the object won't go away. */ |
| 4148 | Py_DECREF(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4149 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4150 | if (newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4151 | newpos = insize+newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4152 | if (newpos<0 || newpos>insize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4153 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", newpos); |
| 4154 | goto onError; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4155 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4156 | |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 4157 | if (_PyUnicode_KIND(*output) != PyUnicode_WCHAR_KIND) { |
| 4158 | /* need more space? (at least enough for what we |
| 4159 | have+the replacement+the rest of the string (starting |
| 4160 | at the new input position), so we won't have to check space |
| 4161 | when there are no errors in the rest of the string) */ |
| 4162 | Py_ssize_t replen = PyUnicode_GET_LENGTH(repunicode); |
| 4163 | requiredsize = *outpos + replen + insize-newpos; |
| 4164 | if (requiredsize > outsize) { |
| 4165 | if (requiredsize<2*outsize) |
| 4166 | requiredsize = 2*outsize; |
| 4167 | if (unicode_resize(output, requiredsize) < 0) |
| 4168 | goto onError; |
| 4169 | } |
Victor Stinner | 1b487b4 | 2012-05-03 12:29:04 +0200 | [diff] [blame] | 4170 | if (unicode_widen(output, *outpos, |
| 4171 | PyUnicode_MAX_CHAR_VALUE(repunicode)) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4172 | goto onError; |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 4173 | copy_characters(*output, *outpos, repunicode, 0, replen); |
| 4174 | *outpos += replen; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4175 | } |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 4176 | else { |
| 4177 | wchar_t *repwstr; |
| 4178 | Py_ssize_t repwlen; |
| 4179 | repwstr = PyUnicode_AsUnicodeAndSize(repunicode, &repwlen); |
| 4180 | if (repwstr == NULL) |
| 4181 | goto onError; |
| 4182 | /* need more space? (at least enough for what we |
| 4183 | have+the replacement+the rest of the string (starting |
| 4184 | at the new input position), so we won't have to check space |
| 4185 | when there are no errors in the rest of the string) */ |
| 4186 | requiredsize = *outpos + repwlen + insize-newpos; |
| 4187 | if (requiredsize > outsize) { |
| 4188 | if (requiredsize < 2*outsize) |
| 4189 | requiredsize = 2*outsize; |
| 4190 | if (unicode_resize(output, requiredsize) < 0) |
| 4191 | goto onError; |
| 4192 | } |
| 4193 | wcsncpy(_PyUnicode_WSTR(*output) + *outpos, repwstr, repwlen); |
| 4194 | *outpos += repwlen; |
| 4195 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4196 | *endinpos = newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4197 | *inptr = *input + newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4198 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4199 | /* we made it! */ |
| 4200 | res = 0; |
| 4201 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4202 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4203 | Py_XDECREF(restuple); |
| 4204 | return res; |
| 4205 | } |
| 4206 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4207 | /* --- UTF-7 Codec -------------------------------------------------------- */ |
| 4208 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4209 | /* See RFC2152 for details. We encode conservatively and decode liberally. */ |
| 4210 | |
| 4211 | /* Three simple macros defining base-64. */ |
| 4212 | |
| 4213 | /* Is c a base-64 character? */ |
| 4214 | |
| 4215 | #define IS_BASE64(c) \ |
| 4216 | (((c) >= 'A' && (c) <= 'Z') || \ |
| 4217 | ((c) >= 'a' && (c) <= 'z') || \ |
| 4218 | ((c) >= '0' && (c) <= '9') || \ |
| 4219 | (c) == '+' || (c) == '/') |
| 4220 | |
| 4221 | /* given that c is a base-64 character, what is its base-64 value? */ |
| 4222 | |
| 4223 | #define FROM_BASE64(c) \ |
| 4224 | (((c) >= 'A' && (c) <= 'Z') ? (c) - 'A' : \ |
| 4225 | ((c) >= 'a' && (c) <= 'z') ? (c) - 'a' + 26 : \ |
| 4226 | ((c) >= '0' && (c) <= '9') ? (c) - '0' + 52 : \ |
| 4227 | (c) == '+' ? 62 : 63) |
| 4228 | |
| 4229 | /* What is the base-64 character of the bottom 6 bits of n? */ |
| 4230 | |
| 4231 | #define TO_BASE64(n) \ |
| 4232 | ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) |
| 4233 | |
| 4234 | /* DECODE_DIRECT: this byte encountered in a UTF-7 string should be |
| 4235 | * decoded as itself. We are permissive on decoding; the only ASCII |
| 4236 | * byte not decoding to itself is the + which begins a base64 |
| 4237 | * string. */ |
| 4238 | |
| 4239 | #define DECODE_DIRECT(c) \ |
| 4240 | ((c) <= 127 && (c) != '+') |
| 4241 | |
| 4242 | /* The UTF-7 encoder treats ASCII characters differently according to |
| 4243 | * whether they are Set D, Set O, Whitespace, or special (i.e. none of |
| 4244 | * the above). See RFC2152. This array identifies these different |
| 4245 | * sets: |
| 4246 | * 0 : "Set D" |
| 4247 | * alphanumeric and '(),-./:? |
| 4248 | * 1 : "Set O" |
| 4249 | * !"#$%&*;<=>@[]^_`{|} |
| 4250 | * 2 : "whitespace" |
| 4251 | * ht nl cr sp |
| 4252 | * 3 : special (must be base64 encoded) |
| 4253 | * everything else (i.e. +\~ and non-printing codes 0-8 11-12 14-31 127) |
| 4254 | */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4255 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4256 | static |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4257 | char utf7_category[128] = { |
| 4258 | /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */ |
| 4259 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 3, 3, 2, 3, 3, |
| 4260 | /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */ |
| 4261 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 4262 | /* sp ! " # $ % & ' ( ) * + , - . / */ |
| 4263 | 2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 3, 0, 0, 0, 0, |
| 4264 | /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */ |
| 4265 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, |
| 4266 | /* @ A B C D E F G H I J K L M N O */ |
| 4267 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4268 | /* P Q R S T U V W X Y Z [ \ ] ^ _ */ |
| 4269 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 1, 1, 1, |
| 4270 | /* ` a b c d e f g h i j k l m n o */ |
| 4271 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4272 | /* p q r s t u v w x y z { | } ~ del */ |
| 4273 | 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] | 4274 | }; |
| 4275 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4276 | /* ENCODE_DIRECT: this character should be encoded as itself. The |
| 4277 | * answer depends on whether we are encoding set O as itself, and also |
| 4278 | * on whether we are encoding whitespace as itself. RFC2152 makes it |
| 4279 | * clear that the answers to these questions vary between |
| 4280 | * applications, so this code needs to be flexible. */ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 4281 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4282 | #define ENCODE_DIRECT(c, directO, directWS) \ |
| 4283 | ((c) < 128 && (c) > 0 && \ |
| 4284 | ((utf7_category[(c)] == 0) || \ |
| 4285 | (directWS && (utf7_category[(c)] == 2)) || \ |
| 4286 | (directO && (utf7_category[(c)] == 1)))) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4287 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4288 | PyObject * |
| 4289 | PyUnicode_DecodeUTF7(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4290 | Py_ssize_t size, |
| 4291 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4292 | { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4293 | return PyUnicode_DecodeUTF7Stateful(s, size, errors, NULL); |
| 4294 | } |
| 4295 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4296 | /* The decoder. The only state we preserve is our read position, |
| 4297 | * i.e. how many characters we have consumed. So if we end in the |
| 4298 | * middle of a shift sequence we have to back off the read position |
| 4299 | * and the output to the beginning of the sequence, otherwise we lose |
| 4300 | * all the shift state (seen bits, number of bits seen, high |
| 4301 | * surrogate). */ |
| 4302 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4303 | PyObject * |
| 4304 | PyUnicode_DecodeUTF7Stateful(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4305 | Py_ssize_t size, |
| 4306 | const char *errors, |
| 4307 | Py_ssize_t *consumed) |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4308 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4309 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4310 | Py_ssize_t startinpos; |
| 4311 | Py_ssize_t endinpos; |
| 4312 | Py_ssize_t outpos; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4313 | const char *e; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4314 | PyObject *unicode; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4315 | const char *errmsg = ""; |
| 4316 | int inShift = 0; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4317 | Py_ssize_t shiftOutStart; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4318 | unsigned int base64bits = 0; |
| 4319 | unsigned long base64buffer = 0; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 4320 | Py_UCS4 surrogate = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4321 | PyObject *errorHandler = NULL; |
| 4322 | PyObject *exc = NULL; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4323 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4324 | /* Start off assuming it's all ASCII. Widen later as necessary. */ |
| 4325 | unicode = PyUnicode_New(size, 127); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4326 | if (!unicode) |
| 4327 | return NULL; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4328 | if (size == 0) { |
| 4329 | if (consumed) |
| 4330 | *consumed = 0; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4331 | return unicode; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4332 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4333 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4334 | shiftOutStart = outpos = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4335 | e = s + size; |
| 4336 | |
| 4337 | while (s < e) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4338 | Py_UCS4 ch; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4339 | restart: |
Antoine Pitrou | 5ffd9e9 | 2008-07-25 18:05:24 +0000 | [diff] [blame] | 4340 | ch = (unsigned char) *s; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4341 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4342 | if (inShift) { /* in a base-64 section */ |
| 4343 | if (IS_BASE64(ch)) { /* consume a base-64 character */ |
| 4344 | base64buffer = (base64buffer << 6) | FROM_BASE64(ch); |
| 4345 | base64bits += 6; |
| 4346 | s++; |
| 4347 | if (base64bits >= 16) { |
| 4348 | /* we have enough bits for a UTF-16 value */ |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 4349 | Py_UCS4 outCh = (Py_UCS4)(base64buffer >> (base64bits-16)); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4350 | base64bits -= 16; |
| 4351 | base64buffer &= (1 << base64bits) - 1; /* clear high bits */ |
| 4352 | if (surrogate) { |
| 4353 | /* expecting a second surrogate */ |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 4354 | if (Py_UNICODE_IS_LOW_SURROGATE(outCh)) { |
| 4355 | Py_UCS4 ch2 = Py_UNICODE_JOIN_SURROGATES(surrogate, outCh); |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4356 | if (unicode_putchar(&unicode, &outpos, ch2) < 0) |
| 4357 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4358 | surrogate = 0; |
Antoine Pitrou | 5418ee0 | 2011-11-15 01:42:21 +0100 | [diff] [blame] | 4359 | continue; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4360 | } |
| 4361 | else { |
Antoine Pitrou | 78edf75 | 2011-11-15 01:44:16 +0100 | [diff] [blame] | 4362 | if (unicode_putchar(&unicode, &outpos, surrogate) < 0) |
| 4363 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4364 | surrogate = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4365 | } |
| 4366 | } |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 4367 | if (Py_UNICODE_IS_HIGH_SURROGATE(outCh)) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4368 | /* first surrogate */ |
| 4369 | surrogate = outCh; |
| 4370 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4371 | else { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4372 | if (unicode_putchar(&unicode, &outpos, outCh) < 0) |
| 4373 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4374 | } |
| 4375 | } |
| 4376 | } |
| 4377 | else { /* now leaving a base-64 section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4378 | inShift = 0; |
| 4379 | s++; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4380 | if (surrogate) { |
Antoine Pitrou | 78edf75 | 2011-11-15 01:44:16 +0100 | [diff] [blame] | 4381 | if (unicode_putchar(&unicode, &outpos, surrogate) < 0) |
| 4382 | goto onError; |
Antoine Pitrou | 5418ee0 | 2011-11-15 01:42:21 +0100 | [diff] [blame] | 4383 | surrogate = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4384 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4385 | if (base64bits > 0) { /* left-over bits */ |
| 4386 | if (base64bits >= 6) { |
| 4387 | /* We've seen at least one base-64 character */ |
| 4388 | errmsg = "partial character in shift sequence"; |
| 4389 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4390 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4391 | else { |
| 4392 | /* Some bits remain; they should be zero */ |
| 4393 | if (base64buffer != 0) { |
| 4394 | errmsg = "non-zero padding bits in shift sequence"; |
| 4395 | goto utf7Error; |
| 4396 | } |
| 4397 | } |
| 4398 | } |
| 4399 | if (ch != '-') { |
| 4400 | /* '-' is absorbed; other terminating |
| 4401 | characters are preserved */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4402 | if (unicode_putchar(&unicode, &outpos, ch) < 0) |
| 4403 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4404 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4405 | } |
| 4406 | } |
| 4407 | else if ( ch == '+' ) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4408 | startinpos = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4409 | s++; /* consume '+' */ |
| 4410 | if (s < e && *s == '-') { /* '+-' encodes '+' */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4411 | s++; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4412 | if (unicode_putchar(&unicode, &outpos, '+') < 0) |
| 4413 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4414 | } |
| 4415 | else { /* begin base64-encoded section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4416 | inShift = 1; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4417 | shiftOutStart = outpos; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4418 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4419 | } |
| 4420 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4421 | else if (DECODE_DIRECT(ch)) { /* character decodes as itself */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4422 | if (unicode_putchar(&unicode, &outpos, ch) < 0) |
| 4423 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4424 | s++; |
| 4425 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4426 | else { |
| 4427 | startinpos = s-starts; |
| 4428 | s++; |
| 4429 | errmsg = "unexpected special character"; |
| 4430 | goto utf7Error; |
| 4431 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4432 | continue; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4433 | utf7Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4434 | endinpos = s-starts; |
| 4435 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4436 | errors, &errorHandler, |
| 4437 | "utf7", errmsg, |
| 4438 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4439 | &unicode, &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4440 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4441 | } |
| 4442 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4443 | /* end of string */ |
| 4444 | |
| 4445 | if (inShift && !consumed) { /* in shift sequence, no more to follow */ |
| 4446 | /* if we're in an inconsistent state, that's an error */ |
| 4447 | if (surrogate || |
| 4448 | (base64bits >= 6) || |
| 4449 | (base64bits > 0 && base64buffer != 0)) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4450 | endinpos = size; |
| 4451 | if (unicode_decode_call_errorhandler( |
| 4452 | errors, &errorHandler, |
| 4453 | "utf7", "unterminated shift sequence", |
| 4454 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4455 | &unicode, &outpos)) |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4456 | goto onError; |
| 4457 | if (s < e) |
| 4458 | goto restart; |
| 4459 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4460 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4461 | |
| 4462 | /* return state */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4463 | if (consumed) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4464 | if (inShift) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4465 | outpos = shiftOutStart; /* back off output */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4466 | *consumed = startinpos; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4467 | } |
| 4468 | else { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4469 | *consumed = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4470 | } |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4471 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4472 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4473 | if (unicode_resize(&unicode, outpos) < 0) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4474 | goto onError; |
| 4475 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4476 | Py_XDECREF(errorHandler); |
| 4477 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 4478 | return unicode_result(unicode); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4479 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4480 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4481 | Py_XDECREF(errorHandler); |
| 4482 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4483 | Py_DECREF(unicode); |
| 4484 | return NULL; |
| 4485 | } |
| 4486 | |
| 4487 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4488 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4489 | _PyUnicode_EncodeUTF7(PyObject *str, |
| 4490 | int base64SetO, |
| 4491 | int base64WhiteSpace, |
| 4492 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4493 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4494 | int kind; |
| 4495 | void *data; |
| 4496 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4497 | PyObject *v; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4498 | Py_ssize_t allocated; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4499 | int inShift = 0; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4500 | Py_ssize_t i; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4501 | unsigned int base64bits = 0; |
| 4502 | unsigned long base64buffer = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4503 | char * out; |
| 4504 | char * start; |
| 4505 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 4506 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4507 | return NULL; |
| 4508 | kind = PyUnicode_KIND(str); |
| 4509 | data = PyUnicode_DATA(str); |
| 4510 | len = PyUnicode_GET_LENGTH(str); |
| 4511 | |
| 4512 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4513 | return PyBytes_FromStringAndSize(NULL, 0); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4514 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4515 | /* It might be possible to tighten this worst case */ |
| 4516 | allocated = 8 * len; |
| 4517 | if (allocated / 8 != len) |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4518 | return PyErr_NoMemory(); |
| 4519 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4520 | v = PyBytes_FromStringAndSize(NULL, allocated); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4521 | if (v == NULL) |
| 4522 | return NULL; |
| 4523 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4524 | start = out = PyBytes_AS_STRING(v); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4525 | for (i = 0; i < len; ++i) { |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 4526 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4527 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4528 | if (inShift) { |
| 4529 | if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 4530 | /* shifting out */ |
| 4531 | if (base64bits) { /* output remaining bits */ |
| 4532 | *out++ = TO_BASE64(base64buffer << (6-base64bits)); |
| 4533 | base64buffer = 0; |
| 4534 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4535 | } |
| 4536 | inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4537 | /* Characters not in the BASE64 set implicitly unshift the sequence |
| 4538 | so no '-' is required, except if the character is itself a '-' */ |
| 4539 | if (IS_BASE64(ch) || ch == '-') { |
| 4540 | *out++ = '-'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4541 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4542 | *out++ = (char) ch; |
| 4543 | } |
| 4544 | else { |
| 4545 | goto encode_char; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4546 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4547 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4548 | else { /* not in a shift sequence */ |
| 4549 | if (ch == '+') { |
| 4550 | *out++ = '+'; |
| 4551 | *out++ = '-'; |
| 4552 | } |
| 4553 | else if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 4554 | *out++ = (char) ch; |
| 4555 | } |
| 4556 | else { |
| 4557 | *out++ = '+'; |
| 4558 | inShift = 1; |
| 4559 | goto encode_char; |
| 4560 | } |
| 4561 | } |
| 4562 | continue; |
| 4563 | encode_char: |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4564 | if (ch >= 0x10000) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 4565 | assert(ch <= MAX_UNICODE); |
Victor Stinner | 0d3721d | 2011-11-22 03:27:53 +0100 | [diff] [blame] | 4566 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4567 | /* code first surrogate */ |
| 4568 | base64bits += 16; |
| 4569 | base64buffer = (base64buffer << 16) | 0xd800 | ((ch-0x10000) >> 10); |
| 4570 | while (base64bits >= 6) { |
| 4571 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 4572 | base64bits -= 6; |
| 4573 | } |
| 4574 | /* prepare second surrogate */ |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 4575 | ch = Py_UNICODE_LOW_SURROGATE(ch); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4576 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4577 | base64bits += 16; |
| 4578 | base64buffer = (base64buffer << 16) | ch; |
| 4579 | while (base64bits >= 6) { |
| 4580 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 4581 | base64bits -= 6; |
| 4582 | } |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 4583 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4584 | if (base64bits) |
| 4585 | *out++= TO_BASE64(base64buffer << (6-base64bits) ); |
| 4586 | if (inShift) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4587 | *out++ = '-'; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4588 | if (_PyBytes_Resize(&v, out - start) < 0) |
| 4589 | return NULL; |
| 4590 | return v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4591 | } |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4592 | PyObject * |
| 4593 | PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
| 4594 | Py_ssize_t size, |
| 4595 | int base64SetO, |
| 4596 | int base64WhiteSpace, |
| 4597 | const char *errors) |
| 4598 | { |
| 4599 | PyObject *result; |
| 4600 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 4601 | if (tmp == NULL) |
| 4602 | return NULL; |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 4603 | result = _PyUnicode_EncodeUTF7(tmp, base64SetO, |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4604 | base64WhiteSpace, errors); |
| 4605 | Py_DECREF(tmp); |
| 4606 | return result; |
| 4607 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4608 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4609 | #undef IS_BASE64 |
| 4610 | #undef FROM_BASE64 |
| 4611 | #undef TO_BASE64 |
| 4612 | #undef DECODE_DIRECT |
| 4613 | #undef ENCODE_DIRECT |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4614 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4615 | /* --- UTF-8 Codec -------------------------------------------------------- */ |
| 4616 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4617 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4618 | char utf8_code_length[256] = { |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4619 | /* Map UTF-8 encoded prefix byte to sequence length. Zero means |
| 4620 | illegal prefix. See RFC 3629 for details */ |
| 4621 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 00-0F */ |
| 4622 | 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] | 4623 | 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] | 4624 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 4625 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 4626 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 4627 | 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] | 4628 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 70-7F */ |
| 4629 | 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] | 4630 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4631 | 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] | 4632 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* B0-BF */ |
| 4633 | 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* C0-C1 + C2-CF */ |
| 4634 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* D0-DF */ |
| 4635 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* E0-EF */ |
| 4636 | 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] | 4637 | }; |
| 4638 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4639 | PyObject * |
| 4640 | PyUnicode_DecodeUTF8(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4641 | Py_ssize_t size, |
| 4642 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4643 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4644 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
| 4645 | } |
| 4646 | |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4647 | #include "stringlib/ucs1lib.h" |
| 4648 | #include "stringlib/codecs.h" |
| 4649 | #include "stringlib/undef.h" |
| 4650 | |
| 4651 | #include "stringlib/ucs2lib.h" |
| 4652 | #include "stringlib/codecs.h" |
| 4653 | #include "stringlib/undef.h" |
| 4654 | |
| 4655 | #include "stringlib/ucs4lib.h" |
| 4656 | #include "stringlib/codecs.h" |
| 4657 | #include "stringlib/undef.h" |
| 4658 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4659 | /* Mask to check or force alignment of a pointer to C 'long' boundaries */ |
| 4660 | #define LONG_PTR_MASK (size_t) (SIZEOF_LONG - 1) |
| 4661 | |
| 4662 | /* Mask to quickly check whether a C 'long' contains a |
| 4663 | non-ASCII, UTF8-encoded char. */ |
| 4664 | #if (SIZEOF_LONG == 8) |
| 4665 | # define ASCII_CHAR_MASK 0x8080808080808080L |
| 4666 | #elif (SIZEOF_LONG == 4) |
| 4667 | # define ASCII_CHAR_MASK 0x80808080L |
| 4668 | #else |
| 4669 | # error C 'long' size should be either 4 or 8! |
| 4670 | #endif |
| 4671 | |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4672 | /* Scans a UTF-8 string and returns the maximum character to be expected |
| 4673 | and the size of the decoded unicode string. |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4674 | |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4675 | 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] | 4676 | PyUnicode_DecodeUTF8Stateful. |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4677 | */ |
| 4678 | static Py_UCS4 |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 4679 | 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] | 4680 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4681 | Py_ssize_t char_count = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4682 | const unsigned char *end = p + string_size; |
| 4683 | 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] | 4684 | |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4685 | assert(unicode_size != NULL); |
| 4686 | |
| 4687 | /* By having a cascade of independent loops which fallback onto each |
| 4688 | other, we minimize the amount of work done in the average loop |
| 4689 | iteration, and we also maximize the CPU's ability to predict |
| 4690 | branches correctly (because a given condition will have always the |
| 4691 | same boolean outcome except perhaps in the last iteration of the |
| 4692 | corresponding loop). |
| 4693 | In the general case this brings us rather close to decoding |
| 4694 | performance pre-PEP 393, despite the two-pass decoding. |
| 4695 | |
| 4696 | Note that the pure ASCII loop is not duplicated once a non-ASCII |
| 4697 | character has been encountered. It is actually a pessimization (by |
| 4698 | a significant factor) to use this loop on text with many non-ASCII |
| 4699 | characters, and it is important to avoid bad performance on valid |
| 4700 | utf-8 data (invalid utf-8 being a different can of worms). |
| 4701 | */ |
| 4702 | |
| 4703 | /* ASCII */ |
| 4704 | for (; p < end; ++p) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4705 | /* Only check value if it's not a ASCII char... */ |
| 4706 | if (*p < 0x80) { |
| 4707 | /* Fast path, see below in PyUnicode_DecodeUTF8Stateful for |
| 4708 | an explanation. */ |
| 4709 | if (!((size_t) p & LONG_PTR_MASK)) { |
| 4710 | /* Help register allocation */ |
| 4711 | register const unsigned char *_p = p; |
| 4712 | while (_p < aligned_end) { |
| 4713 | unsigned long value = *(unsigned long *) _p; |
| 4714 | if (value & ASCII_CHAR_MASK) |
| 4715 | break; |
| 4716 | _p += SIZEOF_LONG; |
| 4717 | char_count += SIZEOF_LONG; |
| 4718 | } |
| 4719 | p = _p; |
| 4720 | if (p == end) |
| 4721 | break; |
| 4722 | } |
| 4723 | } |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4724 | if (*p < 0x80) |
| 4725 | ++char_count; |
| 4726 | else |
| 4727 | goto _ucs1loop; |
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 | *unicode_size = char_count; |
| 4730 | return 127; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4731 | |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4732 | _ucs1loop: |
| 4733 | for (; p < end; ++p) { |
| 4734 | if (*p < 0xc4) |
| 4735 | char_count += ((*p & 0xc0) != 0x80); |
| 4736 | else |
| 4737 | goto _ucs2loop; |
| 4738 | } |
| 4739 | *unicode_size = char_count; |
| 4740 | return 255; |
| 4741 | |
| 4742 | _ucs2loop: |
| 4743 | for (; p < end; ++p) { |
| 4744 | if (*p < 0xf0) |
| 4745 | char_count += ((*p & 0xc0) != 0x80); |
| 4746 | else |
| 4747 | goto _ucs4loop; |
| 4748 | } |
| 4749 | *unicode_size = char_count; |
| 4750 | return 65535; |
| 4751 | |
| 4752 | _ucs4loop: |
| 4753 | for (; p < end; ++p) { |
| 4754 | char_count += ((*p & 0xc0) != 0x80); |
| 4755 | } |
| 4756 | *unicode_size = char_count; |
| 4757 | return 65537; |
| 4758 | } |
| 4759 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4760 | /* 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] | 4761 | in case of errors. Implicit parameters: unicode, kind, data, onError. |
| 4762 | 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] | 4763 | */ |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4764 | #define WRITE_MAYBE_FAIL(index, value) \ |
| 4765 | do { \ |
| 4766 | Py_ssize_t pos = index; \ |
| 4767 | if (pos > PyUnicode_GET_LENGTH(unicode) && \ |
| 4768 | unicode_resize(&unicode, pos + pos/8) < 0) \ |
| 4769 | goto onError; \ |
| 4770 | if (unicode_putchar(&unicode, &pos, value) < 0) \ |
| 4771 | goto onError; \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4772 | } while (0) |
| 4773 | |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 4774 | static PyObject * |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4775 | decode_utf8_errors(const char *starts, |
| 4776 | Py_ssize_t size, |
| 4777 | const char *errors, |
| 4778 | Py_ssize_t *consumed, |
| 4779 | const char *s, |
| 4780 | PyObject *unicode, |
| 4781 | Py_ssize_t i) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4782 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4783 | int n; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4784 | int k; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4785 | Py_ssize_t startinpos; |
| 4786 | Py_ssize_t endinpos; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4787 | const char *e = starts + size; |
| 4788 | const char *aligned_end; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4789 | const char *errmsg = ""; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4790 | PyObject *errorHandler = NULL; |
| 4791 | PyObject *exc = NULL; |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4792 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4793 | aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4794 | |
| 4795 | while (s < e) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 4796 | Py_UCS4 ch = (unsigned char)*s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4797 | |
| 4798 | if (ch < 0x80) { |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4799 | /* Fast path for runs of ASCII characters. Given that common UTF-8 |
| 4800 | input will consist of an overwhelming majority of ASCII |
| 4801 | characters, we try to optimize for this case by checking |
| 4802 | as many characters as a C 'long' can contain. |
| 4803 | First, check if we can do an aligned read, as most CPUs have |
| 4804 | a penalty for unaligned reads. |
| 4805 | */ |
| 4806 | if (!((size_t) s & LONG_PTR_MASK)) { |
| 4807 | /* Help register allocation */ |
| 4808 | register const char *_s = s; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4809 | register Py_ssize_t _i = i; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4810 | while (_s < aligned_end) { |
| 4811 | /* Read a whole long at a time (either 4 or 8 bytes), |
| 4812 | and do a fast unrolled copy if it only contains ASCII |
| 4813 | characters. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4814 | unsigned long value = *(unsigned long *) _s; |
| 4815 | if (value & ASCII_CHAR_MASK) |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4816 | break; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4817 | WRITE_MAYBE_FAIL(_i+0, _s[0]); |
| 4818 | WRITE_MAYBE_FAIL(_i+1, _s[1]); |
| 4819 | WRITE_MAYBE_FAIL(_i+2, _s[2]); |
| 4820 | WRITE_MAYBE_FAIL(_i+3, _s[3]); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4821 | #if (SIZEOF_LONG == 8) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4822 | WRITE_MAYBE_FAIL(_i+4, _s[4]); |
| 4823 | WRITE_MAYBE_FAIL(_i+5, _s[5]); |
| 4824 | WRITE_MAYBE_FAIL(_i+6, _s[6]); |
| 4825 | WRITE_MAYBE_FAIL(_i+7, _s[7]); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4826 | #endif |
| 4827 | _s += SIZEOF_LONG; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4828 | _i += SIZEOF_LONG; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4829 | } |
| 4830 | s = _s; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4831 | i = _i; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4832 | if (s == e) |
| 4833 | break; |
| 4834 | ch = (unsigned char)*s; |
| 4835 | } |
| 4836 | } |
| 4837 | |
| 4838 | if (ch < 0x80) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4839 | WRITE_MAYBE_FAIL(i++, ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4840 | s++; |
| 4841 | continue; |
| 4842 | } |
| 4843 | |
| 4844 | n = utf8_code_length[ch]; |
| 4845 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4846 | if (s + n > e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4847 | if (consumed) |
| 4848 | break; |
| 4849 | else { |
| 4850 | errmsg = "unexpected end of data"; |
| 4851 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4852 | endinpos = startinpos+1; |
| 4853 | for (k=1; (k < size-startinpos) && ((s[k]&0xC0) == 0x80); k++) |
| 4854 | endinpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4855 | goto utf8Error; |
| 4856 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4857 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4858 | |
| 4859 | switch (n) { |
| 4860 | |
| 4861 | case 0: |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4862 | errmsg = "invalid start byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4863 | startinpos = s-starts; |
| 4864 | endinpos = startinpos+1; |
| 4865 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4866 | |
| 4867 | case 1: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4868 | errmsg = "internal error"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4869 | startinpos = s-starts; |
| 4870 | endinpos = startinpos+1; |
| 4871 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4872 | |
| 4873 | case 2: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4874 | if ((s[1] & 0xc0) != 0x80) { |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4875 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4876 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4877 | endinpos = startinpos + 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4878 | goto utf8Error; |
| 4879 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4880 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4881 | assert ((ch > 0x007F) && (ch <= 0x07FF)); |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4882 | WRITE_MAYBE_FAIL(i++, ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4883 | break; |
| 4884 | |
| 4885 | case 3: |
Ezio Melotti | 9bf2b3a | 2010-07-03 04:52:19 +0000 | [diff] [blame] | 4886 | /* Decoding UTF-8 sequences in range \xed\xa0\x80-\xed\xbf\xbf |
| 4887 | will result in surrogates in range d800-dfff. Surrogates are |
| 4888 | not valid UTF-8 so they are rejected. |
| 4889 | See http://www.unicode.org/versions/Unicode5.2.0/ch03.pdf |
| 4890 | (table 3-7) and http://www.rfc-editor.org/rfc/rfc3629.txt */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4891 | if ((s[1] & 0xc0) != 0x80 || |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4892 | (s[2] & 0xc0) != 0x80 || |
| 4893 | ((unsigned char)s[0] == 0xE0 && |
| 4894 | (unsigned char)s[1] < 0xA0) || |
| 4895 | ((unsigned char)s[0] == 0xED && |
| 4896 | (unsigned char)s[1] > 0x9F)) { |
| 4897 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4898 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4899 | endinpos = startinpos + 1; |
| 4900 | |
| 4901 | /* if s[1] first two bits are 1 and 0, then the invalid |
| 4902 | continuation byte is s[2], so increment endinpos by 1, |
| 4903 | if not, s[1] is invalid and endinpos doesn't need to |
| 4904 | be incremented. */ |
| 4905 | if ((s[1] & 0xC0) == 0x80) |
| 4906 | endinpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4907 | goto utf8Error; |
| 4908 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4909 | ch = ((s[0] & 0x0f) << 12) + ((s[1] & 0x3f) << 6) + (s[2] & 0x3f); |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4910 | assert ((ch > 0x07FF) && (ch <= 0xFFFF)); |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4911 | WRITE_MAYBE_FAIL(i++, ch); |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 4912 | break; |
| 4913 | |
| 4914 | case 4: |
| 4915 | if ((s[1] & 0xc0) != 0x80 || |
| 4916 | (s[2] & 0xc0) != 0x80 || |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4917 | (s[3] & 0xc0) != 0x80 || |
| 4918 | ((unsigned char)s[0] == 0xF0 && |
| 4919 | (unsigned char)s[1] < 0x90) || |
| 4920 | ((unsigned char)s[0] == 0xF4 && |
| 4921 | (unsigned char)s[1] > 0x8F)) { |
| 4922 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4923 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4924 | endinpos = startinpos + 1; |
| 4925 | if ((s[1] & 0xC0) == 0x80) { |
| 4926 | endinpos++; |
| 4927 | if ((s[2] & 0xC0) == 0x80) |
| 4928 | endinpos++; |
| 4929 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4930 | goto utf8Error; |
| 4931 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 4932 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4933 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 4934 | assert ((ch > 0xFFFF) && (ch <= MAX_UNICODE)); |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4935 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4936 | WRITE_MAYBE_FAIL(i++, ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4937 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4938 | } |
| 4939 | s += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4940 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4941 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4942 | utf8Error: |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4943 | if (unicode_decode_call_errorhandler( |
| 4944 | errors, &errorHandler, |
Victor Stinner | cbe0134 | 2012-02-14 01:17:45 +0100 | [diff] [blame] | 4945 | "utf-8", errmsg, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4946 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4947 | &unicode, &i)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4948 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4949 | /* Update data because unicode_decode_call_errorhandler might have |
| 4950 | re-created or resized the unicode object. */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4951 | aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4952 | } |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4953 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4954 | *consumed = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4955 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4956 | /* Adjust length and ready string when it contained errors and |
| 4957 | is of the old resizable kind. */ |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4958 | if (unicode_resize(&unicode, i) < 0) |
| 4959 | goto onError; |
| 4960 | unicode_adjust_maxchar(&unicode); |
| 4961 | if (unicode == NULL) |
| 4962 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4963 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4964 | Py_XDECREF(errorHandler); |
| 4965 | Py_XDECREF(exc); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 4966 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4967 | return unicode; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4968 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4969 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4970 | Py_XDECREF(errorHandler); |
| 4971 | Py_XDECREF(exc); |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4972 | Py_XDECREF(unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4973 | return NULL; |
| 4974 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4975 | #undef WRITE_MAYBE_FAIL |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4976 | |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4977 | PyObject * |
| 4978 | PyUnicode_DecodeUTF8Stateful(const char *s, |
| 4979 | Py_ssize_t size, |
| 4980 | const char *errors, |
| 4981 | Py_ssize_t *consumed) |
| 4982 | { |
| 4983 | Py_UCS4 maxchar = 0; |
| 4984 | Py_ssize_t unicode_size; |
| 4985 | int has_errors = 0; |
| 4986 | PyObject *unicode; |
| 4987 | int kind; |
| 4988 | void *data; |
| 4989 | const char *starts = s; |
| 4990 | const char *e; |
| 4991 | Py_ssize_t i; |
| 4992 | |
| 4993 | if (size == 0) { |
| 4994 | if (consumed) |
| 4995 | *consumed = 0; |
Victor Stinner | 382955f | 2011-12-11 21:44:00 +0100 | [diff] [blame] | 4996 | Py_INCREF(unicode_empty); |
| 4997 | return unicode_empty; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4998 | } |
| 4999 | |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 5000 | maxchar = utf8_scanner((const unsigned char *)s, size, &unicode_size); |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 5001 | |
| 5002 | /* When the string is ASCII only, just use memcpy and return. |
| 5003 | unicode_size may be != size if there is an incomplete UTF-8 |
| 5004 | sequence at the end of the ASCII block. */ |
| 5005 | if (maxchar < 128 && size == unicode_size) { |
| 5006 | if (consumed) |
| 5007 | *consumed = size; |
Victor Stinner | ab87021 | 2011-12-17 22:39:43 +0100 | [diff] [blame] | 5008 | return unicode_fromascii((const unsigned char *)s, size); |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 5009 | } |
| 5010 | |
| 5011 | unicode = PyUnicode_New(unicode_size, maxchar); |
| 5012 | if (!unicode) |
| 5013 | return NULL; |
| 5014 | kind = PyUnicode_KIND(unicode); |
| 5015 | data = PyUnicode_DATA(unicode); |
| 5016 | |
| 5017 | /* Unpack UTF-8 encoded data */ |
| 5018 | i = 0; |
| 5019 | e = starts + size; |
| 5020 | switch (kind) { |
| 5021 | case PyUnicode_1BYTE_KIND: |
| 5022 | has_errors = ucs1lib_utf8_try_decode(s, e, (Py_UCS1 *) data, &s, &i); |
| 5023 | break; |
| 5024 | case PyUnicode_2BYTE_KIND: |
| 5025 | has_errors = ucs2lib_utf8_try_decode(s, e, (Py_UCS2 *) data, &s, &i); |
| 5026 | break; |
| 5027 | case PyUnicode_4BYTE_KIND: |
| 5028 | has_errors = ucs4lib_utf8_try_decode(s, e, (Py_UCS4 *) data, &s, &i); |
| 5029 | break; |
| 5030 | } |
| 5031 | if (!has_errors) { |
| 5032 | /* Ensure the unicode size calculation was correct */ |
| 5033 | assert(i == unicode_size); |
| 5034 | assert(s == e); |
| 5035 | if (consumed) |
| 5036 | *consumed = size; |
| 5037 | return unicode; |
| 5038 | } |
| 5039 | |
| 5040 | /* In case of errors, maxchar and size computation might be incorrect; |
| 5041 | code below refits and resizes as necessary. */ |
| 5042 | return decode_utf8_errors(starts, size, errors, consumed, s, unicode, i); |
| 5043 | } |
| 5044 | |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 5045 | #ifdef __APPLE__ |
| 5046 | |
| 5047 | /* Simplified UTF-8 decoder using surrogateescape error handler, |
| 5048 | used to decode the command line arguments on Mac OS X. */ |
| 5049 | |
| 5050 | wchar_t* |
| 5051 | _Py_DecodeUTF8_surrogateescape(const char *s, Py_ssize_t size) |
| 5052 | { |
| 5053 | int n; |
| 5054 | const char *e; |
| 5055 | wchar_t *unicode, *p; |
| 5056 | |
| 5057 | /* Note: size will always be longer than the resulting Unicode |
| 5058 | character count */ |
| 5059 | if (PY_SSIZE_T_MAX / sizeof(wchar_t) < (size + 1)) { |
| 5060 | PyErr_NoMemory(); |
| 5061 | return NULL; |
| 5062 | } |
| 5063 | unicode = PyMem_Malloc((size + 1) * sizeof(wchar_t)); |
| 5064 | if (!unicode) |
| 5065 | return NULL; |
| 5066 | |
| 5067 | /* Unpack UTF-8 encoded data */ |
| 5068 | p = unicode; |
| 5069 | e = s + size; |
| 5070 | while (s < e) { |
| 5071 | Py_UCS4 ch = (unsigned char)*s; |
| 5072 | |
| 5073 | if (ch < 0x80) { |
| 5074 | *p++ = (wchar_t)ch; |
| 5075 | s++; |
| 5076 | continue; |
| 5077 | } |
| 5078 | |
| 5079 | n = utf8_code_length[ch]; |
| 5080 | if (s + n > e) { |
| 5081 | goto surrogateescape; |
| 5082 | } |
| 5083 | |
| 5084 | switch (n) { |
| 5085 | case 0: |
| 5086 | case 1: |
| 5087 | goto surrogateescape; |
| 5088 | |
| 5089 | case 2: |
| 5090 | if ((s[1] & 0xc0) != 0x80) |
| 5091 | goto surrogateescape; |
| 5092 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
| 5093 | assert ((ch > 0x007F) && (ch <= 0x07FF)); |
| 5094 | *p++ = (wchar_t)ch; |
| 5095 | break; |
| 5096 | |
| 5097 | case 3: |
| 5098 | /* Decoding UTF-8 sequences in range \xed\xa0\x80-\xed\xbf\xbf |
| 5099 | will result in surrogates in range d800-dfff. Surrogates are |
| 5100 | not valid UTF-8 so they are rejected. |
| 5101 | See http://www.unicode.org/versions/Unicode5.2.0/ch03.pdf |
| 5102 | (table 3-7) and http://www.rfc-editor.org/rfc/rfc3629.txt */ |
| 5103 | if ((s[1] & 0xc0) != 0x80 || |
| 5104 | (s[2] & 0xc0) != 0x80 || |
| 5105 | ((unsigned char)s[0] == 0xE0 && |
| 5106 | (unsigned char)s[1] < 0xA0) || |
| 5107 | ((unsigned char)s[0] == 0xED && |
| 5108 | (unsigned char)s[1] > 0x9F)) { |
| 5109 | |
| 5110 | goto surrogateescape; |
| 5111 | } |
| 5112 | ch = ((s[0] & 0x0f) << 12) + ((s[1] & 0x3f) << 6) + (s[2] & 0x3f); |
| 5113 | assert ((ch > 0x07FF) && (ch <= 0xFFFF)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5114 | *p++ = (wchar_t)ch; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 5115 | break; |
| 5116 | |
| 5117 | case 4: |
| 5118 | if ((s[1] & 0xc0) != 0x80 || |
| 5119 | (s[2] & 0xc0) != 0x80 || |
| 5120 | (s[3] & 0xc0) != 0x80 || |
| 5121 | ((unsigned char)s[0] == 0xF0 && |
| 5122 | (unsigned char)s[1] < 0x90) || |
| 5123 | ((unsigned char)s[0] == 0xF4 && |
| 5124 | (unsigned char)s[1] > 0x8F)) { |
| 5125 | goto surrogateescape; |
| 5126 | } |
| 5127 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
| 5128 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 5129 | assert ((ch > 0xFFFF) && (ch <= MAX_UNICODE)); |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 5130 | |
| 5131 | #if SIZEOF_WCHAR_T == 4 |
| 5132 | *p++ = (wchar_t)ch; |
| 5133 | #else |
| 5134 | /* compute and append the two surrogates: */ |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 5135 | *p++ = (wchar_t)Py_UNICODE_HIGH_SURROGATE(ch); |
| 5136 | *p++ = (wchar_t)Py_UNICODE_LOW_SURROGATE(ch); |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 5137 | #endif |
| 5138 | break; |
| 5139 | } |
| 5140 | s += n; |
| 5141 | continue; |
| 5142 | |
| 5143 | surrogateescape: |
| 5144 | *p++ = 0xDC00 + ch; |
| 5145 | s++; |
| 5146 | } |
| 5147 | *p = L'\0'; |
| 5148 | return unicode; |
| 5149 | } |
| 5150 | |
| 5151 | #endif /* __APPLE__ */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5152 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5153 | /* Primary internal function which creates utf8 encoded bytes objects. |
| 5154 | |
| 5155 | Allocation strategy: if the string is short, convert into a stack buffer |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 5156 | and allocate exactly as much space needed at the end. Else allocate the |
| 5157 | maximum possible needed (4 result bytes per Unicode character), and return |
| 5158 | the excess memory at the end. |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 5159 | */ |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 5160 | PyObject * |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5161 | _PyUnicode_AsUTF8String(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5162 | { |
Victor Stinner | 6099a03 | 2011-12-18 14:22:26 +0100 | [diff] [blame] | 5163 | enum PyUnicode_Kind kind; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5164 | void *data; |
| 5165 | Py_ssize_t size; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 5166 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5167 | if (!PyUnicode_Check(unicode)) { |
| 5168 | PyErr_BadArgument(); |
| 5169 | return NULL; |
| 5170 | } |
| 5171 | |
| 5172 | if (PyUnicode_READY(unicode) == -1) |
| 5173 | return NULL; |
| 5174 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 5175 | if (PyUnicode_UTF8(unicode)) |
| 5176 | return PyBytes_FromStringAndSize(PyUnicode_UTF8(unicode), |
| 5177 | PyUnicode_UTF8_LENGTH(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5178 | |
| 5179 | kind = PyUnicode_KIND(unicode); |
| 5180 | data = PyUnicode_DATA(unicode); |
| 5181 | size = PyUnicode_GET_LENGTH(unicode); |
| 5182 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 5183 | switch (kind) { |
Victor Stinner | 6099a03 | 2011-12-18 14:22:26 +0100 | [diff] [blame] | 5184 | default: |
| 5185 | assert(0); |
| 5186 | case PyUnicode_1BYTE_KIND: |
| 5187 | /* the string cannot be ASCII, or PyUnicode_UTF8() would be set */ |
| 5188 | assert(!PyUnicode_IS_ASCII(unicode)); |
| 5189 | return ucs1lib_utf8_encoder(unicode, data, size, errors); |
| 5190 | case PyUnicode_2BYTE_KIND: |
| 5191 | return ucs2lib_utf8_encoder(unicode, data, size, errors); |
| 5192 | case PyUnicode_4BYTE_KIND: |
| 5193 | return ucs4lib_utf8_encoder(unicode, data, size, errors); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 5194 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5195 | } |
| 5196 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5197 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5198 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
| 5199 | Py_ssize_t size, |
| 5200 | const char *errors) |
| 5201 | { |
| 5202 | PyObject *v, *unicode; |
| 5203 | |
| 5204 | unicode = PyUnicode_FromUnicode(s, size); |
| 5205 | if (unicode == NULL) |
| 5206 | return NULL; |
| 5207 | v = _PyUnicode_AsUTF8String(unicode, errors); |
| 5208 | Py_DECREF(unicode); |
| 5209 | return v; |
| 5210 | } |
| 5211 | |
| 5212 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5213 | PyUnicode_AsUTF8String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5214 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5215 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5216 | } |
| 5217 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5218 | /* --- UTF-32 Codec ------------------------------------------------------- */ |
| 5219 | |
| 5220 | PyObject * |
| 5221 | PyUnicode_DecodeUTF32(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5222 | Py_ssize_t size, |
| 5223 | const char *errors, |
| 5224 | int *byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5225 | { |
| 5226 | return PyUnicode_DecodeUTF32Stateful(s, size, errors, byteorder, NULL); |
| 5227 | } |
| 5228 | |
| 5229 | PyObject * |
| 5230 | PyUnicode_DecodeUTF32Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5231 | Py_ssize_t size, |
| 5232 | const char *errors, |
| 5233 | int *byteorder, |
| 5234 | Py_ssize_t *consumed) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5235 | { |
| 5236 | const char *starts = s; |
| 5237 | Py_ssize_t startinpos; |
| 5238 | Py_ssize_t endinpos; |
| 5239 | Py_ssize_t outpos; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5240 | PyObject *unicode; |
Mark Dickinson | 7db923c | 2010-06-12 09:10:14 +0000 | [diff] [blame] | 5241 | const unsigned char *q, *e; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5242 | int bo = 0; /* assume native ordering by default */ |
| 5243 | const char *errmsg = ""; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5244 | /* Offsets from q for retrieving bytes in the right order. */ |
| 5245 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5246 | int iorder[] = {0, 1, 2, 3}; |
| 5247 | #else |
| 5248 | int iorder[] = {3, 2, 1, 0}; |
| 5249 | #endif |
| 5250 | PyObject *errorHandler = NULL; |
| 5251 | PyObject *exc = NULL; |
Victor Stinner | 313a120 | 2010-06-11 23:56:51 +0000 | [diff] [blame] | 5252 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5253 | q = (unsigned char *)s; |
| 5254 | e = q + size; |
| 5255 | |
| 5256 | if (byteorder) |
| 5257 | bo = *byteorder; |
| 5258 | |
| 5259 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 5260 | byte order setting accordingly. In native mode, the leading BOM |
| 5261 | mark is skipped, in all other modes, it is copied to the output |
| 5262 | stream as-is (giving a ZWNBSP character). */ |
| 5263 | if (bo == 0) { |
| 5264 | if (size >= 4) { |
| 5265 | const Py_UCS4 bom = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5266 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5267 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5268 | if (bom == 0x0000FEFF) { |
| 5269 | q += 4; |
| 5270 | bo = -1; |
| 5271 | } |
| 5272 | else if (bom == 0xFFFE0000) { |
| 5273 | q += 4; |
| 5274 | bo = 1; |
| 5275 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5276 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5277 | if (bom == 0x0000FEFF) { |
| 5278 | q += 4; |
| 5279 | bo = 1; |
| 5280 | } |
| 5281 | else if (bom == 0xFFFE0000) { |
| 5282 | q += 4; |
| 5283 | bo = -1; |
| 5284 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5285 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5286 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5287 | } |
| 5288 | |
| 5289 | if (bo == -1) { |
| 5290 | /* force LE */ |
| 5291 | iorder[0] = 0; |
| 5292 | iorder[1] = 1; |
| 5293 | iorder[2] = 2; |
| 5294 | iorder[3] = 3; |
| 5295 | } |
| 5296 | else if (bo == 1) { |
| 5297 | /* force BE */ |
| 5298 | iorder[0] = 3; |
| 5299 | iorder[1] = 2; |
| 5300 | iorder[2] = 1; |
| 5301 | iorder[3] = 0; |
| 5302 | } |
| 5303 | |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 5304 | /* This might be one to much, because of a BOM */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5305 | unicode = PyUnicode_New((size+3)/4, 127); |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 5306 | if (!unicode) |
| 5307 | return NULL; |
| 5308 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5309 | return unicode; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5310 | outpos = 0; |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 5311 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5312 | while (q < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5313 | Py_UCS4 ch; |
| 5314 | /* remaining bytes at the end? (size should be divisible by 4) */ |
| 5315 | if (e-q<4) { |
| 5316 | if (consumed) |
| 5317 | break; |
| 5318 | errmsg = "truncated data"; |
| 5319 | startinpos = ((const char *)q)-starts; |
| 5320 | endinpos = ((const char *)e)-starts; |
| 5321 | goto utf32Error; |
| 5322 | /* The remaining input chars are ignored if the callback |
| 5323 | chooses to skip the input */ |
| 5324 | } |
| 5325 | ch = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
| 5326 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5327 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5328 | if (ch >= 0x110000) |
| 5329 | { |
| 5330 | errmsg = "codepoint not in range(0x110000)"; |
| 5331 | startinpos = ((const char *)q)-starts; |
| 5332 | endinpos = startinpos+4; |
| 5333 | goto utf32Error; |
| 5334 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5335 | if (unicode_putchar(&unicode, &outpos, ch) < 0) |
| 5336 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5337 | q += 4; |
| 5338 | continue; |
| 5339 | utf32Error: |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5340 | if (unicode_decode_call_errorhandler( |
| 5341 | errors, &errorHandler, |
| 5342 | "utf32", errmsg, |
| 5343 | &starts, (const char **)&e, &startinpos, &endinpos, &exc, (const char **)&q, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5344 | &unicode, &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5345 | goto onError; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5346 | } |
| 5347 | |
| 5348 | if (byteorder) |
| 5349 | *byteorder = bo; |
| 5350 | |
| 5351 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5352 | *consumed = (const char *)q-starts; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5353 | |
| 5354 | /* Adjust length */ |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 5355 | if (unicode_resize(&unicode, outpos) < 0) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5356 | goto onError; |
| 5357 | |
| 5358 | Py_XDECREF(errorHandler); |
| 5359 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 5360 | return unicode_result(unicode); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5361 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5362 | onError: |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5363 | Py_DECREF(unicode); |
| 5364 | Py_XDECREF(errorHandler); |
| 5365 | Py_XDECREF(exc); |
| 5366 | return NULL; |
| 5367 | } |
| 5368 | |
| 5369 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5370 | _PyUnicode_EncodeUTF32(PyObject *str, |
| 5371 | const char *errors, |
| 5372 | int byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5373 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5374 | int kind; |
| 5375 | void *data; |
| 5376 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5377 | PyObject *v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5378 | unsigned char *p; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5379 | Py_ssize_t nsize, bytesize, i; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5380 | /* Offsets from p for storing byte pairs in the right order. */ |
| 5381 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5382 | int iorder[] = {0, 1, 2, 3}; |
| 5383 | #else |
| 5384 | int iorder[] = {3, 2, 1, 0}; |
| 5385 | #endif |
| 5386 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5387 | #define STORECHAR(CH) \ |
| 5388 | do { \ |
| 5389 | p[iorder[3]] = ((CH) >> 24) & 0xff; \ |
| 5390 | p[iorder[2]] = ((CH) >> 16) & 0xff; \ |
| 5391 | p[iorder[1]] = ((CH) >> 8) & 0xff; \ |
| 5392 | p[iorder[0]] = (CH) & 0xff; \ |
| 5393 | p += 4; \ |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5394 | } while(0) |
| 5395 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5396 | if (!PyUnicode_Check(str)) { |
| 5397 | PyErr_BadArgument(); |
| 5398 | return NULL; |
| 5399 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 5400 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5401 | return NULL; |
| 5402 | kind = PyUnicode_KIND(str); |
| 5403 | data = PyUnicode_DATA(str); |
| 5404 | len = PyUnicode_GET_LENGTH(str); |
| 5405 | |
| 5406 | nsize = len + (byteorder == 0); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5407 | bytesize = nsize * 4; |
| 5408 | if (bytesize / 4 != nsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5409 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5410 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5411 | if (v == NULL) |
| 5412 | return NULL; |
| 5413 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5414 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5415 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5416 | STORECHAR(0xFEFF); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5417 | if (len == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5418 | goto done; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5419 | |
| 5420 | if (byteorder == -1) { |
| 5421 | /* force LE */ |
| 5422 | iorder[0] = 0; |
| 5423 | iorder[1] = 1; |
| 5424 | iorder[2] = 2; |
| 5425 | iorder[3] = 3; |
| 5426 | } |
| 5427 | else if (byteorder == 1) { |
| 5428 | /* force BE */ |
| 5429 | iorder[0] = 3; |
| 5430 | iorder[1] = 2; |
| 5431 | iorder[2] = 1; |
| 5432 | iorder[3] = 0; |
| 5433 | } |
| 5434 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5435 | for (i = 0; i < len; i++) |
| 5436 | STORECHAR(PyUnicode_READ(kind, data, i)); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5437 | |
| 5438 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5439 | return v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5440 | #undef STORECHAR |
| 5441 | } |
| 5442 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5443 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5444 | PyUnicode_EncodeUTF32(const Py_UNICODE *s, |
| 5445 | Py_ssize_t size, |
| 5446 | const char *errors, |
| 5447 | int byteorder) |
| 5448 | { |
| 5449 | PyObject *result; |
| 5450 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 5451 | if (tmp == NULL) |
| 5452 | return NULL; |
| 5453 | result = _PyUnicode_EncodeUTF32(tmp, errors, byteorder); |
| 5454 | Py_DECREF(tmp); |
| 5455 | return result; |
| 5456 | } |
| 5457 | |
| 5458 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5459 | PyUnicode_AsUTF32String(PyObject *unicode) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5460 | { |
Victor Stinner | b960b34 | 2011-11-20 19:12:52 +0100 | [diff] [blame] | 5461 | return _PyUnicode_EncodeUTF32(unicode, NULL, 0); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5462 | } |
| 5463 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5464 | /* --- UTF-16 Codec ------------------------------------------------------- */ |
| 5465 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5466 | PyObject * |
| 5467 | PyUnicode_DecodeUTF16(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5468 | Py_ssize_t size, |
| 5469 | const char *errors, |
| 5470 | int *byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5471 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5472 | return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL); |
| 5473 | } |
| 5474 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5475 | /* Two masks for fast checking of whether a C 'long' may contain |
| 5476 | UTF16-encoded surrogate characters. This is an efficient heuristic, |
| 5477 | assuming that non-surrogate characters with a code point >= 0x8000 are |
| 5478 | rare in most input. |
| 5479 | FAST_CHAR_MASK is used when the input is in native byte ordering, |
| 5480 | SWAPPED_FAST_CHAR_MASK when the input is in byteswapped ordering. |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5481 | */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5482 | #if (SIZEOF_LONG == 8) |
| 5483 | # define FAST_CHAR_MASK 0x8000800080008000L |
| 5484 | # define SWAPPED_FAST_CHAR_MASK 0x0080008000800080L |
Victor Stinner | afb5205 | 2012-04-05 22:54:49 +0200 | [diff] [blame] | 5485 | # define STRIPPED_MASK 0x00FF00FF00FF00FFL |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5486 | #elif (SIZEOF_LONG == 4) |
| 5487 | # define FAST_CHAR_MASK 0x80008000L |
| 5488 | # define SWAPPED_FAST_CHAR_MASK 0x00800080L |
Victor Stinner | afb5205 | 2012-04-05 22:54:49 +0200 | [diff] [blame] | 5489 | # define STRIPPED_MASK 0x00FF00FFL |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5490 | #else |
| 5491 | # error C 'long' size should be either 4 or 8! |
| 5492 | #endif |
| 5493 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5494 | PyObject * |
| 5495 | PyUnicode_DecodeUTF16Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5496 | Py_ssize_t size, |
| 5497 | const char *errors, |
| 5498 | int *byteorder, |
| 5499 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5500 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5501 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5502 | Py_ssize_t startinpos; |
| 5503 | Py_ssize_t endinpos; |
| 5504 | Py_ssize_t outpos; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5505 | PyObject *unicode; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5506 | const unsigned char *q, *e, *aligned_end; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5507 | int bo = 0; /* assume native ordering by default */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5508 | int native_ordering = 0; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 5509 | const char *errmsg = ""; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5510 | /* Offsets from q for retrieving byte pairs in the right order. */ |
| 5511 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5512 | int ihi = 1, ilo = 0; |
| 5513 | #else |
| 5514 | int ihi = 0, ilo = 1; |
| 5515 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5516 | PyObject *errorHandler = NULL; |
| 5517 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5518 | |
| 5519 | /* Note: size will always be longer than the resulting Unicode |
| 5520 | character count */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5521 | unicode = PyUnicode_New(size, 127); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5522 | if (!unicode) |
| 5523 | return NULL; |
| 5524 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5525 | return unicode; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5526 | outpos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5527 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5528 | q = (unsigned char *)s; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5529 | e = q + size - 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5530 | |
| 5531 | if (byteorder) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5532 | bo = *byteorder; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5533 | |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5534 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 5535 | byte order setting accordingly. In native mode, the leading BOM |
| 5536 | mark is skipped, in all other modes, it is copied to the output |
| 5537 | stream as-is (giving a ZWNBSP character). */ |
| 5538 | if (bo == 0) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5539 | if (size >= 2) { |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 5540 | const Py_UCS4 bom = (q[ihi] << 8) | q[ilo]; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5541 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5542 | if (bom == 0xFEFF) { |
| 5543 | q += 2; |
| 5544 | bo = -1; |
| 5545 | } |
| 5546 | else if (bom == 0xFFFE) { |
| 5547 | q += 2; |
| 5548 | bo = 1; |
| 5549 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5550 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5551 | if (bom == 0xFEFF) { |
| 5552 | q += 2; |
| 5553 | bo = 1; |
| 5554 | } |
| 5555 | else if (bom == 0xFFFE) { |
| 5556 | q += 2; |
| 5557 | bo = -1; |
| 5558 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5559 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5560 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5561 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5562 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5563 | if (bo == -1) { |
| 5564 | /* force LE */ |
| 5565 | ihi = 1; |
| 5566 | ilo = 0; |
| 5567 | } |
| 5568 | else if (bo == 1) { |
| 5569 | /* force BE */ |
| 5570 | ihi = 0; |
| 5571 | ilo = 1; |
| 5572 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5573 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5574 | native_ordering = ilo < ihi; |
| 5575 | #else |
| 5576 | native_ordering = ilo > ihi; |
| 5577 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5578 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5579 | aligned_end = (const unsigned char *) ((size_t) e & ~LONG_PTR_MASK); |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5580 | while (q < e) { |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 5581 | Py_UCS4 ch; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5582 | /* First check for possible aligned read of a C 'long'. Unaligned |
| 5583 | reads are more expensive, better to defer to another iteration. */ |
| 5584 | if (!((size_t) q & LONG_PTR_MASK)) { |
| 5585 | /* Fast path for runs of non-surrogate chars. */ |
| 5586 | register const unsigned char *_q = q; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5587 | int kind = PyUnicode_KIND(unicode); |
| 5588 | void *data = PyUnicode_DATA(unicode); |
| 5589 | while (_q < aligned_end) { |
Victor Stinner | afb5205 | 2012-04-05 22:54:49 +0200 | [diff] [blame] | 5590 | unsigned long block = * (unsigned long *) _q; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5591 | Py_UCS4 maxch; |
| 5592 | if (native_ordering) { |
| 5593 | /* Can use buffer directly */ |
Victor Stinner | afb5205 | 2012-04-05 22:54:49 +0200 | [diff] [blame] | 5594 | if (block & FAST_CHAR_MASK) |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5595 | break; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5596 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5597 | else { |
| 5598 | /* Need to byte-swap */ |
Victor Stinner | afb5205 | 2012-04-05 22:54:49 +0200 | [diff] [blame] | 5599 | if (block & SWAPPED_FAST_CHAR_MASK) |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5600 | break; |
Victor Stinner | afb5205 | 2012-04-05 22:54:49 +0200 | [diff] [blame] | 5601 | block = ((block >> 8) & STRIPPED_MASK) | |
| 5602 | ((block & STRIPPED_MASK) << 8); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5603 | } |
Victor Stinner | afb5205 | 2012-04-05 22:54:49 +0200 | [diff] [blame] | 5604 | maxch = (Py_UCS2)(block & 0xFFFF); |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5605 | #if SIZEOF_LONG == 8 |
Victor Stinner | afb5205 | 2012-04-05 22:54:49 +0200 | [diff] [blame] | 5606 | ch = (Py_UCS2)((block >> 16) & 0xFFFF); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 5607 | maxch = MAX_MAXCHAR(maxch, ch); |
Victor Stinner | afb5205 | 2012-04-05 22:54:49 +0200 | [diff] [blame] | 5608 | ch = (Py_UCS2)((block >> 32) & 0xFFFF); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 5609 | maxch = MAX_MAXCHAR(maxch, ch); |
Victor Stinner | afb5205 | 2012-04-05 22:54:49 +0200 | [diff] [blame] | 5610 | ch = (Py_UCS2)(block >> 48); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 5611 | maxch = MAX_MAXCHAR(maxch, ch); |
Victor Stinner | afb5205 | 2012-04-05 22:54:49 +0200 | [diff] [blame] | 5612 | #else |
| 5613 | ch = (Py_UCS2)(block >> 16); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 5614 | maxch = MAX_MAXCHAR(maxch, ch); |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5615 | #endif |
| 5616 | if (maxch > PyUnicode_MAX_CHAR_VALUE(unicode)) { |
Victor Stinner | 1b487b4 | 2012-05-03 12:29:04 +0200 | [diff] [blame] | 5617 | if (unicode_widen(&unicode, outpos, maxch) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5618 | goto onError; |
| 5619 | kind = PyUnicode_KIND(unicode); |
| 5620 | data = PyUnicode_DATA(unicode); |
| 5621 | } |
Victor Stinner | afb5205 | 2012-04-05 22:54:49 +0200 | [diff] [blame] | 5622 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5623 | PyUnicode_WRITE(kind, data, outpos++, (Py_UCS2)(block & 0xFFFF)); |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5624 | #if SIZEOF_LONG == 8 |
Victor Stinner | afb5205 | 2012-04-05 22:54:49 +0200 | [diff] [blame] | 5625 | PyUnicode_WRITE(kind, data, outpos++, (Py_UCS2)((block >> 16) & 0xFFFF)); |
| 5626 | PyUnicode_WRITE(kind, data, outpos++, (Py_UCS2)((block >> 32) & 0xFFFF)); |
| 5627 | PyUnicode_WRITE(kind, data, outpos++, (Py_UCS2)((block >> 48))); |
| 5628 | #else |
| 5629 | PyUnicode_WRITE(kind, data, outpos++, (Py_UCS2)(block >> 16)); |
| 5630 | #endif |
| 5631 | #else |
| 5632 | #if SIZEOF_LONG == 8 |
| 5633 | PyUnicode_WRITE(kind, data, outpos++, (Py_UCS2)((block >> 48))); |
| 5634 | PyUnicode_WRITE(kind, data, outpos++, (Py_UCS2)((block >> 32) & 0xFFFF)); |
| 5635 | PyUnicode_WRITE(kind, data, outpos++, (Py_UCS2)((block >> 16) & 0xFFFF)); |
| 5636 | #else |
| 5637 | PyUnicode_WRITE(kind, data, outpos++, (Py_UCS2)(block >> 16)); |
| 5638 | #endif |
| 5639 | PyUnicode_WRITE(kind, data, outpos++, (Py_UCS2)(block & 0xFFFF)); |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5640 | #endif |
| 5641 | _q += SIZEOF_LONG; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5642 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5643 | q = _q; |
| 5644 | if (q >= e) |
| 5645 | break; |
| 5646 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5647 | ch = (q[ihi] << 8) | q[ilo]; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5648 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5649 | q += 2; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5650 | |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 5651 | if (!Py_UNICODE_IS_SURROGATE(ch)) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5652 | if (unicode_putchar(&unicode, &outpos, ch) < 0) |
| 5653 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5654 | continue; |
| 5655 | } |
| 5656 | |
| 5657 | /* UTF-16 code pair: */ |
| 5658 | if (q > e) { |
| 5659 | errmsg = "unexpected end of data"; |
| 5660 | startinpos = (((const char *)q) - 2) - starts; |
| 5661 | endinpos = ((const char *)e) + 1 - starts; |
| 5662 | goto utf16Error; |
| 5663 | } |
Victor Stinner | 2e9cfad | 2011-11-20 18:40:27 +0100 | [diff] [blame] | 5664 | if (Py_UNICODE_IS_HIGH_SURROGATE(ch)) { |
| 5665 | Py_UCS4 ch2 = (q[ihi] << 8) | q[ilo]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5666 | q += 2; |
Victor Stinner | 2e9cfad | 2011-11-20 18:40:27 +0100 | [diff] [blame] | 5667 | if (Py_UNICODE_IS_LOW_SURROGATE(ch2)) { |
Victor Stinner | 62aa4d0 | 2011-11-09 00:03:45 +0100 | [diff] [blame] | 5668 | if (unicode_putchar(&unicode, &outpos, |
Victor Stinner | 2e9cfad | 2011-11-20 18:40:27 +0100 | [diff] [blame] | 5669 | Py_UNICODE_JOIN_SURROGATES(ch, ch2)) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5670 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5671 | continue; |
| 5672 | } |
| 5673 | else { |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5674 | errmsg = "illegal UTF-16 surrogate"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5675 | startinpos = (((const char *)q)-4)-starts; |
| 5676 | endinpos = startinpos+2; |
| 5677 | goto utf16Error; |
| 5678 | } |
| 5679 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5680 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5681 | errmsg = "illegal encoding"; |
| 5682 | startinpos = (((const char *)q)-2)-starts; |
| 5683 | endinpos = startinpos+2; |
| 5684 | /* Fall through to report the error */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5685 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5686 | utf16Error: |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5687 | if (unicode_decode_call_errorhandler( |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5688 | errors, |
| 5689 | &errorHandler, |
| 5690 | "utf16", errmsg, |
| 5691 | &starts, |
| 5692 | (const char **)&e, |
| 5693 | &startinpos, |
| 5694 | &endinpos, |
| 5695 | &exc, |
| 5696 | (const char **)&q, |
| 5697 | &unicode, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5698 | &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5699 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5700 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5701 | /* remaining byte at the end? (size should be even) */ |
| 5702 | if (e == q) { |
| 5703 | if (!consumed) { |
| 5704 | errmsg = "truncated data"; |
| 5705 | startinpos = ((const char *)q) - starts; |
| 5706 | endinpos = ((const char *)e) + 1 - starts; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5707 | if (unicode_decode_call_errorhandler( |
| 5708 | errors, |
| 5709 | &errorHandler, |
| 5710 | "utf16", errmsg, |
| 5711 | &starts, |
| 5712 | (const char **)&e, |
| 5713 | &startinpos, |
| 5714 | &endinpos, |
| 5715 | &exc, |
| 5716 | (const char **)&q, |
| 5717 | &unicode, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5718 | &outpos)) |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5719 | goto onError; |
| 5720 | /* The remaining input chars are ignored if the callback |
| 5721 | chooses to skip the input */ |
| 5722 | } |
| 5723 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5724 | |
| 5725 | if (byteorder) |
| 5726 | *byteorder = bo; |
| 5727 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5728 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5729 | *consumed = (const char *)q-starts; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5730 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5731 | /* Adjust length */ |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 5732 | if (unicode_resize(&unicode, outpos) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5733 | goto onError; |
| 5734 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5735 | Py_XDECREF(errorHandler); |
| 5736 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 5737 | return unicode_result(unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5738 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5739 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5740 | Py_DECREF(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5741 | Py_XDECREF(errorHandler); |
| 5742 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5743 | return NULL; |
| 5744 | } |
| 5745 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5746 | #undef FAST_CHAR_MASK |
| 5747 | #undef SWAPPED_FAST_CHAR_MASK |
| 5748 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5749 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5750 | _PyUnicode_EncodeUTF16(PyObject *str, |
| 5751 | const char *errors, |
| 5752 | int byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5753 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5754 | int kind; |
| 5755 | void *data; |
| 5756 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5757 | PyObject *v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5758 | unsigned char *p; |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5759 | Py_ssize_t nsize, bytesize; |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5760 | Py_ssize_t i, pairs; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5761 | /* Offsets from p for storing byte pairs in the right order. */ |
| 5762 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5763 | int ihi = 1, ilo = 0; |
| 5764 | #else |
| 5765 | int ihi = 0, ilo = 1; |
| 5766 | #endif |
| 5767 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5768 | #define STORECHAR(CH) \ |
| 5769 | do { \ |
| 5770 | p[ihi] = ((CH) >> 8) & 0xff; \ |
| 5771 | p[ilo] = (CH) & 0xff; \ |
| 5772 | p += 2; \ |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5773 | } while(0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5774 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5775 | if (!PyUnicode_Check(str)) { |
| 5776 | PyErr_BadArgument(); |
| 5777 | return NULL; |
| 5778 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 5779 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5780 | return NULL; |
| 5781 | kind = PyUnicode_KIND(str); |
| 5782 | data = PyUnicode_DATA(str); |
| 5783 | len = PyUnicode_GET_LENGTH(str); |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 5784 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5785 | pairs = 0; |
| 5786 | if (kind == PyUnicode_4BYTE_KIND) |
| 5787 | for (i = 0; i < len; i++) |
| 5788 | if (PyUnicode_READ(kind, data, i) >= 0x10000) |
| 5789 | pairs++; |
| 5790 | /* 2 * (len + pairs + (byteorder == 0)) */ |
| 5791 | if (len > PY_SSIZE_T_MAX - pairs - (byteorder == 0)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5792 | return PyErr_NoMemory(); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5793 | nsize = len + pairs + (byteorder == 0); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5794 | bytesize = nsize * 2; |
| 5795 | if (bytesize / 2 != nsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5796 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5797 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5798 | if (v == NULL) |
| 5799 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5800 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5801 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5802 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5803 | STORECHAR(0xFEFF); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5804 | if (len == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5805 | goto done; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5806 | |
| 5807 | if (byteorder == -1) { |
| 5808 | /* force LE */ |
| 5809 | ihi = 1; |
| 5810 | ilo = 0; |
| 5811 | } |
| 5812 | else if (byteorder == 1) { |
| 5813 | /* force BE */ |
| 5814 | ihi = 0; |
| 5815 | ilo = 1; |
| 5816 | } |
| 5817 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5818 | for (i = 0; i < len; i++) { |
| 5819 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 5820 | Py_UCS4 ch2 = 0; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5821 | if (ch >= 0x10000) { |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 5822 | ch2 = Py_UNICODE_LOW_SURROGATE(ch); |
| 5823 | ch = Py_UNICODE_HIGH_SURROGATE(ch); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5824 | } |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5825 | STORECHAR(ch); |
| 5826 | if (ch2) |
| 5827 | STORECHAR(ch2); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5828 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5829 | |
| 5830 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5831 | return v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5832 | #undef STORECHAR |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5833 | } |
| 5834 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5835 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5836 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
| 5837 | Py_ssize_t size, |
| 5838 | const char *errors, |
| 5839 | int byteorder) |
| 5840 | { |
| 5841 | PyObject *result; |
| 5842 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 5843 | if (tmp == NULL) |
| 5844 | return NULL; |
| 5845 | result = _PyUnicode_EncodeUTF16(tmp, errors, byteorder); |
| 5846 | Py_DECREF(tmp); |
| 5847 | return result; |
| 5848 | } |
| 5849 | |
| 5850 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5851 | PyUnicode_AsUTF16String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5852 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5853 | return _PyUnicode_EncodeUTF16(unicode, NULL, 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5854 | } |
| 5855 | |
| 5856 | /* --- Unicode Escape Codec ----------------------------------------------- */ |
| 5857 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5858 | /* Helper function for PyUnicode_DecodeUnicodeEscape, determines |
| 5859 | if all the escapes in the string make it still a valid ASCII string. |
| 5860 | Returns -1 if any escapes were found which cause the string to |
| 5861 | pop out of ASCII range. Otherwise returns the length of the |
| 5862 | required buffer to hold the string. |
| 5863 | */ |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 5864 | static Py_ssize_t |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5865 | length_of_escaped_ascii_string(const char *s, Py_ssize_t size) |
| 5866 | { |
| 5867 | const unsigned char *p = (const unsigned char *)s; |
| 5868 | const unsigned char *end = p + size; |
| 5869 | Py_ssize_t length = 0; |
| 5870 | |
| 5871 | if (size < 0) |
| 5872 | return -1; |
| 5873 | |
| 5874 | for (; p < end; ++p) { |
| 5875 | if (*p > 127) { |
| 5876 | /* Non-ASCII */ |
| 5877 | return -1; |
| 5878 | } |
| 5879 | else if (*p != '\\') { |
| 5880 | /* Normal character */ |
| 5881 | ++length; |
| 5882 | } |
| 5883 | else { |
| 5884 | /* Backslash-escape, check next char */ |
| 5885 | ++p; |
| 5886 | /* Escape sequence reaches till end of string or |
| 5887 | non-ASCII follow-up. */ |
| 5888 | if (p >= end || *p > 127) |
| 5889 | return -1; |
| 5890 | switch (*p) { |
| 5891 | case '\n': |
| 5892 | /* backslash + \n result in zero characters */ |
| 5893 | break; |
| 5894 | case '\\': case '\'': case '\"': |
| 5895 | case 'b': case 'f': case 't': |
| 5896 | case 'n': case 'r': case 'v': case 'a': |
| 5897 | ++length; |
| 5898 | break; |
| 5899 | case '0': case '1': case '2': case '3': |
| 5900 | case '4': case '5': case '6': case '7': |
| 5901 | case 'x': case 'u': case 'U': case 'N': |
| 5902 | /* these do not guarantee ASCII characters */ |
| 5903 | return -1; |
| 5904 | default: |
| 5905 | /* count the backslash + the other character */ |
| 5906 | length += 2; |
| 5907 | } |
| 5908 | } |
| 5909 | } |
| 5910 | return length; |
| 5911 | } |
| 5912 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 5913 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; |
Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 5914 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5915 | PyObject * |
| 5916 | PyUnicode_DecodeUnicodeEscape(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5917 | Py_ssize_t size, |
Victor Stinner | c17f540 | 2011-09-29 00:16:58 +0200 | [diff] [blame] | 5918 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5919 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5920 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5921 | Py_ssize_t startinpos; |
| 5922 | Py_ssize_t endinpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5923 | int j; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5924 | PyObject *v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5925 | const char *end; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5926 | char* message; |
| 5927 | Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5928 | PyObject *errorHandler = NULL; |
| 5929 | PyObject *exc = NULL; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5930 | Py_ssize_t len; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5931 | Py_ssize_t i; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5932 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5933 | len = length_of_escaped_ascii_string(s, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5934 | |
| 5935 | /* After length_of_escaped_ascii_string() there are two alternatives, |
| 5936 | either the string is pure ASCII with named escapes like \n, etc. |
| 5937 | and we determined it's exact size (common case) |
| 5938 | or it contains \x, \u, ... escape sequences. then we create a |
| 5939 | 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] | 5940 | if (len >= 0) { |
| 5941 | v = PyUnicode_New(len, 127); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5942 | if (!v) |
| 5943 | goto onError; |
| 5944 | assert(PyUnicode_KIND(v) == PyUnicode_1BYTE_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5945 | } |
| 5946 | else { |
| 5947 | /* Escaped strings will always be longer than the resulting |
| 5948 | Unicode string, so we start with size here and then reduce the |
| 5949 | length after conversion to the true value. |
| 5950 | (but if the error callback returns a long replacement string |
| 5951 | we'll have to allocate more space) */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5952 | v = PyUnicode_New(size, 127); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5953 | if (!v) |
| 5954 | goto onError; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5955 | len = size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5956 | } |
| 5957 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5958 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5959 | return v; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5960 | i = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5961 | end = s + size; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5962 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5963 | while (s < end) { |
| 5964 | unsigned char c; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 5965 | Py_UCS4 x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5966 | int digits; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5967 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5968 | /* The only case in which i == ascii_length is a backslash |
| 5969 | followed by a newline. */ |
| 5970 | assert(i <= len); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5971 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5972 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 5973 | if (*s != '\\') { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5974 | if (unicode_putchar(&v, &i, (unsigned char) *s++) < 0) |
| 5975 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5976 | continue; |
| 5977 | } |
| 5978 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5979 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5980 | /* \ - Escapes */ |
| 5981 | s++; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5982 | c = *s++; |
| 5983 | if (s > end) |
| 5984 | c = '\0'; /* Invalid after \ */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5985 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5986 | /* The only case in which i == ascii_length is a backslash |
| 5987 | followed by a newline. */ |
| 5988 | assert(i < len || (i == len && c == '\n')); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5989 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5990 | switch (c) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5991 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5992 | /* \x escapes */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5993 | #define WRITECHAR(ch) \ |
| 5994 | do { \ |
| 5995 | if (unicode_putchar(&v, &i, ch) < 0) \ |
| 5996 | goto onError; \ |
| 5997 | }while(0) |
| 5998 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5999 | case '\n': break; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6000 | case '\\': WRITECHAR('\\'); break; |
| 6001 | case '\'': WRITECHAR('\''); break; |
| 6002 | case '\"': WRITECHAR('\"'); break; |
| 6003 | case 'b': WRITECHAR('\b'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6004 | /* FF */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6005 | case 'f': WRITECHAR('\014'); break; |
| 6006 | case 't': WRITECHAR('\t'); break; |
| 6007 | case 'n': WRITECHAR('\n'); break; |
| 6008 | case 'r': WRITECHAR('\r'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6009 | /* VT */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6010 | case 'v': WRITECHAR('\013'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6011 | /* BEL, not classic C */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6012 | case 'a': WRITECHAR('\007'); break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6013 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6014 | /* \OOO (octal) escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6015 | case '0': case '1': case '2': case '3': |
| 6016 | case '4': case '5': case '6': case '7': |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 6017 | x = s[-1] - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 6018 | if (s < end && '0' <= *s && *s <= '7') { |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 6019 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 6020 | if (s < end && '0' <= *s && *s <= '7') |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 6021 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6022 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6023 | WRITECHAR(x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6024 | break; |
| 6025 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6026 | /* hex escapes */ |
| 6027 | /* \xXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6028 | case 'x': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 6029 | digits = 2; |
| 6030 | message = "truncated \\xXX escape"; |
| 6031 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6032 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6033 | /* \uXXXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6034 | case 'u': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 6035 | digits = 4; |
| 6036 | message = "truncated \\uXXXX escape"; |
| 6037 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6038 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6039 | /* \UXXXXXXXX */ |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 6040 | case 'U': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 6041 | digits = 8; |
| 6042 | message = "truncated \\UXXXXXXXX escape"; |
| 6043 | hexescape: |
| 6044 | chr = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6045 | if (s+digits>end) { |
| 6046 | endinpos = size; |
| 6047 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6048 | errors, &errorHandler, |
| 6049 | "unicodeescape", "end of string in escape sequence", |
| 6050 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6051 | &v, &i)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6052 | goto onError; |
| 6053 | goto nextByte; |
| 6054 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6055 | for (j = 0; j < digits; ++j) { |
| 6056 | c = (unsigned char) s[j]; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 6057 | if (!Py_ISXDIGIT(c)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6058 | endinpos = (s+j+1)-starts; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6059 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6060 | errors, &errorHandler, |
| 6061 | "unicodeescape", message, |
| 6062 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6063 | &v, &i)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 6064 | goto onError; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6065 | len = PyUnicode_GET_LENGTH(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6066 | goto nextByte; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 6067 | } |
| 6068 | chr = (chr<<4) & ~0xF; |
| 6069 | if (c >= '0' && c <= '9') |
| 6070 | chr += c - '0'; |
| 6071 | else if (c >= 'a' && c <= 'f') |
| 6072 | chr += 10 + c - 'a'; |
| 6073 | else |
| 6074 | chr += 10 + c - 'A'; |
| 6075 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6076 | s += j; |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 6077 | if (chr == 0xffffffff && PyErr_Occurred()) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6078 | /* _decoding_error will have already written into the |
| 6079 | target buffer. */ |
| 6080 | break; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 6081 | store: |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 6082 | /* when we get here, chr is a 32-bit unicode character */ |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 6083 | if (chr <= MAX_UNICODE) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6084 | WRITECHAR(chr); |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 6085 | } else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6086 | endinpos = s-starts; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6087 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6088 | errors, &errorHandler, |
| 6089 | "unicodeescape", "illegal Unicode character", |
| 6090 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6091 | &v, &i)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 6092 | goto onError; |
| 6093 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 6094 | break; |
| 6095 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6096 | /* \N{name} */ |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 6097 | case 'N': |
| 6098 | message = "malformed \\N character escape"; |
| 6099 | if (ucnhash_CAPI == NULL) { |
| 6100 | /* load the unicode data module */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6101 | ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCapsule_Import( |
| 6102 | PyUnicodeData_CAPSULE_NAME, 1); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 6103 | if (ucnhash_CAPI == NULL) |
| 6104 | goto ucnhashError; |
| 6105 | } |
| 6106 | if (*s == '{') { |
| 6107 | const char *start = s+1; |
| 6108 | /* look for the closing brace */ |
| 6109 | while (*s != '}' && s < end) |
| 6110 | s++; |
| 6111 | if (s > start && s < end && *s == '}') { |
| 6112 | /* found a name. look it up in the unicode database */ |
| 6113 | message = "unknown Unicode character name"; |
| 6114 | s++; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6115 | if (ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), |
Ezio Melotti | 931b8aa | 2011-10-21 21:57:36 +0300 | [diff] [blame] | 6116 | &chr, 0)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 6117 | goto store; |
| 6118 | } |
| 6119 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6120 | endinpos = s-starts; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6121 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6122 | errors, &errorHandler, |
| 6123 | "unicodeescape", message, |
| 6124 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6125 | &v, &i)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 6126 | goto onError; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 6127 | break; |
| 6128 | |
| 6129 | default: |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 6130 | if (s > end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6131 | message = "\\ at end of string"; |
| 6132 | s--; |
| 6133 | endinpos = s-starts; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6134 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6135 | errors, &errorHandler, |
| 6136 | "unicodeescape", message, |
| 6137 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6138 | &v, &i)) |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 6139 | goto onError; |
| 6140 | } |
| 6141 | else { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6142 | WRITECHAR('\\'); |
| 6143 | WRITECHAR(s[-1]); |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 6144 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 6145 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6146 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6147 | nextByte: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6148 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6149 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6150 | #undef WRITECHAR |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6151 | |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 6152 | if (unicode_resize(&v, i) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6153 | goto onError; |
Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 6154 | Py_XDECREF(errorHandler); |
| 6155 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 6156 | return unicode_result(v); |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 6157 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6158 | ucnhashError: |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 6159 | PyErr_SetString( |
| 6160 | PyExc_UnicodeError, |
| 6161 | "\\N escapes not supported (can't load unicodedata module)" |
| 6162 | ); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 6163 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6164 | Py_XDECREF(errorHandler); |
| 6165 | Py_XDECREF(exc); |
Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 6166 | return NULL; |
| 6167 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6168 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6169 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6170 | Py_XDECREF(errorHandler); |
| 6171 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6172 | return NULL; |
| 6173 | } |
| 6174 | |
| 6175 | /* Return a Unicode-Escape string version of the Unicode object. |
| 6176 | |
| 6177 | If quotes is true, the string is enclosed in u"" or u'' quotes as |
| 6178 | appropriate. |
| 6179 | |
| 6180 | */ |
| 6181 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6182 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6183 | PyUnicode_AsUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6184 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6185 | Py_ssize_t i, len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6186 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6187 | char *p; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6188 | int kind; |
| 6189 | void *data; |
| 6190 | Py_ssize_t expandsize = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6191 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6192 | /* Initial allocation is based on the longest-possible unichr |
| 6193 | escape. |
| 6194 | |
| 6195 | In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source |
| 6196 | unichr, so in this case it's the longest unichr escape. In |
| 6197 | narrow (UTF-16) builds this is five chars per source unichr |
| 6198 | since there are two unichrs in the surrogate pair, so in narrow |
| 6199 | (UTF-16) builds it's not the longest unichr escape. |
| 6200 | |
| 6201 | In wide or narrow builds '\uxxxx' is 6 chars per source unichr, |
| 6202 | so in the narrow (UTF-16) build case it's the longest unichr |
| 6203 | escape. |
| 6204 | */ |
| 6205 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6206 | if (!PyUnicode_Check(unicode)) { |
| 6207 | PyErr_BadArgument(); |
| 6208 | return NULL; |
| 6209 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6210 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6211 | return NULL; |
| 6212 | len = PyUnicode_GET_LENGTH(unicode); |
| 6213 | kind = PyUnicode_KIND(unicode); |
| 6214 | data = PyUnicode_DATA(unicode); |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 6215 | switch (kind) { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6216 | case PyUnicode_1BYTE_KIND: expandsize = 4; break; |
| 6217 | case PyUnicode_2BYTE_KIND: expandsize = 6; break; |
| 6218 | case PyUnicode_4BYTE_KIND: expandsize = 10; break; |
| 6219 | } |
| 6220 | |
| 6221 | if (len == 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6222 | return PyBytes_FromStringAndSize(NULL, 0); |
| 6223 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6224 | if (len > (PY_SSIZE_T_MAX - 2 - 1) / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6225 | return PyErr_NoMemory(); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 6226 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6227 | repr = PyBytes_FromStringAndSize(NULL, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6228 | 2 |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6229 | + expandsize*len |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6230 | + 1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6231 | if (repr == NULL) |
| 6232 | return NULL; |
| 6233 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6234 | p = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6235 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6236 | for (i = 0; i < len; i++) { |
Victor Stinner | 3326cb6 | 2011-11-10 20:15:25 +0100 | [diff] [blame] | 6237 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 6238 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 6239 | /* Escape backslashes */ |
| 6240 | if (ch == '\\') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6241 | *p++ = '\\'; |
| 6242 | *p++ = (char) ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 6243 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6244 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 6245 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 6246 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 6247 | else if (ch >= 0x10000) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 6248 | assert(ch <= MAX_UNICODE); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 6249 | *p++ = '\\'; |
| 6250 | *p++ = 'U'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6251 | *p++ = Py_hexdigits[(ch >> 28) & 0x0000000F]; |
| 6252 | *p++ = Py_hexdigits[(ch >> 24) & 0x0000000F]; |
| 6253 | *p++ = Py_hexdigits[(ch >> 20) & 0x0000000F]; |
| 6254 | *p++ = Py_hexdigits[(ch >> 16) & 0x0000000F]; |
| 6255 | *p++ = Py_hexdigits[(ch >> 12) & 0x0000000F]; |
| 6256 | *p++ = Py_hexdigits[(ch >> 8) & 0x0000000F]; |
| 6257 | *p++ = Py_hexdigits[(ch >> 4) & 0x0000000F]; |
| 6258 | *p++ = Py_hexdigits[ch & 0x0000000F]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6259 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 6260 | } |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 6261 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6262 | /* Map 16-bit characters to '\uxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 6263 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6264 | *p++ = '\\'; |
| 6265 | *p++ = 'u'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6266 | *p++ = Py_hexdigits[(ch >> 12) & 0x000F]; |
| 6267 | *p++ = Py_hexdigits[(ch >> 8) & 0x000F]; |
| 6268 | *p++ = Py_hexdigits[(ch >> 4) & 0x000F]; |
| 6269 | *p++ = Py_hexdigits[ch & 0x000F]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6270 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 6271 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 6272 | /* Map special whitespace to '\t', \n', '\r' */ |
| 6273 | else if (ch == '\t') { |
| 6274 | *p++ = '\\'; |
| 6275 | *p++ = 't'; |
| 6276 | } |
| 6277 | else if (ch == '\n') { |
| 6278 | *p++ = '\\'; |
| 6279 | *p++ = 'n'; |
| 6280 | } |
| 6281 | else if (ch == '\r') { |
| 6282 | *p++ = '\\'; |
| 6283 | *p++ = 'r'; |
| 6284 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 6285 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 6286 | /* Map non-printable US ASCII to '\xhh' */ |
Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 6287 | else if (ch < ' ' || ch >= 0x7F) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6288 | *p++ = '\\'; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 6289 | *p++ = 'x'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6290 | *p++ = Py_hexdigits[(ch >> 4) & 0x000F]; |
| 6291 | *p++ = Py_hexdigits[ch & 0x000F]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6292 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 6293 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6294 | /* Copy everything else as-is */ |
| 6295 | else |
| 6296 | *p++ = (char) ch; |
| 6297 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6298 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6299 | assert(p - PyBytes_AS_STRING(repr) > 0); |
| 6300 | if (_PyBytes_Resize(&repr, p - PyBytes_AS_STRING(repr)) < 0) |
| 6301 | return NULL; |
| 6302 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6303 | } |
| 6304 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6305 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6306 | PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, |
| 6307 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6308 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6309 | PyObject *result; |
| 6310 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 6311 | if (tmp == NULL) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6312 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6313 | result = PyUnicode_AsUnicodeEscapeString(tmp); |
| 6314 | Py_DECREF(tmp); |
| 6315 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6316 | } |
| 6317 | |
| 6318 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ |
| 6319 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6320 | PyObject * |
| 6321 | PyUnicode_DecodeRawUnicodeEscape(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6322 | Py_ssize_t size, |
| 6323 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6324 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6325 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6326 | Py_ssize_t startinpos; |
| 6327 | Py_ssize_t endinpos; |
| 6328 | Py_ssize_t outpos; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6329 | PyObject *v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6330 | const char *end; |
| 6331 | const char *bs; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6332 | PyObject *errorHandler = NULL; |
| 6333 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6334 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6335 | /* Escaped strings will always be longer than the resulting |
| 6336 | 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] | 6337 | length after conversion to the true value. (But decoding error |
| 6338 | handler might have to resize the string) */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6339 | v = PyUnicode_New(size, 127); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6340 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6341 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6342 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6343 | return v; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6344 | outpos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6345 | end = s + size; |
| 6346 | while (s < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6347 | unsigned char c; |
| 6348 | Py_UCS4 x; |
| 6349 | int i; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6350 | int count; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6351 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6352 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 6353 | if (*s != '\\') { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6354 | if (unicode_putchar(&v, &outpos, (unsigned char)*s++) < 0) |
| 6355 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6356 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6357 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6358 | startinpos = s-starts; |
| 6359 | |
| 6360 | /* \u-escapes are only interpreted iff the number of leading |
| 6361 | backslashes if odd */ |
| 6362 | bs = s; |
| 6363 | for (;s < end;) { |
| 6364 | if (*s != '\\') |
| 6365 | break; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6366 | if (unicode_putchar(&v, &outpos, (unsigned char)*s++) < 0) |
| 6367 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6368 | } |
| 6369 | if (((s - bs) & 1) == 0 || |
| 6370 | s >= end || |
| 6371 | (*s != 'u' && *s != 'U')) { |
| 6372 | continue; |
| 6373 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6374 | outpos--; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6375 | count = *s=='u' ? 4 : 8; |
| 6376 | s++; |
| 6377 | |
| 6378 | /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6379 | for (x = 0, i = 0; i < count; ++i, ++s) { |
| 6380 | c = (unsigned char)*s; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 6381 | if (!Py_ISXDIGIT(c)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6382 | endinpos = s-starts; |
| 6383 | if (unicode_decode_call_errorhandler( |
| 6384 | errors, &errorHandler, |
| 6385 | "rawunicodeescape", "truncated \\uXXXX", |
| 6386 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6387 | &v, &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6388 | goto onError; |
| 6389 | goto nextByte; |
| 6390 | } |
| 6391 | x = (x<<4) & ~0xF; |
| 6392 | if (c >= '0' && c <= '9') |
| 6393 | x += c - '0'; |
| 6394 | else if (c >= 'a' && c <= 'f') |
| 6395 | x += 10 + c - 'a'; |
| 6396 | else |
| 6397 | x += 10 + c - 'A'; |
| 6398 | } |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 6399 | if (x <= MAX_UNICODE) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6400 | if (unicode_putchar(&v, &outpos, x) < 0) |
| 6401 | goto onError; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 6402 | } else { |
| 6403 | endinpos = s-starts; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6404 | if (unicode_decode_call_errorhandler( |
| 6405 | errors, &errorHandler, |
| 6406 | "rawunicodeescape", "\\Uxxxxxxxx out of range", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6407 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6408 | &v, &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6409 | goto onError; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6410 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6411 | nextByte: |
| 6412 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6413 | } |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 6414 | if (unicode_resize(&v, outpos) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6415 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6416 | Py_XDECREF(errorHandler); |
| 6417 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 6418 | return unicode_result(v); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6419 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6420 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6421 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6422 | Py_XDECREF(errorHandler); |
| 6423 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6424 | return NULL; |
| 6425 | } |
| 6426 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6427 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6428 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6429 | PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6430 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6431 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6432 | char *p; |
| 6433 | char *q; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6434 | Py_ssize_t expandsize, pos; |
| 6435 | int kind; |
| 6436 | void *data; |
| 6437 | Py_ssize_t len; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6438 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6439 | if (!PyUnicode_Check(unicode)) { |
| 6440 | PyErr_BadArgument(); |
| 6441 | return NULL; |
| 6442 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6443 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6444 | return NULL; |
| 6445 | kind = PyUnicode_KIND(unicode); |
| 6446 | data = PyUnicode_DATA(unicode); |
| 6447 | len = PyUnicode_GET_LENGTH(unicode); |
Benjamin Peterson | 1518e87 | 2011-11-23 10:44:52 -0600 | [diff] [blame] | 6448 | /* 4 byte characters can take up 10 bytes, 2 byte characters can take up 6 |
| 6449 | bytes, and 1 byte characters 4. */ |
| 6450 | expandsize = kind * 2 + 2; |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 6451 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6452 | if (len > PY_SSIZE_T_MAX / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6453 | return PyErr_NoMemory(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6454 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6455 | repr = PyBytes_FromStringAndSize(NULL, expandsize * len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6456 | if (repr == NULL) |
| 6457 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6458 | if (len == 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6459 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6460 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6461 | p = q = PyBytes_AS_STRING(repr); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6462 | for (pos = 0; pos < len; pos++) { |
| 6463 | Py_UCS4 ch = PyUnicode_READ(kind, data, pos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6464 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 6465 | if (ch >= 0x10000) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 6466 | assert(ch <= MAX_UNICODE); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6467 | *p++ = '\\'; |
| 6468 | *p++ = 'U'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6469 | *p++ = Py_hexdigits[(ch >> 28) & 0xf]; |
| 6470 | *p++ = Py_hexdigits[(ch >> 24) & 0xf]; |
| 6471 | *p++ = Py_hexdigits[(ch >> 20) & 0xf]; |
| 6472 | *p++ = Py_hexdigits[(ch >> 16) & 0xf]; |
| 6473 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 6474 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 6475 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 6476 | *p++ = Py_hexdigits[ch & 15]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6477 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6478 | /* Map 16-bit characters to '\uxxxx' */ |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6479 | else if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6480 | *p++ = '\\'; |
| 6481 | *p++ = 'u'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6482 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 6483 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 6484 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 6485 | *p++ = Py_hexdigits[ch & 15]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6486 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6487 | /* Copy everything else as-is */ |
| 6488 | else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6489 | *p++ = (char) ch; |
| 6490 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6491 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6492 | assert(p > q); |
| 6493 | if (_PyBytes_Resize(&repr, p - q) < 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6494 | return NULL; |
| 6495 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6496 | } |
| 6497 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6498 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6499 | PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
| 6500 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6501 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6502 | PyObject *result; |
| 6503 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 6504 | if (tmp == NULL) |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 6505 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6506 | result = PyUnicode_AsRawUnicodeEscapeString(tmp); |
| 6507 | Py_DECREF(tmp); |
| 6508 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6509 | } |
| 6510 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6511 | /* --- Unicode Internal Codec ------------------------------------------- */ |
| 6512 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6513 | PyObject * |
| 6514 | _PyUnicode_DecodeUnicodeInternal(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6515 | Py_ssize_t size, |
| 6516 | const char *errors) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6517 | { |
| 6518 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6519 | Py_ssize_t startinpos; |
| 6520 | Py_ssize_t endinpos; |
| 6521 | Py_ssize_t outpos; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6522 | PyObject *v; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6523 | const char *end; |
| 6524 | const char *reason; |
| 6525 | PyObject *errorHandler = NULL; |
| 6526 | PyObject *exc = NULL; |
| 6527 | |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6528 | if (PyErr_WarnEx(PyExc_DeprecationWarning, |
Ezio Melotti | 11060a4 | 2011-11-16 09:39:10 +0200 | [diff] [blame] | 6529 | "unicode_internal codec has been deprecated", |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6530 | 1)) |
| 6531 | return NULL; |
| 6532 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6533 | /* XXX overflow detection missing */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6534 | 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] | 6535 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6536 | goto onError; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6537 | if (PyUnicode_GET_LENGTH(v) == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6538 | return v; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6539 | outpos = 0; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6540 | end = s + size; |
| 6541 | |
| 6542 | while (s < end) { |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6543 | Py_UNICODE uch; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6544 | Py_UCS4 ch; |
| 6545 | /* We copy the raw representation one byte at a time because the |
| 6546 | pointer may be unaligned (see test_codeccallbacks). */ |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6547 | ((char *) &uch)[0] = s[0]; |
| 6548 | ((char *) &uch)[1] = s[1]; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6549 | #ifdef Py_UNICODE_WIDE |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6550 | ((char *) &uch)[2] = s[2]; |
| 6551 | ((char *) &uch)[3] = s[3]; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6552 | #endif |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6553 | ch = uch; |
| 6554 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6555 | /* We have to sanity check the raw data, otherwise doom looms for |
| 6556 | some malformed UCS-4 data. */ |
| 6557 | if ( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6558 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6559 | ch > 0x10ffff || |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6560 | #endif |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6561 | end-s < Py_UNICODE_SIZE |
| 6562 | ) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6563 | { |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6564 | startinpos = s - starts; |
| 6565 | if (end-s < Py_UNICODE_SIZE) { |
| 6566 | endinpos = end-starts; |
| 6567 | reason = "truncated input"; |
| 6568 | } |
| 6569 | else { |
| 6570 | endinpos = s - starts + Py_UNICODE_SIZE; |
| 6571 | reason = "illegal code point (> 0x10FFFF)"; |
| 6572 | } |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6573 | if (unicode_decode_call_errorhandler( |
| 6574 | errors, &errorHandler, |
| 6575 | "unicode_internal", reason, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 6576 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6577 | &v, &outpos)) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6578 | goto onError; |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6579 | continue; |
| 6580 | } |
| 6581 | |
| 6582 | s += Py_UNICODE_SIZE; |
| 6583 | #ifndef Py_UNICODE_WIDE |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 6584 | if (Py_UNICODE_IS_HIGH_SURROGATE(ch) && s < end) |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6585 | { |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6586 | Py_UNICODE uch2; |
| 6587 | ((char *) &uch2)[0] = s[0]; |
| 6588 | ((char *) &uch2)[1] = s[1]; |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 6589 | if (Py_UNICODE_IS_LOW_SURROGATE(uch2)) |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6590 | { |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 6591 | ch = Py_UNICODE_JOIN_SURROGATES(uch, uch2); |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6592 | s += Py_UNICODE_SIZE; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6593 | } |
| 6594 | } |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6595 | #endif |
| 6596 | |
| 6597 | if (unicode_putchar(&v, &outpos, ch) < 0) |
| 6598 | goto onError; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6599 | } |
| 6600 | |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 6601 | if (unicode_resize(&v, outpos) < 0) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6602 | goto onError; |
| 6603 | Py_XDECREF(errorHandler); |
| 6604 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 6605 | return unicode_result(v); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6606 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6607 | onError: |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6608 | Py_XDECREF(v); |
| 6609 | Py_XDECREF(errorHandler); |
| 6610 | Py_XDECREF(exc); |
| 6611 | return NULL; |
| 6612 | } |
| 6613 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6614 | /* --- Latin-1 Codec ------------------------------------------------------ */ |
| 6615 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6616 | PyObject * |
| 6617 | PyUnicode_DecodeLatin1(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6618 | Py_ssize_t size, |
| 6619 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6620 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6621 | /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 6622 | return _PyUnicode_FromUCS1((unsigned char*)s, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6623 | } |
| 6624 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6625 | /* create or adjust a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6626 | static void |
| 6627 | make_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6628 | const char *encoding, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6629 | PyObject *unicode, |
| 6630 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6631 | const char *reason) |
| 6632 | { |
| 6633 | if (*exceptionObject == NULL) { |
| 6634 | *exceptionObject = PyObject_CallFunction( |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6635 | PyExc_UnicodeEncodeError, "sOnns", |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6636 | encoding, unicode, startpos, endpos, reason); |
| 6637 | } |
| 6638 | else { |
| 6639 | if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 6640 | goto onError; |
| 6641 | if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) |
| 6642 | goto onError; |
| 6643 | if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) |
| 6644 | goto onError; |
| 6645 | return; |
| 6646 | onError: |
| 6647 | Py_DECREF(*exceptionObject); |
| 6648 | *exceptionObject = NULL; |
| 6649 | } |
| 6650 | } |
| 6651 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6652 | /* raises a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6653 | static void |
| 6654 | raise_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6655 | const char *encoding, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6656 | PyObject *unicode, |
| 6657 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6658 | const char *reason) |
| 6659 | { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6660 | make_encode_exception(exceptionObject, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6661 | encoding, unicode, startpos, endpos, reason); |
| 6662 | if (*exceptionObject != NULL) |
| 6663 | PyCodec_StrictErrors(*exceptionObject); |
| 6664 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6665 | |
| 6666 | /* error handling callback helper: |
| 6667 | build arguments, call the callback and check the arguments, |
| 6668 | put the result into newpos and return the replacement string, which |
| 6669 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6670 | static PyObject * |
| 6671 | unicode_encode_call_errorhandler(const char *errors, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6672 | PyObject **errorHandler, |
| 6673 | const char *encoding, const char *reason, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6674 | PyObject *unicode, PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6675 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6676 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6677 | { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6678 | 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] | 6679 | Py_ssize_t len; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6680 | PyObject *restuple; |
| 6681 | PyObject *resunicode; |
| 6682 | |
| 6683 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6684 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6685 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6686 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6687 | } |
| 6688 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6689 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6690 | return NULL; |
| 6691 | len = PyUnicode_GET_LENGTH(unicode); |
| 6692 | |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6693 | make_encode_exception(exceptionObject, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6694 | encoding, unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6695 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6696 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6697 | |
| 6698 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6699 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6700 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6701 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6702 | if (!PyTuple_Check(restuple)) { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6703 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6704 | Py_DECREF(restuple); |
| 6705 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6706 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6707 | if (!PyArg_ParseTuple(restuple, argparse, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6708 | &resunicode, newpos)) { |
| 6709 | Py_DECREF(restuple); |
| 6710 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6711 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6712 | if (!PyUnicode_Check(resunicode) && !PyBytes_Check(resunicode)) { |
| 6713 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
| 6714 | Py_DECREF(restuple); |
| 6715 | return NULL; |
| 6716 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6717 | if (*newpos<0) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6718 | *newpos = len + *newpos; |
| 6719 | if (*newpos<0 || *newpos>len) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6720 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 6721 | Py_DECREF(restuple); |
| 6722 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 6723 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6724 | Py_INCREF(resunicode); |
| 6725 | Py_DECREF(restuple); |
| 6726 | return resunicode; |
| 6727 | } |
| 6728 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6729 | static PyObject * |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6730 | unicode_encode_ucs1(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6731 | const char *errors, |
Victor Stinner | fcd9653 | 2011-11-04 00:28:50 +0100 | [diff] [blame] | 6732 | unsigned int limit) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6733 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6734 | /* input state */ |
| 6735 | Py_ssize_t pos=0, size; |
| 6736 | int kind; |
| 6737 | void *data; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6738 | /* output object */ |
| 6739 | PyObject *res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6740 | /* pointer into the output */ |
| 6741 | char *str; |
| 6742 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6743 | Py_ssize_t ressize; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6744 | const char *encoding = (limit == 256) ? "latin-1" : "ascii"; |
| 6745 | 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] | 6746 | PyObject *errorHandler = NULL; |
| 6747 | PyObject *exc = NULL; |
| 6748 | /* the following variable is used for caching string comparisons |
| 6749 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 6750 | int known_errorHandler = -1; |
| 6751 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6752 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6753 | return NULL; |
| 6754 | size = PyUnicode_GET_LENGTH(unicode); |
| 6755 | kind = PyUnicode_KIND(unicode); |
| 6756 | data = PyUnicode_DATA(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6757 | /* allocate enough for a simple encoding without |
| 6758 | replacements, if we need more, we'll resize */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6759 | if (size == 0) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 6760 | return PyBytes_FromStringAndSize(NULL, 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6761 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6762 | if (res == NULL) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6763 | return NULL; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6764 | str = PyBytes_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6765 | ressize = size; |
| 6766 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6767 | while (pos < size) { |
| 6768 | Py_UCS4 c = PyUnicode_READ(kind, data, pos); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6769 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6770 | /* can we encode this? */ |
| 6771 | if (c<limit) { |
| 6772 | /* no overflow check, because we know that the space is enough */ |
| 6773 | *str++ = (char)c; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6774 | ++pos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6775 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6776 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6777 | Py_ssize_t requiredsize; |
| 6778 | PyObject *repunicode; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6779 | Py_ssize_t repsize, newpos, respos, i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6780 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6781 | Py_ssize_t collstart = pos; |
| 6782 | Py_ssize_t collend = pos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6783 | /* find all unecodable characters */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6784 | while ((collend < size) && (PyUnicode_READ(kind, data, collend)>=limit)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6785 | ++collend; |
| 6786 | /* cache callback name lookup (if not done yet, i.e. it's the first error) */ |
| 6787 | if (known_errorHandler==-1) { |
| 6788 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 6789 | known_errorHandler = 1; |
| 6790 | else if (!strcmp(errors, "replace")) |
| 6791 | known_errorHandler = 2; |
| 6792 | else if (!strcmp(errors, "ignore")) |
| 6793 | known_errorHandler = 3; |
| 6794 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 6795 | known_errorHandler = 4; |
| 6796 | else |
| 6797 | known_errorHandler = 0; |
| 6798 | } |
| 6799 | switch (known_errorHandler) { |
| 6800 | case 1: /* strict */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6801 | raise_encode_exception(&exc, encoding, unicode, collstart, collend, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6802 | goto onError; |
| 6803 | case 2: /* replace */ |
| 6804 | while (collstart++<collend) |
| 6805 | *str++ = '?'; /* fall through */ |
| 6806 | case 3: /* ignore */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6807 | pos = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6808 | break; |
| 6809 | case 4: /* xmlcharrefreplace */ |
| 6810 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6811 | /* determine replacement size */ |
| 6812 | for (i = collstart, repsize = 0; i < collend; ++i) { |
| 6813 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 6814 | if (ch < 10) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6815 | repsize += 2+1+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6816 | else if (ch < 100) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6817 | repsize += 2+2+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6818 | else if (ch < 1000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6819 | repsize += 2+3+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6820 | else if (ch < 10000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6821 | repsize += 2+4+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6822 | else if (ch < 100000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6823 | repsize += 2+5+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6824 | else if (ch < 1000000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6825 | repsize += 2+6+1; |
Victor Stinner | 0d3721d | 2011-11-22 03:27:53 +0100 | [diff] [blame] | 6826 | else { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 6827 | assert(ch <= MAX_UNICODE); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6828 | repsize += 2+7+1; |
Victor Stinner | 0d3721d | 2011-11-22 03:27:53 +0100 | [diff] [blame] | 6829 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6830 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6831 | requiredsize = respos+repsize+(size-collend); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6832 | if (requiredsize > ressize) { |
| 6833 | if (requiredsize<2*ressize) |
| 6834 | requiredsize = 2*ressize; |
| 6835 | if (_PyBytes_Resize(&res, requiredsize)) |
| 6836 | goto onError; |
| 6837 | str = PyBytes_AS_STRING(res) + respos; |
| 6838 | ressize = requiredsize; |
| 6839 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6840 | /* generate replacement */ |
| 6841 | for (i = collstart; i < collend; ++i) { |
| 6842 | str += sprintf(str, "&#%d;", PyUnicode_READ(kind, data, i)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6843 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6844 | pos = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6845 | break; |
| 6846 | default: |
| 6847 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6848 | encoding, reason, unicode, &exc, |
| 6849 | collstart, collend, &newpos); |
| 6850 | if (repunicode == NULL || (PyUnicode_Check(repunicode) && |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6851 | PyUnicode_READY(repunicode) == -1)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6852 | goto onError; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6853 | if (PyBytes_Check(repunicode)) { |
| 6854 | /* Directly copy bytes result to output. */ |
| 6855 | repsize = PyBytes_Size(repunicode); |
| 6856 | if (repsize > 1) { |
| 6857 | /* Make room for all additional bytes. */ |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 6858 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6859 | if (_PyBytes_Resize(&res, ressize+repsize-1)) { |
| 6860 | Py_DECREF(repunicode); |
| 6861 | goto onError; |
| 6862 | } |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 6863 | str = PyBytes_AS_STRING(res) + respos; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6864 | ressize += repsize-1; |
| 6865 | } |
| 6866 | memcpy(str, PyBytes_AsString(repunicode), repsize); |
| 6867 | str += repsize; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6868 | pos = newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6869 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6870 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6871 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6872 | /* need more space? (at least enough for what we |
| 6873 | have+the replacement+the rest of the string, so |
| 6874 | we won't have to check space for encodable characters) */ |
| 6875 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6876 | repsize = PyUnicode_GET_LENGTH(repunicode); |
| 6877 | requiredsize = respos+repsize+(size-collend); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6878 | if (requiredsize > ressize) { |
| 6879 | if (requiredsize<2*ressize) |
| 6880 | requiredsize = 2*ressize; |
| 6881 | if (_PyBytes_Resize(&res, requiredsize)) { |
| 6882 | Py_DECREF(repunicode); |
| 6883 | goto onError; |
| 6884 | } |
| 6885 | str = PyBytes_AS_STRING(res) + respos; |
| 6886 | ressize = requiredsize; |
| 6887 | } |
| 6888 | /* check if there is anything unencodable in the replacement |
| 6889 | and copy it to the output */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6890 | for (i = 0; repsize-->0; ++i, ++str) { |
| 6891 | c = PyUnicode_READ_CHAR(repunicode, i); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6892 | if (c >= limit) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6893 | raise_encode_exception(&exc, encoding, unicode, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6894 | pos, pos+1, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6895 | Py_DECREF(repunicode); |
| 6896 | goto onError; |
| 6897 | } |
| 6898 | *str = (char)c; |
| 6899 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6900 | pos = newpos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6901 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6902 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6903 | } |
| 6904 | } |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6905 | /* Resize if we allocated to much */ |
| 6906 | size = str - PyBytes_AS_STRING(res); |
| 6907 | if (size < ressize) { /* If this falls res will be NULL */ |
Alexandre Vassalotti | bad1b92 | 2008-12-27 09:49:09 +0000 | [diff] [blame] | 6908 | assert(size >= 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6909 | if (_PyBytes_Resize(&res, size) < 0) |
| 6910 | goto onError; |
| 6911 | } |
| 6912 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6913 | Py_XDECREF(errorHandler); |
| 6914 | Py_XDECREF(exc); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6915 | return res; |
| 6916 | |
| 6917 | onError: |
| 6918 | Py_XDECREF(res); |
| 6919 | Py_XDECREF(errorHandler); |
| 6920 | Py_XDECREF(exc); |
| 6921 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6922 | } |
| 6923 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6924 | /* Deprecated */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6925 | PyObject * |
| 6926 | PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6927 | Py_ssize_t size, |
| 6928 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6929 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6930 | PyObject *result; |
| 6931 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 6932 | if (unicode == NULL) |
| 6933 | return NULL; |
| 6934 | result = unicode_encode_ucs1(unicode, errors, 256); |
| 6935 | Py_DECREF(unicode); |
| 6936 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6937 | } |
| 6938 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6939 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6940 | _PyUnicode_AsLatin1String(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6941 | { |
| 6942 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6943 | PyErr_BadArgument(); |
| 6944 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6945 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6946 | if (PyUnicode_READY(unicode) == -1) |
| 6947 | return NULL; |
| 6948 | /* Fast path: if it is a one-byte string, construct |
| 6949 | bytes object directly. */ |
| 6950 | if (PyUnicode_KIND(unicode) == PyUnicode_1BYTE_KIND) |
| 6951 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 6952 | PyUnicode_GET_LENGTH(unicode)); |
| 6953 | /* Non-Latin-1 characters present. Defer to above function to |
| 6954 | raise the exception. */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6955 | return unicode_encode_ucs1(unicode, errors, 256); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6956 | } |
| 6957 | |
| 6958 | PyObject* |
| 6959 | PyUnicode_AsLatin1String(PyObject *unicode) |
| 6960 | { |
| 6961 | return _PyUnicode_AsLatin1String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6962 | } |
| 6963 | |
| 6964 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ |
| 6965 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6966 | PyObject * |
| 6967 | PyUnicode_DecodeASCII(const char *s, |
| 6968 | Py_ssize_t size, |
| 6969 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6970 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6971 | const char *starts = s; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6972 | PyObject *v; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6973 | int kind; |
| 6974 | void *data; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6975 | Py_ssize_t startinpos; |
| 6976 | Py_ssize_t endinpos; |
| 6977 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6978 | const char *e; |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6979 | int has_error; |
| 6980 | const unsigned char *p = (const unsigned char *)s; |
| 6981 | const unsigned char *end = p + size; |
| 6982 | 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] | 6983 | PyObject *errorHandler = NULL; |
| 6984 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6985 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 6986 | if (size == 0) { |
| 6987 | Py_INCREF(unicode_empty); |
| 6988 | return unicode_empty; |
| 6989 | } |
| 6990 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6991 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6992 | if (size == 1 && (unsigned char)s[0] < 128) |
| 6993 | return get_latin1_char((unsigned char)s[0]); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6994 | |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6995 | has_error = 0; |
| 6996 | while (p < end && !has_error) { |
| 6997 | /* Fast path, see below in PyUnicode_DecodeUTF8Stateful for |
| 6998 | an explanation. */ |
| 6999 | if (!((size_t) p & LONG_PTR_MASK)) { |
| 7000 | /* Help register allocation */ |
| 7001 | register const unsigned char *_p = p; |
| 7002 | while (_p < aligned_end) { |
| 7003 | unsigned long value = *(unsigned long *) _p; |
| 7004 | if (value & ASCII_CHAR_MASK) { |
| 7005 | has_error = 1; |
| 7006 | break; |
| 7007 | } |
| 7008 | _p += SIZEOF_LONG; |
| 7009 | } |
| 7010 | if (_p == end) |
| 7011 | break; |
| 7012 | if (has_error) |
| 7013 | break; |
| 7014 | p = _p; |
| 7015 | } |
| 7016 | if (*p & 0x80) { |
| 7017 | has_error = 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7018 | break; |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 7019 | } |
| 7020 | else { |
| 7021 | ++p; |
| 7022 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 7023 | } |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 7024 | if (!has_error) |
| 7025 | return unicode_fromascii((const unsigned char *)s, size); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7026 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7027 | v = PyUnicode_New(size, 127); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7028 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7029 | goto onError; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7030 | kind = PyUnicode_KIND(v); |
| 7031 | data = PyUnicode_DATA(v); |
| 7032 | outpos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7033 | e = s + size; |
| 7034 | while (s < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7035 | register unsigned char c = (unsigned char)*s; |
| 7036 | if (c < 128) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7037 | PyUnicode_WRITE(kind, data, outpos++, c); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7038 | ++s; |
| 7039 | } |
| 7040 | else { |
| 7041 | startinpos = s-starts; |
| 7042 | endinpos = startinpos + 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7043 | if (unicode_decode_call_errorhandler( |
| 7044 | errors, &errorHandler, |
| 7045 | "ascii", "ordinal not in range(128)", |
| 7046 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7047 | &v, &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7048 | goto onError; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7049 | kind = PyUnicode_KIND(v); |
| 7050 | data = PyUnicode_DATA(v); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7051 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7052 | } |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 7053 | if (unicode_resize(&v, outpos) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7054 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7055 | Py_XDECREF(errorHandler); |
| 7056 | Py_XDECREF(exc); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 7057 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 7058 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7059 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7060 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7061 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7062 | Py_XDECREF(errorHandler); |
| 7063 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7064 | return NULL; |
| 7065 | } |
| 7066 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7067 | /* Deprecated */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7068 | PyObject * |
| 7069 | PyUnicode_EncodeASCII(const Py_UNICODE *p, |
| 7070 | Py_ssize_t size, |
| 7071 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7072 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7073 | PyObject *result; |
| 7074 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 7075 | if (unicode == NULL) |
| 7076 | return NULL; |
| 7077 | result = unicode_encode_ucs1(unicode, errors, 128); |
| 7078 | Py_DECREF(unicode); |
| 7079 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7080 | } |
| 7081 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7082 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7083 | _PyUnicode_AsASCIIString(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7084 | { |
| 7085 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7086 | PyErr_BadArgument(); |
| 7087 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7088 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7089 | if (PyUnicode_READY(unicode) == -1) |
| 7090 | return NULL; |
| 7091 | /* Fast path: if it is an ASCII-only string, construct bytes object |
| 7092 | directly. Else defer to above function to raise the exception. */ |
| 7093 | if (PyUnicode_MAX_CHAR_VALUE(unicode) < 128) |
| 7094 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 7095 | PyUnicode_GET_LENGTH(unicode)); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7096 | return unicode_encode_ucs1(unicode, errors, 128); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7097 | } |
| 7098 | |
| 7099 | PyObject * |
| 7100 | PyUnicode_AsASCIIString(PyObject *unicode) |
| 7101 | { |
| 7102 | return _PyUnicode_AsASCIIString(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7103 | } |
| 7104 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 7105 | #ifdef HAVE_MBCS |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 7106 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7107 | /* --- MBCS codecs for Windows -------------------------------------------- */ |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 7108 | |
Hirokazu Yamamoto | 3530246 | 2009-03-21 13:23:27 +0000 | [diff] [blame] | 7109 | #if SIZEOF_INT < SIZEOF_SIZE_T |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7110 | #define NEED_RETRY |
| 7111 | #endif |
| 7112 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7113 | #ifndef WC_ERR_INVALID_CHARS |
| 7114 | # define WC_ERR_INVALID_CHARS 0x0080 |
| 7115 | #endif |
| 7116 | |
| 7117 | static char* |
| 7118 | code_page_name(UINT code_page, PyObject **obj) |
| 7119 | { |
| 7120 | *obj = NULL; |
| 7121 | if (code_page == CP_ACP) |
| 7122 | return "mbcs"; |
| 7123 | if (code_page == CP_UTF7) |
| 7124 | return "CP_UTF7"; |
| 7125 | if (code_page == CP_UTF8) |
| 7126 | return "CP_UTF8"; |
| 7127 | |
| 7128 | *obj = PyBytes_FromFormat("cp%u", code_page); |
| 7129 | if (*obj == NULL) |
| 7130 | return NULL; |
| 7131 | return PyBytes_AS_STRING(*obj); |
| 7132 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7133 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7134 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7135 | is_dbcs_lead_byte(UINT code_page, const char *s, int offset) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7136 | { |
| 7137 | const char *curr = s + offset; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7138 | const char *prev; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7139 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7140 | if (!IsDBCSLeadByteEx(code_page, *curr)) |
| 7141 | return 0; |
| 7142 | |
| 7143 | prev = CharPrevExA(code_page, s, curr, 0); |
| 7144 | if (prev == curr) |
| 7145 | return 1; |
| 7146 | /* FIXME: This code is limited to "true" double-byte encodings, |
| 7147 | as it assumes an incomplete character consists of a single |
| 7148 | byte. */ |
| 7149 | if (curr - prev == 2) |
| 7150 | return 1; |
| 7151 | if (!IsDBCSLeadByteEx(code_page, *prev)) |
| 7152 | return 1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7153 | return 0; |
| 7154 | } |
| 7155 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7156 | static DWORD |
| 7157 | decode_code_page_flags(UINT code_page) |
| 7158 | { |
| 7159 | if (code_page == CP_UTF7) { |
| 7160 | /* The CP_UTF7 decoder only supports flags=0 */ |
| 7161 | return 0; |
| 7162 | } |
| 7163 | else |
| 7164 | return MB_ERR_INVALID_CHARS; |
| 7165 | } |
| 7166 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7167 | /* |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7168 | * Decode a byte string from a Windows code page into unicode object in strict |
| 7169 | * mode. |
| 7170 | * |
| 7171 | * Returns consumed size if succeed, returns -2 on decode error, or raise a |
| 7172 | * WindowsError and returns -1 on other error. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7173 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7174 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7175 | decode_code_page_strict(UINT code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7176 | PyObject **v, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7177 | const char *in, |
| 7178 | int insize) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7179 | { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7180 | const DWORD flags = decode_code_page_flags(code_page); |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 7181 | wchar_t *out; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7182 | DWORD outsize; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7183 | |
| 7184 | /* First get the size of the result */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7185 | assert(insize > 0); |
| 7186 | outsize = MultiByteToWideChar(code_page, flags, in, insize, NULL, 0); |
| 7187 | if (outsize <= 0) |
| 7188 | goto error; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7189 | |
| 7190 | if (*v == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7191 | /* Create unicode object */ |
Victor Stinner | ab59594 | 2011-12-17 04:59:06 +0100 | [diff] [blame] | 7192 | /* FIXME: don't use _PyUnicode_New(), but allocate a wchar_t* buffer */ |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7193 | *v = (PyObject*)_PyUnicode_New(outsize); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7194 | if (*v == NULL) |
| 7195 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7196 | out = PyUnicode_AS_UNICODE(*v); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7197 | } |
| 7198 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7199 | /* Extend unicode object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7200 | Py_ssize_t n = PyUnicode_GET_SIZE(*v); |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 7201 | if (unicode_resize(v, n + outsize) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7202 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7203 | out = PyUnicode_AS_UNICODE(*v) + n; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7204 | } |
| 7205 | |
| 7206 | /* Do the conversion */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7207 | outsize = MultiByteToWideChar(code_page, flags, in, insize, out, outsize); |
| 7208 | if (outsize <= 0) |
| 7209 | goto error; |
| 7210 | return insize; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7211 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7212 | error: |
| 7213 | if (GetLastError() == ERROR_NO_UNICODE_TRANSLATION) |
| 7214 | return -2; |
| 7215 | PyErr_SetFromWindowsErr(0); |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7216 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7217 | } |
| 7218 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7219 | /* |
| 7220 | * Decode a byte string from a code page into unicode object with an error |
| 7221 | * handler. |
| 7222 | * |
| 7223 | * Returns consumed size if succeed, or raise a WindowsError or |
| 7224 | * UnicodeDecodeError exception and returns -1 on error. |
| 7225 | */ |
| 7226 | static int |
| 7227 | decode_code_page_errors(UINT code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7228 | PyObject **v, |
| 7229 | const char *in, const int size, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7230 | const char *errors) |
| 7231 | { |
| 7232 | const char *startin = in; |
| 7233 | const char *endin = in + size; |
| 7234 | const DWORD flags = decode_code_page_flags(code_page); |
| 7235 | /* Ideally, we should get reason from FormatMessage. This is the Windows |
| 7236 | 2000 English version of the message. */ |
| 7237 | const char *reason = "No mapping for the Unicode character exists " |
| 7238 | "in the target code page."; |
| 7239 | /* each step cannot decode more than 1 character, but a character can be |
| 7240 | represented as a surrogate pair */ |
| 7241 | wchar_t buffer[2], *startout, *out; |
| 7242 | int insize, outsize; |
| 7243 | PyObject *errorHandler = NULL; |
| 7244 | PyObject *exc = NULL; |
| 7245 | PyObject *encoding_obj = NULL; |
| 7246 | char *encoding; |
| 7247 | DWORD err; |
| 7248 | int ret = -1; |
| 7249 | |
| 7250 | assert(size > 0); |
| 7251 | |
| 7252 | encoding = code_page_name(code_page, &encoding_obj); |
| 7253 | if (encoding == NULL) |
| 7254 | return -1; |
| 7255 | |
| 7256 | if (errors == NULL || strcmp(errors, "strict") == 0) { |
| 7257 | /* The last error was ERROR_NO_UNICODE_TRANSLATION, then we raise a |
| 7258 | UnicodeDecodeError. */ |
| 7259 | make_decode_exception(&exc, encoding, in, size, 0, 0, reason); |
| 7260 | if (exc != NULL) { |
| 7261 | PyCodec_StrictErrors(exc); |
| 7262 | Py_CLEAR(exc); |
| 7263 | } |
| 7264 | goto error; |
| 7265 | } |
| 7266 | |
| 7267 | if (*v == NULL) { |
| 7268 | /* Create unicode object */ |
| 7269 | if (size > PY_SSIZE_T_MAX / (Py_ssize_t)Py_ARRAY_LENGTH(buffer)) { |
| 7270 | PyErr_NoMemory(); |
| 7271 | goto error; |
| 7272 | } |
Victor Stinner | ab59594 | 2011-12-17 04:59:06 +0100 | [diff] [blame] | 7273 | /* FIXME: don't use _PyUnicode_New(), but allocate a wchar_t* buffer */ |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7274 | *v = (PyObject*)_PyUnicode_New(size * Py_ARRAY_LENGTH(buffer)); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7275 | if (*v == NULL) |
| 7276 | goto error; |
| 7277 | startout = PyUnicode_AS_UNICODE(*v); |
| 7278 | } |
| 7279 | else { |
| 7280 | /* Extend unicode object */ |
| 7281 | Py_ssize_t n = PyUnicode_GET_SIZE(*v); |
| 7282 | if (size > (PY_SSIZE_T_MAX - n) / (Py_ssize_t)Py_ARRAY_LENGTH(buffer)) { |
| 7283 | PyErr_NoMemory(); |
| 7284 | goto error; |
| 7285 | } |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 7286 | if (unicode_resize(v, n + size * Py_ARRAY_LENGTH(buffer)) < 0) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7287 | goto error; |
| 7288 | startout = PyUnicode_AS_UNICODE(*v) + n; |
| 7289 | } |
| 7290 | |
| 7291 | /* Decode the byte string character per character */ |
| 7292 | out = startout; |
| 7293 | while (in < endin) |
| 7294 | { |
| 7295 | /* Decode a character */ |
| 7296 | insize = 1; |
| 7297 | do |
| 7298 | { |
| 7299 | outsize = MultiByteToWideChar(code_page, flags, |
| 7300 | in, insize, |
| 7301 | buffer, Py_ARRAY_LENGTH(buffer)); |
| 7302 | if (outsize > 0) |
| 7303 | break; |
| 7304 | err = GetLastError(); |
| 7305 | if (err != ERROR_NO_UNICODE_TRANSLATION |
| 7306 | && err != ERROR_INSUFFICIENT_BUFFER) |
| 7307 | { |
| 7308 | PyErr_SetFromWindowsErr(0); |
| 7309 | goto error; |
| 7310 | } |
| 7311 | insize++; |
| 7312 | } |
| 7313 | /* 4=maximum length of a UTF-8 sequence */ |
| 7314 | while (insize <= 4 && (in + insize) <= endin); |
| 7315 | |
| 7316 | if (outsize <= 0) { |
| 7317 | Py_ssize_t startinpos, endinpos, outpos; |
| 7318 | |
| 7319 | startinpos = in - startin; |
| 7320 | endinpos = startinpos + 1; |
| 7321 | outpos = out - PyUnicode_AS_UNICODE(*v); |
| 7322 | if (unicode_decode_call_errorhandler( |
| 7323 | errors, &errorHandler, |
| 7324 | encoding, reason, |
| 7325 | &startin, &endin, &startinpos, &endinpos, &exc, &in, |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 7326 | v, &outpos)) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7327 | { |
| 7328 | goto error; |
| 7329 | } |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 7330 | out = PyUnicode_AS_UNICODE(*v) + outpos; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7331 | } |
| 7332 | else { |
| 7333 | in += insize; |
| 7334 | memcpy(out, buffer, outsize * sizeof(wchar_t)); |
| 7335 | out += outsize; |
| 7336 | } |
| 7337 | } |
| 7338 | |
| 7339 | /* write a NUL character at the end */ |
| 7340 | *out = 0; |
| 7341 | |
| 7342 | /* Extend unicode object */ |
| 7343 | outsize = out - startout; |
| 7344 | assert(outsize <= PyUnicode_WSTR_LENGTH(*v)); |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 7345 | if (unicode_resize(v, outsize) < 0) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7346 | goto error; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7347 | ret = size; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7348 | |
| 7349 | error: |
| 7350 | Py_XDECREF(encoding_obj); |
| 7351 | Py_XDECREF(errorHandler); |
| 7352 | Py_XDECREF(exc); |
| 7353 | return ret; |
| 7354 | } |
| 7355 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7356 | static PyObject * |
| 7357 | decode_code_page_stateful(int code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7358 | const char *s, Py_ssize_t size, |
| 7359 | const char *errors, Py_ssize_t *consumed) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7360 | { |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7361 | PyObject *v = NULL; |
| 7362 | int chunk_size, final, converted, done; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7363 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7364 | if (code_page < 0) { |
| 7365 | PyErr_SetString(PyExc_ValueError, "invalid code page number"); |
| 7366 | return NULL; |
| 7367 | } |
| 7368 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7369 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7370 | *consumed = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7371 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7372 | do |
| 7373 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7374 | #ifdef NEED_RETRY |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7375 | if (size > INT_MAX) { |
| 7376 | chunk_size = INT_MAX; |
| 7377 | final = 0; |
| 7378 | done = 0; |
| 7379 | } |
| 7380 | else |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7381 | #endif |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7382 | { |
| 7383 | chunk_size = (int)size; |
| 7384 | final = (consumed == NULL); |
| 7385 | done = 1; |
| 7386 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7387 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7388 | /* Skip trailing lead-byte unless 'final' is set */ |
| 7389 | if (!final && is_dbcs_lead_byte(code_page, s, chunk_size - 1)) |
| 7390 | --chunk_size; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7391 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7392 | if (chunk_size == 0 && done) { |
| 7393 | if (v != NULL) |
| 7394 | break; |
| 7395 | Py_INCREF(unicode_empty); |
| 7396 | return unicode_empty; |
| 7397 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7398 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7399 | |
| 7400 | converted = decode_code_page_strict(code_page, &v, |
| 7401 | s, chunk_size); |
| 7402 | if (converted == -2) |
| 7403 | converted = decode_code_page_errors(code_page, &v, |
| 7404 | s, chunk_size, |
| 7405 | errors); |
| 7406 | assert(converted != 0); |
| 7407 | |
| 7408 | if (converted < 0) { |
| 7409 | Py_XDECREF(v); |
| 7410 | return NULL; |
| 7411 | } |
| 7412 | |
| 7413 | if (consumed) |
| 7414 | *consumed += converted; |
| 7415 | |
| 7416 | s += converted; |
| 7417 | size -= converted; |
| 7418 | } while (!done); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7419 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 7420 | return unicode_result(v); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7421 | } |
| 7422 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7423 | PyObject * |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7424 | PyUnicode_DecodeCodePageStateful(int code_page, |
| 7425 | const char *s, |
| 7426 | Py_ssize_t size, |
| 7427 | const char *errors, |
| 7428 | Py_ssize_t *consumed) |
| 7429 | { |
| 7430 | return decode_code_page_stateful(code_page, s, size, errors, consumed); |
| 7431 | } |
| 7432 | |
| 7433 | PyObject * |
| 7434 | PyUnicode_DecodeMBCSStateful(const char *s, |
| 7435 | Py_ssize_t size, |
| 7436 | const char *errors, |
| 7437 | Py_ssize_t *consumed) |
| 7438 | { |
| 7439 | return decode_code_page_stateful(CP_ACP, s, size, errors, consumed); |
| 7440 | } |
| 7441 | |
| 7442 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7443 | PyUnicode_DecodeMBCS(const char *s, |
| 7444 | Py_ssize_t size, |
| 7445 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7446 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7447 | return PyUnicode_DecodeMBCSStateful(s, size, errors, NULL); |
| 7448 | } |
| 7449 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7450 | static DWORD |
| 7451 | encode_code_page_flags(UINT code_page, const char *errors) |
| 7452 | { |
| 7453 | if (code_page == CP_UTF8) { |
| 7454 | if (winver.dwMajorVersion >= 6) |
| 7455 | /* CP_UTF8 supports WC_ERR_INVALID_CHARS on Windows Vista |
| 7456 | and later */ |
| 7457 | return WC_ERR_INVALID_CHARS; |
| 7458 | else |
| 7459 | /* CP_UTF8 only supports flags=0 on Windows older than Vista */ |
| 7460 | return 0; |
| 7461 | } |
| 7462 | else if (code_page == CP_UTF7) { |
| 7463 | /* CP_UTF7 only supports flags=0 */ |
| 7464 | return 0; |
| 7465 | } |
| 7466 | else { |
| 7467 | if (errors != NULL && strcmp(errors, "replace") == 0) |
| 7468 | return 0; |
| 7469 | else |
| 7470 | return WC_NO_BEST_FIT_CHARS; |
| 7471 | } |
| 7472 | } |
| 7473 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7474 | /* |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7475 | * Encode a Unicode string to a Windows code page into a byte string in strict |
| 7476 | * mode. |
| 7477 | * |
| 7478 | * Returns consumed characters if succeed, returns -2 on encode error, or raise |
| 7479 | * a WindowsError and returns -1 on other error. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7480 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7481 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7482 | encode_code_page_strict(UINT code_page, PyObject **outbytes, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7483 | PyObject *unicode, Py_ssize_t offset, int len, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7484 | const char* errors) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7485 | { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7486 | BOOL usedDefaultChar = FALSE; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7487 | BOOL *pusedDefaultChar = &usedDefaultChar; |
| 7488 | int outsize; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7489 | PyObject *exc = NULL; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 7490 | wchar_t *p; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7491 | Py_ssize_t size; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7492 | const DWORD flags = encode_code_page_flags(code_page, NULL); |
| 7493 | char *out; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7494 | /* Create a substring so that we can get the UTF-16 representation |
| 7495 | of just the slice under consideration. */ |
| 7496 | PyObject *substring; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7497 | |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7498 | assert(len > 0); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7499 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7500 | if (code_page != CP_UTF8 && code_page != CP_UTF7) |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7501 | pusedDefaultChar = &usedDefaultChar; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7502 | else |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7503 | pusedDefaultChar = NULL; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7504 | |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7505 | substring = PyUnicode_Substring(unicode, offset, offset+len); |
| 7506 | if (substring == NULL) |
| 7507 | return -1; |
| 7508 | p = PyUnicode_AsUnicodeAndSize(substring, &size); |
| 7509 | if (p == NULL) { |
| 7510 | Py_DECREF(substring); |
| 7511 | return -1; |
| 7512 | } |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7513 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7514 | /* First get the size of the result */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7515 | outsize = WideCharToMultiByte(code_page, flags, |
| 7516 | p, size, |
| 7517 | NULL, 0, |
| 7518 | NULL, pusedDefaultChar); |
| 7519 | if (outsize <= 0) |
| 7520 | goto error; |
| 7521 | /* If we used a default char, then we failed! */ |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7522 | if (pusedDefaultChar && *pusedDefaultChar) { |
| 7523 | Py_DECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7524 | return -2; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7525 | } |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7526 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7527 | if (*outbytes == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7528 | /* Create string object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7529 | *outbytes = PyBytes_FromStringAndSize(NULL, outsize); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7530 | if (*outbytes == NULL) { |
| 7531 | Py_DECREF(substring); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7532 | return -1; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7533 | } |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7534 | out = PyBytes_AS_STRING(*outbytes); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7535 | } |
| 7536 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7537 | /* Extend string object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7538 | const Py_ssize_t n = PyBytes_Size(*outbytes); |
| 7539 | if (outsize > PY_SSIZE_T_MAX - n) { |
| 7540 | PyErr_NoMemory(); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7541 | Py_DECREF(substring); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7542 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7543 | } |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7544 | if (_PyBytes_Resize(outbytes, n + outsize) < 0) { |
| 7545 | Py_DECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7546 | return -1; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7547 | } |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7548 | out = PyBytes_AS_STRING(*outbytes) + n; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7549 | } |
| 7550 | |
| 7551 | /* Do the conversion */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7552 | outsize = WideCharToMultiByte(code_page, flags, |
| 7553 | p, size, |
| 7554 | out, outsize, |
| 7555 | NULL, pusedDefaultChar); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7556 | Py_CLEAR(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7557 | if (outsize <= 0) |
| 7558 | goto error; |
| 7559 | if (pusedDefaultChar && *pusedDefaultChar) |
| 7560 | return -2; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7561 | return 0; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7562 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7563 | error: |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7564 | Py_XDECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7565 | if (GetLastError() == ERROR_NO_UNICODE_TRANSLATION) |
| 7566 | return -2; |
| 7567 | PyErr_SetFromWindowsErr(0); |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7568 | return -1; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7569 | } |
| 7570 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7571 | /* |
| 7572 | * Encode a Unicode string to a Windows code page into a byte string using a |
| 7573 | * error handler. |
| 7574 | * |
| 7575 | * Returns consumed characters if succeed, or raise a WindowsError and returns |
| 7576 | * -1 on other error. |
| 7577 | */ |
| 7578 | static int |
| 7579 | encode_code_page_errors(UINT code_page, PyObject **outbytes, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7580 | PyObject *unicode, Py_ssize_t unicode_offset, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7581 | Py_ssize_t insize, const char* errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7582 | { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7583 | const DWORD flags = encode_code_page_flags(code_page, errors); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7584 | Py_ssize_t pos = unicode_offset; |
| 7585 | Py_ssize_t endin = unicode_offset + insize; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7586 | /* Ideally, we should get reason from FormatMessage. This is the Windows |
| 7587 | 2000 English version of the message. */ |
| 7588 | const char *reason = "invalid character"; |
| 7589 | /* 4=maximum length of a UTF-8 sequence */ |
| 7590 | char buffer[4]; |
| 7591 | BOOL usedDefaultChar = FALSE, *pusedDefaultChar; |
| 7592 | Py_ssize_t outsize; |
| 7593 | char *out; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7594 | PyObject *errorHandler = NULL; |
| 7595 | PyObject *exc = NULL; |
| 7596 | PyObject *encoding_obj = NULL; |
| 7597 | char *encoding; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7598 | Py_ssize_t newpos, newoutsize; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7599 | PyObject *rep; |
| 7600 | int ret = -1; |
| 7601 | |
| 7602 | assert(insize > 0); |
| 7603 | |
| 7604 | encoding = code_page_name(code_page, &encoding_obj); |
| 7605 | if (encoding == NULL) |
| 7606 | return -1; |
| 7607 | |
| 7608 | if (errors == NULL || strcmp(errors, "strict") == 0) { |
| 7609 | /* The last error was ERROR_NO_UNICODE_TRANSLATION, |
| 7610 | then we raise a UnicodeEncodeError. */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 7611 | make_encode_exception(&exc, encoding, unicode, 0, 0, reason); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7612 | if (exc != NULL) { |
| 7613 | PyCodec_StrictErrors(exc); |
| 7614 | Py_DECREF(exc); |
| 7615 | } |
| 7616 | Py_XDECREF(encoding_obj); |
| 7617 | return -1; |
| 7618 | } |
| 7619 | |
| 7620 | if (code_page != CP_UTF8 && code_page != CP_UTF7) |
| 7621 | pusedDefaultChar = &usedDefaultChar; |
| 7622 | else |
| 7623 | pusedDefaultChar = NULL; |
| 7624 | |
| 7625 | if (Py_ARRAY_LENGTH(buffer) > PY_SSIZE_T_MAX / insize) { |
| 7626 | PyErr_NoMemory(); |
| 7627 | goto error; |
| 7628 | } |
| 7629 | outsize = insize * Py_ARRAY_LENGTH(buffer); |
| 7630 | |
| 7631 | if (*outbytes == NULL) { |
| 7632 | /* Create string object */ |
| 7633 | *outbytes = PyBytes_FromStringAndSize(NULL, outsize); |
| 7634 | if (*outbytes == NULL) |
| 7635 | goto error; |
| 7636 | out = PyBytes_AS_STRING(*outbytes); |
| 7637 | } |
| 7638 | else { |
| 7639 | /* Extend string object */ |
| 7640 | Py_ssize_t n = PyBytes_Size(*outbytes); |
| 7641 | if (n > PY_SSIZE_T_MAX - outsize) { |
| 7642 | PyErr_NoMemory(); |
| 7643 | goto error; |
| 7644 | } |
| 7645 | if (_PyBytes_Resize(outbytes, n + outsize) < 0) |
| 7646 | goto error; |
| 7647 | out = PyBytes_AS_STRING(*outbytes) + n; |
| 7648 | } |
| 7649 | |
| 7650 | /* Encode the string character per character */ |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7651 | while (pos < endin) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7652 | { |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7653 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, pos); |
| 7654 | wchar_t chars[2]; |
| 7655 | int charsize; |
| 7656 | if (ch < 0x10000) { |
| 7657 | chars[0] = (wchar_t)ch; |
| 7658 | charsize = 1; |
| 7659 | } |
| 7660 | else { |
| 7661 | ch -= 0x10000; |
| 7662 | chars[0] = 0xd800 + (ch >> 10); |
| 7663 | chars[1] = 0xdc00 + (ch & 0x3ff); |
| 7664 | charsize = 2; |
| 7665 | } |
| 7666 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7667 | outsize = WideCharToMultiByte(code_page, flags, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7668 | chars, charsize, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7669 | buffer, Py_ARRAY_LENGTH(buffer), |
| 7670 | NULL, pusedDefaultChar); |
| 7671 | if (outsize > 0) { |
| 7672 | if (pusedDefaultChar == NULL || !(*pusedDefaultChar)) |
| 7673 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7674 | pos++; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7675 | memcpy(out, buffer, outsize); |
| 7676 | out += outsize; |
| 7677 | continue; |
| 7678 | } |
| 7679 | } |
| 7680 | else if (GetLastError() != ERROR_NO_UNICODE_TRANSLATION) { |
| 7681 | PyErr_SetFromWindowsErr(0); |
| 7682 | goto error; |
| 7683 | } |
| 7684 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7685 | rep = unicode_encode_call_errorhandler( |
| 7686 | errors, &errorHandler, encoding, reason, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7687 | unicode, &exc, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7688 | pos, pos + 1, &newpos); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7689 | if (rep == NULL) |
| 7690 | goto error; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7691 | pos = newpos; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7692 | |
| 7693 | if (PyBytes_Check(rep)) { |
| 7694 | outsize = PyBytes_GET_SIZE(rep); |
| 7695 | if (outsize != 1) { |
| 7696 | Py_ssize_t offset = out - PyBytes_AS_STRING(*outbytes); |
| 7697 | newoutsize = PyBytes_GET_SIZE(*outbytes) + (outsize - 1); |
| 7698 | if (_PyBytes_Resize(outbytes, newoutsize) < 0) { |
| 7699 | Py_DECREF(rep); |
| 7700 | goto error; |
| 7701 | } |
| 7702 | out = PyBytes_AS_STRING(*outbytes) + offset; |
| 7703 | } |
| 7704 | memcpy(out, PyBytes_AS_STRING(rep), outsize); |
| 7705 | out += outsize; |
| 7706 | } |
| 7707 | else { |
| 7708 | Py_ssize_t i; |
| 7709 | enum PyUnicode_Kind kind; |
| 7710 | void *data; |
| 7711 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7712 | if (PyUnicode_READY(rep) == -1) { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7713 | Py_DECREF(rep); |
| 7714 | goto error; |
| 7715 | } |
| 7716 | |
| 7717 | outsize = PyUnicode_GET_LENGTH(rep); |
| 7718 | if (outsize != 1) { |
| 7719 | Py_ssize_t offset = out - PyBytes_AS_STRING(*outbytes); |
| 7720 | newoutsize = PyBytes_GET_SIZE(*outbytes) + (outsize - 1); |
| 7721 | if (_PyBytes_Resize(outbytes, newoutsize) < 0) { |
| 7722 | Py_DECREF(rep); |
| 7723 | goto error; |
| 7724 | } |
| 7725 | out = PyBytes_AS_STRING(*outbytes) + offset; |
| 7726 | } |
| 7727 | kind = PyUnicode_KIND(rep); |
| 7728 | data = PyUnicode_DATA(rep); |
| 7729 | for (i=0; i < outsize; i++) { |
| 7730 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 7731 | if (ch > 127) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 7732 | raise_encode_exception(&exc, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7733 | encoding, unicode, |
| 7734 | pos, pos + 1, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7735 | "unable to encode error handler result to ASCII"); |
| 7736 | Py_DECREF(rep); |
| 7737 | goto error; |
| 7738 | } |
| 7739 | *out = (unsigned char)ch; |
| 7740 | out++; |
| 7741 | } |
| 7742 | } |
| 7743 | Py_DECREF(rep); |
| 7744 | } |
| 7745 | /* write a NUL byte */ |
| 7746 | *out = 0; |
| 7747 | outsize = out - PyBytes_AS_STRING(*outbytes); |
| 7748 | assert(outsize <= PyBytes_GET_SIZE(*outbytes)); |
| 7749 | if (_PyBytes_Resize(outbytes, outsize) < 0) |
| 7750 | goto error; |
| 7751 | ret = 0; |
| 7752 | |
| 7753 | error: |
| 7754 | Py_XDECREF(encoding_obj); |
| 7755 | Py_XDECREF(errorHandler); |
| 7756 | Py_XDECREF(exc); |
| 7757 | return ret; |
| 7758 | } |
| 7759 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7760 | static PyObject * |
| 7761 | encode_code_page(int code_page, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7762 | PyObject *unicode, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7763 | const char *errors) |
| 7764 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7765 | Py_ssize_t len; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7766 | PyObject *outbytes = NULL; |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7767 | Py_ssize_t offset; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7768 | int chunk_len, ret, done; |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7769 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7770 | if (PyUnicode_READY(unicode) == -1) |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7771 | return NULL; |
| 7772 | len = PyUnicode_GET_LENGTH(unicode); |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 7773 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7774 | if (code_page < 0) { |
| 7775 | PyErr_SetString(PyExc_ValueError, "invalid code page number"); |
| 7776 | return NULL; |
| 7777 | } |
| 7778 | |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7779 | if (len == 0) |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7780 | return PyBytes_FromStringAndSize(NULL, 0); |
| 7781 | |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7782 | offset = 0; |
| 7783 | do |
| 7784 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7785 | #ifdef NEED_RETRY |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7786 | /* 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] | 7787 | chunks. */ |
| 7788 | if (len > INT_MAX/2) { |
| 7789 | chunk_len = INT_MAX/2; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7790 | done = 0; |
| 7791 | } |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7792 | else |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7793 | #endif |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7794 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7795 | chunk_len = (int)len; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7796 | done = 1; |
| 7797 | } |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7798 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7799 | ret = encode_code_page_strict(code_page, &outbytes, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7800 | unicode, offset, chunk_len, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7801 | errors); |
| 7802 | if (ret == -2) |
| 7803 | ret = encode_code_page_errors(code_page, &outbytes, |
| 7804 | unicode, offset, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7805 | chunk_len, errors); |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7806 | if (ret < 0) { |
| 7807 | Py_XDECREF(outbytes); |
| 7808 | return NULL; |
| 7809 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7810 | |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7811 | offset += chunk_len; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7812 | len -= chunk_len; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7813 | } while (!done); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7814 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7815 | return outbytes; |
| 7816 | } |
| 7817 | |
| 7818 | PyObject * |
| 7819 | PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
| 7820 | Py_ssize_t size, |
| 7821 | const char *errors) |
| 7822 | { |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7823 | PyObject *unicode, *res; |
| 7824 | unicode = PyUnicode_FromUnicode(p, size); |
| 7825 | if (unicode == NULL) |
| 7826 | return NULL; |
| 7827 | res = encode_code_page(CP_ACP, unicode, errors); |
| 7828 | Py_DECREF(unicode); |
| 7829 | return res; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7830 | } |
| 7831 | |
| 7832 | PyObject * |
| 7833 | PyUnicode_EncodeCodePage(int code_page, |
| 7834 | PyObject *unicode, |
| 7835 | const char *errors) |
| 7836 | { |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7837 | return encode_code_page(code_page, unicode, errors); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7838 | } |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 7839 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7840 | PyObject * |
| 7841 | PyUnicode_AsMBCSString(PyObject *unicode) |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 7842 | { |
| 7843 | if (!PyUnicode_Check(unicode)) { |
| 7844 | PyErr_BadArgument(); |
| 7845 | return NULL; |
| 7846 | } |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7847 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, NULL); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 7848 | } |
| 7849 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7850 | #undef NEED_RETRY |
| 7851 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 7852 | #endif /* HAVE_MBCS */ |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7853 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7854 | /* --- Character Mapping Codec -------------------------------------------- */ |
| 7855 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7856 | PyObject * |
| 7857 | PyUnicode_DecodeCharmap(const char *s, |
| 7858 | Py_ssize_t size, |
| 7859 | PyObject *mapping, |
| 7860 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7861 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7862 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7863 | Py_ssize_t startinpos; |
| 7864 | Py_ssize_t endinpos; |
| 7865 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7866 | const char *e; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 7867 | PyObject *v; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7868 | Py_ssize_t extrachars = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7869 | PyObject *errorHandler = NULL; |
| 7870 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7871 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7872 | /* Default to Latin-1 */ |
| 7873 | if (mapping == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7874 | return PyUnicode_DecodeLatin1(s, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7875 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7876 | v = PyUnicode_New(size, 127); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7877 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7878 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7879 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 7880 | return v; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7881 | outpos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7882 | e = s + size; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7883 | if (PyUnicode_CheckExact(mapping)) { |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7884 | Py_ssize_t maplen; |
| 7885 | enum PyUnicode_Kind kind; |
| 7886 | void *data; |
| 7887 | Py_UCS4 x; |
| 7888 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7889 | if (PyUnicode_READY(mapping) == -1) |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7890 | return NULL; |
| 7891 | |
| 7892 | maplen = PyUnicode_GET_LENGTH(mapping); |
| 7893 | data = PyUnicode_DATA(mapping); |
| 7894 | kind = PyUnicode_KIND(mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7895 | while (s < e) { |
| 7896 | unsigned char ch = *s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7897 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7898 | if (ch < maplen) |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7899 | x = PyUnicode_READ(kind, data, ch); |
| 7900 | else |
| 7901 | x = 0xfffe; /* invalid value */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7902 | |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7903 | if (x == 0xfffe) |
| 7904 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7905 | /* undefined mapping */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7906 | startinpos = s-starts; |
| 7907 | endinpos = startinpos+1; |
| 7908 | if (unicode_decode_call_errorhandler( |
| 7909 | errors, &errorHandler, |
| 7910 | "charmap", "character maps to <undefined>", |
| 7911 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7912 | &v, &outpos)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7913 | goto onError; |
| 7914 | } |
| 7915 | continue; |
| 7916 | } |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7917 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7918 | if (unicode_putchar(&v, &outpos, x) < 0) |
| 7919 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7920 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7921 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7922 | } |
| 7923 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7924 | while (s < e) { |
| 7925 | unsigned char ch = *s; |
| 7926 | PyObject *w, *x; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7927 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7928 | /* Get mapping (char ordinal -> integer, Unicode char or None) */ |
| 7929 | w = PyLong_FromLong((long)ch); |
| 7930 | if (w == NULL) |
| 7931 | goto onError; |
| 7932 | x = PyObject_GetItem(mapping, w); |
| 7933 | Py_DECREF(w); |
| 7934 | if (x == NULL) { |
| 7935 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 7936 | /* No mapping found means: mapping is undefined. */ |
| 7937 | PyErr_Clear(); |
| 7938 | x = Py_None; |
| 7939 | Py_INCREF(x); |
| 7940 | } else |
| 7941 | goto onError; |
| 7942 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7943 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7944 | /* Apply mapping */ |
| 7945 | if (PyLong_Check(x)) { |
| 7946 | long value = PyLong_AS_LONG(x); |
| 7947 | if (value < 0 || value > 65535) { |
| 7948 | PyErr_SetString(PyExc_TypeError, |
| 7949 | "character mapping must be in range(65536)"); |
| 7950 | Py_DECREF(x); |
| 7951 | goto onError; |
| 7952 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7953 | if (unicode_putchar(&v, &outpos, value) < 0) |
| 7954 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7955 | } |
| 7956 | else if (x == Py_None) { |
| 7957 | /* undefined mapping */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7958 | startinpos = s-starts; |
| 7959 | endinpos = startinpos+1; |
| 7960 | if (unicode_decode_call_errorhandler( |
| 7961 | errors, &errorHandler, |
| 7962 | "charmap", "character maps to <undefined>", |
| 7963 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7964 | &v, &outpos)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7965 | Py_DECREF(x); |
| 7966 | goto onError; |
| 7967 | } |
| 7968 | Py_DECREF(x); |
| 7969 | continue; |
| 7970 | } |
| 7971 | else if (PyUnicode_Check(x)) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7972 | Py_ssize_t targetsize; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7973 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7974 | if (PyUnicode_READY(x) == -1) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7975 | goto onError; |
| 7976 | targetsize = PyUnicode_GET_LENGTH(x); |
| 7977 | |
| 7978 | if (targetsize == 1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7979 | /* 1-1 mapping */ |
Victor Stinner | 62aa4d0 | 2011-11-09 00:03:45 +0100 | [diff] [blame] | 7980 | if (unicode_putchar(&v, &outpos, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7981 | PyUnicode_READ_CHAR(x, 0)) < 0) |
| 7982 | goto onError; |
| 7983 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7984 | else if (targetsize > 1) { |
| 7985 | /* 1-n mapping */ |
| 7986 | if (targetsize > extrachars) { |
| 7987 | /* resize first */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7988 | Py_ssize_t needed = (targetsize - extrachars) + \ |
| 7989 | (targetsize << 2); |
| 7990 | extrachars += needed; |
| 7991 | /* XXX overflow detection missing */ |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 7992 | if (unicode_resize(&v, |
| 7993 | PyUnicode_GET_LENGTH(v) + needed) < 0) |
| 7994 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7995 | Py_DECREF(x); |
| 7996 | goto onError; |
| 7997 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7998 | } |
Victor Stinner | 1b487b4 | 2012-05-03 12:29:04 +0200 | [diff] [blame] | 7999 | if (unicode_widen(&v, outpos, PyUnicode_MAX_CHAR_VALUE(x)) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 8000 | goto onError; |
| 8001 | PyUnicode_CopyCharacters(v, outpos, x, 0, targetsize); |
| 8002 | outpos += targetsize; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8003 | extrachars -= targetsize; |
| 8004 | } |
| 8005 | /* 1-0 mapping: skip the character */ |
| 8006 | } |
| 8007 | else { |
| 8008 | /* wrong return value */ |
| 8009 | PyErr_SetString(PyExc_TypeError, |
| 8010 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8011 | Py_DECREF(x); |
| 8012 | goto onError; |
| 8013 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8014 | Py_DECREF(x); |
| 8015 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8016 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8017 | } |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 8018 | if (unicode_resize(&v, outpos) < 0) |
Antoine Pitrou | a8f63c0 | 2011-11-08 18:37:16 +0100 | [diff] [blame] | 8019 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8020 | Py_XDECREF(errorHandler); |
| 8021 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 8022 | return unicode_result(v); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8023 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8024 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8025 | Py_XDECREF(errorHandler); |
| 8026 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8027 | Py_XDECREF(v); |
| 8028 | return NULL; |
| 8029 | } |
| 8030 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8031 | /* Charmap encoding: the lookup table */ |
| 8032 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8033 | struct encoding_map { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8034 | PyObject_HEAD |
| 8035 | unsigned char level1[32]; |
| 8036 | int count2, count3; |
| 8037 | unsigned char level23[1]; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8038 | }; |
| 8039 | |
| 8040 | static PyObject* |
| 8041 | encoding_map_size(PyObject *obj, PyObject* args) |
| 8042 | { |
| 8043 | struct encoding_map *map = (struct encoding_map*)obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8044 | return PyLong_FromLong(sizeof(*map) - 1 + 16*map->count2 + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8045 | 128*map->count3); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8046 | } |
| 8047 | |
| 8048 | static PyMethodDef encoding_map_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8049 | {"size", encoding_map_size, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8050 | PyDoc_STR("Return the size (in bytes) of this object") }, |
| 8051 | { 0 } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8052 | }; |
| 8053 | |
| 8054 | static void |
| 8055 | encoding_map_dealloc(PyObject* o) |
| 8056 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8057 | PyObject_FREE(o); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8058 | } |
| 8059 | |
| 8060 | static PyTypeObject EncodingMapType = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8061 | PyVarObject_HEAD_INIT(NULL, 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8062 | "EncodingMap", /*tp_name*/ |
| 8063 | sizeof(struct encoding_map), /*tp_basicsize*/ |
| 8064 | 0, /*tp_itemsize*/ |
| 8065 | /* methods */ |
| 8066 | encoding_map_dealloc, /*tp_dealloc*/ |
| 8067 | 0, /*tp_print*/ |
| 8068 | 0, /*tp_getattr*/ |
| 8069 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 8070 | 0, /*tp_reserved*/ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8071 | 0, /*tp_repr*/ |
| 8072 | 0, /*tp_as_number*/ |
| 8073 | 0, /*tp_as_sequence*/ |
| 8074 | 0, /*tp_as_mapping*/ |
| 8075 | 0, /*tp_hash*/ |
| 8076 | 0, /*tp_call*/ |
| 8077 | 0, /*tp_str*/ |
| 8078 | 0, /*tp_getattro*/ |
| 8079 | 0, /*tp_setattro*/ |
| 8080 | 0, /*tp_as_buffer*/ |
| 8081 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 8082 | 0, /*tp_doc*/ |
| 8083 | 0, /*tp_traverse*/ |
| 8084 | 0, /*tp_clear*/ |
| 8085 | 0, /*tp_richcompare*/ |
| 8086 | 0, /*tp_weaklistoffset*/ |
| 8087 | 0, /*tp_iter*/ |
| 8088 | 0, /*tp_iternext*/ |
| 8089 | encoding_map_methods, /*tp_methods*/ |
| 8090 | 0, /*tp_members*/ |
| 8091 | 0, /*tp_getset*/ |
| 8092 | 0, /*tp_base*/ |
| 8093 | 0, /*tp_dict*/ |
| 8094 | 0, /*tp_descr_get*/ |
| 8095 | 0, /*tp_descr_set*/ |
| 8096 | 0, /*tp_dictoffset*/ |
| 8097 | 0, /*tp_init*/ |
| 8098 | 0, /*tp_alloc*/ |
| 8099 | 0, /*tp_new*/ |
| 8100 | 0, /*tp_free*/ |
| 8101 | 0, /*tp_is_gc*/ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8102 | }; |
| 8103 | |
| 8104 | PyObject* |
| 8105 | PyUnicode_BuildEncodingMap(PyObject* string) |
| 8106 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8107 | PyObject *result; |
| 8108 | struct encoding_map *mresult; |
| 8109 | int i; |
| 8110 | int need_dict = 0; |
| 8111 | unsigned char level1[32]; |
| 8112 | unsigned char level2[512]; |
| 8113 | unsigned char *mlevel1, *mlevel2, *mlevel3; |
| 8114 | int count2 = 0, count3 = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8115 | int kind; |
| 8116 | void *data; |
| 8117 | Py_UCS4 ch; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8118 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8119 | if (!PyUnicode_Check(string) || PyUnicode_GET_LENGTH(string) != 256) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8120 | PyErr_BadArgument(); |
| 8121 | return NULL; |
| 8122 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8123 | kind = PyUnicode_KIND(string); |
| 8124 | data = PyUnicode_DATA(string); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8125 | memset(level1, 0xFF, sizeof level1); |
| 8126 | memset(level2, 0xFF, sizeof level2); |
| 8127 | |
| 8128 | /* If there isn't a one-to-one mapping of NULL to \0, |
| 8129 | or if there are non-BMP characters, we need to use |
| 8130 | a mapping dictionary. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8131 | if (PyUnicode_READ(kind, data, 0) != 0) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8132 | need_dict = 1; |
| 8133 | for (i = 1; i < 256; i++) { |
| 8134 | int l1, l2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8135 | ch = PyUnicode_READ(kind, data, i); |
| 8136 | if (ch == 0 || ch > 0xFFFF) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8137 | need_dict = 1; |
| 8138 | break; |
| 8139 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8140 | if (ch == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8141 | /* unmapped character */ |
| 8142 | continue; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8143 | l1 = ch >> 11; |
| 8144 | l2 = ch >> 7; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8145 | if (level1[l1] == 0xFF) |
| 8146 | level1[l1] = count2++; |
| 8147 | if (level2[l2] == 0xFF) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8148 | level2[l2] = count3++; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8149 | } |
| 8150 | |
| 8151 | if (count2 >= 0xFF || count3 >= 0xFF) |
| 8152 | need_dict = 1; |
| 8153 | |
| 8154 | if (need_dict) { |
| 8155 | PyObject *result = PyDict_New(); |
| 8156 | PyObject *key, *value; |
| 8157 | if (!result) |
| 8158 | return NULL; |
| 8159 | for (i = 0; i < 256; i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8160 | key = PyLong_FromLong(PyUnicode_READ(kind, data, i)); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8161 | value = PyLong_FromLong(i); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8162 | if (!key || !value) |
| 8163 | goto failed1; |
| 8164 | if (PyDict_SetItem(result, key, value) == -1) |
| 8165 | goto failed1; |
| 8166 | Py_DECREF(key); |
| 8167 | Py_DECREF(value); |
| 8168 | } |
| 8169 | return result; |
| 8170 | failed1: |
| 8171 | Py_XDECREF(key); |
| 8172 | Py_XDECREF(value); |
| 8173 | Py_DECREF(result); |
| 8174 | return NULL; |
| 8175 | } |
| 8176 | |
| 8177 | /* Create a three-level trie */ |
| 8178 | result = PyObject_MALLOC(sizeof(struct encoding_map) + |
| 8179 | 16*count2 + 128*count3 - 1); |
| 8180 | if (!result) |
| 8181 | return PyErr_NoMemory(); |
| 8182 | PyObject_Init(result, &EncodingMapType); |
| 8183 | mresult = (struct encoding_map*)result; |
| 8184 | mresult->count2 = count2; |
| 8185 | mresult->count3 = count3; |
| 8186 | mlevel1 = mresult->level1; |
| 8187 | mlevel2 = mresult->level23; |
| 8188 | mlevel3 = mresult->level23 + 16*count2; |
| 8189 | memcpy(mlevel1, level1, 32); |
| 8190 | memset(mlevel2, 0xFF, 16*count2); |
| 8191 | memset(mlevel3, 0, 128*count3); |
| 8192 | count3 = 0; |
| 8193 | for (i = 1; i < 256; i++) { |
| 8194 | int o1, o2, o3, i2, i3; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8195 | if (PyUnicode_READ(kind, data, i) == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8196 | /* unmapped character */ |
| 8197 | continue; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8198 | o1 = PyUnicode_READ(kind, data, i)>>11; |
| 8199 | o2 = (PyUnicode_READ(kind, data, i)>>7) & 0xF; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8200 | i2 = 16*mlevel1[o1] + o2; |
| 8201 | if (mlevel2[i2] == 0xFF) |
| 8202 | mlevel2[i2] = count3++; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8203 | o3 = PyUnicode_READ(kind, data, i) & 0x7F; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8204 | i3 = 128*mlevel2[i2] + o3; |
| 8205 | mlevel3[i3] = i; |
| 8206 | } |
| 8207 | return result; |
| 8208 | } |
| 8209 | |
| 8210 | static int |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 8211 | encoding_map_lookup(Py_UCS4 c, PyObject *mapping) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8212 | { |
| 8213 | struct encoding_map *map = (struct encoding_map*)mapping; |
| 8214 | int l1 = c>>11; |
| 8215 | int l2 = (c>>7) & 0xF; |
| 8216 | int l3 = c & 0x7F; |
| 8217 | int i; |
| 8218 | |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 8219 | if (c > 0xFFFF) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8220 | return -1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8221 | if (c == 0) |
| 8222 | return 0; |
| 8223 | /* level 1*/ |
| 8224 | i = map->level1[l1]; |
| 8225 | if (i == 0xFF) { |
| 8226 | return -1; |
| 8227 | } |
| 8228 | /* level 2*/ |
| 8229 | i = map->level23[16*i+l2]; |
| 8230 | if (i == 0xFF) { |
| 8231 | return -1; |
| 8232 | } |
| 8233 | /* level 3 */ |
| 8234 | i = map->level23[16*map->count2 + 128*i + l3]; |
| 8235 | if (i == 0) { |
| 8236 | return -1; |
| 8237 | } |
| 8238 | return i; |
| 8239 | } |
| 8240 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8241 | /* Lookup the character ch in the mapping. If the character |
| 8242 | can't be found, Py_None is returned (or NULL, if another |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 8243 | error occurred). */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8244 | static PyObject * |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 8245 | charmapencode_lookup(Py_UCS4 c, PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8246 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8247 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8248 | PyObject *x; |
| 8249 | |
| 8250 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8251 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8252 | x = PyObject_GetItem(mapping, w); |
| 8253 | Py_DECREF(w); |
| 8254 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8255 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 8256 | /* No mapping found means: mapping is undefined. */ |
| 8257 | PyErr_Clear(); |
| 8258 | x = Py_None; |
| 8259 | Py_INCREF(x); |
| 8260 | return x; |
| 8261 | } else |
| 8262 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8263 | } |
Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 8264 | else if (x == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8265 | return x; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8266 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8267 | long value = PyLong_AS_LONG(x); |
| 8268 | if (value < 0 || value > 255) { |
| 8269 | PyErr_SetString(PyExc_TypeError, |
| 8270 | "character mapping must be in range(256)"); |
| 8271 | Py_DECREF(x); |
| 8272 | return NULL; |
| 8273 | } |
| 8274 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8275 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8276 | else if (PyBytes_Check(x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8277 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8278 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8279 | /* wrong return value */ |
| 8280 | PyErr_Format(PyExc_TypeError, |
| 8281 | "character mapping must return integer, bytes or None, not %.400s", |
| 8282 | x->ob_type->tp_name); |
| 8283 | Py_DECREF(x); |
| 8284 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8285 | } |
| 8286 | } |
| 8287 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8288 | static int |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 8289 | charmapencode_resize(PyObject **outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8290 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8291 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
| 8292 | /* exponentially overallocate to minimize reallocations */ |
| 8293 | if (requiredsize < 2*outsize) |
| 8294 | requiredsize = 2*outsize; |
| 8295 | if (_PyBytes_Resize(outobj, requiredsize)) |
| 8296 | return -1; |
| 8297 | return 0; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8298 | } |
| 8299 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8300 | typedef enum charmapencode_result { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8301 | enc_SUCCESS, enc_FAILED, enc_EXCEPTION |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8302 | } charmapencode_result; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8303 | /* 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] | 8304 | various state variables. Resize the output bytes object if not enough |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8305 | space is available. Return a new reference to the object that |
| 8306 | was put in the output buffer, or Py_None, if the mapping was undefined |
| 8307 | (in which case no character was written) or NULL, if a |
Andrew M. Kuchling | 8294de5 | 2005-11-02 16:36:12 +0000 | [diff] [blame] | 8308 | reallocation error occurred. The caller must decref the result */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8309 | static charmapencode_result |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 8310 | charmapencode_output(Py_UCS4 c, PyObject *mapping, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8311 | PyObject **outobj, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8312 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8313 | PyObject *rep; |
| 8314 | char *outstart; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8315 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8316 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 8317 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8318 | int res = encoding_map_lookup(c, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8319 | Py_ssize_t requiredsize = *outpos+1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8320 | if (res == -1) |
| 8321 | return enc_FAILED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8322 | if (outsize<requiredsize) |
| 8323 | if (charmapencode_resize(outobj, outpos, requiredsize)) |
| 8324 | return enc_EXCEPTION; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8325 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8326 | outstart[(*outpos)++] = (char)res; |
| 8327 | return enc_SUCCESS; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8328 | } |
| 8329 | |
| 8330 | rep = charmapencode_lookup(c, mapping); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8331 | if (rep==NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8332 | return enc_EXCEPTION; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8333 | else if (rep==Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8334 | Py_DECREF(rep); |
| 8335 | return enc_FAILED; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8336 | } else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8337 | if (PyLong_Check(rep)) { |
| 8338 | Py_ssize_t requiredsize = *outpos+1; |
| 8339 | if (outsize<requiredsize) |
| 8340 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 8341 | Py_DECREF(rep); |
| 8342 | return enc_EXCEPTION; |
| 8343 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8344 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8345 | outstart[(*outpos)++] = (char)PyLong_AS_LONG(rep); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8346 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8347 | else { |
| 8348 | const char *repchars = PyBytes_AS_STRING(rep); |
| 8349 | Py_ssize_t repsize = PyBytes_GET_SIZE(rep); |
| 8350 | Py_ssize_t requiredsize = *outpos+repsize; |
| 8351 | if (outsize<requiredsize) |
| 8352 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 8353 | Py_DECREF(rep); |
| 8354 | return enc_EXCEPTION; |
| 8355 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8356 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8357 | memcpy(outstart + *outpos, repchars, repsize); |
| 8358 | *outpos += repsize; |
| 8359 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8360 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8361 | Py_DECREF(rep); |
| 8362 | return enc_SUCCESS; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8363 | } |
| 8364 | |
| 8365 | /* handle an error in PyUnicode_EncodeCharmap |
| 8366 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8367 | static int |
| 8368 | charmap_encoding_error( |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8369 | PyObject *unicode, Py_ssize_t *inpos, PyObject *mapping, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8370 | PyObject **exceptionObject, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 8371 | int *known_errorHandler, PyObject **errorHandler, const char *errors, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 8372 | PyObject **res, Py_ssize_t *respos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8373 | { |
| 8374 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8375 | Py_ssize_t size, repsize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8376 | Py_ssize_t newpos; |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8377 | enum PyUnicode_Kind kind; |
| 8378 | void *data; |
| 8379 | Py_ssize_t index; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8380 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8381 | Py_ssize_t collstartpos = *inpos; |
| 8382 | Py_ssize_t collendpos = *inpos+1; |
| 8383 | Py_ssize_t collpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8384 | char *encoding = "charmap"; |
| 8385 | char *reason = "character maps to <undefined>"; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8386 | charmapencode_result x; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8387 | Py_UCS4 ch; |
Brian Curtin | 2787ea4 | 2011-11-02 15:09:37 -0500 | [diff] [blame] | 8388 | int val; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8389 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 8390 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8391 | return -1; |
| 8392 | size = PyUnicode_GET_LENGTH(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8393 | /* find all unencodable characters */ |
| 8394 | while (collendpos < size) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8395 | PyObject *rep; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 8396 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8397 | ch = PyUnicode_READ_CHAR(unicode, collendpos); |
Brian Curtin | 2787ea4 | 2011-11-02 15:09:37 -0500 | [diff] [blame] | 8398 | val = encoding_map_lookup(ch, mapping); |
| 8399 | if (val != -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8400 | break; |
| 8401 | ++collendpos; |
| 8402 | continue; |
| 8403 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8404 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8405 | ch = PyUnicode_READ_CHAR(unicode, collendpos); |
| 8406 | rep = charmapencode_lookup(ch, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8407 | if (rep==NULL) |
| 8408 | return -1; |
| 8409 | else if (rep!=Py_None) { |
| 8410 | Py_DECREF(rep); |
| 8411 | break; |
| 8412 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8413 | Py_DECREF(rep); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8414 | ++collendpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8415 | } |
| 8416 | /* cache callback name lookup |
| 8417 | * (if not done yet, i.e. it's the first error) */ |
| 8418 | if (*known_errorHandler==-1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8419 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 8420 | *known_errorHandler = 1; |
| 8421 | else if (!strcmp(errors, "replace")) |
| 8422 | *known_errorHandler = 2; |
| 8423 | else if (!strcmp(errors, "ignore")) |
| 8424 | *known_errorHandler = 3; |
| 8425 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 8426 | *known_errorHandler = 4; |
| 8427 | else |
| 8428 | *known_errorHandler = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8429 | } |
| 8430 | switch (*known_errorHandler) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8431 | case 1: /* strict */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8432 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8433 | return -1; |
| 8434 | case 2: /* replace */ |
| 8435 | for (collpos = collstartpos; collpos<collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8436 | x = charmapencode_output('?', mapping, res, respos); |
| 8437 | if (x==enc_EXCEPTION) { |
| 8438 | return -1; |
| 8439 | } |
| 8440 | else if (x==enc_FAILED) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8441 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8442 | return -1; |
| 8443 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8444 | } |
| 8445 | /* fall through */ |
| 8446 | case 3: /* ignore */ |
| 8447 | *inpos = collendpos; |
| 8448 | break; |
| 8449 | case 4: /* xmlcharrefreplace */ |
| 8450 | /* generate replacement (temporarily (mis)uses p) */ |
| 8451 | for (collpos = collstartpos; collpos < collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8452 | char buffer[2+29+1+1]; |
| 8453 | char *cp; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8454 | sprintf(buffer, "&#%d;", (int)PyUnicode_READ_CHAR(unicode, collpos)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8455 | for (cp = buffer; *cp; ++cp) { |
| 8456 | x = charmapencode_output(*cp, mapping, res, respos); |
| 8457 | if (x==enc_EXCEPTION) |
| 8458 | return -1; |
| 8459 | else if (x==enc_FAILED) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8460 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8461 | return -1; |
| 8462 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8463 | } |
| 8464 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8465 | *inpos = collendpos; |
| 8466 | break; |
| 8467 | default: |
| 8468 | repunicode = unicode_encode_call_errorhandler(errors, errorHandler, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8469 | encoding, reason, unicode, exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8470 | collstartpos, collendpos, &newpos); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8471 | if (repunicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8472 | return -1; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8473 | if (PyBytes_Check(repunicode)) { |
| 8474 | /* Directly copy bytes result to output. */ |
| 8475 | Py_ssize_t outsize = PyBytes_Size(*res); |
| 8476 | Py_ssize_t requiredsize; |
| 8477 | repsize = PyBytes_Size(repunicode); |
| 8478 | requiredsize = *respos + repsize; |
| 8479 | if (requiredsize > outsize) |
| 8480 | /* Make room for all additional bytes. */ |
| 8481 | if (charmapencode_resize(res, respos, requiredsize)) { |
| 8482 | Py_DECREF(repunicode); |
| 8483 | return -1; |
| 8484 | } |
| 8485 | memcpy(PyBytes_AsString(*res) + *respos, |
| 8486 | PyBytes_AsString(repunicode), repsize); |
| 8487 | *respos += repsize; |
| 8488 | *inpos = newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 8489 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8490 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 8491 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8492 | /* generate replacement */ |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 8493 | if (PyUnicode_READY(repunicode) == -1) { |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8494 | Py_DECREF(repunicode); |
| 8495 | return -1; |
| 8496 | } |
Victor Stinner | 9e30aa5 | 2011-11-21 02:49:52 +0100 | [diff] [blame] | 8497 | repsize = PyUnicode_GET_LENGTH(repunicode); |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8498 | data = PyUnicode_DATA(repunicode); |
| 8499 | kind = PyUnicode_KIND(repunicode); |
| 8500 | for (index = 0; index < repsize; index++) { |
| 8501 | Py_UCS4 repch = PyUnicode_READ(kind, data, index); |
| 8502 | x = charmapencode_output(repch, mapping, res, respos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8503 | if (x==enc_EXCEPTION) { |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8504 | Py_DECREF(repunicode); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8505 | return -1; |
| 8506 | } |
| 8507 | else if (x==enc_FAILED) { |
| 8508 | Py_DECREF(repunicode); |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8509 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8510 | return -1; |
| 8511 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8512 | } |
| 8513 | *inpos = newpos; |
| 8514 | Py_DECREF(repunicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8515 | } |
| 8516 | return 0; |
| 8517 | } |
| 8518 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8519 | PyObject * |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8520 | _PyUnicode_EncodeCharmap(PyObject *unicode, |
| 8521 | PyObject *mapping, |
| 8522 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8523 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8524 | /* output object */ |
| 8525 | PyObject *res = NULL; |
| 8526 | /* current input position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8527 | Py_ssize_t inpos = 0; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8528 | Py_ssize_t size; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8529 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8530 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8531 | PyObject *errorHandler = NULL; |
| 8532 | PyObject *exc = NULL; |
| 8533 | /* the following variable is used for caching string comparisons |
| 8534 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 8535 | * 3=ignore, 4=xmlcharrefreplace */ |
| 8536 | int known_errorHandler = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8537 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 8538 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8539 | return NULL; |
| 8540 | size = PyUnicode_GET_LENGTH(unicode); |
| 8541 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8542 | /* Default to Latin-1 */ |
| 8543 | if (mapping == NULL) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8544 | return unicode_encode_ucs1(unicode, errors, 256); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8545 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8546 | /* allocate enough for a simple encoding without |
| 8547 | replacements, if we need more, we'll resize */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8548 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8549 | if (res == NULL) |
| 8550 | goto onError; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 8551 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8552 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8553 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8554 | while (inpos<size) { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8555 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, inpos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8556 | /* try to encode it */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8557 | charmapencode_result x = charmapencode_output(ch, mapping, &res, &respos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8558 | if (x==enc_EXCEPTION) /* error */ |
| 8559 | goto onError; |
| 8560 | if (x==enc_FAILED) { /* unencodable character */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8561 | if (charmap_encoding_error(unicode, &inpos, mapping, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8562 | &exc, |
| 8563 | &known_errorHandler, &errorHandler, errors, |
| 8564 | &res, &respos)) { |
| 8565 | goto onError; |
| 8566 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8567 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8568 | else |
| 8569 | /* done with this character => adjust input position */ |
| 8570 | ++inpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8571 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8572 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8573 | /* Resize if we allocated to much */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8574 | if (respos<PyBytes_GET_SIZE(res)) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 8575 | if (_PyBytes_Resize(&res, respos) < 0) |
| 8576 | goto onError; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 8577 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8578 | Py_XDECREF(exc); |
| 8579 | Py_XDECREF(errorHandler); |
| 8580 | return res; |
| 8581 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8582 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8583 | Py_XDECREF(res); |
| 8584 | Py_XDECREF(exc); |
| 8585 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8586 | return NULL; |
| 8587 | } |
| 8588 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8589 | /* Deprecated */ |
| 8590 | PyObject * |
| 8591 | PyUnicode_EncodeCharmap(const Py_UNICODE *p, |
| 8592 | Py_ssize_t size, |
| 8593 | PyObject *mapping, |
| 8594 | const char *errors) |
| 8595 | { |
| 8596 | PyObject *result; |
| 8597 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 8598 | if (unicode == NULL) |
| 8599 | return NULL; |
| 8600 | result = _PyUnicode_EncodeCharmap(unicode, mapping, errors); |
| 8601 | Py_DECREF(unicode); |
Victor Stinner | fc026c9 | 2011-11-04 00:24:51 +0100 | [diff] [blame] | 8602 | return result; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8603 | } |
| 8604 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8605 | PyObject * |
| 8606 | PyUnicode_AsCharmapString(PyObject *unicode, |
| 8607 | PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8608 | { |
| 8609 | if (!PyUnicode_Check(unicode) || mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8610 | PyErr_BadArgument(); |
| 8611 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8612 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8613 | return _PyUnicode_EncodeCharmap(unicode, mapping, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8614 | } |
| 8615 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8616 | /* create or adjust a UnicodeTranslateError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8617 | static void |
| 8618 | make_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8619 | PyObject *unicode, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8620 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 8621 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8622 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8623 | if (*exceptionObject == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8624 | *exceptionObject = _PyUnicodeTranslateError_Create( |
| 8625 | unicode, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8626 | } |
| 8627 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8628 | if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
| 8629 | goto onError; |
| 8630 | if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) |
| 8631 | goto onError; |
| 8632 | if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) |
| 8633 | goto onError; |
| 8634 | return; |
| 8635 | onError: |
| 8636 | Py_DECREF(*exceptionObject); |
| 8637 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8638 | } |
| 8639 | } |
| 8640 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8641 | /* raises a UnicodeTranslateError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8642 | static void |
| 8643 | raise_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8644 | PyObject *unicode, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8645 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 8646 | const char *reason) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8647 | { |
| 8648 | make_translate_exception(exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8649 | unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8650 | if (*exceptionObject != NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8651 | PyCodec_StrictErrors(*exceptionObject); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8652 | } |
| 8653 | |
| 8654 | /* error handling callback helper: |
| 8655 | build arguments, call the callback and check the arguments, |
| 8656 | put the result into newpos and return the replacement string, which |
| 8657 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8658 | static PyObject * |
| 8659 | unicode_translate_call_errorhandler(const char *errors, |
| 8660 | PyObject **errorHandler, |
| 8661 | const char *reason, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8662 | PyObject *unicode, PyObject **exceptionObject, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8663 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 8664 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8665 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 8666 | 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] | 8667 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8668 | Py_ssize_t i_newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8669 | PyObject *restuple; |
| 8670 | PyObject *resunicode; |
| 8671 | |
| 8672 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8673 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8674 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8675 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8676 | } |
| 8677 | |
| 8678 | make_translate_exception(exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8679 | unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8680 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8681 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8682 | |
| 8683 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8684 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8685 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8686 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8687 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 8688 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8689 | Py_DECREF(restuple); |
| 8690 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8691 | } |
| 8692 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8693 | &resunicode, &i_newpos)) { |
| 8694 | Py_DECREF(restuple); |
| 8695 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8696 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8697 | if (i_newpos<0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8698 | *newpos = PyUnicode_GET_LENGTH(unicode)+i_newpos; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8699 | else |
| 8700 | *newpos = i_newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8701 | if (*newpos<0 || *newpos>PyUnicode_GET_LENGTH(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8702 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 8703 | Py_DECREF(restuple); |
| 8704 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 8705 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8706 | Py_INCREF(resunicode); |
| 8707 | Py_DECREF(restuple); |
| 8708 | return resunicode; |
| 8709 | } |
| 8710 | |
| 8711 | /* Lookup the character ch in the mapping and put the result in result, |
| 8712 | which must be decrefed by the caller. |
| 8713 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8714 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8715 | charmaptranslate_lookup(Py_UCS4 c, PyObject *mapping, PyObject **result) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8716 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8717 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8718 | PyObject *x; |
| 8719 | |
| 8720 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8721 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8722 | x = PyObject_GetItem(mapping, w); |
| 8723 | Py_DECREF(w); |
| 8724 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8725 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 8726 | /* No mapping found means: use 1:1 mapping. */ |
| 8727 | PyErr_Clear(); |
| 8728 | *result = NULL; |
| 8729 | return 0; |
| 8730 | } else |
| 8731 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8732 | } |
| 8733 | else if (x == Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8734 | *result = x; |
| 8735 | return 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8736 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8737 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8738 | long value = PyLong_AS_LONG(x); |
| 8739 | long max = PyUnicode_GetMax(); |
| 8740 | if (value < 0 || value > max) { |
| 8741 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5a2f7e60 | 2007-10-24 21:13:09 +0000 | [diff] [blame] | 8742 | "character mapping must be in range(0x%x)", max+1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8743 | Py_DECREF(x); |
| 8744 | return -1; |
| 8745 | } |
| 8746 | *result = x; |
| 8747 | return 0; |
| 8748 | } |
| 8749 | else if (PyUnicode_Check(x)) { |
| 8750 | *result = x; |
| 8751 | return 0; |
| 8752 | } |
| 8753 | else { |
| 8754 | /* wrong return value */ |
| 8755 | PyErr_SetString(PyExc_TypeError, |
| 8756 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8757 | Py_DECREF(x); |
| 8758 | return -1; |
| 8759 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8760 | } |
| 8761 | /* ensure that *outobj is at least requiredsize characters long, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8762 | if not reallocate and adjust various state variables. |
| 8763 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8764 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8765 | charmaptranslate_makespace(Py_UCS4 **outobj, Py_ssize_t *psize, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8766 | Py_ssize_t requiredsize) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8767 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8768 | Py_ssize_t oldsize = *psize; |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 8769 | if (requiredsize > oldsize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8770 | /* exponentially overallocate to minimize reallocations */ |
| 8771 | if (requiredsize < 2 * oldsize) |
| 8772 | requiredsize = 2 * oldsize; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8773 | *outobj = PyMem_Realloc(*outobj, requiredsize * sizeof(Py_UCS4)); |
| 8774 | if (*outobj == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8775 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8776 | *psize = requiredsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8777 | } |
| 8778 | return 0; |
| 8779 | } |
| 8780 | /* lookup the character, put the result in the output string and adjust |
| 8781 | various state variables. Return a new reference to the object that |
| 8782 | was put in the output buffer in *result, or Py_None, if the mapping was |
| 8783 | undefined (in which case no character was written). |
| 8784 | The called must decref result. |
| 8785 | Return 0 on success, -1 on error. */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8786 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8787 | charmaptranslate_output(PyObject *input, Py_ssize_t ipos, |
| 8788 | PyObject *mapping, Py_UCS4 **output, |
| 8789 | Py_ssize_t *osize, Py_ssize_t *opos, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8790 | PyObject **res) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8791 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8792 | Py_UCS4 curinp = PyUnicode_READ_CHAR(input, ipos); |
| 8793 | if (charmaptranslate_lookup(curinp, mapping, res)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8794 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8795 | if (*res==NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8796 | /* not found => default to 1:1 mapping */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8797 | (*output)[(*opos)++] = curinp; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8798 | } |
| 8799 | else if (*res==Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8800 | ; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8801 | else if (PyLong_Check(*res)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8802 | /* 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] | 8803 | (*output)[(*opos)++] = (Py_UCS4)PyLong_AS_LONG(*res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8804 | } |
| 8805 | else if (PyUnicode_Check(*res)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8806 | Py_ssize_t repsize; |
| 8807 | if (PyUnicode_READY(*res) == -1) |
| 8808 | return -1; |
| 8809 | repsize = PyUnicode_GET_LENGTH(*res); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8810 | if (repsize==1) { |
| 8811 | /* 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] | 8812 | (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8813 | } |
| 8814 | else if (repsize!=0) { |
| 8815 | /* more than one character */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8816 | Py_ssize_t requiredsize = *opos + |
| 8817 | (PyUnicode_GET_LENGTH(input) - ipos) + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8818 | repsize - 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8819 | Py_ssize_t i; |
| 8820 | if (charmaptranslate_makespace(output, osize, requiredsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8821 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8822 | for(i = 0; i < repsize; i++) |
| 8823 | (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, i); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8824 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8825 | } |
| 8826 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8827 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8828 | return 0; |
| 8829 | } |
| 8830 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8831 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8832 | _PyUnicode_TranslateCharmap(PyObject *input, |
| 8833 | PyObject *mapping, |
| 8834 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8835 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8836 | /* input object */ |
| 8837 | char *idata; |
| 8838 | Py_ssize_t size, i; |
| 8839 | int kind; |
| 8840 | /* output buffer */ |
| 8841 | Py_UCS4 *output = NULL; |
| 8842 | Py_ssize_t osize; |
| 8843 | PyObject *res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8844 | /* current output position */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8845 | Py_ssize_t opos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8846 | char *reason = "character maps to <undefined>"; |
| 8847 | PyObject *errorHandler = NULL; |
| 8848 | PyObject *exc = NULL; |
| 8849 | /* the following variable is used for caching string comparisons |
| 8850 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 8851 | * 3=ignore, 4=xmlcharrefreplace */ |
| 8852 | int known_errorHandler = -1; |
| 8853 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8854 | if (mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8855 | PyErr_BadArgument(); |
| 8856 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8857 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8858 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8859 | if (PyUnicode_READY(input) == -1) |
| 8860 | return NULL; |
| 8861 | idata = (char*)PyUnicode_DATA(input); |
| 8862 | kind = PyUnicode_KIND(input); |
| 8863 | size = PyUnicode_GET_LENGTH(input); |
| 8864 | i = 0; |
| 8865 | |
| 8866 | if (size == 0) { |
| 8867 | Py_INCREF(input); |
| 8868 | return input; |
| 8869 | } |
| 8870 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8871 | /* allocate enough for a simple 1:1 translation without |
| 8872 | replacements, if we need more, we'll resize */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8873 | osize = size; |
| 8874 | output = PyMem_Malloc(osize * sizeof(Py_UCS4)); |
| 8875 | opos = 0; |
| 8876 | if (output == NULL) { |
| 8877 | PyErr_NoMemory(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8878 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8879 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8880 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8881 | while (i<size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8882 | /* try to encode it */ |
| 8883 | PyObject *x = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8884 | if (charmaptranslate_output(input, i, mapping, |
| 8885 | &output, &osize, &opos, &x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8886 | Py_XDECREF(x); |
| 8887 | goto onError; |
| 8888 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8889 | Py_XDECREF(x); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8890 | if (x!=Py_None) /* it worked => adjust input pointer */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8891 | ++i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8892 | else { /* untranslatable character */ |
| 8893 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
| 8894 | Py_ssize_t repsize; |
| 8895 | Py_ssize_t newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8896 | Py_ssize_t uni2; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8897 | /* startpos for collecting untranslatable chars */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8898 | Py_ssize_t collstart = i; |
| 8899 | Py_ssize_t collend = i+1; |
| 8900 | Py_ssize_t coll; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8901 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8902 | /* find all untranslatable characters */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8903 | while (collend < size) { |
| 8904 | if (charmaptranslate_lookup(PyUnicode_READ(kind,idata, collend), mapping, &x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8905 | goto onError; |
| 8906 | Py_XDECREF(x); |
| 8907 | if (x!=Py_None) |
| 8908 | break; |
| 8909 | ++collend; |
| 8910 | } |
| 8911 | /* cache callback name lookup |
| 8912 | * (if not done yet, i.e. it's the first error) */ |
| 8913 | if (known_errorHandler==-1) { |
| 8914 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 8915 | known_errorHandler = 1; |
| 8916 | else if (!strcmp(errors, "replace")) |
| 8917 | known_errorHandler = 2; |
| 8918 | else if (!strcmp(errors, "ignore")) |
| 8919 | known_errorHandler = 3; |
| 8920 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 8921 | known_errorHandler = 4; |
| 8922 | else |
| 8923 | known_errorHandler = 0; |
| 8924 | } |
| 8925 | switch (known_errorHandler) { |
| 8926 | case 1: /* strict */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8927 | raise_translate_exception(&exc, input, collstart, |
| 8928 | collend, reason); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8929 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8930 | case 2: /* replace */ |
| 8931 | /* 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] | 8932 | for (coll = collstart; coll<collend; coll++) |
| 8933 | output[opos++] = '?'; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8934 | /* fall through */ |
| 8935 | case 3: /* ignore */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8936 | i = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8937 | break; |
| 8938 | case 4: /* xmlcharrefreplace */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8939 | /* generate replacement (temporarily (mis)uses i) */ |
| 8940 | for (i = collstart; i < collend; ++i) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8941 | char buffer[2+29+1+1]; |
| 8942 | char *cp; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8943 | sprintf(buffer, "&#%d;", PyUnicode_READ(kind, idata, i)); |
| 8944 | if (charmaptranslate_makespace(&output, &osize, |
| 8945 | opos+strlen(buffer)+(size-collend))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8946 | goto onError; |
| 8947 | for (cp = buffer; *cp; ++cp) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8948 | output[opos++] = *cp; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8949 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8950 | i = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8951 | break; |
| 8952 | default: |
| 8953 | repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8954 | reason, input, &exc, |
| 8955 | collstart, collend, &newpos); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 8956 | if (repunicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8957 | goto onError; |
Benjamin Peterson | 9ca3ffa | 2012-01-01 16:04:29 -0600 | [diff] [blame] | 8958 | if (PyUnicode_READY(repunicode) == -1) { |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 8959 | Py_DECREF(repunicode); |
| 8960 | goto onError; |
| 8961 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8962 | /* generate replacement */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8963 | repsize = PyUnicode_GET_LENGTH(repunicode); |
| 8964 | if (charmaptranslate_makespace(&output, &osize, |
| 8965 | opos+repsize+(size-collend))) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8966 | Py_DECREF(repunicode); |
| 8967 | goto onError; |
| 8968 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8969 | for (uni2 = 0; repsize-->0; ++uni2) |
| 8970 | output[opos++] = PyUnicode_READ_CHAR(repunicode, uni2); |
| 8971 | i = newpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8972 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8973 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8974 | } |
| 8975 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8976 | res = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, output, opos); |
| 8977 | if (!res) |
| 8978 | goto onError; |
| 8979 | PyMem_Free(output); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8980 | Py_XDECREF(exc); |
| 8981 | Py_XDECREF(errorHandler); |
| 8982 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8983 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8984 | onError: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8985 | PyMem_Free(output); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8986 | Py_XDECREF(exc); |
| 8987 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8988 | return NULL; |
| 8989 | } |
| 8990 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8991 | /* Deprecated. Use PyUnicode_Translate instead. */ |
| 8992 | PyObject * |
| 8993 | PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
| 8994 | Py_ssize_t size, |
| 8995 | PyObject *mapping, |
| 8996 | const char *errors) |
| 8997 | { |
| 8998 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 8999 | if (!unicode) |
| 9000 | return NULL; |
| 9001 | return _PyUnicode_TranslateCharmap(unicode, mapping, errors); |
| 9002 | } |
| 9003 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9004 | PyObject * |
| 9005 | PyUnicode_Translate(PyObject *str, |
| 9006 | PyObject *mapping, |
| 9007 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9008 | { |
| 9009 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9010 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9011 | str = PyUnicode_FromObject(str); |
| 9012 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9013 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9014 | result = _PyUnicode_TranslateCharmap(str, mapping, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9015 | Py_DECREF(str); |
| 9016 | return result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9017 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9018 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9019 | Py_XDECREF(str); |
| 9020 | return NULL; |
| 9021 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9022 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9023 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9024 | fix_decimal_and_space_to_ascii(PyObject *self) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9025 | { |
| 9026 | /* No need to call PyUnicode_READY(self) because this function is only |
| 9027 | called as a callback from fixup() which does it already. */ |
| 9028 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 9029 | const int kind = PyUnicode_KIND(self); |
| 9030 | void *data = PyUnicode_DATA(self); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9031 | Py_UCS4 maxchar = 127, ch, fixed; |
Benjamin Peterson | 821e4cf | 2012-01-12 15:40:18 -0500 | [diff] [blame] | 9032 | int modified = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9033 | Py_ssize_t i; |
| 9034 | |
| 9035 | for (i = 0; i < len; ++i) { |
| 9036 | ch = PyUnicode_READ(kind, data, i); |
| 9037 | fixed = 0; |
| 9038 | if (ch > 127) { |
| 9039 | if (Py_UNICODE_ISSPACE(ch)) |
| 9040 | fixed = ' '; |
| 9041 | else { |
| 9042 | const int decimal = Py_UNICODE_TODECIMAL(ch); |
| 9043 | if (decimal >= 0) |
| 9044 | fixed = '0' + decimal; |
| 9045 | } |
| 9046 | if (fixed != 0) { |
Benjamin Peterson | 821e4cf | 2012-01-12 15:40:18 -0500 | [diff] [blame] | 9047 | modified = 1; |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9048 | maxchar = MAX_MAXCHAR(maxchar, fixed); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9049 | PyUnicode_WRITE(kind, data, i, fixed); |
| 9050 | } |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9051 | else |
| 9052 | maxchar = MAX_MAXCHAR(maxchar, ch); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9053 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9054 | } |
| 9055 | |
Benjamin Peterson | 821e4cf | 2012-01-12 15:40:18 -0500 | [diff] [blame] | 9056 | return (modified) ? maxchar : 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9057 | } |
| 9058 | |
| 9059 | PyObject * |
| 9060 | _PyUnicode_TransformDecimalAndSpaceToASCII(PyObject *unicode) |
| 9061 | { |
| 9062 | if (!PyUnicode_Check(unicode)) { |
| 9063 | PyErr_BadInternalCall(); |
| 9064 | return NULL; |
| 9065 | } |
| 9066 | if (PyUnicode_READY(unicode) == -1) |
| 9067 | return NULL; |
| 9068 | if (PyUnicode_MAX_CHAR_VALUE(unicode) <= 127) { |
| 9069 | /* If the string is already ASCII, just return the same string */ |
| 9070 | Py_INCREF(unicode); |
| 9071 | return unicode; |
| 9072 | } |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9073 | return fixup(unicode, fix_decimal_and_space_to_ascii); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9074 | } |
| 9075 | |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 9076 | PyObject * |
| 9077 | PyUnicode_TransformDecimalToASCII(Py_UNICODE *s, |
| 9078 | Py_ssize_t length) |
| 9079 | { |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 9080 | PyObject *decimal; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 9081 | Py_ssize_t i; |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 9082 | Py_UCS4 maxchar; |
| 9083 | enum PyUnicode_Kind kind; |
| 9084 | void *data; |
| 9085 | |
Victor Stinner | 99d7ad0 | 2012-02-22 13:37:39 +0100 | [diff] [blame] | 9086 | maxchar = 127; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 9087 | for (i = 0; i < length; i++) { |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 9088 | Py_UNICODE ch = s[i]; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 9089 | if (ch > 127) { |
| 9090 | int decimal = Py_UNICODE_TODECIMAL(ch); |
| 9091 | if (decimal >= 0) |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 9092 | ch = '0' + decimal; |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9093 | maxchar = MAX_MAXCHAR(maxchar, ch); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 9094 | } |
| 9095 | } |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 9096 | |
| 9097 | /* Copy to a new string */ |
| 9098 | decimal = PyUnicode_New(length, maxchar); |
| 9099 | if (decimal == NULL) |
| 9100 | return decimal; |
| 9101 | kind = PyUnicode_KIND(decimal); |
| 9102 | data = PyUnicode_DATA(decimal); |
| 9103 | /* Iterate over code points */ |
| 9104 | for (i = 0; i < length; i++) { |
| 9105 | Py_UNICODE ch = s[i]; |
| 9106 | if (ch > 127) { |
| 9107 | int decimal = Py_UNICODE_TODECIMAL(ch); |
| 9108 | if (decimal >= 0) |
| 9109 | ch = '0' + decimal; |
| 9110 | } |
| 9111 | PyUnicode_WRITE(kind, data, i, ch); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9112 | } |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 9113 | return unicode_result(decimal); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 9114 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 9115 | /* --- Decimal Encoder ---------------------------------------------------- */ |
| 9116 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9117 | int |
| 9118 | PyUnicode_EncodeDecimal(Py_UNICODE *s, |
| 9119 | Py_ssize_t length, |
| 9120 | char *output, |
| 9121 | const char *errors) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 9122 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 9123 | PyObject *unicode; |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 9124 | Py_ssize_t i; |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 9125 | enum PyUnicode_Kind kind; |
| 9126 | void *data; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 9127 | |
| 9128 | if (output == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9129 | PyErr_BadArgument(); |
| 9130 | return -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 9131 | } |
| 9132 | |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 9133 | unicode = PyUnicode_FromUnicode(s, length); |
| 9134 | if (unicode == NULL) |
| 9135 | return -1; |
| 9136 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 9137 | if (PyUnicode_READY(unicode) == -1) { |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 9138 | Py_DECREF(unicode); |
| 9139 | return -1; |
| 9140 | } |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 9141 | kind = PyUnicode_KIND(unicode); |
| 9142 | data = PyUnicode_DATA(unicode); |
| 9143 | |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 9144 | for (i=0; i < length; ) { |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 9145 | PyObject *exc; |
| 9146 | Py_UCS4 ch; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9147 | int decimal; |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 9148 | Py_ssize_t startpos; |
| 9149 | |
| 9150 | ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9151 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9152 | if (Py_UNICODE_ISSPACE(ch)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9153 | *output++ = ' '; |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 9154 | i++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9155 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9156 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9157 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 9158 | if (decimal >= 0) { |
| 9159 | *output++ = '0' + decimal; |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 9160 | i++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9161 | continue; |
| 9162 | } |
| 9163 | if (0 < ch && ch < 256) { |
| 9164 | *output++ = (char)ch; |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 9165 | i++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9166 | continue; |
| 9167 | } |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 9168 | |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 9169 | startpos = i; |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 9170 | exc = NULL; |
| 9171 | raise_encode_exception(&exc, "decimal", unicode, |
| 9172 | startpos, startpos+1, |
| 9173 | "invalid decimal Unicode string"); |
| 9174 | Py_XDECREF(exc); |
| 9175 | Py_DECREF(unicode); |
| 9176 | return -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 9177 | } |
| 9178 | /* 0-terminate the output string */ |
| 9179 | *output++ = '\0'; |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 9180 | Py_DECREF(unicode); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 9181 | return 0; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 9182 | } |
| 9183 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9184 | /* --- Helpers ------------------------------------------------------------ */ |
| 9185 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9186 | static Py_ssize_t |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 9187 | any_find_slice(int direction, PyObject* s1, PyObject* s2, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9188 | Py_ssize_t start, |
| 9189 | Py_ssize_t end) |
| 9190 | { |
| 9191 | int kind1, kind2, kind; |
| 9192 | void *buf1, *buf2; |
| 9193 | Py_ssize_t len1, len2, result; |
| 9194 | |
| 9195 | kind1 = PyUnicode_KIND(s1); |
| 9196 | kind2 = PyUnicode_KIND(s2); |
| 9197 | kind = kind1 > kind2 ? kind1 : kind2; |
| 9198 | buf1 = PyUnicode_DATA(s1); |
| 9199 | buf2 = PyUnicode_DATA(s2); |
| 9200 | if (kind1 != kind) |
| 9201 | buf1 = _PyUnicode_AsKind(s1, kind); |
| 9202 | if (!buf1) |
| 9203 | return -2; |
| 9204 | if (kind2 != kind) |
| 9205 | buf2 = _PyUnicode_AsKind(s2, kind); |
| 9206 | if (!buf2) { |
| 9207 | if (kind1 != kind) PyMem_Free(buf1); |
| 9208 | return -2; |
| 9209 | } |
| 9210 | len1 = PyUnicode_GET_LENGTH(s1); |
| 9211 | len2 = PyUnicode_GET_LENGTH(s2); |
| 9212 | |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 9213 | if (direction > 0) { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9214 | switch (kind) { |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 9215 | case PyUnicode_1BYTE_KIND: |
| 9216 | if (PyUnicode_IS_ASCII(s1) && PyUnicode_IS_ASCII(s2)) |
| 9217 | result = asciilib_find_slice(buf1, len1, buf2, len2, start, end); |
| 9218 | else |
| 9219 | result = ucs1lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 9220 | break; |
| 9221 | case PyUnicode_2BYTE_KIND: |
| 9222 | result = ucs2lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 9223 | break; |
| 9224 | case PyUnicode_4BYTE_KIND: |
| 9225 | result = ucs4lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 9226 | break; |
| 9227 | default: |
| 9228 | assert(0); result = -2; |
| 9229 | } |
| 9230 | } |
| 9231 | else { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9232 | switch (kind) { |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 9233 | case PyUnicode_1BYTE_KIND: |
| 9234 | if (PyUnicode_IS_ASCII(s1) && PyUnicode_IS_ASCII(s2)) |
| 9235 | result = asciilib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 9236 | else |
| 9237 | result = ucs1lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 9238 | break; |
| 9239 | case PyUnicode_2BYTE_KIND: |
| 9240 | result = ucs2lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 9241 | break; |
| 9242 | case PyUnicode_4BYTE_KIND: |
| 9243 | result = ucs4lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 9244 | break; |
| 9245 | default: |
| 9246 | assert(0); result = -2; |
| 9247 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9248 | } |
| 9249 | |
| 9250 | if (kind1 != kind) |
| 9251 | PyMem_Free(buf1); |
| 9252 | if (kind2 != kind) |
| 9253 | PyMem_Free(buf2); |
| 9254 | |
| 9255 | return result; |
| 9256 | } |
| 9257 | |
| 9258 | Py_ssize_t |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9259 | _PyUnicode_InsertThousandsGrouping( |
| 9260 | PyObject *unicode, Py_ssize_t index, |
| 9261 | Py_ssize_t n_buffer, |
| 9262 | void *digits, Py_ssize_t n_digits, |
| 9263 | Py_ssize_t min_width, |
| 9264 | const char *grouping, PyObject *thousands_sep, |
| 9265 | Py_UCS4 *maxchar) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9266 | { |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9267 | unsigned int kind, thousands_sep_kind; |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9268 | char *data, *thousands_sep_data; |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9269 | Py_ssize_t thousands_sep_len; |
| 9270 | Py_ssize_t len; |
| 9271 | |
| 9272 | if (unicode != NULL) { |
| 9273 | kind = PyUnicode_KIND(unicode); |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9274 | data = (char *) PyUnicode_DATA(unicode) + index * kind; |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9275 | } |
| 9276 | else { |
| 9277 | kind = PyUnicode_1BYTE_KIND; |
| 9278 | data = NULL; |
| 9279 | } |
| 9280 | thousands_sep_kind = PyUnicode_KIND(thousands_sep); |
| 9281 | thousands_sep_data = PyUnicode_DATA(thousands_sep); |
| 9282 | thousands_sep_len = PyUnicode_GET_LENGTH(thousands_sep); |
| 9283 | if (unicode != NULL && thousands_sep_kind != kind) { |
Victor Stinner | 90f50d4 | 2012-02-24 01:44:47 +0100 | [diff] [blame] | 9284 | if (thousands_sep_kind < kind) { |
| 9285 | thousands_sep_data = _PyUnicode_AsKind(thousands_sep, kind); |
| 9286 | if (!thousands_sep_data) |
| 9287 | return -1; |
| 9288 | } |
| 9289 | else { |
| 9290 | data = _PyUnicode_AsKind(unicode, thousands_sep_kind); |
| 9291 | if (!data) |
| 9292 | return -1; |
| 9293 | } |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9294 | } |
| 9295 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9296 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9297 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9298 | if (unicode != NULL && PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9299 | len = asciilib_InsertThousandsGrouping( |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9300 | (Py_UCS1 *) data, n_buffer, (Py_UCS1 *) digits, n_digits, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9301 | min_width, grouping, |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9302 | (Py_UCS1 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9303 | else |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9304 | len = ucs1lib_InsertThousandsGrouping( |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9305 | (Py_UCS1*)data, n_buffer, (Py_UCS1*)digits, n_digits, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9306 | min_width, grouping, |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9307 | (Py_UCS1 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9308 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9309 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9310 | len = ucs2lib_InsertThousandsGrouping( |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9311 | (Py_UCS2 *) data, n_buffer, (Py_UCS2 *) digits, n_digits, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9312 | min_width, grouping, |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9313 | (Py_UCS2 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9314 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9315 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9316 | len = ucs4lib_InsertThousandsGrouping( |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9317 | (Py_UCS4 *) data, n_buffer, (Py_UCS4 *) digits, n_digits, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9318 | min_width, grouping, |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9319 | (Py_UCS4 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9320 | break; |
| 9321 | default: |
| 9322 | assert(0); |
| 9323 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9324 | } |
Victor Stinner | 90f50d4 | 2012-02-24 01:44:47 +0100 | [diff] [blame] | 9325 | if (unicode != NULL && thousands_sep_kind != kind) { |
| 9326 | if (thousands_sep_kind < kind) |
| 9327 | PyMem_Free(thousands_sep_data); |
| 9328 | else |
| 9329 | PyMem_Free(data); |
| 9330 | } |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9331 | if (unicode == NULL) { |
| 9332 | *maxchar = 127; |
| 9333 | if (len != n_digits) { |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9334 | *maxchar = MAX_MAXCHAR(*maxchar, |
| 9335 | PyUnicode_MAX_CHAR_VALUE(thousands_sep)); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9336 | } |
| 9337 | } |
| 9338 | return len; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9339 | } |
| 9340 | |
| 9341 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9342 | /* helper macro to fixup start/end slice values */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9343 | #define ADJUST_INDICES(start, end, len) \ |
| 9344 | if (end > len) \ |
| 9345 | end = len; \ |
| 9346 | else if (end < 0) { \ |
| 9347 | end += len; \ |
| 9348 | if (end < 0) \ |
| 9349 | end = 0; \ |
| 9350 | } \ |
| 9351 | if (start < 0) { \ |
| 9352 | start += len; \ |
| 9353 | if (start < 0) \ |
| 9354 | start = 0; \ |
| 9355 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9356 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9357 | Py_ssize_t |
| 9358 | PyUnicode_Count(PyObject *str, |
| 9359 | PyObject *substr, |
| 9360 | Py_ssize_t start, |
| 9361 | Py_ssize_t end) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9362 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9363 | Py_ssize_t result; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9364 | PyObject* str_obj; |
| 9365 | PyObject* sub_obj; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9366 | int kind1, kind2, kind; |
| 9367 | void *buf1 = NULL, *buf2 = NULL; |
| 9368 | Py_ssize_t len1, len2; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9369 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9370 | str_obj = PyUnicode_FromObject(str); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9371 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9372 | return -1; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9373 | sub_obj = PyUnicode_FromObject(substr); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9374 | if (!sub_obj) { |
| 9375 | Py_DECREF(str_obj); |
| 9376 | return -1; |
| 9377 | } |
Benjamin Peterson | 4c13a4a | 2012-01-02 09:07:38 -0600 | [diff] [blame] | 9378 | if (PyUnicode_READY(sub_obj) == -1 || PyUnicode_READY(str_obj) == -1) { |
Benjamin Peterson | 5e458f5 | 2012-01-02 10:12:13 -0600 | [diff] [blame] | 9379 | Py_DECREF(sub_obj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9380 | Py_DECREF(str_obj); |
| 9381 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9382 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9383 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9384 | kind1 = PyUnicode_KIND(str_obj); |
| 9385 | kind2 = PyUnicode_KIND(sub_obj); |
| 9386 | kind = kind1 > kind2 ? kind1 : kind2; |
| 9387 | buf1 = PyUnicode_DATA(str_obj); |
| 9388 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9389 | buf1 = _PyUnicode_AsKind(str_obj, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9390 | if (!buf1) |
| 9391 | goto onError; |
| 9392 | buf2 = PyUnicode_DATA(sub_obj); |
| 9393 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9394 | buf2 = _PyUnicode_AsKind(sub_obj, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9395 | if (!buf2) |
| 9396 | goto onError; |
| 9397 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 9398 | len2 = PyUnicode_GET_LENGTH(sub_obj); |
| 9399 | |
| 9400 | ADJUST_INDICES(start, end, len1); |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9401 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9402 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9403 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sub_obj)) |
| 9404 | result = asciilib_count( |
| 9405 | ((Py_UCS1*)buf1) + start, end - start, |
| 9406 | buf2, len2, PY_SSIZE_T_MAX |
| 9407 | ); |
| 9408 | else |
| 9409 | result = ucs1lib_count( |
| 9410 | ((Py_UCS1*)buf1) + start, end - start, |
| 9411 | buf2, len2, PY_SSIZE_T_MAX |
| 9412 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9413 | break; |
| 9414 | case PyUnicode_2BYTE_KIND: |
| 9415 | result = ucs2lib_count( |
| 9416 | ((Py_UCS2*)buf1) + start, end - start, |
| 9417 | buf2, len2, PY_SSIZE_T_MAX |
| 9418 | ); |
| 9419 | break; |
| 9420 | case PyUnicode_4BYTE_KIND: |
| 9421 | result = ucs4lib_count( |
| 9422 | ((Py_UCS4*)buf1) + start, end - start, |
| 9423 | buf2, len2, PY_SSIZE_T_MAX |
| 9424 | ); |
| 9425 | break; |
| 9426 | default: |
| 9427 | assert(0); result = 0; |
| 9428 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9429 | |
| 9430 | Py_DECREF(sub_obj); |
| 9431 | Py_DECREF(str_obj); |
| 9432 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9433 | if (kind1 != kind) |
| 9434 | PyMem_Free(buf1); |
| 9435 | if (kind2 != kind) |
| 9436 | PyMem_Free(buf2); |
| 9437 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9438 | return result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9439 | onError: |
| 9440 | Py_DECREF(sub_obj); |
| 9441 | Py_DECREF(str_obj); |
| 9442 | if (kind1 != kind && buf1) |
| 9443 | PyMem_Free(buf1); |
| 9444 | if (kind2 != kind && buf2) |
| 9445 | PyMem_Free(buf2); |
| 9446 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9447 | } |
| 9448 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9449 | Py_ssize_t |
| 9450 | PyUnicode_Find(PyObject *str, |
| 9451 | PyObject *sub, |
| 9452 | Py_ssize_t start, |
| 9453 | Py_ssize_t end, |
| 9454 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9455 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9456 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9457 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9458 | str = PyUnicode_FromObject(str); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9459 | if (!str) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9460 | return -2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9461 | sub = PyUnicode_FromObject(sub); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9462 | if (!sub) { |
| 9463 | Py_DECREF(str); |
| 9464 | return -2; |
| 9465 | } |
| 9466 | if (PyUnicode_READY(sub) == -1 || PyUnicode_READY(str) == -1) { |
| 9467 | Py_DECREF(sub); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9468 | Py_DECREF(str); |
| 9469 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9470 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9471 | |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 9472 | result = any_find_slice(direction, |
| 9473 | str, sub, start, end |
| 9474 | ); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9475 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9476 | Py_DECREF(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9477 | Py_DECREF(sub); |
| 9478 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9479 | return result; |
| 9480 | } |
| 9481 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9482 | Py_ssize_t |
| 9483 | PyUnicode_FindChar(PyObject *str, Py_UCS4 ch, |
| 9484 | Py_ssize_t start, Py_ssize_t end, |
| 9485 | int direction) |
| 9486 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9487 | int kind; |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 9488 | Py_ssize_t result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9489 | if (PyUnicode_READY(str) == -1) |
| 9490 | return -2; |
Victor Stinner | 267aa24 | 2011-10-02 01:08:37 +0200 | [diff] [blame] | 9491 | if (start < 0 || end < 0) { |
| 9492 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 9493 | return -2; |
| 9494 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9495 | if (end > PyUnicode_GET_LENGTH(str)) |
| 9496 | end = PyUnicode_GET_LENGTH(str); |
| 9497 | kind = PyUnicode_KIND(str); |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 9498 | result = findchar(PyUnicode_1BYTE_DATA(str) + kind*start, |
| 9499 | kind, end-start, ch, direction); |
| 9500 | if (result == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9501 | return -1; |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 9502 | else |
| 9503 | return start + result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9504 | } |
| 9505 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9506 | static int |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9507 | tailmatch(PyObject *self, |
| 9508 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9509 | Py_ssize_t start, |
| 9510 | Py_ssize_t end, |
| 9511 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9512 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9513 | int kind_self; |
| 9514 | int kind_sub; |
| 9515 | void *data_self; |
| 9516 | void *data_sub; |
| 9517 | Py_ssize_t offset; |
| 9518 | Py_ssize_t i; |
| 9519 | Py_ssize_t end_sub; |
| 9520 | |
| 9521 | if (PyUnicode_READY(self) == -1 || |
| 9522 | PyUnicode_READY(substring) == -1) |
| 9523 | return 0; |
| 9524 | |
| 9525 | if (PyUnicode_GET_LENGTH(substring) == 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9526 | return 1; |
| 9527 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9528 | ADJUST_INDICES(start, end, PyUnicode_GET_LENGTH(self)); |
| 9529 | end -= PyUnicode_GET_LENGTH(substring); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9530 | if (end < start) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9531 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9532 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9533 | kind_self = PyUnicode_KIND(self); |
| 9534 | data_self = PyUnicode_DATA(self); |
| 9535 | kind_sub = PyUnicode_KIND(substring); |
| 9536 | data_sub = PyUnicode_DATA(substring); |
| 9537 | end_sub = PyUnicode_GET_LENGTH(substring) - 1; |
| 9538 | |
| 9539 | if (direction > 0) |
| 9540 | offset = end; |
| 9541 | else |
| 9542 | offset = start; |
| 9543 | |
| 9544 | if (PyUnicode_READ(kind_self, data_self, offset) == |
| 9545 | PyUnicode_READ(kind_sub, data_sub, 0) && |
| 9546 | PyUnicode_READ(kind_self, data_self, offset + end_sub) == |
| 9547 | PyUnicode_READ(kind_sub, data_sub, end_sub)) { |
| 9548 | /* If both are of the same kind, memcmp is sufficient */ |
| 9549 | if (kind_self == kind_sub) { |
| 9550 | return ! memcmp((char *)data_self + |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9551 | (offset * PyUnicode_KIND(substring)), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9552 | data_sub, |
| 9553 | PyUnicode_GET_LENGTH(substring) * |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9554 | PyUnicode_KIND(substring)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9555 | } |
| 9556 | /* otherwise we have to compare each character by first accesing it */ |
| 9557 | else { |
| 9558 | /* We do not need to compare 0 and len(substring)-1 because |
| 9559 | the if statement above ensured already that they are equal |
| 9560 | when we end up here. */ |
| 9561 | // TODO: honor direction and do a forward or backwards search |
| 9562 | for (i = 1; i < end_sub; ++i) { |
| 9563 | if (PyUnicode_READ(kind_self, data_self, offset + i) != |
| 9564 | PyUnicode_READ(kind_sub, data_sub, i)) |
| 9565 | return 0; |
| 9566 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9567 | return 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9568 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9569 | } |
| 9570 | |
| 9571 | return 0; |
| 9572 | } |
| 9573 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9574 | Py_ssize_t |
| 9575 | PyUnicode_Tailmatch(PyObject *str, |
| 9576 | PyObject *substr, |
| 9577 | Py_ssize_t start, |
| 9578 | Py_ssize_t end, |
| 9579 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9580 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9581 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9582 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9583 | str = PyUnicode_FromObject(str); |
| 9584 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9585 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9586 | substr = PyUnicode_FromObject(substr); |
| 9587 | if (substr == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9588 | Py_DECREF(str); |
| 9589 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9590 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9591 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9592 | result = tailmatch(str, substr, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9593 | start, end, direction); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9594 | Py_DECREF(str); |
| 9595 | Py_DECREF(substr); |
| 9596 | return result; |
| 9597 | } |
| 9598 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9599 | /* Apply fixfct filter to the Unicode object self and return a |
| 9600 | reference to the modified object */ |
| 9601 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9602 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9603 | fixup(PyObject *self, |
| 9604 | Py_UCS4 (*fixfct)(PyObject *s)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9605 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9606 | PyObject *u; |
| 9607 | Py_UCS4 maxchar_old, maxchar_new = 0; |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9608 | PyObject *v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9609 | |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 9610 | u = _PyUnicode_Copy(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9611 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9612 | return NULL; |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 9613 | maxchar_old = PyUnicode_MAX_CHAR_VALUE(u); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9614 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9615 | /* fix functions return the new maximum character in a string, |
| 9616 | if the kind of the resulting unicode object does not change, |
| 9617 | everything is fine. Otherwise we need to change the string kind |
| 9618 | and re-run the fix function. */ |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9619 | maxchar_new = fixfct(u); |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9620 | |
| 9621 | if (maxchar_new == 0) { |
| 9622 | /* no changes */; |
| 9623 | if (PyUnicode_CheckExact(self)) { |
| 9624 | Py_DECREF(u); |
| 9625 | Py_INCREF(self); |
| 9626 | return self; |
| 9627 | } |
| 9628 | else |
| 9629 | return u; |
| 9630 | } |
| 9631 | |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9632 | maxchar_new = align_maxchar(maxchar_new); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9633 | |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9634 | if (maxchar_new == maxchar_old) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9635 | return u; |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9636 | |
| 9637 | /* In case the maximum character changed, we need to |
| 9638 | convert the string to the new category. */ |
| 9639 | v = PyUnicode_New(PyUnicode_GET_LENGTH(self), maxchar_new); |
| 9640 | if (v == NULL) { |
| 9641 | Py_DECREF(u); |
| 9642 | return NULL; |
| 9643 | } |
| 9644 | if (maxchar_new > maxchar_old) { |
| 9645 | /* If the maxchar increased so that the kind changed, not all |
| 9646 | characters are representable anymore and we need to fix the |
| 9647 | string again. This only happens in very few cases. */ |
| 9648 | copy_characters(v, 0, self, 0, PyUnicode_GET_LENGTH(self)); |
| 9649 | maxchar_old = fixfct(v); |
| 9650 | assert(maxchar_old > 0 && maxchar_old <= maxchar_new); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9651 | } |
| 9652 | else { |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9653 | copy_characters(v, 0, u, 0, PyUnicode_GET_LENGTH(self)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9654 | } |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9655 | Py_DECREF(u); |
| 9656 | assert(_PyUnicode_CheckConsistency(v, 1)); |
| 9657 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9658 | } |
| 9659 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9660 | static PyObject * |
| 9661 | ascii_upper_or_lower(PyObject *self, int lower) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9662 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9663 | Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 9664 | char *resdata, *data = PyUnicode_DATA(self); |
| 9665 | PyObject *res; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9666 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9667 | res = PyUnicode_New(len, 127); |
| 9668 | if (res == NULL) |
| 9669 | return NULL; |
| 9670 | resdata = PyUnicode_DATA(res); |
| 9671 | if (lower) |
| 9672 | _Py_bytes_lower(resdata, data, len); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9673 | else |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9674 | _Py_bytes_upper(resdata, data, len); |
| 9675 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9676 | } |
| 9677 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9678 | static Py_UCS4 |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9679 | 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] | 9680 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9681 | Py_ssize_t j; |
| 9682 | int final_sigma; |
| 9683 | Py_UCS4 c; |
| 9684 | /* 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] | 9685 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9686 | \p{cased}\p{case-ignorable}*U+03A3!(\p{case-ignorable}*\p{cased}) |
| 9687 | |
| 9688 | where ! is a negation and \p{xxx} is a character with property xxx. |
| 9689 | */ |
| 9690 | for (j = i - 1; j >= 0; j--) { |
| 9691 | c = PyUnicode_READ(kind, data, j); |
| 9692 | if (!_PyUnicode_IsCaseIgnorable(c)) |
| 9693 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9694 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9695 | final_sigma = j >= 0 && _PyUnicode_IsCased(c); |
| 9696 | if (final_sigma) { |
| 9697 | for (j = i + 1; j < length; j++) { |
| 9698 | c = PyUnicode_READ(kind, data, j); |
| 9699 | if (!_PyUnicode_IsCaseIgnorable(c)) |
| 9700 | break; |
| 9701 | } |
| 9702 | final_sigma = j == length || !_PyUnicode_IsCased(c); |
| 9703 | } |
| 9704 | return (final_sigma) ? 0x3C2 : 0x3C3; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9705 | } |
| 9706 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9707 | static int |
| 9708 | lower_ucs4(int kind, void *data, Py_ssize_t length, Py_ssize_t i, |
| 9709 | Py_UCS4 c, Py_UCS4 *mapped) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9710 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9711 | /* Obscure special case. */ |
| 9712 | if (c == 0x3A3) { |
| 9713 | mapped[0] = handle_capital_sigma(kind, data, length, i); |
| 9714 | return 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9715 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9716 | return _PyUnicode_ToLowerFull(c, mapped); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9717 | } |
| 9718 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9719 | static Py_ssize_t |
| 9720 | 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] | 9721 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9722 | Py_ssize_t i, k = 0; |
| 9723 | int n_res, j; |
| 9724 | Py_UCS4 c, mapped[3]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9725 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9726 | c = PyUnicode_READ(kind, data, 0); |
| 9727 | n_res = _PyUnicode_ToUpperFull(c, mapped); |
| 9728 | for (j = 0; j < n_res; j++) { |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9729 | *maxchar = MAX_MAXCHAR(*maxchar, mapped[j]); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9730 | res[k++] = mapped[j]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9731 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9732 | for (i = 1; i < length; i++) { |
| 9733 | c = PyUnicode_READ(kind, data, i); |
| 9734 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9735 | for (j = 0; j < n_res; j++) { |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9736 | *maxchar = MAX_MAXCHAR(*maxchar, mapped[j]); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9737 | res[k++] = mapped[j]; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9738 | } |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9739 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9740 | return k; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9741 | } |
| 9742 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9743 | static Py_ssize_t |
| 9744 | do_swapcase(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) { |
| 9745 | Py_ssize_t i, k = 0; |
| 9746 | |
| 9747 | for (i = 0; i < length; i++) { |
| 9748 | Py_UCS4 c = PyUnicode_READ(kind, data, i), mapped[3]; |
| 9749 | int n_res, j; |
| 9750 | if (Py_UNICODE_ISUPPER(c)) { |
| 9751 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9752 | } |
| 9753 | else if (Py_UNICODE_ISLOWER(c)) { |
| 9754 | n_res = _PyUnicode_ToUpperFull(c, mapped); |
| 9755 | } |
| 9756 | else { |
| 9757 | n_res = 1; |
| 9758 | mapped[0] = c; |
| 9759 | } |
| 9760 | for (j = 0; j < n_res; j++) { |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9761 | *maxchar = MAX_MAXCHAR(*maxchar, mapped[j]); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9762 | res[k++] = mapped[j]; |
| 9763 | } |
| 9764 | } |
| 9765 | return k; |
| 9766 | } |
| 9767 | |
| 9768 | static Py_ssize_t |
| 9769 | do_upper_or_lower(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, |
| 9770 | Py_UCS4 *maxchar, int lower) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9771 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9772 | Py_ssize_t i, k = 0; |
| 9773 | |
| 9774 | for (i = 0; i < length; i++) { |
| 9775 | Py_UCS4 c = PyUnicode_READ(kind, data, i), mapped[3]; |
| 9776 | int n_res, j; |
| 9777 | if (lower) |
| 9778 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9779 | else |
| 9780 | n_res = _PyUnicode_ToUpperFull(c, mapped); |
| 9781 | for (j = 0; j < n_res; j++) { |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9782 | *maxchar = MAX_MAXCHAR(*maxchar, mapped[j]); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9783 | res[k++] = mapped[j]; |
| 9784 | } |
| 9785 | } |
| 9786 | return k; |
| 9787 | } |
| 9788 | |
| 9789 | static Py_ssize_t |
| 9790 | do_upper(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9791 | { |
| 9792 | return do_upper_or_lower(kind, data, length, res, maxchar, 0); |
| 9793 | } |
| 9794 | |
| 9795 | static Py_ssize_t |
| 9796 | do_lower(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9797 | { |
| 9798 | return do_upper_or_lower(kind, data, length, res, maxchar, 1); |
| 9799 | } |
| 9800 | |
Benjamin Peterson | e51757f | 2012-01-12 21:10:29 -0500 | [diff] [blame] | 9801 | static Py_ssize_t |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 9802 | do_casefold(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9803 | { |
| 9804 | Py_ssize_t i, k = 0; |
| 9805 | |
| 9806 | for (i = 0; i < length; i++) { |
| 9807 | Py_UCS4 c = PyUnicode_READ(kind, data, i); |
| 9808 | Py_UCS4 mapped[3]; |
| 9809 | int j, n_res = _PyUnicode_ToFoldedFull(c, mapped); |
| 9810 | for (j = 0; j < n_res; j++) { |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9811 | *maxchar = MAX_MAXCHAR(*maxchar, mapped[j]); |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 9812 | res[k++] = mapped[j]; |
| 9813 | } |
| 9814 | } |
| 9815 | return k; |
| 9816 | } |
| 9817 | |
| 9818 | static Py_ssize_t |
Benjamin Peterson | e51757f | 2012-01-12 21:10:29 -0500 | [diff] [blame] | 9819 | do_title(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9820 | { |
| 9821 | Py_ssize_t i, k = 0; |
| 9822 | int previous_is_cased; |
| 9823 | |
| 9824 | previous_is_cased = 0; |
| 9825 | for (i = 0; i < length; i++) { |
| 9826 | const Py_UCS4 c = PyUnicode_READ(kind, data, i); |
| 9827 | Py_UCS4 mapped[3]; |
| 9828 | int n_res, j; |
| 9829 | |
| 9830 | if (previous_is_cased) |
| 9831 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9832 | else |
| 9833 | n_res = _PyUnicode_ToTitleFull(c, mapped); |
| 9834 | |
| 9835 | for (j = 0; j < n_res; j++) { |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9836 | *maxchar = MAX_MAXCHAR(*maxchar, mapped[j]); |
Benjamin Peterson | e51757f | 2012-01-12 21:10:29 -0500 | [diff] [blame] | 9837 | res[k++] = mapped[j]; |
| 9838 | } |
| 9839 | |
| 9840 | previous_is_cased = _PyUnicode_IsCased(c); |
| 9841 | } |
| 9842 | return k; |
| 9843 | } |
| 9844 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9845 | static PyObject * |
| 9846 | case_operation(PyObject *self, |
| 9847 | Py_ssize_t (*perform)(int, void *, Py_ssize_t, Py_UCS4 *, Py_UCS4 *)) |
| 9848 | { |
| 9849 | PyObject *res = NULL; |
| 9850 | Py_ssize_t length, newlength = 0; |
| 9851 | int kind, outkind; |
| 9852 | void *data, *outdata; |
| 9853 | Py_UCS4 maxchar = 0, *tmp, *tmpend; |
| 9854 | |
Benjamin Peterson | eea4846 | 2012-01-16 14:28:50 -0500 | [diff] [blame] | 9855 | assert(PyUnicode_IS_READY(self)); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9856 | |
| 9857 | kind = PyUnicode_KIND(self); |
| 9858 | data = PyUnicode_DATA(self); |
| 9859 | length = PyUnicode_GET_LENGTH(self); |
| 9860 | tmp = PyMem_MALLOC(sizeof(Py_UCS4) * 3 * length); |
| 9861 | if (tmp == NULL) |
| 9862 | return PyErr_NoMemory(); |
| 9863 | newlength = perform(kind, data, length, tmp, &maxchar); |
| 9864 | res = PyUnicode_New(newlength, maxchar); |
| 9865 | if (res == NULL) |
| 9866 | goto leave; |
| 9867 | tmpend = tmp + newlength; |
| 9868 | outdata = PyUnicode_DATA(res); |
| 9869 | outkind = PyUnicode_KIND(res); |
| 9870 | switch (outkind) { |
| 9871 | case PyUnicode_1BYTE_KIND: |
| 9872 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS1, tmp, tmpend, outdata); |
| 9873 | break; |
| 9874 | case PyUnicode_2BYTE_KIND: |
| 9875 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS2, tmp, tmpend, outdata); |
| 9876 | break; |
| 9877 | case PyUnicode_4BYTE_KIND: |
| 9878 | memcpy(outdata, tmp, sizeof(Py_UCS4) * newlength); |
| 9879 | break; |
| 9880 | default: |
| 9881 | assert(0); |
| 9882 | break; |
| 9883 | } |
| 9884 | leave: |
| 9885 | PyMem_FREE(tmp); |
| 9886 | return res; |
| 9887 | } |
| 9888 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9889 | PyObject * |
| 9890 | PyUnicode_Join(PyObject *separator, PyObject *seq) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9891 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9892 | PyObject *sep = NULL; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9893 | Py_ssize_t seplen; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9894 | PyObject *res = NULL; /* the result */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9895 | PyObject *fseq; /* PySequence_Fast(seq) */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9896 | Py_ssize_t seqlen; /* len(fseq) -- number of items in sequence */ |
| 9897 | PyObject **items; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9898 | PyObject *item; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9899 | Py_ssize_t sz, i, res_offset; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9900 | Py_UCS4 maxchar; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9901 | Py_UCS4 item_maxchar; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9902 | int use_memcpy; |
| 9903 | unsigned char *res_data = NULL, *sep_data = NULL; |
| 9904 | PyObject *last_obj; |
| 9905 | unsigned int kind = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9906 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9907 | fseq = PySequence_Fast(seq, ""); |
| 9908 | if (fseq == NULL) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9909 | return NULL; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9910 | } |
| 9911 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9912 | /* NOTE: the following code can't call back into Python code, |
| 9913 | * so we are sure that fseq won't be mutated. |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 9914 | */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9915 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9916 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 9917 | /* If empty sequence, return u"". */ |
| 9918 | if (seqlen == 0) { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9919 | Py_DECREF(fseq); |
| 9920 | Py_INCREF(unicode_empty); |
| 9921 | res = unicode_empty; |
| 9922 | return res; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9923 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9924 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9925 | /* If singleton sequence with an exact Unicode, return that. */ |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9926 | last_obj = NULL; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9927 | items = PySequence_Fast_ITEMS(fseq); |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9928 | if (seqlen == 1) { |
| 9929 | if (PyUnicode_CheckExact(items[0])) { |
| 9930 | res = items[0]; |
| 9931 | Py_INCREF(res); |
| 9932 | Py_DECREF(fseq); |
| 9933 | return res; |
| 9934 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9935 | seplen = 0; |
Victor Stinner | c6f0df7 | 2011-10-06 15:58:54 +0200 | [diff] [blame] | 9936 | maxchar = 0; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9937 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9938 | else { |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9939 | /* Set up sep and seplen */ |
| 9940 | if (separator == NULL) { |
| 9941 | /* fall back to a blank space separator */ |
| 9942 | sep = PyUnicode_FromOrdinal(' '); |
| 9943 | if (!sep) |
| 9944 | goto onError; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9945 | seplen = 1; |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9946 | maxchar = 32; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9947 | } |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9948 | else { |
| 9949 | if (!PyUnicode_Check(separator)) { |
| 9950 | PyErr_Format(PyExc_TypeError, |
| 9951 | "separator: expected str instance," |
| 9952 | " %.80s found", |
| 9953 | Py_TYPE(separator)->tp_name); |
| 9954 | goto onError; |
| 9955 | } |
| 9956 | if (PyUnicode_READY(separator)) |
| 9957 | goto onError; |
| 9958 | sep = separator; |
| 9959 | seplen = PyUnicode_GET_LENGTH(separator); |
| 9960 | maxchar = PyUnicode_MAX_CHAR_VALUE(separator); |
| 9961 | /* inc refcount to keep this code path symmetric with the |
| 9962 | above case of a blank separator */ |
| 9963 | Py_INCREF(sep); |
| 9964 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9965 | last_obj = sep; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9966 | } |
| 9967 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9968 | /* There are at least two things to join, or else we have a subclass |
| 9969 | * of str in the sequence. |
| 9970 | * Do a pre-pass to figure out the total amount of space we'll |
| 9971 | * need (sz), and see whether all argument are strings. |
| 9972 | */ |
| 9973 | sz = 0; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9974 | #ifdef Py_DEBUG |
| 9975 | use_memcpy = 0; |
| 9976 | #else |
| 9977 | use_memcpy = 1; |
| 9978 | #endif |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9979 | for (i = 0; i < seqlen; i++) { |
| 9980 | const Py_ssize_t old_sz = sz; |
| 9981 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9982 | if (!PyUnicode_Check(item)) { |
| 9983 | PyErr_Format(PyExc_TypeError, |
| 9984 | "sequence item %zd: expected str instance," |
| 9985 | " %.80s found", |
| 9986 | i, Py_TYPE(item)->tp_name); |
| 9987 | goto onError; |
| 9988 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9989 | if (PyUnicode_READY(item) == -1) |
| 9990 | goto onError; |
| 9991 | sz += PyUnicode_GET_LENGTH(item); |
| 9992 | item_maxchar = PyUnicode_MAX_CHAR_VALUE(item); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9993 | maxchar = MAX_MAXCHAR(maxchar, item_maxchar); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9994 | if (i != 0) |
| 9995 | sz += seplen; |
| 9996 | if (sz < old_sz || sz > PY_SSIZE_T_MAX) { |
| 9997 | PyErr_SetString(PyExc_OverflowError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9998 | "join() result is too long for a Python string"); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9999 | goto onError; |
| 10000 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 10001 | if (use_memcpy && last_obj != NULL) { |
| 10002 | if (PyUnicode_KIND(last_obj) != PyUnicode_KIND(item)) |
| 10003 | use_memcpy = 0; |
| 10004 | } |
| 10005 | last_obj = item; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 10006 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 10007 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10008 | res = PyUnicode_New(sz, maxchar); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 10009 | if (res == NULL) |
| 10010 | goto onError; |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 10011 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 10012 | /* Catenate everything. */ |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 10013 | #ifdef Py_DEBUG |
| 10014 | use_memcpy = 0; |
| 10015 | #else |
| 10016 | if (use_memcpy) { |
| 10017 | res_data = PyUnicode_1BYTE_DATA(res); |
| 10018 | kind = PyUnicode_KIND(res); |
| 10019 | if (seplen != 0) |
| 10020 | sep_data = PyUnicode_1BYTE_DATA(sep); |
| 10021 | } |
| 10022 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10023 | for (i = 0, res_offset = 0; i < seqlen; ++i) { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 10024 | Py_ssize_t itemlen; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 10025 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10026 | /* Copy item, and maybe the separator. */ |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 10027 | if (i && seplen != 0) { |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 10028 | if (use_memcpy) { |
| 10029 | Py_MEMCPY(res_data, |
| 10030 | sep_data, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10031 | kind * seplen); |
| 10032 | res_data += kind * seplen; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 10033 | } |
| 10034 | else { |
| 10035 | copy_characters(res, res_offset, sep, 0, seplen); |
| 10036 | res_offset += seplen; |
| 10037 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10038 | } |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 10039 | itemlen = PyUnicode_GET_LENGTH(item); |
| 10040 | if (itemlen != 0) { |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 10041 | if (use_memcpy) { |
| 10042 | Py_MEMCPY(res_data, |
| 10043 | PyUnicode_DATA(item), |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10044 | kind * itemlen); |
| 10045 | res_data += kind * itemlen; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 10046 | } |
| 10047 | else { |
| 10048 | copy_characters(res, res_offset, item, 0, itemlen); |
| 10049 | res_offset += itemlen; |
| 10050 | } |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 10051 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 10052 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 10053 | if (use_memcpy) |
| 10054 | assert(res_data == PyUnicode_1BYTE_DATA(res) |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10055 | + kind * PyUnicode_GET_LENGTH(res)); |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 10056 | else |
| 10057 | assert(res_offset == PyUnicode_GET_LENGTH(res)); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 10058 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 10059 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10060 | Py_XDECREF(sep); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10061 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10062 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10063 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10064 | onError: |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 10065 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10066 | Py_XDECREF(sep); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 10067 | Py_XDECREF(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10068 | return NULL; |
| 10069 | } |
| 10070 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10071 | #define FILL(kind, data, value, start, length) \ |
| 10072 | do { \ |
| 10073 | Py_ssize_t i_ = 0; \ |
| 10074 | assert(kind != PyUnicode_WCHAR_KIND); \ |
| 10075 | switch ((kind)) { \ |
| 10076 | case PyUnicode_1BYTE_KIND: { \ |
| 10077 | unsigned char * to_ = (unsigned char *)((data)) + (start); \ |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 10078 | memset(to_, (unsigned char)value, (length)); \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10079 | break; \ |
| 10080 | } \ |
| 10081 | case PyUnicode_2BYTE_KIND: { \ |
| 10082 | Py_UCS2 * to_ = (Py_UCS2 *)((data)) + (start); \ |
| 10083 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 10084 | break; \ |
| 10085 | } \ |
Benjamin Peterson | e157cf1 | 2012-01-01 15:56:20 -0600 | [diff] [blame] | 10086 | case PyUnicode_4BYTE_KIND: { \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10087 | Py_UCS4 * to_ = (Py_UCS4 *)((data)) + (start); \ |
| 10088 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 10089 | break; \ |
Benjamin Peterson | e157cf1 | 2012-01-01 15:56:20 -0600 | [diff] [blame] | 10090 | default: assert(0); \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10091 | } \ |
| 10092 | } \ |
| 10093 | } while (0) |
| 10094 | |
Victor Stinner | 3fe5531 | 2012-01-04 00:33:50 +0100 | [diff] [blame] | 10095 | Py_ssize_t |
| 10096 | PyUnicode_Fill(PyObject *unicode, Py_ssize_t start, Py_ssize_t length, |
| 10097 | Py_UCS4 fill_char) |
| 10098 | { |
| 10099 | Py_ssize_t maxlen; |
| 10100 | enum PyUnicode_Kind kind; |
| 10101 | void *data; |
| 10102 | |
| 10103 | if (!PyUnicode_Check(unicode)) { |
| 10104 | PyErr_BadInternalCall(); |
| 10105 | return -1; |
| 10106 | } |
| 10107 | if (PyUnicode_READY(unicode) == -1) |
| 10108 | return -1; |
| 10109 | if (unicode_check_modifiable(unicode)) |
| 10110 | return -1; |
| 10111 | |
| 10112 | if (fill_char > PyUnicode_MAX_CHAR_VALUE(unicode)) { |
| 10113 | PyErr_SetString(PyExc_ValueError, |
| 10114 | "fill character is bigger than " |
| 10115 | "the string maximum character"); |
| 10116 | return -1; |
| 10117 | } |
| 10118 | |
| 10119 | maxlen = PyUnicode_GET_LENGTH(unicode) - start; |
| 10120 | length = Py_MIN(maxlen, length); |
| 10121 | if (length <= 0) |
| 10122 | return 0; |
| 10123 | |
| 10124 | kind = PyUnicode_KIND(unicode); |
| 10125 | data = PyUnicode_DATA(unicode); |
| 10126 | FILL(kind, data, fill_char, start, length); |
| 10127 | return length; |
| 10128 | } |
| 10129 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10130 | static PyObject * |
| 10131 | pad(PyObject *self, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10132 | Py_ssize_t left, |
| 10133 | Py_ssize_t right, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10134 | Py_UCS4 fill) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10135 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10136 | PyObject *u; |
| 10137 | Py_UCS4 maxchar; |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 10138 | int kind; |
| 10139 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10140 | |
| 10141 | if (left < 0) |
| 10142 | left = 0; |
| 10143 | if (right < 0) |
| 10144 | right = 0; |
| 10145 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10146 | if (left == 0 && right == 0) |
| 10147 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10148 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10149 | if (left > PY_SSIZE_T_MAX - _PyUnicode_LENGTH(self) || |
| 10150 | right > PY_SSIZE_T_MAX - (left + _PyUnicode_LENGTH(self))) { |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 10151 | PyErr_SetString(PyExc_OverflowError, "padded string is too long"); |
| 10152 | return NULL; |
| 10153 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10154 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 10155 | maxchar = MAX_MAXCHAR(maxchar, fill); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10156 | u = PyUnicode_New(left + _PyUnicode_LENGTH(self) + right, maxchar); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 10157 | if (!u) |
| 10158 | return NULL; |
| 10159 | |
| 10160 | kind = PyUnicode_KIND(u); |
| 10161 | data = PyUnicode_DATA(u); |
| 10162 | if (left) |
| 10163 | FILL(kind, data, fill, 0, left); |
| 10164 | if (right) |
| 10165 | FILL(kind, data, fill, left + _PyUnicode_LENGTH(self), right); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 10166 | copy_characters(u, left, self, 0, _PyUnicode_LENGTH(self)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10167 | assert(_PyUnicode_CheckConsistency(u, 1)); |
| 10168 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10169 | } |
| 10170 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10171 | PyObject * |
| 10172 | PyUnicode_Splitlines(PyObject *string, int keepends) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10173 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10174 | PyObject *list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10175 | |
| 10176 | string = PyUnicode_FromObject(string); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 10177 | if (string == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10178 | return NULL; |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 10179 | if (PyUnicode_READY(string) == -1) { |
| 10180 | Py_DECREF(string); |
| 10181 | return NULL; |
| 10182 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10183 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10184 | switch (PyUnicode_KIND(string)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10185 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10186 | if (PyUnicode_IS_ASCII(string)) |
| 10187 | list = asciilib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10188 | string, PyUnicode_1BYTE_DATA(string), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10189 | PyUnicode_GET_LENGTH(string), keepends); |
| 10190 | else |
| 10191 | list = ucs1lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10192 | string, PyUnicode_1BYTE_DATA(string), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10193 | PyUnicode_GET_LENGTH(string), keepends); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10194 | break; |
| 10195 | case PyUnicode_2BYTE_KIND: |
| 10196 | list = ucs2lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10197 | string, PyUnicode_2BYTE_DATA(string), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10198 | PyUnicode_GET_LENGTH(string), keepends); |
| 10199 | break; |
| 10200 | case PyUnicode_4BYTE_KIND: |
| 10201 | list = ucs4lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10202 | string, PyUnicode_4BYTE_DATA(string), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10203 | PyUnicode_GET_LENGTH(string), keepends); |
| 10204 | break; |
| 10205 | default: |
| 10206 | assert(0); |
| 10207 | list = 0; |
| 10208 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10209 | Py_DECREF(string); |
| 10210 | return list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10211 | } |
| 10212 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10213 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10214 | split(PyObject *self, |
| 10215 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10216 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10217 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10218 | int kind1, kind2, kind; |
| 10219 | void *buf1, *buf2; |
| 10220 | Py_ssize_t len1, len2; |
| 10221 | PyObject* out; |
| 10222 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10223 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10224 | maxcount = PY_SSIZE_T_MAX; |
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 (PyUnicode_READY(self) == -1) |
| 10227 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10228 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10229 | if (substring == NULL) |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10230 | switch (PyUnicode_KIND(self)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10231 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10232 | if (PyUnicode_IS_ASCII(self)) |
| 10233 | return asciilib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10234 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10235 | PyUnicode_GET_LENGTH(self), maxcount |
| 10236 | ); |
| 10237 | else |
| 10238 | return ucs1lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10239 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10240 | PyUnicode_GET_LENGTH(self), maxcount |
| 10241 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10242 | case PyUnicode_2BYTE_KIND: |
| 10243 | return ucs2lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10244 | self, PyUnicode_2BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10245 | PyUnicode_GET_LENGTH(self), maxcount |
| 10246 | ); |
| 10247 | case PyUnicode_4BYTE_KIND: |
| 10248 | return ucs4lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10249 | self, PyUnicode_4BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10250 | PyUnicode_GET_LENGTH(self), maxcount |
| 10251 | ); |
| 10252 | default: |
| 10253 | assert(0); |
| 10254 | return NULL; |
| 10255 | } |
| 10256 | |
| 10257 | if (PyUnicode_READY(substring) == -1) |
| 10258 | return NULL; |
| 10259 | |
| 10260 | kind1 = PyUnicode_KIND(self); |
| 10261 | kind2 = PyUnicode_KIND(substring); |
| 10262 | kind = kind1 > kind2 ? kind1 : kind2; |
| 10263 | buf1 = PyUnicode_DATA(self); |
| 10264 | buf2 = PyUnicode_DATA(substring); |
| 10265 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10266 | buf1 = _PyUnicode_AsKind(self, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10267 | if (!buf1) |
| 10268 | return NULL; |
| 10269 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10270 | buf2 = _PyUnicode_AsKind(substring, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10271 | if (!buf2) { |
| 10272 | if (kind1 != kind) PyMem_Free(buf1); |
| 10273 | return NULL; |
| 10274 | } |
| 10275 | len1 = PyUnicode_GET_LENGTH(self); |
| 10276 | len2 = PyUnicode_GET_LENGTH(substring); |
| 10277 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10278 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10279 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10280 | if (PyUnicode_IS_ASCII(self) && PyUnicode_IS_ASCII(substring)) |
| 10281 | out = asciilib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10282 | self, buf1, len1, buf2, len2, maxcount); |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10283 | else |
| 10284 | out = ucs1lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10285 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10286 | break; |
| 10287 | case PyUnicode_2BYTE_KIND: |
| 10288 | out = ucs2lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10289 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10290 | break; |
| 10291 | case PyUnicode_4BYTE_KIND: |
| 10292 | out = ucs4lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10293 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10294 | break; |
| 10295 | default: |
| 10296 | out = NULL; |
| 10297 | } |
| 10298 | if (kind1 != kind) |
| 10299 | PyMem_Free(buf1); |
| 10300 | if (kind2 != kind) |
| 10301 | PyMem_Free(buf2); |
| 10302 | return out; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10303 | } |
| 10304 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10305 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10306 | rsplit(PyObject *self, |
| 10307 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10308 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10309 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10310 | int kind1, kind2, kind; |
| 10311 | void *buf1, *buf2; |
| 10312 | Py_ssize_t len1, len2; |
| 10313 | PyObject* out; |
| 10314 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10315 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10316 | maxcount = PY_SSIZE_T_MAX; |
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 (PyUnicode_READY(self) == -1) |
| 10319 | return NULL; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10320 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10321 | if (substring == NULL) |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10322 | switch (PyUnicode_KIND(self)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10323 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10324 | if (PyUnicode_IS_ASCII(self)) |
| 10325 | return asciilib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10326 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10327 | PyUnicode_GET_LENGTH(self), maxcount |
| 10328 | ); |
| 10329 | else |
| 10330 | return ucs1lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10331 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10332 | PyUnicode_GET_LENGTH(self), maxcount |
| 10333 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10334 | case PyUnicode_2BYTE_KIND: |
| 10335 | return ucs2lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10336 | self, PyUnicode_2BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10337 | PyUnicode_GET_LENGTH(self), maxcount |
| 10338 | ); |
| 10339 | case PyUnicode_4BYTE_KIND: |
| 10340 | return ucs4lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10341 | self, PyUnicode_4BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10342 | PyUnicode_GET_LENGTH(self), maxcount |
| 10343 | ); |
| 10344 | default: |
| 10345 | assert(0); |
| 10346 | return NULL; |
| 10347 | } |
| 10348 | |
| 10349 | if (PyUnicode_READY(substring) == -1) |
| 10350 | return NULL; |
| 10351 | |
| 10352 | kind1 = PyUnicode_KIND(self); |
| 10353 | kind2 = PyUnicode_KIND(substring); |
| 10354 | kind = kind1 > kind2 ? kind1 : kind2; |
| 10355 | buf1 = PyUnicode_DATA(self); |
| 10356 | buf2 = PyUnicode_DATA(substring); |
| 10357 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10358 | buf1 = _PyUnicode_AsKind(self, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10359 | if (!buf1) |
| 10360 | return NULL; |
| 10361 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10362 | buf2 = _PyUnicode_AsKind(substring, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10363 | if (!buf2) { |
| 10364 | if (kind1 != kind) PyMem_Free(buf1); |
| 10365 | return NULL; |
| 10366 | } |
| 10367 | len1 = PyUnicode_GET_LENGTH(self); |
| 10368 | len2 = PyUnicode_GET_LENGTH(substring); |
| 10369 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10370 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10371 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10372 | if (PyUnicode_IS_ASCII(self) && PyUnicode_IS_ASCII(substring)) |
| 10373 | out = asciilib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10374 | self, buf1, len1, buf2, len2, maxcount); |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10375 | else |
| 10376 | out = ucs1lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10377 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10378 | break; |
| 10379 | case PyUnicode_2BYTE_KIND: |
| 10380 | out = ucs2lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10381 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10382 | break; |
| 10383 | case PyUnicode_4BYTE_KIND: |
| 10384 | out = ucs4lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10385 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10386 | break; |
| 10387 | default: |
| 10388 | out = NULL; |
| 10389 | } |
| 10390 | if (kind1 != kind) |
| 10391 | PyMem_Free(buf1); |
| 10392 | if (kind2 != kind) |
| 10393 | PyMem_Free(buf2); |
| 10394 | return out; |
| 10395 | } |
| 10396 | |
| 10397 | static Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10398 | anylib_find(int kind, PyObject *str1, void *buf1, Py_ssize_t len1, |
| 10399 | 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] | 10400 | { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10401 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10402 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10403 | if (PyUnicode_IS_ASCII(str1) && PyUnicode_IS_ASCII(str2)) |
| 10404 | return asciilib_find(buf1, len1, buf2, len2, offset); |
| 10405 | else |
| 10406 | return ucs1lib_find(buf1, len1, buf2, len2, offset); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10407 | case PyUnicode_2BYTE_KIND: |
| 10408 | return ucs2lib_find(buf1, len1, buf2, len2, offset); |
| 10409 | case PyUnicode_4BYTE_KIND: |
| 10410 | return ucs4lib_find(buf1, len1, buf2, len2, offset); |
| 10411 | } |
| 10412 | assert(0); |
| 10413 | return -1; |
| 10414 | } |
| 10415 | |
| 10416 | static Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10417 | anylib_count(int kind, PyObject *sstr, void* sbuf, Py_ssize_t slen, |
| 10418 | 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] | 10419 | { |
Benjamin Peterson | c0b95d1 | 2011-12-20 17:24:05 -0600 | [diff] [blame] | 10420 | switch (kind) { |
| 10421 | case PyUnicode_1BYTE_KIND: |
| 10422 | if (PyUnicode_IS_ASCII(sstr) && PyUnicode_IS_ASCII(str1)) |
| 10423 | return asciilib_count(sbuf, slen, buf1, len1, maxcount); |
| 10424 | else |
| 10425 | return ucs1lib_count(sbuf, slen, buf1, len1, maxcount); |
| 10426 | case PyUnicode_2BYTE_KIND: |
| 10427 | return ucs2lib_count(sbuf, slen, buf1, len1, maxcount); |
| 10428 | case PyUnicode_4BYTE_KIND: |
| 10429 | return ucs4lib_count(sbuf, slen, buf1, len1, maxcount); |
| 10430 | } |
| 10431 | assert(0); |
| 10432 | return 0; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10433 | } |
| 10434 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10435 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10436 | replace(PyObject *self, PyObject *str1, |
| 10437 | PyObject *str2, Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10438 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10439 | PyObject *u; |
| 10440 | char *sbuf = PyUnicode_DATA(self); |
| 10441 | char *buf1 = PyUnicode_DATA(str1); |
| 10442 | char *buf2 = PyUnicode_DATA(str2); |
| 10443 | int srelease = 0, release1 = 0, release2 = 0; |
| 10444 | int skind = PyUnicode_KIND(self); |
| 10445 | int kind1 = PyUnicode_KIND(str1); |
| 10446 | int kind2 = PyUnicode_KIND(str2); |
| 10447 | Py_ssize_t slen = PyUnicode_GET_LENGTH(self); |
| 10448 | Py_ssize_t len1 = PyUnicode_GET_LENGTH(str1); |
| 10449 | Py_ssize_t len2 = PyUnicode_GET_LENGTH(str2); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10450 | int mayshrink; |
| 10451 | Py_UCS4 maxchar, maxchar_str2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10452 | |
| 10453 | if (maxcount < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10454 | maxcount = PY_SSIZE_T_MAX; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10455 | else if (maxcount == 0 || slen == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10456 | goto nothing; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10457 | |
Victor Stinner | 59de0ee | 2011-10-07 10:01:28 +0200 | [diff] [blame] | 10458 | if (str1 == str2) |
| 10459 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10460 | if (skind < kind1) |
| 10461 | /* substring too wide to be present */ |
| 10462 | goto nothing; |
| 10463 | |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10464 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
| 10465 | maxchar_str2 = PyUnicode_MAX_CHAR_VALUE(str2); |
| 10466 | /* Replacing str1 with str2 may cause a maxchar reduction in the |
| 10467 | result string. */ |
| 10468 | mayshrink = (maxchar_str2 < maxchar); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 10469 | maxchar = MAX_MAXCHAR(maxchar, maxchar_str2); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10470 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10471 | if (len1 == len2) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10472 | /* same length */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10473 | if (len1 == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10474 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10475 | if (len1 == 1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10476 | /* replace characters */ |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10477 | Py_UCS4 u1, u2; |
| 10478 | int rkind; |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10479 | Py_ssize_t index, pos; |
| 10480 | char *src; |
| 10481 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10482 | u1 = PyUnicode_READ_CHAR(str1, 0); |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10483 | pos = findchar(sbuf, PyUnicode_KIND(self), slen, u1, 1); |
| 10484 | if (pos < 0) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10485 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10486 | u2 = PyUnicode_READ_CHAR(str2, 0); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10487 | u = PyUnicode_New(slen, maxchar); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10488 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10489 | goto error; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 10490 | copy_characters(u, 0, self, 0, slen); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10491 | rkind = PyUnicode_KIND(u); |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10492 | |
| 10493 | PyUnicode_WRITE(rkind, PyUnicode_DATA(u), pos, u2); |
| 10494 | index = 0; |
| 10495 | src = sbuf; |
| 10496 | while (--maxcount) |
| 10497 | { |
| 10498 | pos++; |
| 10499 | src += pos * PyUnicode_KIND(self); |
| 10500 | slen -= pos; |
| 10501 | index += pos; |
| 10502 | pos = findchar(src, PyUnicode_KIND(self), slen, u1, 1); |
| 10503 | if (pos < 0) |
| 10504 | break; |
| 10505 | PyUnicode_WRITE(rkind, PyUnicode_DATA(u), index + pos, u2); |
| 10506 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10507 | } |
| 10508 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10509 | int rkind = skind; |
| 10510 | char *res; |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10511 | Py_ssize_t i; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10512 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10513 | if (kind1 < rkind) { |
| 10514 | /* widen substring */ |
| 10515 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10516 | if (!buf1) goto error; |
| 10517 | release1 = 1; |
| 10518 | } |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10519 | i = anylib_find(rkind, self, sbuf, slen, str1, buf1, len1, 0); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10520 | if (i < 0) |
| 10521 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10522 | if (rkind > kind2) { |
| 10523 | /* widen replacement */ |
| 10524 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 10525 | if (!buf2) goto error; |
| 10526 | release2 = 1; |
| 10527 | } |
| 10528 | else if (rkind < kind2) { |
| 10529 | /* widen self and buf1 */ |
| 10530 | rkind = kind2; |
| 10531 | if (release1) PyMem_Free(buf1); |
| 10532 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 10533 | if (!sbuf) goto error; |
| 10534 | srelease = 1; |
| 10535 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10536 | if (!buf1) goto error; |
| 10537 | release1 = 1; |
| 10538 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10539 | u = PyUnicode_New(slen, maxchar); |
| 10540 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10541 | goto error; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10542 | assert(PyUnicode_KIND(u) == rkind); |
| 10543 | res = PyUnicode_DATA(u); |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10544 | |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10545 | memcpy(res, sbuf, rkind * slen); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10546 | /* change everything in-place, starting with this one */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10547 | memcpy(res + rkind * i, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10548 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10549 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10550 | i += len1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10551 | |
| 10552 | while ( --maxcount > 0) { |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10553 | i = anylib_find(rkind, self, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10554 | sbuf+rkind*i, slen-i, |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10555 | str1, buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10556 | if (i == -1) |
| 10557 | break; |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10558 | memcpy(res + rkind * i, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10559 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10560 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10561 | i += len1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10562 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10563 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10564 | } |
| 10565 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10566 | Py_ssize_t n, i, j, ires; |
| 10567 | Py_ssize_t product, new_size; |
| 10568 | int rkind = skind; |
| 10569 | char *res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10570 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10571 | if (kind1 < rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10572 | /* widen substring */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10573 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10574 | if (!buf1) goto error; |
| 10575 | release1 = 1; |
| 10576 | } |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10577 | n = anylib_count(rkind, self, sbuf, slen, str1, buf1, len1, maxcount); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10578 | if (n == 0) |
| 10579 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10580 | if (kind2 < rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10581 | /* widen replacement */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10582 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 10583 | if (!buf2) goto error; |
| 10584 | release2 = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10585 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10586 | else if (kind2 > rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10587 | /* widen self and buf1 */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10588 | rkind = kind2; |
| 10589 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 10590 | if (!sbuf) goto error; |
| 10591 | srelease = 1; |
| 10592 | if (release1) PyMem_Free(buf1); |
| 10593 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10594 | if (!buf1) goto error; |
| 10595 | release1 = 1; |
| 10596 | } |
| 10597 | /* new_size = PyUnicode_GET_LENGTH(self) + n * (PyUnicode_GET_LENGTH(str2) - |
| 10598 | PyUnicode_GET_LENGTH(str1))); */ |
| 10599 | product = n * (len2-len1); |
| 10600 | if ((product / (len2-len1)) != n) { |
| 10601 | PyErr_SetString(PyExc_OverflowError, |
| 10602 | "replace string is too long"); |
| 10603 | goto error; |
| 10604 | } |
| 10605 | new_size = slen + product; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10606 | if (new_size == 0) { |
| 10607 | Py_INCREF(unicode_empty); |
| 10608 | u = unicode_empty; |
| 10609 | goto done; |
| 10610 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10611 | if (new_size < 0 || new_size > (PY_SSIZE_T_MAX >> (rkind-1))) { |
| 10612 | PyErr_SetString(PyExc_OverflowError, |
| 10613 | "replace string is too long"); |
| 10614 | goto error; |
| 10615 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10616 | u = PyUnicode_New(new_size, maxchar); |
| 10617 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10618 | goto error; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10619 | assert(PyUnicode_KIND(u) == rkind); |
| 10620 | res = PyUnicode_DATA(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10621 | ires = i = 0; |
| 10622 | if (len1 > 0) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10623 | while (n-- > 0) { |
| 10624 | /* look for next match */ |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10625 | j = anylib_find(rkind, self, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10626 | sbuf + rkind * i, slen-i, |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10627 | str1, buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10628 | if (j == -1) |
| 10629 | break; |
| 10630 | else if (j > i) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10631 | /* copy unchanged part [i:j] */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10632 | memcpy(res + rkind * ires, |
| 10633 | sbuf + rkind * i, |
| 10634 | rkind * (j-i)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10635 | ires += j - i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10636 | } |
| 10637 | /* copy substitution string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10638 | if (len2 > 0) { |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10639 | memcpy(res + rkind * ires, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10640 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10641 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10642 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10643 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10644 | i = j + len1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10645 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10646 | if (i < slen) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10647 | /* copy tail [i:] */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10648 | memcpy(res + rkind * ires, |
| 10649 | sbuf + rkind * i, |
| 10650 | rkind * (slen-i)); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10651 | } |
| 10652 | else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10653 | /* interleave */ |
| 10654 | while (n > 0) { |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10655 | memcpy(res + rkind * ires, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10656 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10657 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10658 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10659 | if (--n <= 0) |
| 10660 | break; |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10661 | memcpy(res + rkind * ires, |
| 10662 | sbuf + rkind * i, |
| 10663 | rkind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10664 | ires++; |
| 10665 | i++; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10666 | } |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10667 | memcpy(res + rkind * ires, |
| 10668 | sbuf + rkind * i, |
| 10669 | rkind * (slen-i)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10670 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10671 | } |
| 10672 | |
| 10673 | if (mayshrink) { |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10674 | unicode_adjust_maxchar(&u); |
| 10675 | if (u == NULL) |
| 10676 | goto error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10677 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10678 | |
| 10679 | done: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10680 | if (srelease) |
| 10681 | PyMem_FREE(sbuf); |
| 10682 | if (release1) |
| 10683 | PyMem_FREE(buf1); |
| 10684 | if (release2) |
| 10685 | PyMem_FREE(buf2); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10686 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10687 | return u; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10688 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10689 | nothing: |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10690 | /* nothing to replace; return original string (when possible) */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10691 | if (srelease) |
| 10692 | PyMem_FREE(sbuf); |
| 10693 | if (release1) |
| 10694 | PyMem_FREE(buf1); |
| 10695 | if (release2) |
| 10696 | PyMem_FREE(buf2); |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10697 | return unicode_result_unchanged(self); |
| 10698 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10699 | error: |
| 10700 | if (srelease && sbuf) |
| 10701 | PyMem_FREE(sbuf); |
| 10702 | if (release1 && buf1) |
| 10703 | PyMem_FREE(buf1); |
| 10704 | if (release2 && buf2) |
| 10705 | PyMem_FREE(buf2); |
| 10706 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10707 | } |
| 10708 | |
| 10709 | /* --- Unicode Object Methods --------------------------------------------- */ |
| 10710 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10711 | PyDoc_STRVAR(title__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10712 | "S.title() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10713 | \n\ |
| 10714 | 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] | 10715 | characters, all remaining cased characters have lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10716 | |
| 10717 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10718 | unicode_title(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10719 | { |
Benjamin Peterson | eea4846 | 2012-01-16 14:28:50 -0500 | [diff] [blame] | 10720 | if (PyUnicode_READY(self) == -1) |
| 10721 | return NULL; |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 10722 | return case_operation(self, do_title); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10723 | } |
| 10724 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10725 | PyDoc_STRVAR(capitalize__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10726 | "S.capitalize() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10727 | \n\ |
| 10728 | 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] | 10729 | have upper case and the rest lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10730 | |
| 10731 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10732 | unicode_capitalize(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10733 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 10734 | if (PyUnicode_READY(self) == -1) |
| 10735 | return NULL; |
| 10736 | if (PyUnicode_GET_LENGTH(self) == 0) |
| 10737 | return unicode_result_unchanged(self); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 10738 | return case_operation(self, do_capitalize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10739 | } |
| 10740 | |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 10741 | PyDoc_STRVAR(casefold__doc__, |
| 10742 | "S.casefold() -> str\n\ |
| 10743 | \n\ |
| 10744 | Return a version of S suitable for caseless comparisons."); |
| 10745 | |
| 10746 | static PyObject * |
| 10747 | unicode_casefold(PyObject *self) |
| 10748 | { |
| 10749 | if (PyUnicode_READY(self) == -1) |
| 10750 | return NULL; |
| 10751 | if (PyUnicode_IS_ASCII(self)) |
| 10752 | return ascii_upper_or_lower(self, 1); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 10753 | return case_operation(self, do_casefold); |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 10754 | } |
| 10755 | |
| 10756 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10757 | /* Argument converter. Coerces to a single unicode character */ |
| 10758 | |
| 10759 | static int |
| 10760 | convert_uc(PyObject *obj, void *addr) |
| 10761 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10762 | Py_UCS4 *fillcharloc = (Py_UCS4 *)addr; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10763 | PyObject *uniobj; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10764 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10765 | uniobj = PyUnicode_FromObject(obj); |
| 10766 | if (uniobj == NULL) { |
| 10767 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10768 | "The fill character cannot be converted to Unicode"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10769 | return 0; |
| 10770 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10771 | if (PyUnicode_GET_LENGTH(uniobj) != 1) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10772 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10773 | "The fill character must be exactly one character long"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10774 | Py_DECREF(uniobj); |
| 10775 | return 0; |
| 10776 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10777 | *fillcharloc = PyUnicode_READ_CHAR(uniobj, 0); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10778 | Py_DECREF(uniobj); |
| 10779 | return 1; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10780 | } |
| 10781 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10782 | PyDoc_STRVAR(center__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10783 | "S.center(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10784 | \n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 10785 | Return S centered in a string of length width. Padding is\n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10786 | done using the specified fill character (default is a space)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10787 | |
| 10788 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10789 | unicode_center(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10790 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10791 | Py_ssize_t marg, left; |
| 10792 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10793 | Py_UCS4 fillchar = ' '; |
| 10794 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 10795 | 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] | 10796 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10797 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 10798 | if (PyUnicode_READY(self) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10799 | return NULL; |
| 10800 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10801 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 10802 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10803 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10804 | marg = width - PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10805 | left = marg / 2 + (marg & width & 1); |
| 10806 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10807 | return pad(self, left, marg - left, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10808 | } |
| 10809 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10810 | /* This function assumes that str1 and str2 are readied by the caller. */ |
| 10811 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10812 | static int |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10813 | unicode_compare(PyObject *str1, PyObject *str2) |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10814 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10815 | int kind1, kind2; |
| 10816 | void *data1, *data2; |
| 10817 | Py_ssize_t len1, len2, i; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10818 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10819 | kind1 = PyUnicode_KIND(str1); |
| 10820 | kind2 = PyUnicode_KIND(str2); |
| 10821 | data1 = PyUnicode_DATA(str1); |
| 10822 | data2 = PyUnicode_DATA(str2); |
| 10823 | len1 = PyUnicode_GET_LENGTH(str1); |
| 10824 | len2 = PyUnicode_GET_LENGTH(str2); |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10825 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10826 | for (i = 0; i < len1 && i < len2; ++i) { |
| 10827 | Py_UCS4 c1, c2; |
| 10828 | c1 = PyUnicode_READ(kind1, data1, i); |
| 10829 | c2 = PyUnicode_READ(kind2, data2, i); |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 10830 | |
| 10831 | if (c1 != c2) |
| 10832 | return (c1 < c2) ? -1 : 1; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10833 | } |
| 10834 | |
| 10835 | return (len1 < len2) ? -1 : (len1 != len2); |
| 10836 | } |
| 10837 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10838 | int |
| 10839 | PyUnicode_Compare(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10840 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10841 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 10842 | if (PyUnicode_READY(left) == -1 || |
| 10843 | PyUnicode_READY(right) == -1) |
| 10844 | return -1; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10845 | return unicode_compare(left, right); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10846 | } |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 10847 | PyErr_Format(PyExc_TypeError, |
| 10848 | "Can't compare %.100s and %.100s", |
| 10849 | left->ob_type->tp_name, |
| 10850 | right->ob_type->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10851 | return -1; |
| 10852 | } |
| 10853 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10854 | int |
| 10855 | PyUnicode_CompareWithASCIIString(PyObject* uni, const char* str) |
| 10856 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10857 | Py_ssize_t i; |
| 10858 | int kind; |
| 10859 | void *data; |
| 10860 | Py_UCS4 chr; |
| 10861 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 10862 | assert(_PyUnicode_CHECK(uni)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10863 | if (PyUnicode_READY(uni) == -1) |
| 10864 | return -1; |
| 10865 | kind = PyUnicode_KIND(uni); |
| 10866 | data = PyUnicode_DATA(uni); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10867 | /* Compare Unicode string and source character set string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10868 | for (i = 0; (chr = PyUnicode_READ(kind, data, i)) && str[i]; i++) |
| 10869 | if (chr != str[i]) |
| 10870 | return (chr < (unsigned char)(str[i])) ? -1 : 1; |
Benjamin Peterson | 8667a9b | 2010-01-09 21:45:28 +0000 | [diff] [blame] | 10871 | /* This check keeps Python strings that end in '\0' from comparing equal |
| 10872 | to C strings identical up to that point. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10873 | if (PyUnicode_GET_LENGTH(uni) != i || chr) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10874 | return 1; /* uni is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10875 | if (str[i]) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10876 | return -1; /* str is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10877 | return 0; |
| 10878 | } |
| 10879 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10880 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10881 | #define TEST_COND(cond) \ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10882 | ((cond) ? Py_True : Py_False) |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10883 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10884 | PyObject * |
| 10885 | PyUnicode_RichCompare(PyObject *left, PyObject *right, int op) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10886 | { |
| 10887 | int result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10888 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10889 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 10890 | PyObject *v; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10891 | if (PyUnicode_READY(left) == -1 || |
| 10892 | PyUnicode_READY(right) == -1) |
| 10893 | return NULL; |
| 10894 | if (PyUnicode_GET_LENGTH(left) != PyUnicode_GET_LENGTH(right) || |
| 10895 | PyUnicode_KIND(left) != PyUnicode_KIND(right)) { |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10896 | if (op == Py_EQ) { |
| 10897 | Py_INCREF(Py_False); |
| 10898 | return Py_False; |
| 10899 | } |
| 10900 | if (op == Py_NE) { |
| 10901 | Py_INCREF(Py_True); |
| 10902 | return Py_True; |
| 10903 | } |
| 10904 | } |
| 10905 | if (left == right) |
| 10906 | result = 0; |
| 10907 | else |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10908 | result = unicode_compare(left, right); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10909 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10910 | /* Convert the return value to a Boolean */ |
| 10911 | switch (op) { |
| 10912 | case Py_EQ: |
| 10913 | v = TEST_COND(result == 0); |
| 10914 | break; |
| 10915 | case Py_NE: |
| 10916 | v = TEST_COND(result != 0); |
| 10917 | break; |
| 10918 | case Py_LE: |
| 10919 | v = TEST_COND(result <= 0); |
| 10920 | break; |
| 10921 | case Py_GE: |
| 10922 | v = TEST_COND(result >= 0); |
| 10923 | break; |
| 10924 | case Py_LT: |
| 10925 | v = TEST_COND(result == -1); |
| 10926 | break; |
| 10927 | case Py_GT: |
| 10928 | v = TEST_COND(result == 1); |
| 10929 | break; |
| 10930 | default: |
| 10931 | PyErr_BadArgument(); |
| 10932 | return NULL; |
| 10933 | } |
| 10934 | Py_INCREF(v); |
| 10935 | return v; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10936 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10937 | |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 10938 | Py_RETURN_NOTIMPLEMENTED; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10939 | } |
| 10940 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10941 | int |
| 10942 | PyUnicode_Contains(PyObject *container, PyObject *element) |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10943 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10944 | PyObject *str, *sub; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10945 | int kind1, kind2, kind; |
| 10946 | void *buf1, *buf2; |
| 10947 | Py_ssize_t len1, len2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10948 | int result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10949 | |
| 10950 | /* Coerce the two arguments */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10951 | sub = PyUnicode_FromObject(element); |
| 10952 | if (!sub) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10953 | PyErr_Format(PyExc_TypeError, |
| 10954 | "'in <string>' requires string as left operand, not %s", |
| 10955 | element->ob_type->tp_name); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10956 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10957 | } |
| 10958 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10959 | str = PyUnicode_FromObject(container); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 10960 | if (!str) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10961 | Py_DECREF(sub); |
| 10962 | return -1; |
| 10963 | } |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 10964 | if (PyUnicode_READY(sub) == -1 || PyUnicode_READY(str) == -1) { |
| 10965 | Py_DECREF(sub); |
| 10966 | Py_DECREF(str); |
| 10967 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10968 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10969 | kind1 = PyUnicode_KIND(str); |
| 10970 | kind2 = PyUnicode_KIND(sub); |
| 10971 | kind = kind1 > kind2 ? kind1 : kind2; |
| 10972 | buf1 = PyUnicode_DATA(str); |
| 10973 | buf2 = PyUnicode_DATA(sub); |
| 10974 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10975 | buf1 = _PyUnicode_AsKind(str, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10976 | if (!buf1) { |
| 10977 | Py_DECREF(sub); |
| 10978 | return -1; |
| 10979 | } |
| 10980 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10981 | buf2 = _PyUnicode_AsKind(sub, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10982 | if (!buf2) { |
| 10983 | Py_DECREF(sub); |
| 10984 | if (kind1 != kind) PyMem_Free(buf1); |
| 10985 | return -1; |
| 10986 | } |
| 10987 | len1 = PyUnicode_GET_LENGTH(str); |
| 10988 | len2 = PyUnicode_GET_LENGTH(sub); |
| 10989 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10990 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10991 | case PyUnicode_1BYTE_KIND: |
| 10992 | result = ucs1lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10993 | break; |
| 10994 | case PyUnicode_2BYTE_KIND: |
| 10995 | result = ucs2lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10996 | break; |
| 10997 | case PyUnicode_4BYTE_KIND: |
| 10998 | result = ucs4lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10999 | break; |
| 11000 | default: |
| 11001 | result = -1; |
| 11002 | assert(0); |
| 11003 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11004 | |
| 11005 | Py_DECREF(str); |
| 11006 | Py_DECREF(sub); |
| 11007 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11008 | if (kind1 != kind) |
| 11009 | PyMem_Free(buf1); |
| 11010 | if (kind2 != kind) |
| 11011 | PyMem_Free(buf2); |
| 11012 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 11013 | return result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 11014 | } |
| 11015 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11016 | /* Concat to string or Unicode object giving a new Unicode object. */ |
| 11017 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 11018 | PyObject * |
| 11019 | PyUnicode_Concat(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11020 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11021 | PyObject *u = NULL, *v = NULL, *w; |
Victor Stinner | 127226b | 2011-10-13 01:12:34 +0200 | [diff] [blame] | 11022 | Py_UCS4 maxchar, maxchar2; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11023 | Py_ssize_t u_len, v_len, new_len; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11024 | |
| 11025 | /* Coerce the two arguments */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11026 | u = PyUnicode_FromObject(left); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11027 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11028 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11029 | v = PyUnicode_FromObject(right); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11030 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11031 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11032 | |
| 11033 | /* Shortcuts */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 11034 | if (v == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11035 | Py_DECREF(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11036 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11037 | } |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 11038 | if (u == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11039 | Py_DECREF(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11040 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11041 | } |
| 11042 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11043 | u_len = PyUnicode_GET_LENGTH(u); |
| 11044 | v_len = PyUnicode_GET_LENGTH(v); |
| 11045 | if (u_len > PY_SSIZE_T_MAX - v_len) { |
| 11046 | PyErr_SetString(PyExc_OverflowError, |
| 11047 | "strings are too large to concat"); |
| 11048 | goto onError; |
| 11049 | } |
| 11050 | new_len = u_len + v_len; |
| 11051 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11052 | maxchar = PyUnicode_MAX_CHAR_VALUE(u); |
Victor Stinner | 127226b | 2011-10-13 01:12:34 +0200 | [diff] [blame] | 11053 | maxchar2 = PyUnicode_MAX_CHAR_VALUE(v); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 11054 | maxchar = MAX_MAXCHAR(maxchar, maxchar2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11055 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11056 | /* Concat the two Unicode strings */ |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11057 | w = PyUnicode_New(new_len, maxchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11058 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11059 | goto onError; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11060 | copy_characters(w, 0, u, 0, u_len); |
| 11061 | copy_characters(w, u_len, v, 0, v_len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11062 | Py_DECREF(u); |
| 11063 | Py_DECREF(v); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 11064 | assert(_PyUnicode_CheckConsistency(w, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11065 | return w; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11066 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11067 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11068 | Py_XDECREF(u); |
| 11069 | Py_XDECREF(v); |
| 11070 | return NULL; |
| 11071 | } |
| 11072 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 11073 | void |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11074 | PyUnicode_Append(PyObject **p_left, PyObject *right) |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 11075 | { |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11076 | PyObject *left, *res; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11077 | Py_UCS4 maxchar, maxchar2; |
| 11078 | Py_ssize_t left_len, right_len, new_len; |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11079 | |
| 11080 | if (p_left == NULL) { |
| 11081 | if (!PyErr_Occurred()) |
| 11082 | PyErr_BadInternalCall(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11083 | return; |
| 11084 | } |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11085 | left = *p_left; |
| 11086 | if (right == NULL || !PyUnicode_Check(left)) { |
| 11087 | if (!PyErr_Occurred()) |
| 11088 | PyErr_BadInternalCall(); |
| 11089 | goto error; |
| 11090 | } |
| 11091 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 11092 | if (PyUnicode_READY(left) == -1) |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 11093 | goto error; |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 11094 | if (PyUnicode_READY(right) == -1) |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 11095 | goto error; |
| 11096 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11097 | /* Shortcuts */ |
| 11098 | if (left == unicode_empty) { |
| 11099 | Py_DECREF(left); |
| 11100 | Py_INCREF(right); |
| 11101 | *p_left = right; |
| 11102 | return; |
| 11103 | } |
| 11104 | if (right == unicode_empty) |
| 11105 | return; |
| 11106 | |
| 11107 | left_len = PyUnicode_GET_LENGTH(left); |
| 11108 | right_len = PyUnicode_GET_LENGTH(right); |
| 11109 | if (left_len > PY_SSIZE_T_MAX - right_len) { |
| 11110 | PyErr_SetString(PyExc_OverflowError, |
| 11111 | "strings are too large to concat"); |
| 11112 | goto error; |
| 11113 | } |
| 11114 | new_len = left_len + right_len; |
| 11115 | |
| 11116 | if (unicode_modifiable(left) |
| 11117 | && PyUnicode_CheckExact(right) |
| 11118 | && PyUnicode_KIND(right) <= PyUnicode_KIND(left) |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 11119 | /* Don't resize for ascii += latin1. Convert ascii to latin1 requires |
| 11120 | to change the structure size, but characters are stored just after |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 11121 | the structure, and so it requires to move all characters which is |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 11122 | not so different than duplicating the string. */ |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11123 | && !(PyUnicode_IS_ASCII(left) && !PyUnicode_IS_ASCII(right))) |
| 11124 | { |
| 11125 | /* append inplace */ |
| 11126 | if (unicode_resize(p_left, new_len) != 0) { |
| 11127 | /* XXX if _PyUnicode_Resize() fails, 'left' has been |
| 11128 | * deallocated so it cannot be put back into |
| 11129 | * 'variable'. The MemoryError is raised when there |
| 11130 | * is no value in 'variable', which might (very |
| 11131 | * remotely) be a cause of incompatibilities. |
| 11132 | */ |
| 11133 | goto error; |
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 | /* copy 'right' into the newly allocated area of 'left' */ |
| 11136 | copy_characters(*p_left, left_len, right, 0, right_len); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11137 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11138 | else { |
| 11139 | maxchar = PyUnicode_MAX_CHAR_VALUE(left); |
| 11140 | maxchar2 = PyUnicode_MAX_CHAR_VALUE(right); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 11141 | maxchar = MAX_MAXCHAR(maxchar, maxchar2); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11142 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11143 | /* Concat the two Unicode strings */ |
| 11144 | res = PyUnicode_New(new_len, maxchar); |
| 11145 | if (res == NULL) |
| 11146 | goto error; |
| 11147 | copy_characters(res, 0, left, 0, left_len); |
| 11148 | copy_characters(res, left_len, right, 0, right_len); |
| 11149 | Py_DECREF(left); |
| 11150 | *p_left = res; |
| 11151 | } |
| 11152 | assert(_PyUnicode_CheckConsistency(*p_left, 1)); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11153 | return; |
| 11154 | |
| 11155 | error: |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11156 | Py_CLEAR(*p_left); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 11157 | } |
| 11158 | |
| 11159 | void |
| 11160 | PyUnicode_AppendAndDel(PyObject **pleft, PyObject *right) |
| 11161 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11162 | PyUnicode_Append(pleft, right); |
| 11163 | Py_XDECREF(right); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 11164 | } |
| 11165 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11166 | PyDoc_STRVAR(count__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11167 | "S.count(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11168 | \n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11169 | Return the number of non-overlapping occurrences of substring sub in\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11170 | 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] | 11171 | interpreted as in slice notation."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11172 | |
| 11173 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11174 | unicode_count(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11175 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11176 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11177 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11178 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11179 | PyObject *result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11180 | int kind1, kind2, kind; |
| 11181 | void *buf1, *buf2; |
| 11182 | Py_ssize_t len1, len2, iresult; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11183 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11184 | if (!stringlib_parse_args_finds_unicode("count", args, &substring, |
| 11185 | &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11186 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11187 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11188 | kind1 = PyUnicode_KIND(self); |
| 11189 | kind2 = PyUnicode_KIND(substring); |
Benjamin Peterson | b63f49f | 2012-05-03 18:31:07 -0400 | [diff] [blame] | 11190 | if (kind2 > kind1) |
| 11191 | return PyLong_FromLong(0); |
| 11192 | kind = kind1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11193 | buf1 = PyUnicode_DATA(self); |
| 11194 | buf2 = PyUnicode_DATA(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11195 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11196 | buf2 = _PyUnicode_AsKind(substring, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11197 | if (!buf2) { |
| 11198 | Py_DECREF(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11199 | return NULL; |
| 11200 | } |
| 11201 | len1 = PyUnicode_GET_LENGTH(self); |
| 11202 | len2 = PyUnicode_GET_LENGTH(substring); |
| 11203 | |
| 11204 | ADJUST_INDICES(start, end, len1); |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 11205 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11206 | case PyUnicode_1BYTE_KIND: |
| 11207 | iresult = ucs1lib_count( |
| 11208 | ((Py_UCS1*)buf1) + start, end - start, |
| 11209 | buf2, len2, PY_SSIZE_T_MAX |
| 11210 | ); |
| 11211 | break; |
| 11212 | case PyUnicode_2BYTE_KIND: |
| 11213 | iresult = ucs2lib_count( |
| 11214 | ((Py_UCS2*)buf1) + start, end - start, |
| 11215 | buf2, len2, PY_SSIZE_T_MAX |
| 11216 | ); |
| 11217 | break; |
| 11218 | case PyUnicode_4BYTE_KIND: |
| 11219 | iresult = ucs4lib_count( |
| 11220 | ((Py_UCS4*)buf1) + start, end - start, |
| 11221 | buf2, len2, PY_SSIZE_T_MAX |
| 11222 | ); |
| 11223 | break; |
| 11224 | default: |
| 11225 | assert(0); iresult = 0; |
| 11226 | } |
| 11227 | |
| 11228 | result = PyLong_FromSsize_t(iresult); |
| 11229 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11230 | if (kind2 != kind) |
| 11231 | PyMem_Free(buf2); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11232 | |
| 11233 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11234 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11235 | return result; |
| 11236 | } |
| 11237 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11238 | PyDoc_STRVAR(encode__doc__, |
Victor Stinner | c911bbf | 2010-11-07 19:04:46 +0000 | [diff] [blame] | 11239 | "S.encode(encoding='utf-8', errors='strict') -> bytes\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11240 | \n\ |
Victor Stinner | e14e212 | 2010-11-07 18:41:46 +0000 | [diff] [blame] | 11241 | Encode S using the codec registered for encoding. Default encoding\n\ |
| 11242 | 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] | 11243 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 11244 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 11245 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 11246 | codecs.register_error that can handle UnicodeEncodeErrors."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11247 | |
| 11248 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11249 | unicode_encode(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11250 | { |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 11251 | static char *kwlist[] = {"encoding", "errors", 0}; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11252 | char *encoding = NULL; |
| 11253 | char *errors = NULL; |
Guido van Rossum | 35d9428 | 2007-08-27 18:20:11 +0000 | [diff] [blame] | 11254 | |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 11255 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:encode", |
| 11256 | kwlist, &encoding, &errors)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11257 | return NULL; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11258 | return PyUnicode_AsEncodedString(self, encoding, errors); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 11259 | } |
| 11260 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11261 | PyDoc_STRVAR(expandtabs__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11262 | "S.expandtabs([tabsize]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11263 | \n\ |
| 11264 | 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] | 11265 | 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] | 11266 | |
| 11267 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11268 | unicode_expandtabs(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11269 | { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11270 | Py_ssize_t i, j, line_pos, src_len, incr; |
| 11271 | Py_UCS4 ch; |
| 11272 | PyObject *u; |
| 11273 | void *src_data, *dest_data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11274 | int tabsize = 8; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11275 | int kind; |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 11276 | int found; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11277 | |
| 11278 | if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11279 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11280 | |
Antoine Pitrou | 2242522 | 2011-10-04 19:10:51 +0200 | [diff] [blame] | 11281 | if (PyUnicode_READY(self) == -1) |
| 11282 | return NULL; |
| 11283 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 11284 | /* First pass: determine size of output string */ |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11285 | src_len = PyUnicode_GET_LENGTH(self); |
| 11286 | i = j = line_pos = 0; |
| 11287 | kind = PyUnicode_KIND(self); |
| 11288 | src_data = PyUnicode_DATA(self); |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 11289 | found = 0; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11290 | for (; i < src_len; i++) { |
| 11291 | ch = PyUnicode_READ(kind, src_data, i); |
| 11292 | if (ch == '\t') { |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 11293 | found = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11294 | if (tabsize > 0) { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11295 | incr = tabsize - (line_pos % tabsize); /* cannot overflow */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11296 | if (j > PY_SSIZE_T_MAX - incr) |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11297 | goto overflow; |
| 11298 | line_pos += incr; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11299 | j += incr; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11300 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11301 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11302 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11303 | if (j > PY_SSIZE_T_MAX - 1) |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11304 | goto overflow; |
| 11305 | line_pos++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11306 | j++; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11307 | if (ch == '\n' || ch == '\r') |
| 11308 | line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11309 | } |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11310 | } |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11311 | if (!found) |
| 11312 | return unicode_result_unchanged(self); |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 11313 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11314 | /* Second pass: create output string and fill it */ |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11315 | u = PyUnicode_New(j, PyUnicode_MAX_CHAR_VALUE(self)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11316 | if (!u) |
| 11317 | return NULL; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11318 | dest_data = PyUnicode_DATA(u); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11319 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11320 | i = j = line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11321 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11322 | for (; i < src_len; i++) { |
| 11323 | ch = PyUnicode_READ(kind, src_data, i); |
| 11324 | if (ch == '\t') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11325 | if (tabsize > 0) { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11326 | incr = tabsize - (line_pos % tabsize); |
| 11327 | line_pos += incr; |
Victor Stinner | da79e63 | 2012-02-22 13:37:04 +0100 | [diff] [blame] | 11328 | FILL(kind, dest_data, ' ', j, incr); |
| 11329 | j += incr; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11330 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11331 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11332 | else { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11333 | line_pos++; |
| 11334 | PyUnicode_WRITE(kind, dest_data, j, ch); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11335 | j++; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11336 | if (ch == '\n' || ch == '\r') |
| 11337 | line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11338 | } |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11339 | } |
| 11340 | assert (j == PyUnicode_GET_LENGTH(u)); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 11341 | return unicode_result(u); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11342 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11343 | overflow: |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11344 | PyErr_SetString(PyExc_OverflowError, "new string is too long"); |
| 11345 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11346 | } |
| 11347 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11348 | PyDoc_STRVAR(find__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11349 | "S.find(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11350 | \n\ |
| 11351 | Return the lowest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 11352 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11353 | arguments start and end are interpreted as in slice notation.\n\ |
| 11354 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11355 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11356 | |
| 11357 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11358 | unicode_find(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11359 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11360 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 11361 | Py_ssize_t start; |
| 11362 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11363 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11364 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11365 | if (!stringlib_parse_args_finds_unicode("find", args, &substring, |
| 11366 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11367 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11368 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11369 | if (PyUnicode_READY(self) == -1) |
| 11370 | return NULL; |
| 11371 | if (PyUnicode_READY(substring) == -1) |
| 11372 | return NULL; |
| 11373 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11374 | result = any_find_slice(1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11375 | |
| 11376 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11377 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11378 | if (result == -2) |
| 11379 | return NULL; |
| 11380 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 11381 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11382 | } |
| 11383 | |
| 11384 | static PyObject * |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 11385 | unicode_getitem(PyObject *self, Py_ssize_t index) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11386 | { |
Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 11387 | void *data; |
| 11388 | enum PyUnicode_Kind kind; |
| 11389 | Py_UCS4 ch; |
| 11390 | PyObject *res; |
| 11391 | |
| 11392 | if (!PyUnicode_Check(self) || PyUnicode_READY(self) == -1) { |
| 11393 | PyErr_BadArgument(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11394 | return NULL; |
Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 11395 | } |
| 11396 | if (index < 0 || index >= PyUnicode_GET_LENGTH(self)) { |
| 11397 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 11398 | return NULL; |
| 11399 | } |
| 11400 | kind = PyUnicode_KIND(self); |
| 11401 | data = PyUnicode_DATA(self); |
| 11402 | ch = PyUnicode_READ(kind, data, index); |
| 11403 | if (ch < 256) |
| 11404 | return get_latin1_char(ch); |
| 11405 | |
| 11406 | res = PyUnicode_New(1, ch); |
| 11407 | if (res == NULL) |
| 11408 | return NULL; |
| 11409 | kind = PyUnicode_KIND(res); |
| 11410 | data = PyUnicode_DATA(res); |
| 11411 | PyUnicode_WRITE(kind, data, 0, ch); |
| 11412 | assert(_PyUnicode_CheckConsistency(res, 1)); |
| 11413 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11414 | } |
| 11415 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11416 | /* Believe it or not, this produces the same value for ASCII strings |
Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 11417 | as bytes_hash(). */ |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 11418 | static Py_hash_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11419 | unicode_hash(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11420 | { |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11421 | Py_ssize_t len; |
Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 11422 | Py_uhash_t x; |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11423 | |
Benjamin Peterson | f6622c8 | 2012-04-09 14:53:07 -0400 | [diff] [blame] | 11424 | #ifdef Py_DEBUG |
Benjamin Peterson | 69e9727 | 2012-02-21 11:08:50 -0500 | [diff] [blame] | 11425 | assert(_Py_HashSecret_Initialized); |
Benjamin Peterson | f6622c8 | 2012-04-09 14:53:07 -0400 | [diff] [blame] | 11426 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11427 | if (_PyUnicode_HASH(self) != -1) |
| 11428 | return _PyUnicode_HASH(self); |
| 11429 | if (PyUnicode_READY(self) == -1) |
| 11430 | return -1; |
| 11431 | len = PyUnicode_GET_LENGTH(self); |
Georg Brandl | 16fa2a1 | 2012-02-21 00:50:13 +0100 | [diff] [blame] | 11432 | /* |
| 11433 | We make the hash of the empty string be 0, rather than using |
| 11434 | (prefix ^ suffix), since this slightly obfuscates the hash secret |
| 11435 | */ |
| 11436 | if (len == 0) { |
| 11437 | _PyUnicode_HASH(self) = 0; |
| 11438 | return 0; |
| 11439 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11440 | |
| 11441 | /* The hash function as a macro, gets expanded three times below. */ |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 11442 | #define HASH(P) \ |
| 11443 | x ^= (Py_uhash_t) *P << 7; \ |
| 11444 | while (--len >= 0) \ |
| 11445 | x = (_PyHASH_MULTIPLIER * x) ^ (Py_uhash_t) *P++; \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11446 | |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 11447 | x = (Py_uhash_t) _Py_HashSecret.prefix; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11448 | switch (PyUnicode_KIND(self)) { |
| 11449 | case PyUnicode_1BYTE_KIND: { |
| 11450 | const unsigned char *c = PyUnicode_1BYTE_DATA(self); |
| 11451 | HASH(c); |
| 11452 | break; |
| 11453 | } |
| 11454 | case PyUnicode_2BYTE_KIND: { |
| 11455 | const Py_UCS2 *s = PyUnicode_2BYTE_DATA(self); |
| 11456 | HASH(s); |
| 11457 | break; |
| 11458 | } |
| 11459 | default: { |
| 11460 | Py_UCS4 *l; |
| 11461 | assert(PyUnicode_KIND(self) == PyUnicode_4BYTE_KIND && |
| 11462 | "Impossible switch case in unicode_hash"); |
| 11463 | l = PyUnicode_4BYTE_DATA(self); |
| 11464 | HASH(l); |
| 11465 | break; |
| 11466 | } |
| 11467 | } |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 11468 | x ^= (Py_uhash_t) PyUnicode_GET_LENGTH(self); |
| 11469 | x ^= (Py_uhash_t) _Py_HashSecret.suffix; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11470 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11471 | if (x == -1) |
| 11472 | x = -2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11473 | _PyUnicode_HASH(self) = x; |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11474 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11475 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11476 | #undef HASH |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11477 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11478 | PyDoc_STRVAR(index__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11479 | "S.index(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11480 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11481 | 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] | 11482 | |
| 11483 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11484 | unicode_index(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11485 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11486 | Py_ssize_t result; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11487 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 11488 | Py_ssize_t start; |
| 11489 | Py_ssize_t end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11490 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11491 | if (!stringlib_parse_args_finds_unicode("index", args, &substring, |
| 11492 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11493 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11494 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11495 | if (PyUnicode_READY(self) == -1) |
| 11496 | return NULL; |
| 11497 | if (PyUnicode_READY(substring) == -1) |
| 11498 | return NULL; |
| 11499 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11500 | result = any_find_slice(1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11501 | |
| 11502 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11503 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11504 | if (result == -2) |
| 11505 | return NULL; |
| 11506 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11507 | if (result < 0) { |
| 11508 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 11509 | return NULL; |
| 11510 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11511 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 11512 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11513 | } |
| 11514 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11515 | PyDoc_STRVAR(islower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11516 | "S.islower() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11517 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11518 | 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] | 11519 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11520 | |
| 11521 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11522 | unicode_islower(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11523 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11524 | Py_ssize_t i, length; |
| 11525 | int kind; |
| 11526 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11527 | int cased; |
| 11528 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11529 | if (PyUnicode_READY(self) == -1) |
| 11530 | return NULL; |
| 11531 | length = PyUnicode_GET_LENGTH(self); |
| 11532 | kind = PyUnicode_KIND(self); |
| 11533 | data = PyUnicode_DATA(self); |
| 11534 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11535 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11536 | if (length == 1) |
| 11537 | return PyBool_FromLong( |
| 11538 | Py_UNICODE_ISLOWER(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11539 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11540 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11541 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11542 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11543 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11544 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11545 | for (i = 0; i < length; i++) { |
| 11546 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11547 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11548 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 11549 | return PyBool_FromLong(0); |
| 11550 | else if (!cased && Py_UNICODE_ISLOWER(ch)) |
| 11551 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11552 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11553 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11554 | } |
| 11555 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11556 | PyDoc_STRVAR(isupper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11557 | "S.isupper() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11558 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11559 | 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] | 11560 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11561 | |
| 11562 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11563 | unicode_isupper(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11564 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11565 | Py_ssize_t i, length; |
| 11566 | int kind; |
| 11567 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11568 | int cased; |
| 11569 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11570 | if (PyUnicode_READY(self) == -1) |
| 11571 | return NULL; |
| 11572 | length = PyUnicode_GET_LENGTH(self); |
| 11573 | kind = PyUnicode_KIND(self); |
| 11574 | data = PyUnicode_DATA(self); |
| 11575 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11576 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11577 | if (length == 1) |
| 11578 | return PyBool_FromLong( |
| 11579 | Py_UNICODE_ISUPPER(PyUnicode_READ(kind, data, 0)) != 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11580 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11581 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11582 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11583 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11584 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11585 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11586 | for (i = 0; i < length; i++) { |
| 11587 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11588 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11589 | if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 11590 | return PyBool_FromLong(0); |
| 11591 | else if (!cased && Py_UNICODE_ISUPPER(ch)) |
| 11592 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11593 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11594 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11595 | } |
| 11596 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11597 | PyDoc_STRVAR(istitle__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11598 | "S.istitle() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11599 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11600 | Return True if S is a titlecased string and there is at least one\n\ |
| 11601 | character in S, i.e. upper- and titlecase characters may only\n\ |
| 11602 | follow uncased characters and lowercase characters only cased ones.\n\ |
| 11603 | Return False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11604 | |
| 11605 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11606 | unicode_istitle(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11607 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11608 | Py_ssize_t i, length; |
| 11609 | int kind; |
| 11610 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11611 | int cased, previous_is_cased; |
| 11612 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11613 | if (PyUnicode_READY(self) == -1) |
| 11614 | return NULL; |
| 11615 | length = PyUnicode_GET_LENGTH(self); |
| 11616 | kind = PyUnicode_KIND(self); |
| 11617 | data = PyUnicode_DATA(self); |
| 11618 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11619 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11620 | if (length == 1) { |
| 11621 | Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11622 | return PyBool_FromLong((Py_UNICODE_ISTITLE(ch) != 0) || |
| 11623 | (Py_UNICODE_ISUPPER(ch) != 0)); |
| 11624 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11625 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11626 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11627 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11628 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11629 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11630 | cased = 0; |
| 11631 | previous_is_cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11632 | for (i = 0; i < length; i++) { |
| 11633 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11634 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11635 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { |
| 11636 | if (previous_is_cased) |
| 11637 | return PyBool_FromLong(0); |
| 11638 | previous_is_cased = 1; |
| 11639 | cased = 1; |
| 11640 | } |
| 11641 | else if (Py_UNICODE_ISLOWER(ch)) { |
| 11642 | if (!previous_is_cased) |
| 11643 | return PyBool_FromLong(0); |
| 11644 | previous_is_cased = 1; |
| 11645 | cased = 1; |
| 11646 | } |
| 11647 | else |
| 11648 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11649 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11650 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11651 | } |
| 11652 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11653 | PyDoc_STRVAR(isspace__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11654 | "S.isspace() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11655 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11656 | Return True if all characters in S are whitespace\n\ |
| 11657 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11658 | |
| 11659 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11660 | unicode_isspace(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11661 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11662 | Py_ssize_t i, length; |
| 11663 | int kind; |
| 11664 | void *data; |
| 11665 | |
| 11666 | if (PyUnicode_READY(self) == -1) |
| 11667 | return NULL; |
| 11668 | length = PyUnicode_GET_LENGTH(self); |
| 11669 | kind = PyUnicode_KIND(self); |
| 11670 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11671 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11672 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11673 | if (length == 1) |
| 11674 | return PyBool_FromLong( |
| 11675 | Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11676 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11677 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11678 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11679 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11680 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11681 | for (i = 0; i < length; i++) { |
| 11682 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11683 | if (!Py_UNICODE_ISSPACE(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11684 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11685 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11686 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11687 | } |
| 11688 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11689 | PyDoc_STRVAR(isalpha__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11690 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11691 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11692 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11693 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11694 | |
| 11695 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11696 | unicode_isalpha(PyObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11697 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11698 | Py_ssize_t i, length; |
| 11699 | int kind; |
| 11700 | void *data; |
| 11701 | |
| 11702 | if (PyUnicode_READY(self) == -1) |
| 11703 | return NULL; |
| 11704 | length = PyUnicode_GET_LENGTH(self); |
| 11705 | kind = PyUnicode_KIND(self); |
| 11706 | data = PyUnicode_DATA(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11707 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11708 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11709 | if (length == 1) |
| 11710 | return PyBool_FromLong( |
| 11711 | Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, 0))); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11712 | |
| 11713 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11714 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11715 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11716 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11717 | for (i = 0; i < length; i++) { |
| 11718 | if (!Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, i))) |
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 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11721 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11722 | } |
| 11723 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11724 | PyDoc_STRVAR(isalnum__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11725 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11726 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11727 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11728 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11729 | |
| 11730 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11731 | unicode_isalnum(PyObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11732 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11733 | int kind; |
| 11734 | void *data; |
| 11735 | Py_ssize_t len, i; |
| 11736 | |
| 11737 | if (PyUnicode_READY(self) == -1) |
| 11738 | return NULL; |
| 11739 | |
| 11740 | kind = PyUnicode_KIND(self); |
| 11741 | data = PyUnicode_DATA(self); |
| 11742 | len = PyUnicode_GET_LENGTH(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11743 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11744 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11745 | if (len == 1) { |
| 11746 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11747 | return PyBool_FromLong(Py_UNICODE_ISALNUM(ch)); |
| 11748 | } |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11749 | |
| 11750 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11751 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11752 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11753 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11754 | for (i = 0; i < len; i++) { |
| 11755 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11756 | if (!Py_UNICODE_ISALNUM(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11757 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11758 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11759 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11760 | } |
| 11761 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11762 | PyDoc_STRVAR(isdecimal__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11763 | "S.isdecimal() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11764 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11765 | 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] | 11766 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11767 | |
| 11768 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11769 | unicode_isdecimal(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11770 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11771 | Py_ssize_t i, length; |
| 11772 | int kind; |
| 11773 | void *data; |
| 11774 | |
| 11775 | if (PyUnicode_READY(self) == -1) |
| 11776 | return NULL; |
| 11777 | length = PyUnicode_GET_LENGTH(self); |
| 11778 | kind = PyUnicode_KIND(self); |
| 11779 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11780 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11781 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11782 | if (length == 1) |
| 11783 | return PyBool_FromLong( |
| 11784 | Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11785 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11786 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11787 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11788 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11789 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11790 | for (i = 0; i < length; i++) { |
| 11791 | if (!Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11792 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11793 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11794 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11795 | } |
| 11796 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11797 | PyDoc_STRVAR(isdigit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11798 | "S.isdigit() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11799 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11800 | Return True if all characters in S are digits\n\ |
| 11801 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11802 | |
| 11803 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11804 | unicode_isdigit(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11805 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11806 | Py_ssize_t i, length; |
| 11807 | int kind; |
| 11808 | void *data; |
| 11809 | |
| 11810 | if (PyUnicode_READY(self) == -1) |
| 11811 | return NULL; |
| 11812 | length = PyUnicode_GET_LENGTH(self); |
| 11813 | kind = PyUnicode_KIND(self); |
| 11814 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11815 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11816 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11817 | if (length == 1) { |
| 11818 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11819 | return PyBool_FromLong(Py_UNICODE_ISDIGIT(ch)); |
| 11820 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11821 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11822 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11823 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11824 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11825 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11826 | for (i = 0; i < length; i++) { |
| 11827 | if (!Py_UNICODE_ISDIGIT(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11828 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11829 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11830 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11831 | } |
| 11832 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11833 | PyDoc_STRVAR(isnumeric__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11834 | "S.isnumeric() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11835 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11836 | 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] | 11837 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11838 | |
| 11839 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11840 | unicode_isnumeric(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11841 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11842 | Py_ssize_t i, length; |
| 11843 | int kind; |
| 11844 | void *data; |
| 11845 | |
| 11846 | if (PyUnicode_READY(self) == -1) |
| 11847 | return NULL; |
| 11848 | length = PyUnicode_GET_LENGTH(self); |
| 11849 | kind = PyUnicode_KIND(self); |
| 11850 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11851 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11852 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11853 | if (length == 1) |
| 11854 | return PyBool_FromLong( |
| 11855 | Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11856 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11857 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11858 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11859 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11860 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11861 | for (i = 0; i < length; i++) { |
| 11862 | if (!Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11863 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11864 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11865 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11866 | } |
| 11867 | |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11868 | int |
| 11869 | PyUnicode_IsIdentifier(PyObject *self) |
| 11870 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11871 | int kind; |
| 11872 | void *data; |
| 11873 | Py_ssize_t i; |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11874 | Py_UCS4 first; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11875 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11876 | if (PyUnicode_READY(self) == -1) { |
| 11877 | Py_FatalError("identifier not ready"); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11878 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11879 | } |
| 11880 | |
| 11881 | /* Special case for empty strings */ |
| 11882 | if (PyUnicode_GET_LENGTH(self) == 0) |
| 11883 | return 0; |
| 11884 | kind = PyUnicode_KIND(self); |
| 11885 | data = PyUnicode_DATA(self); |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11886 | |
| 11887 | /* PEP 3131 says that the first character must be in |
| 11888 | XID_Start and subsequent characters in XID_Continue, |
| 11889 | and for the ASCII range, the 2.x rules apply (i.e |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11890 | start with letters and underscore, continue with |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11891 | letters, digits, underscore). However, given the current |
| 11892 | definition of XID_Start and XID_Continue, it is sufficient |
| 11893 | to check just for these, except that _ must be allowed |
| 11894 | as starting an identifier. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11895 | first = PyUnicode_READ(kind, data, 0); |
Benjamin Peterson | f413b80 | 2011-08-12 22:17:18 -0500 | [diff] [blame] | 11896 | if (!_PyUnicode_IsXidStart(first) && first != 0x5F /* LOW LINE */) |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11897 | return 0; |
| 11898 | |
Benjamin Peterson | 9c6e6a0 | 2011-09-28 08:09:05 -0400 | [diff] [blame] | 11899 | for (i = 1; i < PyUnicode_GET_LENGTH(self); i++) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11900 | if (!_PyUnicode_IsXidContinue(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11901 | return 0; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11902 | return 1; |
| 11903 | } |
| 11904 | |
| 11905 | PyDoc_STRVAR(isidentifier__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11906 | "S.isidentifier() -> bool\n\ |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11907 | \n\ |
| 11908 | Return True if S is a valid identifier according\n\ |
| 11909 | to the language definition."); |
| 11910 | |
| 11911 | static PyObject* |
| 11912 | unicode_isidentifier(PyObject *self) |
| 11913 | { |
| 11914 | return PyBool_FromLong(PyUnicode_IsIdentifier(self)); |
| 11915 | } |
| 11916 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11917 | PyDoc_STRVAR(isprintable__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11918 | "S.isprintable() -> bool\n\ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11919 | \n\ |
| 11920 | Return True if all characters in S are considered\n\ |
| 11921 | printable in repr() or S is empty, False otherwise."); |
| 11922 | |
| 11923 | static PyObject* |
| 11924 | unicode_isprintable(PyObject *self) |
| 11925 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11926 | Py_ssize_t i, length; |
| 11927 | int kind; |
| 11928 | void *data; |
| 11929 | |
| 11930 | if (PyUnicode_READY(self) == -1) |
| 11931 | return NULL; |
| 11932 | length = PyUnicode_GET_LENGTH(self); |
| 11933 | kind = PyUnicode_KIND(self); |
| 11934 | data = PyUnicode_DATA(self); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11935 | |
| 11936 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11937 | if (length == 1) |
| 11938 | return PyBool_FromLong( |
| 11939 | Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, 0))); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11940 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11941 | for (i = 0; i < length; i++) { |
| 11942 | if (!Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, i))) { |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11943 | Py_RETURN_FALSE; |
| 11944 | } |
| 11945 | } |
| 11946 | Py_RETURN_TRUE; |
| 11947 | } |
| 11948 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11949 | PyDoc_STRVAR(join__doc__, |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 11950 | "S.join(iterable) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11951 | \n\ |
| 11952 | 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] | 11953 | iterable. The separator between elements is S."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11954 | |
| 11955 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11956 | unicode_join(PyObject *self, PyObject *data) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11957 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11958 | return PyUnicode_Join(self, data); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11959 | } |
| 11960 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11961 | static Py_ssize_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11962 | unicode_length(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11963 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11964 | if (PyUnicode_READY(self) == -1) |
| 11965 | return -1; |
| 11966 | return PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11967 | } |
| 11968 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11969 | PyDoc_STRVAR(ljust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11970 | "S.ljust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11971 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 11972 | 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] | 11973 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11974 | |
| 11975 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11976 | unicode_ljust(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11977 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11978 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11979 | Py_UCS4 fillchar = ' '; |
| 11980 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11981 | if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11982 | return NULL; |
| 11983 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 11984 | if (PyUnicode_READY(self) == -1) |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11985 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11986 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11987 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 11988 | return unicode_result_unchanged(self); |
| 11989 | |
| 11990 | return pad(self, 0, width - PyUnicode_GET_LENGTH(self), fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11991 | } |
| 11992 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11993 | PyDoc_STRVAR(lower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11994 | "S.lower() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11995 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11996 | Return a copy of the string S converted to lowercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11997 | |
| 11998 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11999 | unicode_lower(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12000 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 12001 | if (PyUnicode_READY(self) == -1) |
| 12002 | return NULL; |
| 12003 | if (PyUnicode_IS_ASCII(self)) |
| 12004 | return ascii_upper_or_lower(self, 1); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 12005 | return case_operation(self, do_lower); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12006 | } |
| 12007 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12008 | #define LEFTSTRIP 0 |
| 12009 | #define RIGHTSTRIP 1 |
| 12010 | #define BOTHSTRIP 2 |
| 12011 | |
| 12012 | /* Arrays indexed by above */ |
| 12013 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 12014 | |
| 12015 | #define STRIPNAME(i) (stripformat[i]+3) |
| 12016 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12017 | /* externally visible for str.strip(unicode) */ |
| 12018 | PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12019 | _PyUnicode_XStrip(PyObject *self, int striptype, PyObject *sepobj) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12020 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12021 | void *data; |
| 12022 | int kind; |
| 12023 | Py_ssize_t i, j, len; |
| 12024 | BLOOM_MASK sepmask; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12025 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12026 | if (PyUnicode_READY(self) == -1 || PyUnicode_READY(sepobj) == -1) |
| 12027 | return NULL; |
| 12028 | |
| 12029 | kind = PyUnicode_KIND(self); |
| 12030 | data = PyUnicode_DATA(self); |
| 12031 | len = PyUnicode_GET_LENGTH(self); |
| 12032 | sepmask = make_bloom_mask(PyUnicode_KIND(sepobj), |
| 12033 | PyUnicode_DATA(sepobj), |
| 12034 | PyUnicode_GET_LENGTH(sepobj)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12035 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12036 | i = 0; |
| 12037 | if (striptype != RIGHTSTRIP) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12038 | while (i < len && |
| 12039 | BLOOM_MEMBER(sepmask, PyUnicode_READ(kind, data, i), sepobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12040 | i++; |
| 12041 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12042 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12043 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12044 | j = len; |
| 12045 | if (striptype != LEFTSTRIP) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12046 | do { |
| 12047 | j--; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12048 | } while (j >= i && |
| 12049 | BLOOM_MEMBER(sepmask, PyUnicode_READ(kind, data, j), sepobj)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12050 | j++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12051 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12052 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12053 | return PyUnicode_Substring(self, i, j); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12054 | } |
| 12055 | |
| 12056 | PyObject* |
| 12057 | PyUnicode_Substring(PyObject *self, Py_ssize_t start, Py_ssize_t end) |
| 12058 | { |
| 12059 | unsigned char *data; |
| 12060 | int kind; |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 12061 | Py_ssize_t length; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12062 | |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 12063 | if (PyUnicode_READY(self) == -1) |
| 12064 | return NULL; |
| 12065 | |
Victor Stinner | 684d5fd | 2012-05-03 02:32:34 +0200 | [diff] [blame] | 12066 | length = PyUnicode_GET_LENGTH(self); |
| 12067 | end = Py_MIN(end, length); |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 12068 | |
Victor Stinner | 684d5fd | 2012-05-03 02:32:34 +0200 | [diff] [blame] | 12069 | if (start == 0 && end == length) |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12070 | return unicode_result_unchanged(self); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12071 | |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 12072 | if (start < 0 || end < 0) { |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 12073 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 12074 | return NULL; |
| 12075 | } |
Victor Stinner | 684d5fd | 2012-05-03 02:32:34 +0200 | [diff] [blame] | 12076 | if (start >= length || end < start) { |
Victor Stinner | 3a7f7977 | 2012-05-03 03:36:40 +0200 | [diff] [blame] | 12077 | Py_INCREF(unicode_empty); |
| 12078 | return unicode_empty; |
Victor Stinner | 684d5fd | 2012-05-03 02:32:34 +0200 | [diff] [blame] | 12079 | } |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 12080 | |
Victor Stinner | 684d5fd | 2012-05-03 02:32:34 +0200 | [diff] [blame] | 12081 | length = end - start; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 12082 | if (PyUnicode_IS_ASCII(self)) { |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 12083 | data = PyUnicode_1BYTE_DATA(self); |
| 12084 | return unicode_fromascii(data + start, length); |
| 12085 | } |
| 12086 | else { |
| 12087 | kind = PyUnicode_KIND(self); |
| 12088 | data = PyUnicode_1BYTE_DATA(self); |
| 12089 | return PyUnicode_FromKindAndData(kind, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 12090 | data + kind * start, |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 12091 | length); |
| 12092 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12093 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12094 | |
| 12095 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12096 | do_strip(PyObject *self, int striptype) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12097 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12098 | int kind; |
| 12099 | void *data; |
| 12100 | Py_ssize_t len, i, j; |
| 12101 | |
| 12102 | if (PyUnicode_READY(self) == -1) |
| 12103 | return NULL; |
| 12104 | |
| 12105 | kind = PyUnicode_KIND(self); |
| 12106 | data = PyUnicode_DATA(self); |
| 12107 | len = PyUnicode_GET_LENGTH(self); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12108 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12109 | i = 0; |
| 12110 | if (striptype != RIGHTSTRIP) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12111 | while (i < len && Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, i))) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12112 | i++; |
| 12113 | } |
| 12114 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12115 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12116 | j = len; |
| 12117 | if (striptype != LEFTSTRIP) { |
| 12118 | do { |
| 12119 | j--; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12120 | } while (j >= i && Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, j))); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12121 | j++; |
| 12122 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12123 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12124 | return PyUnicode_Substring(self, i, j); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12125 | } |
| 12126 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12127 | |
| 12128 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12129 | do_argstrip(PyObject *self, int striptype, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12130 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12131 | PyObject *sep = NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12132 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12133 | if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) |
| 12134 | return NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12135 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12136 | if (sep != NULL && sep != Py_None) { |
| 12137 | if (PyUnicode_Check(sep)) |
| 12138 | return _PyUnicode_XStrip(self, striptype, sep); |
| 12139 | else { |
| 12140 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12141 | "%s arg must be None or str", |
| 12142 | STRIPNAME(striptype)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12143 | return NULL; |
| 12144 | } |
| 12145 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12146 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12147 | return do_strip(self, striptype); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12148 | } |
| 12149 | |
| 12150 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12151 | PyDoc_STRVAR(strip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12152 | "S.strip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12153 | \n\ |
| 12154 | Return a copy of the string S with leading and trailing\n\ |
| 12155 | whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12156 | 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] | 12157 | |
| 12158 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12159 | unicode_strip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12160 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12161 | if (PyTuple_GET_SIZE(args) == 0) |
| 12162 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 12163 | else |
| 12164 | return do_argstrip(self, BOTHSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12165 | } |
| 12166 | |
| 12167 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12168 | PyDoc_STRVAR(lstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12169 | "S.lstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12170 | \n\ |
| 12171 | Return a copy of the string S with leading whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12172 | 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] | 12173 | |
| 12174 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12175 | unicode_lstrip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12176 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12177 | if (PyTuple_GET_SIZE(args) == 0) |
| 12178 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 12179 | else |
| 12180 | return do_argstrip(self, LEFTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12181 | } |
| 12182 | |
| 12183 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12184 | PyDoc_STRVAR(rstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12185 | "S.rstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12186 | \n\ |
| 12187 | Return a copy of the string S with trailing whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12188 | 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] | 12189 | |
| 12190 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12191 | unicode_rstrip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12192 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12193 | if (PyTuple_GET_SIZE(args) == 0) |
| 12194 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 12195 | else |
| 12196 | return do_argstrip(self, RIGHTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12197 | } |
| 12198 | |
| 12199 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12200 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12201 | unicode_repeat(PyObject *str, Py_ssize_t len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12202 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12203 | PyObject *u; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12204 | Py_ssize_t nchars, n; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12205 | |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 12206 | if (len < 1) { |
| 12207 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 12208 | return unicode_empty; |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 12209 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12210 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12211 | /* no repeat, return original string */ |
| 12212 | if (len == 1) |
| 12213 | return unicode_result_unchanged(str); |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 12214 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 12215 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12216 | return NULL; |
| 12217 | |
Victor Stinner | c759f3e | 2011-10-01 03:09:58 +0200 | [diff] [blame] | 12218 | if (PyUnicode_GET_LENGTH(str) > PY_SSIZE_T_MAX / len) { |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 12219 | PyErr_SetString(PyExc_OverflowError, |
| 12220 | "repeated string is too long"); |
| 12221 | return NULL; |
| 12222 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12223 | nchars = len * PyUnicode_GET_LENGTH(str); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 12224 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12225 | u = PyUnicode_New(nchars, PyUnicode_MAX_CHAR_VALUE(str)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12226 | if (!u) |
| 12227 | return NULL; |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 12228 | assert(PyUnicode_KIND(u) == PyUnicode_KIND(str)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12229 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12230 | if (PyUnicode_GET_LENGTH(str) == 1) { |
| 12231 | const int kind = PyUnicode_KIND(str); |
| 12232 | const Py_UCS4 fill_char = PyUnicode_READ(kind, PyUnicode_DATA(str), 0); |
Victor Stinner | 73f53b5 | 2011-12-18 03:26:31 +0100 | [diff] [blame] | 12233 | if (kind == PyUnicode_1BYTE_KIND) { |
| 12234 | void *to = PyUnicode_DATA(u); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 12235 | memset(to, (unsigned char)fill_char, len); |
Victor Stinner | 73f53b5 | 2011-12-18 03:26:31 +0100 | [diff] [blame] | 12236 | } |
| 12237 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 12238 | Py_UCS2 *ucs2 = PyUnicode_2BYTE_DATA(u); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 12239 | for (n = 0; n < len; ++n) |
Victor Stinner | 73f53b5 | 2011-12-18 03:26:31 +0100 | [diff] [blame] | 12240 | ucs2[n] = fill_char; |
| 12241 | } else { |
| 12242 | Py_UCS4 *ucs4 = PyUnicode_4BYTE_DATA(u); |
| 12243 | assert(kind == PyUnicode_4BYTE_KIND); |
| 12244 | for (n = 0; n < len; ++n) |
| 12245 | ucs4[n] = fill_char; |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 12246 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12247 | } |
| 12248 | else { |
| 12249 | /* number of characters copied this far */ |
| 12250 | Py_ssize_t done = PyUnicode_GET_LENGTH(str); |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 12251 | const Py_ssize_t char_size = PyUnicode_KIND(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12252 | char *to = (char *) PyUnicode_DATA(u); |
| 12253 | Py_MEMCPY(to, PyUnicode_DATA(str), |
| 12254 | PyUnicode_GET_LENGTH(str) * char_size); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12255 | while (done < nchars) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12256 | n = (done <= nchars-done) ? done : nchars-done; |
| 12257 | Py_MEMCPY(to + (done * char_size), to, n * char_size); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12258 | done += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12259 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12260 | } |
| 12261 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12262 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12263 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12264 | } |
| 12265 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12266 | PyObject * |
| 12267 | PyUnicode_Replace(PyObject *obj, |
| 12268 | PyObject *subobj, |
| 12269 | PyObject *replobj, |
| 12270 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12271 | { |
| 12272 | PyObject *self; |
| 12273 | PyObject *str1; |
| 12274 | PyObject *str2; |
| 12275 | PyObject *result; |
| 12276 | |
| 12277 | self = PyUnicode_FromObject(obj); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12278 | if (self == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12279 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12280 | str1 = PyUnicode_FromObject(subobj); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12281 | if (str1 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12282 | Py_DECREF(self); |
| 12283 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12284 | } |
| 12285 | str2 = PyUnicode_FromObject(replobj); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12286 | if (str2 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12287 | Py_DECREF(self); |
| 12288 | Py_DECREF(str1); |
| 12289 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12290 | } |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12291 | if (PyUnicode_READY(self) == -1 || |
| 12292 | PyUnicode_READY(str1) == -1 || |
| 12293 | PyUnicode_READY(str2) == -1) |
| 12294 | result = NULL; |
| 12295 | else |
| 12296 | result = replace(self, str1, str2, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12297 | Py_DECREF(self); |
| 12298 | Py_DECREF(str1); |
| 12299 | Py_DECREF(str2); |
| 12300 | return result; |
| 12301 | } |
| 12302 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12303 | PyDoc_STRVAR(replace__doc__, |
Ezio Melotti | c1897e7 | 2010-06-26 18:50:39 +0000 | [diff] [blame] | 12304 | "S.replace(old, new[, count]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12305 | \n\ |
| 12306 | Return a copy of S with all occurrences of substring\n\ |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 12307 | old replaced by new. If the optional argument count is\n\ |
| 12308 | given, only the first count occurrences are replaced."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12309 | |
| 12310 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12311 | unicode_replace(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12312 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12313 | PyObject *str1; |
| 12314 | PyObject *str2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12315 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12316 | PyObject *result; |
| 12317 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12318 | if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12319 | return NULL; |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12320 | if (PyUnicode_READY(self) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12321 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12322 | str1 = PyUnicode_FromObject(str1); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12323 | if (str1 == NULL) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12324 | return NULL; |
| 12325 | str2 = PyUnicode_FromObject(str2); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12326 | if (str2 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12327 | Py_DECREF(str1); |
| 12328 | return NULL; |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 12329 | } |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12330 | if (PyUnicode_READY(str1) == -1 || PyUnicode_READY(str2) == -1) |
| 12331 | result = NULL; |
| 12332 | else |
| 12333 | result = replace(self, str1, str2, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12334 | |
| 12335 | Py_DECREF(str1); |
| 12336 | Py_DECREF(str2); |
| 12337 | return result; |
| 12338 | } |
| 12339 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12340 | static PyObject * |
| 12341 | unicode_repr(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12342 | { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12343 | PyObject *repr; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12344 | Py_ssize_t isize; |
| 12345 | Py_ssize_t osize, squote, dquote, i, o; |
| 12346 | Py_UCS4 max, quote; |
| 12347 | int ikind, okind; |
| 12348 | void *idata, *odata; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12349 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12350 | if (PyUnicode_READY(unicode) == -1) |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12351 | return NULL; |
| 12352 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12353 | isize = PyUnicode_GET_LENGTH(unicode); |
| 12354 | idata = PyUnicode_DATA(unicode); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12355 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12356 | /* Compute length of output, quote characters, and |
| 12357 | maximum character */ |
| 12358 | osize = 2; /* quotes */ |
| 12359 | max = 127; |
| 12360 | squote = dquote = 0; |
| 12361 | ikind = PyUnicode_KIND(unicode); |
| 12362 | for (i = 0; i < isize; i++) { |
| 12363 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
| 12364 | switch (ch) { |
| 12365 | case '\'': squote++; osize++; break; |
| 12366 | case '"': dquote++; osize++; break; |
| 12367 | case '\\': case '\t': case '\r': case '\n': |
| 12368 | osize += 2; break; |
| 12369 | default: |
| 12370 | /* Fast-path ASCII */ |
| 12371 | if (ch < ' ' || ch == 0x7f) |
| 12372 | osize += 4; /* \xHH */ |
| 12373 | else if (ch < 0x7f) |
| 12374 | osize++; |
| 12375 | else if (Py_UNICODE_ISPRINTABLE(ch)) { |
| 12376 | osize++; |
| 12377 | max = ch > max ? ch : max; |
| 12378 | } |
| 12379 | else if (ch < 0x100) |
| 12380 | osize += 4; /* \xHH */ |
| 12381 | else if (ch < 0x10000) |
| 12382 | osize += 6; /* \uHHHH */ |
| 12383 | else |
| 12384 | osize += 10; /* \uHHHHHHHH */ |
| 12385 | } |
| 12386 | } |
| 12387 | |
| 12388 | quote = '\''; |
| 12389 | if (squote) { |
| 12390 | if (dquote) |
| 12391 | /* Both squote and dquote present. Use squote, |
| 12392 | and escape them */ |
| 12393 | osize += squote; |
| 12394 | else |
| 12395 | quote = '"'; |
| 12396 | } |
| 12397 | |
| 12398 | repr = PyUnicode_New(osize, max); |
| 12399 | if (repr == NULL) |
| 12400 | return NULL; |
| 12401 | okind = PyUnicode_KIND(repr); |
| 12402 | odata = PyUnicode_DATA(repr); |
| 12403 | |
| 12404 | PyUnicode_WRITE(okind, odata, 0, quote); |
| 12405 | PyUnicode_WRITE(okind, odata, osize-1, quote); |
| 12406 | |
| 12407 | for (i = 0, o = 1; i < isize; i++) { |
| 12408 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12409 | |
| 12410 | /* Escape quotes and backslashes */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12411 | if ((ch == quote) || (ch == '\\')) { |
| 12412 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12413 | PyUnicode_WRITE(okind, odata, o++, ch); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12414 | continue; |
| 12415 | } |
| 12416 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12417 | /* Map special whitespace to '\t', \n', '\r' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12418 | if (ch == '\t') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12419 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12420 | PyUnicode_WRITE(okind, odata, o++, 't'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12421 | } |
| 12422 | else if (ch == '\n') { |
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++, 'n'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12425 | } |
| 12426 | else if (ch == '\r') { |
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++, 'r'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12429 | } |
| 12430 | |
| 12431 | /* Map non-printable US ASCII to '\xhh' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12432 | else if (ch < ' ' || ch == 0x7F) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12433 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12434 | PyUnicode_WRITE(okind, odata, o++, 'x'); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 12435 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0x000F]); |
| 12436 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0x000F]); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12437 | } |
| 12438 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12439 | /* Copy ASCII characters as-is */ |
| 12440 | else if (ch < 0x7F) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12441 | PyUnicode_WRITE(okind, odata, o++, ch); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12442 | } |
| 12443 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12444 | /* Non-ASCII characters */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12445 | else { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12446 | /* Map Unicode whitespace and control characters |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12447 | (categories Z* and C* except ASCII space) |
| 12448 | */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12449 | if (!Py_UNICODE_ISPRINTABLE(ch)) { |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12450 | /* Map 8-bit characters to '\xhh' */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12451 | if (ch <= 0xff) { |
| 12452 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12453 | PyUnicode_WRITE(okind, odata, o++, 'x'); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 12454 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0x000F]); |
| 12455 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0x000F]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12456 | } |
| 12457 | /* Map 21-bit characters to '\U00xxxxxx' */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12458 | else if (ch >= 0x10000) { |
| 12459 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12460 | PyUnicode_WRITE(okind, odata, o++, 'U'); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 12461 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 28) & 0xF]); |
| 12462 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 24) & 0xF]); |
| 12463 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 20) & 0xF]); |
| 12464 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 16) & 0xF]); |
| 12465 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 12) & 0xF]); |
| 12466 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 8) & 0xF]); |
| 12467 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0xF]); |
| 12468 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0xF]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12469 | } |
| 12470 | /* Map 16-bit characters to '\uxxxx' */ |
| 12471 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12472 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12473 | PyUnicode_WRITE(okind, odata, o++, 'u'); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 12474 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 12) & 0xF]); |
| 12475 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 8) & 0xF]); |
| 12476 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0xF]); |
| 12477 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0xF]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12478 | } |
| 12479 | } |
| 12480 | /* Copy characters as-is */ |
| 12481 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12482 | PyUnicode_WRITE(okind, odata, o++, ch); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12483 | } |
| 12484 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12485 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12486 | /* Closing quote already added at the beginning */ |
Victor Stinner | 05d1189 | 2011-10-06 01:13:58 +0200 | [diff] [blame] | 12487 | assert(_PyUnicode_CheckConsistency(repr, 1)); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12488 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12489 | } |
| 12490 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12491 | PyDoc_STRVAR(rfind__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12492 | "S.rfind(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12493 | \n\ |
| 12494 | Return the highest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 12495 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12496 | arguments start and end are interpreted as in slice notation.\n\ |
| 12497 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12498 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12499 | |
| 12500 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12501 | unicode_rfind(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12502 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12503 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 12504 | Py_ssize_t start; |
| 12505 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12506 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12507 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12508 | if (!stringlib_parse_args_finds_unicode("rfind", args, &substring, |
| 12509 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12510 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12511 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12512 | if (PyUnicode_READY(self) == -1) |
| 12513 | return NULL; |
| 12514 | if (PyUnicode_READY(substring) == -1) |
| 12515 | return NULL; |
| 12516 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12517 | result = any_find_slice(-1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12518 | |
| 12519 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12520 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12521 | if (result == -2) |
| 12522 | return NULL; |
| 12523 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12524 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12525 | } |
| 12526 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12527 | PyDoc_STRVAR(rindex__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12528 | "S.rindex(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12529 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12530 | 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] | 12531 | |
| 12532 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12533 | unicode_rindex(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12534 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12535 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 12536 | Py_ssize_t start; |
| 12537 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12538 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12539 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12540 | if (!stringlib_parse_args_finds_unicode("rindex", args, &substring, |
| 12541 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12542 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12543 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12544 | if (PyUnicode_READY(self) == -1) |
| 12545 | return NULL; |
| 12546 | if (PyUnicode_READY(substring) == -1) |
| 12547 | return NULL; |
| 12548 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12549 | result = any_find_slice(-1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12550 | |
| 12551 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12552 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12553 | if (result == -2) |
| 12554 | return NULL; |
| 12555 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12556 | if (result < 0) { |
| 12557 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 12558 | return NULL; |
| 12559 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12560 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12561 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12562 | } |
| 12563 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12564 | PyDoc_STRVAR(rjust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12565 | "S.rjust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12566 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 12567 | 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] | 12568 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12569 | |
| 12570 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12571 | unicode_rjust(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12572 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12573 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12574 | Py_UCS4 fillchar = ' '; |
| 12575 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 12576 | 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] | 12577 | return NULL; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 12578 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 12579 | if (PyUnicode_READY(self) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12580 | return NULL; |
| 12581 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12582 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 12583 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12584 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12585 | return pad(self, width - PyUnicode_GET_LENGTH(self), 0, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12586 | } |
| 12587 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12588 | PyObject * |
| 12589 | PyUnicode_Split(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12590 | { |
| 12591 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 12592 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12593 | s = PyUnicode_FromObject(s); |
| 12594 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12595 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12596 | if (sep != NULL) { |
| 12597 | sep = PyUnicode_FromObject(sep); |
| 12598 | if (sep == NULL) { |
| 12599 | Py_DECREF(s); |
| 12600 | return NULL; |
| 12601 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12602 | } |
| 12603 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12604 | result = split(s, sep, maxsplit); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12605 | |
| 12606 | Py_DECREF(s); |
| 12607 | Py_XDECREF(sep); |
| 12608 | return result; |
| 12609 | } |
| 12610 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12611 | PyDoc_STRVAR(split__doc__, |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12612 | "S.split(sep=None, maxsplit=-1) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12613 | \n\ |
| 12614 | Return a list of the words in S, using sep as the\n\ |
| 12615 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 12616 | 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] | 12617 | whitespace string is a separator and empty strings are\n\ |
| 12618 | removed from the result."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12619 | |
| 12620 | static PyObject* |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12621 | unicode_split(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12622 | { |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12623 | static char *kwlist[] = {"sep", "maxsplit", 0}; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12624 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12625 | Py_ssize_t maxcount = -1; |
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 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|On:split", |
| 12628 | kwlist, &substring, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12629 | return NULL; |
| 12630 | |
| 12631 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12632 | return split(self, NULL, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12633 | else if (PyUnicode_Check(substring)) |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12634 | return split(self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12635 | else |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12636 | return PyUnicode_Split(self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12637 | } |
| 12638 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12639 | PyObject * |
| 12640 | PyUnicode_Partition(PyObject *str_in, PyObject *sep_in) |
| 12641 | { |
| 12642 | PyObject* str_obj; |
| 12643 | PyObject* sep_obj; |
| 12644 | PyObject* out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12645 | int kind1, kind2, kind; |
| 12646 | void *buf1 = NULL, *buf2 = NULL; |
| 12647 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12648 | |
| 12649 | str_obj = PyUnicode_FromObject(str_in); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12650 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12651 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12652 | sep_obj = PyUnicode_FromObject(sep_in); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12653 | if (!sep_obj) { |
| 12654 | Py_DECREF(str_obj); |
| 12655 | return NULL; |
| 12656 | } |
| 12657 | if (PyUnicode_READY(sep_obj) == -1 || PyUnicode_READY(str_obj) == -1) { |
| 12658 | Py_DECREF(sep_obj); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12659 | Py_DECREF(str_obj); |
| 12660 | return NULL; |
| 12661 | } |
| 12662 | |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12663 | kind1 = PyUnicode_KIND(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12664 | kind2 = PyUnicode_KIND(sep_obj); |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12665 | kind = Py_MAX(kind1, kind2); |
| 12666 | buf1 = PyUnicode_DATA(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12667 | if (kind1 != kind) |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12668 | buf1 = _PyUnicode_AsKind(str_obj, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12669 | if (!buf1) |
| 12670 | goto onError; |
| 12671 | buf2 = PyUnicode_DATA(sep_obj); |
| 12672 | if (kind2 != kind) |
| 12673 | buf2 = _PyUnicode_AsKind(sep_obj, kind); |
| 12674 | if (!buf2) |
| 12675 | goto onError; |
| 12676 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 12677 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
| 12678 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 12679 | switch (PyUnicode_KIND(str_obj)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12680 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 12681 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sep_obj)) |
| 12682 | out = asciilib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12683 | else |
| 12684 | 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] | 12685 | break; |
| 12686 | case PyUnicode_2BYTE_KIND: |
| 12687 | out = ucs2lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12688 | break; |
| 12689 | case PyUnicode_4BYTE_KIND: |
| 12690 | out = ucs4lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12691 | break; |
| 12692 | default: |
| 12693 | assert(0); |
| 12694 | out = 0; |
| 12695 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12696 | |
| 12697 | Py_DECREF(sep_obj); |
| 12698 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12699 | if (kind1 != kind) |
| 12700 | PyMem_Free(buf1); |
| 12701 | if (kind2 != kind) |
| 12702 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12703 | |
| 12704 | return out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12705 | onError: |
| 12706 | Py_DECREF(sep_obj); |
| 12707 | Py_DECREF(str_obj); |
| 12708 | if (kind1 != kind && buf1) |
| 12709 | PyMem_Free(buf1); |
| 12710 | if (kind2 != kind && buf2) |
| 12711 | PyMem_Free(buf2); |
| 12712 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12713 | } |
| 12714 | |
| 12715 | |
| 12716 | PyObject * |
| 12717 | PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in) |
| 12718 | { |
| 12719 | PyObject* str_obj; |
| 12720 | PyObject* sep_obj; |
| 12721 | PyObject* out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12722 | int kind1, kind2, kind; |
| 12723 | void *buf1 = NULL, *buf2 = NULL; |
| 12724 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12725 | |
| 12726 | str_obj = PyUnicode_FromObject(str_in); |
| 12727 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12728 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12729 | sep_obj = PyUnicode_FromObject(sep_in); |
| 12730 | if (!sep_obj) { |
| 12731 | Py_DECREF(str_obj); |
| 12732 | return NULL; |
| 12733 | } |
| 12734 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12735 | kind1 = PyUnicode_KIND(str_in); |
| 12736 | kind2 = PyUnicode_KIND(sep_obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 12737 | kind = Py_MAX(kind1, kind2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12738 | buf1 = PyUnicode_DATA(str_in); |
| 12739 | if (kind1 != kind) |
| 12740 | buf1 = _PyUnicode_AsKind(str_in, kind); |
| 12741 | if (!buf1) |
| 12742 | goto onError; |
| 12743 | buf2 = PyUnicode_DATA(sep_obj); |
| 12744 | if (kind2 != kind) |
| 12745 | buf2 = _PyUnicode_AsKind(sep_obj, kind); |
| 12746 | if (!buf2) |
| 12747 | goto onError; |
| 12748 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 12749 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
| 12750 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 12751 | switch (PyUnicode_KIND(str_in)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12752 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 12753 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sep_obj)) |
| 12754 | out = asciilib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12755 | else |
| 12756 | 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] | 12757 | break; |
| 12758 | case PyUnicode_2BYTE_KIND: |
| 12759 | out = ucs2lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12760 | break; |
| 12761 | case PyUnicode_4BYTE_KIND: |
| 12762 | out = ucs4lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12763 | break; |
| 12764 | default: |
| 12765 | assert(0); |
| 12766 | out = 0; |
| 12767 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12768 | |
| 12769 | Py_DECREF(sep_obj); |
| 12770 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12771 | if (kind1 != kind) |
| 12772 | PyMem_Free(buf1); |
| 12773 | if (kind2 != kind) |
| 12774 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12775 | |
| 12776 | return out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12777 | onError: |
| 12778 | Py_DECREF(sep_obj); |
| 12779 | Py_DECREF(str_obj); |
| 12780 | if (kind1 != kind && buf1) |
| 12781 | PyMem_Free(buf1); |
| 12782 | if (kind2 != kind && buf2) |
| 12783 | PyMem_Free(buf2); |
| 12784 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12785 | } |
| 12786 | |
| 12787 | PyDoc_STRVAR(partition__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12788 | "S.partition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12789 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 12790 | 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] | 12791 | 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] | 12792 | found, return S and two empty strings."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12793 | |
| 12794 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12795 | unicode_partition(PyObject *self, PyObject *separator) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12796 | { |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12797 | return PyUnicode_Partition(self, separator); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12798 | } |
| 12799 | |
| 12800 | PyDoc_STRVAR(rpartition__doc__, |
Ezio Melotti | 5b2b242 | 2010-01-25 11:58:28 +0000 | [diff] [blame] | 12801 | "S.rpartition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12802 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 12803 | 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] | 12804 | 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] | 12805 | separator is not found, return two empty strings and S."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12806 | |
| 12807 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12808 | unicode_rpartition(PyObject *self, PyObject *separator) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12809 | { |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12810 | return PyUnicode_RPartition(self, separator); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12811 | } |
| 12812 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12813 | PyObject * |
| 12814 | PyUnicode_RSplit(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12815 | { |
| 12816 | PyObject *result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12817 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12818 | s = PyUnicode_FromObject(s); |
| 12819 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12820 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12821 | if (sep != NULL) { |
| 12822 | sep = PyUnicode_FromObject(sep); |
| 12823 | if (sep == NULL) { |
| 12824 | Py_DECREF(s); |
| 12825 | return NULL; |
| 12826 | } |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12827 | } |
| 12828 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12829 | result = rsplit(s, sep, maxsplit); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12830 | |
| 12831 | Py_DECREF(s); |
| 12832 | Py_XDECREF(sep); |
| 12833 | return result; |
| 12834 | } |
| 12835 | |
| 12836 | PyDoc_STRVAR(rsplit__doc__, |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12837 | "S.rsplit(sep=None, maxsplit=-1) -> list of strings\n\ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12838 | \n\ |
| 12839 | Return a list of the words in S, using sep as the\n\ |
| 12840 | delimiter string, starting at the end of the string and\n\ |
| 12841 | working to the front. If maxsplit is given, at most maxsplit\n\ |
| 12842 | splits are done. If sep is not specified, any whitespace string\n\ |
| 12843 | is a separator."); |
| 12844 | |
| 12845 | static PyObject* |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12846 | unicode_rsplit(PyObject *self, PyObject *args, PyObject *kwds) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12847 | { |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12848 | static char *kwlist[] = {"sep", "maxsplit", 0}; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12849 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12850 | Py_ssize_t maxcount = -1; |
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 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|On:rsplit", |
| 12853 | kwlist, &substring, &maxcount)) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12854 | return NULL; |
| 12855 | |
| 12856 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12857 | return rsplit(self, NULL, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12858 | else if (PyUnicode_Check(substring)) |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12859 | return rsplit(self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12860 | else |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12861 | return PyUnicode_RSplit(self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12862 | } |
| 12863 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12864 | PyDoc_STRVAR(splitlines__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12865 | "S.splitlines([keepends]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12866 | \n\ |
| 12867 | 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] | 12868 | 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] | 12869 | is given and true."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12870 | |
| 12871 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12872 | unicode_splitlines(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12873 | { |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12874 | static char *kwlist[] = {"keepends", 0}; |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 12875 | int keepends = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12876 | |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12877 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|i:splitlines", |
| 12878 | kwlist, &keepends)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12879 | return NULL; |
| 12880 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12881 | return PyUnicode_Splitlines(self, keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12882 | } |
| 12883 | |
| 12884 | static |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 12885 | PyObject *unicode_str(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12886 | { |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12887 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12888 | } |
| 12889 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12890 | PyDoc_STRVAR(swapcase__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12891 | "S.swapcase() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12892 | \n\ |
| 12893 | 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] | 12894 | and vice versa."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12895 | |
| 12896 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12897 | unicode_swapcase(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12898 | { |
Benjamin Peterson | eea4846 | 2012-01-16 14:28:50 -0500 | [diff] [blame] | 12899 | if (PyUnicode_READY(self) == -1) |
| 12900 | return NULL; |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 12901 | return case_operation(self, do_swapcase); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12902 | } |
| 12903 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12904 | PyDoc_STRVAR(maketrans__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12905 | "str.maketrans(x[, y[, z]]) -> dict (static method)\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12906 | \n\ |
| 12907 | Return a translation table usable for str.translate().\n\ |
| 12908 | If there is only one argument, it must be a dictionary mapping Unicode\n\ |
| 12909 | ordinals (integers) or characters to Unicode ordinals, strings or None.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12910 | Character keys will be then converted to ordinals.\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12911 | If there are two arguments, they must be strings of equal length, and\n\ |
| 12912 | in the resulting dictionary, each character in x will be mapped to the\n\ |
| 12913 | character at the same position in y. If there is a third argument, it\n\ |
| 12914 | must be a string, whose characters will be mapped to None in the result."); |
| 12915 | |
| 12916 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12917 | unicode_maketrans(PyObject *null, PyObject *args) |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12918 | { |
| 12919 | PyObject *x, *y = NULL, *z = NULL; |
| 12920 | PyObject *new = NULL, *key, *value; |
| 12921 | Py_ssize_t i = 0; |
| 12922 | int res; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12923 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12924 | if (!PyArg_ParseTuple(args, "O|UU:maketrans", &x, &y, &z)) |
| 12925 | return NULL; |
| 12926 | new = PyDict_New(); |
| 12927 | if (!new) |
| 12928 | return NULL; |
| 12929 | if (y != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12930 | int x_kind, y_kind, z_kind; |
| 12931 | void *x_data, *y_data, *z_data; |
| 12932 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12933 | /* x must be a string too, of equal length */ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12934 | if (!PyUnicode_Check(x)) { |
| 12935 | PyErr_SetString(PyExc_TypeError, "first maketrans argument must " |
| 12936 | "be a string if there is a second argument"); |
| 12937 | goto err; |
| 12938 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12939 | if (PyUnicode_GET_LENGTH(x) != PyUnicode_GET_LENGTH(y)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12940 | PyErr_SetString(PyExc_ValueError, "the first two maketrans " |
| 12941 | "arguments must have equal length"); |
| 12942 | goto err; |
| 12943 | } |
| 12944 | /* 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] | 12945 | x_kind = PyUnicode_KIND(x); |
| 12946 | y_kind = PyUnicode_KIND(y); |
| 12947 | x_data = PyUnicode_DATA(x); |
| 12948 | y_data = PyUnicode_DATA(y); |
| 12949 | for (i = 0; i < PyUnicode_GET_LENGTH(x); i++) { |
| 12950 | key = PyLong_FromLong(PyUnicode_READ(x_kind, x_data, i)); |
Benjamin Peterson | 53aa1d7 | 2011-12-20 13:29:45 -0600 | [diff] [blame] | 12951 | if (!key) |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12952 | goto err; |
Benjamin Peterson | 822c790 | 2011-12-20 13:32:50 -0600 | [diff] [blame] | 12953 | value = PyLong_FromLong(PyUnicode_READ(y_kind, y_data, i)); |
Benjamin Peterson | 53aa1d7 | 2011-12-20 13:29:45 -0600 | [diff] [blame] | 12954 | if (!value) { |
| 12955 | Py_DECREF(key); |
| 12956 | goto err; |
| 12957 | } |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12958 | res = PyDict_SetItem(new, key, value); |
| 12959 | Py_DECREF(key); |
| 12960 | Py_DECREF(value); |
| 12961 | if (res < 0) |
| 12962 | goto err; |
| 12963 | } |
| 12964 | /* create entries for deleting chars in z */ |
| 12965 | if (z != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12966 | z_kind = PyUnicode_KIND(z); |
| 12967 | z_data = PyUnicode_DATA(z); |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 12968 | for (i = 0; i < PyUnicode_GET_LENGTH(z); i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12969 | key = PyLong_FromLong(PyUnicode_READ(z_kind, z_data, i)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12970 | if (!key) |
| 12971 | goto err; |
| 12972 | res = PyDict_SetItem(new, key, Py_None); |
| 12973 | Py_DECREF(key); |
| 12974 | if (res < 0) |
| 12975 | goto err; |
| 12976 | } |
| 12977 | } |
| 12978 | } else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12979 | int kind; |
| 12980 | void *data; |
| 12981 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12982 | /* x must be a dict */ |
Raymond Hettinger | 3ad0576 | 2009-05-29 22:11:22 +0000 | [diff] [blame] | 12983 | if (!PyDict_CheckExact(x)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12984 | PyErr_SetString(PyExc_TypeError, "if you give only one argument " |
| 12985 | "to maketrans it must be a dict"); |
| 12986 | goto err; |
| 12987 | } |
| 12988 | /* copy entries into the new dict, converting string keys to int keys */ |
| 12989 | while (PyDict_Next(x, &i, &key, &value)) { |
| 12990 | if (PyUnicode_Check(key)) { |
| 12991 | /* convert string keys to integer keys */ |
| 12992 | PyObject *newkey; |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 12993 | if (PyUnicode_GET_LENGTH(key) != 1) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12994 | PyErr_SetString(PyExc_ValueError, "string keys in translate " |
| 12995 | "table must be of length 1"); |
| 12996 | goto err; |
| 12997 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12998 | kind = PyUnicode_KIND(key); |
| 12999 | data = PyUnicode_DATA(key); |
| 13000 | newkey = PyLong_FromLong(PyUnicode_READ(kind, data, 0)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 13001 | if (!newkey) |
| 13002 | goto err; |
| 13003 | res = PyDict_SetItem(new, newkey, value); |
| 13004 | Py_DECREF(newkey); |
| 13005 | if (res < 0) |
| 13006 | goto err; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 13007 | } else if (PyLong_Check(key)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 13008 | /* just keep integer keys */ |
| 13009 | if (PyDict_SetItem(new, key, value) < 0) |
| 13010 | goto err; |
| 13011 | } else { |
| 13012 | PyErr_SetString(PyExc_TypeError, "keys in translate table must " |
| 13013 | "be strings or integers"); |
| 13014 | goto err; |
| 13015 | } |
| 13016 | } |
| 13017 | } |
| 13018 | return new; |
| 13019 | err: |
| 13020 | Py_DECREF(new); |
| 13021 | return NULL; |
| 13022 | } |
| 13023 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 13024 | PyDoc_STRVAR(translate__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13025 | "S.translate(table) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13026 | \n\ |
| 13027 | Return a copy of the string S, where all characters have been mapped\n\ |
| 13028 | through the given translation table, which must be a mapping of\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 13029 | Unicode ordinals to Unicode ordinals, strings, or None.\n\ |
Walter Dörwald | 5c1ee17 | 2002-09-04 20:31:32 +0000 | [diff] [blame] | 13030 | Unmapped characters are left untouched. Characters mapped to None\n\ |
| 13031 | are deleted."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13032 | |
| 13033 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13034 | unicode_translate(PyObject *self, PyObject *table) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13035 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13036 | return _PyUnicode_TranslateCharmap(self, table, "ignore"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13037 | } |
| 13038 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 13039 | PyDoc_STRVAR(upper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13040 | "S.upper() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13041 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 13042 | Return a copy of S converted to uppercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13043 | |
| 13044 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 13045 | unicode_upper(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13046 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 13047 | if (PyUnicode_READY(self) == -1) |
| 13048 | return NULL; |
| 13049 | if (PyUnicode_IS_ASCII(self)) |
| 13050 | return ascii_upper_or_lower(self, 0); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 13051 | return case_operation(self, do_upper); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13052 | } |
| 13053 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 13054 | PyDoc_STRVAR(zfill__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13055 | "S.zfill(width) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13056 | \n\ |
Benjamin Peterson | 9aa4299 | 2008-09-10 21:57:34 +0000 | [diff] [blame] | 13057 | Pad a numeric string S with zeros on the left, to fill a field\n\ |
| 13058 | of the specified width. The string S is never truncated."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13059 | |
| 13060 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 13061 | unicode_zfill(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13062 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13063 | Py_ssize_t fill; |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 13064 | PyObject *u; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13065 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13066 | int kind; |
| 13067 | void *data; |
| 13068 | Py_UCS4 chr; |
| 13069 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13070 | if (!PyArg_ParseTuple(args, "n:zfill", &width)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13071 | return NULL; |
| 13072 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 13073 | if (PyUnicode_READY(self) == -1) |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 13074 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13075 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 13076 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 13077 | return unicode_result_unchanged(self); |
| 13078 | |
| 13079 | fill = width - PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13080 | |
| 13081 | u = pad(self, fill, 0, '0'); |
| 13082 | |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 13083 | if (u == NULL) |
| 13084 | return NULL; |
| 13085 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13086 | kind = PyUnicode_KIND(u); |
| 13087 | data = PyUnicode_DATA(u); |
| 13088 | chr = PyUnicode_READ(kind, data, fill); |
| 13089 | |
| 13090 | if (chr == '+' || chr == '-') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13091 | /* move sign to beginning of string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13092 | PyUnicode_WRITE(kind, data, 0, chr); |
| 13093 | PyUnicode_WRITE(kind, data, fill, '0'); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13094 | } |
| 13095 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13096 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 13097 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13098 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13099 | |
| 13100 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 13101 | static PyObject * |
| 13102 | unicode__decimal2ascii(PyObject *self) |
| 13103 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13104 | return PyUnicode_TransformDecimalAndSpaceToASCII(self); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 13105 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13106 | #endif |
| 13107 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 13108 | PyDoc_STRVAR(startswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13109 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13110 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 13111 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 13112 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13113 | With optional end, stop comparing S at that position.\n\ |
| 13114 | prefix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13115 | |
| 13116 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13117 | unicode_startswith(PyObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13118 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13119 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13120 | PyObject *subobj; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13121 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13122 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 13123 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13124 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13125 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 13126 | if (!stringlib_parse_args_finds("startswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13127 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13128 | if (PyTuple_Check(subobj)) { |
| 13129 | Py_ssize_t i; |
| 13130 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13131 | substring = PyUnicode_FromObject(PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13132 | if (substring == NULL) |
| 13133 | return NULL; |
| 13134 | result = tailmatch(self, substring, start, end, -1); |
| 13135 | Py_DECREF(substring); |
| 13136 | if (result) { |
| 13137 | Py_RETURN_TRUE; |
| 13138 | } |
| 13139 | } |
| 13140 | /* nothing matched */ |
| 13141 | Py_RETURN_FALSE; |
| 13142 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13143 | substring = PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 13144 | if (substring == NULL) { |
| 13145 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 13146 | PyErr_Format(PyExc_TypeError, "startswith first arg must be str or " |
| 13147 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13148 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 13149 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13150 | result = tailmatch(self, substring, start, end, -1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13151 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13152 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13153 | } |
| 13154 | |
| 13155 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 13156 | PyDoc_STRVAR(endswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13157 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13158 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 13159 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 13160 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13161 | With optional end, stop comparing S at that position.\n\ |
| 13162 | suffix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13163 | |
| 13164 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13165 | unicode_endswith(PyObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13166 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13167 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13168 | PyObject *subobj; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13169 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13170 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 13171 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13172 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13173 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 13174 | if (!stringlib_parse_args_finds("endswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13175 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13176 | if (PyTuple_Check(subobj)) { |
| 13177 | Py_ssize_t i; |
| 13178 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13179 | substring = PyUnicode_FromObject( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13180 | PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13181 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13182 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13183 | result = tailmatch(self, substring, start, end, +1); |
| 13184 | Py_DECREF(substring); |
| 13185 | if (result) { |
| 13186 | Py_RETURN_TRUE; |
| 13187 | } |
| 13188 | } |
| 13189 | Py_RETURN_FALSE; |
| 13190 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13191 | substring = PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 13192 | if (substring == NULL) { |
| 13193 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 13194 | PyErr_Format(PyExc_TypeError, "endswith first arg must be str or " |
| 13195 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13196 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 13197 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13198 | result = tailmatch(self, substring, start, end, +1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13199 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13200 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13201 | } |
| 13202 | |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13203 | typedef struct { |
| 13204 | PyObject *buffer; |
| 13205 | void *data; |
| 13206 | enum PyUnicode_Kind kind; |
| 13207 | Py_UCS4 maxchar; |
| 13208 | Py_ssize_t pos; |
| 13209 | } unicode_writer_t; |
| 13210 | |
| 13211 | Py_LOCAL_INLINE(void) |
| 13212 | unicode_writer_update(unicode_writer_t *writer) |
| 13213 | { |
| 13214 | writer->maxchar = PyUnicode_MAX_CHAR_VALUE(writer->buffer); |
| 13215 | writer->data = PyUnicode_DATA(writer->buffer); |
| 13216 | writer->kind = PyUnicode_KIND(writer->buffer); |
| 13217 | } |
| 13218 | |
| 13219 | Py_LOCAL(int) |
| 13220 | unicode_writer_init(unicode_writer_t *writer, |
| 13221 | Py_ssize_t length, Py_UCS4 maxchar) |
| 13222 | { |
| 13223 | writer->pos = 0; |
| 13224 | writer->buffer = PyUnicode_New(length, maxchar); |
| 13225 | if (writer->buffer == NULL) |
| 13226 | return -1; |
| 13227 | unicode_writer_update(writer); |
| 13228 | return 0; |
| 13229 | } |
| 13230 | |
| 13231 | Py_LOCAL_INLINE(int) |
| 13232 | unicode_writer_prepare(unicode_writer_t *writer, |
| 13233 | Py_ssize_t length, Py_UCS4 maxchar) |
| 13234 | { |
| 13235 | Py_ssize_t newlen; |
| 13236 | PyObject *newbuffer; |
| 13237 | |
| 13238 | if (length > PY_SSIZE_T_MAX - writer->pos) { |
| 13239 | PyErr_NoMemory(); |
| 13240 | return -1; |
| 13241 | } |
| 13242 | newlen = writer->pos + length; |
| 13243 | |
| 13244 | if (newlen > PyUnicode_GET_LENGTH(writer->buffer)) { |
Victor Stinner | 0576f9b | 2012-05-07 13:02:44 +0200 | [diff] [blame] | 13245 | /* Overallocate 25% to limit the number of resize. |
| 13246 | Check for integer overflow: |
| 13247 | (newlen + newlen / 4) <= PY_SSIZE_T_MAX */ |
| 13248 | if (newlen <= (PY_SSIZE_T_MAX - PY_SSIZE_T_MAX / 5)) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13249 | newlen += newlen / 4; |
| 13250 | |
| 13251 | if (maxchar > writer->maxchar) { |
| 13252 | /* resize + widen */ |
| 13253 | newbuffer = PyUnicode_New(newlen, maxchar); |
| 13254 | if (newbuffer == NULL) |
| 13255 | return -1; |
| 13256 | PyUnicode_CopyCharacters(newbuffer, 0, |
| 13257 | writer->buffer, 0, writer->pos); |
| 13258 | Py_DECREF(writer->buffer); |
| 13259 | } |
| 13260 | else { |
| 13261 | newbuffer = resize_compact(writer->buffer, newlen); |
| 13262 | if (newbuffer == NULL) |
| 13263 | return -1; |
| 13264 | } |
| 13265 | writer->buffer = newbuffer; |
| 13266 | unicode_writer_update(writer); |
| 13267 | } |
| 13268 | else if (maxchar > writer->maxchar) { |
| 13269 | if (unicode_widen(&writer->buffer, writer->pos, maxchar) < 0) |
| 13270 | return -1; |
| 13271 | unicode_writer_update(writer); |
| 13272 | } |
| 13273 | return 0; |
| 13274 | } |
| 13275 | |
| 13276 | Py_LOCAL_INLINE(int) |
| 13277 | unicode_writer_write_str( |
| 13278 | unicode_writer_t *writer, |
| 13279 | PyObject *str, Py_ssize_t start, Py_ssize_t length) |
| 13280 | { |
| 13281 | Py_UCS4 maxchar; |
| 13282 | |
| 13283 | assert(str != NULL); |
| 13284 | assert(PyUnicode_Check(str)); |
| 13285 | if (PyUnicode_READY(str) == -1) |
| 13286 | return -1; |
| 13287 | |
| 13288 | assert(0 <= start); |
| 13289 | assert(0 <= length); |
| 13290 | assert(start + length <= PyUnicode_GET_LENGTH(str)); |
| 13291 | if (length == 0) |
| 13292 | return 0; |
| 13293 | |
| 13294 | maxchar = _PyUnicode_FindMaxChar(str, start, start + length); |
| 13295 | if (unicode_writer_prepare(writer, length, maxchar) == -1) |
| 13296 | return -1; |
| 13297 | |
| 13298 | assert((writer->pos + length) <= PyUnicode_GET_LENGTH(writer->buffer)); |
| 13299 | copy_characters(writer->buffer, writer->pos, |
| 13300 | str, start, length); |
| 13301 | writer->pos += length; |
| 13302 | return 0; |
| 13303 | } |
| 13304 | |
| 13305 | Py_LOCAL_INLINE(int) |
| 13306 | unicode_writer_write_char( |
| 13307 | unicode_writer_t *writer, |
| 13308 | Py_UCS4 ch) |
| 13309 | { |
| 13310 | if (unicode_writer_prepare(writer, 1, ch) == -1) |
| 13311 | return -1; |
| 13312 | assert((writer->pos + 1) <= PyUnicode_GET_LENGTH(writer->buffer)); |
| 13313 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos, ch); |
| 13314 | writer->pos += 1; |
| 13315 | return 0; |
| 13316 | } |
| 13317 | |
| 13318 | Py_LOCAL(PyObject *) |
| 13319 | unicode_writer_finish(unicode_writer_t *writer) |
| 13320 | { |
| 13321 | if (PyUnicode_Resize(&writer->buffer, writer->pos) < 0) { |
| 13322 | Py_DECREF(writer->buffer); |
| 13323 | return NULL; |
| 13324 | } |
| 13325 | return writer->buffer; |
| 13326 | } |
| 13327 | |
| 13328 | Py_LOCAL(void) |
| 13329 | unicode_writer_dealloc(unicode_writer_t *writer) |
| 13330 | { |
| 13331 | Py_CLEAR(writer->buffer); |
| 13332 | } |
| 13333 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13334 | #include "stringlib/unicode_format.h" |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13335 | |
| 13336 | PyDoc_STRVAR(format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13337 | "S.format(*args, **kwargs) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13338 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 13339 | Return a formatted version of S, using substitutions from args and kwargs.\n\ |
| 13340 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13341 | |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 13342 | PyDoc_STRVAR(format_map__doc__, |
| 13343 | "S.format_map(mapping) -> str\n\ |
| 13344 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 13345 | Return a formatted version of S, using substitutions from mapping.\n\ |
| 13346 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 13347 | |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 13348 | static PyObject * |
| 13349 | unicode__format__(PyObject* self, PyObject* args) |
| 13350 | { |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13351 | PyObject *format_spec, *out; |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 13352 | |
| 13353 | if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) |
| 13354 | return NULL; |
| 13355 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13356 | out = _PyUnicode_FormatAdvanced(self, format_spec, 0, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13357 | PyUnicode_GET_LENGTH(format_spec)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13358 | return out; |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 13359 | } |
| 13360 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13361 | PyDoc_STRVAR(p_format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13362 | "S.__format__(format_spec) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13363 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 13364 | Return a formatted version of S as described by format_spec."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13365 | |
| 13366 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13367 | unicode__sizeof__(PyObject *v) |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13368 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13369 | Py_ssize_t size; |
| 13370 | |
| 13371 | /* If it's a compact object, account for base structure + |
| 13372 | character data. */ |
| 13373 | if (PyUnicode_IS_COMPACT_ASCII(v)) |
| 13374 | size = sizeof(PyASCIIObject) + PyUnicode_GET_LENGTH(v) + 1; |
| 13375 | else if (PyUnicode_IS_COMPACT(v)) |
| 13376 | size = sizeof(PyCompactUnicodeObject) + |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 13377 | (PyUnicode_GET_LENGTH(v) + 1) * PyUnicode_KIND(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13378 | else { |
| 13379 | /* If it is a two-block object, account for base object, and |
| 13380 | for character block if present. */ |
| 13381 | size = sizeof(PyUnicodeObject); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 13382 | if (_PyUnicode_DATA_ANY(v)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13383 | size += (PyUnicode_GET_LENGTH(v) + 1) * |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 13384 | PyUnicode_KIND(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13385 | } |
| 13386 | /* 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] | 13387 | with the data pointer. Check if the data is not shared. */ |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 13388 | if (_PyUnicode_HAS_WSTR_MEMORY(v)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13389 | size += (PyUnicode_WSTR_LENGTH(v) + 1) * sizeof(wchar_t); |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 13390 | if (_PyUnicode_HAS_UTF8_MEMORY(v)) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 13391 | size += PyUnicode_UTF8_LENGTH(v) + 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13392 | |
| 13393 | return PyLong_FromSsize_t(size); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13394 | } |
| 13395 | |
| 13396 | PyDoc_STRVAR(sizeof__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13397 | "S.__sizeof__() -> size of S in memory, in bytes"); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13398 | |
| 13399 | static PyObject * |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 13400 | unicode_getnewargs(PyObject *v) |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 13401 | { |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 13402 | PyObject *copy = _PyUnicode_Copy(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13403 | if (!copy) |
| 13404 | return NULL; |
| 13405 | return Py_BuildValue("(N)", copy); |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 13406 | } |
| 13407 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13408 | static PyMethodDef unicode_methods[] = { |
Benjamin Peterson | 28a4dce | 2010-12-12 01:33:04 +0000 | [diff] [blame] | 13409 | {"encode", (PyCFunction) unicode_encode, METH_VARARGS | METH_KEYWORDS, encode__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13410 | {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 13411 | {"split", (PyCFunction) unicode_split, METH_VARARGS | METH_KEYWORDS, split__doc__}, |
| 13412 | {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS | METH_KEYWORDS, rsplit__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13413 | {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, |
| 13414 | {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 13415 | {"casefold", (PyCFunction) unicode_casefold, METH_NOARGS, casefold__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13416 | {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, |
| 13417 | {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, |
| 13418 | {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, |
| 13419 | {"expandtabs", (PyCFunction) unicode_expandtabs, METH_VARARGS, expandtabs__doc__}, |
| 13420 | {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13421 | {"partition", (PyCFunction) unicode_partition, METH_O, partition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13422 | {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, |
| 13423 | {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, |
| 13424 | {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 13425 | {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13426 | {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, |
| 13427 | {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, |
| 13428 | {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 13429 | {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13430 | {"rpartition", (PyCFunction) unicode_rpartition, METH_O, rpartition__doc__}, |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 13431 | {"splitlines", (PyCFunction) unicode_splitlines, METH_VARARGS | METH_KEYWORDS, splitlines__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 13432 | {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13433 | {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, |
| 13434 | {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, |
| 13435 | {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, |
| 13436 | {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, |
| 13437 | {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, |
| 13438 | {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, |
| 13439 | {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, |
| 13440 | {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, |
| 13441 | {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, |
| 13442 | {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, |
| 13443 | {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, |
| 13444 | {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, |
| 13445 | {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, |
| 13446 | {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 13447 | {"isidentifier", (PyCFunction) unicode_isidentifier, METH_NOARGS, isidentifier__doc__}, |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 13448 | {"isprintable", (PyCFunction) unicode_isprintable, METH_NOARGS, isprintable__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13449 | {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, |
Eric Smith | 9cd1e09 | 2007-08-31 18:39:38 +0000 | [diff] [blame] | 13450 | {"format", (PyCFunction) do_string_format, METH_VARARGS | METH_KEYWORDS, format__doc__}, |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 13451 | {"format_map", (PyCFunction) do_string_format_map, METH_O, format_map__doc__}, |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 13452 | {"__format__", (PyCFunction) unicode__format__, METH_VARARGS, p_format__doc__}, |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 13453 | {"maketrans", (PyCFunction) unicode_maketrans, |
| 13454 | METH_VARARGS | METH_STATIC, maketrans__doc__}, |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13455 | {"__sizeof__", (PyCFunction) unicode__sizeof__, METH_NOARGS, sizeof__doc__}, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 13456 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 13457 | /* These methods are just used for debugging the implementation. */ |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 13458 | {"_decimal2ascii", (PyCFunction) unicode__decimal2ascii, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13459 | #endif |
| 13460 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13461 | {"__getnewargs__", (PyCFunction)unicode_getnewargs, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13462 | {NULL, NULL} |
| 13463 | }; |
| 13464 | |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 13465 | static PyObject * |
| 13466 | unicode_mod(PyObject *v, PyObject *w) |
| 13467 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 13468 | if (!PyUnicode_Check(v)) |
| 13469 | Py_RETURN_NOTIMPLEMENTED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13470 | return PyUnicode_Format(v, w); |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 13471 | } |
| 13472 | |
| 13473 | static PyNumberMethods unicode_as_number = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13474 | 0, /*nb_add*/ |
| 13475 | 0, /*nb_subtract*/ |
| 13476 | 0, /*nb_multiply*/ |
| 13477 | unicode_mod, /*nb_remainder*/ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 13478 | }; |
| 13479 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13480 | static PySequenceMethods unicode_as_sequence = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13481 | (lenfunc) unicode_length, /* sq_length */ |
| 13482 | PyUnicode_Concat, /* sq_concat */ |
| 13483 | (ssizeargfunc) unicode_repeat, /* sq_repeat */ |
| 13484 | (ssizeargfunc) unicode_getitem, /* sq_item */ |
| 13485 | 0, /* sq_slice */ |
| 13486 | 0, /* sq_ass_item */ |
| 13487 | 0, /* sq_ass_slice */ |
| 13488 | PyUnicode_Contains, /* sq_contains */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13489 | }; |
| 13490 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13491 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13492 | unicode_subscript(PyObject* self, PyObject* item) |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13493 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13494 | if (PyUnicode_READY(self) == -1) |
| 13495 | return NULL; |
| 13496 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 13497 | if (PyIndex_Check(item)) { |
| 13498 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13499 | if (i == -1 && PyErr_Occurred()) |
| 13500 | return NULL; |
| 13501 | if (i < 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13502 | i += PyUnicode_GET_LENGTH(self); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13503 | return unicode_getitem(self, i); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13504 | } else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13505 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13506 | PyObject *result; |
| 13507 | void *src_data, *dest_data; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13508 | int src_kind, dest_kind; |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 13509 | Py_UCS4 ch, max_char, kind_limit; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13510 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13511 | if (PySlice_GetIndicesEx(item, PyUnicode_GET_LENGTH(self), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13512 | &start, &stop, &step, &slicelength) < 0) { |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13513 | return NULL; |
| 13514 | } |
| 13515 | |
| 13516 | if (slicelength <= 0) { |
Victor Stinner | 382955f | 2011-12-11 21:44:00 +0100 | [diff] [blame] | 13517 | Py_INCREF(unicode_empty); |
| 13518 | return unicode_empty; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13519 | } else if (start == 0 && step == 1 && |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 13520 | slicelength == PyUnicode_GET_LENGTH(self)) { |
| 13521 | return unicode_result_unchanged(self); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 13522 | } else if (step == 1) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13523 | return PyUnicode_Substring(self, |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 13524 | start, start + slicelength); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13525 | } |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13526 | /* General case */ |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13527 | src_kind = PyUnicode_KIND(self); |
| 13528 | src_data = PyUnicode_DATA(self); |
Victor Stinner | 55c9911 | 2011-10-13 01:17:06 +0200 | [diff] [blame] | 13529 | if (!PyUnicode_IS_ASCII(self)) { |
| 13530 | kind_limit = kind_maxchar_limit(src_kind); |
| 13531 | max_char = 0; |
| 13532 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
| 13533 | ch = PyUnicode_READ(src_kind, src_data, cur); |
| 13534 | if (ch > max_char) { |
| 13535 | max_char = ch; |
| 13536 | if (max_char >= kind_limit) |
| 13537 | break; |
| 13538 | } |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 13539 | } |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13540 | } |
Victor Stinner | 55c9911 | 2011-10-13 01:17:06 +0200 | [diff] [blame] | 13541 | else |
| 13542 | max_char = 127; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13543 | result = PyUnicode_New(slicelength, max_char); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13544 | if (result == NULL) |
| 13545 | return NULL; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13546 | dest_kind = PyUnicode_KIND(result); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13547 | dest_data = PyUnicode_DATA(result); |
| 13548 | |
| 13549 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13550 | Py_UCS4 ch = PyUnicode_READ(src_kind, src_data, cur); |
| 13551 | PyUnicode_WRITE(dest_kind, dest_data, i, ch); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13552 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13553 | assert(_PyUnicode_CheckConsistency(result, 1)); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13554 | return result; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13555 | } else { |
| 13556 | PyErr_SetString(PyExc_TypeError, "string indices must be integers"); |
| 13557 | return NULL; |
| 13558 | } |
| 13559 | } |
| 13560 | |
| 13561 | static PyMappingMethods unicode_as_mapping = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13562 | (lenfunc)unicode_length, /* mp_length */ |
| 13563 | (binaryfunc)unicode_subscript, /* mp_subscript */ |
| 13564 | (objobjargproc)0, /* mp_ass_subscript */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13565 | }; |
| 13566 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13567 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13568 | /* Helpers for PyUnicode_Format() */ |
| 13569 | |
| 13570 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13571 | 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] | 13572 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13573 | Py_ssize_t argidx = *p_argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13574 | if (argidx < arglen) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13575 | (*p_argidx)++; |
| 13576 | if (arglen < 0) |
| 13577 | return args; |
| 13578 | else |
| 13579 | return PyTuple_GetItem(args, argidx); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13580 | } |
| 13581 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13582 | "not enough arguments for format string"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13583 | return NULL; |
| 13584 | } |
| 13585 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13586 | /* 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] | 13587 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13588 | static PyObject * |
| 13589 | formatfloat(PyObject *v, int flags, int prec, int type) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13590 | { |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13591 | char *p; |
| 13592 | PyObject *result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13593 | double x; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 13594 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13595 | x = PyFloat_AsDouble(v); |
| 13596 | if (x == -1.0 && PyErr_Occurred()) |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13597 | return NULL; |
| 13598 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13599 | if (prec < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13600 | prec = 6; |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 13601 | |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 13602 | p = PyOS_double_to_string(x, type, prec, |
| 13603 | (flags & F_ALT) ? Py_DTSF_ALT : 0, NULL); |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13604 | if (p == NULL) |
| 13605 | return NULL; |
Victor Stinner | b5c3ea3 | 2012-05-02 00:29:36 +0200 | [diff] [blame] | 13606 | result = unicode_fromascii((unsigned char*)p, strlen(p)); |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 13607 | PyMem_Free(p); |
| 13608 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13609 | } |
| 13610 | |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13611 | /* formatlong() emulates the format codes d, u, o, x and X, and |
| 13612 | * the F_ALT flag, for Python's long (unbounded) ints. It's not used for |
| 13613 | * Python's regular ints. |
| 13614 | * Return value: a new PyUnicodeObject*, or NULL if error. |
| 13615 | * The output string is of the form |
| 13616 | * "-"? ("0x" | "0X")? digit+ |
| 13617 | * "0x"/"0X" are present only for x and X conversions, with F_ALT |
| 13618 | * set in flags. The case of hex digits will be correct, |
| 13619 | * There will be at least prec digits, zero-filled on the left if |
| 13620 | * necessary to get that many. |
| 13621 | * val object to be converted |
| 13622 | * flags bitmask of format flags; only F_ALT is looked at |
| 13623 | * prec minimum number of digits; 0-fill on left if needed |
| 13624 | * type a character in [duoxX]; u acts the same as d |
| 13625 | * |
| 13626 | * CAUTION: o, x and X conversions on regular ints can never |
| 13627 | * produce a '-' sign, but can for Python's unbounded ints. |
| 13628 | */ |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13629 | static PyObject* |
| 13630 | formatlong(PyObject *val, int flags, int prec, int type) |
| 13631 | { |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13632 | PyObject *result = NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13633 | char *buf; |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13634 | Py_ssize_t i; |
| 13635 | int sign; /* 1 if '-', else 0 */ |
| 13636 | int len; /* number of characters */ |
| 13637 | Py_ssize_t llen; |
| 13638 | int numdigits; /* len == numnondigits + numdigits */ |
| 13639 | int numnondigits = 0; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13640 | |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13641 | /* Avoid exceeding SSIZE_T_MAX */ |
| 13642 | if (prec > INT_MAX-3) { |
| 13643 | PyErr_SetString(PyExc_OverflowError, |
| 13644 | "precision too large"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13645 | return NULL; |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13646 | } |
| 13647 | |
| 13648 | assert(PyLong_Check(val)); |
| 13649 | |
| 13650 | switch (type) { |
| 13651 | case 'd': |
| 13652 | case 'u': |
| 13653 | /* Special-case boolean: we want 0/1 */ |
Victor Stinner | b11d91d | 2012-04-28 00:25:34 +0200 | [diff] [blame] | 13654 | if (PyBool_Check(val)) |
| 13655 | result = PyNumber_ToBase(val, 10); |
| 13656 | else |
| 13657 | result = Py_TYPE(val)->tp_str(val); |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13658 | break; |
| 13659 | case 'o': |
| 13660 | numnondigits = 2; |
| 13661 | result = PyNumber_ToBase(val, 8); |
| 13662 | break; |
| 13663 | case 'x': |
| 13664 | case 'X': |
| 13665 | numnondigits = 2; |
| 13666 | result = PyNumber_ToBase(val, 16); |
| 13667 | break; |
| 13668 | default: |
| 13669 | assert(!"'type' not in [duoxX]"); |
| 13670 | } |
| 13671 | if (!result) |
| 13672 | return NULL; |
| 13673 | |
| 13674 | assert(unicode_modifiable(result)); |
| 13675 | assert(PyUnicode_IS_READY(result)); |
| 13676 | assert(PyUnicode_IS_ASCII(result)); |
| 13677 | |
| 13678 | /* To modify the string in-place, there can only be one reference. */ |
| 13679 | if (Py_REFCNT(result) != 1) { |
| 13680 | PyErr_BadInternalCall(); |
| 13681 | return NULL; |
| 13682 | } |
| 13683 | buf = PyUnicode_DATA(result); |
| 13684 | llen = PyUnicode_GET_LENGTH(result); |
| 13685 | if (llen > INT_MAX) { |
| 13686 | PyErr_SetString(PyExc_ValueError, |
| 13687 | "string too large in _PyBytes_FormatLong"); |
| 13688 | return NULL; |
| 13689 | } |
| 13690 | len = (int)llen; |
| 13691 | sign = buf[0] == '-'; |
| 13692 | numnondigits += sign; |
| 13693 | numdigits = len - numnondigits; |
| 13694 | assert(numdigits > 0); |
| 13695 | |
| 13696 | /* Get rid of base marker unless F_ALT */ |
| 13697 | if (((flags & F_ALT) == 0 && |
| 13698 | (type == 'o' || type == 'x' || type == 'X'))) { |
| 13699 | assert(buf[sign] == '0'); |
| 13700 | assert(buf[sign+1] == 'x' || buf[sign+1] == 'X' || |
| 13701 | buf[sign+1] == 'o'); |
| 13702 | numnondigits -= 2; |
| 13703 | buf += 2; |
| 13704 | len -= 2; |
| 13705 | if (sign) |
| 13706 | buf[0] = '-'; |
| 13707 | assert(len == numnondigits + numdigits); |
| 13708 | assert(numdigits > 0); |
| 13709 | } |
| 13710 | |
| 13711 | /* Fill with leading zeroes to meet minimum width. */ |
| 13712 | if (prec > numdigits) { |
| 13713 | PyObject *r1 = PyBytes_FromStringAndSize(NULL, |
| 13714 | numnondigits + prec); |
| 13715 | char *b1; |
| 13716 | if (!r1) { |
| 13717 | Py_DECREF(result); |
| 13718 | return NULL; |
| 13719 | } |
| 13720 | b1 = PyBytes_AS_STRING(r1); |
| 13721 | for (i = 0; i < numnondigits; ++i) |
| 13722 | *b1++ = *buf++; |
| 13723 | for (i = 0; i < prec - numdigits; i++) |
| 13724 | *b1++ = '0'; |
| 13725 | for (i = 0; i < numdigits; i++) |
| 13726 | *b1++ = *buf++; |
| 13727 | *b1 = '\0'; |
| 13728 | Py_DECREF(result); |
| 13729 | result = r1; |
| 13730 | buf = PyBytes_AS_STRING(result); |
| 13731 | len = numnondigits + prec; |
| 13732 | } |
| 13733 | |
| 13734 | /* Fix up case for hex conversions. */ |
| 13735 | if (type == 'X') { |
| 13736 | /* Need to convert all lower case letters to upper case. |
| 13737 | and need to convert 0x to 0X (and -0x to -0X). */ |
| 13738 | for (i = 0; i < len; i++) |
| 13739 | if (buf[i] >= 'a' && buf[i] <= 'x') |
| 13740 | buf[i] -= 'a'-'A'; |
| 13741 | } |
| 13742 | if (!PyUnicode_Check(result) || len != PyUnicode_GET_LENGTH(result)) { |
| 13743 | PyObject *unicode; |
| 13744 | unicode = unicode_fromascii((unsigned char *)buf, len); |
| 13745 | Py_DECREF(result); |
| 13746 | result = unicode; |
| 13747 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13748 | return result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13749 | } |
| 13750 | |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13751 | static Py_UCS4 |
| 13752 | formatchar(PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13753 | { |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 13754 | /* presume that the buffer is at least 3 characters long */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 13755 | if (PyUnicode_Check(v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13756 | if (PyUnicode_GET_LENGTH(v) == 1) { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13757 | return PyUnicode_READ_CHAR(v, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13758 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13759 | goto onError; |
| 13760 | } |
| 13761 | else { |
| 13762 | /* Integer input truncated to a character */ |
| 13763 | long x; |
| 13764 | x = PyLong_AsLong(v); |
| 13765 | if (x == -1 && PyErr_Occurred()) |
| 13766 | goto onError; |
| 13767 | |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 13768 | if (x < 0 || x > MAX_UNICODE) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13769 | PyErr_SetString(PyExc_OverflowError, |
| 13770 | "%c arg not in range(0x110000)"); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13771 | return (Py_UCS4) -1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13772 | } |
| 13773 | |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13774 | return (Py_UCS4) x; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13775 | } |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 13776 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13777 | onError: |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 13778 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13779 | "%c requires int or char"); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13780 | return (Py_UCS4) -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13781 | } |
| 13782 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 13783 | PyObject * |
| 13784 | PyUnicode_Format(PyObject *format, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13785 | { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13786 | Py_ssize_t fmtcnt, fmtpos, arglen, argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13787 | int args_owned = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13788 | PyObject *dict = NULL; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13789 | PyObject *temp = NULL; |
| 13790 | PyObject *second = NULL; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13791 | PyObject *uformat; |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 13792 | void *fmt; |
| 13793 | enum PyUnicode_Kind kind, fmtkind; |
Victor Stinner | a7b654b | 2012-05-03 23:58:55 +0200 | [diff] [blame] | 13794 | unicode_writer_t writer; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 13795 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13796 | if (format == NULL || args == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13797 | PyErr_BadInternalCall(); |
| 13798 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13799 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13800 | uformat = PyUnicode_FromObject(format); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 13801 | if (uformat == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13802 | return NULL; |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 13803 | if (PyUnicode_READY(uformat) == -1) |
| 13804 | Py_DECREF(uformat); |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 13805 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13806 | fmt = PyUnicode_DATA(uformat); |
| 13807 | fmtkind = PyUnicode_KIND(uformat); |
| 13808 | fmtcnt = PyUnicode_GET_LENGTH(uformat); |
| 13809 | fmtpos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13810 | |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 13811 | if (unicode_writer_init(&writer, fmtcnt + 100, 127) < 0) |
| 13812 | goto onError; |
| 13813 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13814 | if (PyTuple_Check(args)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13815 | arglen = PyTuple_Size(args); |
| 13816 | argidx = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13817 | } |
| 13818 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13819 | arglen = -1; |
| 13820 | argidx = -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13821 | } |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 13822 | if (Py_TYPE(args)->tp_as_mapping && !PyTuple_Check(args) && |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 13823 | !PyUnicode_Check(args)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13824 | dict = args; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13825 | |
| 13826 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13827 | if (PyUnicode_READ(fmtkind, fmt, fmtpos) != '%') { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13828 | Py_ssize_t nonfmtpos; |
| 13829 | nonfmtpos = fmtpos++; |
| 13830 | while (fmtcnt >= 0 && |
| 13831 | PyUnicode_READ(fmtkind, fmt, fmtpos) != '%') { |
| 13832 | fmtpos++; |
| 13833 | fmtcnt--; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13834 | } |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 13835 | if (fmtcnt < 0) |
| 13836 | fmtpos--; |
| 13837 | if (unicode_writer_write_str(&writer, uformat, nonfmtpos, fmtpos - nonfmtpos) < 0) |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13838 | goto onError; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13839 | } |
| 13840 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13841 | /* Got a format specifier */ |
| 13842 | int flags = 0; |
| 13843 | Py_ssize_t width = -1; |
| 13844 | int prec = -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13845 | Py_UCS4 c = '\0'; |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 13846 | Py_UCS4 fill; |
| 13847 | int sign; |
| 13848 | Py_UCS4 signchar; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13849 | int isnumok; |
| 13850 | PyObject *v = NULL; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13851 | void *pbuf = NULL; |
| 13852 | Py_ssize_t pindex, len; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13853 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13854 | fmtpos++; |
Victor Stinner | 438106b | 2012-05-02 00:41:57 +0200 | [diff] [blame] | 13855 | c = PyUnicode_READ(fmtkind, fmt, fmtpos); |
| 13856 | if (c == '(') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13857 | Py_ssize_t keystart; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13858 | Py_ssize_t keylen; |
| 13859 | PyObject *key; |
| 13860 | int pcount = 1; |
Christian Heimes | a612dc0 | 2008-02-24 13:08:18 +0000 | [diff] [blame] | 13861 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13862 | if (dict == NULL) { |
| 13863 | PyErr_SetString(PyExc_TypeError, |
| 13864 | "format requires a mapping"); |
| 13865 | goto onError; |
| 13866 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13867 | ++fmtpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13868 | --fmtcnt; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13869 | keystart = fmtpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13870 | /* Skip over balanced parentheses */ |
| 13871 | while (pcount > 0 && --fmtcnt >= 0) { |
Victor Stinner | bff7c96 | 2012-05-03 01:44:59 +0200 | [diff] [blame] | 13872 | c = PyUnicode_READ(fmtkind, fmt, fmtpos); |
| 13873 | if (c == ')') |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13874 | --pcount; |
Victor Stinner | bff7c96 | 2012-05-03 01:44:59 +0200 | [diff] [blame] | 13875 | else if (c == '(') |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13876 | ++pcount; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13877 | fmtpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13878 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13879 | keylen = fmtpos - keystart - 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13880 | if (fmtcnt < 0 || pcount > 0) { |
| 13881 | PyErr_SetString(PyExc_ValueError, |
| 13882 | "incomplete format key"); |
| 13883 | goto onError; |
| 13884 | } |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 13885 | key = PyUnicode_Substring(uformat, |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 13886 | keystart, keystart + keylen); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13887 | if (key == NULL) |
| 13888 | goto onError; |
| 13889 | if (args_owned) { |
| 13890 | Py_DECREF(args); |
| 13891 | args_owned = 0; |
| 13892 | } |
| 13893 | args = PyObject_GetItem(dict, key); |
| 13894 | Py_DECREF(key); |
| 13895 | if (args == NULL) { |
| 13896 | goto onError; |
| 13897 | } |
| 13898 | args_owned = 1; |
| 13899 | arglen = -1; |
| 13900 | argidx = -2; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13901 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13902 | while (--fmtcnt >= 0) { |
Victor Stinner | 438106b | 2012-05-02 00:41:57 +0200 | [diff] [blame] | 13903 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
| 13904 | switch (c) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13905 | case '-': flags |= F_LJUST; continue; |
| 13906 | case '+': flags |= F_SIGN; continue; |
| 13907 | case ' ': flags |= F_BLANK; continue; |
| 13908 | case '#': flags |= F_ALT; continue; |
| 13909 | case '0': flags |= F_ZERO; continue; |
| 13910 | } |
| 13911 | break; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13912 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13913 | if (c == '*') { |
| 13914 | v = getnextarg(args, arglen, &argidx); |
| 13915 | if (v == NULL) |
| 13916 | goto onError; |
| 13917 | if (!PyLong_Check(v)) { |
| 13918 | PyErr_SetString(PyExc_TypeError, |
| 13919 | "* wants int"); |
| 13920 | goto onError; |
| 13921 | } |
| 13922 | width = PyLong_AsLong(v); |
| 13923 | if (width == -1 && PyErr_Occurred()) |
| 13924 | goto onError; |
| 13925 | if (width < 0) { |
| 13926 | flags |= F_LJUST; |
| 13927 | width = -width; |
| 13928 | } |
| 13929 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13930 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13931 | } |
| 13932 | else if (c >= '0' && c <= '9') { |
| 13933 | width = c - '0'; |
| 13934 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13935 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13936 | if (c < '0' || c > '9') |
| 13937 | break; |
Mark Dickinson | 99e2e55 | 2012-05-07 11:20:50 +0100 | [diff] [blame] | 13938 | if (width > (PY_SSIZE_T_MAX - (c - '0')) / 10) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13939 | PyErr_SetString(PyExc_ValueError, |
| 13940 | "width too big"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13941 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13942 | } |
| 13943 | width = width*10 + (c - '0'); |
| 13944 | } |
| 13945 | } |
| 13946 | if (c == '.') { |
| 13947 | prec = 0; |
| 13948 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13949 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13950 | if (c == '*') { |
| 13951 | v = getnextarg(args, arglen, &argidx); |
| 13952 | if (v == NULL) |
| 13953 | goto onError; |
| 13954 | if (!PyLong_Check(v)) { |
| 13955 | PyErr_SetString(PyExc_TypeError, |
| 13956 | "* wants int"); |
| 13957 | goto onError; |
| 13958 | } |
| 13959 | prec = PyLong_AsLong(v); |
| 13960 | if (prec == -1 && PyErr_Occurred()) |
| 13961 | goto onError; |
| 13962 | if (prec < 0) |
| 13963 | prec = 0; |
| 13964 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13965 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13966 | } |
| 13967 | else if (c >= '0' && c <= '9') { |
| 13968 | prec = c - '0'; |
| 13969 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13970 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13971 | if (c < '0' || c > '9') |
| 13972 | break; |
Mark Dickinson | 99e2e55 | 2012-05-07 11:20:50 +0100 | [diff] [blame] | 13973 | if (prec > (INT_MAX - (c - '0')) / 10) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13974 | PyErr_SetString(PyExc_ValueError, |
| 13975 | "prec too big"); |
| 13976 | goto onError; |
| 13977 | } |
| 13978 | prec = prec*10 + (c - '0'); |
| 13979 | } |
| 13980 | } |
| 13981 | } /* prec */ |
| 13982 | if (fmtcnt >= 0) { |
| 13983 | if (c == 'h' || c == 'l' || c == 'L') { |
| 13984 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13985 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13986 | } |
| 13987 | } |
| 13988 | if (fmtcnt < 0) { |
| 13989 | PyErr_SetString(PyExc_ValueError, |
| 13990 | "incomplete format"); |
| 13991 | goto onError; |
| 13992 | } |
Victor Stinner | aff3cc6 | 2012-04-30 05:19:21 +0200 | [diff] [blame] | 13993 | |
| 13994 | if (c == '%') { |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 13995 | if (unicode_writer_write_char(&writer, '%') < 0) |
| 13996 | goto onError; |
Victor Stinner | aff3cc6 | 2012-04-30 05:19:21 +0200 | [diff] [blame] | 13997 | continue; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13998 | } |
Victor Stinner | aff3cc6 | 2012-04-30 05:19:21 +0200 | [diff] [blame] | 13999 | |
| 14000 | |
| 14001 | v = getnextarg(args, arglen, &argidx); |
| 14002 | if (v == NULL) |
| 14003 | goto onError; |
| 14004 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14005 | sign = 0; |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 14006 | signchar = '\0'; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14007 | fill = ' '; |
| 14008 | switch (c) { |
| 14009 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14010 | case 's': |
| 14011 | case 'r': |
| 14012 | case 'a': |
Victor Stinner | 808fc0a | 2010-03-22 12:50:40 +0000 | [diff] [blame] | 14013 | if (PyUnicode_CheckExact(v) && c == 's') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14014 | temp = v; |
| 14015 | Py_INCREF(temp); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14016 | } |
| 14017 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14018 | if (c == 's') |
| 14019 | temp = PyObject_Str(v); |
| 14020 | else if (c == 'r') |
| 14021 | temp = PyObject_Repr(v); |
| 14022 | else |
| 14023 | temp = PyObject_ASCII(v); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14024 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14025 | break; |
| 14026 | |
| 14027 | case 'i': |
| 14028 | case 'd': |
| 14029 | case 'u': |
| 14030 | case 'o': |
| 14031 | case 'x': |
| 14032 | case 'X': |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14033 | isnumok = 0; |
| 14034 | if (PyNumber_Check(v)) { |
| 14035 | PyObject *iobj=NULL; |
| 14036 | |
| 14037 | if (PyLong_Check(v)) { |
| 14038 | iobj = v; |
| 14039 | Py_INCREF(iobj); |
| 14040 | } |
| 14041 | else { |
| 14042 | iobj = PyNumber_Long(v); |
| 14043 | } |
| 14044 | if (iobj!=NULL) { |
| 14045 | if (PyLong_Check(iobj)) { |
| 14046 | isnumok = 1; |
Victor Stinner | aff3cc6 | 2012-04-30 05:19:21 +0200 | [diff] [blame] | 14047 | sign = 1; |
Senthil Kumaran | 9ebe08d | 2011-07-03 21:03:16 -0700 | [diff] [blame] | 14048 | temp = formatlong(iobj, flags, prec, (c == 'i'? 'd': c)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14049 | Py_DECREF(iobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14050 | } |
| 14051 | else { |
| 14052 | Py_DECREF(iobj); |
| 14053 | } |
| 14054 | } |
| 14055 | } |
| 14056 | if (!isnumok) { |
| 14057 | PyErr_Format(PyExc_TypeError, |
| 14058 | "%%%c format: a number is required, " |
| 14059 | "not %.200s", (char)c, Py_TYPE(v)->tp_name); |
| 14060 | goto onError; |
| 14061 | } |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 14062 | if (flags & F_ZERO) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14063 | fill = '0'; |
| 14064 | break; |
| 14065 | |
| 14066 | case 'e': |
| 14067 | case 'E': |
| 14068 | case 'f': |
| 14069 | case 'F': |
| 14070 | case 'g': |
| 14071 | case 'G': |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14072 | sign = 1; |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 14073 | if (flags & F_ZERO) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14074 | fill = '0'; |
Victor Stinner | aff3cc6 | 2012-04-30 05:19:21 +0200 | [diff] [blame] | 14075 | temp = formatfloat(v, flags, prec, c); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14076 | break; |
| 14077 | |
| 14078 | case 'c': |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14079 | { |
| 14080 | Py_UCS4 ch = formatchar(v); |
| 14081 | if (ch == (Py_UCS4) -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14082 | goto onError; |
Victor Stinner | b5c3ea3 | 2012-05-02 00:29:36 +0200 | [diff] [blame] | 14083 | temp = PyUnicode_FromOrdinal(ch); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14084 | break; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14085 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14086 | |
| 14087 | default: |
| 14088 | PyErr_Format(PyExc_ValueError, |
| 14089 | "unsupported format character '%c' (0x%x) " |
| 14090 | "at index %zd", |
| 14091 | (31<=c && c<=126) ? (char)c : '?', |
| 14092 | (int)c, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14093 | fmtpos - 1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14094 | goto onError; |
| 14095 | } |
Victor Stinner | aff3cc6 | 2012-04-30 05:19:21 +0200 | [diff] [blame] | 14096 | if (temp == NULL) |
| 14097 | goto onError; |
| 14098 | assert (PyUnicode_Check(temp)); |
| 14099 | if (PyUnicode_READY(temp) == -1) { |
| 14100 | Py_CLEAR(temp); |
| 14101 | goto onError; |
| 14102 | } |
| 14103 | kind = PyUnicode_KIND(temp); |
| 14104 | pbuf = PyUnicode_DATA(temp); |
| 14105 | len = PyUnicode_GET_LENGTH(temp); |
| 14106 | |
| 14107 | if (c == 's' || c == 'r' || c == 'a') { |
| 14108 | if (prec >= 0 && len > prec) |
| 14109 | len = prec; |
| 14110 | } |
| 14111 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14112 | /* pbuf is initialized here. */ |
| 14113 | pindex = 0; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14114 | if (sign) { |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 14115 | Py_UCS4 ch = PyUnicode_READ(kind, pbuf, pindex); |
| 14116 | if (ch == '-' || ch == '+') { |
| 14117 | signchar = ch; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14118 | len--; |
| 14119 | pindex++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14120 | } |
| 14121 | else if (flags & F_SIGN) |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 14122 | signchar = '+'; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14123 | else if (flags & F_BLANK) |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 14124 | signchar = ' '; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14125 | else |
| 14126 | sign = 0; |
| 14127 | } |
| 14128 | if (width < len) |
| 14129 | width = len; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14130 | if (sign) { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14131 | if (fill != ' ') { |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 14132 | if (unicode_writer_write_char(&writer, signchar) < 0) |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14133 | goto onError; |
| 14134 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14135 | if (width > len) |
| 14136 | width--; |
| 14137 | } |
| 14138 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14139 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14140 | assert(PyUnicode_READ(kind, pbuf, pindex + 1) == c); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14141 | if (fill != ' ') { |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 14142 | if (unicode_writer_prepare(&writer, 2, 127) < 0) |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14143 | goto onError; |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 14144 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, '0'); |
| 14145 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos+1, c); |
| 14146 | writer.pos += 2; |
| 14147 | pindex += 2; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14148 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14149 | width -= 2; |
| 14150 | if (width < 0) |
| 14151 | width = 0; |
| 14152 | len -= 2; |
| 14153 | } |
| 14154 | if (width > len && !(flags & F_LJUST)) { |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 14155 | Py_ssize_t sublen; |
| 14156 | sublen = width - len; |
| 14157 | if (unicode_writer_prepare(&writer, sublen, fill) < 0) |
Antoine Pitrou | 978b9d2 | 2011-10-07 12:35:48 +0200 | [diff] [blame] | 14158 | goto onError; |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 14159 | FILL(writer.kind, writer.data, fill, writer.pos, sublen); |
| 14160 | writer.pos += sublen; |
Antoine Pitrou | 978b9d2 | 2011-10-07 12:35:48 +0200 | [diff] [blame] | 14161 | width = len; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14162 | } |
| 14163 | if (fill == ' ') { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14164 | if (sign) { |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 14165 | if (unicode_writer_write_char(&writer, signchar) < 0) |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14166 | goto onError; |
| 14167 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14168 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14169 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
| 14170 | assert(PyUnicode_READ(kind, pbuf, pindex+1) == c); |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 14171 | |
| 14172 | if (unicode_writer_prepare(&writer, 2, 127) < 0) |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14173 | goto onError; |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 14174 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, '0'); |
| 14175 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos+1, c); |
| 14176 | writer.pos += 2; |
| 14177 | |
| 14178 | pindex += 2; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14179 | } |
| 14180 | } |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 14181 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14182 | /* Copy all characters, preserving len */ |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 14183 | if (unicode_writer_write_str(&writer, temp, pindex, len) < 0) |
| 14184 | goto onError; |
| 14185 | if (width > len) { |
| 14186 | Py_ssize_t sublen = width - len; |
| 14187 | if (unicode_writer_prepare(&writer, sublen, ' ') < 0) |
Victor Stinner | b80e46e | 2012-04-30 05:21:52 +0200 | [diff] [blame] | 14188 | goto onError; |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 14189 | FILL(writer.kind, writer.data, ' ', writer.pos, sublen); |
| 14190 | writer.pos += sublen; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14191 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14192 | if (dict && (argidx < arglen) && c != '%') { |
| 14193 | PyErr_SetString(PyExc_TypeError, |
| 14194 | "not all arguments converted during string formatting"); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14195 | goto onError; |
| 14196 | } |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14197 | Py_CLEAR(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14198 | } /* '%' */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14199 | } /* until end */ |
| 14200 | if (argidx < arglen && !dict) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14201 | PyErr_SetString(PyExc_TypeError, |
| 14202 | "not all arguments converted during string formatting"); |
| 14203 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14204 | } |
| 14205 | |
| 14206 | if (args_owned) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14207 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14208 | } |
| 14209 | Py_DECREF(uformat); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14210 | Py_XDECREF(temp); |
| 14211 | Py_XDECREF(second); |
Victor Stinner | a7b654b | 2012-05-03 23:58:55 +0200 | [diff] [blame] | 14212 | return unicode_writer_finish(&writer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14213 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14214 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14215 | Py_DECREF(uformat); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14216 | Py_XDECREF(temp); |
| 14217 | Py_XDECREF(second); |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 14218 | unicode_writer_dealloc(&writer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14219 | if (args_owned) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14220 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14221 | } |
| 14222 | return NULL; |
| 14223 | } |
| 14224 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 14225 | static PyObject * |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 14226 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 14227 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14228 | static PyObject * |
| 14229 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 14230 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14231 | PyObject *x = NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14232 | static char *kwlist[] = {"object", "encoding", "errors", 0}; |
| 14233 | char *encoding = NULL; |
| 14234 | char *errors = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14235 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14236 | if (type != &PyUnicode_Type) |
| 14237 | return unicode_subtype_new(type, args, kwds); |
| 14238 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:str", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14239 | kwlist, &x, &encoding, &errors)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14240 | return NULL; |
Victor Stinner | 382955f | 2011-12-11 21:44:00 +0100 | [diff] [blame] | 14241 | if (x == NULL) { |
| 14242 | Py_INCREF(unicode_empty); |
| 14243 | return unicode_empty; |
| 14244 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14245 | if (encoding == NULL && errors == NULL) |
| 14246 | return PyObject_Str(x); |
| 14247 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14248 | return PyUnicode_FromEncodedObject(x, encoding, errors); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14249 | } |
| 14250 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 14251 | static PyObject * |
| 14252 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 14253 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14254 | PyObject *unicode, *self; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14255 | Py_ssize_t length, char_size; |
| 14256 | int share_wstr, share_utf8; |
| 14257 | unsigned int kind; |
| 14258 | void *data; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 14259 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14260 | assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14261 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14262 | unicode = unicode_new(&PyUnicode_Type, args, kwds); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14263 | if (unicode == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14264 | return NULL; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 14265 | assert(_PyUnicode_CHECK(unicode)); |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 14266 | if (PyUnicode_READY(unicode) == -1) { |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 14267 | Py_DECREF(unicode); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14268 | return NULL; |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 14269 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14270 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14271 | self = type->tp_alloc(type, 0); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14272 | if (self == NULL) { |
| 14273 | Py_DECREF(unicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14274 | return NULL; |
| 14275 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14276 | kind = PyUnicode_KIND(unicode); |
| 14277 | length = PyUnicode_GET_LENGTH(unicode); |
| 14278 | |
| 14279 | _PyUnicode_LENGTH(self) = length; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 14280 | #ifdef Py_DEBUG |
| 14281 | _PyUnicode_HASH(self) = -1; |
| 14282 | #else |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14283 | _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 14284 | #endif |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14285 | _PyUnicode_STATE(self).interned = 0; |
| 14286 | _PyUnicode_STATE(self).kind = kind; |
| 14287 | _PyUnicode_STATE(self).compact = 0; |
Victor Stinner | 3cf4637 | 2011-10-03 14:42:15 +0200 | [diff] [blame] | 14288 | _PyUnicode_STATE(self).ascii = _PyUnicode_STATE(unicode).ascii; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14289 | _PyUnicode_STATE(self).ready = 1; |
| 14290 | _PyUnicode_WSTR(self) = NULL; |
| 14291 | _PyUnicode_UTF8_LENGTH(self) = 0; |
| 14292 | _PyUnicode_UTF8(self) = NULL; |
| 14293 | _PyUnicode_WSTR_LENGTH(self) = 0; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 14294 | _PyUnicode_DATA_ANY(self) = NULL; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14295 | |
| 14296 | share_utf8 = 0; |
| 14297 | share_wstr = 0; |
| 14298 | if (kind == PyUnicode_1BYTE_KIND) { |
| 14299 | char_size = 1; |
| 14300 | if (PyUnicode_MAX_CHAR_VALUE(unicode) < 128) |
| 14301 | share_utf8 = 1; |
| 14302 | } |
| 14303 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 14304 | char_size = 2; |
| 14305 | if (sizeof(wchar_t) == 2) |
| 14306 | share_wstr = 1; |
| 14307 | } |
| 14308 | else { |
| 14309 | assert(kind == PyUnicode_4BYTE_KIND); |
| 14310 | char_size = 4; |
| 14311 | if (sizeof(wchar_t) == 4) |
| 14312 | share_wstr = 1; |
| 14313 | } |
| 14314 | |
| 14315 | /* Ensure we won't overflow the length. */ |
| 14316 | if (length > (PY_SSIZE_T_MAX / char_size - 1)) { |
| 14317 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14318 | goto onError; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14319 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14320 | data = PyObject_MALLOC((length + 1) * char_size); |
| 14321 | if (data == NULL) { |
| 14322 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14323 | goto onError; |
| 14324 | } |
| 14325 | |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 14326 | _PyUnicode_DATA_ANY(self) = data; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14327 | if (share_utf8) { |
| 14328 | _PyUnicode_UTF8_LENGTH(self) = length; |
| 14329 | _PyUnicode_UTF8(self) = data; |
| 14330 | } |
| 14331 | if (share_wstr) { |
| 14332 | _PyUnicode_WSTR_LENGTH(self) = length; |
| 14333 | _PyUnicode_WSTR(self) = (wchar_t *)data; |
| 14334 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14335 | |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14336 | Py_MEMCPY(data, PyUnicode_DATA(unicode), |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 14337 | kind * (length + 1)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 14338 | assert(_PyUnicode_CheckConsistency(self, 1)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 14339 | #ifdef Py_DEBUG |
| 14340 | _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode); |
| 14341 | #endif |
Victor Stinner | dd18d3a | 2011-10-22 11:08:10 +0200 | [diff] [blame] | 14342 | Py_DECREF(unicode); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 14343 | return self; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14344 | |
| 14345 | onError: |
| 14346 | Py_DECREF(unicode); |
| 14347 | Py_DECREF(self); |
| 14348 | return NULL; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 14349 | } |
| 14350 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 14351 | PyDoc_STRVAR(unicode_doc, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14352 | "str(string[, encoding[, errors]]) -> str\n\ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14353 | \n\ |
Collin Winter | d474ce8 | 2007-08-07 19:42:11 +0000 | [diff] [blame] | 14354 | Create a new string object from the given encoded string.\n\ |
Skip Montanaro | 35b37a5 | 2002-07-26 16:22:46 +0000 | [diff] [blame] | 14355 | encoding defaults to the current default string encoding.\n\ |
| 14356 | errors can be 'strict', 'replace' or 'ignore' and defaults to 'strict'."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14357 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14358 | static PyObject *unicode_iter(PyObject *seq); |
| 14359 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14360 | PyTypeObject PyUnicode_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 14361 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14362 | "str", /* tp_name */ |
| 14363 | sizeof(PyUnicodeObject), /* tp_size */ |
| 14364 | 0, /* tp_itemsize */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14365 | /* Slots */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14366 | (destructor)unicode_dealloc, /* tp_dealloc */ |
| 14367 | 0, /* tp_print */ |
| 14368 | 0, /* tp_getattr */ |
| 14369 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 14370 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14371 | unicode_repr, /* tp_repr */ |
| 14372 | &unicode_as_number, /* tp_as_number */ |
| 14373 | &unicode_as_sequence, /* tp_as_sequence */ |
| 14374 | &unicode_as_mapping, /* tp_as_mapping */ |
| 14375 | (hashfunc) unicode_hash, /* tp_hash*/ |
| 14376 | 0, /* tp_call*/ |
| 14377 | (reprfunc) unicode_str, /* tp_str */ |
| 14378 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 14379 | 0, /* tp_setattro */ |
| 14380 | 0, /* tp_as_buffer */ |
| 14381 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14382 | Py_TPFLAGS_UNICODE_SUBCLASS, /* tp_flags */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14383 | unicode_doc, /* tp_doc */ |
| 14384 | 0, /* tp_traverse */ |
| 14385 | 0, /* tp_clear */ |
| 14386 | PyUnicode_RichCompare, /* tp_richcompare */ |
| 14387 | 0, /* tp_weaklistoffset */ |
| 14388 | unicode_iter, /* tp_iter */ |
| 14389 | 0, /* tp_iternext */ |
| 14390 | unicode_methods, /* tp_methods */ |
| 14391 | 0, /* tp_members */ |
| 14392 | 0, /* tp_getset */ |
| 14393 | &PyBaseObject_Type, /* tp_base */ |
| 14394 | 0, /* tp_dict */ |
| 14395 | 0, /* tp_descr_get */ |
| 14396 | 0, /* tp_descr_set */ |
| 14397 | 0, /* tp_dictoffset */ |
| 14398 | 0, /* tp_init */ |
| 14399 | 0, /* tp_alloc */ |
| 14400 | unicode_new, /* tp_new */ |
| 14401 | PyObject_Del, /* tp_free */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14402 | }; |
| 14403 | |
| 14404 | /* Initialize the Unicode implementation */ |
| 14405 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 14406 | int _PyUnicode_Init(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14407 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 14408 | int i; |
| 14409 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14410 | /* XXX - move this array to unicodectype.c ? */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14411 | Py_UCS2 linebreak[] = { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14412 | 0x000A, /* LINE FEED */ |
| 14413 | 0x000D, /* CARRIAGE RETURN */ |
| 14414 | 0x001C, /* FILE SEPARATOR */ |
| 14415 | 0x001D, /* GROUP SEPARATOR */ |
| 14416 | 0x001E, /* RECORD SEPARATOR */ |
| 14417 | 0x0085, /* NEXT LINE */ |
| 14418 | 0x2028, /* LINE SEPARATOR */ |
| 14419 | 0x2029, /* PARAGRAPH SEPARATOR */ |
| 14420 | }; |
| 14421 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 14422 | /* Init the implementation */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 14423 | unicode_empty = PyUnicode_New(0, 0); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 14424 | if (!unicode_empty) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14425 | Py_FatalError("Can't create empty string"); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 14426 | assert(_PyUnicode_CheckConsistency(unicode_empty, 1)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 14427 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 14428 | for (i = 0; i < 256; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14429 | unicode_latin1[i] = NULL; |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 14430 | if (PyType_Ready(&PyUnicode_Type) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14431 | Py_FatalError("Can't initialize 'unicode'"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14432 | |
| 14433 | /* initialize the linebreak bloom filter */ |
| 14434 | bloom_linebreak = make_bloom_mask( |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14435 | PyUnicode_2BYTE_KIND, linebreak, |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 14436 | Py_ARRAY_LENGTH(linebreak)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 14437 | |
| 14438 | PyType_Ready(&EncodingMapType); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 14439 | |
| 14440 | #ifdef HAVE_MBCS |
| 14441 | winver.dwOSVersionInfoSize = sizeof(winver); |
| 14442 | if (!GetVersionEx((OSVERSIONINFO*)&winver)) { |
| 14443 | PyErr_SetFromWindowsErr(0); |
| 14444 | return -1; |
| 14445 | } |
| 14446 | #endif |
| 14447 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14448 | } |
| 14449 | |
| 14450 | /* Finalize the Unicode implementation */ |
| 14451 | |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 14452 | int |
| 14453 | PyUnicode_ClearFreeList(void) |
| 14454 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14455 | return 0; |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 14456 | } |
| 14457 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14458 | void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 14459 | _PyUnicode_Fini(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14460 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 14461 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14462 | |
Guido van Rossum | 4ae8ef8 | 2000-10-03 18:09:04 +0000 | [diff] [blame] | 14463 | Py_XDECREF(unicode_empty); |
| 14464 | unicode_empty = NULL; |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 14465 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 14466 | for (i = 0; i < 256; i++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14467 | if (unicode_latin1[i]) { |
| 14468 | Py_DECREF(unicode_latin1[i]); |
| 14469 | unicode_latin1[i] = NULL; |
| 14470 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 14471 | } |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 14472 | _PyUnicode_ClearStaticStrings(); |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 14473 | (void)PyUnicode_ClearFreeList(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14474 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 14475 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14476 | void |
| 14477 | PyUnicode_InternInPlace(PyObject **p) |
| 14478 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14479 | register PyObject *s = *p; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14480 | PyObject *t; |
Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 14481 | #ifdef Py_DEBUG |
| 14482 | assert(s != NULL); |
| 14483 | assert(_PyUnicode_CHECK(s)); |
| 14484 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14485 | if (s == NULL || !PyUnicode_Check(s)) |
Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 14486 | return; |
| 14487 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14488 | /* If it's a subclass, we don't really know what putting |
| 14489 | it in the interned dict might do. */ |
| 14490 | if (!PyUnicode_CheckExact(s)) |
| 14491 | return; |
| 14492 | if (PyUnicode_CHECK_INTERNED(s)) |
| 14493 | return; |
| 14494 | if (interned == NULL) { |
| 14495 | interned = PyDict_New(); |
| 14496 | if (interned == NULL) { |
| 14497 | PyErr_Clear(); /* Don't leave an exception */ |
| 14498 | return; |
| 14499 | } |
| 14500 | } |
| 14501 | /* It might be that the GetItem call fails even |
| 14502 | though the key is present in the dictionary, |
| 14503 | namely when this happens during a stack overflow. */ |
| 14504 | Py_ALLOW_RECURSION |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 14505 | t = PyDict_GetItem(interned, s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14506 | Py_END_ALLOW_RECURSION |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 14507 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14508 | if (t) { |
| 14509 | Py_INCREF(t); |
| 14510 | Py_DECREF(*p); |
| 14511 | *p = t; |
| 14512 | return; |
| 14513 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14514 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14515 | PyThreadState_GET()->recursion_critical = 1; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 14516 | if (PyDict_SetItem(interned, s, s) < 0) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14517 | PyErr_Clear(); |
| 14518 | PyThreadState_GET()->recursion_critical = 0; |
| 14519 | return; |
| 14520 | } |
| 14521 | PyThreadState_GET()->recursion_critical = 0; |
| 14522 | /* The two references in interned are not counted by refcnt. |
| 14523 | The deallocator will take care of this */ |
| 14524 | Py_REFCNT(s) -= 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14525 | _PyUnicode_STATE(s).interned = SSTATE_INTERNED_MORTAL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14526 | } |
| 14527 | |
| 14528 | void |
| 14529 | PyUnicode_InternImmortal(PyObject **p) |
| 14530 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14531 | PyUnicode_InternInPlace(p); |
| 14532 | if (PyUnicode_CHECK_INTERNED(*p) != SSTATE_INTERNED_IMMORTAL) { |
Victor Stinner | af9e4b8 | 2011-10-23 20:07:00 +0200 | [diff] [blame] | 14533 | _PyUnicode_STATE(*p).interned = SSTATE_INTERNED_IMMORTAL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14534 | Py_INCREF(*p); |
| 14535 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14536 | } |
| 14537 | |
| 14538 | PyObject * |
| 14539 | PyUnicode_InternFromString(const char *cp) |
| 14540 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14541 | PyObject *s = PyUnicode_FromString(cp); |
| 14542 | if (s == NULL) |
| 14543 | return NULL; |
| 14544 | PyUnicode_InternInPlace(&s); |
| 14545 | return s; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14546 | } |
| 14547 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 14548 | void |
| 14549 | _Py_ReleaseInternedUnicodeStrings(void) |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14550 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14551 | PyObject *keys; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14552 | PyObject *s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14553 | Py_ssize_t i, n; |
| 14554 | Py_ssize_t immortal_size = 0, mortal_size = 0; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14555 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14556 | if (interned == NULL || !PyDict_Check(interned)) |
| 14557 | return; |
| 14558 | keys = PyDict_Keys(interned); |
| 14559 | if (keys == NULL || !PyList_Check(keys)) { |
| 14560 | PyErr_Clear(); |
| 14561 | return; |
| 14562 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14563 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14564 | /* Since _Py_ReleaseInternedUnicodeStrings() is intended to help a leak |
| 14565 | detector, interned unicode strings are not forcibly deallocated; |
| 14566 | rather, we give them their stolen references back, and then clear |
| 14567 | and DECREF the interned dict. */ |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14568 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14569 | n = PyList_GET_SIZE(keys); |
| 14570 | fprintf(stderr, "releasing %" PY_FORMAT_SIZE_T "d interned strings\n", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14571 | n); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14572 | for (i = 0; i < n; i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14573 | s = PyList_GET_ITEM(keys, i); |
Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 14574 | if (PyUnicode_READY(s) == -1) { |
| 14575 | assert(0 && "could not ready string"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14576 | fprintf(stderr, "could not ready string\n"); |
Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 14577 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14578 | switch (PyUnicode_CHECK_INTERNED(s)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14579 | case SSTATE_NOT_INTERNED: |
| 14580 | /* XXX Shouldn't happen */ |
| 14581 | break; |
| 14582 | case SSTATE_INTERNED_IMMORTAL: |
| 14583 | Py_REFCNT(s) += 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14584 | immortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14585 | break; |
| 14586 | case SSTATE_INTERNED_MORTAL: |
| 14587 | Py_REFCNT(s) += 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14588 | mortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14589 | break; |
| 14590 | default: |
| 14591 | Py_FatalError("Inconsistent interned string state."); |
| 14592 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14593 | _PyUnicode_STATE(s).interned = SSTATE_NOT_INTERNED; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14594 | } |
| 14595 | fprintf(stderr, "total size of all interned strings: " |
| 14596 | "%" PY_FORMAT_SIZE_T "d/%" PY_FORMAT_SIZE_T "d " |
| 14597 | "mortal/immortal\n", mortal_size, immortal_size); |
| 14598 | Py_DECREF(keys); |
| 14599 | PyDict_Clear(interned); |
| 14600 | Py_DECREF(interned); |
| 14601 | interned = NULL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14602 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14603 | |
| 14604 | |
| 14605 | /********************* Unicode Iterator **************************/ |
| 14606 | |
| 14607 | typedef struct { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14608 | PyObject_HEAD |
| 14609 | Py_ssize_t it_index; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14610 | PyObject *it_seq; /* Set to NULL when iterator is exhausted */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14611 | } unicodeiterobject; |
| 14612 | |
| 14613 | static void |
| 14614 | unicodeiter_dealloc(unicodeiterobject *it) |
| 14615 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14616 | _PyObject_GC_UNTRACK(it); |
| 14617 | Py_XDECREF(it->it_seq); |
| 14618 | PyObject_GC_Del(it); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14619 | } |
| 14620 | |
| 14621 | static int |
| 14622 | unicodeiter_traverse(unicodeiterobject *it, visitproc visit, void *arg) |
| 14623 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14624 | Py_VISIT(it->it_seq); |
| 14625 | return 0; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14626 | } |
| 14627 | |
| 14628 | static PyObject * |
| 14629 | unicodeiter_next(unicodeiterobject *it) |
| 14630 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14631 | PyObject *seq, *item; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14632 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14633 | assert(it != NULL); |
| 14634 | seq = it->it_seq; |
| 14635 | if (seq == NULL) |
| 14636 | return NULL; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 14637 | assert(_PyUnicode_CHECK(seq)); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14638 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14639 | if (it->it_index < PyUnicode_GET_LENGTH(seq)) { |
| 14640 | int kind = PyUnicode_KIND(seq); |
| 14641 | void *data = PyUnicode_DATA(seq); |
| 14642 | Py_UCS4 chr = PyUnicode_READ(kind, data, it->it_index); |
| 14643 | item = PyUnicode_FromOrdinal(chr); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14644 | if (item != NULL) |
| 14645 | ++it->it_index; |
| 14646 | return item; |
| 14647 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14648 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14649 | Py_DECREF(seq); |
| 14650 | it->it_seq = NULL; |
| 14651 | return NULL; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14652 | } |
| 14653 | |
| 14654 | static PyObject * |
| 14655 | unicodeiter_len(unicodeiterobject *it) |
| 14656 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14657 | Py_ssize_t len = 0; |
| 14658 | if (it->it_seq) |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 14659 | len = PyUnicode_GET_LENGTH(it->it_seq) - it->it_index; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14660 | return PyLong_FromSsize_t(len); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14661 | } |
| 14662 | |
| 14663 | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
| 14664 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 14665 | static PyObject * |
| 14666 | unicodeiter_reduce(unicodeiterobject *it) |
| 14667 | { |
| 14668 | if (it->it_seq != NULL) { |
Antoine Pitrou | a701388 | 2012-04-05 00:04:20 +0200 | [diff] [blame] | 14669 | return Py_BuildValue("N(O)n", _PyObject_GetBuiltin("iter"), |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 14670 | it->it_seq, it->it_index); |
| 14671 | } else { |
| 14672 | PyObject *u = PyUnicode_FromUnicode(NULL, 0); |
| 14673 | if (u == NULL) |
| 14674 | return NULL; |
Antoine Pitrou | a701388 | 2012-04-05 00:04:20 +0200 | [diff] [blame] | 14675 | return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), u); |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 14676 | } |
| 14677 | } |
| 14678 | |
| 14679 | PyDoc_STRVAR(reduce_doc, "Return state information for pickling."); |
| 14680 | |
| 14681 | static PyObject * |
| 14682 | unicodeiter_setstate(unicodeiterobject *it, PyObject *state) |
| 14683 | { |
| 14684 | Py_ssize_t index = PyLong_AsSsize_t(state); |
| 14685 | if (index == -1 && PyErr_Occurred()) |
| 14686 | return NULL; |
| 14687 | if (index < 0) |
| 14688 | index = 0; |
| 14689 | it->it_index = index; |
| 14690 | Py_RETURN_NONE; |
| 14691 | } |
| 14692 | |
| 14693 | PyDoc_STRVAR(setstate_doc, "Set state information for unpickling."); |
| 14694 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14695 | static PyMethodDef unicodeiter_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14696 | {"__length_hint__", (PyCFunction)unicodeiter_len, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14697 | length_hint_doc}, |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 14698 | {"__reduce__", (PyCFunction)unicodeiter_reduce, METH_NOARGS, |
| 14699 | reduce_doc}, |
| 14700 | {"__setstate__", (PyCFunction)unicodeiter_setstate, METH_O, |
| 14701 | setstate_doc}, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14702 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14703 | }; |
| 14704 | |
| 14705 | PyTypeObject PyUnicodeIter_Type = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14706 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 14707 | "str_iterator", /* tp_name */ |
| 14708 | sizeof(unicodeiterobject), /* tp_basicsize */ |
| 14709 | 0, /* tp_itemsize */ |
| 14710 | /* methods */ |
| 14711 | (destructor)unicodeiter_dealloc, /* tp_dealloc */ |
| 14712 | 0, /* tp_print */ |
| 14713 | 0, /* tp_getattr */ |
| 14714 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 14715 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14716 | 0, /* tp_repr */ |
| 14717 | 0, /* tp_as_number */ |
| 14718 | 0, /* tp_as_sequence */ |
| 14719 | 0, /* tp_as_mapping */ |
| 14720 | 0, /* tp_hash */ |
| 14721 | 0, /* tp_call */ |
| 14722 | 0, /* tp_str */ |
| 14723 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 14724 | 0, /* tp_setattro */ |
| 14725 | 0, /* tp_as_buffer */ |
| 14726 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 14727 | 0, /* tp_doc */ |
| 14728 | (traverseproc)unicodeiter_traverse, /* tp_traverse */ |
| 14729 | 0, /* tp_clear */ |
| 14730 | 0, /* tp_richcompare */ |
| 14731 | 0, /* tp_weaklistoffset */ |
| 14732 | PyObject_SelfIter, /* tp_iter */ |
| 14733 | (iternextfunc)unicodeiter_next, /* tp_iternext */ |
| 14734 | unicodeiter_methods, /* tp_methods */ |
| 14735 | 0, |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14736 | }; |
| 14737 | |
| 14738 | static PyObject * |
| 14739 | unicode_iter(PyObject *seq) |
| 14740 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14741 | unicodeiterobject *it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14742 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14743 | if (!PyUnicode_Check(seq)) { |
| 14744 | PyErr_BadInternalCall(); |
| 14745 | return NULL; |
| 14746 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14747 | if (PyUnicode_READY(seq) == -1) |
| 14748 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14749 | it = PyObject_GC_New(unicodeiterobject, &PyUnicodeIter_Type); |
| 14750 | if (it == NULL) |
| 14751 | return NULL; |
| 14752 | it->it_index = 0; |
| 14753 | Py_INCREF(seq); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14754 | it->it_seq = seq; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14755 | _PyObject_GC_TRACK(it); |
| 14756 | return (PyObject *)it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14757 | } |
| 14758 | |
Martin v. Löwis | 0d3072e | 2011-10-31 08:40:56 +0100 | [diff] [blame] | 14759 | |
| 14760 | size_t |
| 14761 | Py_UNICODE_strlen(const Py_UNICODE *u) |
| 14762 | { |
| 14763 | int res = 0; |
| 14764 | while(*u++) |
| 14765 | res++; |
| 14766 | return res; |
| 14767 | } |
| 14768 | |
| 14769 | Py_UNICODE* |
| 14770 | Py_UNICODE_strcpy(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 14771 | { |
| 14772 | Py_UNICODE *u = s1; |
| 14773 | while ((*u++ = *s2++)); |
| 14774 | return s1; |
| 14775 | } |
| 14776 | |
| 14777 | Py_UNICODE* |
| 14778 | Py_UNICODE_strncpy(Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 14779 | { |
| 14780 | Py_UNICODE *u = s1; |
| 14781 | while ((*u++ = *s2++)) |
| 14782 | if (n-- == 0) |
| 14783 | break; |
| 14784 | return s1; |
| 14785 | } |
| 14786 | |
| 14787 | Py_UNICODE* |
| 14788 | Py_UNICODE_strcat(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 14789 | { |
| 14790 | Py_UNICODE *u1 = s1; |
| 14791 | u1 += Py_UNICODE_strlen(u1); |
| 14792 | Py_UNICODE_strcpy(u1, s2); |
| 14793 | return s1; |
| 14794 | } |
| 14795 | |
| 14796 | int |
| 14797 | Py_UNICODE_strcmp(const Py_UNICODE *s1, const Py_UNICODE *s2) |
| 14798 | { |
| 14799 | while (*s1 && *s2 && *s1 == *s2) |
| 14800 | s1++, s2++; |
| 14801 | if (*s1 && *s2) |
| 14802 | return (*s1 < *s2) ? -1 : +1; |
| 14803 | if (*s1) |
| 14804 | return 1; |
| 14805 | if (*s2) |
| 14806 | return -1; |
| 14807 | return 0; |
| 14808 | } |
| 14809 | |
| 14810 | int |
| 14811 | Py_UNICODE_strncmp(const Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 14812 | { |
| 14813 | register Py_UNICODE u1, u2; |
| 14814 | for (; n != 0; n--) { |
| 14815 | u1 = *s1; |
| 14816 | u2 = *s2; |
| 14817 | if (u1 != u2) |
| 14818 | return (u1 < u2) ? -1 : +1; |
| 14819 | if (u1 == '\0') |
| 14820 | return 0; |
| 14821 | s1++; |
| 14822 | s2++; |
| 14823 | } |
| 14824 | return 0; |
| 14825 | } |
| 14826 | |
| 14827 | Py_UNICODE* |
| 14828 | Py_UNICODE_strchr(const Py_UNICODE *s, Py_UNICODE c) |
| 14829 | { |
| 14830 | const Py_UNICODE *p; |
| 14831 | for (p = s; *p; p++) |
| 14832 | if (*p == c) |
| 14833 | return (Py_UNICODE*)p; |
| 14834 | return NULL; |
| 14835 | } |
| 14836 | |
| 14837 | Py_UNICODE* |
| 14838 | Py_UNICODE_strrchr(const Py_UNICODE *s, Py_UNICODE c) |
| 14839 | { |
| 14840 | const Py_UNICODE *p; |
| 14841 | p = s + Py_UNICODE_strlen(s); |
| 14842 | while (p != s) { |
| 14843 | p--; |
| 14844 | if (*p == c) |
| 14845 | return (Py_UNICODE*)p; |
| 14846 | } |
| 14847 | return NULL; |
| 14848 | } |
Victor Stinner | 331ea92 | 2010-08-10 16:37:20 +0000 | [diff] [blame] | 14849 | |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14850 | Py_UNICODE* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14851 | PyUnicode_AsUnicodeCopy(PyObject *unicode) |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14852 | { |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 14853 | Py_UNICODE *u, *copy; |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14854 | Py_ssize_t len, size; |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14855 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14856 | if (!PyUnicode_Check(unicode)) { |
| 14857 | PyErr_BadArgument(); |
| 14858 | return NULL; |
| 14859 | } |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14860 | u = PyUnicode_AsUnicodeAndSize(unicode, &len); |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 14861 | if (u == NULL) |
| 14862 | return NULL; |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14863 | /* Ensure we won't overflow the size. */ |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14864 | if (len > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14865 | PyErr_NoMemory(); |
| 14866 | return NULL; |
| 14867 | } |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14868 | size = len + 1; /* copy the null character */ |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14869 | size *= sizeof(Py_UNICODE); |
| 14870 | copy = PyMem_Malloc(size); |
| 14871 | if (copy == NULL) { |
| 14872 | PyErr_NoMemory(); |
| 14873 | return NULL; |
| 14874 | } |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 14875 | memcpy(copy, u, size); |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14876 | return copy; |
| 14877 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 14878 | |
Georg Brandl | 66c221e | 2010-10-14 07:04:07 +0000 | [diff] [blame] | 14879 | /* A _string module, to export formatter_parser and formatter_field_name_split |
| 14880 | to the string.Formatter class implemented in Python. */ |
| 14881 | |
| 14882 | static PyMethodDef _string_methods[] = { |
| 14883 | {"formatter_field_name_split", (PyCFunction) formatter_field_name_split, |
| 14884 | METH_O, PyDoc_STR("split the argument as a field name")}, |
| 14885 | {"formatter_parser", (PyCFunction) formatter_parser, |
| 14886 | METH_O, PyDoc_STR("parse the argument as a format string")}, |
| 14887 | {NULL, NULL} |
| 14888 | }; |
| 14889 | |
| 14890 | static struct PyModuleDef _string_module = { |
| 14891 | PyModuleDef_HEAD_INIT, |
| 14892 | "_string", |
| 14893 | PyDoc_STR("string helper module"), |
| 14894 | 0, |
| 14895 | _string_methods, |
| 14896 | NULL, |
| 14897 | NULL, |
| 14898 | NULL, |
| 14899 | NULL |
| 14900 | }; |
| 14901 | |
| 14902 | PyMODINIT_FUNC |
| 14903 | PyInit__string(void) |
| 14904 | { |
| 14905 | return PyModule_Create(&_string_module); |
| 14906 | } |
| 14907 | |
| 14908 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 14909 | #ifdef __cplusplus |
| 14910 | } |
| 14911 | #endif |