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 | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 1153 | assert(0 <= how_many); |
| 1154 | assert(0 <= from_start); |
| 1155 | assert(0 <= to_start); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1156 | assert(PyUnicode_Check(from)); |
| 1157 | assert(PyUnicode_Check(to)); |
| 1158 | assert(PyUnicode_IS_READY(from)); |
| 1159 | assert(PyUnicode_IS_READY(to)); |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 1160 | assert(from_start + how_many <= PyUnicode_GET_LENGTH(from)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1161 | assert(to_start + how_many <= PyUnicode_GET_LENGTH(to)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1162 | |
Victor Stinner | f5ca1a2 | 2011-09-28 23:54:59 +0200 | [diff] [blame] | 1163 | if (how_many == 0) |
| 1164 | return 0; |
| 1165 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1166 | from_kind = PyUnicode_KIND(from); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1167 | from_data = PyUnicode_DATA(from); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1168 | to_kind = PyUnicode_KIND(to); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1169 | to_data = PyUnicode_DATA(to); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1170 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1171 | #ifdef Py_DEBUG |
| 1172 | if (!check_maxchar |
| 1173 | && (from_kind > to_kind |
| 1174 | || (!PyUnicode_IS_ASCII(from) && PyUnicode_IS_ASCII(to)))) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 1175 | { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1176 | const Py_UCS4 to_maxchar = PyUnicode_MAX_CHAR_VALUE(to); |
| 1177 | Py_UCS4 ch; |
| 1178 | Py_ssize_t i; |
| 1179 | for (i=0; i < how_many; i++) { |
| 1180 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
| 1181 | assert(ch <= to_maxchar); |
| 1182 | } |
| 1183 | } |
| 1184 | #endif |
| 1185 | fast = (from_kind == to_kind); |
| 1186 | if (check_maxchar |
| 1187 | && (!PyUnicode_IS_ASCII(from) && PyUnicode_IS_ASCII(to))) |
| 1188 | { |
| 1189 | /* deny latin1 => ascii */ |
| 1190 | fast = 0; |
| 1191 | } |
| 1192 | |
| 1193 | if (fast) { |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 1194 | Py_MEMCPY((char*)to_data + to_kind * to_start, |
| 1195 | (char*)from_data + from_kind * from_start, |
| 1196 | to_kind * how_many); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1197 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1198 | else if (from_kind == PyUnicode_1BYTE_KIND |
| 1199 | && to_kind == PyUnicode_2BYTE_KIND) |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1200 | { |
| 1201 | _PyUnicode_CONVERT_BYTES( |
| 1202 | Py_UCS1, Py_UCS2, |
| 1203 | PyUnicode_1BYTE_DATA(from) + from_start, |
| 1204 | PyUnicode_1BYTE_DATA(from) + from_start + how_many, |
| 1205 | PyUnicode_2BYTE_DATA(to) + to_start |
| 1206 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1207 | } |
Victor Stinner | 157f83f | 2011-09-28 21:41:31 +0200 | [diff] [blame] | 1208 | else if (from_kind == PyUnicode_1BYTE_KIND |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1209 | && to_kind == PyUnicode_4BYTE_KIND) |
| 1210 | { |
| 1211 | _PyUnicode_CONVERT_BYTES( |
| 1212 | Py_UCS1, Py_UCS4, |
| 1213 | PyUnicode_1BYTE_DATA(from) + from_start, |
| 1214 | PyUnicode_1BYTE_DATA(from) + from_start + how_many, |
| 1215 | PyUnicode_4BYTE_DATA(to) + to_start |
| 1216 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1217 | } |
| 1218 | else if (from_kind == PyUnicode_2BYTE_KIND |
| 1219 | && to_kind == PyUnicode_4BYTE_KIND) |
| 1220 | { |
| 1221 | _PyUnicode_CONVERT_BYTES( |
| 1222 | Py_UCS2, Py_UCS4, |
| 1223 | PyUnicode_2BYTE_DATA(from) + from_start, |
| 1224 | PyUnicode_2BYTE_DATA(from) + from_start + how_many, |
| 1225 | PyUnicode_4BYTE_DATA(to) + to_start |
| 1226 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1227 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1228 | else { |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 1229 | /* check if max_char(from substring) <= max_char(to) */ |
| 1230 | if (from_kind > to_kind |
| 1231 | /* latin1 => ascii */ |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1232 | || (!PyUnicode_IS_ASCII(from) && PyUnicode_IS_ASCII(to))) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 1233 | { |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1234 | /* slow path to check for character overflow */ |
| 1235 | const Py_UCS4 to_maxchar = PyUnicode_MAX_CHAR_VALUE(to); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1236 | Py_UCS4 ch; |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1237 | Py_ssize_t i; |
| 1238 | |
Victor Stinner | 56c161a | 2011-10-06 02:47:11 +0200 | [diff] [blame] | 1239 | #ifdef Py_DEBUG |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1240 | for (i=0; i < how_many; i++) { |
| 1241 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
Victor Stinner | 56c161a | 2011-10-06 02:47:11 +0200 | [diff] [blame] | 1242 | assert(ch <= to_maxchar); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1243 | PyUnicode_WRITE(to_kind, to_data, to_start + i, ch); |
| 1244 | } |
Victor Stinner | 56c161a | 2011-10-06 02:47:11 +0200 | [diff] [blame] | 1245 | #else |
| 1246 | if (!check_maxchar) { |
| 1247 | for (i=0; i < how_many; i++) { |
| 1248 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
| 1249 | PyUnicode_WRITE(to_kind, to_data, to_start + i, ch); |
| 1250 | } |
| 1251 | } |
| 1252 | else { |
| 1253 | for (i=0; i < how_many; i++) { |
| 1254 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
| 1255 | if (ch > to_maxchar) |
| 1256 | return 1; |
| 1257 | PyUnicode_WRITE(to_kind, to_data, to_start + i, ch); |
| 1258 | } |
| 1259 | } |
| 1260 | #endif |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1261 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1262 | else { |
Victor Stinner | 56c161a | 2011-10-06 02:47:11 +0200 | [diff] [blame] | 1263 | assert(0 && "inconsistent state"); |
| 1264 | return 1; |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1265 | } |
| 1266 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1267 | return 0; |
| 1268 | } |
| 1269 | |
| 1270 | static void |
| 1271 | copy_characters(PyObject *to, Py_ssize_t to_start, |
| 1272 | PyObject *from, Py_ssize_t from_start, |
| 1273 | Py_ssize_t how_many) |
| 1274 | { |
| 1275 | (void)_copy_characters(to, to_start, from, from_start, how_many, 0); |
| 1276 | } |
| 1277 | |
| 1278 | Py_ssize_t |
| 1279 | PyUnicode_CopyCharacters(PyObject *to, Py_ssize_t to_start, |
| 1280 | PyObject *from, Py_ssize_t from_start, |
| 1281 | Py_ssize_t how_many) |
| 1282 | { |
| 1283 | int err; |
| 1284 | |
| 1285 | if (!PyUnicode_Check(from) || !PyUnicode_Check(to)) { |
| 1286 | PyErr_BadInternalCall(); |
| 1287 | return -1; |
| 1288 | } |
| 1289 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 1290 | if (PyUnicode_READY(from) == -1) |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1291 | return -1; |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 1292 | if (PyUnicode_READY(to) == -1) |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1293 | return -1; |
| 1294 | |
| 1295 | how_many = Py_MIN(PyUnicode_GET_LENGTH(from), how_many); |
| 1296 | if (to_start + how_many > PyUnicode_GET_LENGTH(to)) { |
| 1297 | PyErr_Format(PyExc_SystemError, |
| 1298 | "Cannot write %zi characters at %zi " |
| 1299 | "in a string of %zi characters", |
| 1300 | how_many, to_start, PyUnicode_GET_LENGTH(to)); |
| 1301 | return -1; |
| 1302 | } |
| 1303 | |
| 1304 | if (how_many == 0) |
| 1305 | return 0; |
| 1306 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1307 | if (unicode_check_modifiable(to)) |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1308 | return -1; |
| 1309 | |
| 1310 | err = _copy_characters(to, to_start, from, from_start, how_many, 1); |
| 1311 | if (err) { |
| 1312 | PyErr_Format(PyExc_SystemError, |
| 1313 | "Cannot copy %s characters " |
| 1314 | "into a string of %s characters", |
| 1315 | unicode_kind_name(from), |
| 1316 | unicode_kind_name(to)); |
| 1317 | return -1; |
| 1318 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1319 | return how_many; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1320 | } |
| 1321 | |
Victor Stinner | 1722216 | 2011-09-28 22:15:37 +0200 | [diff] [blame] | 1322 | /* Find the maximum code point and count the number of surrogate pairs so a |
| 1323 | correct string length can be computed before converting a string to UCS4. |
| 1324 | This function counts single surrogates as a character and not as a pair. |
| 1325 | |
| 1326 | Return 0 on success, or -1 on error. */ |
| 1327 | static int |
| 1328 | find_maxchar_surrogates(const wchar_t *begin, const wchar_t *end, |
| 1329 | Py_UCS4 *maxchar, Py_ssize_t *num_surrogates) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1330 | { |
| 1331 | const wchar_t *iter; |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1332 | Py_UCS4 ch; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1333 | |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1334 | assert(num_surrogates != NULL && maxchar != NULL); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1335 | *num_surrogates = 0; |
| 1336 | *maxchar = 0; |
| 1337 | |
| 1338 | for (iter = begin; iter < end; ) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1339 | #if SIZEOF_WCHAR_T == 2 |
Victor Stinner | ca4f207 | 2011-11-22 03:38:40 +0100 | [diff] [blame] | 1340 | if (Py_UNICODE_IS_HIGH_SURROGATE(iter[0]) |
| 1341 | && (iter+1) < end |
| 1342 | && Py_UNICODE_IS_LOW_SURROGATE(iter[1])) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1343 | { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1344 | ch = Py_UNICODE_JOIN_SURROGATES(iter[0], iter[1]); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1345 | ++(*num_surrogates); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1346 | iter += 2; |
| 1347 | } |
| 1348 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1349 | #endif |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1350 | { |
| 1351 | ch = *iter; |
| 1352 | iter++; |
| 1353 | } |
| 1354 | if (ch > *maxchar) { |
| 1355 | *maxchar = ch; |
| 1356 | if (*maxchar > MAX_UNICODE) { |
| 1357 | PyErr_Format(PyExc_ValueError, |
| 1358 | "character U+%x is not in range [U+0000; U+10ffff]", |
| 1359 | ch); |
| 1360 | return -1; |
| 1361 | } |
| 1362 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1363 | } |
| 1364 | return 0; |
| 1365 | } |
| 1366 | |
| 1367 | #ifdef Py_DEBUG |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 1368 | static int unicode_ready_calls = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1369 | #endif |
| 1370 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1371 | int |
| 1372 | _PyUnicode_Ready(PyObject *unicode) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1373 | { |
| 1374 | wchar_t *end; |
| 1375 | Py_UCS4 maxchar = 0; |
| 1376 | Py_ssize_t num_surrogates; |
| 1377 | #if SIZEOF_WCHAR_T == 2 |
| 1378 | Py_ssize_t length_wo_surrogates; |
| 1379 | #endif |
| 1380 | |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 1381 | /* _PyUnicode_Ready() is only intended for old-style API usage where |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1382 | strings were created using _PyObject_New() and where no canonical |
| 1383 | representation (the str field) has been set yet aka strings |
| 1384 | which are not yet ready. */ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1385 | assert(_PyUnicode_CHECK(unicode)); |
| 1386 | assert(_PyUnicode_KIND(unicode) == PyUnicode_WCHAR_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1387 | assert(_PyUnicode_WSTR(unicode) != NULL); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1388 | assert(_PyUnicode_DATA_ANY(unicode) == NULL); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1389 | assert(_PyUnicode_UTF8(unicode) == NULL); |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1390 | /* Actually, it should neither be interned nor be anything else: */ |
| 1391 | assert(_PyUnicode_STATE(unicode).interned == SSTATE_NOT_INTERNED); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1392 | |
| 1393 | #ifdef Py_DEBUG |
| 1394 | ++unicode_ready_calls; |
| 1395 | #endif |
| 1396 | |
| 1397 | end = _PyUnicode_WSTR(unicode) + _PyUnicode_WSTR_LENGTH(unicode); |
Victor Stinner | 1722216 | 2011-09-28 22:15:37 +0200 | [diff] [blame] | 1398 | if (find_maxchar_surrogates(_PyUnicode_WSTR(unicode), end, |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1399 | &maxchar, &num_surrogates) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1400 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1401 | |
| 1402 | if (maxchar < 256) { |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1403 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC(_PyUnicode_WSTR_LENGTH(unicode) + 1); |
| 1404 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1405 | PyErr_NoMemory(); |
| 1406 | return -1; |
| 1407 | } |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1408 | _PyUnicode_CONVERT_BYTES(wchar_t, unsigned char, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1409 | _PyUnicode_WSTR(unicode), end, |
| 1410 | PyUnicode_1BYTE_DATA(unicode)); |
| 1411 | PyUnicode_1BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1412 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1413 | _PyUnicode_STATE(unicode).kind = PyUnicode_1BYTE_KIND; |
| 1414 | if (maxchar < 128) { |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1415 | _PyUnicode_STATE(unicode).ascii = 1; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1416 | _PyUnicode_UTF8(unicode) = _PyUnicode_DATA_ANY(unicode); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1417 | _PyUnicode_UTF8_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1418 | } |
| 1419 | else { |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1420 | _PyUnicode_STATE(unicode).ascii = 0; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1421 | _PyUnicode_UTF8(unicode) = NULL; |
| 1422 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1423 | } |
| 1424 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1425 | _PyUnicode_WSTR(unicode) = NULL; |
| 1426 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1427 | } |
| 1428 | /* In this case we might have to convert down from 4-byte native |
| 1429 | wchar_t to 2-byte unicode. */ |
| 1430 | else if (maxchar < 65536) { |
| 1431 | assert(num_surrogates == 0 && |
| 1432 | "FindMaxCharAndNumSurrogatePairs() messed up"); |
| 1433 | |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1434 | #if SIZEOF_WCHAR_T == 2 |
| 1435 | /* We can share representations and are done. */ |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1436 | _PyUnicode_DATA_ANY(unicode) = _PyUnicode_WSTR(unicode); |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1437 | PyUnicode_2BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1438 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1439 | _PyUnicode_STATE(unicode).kind = PyUnicode_2BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1440 | _PyUnicode_UTF8(unicode) = NULL; |
| 1441 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1442 | #else |
| 1443 | /* sizeof(wchar_t) == 4 */ |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1444 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC( |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1445 | 2 * (_PyUnicode_WSTR_LENGTH(unicode) + 1)); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1446 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1447 | PyErr_NoMemory(); |
| 1448 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1449 | } |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1450 | _PyUnicode_CONVERT_BYTES(wchar_t, Py_UCS2, |
| 1451 | _PyUnicode_WSTR(unicode), end, |
| 1452 | PyUnicode_2BYTE_DATA(unicode)); |
| 1453 | PyUnicode_2BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1454 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1455 | _PyUnicode_STATE(unicode).kind = PyUnicode_2BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1456 | _PyUnicode_UTF8(unicode) = NULL; |
| 1457 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1458 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1459 | _PyUnicode_WSTR(unicode) = NULL; |
| 1460 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1461 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1462 | } |
| 1463 | /* maxchar exeeds 16 bit, wee need 4 bytes for unicode characters */ |
| 1464 | else { |
| 1465 | #if SIZEOF_WCHAR_T == 2 |
| 1466 | /* in case the native representation is 2-bytes, we need to allocate a |
| 1467 | new normalized 4-byte version. */ |
| 1468 | length_wo_surrogates = _PyUnicode_WSTR_LENGTH(unicode) - num_surrogates; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1469 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC(4 * (length_wo_surrogates + 1)); |
| 1470 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1471 | PyErr_NoMemory(); |
| 1472 | return -1; |
| 1473 | } |
| 1474 | _PyUnicode_LENGTH(unicode) = length_wo_surrogates; |
| 1475 | _PyUnicode_STATE(unicode).kind = PyUnicode_4BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1476 | _PyUnicode_UTF8(unicode) = NULL; |
| 1477 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 126c559 | 2011-10-03 04:17:10 +0200 | [diff] [blame] | 1478 | /* unicode_convert_wchar_to_ucs4() requires a ready string */ |
| 1479 | _PyUnicode_STATE(unicode).ready = 1; |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1480 | unicode_convert_wchar_to_ucs4(_PyUnicode_WSTR(unicode), end, unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1481 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1482 | _PyUnicode_WSTR(unicode) = NULL; |
| 1483 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1484 | #else |
| 1485 | assert(num_surrogates == 0); |
| 1486 | |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1487 | _PyUnicode_DATA_ANY(unicode) = _PyUnicode_WSTR(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1488 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1489 | _PyUnicode_UTF8(unicode) = NULL; |
| 1490 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1491 | _PyUnicode_STATE(unicode).kind = PyUnicode_4BYTE_KIND; |
| 1492 | #endif |
| 1493 | PyUnicode_4BYTE_DATA(unicode)[_PyUnicode_LENGTH(unicode)] = '\0'; |
| 1494 | } |
| 1495 | _PyUnicode_STATE(unicode).ready = 1; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1496 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1497 | return 0; |
| 1498 | } |
| 1499 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1500 | static void |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1501 | unicode_dealloc(register PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1502 | { |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1503 | switch (PyUnicode_CHECK_INTERNED(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1504 | case SSTATE_NOT_INTERNED: |
| 1505 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1506 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1507 | case SSTATE_INTERNED_MORTAL: |
| 1508 | /* revive dead object temporarily for DelItem */ |
| 1509 | Py_REFCNT(unicode) = 3; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 1510 | if (PyDict_DelItem(interned, unicode) != 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1511 | Py_FatalError( |
| 1512 | "deletion of interned string failed"); |
| 1513 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1514 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1515 | case SSTATE_INTERNED_IMMORTAL: |
| 1516 | Py_FatalError("Immortal interned string died."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1517 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1518 | default: |
| 1519 | Py_FatalError("Inconsistent interned string state."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1520 | } |
| 1521 | |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 1522 | if (_PyUnicode_HAS_WSTR_MEMORY(unicode)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1523 | PyObject_DEL(_PyUnicode_WSTR(unicode)); |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 1524 | if (_PyUnicode_HAS_UTF8_MEMORY(unicode)) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1525 | PyObject_DEL(_PyUnicode_UTF8(unicode)); |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1526 | if (!PyUnicode_IS_COMPACT(unicode) && _PyUnicode_DATA_ANY(unicode)) |
| 1527 | PyObject_DEL(_PyUnicode_DATA_ANY(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1528 | |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1529 | Py_TYPE(unicode)->tp_free(unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1530 | } |
| 1531 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1532 | #ifdef Py_DEBUG |
| 1533 | static int |
| 1534 | unicode_is_singleton(PyObject *unicode) |
| 1535 | { |
| 1536 | PyASCIIObject *ascii = (PyASCIIObject *)unicode; |
| 1537 | if (unicode == unicode_empty) |
| 1538 | return 1; |
| 1539 | if (ascii->state.kind != PyUnicode_WCHAR_KIND && ascii->length == 1) |
| 1540 | { |
| 1541 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, 0); |
| 1542 | if (ch < 256 && unicode_latin1[ch] == unicode) |
| 1543 | return 1; |
| 1544 | } |
| 1545 | return 0; |
| 1546 | } |
| 1547 | #endif |
| 1548 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1549 | static int |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1550 | unicode_modifiable(PyObject *unicode) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1551 | { |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1552 | assert(_PyUnicode_CHECK(unicode)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1553 | if (Py_REFCNT(unicode) != 1) |
| 1554 | return 0; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1555 | if (_PyUnicode_HASH(unicode) != -1) |
| 1556 | return 0; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1557 | if (PyUnicode_CHECK_INTERNED(unicode)) |
| 1558 | return 0; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1559 | if (!PyUnicode_CheckExact(unicode)) |
| 1560 | return 0; |
Victor Stinner | 77bb47b | 2011-10-03 20:06:05 +0200 | [diff] [blame] | 1561 | #ifdef Py_DEBUG |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1562 | /* singleton refcount is greater than 1 */ |
| 1563 | assert(!unicode_is_singleton(unicode)); |
Victor Stinner | 77bb47b | 2011-10-03 20:06:05 +0200 | [diff] [blame] | 1564 | #endif |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1565 | return 1; |
| 1566 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1567 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1568 | static int |
| 1569 | unicode_resize(PyObject **p_unicode, Py_ssize_t length) |
| 1570 | { |
| 1571 | PyObject *unicode; |
| 1572 | Py_ssize_t old_length; |
| 1573 | |
| 1574 | assert(p_unicode != NULL); |
| 1575 | unicode = *p_unicode; |
| 1576 | |
| 1577 | assert(unicode != NULL); |
| 1578 | assert(PyUnicode_Check(unicode)); |
| 1579 | assert(0 <= length); |
| 1580 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1581 | if (_PyUnicode_KIND(unicode) == PyUnicode_WCHAR_KIND) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1582 | old_length = PyUnicode_WSTR_LENGTH(unicode); |
| 1583 | else |
| 1584 | old_length = PyUnicode_GET_LENGTH(unicode); |
| 1585 | if (old_length == length) |
| 1586 | return 0; |
| 1587 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1588 | if (length == 0) { |
| 1589 | Py_DECREF(*p_unicode); |
| 1590 | *p_unicode = unicode_empty; |
| 1591 | Py_INCREF(*p_unicode); |
| 1592 | return 0; |
| 1593 | } |
| 1594 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1595 | if (!unicode_modifiable(unicode)) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1596 | PyObject *copy = resize_copy(unicode, length); |
| 1597 | if (copy == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1598 | return -1; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1599 | Py_DECREF(*p_unicode); |
| 1600 | *p_unicode = copy; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1601 | return 0; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1602 | } |
| 1603 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1604 | if (PyUnicode_IS_COMPACT(unicode)) { |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1605 | PyObject *new_unicode = resize_compact(unicode, length); |
| 1606 | if (new_unicode == NULL) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1607 | return -1; |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1608 | *p_unicode = new_unicode; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1609 | return 0; |
Benjamin Peterson | 4bfce8f | 2011-10-03 19:35:07 -0400 | [diff] [blame] | 1610 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1611 | return resize_inplace(unicode, length); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1612 | } |
| 1613 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1614 | int |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1615 | PyUnicode_Resize(PyObject **p_unicode, Py_ssize_t length) |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 1616 | { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1617 | PyObject *unicode; |
| 1618 | if (p_unicode == NULL) { |
| 1619 | PyErr_BadInternalCall(); |
| 1620 | return -1; |
| 1621 | } |
| 1622 | unicode = *p_unicode; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1623 | if (unicode == NULL || !PyUnicode_Check(unicode) || length < 0) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1624 | { |
| 1625 | PyErr_BadInternalCall(); |
| 1626 | return -1; |
| 1627 | } |
| 1628 | return unicode_resize(p_unicode, length); |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 1629 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1630 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1631 | static int |
Victor Stinner | 1b487b4 | 2012-05-03 12:29:04 +0200 | [diff] [blame] | 1632 | unicode_widen(PyObject **p_unicode, Py_ssize_t length, |
| 1633 | unsigned int maxchar) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1634 | { |
| 1635 | PyObject *result; |
| 1636 | assert(PyUnicode_IS_READY(*p_unicode)); |
Victor Stinner | 1b487b4 | 2012-05-03 12:29:04 +0200 | [diff] [blame] | 1637 | assert(length <= PyUnicode_GET_LENGTH(*p_unicode)); |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1638 | if (maxchar <= PyUnicode_MAX_CHAR_VALUE(*p_unicode)) |
| 1639 | return 0; |
| 1640 | result = PyUnicode_New(PyUnicode_GET_LENGTH(*p_unicode), |
| 1641 | maxchar); |
| 1642 | if (result == NULL) |
| 1643 | return -1; |
Victor Stinner | 1b487b4 | 2012-05-03 12:29:04 +0200 | [diff] [blame] | 1644 | PyUnicode_CopyCharacters(result, 0, *p_unicode, 0, length); |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1645 | Py_DECREF(*p_unicode); |
| 1646 | *p_unicode = result; |
| 1647 | return 0; |
| 1648 | } |
| 1649 | |
| 1650 | static int |
| 1651 | unicode_putchar(PyObject **p_unicode, Py_ssize_t *pos, |
| 1652 | Py_UCS4 ch) |
| 1653 | { |
Victor Stinner | 15e9ed2 | 2012-02-22 13:36:20 +0100 | [diff] [blame] | 1654 | assert(ch <= MAX_UNICODE); |
Victor Stinner | 1b487b4 | 2012-05-03 12:29:04 +0200 | [diff] [blame] | 1655 | if (unicode_widen(p_unicode, *pos, ch) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1656 | return -1; |
| 1657 | PyUnicode_WRITE(PyUnicode_KIND(*p_unicode), |
| 1658 | PyUnicode_DATA(*p_unicode), |
| 1659 | (*pos)++, ch); |
| 1660 | return 0; |
| 1661 | } |
| 1662 | |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1663 | /* Copy a ASCII or latin1 char* string into a Python Unicode string. |
| 1664 | Return the length of the input string. |
| 1665 | |
Victor Stinner | b429d3b | 2012-02-22 21:22:20 +0100 | [diff] [blame] | 1666 | WARNING: The function doesn't copy the terminating null character and |
| 1667 | doesn't check the maximum character (may write a latin1 character in an |
| 1668 | ASCII string). */ |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1669 | static Py_ssize_t |
| 1670 | unicode_write_cstr(PyObject *unicode, Py_ssize_t index, const char *str) |
| 1671 | { |
| 1672 | enum PyUnicode_Kind kind = PyUnicode_KIND(unicode); |
| 1673 | void *data = PyUnicode_DATA(unicode); |
| 1674 | |
| 1675 | switch (kind) { |
| 1676 | case PyUnicode_1BYTE_KIND: { |
| 1677 | Py_ssize_t len = strlen(str); |
| 1678 | assert(index + len <= PyUnicode_GET_LENGTH(unicode)); |
Antoine Pitrou | ba6bafc | 2012-02-22 16:41:50 +0100 | [diff] [blame] | 1679 | memcpy((char *) data + index, str, len); |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1680 | return len; |
| 1681 | } |
| 1682 | case PyUnicode_2BYTE_KIND: { |
| 1683 | Py_UCS2 *start = (Py_UCS2 *)data + index; |
| 1684 | Py_UCS2 *ucs2 = start; |
| 1685 | assert(index <= PyUnicode_GET_LENGTH(unicode)); |
| 1686 | |
| 1687 | for (; *str; ++ucs2, ++str) |
| 1688 | *ucs2 = (Py_UCS2)*str; |
| 1689 | |
| 1690 | assert((ucs2 - start) <= PyUnicode_GET_LENGTH(unicode)); |
| 1691 | return ucs2 - start; |
| 1692 | } |
| 1693 | default: { |
| 1694 | Py_UCS4 *start = (Py_UCS4 *)data + index; |
| 1695 | Py_UCS4 *ucs4 = start; |
| 1696 | assert(kind == PyUnicode_4BYTE_KIND); |
| 1697 | assert(index <= PyUnicode_GET_LENGTH(unicode)); |
| 1698 | |
| 1699 | for (; *str; ++ucs4, ++str) |
| 1700 | *ucs4 = (Py_UCS4)*str; |
| 1701 | |
| 1702 | assert((ucs4 - start) <= PyUnicode_GET_LENGTH(unicode)); |
| 1703 | return ucs4 - start; |
| 1704 | } |
| 1705 | } |
| 1706 | } |
| 1707 | |
| 1708 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1709 | static PyObject* |
| 1710 | get_latin1_char(unsigned char ch) |
| 1711 | { |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1712 | PyObject *unicode = unicode_latin1[ch]; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1713 | if (!unicode) { |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1714 | unicode = PyUnicode_New(1, ch); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1715 | if (!unicode) |
| 1716 | return NULL; |
| 1717 | PyUnicode_1BYTE_DATA(unicode)[0] = ch; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1718 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1719 | unicode_latin1[ch] = unicode; |
| 1720 | } |
| 1721 | Py_INCREF(unicode); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1722 | return unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1723 | } |
| 1724 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1725 | PyObject * |
| 1726 | PyUnicode_FromUnicode(const Py_UNICODE *u, Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1727 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1728 | PyObject *unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1729 | Py_UCS4 maxchar = 0; |
| 1730 | Py_ssize_t num_surrogates; |
| 1731 | |
| 1732 | if (u == NULL) |
| 1733 | return (PyObject*)_PyUnicode_New(size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1734 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1735 | /* If the Unicode data is known at construction time, we can apply |
| 1736 | some optimizations which share commonly used objects. */ |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1737 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1738 | /* Optimization for empty strings */ |
| 1739 | if (size == 0 && unicode_empty != NULL) { |
| 1740 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1741 | return unicode_empty; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1742 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1743 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1744 | /* Single character Unicode objects in the Latin-1 range are |
| 1745 | shared when using this constructor */ |
| 1746 | if (size == 1 && *u < 256) |
| 1747 | return get_latin1_char((unsigned char)*u); |
| 1748 | |
| 1749 | /* If not empty and not single character, copy the Unicode data |
| 1750 | into the new object */ |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1751 | if (find_maxchar_surrogates(u, u + size, |
| 1752 | &maxchar, &num_surrogates) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1753 | return NULL; |
| 1754 | |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1755 | unicode = PyUnicode_New(size - num_surrogates, maxchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1756 | if (!unicode) |
| 1757 | return NULL; |
| 1758 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1759 | switch (PyUnicode_KIND(unicode)) { |
| 1760 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1761 | _PyUnicode_CONVERT_BYTES(Py_UNICODE, unsigned char, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1762 | u, u + size, PyUnicode_1BYTE_DATA(unicode)); |
| 1763 | break; |
| 1764 | case PyUnicode_2BYTE_KIND: |
| 1765 | #if Py_UNICODE_SIZE == 2 |
| 1766 | Py_MEMCPY(PyUnicode_2BYTE_DATA(unicode), u, size * 2); |
| 1767 | #else |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1768 | _PyUnicode_CONVERT_BYTES(Py_UNICODE, Py_UCS2, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1769 | u, u + size, PyUnicode_2BYTE_DATA(unicode)); |
| 1770 | #endif |
| 1771 | break; |
| 1772 | case PyUnicode_4BYTE_KIND: |
| 1773 | #if SIZEOF_WCHAR_T == 2 |
| 1774 | /* This is the only case which has to process surrogates, thus |
| 1775 | a simple copy loop is not enough and we need a function. */ |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1776 | unicode_convert_wchar_to_ucs4(u, u + size, unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1777 | #else |
| 1778 | assert(num_surrogates == 0); |
| 1779 | Py_MEMCPY(PyUnicode_4BYTE_DATA(unicode), u, size * 4); |
| 1780 | #endif |
| 1781 | break; |
| 1782 | default: |
| 1783 | assert(0 && "Impossible state"); |
| 1784 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1785 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1786 | return unicode_result(unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1787 | } |
| 1788 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1789 | PyObject * |
| 1790 | PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size) |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1791 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1792 | if (size < 0) { |
| 1793 | PyErr_SetString(PyExc_SystemError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1794 | "Negative size passed to PyUnicode_FromStringAndSize"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1795 | return NULL; |
| 1796 | } |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 1797 | if (u != NULL) |
| 1798 | return PyUnicode_DecodeUTF8Stateful(u, size, NULL, NULL); |
| 1799 | else |
| 1800 | return (PyObject *)_PyUnicode_New(size); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1801 | } |
| 1802 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1803 | PyObject * |
| 1804 | PyUnicode_FromString(const char *u) |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1805 | { |
| 1806 | size_t size = strlen(u); |
| 1807 | if (size > PY_SSIZE_T_MAX) { |
| 1808 | PyErr_SetString(PyExc_OverflowError, "input too long"); |
| 1809 | return NULL; |
| 1810 | } |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 1811 | return PyUnicode_DecodeUTF8Stateful(u, (Py_ssize_t)size, NULL, NULL); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1812 | } |
| 1813 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1814 | PyObject * |
| 1815 | _PyUnicode_FromId(_Py_Identifier *id) |
| 1816 | { |
| 1817 | if (!id->object) { |
Victor Stinner | d1cd99b | 2012-02-07 23:05:55 +0100 | [diff] [blame] | 1818 | id->object = PyUnicode_DecodeUTF8Stateful(id->string, |
| 1819 | strlen(id->string), |
| 1820 | NULL, NULL); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1821 | if (!id->object) |
| 1822 | return NULL; |
| 1823 | PyUnicode_InternInPlace(&id->object); |
| 1824 | assert(!id->next); |
| 1825 | id->next = static_strings; |
| 1826 | static_strings = id; |
| 1827 | } |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1828 | return id->object; |
| 1829 | } |
| 1830 | |
| 1831 | void |
| 1832 | _PyUnicode_ClearStaticStrings() |
| 1833 | { |
| 1834 | _Py_Identifier *i; |
| 1835 | for (i = static_strings; i; i = i->next) { |
| 1836 | Py_DECREF(i->object); |
| 1837 | i->object = NULL; |
| 1838 | i->next = NULL; |
| 1839 | } |
| 1840 | } |
| 1841 | |
Benjamin Peterson | 0df5429 | 2012-03-26 14:50:32 -0400 | [diff] [blame] | 1842 | /* Internal function, doesn't check maximum character */ |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1843 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1844 | static PyObject* |
Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 1845 | unicode_fromascii(const unsigned char* s, Py_ssize_t size) |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1846 | { |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 1847 | PyObject *unicode; |
Victor Stinner | e6b2d44 | 2011-12-11 21:54:30 +0100 | [diff] [blame] | 1848 | if (size == 1) { |
Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 1849 | #ifdef Py_DEBUG |
Victor Stinner | e6b2d44 | 2011-12-11 21:54:30 +0100 | [diff] [blame] | 1850 | assert(s[0] < 128); |
Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 1851 | #endif |
Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 1852 | return get_latin1_char(s[0]); |
Victor Stinner | e6b2d44 | 2011-12-11 21:54:30 +0100 | [diff] [blame] | 1853 | } |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 1854 | unicode = PyUnicode_New(size, 127); |
| 1855 | if (!unicode) |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1856 | return NULL; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 1857 | memcpy(PyUnicode_1BYTE_DATA(unicode), s, size); |
| 1858 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
| 1859 | return unicode; |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1860 | } |
| 1861 | |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 1862 | static Py_UCS4 |
| 1863 | kind_maxchar_limit(unsigned int kind) |
| 1864 | { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 1865 | switch (kind) { |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 1866 | case PyUnicode_1BYTE_KIND: |
| 1867 | return 0x80; |
| 1868 | case PyUnicode_2BYTE_KIND: |
| 1869 | return 0x100; |
| 1870 | case PyUnicode_4BYTE_KIND: |
| 1871 | return 0x10000; |
| 1872 | default: |
| 1873 | assert(0 && "invalid kind"); |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1874 | return MAX_UNICODE; |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 1875 | } |
| 1876 | } |
| 1877 | |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 1878 | Py_LOCAL_INLINE(Py_UCS4) |
| 1879 | align_maxchar(Py_UCS4 maxchar) |
| 1880 | { |
| 1881 | if (maxchar <= 127) |
| 1882 | return 127; |
| 1883 | else if (maxchar <= 255) |
| 1884 | return 255; |
| 1885 | else if (maxchar <= 65535) |
| 1886 | return 65535; |
| 1887 | else |
| 1888 | return MAX_UNICODE; |
| 1889 | } |
| 1890 | |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1891 | static PyObject* |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1892 | _PyUnicode_FromUCS1(const unsigned char* u, Py_ssize_t size) |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1893 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1894 | PyObject *res; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1895 | unsigned char max_char; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1896 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1897 | if (size == 0) { |
| 1898 | Py_INCREF(unicode_empty); |
| 1899 | return unicode_empty; |
| 1900 | } |
| 1901 | assert(size > 0); |
Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 1902 | if (size == 1) |
| 1903 | return get_latin1_char(u[0]); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1904 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1905 | max_char = ucs1lib_find_max_char(u, u + size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1906 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1907 | if (!res) |
| 1908 | return NULL; |
| 1909 | memcpy(PyUnicode_1BYTE_DATA(res), u, size); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1910 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1911 | return res; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1912 | } |
| 1913 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1914 | static PyObject* |
| 1915 | _PyUnicode_FromUCS2(const Py_UCS2 *u, Py_ssize_t size) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1916 | { |
| 1917 | PyObject *res; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1918 | Py_UCS2 max_char; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1919 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1920 | if (size == 0) { |
| 1921 | Py_INCREF(unicode_empty); |
| 1922 | return unicode_empty; |
| 1923 | } |
| 1924 | assert(size > 0); |
Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 1925 | if (size == 1) { |
| 1926 | Py_UCS4 ch = u[0]; |
| 1927 | if (ch < 256) |
| 1928 | return get_latin1_char((unsigned char)ch); |
| 1929 | |
| 1930 | res = PyUnicode_New(1, ch); |
| 1931 | if (res == NULL) |
| 1932 | return NULL; |
| 1933 | PyUnicode_WRITE(PyUnicode_KIND(res), PyUnicode_DATA(res), 0, ch); |
| 1934 | assert(_PyUnicode_CheckConsistency(res, 1)); |
| 1935 | return res; |
| 1936 | } |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1937 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1938 | max_char = ucs2lib_find_max_char(u, u + size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1939 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1940 | if (!res) |
| 1941 | return NULL; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1942 | if (max_char >= 256) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1943 | memcpy(PyUnicode_2BYTE_DATA(res), u, sizeof(Py_UCS2)*size); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1944 | else { |
| 1945 | _PyUnicode_CONVERT_BYTES( |
| 1946 | Py_UCS2, Py_UCS1, u, u + size, PyUnicode_1BYTE_DATA(res)); |
| 1947 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1948 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1949 | return res; |
| 1950 | } |
| 1951 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1952 | static PyObject* |
| 1953 | _PyUnicode_FromUCS4(const Py_UCS4 *u, Py_ssize_t size) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1954 | { |
| 1955 | PyObject *res; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1956 | Py_UCS4 max_char; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1957 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1958 | if (size == 0) { |
| 1959 | Py_INCREF(unicode_empty); |
| 1960 | return unicode_empty; |
| 1961 | } |
| 1962 | assert(size > 0); |
Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 1963 | if (size == 1) { |
| 1964 | Py_UCS4 ch = u[0]; |
| 1965 | if (ch < 256) |
| 1966 | return get_latin1_char((unsigned char)ch); |
| 1967 | |
| 1968 | res = PyUnicode_New(1, ch); |
| 1969 | if (res == NULL) |
| 1970 | return NULL; |
| 1971 | PyUnicode_WRITE(PyUnicode_KIND(res), PyUnicode_DATA(res), 0, ch); |
| 1972 | assert(_PyUnicode_CheckConsistency(res, 1)); |
| 1973 | return res; |
| 1974 | } |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1975 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1976 | max_char = ucs4lib_find_max_char(u, u + size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1977 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1978 | if (!res) |
| 1979 | return NULL; |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 1980 | if (max_char < 256) |
| 1981 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS1, u, u + size, |
| 1982 | PyUnicode_1BYTE_DATA(res)); |
| 1983 | else if (max_char < 0x10000) |
| 1984 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS2, u, u + size, |
| 1985 | PyUnicode_2BYTE_DATA(res)); |
| 1986 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1987 | memcpy(PyUnicode_4BYTE_DATA(res), u, sizeof(Py_UCS4)*size); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1988 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1989 | return res; |
| 1990 | } |
| 1991 | |
| 1992 | PyObject* |
| 1993 | PyUnicode_FromKindAndData(int kind, const void *buffer, Py_ssize_t size) |
| 1994 | { |
Victor Stinner | cfed46e | 2011-11-22 01:29:14 +0100 | [diff] [blame] | 1995 | if (size < 0) { |
| 1996 | PyErr_SetString(PyExc_ValueError, "size must be positive"); |
| 1997 | return NULL; |
| 1998 | } |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 1999 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2000 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 2001 | return _PyUnicode_FromUCS1(buffer, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2002 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 2003 | return _PyUnicode_FromUCS2(buffer, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2004 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 2005 | return _PyUnicode_FromUCS4(buffer, size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 2006 | default: |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 2007 | PyErr_SetString(PyExc_SystemError, "invalid kind"); |
| 2008 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2009 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2010 | } |
| 2011 | |
Victor Stinner | ece58de | 2012-04-23 23:36:38 +0200 | [diff] [blame] | 2012 | Py_UCS4 |
| 2013 | _PyUnicode_FindMaxChar(PyObject *unicode, Py_ssize_t start, Py_ssize_t end) |
| 2014 | { |
| 2015 | enum PyUnicode_Kind kind; |
| 2016 | void *startptr, *endptr; |
| 2017 | |
| 2018 | assert(PyUnicode_IS_READY(unicode)); |
| 2019 | assert(0 <= start); |
| 2020 | assert(end <= PyUnicode_GET_LENGTH(unicode)); |
| 2021 | assert(start <= end); |
| 2022 | |
| 2023 | if (start == 0 && end == PyUnicode_GET_LENGTH(unicode)) |
| 2024 | return PyUnicode_MAX_CHAR_VALUE(unicode); |
| 2025 | |
| 2026 | if (start == end) |
| 2027 | return 127; |
| 2028 | |
Victor Stinner | 94d558b | 2012-04-27 22:26:58 +0200 | [diff] [blame] | 2029 | if (PyUnicode_IS_ASCII(unicode)) |
| 2030 | return 127; |
| 2031 | |
Victor Stinner | ece58de | 2012-04-23 23:36:38 +0200 | [diff] [blame] | 2032 | kind = PyUnicode_KIND(unicode); |
Benjamin Peterson | f3b7d86 | 2012-04-23 18:07:01 -0400 | [diff] [blame] | 2033 | startptr = PyUnicode_DATA(unicode); |
Benjamin Peterson | b9f4c9d | 2012-04-23 21:45:40 -0400 | [diff] [blame] | 2034 | endptr = (char *)startptr + end * kind; |
| 2035 | startptr = (char *)startptr + start * kind; |
Benjamin Peterson | 2844a7a | 2012-04-23 18:00:25 -0400 | [diff] [blame] | 2036 | switch(kind) { |
| 2037 | case PyUnicode_1BYTE_KIND: |
| 2038 | return ucs1lib_find_max_char(startptr, endptr); |
| 2039 | case PyUnicode_2BYTE_KIND: |
| 2040 | return ucs2lib_find_max_char(startptr, endptr); |
| 2041 | case PyUnicode_4BYTE_KIND: |
| 2042 | return ucs4lib_find_max_char(startptr, endptr); |
Victor Stinner | ece58de | 2012-04-23 23:36:38 +0200 | [diff] [blame] | 2043 | default: |
Benjamin Peterson | 2844a7a | 2012-04-23 18:00:25 -0400 | [diff] [blame] | 2044 | assert(0); |
| 2045 | return 0; |
Victor Stinner | ece58de | 2012-04-23 23:36:38 +0200 | [diff] [blame] | 2046 | } |
| 2047 | } |
| 2048 | |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2049 | /* Ensure that a string uses the most efficient storage, if it is not the |
| 2050 | case: create a new string with of the right kind. Write NULL into *p_unicode |
| 2051 | on error. */ |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 2052 | static void |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2053 | unicode_adjust_maxchar(PyObject **p_unicode) |
| 2054 | { |
| 2055 | PyObject *unicode, *copy; |
| 2056 | Py_UCS4 max_char; |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2057 | Py_ssize_t len; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2058 | unsigned int kind; |
| 2059 | |
| 2060 | assert(p_unicode != NULL); |
| 2061 | unicode = *p_unicode; |
| 2062 | assert(PyUnicode_IS_READY(unicode)); |
| 2063 | if (PyUnicode_IS_ASCII(unicode)) |
| 2064 | return; |
| 2065 | |
| 2066 | len = PyUnicode_GET_LENGTH(unicode); |
| 2067 | kind = PyUnicode_KIND(unicode); |
| 2068 | if (kind == PyUnicode_1BYTE_KIND) { |
| 2069 | const Py_UCS1 *u = PyUnicode_1BYTE_DATA(unicode); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2070 | max_char = ucs1lib_find_max_char(u, u + len); |
| 2071 | if (max_char >= 128) |
| 2072 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2073 | } |
| 2074 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 2075 | const Py_UCS2 *u = PyUnicode_2BYTE_DATA(unicode); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2076 | max_char = ucs2lib_find_max_char(u, u + len); |
| 2077 | if (max_char >= 256) |
| 2078 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2079 | } |
| 2080 | else { |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2081 | const Py_UCS4 *u = PyUnicode_4BYTE_DATA(unicode); |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2082 | assert(kind == PyUnicode_4BYTE_KIND); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2083 | max_char = ucs4lib_find_max_char(u, u + len); |
| 2084 | if (max_char >= 0x10000) |
| 2085 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2086 | } |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2087 | copy = PyUnicode_New(len, max_char); |
| 2088 | copy_characters(copy, 0, unicode, 0, len); |
| 2089 | Py_DECREF(unicode); |
| 2090 | *p_unicode = copy; |
| 2091 | } |
| 2092 | |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2093 | PyObject* |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 2094 | _PyUnicode_Copy(PyObject *unicode) |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2095 | { |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 2096 | Py_ssize_t length; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2097 | PyObject *copy; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2098 | |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2099 | if (!PyUnicode_Check(unicode)) { |
| 2100 | PyErr_BadInternalCall(); |
| 2101 | return NULL; |
| 2102 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 2103 | if (PyUnicode_READY(unicode) == -1) |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2104 | return NULL; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2105 | |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 2106 | length = PyUnicode_GET_LENGTH(unicode); |
| 2107 | copy = PyUnicode_New(length, PyUnicode_MAX_CHAR_VALUE(unicode)); |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2108 | if (!copy) |
| 2109 | return NULL; |
| 2110 | assert(PyUnicode_KIND(copy) == PyUnicode_KIND(unicode)); |
| 2111 | |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 2112 | Py_MEMCPY(PyUnicode_DATA(copy), PyUnicode_DATA(unicode), |
| 2113 | length * PyUnicode_KIND(unicode)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2114 | assert(_PyUnicode_CheckConsistency(copy, 1)); |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2115 | return copy; |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2116 | } |
| 2117 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2118 | |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2119 | /* Widen Unicode objects to larger buffers. Don't write terminating null |
| 2120 | character. Return NULL on error. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2121 | |
| 2122 | void* |
| 2123 | _PyUnicode_AsKind(PyObject *s, unsigned int kind) |
| 2124 | { |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2125 | Py_ssize_t len; |
| 2126 | void *result; |
| 2127 | unsigned int skind; |
| 2128 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 2129 | if (PyUnicode_READY(s) == -1) |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2130 | return NULL; |
| 2131 | |
| 2132 | len = PyUnicode_GET_LENGTH(s); |
| 2133 | skind = PyUnicode_KIND(s); |
| 2134 | if (skind >= kind) { |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 2135 | PyErr_SetString(PyExc_SystemError, "invalid widening attempt"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2136 | return NULL; |
| 2137 | } |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 2138 | switch (kind) { |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2139 | case PyUnicode_2BYTE_KIND: |
| 2140 | result = PyMem_Malloc(len * sizeof(Py_UCS2)); |
| 2141 | if (!result) |
| 2142 | return PyErr_NoMemory(); |
| 2143 | assert(skind == PyUnicode_1BYTE_KIND); |
| 2144 | _PyUnicode_CONVERT_BYTES( |
| 2145 | Py_UCS1, Py_UCS2, |
| 2146 | PyUnicode_1BYTE_DATA(s), |
| 2147 | PyUnicode_1BYTE_DATA(s) + len, |
| 2148 | result); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2149 | return result; |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2150 | case PyUnicode_4BYTE_KIND: |
| 2151 | result = PyMem_Malloc(len * sizeof(Py_UCS4)); |
| 2152 | if (!result) |
| 2153 | return PyErr_NoMemory(); |
| 2154 | if (skind == PyUnicode_2BYTE_KIND) { |
| 2155 | _PyUnicode_CONVERT_BYTES( |
| 2156 | Py_UCS2, Py_UCS4, |
| 2157 | PyUnicode_2BYTE_DATA(s), |
| 2158 | PyUnicode_2BYTE_DATA(s) + len, |
| 2159 | result); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2160 | } |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2161 | else { |
| 2162 | assert(skind == PyUnicode_1BYTE_KIND); |
| 2163 | _PyUnicode_CONVERT_BYTES( |
| 2164 | Py_UCS1, Py_UCS4, |
| 2165 | PyUnicode_1BYTE_DATA(s), |
| 2166 | PyUnicode_1BYTE_DATA(s) + len, |
| 2167 | result); |
| 2168 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2169 | return result; |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2170 | default: |
| 2171 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2172 | } |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 2173 | PyErr_SetString(PyExc_SystemError, "invalid kind"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2174 | return NULL; |
| 2175 | } |
| 2176 | |
| 2177 | static Py_UCS4* |
| 2178 | as_ucs4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, |
| 2179 | int copy_null) |
| 2180 | { |
| 2181 | int kind; |
| 2182 | void *data; |
| 2183 | Py_ssize_t len, targetlen; |
| 2184 | if (PyUnicode_READY(string) == -1) |
| 2185 | return NULL; |
| 2186 | kind = PyUnicode_KIND(string); |
| 2187 | data = PyUnicode_DATA(string); |
| 2188 | len = PyUnicode_GET_LENGTH(string); |
| 2189 | targetlen = len; |
| 2190 | if (copy_null) |
| 2191 | targetlen++; |
| 2192 | if (!target) { |
| 2193 | if (PY_SSIZE_T_MAX / sizeof(Py_UCS4) < targetlen) { |
| 2194 | PyErr_NoMemory(); |
| 2195 | return NULL; |
| 2196 | } |
| 2197 | target = PyMem_Malloc(targetlen * sizeof(Py_UCS4)); |
| 2198 | if (!target) { |
| 2199 | PyErr_NoMemory(); |
| 2200 | return NULL; |
| 2201 | } |
| 2202 | } |
| 2203 | else { |
| 2204 | if (targetsize < targetlen) { |
| 2205 | PyErr_Format(PyExc_SystemError, |
| 2206 | "string is longer than the buffer"); |
| 2207 | if (copy_null && 0 < targetsize) |
| 2208 | target[0] = 0; |
| 2209 | return NULL; |
| 2210 | } |
| 2211 | } |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2212 | if (kind == PyUnicode_1BYTE_KIND) { |
| 2213 | Py_UCS1 *start = (Py_UCS1 *) data; |
| 2214 | _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] | 2215 | } |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2216 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 2217 | Py_UCS2 *start = (Py_UCS2 *) data; |
| 2218 | _PyUnicode_CONVERT_BYTES(Py_UCS2, Py_UCS4, start, start + len, target); |
| 2219 | } |
| 2220 | else { |
| 2221 | assert(kind == PyUnicode_4BYTE_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2222 | Py_MEMCPY(target, data, len * sizeof(Py_UCS4)); |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2223 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2224 | if (copy_null) |
| 2225 | target[len] = 0; |
| 2226 | return target; |
| 2227 | } |
| 2228 | |
| 2229 | Py_UCS4* |
| 2230 | PyUnicode_AsUCS4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, |
| 2231 | int copy_null) |
| 2232 | { |
Antoine Pitrou | de20b0b | 2011-11-10 21:47:38 +0100 | [diff] [blame] | 2233 | if (target == NULL || targetsize < 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2234 | PyErr_BadInternalCall(); |
| 2235 | return NULL; |
| 2236 | } |
| 2237 | return as_ucs4(string, target, targetsize, copy_null); |
| 2238 | } |
| 2239 | |
| 2240 | Py_UCS4* |
| 2241 | PyUnicode_AsUCS4Copy(PyObject *string) |
| 2242 | { |
| 2243 | return as_ucs4(string, NULL, 0, 1); |
| 2244 | } |
| 2245 | |
| 2246 | #ifdef HAVE_WCHAR_H |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 2247 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2248 | PyObject * |
| 2249 | PyUnicode_FromWideChar(register const wchar_t *w, Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2250 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2251 | if (w == NULL) { |
Victor Stinner | 382955f | 2011-12-11 21:44:00 +0100 | [diff] [blame] | 2252 | if (size == 0) { |
| 2253 | Py_INCREF(unicode_empty); |
| 2254 | return unicode_empty; |
| 2255 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2256 | PyErr_BadInternalCall(); |
| 2257 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2258 | } |
| 2259 | |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 2260 | if (size == -1) { |
| 2261 | size = wcslen(w); |
| 2262 | } |
| 2263 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2264 | return PyUnicode_FromUnicode(w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2265 | } |
| 2266 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2267 | #endif /* HAVE_WCHAR_H */ |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 2268 | |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2269 | static void |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2270 | makefmt(char *fmt, int longflag, int longlongflag, int size_tflag, |
| 2271 | int zeropad, int width, int precision, char c) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2272 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2273 | *fmt++ = '%'; |
| 2274 | if (width) { |
| 2275 | if (zeropad) |
| 2276 | *fmt++ = '0'; |
| 2277 | fmt += sprintf(fmt, "%d", width); |
| 2278 | } |
| 2279 | if (precision) |
| 2280 | fmt += sprintf(fmt, ".%d", precision); |
| 2281 | if (longflag) |
| 2282 | *fmt++ = 'l'; |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2283 | else if (longlongflag) { |
| 2284 | /* longlongflag should only ever be nonzero on machines with |
| 2285 | HAVE_LONG_LONG defined */ |
| 2286 | #ifdef HAVE_LONG_LONG |
| 2287 | char *f = PY_FORMAT_LONG_LONG; |
| 2288 | while (*f) |
| 2289 | *fmt++ = *f++; |
| 2290 | #else |
| 2291 | /* we shouldn't ever get here */ |
| 2292 | assert(0); |
| 2293 | *fmt++ = 'l'; |
| 2294 | #endif |
| 2295 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2296 | else if (size_tflag) { |
| 2297 | char *f = PY_FORMAT_SIZE_T; |
| 2298 | while (*f) |
| 2299 | *fmt++ = *f++; |
| 2300 | } |
| 2301 | *fmt++ = c; |
| 2302 | *fmt = '\0'; |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2303 | } |
| 2304 | |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2305 | /* helper for PyUnicode_FromFormatV() */ |
| 2306 | |
| 2307 | static const char* |
| 2308 | parse_format_flags(const char *f, |
| 2309 | int *p_width, int *p_precision, |
| 2310 | int *p_longflag, int *p_longlongflag, int *p_size_tflag) |
| 2311 | { |
| 2312 | int width, precision, longflag, longlongflag, size_tflag; |
| 2313 | |
| 2314 | /* parse the width.precision part, e.g. "%2.5s" => width=2, precision=5 */ |
| 2315 | f++; |
| 2316 | width = 0; |
| 2317 | while (Py_ISDIGIT((unsigned)*f)) |
| 2318 | width = (width*10) + *f++ - '0'; |
| 2319 | precision = 0; |
| 2320 | if (*f == '.') { |
| 2321 | f++; |
| 2322 | while (Py_ISDIGIT((unsigned)*f)) |
| 2323 | precision = (precision*10) + *f++ - '0'; |
| 2324 | if (*f == '%') { |
| 2325 | /* "%.3%s" => f points to "3" */ |
| 2326 | f--; |
| 2327 | } |
| 2328 | } |
| 2329 | if (*f == '\0') { |
| 2330 | /* bogus format "%.1" => go backward, f points to "1" */ |
| 2331 | f--; |
| 2332 | } |
| 2333 | if (p_width != NULL) |
| 2334 | *p_width = width; |
| 2335 | if (p_precision != NULL) |
| 2336 | *p_precision = precision; |
| 2337 | |
| 2338 | /* Handle %ld, %lu, %lld and %llu. */ |
| 2339 | longflag = 0; |
| 2340 | longlongflag = 0; |
Victor Stinner | e7faec1 | 2011-03-02 00:01:53 +0000 | [diff] [blame] | 2341 | size_tflag = 0; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2342 | |
| 2343 | if (*f == 'l') { |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2344 | if (f[1] == 'd' || f[1] == 'u' || f[1] == 'i') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2345 | longflag = 1; |
| 2346 | ++f; |
| 2347 | } |
| 2348 | #ifdef HAVE_LONG_LONG |
| 2349 | else if (f[1] == 'l' && |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2350 | (f[2] == 'd' || f[2] == 'u' || f[2] == 'i')) { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2351 | longlongflag = 1; |
| 2352 | f += 2; |
| 2353 | } |
| 2354 | #endif |
| 2355 | } |
| 2356 | /* handle the size_t flag. */ |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2357 | 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] | 2358 | size_tflag = 1; |
| 2359 | ++f; |
| 2360 | } |
| 2361 | if (p_longflag != NULL) |
| 2362 | *p_longflag = longflag; |
| 2363 | if (p_longlongflag != NULL) |
| 2364 | *p_longlongflag = longlongflag; |
| 2365 | if (p_size_tflag != NULL) |
| 2366 | *p_size_tflag = size_tflag; |
| 2367 | return f; |
| 2368 | } |
| 2369 | |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2370 | /* maximum number of characters required for output of %ld. 21 characters |
| 2371 | allows for 64-bit integers (in decimal) and an optional sign. */ |
| 2372 | #define MAX_LONG_CHARS 21 |
| 2373 | /* maximum number of characters required for output of %lld. |
| 2374 | We need at most ceil(log10(256)*SIZEOF_LONG_LONG) digits, |
| 2375 | plus 1 for the sign. 53/22 is an upper bound for log10(256). */ |
| 2376 | #define MAX_LONG_LONG_CHARS (2 + (SIZEOF_LONG_LONG*53-1) / 22) |
| 2377 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2378 | PyObject * |
| 2379 | PyUnicode_FromFormatV(const char *format, va_list vargs) |
| 2380 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2381 | va_list count; |
| 2382 | Py_ssize_t callcount = 0; |
| 2383 | PyObject **callresults = NULL; |
| 2384 | PyObject **callresult = NULL; |
| 2385 | Py_ssize_t n = 0; |
| 2386 | int width = 0; |
| 2387 | int precision = 0; |
| 2388 | int zeropad; |
| 2389 | const char* f; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2390 | PyObject *string; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2391 | /* used by sprintf */ |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2392 | char fmt[61]; /* should be enough for %0width.precisionlld */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2393 | Py_UCS4 maxchar = 127; /* result is ASCII by default */ |
| 2394 | Py_UCS4 argmaxchar; |
| 2395 | Py_ssize_t numbersize = 0; |
| 2396 | char *numberresults = NULL; |
| 2397 | char *numberresult = NULL; |
| 2398 | Py_ssize_t i; |
| 2399 | int kind; |
| 2400 | void *data; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2401 | |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 2402 | Py_VA_COPY(count, vargs); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2403 | /* step 1: count the number of %S/%R/%A/%s format specifications |
| 2404 | * (we call PyObject_Str()/PyObject_Repr()/PyObject_ASCII()/ |
| 2405 | * 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] | 2406 | * result in an array) |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 2407 | * also estimate a upper bound for all the number formats in the string, |
| 2408 | * 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] | 2409 | * buffer before putting everything together. */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2410 | for (f = format; *f; f++) { |
| 2411 | if (*f == '%') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2412 | int longlongflag; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2413 | /* skip width or width.precision (eg. "1.2" of "%1.2f") */ |
| 2414 | f = parse_format_flags(f, &width, NULL, NULL, &longlongflag, NULL); |
| 2415 | if (*f == 's' || *f=='S' || *f=='R' || *f=='A' || *f=='V') |
| 2416 | ++callcount; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2417 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2418 | else if (*f == 'd' || *f=='u' || *f=='i' || *f=='x' || *f=='p') { |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2419 | #ifdef HAVE_LONG_LONG |
| 2420 | if (longlongflag) { |
| 2421 | if (width < MAX_LONG_LONG_CHARS) |
| 2422 | width = MAX_LONG_LONG_CHARS; |
| 2423 | } |
| 2424 | else |
| 2425 | #endif |
| 2426 | /* MAX_LONG_CHARS is enough to hold a 64-bit integer, |
| 2427 | including sign. Decimal takes the most space. This |
| 2428 | isn't enough for octal. If a width is specified we |
| 2429 | need more (which we allocate later). */ |
| 2430 | if (width < MAX_LONG_CHARS) |
| 2431 | width = MAX_LONG_CHARS; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2432 | |
| 2433 | /* account for the size + '\0' to separate numbers |
| 2434 | inside of the numberresults buffer */ |
| 2435 | numbersize += (width + 1); |
| 2436 | } |
| 2437 | } |
| 2438 | else if ((unsigned char)*f > 127) { |
| 2439 | PyErr_Format(PyExc_ValueError, |
| 2440 | "PyUnicode_FromFormatV() expects an ASCII-encoded format " |
| 2441 | "string, got a non-ASCII byte: 0x%02x", |
| 2442 | (unsigned char)*f); |
| 2443 | return NULL; |
| 2444 | } |
| 2445 | } |
| 2446 | /* step 2: allocate memory for the results of |
| 2447 | * PyObject_Str()/PyObject_Repr()/PyUnicode_DecodeUTF8() calls */ |
| 2448 | if (callcount) { |
| 2449 | callresults = PyObject_Malloc(sizeof(PyObject *) * callcount); |
| 2450 | if (!callresults) { |
| 2451 | PyErr_NoMemory(); |
| 2452 | return NULL; |
| 2453 | } |
| 2454 | callresult = callresults; |
| 2455 | } |
| 2456 | /* step 2.5: allocate memory for the results of formating numbers */ |
| 2457 | if (numbersize) { |
| 2458 | numberresults = PyObject_Malloc(numbersize); |
| 2459 | if (!numberresults) { |
| 2460 | PyErr_NoMemory(); |
| 2461 | goto fail; |
| 2462 | } |
| 2463 | numberresult = numberresults; |
| 2464 | } |
| 2465 | |
| 2466 | /* step 3: format numbers and figure out how large a buffer we need */ |
| 2467 | for (f = format; *f; f++) { |
| 2468 | if (*f == '%') { |
| 2469 | const char* p; |
| 2470 | int longflag; |
| 2471 | int longlongflag; |
| 2472 | int size_tflag; |
| 2473 | int numprinted; |
| 2474 | |
| 2475 | p = f; |
| 2476 | zeropad = (f[1] == '0'); |
| 2477 | f = parse_format_flags(f, &width, &precision, |
| 2478 | &longflag, &longlongflag, &size_tflag); |
| 2479 | switch (*f) { |
| 2480 | case 'c': |
| 2481 | { |
| 2482 | Py_UCS4 ordinal = va_arg(count, int); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 2483 | maxchar = MAX_MAXCHAR(maxchar, ordinal); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2484 | n++; |
| 2485 | break; |
| 2486 | } |
| 2487 | case '%': |
| 2488 | n++; |
| 2489 | break; |
| 2490 | case 'i': |
| 2491 | case 'd': |
| 2492 | makefmt(fmt, longflag, longlongflag, size_tflag, zeropad, |
| 2493 | width, precision, *f); |
| 2494 | if (longflag) |
| 2495 | numprinted = sprintf(numberresult, fmt, |
| 2496 | va_arg(count, long)); |
| 2497 | #ifdef HAVE_LONG_LONG |
| 2498 | else if (longlongflag) |
| 2499 | numprinted = sprintf(numberresult, fmt, |
| 2500 | va_arg(count, PY_LONG_LONG)); |
| 2501 | #endif |
| 2502 | else if (size_tflag) |
| 2503 | numprinted = sprintf(numberresult, fmt, |
| 2504 | va_arg(count, Py_ssize_t)); |
| 2505 | else |
| 2506 | numprinted = sprintf(numberresult, fmt, |
| 2507 | va_arg(count, int)); |
| 2508 | n += numprinted; |
| 2509 | /* advance by +1 to skip over the '\0' */ |
| 2510 | numberresult += (numprinted + 1); |
| 2511 | assert(*(numberresult - 1) == '\0'); |
| 2512 | assert(*(numberresult - 2) != '\0'); |
| 2513 | assert(numprinted >= 0); |
| 2514 | assert(numberresult <= numberresults + numbersize); |
| 2515 | break; |
| 2516 | case 'u': |
| 2517 | makefmt(fmt, longflag, longlongflag, size_tflag, zeropad, |
| 2518 | width, precision, 'u'); |
| 2519 | if (longflag) |
| 2520 | numprinted = sprintf(numberresult, fmt, |
| 2521 | va_arg(count, unsigned long)); |
| 2522 | #ifdef HAVE_LONG_LONG |
| 2523 | else if (longlongflag) |
| 2524 | numprinted = sprintf(numberresult, fmt, |
| 2525 | va_arg(count, unsigned PY_LONG_LONG)); |
| 2526 | #endif |
| 2527 | else if (size_tflag) |
| 2528 | numprinted = sprintf(numberresult, fmt, |
| 2529 | va_arg(count, size_t)); |
| 2530 | else |
| 2531 | numprinted = sprintf(numberresult, fmt, |
| 2532 | va_arg(count, unsigned int)); |
| 2533 | n += numprinted; |
| 2534 | numberresult += (numprinted + 1); |
| 2535 | assert(*(numberresult - 1) == '\0'); |
| 2536 | assert(*(numberresult - 2) != '\0'); |
| 2537 | assert(numprinted >= 0); |
| 2538 | assert(numberresult <= numberresults + numbersize); |
| 2539 | break; |
| 2540 | case 'x': |
| 2541 | makefmt(fmt, 0, 0, 0, zeropad, width, precision, 'x'); |
| 2542 | numprinted = sprintf(numberresult, fmt, va_arg(count, int)); |
| 2543 | n += numprinted; |
| 2544 | numberresult += (numprinted + 1); |
| 2545 | assert(*(numberresult - 1) == '\0'); |
| 2546 | assert(*(numberresult - 2) != '\0'); |
| 2547 | assert(numprinted >= 0); |
| 2548 | assert(numberresult <= numberresults + numbersize); |
| 2549 | break; |
| 2550 | case 'p': |
| 2551 | numprinted = sprintf(numberresult, "%p", va_arg(count, void*)); |
| 2552 | /* %p is ill-defined: ensure leading 0x. */ |
| 2553 | if (numberresult[1] == 'X') |
| 2554 | numberresult[1] = 'x'; |
| 2555 | else if (numberresult[1] != 'x') { |
| 2556 | memmove(numberresult + 2, numberresult, |
| 2557 | strlen(numberresult) + 1); |
| 2558 | numberresult[0] = '0'; |
| 2559 | numberresult[1] = 'x'; |
| 2560 | numprinted += 2; |
| 2561 | } |
| 2562 | n += numprinted; |
| 2563 | numberresult += (numprinted + 1); |
| 2564 | assert(*(numberresult - 1) == '\0'); |
| 2565 | assert(*(numberresult - 2) != '\0'); |
| 2566 | assert(numprinted >= 0); |
| 2567 | assert(numberresult <= numberresults + numbersize); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2568 | break; |
| 2569 | case 's': |
| 2570 | { |
| 2571 | /* UTF-8 */ |
Georg Brandl | 780b2a6 | 2009-05-05 09:19:59 +0000 | [diff] [blame] | 2572 | const char *s = va_arg(count, const char*); |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 2573 | PyObject *str = PyUnicode_DecodeUTF8Stateful(s, strlen(s), "replace", NULL); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2574 | if (!str) |
| 2575 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2576 | /* since PyUnicode_DecodeUTF8 returns already flexible |
| 2577 | unicode objects, there is no need to call ready on them */ |
| 2578 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 2579 | maxchar = MAX_MAXCHAR(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2580 | n += PyUnicode_GET_LENGTH(str); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2581 | /* Remember the str and switch to the next slot */ |
| 2582 | *callresult++ = str; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2583 | break; |
| 2584 | } |
| 2585 | case 'U': |
| 2586 | { |
| 2587 | PyObject *obj = va_arg(count, PyObject *); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 2588 | assert(obj && _PyUnicode_CHECK(obj)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2589 | if (PyUnicode_READY(obj) == -1) |
| 2590 | goto fail; |
| 2591 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(obj); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 2592 | maxchar = MAX_MAXCHAR(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2593 | n += PyUnicode_GET_LENGTH(obj); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2594 | break; |
| 2595 | } |
| 2596 | case 'V': |
| 2597 | { |
| 2598 | PyObject *obj = va_arg(count, PyObject *); |
| 2599 | const char *str = va_arg(count, const char *); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2600 | PyObject *str_obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2601 | assert(obj || str); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 2602 | assert(!obj || _PyUnicode_CHECK(obj)); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2603 | if (obj) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2604 | if (PyUnicode_READY(obj) == -1) |
| 2605 | goto fail; |
| 2606 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(obj); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 2607 | maxchar = MAX_MAXCHAR(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2608 | n += PyUnicode_GET_LENGTH(obj); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2609 | *callresult++ = NULL; |
| 2610 | } |
| 2611 | else { |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 2612 | str_obj = PyUnicode_DecodeUTF8Stateful(str, strlen(str), "replace", NULL); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2613 | if (!str_obj) |
| 2614 | goto fail; |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 2615 | if (PyUnicode_READY(str_obj) == -1) { |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 2616 | Py_DECREF(str_obj); |
| 2617 | goto fail; |
| 2618 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2619 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str_obj); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 2620 | maxchar = MAX_MAXCHAR(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2621 | n += PyUnicode_GET_LENGTH(str_obj); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2622 | *callresult++ = str_obj; |
| 2623 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2624 | break; |
| 2625 | } |
| 2626 | case 'S': |
| 2627 | { |
| 2628 | PyObject *obj = va_arg(count, PyObject *); |
| 2629 | PyObject *str; |
| 2630 | assert(obj); |
| 2631 | str = PyObject_Str(obj); |
Benjamin Peterson | c8d8b88 | 2012-01-14 13:37:31 -0500 | [diff] [blame] | 2632 | if (!str) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2633 | goto fail; |
Benjamin Peterson | c8d8b88 | 2012-01-14 13:37:31 -0500 | [diff] [blame] | 2634 | if (PyUnicode_READY(str) == -1) { |
| 2635 | Py_DECREF(str); |
| 2636 | goto fail; |
| 2637 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2638 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 2639 | maxchar = MAX_MAXCHAR(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2640 | n += PyUnicode_GET_LENGTH(str); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2641 | /* Remember the str and switch to the next slot */ |
| 2642 | *callresult++ = str; |
| 2643 | break; |
| 2644 | } |
| 2645 | case 'R': |
| 2646 | { |
| 2647 | PyObject *obj = va_arg(count, PyObject *); |
| 2648 | PyObject *repr; |
| 2649 | assert(obj); |
| 2650 | repr = PyObject_Repr(obj); |
Benjamin Peterson | c8d8b88 | 2012-01-14 13:37:31 -0500 | [diff] [blame] | 2651 | if (!repr) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2652 | goto fail; |
Benjamin Peterson | c8d8b88 | 2012-01-14 13:37:31 -0500 | [diff] [blame] | 2653 | if (PyUnicode_READY(repr) == -1) { |
| 2654 | Py_DECREF(repr); |
| 2655 | goto fail; |
| 2656 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2657 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(repr); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 2658 | maxchar = MAX_MAXCHAR(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2659 | n += PyUnicode_GET_LENGTH(repr); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2660 | /* Remember the repr and switch to the next slot */ |
| 2661 | *callresult++ = repr; |
| 2662 | break; |
| 2663 | } |
| 2664 | case 'A': |
| 2665 | { |
| 2666 | PyObject *obj = va_arg(count, PyObject *); |
| 2667 | PyObject *ascii; |
| 2668 | assert(obj); |
| 2669 | ascii = PyObject_ASCII(obj); |
Benjamin Peterson | c8d8b88 | 2012-01-14 13:37:31 -0500 | [diff] [blame] | 2670 | if (!ascii) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2671 | goto fail; |
Benjamin Peterson | c8d8b88 | 2012-01-14 13:37:31 -0500 | [diff] [blame] | 2672 | if (PyUnicode_READY(ascii) == -1) { |
| 2673 | Py_DECREF(ascii); |
| 2674 | goto fail; |
| 2675 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2676 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(ascii); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 2677 | maxchar = MAX_MAXCHAR(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2678 | n += PyUnicode_GET_LENGTH(ascii); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2679 | /* Remember the repr and switch to the next slot */ |
| 2680 | *callresult++ = ascii; |
| 2681 | break; |
| 2682 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2683 | default: |
| 2684 | /* if we stumble upon an unknown |
| 2685 | formatting code, copy the rest of |
| 2686 | the format string to the output |
| 2687 | string. (we cannot just skip the |
| 2688 | code, since there's no way to know |
| 2689 | what's in the argument list) */ |
| 2690 | n += strlen(p); |
| 2691 | goto expand; |
| 2692 | } |
| 2693 | } else |
| 2694 | n++; |
| 2695 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2696 | expand: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2697 | /* step 4: fill the buffer */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2698 | /* Since we've analyzed how much space we need, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2699 | we don't have to resize the string. |
| 2700 | There can be no errors beyond this point. */ |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2701 | string = PyUnicode_New(n, maxchar); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2702 | if (!string) |
| 2703 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2704 | kind = PyUnicode_KIND(string); |
| 2705 | data = PyUnicode_DATA(string); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2706 | callresult = callresults; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2707 | numberresult = numberresults; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2708 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2709 | for (i = 0, f = format; *f; f++) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2710 | if (*f == '%') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2711 | const char* p; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2712 | |
| 2713 | p = f; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2714 | f = parse_format_flags(f, NULL, NULL, NULL, NULL, NULL); |
| 2715 | /* checking for == because the last argument could be a empty |
| 2716 | string, which causes i to point to end, the assert at the end of |
| 2717 | the loop */ |
| 2718 | assert(i <= PyUnicode_GET_LENGTH(string)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2719 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2720 | switch (*f) { |
| 2721 | case 'c': |
Victor Stinner | 5ed8b2c | 2011-02-21 21:13:44 +0000 | [diff] [blame] | 2722 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2723 | const int ordinal = va_arg(vargs, int); |
| 2724 | PyUnicode_WRITE(kind, data, i++, ordinal); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2725 | break; |
Victor Stinner | 5ed8b2c | 2011-02-21 21:13:44 +0000 | [diff] [blame] | 2726 | } |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2727 | case 'i': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2728 | case 'd': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2729 | case 'u': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2730 | case 'x': |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2731 | case 'p': |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 2732 | { |
| 2733 | Py_ssize_t written; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2734 | /* unused, since we already have the result */ |
| 2735 | if (*f == 'p') |
| 2736 | (void) va_arg(vargs, void *); |
| 2737 | else |
| 2738 | (void) va_arg(vargs, int); |
| 2739 | /* extract the result from numberresults and append. */ |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 2740 | written = unicode_write_cstr(string, i, numberresult); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2741 | /* skip over the separating '\0' */ |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 2742 | i += written; |
| 2743 | numberresult += written; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2744 | assert(*numberresult == '\0'); |
| 2745 | numberresult++; |
| 2746 | assert(numberresult <= numberresults + numbersize); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2747 | break; |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 2748 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2749 | case 's': |
| 2750 | { |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2751 | /* unused, since we already have the result */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2752 | Py_ssize_t size; |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2753 | (void) va_arg(vargs, char *); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2754 | size = PyUnicode_GET_LENGTH(*callresult); |
| 2755 | assert(PyUnicode_KIND(*callresult) <= PyUnicode_KIND(string)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2756 | copy_characters(string, i, *callresult, 0, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2757 | i += size; |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2758 | /* We're done with the unicode()/repr() => forget it */ |
| 2759 | Py_DECREF(*callresult); |
| 2760 | /* switch to next unicode()/repr() result */ |
| 2761 | ++callresult; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2762 | break; |
| 2763 | } |
| 2764 | case 'U': |
| 2765 | { |
| 2766 | PyObject *obj = va_arg(vargs, PyObject *); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2767 | Py_ssize_t size; |
| 2768 | assert(PyUnicode_KIND(obj) <= PyUnicode_KIND(string)); |
| 2769 | size = PyUnicode_GET_LENGTH(obj); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2770 | copy_characters(string, i, obj, 0, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2771 | i += size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2772 | break; |
| 2773 | } |
| 2774 | case 'V': |
| 2775 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2776 | Py_ssize_t size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2777 | PyObject *obj = va_arg(vargs, PyObject *); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2778 | va_arg(vargs, const char *); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2779 | if (obj) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2780 | size = PyUnicode_GET_LENGTH(obj); |
| 2781 | assert(PyUnicode_KIND(obj) <= PyUnicode_KIND(string)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2782 | copy_characters(string, i, obj, 0, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2783 | i += size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2784 | } else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2785 | size = PyUnicode_GET_LENGTH(*callresult); |
| 2786 | assert(PyUnicode_KIND(*callresult) <= |
| 2787 | PyUnicode_KIND(string)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2788 | copy_characters(string, i, *callresult, 0, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2789 | i += size; |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2790 | Py_DECREF(*callresult); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2791 | } |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2792 | ++callresult; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2793 | break; |
| 2794 | } |
| 2795 | case 'S': |
| 2796 | case 'R': |
Victor Stinner | 9a90900 | 2010-10-18 20:59:24 +0000 | [diff] [blame] | 2797 | case 'A': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2798 | { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2799 | Py_ssize_t size = PyUnicode_GET_LENGTH(*callresult); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2800 | /* unused, since we already have the result */ |
| 2801 | (void) va_arg(vargs, PyObject *); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2802 | assert(PyUnicode_KIND(*callresult) <= PyUnicode_KIND(string)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2803 | copy_characters(string, i, *callresult, 0, size); |
| 2804 | i += size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2805 | /* We're done with the unicode()/repr() => forget it */ |
| 2806 | Py_DECREF(*callresult); |
| 2807 | /* switch to next unicode()/repr() result */ |
| 2808 | ++callresult; |
| 2809 | break; |
| 2810 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2811 | case '%': |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2812 | PyUnicode_WRITE(kind, data, i++, '%'); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2813 | break; |
| 2814 | default: |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 2815 | i += unicode_write_cstr(string, i, p); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2816 | assert(i == PyUnicode_GET_LENGTH(string)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2817 | goto end; |
| 2818 | } |
Victor Stinner | 1205f27 | 2010-09-11 00:54:47 +0000 | [diff] [blame] | 2819 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2820 | else { |
| 2821 | assert(i < PyUnicode_GET_LENGTH(string)); |
| 2822 | PyUnicode_WRITE(kind, data, i++, *f); |
| 2823 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2824 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2825 | assert(i == PyUnicode_GET_LENGTH(string)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2826 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2827 | end: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2828 | if (callresults) |
| 2829 | PyObject_Free(callresults); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2830 | if (numberresults) |
| 2831 | PyObject_Free(numberresults); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 2832 | return unicode_result(string); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2833 | fail: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2834 | if (callresults) { |
| 2835 | PyObject **callresult2 = callresults; |
| 2836 | while (callresult2 < callresult) { |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2837 | Py_XDECREF(*callresult2); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2838 | ++callresult2; |
| 2839 | } |
| 2840 | PyObject_Free(callresults); |
| 2841 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2842 | if (numberresults) |
| 2843 | PyObject_Free(numberresults); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2844 | return NULL; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2845 | } |
| 2846 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2847 | PyObject * |
| 2848 | PyUnicode_FromFormat(const char *format, ...) |
| 2849 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2850 | PyObject* ret; |
| 2851 | va_list vargs; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2852 | |
| 2853 | #ifdef HAVE_STDARG_PROTOTYPES |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2854 | va_start(vargs, format); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2855 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2856 | va_start(vargs); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2857 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2858 | ret = PyUnicode_FromFormatV(format, vargs); |
| 2859 | va_end(vargs); |
| 2860 | return ret; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2861 | } |
| 2862 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2863 | #ifdef HAVE_WCHAR_H |
| 2864 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2865 | /* Helper function for PyUnicode_AsWideChar() and PyUnicode_AsWideCharString(): |
| 2866 | convert a Unicode object to a wide character string. |
| 2867 | |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2868 | - 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] | 2869 | character) required to convert the unicode object. Ignore size argument. |
| 2870 | |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2871 | - Otherwise: return the number of wide characters (excluding the null |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2872 | character) written into w. Write at most size wide characters (including |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2873 | the null character). */ |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2874 | static Py_ssize_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2875 | unicode_aswidechar(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2876 | wchar_t *w, |
| 2877 | Py_ssize_t size) |
| 2878 | { |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2879 | Py_ssize_t res; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2880 | const wchar_t *wstr; |
| 2881 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2882 | wstr = PyUnicode_AsUnicodeAndSize(unicode, &res); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2883 | if (wstr == NULL) |
| 2884 | return -1; |
| 2885 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2886 | if (w != NULL) { |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2887 | if (size > res) |
| 2888 | size = res + 1; |
| 2889 | else |
| 2890 | res = size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2891 | Py_MEMCPY(w, wstr, size * sizeof(wchar_t)); |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2892 | return res; |
| 2893 | } |
| 2894 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2895 | return res + 1; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2896 | } |
| 2897 | |
| 2898 | Py_ssize_t |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2899 | PyUnicode_AsWideChar(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2900 | wchar_t *w, |
| 2901 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2902 | { |
| 2903 | if (unicode == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2904 | PyErr_BadInternalCall(); |
| 2905 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2906 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2907 | return unicode_aswidechar(unicode, w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2908 | } |
| 2909 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2910 | wchar_t* |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 2911 | PyUnicode_AsWideCharString(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2912 | Py_ssize_t *size) |
| 2913 | { |
| 2914 | wchar_t* buffer; |
| 2915 | Py_ssize_t buflen; |
| 2916 | |
| 2917 | if (unicode == NULL) { |
| 2918 | PyErr_BadInternalCall(); |
| 2919 | return NULL; |
| 2920 | } |
| 2921 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2922 | buflen = unicode_aswidechar(unicode, NULL, 0); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2923 | if (buflen == -1) |
| 2924 | return NULL; |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2925 | if (PY_SSIZE_T_MAX / sizeof(wchar_t) < buflen) { |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2926 | PyErr_NoMemory(); |
| 2927 | return NULL; |
| 2928 | } |
| 2929 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2930 | buffer = PyMem_MALLOC(buflen * sizeof(wchar_t)); |
| 2931 | if (buffer == NULL) { |
| 2932 | PyErr_NoMemory(); |
| 2933 | return NULL; |
| 2934 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2935 | buflen = unicode_aswidechar(unicode, buffer, buflen); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2936 | if (buflen == -1) |
| 2937 | return NULL; |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2938 | if (size != NULL) |
| 2939 | *size = buflen; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2940 | return buffer; |
| 2941 | } |
| 2942 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2943 | #endif /* HAVE_WCHAR_H */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2944 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2945 | PyObject * |
| 2946 | PyUnicode_FromOrdinal(int ordinal) |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2947 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2948 | PyObject *v; |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 2949 | if (ordinal < 0 || ordinal > MAX_UNICODE) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2950 | PyErr_SetString(PyExc_ValueError, |
| 2951 | "chr() arg not in range(0x110000)"); |
| 2952 | return NULL; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2953 | } |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 2954 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2955 | if (ordinal < 256) |
| 2956 | return get_latin1_char(ordinal); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2957 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2958 | v = PyUnicode_New(1, ordinal); |
| 2959 | if (v == NULL) |
| 2960 | return NULL; |
| 2961 | PyUnicode_WRITE(PyUnicode_KIND(v), PyUnicode_DATA(v), 0, ordinal); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2962 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2963 | return v; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2964 | } |
| 2965 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2966 | PyObject * |
| 2967 | PyUnicode_FromObject(register PyObject *obj) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2968 | { |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2969 | /* XXX Perhaps we should make this API an alias of |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2970 | PyObject_Str() instead ?! */ |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2971 | if (PyUnicode_CheckExact(obj)) { |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 2972 | if (PyUnicode_READY(obj) == -1) |
Victor Stinner | d3a83d5 | 2011-10-01 03:09:33 +0200 | [diff] [blame] | 2973 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2974 | Py_INCREF(obj); |
| 2975 | return obj; |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2976 | } |
| 2977 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2978 | /* For a Unicode subtype that's not a Unicode object, |
| 2979 | return a true Unicode object with the same data. */ |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 2980 | return _PyUnicode_Copy(obj); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2981 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2982 | PyErr_Format(PyExc_TypeError, |
| 2983 | "Can't convert '%.100s' object to str implicitly", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2984 | Py_TYPE(obj)->tp_name); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2985 | return NULL; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2986 | } |
| 2987 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2988 | PyObject * |
| 2989 | PyUnicode_FromEncodedObject(register PyObject *obj, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2990 | const char *encoding, |
| 2991 | const char *errors) |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2992 | { |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2993 | Py_buffer buffer; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2994 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2995 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2996 | if (obj == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2997 | PyErr_BadInternalCall(); |
| 2998 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2999 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 3000 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 3001 | /* Decoding bytes objects is the most common case and should be fast */ |
| 3002 | if (PyBytes_Check(obj)) { |
| 3003 | if (PyBytes_GET_SIZE(obj) == 0) { |
| 3004 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 3005 | v = unicode_empty; |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 3006 | } |
| 3007 | else { |
| 3008 | v = PyUnicode_Decode( |
| 3009 | PyBytes_AS_STRING(obj), PyBytes_GET_SIZE(obj), |
| 3010 | encoding, errors); |
| 3011 | } |
| 3012 | return v; |
| 3013 | } |
| 3014 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 3015 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3016 | PyErr_SetString(PyExc_TypeError, |
| 3017 | "decoding str is not supported"); |
| 3018 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 3019 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 3020 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 3021 | /* Retrieve a bytes buffer view through the PEP 3118 buffer interface */ |
| 3022 | if (PyObject_GetBuffer(obj, &buffer, PyBUF_SIMPLE) < 0) { |
| 3023 | PyErr_Format(PyExc_TypeError, |
| 3024 | "coercing to str: need bytes, bytearray " |
| 3025 | "or buffer-like object, %.80s found", |
| 3026 | Py_TYPE(obj)->tp_name); |
| 3027 | return NULL; |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 3028 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3029 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 3030 | if (buffer.len == 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3031 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 3032 | v = unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3033 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3034 | else |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 3035 | v = PyUnicode_Decode((char*) buffer.buf, buffer.len, encoding, errors); |
Marc-André Lemburg | ad7c98e | 2001-01-17 17:09:53 +0000 | [diff] [blame] | 3036 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 3037 | PyBuffer_Release(&buffer); |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 3038 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3039 | } |
| 3040 | |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3041 | /* Convert encoding to lower case and replace '_' with '-' in order to |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3042 | catch e.g. UTF_8. Return 0 on error (encoding is longer than lower_len-1), |
| 3043 | 1 on success. */ |
| 3044 | static int |
| 3045 | normalize_encoding(const char *encoding, |
| 3046 | char *lower, |
| 3047 | size_t lower_len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3048 | { |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 3049 | const char *e; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3050 | char *l; |
| 3051 | char *l_end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3052 | |
Benjamin Peterson | 7a6debe | 2011-10-15 09:25:28 -0400 | [diff] [blame] | 3053 | if (encoding == NULL) { |
| 3054 | strcpy(lower, "utf-8"); |
| 3055 | return 1; |
| 3056 | } |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 3057 | e = encoding; |
| 3058 | l = lower; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3059 | l_end = &lower[lower_len - 1]; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3060 | while (*e) { |
| 3061 | if (l == l_end) |
| 3062 | return 0; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 3063 | if (Py_ISUPPER(*e)) { |
| 3064 | *l++ = Py_TOLOWER(*e++); |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 3065 | } |
| 3066 | else if (*e == '_') { |
| 3067 | *l++ = '-'; |
| 3068 | e++; |
| 3069 | } |
| 3070 | else { |
| 3071 | *l++ = *e++; |
| 3072 | } |
| 3073 | } |
| 3074 | *l = '\0'; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3075 | return 1; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3076 | } |
| 3077 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3078 | PyObject * |
| 3079 | PyUnicode_Decode(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3080 | Py_ssize_t size, |
| 3081 | const char *encoding, |
| 3082 | const char *errors) |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3083 | { |
| 3084 | PyObject *buffer = NULL, *unicode; |
| 3085 | Py_buffer info; |
| 3086 | char lower[11]; /* Enough for any encoding shortcut */ |
| 3087 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3088 | /* Shortcuts for common default encodings */ |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3089 | if (normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3090 | if ((strcmp(lower, "utf-8") == 0) || |
| 3091 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 3092 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3093 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3094 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3095 | (strcmp(lower, "iso-8859-1") == 0)) |
| 3096 | return PyUnicode_DecodeLatin1(s, size, errors); |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3097 | #ifdef HAVE_MBCS |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3098 | else if (strcmp(lower, "mbcs") == 0) |
| 3099 | return PyUnicode_DecodeMBCS(s, size, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 3100 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3101 | else if (strcmp(lower, "ascii") == 0) |
| 3102 | return PyUnicode_DecodeASCII(s, size, errors); |
| 3103 | else if (strcmp(lower, "utf-16") == 0) |
| 3104 | return PyUnicode_DecodeUTF16(s, size, errors, 0); |
| 3105 | else if (strcmp(lower, "utf-32") == 0) |
| 3106 | return PyUnicode_DecodeUTF32(s, size, errors, 0); |
| 3107 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3108 | |
| 3109 | /* Decode via the codec registry */ |
Guido van Rossum | be801ac | 2007-10-08 03:32:34 +0000 | [diff] [blame] | 3110 | buffer = NULL; |
Antoine Pitrou | c3b3924 | 2009-01-03 16:59:18 +0000 | [diff] [blame] | 3111 | 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] | 3112 | goto onError; |
Antoine Pitrou | ee58fa4 | 2008-08-19 18:22:14 +0000 | [diff] [blame] | 3113 | buffer = PyMemoryView_FromBuffer(&info); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3114 | if (buffer == NULL) |
| 3115 | goto onError; |
| 3116 | unicode = PyCodec_Decode(buffer, encoding, errors); |
| 3117 | if (unicode == NULL) |
| 3118 | goto onError; |
| 3119 | if (!PyUnicode_Check(unicode)) { |
| 3120 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3121 | "decoder did not return a str object (type=%.400s)", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 3122 | Py_TYPE(unicode)->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3123 | Py_DECREF(unicode); |
| 3124 | goto onError; |
| 3125 | } |
| 3126 | Py_DECREF(buffer); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 3127 | return unicode_result(unicode); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3128 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3129 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3130 | Py_XDECREF(buffer); |
| 3131 | return NULL; |
| 3132 | } |
| 3133 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3134 | PyObject * |
| 3135 | PyUnicode_AsDecodedObject(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3136 | const char *encoding, |
| 3137 | const char *errors) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3138 | { |
| 3139 | PyObject *v; |
| 3140 | |
| 3141 | if (!PyUnicode_Check(unicode)) { |
| 3142 | PyErr_BadArgument(); |
| 3143 | goto onError; |
| 3144 | } |
| 3145 | |
| 3146 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3147 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3148 | |
| 3149 | /* Decode via the codec registry */ |
| 3150 | v = PyCodec_Decode(unicode, encoding, errors); |
| 3151 | if (v == NULL) |
| 3152 | goto onError; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 3153 | return unicode_result(v); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3154 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3155 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3156 | return NULL; |
| 3157 | } |
| 3158 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3159 | PyObject * |
| 3160 | PyUnicode_AsDecodedUnicode(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3161 | const char *encoding, |
| 3162 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3163 | { |
| 3164 | PyObject *v; |
| 3165 | |
| 3166 | if (!PyUnicode_Check(unicode)) { |
| 3167 | PyErr_BadArgument(); |
| 3168 | goto onError; |
| 3169 | } |
| 3170 | |
| 3171 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3172 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3173 | |
| 3174 | /* Decode via the codec registry */ |
| 3175 | v = PyCodec_Decode(unicode, encoding, errors); |
| 3176 | if (v == NULL) |
| 3177 | goto onError; |
| 3178 | if (!PyUnicode_Check(v)) { |
| 3179 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3180 | "decoder did not return a str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3181 | Py_TYPE(v)->tp_name); |
| 3182 | Py_DECREF(v); |
| 3183 | goto onError; |
| 3184 | } |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 3185 | return unicode_result(v); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3186 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3187 | onError: |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3188 | return NULL; |
| 3189 | } |
| 3190 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3191 | PyObject * |
| 3192 | PyUnicode_Encode(const Py_UNICODE *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3193 | Py_ssize_t size, |
| 3194 | const char *encoding, |
| 3195 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3196 | { |
| 3197 | PyObject *v, *unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3198 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3199 | unicode = PyUnicode_FromUnicode(s, size); |
| 3200 | if (unicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3201 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3202 | v = PyUnicode_AsEncodedString(unicode, encoding, errors); |
| 3203 | Py_DECREF(unicode); |
| 3204 | return v; |
| 3205 | } |
| 3206 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3207 | PyObject * |
| 3208 | PyUnicode_AsEncodedObject(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3209 | const char *encoding, |
| 3210 | const char *errors) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3211 | { |
| 3212 | PyObject *v; |
| 3213 | |
| 3214 | if (!PyUnicode_Check(unicode)) { |
| 3215 | PyErr_BadArgument(); |
| 3216 | goto onError; |
| 3217 | } |
| 3218 | |
| 3219 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3220 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3221 | |
| 3222 | /* Encode via the codec registry */ |
| 3223 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3224 | if (v == NULL) |
| 3225 | goto onError; |
| 3226 | return v; |
| 3227 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3228 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3229 | return NULL; |
| 3230 | } |
| 3231 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3232 | static size_t |
| 3233 | wcstombs_errorpos(const wchar_t *wstr) |
| 3234 | { |
| 3235 | size_t len; |
| 3236 | #if SIZEOF_WCHAR_T == 2 |
| 3237 | wchar_t buf[3]; |
| 3238 | #else |
| 3239 | wchar_t buf[2]; |
| 3240 | #endif |
| 3241 | char outbuf[MB_LEN_MAX]; |
| 3242 | const wchar_t *start, *previous; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3243 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3244 | #if SIZEOF_WCHAR_T == 2 |
| 3245 | buf[2] = 0; |
| 3246 | #else |
| 3247 | buf[1] = 0; |
| 3248 | #endif |
| 3249 | start = wstr; |
| 3250 | while (*wstr != L'\0') |
| 3251 | { |
| 3252 | previous = wstr; |
| 3253 | #if SIZEOF_WCHAR_T == 2 |
| 3254 | if (Py_UNICODE_IS_HIGH_SURROGATE(wstr[0]) |
| 3255 | && Py_UNICODE_IS_LOW_SURROGATE(wstr[1])) |
| 3256 | { |
| 3257 | buf[0] = wstr[0]; |
| 3258 | buf[1] = wstr[1]; |
| 3259 | wstr += 2; |
| 3260 | } |
| 3261 | else { |
| 3262 | buf[0] = *wstr; |
| 3263 | buf[1] = 0; |
| 3264 | wstr++; |
| 3265 | } |
| 3266 | #else |
| 3267 | buf[0] = *wstr; |
| 3268 | wstr++; |
| 3269 | #endif |
| 3270 | len = wcstombs(outbuf, buf, sizeof(outbuf)); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3271 | if (len == (size_t)-1) |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3272 | return previous - start; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3273 | } |
| 3274 | |
| 3275 | /* failed to find the unencodable character */ |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3276 | return 0; |
| 3277 | } |
| 3278 | |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3279 | static int |
| 3280 | locale_error_handler(const char *errors, int *surrogateescape) |
| 3281 | { |
| 3282 | if (errors == NULL) { |
| 3283 | *surrogateescape = 0; |
| 3284 | return 0; |
| 3285 | } |
| 3286 | |
| 3287 | if (strcmp(errors, "strict") == 0) { |
| 3288 | *surrogateescape = 0; |
| 3289 | return 0; |
| 3290 | } |
| 3291 | if (strcmp(errors, "surrogateescape") == 0) { |
| 3292 | *surrogateescape = 1; |
| 3293 | return 0; |
| 3294 | } |
| 3295 | PyErr_Format(PyExc_ValueError, |
| 3296 | "only 'strict' and 'surrogateescape' error handlers " |
| 3297 | "are supported, not '%s'", |
| 3298 | errors); |
| 3299 | return -1; |
| 3300 | } |
| 3301 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3302 | PyObject * |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3303 | PyUnicode_EncodeLocale(PyObject *unicode, const char *errors) |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3304 | { |
| 3305 | Py_ssize_t wlen, wlen2; |
| 3306 | wchar_t *wstr; |
| 3307 | PyObject *bytes = NULL; |
| 3308 | char *errmsg; |
Victor Stinner | 1f33f2b | 2011-12-17 04:45:09 +0100 | [diff] [blame] | 3309 | PyObject *reason; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3310 | PyObject *exc; |
| 3311 | size_t error_pos; |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3312 | int surrogateescape; |
| 3313 | |
| 3314 | if (locale_error_handler(errors, &surrogateescape) < 0) |
| 3315 | return NULL; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3316 | |
| 3317 | wstr = PyUnicode_AsWideCharString(unicode, &wlen); |
| 3318 | if (wstr == NULL) |
| 3319 | return NULL; |
| 3320 | |
| 3321 | wlen2 = wcslen(wstr); |
| 3322 | if (wlen2 != wlen) { |
| 3323 | PyMem_Free(wstr); |
| 3324 | PyErr_SetString(PyExc_TypeError, "embedded null character"); |
| 3325 | return NULL; |
| 3326 | } |
| 3327 | |
| 3328 | if (surrogateescape) { |
| 3329 | /* locale encoding with surrogateescape */ |
| 3330 | char *str; |
| 3331 | |
| 3332 | str = _Py_wchar2char(wstr, &error_pos); |
| 3333 | if (str == NULL) { |
| 3334 | if (error_pos == (size_t)-1) { |
| 3335 | PyErr_NoMemory(); |
| 3336 | PyMem_Free(wstr); |
| 3337 | return NULL; |
| 3338 | } |
| 3339 | else { |
| 3340 | goto encode_error; |
| 3341 | } |
| 3342 | } |
| 3343 | PyMem_Free(wstr); |
| 3344 | |
| 3345 | bytes = PyBytes_FromString(str); |
| 3346 | PyMem_Free(str); |
| 3347 | } |
| 3348 | else { |
| 3349 | size_t len, len2; |
| 3350 | |
| 3351 | len = wcstombs(NULL, wstr, 0); |
| 3352 | if (len == (size_t)-1) { |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3353 | error_pos = (size_t)-1; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3354 | goto encode_error; |
| 3355 | } |
| 3356 | |
| 3357 | bytes = PyBytes_FromStringAndSize(NULL, len); |
| 3358 | if (bytes == NULL) { |
| 3359 | PyMem_Free(wstr); |
| 3360 | return NULL; |
| 3361 | } |
| 3362 | |
| 3363 | len2 = wcstombs(PyBytes_AS_STRING(bytes), wstr, len+1); |
| 3364 | if (len2 == (size_t)-1 || len2 > len) { |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3365 | error_pos = (size_t)-1; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3366 | goto encode_error; |
| 3367 | } |
| 3368 | PyMem_Free(wstr); |
| 3369 | } |
| 3370 | return bytes; |
| 3371 | |
| 3372 | encode_error: |
| 3373 | errmsg = strerror(errno); |
| 3374 | assert(errmsg != NULL); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3375 | |
| 3376 | if (error_pos == (size_t)-1) |
| 3377 | error_pos = wcstombs_errorpos(wstr); |
| 3378 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3379 | PyMem_Free(wstr); |
| 3380 | Py_XDECREF(bytes); |
| 3381 | |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3382 | if (errmsg != NULL) { |
| 3383 | size_t errlen; |
| 3384 | wstr = _Py_char2wchar(errmsg, &errlen); |
| 3385 | if (wstr != NULL) { |
| 3386 | reason = PyUnicode_FromWideChar(wstr, errlen); |
| 3387 | PyMem_Free(wstr); |
| 3388 | } else |
| 3389 | errmsg = NULL; |
| 3390 | } |
| 3391 | if (errmsg == NULL) |
Victor Stinner | 1f33f2b | 2011-12-17 04:45:09 +0100 | [diff] [blame] | 3392 | reason = PyUnicode_FromString( |
| 3393 | "wcstombs() encountered an unencodable " |
| 3394 | "wide character"); |
| 3395 | if (reason == NULL) |
| 3396 | return NULL; |
| 3397 | |
| 3398 | exc = PyObject_CallFunction(PyExc_UnicodeEncodeError, "sOnnO", |
| 3399 | "locale", unicode, |
| 3400 | (Py_ssize_t)error_pos, |
| 3401 | (Py_ssize_t)(error_pos+1), |
| 3402 | reason); |
| 3403 | Py_DECREF(reason); |
| 3404 | if (exc != NULL) { |
| 3405 | PyCodec_StrictErrors(exc); |
| 3406 | Py_XDECREF(exc); |
| 3407 | } |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3408 | return NULL; |
| 3409 | } |
| 3410 | |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3411 | PyObject * |
| 3412 | PyUnicode_EncodeFSDefault(PyObject *unicode) |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3413 | { |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3414 | #ifdef HAVE_MBCS |
Victor Stinner | ac931b1 | 2011-11-20 18:27:03 +0100 | [diff] [blame] | 3415 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, NULL); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3416 | #elif defined(__APPLE__) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3417 | return _PyUnicode_AsUTF8String(unicode, "surrogateescape"); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3418 | #else |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 3419 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 3420 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 3421 | cannot use it to encode and decode filenames before it is loaded. Load |
| 3422 | the Python codec requires to encode at least its own filename. Use the C |
| 3423 | version of the locale codec until the codec registry is initialized and |
| 3424 | the Python codec is loaded. |
| 3425 | |
| 3426 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 3427 | cannot only rely on it: check also interp->fscodec_initialized for |
| 3428 | subinterpreters. */ |
| 3429 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3430 | return PyUnicode_AsEncodedString(unicode, |
| 3431 | Py_FileSystemDefaultEncoding, |
| 3432 | "surrogateescape"); |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 3433 | } |
| 3434 | else { |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3435 | return PyUnicode_EncodeLocale(unicode, "surrogateescape"); |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 3436 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3437 | #endif |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3438 | } |
| 3439 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3440 | PyObject * |
| 3441 | PyUnicode_AsEncodedString(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3442 | const char *encoding, |
| 3443 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3444 | { |
| 3445 | PyObject *v; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3446 | char lower[11]; /* Enough for any encoding shortcut */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3447 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3448 | if (!PyUnicode_Check(unicode)) { |
| 3449 | PyErr_BadArgument(); |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3450 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3451 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3452 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3453 | /* Shortcuts for common default encodings */ |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3454 | if (normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3455 | if ((strcmp(lower, "utf-8") == 0) || |
| 3456 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 3457 | { |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3458 | if (errors == NULL || strcmp(errors, "strict") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3459 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3460 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3461 | return _PyUnicode_AsUTF8String(unicode, errors); |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 3462 | } |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3463 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3464 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3465 | (strcmp(lower, "iso-8859-1") == 0)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3466 | return _PyUnicode_AsLatin1String(unicode, errors); |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3467 | #ifdef HAVE_MBCS |
Victor Stinner | ac931b1 | 2011-11-20 18:27:03 +0100 | [diff] [blame] | 3468 | else if (strcmp(lower, "mbcs") == 0) |
| 3469 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 3470 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3471 | else if (strcmp(lower, "ascii") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3472 | return _PyUnicode_AsASCIIString(unicode, errors); |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3473 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3474 | |
| 3475 | /* Encode via the codec registry */ |
| 3476 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3477 | if (v == NULL) |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3478 | return NULL; |
| 3479 | |
| 3480 | /* The normal path */ |
| 3481 | if (PyBytes_Check(v)) |
| 3482 | return v; |
| 3483 | |
| 3484 | /* 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] | 3485 | if (PyByteArray_Check(v)) { |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 3486 | int error; |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3487 | PyObject *b; |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 3488 | |
| 3489 | error = PyErr_WarnFormat(PyExc_RuntimeWarning, 1, |
| 3490 | "encoder %s returned bytearray instead of bytes", |
| 3491 | encoding); |
| 3492 | if (error) { |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3493 | Py_DECREF(v); |
| 3494 | return NULL; |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3495 | } |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3496 | |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3497 | b = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(v), Py_SIZE(v)); |
| 3498 | Py_DECREF(v); |
| 3499 | return b; |
| 3500 | } |
| 3501 | |
| 3502 | PyErr_Format(PyExc_TypeError, |
| 3503 | "encoder did not return a bytes object (type=%.400s)", |
| 3504 | Py_TYPE(v)->tp_name); |
| 3505 | Py_DECREF(v); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3506 | return NULL; |
| 3507 | } |
| 3508 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3509 | PyObject * |
| 3510 | PyUnicode_AsEncodedUnicode(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3511 | const char *encoding, |
| 3512 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3513 | { |
| 3514 | PyObject *v; |
| 3515 | |
| 3516 | if (!PyUnicode_Check(unicode)) { |
| 3517 | PyErr_BadArgument(); |
| 3518 | goto onError; |
| 3519 | } |
| 3520 | |
| 3521 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3522 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3523 | |
| 3524 | /* Encode via the codec registry */ |
| 3525 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3526 | if (v == NULL) |
| 3527 | goto onError; |
| 3528 | if (!PyUnicode_Check(v)) { |
| 3529 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3530 | "encoder did not return an str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3531 | Py_TYPE(v)->tp_name); |
| 3532 | Py_DECREF(v); |
| 3533 | goto onError; |
| 3534 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3535 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3536 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3537 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3538 | return NULL; |
| 3539 | } |
| 3540 | |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3541 | static size_t |
| 3542 | mbstowcs_errorpos(const char *str, size_t len) |
| 3543 | { |
| 3544 | #ifdef HAVE_MBRTOWC |
| 3545 | const char *start = str; |
| 3546 | mbstate_t mbs; |
| 3547 | size_t converted; |
| 3548 | wchar_t ch; |
| 3549 | |
| 3550 | memset(&mbs, 0, sizeof mbs); |
| 3551 | while (len) |
| 3552 | { |
| 3553 | converted = mbrtowc(&ch, (char*)str, len, &mbs); |
| 3554 | if (converted == 0) |
| 3555 | /* Reached end of string */ |
| 3556 | break; |
| 3557 | if (converted == (size_t)-1 || converted == (size_t)-2) { |
| 3558 | /* Conversion error or incomplete character */ |
| 3559 | return str - start; |
| 3560 | } |
| 3561 | else { |
| 3562 | str += converted; |
| 3563 | len -= converted; |
| 3564 | } |
| 3565 | } |
| 3566 | /* failed to find the undecodable byte sequence */ |
| 3567 | return 0; |
| 3568 | #endif |
| 3569 | return 0; |
| 3570 | } |
| 3571 | |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3572 | PyObject* |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3573 | PyUnicode_DecodeLocaleAndSize(const char *str, Py_ssize_t len, |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3574 | const char *errors) |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3575 | { |
| 3576 | wchar_t smallbuf[256]; |
| 3577 | size_t smallbuf_len = Py_ARRAY_LENGTH(smallbuf); |
| 3578 | wchar_t *wstr; |
| 3579 | size_t wlen, wlen2; |
| 3580 | PyObject *unicode; |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3581 | int surrogateescape; |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3582 | size_t error_pos; |
| 3583 | char *errmsg; |
| 3584 | PyObject *reason, *exc; |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3585 | |
| 3586 | if (locale_error_handler(errors, &surrogateescape) < 0) |
| 3587 | return NULL; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3588 | |
| 3589 | if (str[len] != '\0' || len != strlen(str)) { |
| 3590 | PyErr_SetString(PyExc_TypeError, "embedded null character"); |
| 3591 | return NULL; |
| 3592 | } |
| 3593 | |
| 3594 | if (surrogateescape) |
| 3595 | { |
| 3596 | wstr = _Py_char2wchar(str, &wlen); |
| 3597 | if (wstr == NULL) { |
| 3598 | if (wlen == (size_t)-1) |
| 3599 | PyErr_NoMemory(); |
| 3600 | else |
| 3601 | PyErr_SetFromErrno(PyExc_OSError); |
| 3602 | return NULL; |
| 3603 | } |
| 3604 | |
| 3605 | unicode = PyUnicode_FromWideChar(wstr, wlen); |
| 3606 | PyMem_Free(wstr); |
| 3607 | } |
| 3608 | else { |
| 3609 | #ifndef HAVE_BROKEN_MBSTOWCS |
| 3610 | wlen = mbstowcs(NULL, str, 0); |
| 3611 | #else |
| 3612 | wlen = len; |
| 3613 | #endif |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3614 | if (wlen == (size_t)-1) |
| 3615 | goto decode_error; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3616 | if (wlen+1 <= smallbuf_len) { |
| 3617 | wstr = smallbuf; |
| 3618 | } |
| 3619 | else { |
| 3620 | if (wlen > PY_SSIZE_T_MAX / sizeof(wchar_t) - 1) |
| 3621 | return PyErr_NoMemory(); |
| 3622 | |
| 3623 | wstr = PyMem_Malloc((wlen+1) * sizeof(wchar_t)); |
| 3624 | if (!wstr) |
| 3625 | return PyErr_NoMemory(); |
| 3626 | } |
| 3627 | |
| 3628 | /* This shouldn't fail now */ |
| 3629 | wlen2 = mbstowcs(wstr, str, wlen+1); |
| 3630 | if (wlen2 == (size_t)-1) { |
| 3631 | if (wstr != smallbuf) |
| 3632 | PyMem_Free(wstr); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3633 | goto decode_error; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3634 | } |
| 3635 | #ifdef HAVE_BROKEN_MBSTOWCS |
| 3636 | assert(wlen2 == wlen); |
| 3637 | #endif |
| 3638 | unicode = PyUnicode_FromWideChar(wstr, wlen2); |
| 3639 | if (wstr != smallbuf) |
| 3640 | PyMem_Free(wstr); |
| 3641 | } |
| 3642 | return unicode; |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3643 | |
| 3644 | decode_error: |
| 3645 | errmsg = strerror(errno); |
| 3646 | assert(errmsg != NULL); |
| 3647 | |
| 3648 | error_pos = mbstowcs_errorpos(str, len); |
| 3649 | if (errmsg != NULL) { |
| 3650 | size_t errlen; |
| 3651 | wstr = _Py_char2wchar(errmsg, &errlen); |
| 3652 | if (wstr != NULL) { |
| 3653 | reason = PyUnicode_FromWideChar(wstr, errlen); |
| 3654 | PyMem_Free(wstr); |
| 3655 | } else |
| 3656 | errmsg = NULL; |
| 3657 | } |
| 3658 | if (errmsg == NULL) |
| 3659 | reason = PyUnicode_FromString( |
| 3660 | "mbstowcs() encountered an invalid multibyte sequence"); |
| 3661 | if (reason == NULL) |
| 3662 | return NULL; |
| 3663 | |
| 3664 | exc = PyObject_CallFunction(PyExc_UnicodeDecodeError, "sy#nnO", |
| 3665 | "locale", str, len, |
| 3666 | (Py_ssize_t)error_pos, |
| 3667 | (Py_ssize_t)(error_pos+1), |
| 3668 | reason); |
| 3669 | Py_DECREF(reason); |
| 3670 | if (exc != NULL) { |
| 3671 | PyCodec_StrictErrors(exc); |
| 3672 | Py_XDECREF(exc); |
| 3673 | } |
| 3674 | return NULL; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3675 | } |
| 3676 | |
| 3677 | PyObject* |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3678 | PyUnicode_DecodeLocale(const char *str, const char *errors) |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3679 | { |
| 3680 | Py_ssize_t size = (Py_ssize_t)strlen(str); |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3681 | return PyUnicode_DecodeLocaleAndSize(str, size, errors); |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3682 | } |
| 3683 | |
| 3684 | |
| 3685 | PyObject* |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3686 | PyUnicode_DecodeFSDefault(const char *s) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3687 | Py_ssize_t size = (Py_ssize_t)strlen(s); |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3688 | return PyUnicode_DecodeFSDefaultAndSize(s, size); |
| 3689 | } |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3690 | |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3691 | PyObject* |
| 3692 | PyUnicode_DecodeFSDefaultAndSize(const char *s, Py_ssize_t size) |
| 3693 | { |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3694 | #ifdef HAVE_MBCS |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3695 | return PyUnicode_DecodeMBCS(s, size, NULL); |
| 3696 | #elif defined(__APPLE__) |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 3697 | return PyUnicode_DecodeUTF8Stateful(s, size, "surrogateescape", NULL); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3698 | #else |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 3699 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 3700 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 3701 | cannot use it to encode and decode filenames before it is loaded. Load |
| 3702 | the Python codec requires to encode at least its own filename. Use the C |
| 3703 | version of the locale codec until the codec registry is initialized and |
| 3704 | the Python codec is loaded. |
| 3705 | |
| 3706 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 3707 | cannot only rely on it: check also interp->fscodec_initialized for |
| 3708 | subinterpreters. */ |
| 3709 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3710 | return PyUnicode_Decode(s, size, |
| 3711 | Py_FileSystemDefaultEncoding, |
Victor Stinner | b9a20ad | 2010-04-30 16:37:52 +0000 | [diff] [blame] | 3712 | "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3713 | } |
| 3714 | else { |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3715 | return PyUnicode_DecodeLocaleAndSize(s, size, "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3716 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3717 | #endif |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3718 | } |
| 3719 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3720 | |
| 3721 | int |
Antoine Pitrou | 1334884 | 2012-01-29 18:36:34 +0100 | [diff] [blame] | 3722 | _PyUnicode_HasNULChars(PyObject* s) |
| 3723 | { |
| 3724 | static PyObject *nul = NULL; |
| 3725 | |
| 3726 | if (nul == NULL) |
| 3727 | nul = PyUnicode_FromStringAndSize("\0", 1); |
| 3728 | if (nul == NULL) |
| 3729 | return -1; |
| 3730 | return PyUnicode_Contains(s, nul); |
| 3731 | } |
| 3732 | |
| 3733 | |
| 3734 | int |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3735 | PyUnicode_FSConverter(PyObject* arg, void* addr) |
| 3736 | { |
| 3737 | PyObject *output = NULL; |
| 3738 | Py_ssize_t size; |
| 3739 | void *data; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3740 | if (arg == NULL) { |
| 3741 | Py_DECREF(*(PyObject**)addr); |
| 3742 | return 1; |
| 3743 | } |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 3744 | if (PyBytes_Check(arg)) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3745 | output = arg; |
| 3746 | Py_INCREF(output); |
| 3747 | } |
| 3748 | else { |
| 3749 | arg = PyUnicode_FromObject(arg); |
| 3750 | if (!arg) |
| 3751 | return 0; |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3752 | output = PyUnicode_EncodeFSDefault(arg); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3753 | Py_DECREF(arg); |
| 3754 | if (!output) |
| 3755 | return 0; |
| 3756 | if (!PyBytes_Check(output)) { |
| 3757 | Py_DECREF(output); |
| 3758 | PyErr_SetString(PyExc_TypeError, "encoder failed to return bytes"); |
| 3759 | return 0; |
| 3760 | } |
| 3761 | } |
Victor Stinner | 0ea2a46 | 2010-04-30 00:22:08 +0000 | [diff] [blame] | 3762 | size = PyBytes_GET_SIZE(output); |
| 3763 | data = PyBytes_AS_STRING(output); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3764 | if (size != strlen(data)) { |
Benjamin Peterson | 7a6b44a | 2011-08-18 13:51:47 -0500 | [diff] [blame] | 3765 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3766 | Py_DECREF(output); |
| 3767 | return 0; |
| 3768 | } |
| 3769 | *(PyObject**)addr = output; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3770 | return Py_CLEANUP_SUPPORTED; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3771 | } |
| 3772 | |
| 3773 | |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3774 | int |
| 3775 | PyUnicode_FSDecoder(PyObject* arg, void* addr) |
| 3776 | { |
| 3777 | PyObject *output = NULL; |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3778 | if (arg == NULL) { |
| 3779 | Py_DECREF(*(PyObject**)addr); |
| 3780 | return 1; |
| 3781 | } |
| 3782 | if (PyUnicode_Check(arg)) { |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 3783 | if (PyUnicode_READY(arg) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3784 | return 0; |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3785 | output = arg; |
| 3786 | Py_INCREF(output); |
| 3787 | } |
| 3788 | else { |
| 3789 | arg = PyBytes_FromObject(arg); |
| 3790 | if (!arg) |
| 3791 | return 0; |
| 3792 | output = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AS_STRING(arg), |
| 3793 | PyBytes_GET_SIZE(arg)); |
| 3794 | Py_DECREF(arg); |
| 3795 | if (!output) |
| 3796 | return 0; |
| 3797 | if (!PyUnicode_Check(output)) { |
| 3798 | Py_DECREF(output); |
| 3799 | PyErr_SetString(PyExc_TypeError, "decoder failed to return unicode"); |
| 3800 | return 0; |
| 3801 | } |
| 3802 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 3803 | if (PyUnicode_READY(output) == -1) { |
Victor Stinner | 065836e | 2011-10-27 01:56:33 +0200 | [diff] [blame] | 3804 | Py_DECREF(output); |
| 3805 | return 0; |
| 3806 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3807 | if (findchar(PyUnicode_DATA(output), PyUnicode_KIND(output), |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 3808 | PyUnicode_GET_LENGTH(output), 0, 1) >= 0) { |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3809 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
| 3810 | Py_DECREF(output); |
| 3811 | return 0; |
| 3812 | } |
| 3813 | *(PyObject**)addr = output; |
| 3814 | return Py_CLEANUP_SUPPORTED; |
| 3815 | } |
| 3816 | |
| 3817 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3818 | char* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3819 | PyUnicode_AsUTF8AndSize(PyObject *unicode, Py_ssize_t *psize) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3820 | { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 3821 | PyObject *bytes; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3822 | |
Neal Norwitz | e0a0a6e | 2007-08-25 01:04:21 +0000 | [diff] [blame] | 3823 | if (!PyUnicode_Check(unicode)) { |
| 3824 | PyErr_BadArgument(); |
| 3825 | return NULL; |
| 3826 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3827 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3828 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3829 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3830 | if (PyUnicode_UTF8(unicode) == NULL) { |
| 3831 | assert(!PyUnicode_IS_COMPACT_ASCII(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3832 | bytes = _PyUnicode_AsUTF8String(unicode, "strict"); |
| 3833 | if (bytes == NULL) |
| 3834 | return NULL; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3835 | _PyUnicode_UTF8(unicode) = PyObject_MALLOC(PyBytes_GET_SIZE(bytes) + 1); |
| 3836 | if (_PyUnicode_UTF8(unicode) == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3837 | Py_DECREF(bytes); |
| 3838 | return NULL; |
| 3839 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3840 | _PyUnicode_UTF8_LENGTH(unicode) = PyBytes_GET_SIZE(bytes); |
| 3841 | Py_MEMCPY(_PyUnicode_UTF8(unicode), |
| 3842 | PyBytes_AS_STRING(bytes), |
| 3843 | _PyUnicode_UTF8_LENGTH(unicode) + 1); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3844 | Py_DECREF(bytes); |
| 3845 | } |
| 3846 | |
| 3847 | if (psize) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3848 | *psize = PyUnicode_UTF8_LENGTH(unicode); |
| 3849 | return PyUnicode_UTF8(unicode); |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 3850 | } |
| 3851 | |
| 3852 | char* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3853 | PyUnicode_AsUTF8(PyObject *unicode) |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 3854 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3855 | return PyUnicode_AsUTF8AndSize(unicode, NULL); |
| 3856 | } |
| 3857 | |
| 3858 | #ifdef Py_DEBUG |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 3859 | static int unicode_as_unicode_calls = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3860 | #endif |
| 3861 | |
| 3862 | |
| 3863 | Py_UNICODE * |
| 3864 | PyUnicode_AsUnicodeAndSize(PyObject *unicode, Py_ssize_t *size) |
| 3865 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3866 | const unsigned char *one_byte; |
| 3867 | #if SIZEOF_WCHAR_T == 4 |
| 3868 | const Py_UCS2 *two_bytes; |
| 3869 | #else |
| 3870 | const Py_UCS4 *four_bytes; |
| 3871 | const Py_UCS4 *ucs4_end; |
| 3872 | Py_ssize_t num_surrogates; |
| 3873 | #endif |
| 3874 | wchar_t *w; |
| 3875 | wchar_t *wchar_end; |
| 3876 | |
| 3877 | if (!PyUnicode_Check(unicode)) { |
| 3878 | PyErr_BadArgument(); |
| 3879 | return NULL; |
| 3880 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3881 | if (_PyUnicode_WSTR(unicode) == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3882 | /* Non-ASCII compact unicode object */ |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3883 | assert(_PyUnicode_KIND(unicode) != 0); |
| 3884 | assert(PyUnicode_IS_READY(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3885 | |
| 3886 | #ifdef Py_DEBUG |
| 3887 | ++unicode_as_unicode_calls; |
| 3888 | #endif |
| 3889 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3890 | if (PyUnicode_KIND(unicode) == PyUnicode_4BYTE_KIND) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3891 | #if SIZEOF_WCHAR_T == 2 |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3892 | four_bytes = PyUnicode_4BYTE_DATA(unicode); |
| 3893 | ucs4_end = four_bytes + _PyUnicode_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3894 | num_surrogates = 0; |
| 3895 | |
| 3896 | for (; four_bytes < ucs4_end; ++four_bytes) { |
| 3897 | if (*four_bytes > 0xFFFF) |
| 3898 | ++num_surrogates; |
| 3899 | } |
| 3900 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3901 | _PyUnicode_WSTR(unicode) = (wchar_t *) PyObject_MALLOC( |
| 3902 | sizeof(wchar_t) * (_PyUnicode_LENGTH(unicode) + 1 + num_surrogates)); |
| 3903 | if (!_PyUnicode_WSTR(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3904 | PyErr_NoMemory(); |
| 3905 | return NULL; |
| 3906 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3907 | _PyUnicode_WSTR_LENGTH(unicode) = _PyUnicode_LENGTH(unicode) + num_surrogates; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3908 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3909 | w = _PyUnicode_WSTR(unicode); |
| 3910 | wchar_end = w + _PyUnicode_WSTR_LENGTH(unicode); |
| 3911 | four_bytes = PyUnicode_4BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3912 | for (; four_bytes < ucs4_end; ++four_bytes, ++w) { |
| 3913 | if (*four_bytes > 0xFFFF) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 3914 | assert(*four_bytes <= MAX_UNICODE); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3915 | /* encode surrogate pair in this case */ |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 3916 | *w++ = Py_UNICODE_HIGH_SURROGATE(*four_bytes); |
| 3917 | *w = Py_UNICODE_LOW_SURROGATE(*four_bytes); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3918 | } |
| 3919 | else |
| 3920 | *w = *four_bytes; |
| 3921 | |
| 3922 | if (w > wchar_end) { |
| 3923 | assert(0 && "Miscalculated string end"); |
| 3924 | } |
| 3925 | } |
| 3926 | *w = 0; |
| 3927 | #else |
| 3928 | /* sizeof(wchar_t) == 4 */ |
| 3929 | Py_FatalError("Impossible unicode object state, wstr and str " |
| 3930 | "should share memory already."); |
| 3931 | return NULL; |
| 3932 | #endif |
| 3933 | } |
| 3934 | else { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3935 | _PyUnicode_WSTR(unicode) = (wchar_t *) PyObject_MALLOC(sizeof(wchar_t) * |
| 3936 | (_PyUnicode_LENGTH(unicode) + 1)); |
| 3937 | if (!_PyUnicode_WSTR(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3938 | PyErr_NoMemory(); |
| 3939 | return NULL; |
| 3940 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3941 | if (!PyUnicode_IS_COMPACT_ASCII(unicode)) |
| 3942 | _PyUnicode_WSTR_LENGTH(unicode) = _PyUnicode_LENGTH(unicode); |
| 3943 | w = _PyUnicode_WSTR(unicode); |
| 3944 | wchar_end = w + _PyUnicode_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3945 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3946 | if (PyUnicode_KIND(unicode) == PyUnicode_1BYTE_KIND) { |
| 3947 | one_byte = PyUnicode_1BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3948 | for (; w < wchar_end; ++one_byte, ++w) |
| 3949 | *w = *one_byte; |
| 3950 | /* null-terminate the wstr */ |
| 3951 | *w = 0; |
| 3952 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3953 | else if (PyUnicode_KIND(unicode) == PyUnicode_2BYTE_KIND) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3954 | #if SIZEOF_WCHAR_T == 4 |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3955 | two_bytes = PyUnicode_2BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3956 | for (; w < wchar_end; ++two_bytes, ++w) |
| 3957 | *w = *two_bytes; |
| 3958 | /* null-terminate the wstr */ |
| 3959 | *w = 0; |
| 3960 | #else |
| 3961 | /* sizeof(wchar_t) == 2 */ |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3962 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 3963 | _PyUnicode_WSTR(unicode) = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3964 | Py_FatalError("Impossible unicode object state, wstr " |
| 3965 | "and str should share memory already."); |
| 3966 | return NULL; |
| 3967 | #endif |
| 3968 | } |
| 3969 | else { |
| 3970 | assert(0 && "This should never happen."); |
| 3971 | } |
| 3972 | } |
| 3973 | } |
| 3974 | if (size != NULL) |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3975 | *size = PyUnicode_WSTR_LENGTH(unicode); |
| 3976 | return _PyUnicode_WSTR(unicode); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3977 | } |
| 3978 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3979 | Py_UNICODE * |
| 3980 | PyUnicode_AsUnicode(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3981 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3982 | return PyUnicode_AsUnicodeAndSize(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3983 | } |
| 3984 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3985 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3986 | Py_ssize_t |
| 3987 | PyUnicode_GetSize(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3988 | { |
| 3989 | if (!PyUnicode_Check(unicode)) { |
| 3990 | PyErr_BadArgument(); |
| 3991 | goto onError; |
| 3992 | } |
| 3993 | return PyUnicode_GET_SIZE(unicode); |
| 3994 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3995 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3996 | return -1; |
| 3997 | } |
| 3998 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3999 | Py_ssize_t |
| 4000 | PyUnicode_GetLength(PyObject *unicode) |
| 4001 | { |
Victor Stinner | 5a706cf | 2011-10-02 00:36:53 +0200 | [diff] [blame] | 4002 | if (!PyUnicode_Check(unicode) || PyUnicode_READY(unicode) == -1) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4003 | PyErr_BadArgument(); |
| 4004 | return -1; |
| 4005 | } |
| 4006 | |
| 4007 | return PyUnicode_GET_LENGTH(unicode); |
| 4008 | } |
| 4009 | |
| 4010 | Py_UCS4 |
| 4011 | PyUnicode_ReadChar(PyObject *unicode, Py_ssize_t index) |
| 4012 | { |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 4013 | if (!PyUnicode_Check(unicode) || PyUnicode_READY(unicode) == -1) { |
| 4014 | PyErr_BadArgument(); |
| 4015 | return (Py_UCS4)-1; |
| 4016 | } |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 4017 | if (index < 0 || index >= PyUnicode_GET_LENGTH(unicode)) { |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 4018 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4019 | return (Py_UCS4)-1; |
| 4020 | } |
| 4021 | return PyUnicode_READ_CHAR(unicode, index); |
| 4022 | } |
| 4023 | |
| 4024 | int |
| 4025 | PyUnicode_WriteChar(PyObject *unicode, Py_ssize_t index, Py_UCS4 ch) |
| 4026 | { |
| 4027 | if (!PyUnicode_Check(unicode) || !PyUnicode_IS_COMPACT(unicode)) { |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 4028 | PyErr_BadArgument(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4029 | return -1; |
| 4030 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 4031 | assert(PyUnicode_IS_READY(unicode)); |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 4032 | if (index < 0 || index >= PyUnicode_GET_LENGTH(unicode)) { |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 4033 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 4034 | return -1; |
| 4035 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 4036 | if (unicode_check_modifiable(unicode)) |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 4037 | return -1; |
Victor Stinner | c9590ad | 2012-03-04 01:34:37 +0100 | [diff] [blame] | 4038 | if (ch > PyUnicode_MAX_CHAR_VALUE(unicode)) { |
| 4039 | PyErr_SetString(PyExc_ValueError, "character out of range"); |
| 4040 | return -1; |
| 4041 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4042 | PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode), |
| 4043 | index, ch); |
| 4044 | return 0; |
| 4045 | } |
| 4046 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4047 | const char * |
| 4048 | PyUnicode_GetDefaultEncoding(void) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 4049 | { |
Victor Stinner | 42cb462 | 2010-09-01 19:39:01 +0000 | [diff] [blame] | 4050 | return "utf-8"; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 4051 | } |
| 4052 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4053 | /* create or adjust a UnicodeDecodeError */ |
| 4054 | static void |
| 4055 | make_decode_exception(PyObject **exceptionObject, |
| 4056 | const char *encoding, |
| 4057 | const char *input, Py_ssize_t length, |
| 4058 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 4059 | const char *reason) |
| 4060 | { |
| 4061 | if (*exceptionObject == NULL) { |
| 4062 | *exceptionObject = PyUnicodeDecodeError_Create( |
| 4063 | encoding, input, length, startpos, endpos, reason); |
| 4064 | } |
| 4065 | else { |
| 4066 | if (PyUnicodeDecodeError_SetStart(*exceptionObject, startpos)) |
| 4067 | goto onError; |
| 4068 | if (PyUnicodeDecodeError_SetEnd(*exceptionObject, endpos)) |
| 4069 | goto onError; |
| 4070 | if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) |
| 4071 | goto onError; |
| 4072 | } |
| 4073 | return; |
| 4074 | |
| 4075 | onError: |
| 4076 | Py_DECREF(*exceptionObject); |
| 4077 | *exceptionObject = NULL; |
| 4078 | } |
| 4079 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4080 | /* error handling callback helper: |
| 4081 | build arguments, call the callback and check the arguments, |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 4082 | if no exception occurred, copy the replacement to the output |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4083 | and adjust various state variables. |
| 4084 | return 0 on success, -1 on error |
| 4085 | */ |
| 4086 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4087 | static int |
| 4088 | unicode_decode_call_errorhandler(const char *errors, PyObject **errorHandler, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4089 | const char *encoding, const char *reason, |
| 4090 | const char **input, const char **inend, Py_ssize_t *startinpos, |
| 4091 | Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4092 | PyObject **output, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4093 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 4094 | 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] | 4095 | |
| 4096 | PyObject *restuple = NULL; |
| 4097 | PyObject *repunicode = NULL; |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 4098 | Py_ssize_t outsize; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4099 | Py_ssize_t insize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4100 | Py_ssize_t requiredsize; |
| 4101 | Py_ssize_t newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4102 | PyObject *inputobj = NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4103 | int res = -1; |
| 4104 | |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 4105 | if (_PyUnicode_KIND(*output) != PyUnicode_WCHAR_KIND) |
| 4106 | outsize = PyUnicode_GET_LENGTH(*output); |
| 4107 | else |
| 4108 | outsize = _PyUnicode_WSTR_LENGTH(*output); |
| 4109 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4110 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4111 | *errorHandler = PyCodec_LookupError(errors); |
| 4112 | if (*errorHandler == NULL) |
| 4113 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4114 | } |
| 4115 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4116 | make_decode_exception(exceptionObject, |
| 4117 | encoding, |
| 4118 | *input, *inend - *input, |
| 4119 | *startinpos, *endinpos, |
| 4120 | reason); |
| 4121 | if (*exceptionObject == NULL) |
| 4122 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4123 | |
| 4124 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 4125 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4126 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4127 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 4128 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4129 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4130 | } |
| 4131 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4132 | goto onError; |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 4133 | if (PyUnicode_READY(repunicode) == -1) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4134 | goto onError; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4135 | |
| 4136 | /* Copy back the bytes variables, which might have been modified by the |
| 4137 | callback */ |
| 4138 | inputobj = PyUnicodeDecodeError_GetObject(*exceptionObject); |
| 4139 | if (!inputobj) |
| 4140 | goto onError; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4141 | if (!PyBytes_Check(inputobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4142 | PyErr_Format(PyExc_TypeError, "exception attribute object must be bytes"); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4143 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4144 | *input = PyBytes_AS_STRING(inputobj); |
| 4145 | insize = PyBytes_GET_SIZE(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4146 | *inend = *input + insize; |
Walter Dörwald | 36f938f | 2007-08-10 10:11:43 +0000 | [diff] [blame] | 4147 | /* we can DECREF safely, as the exception has another reference, |
| 4148 | so the object won't go away. */ |
| 4149 | Py_DECREF(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4150 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4151 | if (newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4152 | newpos = insize+newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4153 | if (newpos<0 || newpos>insize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4154 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", newpos); |
| 4155 | goto onError; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4156 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4157 | |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 4158 | if (_PyUnicode_KIND(*output) != PyUnicode_WCHAR_KIND) { |
| 4159 | /* need more space? (at least enough for what we |
| 4160 | have+the replacement+the rest of the string (starting |
| 4161 | at the new input position), so we won't have to check space |
| 4162 | when there are no errors in the rest of the string) */ |
| 4163 | Py_ssize_t replen = PyUnicode_GET_LENGTH(repunicode); |
| 4164 | requiredsize = *outpos + replen + insize-newpos; |
| 4165 | if (requiredsize > outsize) { |
| 4166 | if (requiredsize<2*outsize) |
| 4167 | requiredsize = 2*outsize; |
| 4168 | if (unicode_resize(output, requiredsize) < 0) |
| 4169 | goto onError; |
| 4170 | } |
Victor Stinner | 1b487b4 | 2012-05-03 12:29:04 +0200 | [diff] [blame] | 4171 | if (unicode_widen(output, *outpos, |
| 4172 | PyUnicode_MAX_CHAR_VALUE(repunicode)) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4173 | goto onError; |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 4174 | copy_characters(*output, *outpos, repunicode, 0, replen); |
| 4175 | *outpos += replen; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4176 | } |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 4177 | else { |
| 4178 | wchar_t *repwstr; |
| 4179 | Py_ssize_t repwlen; |
| 4180 | repwstr = PyUnicode_AsUnicodeAndSize(repunicode, &repwlen); |
| 4181 | if (repwstr == NULL) |
| 4182 | goto onError; |
| 4183 | /* need more space? (at least enough for what we |
| 4184 | have+the replacement+the rest of the string (starting |
| 4185 | at the new input position), so we won't have to check space |
| 4186 | when there are no errors in the rest of the string) */ |
| 4187 | requiredsize = *outpos + repwlen + insize-newpos; |
| 4188 | if (requiredsize > outsize) { |
| 4189 | if (requiredsize < 2*outsize) |
| 4190 | requiredsize = 2*outsize; |
| 4191 | if (unicode_resize(output, requiredsize) < 0) |
| 4192 | goto onError; |
| 4193 | } |
| 4194 | wcsncpy(_PyUnicode_WSTR(*output) + *outpos, repwstr, repwlen); |
| 4195 | *outpos += repwlen; |
| 4196 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4197 | *endinpos = newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4198 | *inptr = *input + newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4199 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4200 | /* we made it! */ |
| 4201 | res = 0; |
| 4202 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4203 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4204 | Py_XDECREF(restuple); |
| 4205 | return res; |
| 4206 | } |
| 4207 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4208 | /* --- UTF-7 Codec -------------------------------------------------------- */ |
| 4209 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4210 | /* See RFC2152 for details. We encode conservatively and decode liberally. */ |
| 4211 | |
| 4212 | /* Three simple macros defining base-64. */ |
| 4213 | |
| 4214 | /* Is c a base-64 character? */ |
| 4215 | |
| 4216 | #define IS_BASE64(c) \ |
| 4217 | (((c) >= 'A' && (c) <= 'Z') || \ |
| 4218 | ((c) >= 'a' && (c) <= 'z') || \ |
| 4219 | ((c) >= '0' && (c) <= '9') || \ |
| 4220 | (c) == '+' || (c) == '/') |
| 4221 | |
| 4222 | /* given that c is a base-64 character, what is its base-64 value? */ |
| 4223 | |
| 4224 | #define FROM_BASE64(c) \ |
| 4225 | (((c) >= 'A' && (c) <= 'Z') ? (c) - 'A' : \ |
| 4226 | ((c) >= 'a' && (c) <= 'z') ? (c) - 'a' + 26 : \ |
| 4227 | ((c) >= '0' && (c) <= '9') ? (c) - '0' + 52 : \ |
| 4228 | (c) == '+' ? 62 : 63) |
| 4229 | |
| 4230 | /* What is the base-64 character of the bottom 6 bits of n? */ |
| 4231 | |
| 4232 | #define TO_BASE64(n) \ |
| 4233 | ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) |
| 4234 | |
| 4235 | /* DECODE_DIRECT: this byte encountered in a UTF-7 string should be |
| 4236 | * decoded as itself. We are permissive on decoding; the only ASCII |
| 4237 | * byte not decoding to itself is the + which begins a base64 |
| 4238 | * string. */ |
| 4239 | |
| 4240 | #define DECODE_DIRECT(c) \ |
| 4241 | ((c) <= 127 && (c) != '+') |
| 4242 | |
| 4243 | /* The UTF-7 encoder treats ASCII characters differently according to |
| 4244 | * whether they are Set D, Set O, Whitespace, or special (i.e. none of |
| 4245 | * the above). See RFC2152. This array identifies these different |
| 4246 | * sets: |
| 4247 | * 0 : "Set D" |
| 4248 | * alphanumeric and '(),-./:? |
| 4249 | * 1 : "Set O" |
| 4250 | * !"#$%&*;<=>@[]^_`{|} |
| 4251 | * 2 : "whitespace" |
| 4252 | * ht nl cr sp |
| 4253 | * 3 : special (must be base64 encoded) |
| 4254 | * everything else (i.e. +\~ and non-printing codes 0-8 11-12 14-31 127) |
| 4255 | */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4256 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4257 | static |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4258 | char utf7_category[128] = { |
| 4259 | /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */ |
| 4260 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 3, 3, 2, 3, 3, |
| 4261 | /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */ |
| 4262 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 4263 | /* sp ! " # $ % & ' ( ) * + , - . / */ |
| 4264 | 2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 3, 0, 0, 0, 0, |
| 4265 | /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */ |
| 4266 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, |
| 4267 | /* @ A B C D E F G H I J K L M N O */ |
| 4268 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4269 | /* P Q R S T U V W X Y Z [ \ ] ^ _ */ |
| 4270 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 1, 1, 1, |
| 4271 | /* ` a b c d e f g h i j k l m n o */ |
| 4272 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4273 | /* p q r s t u v w x y z { | } ~ del */ |
| 4274 | 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] | 4275 | }; |
| 4276 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4277 | /* ENCODE_DIRECT: this character should be encoded as itself. The |
| 4278 | * answer depends on whether we are encoding set O as itself, and also |
| 4279 | * on whether we are encoding whitespace as itself. RFC2152 makes it |
| 4280 | * clear that the answers to these questions vary between |
| 4281 | * applications, so this code needs to be flexible. */ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 4282 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4283 | #define ENCODE_DIRECT(c, directO, directWS) \ |
| 4284 | ((c) < 128 && (c) > 0 && \ |
| 4285 | ((utf7_category[(c)] == 0) || \ |
| 4286 | (directWS && (utf7_category[(c)] == 2)) || \ |
| 4287 | (directO && (utf7_category[(c)] == 1)))) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4288 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4289 | PyObject * |
| 4290 | PyUnicode_DecodeUTF7(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4291 | Py_ssize_t size, |
| 4292 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4293 | { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4294 | return PyUnicode_DecodeUTF7Stateful(s, size, errors, NULL); |
| 4295 | } |
| 4296 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4297 | /* The decoder. The only state we preserve is our read position, |
| 4298 | * i.e. how many characters we have consumed. So if we end in the |
| 4299 | * middle of a shift sequence we have to back off the read position |
| 4300 | * and the output to the beginning of the sequence, otherwise we lose |
| 4301 | * all the shift state (seen bits, number of bits seen, high |
| 4302 | * surrogate). */ |
| 4303 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4304 | PyObject * |
| 4305 | PyUnicode_DecodeUTF7Stateful(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4306 | Py_ssize_t size, |
| 4307 | const char *errors, |
| 4308 | Py_ssize_t *consumed) |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4309 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4310 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4311 | Py_ssize_t startinpos; |
| 4312 | Py_ssize_t endinpos; |
| 4313 | Py_ssize_t outpos; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4314 | const char *e; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4315 | PyObject *unicode; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4316 | const char *errmsg = ""; |
| 4317 | int inShift = 0; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4318 | Py_ssize_t shiftOutStart; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4319 | unsigned int base64bits = 0; |
| 4320 | unsigned long base64buffer = 0; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 4321 | Py_UCS4 surrogate = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4322 | PyObject *errorHandler = NULL; |
| 4323 | PyObject *exc = NULL; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4324 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4325 | /* Start off assuming it's all ASCII. Widen later as necessary. */ |
| 4326 | unicode = PyUnicode_New(size, 127); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4327 | if (!unicode) |
| 4328 | return NULL; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4329 | if (size == 0) { |
| 4330 | if (consumed) |
| 4331 | *consumed = 0; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4332 | return unicode; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4333 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4334 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4335 | shiftOutStart = outpos = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4336 | e = s + size; |
| 4337 | |
| 4338 | while (s < e) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4339 | Py_UCS4 ch; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4340 | restart: |
Antoine Pitrou | 5ffd9e9 | 2008-07-25 18:05:24 +0000 | [diff] [blame] | 4341 | ch = (unsigned char) *s; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4342 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4343 | if (inShift) { /* in a base-64 section */ |
| 4344 | if (IS_BASE64(ch)) { /* consume a base-64 character */ |
| 4345 | base64buffer = (base64buffer << 6) | FROM_BASE64(ch); |
| 4346 | base64bits += 6; |
| 4347 | s++; |
| 4348 | if (base64bits >= 16) { |
| 4349 | /* we have enough bits for a UTF-16 value */ |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 4350 | Py_UCS4 outCh = (Py_UCS4)(base64buffer >> (base64bits-16)); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4351 | base64bits -= 16; |
| 4352 | base64buffer &= (1 << base64bits) - 1; /* clear high bits */ |
| 4353 | if (surrogate) { |
| 4354 | /* expecting a second surrogate */ |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 4355 | if (Py_UNICODE_IS_LOW_SURROGATE(outCh)) { |
| 4356 | Py_UCS4 ch2 = Py_UNICODE_JOIN_SURROGATES(surrogate, outCh); |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4357 | if (unicode_putchar(&unicode, &outpos, ch2) < 0) |
| 4358 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4359 | surrogate = 0; |
Antoine Pitrou | 5418ee0 | 2011-11-15 01:42:21 +0100 | [diff] [blame] | 4360 | continue; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4361 | } |
| 4362 | else { |
Antoine Pitrou | 78edf75 | 2011-11-15 01:44:16 +0100 | [diff] [blame] | 4363 | if (unicode_putchar(&unicode, &outpos, surrogate) < 0) |
| 4364 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4365 | surrogate = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4366 | } |
| 4367 | } |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 4368 | if (Py_UNICODE_IS_HIGH_SURROGATE(outCh)) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4369 | /* first surrogate */ |
| 4370 | surrogate = outCh; |
| 4371 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4372 | else { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4373 | if (unicode_putchar(&unicode, &outpos, outCh) < 0) |
| 4374 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4375 | } |
| 4376 | } |
| 4377 | } |
| 4378 | else { /* now leaving a base-64 section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4379 | inShift = 0; |
| 4380 | s++; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4381 | if (surrogate) { |
Antoine Pitrou | 78edf75 | 2011-11-15 01:44:16 +0100 | [diff] [blame] | 4382 | if (unicode_putchar(&unicode, &outpos, surrogate) < 0) |
| 4383 | goto onError; |
Antoine Pitrou | 5418ee0 | 2011-11-15 01:42:21 +0100 | [diff] [blame] | 4384 | surrogate = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4385 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4386 | if (base64bits > 0) { /* left-over bits */ |
| 4387 | if (base64bits >= 6) { |
| 4388 | /* We've seen at least one base-64 character */ |
| 4389 | errmsg = "partial character in shift sequence"; |
| 4390 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4391 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4392 | else { |
| 4393 | /* Some bits remain; they should be zero */ |
| 4394 | if (base64buffer != 0) { |
| 4395 | errmsg = "non-zero padding bits in shift sequence"; |
| 4396 | goto utf7Error; |
| 4397 | } |
| 4398 | } |
| 4399 | } |
| 4400 | if (ch != '-') { |
| 4401 | /* '-' is absorbed; other terminating |
| 4402 | characters are preserved */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4403 | if (unicode_putchar(&unicode, &outpos, ch) < 0) |
| 4404 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4405 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4406 | } |
| 4407 | } |
| 4408 | else if ( ch == '+' ) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4409 | startinpos = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4410 | s++; /* consume '+' */ |
| 4411 | if (s < e && *s == '-') { /* '+-' encodes '+' */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4412 | s++; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4413 | if (unicode_putchar(&unicode, &outpos, '+') < 0) |
| 4414 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4415 | } |
| 4416 | else { /* begin base64-encoded section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4417 | inShift = 1; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4418 | shiftOutStart = outpos; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4419 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4420 | } |
| 4421 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4422 | else if (DECODE_DIRECT(ch)) { /* character decodes as itself */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4423 | if (unicode_putchar(&unicode, &outpos, ch) < 0) |
| 4424 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4425 | s++; |
| 4426 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4427 | else { |
| 4428 | startinpos = s-starts; |
| 4429 | s++; |
| 4430 | errmsg = "unexpected special character"; |
| 4431 | goto utf7Error; |
| 4432 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4433 | continue; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4434 | utf7Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4435 | endinpos = s-starts; |
| 4436 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4437 | errors, &errorHandler, |
| 4438 | "utf7", errmsg, |
| 4439 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4440 | &unicode, &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4441 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4442 | } |
| 4443 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4444 | /* end of string */ |
| 4445 | |
| 4446 | if (inShift && !consumed) { /* in shift sequence, no more to follow */ |
| 4447 | /* if we're in an inconsistent state, that's an error */ |
| 4448 | if (surrogate || |
| 4449 | (base64bits >= 6) || |
| 4450 | (base64bits > 0 && base64buffer != 0)) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4451 | endinpos = size; |
| 4452 | if (unicode_decode_call_errorhandler( |
| 4453 | errors, &errorHandler, |
| 4454 | "utf7", "unterminated shift sequence", |
| 4455 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4456 | &unicode, &outpos)) |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4457 | goto onError; |
| 4458 | if (s < e) |
| 4459 | goto restart; |
| 4460 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4461 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4462 | |
| 4463 | /* return state */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4464 | if (consumed) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4465 | if (inShift) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4466 | outpos = shiftOutStart; /* back off output */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4467 | *consumed = startinpos; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4468 | } |
| 4469 | else { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4470 | *consumed = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4471 | } |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4472 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4473 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4474 | if (unicode_resize(&unicode, outpos) < 0) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4475 | goto onError; |
| 4476 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4477 | Py_XDECREF(errorHandler); |
| 4478 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 4479 | return unicode_result(unicode); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4480 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4481 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4482 | Py_XDECREF(errorHandler); |
| 4483 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4484 | Py_DECREF(unicode); |
| 4485 | return NULL; |
| 4486 | } |
| 4487 | |
| 4488 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4489 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4490 | _PyUnicode_EncodeUTF7(PyObject *str, |
| 4491 | int base64SetO, |
| 4492 | int base64WhiteSpace, |
| 4493 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4494 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4495 | int kind; |
| 4496 | void *data; |
| 4497 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4498 | PyObject *v; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4499 | Py_ssize_t allocated; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4500 | int inShift = 0; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4501 | Py_ssize_t i; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4502 | unsigned int base64bits = 0; |
| 4503 | unsigned long base64buffer = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4504 | char * out; |
| 4505 | char * start; |
| 4506 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 4507 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4508 | return NULL; |
| 4509 | kind = PyUnicode_KIND(str); |
| 4510 | data = PyUnicode_DATA(str); |
| 4511 | len = PyUnicode_GET_LENGTH(str); |
| 4512 | |
| 4513 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4514 | return PyBytes_FromStringAndSize(NULL, 0); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4515 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4516 | /* It might be possible to tighten this worst case */ |
| 4517 | allocated = 8 * len; |
| 4518 | if (allocated / 8 != len) |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4519 | return PyErr_NoMemory(); |
| 4520 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4521 | v = PyBytes_FromStringAndSize(NULL, allocated); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4522 | if (v == NULL) |
| 4523 | return NULL; |
| 4524 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4525 | start = out = PyBytes_AS_STRING(v); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4526 | for (i = 0; i < len; ++i) { |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 4527 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4528 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4529 | if (inShift) { |
| 4530 | if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 4531 | /* shifting out */ |
| 4532 | if (base64bits) { /* output remaining bits */ |
| 4533 | *out++ = TO_BASE64(base64buffer << (6-base64bits)); |
| 4534 | base64buffer = 0; |
| 4535 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4536 | } |
| 4537 | inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4538 | /* Characters not in the BASE64 set implicitly unshift the sequence |
| 4539 | so no '-' is required, except if the character is itself a '-' */ |
| 4540 | if (IS_BASE64(ch) || ch == '-') { |
| 4541 | *out++ = '-'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4542 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4543 | *out++ = (char) ch; |
| 4544 | } |
| 4545 | else { |
| 4546 | goto encode_char; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4547 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4548 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4549 | else { /* not in a shift sequence */ |
| 4550 | if (ch == '+') { |
| 4551 | *out++ = '+'; |
| 4552 | *out++ = '-'; |
| 4553 | } |
| 4554 | else if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 4555 | *out++ = (char) ch; |
| 4556 | } |
| 4557 | else { |
| 4558 | *out++ = '+'; |
| 4559 | inShift = 1; |
| 4560 | goto encode_char; |
| 4561 | } |
| 4562 | } |
| 4563 | continue; |
| 4564 | encode_char: |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4565 | if (ch >= 0x10000) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 4566 | assert(ch <= MAX_UNICODE); |
Victor Stinner | 0d3721d | 2011-11-22 03:27:53 +0100 | [diff] [blame] | 4567 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4568 | /* code first surrogate */ |
| 4569 | base64bits += 16; |
| 4570 | base64buffer = (base64buffer << 16) | 0xd800 | ((ch-0x10000) >> 10); |
| 4571 | while (base64bits >= 6) { |
| 4572 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 4573 | base64bits -= 6; |
| 4574 | } |
| 4575 | /* prepare second surrogate */ |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 4576 | ch = Py_UNICODE_LOW_SURROGATE(ch); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4577 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4578 | base64bits += 16; |
| 4579 | base64buffer = (base64buffer << 16) | ch; |
| 4580 | while (base64bits >= 6) { |
| 4581 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 4582 | base64bits -= 6; |
| 4583 | } |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 4584 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4585 | if (base64bits) |
| 4586 | *out++= TO_BASE64(base64buffer << (6-base64bits) ); |
| 4587 | if (inShift) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4588 | *out++ = '-'; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4589 | if (_PyBytes_Resize(&v, out - start) < 0) |
| 4590 | return NULL; |
| 4591 | return v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4592 | } |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4593 | PyObject * |
| 4594 | PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
| 4595 | Py_ssize_t size, |
| 4596 | int base64SetO, |
| 4597 | int base64WhiteSpace, |
| 4598 | const char *errors) |
| 4599 | { |
| 4600 | PyObject *result; |
| 4601 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 4602 | if (tmp == NULL) |
| 4603 | return NULL; |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 4604 | result = _PyUnicode_EncodeUTF7(tmp, base64SetO, |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4605 | base64WhiteSpace, errors); |
| 4606 | Py_DECREF(tmp); |
| 4607 | return result; |
| 4608 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4609 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4610 | #undef IS_BASE64 |
| 4611 | #undef FROM_BASE64 |
| 4612 | #undef TO_BASE64 |
| 4613 | #undef DECODE_DIRECT |
| 4614 | #undef ENCODE_DIRECT |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4615 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4616 | /* --- UTF-8 Codec -------------------------------------------------------- */ |
| 4617 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4618 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4619 | char utf8_code_length[256] = { |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4620 | /* Map UTF-8 encoded prefix byte to sequence length. Zero means |
| 4621 | illegal prefix. See RFC 3629 for details */ |
| 4622 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 00-0F */ |
| 4623 | 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] | 4624 | 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] | 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, |
| 4628 | 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] | 4629 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 70-7F */ |
| 4630 | 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] | 4631 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4632 | 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] | 4633 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* B0-BF */ |
| 4634 | 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* C0-C1 + C2-CF */ |
| 4635 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* D0-DF */ |
| 4636 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* E0-EF */ |
| 4637 | 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] | 4638 | }; |
| 4639 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4640 | PyObject * |
| 4641 | PyUnicode_DecodeUTF8(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4642 | Py_ssize_t size, |
| 4643 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4644 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4645 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
| 4646 | } |
| 4647 | |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4648 | #include "stringlib/ucs1lib.h" |
| 4649 | #include "stringlib/codecs.h" |
| 4650 | #include "stringlib/undef.h" |
| 4651 | |
| 4652 | #include "stringlib/ucs2lib.h" |
| 4653 | #include "stringlib/codecs.h" |
| 4654 | #include "stringlib/undef.h" |
| 4655 | |
| 4656 | #include "stringlib/ucs4lib.h" |
| 4657 | #include "stringlib/codecs.h" |
| 4658 | #include "stringlib/undef.h" |
| 4659 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4660 | /* Mask to check or force alignment of a pointer to C 'long' boundaries */ |
| 4661 | #define LONG_PTR_MASK (size_t) (SIZEOF_LONG - 1) |
| 4662 | |
| 4663 | /* Mask to quickly check whether a C 'long' contains a |
| 4664 | non-ASCII, UTF8-encoded char. */ |
| 4665 | #if (SIZEOF_LONG == 8) |
| 4666 | # define ASCII_CHAR_MASK 0x8080808080808080L |
| 4667 | #elif (SIZEOF_LONG == 4) |
| 4668 | # define ASCII_CHAR_MASK 0x80808080L |
| 4669 | #else |
| 4670 | # error C 'long' size should be either 4 or 8! |
| 4671 | #endif |
| 4672 | |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4673 | /* Scans a UTF-8 string and returns the maximum character to be expected |
| 4674 | and the size of the decoded unicode string. |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4675 | |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4676 | 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] | 4677 | PyUnicode_DecodeUTF8Stateful. |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4678 | */ |
| 4679 | static Py_UCS4 |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 4680 | 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] | 4681 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4682 | Py_ssize_t char_count = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4683 | const unsigned char *end = p + string_size; |
| 4684 | 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] | 4685 | |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4686 | assert(unicode_size != NULL); |
| 4687 | |
| 4688 | /* By having a cascade of independent loops which fallback onto each |
| 4689 | other, we minimize the amount of work done in the average loop |
| 4690 | iteration, and we also maximize the CPU's ability to predict |
| 4691 | branches correctly (because a given condition will have always the |
| 4692 | same boolean outcome except perhaps in the last iteration of the |
| 4693 | corresponding loop). |
| 4694 | In the general case this brings us rather close to decoding |
| 4695 | performance pre-PEP 393, despite the two-pass decoding. |
| 4696 | |
| 4697 | Note that the pure ASCII loop is not duplicated once a non-ASCII |
| 4698 | character has been encountered. It is actually a pessimization (by |
| 4699 | a significant factor) to use this loop on text with many non-ASCII |
| 4700 | characters, and it is important to avoid bad performance on valid |
| 4701 | utf-8 data (invalid utf-8 being a different can of worms). |
| 4702 | */ |
| 4703 | |
| 4704 | /* ASCII */ |
| 4705 | for (; p < end; ++p) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4706 | /* Only check value if it's not a ASCII char... */ |
| 4707 | if (*p < 0x80) { |
| 4708 | /* Fast path, see below in PyUnicode_DecodeUTF8Stateful for |
| 4709 | an explanation. */ |
| 4710 | if (!((size_t) p & LONG_PTR_MASK)) { |
| 4711 | /* Help register allocation */ |
| 4712 | register const unsigned char *_p = p; |
| 4713 | while (_p < aligned_end) { |
| 4714 | unsigned long value = *(unsigned long *) _p; |
| 4715 | if (value & ASCII_CHAR_MASK) |
| 4716 | break; |
| 4717 | _p += SIZEOF_LONG; |
| 4718 | char_count += SIZEOF_LONG; |
| 4719 | } |
| 4720 | p = _p; |
| 4721 | if (p == end) |
| 4722 | break; |
| 4723 | } |
| 4724 | } |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4725 | if (*p < 0x80) |
| 4726 | ++char_count; |
| 4727 | else |
| 4728 | goto _ucs1loop; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4729 | } |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4730 | *unicode_size = char_count; |
| 4731 | return 127; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4732 | |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4733 | _ucs1loop: |
| 4734 | for (; p < end; ++p) { |
| 4735 | if (*p < 0xc4) |
| 4736 | char_count += ((*p & 0xc0) != 0x80); |
| 4737 | else |
| 4738 | goto _ucs2loop; |
| 4739 | } |
| 4740 | *unicode_size = char_count; |
| 4741 | return 255; |
| 4742 | |
| 4743 | _ucs2loop: |
| 4744 | for (; p < end; ++p) { |
| 4745 | if (*p < 0xf0) |
| 4746 | char_count += ((*p & 0xc0) != 0x80); |
| 4747 | else |
| 4748 | goto _ucs4loop; |
| 4749 | } |
| 4750 | *unicode_size = char_count; |
| 4751 | return 65535; |
| 4752 | |
| 4753 | _ucs4loop: |
| 4754 | for (; p < end; ++p) { |
| 4755 | char_count += ((*p & 0xc0) != 0x80); |
| 4756 | } |
| 4757 | *unicode_size = char_count; |
| 4758 | return 65537; |
| 4759 | } |
| 4760 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4761 | /* 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] | 4762 | in case of errors. Implicit parameters: unicode, kind, data, onError. |
| 4763 | 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] | 4764 | */ |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4765 | #define WRITE_MAYBE_FAIL(index, value) \ |
| 4766 | do { \ |
| 4767 | Py_ssize_t pos = index; \ |
| 4768 | if (pos > PyUnicode_GET_LENGTH(unicode) && \ |
| 4769 | unicode_resize(&unicode, pos + pos/8) < 0) \ |
| 4770 | goto onError; \ |
| 4771 | if (unicode_putchar(&unicode, &pos, value) < 0) \ |
| 4772 | goto onError; \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4773 | } while (0) |
| 4774 | |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 4775 | static PyObject * |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4776 | decode_utf8_errors(const char *starts, |
| 4777 | Py_ssize_t size, |
| 4778 | const char *errors, |
| 4779 | Py_ssize_t *consumed, |
| 4780 | const char *s, |
| 4781 | PyObject *unicode, |
| 4782 | Py_ssize_t i) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4783 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4784 | int n; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4785 | int k; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4786 | Py_ssize_t startinpos; |
| 4787 | Py_ssize_t endinpos; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4788 | const char *e = starts + size; |
| 4789 | const char *aligned_end; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4790 | const char *errmsg = ""; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4791 | PyObject *errorHandler = NULL; |
| 4792 | PyObject *exc = NULL; |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4793 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4794 | aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4795 | |
| 4796 | while (s < e) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 4797 | Py_UCS4 ch = (unsigned char)*s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4798 | |
| 4799 | if (ch < 0x80) { |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4800 | /* Fast path for runs of ASCII characters. Given that common UTF-8 |
| 4801 | input will consist of an overwhelming majority of ASCII |
| 4802 | characters, we try to optimize for this case by checking |
| 4803 | as many characters as a C 'long' can contain. |
| 4804 | First, check if we can do an aligned read, as most CPUs have |
| 4805 | a penalty for unaligned reads. |
| 4806 | */ |
| 4807 | if (!((size_t) s & LONG_PTR_MASK)) { |
| 4808 | /* Help register allocation */ |
| 4809 | register const char *_s = s; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4810 | register Py_ssize_t _i = i; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4811 | while (_s < aligned_end) { |
| 4812 | /* Read a whole long at a time (either 4 or 8 bytes), |
| 4813 | and do a fast unrolled copy if it only contains ASCII |
| 4814 | characters. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4815 | unsigned long value = *(unsigned long *) _s; |
| 4816 | if (value & ASCII_CHAR_MASK) |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4817 | break; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4818 | WRITE_MAYBE_FAIL(_i+0, _s[0]); |
| 4819 | WRITE_MAYBE_FAIL(_i+1, _s[1]); |
| 4820 | WRITE_MAYBE_FAIL(_i+2, _s[2]); |
| 4821 | WRITE_MAYBE_FAIL(_i+3, _s[3]); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4822 | #if (SIZEOF_LONG == 8) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4823 | WRITE_MAYBE_FAIL(_i+4, _s[4]); |
| 4824 | WRITE_MAYBE_FAIL(_i+5, _s[5]); |
| 4825 | WRITE_MAYBE_FAIL(_i+6, _s[6]); |
| 4826 | WRITE_MAYBE_FAIL(_i+7, _s[7]); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4827 | #endif |
| 4828 | _s += SIZEOF_LONG; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4829 | _i += SIZEOF_LONG; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4830 | } |
| 4831 | s = _s; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4832 | i = _i; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4833 | if (s == e) |
| 4834 | break; |
| 4835 | ch = (unsigned char)*s; |
| 4836 | } |
| 4837 | } |
| 4838 | |
| 4839 | if (ch < 0x80) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4840 | WRITE_MAYBE_FAIL(i++, ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4841 | s++; |
| 4842 | continue; |
| 4843 | } |
| 4844 | |
| 4845 | n = utf8_code_length[ch]; |
| 4846 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4847 | if (s + n > e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4848 | if (consumed) |
| 4849 | break; |
| 4850 | else { |
| 4851 | errmsg = "unexpected end of data"; |
| 4852 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4853 | endinpos = startinpos+1; |
| 4854 | for (k=1; (k < size-startinpos) && ((s[k]&0xC0) == 0x80); k++) |
| 4855 | endinpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4856 | goto utf8Error; |
| 4857 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4858 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4859 | |
| 4860 | switch (n) { |
| 4861 | |
| 4862 | case 0: |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4863 | errmsg = "invalid start byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4864 | startinpos = s-starts; |
| 4865 | endinpos = startinpos+1; |
| 4866 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4867 | |
| 4868 | case 1: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4869 | errmsg = "internal error"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4870 | startinpos = s-starts; |
| 4871 | endinpos = startinpos+1; |
| 4872 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4873 | |
| 4874 | case 2: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4875 | if ((s[1] & 0xc0) != 0x80) { |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4876 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4877 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4878 | endinpos = startinpos + 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4879 | goto utf8Error; |
| 4880 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4881 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4882 | assert ((ch > 0x007F) && (ch <= 0x07FF)); |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4883 | WRITE_MAYBE_FAIL(i++, ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4884 | break; |
| 4885 | |
| 4886 | case 3: |
Ezio Melotti | 9bf2b3a | 2010-07-03 04:52:19 +0000 | [diff] [blame] | 4887 | /* Decoding UTF-8 sequences in range \xed\xa0\x80-\xed\xbf\xbf |
| 4888 | will result in surrogates in range d800-dfff. Surrogates are |
| 4889 | not valid UTF-8 so they are rejected. |
| 4890 | See http://www.unicode.org/versions/Unicode5.2.0/ch03.pdf |
| 4891 | (table 3-7) and http://www.rfc-editor.org/rfc/rfc3629.txt */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4892 | if ((s[1] & 0xc0) != 0x80 || |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4893 | (s[2] & 0xc0) != 0x80 || |
| 4894 | ((unsigned char)s[0] == 0xE0 && |
| 4895 | (unsigned char)s[1] < 0xA0) || |
| 4896 | ((unsigned char)s[0] == 0xED && |
| 4897 | (unsigned char)s[1] > 0x9F)) { |
| 4898 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4899 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4900 | endinpos = startinpos + 1; |
| 4901 | |
| 4902 | /* if s[1] first two bits are 1 and 0, then the invalid |
| 4903 | continuation byte is s[2], so increment endinpos by 1, |
| 4904 | if not, s[1] is invalid and endinpos doesn't need to |
| 4905 | be incremented. */ |
| 4906 | if ((s[1] & 0xC0) == 0x80) |
| 4907 | endinpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4908 | goto utf8Error; |
| 4909 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4910 | ch = ((s[0] & 0x0f) << 12) + ((s[1] & 0x3f) << 6) + (s[2] & 0x3f); |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4911 | assert ((ch > 0x07FF) && (ch <= 0xFFFF)); |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4912 | WRITE_MAYBE_FAIL(i++, ch); |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 4913 | break; |
| 4914 | |
| 4915 | case 4: |
| 4916 | if ((s[1] & 0xc0) != 0x80 || |
| 4917 | (s[2] & 0xc0) != 0x80 || |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4918 | (s[3] & 0xc0) != 0x80 || |
| 4919 | ((unsigned char)s[0] == 0xF0 && |
| 4920 | (unsigned char)s[1] < 0x90) || |
| 4921 | ((unsigned char)s[0] == 0xF4 && |
| 4922 | (unsigned char)s[1] > 0x8F)) { |
| 4923 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4924 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4925 | endinpos = startinpos + 1; |
| 4926 | if ((s[1] & 0xC0) == 0x80) { |
| 4927 | endinpos++; |
| 4928 | if ((s[2] & 0xC0) == 0x80) |
| 4929 | endinpos++; |
| 4930 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4931 | goto utf8Error; |
| 4932 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 4933 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4934 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 4935 | assert ((ch > 0xFFFF) && (ch <= MAX_UNICODE)); |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4936 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4937 | WRITE_MAYBE_FAIL(i++, ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4938 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4939 | } |
| 4940 | s += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4941 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4942 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4943 | utf8Error: |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4944 | if (unicode_decode_call_errorhandler( |
| 4945 | errors, &errorHandler, |
Victor Stinner | cbe0134 | 2012-02-14 01:17:45 +0100 | [diff] [blame] | 4946 | "utf-8", errmsg, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4947 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4948 | &unicode, &i)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4949 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4950 | /* Update data because unicode_decode_call_errorhandler might have |
| 4951 | re-created or resized the unicode object. */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4952 | aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4953 | } |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4954 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4955 | *consumed = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4956 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4957 | /* Adjust length and ready string when it contained errors and |
| 4958 | is of the old resizable kind. */ |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4959 | if (unicode_resize(&unicode, i) < 0) |
| 4960 | goto onError; |
| 4961 | unicode_adjust_maxchar(&unicode); |
| 4962 | if (unicode == NULL) |
| 4963 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4964 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4965 | Py_XDECREF(errorHandler); |
| 4966 | Py_XDECREF(exc); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 4967 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4968 | return unicode; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4969 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4970 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4971 | Py_XDECREF(errorHandler); |
| 4972 | Py_XDECREF(exc); |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4973 | Py_XDECREF(unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4974 | return NULL; |
| 4975 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4976 | #undef WRITE_MAYBE_FAIL |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4977 | |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4978 | PyObject * |
| 4979 | PyUnicode_DecodeUTF8Stateful(const char *s, |
| 4980 | Py_ssize_t size, |
| 4981 | const char *errors, |
| 4982 | Py_ssize_t *consumed) |
| 4983 | { |
| 4984 | Py_UCS4 maxchar = 0; |
| 4985 | Py_ssize_t unicode_size; |
| 4986 | int has_errors = 0; |
| 4987 | PyObject *unicode; |
| 4988 | int kind; |
| 4989 | void *data; |
| 4990 | const char *starts = s; |
| 4991 | const char *e; |
| 4992 | Py_ssize_t i; |
| 4993 | |
| 4994 | if (size == 0) { |
| 4995 | if (consumed) |
| 4996 | *consumed = 0; |
Victor Stinner | 382955f | 2011-12-11 21:44:00 +0100 | [diff] [blame] | 4997 | Py_INCREF(unicode_empty); |
| 4998 | return unicode_empty; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4999 | } |
| 5000 | |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 5001 | maxchar = utf8_scanner((const unsigned char *)s, size, &unicode_size); |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 5002 | |
| 5003 | /* When the string is ASCII only, just use memcpy and return. |
| 5004 | unicode_size may be != size if there is an incomplete UTF-8 |
| 5005 | sequence at the end of the ASCII block. */ |
| 5006 | if (maxchar < 128 && size == unicode_size) { |
| 5007 | if (consumed) |
| 5008 | *consumed = size; |
Victor Stinner | ab87021 | 2011-12-17 22:39:43 +0100 | [diff] [blame] | 5009 | return unicode_fromascii((const unsigned char *)s, size); |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 5010 | } |
| 5011 | |
| 5012 | unicode = PyUnicode_New(unicode_size, maxchar); |
| 5013 | if (!unicode) |
| 5014 | return NULL; |
| 5015 | kind = PyUnicode_KIND(unicode); |
| 5016 | data = PyUnicode_DATA(unicode); |
| 5017 | |
| 5018 | /* Unpack UTF-8 encoded data */ |
| 5019 | i = 0; |
| 5020 | e = starts + size; |
| 5021 | switch (kind) { |
| 5022 | case PyUnicode_1BYTE_KIND: |
| 5023 | has_errors = ucs1lib_utf8_try_decode(s, e, (Py_UCS1 *) data, &s, &i); |
| 5024 | break; |
| 5025 | case PyUnicode_2BYTE_KIND: |
| 5026 | has_errors = ucs2lib_utf8_try_decode(s, e, (Py_UCS2 *) data, &s, &i); |
| 5027 | break; |
| 5028 | case PyUnicode_4BYTE_KIND: |
| 5029 | has_errors = ucs4lib_utf8_try_decode(s, e, (Py_UCS4 *) data, &s, &i); |
| 5030 | break; |
| 5031 | } |
| 5032 | if (!has_errors) { |
| 5033 | /* Ensure the unicode size calculation was correct */ |
| 5034 | assert(i == unicode_size); |
| 5035 | assert(s == e); |
| 5036 | if (consumed) |
| 5037 | *consumed = size; |
| 5038 | return unicode; |
| 5039 | } |
| 5040 | |
| 5041 | /* In case of errors, maxchar and size computation might be incorrect; |
| 5042 | code below refits and resizes as necessary. */ |
| 5043 | return decode_utf8_errors(starts, size, errors, consumed, s, unicode, i); |
| 5044 | } |
| 5045 | |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 5046 | #ifdef __APPLE__ |
| 5047 | |
| 5048 | /* Simplified UTF-8 decoder using surrogateescape error handler, |
| 5049 | used to decode the command line arguments on Mac OS X. */ |
| 5050 | |
| 5051 | wchar_t* |
| 5052 | _Py_DecodeUTF8_surrogateescape(const char *s, Py_ssize_t size) |
| 5053 | { |
| 5054 | int n; |
| 5055 | const char *e; |
| 5056 | wchar_t *unicode, *p; |
| 5057 | |
| 5058 | /* Note: size will always be longer than the resulting Unicode |
| 5059 | character count */ |
| 5060 | if (PY_SSIZE_T_MAX / sizeof(wchar_t) < (size + 1)) { |
| 5061 | PyErr_NoMemory(); |
| 5062 | return NULL; |
| 5063 | } |
| 5064 | unicode = PyMem_Malloc((size + 1) * sizeof(wchar_t)); |
| 5065 | if (!unicode) |
| 5066 | return NULL; |
| 5067 | |
| 5068 | /* Unpack UTF-8 encoded data */ |
| 5069 | p = unicode; |
| 5070 | e = s + size; |
| 5071 | while (s < e) { |
| 5072 | Py_UCS4 ch = (unsigned char)*s; |
| 5073 | |
| 5074 | if (ch < 0x80) { |
| 5075 | *p++ = (wchar_t)ch; |
| 5076 | s++; |
| 5077 | continue; |
| 5078 | } |
| 5079 | |
| 5080 | n = utf8_code_length[ch]; |
| 5081 | if (s + n > e) { |
| 5082 | goto surrogateescape; |
| 5083 | } |
| 5084 | |
| 5085 | switch (n) { |
| 5086 | case 0: |
| 5087 | case 1: |
| 5088 | goto surrogateescape; |
| 5089 | |
| 5090 | case 2: |
| 5091 | if ((s[1] & 0xc0) != 0x80) |
| 5092 | goto surrogateescape; |
| 5093 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
| 5094 | assert ((ch > 0x007F) && (ch <= 0x07FF)); |
| 5095 | *p++ = (wchar_t)ch; |
| 5096 | break; |
| 5097 | |
| 5098 | case 3: |
| 5099 | /* Decoding UTF-8 sequences in range \xed\xa0\x80-\xed\xbf\xbf |
| 5100 | will result in surrogates in range d800-dfff. Surrogates are |
| 5101 | not valid UTF-8 so they are rejected. |
| 5102 | See http://www.unicode.org/versions/Unicode5.2.0/ch03.pdf |
| 5103 | (table 3-7) and http://www.rfc-editor.org/rfc/rfc3629.txt */ |
| 5104 | if ((s[1] & 0xc0) != 0x80 || |
| 5105 | (s[2] & 0xc0) != 0x80 || |
| 5106 | ((unsigned char)s[0] == 0xE0 && |
| 5107 | (unsigned char)s[1] < 0xA0) || |
| 5108 | ((unsigned char)s[0] == 0xED && |
| 5109 | (unsigned char)s[1] > 0x9F)) { |
| 5110 | |
| 5111 | goto surrogateescape; |
| 5112 | } |
| 5113 | ch = ((s[0] & 0x0f) << 12) + ((s[1] & 0x3f) << 6) + (s[2] & 0x3f); |
| 5114 | assert ((ch > 0x07FF) && (ch <= 0xFFFF)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5115 | *p++ = (wchar_t)ch; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 5116 | break; |
| 5117 | |
| 5118 | case 4: |
| 5119 | if ((s[1] & 0xc0) != 0x80 || |
| 5120 | (s[2] & 0xc0) != 0x80 || |
| 5121 | (s[3] & 0xc0) != 0x80 || |
| 5122 | ((unsigned char)s[0] == 0xF0 && |
| 5123 | (unsigned char)s[1] < 0x90) || |
| 5124 | ((unsigned char)s[0] == 0xF4 && |
| 5125 | (unsigned char)s[1] > 0x8F)) { |
| 5126 | goto surrogateescape; |
| 5127 | } |
| 5128 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
| 5129 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 5130 | assert ((ch > 0xFFFF) && (ch <= MAX_UNICODE)); |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 5131 | |
| 5132 | #if SIZEOF_WCHAR_T == 4 |
| 5133 | *p++ = (wchar_t)ch; |
| 5134 | #else |
| 5135 | /* compute and append the two surrogates: */ |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 5136 | *p++ = (wchar_t)Py_UNICODE_HIGH_SURROGATE(ch); |
| 5137 | *p++ = (wchar_t)Py_UNICODE_LOW_SURROGATE(ch); |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 5138 | #endif |
| 5139 | break; |
| 5140 | } |
| 5141 | s += n; |
| 5142 | continue; |
| 5143 | |
| 5144 | surrogateescape: |
| 5145 | *p++ = 0xDC00 + ch; |
| 5146 | s++; |
| 5147 | } |
| 5148 | *p = L'\0'; |
| 5149 | return unicode; |
| 5150 | } |
| 5151 | |
| 5152 | #endif /* __APPLE__ */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5153 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5154 | /* Primary internal function which creates utf8 encoded bytes objects. |
| 5155 | |
| 5156 | Allocation strategy: if the string is short, convert into a stack buffer |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 5157 | and allocate exactly as much space needed at the end. Else allocate the |
| 5158 | maximum possible needed (4 result bytes per Unicode character), and return |
| 5159 | the excess memory at the end. |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 5160 | */ |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 5161 | PyObject * |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5162 | _PyUnicode_AsUTF8String(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5163 | { |
Victor Stinner | 6099a03 | 2011-12-18 14:22:26 +0100 | [diff] [blame] | 5164 | enum PyUnicode_Kind kind; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5165 | void *data; |
| 5166 | Py_ssize_t size; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 5167 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5168 | if (!PyUnicode_Check(unicode)) { |
| 5169 | PyErr_BadArgument(); |
| 5170 | return NULL; |
| 5171 | } |
| 5172 | |
| 5173 | if (PyUnicode_READY(unicode) == -1) |
| 5174 | return NULL; |
| 5175 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 5176 | if (PyUnicode_UTF8(unicode)) |
| 5177 | return PyBytes_FromStringAndSize(PyUnicode_UTF8(unicode), |
| 5178 | PyUnicode_UTF8_LENGTH(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5179 | |
| 5180 | kind = PyUnicode_KIND(unicode); |
| 5181 | data = PyUnicode_DATA(unicode); |
| 5182 | size = PyUnicode_GET_LENGTH(unicode); |
| 5183 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 5184 | switch (kind) { |
Victor Stinner | 6099a03 | 2011-12-18 14:22:26 +0100 | [diff] [blame] | 5185 | default: |
| 5186 | assert(0); |
| 5187 | case PyUnicode_1BYTE_KIND: |
| 5188 | /* the string cannot be ASCII, or PyUnicode_UTF8() would be set */ |
| 5189 | assert(!PyUnicode_IS_ASCII(unicode)); |
| 5190 | return ucs1lib_utf8_encoder(unicode, data, size, errors); |
| 5191 | case PyUnicode_2BYTE_KIND: |
| 5192 | return ucs2lib_utf8_encoder(unicode, data, size, errors); |
| 5193 | case PyUnicode_4BYTE_KIND: |
| 5194 | return ucs4lib_utf8_encoder(unicode, data, size, errors); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 5195 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5196 | } |
| 5197 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5198 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5199 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
| 5200 | Py_ssize_t size, |
| 5201 | const char *errors) |
| 5202 | { |
| 5203 | PyObject *v, *unicode; |
| 5204 | |
| 5205 | unicode = PyUnicode_FromUnicode(s, size); |
| 5206 | if (unicode == NULL) |
| 5207 | return NULL; |
| 5208 | v = _PyUnicode_AsUTF8String(unicode, errors); |
| 5209 | Py_DECREF(unicode); |
| 5210 | return v; |
| 5211 | } |
| 5212 | |
| 5213 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5214 | PyUnicode_AsUTF8String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5215 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5216 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5217 | } |
| 5218 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5219 | /* --- UTF-32 Codec ------------------------------------------------------- */ |
| 5220 | |
| 5221 | PyObject * |
| 5222 | PyUnicode_DecodeUTF32(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5223 | Py_ssize_t size, |
| 5224 | const char *errors, |
| 5225 | int *byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5226 | { |
| 5227 | return PyUnicode_DecodeUTF32Stateful(s, size, errors, byteorder, NULL); |
| 5228 | } |
| 5229 | |
| 5230 | PyObject * |
| 5231 | PyUnicode_DecodeUTF32Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5232 | Py_ssize_t size, |
| 5233 | const char *errors, |
| 5234 | int *byteorder, |
| 5235 | Py_ssize_t *consumed) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5236 | { |
| 5237 | const char *starts = s; |
| 5238 | Py_ssize_t startinpos; |
| 5239 | Py_ssize_t endinpos; |
| 5240 | Py_ssize_t outpos; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5241 | PyObject *unicode; |
Mark Dickinson | 7db923c | 2010-06-12 09:10:14 +0000 | [diff] [blame] | 5242 | const unsigned char *q, *e; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5243 | int bo = 0; /* assume native ordering by default */ |
| 5244 | const char *errmsg = ""; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5245 | /* Offsets from q for retrieving bytes in the right order. */ |
| 5246 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5247 | int iorder[] = {0, 1, 2, 3}; |
| 5248 | #else |
| 5249 | int iorder[] = {3, 2, 1, 0}; |
| 5250 | #endif |
| 5251 | PyObject *errorHandler = NULL; |
| 5252 | PyObject *exc = NULL; |
Victor Stinner | 313a120 | 2010-06-11 23:56:51 +0000 | [diff] [blame] | 5253 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5254 | q = (unsigned char *)s; |
| 5255 | e = q + size; |
| 5256 | |
| 5257 | if (byteorder) |
| 5258 | bo = *byteorder; |
| 5259 | |
| 5260 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 5261 | byte order setting accordingly. In native mode, the leading BOM |
| 5262 | mark is skipped, in all other modes, it is copied to the output |
| 5263 | stream as-is (giving a ZWNBSP character). */ |
| 5264 | if (bo == 0) { |
| 5265 | if (size >= 4) { |
| 5266 | const Py_UCS4 bom = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5267 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5268 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5269 | if (bom == 0x0000FEFF) { |
| 5270 | q += 4; |
| 5271 | bo = -1; |
| 5272 | } |
| 5273 | else if (bom == 0xFFFE0000) { |
| 5274 | q += 4; |
| 5275 | bo = 1; |
| 5276 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5277 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5278 | if (bom == 0x0000FEFF) { |
| 5279 | q += 4; |
| 5280 | bo = 1; |
| 5281 | } |
| 5282 | else if (bom == 0xFFFE0000) { |
| 5283 | q += 4; |
| 5284 | bo = -1; |
| 5285 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5286 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5287 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5288 | } |
| 5289 | |
| 5290 | if (bo == -1) { |
| 5291 | /* force LE */ |
| 5292 | iorder[0] = 0; |
| 5293 | iorder[1] = 1; |
| 5294 | iorder[2] = 2; |
| 5295 | iorder[3] = 3; |
| 5296 | } |
| 5297 | else if (bo == 1) { |
| 5298 | /* force BE */ |
| 5299 | iorder[0] = 3; |
| 5300 | iorder[1] = 2; |
| 5301 | iorder[2] = 1; |
| 5302 | iorder[3] = 0; |
| 5303 | } |
| 5304 | |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 5305 | /* This might be one to much, because of a BOM */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5306 | unicode = PyUnicode_New((size+3)/4, 127); |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 5307 | if (!unicode) |
| 5308 | return NULL; |
| 5309 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5310 | return unicode; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5311 | outpos = 0; |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 5312 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5313 | while (q < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5314 | Py_UCS4 ch; |
| 5315 | /* remaining bytes at the end? (size should be divisible by 4) */ |
| 5316 | if (e-q<4) { |
| 5317 | if (consumed) |
| 5318 | break; |
| 5319 | errmsg = "truncated data"; |
| 5320 | startinpos = ((const char *)q)-starts; |
| 5321 | endinpos = ((const char *)e)-starts; |
| 5322 | goto utf32Error; |
| 5323 | /* The remaining input chars are ignored if the callback |
| 5324 | chooses to skip the input */ |
| 5325 | } |
| 5326 | ch = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
| 5327 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5328 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5329 | if (ch >= 0x110000) |
| 5330 | { |
| 5331 | errmsg = "codepoint not in range(0x110000)"; |
| 5332 | startinpos = ((const char *)q)-starts; |
| 5333 | endinpos = startinpos+4; |
| 5334 | goto utf32Error; |
| 5335 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5336 | if (unicode_putchar(&unicode, &outpos, ch) < 0) |
| 5337 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5338 | q += 4; |
| 5339 | continue; |
| 5340 | utf32Error: |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5341 | if (unicode_decode_call_errorhandler( |
| 5342 | errors, &errorHandler, |
| 5343 | "utf32", errmsg, |
| 5344 | &starts, (const char **)&e, &startinpos, &endinpos, &exc, (const char **)&q, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5345 | &unicode, &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5346 | goto onError; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5347 | } |
| 5348 | |
| 5349 | if (byteorder) |
| 5350 | *byteorder = bo; |
| 5351 | |
| 5352 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5353 | *consumed = (const char *)q-starts; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5354 | |
| 5355 | /* Adjust length */ |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 5356 | if (unicode_resize(&unicode, outpos) < 0) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5357 | goto onError; |
| 5358 | |
| 5359 | Py_XDECREF(errorHandler); |
| 5360 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 5361 | return unicode_result(unicode); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5362 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5363 | onError: |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5364 | Py_DECREF(unicode); |
| 5365 | Py_XDECREF(errorHandler); |
| 5366 | Py_XDECREF(exc); |
| 5367 | return NULL; |
| 5368 | } |
| 5369 | |
| 5370 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5371 | _PyUnicode_EncodeUTF32(PyObject *str, |
| 5372 | const char *errors, |
| 5373 | int byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5374 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5375 | int kind; |
| 5376 | void *data; |
| 5377 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5378 | PyObject *v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5379 | unsigned char *p; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5380 | Py_ssize_t nsize, bytesize, i; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5381 | /* Offsets from p for storing byte pairs in the right order. */ |
| 5382 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5383 | int iorder[] = {0, 1, 2, 3}; |
| 5384 | #else |
| 5385 | int iorder[] = {3, 2, 1, 0}; |
| 5386 | #endif |
| 5387 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5388 | #define STORECHAR(CH) \ |
| 5389 | do { \ |
| 5390 | p[iorder[3]] = ((CH) >> 24) & 0xff; \ |
| 5391 | p[iorder[2]] = ((CH) >> 16) & 0xff; \ |
| 5392 | p[iorder[1]] = ((CH) >> 8) & 0xff; \ |
| 5393 | p[iorder[0]] = (CH) & 0xff; \ |
| 5394 | p += 4; \ |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5395 | } while(0) |
| 5396 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5397 | if (!PyUnicode_Check(str)) { |
| 5398 | PyErr_BadArgument(); |
| 5399 | return NULL; |
| 5400 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 5401 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5402 | return NULL; |
| 5403 | kind = PyUnicode_KIND(str); |
| 5404 | data = PyUnicode_DATA(str); |
| 5405 | len = PyUnicode_GET_LENGTH(str); |
| 5406 | |
| 5407 | nsize = len + (byteorder == 0); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5408 | bytesize = nsize * 4; |
| 5409 | if (bytesize / 4 != nsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5410 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5411 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5412 | if (v == NULL) |
| 5413 | return NULL; |
| 5414 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5415 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5416 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5417 | STORECHAR(0xFEFF); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5418 | if (len == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5419 | goto done; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5420 | |
| 5421 | if (byteorder == -1) { |
| 5422 | /* force LE */ |
| 5423 | iorder[0] = 0; |
| 5424 | iorder[1] = 1; |
| 5425 | iorder[2] = 2; |
| 5426 | iorder[3] = 3; |
| 5427 | } |
| 5428 | else if (byteorder == 1) { |
| 5429 | /* force BE */ |
| 5430 | iorder[0] = 3; |
| 5431 | iorder[1] = 2; |
| 5432 | iorder[2] = 1; |
| 5433 | iorder[3] = 0; |
| 5434 | } |
| 5435 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5436 | for (i = 0; i < len; i++) |
| 5437 | STORECHAR(PyUnicode_READ(kind, data, i)); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5438 | |
| 5439 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5440 | return v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5441 | #undef STORECHAR |
| 5442 | } |
| 5443 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5444 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5445 | PyUnicode_EncodeUTF32(const Py_UNICODE *s, |
| 5446 | Py_ssize_t size, |
| 5447 | const char *errors, |
| 5448 | int byteorder) |
| 5449 | { |
| 5450 | PyObject *result; |
| 5451 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 5452 | if (tmp == NULL) |
| 5453 | return NULL; |
| 5454 | result = _PyUnicode_EncodeUTF32(tmp, errors, byteorder); |
| 5455 | Py_DECREF(tmp); |
| 5456 | return result; |
| 5457 | } |
| 5458 | |
| 5459 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5460 | PyUnicode_AsUTF32String(PyObject *unicode) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5461 | { |
Victor Stinner | b960b34 | 2011-11-20 19:12:52 +0100 | [diff] [blame] | 5462 | return _PyUnicode_EncodeUTF32(unicode, NULL, 0); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5463 | } |
| 5464 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5465 | /* --- UTF-16 Codec ------------------------------------------------------- */ |
| 5466 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5467 | PyObject * |
| 5468 | PyUnicode_DecodeUTF16(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5469 | Py_ssize_t size, |
| 5470 | const char *errors, |
| 5471 | int *byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5472 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5473 | return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL); |
| 5474 | } |
| 5475 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5476 | /* Two masks for fast checking of whether a C 'long' may contain |
| 5477 | UTF16-encoded surrogate characters. This is an efficient heuristic, |
| 5478 | assuming that non-surrogate characters with a code point >= 0x8000 are |
| 5479 | rare in most input. |
| 5480 | FAST_CHAR_MASK is used when the input is in native byte ordering, |
| 5481 | SWAPPED_FAST_CHAR_MASK when the input is in byteswapped ordering. |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5482 | */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5483 | #if (SIZEOF_LONG == 8) |
| 5484 | # define FAST_CHAR_MASK 0x8000800080008000L |
| 5485 | # define SWAPPED_FAST_CHAR_MASK 0x0080008000800080L |
Victor Stinner | afb5205 | 2012-04-05 22:54:49 +0200 | [diff] [blame] | 5486 | # define STRIPPED_MASK 0x00FF00FF00FF00FFL |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5487 | #elif (SIZEOF_LONG == 4) |
| 5488 | # define FAST_CHAR_MASK 0x80008000L |
| 5489 | # define SWAPPED_FAST_CHAR_MASK 0x00800080L |
Victor Stinner | afb5205 | 2012-04-05 22:54:49 +0200 | [diff] [blame] | 5490 | # define STRIPPED_MASK 0x00FF00FFL |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5491 | #else |
| 5492 | # error C 'long' size should be either 4 or 8! |
| 5493 | #endif |
| 5494 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5495 | PyObject * |
| 5496 | PyUnicode_DecodeUTF16Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5497 | Py_ssize_t size, |
| 5498 | const char *errors, |
| 5499 | int *byteorder, |
| 5500 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5501 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5502 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5503 | Py_ssize_t startinpos; |
| 5504 | Py_ssize_t endinpos; |
| 5505 | Py_ssize_t outpos; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5506 | PyObject *unicode; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5507 | const unsigned char *q, *e, *aligned_end; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5508 | int bo = 0; /* assume native ordering by default */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5509 | int native_ordering = 0; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 5510 | const char *errmsg = ""; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5511 | /* Offsets from q for retrieving byte pairs in the right order. */ |
| 5512 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5513 | int ihi = 1, ilo = 0; |
| 5514 | #else |
| 5515 | int ihi = 0, ilo = 1; |
| 5516 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5517 | PyObject *errorHandler = NULL; |
| 5518 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5519 | |
| 5520 | /* Note: size will always be longer than the resulting Unicode |
| 5521 | character count */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5522 | unicode = PyUnicode_New(size, 127); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5523 | if (!unicode) |
| 5524 | return NULL; |
| 5525 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5526 | return unicode; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5527 | outpos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5528 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5529 | q = (unsigned char *)s; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5530 | e = q + size - 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5531 | |
| 5532 | if (byteorder) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5533 | bo = *byteorder; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5534 | |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5535 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 5536 | byte order setting accordingly. In native mode, the leading BOM |
| 5537 | mark is skipped, in all other modes, it is copied to the output |
| 5538 | stream as-is (giving a ZWNBSP character). */ |
| 5539 | if (bo == 0) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5540 | if (size >= 2) { |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 5541 | const Py_UCS4 bom = (q[ihi] << 8) | q[ilo]; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5542 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5543 | if (bom == 0xFEFF) { |
| 5544 | q += 2; |
| 5545 | bo = -1; |
| 5546 | } |
| 5547 | else if (bom == 0xFFFE) { |
| 5548 | q += 2; |
| 5549 | bo = 1; |
| 5550 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5551 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5552 | if (bom == 0xFEFF) { |
| 5553 | q += 2; |
| 5554 | bo = 1; |
| 5555 | } |
| 5556 | else if (bom == 0xFFFE) { |
| 5557 | q += 2; |
| 5558 | bo = -1; |
| 5559 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5560 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5561 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5562 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5563 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5564 | if (bo == -1) { |
| 5565 | /* force LE */ |
| 5566 | ihi = 1; |
| 5567 | ilo = 0; |
| 5568 | } |
| 5569 | else if (bo == 1) { |
| 5570 | /* force BE */ |
| 5571 | ihi = 0; |
| 5572 | ilo = 1; |
| 5573 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5574 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5575 | native_ordering = ilo < ihi; |
| 5576 | #else |
| 5577 | native_ordering = ilo > ihi; |
| 5578 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5579 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5580 | aligned_end = (const unsigned char *) ((size_t) e & ~LONG_PTR_MASK); |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5581 | while (q < e) { |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 5582 | Py_UCS4 ch; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5583 | /* First check for possible aligned read of a C 'long'. Unaligned |
| 5584 | reads are more expensive, better to defer to another iteration. */ |
| 5585 | if (!((size_t) q & LONG_PTR_MASK)) { |
| 5586 | /* Fast path for runs of non-surrogate chars. */ |
| 5587 | register const unsigned char *_q = q; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5588 | int kind = PyUnicode_KIND(unicode); |
| 5589 | void *data = PyUnicode_DATA(unicode); |
| 5590 | while (_q < aligned_end) { |
Victor Stinner | afb5205 | 2012-04-05 22:54:49 +0200 | [diff] [blame] | 5591 | unsigned long block = * (unsigned long *) _q; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5592 | Py_UCS4 maxch; |
| 5593 | if (native_ordering) { |
| 5594 | /* Can use buffer directly */ |
Victor Stinner | afb5205 | 2012-04-05 22:54:49 +0200 | [diff] [blame] | 5595 | if (block & FAST_CHAR_MASK) |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5596 | break; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5597 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5598 | else { |
| 5599 | /* Need to byte-swap */ |
Victor Stinner | afb5205 | 2012-04-05 22:54:49 +0200 | [diff] [blame] | 5600 | if (block & SWAPPED_FAST_CHAR_MASK) |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5601 | break; |
Victor Stinner | afb5205 | 2012-04-05 22:54:49 +0200 | [diff] [blame] | 5602 | block = ((block >> 8) & STRIPPED_MASK) | |
| 5603 | ((block & STRIPPED_MASK) << 8); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5604 | } |
Victor Stinner | afb5205 | 2012-04-05 22:54:49 +0200 | [diff] [blame] | 5605 | maxch = (Py_UCS2)(block & 0xFFFF); |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5606 | #if SIZEOF_LONG == 8 |
Victor Stinner | afb5205 | 2012-04-05 22:54:49 +0200 | [diff] [blame] | 5607 | ch = (Py_UCS2)((block >> 16) & 0xFFFF); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 5608 | maxch = MAX_MAXCHAR(maxch, ch); |
Victor Stinner | afb5205 | 2012-04-05 22:54:49 +0200 | [diff] [blame] | 5609 | ch = (Py_UCS2)((block >> 32) & 0xFFFF); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 5610 | maxch = MAX_MAXCHAR(maxch, ch); |
Victor Stinner | afb5205 | 2012-04-05 22:54:49 +0200 | [diff] [blame] | 5611 | ch = (Py_UCS2)(block >> 48); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 5612 | maxch = MAX_MAXCHAR(maxch, ch); |
Victor Stinner | afb5205 | 2012-04-05 22:54:49 +0200 | [diff] [blame] | 5613 | #else |
| 5614 | ch = (Py_UCS2)(block >> 16); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 5615 | maxch = MAX_MAXCHAR(maxch, ch); |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5616 | #endif |
| 5617 | if (maxch > PyUnicode_MAX_CHAR_VALUE(unicode)) { |
Victor Stinner | 1b487b4 | 2012-05-03 12:29:04 +0200 | [diff] [blame] | 5618 | if (unicode_widen(&unicode, outpos, maxch) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5619 | goto onError; |
| 5620 | kind = PyUnicode_KIND(unicode); |
| 5621 | data = PyUnicode_DATA(unicode); |
| 5622 | } |
Victor Stinner | afb5205 | 2012-04-05 22:54:49 +0200 | [diff] [blame] | 5623 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5624 | PyUnicode_WRITE(kind, data, outpos++, (Py_UCS2)(block & 0xFFFF)); |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5625 | #if SIZEOF_LONG == 8 |
Victor Stinner | afb5205 | 2012-04-05 22:54:49 +0200 | [diff] [blame] | 5626 | PyUnicode_WRITE(kind, data, outpos++, (Py_UCS2)((block >> 16) & 0xFFFF)); |
| 5627 | PyUnicode_WRITE(kind, data, outpos++, (Py_UCS2)((block >> 32) & 0xFFFF)); |
| 5628 | PyUnicode_WRITE(kind, data, outpos++, (Py_UCS2)((block >> 48))); |
| 5629 | #else |
| 5630 | PyUnicode_WRITE(kind, data, outpos++, (Py_UCS2)(block >> 16)); |
| 5631 | #endif |
| 5632 | #else |
| 5633 | #if SIZEOF_LONG == 8 |
| 5634 | PyUnicode_WRITE(kind, data, outpos++, (Py_UCS2)((block >> 48))); |
| 5635 | PyUnicode_WRITE(kind, data, outpos++, (Py_UCS2)((block >> 32) & 0xFFFF)); |
| 5636 | PyUnicode_WRITE(kind, data, outpos++, (Py_UCS2)((block >> 16) & 0xFFFF)); |
| 5637 | #else |
| 5638 | PyUnicode_WRITE(kind, data, outpos++, (Py_UCS2)(block >> 16)); |
| 5639 | #endif |
| 5640 | PyUnicode_WRITE(kind, data, outpos++, (Py_UCS2)(block & 0xFFFF)); |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5641 | #endif |
| 5642 | _q += SIZEOF_LONG; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5643 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5644 | q = _q; |
| 5645 | if (q >= e) |
| 5646 | break; |
| 5647 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5648 | ch = (q[ihi] << 8) | q[ilo]; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5649 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5650 | q += 2; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5651 | |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 5652 | if (!Py_UNICODE_IS_SURROGATE(ch)) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5653 | if (unicode_putchar(&unicode, &outpos, ch) < 0) |
| 5654 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5655 | continue; |
| 5656 | } |
| 5657 | |
| 5658 | /* UTF-16 code pair: */ |
| 5659 | if (q > e) { |
| 5660 | errmsg = "unexpected end of data"; |
| 5661 | startinpos = (((const char *)q) - 2) - starts; |
| 5662 | endinpos = ((const char *)e) + 1 - starts; |
| 5663 | goto utf16Error; |
| 5664 | } |
Victor Stinner | 2e9cfad | 2011-11-20 18:40:27 +0100 | [diff] [blame] | 5665 | if (Py_UNICODE_IS_HIGH_SURROGATE(ch)) { |
| 5666 | Py_UCS4 ch2 = (q[ihi] << 8) | q[ilo]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5667 | q += 2; |
Victor Stinner | 2e9cfad | 2011-11-20 18:40:27 +0100 | [diff] [blame] | 5668 | if (Py_UNICODE_IS_LOW_SURROGATE(ch2)) { |
Victor Stinner | 62aa4d0 | 2011-11-09 00:03:45 +0100 | [diff] [blame] | 5669 | if (unicode_putchar(&unicode, &outpos, |
Victor Stinner | 2e9cfad | 2011-11-20 18:40:27 +0100 | [diff] [blame] | 5670 | Py_UNICODE_JOIN_SURROGATES(ch, ch2)) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5671 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5672 | continue; |
| 5673 | } |
| 5674 | else { |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5675 | errmsg = "illegal UTF-16 surrogate"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5676 | startinpos = (((const char *)q)-4)-starts; |
| 5677 | endinpos = startinpos+2; |
| 5678 | goto utf16Error; |
| 5679 | } |
| 5680 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5681 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5682 | errmsg = "illegal encoding"; |
| 5683 | startinpos = (((const char *)q)-2)-starts; |
| 5684 | endinpos = startinpos+2; |
| 5685 | /* Fall through to report the error */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5686 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5687 | utf16Error: |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5688 | if (unicode_decode_call_errorhandler( |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5689 | errors, |
| 5690 | &errorHandler, |
| 5691 | "utf16", errmsg, |
| 5692 | &starts, |
| 5693 | (const char **)&e, |
| 5694 | &startinpos, |
| 5695 | &endinpos, |
| 5696 | &exc, |
| 5697 | (const char **)&q, |
| 5698 | &unicode, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5699 | &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5700 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5701 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5702 | /* remaining byte at the end? (size should be even) */ |
| 5703 | if (e == q) { |
| 5704 | if (!consumed) { |
| 5705 | errmsg = "truncated data"; |
| 5706 | startinpos = ((const char *)q) - starts; |
| 5707 | endinpos = ((const char *)e) + 1 - starts; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5708 | if (unicode_decode_call_errorhandler( |
| 5709 | errors, |
| 5710 | &errorHandler, |
| 5711 | "utf16", errmsg, |
| 5712 | &starts, |
| 5713 | (const char **)&e, |
| 5714 | &startinpos, |
| 5715 | &endinpos, |
| 5716 | &exc, |
| 5717 | (const char **)&q, |
| 5718 | &unicode, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5719 | &outpos)) |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5720 | goto onError; |
| 5721 | /* The remaining input chars are ignored if the callback |
| 5722 | chooses to skip the input */ |
| 5723 | } |
| 5724 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5725 | |
| 5726 | if (byteorder) |
| 5727 | *byteorder = bo; |
| 5728 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5729 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5730 | *consumed = (const char *)q-starts; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5731 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5732 | /* Adjust length */ |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 5733 | if (unicode_resize(&unicode, outpos) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5734 | goto onError; |
| 5735 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5736 | Py_XDECREF(errorHandler); |
| 5737 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 5738 | return unicode_result(unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5739 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5740 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5741 | Py_DECREF(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5742 | Py_XDECREF(errorHandler); |
| 5743 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5744 | return NULL; |
| 5745 | } |
| 5746 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5747 | #undef FAST_CHAR_MASK |
| 5748 | #undef SWAPPED_FAST_CHAR_MASK |
| 5749 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5750 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5751 | _PyUnicode_EncodeUTF16(PyObject *str, |
| 5752 | const char *errors, |
| 5753 | int byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5754 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5755 | int kind; |
| 5756 | void *data; |
| 5757 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5758 | PyObject *v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5759 | unsigned char *p; |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5760 | Py_ssize_t nsize, bytesize; |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5761 | Py_ssize_t i, pairs; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5762 | /* Offsets from p for storing byte pairs in the right order. */ |
| 5763 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5764 | int ihi = 1, ilo = 0; |
| 5765 | #else |
| 5766 | int ihi = 0, ilo = 1; |
| 5767 | #endif |
| 5768 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5769 | #define STORECHAR(CH) \ |
| 5770 | do { \ |
| 5771 | p[ihi] = ((CH) >> 8) & 0xff; \ |
| 5772 | p[ilo] = (CH) & 0xff; \ |
| 5773 | p += 2; \ |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5774 | } while(0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5775 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5776 | if (!PyUnicode_Check(str)) { |
| 5777 | PyErr_BadArgument(); |
| 5778 | return NULL; |
| 5779 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 5780 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5781 | return NULL; |
| 5782 | kind = PyUnicode_KIND(str); |
| 5783 | data = PyUnicode_DATA(str); |
| 5784 | len = PyUnicode_GET_LENGTH(str); |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 5785 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5786 | pairs = 0; |
| 5787 | if (kind == PyUnicode_4BYTE_KIND) |
| 5788 | for (i = 0; i < len; i++) |
| 5789 | if (PyUnicode_READ(kind, data, i) >= 0x10000) |
| 5790 | pairs++; |
| 5791 | /* 2 * (len + pairs + (byteorder == 0)) */ |
| 5792 | if (len > PY_SSIZE_T_MAX - pairs - (byteorder == 0)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5793 | return PyErr_NoMemory(); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5794 | nsize = len + pairs + (byteorder == 0); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5795 | bytesize = nsize * 2; |
| 5796 | if (bytesize / 2 != nsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5797 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5798 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5799 | if (v == NULL) |
| 5800 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5801 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5802 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5803 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5804 | STORECHAR(0xFEFF); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5805 | if (len == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5806 | goto done; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5807 | |
| 5808 | if (byteorder == -1) { |
| 5809 | /* force LE */ |
| 5810 | ihi = 1; |
| 5811 | ilo = 0; |
| 5812 | } |
| 5813 | else if (byteorder == 1) { |
| 5814 | /* force BE */ |
| 5815 | ihi = 0; |
| 5816 | ilo = 1; |
| 5817 | } |
| 5818 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5819 | for (i = 0; i < len; i++) { |
| 5820 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 5821 | Py_UCS4 ch2 = 0; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5822 | if (ch >= 0x10000) { |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 5823 | ch2 = Py_UNICODE_LOW_SURROGATE(ch); |
| 5824 | ch = Py_UNICODE_HIGH_SURROGATE(ch); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5825 | } |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5826 | STORECHAR(ch); |
| 5827 | if (ch2) |
| 5828 | STORECHAR(ch2); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5829 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5830 | |
| 5831 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5832 | return v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5833 | #undef STORECHAR |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5834 | } |
| 5835 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5836 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5837 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
| 5838 | Py_ssize_t size, |
| 5839 | const char *errors, |
| 5840 | int byteorder) |
| 5841 | { |
| 5842 | PyObject *result; |
| 5843 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 5844 | if (tmp == NULL) |
| 5845 | return NULL; |
| 5846 | result = _PyUnicode_EncodeUTF16(tmp, errors, byteorder); |
| 5847 | Py_DECREF(tmp); |
| 5848 | return result; |
| 5849 | } |
| 5850 | |
| 5851 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5852 | PyUnicode_AsUTF16String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5853 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5854 | return _PyUnicode_EncodeUTF16(unicode, NULL, 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5855 | } |
| 5856 | |
| 5857 | /* --- Unicode Escape Codec ----------------------------------------------- */ |
| 5858 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5859 | /* Helper function for PyUnicode_DecodeUnicodeEscape, determines |
| 5860 | if all the escapes in the string make it still a valid ASCII string. |
| 5861 | Returns -1 if any escapes were found which cause the string to |
| 5862 | pop out of ASCII range. Otherwise returns the length of the |
| 5863 | required buffer to hold the string. |
| 5864 | */ |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 5865 | static Py_ssize_t |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5866 | length_of_escaped_ascii_string(const char *s, Py_ssize_t size) |
| 5867 | { |
| 5868 | const unsigned char *p = (const unsigned char *)s; |
| 5869 | const unsigned char *end = p + size; |
| 5870 | Py_ssize_t length = 0; |
| 5871 | |
| 5872 | if (size < 0) |
| 5873 | return -1; |
| 5874 | |
| 5875 | for (; p < end; ++p) { |
| 5876 | if (*p > 127) { |
| 5877 | /* Non-ASCII */ |
| 5878 | return -1; |
| 5879 | } |
| 5880 | else if (*p != '\\') { |
| 5881 | /* Normal character */ |
| 5882 | ++length; |
| 5883 | } |
| 5884 | else { |
| 5885 | /* Backslash-escape, check next char */ |
| 5886 | ++p; |
| 5887 | /* Escape sequence reaches till end of string or |
| 5888 | non-ASCII follow-up. */ |
| 5889 | if (p >= end || *p > 127) |
| 5890 | return -1; |
| 5891 | switch (*p) { |
| 5892 | case '\n': |
| 5893 | /* backslash + \n result in zero characters */ |
| 5894 | break; |
| 5895 | case '\\': case '\'': case '\"': |
| 5896 | case 'b': case 'f': case 't': |
| 5897 | case 'n': case 'r': case 'v': case 'a': |
| 5898 | ++length; |
| 5899 | break; |
| 5900 | case '0': case '1': case '2': case '3': |
| 5901 | case '4': case '5': case '6': case '7': |
| 5902 | case 'x': case 'u': case 'U': case 'N': |
| 5903 | /* these do not guarantee ASCII characters */ |
| 5904 | return -1; |
| 5905 | default: |
| 5906 | /* count the backslash + the other character */ |
| 5907 | length += 2; |
| 5908 | } |
| 5909 | } |
| 5910 | } |
| 5911 | return length; |
| 5912 | } |
| 5913 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 5914 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; |
Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 5915 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5916 | PyObject * |
| 5917 | PyUnicode_DecodeUnicodeEscape(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5918 | Py_ssize_t size, |
Victor Stinner | c17f540 | 2011-09-29 00:16:58 +0200 | [diff] [blame] | 5919 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5920 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5921 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5922 | Py_ssize_t startinpos; |
| 5923 | Py_ssize_t endinpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5924 | int j; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5925 | PyObject *v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5926 | const char *end; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5927 | char* message; |
| 5928 | Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5929 | PyObject *errorHandler = NULL; |
| 5930 | PyObject *exc = NULL; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5931 | Py_ssize_t len; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5932 | Py_ssize_t i; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5933 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5934 | len = length_of_escaped_ascii_string(s, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5935 | |
| 5936 | /* After length_of_escaped_ascii_string() there are two alternatives, |
| 5937 | either the string is pure ASCII with named escapes like \n, etc. |
| 5938 | and we determined it's exact size (common case) |
| 5939 | or it contains \x, \u, ... escape sequences. then we create a |
| 5940 | 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] | 5941 | if (len >= 0) { |
| 5942 | v = PyUnicode_New(len, 127); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5943 | if (!v) |
| 5944 | goto onError; |
| 5945 | assert(PyUnicode_KIND(v) == PyUnicode_1BYTE_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5946 | } |
| 5947 | else { |
| 5948 | /* Escaped strings will always be longer than the resulting |
| 5949 | Unicode string, so we start with size here and then reduce the |
| 5950 | length after conversion to the true value. |
| 5951 | (but if the error callback returns a long replacement string |
| 5952 | we'll have to allocate more space) */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5953 | v = PyUnicode_New(size, 127); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5954 | if (!v) |
| 5955 | goto onError; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5956 | len = size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5957 | } |
| 5958 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5959 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5960 | return v; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5961 | i = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5962 | end = s + size; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5963 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5964 | while (s < end) { |
| 5965 | unsigned char c; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 5966 | Py_UCS4 x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5967 | int digits; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5968 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5969 | /* The only case in which i == ascii_length is a backslash |
| 5970 | followed by a newline. */ |
| 5971 | assert(i <= len); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5972 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5973 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 5974 | if (*s != '\\') { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5975 | if (unicode_putchar(&v, &i, (unsigned char) *s++) < 0) |
| 5976 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5977 | continue; |
| 5978 | } |
| 5979 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5980 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5981 | /* \ - Escapes */ |
| 5982 | s++; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5983 | c = *s++; |
| 5984 | if (s > end) |
| 5985 | c = '\0'; /* Invalid after \ */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5986 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5987 | /* The only case in which i == ascii_length is a backslash |
| 5988 | followed by a newline. */ |
| 5989 | assert(i < len || (i == len && c == '\n')); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5990 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5991 | switch (c) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5992 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5993 | /* \x escapes */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5994 | #define WRITECHAR(ch) \ |
| 5995 | do { \ |
| 5996 | if (unicode_putchar(&v, &i, ch) < 0) \ |
| 5997 | goto onError; \ |
| 5998 | }while(0) |
| 5999 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6000 | case '\n': break; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6001 | case '\\': WRITECHAR('\\'); break; |
| 6002 | case '\'': WRITECHAR('\''); break; |
| 6003 | case '\"': WRITECHAR('\"'); break; |
| 6004 | case 'b': WRITECHAR('\b'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6005 | /* FF */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6006 | case 'f': WRITECHAR('\014'); break; |
| 6007 | case 't': WRITECHAR('\t'); break; |
| 6008 | case 'n': WRITECHAR('\n'); break; |
| 6009 | case 'r': WRITECHAR('\r'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6010 | /* VT */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6011 | case 'v': WRITECHAR('\013'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6012 | /* BEL, not classic C */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6013 | case 'a': WRITECHAR('\007'); break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6014 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6015 | /* \OOO (octal) escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6016 | case '0': case '1': case '2': case '3': |
| 6017 | case '4': case '5': case '6': case '7': |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 6018 | x = s[-1] - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 6019 | if (s < end && '0' <= *s && *s <= '7') { |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 6020 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 6021 | if (s < end && '0' <= *s && *s <= '7') |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 6022 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6023 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6024 | WRITECHAR(x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6025 | break; |
| 6026 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6027 | /* hex escapes */ |
| 6028 | /* \xXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6029 | case 'x': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 6030 | digits = 2; |
| 6031 | message = "truncated \\xXX escape"; |
| 6032 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6033 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6034 | /* \uXXXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6035 | case 'u': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 6036 | digits = 4; |
| 6037 | message = "truncated \\uXXXX escape"; |
| 6038 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6039 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6040 | /* \UXXXXXXXX */ |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 6041 | case 'U': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 6042 | digits = 8; |
| 6043 | message = "truncated \\UXXXXXXXX escape"; |
| 6044 | hexescape: |
| 6045 | chr = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6046 | if (s+digits>end) { |
| 6047 | endinpos = size; |
| 6048 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6049 | errors, &errorHandler, |
| 6050 | "unicodeescape", "end of string in escape sequence", |
| 6051 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6052 | &v, &i)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6053 | goto onError; |
| 6054 | goto nextByte; |
| 6055 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6056 | for (j = 0; j < digits; ++j) { |
| 6057 | c = (unsigned char) s[j]; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 6058 | if (!Py_ISXDIGIT(c)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6059 | endinpos = (s+j+1)-starts; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6060 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6061 | errors, &errorHandler, |
| 6062 | "unicodeescape", message, |
| 6063 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6064 | &v, &i)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 6065 | goto onError; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6066 | len = PyUnicode_GET_LENGTH(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6067 | goto nextByte; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 6068 | } |
| 6069 | chr = (chr<<4) & ~0xF; |
| 6070 | if (c >= '0' && c <= '9') |
| 6071 | chr += c - '0'; |
| 6072 | else if (c >= 'a' && c <= 'f') |
| 6073 | chr += 10 + c - 'a'; |
| 6074 | else |
| 6075 | chr += 10 + c - 'A'; |
| 6076 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6077 | s += j; |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 6078 | if (chr == 0xffffffff && PyErr_Occurred()) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6079 | /* _decoding_error will have already written into the |
| 6080 | target buffer. */ |
| 6081 | break; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 6082 | store: |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 6083 | /* when we get here, chr is a 32-bit unicode character */ |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 6084 | if (chr <= MAX_UNICODE) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6085 | WRITECHAR(chr); |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 6086 | } else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6087 | endinpos = s-starts; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6088 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6089 | errors, &errorHandler, |
| 6090 | "unicodeescape", "illegal Unicode character", |
| 6091 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6092 | &v, &i)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 6093 | goto onError; |
| 6094 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 6095 | break; |
| 6096 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6097 | /* \N{name} */ |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 6098 | case 'N': |
| 6099 | message = "malformed \\N character escape"; |
| 6100 | if (ucnhash_CAPI == NULL) { |
| 6101 | /* load the unicode data module */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6102 | ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCapsule_Import( |
| 6103 | PyUnicodeData_CAPSULE_NAME, 1); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 6104 | if (ucnhash_CAPI == NULL) |
| 6105 | goto ucnhashError; |
| 6106 | } |
| 6107 | if (*s == '{') { |
| 6108 | const char *start = s+1; |
| 6109 | /* look for the closing brace */ |
| 6110 | while (*s != '}' && s < end) |
| 6111 | s++; |
| 6112 | if (s > start && s < end && *s == '}') { |
| 6113 | /* found a name. look it up in the unicode database */ |
| 6114 | message = "unknown Unicode character name"; |
| 6115 | s++; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6116 | if (ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), |
Ezio Melotti | 931b8aa | 2011-10-21 21:57:36 +0300 | [diff] [blame] | 6117 | &chr, 0)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 6118 | goto store; |
| 6119 | } |
| 6120 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6121 | endinpos = s-starts; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6122 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6123 | errors, &errorHandler, |
| 6124 | "unicodeescape", message, |
| 6125 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6126 | &v, &i)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 6127 | goto onError; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 6128 | break; |
| 6129 | |
| 6130 | default: |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 6131 | if (s > end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6132 | message = "\\ at end of string"; |
| 6133 | s--; |
| 6134 | endinpos = s-starts; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6135 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6136 | errors, &errorHandler, |
| 6137 | "unicodeescape", message, |
| 6138 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6139 | &v, &i)) |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 6140 | goto onError; |
| 6141 | } |
| 6142 | else { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6143 | WRITECHAR('\\'); |
| 6144 | WRITECHAR(s[-1]); |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 6145 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 6146 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6147 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6148 | nextByte: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6149 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6150 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6151 | #undef WRITECHAR |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6152 | |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 6153 | if (unicode_resize(&v, i) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6154 | goto onError; |
Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 6155 | Py_XDECREF(errorHandler); |
| 6156 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 6157 | return unicode_result(v); |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 6158 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6159 | ucnhashError: |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 6160 | PyErr_SetString( |
| 6161 | PyExc_UnicodeError, |
| 6162 | "\\N escapes not supported (can't load unicodedata module)" |
| 6163 | ); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 6164 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6165 | Py_XDECREF(errorHandler); |
| 6166 | Py_XDECREF(exc); |
Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 6167 | return NULL; |
| 6168 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6169 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6170 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6171 | Py_XDECREF(errorHandler); |
| 6172 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6173 | return NULL; |
| 6174 | } |
| 6175 | |
| 6176 | /* Return a Unicode-Escape string version of the Unicode object. |
| 6177 | |
| 6178 | If quotes is true, the string is enclosed in u"" or u'' quotes as |
| 6179 | appropriate. |
| 6180 | |
| 6181 | */ |
| 6182 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6183 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6184 | PyUnicode_AsUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6185 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6186 | Py_ssize_t i, len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6187 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6188 | char *p; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6189 | int kind; |
| 6190 | void *data; |
| 6191 | Py_ssize_t expandsize = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6192 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6193 | /* Initial allocation is based on the longest-possible unichr |
| 6194 | escape. |
| 6195 | |
| 6196 | In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source |
| 6197 | unichr, so in this case it's the longest unichr escape. In |
| 6198 | narrow (UTF-16) builds this is five chars per source unichr |
| 6199 | since there are two unichrs in the surrogate pair, so in narrow |
| 6200 | (UTF-16) builds it's not the longest unichr escape. |
| 6201 | |
| 6202 | In wide or narrow builds '\uxxxx' is 6 chars per source unichr, |
| 6203 | so in the narrow (UTF-16) build case it's the longest unichr |
| 6204 | escape. |
| 6205 | */ |
| 6206 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6207 | if (!PyUnicode_Check(unicode)) { |
| 6208 | PyErr_BadArgument(); |
| 6209 | return NULL; |
| 6210 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6211 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6212 | return NULL; |
| 6213 | len = PyUnicode_GET_LENGTH(unicode); |
| 6214 | kind = PyUnicode_KIND(unicode); |
| 6215 | data = PyUnicode_DATA(unicode); |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 6216 | switch (kind) { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6217 | case PyUnicode_1BYTE_KIND: expandsize = 4; break; |
| 6218 | case PyUnicode_2BYTE_KIND: expandsize = 6; break; |
| 6219 | case PyUnicode_4BYTE_KIND: expandsize = 10; break; |
| 6220 | } |
| 6221 | |
| 6222 | if (len == 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6223 | return PyBytes_FromStringAndSize(NULL, 0); |
| 6224 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6225 | if (len > (PY_SSIZE_T_MAX - 2 - 1) / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6226 | return PyErr_NoMemory(); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 6227 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6228 | repr = PyBytes_FromStringAndSize(NULL, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6229 | 2 |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6230 | + expandsize*len |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6231 | + 1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6232 | if (repr == NULL) |
| 6233 | return NULL; |
| 6234 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6235 | p = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6236 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6237 | for (i = 0; i < len; i++) { |
Victor Stinner | 3326cb6 | 2011-11-10 20:15:25 +0100 | [diff] [blame] | 6238 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 6239 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 6240 | /* Escape backslashes */ |
| 6241 | if (ch == '\\') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6242 | *p++ = '\\'; |
| 6243 | *p++ = (char) ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 6244 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6245 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 6246 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 6247 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 6248 | else if (ch >= 0x10000) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 6249 | assert(ch <= MAX_UNICODE); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 6250 | *p++ = '\\'; |
| 6251 | *p++ = 'U'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6252 | *p++ = Py_hexdigits[(ch >> 28) & 0x0000000F]; |
| 6253 | *p++ = Py_hexdigits[(ch >> 24) & 0x0000000F]; |
| 6254 | *p++ = Py_hexdigits[(ch >> 20) & 0x0000000F]; |
| 6255 | *p++ = Py_hexdigits[(ch >> 16) & 0x0000000F]; |
| 6256 | *p++ = Py_hexdigits[(ch >> 12) & 0x0000000F]; |
| 6257 | *p++ = Py_hexdigits[(ch >> 8) & 0x0000000F]; |
| 6258 | *p++ = Py_hexdigits[(ch >> 4) & 0x0000000F]; |
| 6259 | *p++ = Py_hexdigits[ch & 0x0000000F]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6260 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 6261 | } |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 6262 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6263 | /* Map 16-bit characters to '\uxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 6264 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6265 | *p++ = '\\'; |
| 6266 | *p++ = 'u'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6267 | *p++ = Py_hexdigits[(ch >> 12) & 0x000F]; |
| 6268 | *p++ = Py_hexdigits[(ch >> 8) & 0x000F]; |
| 6269 | *p++ = Py_hexdigits[(ch >> 4) & 0x000F]; |
| 6270 | *p++ = Py_hexdigits[ch & 0x000F]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6271 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 6272 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 6273 | /* Map special whitespace to '\t', \n', '\r' */ |
| 6274 | else if (ch == '\t') { |
| 6275 | *p++ = '\\'; |
| 6276 | *p++ = 't'; |
| 6277 | } |
| 6278 | else if (ch == '\n') { |
| 6279 | *p++ = '\\'; |
| 6280 | *p++ = 'n'; |
| 6281 | } |
| 6282 | else if (ch == '\r') { |
| 6283 | *p++ = '\\'; |
| 6284 | *p++ = 'r'; |
| 6285 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 6286 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 6287 | /* Map non-printable US ASCII to '\xhh' */ |
Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 6288 | else if (ch < ' ' || ch >= 0x7F) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6289 | *p++ = '\\'; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 6290 | *p++ = 'x'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6291 | *p++ = Py_hexdigits[(ch >> 4) & 0x000F]; |
| 6292 | *p++ = Py_hexdigits[ch & 0x000F]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6293 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 6294 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6295 | /* Copy everything else as-is */ |
| 6296 | else |
| 6297 | *p++ = (char) ch; |
| 6298 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6299 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6300 | assert(p - PyBytes_AS_STRING(repr) > 0); |
| 6301 | if (_PyBytes_Resize(&repr, p - PyBytes_AS_STRING(repr)) < 0) |
| 6302 | return NULL; |
| 6303 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6304 | } |
| 6305 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6306 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6307 | PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, |
| 6308 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6309 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6310 | PyObject *result; |
| 6311 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 6312 | if (tmp == NULL) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6313 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6314 | result = PyUnicode_AsUnicodeEscapeString(tmp); |
| 6315 | Py_DECREF(tmp); |
| 6316 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6317 | } |
| 6318 | |
| 6319 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ |
| 6320 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6321 | PyObject * |
| 6322 | PyUnicode_DecodeRawUnicodeEscape(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6323 | Py_ssize_t size, |
| 6324 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6325 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6326 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6327 | Py_ssize_t startinpos; |
| 6328 | Py_ssize_t endinpos; |
| 6329 | Py_ssize_t outpos; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6330 | PyObject *v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6331 | const char *end; |
| 6332 | const char *bs; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6333 | PyObject *errorHandler = NULL; |
| 6334 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6335 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6336 | /* Escaped strings will always be longer than the resulting |
| 6337 | 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] | 6338 | length after conversion to the true value. (But decoding error |
| 6339 | handler might have to resize the string) */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6340 | v = PyUnicode_New(size, 127); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6341 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6342 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6343 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6344 | return v; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6345 | outpos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6346 | end = s + size; |
| 6347 | while (s < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6348 | unsigned char c; |
| 6349 | Py_UCS4 x; |
| 6350 | int i; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6351 | int count; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6352 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6353 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 6354 | if (*s != '\\') { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6355 | if (unicode_putchar(&v, &outpos, (unsigned char)*s++) < 0) |
| 6356 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6357 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6358 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6359 | startinpos = s-starts; |
| 6360 | |
| 6361 | /* \u-escapes are only interpreted iff the number of leading |
| 6362 | backslashes if odd */ |
| 6363 | bs = s; |
| 6364 | for (;s < end;) { |
| 6365 | if (*s != '\\') |
| 6366 | break; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6367 | if (unicode_putchar(&v, &outpos, (unsigned char)*s++) < 0) |
| 6368 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6369 | } |
| 6370 | if (((s - bs) & 1) == 0 || |
| 6371 | s >= end || |
| 6372 | (*s != 'u' && *s != 'U')) { |
| 6373 | continue; |
| 6374 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6375 | outpos--; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6376 | count = *s=='u' ? 4 : 8; |
| 6377 | s++; |
| 6378 | |
| 6379 | /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6380 | for (x = 0, i = 0; i < count; ++i, ++s) { |
| 6381 | c = (unsigned char)*s; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 6382 | if (!Py_ISXDIGIT(c)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6383 | endinpos = s-starts; |
| 6384 | if (unicode_decode_call_errorhandler( |
| 6385 | errors, &errorHandler, |
| 6386 | "rawunicodeescape", "truncated \\uXXXX", |
| 6387 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6388 | &v, &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6389 | goto onError; |
| 6390 | goto nextByte; |
| 6391 | } |
| 6392 | x = (x<<4) & ~0xF; |
| 6393 | if (c >= '0' && c <= '9') |
| 6394 | x += c - '0'; |
| 6395 | else if (c >= 'a' && c <= 'f') |
| 6396 | x += 10 + c - 'a'; |
| 6397 | else |
| 6398 | x += 10 + c - 'A'; |
| 6399 | } |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 6400 | if (x <= MAX_UNICODE) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6401 | if (unicode_putchar(&v, &outpos, x) < 0) |
| 6402 | goto onError; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 6403 | } else { |
| 6404 | endinpos = s-starts; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6405 | if (unicode_decode_call_errorhandler( |
| 6406 | errors, &errorHandler, |
| 6407 | "rawunicodeescape", "\\Uxxxxxxxx out of range", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6408 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6409 | &v, &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6410 | goto onError; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6411 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6412 | nextByte: |
| 6413 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6414 | } |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 6415 | if (unicode_resize(&v, outpos) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6416 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6417 | Py_XDECREF(errorHandler); |
| 6418 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 6419 | return unicode_result(v); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6420 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6421 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6422 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6423 | Py_XDECREF(errorHandler); |
| 6424 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6425 | return NULL; |
| 6426 | } |
| 6427 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6428 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6429 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6430 | PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6431 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6432 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6433 | char *p; |
| 6434 | char *q; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6435 | Py_ssize_t expandsize, pos; |
| 6436 | int kind; |
| 6437 | void *data; |
| 6438 | Py_ssize_t len; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6439 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6440 | if (!PyUnicode_Check(unicode)) { |
| 6441 | PyErr_BadArgument(); |
| 6442 | return NULL; |
| 6443 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6444 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6445 | return NULL; |
| 6446 | kind = PyUnicode_KIND(unicode); |
| 6447 | data = PyUnicode_DATA(unicode); |
| 6448 | len = PyUnicode_GET_LENGTH(unicode); |
Benjamin Peterson | 1518e87 | 2011-11-23 10:44:52 -0600 | [diff] [blame] | 6449 | /* 4 byte characters can take up 10 bytes, 2 byte characters can take up 6 |
| 6450 | bytes, and 1 byte characters 4. */ |
| 6451 | expandsize = kind * 2 + 2; |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 6452 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6453 | if (len > PY_SSIZE_T_MAX / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6454 | return PyErr_NoMemory(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6455 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6456 | repr = PyBytes_FromStringAndSize(NULL, expandsize * len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6457 | if (repr == NULL) |
| 6458 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6459 | if (len == 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6460 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6461 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6462 | p = q = PyBytes_AS_STRING(repr); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6463 | for (pos = 0; pos < len; pos++) { |
| 6464 | Py_UCS4 ch = PyUnicode_READ(kind, data, pos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6465 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 6466 | if (ch >= 0x10000) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 6467 | assert(ch <= MAX_UNICODE); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6468 | *p++ = '\\'; |
| 6469 | *p++ = 'U'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6470 | *p++ = Py_hexdigits[(ch >> 28) & 0xf]; |
| 6471 | *p++ = Py_hexdigits[(ch >> 24) & 0xf]; |
| 6472 | *p++ = Py_hexdigits[(ch >> 20) & 0xf]; |
| 6473 | *p++ = Py_hexdigits[(ch >> 16) & 0xf]; |
| 6474 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 6475 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 6476 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 6477 | *p++ = Py_hexdigits[ch & 15]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6478 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6479 | /* Map 16-bit characters to '\uxxxx' */ |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6480 | else if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6481 | *p++ = '\\'; |
| 6482 | *p++ = 'u'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6483 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 6484 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 6485 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 6486 | *p++ = Py_hexdigits[ch & 15]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6487 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6488 | /* Copy everything else as-is */ |
| 6489 | else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6490 | *p++ = (char) ch; |
| 6491 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6492 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6493 | assert(p > q); |
| 6494 | if (_PyBytes_Resize(&repr, p - q) < 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6495 | return NULL; |
| 6496 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6497 | } |
| 6498 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6499 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6500 | PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
| 6501 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6502 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6503 | PyObject *result; |
| 6504 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 6505 | if (tmp == NULL) |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 6506 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6507 | result = PyUnicode_AsRawUnicodeEscapeString(tmp); |
| 6508 | Py_DECREF(tmp); |
| 6509 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6510 | } |
| 6511 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6512 | /* --- Unicode Internal Codec ------------------------------------------- */ |
| 6513 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6514 | PyObject * |
| 6515 | _PyUnicode_DecodeUnicodeInternal(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6516 | Py_ssize_t size, |
| 6517 | const char *errors) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6518 | { |
| 6519 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6520 | Py_ssize_t startinpos; |
| 6521 | Py_ssize_t endinpos; |
| 6522 | Py_ssize_t outpos; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6523 | PyObject *v; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6524 | const char *end; |
| 6525 | const char *reason; |
| 6526 | PyObject *errorHandler = NULL; |
| 6527 | PyObject *exc = NULL; |
| 6528 | |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6529 | if (PyErr_WarnEx(PyExc_DeprecationWarning, |
Ezio Melotti | 11060a4 | 2011-11-16 09:39:10 +0200 | [diff] [blame] | 6530 | "unicode_internal codec has been deprecated", |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6531 | 1)) |
| 6532 | return NULL; |
| 6533 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6534 | /* XXX overflow detection missing */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6535 | 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] | 6536 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6537 | goto onError; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6538 | if (PyUnicode_GET_LENGTH(v) == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6539 | return v; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6540 | outpos = 0; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6541 | end = s + size; |
| 6542 | |
| 6543 | while (s < end) { |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6544 | Py_UNICODE uch; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6545 | Py_UCS4 ch; |
| 6546 | /* We copy the raw representation one byte at a time because the |
| 6547 | pointer may be unaligned (see test_codeccallbacks). */ |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6548 | ((char *) &uch)[0] = s[0]; |
| 6549 | ((char *) &uch)[1] = s[1]; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6550 | #ifdef Py_UNICODE_WIDE |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6551 | ((char *) &uch)[2] = s[2]; |
| 6552 | ((char *) &uch)[3] = s[3]; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6553 | #endif |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6554 | ch = uch; |
| 6555 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6556 | /* We have to sanity check the raw data, otherwise doom looms for |
| 6557 | some malformed UCS-4 data. */ |
| 6558 | if ( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6559 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6560 | ch > 0x10ffff || |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6561 | #endif |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6562 | end-s < Py_UNICODE_SIZE |
| 6563 | ) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6564 | { |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6565 | startinpos = s - starts; |
| 6566 | if (end-s < Py_UNICODE_SIZE) { |
| 6567 | endinpos = end-starts; |
| 6568 | reason = "truncated input"; |
| 6569 | } |
| 6570 | else { |
| 6571 | endinpos = s - starts + Py_UNICODE_SIZE; |
| 6572 | reason = "illegal code point (> 0x10FFFF)"; |
| 6573 | } |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6574 | if (unicode_decode_call_errorhandler( |
| 6575 | errors, &errorHandler, |
| 6576 | "unicode_internal", reason, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 6577 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6578 | &v, &outpos)) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6579 | goto onError; |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6580 | continue; |
| 6581 | } |
| 6582 | |
| 6583 | s += Py_UNICODE_SIZE; |
| 6584 | #ifndef Py_UNICODE_WIDE |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 6585 | if (Py_UNICODE_IS_HIGH_SURROGATE(ch) && s < end) |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6586 | { |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6587 | Py_UNICODE uch2; |
| 6588 | ((char *) &uch2)[0] = s[0]; |
| 6589 | ((char *) &uch2)[1] = s[1]; |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 6590 | if (Py_UNICODE_IS_LOW_SURROGATE(uch2)) |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6591 | { |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 6592 | ch = Py_UNICODE_JOIN_SURROGATES(uch, uch2); |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6593 | s += Py_UNICODE_SIZE; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6594 | } |
| 6595 | } |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6596 | #endif |
| 6597 | |
| 6598 | if (unicode_putchar(&v, &outpos, ch) < 0) |
| 6599 | goto onError; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6600 | } |
| 6601 | |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 6602 | if (unicode_resize(&v, outpos) < 0) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6603 | goto onError; |
| 6604 | Py_XDECREF(errorHandler); |
| 6605 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 6606 | return unicode_result(v); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6607 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6608 | onError: |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6609 | Py_XDECREF(v); |
| 6610 | Py_XDECREF(errorHandler); |
| 6611 | Py_XDECREF(exc); |
| 6612 | return NULL; |
| 6613 | } |
| 6614 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6615 | /* --- Latin-1 Codec ------------------------------------------------------ */ |
| 6616 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6617 | PyObject * |
| 6618 | PyUnicode_DecodeLatin1(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6619 | Py_ssize_t size, |
| 6620 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6621 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6622 | /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 6623 | return _PyUnicode_FromUCS1((unsigned char*)s, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6624 | } |
| 6625 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6626 | /* create or adjust a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6627 | static void |
| 6628 | make_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6629 | const char *encoding, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6630 | PyObject *unicode, |
| 6631 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6632 | const char *reason) |
| 6633 | { |
| 6634 | if (*exceptionObject == NULL) { |
| 6635 | *exceptionObject = PyObject_CallFunction( |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6636 | PyExc_UnicodeEncodeError, "sOnns", |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6637 | encoding, unicode, startpos, endpos, reason); |
| 6638 | } |
| 6639 | else { |
| 6640 | if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 6641 | goto onError; |
| 6642 | if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) |
| 6643 | goto onError; |
| 6644 | if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) |
| 6645 | goto onError; |
| 6646 | return; |
| 6647 | onError: |
| 6648 | Py_DECREF(*exceptionObject); |
| 6649 | *exceptionObject = NULL; |
| 6650 | } |
| 6651 | } |
| 6652 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6653 | /* raises a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6654 | static void |
| 6655 | raise_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6656 | const char *encoding, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6657 | PyObject *unicode, |
| 6658 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6659 | const char *reason) |
| 6660 | { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6661 | make_encode_exception(exceptionObject, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6662 | encoding, unicode, startpos, endpos, reason); |
| 6663 | if (*exceptionObject != NULL) |
| 6664 | PyCodec_StrictErrors(*exceptionObject); |
| 6665 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6666 | |
| 6667 | /* error handling callback helper: |
| 6668 | build arguments, call the callback and check the arguments, |
| 6669 | put the result into newpos and return the replacement string, which |
| 6670 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6671 | static PyObject * |
| 6672 | unicode_encode_call_errorhandler(const char *errors, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6673 | PyObject **errorHandler, |
| 6674 | const char *encoding, const char *reason, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6675 | PyObject *unicode, PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6676 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6677 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6678 | { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6679 | 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] | 6680 | Py_ssize_t len; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6681 | PyObject *restuple; |
| 6682 | PyObject *resunicode; |
| 6683 | |
| 6684 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6685 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6686 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6687 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6688 | } |
| 6689 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6690 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6691 | return NULL; |
| 6692 | len = PyUnicode_GET_LENGTH(unicode); |
| 6693 | |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6694 | make_encode_exception(exceptionObject, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6695 | encoding, unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6696 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6697 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6698 | |
| 6699 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6700 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6701 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6702 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6703 | if (!PyTuple_Check(restuple)) { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6704 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6705 | Py_DECREF(restuple); |
| 6706 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6707 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6708 | if (!PyArg_ParseTuple(restuple, argparse, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6709 | &resunicode, newpos)) { |
| 6710 | Py_DECREF(restuple); |
| 6711 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6712 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6713 | if (!PyUnicode_Check(resunicode) && !PyBytes_Check(resunicode)) { |
| 6714 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
| 6715 | Py_DECREF(restuple); |
| 6716 | return NULL; |
| 6717 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6718 | if (*newpos<0) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6719 | *newpos = len + *newpos; |
| 6720 | if (*newpos<0 || *newpos>len) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6721 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 6722 | Py_DECREF(restuple); |
| 6723 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 6724 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6725 | Py_INCREF(resunicode); |
| 6726 | Py_DECREF(restuple); |
| 6727 | return resunicode; |
| 6728 | } |
| 6729 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6730 | static PyObject * |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6731 | unicode_encode_ucs1(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6732 | const char *errors, |
Victor Stinner | fcd9653 | 2011-11-04 00:28:50 +0100 | [diff] [blame] | 6733 | unsigned int limit) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6734 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6735 | /* input state */ |
| 6736 | Py_ssize_t pos=0, size; |
| 6737 | int kind; |
| 6738 | void *data; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6739 | /* output object */ |
| 6740 | PyObject *res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6741 | /* pointer into the output */ |
| 6742 | char *str; |
| 6743 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6744 | Py_ssize_t ressize; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6745 | const char *encoding = (limit == 256) ? "latin-1" : "ascii"; |
| 6746 | 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] | 6747 | PyObject *errorHandler = NULL; |
| 6748 | PyObject *exc = NULL; |
| 6749 | /* the following variable is used for caching string comparisons |
| 6750 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 6751 | int known_errorHandler = -1; |
| 6752 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6753 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6754 | return NULL; |
| 6755 | size = PyUnicode_GET_LENGTH(unicode); |
| 6756 | kind = PyUnicode_KIND(unicode); |
| 6757 | data = PyUnicode_DATA(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6758 | /* allocate enough for a simple encoding without |
| 6759 | replacements, if we need more, we'll resize */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6760 | if (size == 0) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 6761 | return PyBytes_FromStringAndSize(NULL, 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6762 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6763 | if (res == NULL) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6764 | return NULL; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6765 | str = PyBytes_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6766 | ressize = size; |
| 6767 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6768 | while (pos < size) { |
| 6769 | Py_UCS4 c = PyUnicode_READ(kind, data, pos); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6770 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6771 | /* can we encode this? */ |
| 6772 | if (c<limit) { |
| 6773 | /* no overflow check, because we know that the space is enough */ |
| 6774 | *str++ = (char)c; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6775 | ++pos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6776 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6777 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6778 | Py_ssize_t requiredsize; |
| 6779 | PyObject *repunicode; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6780 | Py_ssize_t repsize, newpos, respos, i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6781 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6782 | Py_ssize_t collstart = pos; |
| 6783 | Py_ssize_t collend = pos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6784 | /* find all unecodable characters */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6785 | while ((collend < size) && (PyUnicode_READ(kind, data, collend)>=limit)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6786 | ++collend; |
| 6787 | /* cache callback name lookup (if not done yet, i.e. it's the first error) */ |
| 6788 | if (known_errorHandler==-1) { |
| 6789 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 6790 | known_errorHandler = 1; |
| 6791 | else if (!strcmp(errors, "replace")) |
| 6792 | known_errorHandler = 2; |
| 6793 | else if (!strcmp(errors, "ignore")) |
| 6794 | known_errorHandler = 3; |
| 6795 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 6796 | known_errorHandler = 4; |
| 6797 | else |
| 6798 | known_errorHandler = 0; |
| 6799 | } |
| 6800 | switch (known_errorHandler) { |
| 6801 | case 1: /* strict */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6802 | raise_encode_exception(&exc, encoding, unicode, collstart, collend, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6803 | goto onError; |
| 6804 | case 2: /* replace */ |
| 6805 | while (collstart++<collend) |
| 6806 | *str++ = '?'; /* fall through */ |
| 6807 | case 3: /* ignore */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6808 | pos = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6809 | break; |
| 6810 | case 4: /* xmlcharrefreplace */ |
| 6811 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6812 | /* determine replacement size */ |
| 6813 | for (i = collstart, repsize = 0; i < collend; ++i) { |
| 6814 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 6815 | if (ch < 10) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6816 | repsize += 2+1+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6817 | else if (ch < 100) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6818 | repsize += 2+2+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6819 | else if (ch < 1000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6820 | repsize += 2+3+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6821 | else if (ch < 10000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6822 | repsize += 2+4+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6823 | else if (ch < 100000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6824 | repsize += 2+5+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6825 | else if (ch < 1000000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6826 | repsize += 2+6+1; |
Victor Stinner | 0d3721d | 2011-11-22 03:27:53 +0100 | [diff] [blame] | 6827 | else { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 6828 | assert(ch <= MAX_UNICODE); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6829 | repsize += 2+7+1; |
Victor Stinner | 0d3721d | 2011-11-22 03:27:53 +0100 | [diff] [blame] | 6830 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6831 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6832 | requiredsize = respos+repsize+(size-collend); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6833 | if (requiredsize > ressize) { |
| 6834 | if (requiredsize<2*ressize) |
| 6835 | requiredsize = 2*ressize; |
| 6836 | if (_PyBytes_Resize(&res, requiredsize)) |
| 6837 | goto onError; |
| 6838 | str = PyBytes_AS_STRING(res) + respos; |
| 6839 | ressize = requiredsize; |
| 6840 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6841 | /* generate replacement */ |
| 6842 | for (i = collstart; i < collend; ++i) { |
| 6843 | str += sprintf(str, "&#%d;", PyUnicode_READ(kind, data, i)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6844 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6845 | pos = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6846 | break; |
| 6847 | default: |
| 6848 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6849 | encoding, reason, unicode, &exc, |
| 6850 | collstart, collend, &newpos); |
| 6851 | if (repunicode == NULL || (PyUnicode_Check(repunicode) && |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6852 | PyUnicode_READY(repunicode) == -1)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6853 | goto onError; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6854 | if (PyBytes_Check(repunicode)) { |
| 6855 | /* Directly copy bytes result to output. */ |
| 6856 | repsize = PyBytes_Size(repunicode); |
| 6857 | if (repsize > 1) { |
| 6858 | /* Make room for all additional bytes. */ |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 6859 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6860 | if (_PyBytes_Resize(&res, ressize+repsize-1)) { |
| 6861 | Py_DECREF(repunicode); |
| 6862 | goto onError; |
| 6863 | } |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 6864 | str = PyBytes_AS_STRING(res) + respos; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6865 | ressize += repsize-1; |
| 6866 | } |
| 6867 | memcpy(str, PyBytes_AsString(repunicode), repsize); |
| 6868 | str += repsize; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6869 | pos = newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6870 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6871 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6872 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6873 | /* need more space? (at least enough for what we |
| 6874 | have+the replacement+the rest of the string, so |
| 6875 | we won't have to check space for encodable characters) */ |
| 6876 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6877 | repsize = PyUnicode_GET_LENGTH(repunicode); |
| 6878 | requiredsize = respos+repsize+(size-collend); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6879 | if (requiredsize > ressize) { |
| 6880 | if (requiredsize<2*ressize) |
| 6881 | requiredsize = 2*ressize; |
| 6882 | if (_PyBytes_Resize(&res, requiredsize)) { |
| 6883 | Py_DECREF(repunicode); |
| 6884 | goto onError; |
| 6885 | } |
| 6886 | str = PyBytes_AS_STRING(res) + respos; |
| 6887 | ressize = requiredsize; |
| 6888 | } |
| 6889 | /* check if there is anything unencodable in the replacement |
| 6890 | and copy it to the output */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6891 | for (i = 0; repsize-->0; ++i, ++str) { |
| 6892 | c = PyUnicode_READ_CHAR(repunicode, i); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6893 | if (c >= limit) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6894 | raise_encode_exception(&exc, encoding, unicode, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6895 | pos, pos+1, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6896 | Py_DECREF(repunicode); |
| 6897 | goto onError; |
| 6898 | } |
| 6899 | *str = (char)c; |
| 6900 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6901 | pos = newpos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6902 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6903 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6904 | } |
| 6905 | } |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6906 | /* Resize if we allocated to much */ |
| 6907 | size = str - PyBytes_AS_STRING(res); |
| 6908 | if (size < ressize) { /* If this falls res will be NULL */ |
Alexandre Vassalotti | bad1b92 | 2008-12-27 09:49:09 +0000 | [diff] [blame] | 6909 | assert(size >= 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6910 | if (_PyBytes_Resize(&res, size) < 0) |
| 6911 | goto onError; |
| 6912 | } |
| 6913 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6914 | Py_XDECREF(errorHandler); |
| 6915 | Py_XDECREF(exc); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6916 | return res; |
| 6917 | |
| 6918 | onError: |
| 6919 | Py_XDECREF(res); |
| 6920 | Py_XDECREF(errorHandler); |
| 6921 | Py_XDECREF(exc); |
| 6922 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6923 | } |
| 6924 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6925 | /* Deprecated */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6926 | PyObject * |
| 6927 | PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6928 | Py_ssize_t size, |
| 6929 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6930 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6931 | PyObject *result; |
| 6932 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 6933 | if (unicode == NULL) |
| 6934 | return NULL; |
| 6935 | result = unicode_encode_ucs1(unicode, errors, 256); |
| 6936 | Py_DECREF(unicode); |
| 6937 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6938 | } |
| 6939 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6940 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6941 | _PyUnicode_AsLatin1String(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6942 | { |
| 6943 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6944 | PyErr_BadArgument(); |
| 6945 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6946 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6947 | if (PyUnicode_READY(unicode) == -1) |
| 6948 | return NULL; |
| 6949 | /* Fast path: if it is a one-byte string, construct |
| 6950 | bytes object directly. */ |
| 6951 | if (PyUnicode_KIND(unicode) == PyUnicode_1BYTE_KIND) |
| 6952 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 6953 | PyUnicode_GET_LENGTH(unicode)); |
| 6954 | /* Non-Latin-1 characters present. Defer to above function to |
| 6955 | raise the exception. */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6956 | return unicode_encode_ucs1(unicode, errors, 256); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6957 | } |
| 6958 | |
| 6959 | PyObject* |
| 6960 | PyUnicode_AsLatin1String(PyObject *unicode) |
| 6961 | { |
| 6962 | return _PyUnicode_AsLatin1String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6963 | } |
| 6964 | |
| 6965 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ |
| 6966 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6967 | PyObject * |
| 6968 | PyUnicode_DecodeASCII(const char *s, |
| 6969 | Py_ssize_t size, |
| 6970 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6971 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6972 | const char *starts = s; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6973 | PyObject *v; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6974 | int kind; |
| 6975 | void *data; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6976 | Py_ssize_t startinpos; |
| 6977 | Py_ssize_t endinpos; |
| 6978 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6979 | const char *e; |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6980 | int has_error; |
| 6981 | const unsigned char *p = (const unsigned char *)s; |
| 6982 | const unsigned char *end = p + size; |
| 6983 | 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] | 6984 | PyObject *errorHandler = NULL; |
| 6985 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6986 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 6987 | if (size == 0) { |
| 6988 | Py_INCREF(unicode_empty); |
| 6989 | return unicode_empty; |
| 6990 | } |
| 6991 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6992 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6993 | if (size == 1 && (unsigned char)s[0] < 128) |
| 6994 | return get_latin1_char((unsigned char)s[0]); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6995 | |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6996 | has_error = 0; |
| 6997 | while (p < end && !has_error) { |
| 6998 | /* Fast path, see below in PyUnicode_DecodeUTF8Stateful for |
| 6999 | an explanation. */ |
| 7000 | if (!((size_t) p & LONG_PTR_MASK)) { |
| 7001 | /* Help register allocation */ |
| 7002 | register const unsigned char *_p = p; |
| 7003 | while (_p < aligned_end) { |
| 7004 | unsigned long value = *(unsigned long *) _p; |
| 7005 | if (value & ASCII_CHAR_MASK) { |
| 7006 | has_error = 1; |
| 7007 | break; |
| 7008 | } |
| 7009 | _p += SIZEOF_LONG; |
| 7010 | } |
| 7011 | if (_p == end) |
| 7012 | break; |
| 7013 | if (has_error) |
| 7014 | break; |
| 7015 | p = _p; |
| 7016 | } |
| 7017 | if (*p & 0x80) { |
| 7018 | has_error = 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7019 | break; |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 7020 | } |
| 7021 | else { |
| 7022 | ++p; |
| 7023 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 7024 | } |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 7025 | if (!has_error) |
| 7026 | return unicode_fromascii((const unsigned char *)s, size); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7027 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7028 | v = PyUnicode_New(size, 127); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7029 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7030 | goto onError; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7031 | kind = PyUnicode_KIND(v); |
| 7032 | data = PyUnicode_DATA(v); |
| 7033 | outpos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7034 | e = s + size; |
| 7035 | while (s < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7036 | register unsigned char c = (unsigned char)*s; |
| 7037 | if (c < 128) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7038 | PyUnicode_WRITE(kind, data, outpos++, c); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7039 | ++s; |
| 7040 | } |
| 7041 | else { |
| 7042 | startinpos = s-starts; |
| 7043 | endinpos = startinpos + 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7044 | if (unicode_decode_call_errorhandler( |
| 7045 | errors, &errorHandler, |
| 7046 | "ascii", "ordinal not in range(128)", |
| 7047 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7048 | &v, &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7049 | goto onError; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7050 | kind = PyUnicode_KIND(v); |
| 7051 | data = PyUnicode_DATA(v); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7052 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7053 | } |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 7054 | if (unicode_resize(&v, outpos) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7055 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7056 | Py_XDECREF(errorHandler); |
| 7057 | Py_XDECREF(exc); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 7058 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 7059 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7060 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7061 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7062 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7063 | Py_XDECREF(errorHandler); |
| 7064 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7065 | return NULL; |
| 7066 | } |
| 7067 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7068 | /* Deprecated */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7069 | PyObject * |
| 7070 | PyUnicode_EncodeASCII(const Py_UNICODE *p, |
| 7071 | Py_ssize_t size, |
| 7072 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7073 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7074 | PyObject *result; |
| 7075 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 7076 | if (unicode == NULL) |
| 7077 | return NULL; |
| 7078 | result = unicode_encode_ucs1(unicode, errors, 128); |
| 7079 | Py_DECREF(unicode); |
| 7080 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7081 | } |
| 7082 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7083 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7084 | _PyUnicode_AsASCIIString(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7085 | { |
| 7086 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7087 | PyErr_BadArgument(); |
| 7088 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7089 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7090 | if (PyUnicode_READY(unicode) == -1) |
| 7091 | return NULL; |
| 7092 | /* Fast path: if it is an ASCII-only string, construct bytes object |
| 7093 | directly. Else defer to above function to raise the exception. */ |
| 7094 | if (PyUnicode_MAX_CHAR_VALUE(unicode) < 128) |
| 7095 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 7096 | PyUnicode_GET_LENGTH(unicode)); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7097 | return unicode_encode_ucs1(unicode, errors, 128); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7098 | } |
| 7099 | |
| 7100 | PyObject * |
| 7101 | PyUnicode_AsASCIIString(PyObject *unicode) |
| 7102 | { |
| 7103 | return _PyUnicode_AsASCIIString(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7104 | } |
| 7105 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 7106 | #ifdef HAVE_MBCS |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 7107 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7108 | /* --- MBCS codecs for Windows -------------------------------------------- */ |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 7109 | |
Hirokazu Yamamoto | 3530246 | 2009-03-21 13:23:27 +0000 | [diff] [blame] | 7110 | #if SIZEOF_INT < SIZEOF_SIZE_T |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7111 | #define NEED_RETRY |
| 7112 | #endif |
| 7113 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7114 | #ifndef WC_ERR_INVALID_CHARS |
| 7115 | # define WC_ERR_INVALID_CHARS 0x0080 |
| 7116 | #endif |
| 7117 | |
| 7118 | static char* |
| 7119 | code_page_name(UINT code_page, PyObject **obj) |
| 7120 | { |
| 7121 | *obj = NULL; |
| 7122 | if (code_page == CP_ACP) |
| 7123 | return "mbcs"; |
| 7124 | if (code_page == CP_UTF7) |
| 7125 | return "CP_UTF7"; |
| 7126 | if (code_page == CP_UTF8) |
| 7127 | return "CP_UTF8"; |
| 7128 | |
| 7129 | *obj = PyBytes_FromFormat("cp%u", code_page); |
| 7130 | if (*obj == NULL) |
| 7131 | return NULL; |
| 7132 | return PyBytes_AS_STRING(*obj); |
| 7133 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7134 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7135 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7136 | is_dbcs_lead_byte(UINT code_page, const char *s, int offset) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7137 | { |
| 7138 | const char *curr = s + offset; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7139 | const char *prev; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7140 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7141 | if (!IsDBCSLeadByteEx(code_page, *curr)) |
| 7142 | return 0; |
| 7143 | |
| 7144 | prev = CharPrevExA(code_page, s, curr, 0); |
| 7145 | if (prev == curr) |
| 7146 | return 1; |
| 7147 | /* FIXME: This code is limited to "true" double-byte encodings, |
| 7148 | as it assumes an incomplete character consists of a single |
| 7149 | byte. */ |
| 7150 | if (curr - prev == 2) |
| 7151 | return 1; |
| 7152 | if (!IsDBCSLeadByteEx(code_page, *prev)) |
| 7153 | return 1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7154 | return 0; |
| 7155 | } |
| 7156 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7157 | static DWORD |
| 7158 | decode_code_page_flags(UINT code_page) |
| 7159 | { |
| 7160 | if (code_page == CP_UTF7) { |
| 7161 | /* The CP_UTF7 decoder only supports flags=0 */ |
| 7162 | return 0; |
| 7163 | } |
| 7164 | else |
| 7165 | return MB_ERR_INVALID_CHARS; |
| 7166 | } |
| 7167 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7168 | /* |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7169 | * Decode a byte string from a Windows code page into unicode object in strict |
| 7170 | * mode. |
| 7171 | * |
| 7172 | * Returns consumed size if succeed, returns -2 on decode error, or raise a |
| 7173 | * WindowsError and returns -1 on other error. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7174 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7175 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7176 | decode_code_page_strict(UINT code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7177 | PyObject **v, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7178 | const char *in, |
| 7179 | int insize) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7180 | { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7181 | const DWORD flags = decode_code_page_flags(code_page); |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 7182 | wchar_t *out; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7183 | DWORD outsize; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7184 | |
| 7185 | /* First get the size of the result */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7186 | assert(insize > 0); |
| 7187 | outsize = MultiByteToWideChar(code_page, flags, in, insize, NULL, 0); |
| 7188 | if (outsize <= 0) |
| 7189 | goto error; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7190 | |
| 7191 | if (*v == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7192 | /* Create unicode object */ |
Victor Stinner | ab59594 | 2011-12-17 04:59:06 +0100 | [diff] [blame] | 7193 | /* FIXME: don't use _PyUnicode_New(), but allocate a wchar_t* buffer */ |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7194 | *v = (PyObject*)_PyUnicode_New(outsize); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7195 | if (*v == NULL) |
| 7196 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7197 | out = PyUnicode_AS_UNICODE(*v); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7198 | } |
| 7199 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7200 | /* Extend unicode object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7201 | Py_ssize_t n = PyUnicode_GET_SIZE(*v); |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 7202 | if (unicode_resize(v, n + outsize) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7203 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7204 | out = PyUnicode_AS_UNICODE(*v) + n; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7205 | } |
| 7206 | |
| 7207 | /* Do the conversion */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7208 | outsize = MultiByteToWideChar(code_page, flags, in, insize, out, outsize); |
| 7209 | if (outsize <= 0) |
| 7210 | goto error; |
| 7211 | return insize; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7212 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7213 | error: |
| 7214 | if (GetLastError() == ERROR_NO_UNICODE_TRANSLATION) |
| 7215 | return -2; |
| 7216 | PyErr_SetFromWindowsErr(0); |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7217 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7218 | } |
| 7219 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7220 | /* |
| 7221 | * Decode a byte string from a code page into unicode object with an error |
| 7222 | * handler. |
| 7223 | * |
| 7224 | * Returns consumed size if succeed, or raise a WindowsError or |
| 7225 | * UnicodeDecodeError exception and returns -1 on error. |
| 7226 | */ |
| 7227 | static int |
| 7228 | decode_code_page_errors(UINT code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7229 | PyObject **v, |
| 7230 | const char *in, const int size, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7231 | const char *errors) |
| 7232 | { |
| 7233 | const char *startin = in; |
| 7234 | const char *endin = in + size; |
| 7235 | const DWORD flags = decode_code_page_flags(code_page); |
| 7236 | /* Ideally, we should get reason from FormatMessage. This is the Windows |
| 7237 | 2000 English version of the message. */ |
| 7238 | const char *reason = "No mapping for the Unicode character exists " |
| 7239 | "in the target code page."; |
| 7240 | /* each step cannot decode more than 1 character, but a character can be |
| 7241 | represented as a surrogate pair */ |
| 7242 | wchar_t buffer[2], *startout, *out; |
| 7243 | int insize, outsize; |
| 7244 | PyObject *errorHandler = NULL; |
| 7245 | PyObject *exc = NULL; |
| 7246 | PyObject *encoding_obj = NULL; |
| 7247 | char *encoding; |
| 7248 | DWORD err; |
| 7249 | int ret = -1; |
| 7250 | |
| 7251 | assert(size > 0); |
| 7252 | |
| 7253 | encoding = code_page_name(code_page, &encoding_obj); |
| 7254 | if (encoding == NULL) |
| 7255 | return -1; |
| 7256 | |
| 7257 | if (errors == NULL || strcmp(errors, "strict") == 0) { |
| 7258 | /* The last error was ERROR_NO_UNICODE_TRANSLATION, then we raise a |
| 7259 | UnicodeDecodeError. */ |
| 7260 | make_decode_exception(&exc, encoding, in, size, 0, 0, reason); |
| 7261 | if (exc != NULL) { |
| 7262 | PyCodec_StrictErrors(exc); |
| 7263 | Py_CLEAR(exc); |
| 7264 | } |
| 7265 | goto error; |
| 7266 | } |
| 7267 | |
| 7268 | if (*v == NULL) { |
| 7269 | /* Create unicode object */ |
| 7270 | if (size > PY_SSIZE_T_MAX / (Py_ssize_t)Py_ARRAY_LENGTH(buffer)) { |
| 7271 | PyErr_NoMemory(); |
| 7272 | goto error; |
| 7273 | } |
Victor Stinner | ab59594 | 2011-12-17 04:59:06 +0100 | [diff] [blame] | 7274 | /* FIXME: don't use _PyUnicode_New(), but allocate a wchar_t* buffer */ |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7275 | *v = (PyObject*)_PyUnicode_New(size * Py_ARRAY_LENGTH(buffer)); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7276 | if (*v == NULL) |
| 7277 | goto error; |
| 7278 | startout = PyUnicode_AS_UNICODE(*v); |
| 7279 | } |
| 7280 | else { |
| 7281 | /* Extend unicode object */ |
| 7282 | Py_ssize_t n = PyUnicode_GET_SIZE(*v); |
| 7283 | if (size > (PY_SSIZE_T_MAX - n) / (Py_ssize_t)Py_ARRAY_LENGTH(buffer)) { |
| 7284 | PyErr_NoMemory(); |
| 7285 | goto error; |
| 7286 | } |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 7287 | if (unicode_resize(v, n + size * Py_ARRAY_LENGTH(buffer)) < 0) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7288 | goto error; |
| 7289 | startout = PyUnicode_AS_UNICODE(*v) + n; |
| 7290 | } |
| 7291 | |
| 7292 | /* Decode the byte string character per character */ |
| 7293 | out = startout; |
| 7294 | while (in < endin) |
| 7295 | { |
| 7296 | /* Decode a character */ |
| 7297 | insize = 1; |
| 7298 | do |
| 7299 | { |
| 7300 | outsize = MultiByteToWideChar(code_page, flags, |
| 7301 | in, insize, |
| 7302 | buffer, Py_ARRAY_LENGTH(buffer)); |
| 7303 | if (outsize > 0) |
| 7304 | break; |
| 7305 | err = GetLastError(); |
| 7306 | if (err != ERROR_NO_UNICODE_TRANSLATION |
| 7307 | && err != ERROR_INSUFFICIENT_BUFFER) |
| 7308 | { |
| 7309 | PyErr_SetFromWindowsErr(0); |
| 7310 | goto error; |
| 7311 | } |
| 7312 | insize++; |
| 7313 | } |
| 7314 | /* 4=maximum length of a UTF-8 sequence */ |
| 7315 | while (insize <= 4 && (in + insize) <= endin); |
| 7316 | |
| 7317 | if (outsize <= 0) { |
| 7318 | Py_ssize_t startinpos, endinpos, outpos; |
| 7319 | |
| 7320 | startinpos = in - startin; |
| 7321 | endinpos = startinpos + 1; |
| 7322 | outpos = out - PyUnicode_AS_UNICODE(*v); |
| 7323 | if (unicode_decode_call_errorhandler( |
| 7324 | errors, &errorHandler, |
| 7325 | encoding, reason, |
| 7326 | &startin, &endin, &startinpos, &endinpos, &exc, &in, |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 7327 | v, &outpos)) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7328 | { |
| 7329 | goto error; |
| 7330 | } |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 7331 | out = PyUnicode_AS_UNICODE(*v) + outpos; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7332 | } |
| 7333 | else { |
| 7334 | in += insize; |
| 7335 | memcpy(out, buffer, outsize * sizeof(wchar_t)); |
| 7336 | out += outsize; |
| 7337 | } |
| 7338 | } |
| 7339 | |
| 7340 | /* write a NUL character at the end */ |
| 7341 | *out = 0; |
| 7342 | |
| 7343 | /* Extend unicode object */ |
| 7344 | outsize = out - startout; |
| 7345 | assert(outsize <= PyUnicode_WSTR_LENGTH(*v)); |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 7346 | if (unicode_resize(v, outsize) < 0) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7347 | goto error; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7348 | ret = size; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7349 | |
| 7350 | error: |
| 7351 | Py_XDECREF(encoding_obj); |
| 7352 | Py_XDECREF(errorHandler); |
| 7353 | Py_XDECREF(exc); |
| 7354 | return ret; |
| 7355 | } |
| 7356 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7357 | static PyObject * |
| 7358 | decode_code_page_stateful(int code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7359 | const char *s, Py_ssize_t size, |
| 7360 | const char *errors, Py_ssize_t *consumed) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7361 | { |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7362 | PyObject *v = NULL; |
| 7363 | int chunk_size, final, converted, done; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7364 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7365 | if (code_page < 0) { |
| 7366 | PyErr_SetString(PyExc_ValueError, "invalid code page number"); |
| 7367 | return NULL; |
| 7368 | } |
| 7369 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7370 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7371 | *consumed = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7372 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7373 | do |
| 7374 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7375 | #ifdef NEED_RETRY |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7376 | if (size > INT_MAX) { |
| 7377 | chunk_size = INT_MAX; |
| 7378 | final = 0; |
| 7379 | done = 0; |
| 7380 | } |
| 7381 | else |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7382 | #endif |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7383 | { |
| 7384 | chunk_size = (int)size; |
| 7385 | final = (consumed == NULL); |
| 7386 | done = 1; |
| 7387 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7388 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7389 | /* Skip trailing lead-byte unless 'final' is set */ |
| 7390 | if (!final && is_dbcs_lead_byte(code_page, s, chunk_size - 1)) |
| 7391 | --chunk_size; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7392 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7393 | if (chunk_size == 0 && done) { |
| 7394 | if (v != NULL) |
| 7395 | break; |
| 7396 | Py_INCREF(unicode_empty); |
| 7397 | return unicode_empty; |
| 7398 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7399 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7400 | |
| 7401 | converted = decode_code_page_strict(code_page, &v, |
| 7402 | s, chunk_size); |
| 7403 | if (converted == -2) |
| 7404 | converted = decode_code_page_errors(code_page, &v, |
| 7405 | s, chunk_size, |
| 7406 | errors); |
| 7407 | assert(converted != 0); |
| 7408 | |
| 7409 | if (converted < 0) { |
| 7410 | Py_XDECREF(v); |
| 7411 | return NULL; |
| 7412 | } |
| 7413 | |
| 7414 | if (consumed) |
| 7415 | *consumed += converted; |
| 7416 | |
| 7417 | s += converted; |
| 7418 | size -= converted; |
| 7419 | } while (!done); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7420 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 7421 | return unicode_result(v); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7422 | } |
| 7423 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7424 | PyObject * |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7425 | PyUnicode_DecodeCodePageStateful(int code_page, |
| 7426 | const char *s, |
| 7427 | Py_ssize_t size, |
| 7428 | const char *errors, |
| 7429 | Py_ssize_t *consumed) |
| 7430 | { |
| 7431 | return decode_code_page_stateful(code_page, s, size, errors, consumed); |
| 7432 | } |
| 7433 | |
| 7434 | PyObject * |
| 7435 | PyUnicode_DecodeMBCSStateful(const char *s, |
| 7436 | Py_ssize_t size, |
| 7437 | const char *errors, |
| 7438 | Py_ssize_t *consumed) |
| 7439 | { |
| 7440 | return decode_code_page_stateful(CP_ACP, s, size, errors, consumed); |
| 7441 | } |
| 7442 | |
| 7443 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7444 | PyUnicode_DecodeMBCS(const char *s, |
| 7445 | Py_ssize_t size, |
| 7446 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7447 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7448 | return PyUnicode_DecodeMBCSStateful(s, size, errors, NULL); |
| 7449 | } |
| 7450 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7451 | static DWORD |
| 7452 | encode_code_page_flags(UINT code_page, const char *errors) |
| 7453 | { |
| 7454 | if (code_page == CP_UTF8) { |
| 7455 | if (winver.dwMajorVersion >= 6) |
| 7456 | /* CP_UTF8 supports WC_ERR_INVALID_CHARS on Windows Vista |
| 7457 | and later */ |
| 7458 | return WC_ERR_INVALID_CHARS; |
| 7459 | else |
| 7460 | /* CP_UTF8 only supports flags=0 on Windows older than Vista */ |
| 7461 | return 0; |
| 7462 | } |
| 7463 | else if (code_page == CP_UTF7) { |
| 7464 | /* CP_UTF7 only supports flags=0 */ |
| 7465 | return 0; |
| 7466 | } |
| 7467 | else { |
| 7468 | if (errors != NULL && strcmp(errors, "replace") == 0) |
| 7469 | return 0; |
| 7470 | else |
| 7471 | return WC_NO_BEST_FIT_CHARS; |
| 7472 | } |
| 7473 | } |
| 7474 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7475 | /* |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7476 | * Encode a Unicode string to a Windows code page into a byte string in strict |
| 7477 | * mode. |
| 7478 | * |
| 7479 | * Returns consumed characters if succeed, returns -2 on encode error, or raise |
| 7480 | * a WindowsError and returns -1 on other error. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7481 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7482 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7483 | encode_code_page_strict(UINT code_page, PyObject **outbytes, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7484 | PyObject *unicode, Py_ssize_t offset, int len, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7485 | const char* errors) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7486 | { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7487 | BOOL usedDefaultChar = FALSE; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7488 | BOOL *pusedDefaultChar = &usedDefaultChar; |
| 7489 | int outsize; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7490 | PyObject *exc = NULL; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 7491 | wchar_t *p; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7492 | Py_ssize_t size; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7493 | const DWORD flags = encode_code_page_flags(code_page, NULL); |
| 7494 | char *out; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7495 | /* Create a substring so that we can get the UTF-16 representation |
| 7496 | of just the slice under consideration. */ |
| 7497 | PyObject *substring; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7498 | |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7499 | assert(len > 0); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7500 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7501 | if (code_page != CP_UTF8 && code_page != CP_UTF7) |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7502 | pusedDefaultChar = &usedDefaultChar; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7503 | else |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7504 | pusedDefaultChar = NULL; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7505 | |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7506 | substring = PyUnicode_Substring(unicode, offset, offset+len); |
| 7507 | if (substring == NULL) |
| 7508 | return -1; |
| 7509 | p = PyUnicode_AsUnicodeAndSize(substring, &size); |
| 7510 | if (p == NULL) { |
| 7511 | Py_DECREF(substring); |
| 7512 | return -1; |
| 7513 | } |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7514 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7515 | /* First get the size of the result */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7516 | outsize = WideCharToMultiByte(code_page, flags, |
| 7517 | p, size, |
| 7518 | NULL, 0, |
| 7519 | NULL, pusedDefaultChar); |
| 7520 | if (outsize <= 0) |
| 7521 | goto error; |
| 7522 | /* If we used a default char, then we failed! */ |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7523 | if (pusedDefaultChar && *pusedDefaultChar) { |
| 7524 | Py_DECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7525 | return -2; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7526 | } |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7527 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7528 | if (*outbytes == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7529 | /* Create string object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7530 | *outbytes = PyBytes_FromStringAndSize(NULL, outsize); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7531 | if (*outbytes == NULL) { |
| 7532 | Py_DECREF(substring); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7533 | return -1; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7534 | } |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7535 | out = PyBytes_AS_STRING(*outbytes); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7536 | } |
| 7537 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7538 | /* Extend string object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7539 | const Py_ssize_t n = PyBytes_Size(*outbytes); |
| 7540 | if (outsize > PY_SSIZE_T_MAX - n) { |
| 7541 | PyErr_NoMemory(); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7542 | Py_DECREF(substring); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7543 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7544 | } |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7545 | if (_PyBytes_Resize(outbytes, n + outsize) < 0) { |
| 7546 | Py_DECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7547 | return -1; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7548 | } |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7549 | out = PyBytes_AS_STRING(*outbytes) + n; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7550 | } |
| 7551 | |
| 7552 | /* Do the conversion */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7553 | outsize = WideCharToMultiByte(code_page, flags, |
| 7554 | p, size, |
| 7555 | out, outsize, |
| 7556 | NULL, pusedDefaultChar); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7557 | Py_CLEAR(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7558 | if (outsize <= 0) |
| 7559 | goto error; |
| 7560 | if (pusedDefaultChar && *pusedDefaultChar) |
| 7561 | return -2; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7562 | return 0; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7563 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7564 | error: |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7565 | Py_XDECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7566 | if (GetLastError() == ERROR_NO_UNICODE_TRANSLATION) |
| 7567 | return -2; |
| 7568 | PyErr_SetFromWindowsErr(0); |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7569 | return -1; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7570 | } |
| 7571 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7572 | /* |
| 7573 | * Encode a Unicode string to a Windows code page into a byte string using a |
| 7574 | * error handler. |
| 7575 | * |
| 7576 | * Returns consumed characters if succeed, or raise a WindowsError and returns |
| 7577 | * -1 on other error. |
| 7578 | */ |
| 7579 | static int |
| 7580 | encode_code_page_errors(UINT code_page, PyObject **outbytes, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7581 | PyObject *unicode, Py_ssize_t unicode_offset, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7582 | Py_ssize_t insize, const char* errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7583 | { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7584 | const DWORD flags = encode_code_page_flags(code_page, errors); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7585 | Py_ssize_t pos = unicode_offset; |
| 7586 | Py_ssize_t endin = unicode_offset + insize; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7587 | /* Ideally, we should get reason from FormatMessage. This is the Windows |
| 7588 | 2000 English version of the message. */ |
| 7589 | const char *reason = "invalid character"; |
| 7590 | /* 4=maximum length of a UTF-8 sequence */ |
| 7591 | char buffer[4]; |
| 7592 | BOOL usedDefaultChar = FALSE, *pusedDefaultChar; |
| 7593 | Py_ssize_t outsize; |
| 7594 | char *out; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7595 | PyObject *errorHandler = NULL; |
| 7596 | PyObject *exc = NULL; |
| 7597 | PyObject *encoding_obj = NULL; |
| 7598 | char *encoding; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7599 | Py_ssize_t newpos, newoutsize; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7600 | PyObject *rep; |
| 7601 | int ret = -1; |
| 7602 | |
| 7603 | assert(insize > 0); |
| 7604 | |
| 7605 | encoding = code_page_name(code_page, &encoding_obj); |
| 7606 | if (encoding == NULL) |
| 7607 | return -1; |
| 7608 | |
| 7609 | if (errors == NULL || strcmp(errors, "strict") == 0) { |
| 7610 | /* The last error was ERROR_NO_UNICODE_TRANSLATION, |
| 7611 | then we raise a UnicodeEncodeError. */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 7612 | make_encode_exception(&exc, encoding, unicode, 0, 0, reason); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7613 | if (exc != NULL) { |
| 7614 | PyCodec_StrictErrors(exc); |
| 7615 | Py_DECREF(exc); |
| 7616 | } |
| 7617 | Py_XDECREF(encoding_obj); |
| 7618 | return -1; |
| 7619 | } |
| 7620 | |
| 7621 | if (code_page != CP_UTF8 && code_page != CP_UTF7) |
| 7622 | pusedDefaultChar = &usedDefaultChar; |
| 7623 | else |
| 7624 | pusedDefaultChar = NULL; |
| 7625 | |
| 7626 | if (Py_ARRAY_LENGTH(buffer) > PY_SSIZE_T_MAX / insize) { |
| 7627 | PyErr_NoMemory(); |
| 7628 | goto error; |
| 7629 | } |
| 7630 | outsize = insize * Py_ARRAY_LENGTH(buffer); |
| 7631 | |
| 7632 | if (*outbytes == NULL) { |
| 7633 | /* Create string object */ |
| 7634 | *outbytes = PyBytes_FromStringAndSize(NULL, outsize); |
| 7635 | if (*outbytes == NULL) |
| 7636 | goto error; |
| 7637 | out = PyBytes_AS_STRING(*outbytes); |
| 7638 | } |
| 7639 | else { |
| 7640 | /* Extend string object */ |
| 7641 | Py_ssize_t n = PyBytes_Size(*outbytes); |
| 7642 | if (n > PY_SSIZE_T_MAX - outsize) { |
| 7643 | PyErr_NoMemory(); |
| 7644 | goto error; |
| 7645 | } |
| 7646 | if (_PyBytes_Resize(outbytes, n + outsize) < 0) |
| 7647 | goto error; |
| 7648 | out = PyBytes_AS_STRING(*outbytes) + n; |
| 7649 | } |
| 7650 | |
| 7651 | /* Encode the string character per character */ |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7652 | while (pos < endin) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7653 | { |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7654 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, pos); |
| 7655 | wchar_t chars[2]; |
| 7656 | int charsize; |
| 7657 | if (ch < 0x10000) { |
| 7658 | chars[0] = (wchar_t)ch; |
| 7659 | charsize = 1; |
| 7660 | } |
| 7661 | else { |
| 7662 | ch -= 0x10000; |
| 7663 | chars[0] = 0xd800 + (ch >> 10); |
| 7664 | chars[1] = 0xdc00 + (ch & 0x3ff); |
| 7665 | charsize = 2; |
| 7666 | } |
| 7667 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7668 | outsize = WideCharToMultiByte(code_page, flags, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7669 | chars, charsize, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7670 | buffer, Py_ARRAY_LENGTH(buffer), |
| 7671 | NULL, pusedDefaultChar); |
| 7672 | if (outsize > 0) { |
| 7673 | if (pusedDefaultChar == NULL || !(*pusedDefaultChar)) |
| 7674 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7675 | pos++; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7676 | memcpy(out, buffer, outsize); |
| 7677 | out += outsize; |
| 7678 | continue; |
| 7679 | } |
| 7680 | } |
| 7681 | else if (GetLastError() != ERROR_NO_UNICODE_TRANSLATION) { |
| 7682 | PyErr_SetFromWindowsErr(0); |
| 7683 | goto error; |
| 7684 | } |
| 7685 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7686 | rep = unicode_encode_call_errorhandler( |
| 7687 | errors, &errorHandler, encoding, reason, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7688 | unicode, &exc, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7689 | pos, pos + 1, &newpos); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7690 | if (rep == NULL) |
| 7691 | goto error; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7692 | pos = newpos; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7693 | |
| 7694 | if (PyBytes_Check(rep)) { |
| 7695 | outsize = PyBytes_GET_SIZE(rep); |
| 7696 | if (outsize != 1) { |
| 7697 | Py_ssize_t offset = out - PyBytes_AS_STRING(*outbytes); |
| 7698 | newoutsize = PyBytes_GET_SIZE(*outbytes) + (outsize - 1); |
| 7699 | if (_PyBytes_Resize(outbytes, newoutsize) < 0) { |
| 7700 | Py_DECREF(rep); |
| 7701 | goto error; |
| 7702 | } |
| 7703 | out = PyBytes_AS_STRING(*outbytes) + offset; |
| 7704 | } |
| 7705 | memcpy(out, PyBytes_AS_STRING(rep), outsize); |
| 7706 | out += outsize; |
| 7707 | } |
| 7708 | else { |
| 7709 | Py_ssize_t i; |
| 7710 | enum PyUnicode_Kind kind; |
| 7711 | void *data; |
| 7712 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7713 | if (PyUnicode_READY(rep) == -1) { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7714 | Py_DECREF(rep); |
| 7715 | goto error; |
| 7716 | } |
| 7717 | |
| 7718 | outsize = PyUnicode_GET_LENGTH(rep); |
| 7719 | if (outsize != 1) { |
| 7720 | Py_ssize_t offset = out - PyBytes_AS_STRING(*outbytes); |
| 7721 | newoutsize = PyBytes_GET_SIZE(*outbytes) + (outsize - 1); |
| 7722 | if (_PyBytes_Resize(outbytes, newoutsize) < 0) { |
| 7723 | Py_DECREF(rep); |
| 7724 | goto error; |
| 7725 | } |
| 7726 | out = PyBytes_AS_STRING(*outbytes) + offset; |
| 7727 | } |
| 7728 | kind = PyUnicode_KIND(rep); |
| 7729 | data = PyUnicode_DATA(rep); |
| 7730 | for (i=0; i < outsize; i++) { |
| 7731 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 7732 | if (ch > 127) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 7733 | raise_encode_exception(&exc, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7734 | encoding, unicode, |
| 7735 | pos, pos + 1, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7736 | "unable to encode error handler result to ASCII"); |
| 7737 | Py_DECREF(rep); |
| 7738 | goto error; |
| 7739 | } |
| 7740 | *out = (unsigned char)ch; |
| 7741 | out++; |
| 7742 | } |
| 7743 | } |
| 7744 | Py_DECREF(rep); |
| 7745 | } |
| 7746 | /* write a NUL byte */ |
| 7747 | *out = 0; |
| 7748 | outsize = out - PyBytes_AS_STRING(*outbytes); |
| 7749 | assert(outsize <= PyBytes_GET_SIZE(*outbytes)); |
| 7750 | if (_PyBytes_Resize(outbytes, outsize) < 0) |
| 7751 | goto error; |
| 7752 | ret = 0; |
| 7753 | |
| 7754 | error: |
| 7755 | Py_XDECREF(encoding_obj); |
| 7756 | Py_XDECREF(errorHandler); |
| 7757 | Py_XDECREF(exc); |
| 7758 | return ret; |
| 7759 | } |
| 7760 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7761 | static PyObject * |
| 7762 | encode_code_page(int code_page, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7763 | PyObject *unicode, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7764 | const char *errors) |
| 7765 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7766 | Py_ssize_t len; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7767 | PyObject *outbytes = NULL; |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7768 | Py_ssize_t offset; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7769 | int chunk_len, ret, done; |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7770 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7771 | if (PyUnicode_READY(unicode) == -1) |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7772 | return NULL; |
| 7773 | len = PyUnicode_GET_LENGTH(unicode); |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 7774 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7775 | if (code_page < 0) { |
| 7776 | PyErr_SetString(PyExc_ValueError, "invalid code page number"); |
| 7777 | return NULL; |
| 7778 | } |
| 7779 | |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7780 | if (len == 0) |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7781 | return PyBytes_FromStringAndSize(NULL, 0); |
| 7782 | |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7783 | offset = 0; |
| 7784 | do |
| 7785 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7786 | #ifdef NEED_RETRY |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7787 | /* 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] | 7788 | chunks. */ |
| 7789 | if (len > INT_MAX/2) { |
| 7790 | chunk_len = INT_MAX/2; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7791 | done = 0; |
| 7792 | } |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7793 | else |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7794 | #endif |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7795 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7796 | chunk_len = (int)len; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7797 | done = 1; |
| 7798 | } |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7799 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7800 | ret = encode_code_page_strict(code_page, &outbytes, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7801 | unicode, offset, chunk_len, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7802 | errors); |
| 7803 | if (ret == -2) |
| 7804 | ret = encode_code_page_errors(code_page, &outbytes, |
| 7805 | unicode, offset, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7806 | chunk_len, errors); |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7807 | if (ret < 0) { |
| 7808 | Py_XDECREF(outbytes); |
| 7809 | return NULL; |
| 7810 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7811 | |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7812 | offset += chunk_len; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7813 | len -= chunk_len; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7814 | } while (!done); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7815 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7816 | return outbytes; |
| 7817 | } |
| 7818 | |
| 7819 | PyObject * |
| 7820 | PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
| 7821 | Py_ssize_t size, |
| 7822 | const char *errors) |
| 7823 | { |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7824 | PyObject *unicode, *res; |
| 7825 | unicode = PyUnicode_FromUnicode(p, size); |
| 7826 | if (unicode == NULL) |
| 7827 | return NULL; |
| 7828 | res = encode_code_page(CP_ACP, unicode, errors); |
| 7829 | Py_DECREF(unicode); |
| 7830 | return res; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7831 | } |
| 7832 | |
| 7833 | PyObject * |
| 7834 | PyUnicode_EncodeCodePage(int code_page, |
| 7835 | PyObject *unicode, |
| 7836 | const char *errors) |
| 7837 | { |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7838 | return encode_code_page(code_page, unicode, errors); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7839 | } |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 7840 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7841 | PyObject * |
| 7842 | PyUnicode_AsMBCSString(PyObject *unicode) |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 7843 | { |
| 7844 | if (!PyUnicode_Check(unicode)) { |
| 7845 | PyErr_BadArgument(); |
| 7846 | return NULL; |
| 7847 | } |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7848 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, NULL); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 7849 | } |
| 7850 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7851 | #undef NEED_RETRY |
| 7852 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 7853 | #endif /* HAVE_MBCS */ |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7854 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7855 | /* --- Character Mapping Codec -------------------------------------------- */ |
| 7856 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7857 | PyObject * |
| 7858 | PyUnicode_DecodeCharmap(const char *s, |
| 7859 | Py_ssize_t size, |
| 7860 | PyObject *mapping, |
| 7861 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7862 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7863 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7864 | Py_ssize_t startinpos; |
| 7865 | Py_ssize_t endinpos; |
| 7866 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7867 | const char *e; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 7868 | PyObject *v; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7869 | Py_ssize_t extrachars = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7870 | PyObject *errorHandler = NULL; |
| 7871 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7872 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7873 | /* Default to Latin-1 */ |
| 7874 | if (mapping == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7875 | return PyUnicode_DecodeLatin1(s, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7876 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7877 | v = PyUnicode_New(size, 127); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7878 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7879 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7880 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 7881 | return v; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7882 | outpos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7883 | e = s + size; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7884 | if (PyUnicode_CheckExact(mapping)) { |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7885 | Py_ssize_t maplen; |
| 7886 | enum PyUnicode_Kind kind; |
| 7887 | void *data; |
| 7888 | Py_UCS4 x; |
| 7889 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7890 | if (PyUnicode_READY(mapping) == -1) |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7891 | return NULL; |
| 7892 | |
| 7893 | maplen = PyUnicode_GET_LENGTH(mapping); |
| 7894 | data = PyUnicode_DATA(mapping); |
| 7895 | kind = PyUnicode_KIND(mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7896 | while (s < e) { |
| 7897 | unsigned char ch = *s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7898 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7899 | if (ch < maplen) |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7900 | x = PyUnicode_READ(kind, data, ch); |
| 7901 | else |
| 7902 | x = 0xfffe; /* invalid value */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7903 | |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7904 | if (x == 0xfffe) |
| 7905 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7906 | /* undefined mapping */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7907 | startinpos = s-starts; |
| 7908 | endinpos = startinpos+1; |
| 7909 | if (unicode_decode_call_errorhandler( |
| 7910 | errors, &errorHandler, |
| 7911 | "charmap", "character maps to <undefined>", |
| 7912 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7913 | &v, &outpos)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7914 | goto onError; |
| 7915 | } |
| 7916 | continue; |
| 7917 | } |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7918 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7919 | if (unicode_putchar(&v, &outpos, x) < 0) |
| 7920 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7921 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7922 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7923 | } |
| 7924 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7925 | while (s < e) { |
| 7926 | unsigned char ch = *s; |
| 7927 | PyObject *w, *x; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7928 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7929 | /* Get mapping (char ordinal -> integer, Unicode char or None) */ |
| 7930 | w = PyLong_FromLong((long)ch); |
| 7931 | if (w == NULL) |
| 7932 | goto onError; |
| 7933 | x = PyObject_GetItem(mapping, w); |
| 7934 | Py_DECREF(w); |
| 7935 | if (x == NULL) { |
| 7936 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 7937 | /* No mapping found means: mapping is undefined. */ |
| 7938 | PyErr_Clear(); |
| 7939 | x = Py_None; |
| 7940 | Py_INCREF(x); |
| 7941 | } else |
| 7942 | goto onError; |
| 7943 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7944 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7945 | /* Apply mapping */ |
| 7946 | if (PyLong_Check(x)) { |
| 7947 | long value = PyLong_AS_LONG(x); |
| 7948 | if (value < 0 || value > 65535) { |
| 7949 | PyErr_SetString(PyExc_TypeError, |
| 7950 | "character mapping must be in range(65536)"); |
| 7951 | Py_DECREF(x); |
| 7952 | goto onError; |
| 7953 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7954 | if (unicode_putchar(&v, &outpos, value) < 0) |
| 7955 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7956 | } |
| 7957 | else if (x == Py_None) { |
| 7958 | /* undefined mapping */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7959 | startinpos = s-starts; |
| 7960 | endinpos = startinpos+1; |
| 7961 | if (unicode_decode_call_errorhandler( |
| 7962 | errors, &errorHandler, |
| 7963 | "charmap", "character maps to <undefined>", |
| 7964 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7965 | &v, &outpos)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7966 | Py_DECREF(x); |
| 7967 | goto onError; |
| 7968 | } |
| 7969 | Py_DECREF(x); |
| 7970 | continue; |
| 7971 | } |
| 7972 | else if (PyUnicode_Check(x)) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7973 | Py_ssize_t targetsize; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7974 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7975 | if (PyUnicode_READY(x) == -1) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7976 | goto onError; |
| 7977 | targetsize = PyUnicode_GET_LENGTH(x); |
| 7978 | |
| 7979 | if (targetsize == 1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7980 | /* 1-1 mapping */ |
Victor Stinner | 62aa4d0 | 2011-11-09 00:03:45 +0100 | [diff] [blame] | 7981 | if (unicode_putchar(&v, &outpos, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7982 | PyUnicode_READ_CHAR(x, 0)) < 0) |
| 7983 | goto onError; |
| 7984 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7985 | else if (targetsize > 1) { |
| 7986 | /* 1-n mapping */ |
| 7987 | if (targetsize > extrachars) { |
| 7988 | /* resize first */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7989 | Py_ssize_t needed = (targetsize - extrachars) + \ |
| 7990 | (targetsize << 2); |
| 7991 | extrachars += needed; |
| 7992 | /* XXX overflow detection missing */ |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 7993 | if (unicode_resize(&v, |
| 7994 | PyUnicode_GET_LENGTH(v) + needed) < 0) |
| 7995 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7996 | Py_DECREF(x); |
| 7997 | goto onError; |
| 7998 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7999 | } |
Victor Stinner | 1b487b4 | 2012-05-03 12:29:04 +0200 | [diff] [blame] | 8000 | 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] | 8001 | goto onError; |
| 8002 | PyUnicode_CopyCharacters(v, outpos, x, 0, targetsize); |
| 8003 | outpos += targetsize; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8004 | extrachars -= targetsize; |
| 8005 | } |
| 8006 | /* 1-0 mapping: skip the character */ |
| 8007 | } |
| 8008 | else { |
| 8009 | /* wrong return value */ |
| 8010 | PyErr_SetString(PyExc_TypeError, |
| 8011 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8012 | Py_DECREF(x); |
| 8013 | goto onError; |
| 8014 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8015 | Py_DECREF(x); |
| 8016 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8017 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8018 | } |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 8019 | if (unicode_resize(&v, outpos) < 0) |
Antoine Pitrou | a8f63c0 | 2011-11-08 18:37:16 +0100 | [diff] [blame] | 8020 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8021 | Py_XDECREF(errorHandler); |
| 8022 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 8023 | return unicode_result(v); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8024 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8025 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8026 | Py_XDECREF(errorHandler); |
| 8027 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8028 | Py_XDECREF(v); |
| 8029 | return NULL; |
| 8030 | } |
| 8031 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8032 | /* Charmap encoding: the lookup table */ |
| 8033 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8034 | struct encoding_map { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8035 | PyObject_HEAD |
| 8036 | unsigned char level1[32]; |
| 8037 | int count2, count3; |
| 8038 | unsigned char level23[1]; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8039 | }; |
| 8040 | |
| 8041 | static PyObject* |
| 8042 | encoding_map_size(PyObject *obj, PyObject* args) |
| 8043 | { |
| 8044 | struct encoding_map *map = (struct encoding_map*)obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8045 | return PyLong_FromLong(sizeof(*map) - 1 + 16*map->count2 + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8046 | 128*map->count3); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8047 | } |
| 8048 | |
| 8049 | static PyMethodDef encoding_map_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8050 | {"size", encoding_map_size, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8051 | PyDoc_STR("Return the size (in bytes) of this object") }, |
| 8052 | { 0 } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8053 | }; |
| 8054 | |
| 8055 | static void |
| 8056 | encoding_map_dealloc(PyObject* o) |
| 8057 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8058 | PyObject_FREE(o); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8059 | } |
| 8060 | |
| 8061 | static PyTypeObject EncodingMapType = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8062 | PyVarObject_HEAD_INIT(NULL, 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8063 | "EncodingMap", /*tp_name*/ |
| 8064 | sizeof(struct encoding_map), /*tp_basicsize*/ |
| 8065 | 0, /*tp_itemsize*/ |
| 8066 | /* methods */ |
| 8067 | encoding_map_dealloc, /*tp_dealloc*/ |
| 8068 | 0, /*tp_print*/ |
| 8069 | 0, /*tp_getattr*/ |
| 8070 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 8071 | 0, /*tp_reserved*/ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8072 | 0, /*tp_repr*/ |
| 8073 | 0, /*tp_as_number*/ |
| 8074 | 0, /*tp_as_sequence*/ |
| 8075 | 0, /*tp_as_mapping*/ |
| 8076 | 0, /*tp_hash*/ |
| 8077 | 0, /*tp_call*/ |
| 8078 | 0, /*tp_str*/ |
| 8079 | 0, /*tp_getattro*/ |
| 8080 | 0, /*tp_setattro*/ |
| 8081 | 0, /*tp_as_buffer*/ |
| 8082 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 8083 | 0, /*tp_doc*/ |
| 8084 | 0, /*tp_traverse*/ |
| 8085 | 0, /*tp_clear*/ |
| 8086 | 0, /*tp_richcompare*/ |
| 8087 | 0, /*tp_weaklistoffset*/ |
| 8088 | 0, /*tp_iter*/ |
| 8089 | 0, /*tp_iternext*/ |
| 8090 | encoding_map_methods, /*tp_methods*/ |
| 8091 | 0, /*tp_members*/ |
| 8092 | 0, /*tp_getset*/ |
| 8093 | 0, /*tp_base*/ |
| 8094 | 0, /*tp_dict*/ |
| 8095 | 0, /*tp_descr_get*/ |
| 8096 | 0, /*tp_descr_set*/ |
| 8097 | 0, /*tp_dictoffset*/ |
| 8098 | 0, /*tp_init*/ |
| 8099 | 0, /*tp_alloc*/ |
| 8100 | 0, /*tp_new*/ |
| 8101 | 0, /*tp_free*/ |
| 8102 | 0, /*tp_is_gc*/ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8103 | }; |
| 8104 | |
| 8105 | PyObject* |
| 8106 | PyUnicode_BuildEncodingMap(PyObject* string) |
| 8107 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8108 | PyObject *result; |
| 8109 | struct encoding_map *mresult; |
| 8110 | int i; |
| 8111 | int need_dict = 0; |
| 8112 | unsigned char level1[32]; |
| 8113 | unsigned char level2[512]; |
| 8114 | unsigned char *mlevel1, *mlevel2, *mlevel3; |
| 8115 | int count2 = 0, count3 = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8116 | int kind; |
| 8117 | void *data; |
| 8118 | Py_UCS4 ch; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8119 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8120 | if (!PyUnicode_Check(string) || PyUnicode_GET_LENGTH(string) != 256) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8121 | PyErr_BadArgument(); |
| 8122 | return NULL; |
| 8123 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8124 | kind = PyUnicode_KIND(string); |
| 8125 | data = PyUnicode_DATA(string); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8126 | memset(level1, 0xFF, sizeof level1); |
| 8127 | memset(level2, 0xFF, sizeof level2); |
| 8128 | |
| 8129 | /* If there isn't a one-to-one mapping of NULL to \0, |
| 8130 | or if there are non-BMP characters, we need to use |
| 8131 | a mapping dictionary. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8132 | if (PyUnicode_READ(kind, data, 0) != 0) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8133 | need_dict = 1; |
| 8134 | for (i = 1; i < 256; i++) { |
| 8135 | int l1, l2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8136 | ch = PyUnicode_READ(kind, data, i); |
| 8137 | if (ch == 0 || ch > 0xFFFF) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8138 | need_dict = 1; |
| 8139 | break; |
| 8140 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8141 | if (ch == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8142 | /* unmapped character */ |
| 8143 | continue; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8144 | l1 = ch >> 11; |
| 8145 | l2 = ch >> 7; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8146 | if (level1[l1] == 0xFF) |
| 8147 | level1[l1] = count2++; |
| 8148 | if (level2[l2] == 0xFF) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8149 | level2[l2] = count3++; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8150 | } |
| 8151 | |
| 8152 | if (count2 >= 0xFF || count3 >= 0xFF) |
| 8153 | need_dict = 1; |
| 8154 | |
| 8155 | if (need_dict) { |
| 8156 | PyObject *result = PyDict_New(); |
| 8157 | PyObject *key, *value; |
| 8158 | if (!result) |
| 8159 | return NULL; |
| 8160 | for (i = 0; i < 256; i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8161 | key = PyLong_FromLong(PyUnicode_READ(kind, data, i)); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8162 | value = PyLong_FromLong(i); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8163 | if (!key || !value) |
| 8164 | goto failed1; |
| 8165 | if (PyDict_SetItem(result, key, value) == -1) |
| 8166 | goto failed1; |
| 8167 | Py_DECREF(key); |
| 8168 | Py_DECREF(value); |
| 8169 | } |
| 8170 | return result; |
| 8171 | failed1: |
| 8172 | Py_XDECREF(key); |
| 8173 | Py_XDECREF(value); |
| 8174 | Py_DECREF(result); |
| 8175 | return NULL; |
| 8176 | } |
| 8177 | |
| 8178 | /* Create a three-level trie */ |
| 8179 | result = PyObject_MALLOC(sizeof(struct encoding_map) + |
| 8180 | 16*count2 + 128*count3 - 1); |
| 8181 | if (!result) |
| 8182 | return PyErr_NoMemory(); |
| 8183 | PyObject_Init(result, &EncodingMapType); |
| 8184 | mresult = (struct encoding_map*)result; |
| 8185 | mresult->count2 = count2; |
| 8186 | mresult->count3 = count3; |
| 8187 | mlevel1 = mresult->level1; |
| 8188 | mlevel2 = mresult->level23; |
| 8189 | mlevel3 = mresult->level23 + 16*count2; |
| 8190 | memcpy(mlevel1, level1, 32); |
| 8191 | memset(mlevel2, 0xFF, 16*count2); |
| 8192 | memset(mlevel3, 0, 128*count3); |
| 8193 | count3 = 0; |
| 8194 | for (i = 1; i < 256; i++) { |
| 8195 | int o1, o2, o3, i2, i3; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8196 | if (PyUnicode_READ(kind, data, i) == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8197 | /* unmapped character */ |
| 8198 | continue; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8199 | o1 = PyUnicode_READ(kind, data, i)>>11; |
| 8200 | o2 = (PyUnicode_READ(kind, data, i)>>7) & 0xF; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8201 | i2 = 16*mlevel1[o1] + o2; |
| 8202 | if (mlevel2[i2] == 0xFF) |
| 8203 | mlevel2[i2] = count3++; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8204 | o3 = PyUnicode_READ(kind, data, i) & 0x7F; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8205 | i3 = 128*mlevel2[i2] + o3; |
| 8206 | mlevel3[i3] = i; |
| 8207 | } |
| 8208 | return result; |
| 8209 | } |
| 8210 | |
| 8211 | static int |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 8212 | encoding_map_lookup(Py_UCS4 c, PyObject *mapping) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8213 | { |
| 8214 | struct encoding_map *map = (struct encoding_map*)mapping; |
| 8215 | int l1 = c>>11; |
| 8216 | int l2 = (c>>7) & 0xF; |
| 8217 | int l3 = c & 0x7F; |
| 8218 | int i; |
| 8219 | |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 8220 | if (c > 0xFFFF) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8221 | return -1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8222 | if (c == 0) |
| 8223 | return 0; |
| 8224 | /* level 1*/ |
| 8225 | i = map->level1[l1]; |
| 8226 | if (i == 0xFF) { |
| 8227 | return -1; |
| 8228 | } |
| 8229 | /* level 2*/ |
| 8230 | i = map->level23[16*i+l2]; |
| 8231 | if (i == 0xFF) { |
| 8232 | return -1; |
| 8233 | } |
| 8234 | /* level 3 */ |
| 8235 | i = map->level23[16*map->count2 + 128*i + l3]; |
| 8236 | if (i == 0) { |
| 8237 | return -1; |
| 8238 | } |
| 8239 | return i; |
| 8240 | } |
| 8241 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8242 | /* Lookup the character ch in the mapping. If the character |
| 8243 | can't be found, Py_None is returned (or NULL, if another |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 8244 | error occurred). */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8245 | static PyObject * |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 8246 | charmapencode_lookup(Py_UCS4 c, PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8247 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8248 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8249 | PyObject *x; |
| 8250 | |
| 8251 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8252 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8253 | x = PyObject_GetItem(mapping, w); |
| 8254 | Py_DECREF(w); |
| 8255 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8256 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 8257 | /* No mapping found means: mapping is undefined. */ |
| 8258 | PyErr_Clear(); |
| 8259 | x = Py_None; |
| 8260 | Py_INCREF(x); |
| 8261 | return x; |
| 8262 | } else |
| 8263 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8264 | } |
Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 8265 | else if (x == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8266 | return x; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8267 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8268 | long value = PyLong_AS_LONG(x); |
| 8269 | if (value < 0 || value > 255) { |
| 8270 | PyErr_SetString(PyExc_TypeError, |
| 8271 | "character mapping must be in range(256)"); |
| 8272 | Py_DECREF(x); |
| 8273 | return NULL; |
| 8274 | } |
| 8275 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8276 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8277 | else if (PyBytes_Check(x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8278 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8279 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8280 | /* wrong return value */ |
| 8281 | PyErr_Format(PyExc_TypeError, |
| 8282 | "character mapping must return integer, bytes or None, not %.400s", |
| 8283 | x->ob_type->tp_name); |
| 8284 | Py_DECREF(x); |
| 8285 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8286 | } |
| 8287 | } |
| 8288 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8289 | static int |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 8290 | charmapencode_resize(PyObject **outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8291 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8292 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
| 8293 | /* exponentially overallocate to minimize reallocations */ |
| 8294 | if (requiredsize < 2*outsize) |
| 8295 | requiredsize = 2*outsize; |
| 8296 | if (_PyBytes_Resize(outobj, requiredsize)) |
| 8297 | return -1; |
| 8298 | return 0; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8299 | } |
| 8300 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8301 | typedef enum charmapencode_result { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8302 | enc_SUCCESS, enc_FAILED, enc_EXCEPTION |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8303 | } charmapencode_result; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8304 | /* 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] | 8305 | various state variables. Resize the output bytes object if not enough |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8306 | space is available. Return a new reference to the object that |
| 8307 | was put in the output buffer, or Py_None, if the mapping was undefined |
| 8308 | (in which case no character was written) or NULL, if a |
Andrew M. Kuchling | 8294de5 | 2005-11-02 16:36:12 +0000 | [diff] [blame] | 8309 | reallocation error occurred. The caller must decref the result */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8310 | static charmapencode_result |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 8311 | charmapencode_output(Py_UCS4 c, PyObject *mapping, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8312 | PyObject **outobj, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8313 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8314 | PyObject *rep; |
| 8315 | char *outstart; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8316 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8317 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 8318 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8319 | int res = encoding_map_lookup(c, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8320 | Py_ssize_t requiredsize = *outpos+1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8321 | if (res == -1) |
| 8322 | return enc_FAILED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8323 | if (outsize<requiredsize) |
| 8324 | if (charmapencode_resize(outobj, outpos, requiredsize)) |
| 8325 | return enc_EXCEPTION; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8326 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8327 | outstart[(*outpos)++] = (char)res; |
| 8328 | return enc_SUCCESS; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8329 | } |
| 8330 | |
| 8331 | rep = charmapencode_lookup(c, mapping); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8332 | if (rep==NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8333 | return enc_EXCEPTION; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8334 | else if (rep==Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8335 | Py_DECREF(rep); |
| 8336 | return enc_FAILED; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8337 | } else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8338 | if (PyLong_Check(rep)) { |
| 8339 | Py_ssize_t requiredsize = *outpos+1; |
| 8340 | if (outsize<requiredsize) |
| 8341 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 8342 | Py_DECREF(rep); |
| 8343 | return enc_EXCEPTION; |
| 8344 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8345 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8346 | outstart[(*outpos)++] = (char)PyLong_AS_LONG(rep); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8347 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8348 | else { |
| 8349 | const char *repchars = PyBytes_AS_STRING(rep); |
| 8350 | Py_ssize_t repsize = PyBytes_GET_SIZE(rep); |
| 8351 | Py_ssize_t requiredsize = *outpos+repsize; |
| 8352 | if (outsize<requiredsize) |
| 8353 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 8354 | Py_DECREF(rep); |
| 8355 | return enc_EXCEPTION; |
| 8356 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8357 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8358 | memcpy(outstart + *outpos, repchars, repsize); |
| 8359 | *outpos += repsize; |
| 8360 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8361 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8362 | Py_DECREF(rep); |
| 8363 | return enc_SUCCESS; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8364 | } |
| 8365 | |
| 8366 | /* handle an error in PyUnicode_EncodeCharmap |
| 8367 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8368 | static int |
| 8369 | charmap_encoding_error( |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8370 | PyObject *unicode, Py_ssize_t *inpos, PyObject *mapping, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8371 | PyObject **exceptionObject, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 8372 | int *known_errorHandler, PyObject **errorHandler, const char *errors, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 8373 | PyObject **res, Py_ssize_t *respos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8374 | { |
| 8375 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8376 | Py_ssize_t size, repsize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8377 | Py_ssize_t newpos; |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8378 | enum PyUnicode_Kind kind; |
| 8379 | void *data; |
| 8380 | Py_ssize_t index; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8381 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8382 | Py_ssize_t collstartpos = *inpos; |
| 8383 | Py_ssize_t collendpos = *inpos+1; |
| 8384 | Py_ssize_t collpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8385 | char *encoding = "charmap"; |
| 8386 | char *reason = "character maps to <undefined>"; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8387 | charmapencode_result x; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8388 | Py_UCS4 ch; |
Brian Curtin | 2787ea4 | 2011-11-02 15:09:37 -0500 | [diff] [blame] | 8389 | int val; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8390 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 8391 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8392 | return -1; |
| 8393 | size = PyUnicode_GET_LENGTH(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8394 | /* find all unencodable characters */ |
| 8395 | while (collendpos < size) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8396 | PyObject *rep; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 8397 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8398 | ch = PyUnicode_READ_CHAR(unicode, collendpos); |
Brian Curtin | 2787ea4 | 2011-11-02 15:09:37 -0500 | [diff] [blame] | 8399 | val = encoding_map_lookup(ch, mapping); |
| 8400 | if (val != -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8401 | break; |
| 8402 | ++collendpos; |
| 8403 | continue; |
| 8404 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8405 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8406 | ch = PyUnicode_READ_CHAR(unicode, collendpos); |
| 8407 | rep = charmapencode_lookup(ch, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8408 | if (rep==NULL) |
| 8409 | return -1; |
| 8410 | else if (rep!=Py_None) { |
| 8411 | Py_DECREF(rep); |
| 8412 | break; |
| 8413 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8414 | Py_DECREF(rep); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8415 | ++collendpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8416 | } |
| 8417 | /* cache callback name lookup |
| 8418 | * (if not done yet, i.e. it's the first error) */ |
| 8419 | if (*known_errorHandler==-1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8420 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 8421 | *known_errorHandler = 1; |
| 8422 | else if (!strcmp(errors, "replace")) |
| 8423 | *known_errorHandler = 2; |
| 8424 | else if (!strcmp(errors, "ignore")) |
| 8425 | *known_errorHandler = 3; |
| 8426 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 8427 | *known_errorHandler = 4; |
| 8428 | else |
| 8429 | *known_errorHandler = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8430 | } |
| 8431 | switch (*known_errorHandler) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8432 | case 1: /* strict */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8433 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8434 | return -1; |
| 8435 | case 2: /* replace */ |
| 8436 | for (collpos = collstartpos; collpos<collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8437 | x = charmapencode_output('?', mapping, res, respos); |
| 8438 | if (x==enc_EXCEPTION) { |
| 8439 | return -1; |
| 8440 | } |
| 8441 | else if (x==enc_FAILED) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8442 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8443 | return -1; |
| 8444 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8445 | } |
| 8446 | /* fall through */ |
| 8447 | case 3: /* ignore */ |
| 8448 | *inpos = collendpos; |
| 8449 | break; |
| 8450 | case 4: /* xmlcharrefreplace */ |
| 8451 | /* generate replacement (temporarily (mis)uses p) */ |
| 8452 | for (collpos = collstartpos; collpos < collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8453 | char buffer[2+29+1+1]; |
| 8454 | char *cp; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8455 | sprintf(buffer, "&#%d;", (int)PyUnicode_READ_CHAR(unicode, collpos)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8456 | for (cp = buffer; *cp; ++cp) { |
| 8457 | x = charmapencode_output(*cp, mapping, res, respos); |
| 8458 | if (x==enc_EXCEPTION) |
| 8459 | return -1; |
| 8460 | else if (x==enc_FAILED) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8461 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8462 | return -1; |
| 8463 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8464 | } |
| 8465 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8466 | *inpos = collendpos; |
| 8467 | break; |
| 8468 | default: |
| 8469 | repunicode = unicode_encode_call_errorhandler(errors, errorHandler, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8470 | encoding, reason, unicode, exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8471 | collstartpos, collendpos, &newpos); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8472 | if (repunicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8473 | return -1; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8474 | if (PyBytes_Check(repunicode)) { |
| 8475 | /* Directly copy bytes result to output. */ |
| 8476 | Py_ssize_t outsize = PyBytes_Size(*res); |
| 8477 | Py_ssize_t requiredsize; |
| 8478 | repsize = PyBytes_Size(repunicode); |
| 8479 | requiredsize = *respos + repsize; |
| 8480 | if (requiredsize > outsize) |
| 8481 | /* Make room for all additional bytes. */ |
| 8482 | if (charmapencode_resize(res, respos, requiredsize)) { |
| 8483 | Py_DECREF(repunicode); |
| 8484 | return -1; |
| 8485 | } |
| 8486 | memcpy(PyBytes_AsString(*res) + *respos, |
| 8487 | PyBytes_AsString(repunicode), repsize); |
| 8488 | *respos += repsize; |
| 8489 | *inpos = newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 8490 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8491 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 8492 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8493 | /* generate replacement */ |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 8494 | if (PyUnicode_READY(repunicode) == -1) { |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8495 | Py_DECREF(repunicode); |
| 8496 | return -1; |
| 8497 | } |
Victor Stinner | 9e30aa5 | 2011-11-21 02:49:52 +0100 | [diff] [blame] | 8498 | repsize = PyUnicode_GET_LENGTH(repunicode); |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8499 | data = PyUnicode_DATA(repunicode); |
| 8500 | kind = PyUnicode_KIND(repunicode); |
| 8501 | for (index = 0; index < repsize; index++) { |
| 8502 | Py_UCS4 repch = PyUnicode_READ(kind, data, index); |
| 8503 | x = charmapencode_output(repch, mapping, res, respos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8504 | if (x==enc_EXCEPTION) { |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8505 | Py_DECREF(repunicode); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8506 | return -1; |
| 8507 | } |
| 8508 | else if (x==enc_FAILED) { |
| 8509 | Py_DECREF(repunicode); |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8510 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8511 | return -1; |
| 8512 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8513 | } |
| 8514 | *inpos = newpos; |
| 8515 | Py_DECREF(repunicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8516 | } |
| 8517 | return 0; |
| 8518 | } |
| 8519 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8520 | PyObject * |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8521 | _PyUnicode_EncodeCharmap(PyObject *unicode, |
| 8522 | PyObject *mapping, |
| 8523 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8524 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8525 | /* output object */ |
| 8526 | PyObject *res = NULL; |
| 8527 | /* current input position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8528 | Py_ssize_t inpos = 0; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8529 | Py_ssize_t size; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8530 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8531 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8532 | PyObject *errorHandler = NULL; |
| 8533 | PyObject *exc = NULL; |
| 8534 | /* the following variable is used for caching string comparisons |
| 8535 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 8536 | * 3=ignore, 4=xmlcharrefreplace */ |
| 8537 | int known_errorHandler = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8538 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 8539 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8540 | return NULL; |
| 8541 | size = PyUnicode_GET_LENGTH(unicode); |
| 8542 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8543 | /* Default to Latin-1 */ |
| 8544 | if (mapping == NULL) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8545 | return unicode_encode_ucs1(unicode, errors, 256); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8546 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8547 | /* allocate enough for a simple encoding without |
| 8548 | replacements, if we need more, we'll resize */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8549 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8550 | if (res == NULL) |
| 8551 | goto onError; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 8552 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8553 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8554 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8555 | while (inpos<size) { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8556 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, inpos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8557 | /* try to encode it */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8558 | charmapencode_result x = charmapencode_output(ch, mapping, &res, &respos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8559 | if (x==enc_EXCEPTION) /* error */ |
| 8560 | goto onError; |
| 8561 | if (x==enc_FAILED) { /* unencodable character */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8562 | if (charmap_encoding_error(unicode, &inpos, mapping, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8563 | &exc, |
| 8564 | &known_errorHandler, &errorHandler, errors, |
| 8565 | &res, &respos)) { |
| 8566 | goto onError; |
| 8567 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8568 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8569 | else |
| 8570 | /* done with this character => adjust input position */ |
| 8571 | ++inpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8572 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8573 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8574 | /* Resize if we allocated to much */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8575 | if (respos<PyBytes_GET_SIZE(res)) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 8576 | if (_PyBytes_Resize(&res, respos) < 0) |
| 8577 | goto onError; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 8578 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8579 | Py_XDECREF(exc); |
| 8580 | Py_XDECREF(errorHandler); |
| 8581 | return res; |
| 8582 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8583 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8584 | Py_XDECREF(res); |
| 8585 | Py_XDECREF(exc); |
| 8586 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8587 | return NULL; |
| 8588 | } |
| 8589 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8590 | /* Deprecated */ |
| 8591 | PyObject * |
| 8592 | PyUnicode_EncodeCharmap(const Py_UNICODE *p, |
| 8593 | Py_ssize_t size, |
| 8594 | PyObject *mapping, |
| 8595 | const char *errors) |
| 8596 | { |
| 8597 | PyObject *result; |
| 8598 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 8599 | if (unicode == NULL) |
| 8600 | return NULL; |
| 8601 | result = _PyUnicode_EncodeCharmap(unicode, mapping, errors); |
| 8602 | Py_DECREF(unicode); |
Victor Stinner | fc026c9 | 2011-11-04 00:24:51 +0100 | [diff] [blame] | 8603 | return result; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8604 | } |
| 8605 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8606 | PyObject * |
| 8607 | PyUnicode_AsCharmapString(PyObject *unicode, |
| 8608 | PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8609 | { |
| 8610 | if (!PyUnicode_Check(unicode) || mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8611 | PyErr_BadArgument(); |
| 8612 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8613 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8614 | return _PyUnicode_EncodeCharmap(unicode, mapping, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8615 | } |
| 8616 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8617 | /* create or adjust a UnicodeTranslateError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8618 | static void |
| 8619 | make_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8620 | PyObject *unicode, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8621 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 8622 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8623 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8624 | if (*exceptionObject == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8625 | *exceptionObject = _PyUnicodeTranslateError_Create( |
| 8626 | unicode, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8627 | } |
| 8628 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8629 | if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
| 8630 | goto onError; |
| 8631 | if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) |
| 8632 | goto onError; |
| 8633 | if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) |
| 8634 | goto onError; |
| 8635 | return; |
| 8636 | onError: |
| 8637 | Py_DECREF(*exceptionObject); |
| 8638 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8639 | } |
| 8640 | } |
| 8641 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8642 | /* raises a UnicodeTranslateError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8643 | static void |
| 8644 | raise_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8645 | PyObject *unicode, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8646 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 8647 | const char *reason) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8648 | { |
| 8649 | make_translate_exception(exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8650 | unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8651 | if (*exceptionObject != NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8652 | PyCodec_StrictErrors(*exceptionObject); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8653 | } |
| 8654 | |
| 8655 | /* error handling callback helper: |
| 8656 | build arguments, call the callback and check the arguments, |
| 8657 | put the result into newpos and return the replacement string, which |
| 8658 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8659 | static PyObject * |
| 8660 | unicode_translate_call_errorhandler(const char *errors, |
| 8661 | PyObject **errorHandler, |
| 8662 | const char *reason, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8663 | PyObject *unicode, PyObject **exceptionObject, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8664 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 8665 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8666 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 8667 | 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] | 8668 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8669 | Py_ssize_t i_newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8670 | PyObject *restuple; |
| 8671 | PyObject *resunicode; |
| 8672 | |
| 8673 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8674 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8675 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8676 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8677 | } |
| 8678 | |
| 8679 | make_translate_exception(exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8680 | unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8681 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8682 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8683 | |
| 8684 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8685 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8686 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8687 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8688 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 8689 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8690 | Py_DECREF(restuple); |
| 8691 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8692 | } |
| 8693 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8694 | &resunicode, &i_newpos)) { |
| 8695 | Py_DECREF(restuple); |
| 8696 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8697 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8698 | if (i_newpos<0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8699 | *newpos = PyUnicode_GET_LENGTH(unicode)+i_newpos; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8700 | else |
| 8701 | *newpos = i_newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8702 | if (*newpos<0 || *newpos>PyUnicode_GET_LENGTH(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8703 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 8704 | Py_DECREF(restuple); |
| 8705 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 8706 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8707 | Py_INCREF(resunicode); |
| 8708 | Py_DECREF(restuple); |
| 8709 | return resunicode; |
| 8710 | } |
| 8711 | |
| 8712 | /* Lookup the character ch in the mapping and put the result in result, |
| 8713 | which must be decrefed by the caller. |
| 8714 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8715 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8716 | charmaptranslate_lookup(Py_UCS4 c, PyObject *mapping, PyObject **result) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8717 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8718 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8719 | PyObject *x; |
| 8720 | |
| 8721 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8722 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8723 | x = PyObject_GetItem(mapping, w); |
| 8724 | Py_DECREF(w); |
| 8725 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8726 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 8727 | /* No mapping found means: use 1:1 mapping. */ |
| 8728 | PyErr_Clear(); |
| 8729 | *result = NULL; |
| 8730 | return 0; |
| 8731 | } else |
| 8732 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8733 | } |
| 8734 | else if (x == Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8735 | *result = x; |
| 8736 | return 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8737 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8738 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8739 | long value = PyLong_AS_LONG(x); |
| 8740 | long max = PyUnicode_GetMax(); |
| 8741 | if (value < 0 || value > max) { |
| 8742 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5a2f7e60 | 2007-10-24 21:13:09 +0000 | [diff] [blame] | 8743 | "character mapping must be in range(0x%x)", max+1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8744 | Py_DECREF(x); |
| 8745 | return -1; |
| 8746 | } |
| 8747 | *result = x; |
| 8748 | return 0; |
| 8749 | } |
| 8750 | else if (PyUnicode_Check(x)) { |
| 8751 | *result = x; |
| 8752 | return 0; |
| 8753 | } |
| 8754 | else { |
| 8755 | /* wrong return value */ |
| 8756 | PyErr_SetString(PyExc_TypeError, |
| 8757 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8758 | Py_DECREF(x); |
| 8759 | return -1; |
| 8760 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8761 | } |
| 8762 | /* ensure that *outobj is at least requiredsize characters long, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8763 | if not reallocate and adjust various state variables. |
| 8764 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8765 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8766 | charmaptranslate_makespace(Py_UCS4 **outobj, Py_ssize_t *psize, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8767 | Py_ssize_t requiredsize) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8768 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8769 | Py_ssize_t oldsize = *psize; |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 8770 | if (requiredsize > oldsize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8771 | /* exponentially overallocate to minimize reallocations */ |
| 8772 | if (requiredsize < 2 * oldsize) |
| 8773 | requiredsize = 2 * oldsize; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8774 | *outobj = PyMem_Realloc(*outobj, requiredsize * sizeof(Py_UCS4)); |
| 8775 | if (*outobj == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8776 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8777 | *psize = requiredsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8778 | } |
| 8779 | return 0; |
| 8780 | } |
| 8781 | /* lookup the character, put the result in the output string and adjust |
| 8782 | various state variables. Return a new reference to the object that |
| 8783 | was put in the output buffer in *result, or Py_None, if the mapping was |
| 8784 | undefined (in which case no character was written). |
| 8785 | The called must decref result. |
| 8786 | Return 0 on success, -1 on error. */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8787 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8788 | charmaptranslate_output(PyObject *input, Py_ssize_t ipos, |
| 8789 | PyObject *mapping, Py_UCS4 **output, |
| 8790 | Py_ssize_t *osize, Py_ssize_t *opos, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8791 | PyObject **res) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8792 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8793 | Py_UCS4 curinp = PyUnicode_READ_CHAR(input, ipos); |
| 8794 | if (charmaptranslate_lookup(curinp, mapping, res)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8795 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8796 | if (*res==NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8797 | /* not found => default to 1:1 mapping */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8798 | (*output)[(*opos)++] = curinp; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8799 | } |
| 8800 | else if (*res==Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8801 | ; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8802 | else if (PyLong_Check(*res)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8803 | /* 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] | 8804 | (*output)[(*opos)++] = (Py_UCS4)PyLong_AS_LONG(*res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8805 | } |
| 8806 | else if (PyUnicode_Check(*res)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8807 | Py_ssize_t repsize; |
| 8808 | if (PyUnicode_READY(*res) == -1) |
| 8809 | return -1; |
| 8810 | repsize = PyUnicode_GET_LENGTH(*res); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8811 | if (repsize==1) { |
| 8812 | /* 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] | 8813 | (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8814 | } |
| 8815 | else if (repsize!=0) { |
| 8816 | /* more than one character */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8817 | Py_ssize_t requiredsize = *opos + |
| 8818 | (PyUnicode_GET_LENGTH(input) - ipos) + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8819 | repsize - 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8820 | Py_ssize_t i; |
| 8821 | if (charmaptranslate_makespace(output, osize, requiredsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8822 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8823 | for(i = 0; i < repsize; i++) |
| 8824 | (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, i); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8825 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8826 | } |
| 8827 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8828 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8829 | return 0; |
| 8830 | } |
| 8831 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8832 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8833 | _PyUnicode_TranslateCharmap(PyObject *input, |
| 8834 | PyObject *mapping, |
| 8835 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8836 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8837 | /* input object */ |
| 8838 | char *idata; |
| 8839 | Py_ssize_t size, i; |
| 8840 | int kind; |
| 8841 | /* output buffer */ |
| 8842 | Py_UCS4 *output = NULL; |
| 8843 | Py_ssize_t osize; |
| 8844 | PyObject *res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8845 | /* current output position */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8846 | Py_ssize_t opos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8847 | char *reason = "character maps to <undefined>"; |
| 8848 | PyObject *errorHandler = NULL; |
| 8849 | PyObject *exc = NULL; |
| 8850 | /* the following variable is used for caching string comparisons |
| 8851 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 8852 | * 3=ignore, 4=xmlcharrefreplace */ |
| 8853 | int known_errorHandler = -1; |
| 8854 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8855 | if (mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8856 | PyErr_BadArgument(); |
| 8857 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8858 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8859 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8860 | if (PyUnicode_READY(input) == -1) |
| 8861 | return NULL; |
| 8862 | idata = (char*)PyUnicode_DATA(input); |
| 8863 | kind = PyUnicode_KIND(input); |
| 8864 | size = PyUnicode_GET_LENGTH(input); |
| 8865 | i = 0; |
| 8866 | |
| 8867 | if (size == 0) { |
| 8868 | Py_INCREF(input); |
| 8869 | return input; |
| 8870 | } |
| 8871 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8872 | /* allocate enough for a simple 1:1 translation without |
| 8873 | replacements, if we need more, we'll resize */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8874 | osize = size; |
| 8875 | output = PyMem_Malloc(osize * sizeof(Py_UCS4)); |
| 8876 | opos = 0; |
| 8877 | if (output == NULL) { |
| 8878 | PyErr_NoMemory(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8879 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8880 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8881 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8882 | while (i<size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8883 | /* try to encode it */ |
| 8884 | PyObject *x = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8885 | if (charmaptranslate_output(input, i, mapping, |
| 8886 | &output, &osize, &opos, &x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8887 | Py_XDECREF(x); |
| 8888 | goto onError; |
| 8889 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8890 | Py_XDECREF(x); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8891 | if (x!=Py_None) /* it worked => adjust input pointer */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8892 | ++i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8893 | else { /* untranslatable character */ |
| 8894 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
| 8895 | Py_ssize_t repsize; |
| 8896 | Py_ssize_t newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8897 | Py_ssize_t uni2; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8898 | /* startpos for collecting untranslatable chars */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8899 | Py_ssize_t collstart = i; |
| 8900 | Py_ssize_t collend = i+1; |
| 8901 | Py_ssize_t coll; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8902 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8903 | /* find all untranslatable characters */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8904 | while (collend < size) { |
| 8905 | if (charmaptranslate_lookup(PyUnicode_READ(kind,idata, collend), mapping, &x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8906 | goto onError; |
| 8907 | Py_XDECREF(x); |
| 8908 | if (x!=Py_None) |
| 8909 | break; |
| 8910 | ++collend; |
| 8911 | } |
| 8912 | /* cache callback name lookup |
| 8913 | * (if not done yet, i.e. it's the first error) */ |
| 8914 | if (known_errorHandler==-1) { |
| 8915 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 8916 | known_errorHandler = 1; |
| 8917 | else if (!strcmp(errors, "replace")) |
| 8918 | known_errorHandler = 2; |
| 8919 | else if (!strcmp(errors, "ignore")) |
| 8920 | known_errorHandler = 3; |
| 8921 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 8922 | known_errorHandler = 4; |
| 8923 | else |
| 8924 | known_errorHandler = 0; |
| 8925 | } |
| 8926 | switch (known_errorHandler) { |
| 8927 | case 1: /* strict */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8928 | raise_translate_exception(&exc, input, collstart, |
| 8929 | collend, reason); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8930 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8931 | case 2: /* replace */ |
| 8932 | /* 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] | 8933 | for (coll = collstart; coll<collend; coll++) |
| 8934 | output[opos++] = '?'; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8935 | /* fall through */ |
| 8936 | case 3: /* ignore */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8937 | i = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8938 | break; |
| 8939 | case 4: /* xmlcharrefreplace */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8940 | /* generate replacement (temporarily (mis)uses i) */ |
| 8941 | for (i = collstart; i < collend; ++i) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8942 | char buffer[2+29+1+1]; |
| 8943 | char *cp; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8944 | sprintf(buffer, "&#%d;", PyUnicode_READ(kind, idata, i)); |
| 8945 | if (charmaptranslate_makespace(&output, &osize, |
| 8946 | opos+strlen(buffer)+(size-collend))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8947 | goto onError; |
| 8948 | for (cp = buffer; *cp; ++cp) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8949 | output[opos++] = *cp; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8950 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8951 | i = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8952 | break; |
| 8953 | default: |
| 8954 | repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8955 | reason, input, &exc, |
| 8956 | collstart, collend, &newpos); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 8957 | if (repunicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8958 | goto onError; |
Benjamin Peterson | 9ca3ffa | 2012-01-01 16:04:29 -0600 | [diff] [blame] | 8959 | if (PyUnicode_READY(repunicode) == -1) { |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 8960 | Py_DECREF(repunicode); |
| 8961 | goto onError; |
| 8962 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8963 | /* generate replacement */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8964 | repsize = PyUnicode_GET_LENGTH(repunicode); |
| 8965 | if (charmaptranslate_makespace(&output, &osize, |
| 8966 | opos+repsize+(size-collend))) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8967 | Py_DECREF(repunicode); |
| 8968 | goto onError; |
| 8969 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8970 | for (uni2 = 0; repsize-->0; ++uni2) |
| 8971 | output[opos++] = PyUnicode_READ_CHAR(repunicode, uni2); |
| 8972 | i = newpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8973 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8974 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8975 | } |
| 8976 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8977 | res = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, output, opos); |
| 8978 | if (!res) |
| 8979 | goto onError; |
| 8980 | PyMem_Free(output); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8981 | Py_XDECREF(exc); |
| 8982 | Py_XDECREF(errorHandler); |
| 8983 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8984 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8985 | onError: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8986 | PyMem_Free(output); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8987 | Py_XDECREF(exc); |
| 8988 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8989 | return NULL; |
| 8990 | } |
| 8991 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8992 | /* Deprecated. Use PyUnicode_Translate instead. */ |
| 8993 | PyObject * |
| 8994 | PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
| 8995 | Py_ssize_t size, |
| 8996 | PyObject *mapping, |
| 8997 | const char *errors) |
| 8998 | { |
| 8999 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 9000 | if (!unicode) |
| 9001 | return NULL; |
| 9002 | return _PyUnicode_TranslateCharmap(unicode, mapping, errors); |
| 9003 | } |
| 9004 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9005 | PyObject * |
| 9006 | PyUnicode_Translate(PyObject *str, |
| 9007 | PyObject *mapping, |
| 9008 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9009 | { |
| 9010 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9011 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9012 | str = PyUnicode_FromObject(str); |
| 9013 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9014 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9015 | result = _PyUnicode_TranslateCharmap(str, mapping, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9016 | Py_DECREF(str); |
| 9017 | return result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9018 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9019 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9020 | Py_XDECREF(str); |
| 9021 | return NULL; |
| 9022 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9023 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9024 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9025 | fix_decimal_and_space_to_ascii(PyObject *self) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9026 | { |
| 9027 | /* No need to call PyUnicode_READY(self) because this function is only |
| 9028 | called as a callback from fixup() which does it already. */ |
| 9029 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 9030 | const int kind = PyUnicode_KIND(self); |
| 9031 | void *data = PyUnicode_DATA(self); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9032 | Py_UCS4 maxchar = 127, ch, fixed; |
Benjamin Peterson | 821e4cf | 2012-01-12 15:40:18 -0500 | [diff] [blame] | 9033 | int modified = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9034 | Py_ssize_t i; |
| 9035 | |
| 9036 | for (i = 0; i < len; ++i) { |
| 9037 | ch = PyUnicode_READ(kind, data, i); |
| 9038 | fixed = 0; |
| 9039 | if (ch > 127) { |
| 9040 | if (Py_UNICODE_ISSPACE(ch)) |
| 9041 | fixed = ' '; |
| 9042 | else { |
| 9043 | const int decimal = Py_UNICODE_TODECIMAL(ch); |
| 9044 | if (decimal >= 0) |
| 9045 | fixed = '0' + decimal; |
| 9046 | } |
| 9047 | if (fixed != 0) { |
Benjamin Peterson | 821e4cf | 2012-01-12 15:40:18 -0500 | [diff] [blame] | 9048 | modified = 1; |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9049 | maxchar = MAX_MAXCHAR(maxchar, fixed); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9050 | PyUnicode_WRITE(kind, data, i, fixed); |
| 9051 | } |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9052 | else |
| 9053 | maxchar = MAX_MAXCHAR(maxchar, ch); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9054 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9055 | } |
| 9056 | |
Benjamin Peterson | 821e4cf | 2012-01-12 15:40:18 -0500 | [diff] [blame] | 9057 | return (modified) ? maxchar : 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9058 | } |
| 9059 | |
| 9060 | PyObject * |
| 9061 | _PyUnicode_TransformDecimalAndSpaceToASCII(PyObject *unicode) |
| 9062 | { |
| 9063 | if (!PyUnicode_Check(unicode)) { |
| 9064 | PyErr_BadInternalCall(); |
| 9065 | return NULL; |
| 9066 | } |
| 9067 | if (PyUnicode_READY(unicode) == -1) |
| 9068 | return NULL; |
| 9069 | if (PyUnicode_MAX_CHAR_VALUE(unicode) <= 127) { |
| 9070 | /* If the string is already ASCII, just return the same string */ |
| 9071 | Py_INCREF(unicode); |
| 9072 | return unicode; |
| 9073 | } |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9074 | return fixup(unicode, fix_decimal_and_space_to_ascii); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9075 | } |
| 9076 | |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 9077 | PyObject * |
| 9078 | PyUnicode_TransformDecimalToASCII(Py_UNICODE *s, |
| 9079 | Py_ssize_t length) |
| 9080 | { |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 9081 | PyObject *decimal; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 9082 | Py_ssize_t i; |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 9083 | Py_UCS4 maxchar; |
| 9084 | enum PyUnicode_Kind kind; |
| 9085 | void *data; |
| 9086 | |
Victor Stinner | 99d7ad0 | 2012-02-22 13:37:39 +0100 | [diff] [blame] | 9087 | maxchar = 127; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 9088 | for (i = 0; i < length; i++) { |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 9089 | Py_UNICODE ch = s[i]; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 9090 | if (ch > 127) { |
| 9091 | int decimal = Py_UNICODE_TODECIMAL(ch); |
| 9092 | if (decimal >= 0) |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 9093 | ch = '0' + decimal; |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9094 | maxchar = MAX_MAXCHAR(maxchar, ch); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 9095 | } |
| 9096 | } |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 9097 | |
| 9098 | /* Copy to a new string */ |
| 9099 | decimal = PyUnicode_New(length, maxchar); |
| 9100 | if (decimal == NULL) |
| 9101 | return decimal; |
| 9102 | kind = PyUnicode_KIND(decimal); |
| 9103 | data = PyUnicode_DATA(decimal); |
| 9104 | /* Iterate over code points */ |
| 9105 | for (i = 0; i < length; i++) { |
| 9106 | Py_UNICODE ch = s[i]; |
| 9107 | if (ch > 127) { |
| 9108 | int decimal = Py_UNICODE_TODECIMAL(ch); |
| 9109 | if (decimal >= 0) |
| 9110 | ch = '0' + decimal; |
| 9111 | } |
| 9112 | PyUnicode_WRITE(kind, data, i, ch); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9113 | } |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 9114 | return unicode_result(decimal); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 9115 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 9116 | /* --- Decimal Encoder ---------------------------------------------------- */ |
| 9117 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9118 | int |
| 9119 | PyUnicode_EncodeDecimal(Py_UNICODE *s, |
| 9120 | Py_ssize_t length, |
| 9121 | char *output, |
| 9122 | const char *errors) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 9123 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 9124 | PyObject *unicode; |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 9125 | Py_ssize_t i; |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 9126 | enum PyUnicode_Kind kind; |
| 9127 | void *data; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 9128 | |
| 9129 | if (output == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9130 | PyErr_BadArgument(); |
| 9131 | return -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 9132 | } |
| 9133 | |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 9134 | unicode = PyUnicode_FromUnicode(s, length); |
| 9135 | if (unicode == NULL) |
| 9136 | return -1; |
| 9137 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 9138 | if (PyUnicode_READY(unicode) == -1) { |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 9139 | Py_DECREF(unicode); |
| 9140 | return -1; |
| 9141 | } |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 9142 | kind = PyUnicode_KIND(unicode); |
| 9143 | data = PyUnicode_DATA(unicode); |
| 9144 | |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 9145 | for (i=0; i < length; ) { |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 9146 | PyObject *exc; |
| 9147 | Py_UCS4 ch; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9148 | int decimal; |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 9149 | Py_ssize_t startpos; |
| 9150 | |
| 9151 | ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9152 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9153 | if (Py_UNICODE_ISSPACE(ch)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9154 | *output++ = ' '; |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 9155 | i++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9156 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9157 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9158 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 9159 | if (decimal >= 0) { |
| 9160 | *output++ = '0' + decimal; |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 9161 | i++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9162 | continue; |
| 9163 | } |
| 9164 | if (0 < ch && ch < 256) { |
| 9165 | *output++ = (char)ch; |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 9166 | i++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9167 | continue; |
| 9168 | } |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 9169 | |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 9170 | startpos = i; |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 9171 | exc = NULL; |
| 9172 | raise_encode_exception(&exc, "decimal", unicode, |
| 9173 | startpos, startpos+1, |
| 9174 | "invalid decimal Unicode string"); |
| 9175 | Py_XDECREF(exc); |
| 9176 | Py_DECREF(unicode); |
| 9177 | return -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 9178 | } |
| 9179 | /* 0-terminate the output string */ |
| 9180 | *output++ = '\0'; |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 9181 | Py_DECREF(unicode); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 9182 | return 0; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 9183 | } |
| 9184 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9185 | /* --- Helpers ------------------------------------------------------------ */ |
| 9186 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9187 | static Py_ssize_t |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 9188 | any_find_slice(int direction, PyObject* s1, PyObject* s2, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9189 | Py_ssize_t start, |
| 9190 | Py_ssize_t end) |
| 9191 | { |
| 9192 | int kind1, kind2, kind; |
| 9193 | void *buf1, *buf2; |
| 9194 | Py_ssize_t len1, len2, result; |
| 9195 | |
| 9196 | kind1 = PyUnicode_KIND(s1); |
| 9197 | kind2 = PyUnicode_KIND(s2); |
| 9198 | kind = kind1 > kind2 ? kind1 : kind2; |
| 9199 | buf1 = PyUnicode_DATA(s1); |
| 9200 | buf2 = PyUnicode_DATA(s2); |
| 9201 | if (kind1 != kind) |
| 9202 | buf1 = _PyUnicode_AsKind(s1, kind); |
| 9203 | if (!buf1) |
| 9204 | return -2; |
| 9205 | if (kind2 != kind) |
| 9206 | buf2 = _PyUnicode_AsKind(s2, kind); |
| 9207 | if (!buf2) { |
| 9208 | if (kind1 != kind) PyMem_Free(buf1); |
| 9209 | return -2; |
| 9210 | } |
| 9211 | len1 = PyUnicode_GET_LENGTH(s1); |
| 9212 | len2 = PyUnicode_GET_LENGTH(s2); |
| 9213 | |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 9214 | if (direction > 0) { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9215 | switch (kind) { |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 9216 | case PyUnicode_1BYTE_KIND: |
| 9217 | if (PyUnicode_IS_ASCII(s1) && PyUnicode_IS_ASCII(s2)) |
| 9218 | result = asciilib_find_slice(buf1, len1, buf2, len2, start, end); |
| 9219 | else |
| 9220 | result = ucs1lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 9221 | break; |
| 9222 | case PyUnicode_2BYTE_KIND: |
| 9223 | result = ucs2lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 9224 | break; |
| 9225 | case PyUnicode_4BYTE_KIND: |
| 9226 | result = ucs4lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 9227 | break; |
| 9228 | default: |
| 9229 | assert(0); result = -2; |
| 9230 | } |
| 9231 | } |
| 9232 | else { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9233 | switch (kind) { |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 9234 | case PyUnicode_1BYTE_KIND: |
| 9235 | if (PyUnicode_IS_ASCII(s1) && PyUnicode_IS_ASCII(s2)) |
| 9236 | result = asciilib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 9237 | else |
| 9238 | result = ucs1lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 9239 | break; |
| 9240 | case PyUnicode_2BYTE_KIND: |
| 9241 | result = ucs2lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 9242 | break; |
| 9243 | case PyUnicode_4BYTE_KIND: |
| 9244 | result = ucs4lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 9245 | break; |
| 9246 | default: |
| 9247 | assert(0); result = -2; |
| 9248 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9249 | } |
| 9250 | |
| 9251 | if (kind1 != kind) |
| 9252 | PyMem_Free(buf1); |
| 9253 | if (kind2 != kind) |
| 9254 | PyMem_Free(buf2); |
| 9255 | |
| 9256 | return result; |
| 9257 | } |
| 9258 | |
| 9259 | Py_ssize_t |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9260 | _PyUnicode_InsertThousandsGrouping( |
| 9261 | PyObject *unicode, Py_ssize_t index, |
| 9262 | Py_ssize_t n_buffer, |
| 9263 | void *digits, Py_ssize_t n_digits, |
| 9264 | Py_ssize_t min_width, |
| 9265 | const char *grouping, PyObject *thousands_sep, |
| 9266 | Py_UCS4 *maxchar) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9267 | { |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9268 | unsigned int kind, thousands_sep_kind; |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9269 | char *data, *thousands_sep_data; |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9270 | Py_ssize_t thousands_sep_len; |
| 9271 | Py_ssize_t len; |
| 9272 | |
| 9273 | if (unicode != NULL) { |
| 9274 | kind = PyUnicode_KIND(unicode); |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9275 | data = (char *) PyUnicode_DATA(unicode) + index * kind; |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9276 | } |
| 9277 | else { |
| 9278 | kind = PyUnicode_1BYTE_KIND; |
| 9279 | data = NULL; |
| 9280 | } |
| 9281 | thousands_sep_kind = PyUnicode_KIND(thousands_sep); |
| 9282 | thousands_sep_data = PyUnicode_DATA(thousands_sep); |
| 9283 | thousands_sep_len = PyUnicode_GET_LENGTH(thousands_sep); |
| 9284 | if (unicode != NULL && thousands_sep_kind != kind) { |
Victor Stinner | 90f50d4 | 2012-02-24 01:44:47 +0100 | [diff] [blame] | 9285 | if (thousands_sep_kind < kind) { |
| 9286 | thousands_sep_data = _PyUnicode_AsKind(thousands_sep, kind); |
| 9287 | if (!thousands_sep_data) |
| 9288 | return -1; |
| 9289 | } |
| 9290 | else { |
| 9291 | data = _PyUnicode_AsKind(unicode, thousands_sep_kind); |
| 9292 | if (!data) |
| 9293 | return -1; |
| 9294 | } |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9295 | } |
| 9296 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9297 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9298 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9299 | if (unicode != NULL && PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9300 | len = asciilib_InsertThousandsGrouping( |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9301 | (Py_UCS1 *) data, n_buffer, (Py_UCS1 *) digits, n_digits, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9302 | min_width, grouping, |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9303 | (Py_UCS1 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9304 | else |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9305 | len = ucs1lib_InsertThousandsGrouping( |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9306 | (Py_UCS1*)data, n_buffer, (Py_UCS1*)digits, n_digits, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9307 | min_width, grouping, |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9308 | (Py_UCS1 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9309 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9310 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9311 | len = ucs2lib_InsertThousandsGrouping( |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9312 | (Py_UCS2 *) data, n_buffer, (Py_UCS2 *) digits, n_digits, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9313 | min_width, grouping, |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9314 | (Py_UCS2 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9315 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9316 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9317 | len = ucs4lib_InsertThousandsGrouping( |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9318 | (Py_UCS4 *) data, n_buffer, (Py_UCS4 *) digits, n_digits, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9319 | min_width, grouping, |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9320 | (Py_UCS4 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9321 | break; |
| 9322 | default: |
| 9323 | assert(0); |
| 9324 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9325 | } |
Victor Stinner | 90f50d4 | 2012-02-24 01:44:47 +0100 | [diff] [blame] | 9326 | if (unicode != NULL && thousands_sep_kind != kind) { |
| 9327 | if (thousands_sep_kind < kind) |
| 9328 | PyMem_Free(thousands_sep_data); |
| 9329 | else |
| 9330 | PyMem_Free(data); |
| 9331 | } |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9332 | if (unicode == NULL) { |
| 9333 | *maxchar = 127; |
| 9334 | if (len != n_digits) { |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9335 | *maxchar = MAX_MAXCHAR(*maxchar, |
| 9336 | PyUnicode_MAX_CHAR_VALUE(thousands_sep)); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9337 | } |
| 9338 | } |
| 9339 | return len; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9340 | } |
| 9341 | |
| 9342 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9343 | /* helper macro to fixup start/end slice values */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9344 | #define ADJUST_INDICES(start, end, len) \ |
| 9345 | if (end > len) \ |
| 9346 | end = len; \ |
| 9347 | else if (end < 0) { \ |
| 9348 | end += len; \ |
| 9349 | if (end < 0) \ |
| 9350 | end = 0; \ |
| 9351 | } \ |
| 9352 | if (start < 0) { \ |
| 9353 | start += len; \ |
| 9354 | if (start < 0) \ |
| 9355 | start = 0; \ |
| 9356 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9357 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9358 | Py_ssize_t |
| 9359 | PyUnicode_Count(PyObject *str, |
| 9360 | PyObject *substr, |
| 9361 | Py_ssize_t start, |
| 9362 | Py_ssize_t end) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9363 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9364 | Py_ssize_t result; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9365 | PyObject* str_obj; |
| 9366 | PyObject* sub_obj; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9367 | int kind1, kind2, kind; |
| 9368 | void *buf1 = NULL, *buf2 = NULL; |
| 9369 | Py_ssize_t len1, len2; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9370 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9371 | str_obj = PyUnicode_FromObject(str); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9372 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9373 | return -1; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9374 | sub_obj = PyUnicode_FromObject(substr); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9375 | if (!sub_obj) { |
| 9376 | Py_DECREF(str_obj); |
| 9377 | return -1; |
| 9378 | } |
Benjamin Peterson | 4c13a4a | 2012-01-02 09:07:38 -0600 | [diff] [blame] | 9379 | if (PyUnicode_READY(sub_obj) == -1 || PyUnicode_READY(str_obj) == -1) { |
Benjamin Peterson | 5e458f5 | 2012-01-02 10:12:13 -0600 | [diff] [blame] | 9380 | Py_DECREF(sub_obj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9381 | Py_DECREF(str_obj); |
| 9382 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9383 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9384 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9385 | kind1 = PyUnicode_KIND(str_obj); |
| 9386 | kind2 = PyUnicode_KIND(sub_obj); |
| 9387 | kind = kind1 > kind2 ? kind1 : kind2; |
| 9388 | buf1 = PyUnicode_DATA(str_obj); |
| 9389 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9390 | buf1 = _PyUnicode_AsKind(str_obj, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9391 | if (!buf1) |
| 9392 | goto onError; |
| 9393 | buf2 = PyUnicode_DATA(sub_obj); |
| 9394 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9395 | buf2 = _PyUnicode_AsKind(sub_obj, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9396 | if (!buf2) |
| 9397 | goto onError; |
| 9398 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 9399 | len2 = PyUnicode_GET_LENGTH(sub_obj); |
| 9400 | |
| 9401 | ADJUST_INDICES(start, end, len1); |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9402 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9403 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9404 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sub_obj)) |
| 9405 | result = asciilib_count( |
| 9406 | ((Py_UCS1*)buf1) + start, end - start, |
| 9407 | buf2, len2, PY_SSIZE_T_MAX |
| 9408 | ); |
| 9409 | else |
| 9410 | result = ucs1lib_count( |
| 9411 | ((Py_UCS1*)buf1) + start, end - start, |
| 9412 | buf2, len2, PY_SSIZE_T_MAX |
| 9413 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9414 | break; |
| 9415 | case PyUnicode_2BYTE_KIND: |
| 9416 | result = ucs2lib_count( |
| 9417 | ((Py_UCS2*)buf1) + start, end - start, |
| 9418 | buf2, len2, PY_SSIZE_T_MAX |
| 9419 | ); |
| 9420 | break; |
| 9421 | case PyUnicode_4BYTE_KIND: |
| 9422 | result = ucs4lib_count( |
| 9423 | ((Py_UCS4*)buf1) + start, end - start, |
| 9424 | buf2, len2, PY_SSIZE_T_MAX |
| 9425 | ); |
| 9426 | break; |
| 9427 | default: |
| 9428 | assert(0); result = 0; |
| 9429 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9430 | |
| 9431 | Py_DECREF(sub_obj); |
| 9432 | Py_DECREF(str_obj); |
| 9433 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9434 | if (kind1 != kind) |
| 9435 | PyMem_Free(buf1); |
| 9436 | if (kind2 != kind) |
| 9437 | PyMem_Free(buf2); |
| 9438 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9439 | return result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9440 | onError: |
| 9441 | Py_DECREF(sub_obj); |
| 9442 | Py_DECREF(str_obj); |
| 9443 | if (kind1 != kind && buf1) |
| 9444 | PyMem_Free(buf1); |
| 9445 | if (kind2 != kind && buf2) |
| 9446 | PyMem_Free(buf2); |
| 9447 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9448 | } |
| 9449 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9450 | Py_ssize_t |
| 9451 | PyUnicode_Find(PyObject *str, |
| 9452 | PyObject *sub, |
| 9453 | Py_ssize_t start, |
| 9454 | Py_ssize_t end, |
| 9455 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9456 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9457 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9458 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9459 | str = PyUnicode_FromObject(str); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9460 | if (!str) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9461 | return -2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9462 | sub = PyUnicode_FromObject(sub); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9463 | if (!sub) { |
| 9464 | Py_DECREF(str); |
| 9465 | return -2; |
| 9466 | } |
| 9467 | if (PyUnicode_READY(sub) == -1 || PyUnicode_READY(str) == -1) { |
| 9468 | Py_DECREF(sub); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9469 | Py_DECREF(str); |
| 9470 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9471 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9472 | |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 9473 | result = any_find_slice(direction, |
| 9474 | str, sub, start, end |
| 9475 | ); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9476 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9477 | Py_DECREF(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9478 | Py_DECREF(sub); |
| 9479 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9480 | return result; |
| 9481 | } |
| 9482 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9483 | Py_ssize_t |
| 9484 | PyUnicode_FindChar(PyObject *str, Py_UCS4 ch, |
| 9485 | Py_ssize_t start, Py_ssize_t end, |
| 9486 | int direction) |
| 9487 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9488 | int kind; |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 9489 | Py_ssize_t result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9490 | if (PyUnicode_READY(str) == -1) |
| 9491 | return -2; |
Victor Stinner | 267aa24 | 2011-10-02 01:08:37 +0200 | [diff] [blame] | 9492 | if (start < 0 || end < 0) { |
| 9493 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 9494 | return -2; |
| 9495 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9496 | if (end > PyUnicode_GET_LENGTH(str)) |
| 9497 | end = PyUnicode_GET_LENGTH(str); |
| 9498 | kind = PyUnicode_KIND(str); |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 9499 | result = findchar(PyUnicode_1BYTE_DATA(str) + kind*start, |
| 9500 | kind, end-start, ch, direction); |
| 9501 | if (result == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9502 | return -1; |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 9503 | else |
| 9504 | return start + result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9505 | } |
| 9506 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9507 | static int |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9508 | tailmatch(PyObject *self, |
| 9509 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9510 | Py_ssize_t start, |
| 9511 | Py_ssize_t end, |
| 9512 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9513 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9514 | int kind_self; |
| 9515 | int kind_sub; |
| 9516 | void *data_self; |
| 9517 | void *data_sub; |
| 9518 | Py_ssize_t offset; |
| 9519 | Py_ssize_t i; |
| 9520 | Py_ssize_t end_sub; |
| 9521 | |
| 9522 | if (PyUnicode_READY(self) == -1 || |
| 9523 | PyUnicode_READY(substring) == -1) |
| 9524 | return 0; |
| 9525 | |
| 9526 | if (PyUnicode_GET_LENGTH(substring) == 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9527 | return 1; |
| 9528 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9529 | ADJUST_INDICES(start, end, PyUnicode_GET_LENGTH(self)); |
| 9530 | end -= PyUnicode_GET_LENGTH(substring); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9531 | if (end < start) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9532 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9533 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9534 | kind_self = PyUnicode_KIND(self); |
| 9535 | data_self = PyUnicode_DATA(self); |
| 9536 | kind_sub = PyUnicode_KIND(substring); |
| 9537 | data_sub = PyUnicode_DATA(substring); |
| 9538 | end_sub = PyUnicode_GET_LENGTH(substring) - 1; |
| 9539 | |
| 9540 | if (direction > 0) |
| 9541 | offset = end; |
| 9542 | else |
| 9543 | offset = start; |
| 9544 | |
| 9545 | if (PyUnicode_READ(kind_self, data_self, offset) == |
| 9546 | PyUnicode_READ(kind_sub, data_sub, 0) && |
| 9547 | PyUnicode_READ(kind_self, data_self, offset + end_sub) == |
| 9548 | PyUnicode_READ(kind_sub, data_sub, end_sub)) { |
| 9549 | /* If both are of the same kind, memcmp is sufficient */ |
| 9550 | if (kind_self == kind_sub) { |
| 9551 | return ! memcmp((char *)data_self + |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9552 | (offset * PyUnicode_KIND(substring)), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9553 | data_sub, |
| 9554 | PyUnicode_GET_LENGTH(substring) * |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9555 | PyUnicode_KIND(substring)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9556 | } |
| 9557 | /* otherwise we have to compare each character by first accesing it */ |
| 9558 | else { |
| 9559 | /* We do not need to compare 0 and len(substring)-1 because |
| 9560 | the if statement above ensured already that they are equal |
| 9561 | when we end up here. */ |
| 9562 | // TODO: honor direction and do a forward or backwards search |
| 9563 | for (i = 1; i < end_sub; ++i) { |
| 9564 | if (PyUnicode_READ(kind_self, data_self, offset + i) != |
| 9565 | PyUnicode_READ(kind_sub, data_sub, i)) |
| 9566 | return 0; |
| 9567 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9568 | return 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9569 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9570 | } |
| 9571 | |
| 9572 | return 0; |
| 9573 | } |
| 9574 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9575 | Py_ssize_t |
| 9576 | PyUnicode_Tailmatch(PyObject *str, |
| 9577 | PyObject *substr, |
| 9578 | Py_ssize_t start, |
| 9579 | Py_ssize_t end, |
| 9580 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9581 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9582 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9583 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9584 | str = PyUnicode_FromObject(str); |
| 9585 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9586 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9587 | substr = PyUnicode_FromObject(substr); |
| 9588 | if (substr == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9589 | Py_DECREF(str); |
| 9590 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9591 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9592 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9593 | result = tailmatch(str, substr, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9594 | start, end, direction); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9595 | Py_DECREF(str); |
| 9596 | Py_DECREF(substr); |
| 9597 | return result; |
| 9598 | } |
| 9599 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9600 | /* Apply fixfct filter to the Unicode object self and return a |
| 9601 | reference to the modified object */ |
| 9602 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9603 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9604 | fixup(PyObject *self, |
| 9605 | Py_UCS4 (*fixfct)(PyObject *s)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9606 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9607 | PyObject *u; |
| 9608 | Py_UCS4 maxchar_old, maxchar_new = 0; |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9609 | PyObject *v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9610 | |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 9611 | u = _PyUnicode_Copy(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9612 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9613 | return NULL; |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 9614 | maxchar_old = PyUnicode_MAX_CHAR_VALUE(u); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9615 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9616 | /* fix functions return the new maximum character in a string, |
| 9617 | if the kind of the resulting unicode object does not change, |
| 9618 | everything is fine. Otherwise we need to change the string kind |
| 9619 | and re-run the fix function. */ |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9620 | maxchar_new = fixfct(u); |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9621 | |
| 9622 | if (maxchar_new == 0) { |
| 9623 | /* no changes */; |
| 9624 | if (PyUnicode_CheckExact(self)) { |
| 9625 | Py_DECREF(u); |
| 9626 | Py_INCREF(self); |
| 9627 | return self; |
| 9628 | } |
| 9629 | else |
| 9630 | return u; |
| 9631 | } |
| 9632 | |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9633 | maxchar_new = align_maxchar(maxchar_new); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9634 | |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9635 | if (maxchar_new == maxchar_old) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9636 | return u; |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9637 | |
| 9638 | /* In case the maximum character changed, we need to |
| 9639 | convert the string to the new category. */ |
| 9640 | v = PyUnicode_New(PyUnicode_GET_LENGTH(self), maxchar_new); |
| 9641 | if (v == NULL) { |
| 9642 | Py_DECREF(u); |
| 9643 | return NULL; |
| 9644 | } |
| 9645 | if (maxchar_new > maxchar_old) { |
| 9646 | /* If the maxchar increased so that the kind changed, not all |
| 9647 | characters are representable anymore and we need to fix the |
| 9648 | string again. This only happens in very few cases. */ |
| 9649 | copy_characters(v, 0, self, 0, PyUnicode_GET_LENGTH(self)); |
| 9650 | maxchar_old = fixfct(v); |
| 9651 | assert(maxchar_old > 0 && maxchar_old <= maxchar_new); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9652 | } |
| 9653 | else { |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9654 | copy_characters(v, 0, u, 0, PyUnicode_GET_LENGTH(self)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9655 | } |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9656 | Py_DECREF(u); |
| 9657 | assert(_PyUnicode_CheckConsistency(v, 1)); |
| 9658 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9659 | } |
| 9660 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9661 | static PyObject * |
| 9662 | ascii_upper_or_lower(PyObject *self, int lower) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9663 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9664 | Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 9665 | char *resdata, *data = PyUnicode_DATA(self); |
| 9666 | PyObject *res; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9667 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9668 | res = PyUnicode_New(len, 127); |
| 9669 | if (res == NULL) |
| 9670 | return NULL; |
| 9671 | resdata = PyUnicode_DATA(res); |
| 9672 | if (lower) |
| 9673 | _Py_bytes_lower(resdata, data, len); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9674 | else |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9675 | _Py_bytes_upper(resdata, data, len); |
| 9676 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9677 | } |
| 9678 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9679 | static Py_UCS4 |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9680 | 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] | 9681 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9682 | Py_ssize_t j; |
| 9683 | int final_sigma; |
| 9684 | Py_UCS4 c; |
| 9685 | /* 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] | 9686 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9687 | \p{cased}\p{case-ignorable}*U+03A3!(\p{case-ignorable}*\p{cased}) |
| 9688 | |
| 9689 | where ! is a negation and \p{xxx} is a character with property xxx. |
| 9690 | */ |
| 9691 | for (j = i - 1; j >= 0; j--) { |
| 9692 | c = PyUnicode_READ(kind, data, j); |
| 9693 | if (!_PyUnicode_IsCaseIgnorable(c)) |
| 9694 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9695 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9696 | final_sigma = j >= 0 && _PyUnicode_IsCased(c); |
| 9697 | if (final_sigma) { |
| 9698 | for (j = i + 1; j < length; j++) { |
| 9699 | c = PyUnicode_READ(kind, data, j); |
| 9700 | if (!_PyUnicode_IsCaseIgnorable(c)) |
| 9701 | break; |
| 9702 | } |
| 9703 | final_sigma = j == length || !_PyUnicode_IsCased(c); |
| 9704 | } |
| 9705 | return (final_sigma) ? 0x3C2 : 0x3C3; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9706 | } |
| 9707 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9708 | static int |
| 9709 | lower_ucs4(int kind, void *data, Py_ssize_t length, Py_ssize_t i, |
| 9710 | Py_UCS4 c, Py_UCS4 *mapped) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9711 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9712 | /* Obscure special case. */ |
| 9713 | if (c == 0x3A3) { |
| 9714 | mapped[0] = handle_capital_sigma(kind, data, length, i); |
| 9715 | return 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9716 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9717 | return _PyUnicode_ToLowerFull(c, mapped); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9718 | } |
| 9719 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9720 | static Py_ssize_t |
| 9721 | 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] | 9722 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9723 | Py_ssize_t i, k = 0; |
| 9724 | int n_res, j; |
| 9725 | Py_UCS4 c, mapped[3]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9726 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9727 | c = PyUnicode_READ(kind, data, 0); |
| 9728 | n_res = _PyUnicode_ToUpperFull(c, mapped); |
| 9729 | for (j = 0; j < n_res; j++) { |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9730 | *maxchar = MAX_MAXCHAR(*maxchar, mapped[j]); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9731 | res[k++] = mapped[j]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9732 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9733 | for (i = 1; i < length; i++) { |
| 9734 | c = PyUnicode_READ(kind, data, i); |
| 9735 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9736 | for (j = 0; j < n_res; j++) { |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9737 | *maxchar = MAX_MAXCHAR(*maxchar, mapped[j]); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9738 | res[k++] = mapped[j]; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9739 | } |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9740 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9741 | return k; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9742 | } |
| 9743 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9744 | static Py_ssize_t |
| 9745 | do_swapcase(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) { |
| 9746 | Py_ssize_t i, k = 0; |
| 9747 | |
| 9748 | for (i = 0; i < length; i++) { |
| 9749 | Py_UCS4 c = PyUnicode_READ(kind, data, i), mapped[3]; |
| 9750 | int n_res, j; |
| 9751 | if (Py_UNICODE_ISUPPER(c)) { |
| 9752 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9753 | } |
| 9754 | else if (Py_UNICODE_ISLOWER(c)) { |
| 9755 | n_res = _PyUnicode_ToUpperFull(c, mapped); |
| 9756 | } |
| 9757 | else { |
| 9758 | n_res = 1; |
| 9759 | mapped[0] = c; |
| 9760 | } |
| 9761 | for (j = 0; j < n_res; j++) { |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9762 | *maxchar = MAX_MAXCHAR(*maxchar, mapped[j]); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9763 | res[k++] = mapped[j]; |
| 9764 | } |
| 9765 | } |
| 9766 | return k; |
| 9767 | } |
| 9768 | |
| 9769 | static Py_ssize_t |
| 9770 | do_upper_or_lower(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, |
| 9771 | Py_UCS4 *maxchar, int lower) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9772 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9773 | Py_ssize_t i, k = 0; |
| 9774 | |
| 9775 | for (i = 0; i < length; i++) { |
| 9776 | Py_UCS4 c = PyUnicode_READ(kind, data, i), mapped[3]; |
| 9777 | int n_res, j; |
| 9778 | if (lower) |
| 9779 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9780 | else |
| 9781 | n_res = _PyUnicode_ToUpperFull(c, mapped); |
| 9782 | for (j = 0; j < n_res; j++) { |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9783 | *maxchar = MAX_MAXCHAR(*maxchar, mapped[j]); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9784 | res[k++] = mapped[j]; |
| 9785 | } |
| 9786 | } |
| 9787 | return k; |
| 9788 | } |
| 9789 | |
| 9790 | static Py_ssize_t |
| 9791 | do_upper(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9792 | { |
| 9793 | return do_upper_or_lower(kind, data, length, res, maxchar, 0); |
| 9794 | } |
| 9795 | |
| 9796 | static Py_ssize_t |
| 9797 | do_lower(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9798 | { |
| 9799 | return do_upper_or_lower(kind, data, length, res, maxchar, 1); |
| 9800 | } |
| 9801 | |
Benjamin Peterson | e51757f | 2012-01-12 21:10:29 -0500 | [diff] [blame] | 9802 | static Py_ssize_t |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 9803 | do_casefold(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9804 | { |
| 9805 | Py_ssize_t i, k = 0; |
| 9806 | |
| 9807 | for (i = 0; i < length; i++) { |
| 9808 | Py_UCS4 c = PyUnicode_READ(kind, data, i); |
| 9809 | Py_UCS4 mapped[3]; |
| 9810 | int j, n_res = _PyUnicode_ToFoldedFull(c, mapped); |
| 9811 | for (j = 0; j < n_res; j++) { |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9812 | *maxchar = MAX_MAXCHAR(*maxchar, mapped[j]); |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 9813 | res[k++] = mapped[j]; |
| 9814 | } |
| 9815 | } |
| 9816 | return k; |
| 9817 | } |
| 9818 | |
| 9819 | static Py_ssize_t |
Benjamin Peterson | e51757f | 2012-01-12 21:10:29 -0500 | [diff] [blame] | 9820 | do_title(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9821 | { |
| 9822 | Py_ssize_t i, k = 0; |
| 9823 | int previous_is_cased; |
| 9824 | |
| 9825 | previous_is_cased = 0; |
| 9826 | for (i = 0; i < length; i++) { |
| 9827 | const Py_UCS4 c = PyUnicode_READ(kind, data, i); |
| 9828 | Py_UCS4 mapped[3]; |
| 9829 | int n_res, j; |
| 9830 | |
| 9831 | if (previous_is_cased) |
| 9832 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9833 | else |
| 9834 | n_res = _PyUnicode_ToTitleFull(c, mapped); |
| 9835 | |
| 9836 | for (j = 0; j < n_res; j++) { |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9837 | *maxchar = MAX_MAXCHAR(*maxchar, mapped[j]); |
Benjamin Peterson | e51757f | 2012-01-12 21:10:29 -0500 | [diff] [blame] | 9838 | res[k++] = mapped[j]; |
| 9839 | } |
| 9840 | |
| 9841 | previous_is_cased = _PyUnicode_IsCased(c); |
| 9842 | } |
| 9843 | return k; |
| 9844 | } |
| 9845 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9846 | static PyObject * |
| 9847 | case_operation(PyObject *self, |
| 9848 | Py_ssize_t (*perform)(int, void *, Py_ssize_t, Py_UCS4 *, Py_UCS4 *)) |
| 9849 | { |
| 9850 | PyObject *res = NULL; |
| 9851 | Py_ssize_t length, newlength = 0; |
| 9852 | int kind, outkind; |
| 9853 | void *data, *outdata; |
| 9854 | Py_UCS4 maxchar = 0, *tmp, *tmpend; |
| 9855 | |
Benjamin Peterson | eea4846 | 2012-01-16 14:28:50 -0500 | [diff] [blame] | 9856 | assert(PyUnicode_IS_READY(self)); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9857 | |
| 9858 | kind = PyUnicode_KIND(self); |
| 9859 | data = PyUnicode_DATA(self); |
| 9860 | length = PyUnicode_GET_LENGTH(self); |
| 9861 | tmp = PyMem_MALLOC(sizeof(Py_UCS4) * 3 * length); |
| 9862 | if (tmp == NULL) |
| 9863 | return PyErr_NoMemory(); |
| 9864 | newlength = perform(kind, data, length, tmp, &maxchar); |
| 9865 | res = PyUnicode_New(newlength, maxchar); |
| 9866 | if (res == NULL) |
| 9867 | goto leave; |
| 9868 | tmpend = tmp + newlength; |
| 9869 | outdata = PyUnicode_DATA(res); |
| 9870 | outkind = PyUnicode_KIND(res); |
| 9871 | switch (outkind) { |
| 9872 | case PyUnicode_1BYTE_KIND: |
| 9873 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS1, tmp, tmpend, outdata); |
| 9874 | break; |
| 9875 | case PyUnicode_2BYTE_KIND: |
| 9876 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS2, tmp, tmpend, outdata); |
| 9877 | break; |
| 9878 | case PyUnicode_4BYTE_KIND: |
| 9879 | memcpy(outdata, tmp, sizeof(Py_UCS4) * newlength); |
| 9880 | break; |
| 9881 | default: |
| 9882 | assert(0); |
| 9883 | break; |
| 9884 | } |
| 9885 | leave: |
| 9886 | PyMem_FREE(tmp); |
| 9887 | return res; |
| 9888 | } |
| 9889 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9890 | PyObject * |
| 9891 | PyUnicode_Join(PyObject *separator, PyObject *seq) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9892 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9893 | PyObject *sep = NULL; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9894 | Py_ssize_t seplen; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9895 | PyObject *res = NULL; /* the result */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9896 | PyObject *fseq; /* PySequence_Fast(seq) */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9897 | Py_ssize_t seqlen; /* len(fseq) -- number of items in sequence */ |
| 9898 | PyObject **items; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9899 | PyObject *item; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9900 | Py_ssize_t sz, i, res_offset; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9901 | Py_UCS4 maxchar; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9902 | Py_UCS4 item_maxchar; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9903 | int use_memcpy; |
| 9904 | unsigned char *res_data = NULL, *sep_data = NULL; |
| 9905 | PyObject *last_obj; |
| 9906 | unsigned int kind = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9907 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9908 | fseq = PySequence_Fast(seq, ""); |
| 9909 | if (fseq == NULL) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9910 | return NULL; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9911 | } |
| 9912 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9913 | /* NOTE: the following code can't call back into Python code, |
| 9914 | * so we are sure that fseq won't be mutated. |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 9915 | */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9916 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9917 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 9918 | /* If empty sequence, return u"". */ |
| 9919 | if (seqlen == 0) { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9920 | Py_DECREF(fseq); |
| 9921 | Py_INCREF(unicode_empty); |
| 9922 | res = unicode_empty; |
| 9923 | return res; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9924 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9925 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9926 | /* If singleton sequence with an exact Unicode, return that. */ |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9927 | last_obj = NULL; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9928 | items = PySequence_Fast_ITEMS(fseq); |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9929 | if (seqlen == 1) { |
| 9930 | if (PyUnicode_CheckExact(items[0])) { |
| 9931 | res = items[0]; |
| 9932 | Py_INCREF(res); |
| 9933 | Py_DECREF(fseq); |
| 9934 | return res; |
| 9935 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9936 | seplen = 0; |
Victor Stinner | c6f0df7 | 2011-10-06 15:58:54 +0200 | [diff] [blame] | 9937 | maxchar = 0; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9938 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9939 | else { |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9940 | /* Set up sep and seplen */ |
| 9941 | if (separator == NULL) { |
| 9942 | /* fall back to a blank space separator */ |
| 9943 | sep = PyUnicode_FromOrdinal(' '); |
| 9944 | if (!sep) |
| 9945 | goto onError; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9946 | seplen = 1; |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9947 | maxchar = 32; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9948 | } |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9949 | else { |
| 9950 | if (!PyUnicode_Check(separator)) { |
| 9951 | PyErr_Format(PyExc_TypeError, |
| 9952 | "separator: expected str instance," |
| 9953 | " %.80s found", |
| 9954 | Py_TYPE(separator)->tp_name); |
| 9955 | goto onError; |
| 9956 | } |
| 9957 | if (PyUnicode_READY(separator)) |
| 9958 | goto onError; |
| 9959 | sep = separator; |
| 9960 | seplen = PyUnicode_GET_LENGTH(separator); |
| 9961 | maxchar = PyUnicode_MAX_CHAR_VALUE(separator); |
| 9962 | /* inc refcount to keep this code path symmetric with the |
| 9963 | above case of a blank separator */ |
| 9964 | Py_INCREF(sep); |
| 9965 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9966 | last_obj = sep; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9967 | } |
| 9968 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9969 | /* There are at least two things to join, or else we have a subclass |
| 9970 | * of str in the sequence. |
| 9971 | * Do a pre-pass to figure out the total amount of space we'll |
| 9972 | * need (sz), and see whether all argument are strings. |
| 9973 | */ |
| 9974 | sz = 0; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9975 | #ifdef Py_DEBUG |
| 9976 | use_memcpy = 0; |
| 9977 | #else |
| 9978 | use_memcpy = 1; |
| 9979 | #endif |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9980 | for (i = 0; i < seqlen; i++) { |
| 9981 | const Py_ssize_t old_sz = sz; |
| 9982 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9983 | if (!PyUnicode_Check(item)) { |
| 9984 | PyErr_Format(PyExc_TypeError, |
| 9985 | "sequence item %zd: expected str instance," |
| 9986 | " %.80s found", |
| 9987 | i, Py_TYPE(item)->tp_name); |
| 9988 | goto onError; |
| 9989 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9990 | if (PyUnicode_READY(item) == -1) |
| 9991 | goto onError; |
| 9992 | sz += PyUnicode_GET_LENGTH(item); |
| 9993 | item_maxchar = PyUnicode_MAX_CHAR_VALUE(item); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9994 | maxchar = MAX_MAXCHAR(maxchar, item_maxchar); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9995 | if (i != 0) |
| 9996 | sz += seplen; |
| 9997 | if (sz < old_sz || sz > PY_SSIZE_T_MAX) { |
| 9998 | PyErr_SetString(PyExc_OverflowError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9999 | "join() result is too long for a Python string"); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 10000 | goto onError; |
| 10001 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 10002 | if (use_memcpy && last_obj != NULL) { |
| 10003 | if (PyUnicode_KIND(last_obj) != PyUnicode_KIND(item)) |
| 10004 | use_memcpy = 0; |
| 10005 | } |
| 10006 | last_obj = item; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 10007 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 10008 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10009 | res = PyUnicode_New(sz, maxchar); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 10010 | if (res == NULL) |
| 10011 | goto onError; |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 10012 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 10013 | /* Catenate everything. */ |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 10014 | #ifdef Py_DEBUG |
| 10015 | use_memcpy = 0; |
| 10016 | #else |
| 10017 | if (use_memcpy) { |
| 10018 | res_data = PyUnicode_1BYTE_DATA(res); |
| 10019 | kind = PyUnicode_KIND(res); |
| 10020 | if (seplen != 0) |
| 10021 | sep_data = PyUnicode_1BYTE_DATA(sep); |
| 10022 | } |
| 10023 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10024 | for (i = 0, res_offset = 0; i < seqlen; ++i) { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 10025 | Py_ssize_t itemlen; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 10026 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10027 | /* Copy item, and maybe the separator. */ |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 10028 | if (i && seplen != 0) { |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 10029 | if (use_memcpy) { |
| 10030 | Py_MEMCPY(res_data, |
| 10031 | sep_data, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10032 | kind * seplen); |
| 10033 | res_data += kind * seplen; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 10034 | } |
| 10035 | else { |
| 10036 | copy_characters(res, res_offset, sep, 0, seplen); |
| 10037 | res_offset += seplen; |
| 10038 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10039 | } |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 10040 | itemlen = PyUnicode_GET_LENGTH(item); |
| 10041 | if (itemlen != 0) { |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 10042 | if (use_memcpy) { |
| 10043 | Py_MEMCPY(res_data, |
| 10044 | PyUnicode_DATA(item), |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10045 | kind * itemlen); |
| 10046 | res_data += kind * itemlen; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 10047 | } |
| 10048 | else { |
| 10049 | copy_characters(res, res_offset, item, 0, itemlen); |
| 10050 | res_offset += itemlen; |
| 10051 | } |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 10052 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 10053 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 10054 | if (use_memcpy) |
| 10055 | assert(res_data == PyUnicode_1BYTE_DATA(res) |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10056 | + kind * PyUnicode_GET_LENGTH(res)); |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 10057 | else |
| 10058 | assert(res_offset == PyUnicode_GET_LENGTH(res)); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 10059 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 10060 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10061 | Py_XDECREF(sep); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10062 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10063 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10064 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10065 | onError: |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 10066 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10067 | Py_XDECREF(sep); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 10068 | Py_XDECREF(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10069 | return NULL; |
| 10070 | } |
| 10071 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10072 | #define FILL(kind, data, value, start, length) \ |
| 10073 | do { \ |
| 10074 | Py_ssize_t i_ = 0; \ |
| 10075 | assert(kind != PyUnicode_WCHAR_KIND); \ |
| 10076 | switch ((kind)) { \ |
| 10077 | case PyUnicode_1BYTE_KIND: { \ |
| 10078 | unsigned char * to_ = (unsigned char *)((data)) + (start); \ |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 10079 | memset(to_, (unsigned char)value, (length)); \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10080 | break; \ |
| 10081 | } \ |
| 10082 | case PyUnicode_2BYTE_KIND: { \ |
| 10083 | Py_UCS2 * to_ = (Py_UCS2 *)((data)) + (start); \ |
| 10084 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 10085 | break; \ |
| 10086 | } \ |
Benjamin Peterson | e157cf1 | 2012-01-01 15:56:20 -0600 | [diff] [blame] | 10087 | case PyUnicode_4BYTE_KIND: { \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10088 | Py_UCS4 * to_ = (Py_UCS4 *)((data)) + (start); \ |
| 10089 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 10090 | break; \ |
Benjamin Peterson | e157cf1 | 2012-01-01 15:56:20 -0600 | [diff] [blame] | 10091 | default: assert(0); \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10092 | } \ |
| 10093 | } \ |
| 10094 | } while (0) |
| 10095 | |
Victor Stinner | 3fe5531 | 2012-01-04 00:33:50 +0100 | [diff] [blame] | 10096 | Py_ssize_t |
| 10097 | PyUnicode_Fill(PyObject *unicode, Py_ssize_t start, Py_ssize_t length, |
| 10098 | Py_UCS4 fill_char) |
| 10099 | { |
| 10100 | Py_ssize_t maxlen; |
| 10101 | enum PyUnicode_Kind kind; |
| 10102 | void *data; |
| 10103 | |
| 10104 | if (!PyUnicode_Check(unicode)) { |
| 10105 | PyErr_BadInternalCall(); |
| 10106 | return -1; |
| 10107 | } |
| 10108 | if (PyUnicode_READY(unicode) == -1) |
| 10109 | return -1; |
| 10110 | if (unicode_check_modifiable(unicode)) |
| 10111 | return -1; |
| 10112 | |
| 10113 | if (fill_char > PyUnicode_MAX_CHAR_VALUE(unicode)) { |
| 10114 | PyErr_SetString(PyExc_ValueError, |
| 10115 | "fill character is bigger than " |
| 10116 | "the string maximum character"); |
| 10117 | return -1; |
| 10118 | } |
| 10119 | |
| 10120 | maxlen = PyUnicode_GET_LENGTH(unicode) - start; |
| 10121 | length = Py_MIN(maxlen, length); |
| 10122 | if (length <= 0) |
| 10123 | return 0; |
| 10124 | |
| 10125 | kind = PyUnicode_KIND(unicode); |
| 10126 | data = PyUnicode_DATA(unicode); |
| 10127 | FILL(kind, data, fill_char, start, length); |
| 10128 | return length; |
| 10129 | } |
| 10130 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10131 | static PyObject * |
| 10132 | pad(PyObject *self, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10133 | Py_ssize_t left, |
| 10134 | Py_ssize_t right, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10135 | Py_UCS4 fill) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10136 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10137 | PyObject *u; |
| 10138 | Py_UCS4 maxchar; |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 10139 | int kind; |
| 10140 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10141 | |
| 10142 | if (left < 0) |
| 10143 | left = 0; |
| 10144 | if (right < 0) |
| 10145 | right = 0; |
| 10146 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10147 | if (left == 0 && right == 0) |
| 10148 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10149 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10150 | if (left > PY_SSIZE_T_MAX - _PyUnicode_LENGTH(self) || |
| 10151 | right > PY_SSIZE_T_MAX - (left + _PyUnicode_LENGTH(self))) { |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 10152 | PyErr_SetString(PyExc_OverflowError, "padded string is too long"); |
| 10153 | return NULL; |
| 10154 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10155 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 10156 | maxchar = MAX_MAXCHAR(maxchar, fill); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10157 | u = PyUnicode_New(left + _PyUnicode_LENGTH(self) + right, maxchar); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 10158 | if (!u) |
| 10159 | return NULL; |
| 10160 | |
| 10161 | kind = PyUnicode_KIND(u); |
| 10162 | data = PyUnicode_DATA(u); |
| 10163 | if (left) |
| 10164 | FILL(kind, data, fill, 0, left); |
| 10165 | if (right) |
| 10166 | FILL(kind, data, fill, left + _PyUnicode_LENGTH(self), right); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 10167 | copy_characters(u, left, self, 0, _PyUnicode_LENGTH(self)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10168 | assert(_PyUnicode_CheckConsistency(u, 1)); |
| 10169 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10170 | } |
| 10171 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10172 | PyObject * |
| 10173 | PyUnicode_Splitlines(PyObject *string, int keepends) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10174 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10175 | PyObject *list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10176 | |
| 10177 | string = PyUnicode_FromObject(string); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 10178 | if (string == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10179 | return NULL; |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 10180 | if (PyUnicode_READY(string) == -1) { |
| 10181 | Py_DECREF(string); |
| 10182 | return NULL; |
| 10183 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10184 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10185 | switch (PyUnicode_KIND(string)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10186 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10187 | if (PyUnicode_IS_ASCII(string)) |
| 10188 | list = asciilib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10189 | string, PyUnicode_1BYTE_DATA(string), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10190 | PyUnicode_GET_LENGTH(string), keepends); |
| 10191 | else |
| 10192 | list = ucs1lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10193 | string, PyUnicode_1BYTE_DATA(string), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10194 | PyUnicode_GET_LENGTH(string), keepends); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10195 | break; |
| 10196 | case PyUnicode_2BYTE_KIND: |
| 10197 | list = ucs2lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10198 | string, PyUnicode_2BYTE_DATA(string), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10199 | PyUnicode_GET_LENGTH(string), keepends); |
| 10200 | break; |
| 10201 | case PyUnicode_4BYTE_KIND: |
| 10202 | list = ucs4lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10203 | string, PyUnicode_4BYTE_DATA(string), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10204 | PyUnicode_GET_LENGTH(string), keepends); |
| 10205 | break; |
| 10206 | default: |
| 10207 | assert(0); |
| 10208 | list = 0; |
| 10209 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10210 | Py_DECREF(string); |
| 10211 | return list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10212 | } |
| 10213 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10214 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10215 | split(PyObject *self, |
| 10216 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10217 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10218 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10219 | int kind1, kind2, kind; |
| 10220 | void *buf1, *buf2; |
| 10221 | Py_ssize_t len1, len2; |
| 10222 | PyObject* out; |
| 10223 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10224 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10225 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10226 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10227 | if (PyUnicode_READY(self) == -1) |
| 10228 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10229 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10230 | if (substring == NULL) |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10231 | switch (PyUnicode_KIND(self)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10232 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10233 | if (PyUnicode_IS_ASCII(self)) |
| 10234 | return asciilib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10235 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10236 | PyUnicode_GET_LENGTH(self), maxcount |
| 10237 | ); |
| 10238 | else |
| 10239 | return ucs1lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10240 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10241 | PyUnicode_GET_LENGTH(self), maxcount |
| 10242 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10243 | case PyUnicode_2BYTE_KIND: |
| 10244 | return ucs2lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10245 | self, PyUnicode_2BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10246 | PyUnicode_GET_LENGTH(self), maxcount |
| 10247 | ); |
| 10248 | case PyUnicode_4BYTE_KIND: |
| 10249 | return ucs4lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10250 | self, PyUnicode_4BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10251 | PyUnicode_GET_LENGTH(self), maxcount |
| 10252 | ); |
| 10253 | default: |
| 10254 | assert(0); |
| 10255 | return NULL; |
| 10256 | } |
| 10257 | |
| 10258 | if (PyUnicode_READY(substring) == -1) |
| 10259 | return NULL; |
| 10260 | |
| 10261 | kind1 = PyUnicode_KIND(self); |
| 10262 | kind2 = PyUnicode_KIND(substring); |
| 10263 | kind = kind1 > kind2 ? kind1 : kind2; |
| 10264 | buf1 = PyUnicode_DATA(self); |
| 10265 | buf2 = PyUnicode_DATA(substring); |
| 10266 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10267 | buf1 = _PyUnicode_AsKind(self, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10268 | if (!buf1) |
| 10269 | return NULL; |
| 10270 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10271 | buf2 = _PyUnicode_AsKind(substring, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10272 | if (!buf2) { |
| 10273 | if (kind1 != kind) PyMem_Free(buf1); |
| 10274 | return NULL; |
| 10275 | } |
| 10276 | len1 = PyUnicode_GET_LENGTH(self); |
| 10277 | len2 = PyUnicode_GET_LENGTH(substring); |
| 10278 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10279 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10280 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10281 | if (PyUnicode_IS_ASCII(self) && PyUnicode_IS_ASCII(substring)) |
| 10282 | out = asciilib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10283 | self, buf1, len1, buf2, len2, maxcount); |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10284 | else |
| 10285 | out = ucs1lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10286 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10287 | break; |
| 10288 | case PyUnicode_2BYTE_KIND: |
| 10289 | out = ucs2lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10290 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10291 | break; |
| 10292 | case PyUnicode_4BYTE_KIND: |
| 10293 | out = ucs4lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10294 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10295 | break; |
| 10296 | default: |
| 10297 | out = NULL; |
| 10298 | } |
| 10299 | if (kind1 != kind) |
| 10300 | PyMem_Free(buf1); |
| 10301 | if (kind2 != kind) |
| 10302 | PyMem_Free(buf2); |
| 10303 | return out; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10304 | } |
| 10305 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10306 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10307 | rsplit(PyObject *self, |
| 10308 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10309 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10310 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10311 | int kind1, kind2, kind; |
| 10312 | void *buf1, *buf2; |
| 10313 | Py_ssize_t len1, len2; |
| 10314 | PyObject* out; |
| 10315 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10316 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10317 | maxcount = PY_SSIZE_T_MAX; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10318 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10319 | if (PyUnicode_READY(self) == -1) |
| 10320 | return NULL; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10321 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10322 | if (substring == NULL) |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10323 | switch (PyUnicode_KIND(self)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10324 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10325 | if (PyUnicode_IS_ASCII(self)) |
| 10326 | return asciilib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10327 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10328 | PyUnicode_GET_LENGTH(self), maxcount |
| 10329 | ); |
| 10330 | else |
| 10331 | return ucs1lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10332 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10333 | PyUnicode_GET_LENGTH(self), maxcount |
| 10334 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10335 | case PyUnicode_2BYTE_KIND: |
| 10336 | return ucs2lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10337 | self, PyUnicode_2BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10338 | PyUnicode_GET_LENGTH(self), maxcount |
| 10339 | ); |
| 10340 | case PyUnicode_4BYTE_KIND: |
| 10341 | return ucs4lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10342 | self, PyUnicode_4BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10343 | PyUnicode_GET_LENGTH(self), maxcount |
| 10344 | ); |
| 10345 | default: |
| 10346 | assert(0); |
| 10347 | return NULL; |
| 10348 | } |
| 10349 | |
| 10350 | if (PyUnicode_READY(substring) == -1) |
| 10351 | return NULL; |
| 10352 | |
| 10353 | kind1 = PyUnicode_KIND(self); |
| 10354 | kind2 = PyUnicode_KIND(substring); |
| 10355 | kind = kind1 > kind2 ? kind1 : kind2; |
| 10356 | buf1 = PyUnicode_DATA(self); |
| 10357 | buf2 = PyUnicode_DATA(substring); |
| 10358 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10359 | buf1 = _PyUnicode_AsKind(self, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10360 | if (!buf1) |
| 10361 | return NULL; |
| 10362 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10363 | buf2 = _PyUnicode_AsKind(substring, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10364 | if (!buf2) { |
| 10365 | if (kind1 != kind) PyMem_Free(buf1); |
| 10366 | return NULL; |
| 10367 | } |
| 10368 | len1 = PyUnicode_GET_LENGTH(self); |
| 10369 | len2 = PyUnicode_GET_LENGTH(substring); |
| 10370 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10371 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10372 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10373 | if (PyUnicode_IS_ASCII(self) && PyUnicode_IS_ASCII(substring)) |
| 10374 | out = asciilib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10375 | self, buf1, len1, buf2, len2, maxcount); |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10376 | else |
| 10377 | out = ucs1lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10378 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10379 | break; |
| 10380 | case PyUnicode_2BYTE_KIND: |
| 10381 | out = ucs2lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10382 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10383 | break; |
| 10384 | case PyUnicode_4BYTE_KIND: |
| 10385 | out = ucs4lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10386 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10387 | break; |
| 10388 | default: |
| 10389 | out = NULL; |
| 10390 | } |
| 10391 | if (kind1 != kind) |
| 10392 | PyMem_Free(buf1); |
| 10393 | if (kind2 != kind) |
| 10394 | PyMem_Free(buf2); |
| 10395 | return out; |
| 10396 | } |
| 10397 | |
| 10398 | static Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10399 | anylib_find(int kind, PyObject *str1, void *buf1, Py_ssize_t len1, |
| 10400 | 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] | 10401 | { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10402 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10403 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10404 | if (PyUnicode_IS_ASCII(str1) && PyUnicode_IS_ASCII(str2)) |
| 10405 | return asciilib_find(buf1, len1, buf2, len2, offset); |
| 10406 | else |
| 10407 | return ucs1lib_find(buf1, len1, buf2, len2, offset); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10408 | case PyUnicode_2BYTE_KIND: |
| 10409 | return ucs2lib_find(buf1, len1, buf2, len2, offset); |
| 10410 | case PyUnicode_4BYTE_KIND: |
| 10411 | return ucs4lib_find(buf1, len1, buf2, len2, offset); |
| 10412 | } |
| 10413 | assert(0); |
| 10414 | return -1; |
| 10415 | } |
| 10416 | |
| 10417 | static Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10418 | anylib_count(int kind, PyObject *sstr, void* sbuf, Py_ssize_t slen, |
| 10419 | 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] | 10420 | { |
Benjamin Peterson | c0b95d1 | 2011-12-20 17:24:05 -0600 | [diff] [blame] | 10421 | switch (kind) { |
| 10422 | case PyUnicode_1BYTE_KIND: |
| 10423 | if (PyUnicode_IS_ASCII(sstr) && PyUnicode_IS_ASCII(str1)) |
| 10424 | return asciilib_count(sbuf, slen, buf1, len1, maxcount); |
| 10425 | else |
| 10426 | return ucs1lib_count(sbuf, slen, buf1, len1, maxcount); |
| 10427 | case PyUnicode_2BYTE_KIND: |
| 10428 | return ucs2lib_count(sbuf, slen, buf1, len1, maxcount); |
| 10429 | case PyUnicode_4BYTE_KIND: |
| 10430 | return ucs4lib_count(sbuf, slen, buf1, len1, maxcount); |
| 10431 | } |
| 10432 | assert(0); |
| 10433 | return 0; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10434 | } |
| 10435 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10436 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10437 | replace(PyObject *self, PyObject *str1, |
| 10438 | PyObject *str2, Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10439 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10440 | PyObject *u; |
| 10441 | char *sbuf = PyUnicode_DATA(self); |
| 10442 | char *buf1 = PyUnicode_DATA(str1); |
| 10443 | char *buf2 = PyUnicode_DATA(str2); |
| 10444 | int srelease = 0, release1 = 0, release2 = 0; |
| 10445 | int skind = PyUnicode_KIND(self); |
| 10446 | int kind1 = PyUnicode_KIND(str1); |
| 10447 | int kind2 = PyUnicode_KIND(str2); |
| 10448 | Py_ssize_t slen = PyUnicode_GET_LENGTH(self); |
| 10449 | Py_ssize_t len1 = PyUnicode_GET_LENGTH(str1); |
| 10450 | Py_ssize_t len2 = PyUnicode_GET_LENGTH(str2); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10451 | int mayshrink; |
| 10452 | Py_UCS4 maxchar, maxchar_str2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10453 | |
| 10454 | if (maxcount < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10455 | maxcount = PY_SSIZE_T_MAX; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10456 | else if (maxcount == 0 || slen == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10457 | goto nothing; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10458 | |
Victor Stinner | 59de0ee | 2011-10-07 10:01:28 +0200 | [diff] [blame] | 10459 | if (str1 == str2) |
| 10460 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10461 | if (skind < kind1) |
| 10462 | /* substring too wide to be present */ |
| 10463 | goto nothing; |
| 10464 | |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10465 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
| 10466 | maxchar_str2 = PyUnicode_MAX_CHAR_VALUE(str2); |
| 10467 | /* Replacing str1 with str2 may cause a maxchar reduction in the |
| 10468 | result string. */ |
| 10469 | mayshrink = (maxchar_str2 < maxchar); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 10470 | maxchar = MAX_MAXCHAR(maxchar, maxchar_str2); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10471 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10472 | if (len1 == len2) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10473 | /* same length */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10474 | if (len1 == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10475 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10476 | if (len1 == 1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10477 | /* replace characters */ |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10478 | Py_UCS4 u1, u2; |
| 10479 | int rkind; |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10480 | Py_ssize_t index, pos; |
| 10481 | char *src; |
| 10482 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10483 | u1 = PyUnicode_READ_CHAR(str1, 0); |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10484 | pos = findchar(sbuf, PyUnicode_KIND(self), slen, u1, 1); |
| 10485 | if (pos < 0) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10486 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10487 | u2 = PyUnicode_READ_CHAR(str2, 0); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10488 | u = PyUnicode_New(slen, maxchar); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10489 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10490 | goto error; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 10491 | copy_characters(u, 0, self, 0, slen); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10492 | rkind = PyUnicode_KIND(u); |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10493 | |
| 10494 | PyUnicode_WRITE(rkind, PyUnicode_DATA(u), pos, u2); |
| 10495 | index = 0; |
| 10496 | src = sbuf; |
| 10497 | while (--maxcount) |
| 10498 | { |
| 10499 | pos++; |
| 10500 | src += pos * PyUnicode_KIND(self); |
| 10501 | slen -= pos; |
| 10502 | index += pos; |
| 10503 | pos = findchar(src, PyUnicode_KIND(self), slen, u1, 1); |
| 10504 | if (pos < 0) |
| 10505 | break; |
| 10506 | PyUnicode_WRITE(rkind, PyUnicode_DATA(u), index + pos, u2); |
| 10507 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10508 | } |
| 10509 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10510 | int rkind = skind; |
| 10511 | char *res; |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10512 | Py_ssize_t i; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10513 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10514 | if (kind1 < rkind) { |
| 10515 | /* widen substring */ |
| 10516 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10517 | if (!buf1) goto error; |
| 10518 | release1 = 1; |
| 10519 | } |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10520 | i = anylib_find(rkind, self, sbuf, slen, str1, buf1, len1, 0); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10521 | if (i < 0) |
| 10522 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10523 | if (rkind > kind2) { |
| 10524 | /* widen replacement */ |
| 10525 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 10526 | if (!buf2) goto error; |
| 10527 | release2 = 1; |
| 10528 | } |
| 10529 | else if (rkind < kind2) { |
| 10530 | /* widen self and buf1 */ |
| 10531 | rkind = kind2; |
| 10532 | if (release1) PyMem_Free(buf1); |
| 10533 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 10534 | if (!sbuf) goto error; |
| 10535 | srelease = 1; |
| 10536 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10537 | if (!buf1) goto error; |
| 10538 | release1 = 1; |
| 10539 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10540 | u = PyUnicode_New(slen, maxchar); |
| 10541 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10542 | goto error; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10543 | assert(PyUnicode_KIND(u) == rkind); |
| 10544 | res = PyUnicode_DATA(u); |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10545 | |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10546 | memcpy(res, sbuf, rkind * slen); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10547 | /* change everything in-place, starting with this one */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10548 | memcpy(res + rkind * i, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10549 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10550 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10551 | i += len1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10552 | |
| 10553 | while ( --maxcount > 0) { |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10554 | i = anylib_find(rkind, self, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10555 | sbuf+rkind*i, slen-i, |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10556 | str1, buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10557 | if (i == -1) |
| 10558 | break; |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10559 | memcpy(res + rkind * i, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10560 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10561 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10562 | i += len1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10563 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10564 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10565 | } |
| 10566 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10567 | Py_ssize_t n, i, j, ires; |
| 10568 | Py_ssize_t product, new_size; |
| 10569 | int rkind = skind; |
| 10570 | char *res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10571 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10572 | if (kind1 < rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10573 | /* widen substring */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10574 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10575 | if (!buf1) goto error; |
| 10576 | release1 = 1; |
| 10577 | } |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10578 | n = anylib_count(rkind, self, sbuf, slen, str1, buf1, len1, maxcount); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10579 | if (n == 0) |
| 10580 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10581 | if (kind2 < rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10582 | /* widen replacement */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10583 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 10584 | if (!buf2) goto error; |
| 10585 | release2 = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10586 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10587 | else if (kind2 > rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10588 | /* widen self and buf1 */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10589 | rkind = kind2; |
| 10590 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 10591 | if (!sbuf) goto error; |
| 10592 | srelease = 1; |
| 10593 | if (release1) PyMem_Free(buf1); |
| 10594 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10595 | if (!buf1) goto error; |
| 10596 | release1 = 1; |
| 10597 | } |
| 10598 | /* new_size = PyUnicode_GET_LENGTH(self) + n * (PyUnicode_GET_LENGTH(str2) - |
| 10599 | PyUnicode_GET_LENGTH(str1))); */ |
| 10600 | product = n * (len2-len1); |
| 10601 | if ((product / (len2-len1)) != n) { |
| 10602 | PyErr_SetString(PyExc_OverflowError, |
| 10603 | "replace string is too long"); |
| 10604 | goto error; |
| 10605 | } |
| 10606 | new_size = slen + product; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10607 | if (new_size == 0) { |
| 10608 | Py_INCREF(unicode_empty); |
| 10609 | u = unicode_empty; |
| 10610 | goto done; |
| 10611 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10612 | if (new_size < 0 || new_size > (PY_SSIZE_T_MAX >> (rkind-1))) { |
| 10613 | PyErr_SetString(PyExc_OverflowError, |
| 10614 | "replace string is too long"); |
| 10615 | goto error; |
| 10616 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10617 | u = PyUnicode_New(new_size, maxchar); |
| 10618 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10619 | goto error; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10620 | assert(PyUnicode_KIND(u) == rkind); |
| 10621 | res = PyUnicode_DATA(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10622 | ires = i = 0; |
| 10623 | if (len1 > 0) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10624 | while (n-- > 0) { |
| 10625 | /* look for next match */ |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10626 | j = anylib_find(rkind, self, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10627 | sbuf + rkind * i, slen-i, |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10628 | str1, buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10629 | if (j == -1) |
| 10630 | break; |
| 10631 | else if (j > i) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10632 | /* copy unchanged part [i:j] */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10633 | memcpy(res + rkind * ires, |
| 10634 | sbuf + rkind * i, |
| 10635 | rkind * (j-i)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10636 | ires += j - i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10637 | } |
| 10638 | /* copy substitution string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10639 | if (len2 > 0) { |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10640 | memcpy(res + rkind * ires, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10641 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10642 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10643 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10644 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10645 | i = j + len1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10646 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10647 | if (i < slen) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10648 | /* copy tail [i:] */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10649 | memcpy(res + rkind * ires, |
| 10650 | sbuf + rkind * i, |
| 10651 | rkind * (slen-i)); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10652 | } |
| 10653 | else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10654 | /* interleave */ |
| 10655 | while (n > 0) { |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10656 | memcpy(res + rkind * ires, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10657 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10658 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10659 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10660 | if (--n <= 0) |
| 10661 | break; |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10662 | memcpy(res + rkind * ires, |
| 10663 | sbuf + rkind * i, |
| 10664 | rkind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10665 | ires++; |
| 10666 | i++; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10667 | } |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10668 | memcpy(res + rkind * ires, |
| 10669 | sbuf + rkind * i, |
| 10670 | rkind * (slen-i)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10671 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10672 | } |
| 10673 | |
| 10674 | if (mayshrink) { |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10675 | unicode_adjust_maxchar(&u); |
| 10676 | if (u == NULL) |
| 10677 | goto error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10678 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10679 | |
| 10680 | done: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10681 | if (srelease) |
| 10682 | PyMem_FREE(sbuf); |
| 10683 | if (release1) |
| 10684 | PyMem_FREE(buf1); |
| 10685 | if (release2) |
| 10686 | PyMem_FREE(buf2); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10687 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10688 | return u; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10689 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10690 | nothing: |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10691 | /* nothing to replace; return original string (when possible) */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10692 | if (srelease) |
| 10693 | PyMem_FREE(sbuf); |
| 10694 | if (release1) |
| 10695 | PyMem_FREE(buf1); |
| 10696 | if (release2) |
| 10697 | PyMem_FREE(buf2); |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10698 | return unicode_result_unchanged(self); |
| 10699 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10700 | error: |
| 10701 | if (srelease && sbuf) |
| 10702 | PyMem_FREE(sbuf); |
| 10703 | if (release1 && buf1) |
| 10704 | PyMem_FREE(buf1); |
| 10705 | if (release2 && buf2) |
| 10706 | PyMem_FREE(buf2); |
| 10707 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10708 | } |
| 10709 | |
| 10710 | /* --- Unicode Object Methods --------------------------------------------- */ |
| 10711 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10712 | PyDoc_STRVAR(title__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10713 | "S.title() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10714 | \n\ |
| 10715 | 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] | 10716 | characters, all remaining cased characters have lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10717 | |
| 10718 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10719 | unicode_title(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10720 | { |
Benjamin Peterson | eea4846 | 2012-01-16 14:28:50 -0500 | [diff] [blame] | 10721 | if (PyUnicode_READY(self) == -1) |
| 10722 | return NULL; |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 10723 | return case_operation(self, do_title); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10724 | } |
| 10725 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10726 | PyDoc_STRVAR(capitalize__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10727 | "S.capitalize() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10728 | \n\ |
| 10729 | 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] | 10730 | have upper case and the rest lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10731 | |
| 10732 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10733 | unicode_capitalize(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10734 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 10735 | if (PyUnicode_READY(self) == -1) |
| 10736 | return NULL; |
| 10737 | if (PyUnicode_GET_LENGTH(self) == 0) |
| 10738 | return unicode_result_unchanged(self); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 10739 | return case_operation(self, do_capitalize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10740 | } |
| 10741 | |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 10742 | PyDoc_STRVAR(casefold__doc__, |
| 10743 | "S.casefold() -> str\n\ |
| 10744 | \n\ |
| 10745 | Return a version of S suitable for caseless comparisons."); |
| 10746 | |
| 10747 | static PyObject * |
| 10748 | unicode_casefold(PyObject *self) |
| 10749 | { |
| 10750 | if (PyUnicode_READY(self) == -1) |
| 10751 | return NULL; |
| 10752 | if (PyUnicode_IS_ASCII(self)) |
| 10753 | return ascii_upper_or_lower(self, 1); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 10754 | return case_operation(self, do_casefold); |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 10755 | } |
| 10756 | |
| 10757 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10758 | /* Argument converter. Coerces to a single unicode character */ |
| 10759 | |
| 10760 | static int |
| 10761 | convert_uc(PyObject *obj, void *addr) |
| 10762 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10763 | Py_UCS4 *fillcharloc = (Py_UCS4 *)addr; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10764 | PyObject *uniobj; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10765 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10766 | uniobj = PyUnicode_FromObject(obj); |
| 10767 | if (uniobj == NULL) { |
| 10768 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10769 | "The fill character cannot be converted to Unicode"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10770 | return 0; |
| 10771 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10772 | if (PyUnicode_GET_LENGTH(uniobj) != 1) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10773 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10774 | "The fill character must be exactly one character long"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10775 | Py_DECREF(uniobj); |
| 10776 | return 0; |
| 10777 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10778 | *fillcharloc = PyUnicode_READ_CHAR(uniobj, 0); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10779 | Py_DECREF(uniobj); |
| 10780 | return 1; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10781 | } |
| 10782 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10783 | PyDoc_STRVAR(center__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10784 | "S.center(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10785 | \n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 10786 | Return S centered in a string of length width. Padding is\n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10787 | done using the specified fill character (default is a space)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10788 | |
| 10789 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10790 | unicode_center(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10791 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10792 | Py_ssize_t marg, left; |
| 10793 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10794 | Py_UCS4 fillchar = ' '; |
| 10795 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 10796 | 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] | 10797 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10798 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 10799 | if (PyUnicode_READY(self) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10800 | return NULL; |
| 10801 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10802 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 10803 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10804 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10805 | marg = width - PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10806 | left = marg / 2 + (marg & width & 1); |
| 10807 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10808 | return pad(self, left, marg - left, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10809 | } |
| 10810 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10811 | /* This function assumes that str1 and str2 are readied by the caller. */ |
| 10812 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10813 | static int |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10814 | unicode_compare(PyObject *str1, PyObject *str2) |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10815 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10816 | int kind1, kind2; |
| 10817 | void *data1, *data2; |
| 10818 | Py_ssize_t len1, len2, i; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10819 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10820 | kind1 = PyUnicode_KIND(str1); |
| 10821 | kind2 = PyUnicode_KIND(str2); |
| 10822 | data1 = PyUnicode_DATA(str1); |
| 10823 | data2 = PyUnicode_DATA(str2); |
| 10824 | len1 = PyUnicode_GET_LENGTH(str1); |
| 10825 | len2 = PyUnicode_GET_LENGTH(str2); |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10826 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10827 | for (i = 0; i < len1 && i < len2; ++i) { |
| 10828 | Py_UCS4 c1, c2; |
| 10829 | c1 = PyUnicode_READ(kind1, data1, i); |
| 10830 | c2 = PyUnicode_READ(kind2, data2, i); |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 10831 | |
| 10832 | if (c1 != c2) |
| 10833 | return (c1 < c2) ? -1 : 1; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10834 | } |
| 10835 | |
| 10836 | return (len1 < len2) ? -1 : (len1 != len2); |
| 10837 | } |
| 10838 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10839 | int |
| 10840 | PyUnicode_Compare(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10841 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10842 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 10843 | if (PyUnicode_READY(left) == -1 || |
| 10844 | PyUnicode_READY(right) == -1) |
| 10845 | return -1; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10846 | return unicode_compare(left, right); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10847 | } |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 10848 | PyErr_Format(PyExc_TypeError, |
| 10849 | "Can't compare %.100s and %.100s", |
| 10850 | left->ob_type->tp_name, |
| 10851 | right->ob_type->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10852 | return -1; |
| 10853 | } |
| 10854 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10855 | int |
| 10856 | PyUnicode_CompareWithASCIIString(PyObject* uni, const char* str) |
| 10857 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10858 | Py_ssize_t i; |
| 10859 | int kind; |
| 10860 | void *data; |
| 10861 | Py_UCS4 chr; |
| 10862 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 10863 | assert(_PyUnicode_CHECK(uni)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10864 | if (PyUnicode_READY(uni) == -1) |
| 10865 | return -1; |
| 10866 | kind = PyUnicode_KIND(uni); |
| 10867 | data = PyUnicode_DATA(uni); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10868 | /* Compare Unicode string and source character set string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10869 | for (i = 0; (chr = PyUnicode_READ(kind, data, i)) && str[i]; i++) |
| 10870 | if (chr != str[i]) |
| 10871 | return (chr < (unsigned char)(str[i])) ? -1 : 1; |
Benjamin Peterson | 8667a9b | 2010-01-09 21:45:28 +0000 | [diff] [blame] | 10872 | /* This check keeps Python strings that end in '\0' from comparing equal |
| 10873 | to C strings identical up to that point. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10874 | if (PyUnicode_GET_LENGTH(uni) != i || chr) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10875 | return 1; /* uni is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10876 | if (str[i]) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10877 | return -1; /* str is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10878 | return 0; |
| 10879 | } |
| 10880 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10881 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10882 | #define TEST_COND(cond) \ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10883 | ((cond) ? Py_True : Py_False) |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10884 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10885 | PyObject * |
| 10886 | PyUnicode_RichCompare(PyObject *left, PyObject *right, int op) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10887 | { |
| 10888 | int result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10889 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10890 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 10891 | PyObject *v; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10892 | if (PyUnicode_READY(left) == -1 || |
| 10893 | PyUnicode_READY(right) == -1) |
| 10894 | return NULL; |
| 10895 | if (PyUnicode_GET_LENGTH(left) != PyUnicode_GET_LENGTH(right) || |
| 10896 | PyUnicode_KIND(left) != PyUnicode_KIND(right)) { |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10897 | if (op == Py_EQ) { |
| 10898 | Py_INCREF(Py_False); |
| 10899 | return Py_False; |
| 10900 | } |
| 10901 | if (op == Py_NE) { |
| 10902 | Py_INCREF(Py_True); |
| 10903 | return Py_True; |
| 10904 | } |
| 10905 | } |
| 10906 | if (left == right) |
| 10907 | result = 0; |
| 10908 | else |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10909 | result = unicode_compare(left, right); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10910 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10911 | /* Convert the return value to a Boolean */ |
| 10912 | switch (op) { |
| 10913 | case Py_EQ: |
| 10914 | v = TEST_COND(result == 0); |
| 10915 | break; |
| 10916 | case Py_NE: |
| 10917 | v = TEST_COND(result != 0); |
| 10918 | break; |
| 10919 | case Py_LE: |
| 10920 | v = TEST_COND(result <= 0); |
| 10921 | break; |
| 10922 | case Py_GE: |
| 10923 | v = TEST_COND(result >= 0); |
| 10924 | break; |
| 10925 | case Py_LT: |
| 10926 | v = TEST_COND(result == -1); |
| 10927 | break; |
| 10928 | case Py_GT: |
| 10929 | v = TEST_COND(result == 1); |
| 10930 | break; |
| 10931 | default: |
| 10932 | PyErr_BadArgument(); |
| 10933 | return NULL; |
| 10934 | } |
| 10935 | Py_INCREF(v); |
| 10936 | return v; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10937 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10938 | |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 10939 | Py_RETURN_NOTIMPLEMENTED; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10940 | } |
| 10941 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10942 | int |
| 10943 | PyUnicode_Contains(PyObject *container, PyObject *element) |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10944 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10945 | PyObject *str, *sub; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10946 | int kind1, kind2, kind; |
| 10947 | void *buf1, *buf2; |
| 10948 | Py_ssize_t len1, len2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10949 | int result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10950 | |
| 10951 | /* Coerce the two arguments */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10952 | sub = PyUnicode_FromObject(element); |
| 10953 | if (!sub) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10954 | PyErr_Format(PyExc_TypeError, |
| 10955 | "'in <string>' requires string as left operand, not %s", |
| 10956 | element->ob_type->tp_name); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10957 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10958 | } |
| 10959 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10960 | str = PyUnicode_FromObject(container); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 10961 | if (!str) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10962 | Py_DECREF(sub); |
| 10963 | return -1; |
| 10964 | } |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 10965 | if (PyUnicode_READY(sub) == -1 || PyUnicode_READY(str) == -1) { |
| 10966 | Py_DECREF(sub); |
| 10967 | Py_DECREF(str); |
| 10968 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10969 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10970 | kind1 = PyUnicode_KIND(str); |
| 10971 | kind2 = PyUnicode_KIND(sub); |
| 10972 | kind = kind1 > kind2 ? kind1 : kind2; |
| 10973 | buf1 = PyUnicode_DATA(str); |
| 10974 | buf2 = PyUnicode_DATA(sub); |
| 10975 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10976 | buf1 = _PyUnicode_AsKind(str, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10977 | if (!buf1) { |
| 10978 | Py_DECREF(sub); |
| 10979 | return -1; |
| 10980 | } |
| 10981 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10982 | buf2 = _PyUnicode_AsKind(sub, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10983 | if (!buf2) { |
| 10984 | Py_DECREF(sub); |
| 10985 | if (kind1 != kind) PyMem_Free(buf1); |
| 10986 | return -1; |
| 10987 | } |
| 10988 | len1 = PyUnicode_GET_LENGTH(str); |
| 10989 | len2 = PyUnicode_GET_LENGTH(sub); |
| 10990 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10991 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10992 | case PyUnicode_1BYTE_KIND: |
| 10993 | result = ucs1lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10994 | break; |
| 10995 | case PyUnicode_2BYTE_KIND: |
| 10996 | result = ucs2lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10997 | break; |
| 10998 | case PyUnicode_4BYTE_KIND: |
| 10999 | result = ucs4lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 11000 | break; |
| 11001 | default: |
| 11002 | result = -1; |
| 11003 | assert(0); |
| 11004 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11005 | |
| 11006 | Py_DECREF(str); |
| 11007 | Py_DECREF(sub); |
| 11008 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11009 | if (kind1 != kind) |
| 11010 | PyMem_Free(buf1); |
| 11011 | if (kind2 != kind) |
| 11012 | PyMem_Free(buf2); |
| 11013 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 11014 | return result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 11015 | } |
| 11016 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11017 | /* Concat to string or Unicode object giving a new Unicode object. */ |
| 11018 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 11019 | PyObject * |
| 11020 | PyUnicode_Concat(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11021 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11022 | PyObject *u = NULL, *v = NULL, *w; |
Victor Stinner | 127226b | 2011-10-13 01:12:34 +0200 | [diff] [blame] | 11023 | Py_UCS4 maxchar, maxchar2; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11024 | Py_ssize_t u_len, v_len, new_len; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11025 | |
| 11026 | /* Coerce the two arguments */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11027 | u = PyUnicode_FromObject(left); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11028 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11029 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11030 | v = PyUnicode_FromObject(right); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11031 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11032 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11033 | |
| 11034 | /* Shortcuts */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 11035 | if (v == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11036 | Py_DECREF(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11037 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11038 | } |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 11039 | if (u == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11040 | Py_DECREF(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11041 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11042 | } |
| 11043 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11044 | u_len = PyUnicode_GET_LENGTH(u); |
| 11045 | v_len = PyUnicode_GET_LENGTH(v); |
| 11046 | if (u_len > PY_SSIZE_T_MAX - v_len) { |
| 11047 | PyErr_SetString(PyExc_OverflowError, |
| 11048 | "strings are too large to concat"); |
| 11049 | goto onError; |
| 11050 | } |
| 11051 | new_len = u_len + v_len; |
| 11052 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11053 | maxchar = PyUnicode_MAX_CHAR_VALUE(u); |
Victor Stinner | 127226b | 2011-10-13 01:12:34 +0200 | [diff] [blame] | 11054 | maxchar2 = PyUnicode_MAX_CHAR_VALUE(v); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 11055 | maxchar = MAX_MAXCHAR(maxchar, maxchar2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11056 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11057 | /* Concat the two Unicode strings */ |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11058 | w = PyUnicode_New(new_len, maxchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11059 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11060 | goto onError; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11061 | copy_characters(w, 0, u, 0, u_len); |
| 11062 | copy_characters(w, u_len, v, 0, v_len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11063 | Py_DECREF(u); |
| 11064 | Py_DECREF(v); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 11065 | assert(_PyUnicode_CheckConsistency(w, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11066 | return w; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11067 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11068 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11069 | Py_XDECREF(u); |
| 11070 | Py_XDECREF(v); |
| 11071 | return NULL; |
| 11072 | } |
| 11073 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 11074 | void |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11075 | PyUnicode_Append(PyObject **p_left, PyObject *right) |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 11076 | { |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11077 | PyObject *left, *res; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11078 | Py_UCS4 maxchar, maxchar2; |
| 11079 | Py_ssize_t left_len, right_len, new_len; |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11080 | |
| 11081 | if (p_left == NULL) { |
| 11082 | if (!PyErr_Occurred()) |
| 11083 | PyErr_BadInternalCall(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11084 | return; |
| 11085 | } |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11086 | left = *p_left; |
| 11087 | if (right == NULL || !PyUnicode_Check(left)) { |
| 11088 | if (!PyErr_Occurred()) |
| 11089 | PyErr_BadInternalCall(); |
| 11090 | goto error; |
| 11091 | } |
| 11092 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 11093 | if (PyUnicode_READY(left) == -1) |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 11094 | goto error; |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 11095 | if (PyUnicode_READY(right) == -1) |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 11096 | goto error; |
| 11097 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11098 | /* Shortcuts */ |
| 11099 | if (left == unicode_empty) { |
| 11100 | Py_DECREF(left); |
| 11101 | Py_INCREF(right); |
| 11102 | *p_left = right; |
| 11103 | return; |
| 11104 | } |
| 11105 | if (right == unicode_empty) |
| 11106 | return; |
| 11107 | |
| 11108 | left_len = PyUnicode_GET_LENGTH(left); |
| 11109 | right_len = PyUnicode_GET_LENGTH(right); |
| 11110 | if (left_len > PY_SSIZE_T_MAX - right_len) { |
| 11111 | PyErr_SetString(PyExc_OverflowError, |
| 11112 | "strings are too large to concat"); |
| 11113 | goto error; |
| 11114 | } |
| 11115 | new_len = left_len + right_len; |
| 11116 | |
| 11117 | if (unicode_modifiable(left) |
| 11118 | && PyUnicode_CheckExact(right) |
| 11119 | && PyUnicode_KIND(right) <= PyUnicode_KIND(left) |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 11120 | /* Don't resize for ascii += latin1. Convert ascii to latin1 requires |
| 11121 | to change the structure size, but characters are stored just after |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 11122 | the structure, and so it requires to move all characters which is |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 11123 | not so different than duplicating the string. */ |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11124 | && !(PyUnicode_IS_ASCII(left) && !PyUnicode_IS_ASCII(right))) |
| 11125 | { |
| 11126 | /* append inplace */ |
| 11127 | if (unicode_resize(p_left, new_len) != 0) { |
| 11128 | /* XXX if _PyUnicode_Resize() fails, 'left' has been |
| 11129 | * deallocated so it cannot be put back into |
| 11130 | * 'variable'. The MemoryError is raised when there |
| 11131 | * is no value in 'variable', which might (very |
| 11132 | * remotely) be a cause of incompatibilities. |
| 11133 | */ |
| 11134 | goto error; |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11135 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11136 | /* copy 'right' into the newly allocated area of 'left' */ |
| 11137 | copy_characters(*p_left, left_len, right, 0, right_len); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11138 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11139 | else { |
| 11140 | maxchar = PyUnicode_MAX_CHAR_VALUE(left); |
| 11141 | maxchar2 = PyUnicode_MAX_CHAR_VALUE(right); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 11142 | maxchar = MAX_MAXCHAR(maxchar, maxchar2); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11143 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11144 | /* Concat the two Unicode strings */ |
| 11145 | res = PyUnicode_New(new_len, maxchar); |
| 11146 | if (res == NULL) |
| 11147 | goto error; |
| 11148 | copy_characters(res, 0, left, 0, left_len); |
| 11149 | copy_characters(res, left_len, right, 0, right_len); |
| 11150 | Py_DECREF(left); |
| 11151 | *p_left = res; |
| 11152 | } |
| 11153 | assert(_PyUnicode_CheckConsistency(*p_left, 1)); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11154 | return; |
| 11155 | |
| 11156 | error: |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11157 | Py_CLEAR(*p_left); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 11158 | } |
| 11159 | |
| 11160 | void |
| 11161 | PyUnicode_AppendAndDel(PyObject **pleft, PyObject *right) |
| 11162 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11163 | PyUnicode_Append(pleft, right); |
| 11164 | Py_XDECREF(right); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 11165 | } |
| 11166 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11167 | PyDoc_STRVAR(count__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11168 | "S.count(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11169 | \n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11170 | Return the number of non-overlapping occurrences of substring sub in\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11171 | 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] | 11172 | interpreted as in slice notation."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11173 | |
| 11174 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11175 | unicode_count(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11176 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11177 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11178 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11179 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11180 | PyObject *result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11181 | int kind1, kind2, kind; |
| 11182 | void *buf1, *buf2; |
| 11183 | Py_ssize_t len1, len2, iresult; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11184 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11185 | if (!stringlib_parse_args_finds_unicode("count", args, &substring, |
| 11186 | &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11187 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11188 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11189 | kind1 = PyUnicode_KIND(self); |
| 11190 | kind2 = PyUnicode_KIND(substring); |
Benjamin Peterson | b63f49f | 2012-05-03 18:31:07 -0400 | [diff] [blame] | 11191 | if (kind2 > kind1) |
| 11192 | return PyLong_FromLong(0); |
| 11193 | kind = kind1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11194 | buf1 = PyUnicode_DATA(self); |
| 11195 | buf2 = PyUnicode_DATA(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11196 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11197 | buf2 = _PyUnicode_AsKind(substring, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11198 | if (!buf2) { |
| 11199 | Py_DECREF(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11200 | return NULL; |
| 11201 | } |
| 11202 | len1 = PyUnicode_GET_LENGTH(self); |
| 11203 | len2 = PyUnicode_GET_LENGTH(substring); |
| 11204 | |
| 11205 | ADJUST_INDICES(start, end, len1); |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 11206 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11207 | case PyUnicode_1BYTE_KIND: |
| 11208 | iresult = ucs1lib_count( |
| 11209 | ((Py_UCS1*)buf1) + start, end - start, |
| 11210 | buf2, len2, PY_SSIZE_T_MAX |
| 11211 | ); |
| 11212 | break; |
| 11213 | case PyUnicode_2BYTE_KIND: |
| 11214 | iresult = ucs2lib_count( |
| 11215 | ((Py_UCS2*)buf1) + start, end - start, |
| 11216 | buf2, len2, PY_SSIZE_T_MAX |
| 11217 | ); |
| 11218 | break; |
| 11219 | case PyUnicode_4BYTE_KIND: |
| 11220 | iresult = ucs4lib_count( |
| 11221 | ((Py_UCS4*)buf1) + start, end - start, |
| 11222 | buf2, len2, PY_SSIZE_T_MAX |
| 11223 | ); |
| 11224 | break; |
| 11225 | default: |
| 11226 | assert(0); iresult = 0; |
| 11227 | } |
| 11228 | |
| 11229 | result = PyLong_FromSsize_t(iresult); |
| 11230 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11231 | if (kind2 != kind) |
| 11232 | PyMem_Free(buf2); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11233 | |
| 11234 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11235 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11236 | return result; |
| 11237 | } |
| 11238 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11239 | PyDoc_STRVAR(encode__doc__, |
Victor Stinner | c911bbf | 2010-11-07 19:04:46 +0000 | [diff] [blame] | 11240 | "S.encode(encoding='utf-8', errors='strict') -> bytes\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11241 | \n\ |
Victor Stinner | e14e212 | 2010-11-07 18:41:46 +0000 | [diff] [blame] | 11242 | Encode S using the codec registered for encoding. Default encoding\n\ |
| 11243 | 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] | 11244 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 11245 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 11246 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 11247 | codecs.register_error that can handle UnicodeEncodeErrors."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11248 | |
| 11249 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11250 | unicode_encode(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11251 | { |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 11252 | static char *kwlist[] = {"encoding", "errors", 0}; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11253 | char *encoding = NULL; |
| 11254 | char *errors = NULL; |
Guido van Rossum | 35d9428 | 2007-08-27 18:20:11 +0000 | [diff] [blame] | 11255 | |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 11256 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:encode", |
| 11257 | kwlist, &encoding, &errors)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11258 | return NULL; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11259 | return PyUnicode_AsEncodedString(self, encoding, errors); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 11260 | } |
| 11261 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11262 | PyDoc_STRVAR(expandtabs__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11263 | "S.expandtabs([tabsize]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11264 | \n\ |
| 11265 | 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] | 11266 | 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] | 11267 | |
| 11268 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11269 | unicode_expandtabs(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11270 | { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11271 | Py_ssize_t i, j, line_pos, src_len, incr; |
| 11272 | Py_UCS4 ch; |
| 11273 | PyObject *u; |
| 11274 | void *src_data, *dest_data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11275 | int tabsize = 8; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11276 | int kind; |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 11277 | int found; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11278 | |
| 11279 | if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11280 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11281 | |
Antoine Pitrou | 2242522 | 2011-10-04 19:10:51 +0200 | [diff] [blame] | 11282 | if (PyUnicode_READY(self) == -1) |
| 11283 | return NULL; |
| 11284 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 11285 | /* First pass: determine size of output string */ |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11286 | src_len = PyUnicode_GET_LENGTH(self); |
| 11287 | i = j = line_pos = 0; |
| 11288 | kind = PyUnicode_KIND(self); |
| 11289 | src_data = PyUnicode_DATA(self); |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 11290 | found = 0; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11291 | for (; i < src_len; i++) { |
| 11292 | ch = PyUnicode_READ(kind, src_data, i); |
| 11293 | if (ch == '\t') { |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 11294 | found = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11295 | if (tabsize > 0) { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11296 | incr = tabsize - (line_pos % tabsize); /* cannot overflow */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11297 | if (j > PY_SSIZE_T_MAX - incr) |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11298 | goto overflow; |
| 11299 | line_pos += incr; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11300 | j += incr; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11301 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11302 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11303 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11304 | if (j > PY_SSIZE_T_MAX - 1) |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11305 | goto overflow; |
| 11306 | line_pos++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11307 | j++; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11308 | if (ch == '\n' || ch == '\r') |
| 11309 | line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11310 | } |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11311 | } |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11312 | if (!found) |
| 11313 | return unicode_result_unchanged(self); |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 11314 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11315 | /* Second pass: create output string and fill it */ |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11316 | u = PyUnicode_New(j, PyUnicode_MAX_CHAR_VALUE(self)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11317 | if (!u) |
| 11318 | return NULL; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11319 | dest_data = PyUnicode_DATA(u); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11320 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11321 | i = j = line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11322 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11323 | for (; i < src_len; i++) { |
| 11324 | ch = PyUnicode_READ(kind, src_data, i); |
| 11325 | if (ch == '\t') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11326 | if (tabsize > 0) { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11327 | incr = tabsize - (line_pos % tabsize); |
| 11328 | line_pos += incr; |
Victor Stinner | da79e63 | 2012-02-22 13:37:04 +0100 | [diff] [blame] | 11329 | FILL(kind, dest_data, ' ', j, incr); |
| 11330 | j += incr; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11331 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11332 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11333 | else { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11334 | line_pos++; |
| 11335 | PyUnicode_WRITE(kind, dest_data, j, ch); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11336 | j++; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11337 | if (ch == '\n' || ch == '\r') |
| 11338 | line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11339 | } |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11340 | } |
| 11341 | assert (j == PyUnicode_GET_LENGTH(u)); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 11342 | return unicode_result(u); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11343 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11344 | overflow: |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11345 | PyErr_SetString(PyExc_OverflowError, "new string is too long"); |
| 11346 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11347 | } |
| 11348 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11349 | PyDoc_STRVAR(find__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11350 | "S.find(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11351 | \n\ |
| 11352 | Return the lowest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 11353 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11354 | arguments start and end are interpreted as in slice notation.\n\ |
| 11355 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11356 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11357 | |
| 11358 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11359 | unicode_find(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11360 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11361 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 11362 | Py_ssize_t start; |
| 11363 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11364 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11365 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11366 | if (!stringlib_parse_args_finds_unicode("find", args, &substring, |
| 11367 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11368 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11369 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11370 | if (PyUnicode_READY(self) == -1) |
| 11371 | return NULL; |
| 11372 | if (PyUnicode_READY(substring) == -1) |
| 11373 | return NULL; |
| 11374 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11375 | result = any_find_slice(1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11376 | |
| 11377 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11378 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11379 | if (result == -2) |
| 11380 | return NULL; |
| 11381 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 11382 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11383 | } |
| 11384 | |
| 11385 | static PyObject * |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 11386 | unicode_getitem(PyObject *self, Py_ssize_t index) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11387 | { |
Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 11388 | void *data; |
| 11389 | enum PyUnicode_Kind kind; |
| 11390 | Py_UCS4 ch; |
| 11391 | PyObject *res; |
| 11392 | |
| 11393 | if (!PyUnicode_Check(self) || PyUnicode_READY(self) == -1) { |
| 11394 | PyErr_BadArgument(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11395 | return NULL; |
Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 11396 | } |
| 11397 | if (index < 0 || index >= PyUnicode_GET_LENGTH(self)) { |
| 11398 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 11399 | return NULL; |
| 11400 | } |
| 11401 | kind = PyUnicode_KIND(self); |
| 11402 | data = PyUnicode_DATA(self); |
| 11403 | ch = PyUnicode_READ(kind, data, index); |
| 11404 | if (ch < 256) |
| 11405 | return get_latin1_char(ch); |
| 11406 | |
| 11407 | res = PyUnicode_New(1, ch); |
| 11408 | if (res == NULL) |
| 11409 | return NULL; |
| 11410 | kind = PyUnicode_KIND(res); |
| 11411 | data = PyUnicode_DATA(res); |
| 11412 | PyUnicode_WRITE(kind, data, 0, ch); |
| 11413 | assert(_PyUnicode_CheckConsistency(res, 1)); |
| 11414 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11415 | } |
| 11416 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11417 | /* Believe it or not, this produces the same value for ASCII strings |
Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 11418 | as bytes_hash(). */ |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 11419 | static Py_hash_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11420 | unicode_hash(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11421 | { |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11422 | Py_ssize_t len; |
Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 11423 | Py_uhash_t x; |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11424 | |
Benjamin Peterson | f6622c8 | 2012-04-09 14:53:07 -0400 | [diff] [blame] | 11425 | #ifdef Py_DEBUG |
Benjamin Peterson | 69e9727 | 2012-02-21 11:08:50 -0500 | [diff] [blame] | 11426 | assert(_Py_HashSecret_Initialized); |
Benjamin Peterson | f6622c8 | 2012-04-09 14:53:07 -0400 | [diff] [blame] | 11427 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11428 | if (_PyUnicode_HASH(self) != -1) |
| 11429 | return _PyUnicode_HASH(self); |
| 11430 | if (PyUnicode_READY(self) == -1) |
| 11431 | return -1; |
| 11432 | len = PyUnicode_GET_LENGTH(self); |
Georg Brandl | 16fa2a1 | 2012-02-21 00:50:13 +0100 | [diff] [blame] | 11433 | /* |
| 11434 | We make the hash of the empty string be 0, rather than using |
| 11435 | (prefix ^ suffix), since this slightly obfuscates the hash secret |
| 11436 | */ |
| 11437 | if (len == 0) { |
| 11438 | _PyUnicode_HASH(self) = 0; |
| 11439 | return 0; |
| 11440 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11441 | |
| 11442 | /* The hash function as a macro, gets expanded three times below. */ |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 11443 | #define HASH(P) \ |
| 11444 | x ^= (Py_uhash_t) *P << 7; \ |
| 11445 | while (--len >= 0) \ |
| 11446 | x = (_PyHASH_MULTIPLIER * x) ^ (Py_uhash_t) *P++; \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11447 | |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 11448 | x = (Py_uhash_t) _Py_HashSecret.prefix; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11449 | switch (PyUnicode_KIND(self)) { |
| 11450 | case PyUnicode_1BYTE_KIND: { |
| 11451 | const unsigned char *c = PyUnicode_1BYTE_DATA(self); |
| 11452 | HASH(c); |
| 11453 | break; |
| 11454 | } |
| 11455 | case PyUnicode_2BYTE_KIND: { |
| 11456 | const Py_UCS2 *s = PyUnicode_2BYTE_DATA(self); |
| 11457 | HASH(s); |
| 11458 | break; |
| 11459 | } |
| 11460 | default: { |
| 11461 | Py_UCS4 *l; |
| 11462 | assert(PyUnicode_KIND(self) == PyUnicode_4BYTE_KIND && |
| 11463 | "Impossible switch case in unicode_hash"); |
| 11464 | l = PyUnicode_4BYTE_DATA(self); |
| 11465 | HASH(l); |
| 11466 | break; |
| 11467 | } |
| 11468 | } |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 11469 | x ^= (Py_uhash_t) PyUnicode_GET_LENGTH(self); |
| 11470 | x ^= (Py_uhash_t) _Py_HashSecret.suffix; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11471 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11472 | if (x == -1) |
| 11473 | x = -2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11474 | _PyUnicode_HASH(self) = x; |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11475 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11476 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11477 | #undef HASH |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11478 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11479 | PyDoc_STRVAR(index__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11480 | "S.index(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11481 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11482 | 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] | 11483 | |
| 11484 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11485 | unicode_index(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11486 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11487 | Py_ssize_t result; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11488 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 11489 | Py_ssize_t start; |
| 11490 | Py_ssize_t end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11491 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11492 | if (!stringlib_parse_args_finds_unicode("index", args, &substring, |
| 11493 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11494 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11495 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11496 | if (PyUnicode_READY(self) == -1) |
| 11497 | return NULL; |
| 11498 | if (PyUnicode_READY(substring) == -1) |
| 11499 | return NULL; |
| 11500 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11501 | result = any_find_slice(1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11502 | |
| 11503 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11504 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11505 | if (result == -2) |
| 11506 | return NULL; |
| 11507 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11508 | if (result < 0) { |
| 11509 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 11510 | return NULL; |
| 11511 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11512 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 11513 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11514 | } |
| 11515 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11516 | PyDoc_STRVAR(islower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11517 | "S.islower() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11518 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11519 | 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] | 11520 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11521 | |
| 11522 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11523 | unicode_islower(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11524 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11525 | Py_ssize_t i, length; |
| 11526 | int kind; |
| 11527 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11528 | int cased; |
| 11529 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11530 | if (PyUnicode_READY(self) == -1) |
| 11531 | return NULL; |
| 11532 | length = PyUnicode_GET_LENGTH(self); |
| 11533 | kind = PyUnicode_KIND(self); |
| 11534 | data = PyUnicode_DATA(self); |
| 11535 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11536 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11537 | if (length == 1) |
| 11538 | return PyBool_FromLong( |
| 11539 | Py_UNICODE_ISLOWER(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11540 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11541 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11542 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11543 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11544 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11545 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11546 | for (i = 0; i < length; i++) { |
| 11547 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11548 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11549 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 11550 | return PyBool_FromLong(0); |
| 11551 | else if (!cased && Py_UNICODE_ISLOWER(ch)) |
| 11552 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11553 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11554 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11555 | } |
| 11556 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11557 | PyDoc_STRVAR(isupper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11558 | "S.isupper() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11559 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11560 | 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] | 11561 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11562 | |
| 11563 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11564 | unicode_isupper(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11565 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11566 | Py_ssize_t i, length; |
| 11567 | int kind; |
| 11568 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11569 | int cased; |
| 11570 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11571 | if (PyUnicode_READY(self) == -1) |
| 11572 | return NULL; |
| 11573 | length = PyUnicode_GET_LENGTH(self); |
| 11574 | kind = PyUnicode_KIND(self); |
| 11575 | data = PyUnicode_DATA(self); |
| 11576 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11577 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11578 | if (length == 1) |
| 11579 | return PyBool_FromLong( |
| 11580 | Py_UNICODE_ISUPPER(PyUnicode_READ(kind, data, 0)) != 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11581 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11582 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11583 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11584 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11585 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11586 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11587 | for (i = 0; i < length; i++) { |
| 11588 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11589 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11590 | if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 11591 | return PyBool_FromLong(0); |
| 11592 | else if (!cased && Py_UNICODE_ISUPPER(ch)) |
| 11593 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11594 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11595 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11596 | } |
| 11597 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11598 | PyDoc_STRVAR(istitle__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11599 | "S.istitle() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11600 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11601 | Return True if S is a titlecased string and there is at least one\n\ |
| 11602 | character in S, i.e. upper- and titlecase characters may only\n\ |
| 11603 | follow uncased characters and lowercase characters only cased ones.\n\ |
| 11604 | Return False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11605 | |
| 11606 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11607 | unicode_istitle(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11608 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11609 | Py_ssize_t i, length; |
| 11610 | int kind; |
| 11611 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11612 | int cased, previous_is_cased; |
| 11613 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11614 | if (PyUnicode_READY(self) == -1) |
| 11615 | return NULL; |
| 11616 | length = PyUnicode_GET_LENGTH(self); |
| 11617 | kind = PyUnicode_KIND(self); |
| 11618 | data = PyUnicode_DATA(self); |
| 11619 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11620 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11621 | if (length == 1) { |
| 11622 | Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11623 | return PyBool_FromLong((Py_UNICODE_ISTITLE(ch) != 0) || |
| 11624 | (Py_UNICODE_ISUPPER(ch) != 0)); |
| 11625 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11626 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11627 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11628 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11629 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11630 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11631 | cased = 0; |
| 11632 | previous_is_cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11633 | for (i = 0; i < length; i++) { |
| 11634 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11635 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11636 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { |
| 11637 | if (previous_is_cased) |
| 11638 | return PyBool_FromLong(0); |
| 11639 | previous_is_cased = 1; |
| 11640 | cased = 1; |
| 11641 | } |
| 11642 | else if (Py_UNICODE_ISLOWER(ch)) { |
| 11643 | if (!previous_is_cased) |
| 11644 | return PyBool_FromLong(0); |
| 11645 | previous_is_cased = 1; |
| 11646 | cased = 1; |
| 11647 | } |
| 11648 | else |
| 11649 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11650 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11651 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11652 | } |
| 11653 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11654 | PyDoc_STRVAR(isspace__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11655 | "S.isspace() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11656 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11657 | Return True if all characters in S are whitespace\n\ |
| 11658 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11659 | |
| 11660 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11661 | unicode_isspace(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11662 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11663 | Py_ssize_t i, length; |
| 11664 | int kind; |
| 11665 | void *data; |
| 11666 | |
| 11667 | if (PyUnicode_READY(self) == -1) |
| 11668 | return NULL; |
| 11669 | length = PyUnicode_GET_LENGTH(self); |
| 11670 | kind = PyUnicode_KIND(self); |
| 11671 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11672 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11673 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11674 | if (length == 1) |
| 11675 | return PyBool_FromLong( |
| 11676 | Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11677 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11678 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11679 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11680 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11681 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11682 | for (i = 0; i < length; i++) { |
| 11683 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11684 | if (!Py_UNICODE_ISSPACE(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11685 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11686 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11687 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11688 | } |
| 11689 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11690 | PyDoc_STRVAR(isalpha__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11691 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11692 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11693 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11694 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11695 | |
| 11696 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11697 | unicode_isalpha(PyObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11698 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11699 | Py_ssize_t i, length; |
| 11700 | int kind; |
| 11701 | void *data; |
| 11702 | |
| 11703 | if (PyUnicode_READY(self) == -1) |
| 11704 | return NULL; |
| 11705 | length = PyUnicode_GET_LENGTH(self); |
| 11706 | kind = PyUnicode_KIND(self); |
| 11707 | data = PyUnicode_DATA(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11708 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11709 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11710 | if (length == 1) |
| 11711 | return PyBool_FromLong( |
| 11712 | Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, 0))); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11713 | |
| 11714 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11715 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11716 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11717 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11718 | for (i = 0; i < length; i++) { |
| 11719 | if (!Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11720 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11721 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11722 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11723 | } |
| 11724 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11725 | PyDoc_STRVAR(isalnum__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11726 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11727 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11728 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11729 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11730 | |
| 11731 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11732 | unicode_isalnum(PyObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11733 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11734 | int kind; |
| 11735 | void *data; |
| 11736 | Py_ssize_t len, i; |
| 11737 | |
| 11738 | if (PyUnicode_READY(self) == -1) |
| 11739 | return NULL; |
| 11740 | |
| 11741 | kind = PyUnicode_KIND(self); |
| 11742 | data = PyUnicode_DATA(self); |
| 11743 | len = PyUnicode_GET_LENGTH(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11744 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11745 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11746 | if (len == 1) { |
| 11747 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11748 | return PyBool_FromLong(Py_UNICODE_ISALNUM(ch)); |
| 11749 | } |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11750 | |
| 11751 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11752 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11753 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11754 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11755 | for (i = 0; i < len; i++) { |
| 11756 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11757 | if (!Py_UNICODE_ISALNUM(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11758 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11759 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11760 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11761 | } |
| 11762 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11763 | PyDoc_STRVAR(isdecimal__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11764 | "S.isdecimal() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11765 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11766 | 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] | 11767 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11768 | |
| 11769 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11770 | unicode_isdecimal(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11771 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11772 | Py_ssize_t i, length; |
| 11773 | int kind; |
| 11774 | void *data; |
| 11775 | |
| 11776 | if (PyUnicode_READY(self) == -1) |
| 11777 | return NULL; |
| 11778 | length = PyUnicode_GET_LENGTH(self); |
| 11779 | kind = PyUnicode_KIND(self); |
| 11780 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11781 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11782 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11783 | if (length == 1) |
| 11784 | return PyBool_FromLong( |
| 11785 | Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11786 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11787 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11788 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11789 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11790 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11791 | for (i = 0; i < length; i++) { |
| 11792 | if (!Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11793 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11794 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11795 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11796 | } |
| 11797 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11798 | PyDoc_STRVAR(isdigit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11799 | "S.isdigit() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11800 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11801 | Return True if all characters in S are digits\n\ |
| 11802 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11803 | |
| 11804 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11805 | unicode_isdigit(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11806 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11807 | Py_ssize_t i, length; |
| 11808 | int kind; |
| 11809 | void *data; |
| 11810 | |
| 11811 | if (PyUnicode_READY(self) == -1) |
| 11812 | return NULL; |
| 11813 | length = PyUnicode_GET_LENGTH(self); |
| 11814 | kind = PyUnicode_KIND(self); |
| 11815 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11816 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11817 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11818 | if (length == 1) { |
| 11819 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11820 | return PyBool_FromLong(Py_UNICODE_ISDIGIT(ch)); |
| 11821 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11822 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11823 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11824 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11825 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11826 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11827 | for (i = 0; i < length; i++) { |
| 11828 | if (!Py_UNICODE_ISDIGIT(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11829 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11830 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11831 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11832 | } |
| 11833 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11834 | PyDoc_STRVAR(isnumeric__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11835 | "S.isnumeric() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11836 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11837 | 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] | 11838 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11839 | |
| 11840 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11841 | unicode_isnumeric(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11842 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11843 | Py_ssize_t i, length; |
| 11844 | int kind; |
| 11845 | void *data; |
| 11846 | |
| 11847 | if (PyUnicode_READY(self) == -1) |
| 11848 | return NULL; |
| 11849 | length = PyUnicode_GET_LENGTH(self); |
| 11850 | kind = PyUnicode_KIND(self); |
| 11851 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11852 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11853 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11854 | if (length == 1) |
| 11855 | return PyBool_FromLong( |
| 11856 | Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11857 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11858 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11859 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11860 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11861 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11862 | for (i = 0; i < length; i++) { |
| 11863 | if (!Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11864 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11865 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11866 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11867 | } |
| 11868 | |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11869 | int |
| 11870 | PyUnicode_IsIdentifier(PyObject *self) |
| 11871 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11872 | int kind; |
| 11873 | void *data; |
| 11874 | Py_ssize_t i; |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11875 | Py_UCS4 first; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11876 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11877 | if (PyUnicode_READY(self) == -1) { |
| 11878 | Py_FatalError("identifier not ready"); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11879 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11880 | } |
| 11881 | |
| 11882 | /* Special case for empty strings */ |
| 11883 | if (PyUnicode_GET_LENGTH(self) == 0) |
| 11884 | return 0; |
| 11885 | kind = PyUnicode_KIND(self); |
| 11886 | data = PyUnicode_DATA(self); |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11887 | |
| 11888 | /* PEP 3131 says that the first character must be in |
| 11889 | XID_Start and subsequent characters in XID_Continue, |
| 11890 | and for the ASCII range, the 2.x rules apply (i.e |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11891 | start with letters and underscore, continue with |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11892 | letters, digits, underscore). However, given the current |
| 11893 | definition of XID_Start and XID_Continue, it is sufficient |
| 11894 | to check just for these, except that _ must be allowed |
| 11895 | as starting an identifier. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11896 | first = PyUnicode_READ(kind, data, 0); |
Benjamin Peterson | f413b80 | 2011-08-12 22:17:18 -0500 | [diff] [blame] | 11897 | if (!_PyUnicode_IsXidStart(first) && first != 0x5F /* LOW LINE */) |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11898 | return 0; |
| 11899 | |
Benjamin Peterson | 9c6e6a0 | 2011-09-28 08:09:05 -0400 | [diff] [blame] | 11900 | for (i = 1; i < PyUnicode_GET_LENGTH(self); i++) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11901 | if (!_PyUnicode_IsXidContinue(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11902 | return 0; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11903 | return 1; |
| 11904 | } |
| 11905 | |
| 11906 | PyDoc_STRVAR(isidentifier__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11907 | "S.isidentifier() -> bool\n\ |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11908 | \n\ |
| 11909 | Return True if S is a valid identifier according\n\ |
| 11910 | to the language definition."); |
| 11911 | |
| 11912 | static PyObject* |
| 11913 | unicode_isidentifier(PyObject *self) |
| 11914 | { |
| 11915 | return PyBool_FromLong(PyUnicode_IsIdentifier(self)); |
| 11916 | } |
| 11917 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11918 | PyDoc_STRVAR(isprintable__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11919 | "S.isprintable() -> bool\n\ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11920 | \n\ |
| 11921 | Return True if all characters in S are considered\n\ |
| 11922 | printable in repr() or S is empty, False otherwise."); |
| 11923 | |
| 11924 | static PyObject* |
| 11925 | unicode_isprintable(PyObject *self) |
| 11926 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11927 | Py_ssize_t i, length; |
| 11928 | int kind; |
| 11929 | void *data; |
| 11930 | |
| 11931 | if (PyUnicode_READY(self) == -1) |
| 11932 | return NULL; |
| 11933 | length = PyUnicode_GET_LENGTH(self); |
| 11934 | kind = PyUnicode_KIND(self); |
| 11935 | data = PyUnicode_DATA(self); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11936 | |
| 11937 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11938 | if (length == 1) |
| 11939 | return PyBool_FromLong( |
| 11940 | Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, 0))); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11941 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11942 | for (i = 0; i < length; i++) { |
| 11943 | if (!Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, i))) { |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11944 | Py_RETURN_FALSE; |
| 11945 | } |
| 11946 | } |
| 11947 | Py_RETURN_TRUE; |
| 11948 | } |
| 11949 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11950 | PyDoc_STRVAR(join__doc__, |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 11951 | "S.join(iterable) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11952 | \n\ |
| 11953 | 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] | 11954 | iterable. The separator between elements is S."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11955 | |
| 11956 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11957 | unicode_join(PyObject *self, PyObject *data) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11958 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11959 | return PyUnicode_Join(self, data); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11960 | } |
| 11961 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11962 | static Py_ssize_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11963 | unicode_length(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11964 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11965 | if (PyUnicode_READY(self) == -1) |
| 11966 | return -1; |
| 11967 | return PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11968 | } |
| 11969 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11970 | PyDoc_STRVAR(ljust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11971 | "S.ljust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11972 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 11973 | 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] | 11974 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11975 | |
| 11976 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11977 | unicode_ljust(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11978 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11979 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11980 | Py_UCS4 fillchar = ' '; |
| 11981 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11982 | if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11983 | return NULL; |
| 11984 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 11985 | if (PyUnicode_READY(self) == -1) |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11986 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11987 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11988 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 11989 | return unicode_result_unchanged(self); |
| 11990 | |
| 11991 | return pad(self, 0, width - PyUnicode_GET_LENGTH(self), fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11992 | } |
| 11993 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11994 | PyDoc_STRVAR(lower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11995 | "S.lower() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11996 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11997 | Return a copy of the string S converted to lowercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11998 | |
| 11999 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12000 | unicode_lower(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12001 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 12002 | if (PyUnicode_READY(self) == -1) |
| 12003 | return NULL; |
| 12004 | if (PyUnicode_IS_ASCII(self)) |
| 12005 | return ascii_upper_or_lower(self, 1); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 12006 | return case_operation(self, do_lower); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12007 | } |
| 12008 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12009 | #define LEFTSTRIP 0 |
| 12010 | #define RIGHTSTRIP 1 |
| 12011 | #define BOTHSTRIP 2 |
| 12012 | |
| 12013 | /* Arrays indexed by above */ |
| 12014 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 12015 | |
| 12016 | #define STRIPNAME(i) (stripformat[i]+3) |
| 12017 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12018 | /* externally visible for str.strip(unicode) */ |
| 12019 | PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12020 | _PyUnicode_XStrip(PyObject *self, int striptype, PyObject *sepobj) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12021 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12022 | void *data; |
| 12023 | int kind; |
| 12024 | Py_ssize_t i, j, len; |
| 12025 | BLOOM_MASK sepmask; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12026 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12027 | if (PyUnicode_READY(self) == -1 || PyUnicode_READY(sepobj) == -1) |
| 12028 | return NULL; |
| 12029 | |
| 12030 | kind = PyUnicode_KIND(self); |
| 12031 | data = PyUnicode_DATA(self); |
| 12032 | len = PyUnicode_GET_LENGTH(self); |
| 12033 | sepmask = make_bloom_mask(PyUnicode_KIND(sepobj), |
| 12034 | PyUnicode_DATA(sepobj), |
| 12035 | PyUnicode_GET_LENGTH(sepobj)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12036 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12037 | i = 0; |
| 12038 | if (striptype != RIGHTSTRIP) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12039 | while (i < len && |
| 12040 | BLOOM_MEMBER(sepmask, PyUnicode_READ(kind, data, i), sepobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12041 | i++; |
| 12042 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12043 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12044 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12045 | j = len; |
| 12046 | if (striptype != LEFTSTRIP) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12047 | do { |
| 12048 | j--; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12049 | } while (j >= i && |
| 12050 | BLOOM_MEMBER(sepmask, PyUnicode_READ(kind, data, j), sepobj)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12051 | j++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12052 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12053 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12054 | return PyUnicode_Substring(self, i, j); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12055 | } |
| 12056 | |
| 12057 | PyObject* |
| 12058 | PyUnicode_Substring(PyObject *self, Py_ssize_t start, Py_ssize_t end) |
| 12059 | { |
| 12060 | unsigned char *data; |
| 12061 | int kind; |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 12062 | Py_ssize_t length; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12063 | |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 12064 | if (PyUnicode_READY(self) == -1) |
| 12065 | return NULL; |
| 12066 | |
Victor Stinner | 684d5fd | 2012-05-03 02:32:34 +0200 | [diff] [blame] | 12067 | length = PyUnicode_GET_LENGTH(self); |
| 12068 | end = Py_MIN(end, length); |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 12069 | |
Victor Stinner | 684d5fd | 2012-05-03 02:32:34 +0200 | [diff] [blame] | 12070 | if (start == 0 && end == length) |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12071 | return unicode_result_unchanged(self); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12072 | |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 12073 | if (start < 0 || end < 0) { |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 12074 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 12075 | return NULL; |
| 12076 | } |
Victor Stinner | 684d5fd | 2012-05-03 02:32:34 +0200 | [diff] [blame] | 12077 | if (start >= length || end < start) { |
Victor Stinner | 3a7f7977 | 2012-05-03 03:36:40 +0200 | [diff] [blame] | 12078 | Py_INCREF(unicode_empty); |
| 12079 | return unicode_empty; |
Victor Stinner | 684d5fd | 2012-05-03 02:32:34 +0200 | [diff] [blame] | 12080 | } |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 12081 | |
Victor Stinner | 684d5fd | 2012-05-03 02:32:34 +0200 | [diff] [blame] | 12082 | length = end - start; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 12083 | if (PyUnicode_IS_ASCII(self)) { |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 12084 | data = PyUnicode_1BYTE_DATA(self); |
| 12085 | return unicode_fromascii(data + start, length); |
| 12086 | } |
| 12087 | else { |
| 12088 | kind = PyUnicode_KIND(self); |
| 12089 | data = PyUnicode_1BYTE_DATA(self); |
| 12090 | return PyUnicode_FromKindAndData(kind, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 12091 | data + kind * start, |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 12092 | length); |
| 12093 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12094 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12095 | |
| 12096 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12097 | do_strip(PyObject *self, int striptype) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12098 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12099 | int kind; |
| 12100 | void *data; |
| 12101 | Py_ssize_t len, i, j; |
| 12102 | |
| 12103 | if (PyUnicode_READY(self) == -1) |
| 12104 | return NULL; |
| 12105 | |
| 12106 | kind = PyUnicode_KIND(self); |
| 12107 | data = PyUnicode_DATA(self); |
| 12108 | len = PyUnicode_GET_LENGTH(self); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12109 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12110 | i = 0; |
| 12111 | if (striptype != RIGHTSTRIP) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12112 | while (i < len && Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, i))) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12113 | i++; |
| 12114 | } |
| 12115 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12116 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12117 | j = len; |
| 12118 | if (striptype != LEFTSTRIP) { |
| 12119 | do { |
| 12120 | j--; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12121 | } while (j >= i && Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, j))); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12122 | j++; |
| 12123 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12124 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12125 | return PyUnicode_Substring(self, i, j); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12126 | } |
| 12127 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12128 | |
| 12129 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12130 | do_argstrip(PyObject *self, int striptype, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12131 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12132 | PyObject *sep = NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12133 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12134 | if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) |
| 12135 | return NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12136 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12137 | if (sep != NULL && sep != Py_None) { |
| 12138 | if (PyUnicode_Check(sep)) |
| 12139 | return _PyUnicode_XStrip(self, striptype, sep); |
| 12140 | else { |
| 12141 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12142 | "%s arg must be None or str", |
| 12143 | STRIPNAME(striptype)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12144 | return NULL; |
| 12145 | } |
| 12146 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12147 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12148 | return do_strip(self, striptype); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12149 | } |
| 12150 | |
| 12151 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12152 | PyDoc_STRVAR(strip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12153 | "S.strip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12154 | \n\ |
| 12155 | Return a copy of the string S with leading and trailing\n\ |
| 12156 | whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12157 | 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] | 12158 | |
| 12159 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12160 | unicode_strip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12161 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12162 | if (PyTuple_GET_SIZE(args) == 0) |
| 12163 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 12164 | else |
| 12165 | return do_argstrip(self, BOTHSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12166 | } |
| 12167 | |
| 12168 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12169 | PyDoc_STRVAR(lstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12170 | "S.lstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12171 | \n\ |
| 12172 | Return a copy of the string S with leading whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12173 | 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] | 12174 | |
| 12175 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12176 | unicode_lstrip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12177 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12178 | if (PyTuple_GET_SIZE(args) == 0) |
| 12179 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 12180 | else |
| 12181 | return do_argstrip(self, LEFTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12182 | } |
| 12183 | |
| 12184 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12185 | PyDoc_STRVAR(rstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12186 | "S.rstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12187 | \n\ |
| 12188 | Return a copy of the string S with trailing whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12189 | 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] | 12190 | |
| 12191 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12192 | unicode_rstrip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12193 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12194 | if (PyTuple_GET_SIZE(args) == 0) |
| 12195 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 12196 | else |
| 12197 | return do_argstrip(self, RIGHTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12198 | } |
| 12199 | |
| 12200 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12201 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12202 | unicode_repeat(PyObject *str, Py_ssize_t len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12203 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12204 | PyObject *u; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12205 | Py_ssize_t nchars, n; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12206 | |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 12207 | if (len < 1) { |
| 12208 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 12209 | return unicode_empty; |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 12210 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12211 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12212 | /* no repeat, return original string */ |
| 12213 | if (len == 1) |
| 12214 | return unicode_result_unchanged(str); |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 12215 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 12216 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12217 | return NULL; |
| 12218 | |
Victor Stinner | c759f3e | 2011-10-01 03:09:58 +0200 | [diff] [blame] | 12219 | if (PyUnicode_GET_LENGTH(str) > PY_SSIZE_T_MAX / len) { |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 12220 | PyErr_SetString(PyExc_OverflowError, |
| 12221 | "repeated string is too long"); |
| 12222 | return NULL; |
| 12223 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12224 | nchars = len * PyUnicode_GET_LENGTH(str); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 12225 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12226 | u = PyUnicode_New(nchars, PyUnicode_MAX_CHAR_VALUE(str)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12227 | if (!u) |
| 12228 | return NULL; |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 12229 | assert(PyUnicode_KIND(u) == PyUnicode_KIND(str)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12230 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12231 | if (PyUnicode_GET_LENGTH(str) == 1) { |
| 12232 | const int kind = PyUnicode_KIND(str); |
| 12233 | const Py_UCS4 fill_char = PyUnicode_READ(kind, PyUnicode_DATA(str), 0); |
Victor Stinner | 73f53b5 | 2011-12-18 03:26:31 +0100 | [diff] [blame] | 12234 | if (kind == PyUnicode_1BYTE_KIND) { |
| 12235 | void *to = PyUnicode_DATA(u); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 12236 | memset(to, (unsigned char)fill_char, len); |
Victor Stinner | 73f53b5 | 2011-12-18 03:26:31 +0100 | [diff] [blame] | 12237 | } |
| 12238 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 12239 | Py_UCS2 *ucs2 = PyUnicode_2BYTE_DATA(u); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 12240 | for (n = 0; n < len; ++n) |
Victor Stinner | 73f53b5 | 2011-12-18 03:26:31 +0100 | [diff] [blame] | 12241 | ucs2[n] = fill_char; |
| 12242 | } else { |
| 12243 | Py_UCS4 *ucs4 = PyUnicode_4BYTE_DATA(u); |
| 12244 | assert(kind == PyUnicode_4BYTE_KIND); |
| 12245 | for (n = 0; n < len; ++n) |
| 12246 | ucs4[n] = fill_char; |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 12247 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12248 | } |
| 12249 | else { |
| 12250 | /* number of characters copied this far */ |
| 12251 | Py_ssize_t done = PyUnicode_GET_LENGTH(str); |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 12252 | const Py_ssize_t char_size = PyUnicode_KIND(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12253 | char *to = (char *) PyUnicode_DATA(u); |
| 12254 | Py_MEMCPY(to, PyUnicode_DATA(str), |
| 12255 | PyUnicode_GET_LENGTH(str) * char_size); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12256 | while (done < nchars) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12257 | n = (done <= nchars-done) ? done : nchars-done; |
| 12258 | Py_MEMCPY(to + (done * char_size), to, n * char_size); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12259 | done += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12260 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12261 | } |
| 12262 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12263 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12264 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12265 | } |
| 12266 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12267 | PyObject * |
| 12268 | PyUnicode_Replace(PyObject *obj, |
| 12269 | PyObject *subobj, |
| 12270 | PyObject *replobj, |
| 12271 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12272 | { |
| 12273 | PyObject *self; |
| 12274 | PyObject *str1; |
| 12275 | PyObject *str2; |
| 12276 | PyObject *result; |
| 12277 | |
| 12278 | self = PyUnicode_FromObject(obj); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12279 | if (self == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12280 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12281 | str1 = PyUnicode_FromObject(subobj); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12282 | if (str1 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12283 | Py_DECREF(self); |
| 12284 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12285 | } |
| 12286 | str2 = PyUnicode_FromObject(replobj); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12287 | if (str2 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12288 | Py_DECREF(self); |
| 12289 | Py_DECREF(str1); |
| 12290 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12291 | } |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12292 | if (PyUnicode_READY(self) == -1 || |
| 12293 | PyUnicode_READY(str1) == -1 || |
| 12294 | PyUnicode_READY(str2) == -1) |
| 12295 | result = NULL; |
| 12296 | else |
| 12297 | result = replace(self, str1, str2, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12298 | Py_DECREF(self); |
| 12299 | Py_DECREF(str1); |
| 12300 | Py_DECREF(str2); |
| 12301 | return result; |
| 12302 | } |
| 12303 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12304 | PyDoc_STRVAR(replace__doc__, |
Ezio Melotti | c1897e7 | 2010-06-26 18:50:39 +0000 | [diff] [blame] | 12305 | "S.replace(old, new[, count]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12306 | \n\ |
| 12307 | Return a copy of S with all occurrences of substring\n\ |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 12308 | old replaced by new. If the optional argument count is\n\ |
| 12309 | given, only the first count occurrences are replaced."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12310 | |
| 12311 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12312 | unicode_replace(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12313 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12314 | PyObject *str1; |
| 12315 | PyObject *str2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12316 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12317 | PyObject *result; |
| 12318 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12319 | if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12320 | return NULL; |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12321 | if (PyUnicode_READY(self) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12322 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12323 | str1 = PyUnicode_FromObject(str1); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12324 | if (str1 == NULL) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12325 | return NULL; |
| 12326 | str2 = PyUnicode_FromObject(str2); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12327 | if (str2 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12328 | Py_DECREF(str1); |
| 12329 | return NULL; |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 12330 | } |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12331 | if (PyUnicode_READY(str1) == -1 || PyUnicode_READY(str2) == -1) |
| 12332 | result = NULL; |
| 12333 | else |
| 12334 | result = replace(self, str1, str2, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12335 | |
| 12336 | Py_DECREF(str1); |
| 12337 | Py_DECREF(str2); |
| 12338 | return result; |
| 12339 | } |
| 12340 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12341 | static PyObject * |
| 12342 | unicode_repr(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12343 | { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12344 | PyObject *repr; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12345 | Py_ssize_t isize; |
| 12346 | Py_ssize_t osize, squote, dquote, i, o; |
| 12347 | Py_UCS4 max, quote; |
| 12348 | int ikind, okind; |
| 12349 | void *idata, *odata; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12350 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12351 | if (PyUnicode_READY(unicode) == -1) |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12352 | return NULL; |
| 12353 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12354 | isize = PyUnicode_GET_LENGTH(unicode); |
| 12355 | idata = PyUnicode_DATA(unicode); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12356 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12357 | /* Compute length of output, quote characters, and |
| 12358 | maximum character */ |
| 12359 | osize = 2; /* quotes */ |
| 12360 | max = 127; |
| 12361 | squote = dquote = 0; |
| 12362 | ikind = PyUnicode_KIND(unicode); |
| 12363 | for (i = 0; i < isize; i++) { |
| 12364 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
| 12365 | switch (ch) { |
| 12366 | case '\'': squote++; osize++; break; |
| 12367 | case '"': dquote++; osize++; break; |
| 12368 | case '\\': case '\t': case '\r': case '\n': |
| 12369 | osize += 2; break; |
| 12370 | default: |
| 12371 | /* Fast-path ASCII */ |
| 12372 | if (ch < ' ' || ch == 0x7f) |
| 12373 | osize += 4; /* \xHH */ |
| 12374 | else if (ch < 0x7f) |
| 12375 | osize++; |
| 12376 | else if (Py_UNICODE_ISPRINTABLE(ch)) { |
| 12377 | osize++; |
| 12378 | max = ch > max ? ch : max; |
| 12379 | } |
| 12380 | else if (ch < 0x100) |
| 12381 | osize += 4; /* \xHH */ |
| 12382 | else if (ch < 0x10000) |
| 12383 | osize += 6; /* \uHHHH */ |
| 12384 | else |
| 12385 | osize += 10; /* \uHHHHHHHH */ |
| 12386 | } |
| 12387 | } |
| 12388 | |
| 12389 | quote = '\''; |
| 12390 | if (squote) { |
| 12391 | if (dquote) |
| 12392 | /* Both squote and dquote present. Use squote, |
| 12393 | and escape them */ |
| 12394 | osize += squote; |
| 12395 | else |
| 12396 | quote = '"'; |
| 12397 | } |
| 12398 | |
| 12399 | repr = PyUnicode_New(osize, max); |
| 12400 | if (repr == NULL) |
| 12401 | return NULL; |
| 12402 | okind = PyUnicode_KIND(repr); |
| 12403 | odata = PyUnicode_DATA(repr); |
| 12404 | |
| 12405 | PyUnicode_WRITE(okind, odata, 0, quote); |
| 12406 | PyUnicode_WRITE(okind, odata, osize-1, quote); |
| 12407 | |
| 12408 | for (i = 0, o = 1; i < isize; i++) { |
| 12409 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12410 | |
| 12411 | /* Escape quotes and backslashes */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12412 | if ((ch == quote) || (ch == '\\')) { |
| 12413 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12414 | PyUnicode_WRITE(okind, odata, o++, ch); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12415 | continue; |
| 12416 | } |
| 12417 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12418 | /* Map special whitespace to '\t', \n', '\r' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12419 | if (ch == '\t') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12420 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12421 | PyUnicode_WRITE(okind, odata, o++, 't'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12422 | } |
| 12423 | else if (ch == '\n') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12424 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12425 | PyUnicode_WRITE(okind, odata, o++, 'n'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12426 | } |
| 12427 | else if (ch == '\r') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12428 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12429 | PyUnicode_WRITE(okind, odata, o++, 'r'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12430 | } |
| 12431 | |
| 12432 | /* Map non-printable US ASCII to '\xhh' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12433 | else if (ch < ' ' || ch == 0x7F) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12434 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12435 | PyUnicode_WRITE(okind, odata, o++, 'x'); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 12436 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0x000F]); |
| 12437 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0x000F]); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12438 | } |
| 12439 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12440 | /* Copy ASCII characters as-is */ |
| 12441 | else if (ch < 0x7F) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12442 | PyUnicode_WRITE(okind, odata, o++, ch); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12443 | } |
| 12444 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12445 | /* Non-ASCII characters */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12446 | else { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12447 | /* Map Unicode whitespace and control characters |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12448 | (categories Z* and C* except ASCII space) |
| 12449 | */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12450 | if (!Py_UNICODE_ISPRINTABLE(ch)) { |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12451 | /* Map 8-bit characters to '\xhh' */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12452 | if (ch <= 0xff) { |
| 12453 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12454 | PyUnicode_WRITE(okind, odata, o++, 'x'); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 12455 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0x000F]); |
| 12456 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0x000F]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12457 | } |
| 12458 | /* Map 21-bit characters to '\U00xxxxxx' */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12459 | else if (ch >= 0x10000) { |
| 12460 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12461 | PyUnicode_WRITE(okind, odata, o++, 'U'); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 12462 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 28) & 0xF]); |
| 12463 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 24) & 0xF]); |
| 12464 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 20) & 0xF]); |
| 12465 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 16) & 0xF]); |
| 12466 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 12) & 0xF]); |
| 12467 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 8) & 0xF]); |
| 12468 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0xF]); |
| 12469 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0xF]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12470 | } |
| 12471 | /* Map 16-bit characters to '\uxxxx' */ |
| 12472 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12473 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12474 | PyUnicode_WRITE(okind, odata, o++, 'u'); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 12475 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 12) & 0xF]); |
| 12476 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 8) & 0xF]); |
| 12477 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0xF]); |
| 12478 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0xF]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12479 | } |
| 12480 | } |
| 12481 | /* Copy characters as-is */ |
| 12482 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12483 | PyUnicode_WRITE(okind, odata, o++, ch); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12484 | } |
| 12485 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12486 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12487 | /* Closing quote already added at the beginning */ |
Victor Stinner | 05d1189 | 2011-10-06 01:13:58 +0200 | [diff] [blame] | 12488 | assert(_PyUnicode_CheckConsistency(repr, 1)); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12489 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12490 | } |
| 12491 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12492 | PyDoc_STRVAR(rfind__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12493 | "S.rfind(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12494 | \n\ |
| 12495 | Return the highest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 12496 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12497 | arguments start and end are interpreted as in slice notation.\n\ |
| 12498 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12499 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12500 | |
| 12501 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12502 | unicode_rfind(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12503 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12504 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 12505 | Py_ssize_t start; |
| 12506 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12507 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12508 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12509 | if (!stringlib_parse_args_finds_unicode("rfind", args, &substring, |
| 12510 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12511 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12512 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12513 | if (PyUnicode_READY(self) == -1) |
| 12514 | return NULL; |
| 12515 | if (PyUnicode_READY(substring) == -1) |
| 12516 | return NULL; |
| 12517 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12518 | result = any_find_slice(-1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12519 | |
| 12520 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12521 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12522 | if (result == -2) |
| 12523 | return NULL; |
| 12524 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12525 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12526 | } |
| 12527 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12528 | PyDoc_STRVAR(rindex__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12529 | "S.rindex(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12530 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12531 | 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] | 12532 | |
| 12533 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12534 | unicode_rindex(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12535 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12536 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 12537 | Py_ssize_t start; |
| 12538 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12539 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12540 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12541 | if (!stringlib_parse_args_finds_unicode("rindex", args, &substring, |
| 12542 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12543 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12544 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12545 | if (PyUnicode_READY(self) == -1) |
| 12546 | return NULL; |
| 12547 | if (PyUnicode_READY(substring) == -1) |
| 12548 | return NULL; |
| 12549 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12550 | result = any_find_slice(-1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12551 | |
| 12552 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12553 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12554 | if (result == -2) |
| 12555 | return NULL; |
| 12556 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12557 | if (result < 0) { |
| 12558 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 12559 | return NULL; |
| 12560 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12561 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12562 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12563 | } |
| 12564 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12565 | PyDoc_STRVAR(rjust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12566 | "S.rjust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12567 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 12568 | 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] | 12569 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12570 | |
| 12571 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12572 | unicode_rjust(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12573 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12574 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12575 | Py_UCS4 fillchar = ' '; |
| 12576 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 12577 | 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] | 12578 | return NULL; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 12579 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 12580 | if (PyUnicode_READY(self) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12581 | return NULL; |
| 12582 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12583 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 12584 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12585 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12586 | return pad(self, width - PyUnicode_GET_LENGTH(self), 0, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12587 | } |
| 12588 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12589 | PyObject * |
| 12590 | PyUnicode_Split(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12591 | { |
| 12592 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 12593 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12594 | s = PyUnicode_FromObject(s); |
| 12595 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12596 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12597 | if (sep != NULL) { |
| 12598 | sep = PyUnicode_FromObject(sep); |
| 12599 | if (sep == NULL) { |
| 12600 | Py_DECREF(s); |
| 12601 | return NULL; |
| 12602 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12603 | } |
| 12604 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12605 | result = split(s, sep, maxsplit); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12606 | |
| 12607 | Py_DECREF(s); |
| 12608 | Py_XDECREF(sep); |
| 12609 | return result; |
| 12610 | } |
| 12611 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12612 | PyDoc_STRVAR(split__doc__, |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12613 | "S.split(sep=None, maxsplit=-1) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12614 | \n\ |
| 12615 | Return a list of the words in S, using sep as the\n\ |
| 12616 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 12617 | 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] | 12618 | whitespace string is a separator and empty strings are\n\ |
| 12619 | removed from the result."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12620 | |
| 12621 | static PyObject* |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12622 | unicode_split(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12623 | { |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12624 | static char *kwlist[] = {"sep", "maxsplit", 0}; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12625 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12626 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12627 | |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12628 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|On:split", |
| 12629 | kwlist, &substring, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12630 | return NULL; |
| 12631 | |
| 12632 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12633 | return split(self, NULL, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12634 | else if (PyUnicode_Check(substring)) |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12635 | return split(self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12636 | else |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12637 | return PyUnicode_Split(self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12638 | } |
| 12639 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12640 | PyObject * |
| 12641 | PyUnicode_Partition(PyObject *str_in, PyObject *sep_in) |
| 12642 | { |
| 12643 | PyObject* str_obj; |
| 12644 | PyObject* sep_obj; |
| 12645 | PyObject* out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12646 | int kind1, kind2, kind; |
| 12647 | void *buf1 = NULL, *buf2 = NULL; |
| 12648 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12649 | |
| 12650 | str_obj = PyUnicode_FromObject(str_in); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12651 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12652 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12653 | sep_obj = PyUnicode_FromObject(sep_in); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12654 | if (!sep_obj) { |
| 12655 | Py_DECREF(str_obj); |
| 12656 | return NULL; |
| 12657 | } |
| 12658 | if (PyUnicode_READY(sep_obj) == -1 || PyUnicode_READY(str_obj) == -1) { |
| 12659 | Py_DECREF(sep_obj); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12660 | Py_DECREF(str_obj); |
| 12661 | return NULL; |
| 12662 | } |
| 12663 | |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12664 | kind1 = PyUnicode_KIND(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12665 | kind2 = PyUnicode_KIND(sep_obj); |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12666 | kind = Py_MAX(kind1, kind2); |
| 12667 | buf1 = PyUnicode_DATA(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12668 | if (kind1 != kind) |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12669 | buf1 = _PyUnicode_AsKind(str_obj, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12670 | if (!buf1) |
| 12671 | goto onError; |
| 12672 | buf2 = PyUnicode_DATA(sep_obj); |
| 12673 | if (kind2 != kind) |
| 12674 | buf2 = _PyUnicode_AsKind(sep_obj, kind); |
| 12675 | if (!buf2) |
| 12676 | goto onError; |
| 12677 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 12678 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
| 12679 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 12680 | switch (PyUnicode_KIND(str_obj)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12681 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 12682 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sep_obj)) |
| 12683 | out = asciilib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12684 | else |
| 12685 | 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] | 12686 | break; |
| 12687 | case PyUnicode_2BYTE_KIND: |
| 12688 | out = ucs2lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12689 | break; |
| 12690 | case PyUnicode_4BYTE_KIND: |
| 12691 | out = ucs4lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12692 | break; |
| 12693 | default: |
| 12694 | assert(0); |
| 12695 | out = 0; |
| 12696 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12697 | |
| 12698 | Py_DECREF(sep_obj); |
| 12699 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12700 | if (kind1 != kind) |
| 12701 | PyMem_Free(buf1); |
| 12702 | if (kind2 != kind) |
| 12703 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12704 | |
| 12705 | return out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12706 | onError: |
| 12707 | Py_DECREF(sep_obj); |
| 12708 | Py_DECREF(str_obj); |
| 12709 | if (kind1 != kind && buf1) |
| 12710 | PyMem_Free(buf1); |
| 12711 | if (kind2 != kind && buf2) |
| 12712 | PyMem_Free(buf2); |
| 12713 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12714 | } |
| 12715 | |
| 12716 | |
| 12717 | PyObject * |
| 12718 | PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in) |
| 12719 | { |
| 12720 | PyObject* str_obj; |
| 12721 | PyObject* sep_obj; |
| 12722 | PyObject* out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12723 | int kind1, kind2, kind; |
| 12724 | void *buf1 = NULL, *buf2 = NULL; |
| 12725 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12726 | |
| 12727 | str_obj = PyUnicode_FromObject(str_in); |
| 12728 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12729 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12730 | sep_obj = PyUnicode_FromObject(sep_in); |
| 12731 | if (!sep_obj) { |
| 12732 | Py_DECREF(str_obj); |
| 12733 | return NULL; |
| 12734 | } |
| 12735 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12736 | kind1 = PyUnicode_KIND(str_in); |
| 12737 | kind2 = PyUnicode_KIND(sep_obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 12738 | kind = Py_MAX(kind1, kind2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12739 | buf1 = PyUnicode_DATA(str_in); |
| 12740 | if (kind1 != kind) |
| 12741 | buf1 = _PyUnicode_AsKind(str_in, kind); |
| 12742 | if (!buf1) |
| 12743 | goto onError; |
| 12744 | buf2 = PyUnicode_DATA(sep_obj); |
| 12745 | if (kind2 != kind) |
| 12746 | buf2 = _PyUnicode_AsKind(sep_obj, kind); |
| 12747 | if (!buf2) |
| 12748 | goto onError; |
| 12749 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 12750 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
| 12751 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 12752 | switch (PyUnicode_KIND(str_in)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12753 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 12754 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sep_obj)) |
| 12755 | out = asciilib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12756 | else |
| 12757 | 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] | 12758 | break; |
| 12759 | case PyUnicode_2BYTE_KIND: |
| 12760 | out = ucs2lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12761 | break; |
| 12762 | case PyUnicode_4BYTE_KIND: |
| 12763 | out = ucs4lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12764 | break; |
| 12765 | default: |
| 12766 | assert(0); |
| 12767 | out = 0; |
| 12768 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12769 | |
| 12770 | Py_DECREF(sep_obj); |
| 12771 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12772 | if (kind1 != kind) |
| 12773 | PyMem_Free(buf1); |
| 12774 | if (kind2 != kind) |
| 12775 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12776 | |
| 12777 | return out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12778 | onError: |
| 12779 | Py_DECREF(sep_obj); |
| 12780 | Py_DECREF(str_obj); |
| 12781 | if (kind1 != kind && buf1) |
| 12782 | PyMem_Free(buf1); |
| 12783 | if (kind2 != kind && buf2) |
| 12784 | PyMem_Free(buf2); |
| 12785 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12786 | } |
| 12787 | |
| 12788 | PyDoc_STRVAR(partition__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12789 | "S.partition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12790 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 12791 | 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] | 12792 | 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] | 12793 | found, return S and two empty strings."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12794 | |
| 12795 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12796 | unicode_partition(PyObject *self, PyObject *separator) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12797 | { |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12798 | return PyUnicode_Partition(self, separator); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12799 | } |
| 12800 | |
| 12801 | PyDoc_STRVAR(rpartition__doc__, |
Ezio Melotti | 5b2b242 | 2010-01-25 11:58:28 +0000 | [diff] [blame] | 12802 | "S.rpartition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12803 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 12804 | 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] | 12805 | 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] | 12806 | separator is not found, return two empty strings and S."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12807 | |
| 12808 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12809 | unicode_rpartition(PyObject *self, PyObject *separator) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12810 | { |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12811 | return PyUnicode_RPartition(self, separator); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12812 | } |
| 12813 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12814 | PyObject * |
| 12815 | PyUnicode_RSplit(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12816 | { |
| 12817 | PyObject *result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12818 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12819 | s = PyUnicode_FromObject(s); |
| 12820 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12821 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12822 | if (sep != NULL) { |
| 12823 | sep = PyUnicode_FromObject(sep); |
| 12824 | if (sep == NULL) { |
| 12825 | Py_DECREF(s); |
| 12826 | return NULL; |
| 12827 | } |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12828 | } |
| 12829 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12830 | result = rsplit(s, sep, maxsplit); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12831 | |
| 12832 | Py_DECREF(s); |
| 12833 | Py_XDECREF(sep); |
| 12834 | return result; |
| 12835 | } |
| 12836 | |
| 12837 | PyDoc_STRVAR(rsplit__doc__, |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12838 | "S.rsplit(sep=None, maxsplit=-1) -> list of strings\n\ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12839 | \n\ |
| 12840 | Return a list of the words in S, using sep as the\n\ |
| 12841 | delimiter string, starting at the end of the string and\n\ |
| 12842 | working to the front. If maxsplit is given, at most maxsplit\n\ |
| 12843 | splits are done. If sep is not specified, any whitespace string\n\ |
| 12844 | is a separator."); |
| 12845 | |
| 12846 | static PyObject* |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12847 | unicode_rsplit(PyObject *self, PyObject *args, PyObject *kwds) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12848 | { |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12849 | static char *kwlist[] = {"sep", "maxsplit", 0}; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12850 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12851 | Py_ssize_t maxcount = -1; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12852 | |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12853 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|On:rsplit", |
| 12854 | kwlist, &substring, &maxcount)) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12855 | return NULL; |
| 12856 | |
| 12857 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12858 | return rsplit(self, NULL, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12859 | else if (PyUnicode_Check(substring)) |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12860 | return rsplit(self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12861 | else |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12862 | return PyUnicode_RSplit(self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12863 | } |
| 12864 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12865 | PyDoc_STRVAR(splitlines__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12866 | "S.splitlines([keepends]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12867 | \n\ |
| 12868 | 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] | 12869 | 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] | 12870 | is given and true."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12871 | |
| 12872 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12873 | unicode_splitlines(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12874 | { |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12875 | static char *kwlist[] = {"keepends", 0}; |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 12876 | int keepends = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12877 | |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12878 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|i:splitlines", |
| 12879 | kwlist, &keepends)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12880 | return NULL; |
| 12881 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12882 | return PyUnicode_Splitlines(self, keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12883 | } |
| 12884 | |
| 12885 | static |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 12886 | PyObject *unicode_str(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12887 | { |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12888 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12889 | } |
| 12890 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12891 | PyDoc_STRVAR(swapcase__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12892 | "S.swapcase() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12893 | \n\ |
| 12894 | 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] | 12895 | and vice versa."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12896 | |
| 12897 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12898 | unicode_swapcase(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12899 | { |
Benjamin Peterson | eea4846 | 2012-01-16 14:28:50 -0500 | [diff] [blame] | 12900 | if (PyUnicode_READY(self) == -1) |
| 12901 | return NULL; |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 12902 | return case_operation(self, do_swapcase); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12903 | } |
| 12904 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12905 | PyDoc_STRVAR(maketrans__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12906 | "str.maketrans(x[, y[, z]]) -> dict (static method)\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12907 | \n\ |
| 12908 | Return a translation table usable for str.translate().\n\ |
| 12909 | If there is only one argument, it must be a dictionary mapping Unicode\n\ |
| 12910 | ordinals (integers) or characters to Unicode ordinals, strings or None.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12911 | Character keys will be then converted to ordinals.\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12912 | If there are two arguments, they must be strings of equal length, and\n\ |
| 12913 | in the resulting dictionary, each character in x will be mapped to the\n\ |
| 12914 | character at the same position in y. If there is a third argument, it\n\ |
| 12915 | must be a string, whose characters will be mapped to None in the result."); |
| 12916 | |
| 12917 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12918 | unicode_maketrans(PyObject *null, PyObject *args) |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12919 | { |
| 12920 | PyObject *x, *y = NULL, *z = NULL; |
| 12921 | PyObject *new = NULL, *key, *value; |
| 12922 | Py_ssize_t i = 0; |
| 12923 | int res; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12924 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12925 | if (!PyArg_ParseTuple(args, "O|UU:maketrans", &x, &y, &z)) |
| 12926 | return NULL; |
| 12927 | new = PyDict_New(); |
| 12928 | if (!new) |
| 12929 | return NULL; |
| 12930 | if (y != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12931 | int x_kind, y_kind, z_kind; |
| 12932 | void *x_data, *y_data, *z_data; |
| 12933 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12934 | /* x must be a string too, of equal length */ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12935 | if (!PyUnicode_Check(x)) { |
| 12936 | PyErr_SetString(PyExc_TypeError, "first maketrans argument must " |
| 12937 | "be a string if there is a second argument"); |
| 12938 | goto err; |
| 12939 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12940 | if (PyUnicode_GET_LENGTH(x) != PyUnicode_GET_LENGTH(y)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12941 | PyErr_SetString(PyExc_ValueError, "the first two maketrans " |
| 12942 | "arguments must have equal length"); |
| 12943 | goto err; |
| 12944 | } |
| 12945 | /* 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] | 12946 | x_kind = PyUnicode_KIND(x); |
| 12947 | y_kind = PyUnicode_KIND(y); |
| 12948 | x_data = PyUnicode_DATA(x); |
| 12949 | y_data = PyUnicode_DATA(y); |
| 12950 | for (i = 0; i < PyUnicode_GET_LENGTH(x); i++) { |
| 12951 | key = PyLong_FromLong(PyUnicode_READ(x_kind, x_data, i)); |
Benjamin Peterson | 53aa1d7 | 2011-12-20 13:29:45 -0600 | [diff] [blame] | 12952 | if (!key) |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12953 | goto err; |
Benjamin Peterson | 822c790 | 2011-12-20 13:32:50 -0600 | [diff] [blame] | 12954 | value = PyLong_FromLong(PyUnicode_READ(y_kind, y_data, i)); |
Benjamin Peterson | 53aa1d7 | 2011-12-20 13:29:45 -0600 | [diff] [blame] | 12955 | if (!value) { |
| 12956 | Py_DECREF(key); |
| 12957 | goto err; |
| 12958 | } |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12959 | res = PyDict_SetItem(new, key, value); |
| 12960 | Py_DECREF(key); |
| 12961 | Py_DECREF(value); |
| 12962 | if (res < 0) |
| 12963 | goto err; |
| 12964 | } |
| 12965 | /* create entries for deleting chars in z */ |
| 12966 | if (z != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12967 | z_kind = PyUnicode_KIND(z); |
| 12968 | z_data = PyUnicode_DATA(z); |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 12969 | for (i = 0; i < PyUnicode_GET_LENGTH(z); i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12970 | key = PyLong_FromLong(PyUnicode_READ(z_kind, z_data, i)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12971 | if (!key) |
| 12972 | goto err; |
| 12973 | res = PyDict_SetItem(new, key, Py_None); |
| 12974 | Py_DECREF(key); |
| 12975 | if (res < 0) |
| 12976 | goto err; |
| 12977 | } |
| 12978 | } |
| 12979 | } else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12980 | int kind; |
| 12981 | void *data; |
| 12982 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12983 | /* x must be a dict */ |
Raymond Hettinger | 3ad0576 | 2009-05-29 22:11:22 +0000 | [diff] [blame] | 12984 | if (!PyDict_CheckExact(x)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12985 | PyErr_SetString(PyExc_TypeError, "if you give only one argument " |
| 12986 | "to maketrans it must be a dict"); |
| 12987 | goto err; |
| 12988 | } |
| 12989 | /* copy entries into the new dict, converting string keys to int keys */ |
| 12990 | while (PyDict_Next(x, &i, &key, &value)) { |
| 12991 | if (PyUnicode_Check(key)) { |
| 12992 | /* convert string keys to integer keys */ |
| 12993 | PyObject *newkey; |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 12994 | if (PyUnicode_GET_LENGTH(key) != 1) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12995 | PyErr_SetString(PyExc_ValueError, "string keys in translate " |
| 12996 | "table must be of length 1"); |
| 12997 | goto err; |
| 12998 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12999 | kind = PyUnicode_KIND(key); |
| 13000 | data = PyUnicode_DATA(key); |
| 13001 | newkey = PyLong_FromLong(PyUnicode_READ(kind, data, 0)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 13002 | if (!newkey) |
| 13003 | goto err; |
| 13004 | res = PyDict_SetItem(new, newkey, value); |
| 13005 | Py_DECREF(newkey); |
| 13006 | if (res < 0) |
| 13007 | goto err; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 13008 | } else if (PyLong_Check(key)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 13009 | /* just keep integer keys */ |
| 13010 | if (PyDict_SetItem(new, key, value) < 0) |
| 13011 | goto err; |
| 13012 | } else { |
| 13013 | PyErr_SetString(PyExc_TypeError, "keys in translate table must " |
| 13014 | "be strings or integers"); |
| 13015 | goto err; |
| 13016 | } |
| 13017 | } |
| 13018 | } |
| 13019 | return new; |
| 13020 | err: |
| 13021 | Py_DECREF(new); |
| 13022 | return NULL; |
| 13023 | } |
| 13024 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 13025 | PyDoc_STRVAR(translate__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13026 | "S.translate(table) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13027 | \n\ |
| 13028 | Return a copy of the string S, where all characters have been mapped\n\ |
| 13029 | through the given translation table, which must be a mapping of\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 13030 | Unicode ordinals to Unicode ordinals, strings, or None.\n\ |
Walter Dörwald | 5c1ee17 | 2002-09-04 20:31:32 +0000 | [diff] [blame] | 13031 | Unmapped characters are left untouched. Characters mapped to None\n\ |
| 13032 | are deleted."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13033 | |
| 13034 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13035 | unicode_translate(PyObject *self, PyObject *table) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13036 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13037 | return _PyUnicode_TranslateCharmap(self, table, "ignore"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13038 | } |
| 13039 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 13040 | PyDoc_STRVAR(upper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13041 | "S.upper() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13042 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 13043 | Return a copy of S converted to uppercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13044 | |
| 13045 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 13046 | unicode_upper(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13047 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 13048 | if (PyUnicode_READY(self) == -1) |
| 13049 | return NULL; |
| 13050 | if (PyUnicode_IS_ASCII(self)) |
| 13051 | return ascii_upper_or_lower(self, 0); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 13052 | return case_operation(self, do_upper); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13053 | } |
| 13054 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 13055 | PyDoc_STRVAR(zfill__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13056 | "S.zfill(width) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13057 | \n\ |
Benjamin Peterson | 9aa4299 | 2008-09-10 21:57:34 +0000 | [diff] [blame] | 13058 | Pad a numeric string S with zeros on the left, to fill a field\n\ |
| 13059 | of the specified width. The string S is never truncated."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13060 | |
| 13061 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 13062 | unicode_zfill(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13063 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13064 | Py_ssize_t fill; |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 13065 | PyObject *u; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13066 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13067 | int kind; |
| 13068 | void *data; |
| 13069 | Py_UCS4 chr; |
| 13070 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13071 | if (!PyArg_ParseTuple(args, "n:zfill", &width)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13072 | return NULL; |
| 13073 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 13074 | if (PyUnicode_READY(self) == -1) |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 13075 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13076 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 13077 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 13078 | return unicode_result_unchanged(self); |
| 13079 | |
| 13080 | fill = width - PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13081 | |
| 13082 | u = pad(self, fill, 0, '0'); |
| 13083 | |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 13084 | if (u == NULL) |
| 13085 | return NULL; |
| 13086 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13087 | kind = PyUnicode_KIND(u); |
| 13088 | data = PyUnicode_DATA(u); |
| 13089 | chr = PyUnicode_READ(kind, data, fill); |
| 13090 | |
| 13091 | if (chr == '+' || chr == '-') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13092 | /* move sign to beginning of string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13093 | PyUnicode_WRITE(kind, data, 0, chr); |
| 13094 | PyUnicode_WRITE(kind, data, fill, '0'); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13095 | } |
| 13096 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13097 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 13098 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13099 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13100 | |
| 13101 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 13102 | static PyObject * |
| 13103 | unicode__decimal2ascii(PyObject *self) |
| 13104 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13105 | return PyUnicode_TransformDecimalAndSpaceToASCII(self); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 13106 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13107 | #endif |
| 13108 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 13109 | PyDoc_STRVAR(startswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13110 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13111 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 13112 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 13113 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13114 | With optional end, stop comparing S at that position.\n\ |
| 13115 | prefix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13116 | |
| 13117 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13118 | unicode_startswith(PyObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13119 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13120 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13121 | PyObject *subobj; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13122 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13123 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 13124 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13125 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13126 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 13127 | if (!stringlib_parse_args_finds("startswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13128 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13129 | if (PyTuple_Check(subobj)) { |
| 13130 | Py_ssize_t i; |
| 13131 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13132 | substring = PyUnicode_FromObject(PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13133 | if (substring == NULL) |
| 13134 | return NULL; |
| 13135 | result = tailmatch(self, substring, start, end, -1); |
| 13136 | Py_DECREF(substring); |
| 13137 | if (result) { |
| 13138 | Py_RETURN_TRUE; |
| 13139 | } |
| 13140 | } |
| 13141 | /* nothing matched */ |
| 13142 | Py_RETURN_FALSE; |
| 13143 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13144 | substring = PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 13145 | if (substring == NULL) { |
| 13146 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 13147 | PyErr_Format(PyExc_TypeError, "startswith first arg must be str or " |
| 13148 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13149 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 13150 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13151 | result = tailmatch(self, substring, start, end, -1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13152 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13153 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13154 | } |
| 13155 | |
| 13156 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 13157 | PyDoc_STRVAR(endswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13158 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13159 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 13160 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 13161 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13162 | With optional end, stop comparing S at that position.\n\ |
| 13163 | suffix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13164 | |
| 13165 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13166 | unicode_endswith(PyObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13167 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13168 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13169 | PyObject *subobj; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13170 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13171 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 13172 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13173 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13174 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 13175 | if (!stringlib_parse_args_finds("endswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13176 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13177 | if (PyTuple_Check(subobj)) { |
| 13178 | Py_ssize_t i; |
| 13179 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13180 | substring = PyUnicode_FromObject( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13181 | PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13182 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13183 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13184 | result = tailmatch(self, substring, start, end, +1); |
| 13185 | Py_DECREF(substring); |
| 13186 | if (result) { |
| 13187 | Py_RETURN_TRUE; |
| 13188 | } |
| 13189 | } |
| 13190 | Py_RETURN_FALSE; |
| 13191 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13192 | substring = PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 13193 | if (substring == NULL) { |
| 13194 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 13195 | PyErr_Format(PyExc_TypeError, "endswith first arg must be str or " |
| 13196 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13197 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 13198 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13199 | result = tailmatch(self, substring, start, end, +1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13200 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13201 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13202 | } |
| 13203 | |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13204 | typedef struct { |
| 13205 | PyObject *buffer; |
| 13206 | void *data; |
| 13207 | enum PyUnicode_Kind kind; |
| 13208 | Py_UCS4 maxchar; |
| 13209 | Py_ssize_t pos; |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame^] | 13210 | } _PyUnicodeWriter ; |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13211 | |
| 13212 | Py_LOCAL_INLINE(void) |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame^] | 13213 | _PyUnicodeWriter_Update(_PyUnicodeWriter *writer) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13214 | { |
| 13215 | writer->maxchar = PyUnicode_MAX_CHAR_VALUE(writer->buffer); |
| 13216 | writer->data = PyUnicode_DATA(writer->buffer); |
| 13217 | writer->kind = PyUnicode_KIND(writer->buffer); |
| 13218 | } |
| 13219 | |
| 13220 | Py_LOCAL(int) |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame^] | 13221 | _PyUnicodeWriter_Init(_PyUnicodeWriter *writer, |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13222 | Py_ssize_t length, Py_UCS4 maxchar) |
| 13223 | { |
| 13224 | writer->pos = 0; |
| 13225 | writer->buffer = PyUnicode_New(length, maxchar); |
| 13226 | if (writer->buffer == NULL) |
| 13227 | return -1; |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame^] | 13228 | _PyUnicodeWriter_Update(writer); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13229 | return 0; |
| 13230 | } |
| 13231 | |
| 13232 | Py_LOCAL_INLINE(int) |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame^] | 13233 | _PyUnicodeWriter_Prepare(_PyUnicodeWriter *writer, |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13234 | Py_ssize_t length, Py_UCS4 maxchar) |
| 13235 | { |
| 13236 | Py_ssize_t newlen; |
| 13237 | PyObject *newbuffer; |
| 13238 | |
| 13239 | if (length > PY_SSIZE_T_MAX - writer->pos) { |
| 13240 | PyErr_NoMemory(); |
| 13241 | return -1; |
| 13242 | } |
| 13243 | newlen = writer->pos + length; |
| 13244 | |
| 13245 | if (newlen > PyUnicode_GET_LENGTH(writer->buffer)) { |
Victor Stinner | 1068025 | 2012-05-07 23:50:05 +0200 | [diff] [blame] | 13246 | /* overallocate 25% to limit the number of resize */ |
| 13247 | if (newlen <= (PY_SSIZE_T_MAX - newlen / 4)) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13248 | newlen += newlen / 4; |
| 13249 | |
| 13250 | if (maxchar > writer->maxchar) { |
| 13251 | /* resize + widen */ |
| 13252 | newbuffer = PyUnicode_New(newlen, maxchar); |
| 13253 | if (newbuffer == NULL) |
| 13254 | return -1; |
| 13255 | PyUnicode_CopyCharacters(newbuffer, 0, |
| 13256 | writer->buffer, 0, writer->pos); |
| 13257 | Py_DECREF(writer->buffer); |
| 13258 | } |
| 13259 | else { |
| 13260 | newbuffer = resize_compact(writer->buffer, newlen); |
| 13261 | if (newbuffer == NULL) |
| 13262 | return -1; |
| 13263 | } |
| 13264 | writer->buffer = newbuffer; |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame^] | 13265 | _PyUnicodeWriter_Update(writer); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13266 | } |
| 13267 | else if (maxchar > writer->maxchar) { |
| 13268 | if (unicode_widen(&writer->buffer, writer->pos, maxchar) < 0) |
| 13269 | return -1; |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame^] | 13270 | _PyUnicodeWriter_Update(writer); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13271 | } |
| 13272 | return 0; |
| 13273 | } |
| 13274 | |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13275 | Py_LOCAL(PyObject *) |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame^] | 13276 | _PyUnicodeWriter_Finish(_PyUnicodeWriter *writer) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13277 | { |
| 13278 | if (PyUnicode_Resize(&writer->buffer, writer->pos) < 0) { |
| 13279 | Py_DECREF(writer->buffer); |
| 13280 | return NULL; |
| 13281 | } |
Victor Stinner | f59c28c | 2012-05-09 03:24:14 +0200 | [diff] [blame] | 13282 | assert(_PyUnicode_CheckConsistency(writer->buffer, 1)); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13283 | return writer->buffer; |
| 13284 | } |
| 13285 | |
| 13286 | Py_LOCAL(void) |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame^] | 13287 | _PyUnicodeWriter_Dealloc(_PyUnicodeWriter *writer) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13288 | { |
| 13289 | Py_CLEAR(writer->buffer); |
| 13290 | } |
| 13291 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13292 | #include "stringlib/unicode_format.h" |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13293 | |
| 13294 | PyDoc_STRVAR(format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13295 | "S.format(*args, **kwargs) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13296 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 13297 | Return a formatted version of S, using substitutions from args and kwargs.\n\ |
| 13298 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13299 | |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 13300 | PyDoc_STRVAR(format_map__doc__, |
| 13301 | "S.format_map(mapping) -> str\n\ |
| 13302 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 13303 | Return a formatted version of S, using substitutions from mapping.\n\ |
| 13304 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 13305 | |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 13306 | static PyObject * |
| 13307 | unicode__format__(PyObject* self, PyObject* args) |
| 13308 | { |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13309 | PyObject *format_spec, *out; |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 13310 | |
| 13311 | if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) |
| 13312 | return NULL; |
| 13313 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13314 | out = _PyUnicode_FormatAdvanced(self, format_spec, 0, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13315 | PyUnicode_GET_LENGTH(format_spec)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13316 | return out; |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 13317 | } |
| 13318 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13319 | PyDoc_STRVAR(p_format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13320 | "S.__format__(format_spec) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13321 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 13322 | Return a formatted version of S as described by format_spec."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13323 | |
| 13324 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13325 | unicode__sizeof__(PyObject *v) |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13326 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13327 | Py_ssize_t size; |
| 13328 | |
| 13329 | /* If it's a compact object, account for base structure + |
| 13330 | character data. */ |
| 13331 | if (PyUnicode_IS_COMPACT_ASCII(v)) |
| 13332 | size = sizeof(PyASCIIObject) + PyUnicode_GET_LENGTH(v) + 1; |
| 13333 | else if (PyUnicode_IS_COMPACT(v)) |
| 13334 | size = sizeof(PyCompactUnicodeObject) + |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 13335 | (PyUnicode_GET_LENGTH(v) + 1) * PyUnicode_KIND(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13336 | else { |
| 13337 | /* If it is a two-block object, account for base object, and |
| 13338 | for character block if present. */ |
| 13339 | size = sizeof(PyUnicodeObject); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 13340 | if (_PyUnicode_DATA_ANY(v)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13341 | size += (PyUnicode_GET_LENGTH(v) + 1) * |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 13342 | PyUnicode_KIND(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13343 | } |
| 13344 | /* 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] | 13345 | with the data pointer. Check if the data is not shared. */ |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 13346 | if (_PyUnicode_HAS_WSTR_MEMORY(v)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13347 | size += (PyUnicode_WSTR_LENGTH(v) + 1) * sizeof(wchar_t); |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 13348 | if (_PyUnicode_HAS_UTF8_MEMORY(v)) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 13349 | size += PyUnicode_UTF8_LENGTH(v) + 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13350 | |
| 13351 | return PyLong_FromSsize_t(size); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13352 | } |
| 13353 | |
| 13354 | PyDoc_STRVAR(sizeof__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13355 | "S.__sizeof__() -> size of S in memory, in bytes"); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13356 | |
| 13357 | static PyObject * |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 13358 | unicode_getnewargs(PyObject *v) |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 13359 | { |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 13360 | PyObject *copy = _PyUnicode_Copy(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13361 | if (!copy) |
| 13362 | return NULL; |
| 13363 | return Py_BuildValue("(N)", copy); |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 13364 | } |
| 13365 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13366 | static PyMethodDef unicode_methods[] = { |
Benjamin Peterson | 28a4dce | 2010-12-12 01:33:04 +0000 | [diff] [blame] | 13367 | {"encode", (PyCFunction) unicode_encode, METH_VARARGS | METH_KEYWORDS, encode__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13368 | {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 13369 | {"split", (PyCFunction) unicode_split, METH_VARARGS | METH_KEYWORDS, split__doc__}, |
| 13370 | {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS | METH_KEYWORDS, rsplit__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13371 | {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, |
| 13372 | {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 13373 | {"casefold", (PyCFunction) unicode_casefold, METH_NOARGS, casefold__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13374 | {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, |
| 13375 | {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, |
| 13376 | {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, |
| 13377 | {"expandtabs", (PyCFunction) unicode_expandtabs, METH_VARARGS, expandtabs__doc__}, |
| 13378 | {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13379 | {"partition", (PyCFunction) unicode_partition, METH_O, partition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13380 | {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, |
| 13381 | {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, |
| 13382 | {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 13383 | {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13384 | {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, |
| 13385 | {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, |
| 13386 | {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 13387 | {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13388 | {"rpartition", (PyCFunction) unicode_rpartition, METH_O, rpartition__doc__}, |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 13389 | {"splitlines", (PyCFunction) unicode_splitlines, METH_VARARGS | METH_KEYWORDS, splitlines__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 13390 | {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13391 | {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, |
| 13392 | {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, |
| 13393 | {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, |
| 13394 | {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, |
| 13395 | {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, |
| 13396 | {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, |
| 13397 | {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, |
| 13398 | {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, |
| 13399 | {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, |
| 13400 | {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, |
| 13401 | {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, |
| 13402 | {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, |
| 13403 | {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, |
| 13404 | {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 13405 | {"isidentifier", (PyCFunction) unicode_isidentifier, METH_NOARGS, isidentifier__doc__}, |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 13406 | {"isprintable", (PyCFunction) unicode_isprintable, METH_NOARGS, isprintable__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13407 | {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, |
Eric Smith | 9cd1e09 | 2007-08-31 18:39:38 +0000 | [diff] [blame] | 13408 | {"format", (PyCFunction) do_string_format, METH_VARARGS | METH_KEYWORDS, format__doc__}, |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 13409 | {"format_map", (PyCFunction) do_string_format_map, METH_O, format_map__doc__}, |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 13410 | {"__format__", (PyCFunction) unicode__format__, METH_VARARGS, p_format__doc__}, |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 13411 | {"maketrans", (PyCFunction) unicode_maketrans, |
| 13412 | METH_VARARGS | METH_STATIC, maketrans__doc__}, |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13413 | {"__sizeof__", (PyCFunction) unicode__sizeof__, METH_NOARGS, sizeof__doc__}, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 13414 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 13415 | /* These methods are just used for debugging the implementation. */ |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 13416 | {"_decimal2ascii", (PyCFunction) unicode__decimal2ascii, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13417 | #endif |
| 13418 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13419 | {"__getnewargs__", (PyCFunction)unicode_getnewargs, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13420 | {NULL, NULL} |
| 13421 | }; |
| 13422 | |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 13423 | static PyObject * |
| 13424 | unicode_mod(PyObject *v, PyObject *w) |
| 13425 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 13426 | if (!PyUnicode_Check(v)) |
| 13427 | Py_RETURN_NOTIMPLEMENTED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13428 | return PyUnicode_Format(v, w); |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 13429 | } |
| 13430 | |
| 13431 | static PyNumberMethods unicode_as_number = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13432 | 0, /*nb_add*/ |
| 13433 | 0, /*nb_subtract*/ |
| 13434 | 0, /*nb_multiply*/ |
| 13435 | unicode_mod, /*nb_remainder*/ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 13436 | }; |
| 13437 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13438 | static PySequenceMethods unicode_as_sequence = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13439 | (lenfunc) unicode_length, /* sq_length */ |
| 13440 | PyUnicode_Concat, /* sq_concat */ |
| 13441 | (ssizeargfunc) unicode_repeat, /* sq_repeat */ |
| 13442 | (ssizeargfunc) unicode_getitem, /* sq_item */ |
| 13443 | 0, /* sq_slice */ |
| 13444 | 0, /* sq_ass_item */ |
| 13445 | 0, /* sq_ass_slice */ |
| 13446 | PyUnicode_Contains, /* sq_contains */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13447 | }; |
| 13448 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13449 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13450 | unicode_subscript(PyObject* self, PyObject* item) |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13451 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13452 | if (PyUnicode_READY(self) == -1) |
| 13453 | return NULL; |
| 13454 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 13455 | if (PyIndex_Check(item)) { |
| 13456 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13457 | if (i == -1 && PyErr_Occurred()) |
| 13458 | return NULL; |
| 13459 | if (i < 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13460 | i += PyUnicode_GET_LENGTH(self); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13461 | return unicode_getitem(self, i); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13462 | } else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13463 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13464 | PyObject *result; |
| 13465 | void *src_data, *dest_data; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13466 | int src_kind, dest_kind; |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 13467 | Py_UCS4 ch, max_char, kind_limit; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13468 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13469 | if (PySlice_GetIndicesEx(item, PyUnicode_GET_LENGTH(self), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13470 | &start, &stop, &step, &slicelength) < 0) { |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13471 | return NULL; |
| 13472 | } |
| 13473 | |
| 13474 | if (slicelength <= 0) { |
Victor Stinner | 382955f | 2011-12-11 21:44:00 +0100 | [diff] [blame] | 13475 | Py_INCREF(unicode_empty); |
| 13476 | return unicode_empty; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13477 | } else if (start == 0 && step == 1 && |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 13478 | slicelength == PyUnicode_GET_LENGTH(self)) { |
| 13479 | return unicode_result_unchanged(self); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 13480 | } else if (step == 1) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13481 | return PyUnicode_Substring(self, |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 13482 | start, start + slicelength); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13483 | } |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13484 | /* General case */ |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13485 | src_kind = PyUnicode_KIND(self); |
| 13486 | src_data = PyUnicode_DATA(self); |
Victor Stinner | 55c9911 | 2011-10-13 01:17:06 +0200 | [diff] [blame] | 13487 | if (!PyUnicode_IS_ASCII(self)) { |
| 13488 | kind_limit = kind_maxchar_limit(src_kind); |
| 13489 | max_char = 0; |
| 13490 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
| 13491 | ch = PyUnicode_READ(src_kind, src_data, cur); |
| 13492 | if (ch > max_char) { |
| 13493 | max_char = ch; |
| 13494 | if (max_char >= kind_limit) |
| 13495 | break; |
| 13496 | } |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 13497 | } |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13498 | } |
Victor Stinner | 55c9911 | 2011-10-13 01:17:06 +0200 | [diff] [blame] | 13499 | else |
| 13500 | max_char = 127; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13501 | result = PyUnicode_New(slicelength, max_char); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13502 | if (result == NULL) |
| 13503 | return NULL; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13504 | dest_kind = PyUnicode_KIND(result); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13505 | dest_data = PyUnicode_DATA(result); |
| 13506 | |
| 13507 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13508 | Py_UCS4 ch = PyUnicode_READ(src_kind, src_data, cur); |
| 13509 | PyUnicode_WRITE(dest_kind, dest_data, i, ch); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13510 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13511 | assert(_PyUnicode_CheckConsistency(result, 1)); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13512 | return result; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13513 | } else { |
| 13514 | PyErr_SetString(PyExc_TypeError, "string indices must be integers"); |
| 13515 | return NULL; |
| 13516 | } |
| 13517 | } |
| 13518 | |
| 13519 | static PyMappingMethods unicode_as_mapping = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13520 | (lenfunc)unicode_length, /* mp_length */ |
| 13521 | (binaryfunc)unicode_subscript, /* mp_subscript */ |
| 13522 | (objobjargproc)0, /* mp_ass_subscript */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13523 | }; |
| 13524 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13525 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13526 | /* Helpers for PyUnicode_Format() */ |
| 13527 | |
| 13528 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13529 | 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] | 13530 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13531 | Py_ssize_t argidx = *p_argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13532 | if (argidx < arglen) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13533 | (*p_argidx)++; |
| 13534 | if (arglen < 0) |
| 13535 | return args; |
| 13536 | else |
| 13537 | return PyTuple_GetItem(args, argidx); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13538 | } |
| 13539 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13540 | "not enough arguments for format string"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13541 | return NULL; |
| 13542 | } |
| 13543 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13544 | /* 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] | 13545 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13546 | static PyObject * |
| 13547 | formatfloat(PyObject *v, int flags, int prec, int type) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13548 | { |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13549 | char *p; |
| 13550 | PyObject *result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13551 | double x; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 13552 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13553 | x = PyFloat_AsDouble(v); |
| 13554 | if (x == -1.0 && PyErr_Occurred()) |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13555 | return NULL; |
| 13556 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13557 | if (prec < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13558 | prec = 6; |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 13559 | |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 13560 | p = PyOS_double_to_string(x, type, prec, |
| 13561 | (flags & F_ALT) ? Py_DTSF_ALT : 0, NULL); |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13562 | if (p == NULL) |
| 13563 | return NULL; |
Victor Stinner | b5c3ea3 | 2012-05-02 00:29:36 +0200 | [diff] [blame] | 13564 | result = unicode_fromascii((unsigned char*)p, strlen(p)); |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 13565 | PyMem_Free(p); |
| 13566 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13567 | } |
| 13568 | |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13569 | /* formatlong() emulates the format codes d, u, o, x and X, and |
| 13570 | * the F_ALT flag, for Python's long (unbounded) ints. It's not used for |
| 13571 | * Python's regular ints. |
| 13572 | * Return value: a new PyUnicodeObject*, or NULL if error. |
| 13573 | * The output string is of the form |
| 13574 | * "-"? ("0x" | "0X")? digit+ |
| 13575 | * "0x"/"0X" are present only for x and X conversions, with F_ALT |
| 13576 | * set in flags. The case of hex digits will be correct, |
| 13577 | * There will be at least prec digits, zero-filled on the left if |
| 13578 | * necessary to get that many. |
| 13579 | * val object to be converted |
| 13580 | * flags bitmask of format flags; only F_ALT is looked at |
| 13581 | * prec minimum number of digits; 0-fill on left if needed |
| 13582 | * type a character in [duoxX]; u acts the same as d |
| 13583 | * |
| 13584 | * CAUTION: o, x and X conversions on regular ints can never |
| 13585 | * produce a '-' sign, but can for Python's unbounded ints. |
| 13586 | */ |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13587 | static PyObject* |
| 13588 | formatlong(PyObject *val, int flags, int prec, int type) |
| 13589 | { |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13590 | PyObject *result = NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13591 | char *buf; |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13592 | Py_ssize_t i; |
| 13593 | int sign; /* 1 if '-', else 0 */ |
| 13594 | int len; /* number of characters */ |
| 13595 | Py_ssize_t llen; |
| 13596 | int numdigits; /* len == numnondigits + numdigits */ |
| 13597 | int numnondigits = 0; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13598 | |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13599 | /* Avoid exceeding SSIZE_T_MAX */ |
| 13600 | if (prec > INT_MAX-3) { |
| 13601 | PyErr_SetString(PyExc_OverflowError, |
| 13602 | "precision too large"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13603 | return NULL; |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13604 | } |
| 13605 | |
| 13606 | assert(PyLong_Check(val)); |
| 13607 | |
| 13608 | switch (type) { |
| 13609 | case 'd': |
| 13610 | case 'u': |
| 13611 | /* Special-case boolean: we want 0/1 */ |
Victor Stinner | b11d91d | 2012-04-28 00:25:34 +0200 | [diff] [blame] | 13612 | if (PyBool_Check(val)) |
| 13613 | result = PyNumber_ToBase(val, 10); |
| 13614 | else |
| 13615 | result = Py_TYPE(val)->tp_str(val); |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13616 | break; |
| 13617 | case 'o': |
| 13618 | numnondigits = 2; |
| 13619 | result = PyNumber_ToBase(val, 8); |
| 13620 | break; |
| 13621 | case 'x': |
| 13622 | case 'X': |
| 13623 | numnondigits = 2; |
| 13624 | result = PyNumber_ToBase(val, 16); |
| 13625 | break; |
| 13626 | default: |
| 13627 | assert(!"'type' not in [duoxX]"); |
| 13628 | } |
| 13629 | if (!result) |
| 13630 | return NULL; |
| 13631 | |
| 13632 | assert(unicode_modifiable(result)); |
| 13633 | assert(PyUnicode_IS_READY(result)); |
| 13634 | assert(PyUnicode_IS_ASCII(result)); |
| 13635 | |
| 13636 | /* To modify the string in-place, there can only be one reference. */ |
| 13637 | if (Py_REFCNT(result) != 1) { |
| 13638 | PyErr_BadInternalCall(); |
| 13639 | return NULL; |
| 13640 | } |
| 13641 | buf = PyUnicode_DATA(result); |
| 13642 | llen = PyUnicode_GET_LENGTH(result); |
| 13643 | if (llen > INT_MAX) { |
| 13644 | PyErr_SetString(PyExc_ValueError, |
| 13645 | "string too large in _PyBytes_FormatLong"); |
| 13646 | return NULL; |
| 13647 | } |
| 13648 | len = (int)llen; |
| 13649 | sign = buf[0] == '-'; |
| 13650 | numnondigits += sign; |
| 13651 | numdigits = len - numnondigits; |
| 13652 | assert(numdigits > 0); |
| 13653 | |
| 13654 | /* Get rid of base marker unless F_ALT */ |
| 13655 | if (((flags & F_ALT) == 0 && |
| 13656 | (type == 'o' || type == 'x' || type == 'X'))) { |
| 13657 | assert(buf[sign] == '0'); |
| 13658 | assert(buf[sign+1] == 'x' || buf[sign+1] == 'X' || |
| 13659 | buf[sign+1] == 'o'); |
| 13660 | numnondigits -= 2; |
| 13661 | buf += 2; |
| 13662 | len -= 2; |
| 13663 | if (sign) |
| 13664 | buf[0] = '-'; |
| 13665 | assert(len == numnondigits + numdigits); |
| 13666 | assert(numdigits > 0); |
| 13667 | } |
| 13668 | |
| 13669 | /* Fill with leading zeroes to meet minimum width. */ |
| 13670 | if (prec > numdigits) { |
| 13671 | PyObject *r1 = PyBytes_FromStringAndSize(NULL, |
| 13672 | numnondigits + prec); |
| 13673 | char *b1; |
| 13674 | if (!r1) { |
| 13675 | Py_DECREF(result); |
| 13676 | return NULL; |
| 13677 | } |
| 13678 | b1 = PyBytes_AS_STRING(r1); |
| 13679 | for (i = 0; i < numnondigits; ++i) |
| 13680 | *b1++ = *buf++; |
| 13681 | for (i = 0; i < prec - numdigits; i++) |
| 13682 | *b1++ = '0'; |
| 13683 | for (i = 0; i < numdigits; i++) |
| 13684 | *b1++ = *buf++; |
| 13685 | *b1 = '\0'; |
| 13686 | Py_DECREF(result); |
| 13687 | result = r1; |
| 13688 | buf = PyBytes_AS_STRING(result); |
| 13689 | len = numnondigits + prec; |
| 13690 | } |
| 13691 | |
| 13692 | /* Fix up case for hex conversions. */ |
| 13693 | if (type == 'X') { |
| 13694 | /* Need to convert all lower case letters to upper case. |
| 13695 | and need to convert 0x to 0X (and -0x to -0X). */ |
| 13696 | for (i = 0; i < len; i++) |
| 13697 | if (buf[i] >= 'a' && buf[i] <= 'x') |
| 13698 | buf[i] -= 'a'-'A'; |
| 13699 | } |
| 13700 | if (!PyUnicode_Check(result) || len != PyUnicode_GET_LENGTH(result)) { |
| 13701 | PyObject *unicode; |
| 13702 | unicode = unicode_fromascii((unsigned char *)buf, len); |
| 13703 | Py_DECREF(result); |
| 13704 | result = unicode; |
| 13705 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13706 | return result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13707 | } |
| 13708 | |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13709 | static Py_UCS4 |
| 13710 | formatchar(PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13711 | { |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 13712 | /* presume that the buffer is at least 3 characters long */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 13713 | if (PyUnicode_Check(v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13714 | if (PyUnicode_GET_LENGTH(v) == 1) { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13715 | return PyUnicode_READ_CHAR(v, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13716 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13717 | goto onError; |
| 13718 | } |
| 13719 | else { |
| 13720 | /* Integer input truncated to a character */ |
| 13721 | long x; |
| 13722 | x = PyLong_AsLong(v); |
| 13723 | if (x == -1 && PyErr_Occurred()) |
| 13724 | goto onError; |
| 13725 | |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 13726 | if (x < 0 || x > MAX_UNICODE) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13727 | PyErr_SetString(PyExc_OverflowError, |
| 13728 | "%c arg not in range(0x110000)"); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13729 | return (Py_UCS4) -1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13730 | } |
| 13731 | |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13732 | return (Py_UCS4) x; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13733 | } |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 13734 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13735 | onError: |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 13736 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13737 | "%c requires int or char"); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13738 | return (Py_UCS4) -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13739 | } |
| 13740 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 13741 | PyObject * |
| 13742 | PyUnicode_Format(PyObject *format, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13743 | { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13744 | Py_ssize_t fmtcnt, fmtpos, arglen, argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13745 | int args_owned = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13746 | PyObject *dict = NULL; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13747 | PyObject *temp = NULL; |
| 13748 | PyObject *second = NULL; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13749 | PyObject *uformat; |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 13750 | void *fmt; |
| 13751 | enum PyUnicode_Kind kind, fmtkind; |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame^] | 13752 | _PyUnicodeWriter writer; |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 13753 | Py_ssize_t sublen; |
| 13754 | Py_UCS4 maxchar; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 13755 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13756 | if (format == NULL || args == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13757 | PyErr_BadInternalCall(); |
| 13758 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13759 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13760 | uformat = PyUnicode_FromObject(format); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 13761 | if (uformat == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13762 | return NULL; |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 13763 | if (PyUnicode_READY(uformat) == -1) |
| 13764 | Py_DECREF(uformat); |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 13765 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13766 | fmt = PyUnicode_DATA(uformat); |
| 13767 | fmtkind = PyUnicode_KIND(uformat); |
| 13768 | fmtcnt = PyUnicode_GET_LENGTH(uformat); |
| 13769 | fmtpos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13770 | |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame^] | 13771 | if (_PyUnicodeWriter_Init(&writer, fmtcnt + 100, 127) < 0) |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 13772 | goto onError; |
| 13773 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13774 | if (PyTuple_Check(args)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13775 | arglen = PyTuple_Size(args); |
| 13776 | argidx = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13777 | } |
| 13778 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13779 | arglen = -1; |
| 13780 | argidx = -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13781 | } |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 13782 | if (Py_TYPE(args)->tp_as_mapping && !PyTuple_Check(args) && |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 13783 | !PyUnicode_Check(args)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13784 | dict = args; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13785 | |
| 13786 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13787 | if (PyUnicode_READ(fmtkind, fmt, fmtpos) != '%') { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13788 | Py_ssize_t nonfmtpos; |
| 13789 | nonfmtpos = fmtpos++; |
| 13790 | while (fmtcnt >= 0 && |
| 13791 | PyUnicode_READ(fmtkind, fmt, fmtpos) != '%') { |
| 13792 | fmtpos++; |
| 13793 | fmtcnt--; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13794 | } |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 13795 | if (fmtcnt < 0) |
| 13796 | fmtpos--; |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 13797 | sublen = fmtpos - nonfmtpos; |
| 13798 | maxchar = _PyUnicode_FindMaxChar(uformat, |
| 13799 | nonfmtpos, nonfmtpos + sublen); |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame^] | 13800 | if (_PyUnicodeWriter_Prepare(&writer, sublen, maxchar) == -1) |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13801 | goto onError; |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 13802 | |
| 13803 | copy_characters(writer.buffer, writer.pos, |
| 13804 | uformat, nonfmtpos, sublen); |
| 13805 | writer.pos += sublen; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13806 | } |
| 13807 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13808 | /* Got a format specifier */ |
| 13809 | int flags = 0; |
| 13810 | Py_ssize_t width = -1; |
| 13811 | int prec = -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13812 | Py_UCS4 c = '\0'; |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 13813 | Py_UCS4 fill; |
| 13814 | int sign; |
| 13815 | Py_UCS4 signchar; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13816 | int isnumok; |
| 13817 | PyObject *v = NULL; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13818 | void *pbuf = NULL; |
| 13819 | Py_ssize_t pindex, len; |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 13820 | Py_UCS4 bufmaxchar; |
| 13821 | Py_ssize_t buflen; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13822 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13823 | fmtpos++; |
Victor Stinner | 438106b | 2012-05-02 00:41:57 +0200 | [diff] [blame] | 13824 | c = PyUnicode_READ(fmtkind, fmt, fmtpos); |
| 13825 | if (c == '(') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13826 | Py_ssize_t keystart; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13827 | Py_ssize_t keylen; |
| 13828 | PyObject *key; |
| 13829 | int pcount = 1; |
Christian Heimes | a612dc0 | 2008-02-24 13:08:18 +0000 | [diff] [blame] | 13830 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13831 | if (dict == NULL) { |
| 13832 | PyErr_SetString(PyExc_TypeError, |
| 13833 | "format requires a mapping"); |
| 13834 | goto onError; |
| 13835 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13836 | ++fmtpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13837 | --fmtcnt; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13838 | keystart = fmtpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13839 | /* Skip over balanced parentheses */ |
| 13840 | while (pcount > 0 && --fmtcnt >= 0) { |
Victor Stinner | bff7c96 | 2012-05-03 01:44:59 +0200 | [diff] [blame] | 13841 | c = PyUnicode_READ(fmtkind, fmt, fmtpos); |
| 13842 | if (c == ')') |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13843 | --pcount; |
Victor Stinner | bff7c96 | 2012-05-03 01:44:59 +0200 | [diff] [blame] | 13844 | else if (c == '(') |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13845 | ++pcount; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13846 | fmtpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13847 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13848 | keylen = fmtpos - keystart - 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13849 | if (fmtcnt < 0 || pcount > 0) { |
| 13850 | PyErr_SetString(PyExc_ValueError, |
| 13851 | "incomplete format key"); |
| 13852 | goto onError; |
| 13853 | } |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 13854 | key = PyUnicode_Substring(uformat, |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 13855 | keystart, keystart + keylen); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13856 | if (key == NULL) |
| 13857 | goto onError; |
| 13858 | if (args_owned) { |
| 13859 | Py_DECREF(args); |
| 13860 | args_owned = 0; |
| 13861 | } |
| 13862 | args = PyObject_GetItem(dict, key); |
| 13863 | Py_DECREF(key); |
| 13864 | if (args == NULL) { |
| 13865 | goto onError; |
| 13866 | } |
| 13867 | args_owned = 1; |
| 13868 | arglen = -1; |
| 13869 | argidx = -2; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13870 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13871 | while (--fmtcnt >= 0) { |
Victor Stinner | 438106b | 2012-05-02 00:41:57 +0200 | [diff] [blame] | 13872 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
| 13873 | switch (c) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13874 | case '-': flags |= F_LJUST; continue; |
| 13875 | case '+': flags |= F_SIGN; continue; |
| 13876 | case ' ': flags |= F_BLANK; continue; |
| 13877 | case '#': flags |= F_ALT; continue; |
| 13878 | case '0': flags |= F_ZERO; continue; |
| 13879 | } |
| 13880 | break; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13881 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13882 | if (c == '*') { |
| 13883 | v = getnextarg(args, arglen, &argidx); |
| 13884 | if (v == NULL) |
| 13885 | goto onError; |
| 13886 | if (!PyLong_Check(v)) { |
| 13887 | PyErr_SetString(PyExc_TypeError, |
| 13888 | "* wants int"); |
| 13889 | goto onError; |
| 13890 | } |
| 13891 | width = PyLong_AsLong(v); |
| 13892 | if (width == -1 && PyErr_Occurred()) |
| 13893 | goto onError; |
| 13894 | if (width < 0) { |
| 13895 | flags |= F_LJUST; |
| 13896 | width = -width; |
| 13897 | } |
| 13898 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13899 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13900 | } |
| 13901 | else if (c >= '0' && c <= '9') { |
| 13902 | width = c - '0'; |
| 13903 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13904 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13905 | if (c < '0' || c > '9') |
| 13906 | break; |
Mark Dickinson | 99e2e55 | 2012-05-07 11:20:50 +0100 | [diff] [blame] | 13907 | if (width > (PY_SSIZE_T_MAX - (c - '0')) / 10) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13908 | PyErr_SetString(PyExc_ValueError, |
| 13909 | "width too big"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13910 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13911 | } |
| 13912 | width = width*10 + (c - '0'); |
| 13913 | } |
| 13914 | } |
| 13915 | if (c == '.') { |
| 13916 | prec = 0; |
| 13917 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13918 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13919 | if (c == '*') { |
| 13920 | v = getnextarg(args, arglen, &argidx); |
| 13921 | if (v == NULL) |
| 13922 | goto onError; |
| 13923 | if (!PyLong_Check(v)) { |
| 13924 | PyErr_SetString(PyExc_TypeError, |
| 13925 | "* wants int"); |
| 13926 | goto onError; |
| 13927 | } |
| 13928 | prec = PyLong_AsLong(v); |
| 13929 | if (prec == -1 && PyErr_Occurred()) |
| 13930 | goto onError; |
| 13931 | if (prec < 0) |
| 13932 | prec = 0; |
| 13933 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13934 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13935 | } |
| 13936 | else if (c >= '0' && c <= '9') { |
| 13937 | prec = c - '0'; |
| 13938 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13939 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13940 | if (c < '0' || c > '9') |
| 13941 | break; |
Mark Dickinson | 99e2e55 | 2012-05-07 11:20:50 +0100 | [diff] [blame] | 13942 | if (prec > (INT_MAX - (c - '0')) / 10) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13943 | PyErr_SetString(PyExc_ValueError, |
| 13944 | "prec too big"); |
| 13945 | goto onError; |
| 13946 | } |
| 13947 | prec = prec*10 + (c - '0'); |
| 13948 | } |
| 13949 | } |
| 13950 | } /* prec */ |
| 13951 | if (fmtcnt >= 0) { |
| 13952 | if (c == 'h' || c == 'l' || c == 'L') { |
| 13953 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13954 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13955 | } |
| 13956 | } |
| 13957 | if (fmtcnt < 0) { |
| 13958 | PyErr_SetString(PyExc_ValueError, |
| 13959 | "incomplete format"); |
| 13960 | goto onError; |
| 13961 | } |
Victor Stinner | aff3cc6 | 2012-04-30 05:19:21 +0200 | [diff] [blame] | 13962 | |
| 13963 | if (c == '%') { |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame^] | 13964 | if (_PyUnicodeWriter_Prepare(&writer, 1, '%') == -1) |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 13965 | goto onError; |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 13966 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, '%'); |
| 13967 | writer.pos += 1; |
Victor Stinner | aff3cc6 | 2012-04-30 05:19:21 +0200 | [diff] [blame] | 13968 | continue; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13969 | } |
Victor Stinner | aff3cc6 | 2012-04-30 05:19:21 +0200 | [diff] [blame] | 13970 | |
| 13971 | |
| 13972 | v = getnextarg(args, arglen, &argidx); |
| 13973 | if (v == NULL) |
| 13974 | goto onError; |
| 13975 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13976 | sign = 0; |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 13977 | signchar = '\0'; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13978 | fill = ' '; |
| 13979 | switch (c) { |
| 13980 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13981 | case 's': |
| 13982 | case 'r': |
| 13983 | case 'a': |
Victor Stinner | 808fc0a | 2010-03-22 12:50:40 +0000 | [diff] [blame] | 13984 | if (PyUnicode_CheckExact(v) && c == 's') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13985 | temp = v; |
| 13986 | Py_INCREF(temp); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13987 | } |
| 13988 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13989 | if (c == 's') |
| 13990 | temp = PyObject_Str(v); |
| 13991 | else if (c == 'r') |
| 13992 | temp = PyObject_Repr(v); |
| 13993 | else |
| 13994 | temp = PyObject_ASCII(v); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13995 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13996 | break; |
| 13997 | |
| 13998 | case 'i': |
| 13999 | case 'd': |
| 14000 | case 'u': |
| 14001 | case 'o': |
| 14002 | case 'x': |
| 14003 | case 'X': |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14004 | isnumok = 0; |
| 14005 | if (PyNumber_Check(v)) { |
| 14006 | PyObject *iobj=NULL; |
| 14007 | |
| 14008 | if (PyLong_Check(v)) { |
| 14009 | iobj = v; |
| 14010 | Py_INCREF(iobj); |
| 14011 | } |
| 14012 | else { |
| 14013 | iobj = PyNumber_Long(v); |
| 14014 | } |
| 14015 | if (iobj!=NULL) { |
| 14016 | if (PyLong_Check(iobj)) { |
| 14017 | isnumok = 1; |
Victor Stinner | aff3cc6 | 2012-04-30 05:19:21 +0200 | [diff] [blame] | 14018 | sign = 1; |
Senthil Kumaran | 9ebe08d | 2011-07-03 21:03:16 -0700 | [diff] [blame] | 14019 | temp = formatlong(iobj, flags, prec, (c == 'i'? 'd': c)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14020 | Py_DECREF(iobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14021 | } |
| 14022 | else { |
| 14023 | Py_DECREF(iobj); |
| 14024 | } |
| 14025 | } |
| 14026 | } |
| 14027 | if (!isnumok) { |
| 14028 | PyErr_Format(PyExc_TypeError, |
| 14029 | "%%%c format: a number is required, " |
| 14030 | "not %.200s", (char)c, Py_TYPE(v)->tp_name); |
| 14031 | goto onError; |
| 14032 | } |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 14033 | if (flags & F_ZERO) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14034 | fill = '0'; |
| 14035 | break; |
| 14036 | |
| 14037 | case 'e': |
| 14038 | case 'E': |
| 14039 | case 'f': |
| 14040 | case 'F': |
| 14041 | case 'g': |
| 14042 | case 'G': |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14043 | sign = 1; |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 14044 | if (flags & F_ZERO) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14045 | fill = '0'; |
Victor Stinner | aff3cc6 | 2012-04-30 05:19:21 +0200 | [diff] [blame] | 14046 | temp = formatfloat(v, flags, prec, c); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14047 | break; |
| 14048 | |
| 14049 | case 'c': |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14050 | { |
| 14051 | Py_UCS4 ch = formatchar(v); |
| 14052 | if (ch == (Py_UCS4) -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14053 | goto onError; |
Victor Stinner | b5c3ea3 | 2012-05-02 00:29:36 +0200 | [diff] [blame] | 14054 | temp = PyUnicode_FromOrdinal(ch); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14055 | break; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14056 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14057 | |
| 14058 | default: |
| 14059 | PyErr_Format(PyExc_ValueError, |
| 14060 | "unsupported format character '%c' (0x%x) " |
| 14061 | "at index %zd", |
| 14062 | (31<=c && c<=126) ? (char)c : '?', |
| 14063 | (int)c, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14064 | fmtpos - 1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14065 | goto onError; |
| 14066 | } |
Victor Stinner | aff3cc6 | 2012-04-30 05:19:21 +0200 | [diff] [blame] | 14067 | if (temp == NULL) |
| 14068 | goto onError; |
| 14069 | assert (PyUnicode_Check(temp)); |
| 14070 | if (PyUnicode_READY(temp) == -1) { |
| 14071 | Py_CLEAR(temp); |
| 14072 | goto onError; |
| 14073 | } |
| 14074 | kind = PyUnicode_KIND(temp); |
| 14075 | pbuf = PyUnicode_DATA(temp); |
| 14076 | len = PyUnicode_GET_LENGTH(temp); |
| 14077 | |
| 14078 | if (c == 's' || c == 'r' || c == 'a') { |
| 14079 | if (prec >= 0 && len > prec) |
| 14080 | len = prec; |
| 14081 | } |
| 14082 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14083 | /* pbuf is initialized here. */ |
| 14084 | pindex = 0; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14085 | if (sign) { |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 14086 | Py_UCS4 ch = PyUnicode_READ(kind, pbuf, pindex); |
| 14087 | if (ch == '-' || ch == '+') { |
| 14088 | signchar = ch; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14089 | len--; |
| 14090 | pindex++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14091 | } |
| 14092 | else if (flags & F_SIGN) |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 14093 | signchar = '+'; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14094 | else if (flags & F_BLANK) |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 14095 | signchar = ' '; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14096 | else |
| 14097 | sign = 0; |
| 14098 | } |
| 14099 | if (width < len) |
| 14100 | width = len; |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 14101 | |
| 14102 | /* Compute the length and maximum character of the |
| 14103 | written characters */ |
| 14104 | bufmaxchar = 127; |
| 14105 | if (!(flags & F_LJUST)) { |
| 14106 | if (sign) { |
| 14107 | if ((width-1) > len) |
| 14108 | bufmaxchar = Py_MAX(bufmaxchar, fill); |
| 14109 | } |
| 14110 | else { |
| 14111 | if (width > len) |
| 14112 | bufmaxchar = Py_MAX(bufmaxchar, fill); |
| 14113 | } |
| 14114 | } |
| 14115 | maxchar = _PyUnicode_FindMaxChar(temp, 0, pindex+len); |
| 14116 | bufmaxchar = Py_MAX(bufmaxchar, maxchar); |
| 14117 | |
| 14118 | buflen = width; |
| 14119 | if (sign && len == width) |
| 14120 | buflen++; |
| 14121 | |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame^] | 14122 | if (_PyUnicodeWriter_Prepare(&writer, buflen, bufmaxchar) == -1) |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 14123 | goto onError; |
| 14124 | |
| 14125 | /* Write characters */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14126 | if (sign) { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14127 | if (fill != ' ') { |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 14128 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, signchar); |
| 14129 | writer.pos += 1; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14130 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14131 | if (width > len) |
| 14132 | width--; |
| 14133 | } |
| 14134 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14135 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14136 | assert(PyUnicode_READ(kind, pbuf, pindex + 1) == c); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14137 | if (fill != ' ') { |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 14138 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, '0'); |
| 14139 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos+1, c); |
| 14140 | writer.pos += 2; |
| 14141 | pindex += 2; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14142 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14143 | width -= 2; |
| 14144 | if (width < 0) |
| 14145 | width = 0; |
| 14146 | len -= 2; |
| 14147 | } |
| 14148 | if (width > len && !(flags & F_LJUST)) { |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 14149 | sublen = width - len; |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 14150 | FILL(writer.kind, writer.data, fill, writer.pos, sublen); |
| 14151 | writer.pos += sublen; |
Antoine Pitrou | 978b9d2 | 2011-10-07 12:35:48 +0200 | [diff] [blame] | 14152 | width = len; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14153 | } |
| 14154 | if (fill == ' ') { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14155 | if (sign) { |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 14156 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, signchar); |
| 14157 | writer.pos += 1; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14158 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14159 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14160 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
| 14161 | assert(PyUnicode_READ(kind, pbuf, pindex+1) == c); |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 14162 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, '0'); |
| 14163 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos+1, c); |
| 14164 | writer.pos += 2; |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 14165 | pindex += 2; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14166 | } |
| 14167 | } |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 14168 | |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 14169 | copy_characters(writer.buffer, writer.pos, |
| 14170 | temp, pindex, len); |
| 14171 | writer.pos += len; |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 14172 | if (width > len) { |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 14173 | sublen = width - len; |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 14174 | FILL(writer.kind, writer.data, ' ', writer.pos, sublen); |
| 14175 | writer.pos += sublen; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14176 | } |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 14177 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14178 | if (dict && (argidx < arglen) && c != '%') { |
| 14179 | PyErr_SetString(PyExc_TypeError, |
| 14180 | "not all arguments converted during string formatting"); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14181 | goto onError; |
| 14182 | } |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14183 | Py_CLEAR(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14184 | } /* '%' */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14185 | } /* until end */ |
| 14186 | if (argidx < arglen && !dict) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14187 | PyErr_SetString(PyExc_TypeError, |
| 14188 | "not all arguments converted during string formatting"); |
| 14189 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14190 | } |
| 14191 | |
| 14192 | if (args_owned) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14193 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14194 | } |
| 14195 | Py_DECREF(uformat); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14196 | Py_XDECREF(temp); |
| 14197 | Py_XDECREF(second); |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame^] | 14198 | return _PyUnicodeWriter_Finish(&writer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14199 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14200 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14201 | Py_DECREF(uformat); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14202 | Py_XDECREF(temp); |
| 14203 | Py_XDECREF(second); |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame^] | 14204 | _PyUnicodeWriter_Dealloc(&writer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14205 | if (args_owned) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14206 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14207 | } |
| 14208 | return NULL; |
| 14209 | } |
| 14210 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 14211 | static PyObject * |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 14212 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 14213 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14214 | static PyObject * |
| 14215 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 14216 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14217 | PyObject *x = NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14218 | static char *kwlist[] = {"object", "encoding", "errors", 0}; |
| 14219 | char *encoding = NULL; |
| 14220 | char *errors = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14221 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14222 | if (type != &PyUnicode_Type) |
| 14223 | return unicode_subtype_new(type, args, kwds); |
| 14224 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:str", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14225 | kwlist, &x, &encoding, &errors)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14226 | return NULL; |
Victor Stinner | 382955f | 2011-12-11 21:44:00 +0100 | [diff] [blame] | 14227 | if (x == NULL) { |
| 14228 | Py_INCREF(unicode_empty); |
| 14229 | return unicode_empty; |
| 14230 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14231 | if (encoding == NULL && errors == NULL) |
| 14232 | return PyObject_Str(x); |
| 14233 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14234 | return PyUnicode_FromEncodedObject(x, encoding, errors); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14235 | } |
| 14236 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 14237 | static PyObject * |
| 14238 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 14239 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14240 | PyObject *unicode, *self; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14241 | Py_ssize_t length, char_size; |
| 14242 | int share_wstr, share_utf8; |
| 14243 | unsigned int kind; |
| 14244 | void *data; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 14245 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14246 | assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14247 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14248 | unicode = unicode_new(&PyUnicode_Type, args, kwds); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14249 | if (unicode == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14250 | return NULL; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 14251 | assert(_PyUnicode_CHECK(unicode)); |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 14252 | if (PyUnicode_READY(unicode) == -1) { |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 14253 | Py_DECREF(unicode); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14254 | return NULL; |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 14255 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14256 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14257 | self = type->tp_alloc(type, 0); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14258 | if (self == NULL) { |
| 14259 | Py_DECREF(unicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14260 | return NULL; |
| 14261 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14262 | kind = PyUnicode_KIND(unicode); |
| 14263 | length = PyUnicode_GET_LENGTH(unicode); |
| 14264 | |
| 14265 | _PyUnicode_LENGTH(self) = length; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 14266 | #ifdef Py_DEBUG |
| 14267 | _PyUnicode_HASH(self) = -1; |
| 14268 | #else |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14269 | _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 14270 | #endif |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14271 | _PyUnicode_STATE(self).interned = 0; |
| 14272 | _PyUnicode_STATE(self).kind = kind; |
| 14273 | _PyUnicode_STATE(self).compact = 0; |
Victor Stinner | 3cf4637 | 2011-10-03 14:42:15 +0200 | [diff] [blame] | 14274 | _PyUnicode_STATE(self).ascii = _PyUnicode_STATE(unicode).ascii; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14275 | _PyUnicode_STATE(self).ready = 1; |
| 14276 | _PyUnicode_WSTR(self) = NULL; |
| 14277 | _PyUnicode_UTF8_LENGTH(self) = 0; |
| 14278 | _PyUnicode_UTF8(self) = NULL; |
| 14279 | _PyUnicode_WSTR_LENGTH(self) = 0; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 14280 | _PyUnicode_DATA_ANY(self) = NULL; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14281 | |
| 14282 | share_utf8 = 0; |
| 14283 | share_wstr = 0; |
| 14284 | if (kind == PyUnicode_1BYTE_KIND) { |
| 14285 | char_size = 1; |
| 14286 | if (PyUnicode_MAX_CHAR_VALUE(unicode) < 128) |
| 14287 | share_utf8 = 1; |
| 14288 | } |
| 14289 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 14290 | char_size = 2; |
| 14291 | if (sizeof(wchar_t) == 2) |
| 14292 | share_wstr = 1; |
| 14293 | } |
| 14294 | else { |
| 14295 | assert(kind == PyUnicode_4BYTE_KIND); |
| 14296 | char_size = 4; |
| 14297 | if (sizeof(wchar_t) == 4) |
| 14298 | share_wstr = 1; |
| 14299 | } |
| 14300 | |
| 14301 | /* Ensure we won't overflow the length. */ |
| 14302 | if (length > (PY_SSIZE_T_MAX / char_size - 1)) { |
| 14303 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14304 | goto onError; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14305 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14306 | data = PyObject_MALLOC((length + 1) * char_size); |
| 14307 | if (data == NULL) { |
| 14308 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14309 | goto onError; |
| 14310 | } |
| 14311 | |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 14312 | _PyUnicode_DATA_ANY(self) = data; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14313 | if (share_utf8) { |
| 14314 | _PyUnicode_UTF8_LENGTH(self) = length; |
| 14315 | _PyUnicode_UTF8(self) = data; |
| 14316 | } |
| 14317 | if (share_wstr) { |
| 14318 | _PyUnicode_WSTR_LENGTH(self) = length; |
| 14319 | _PyUnicode_WSTR(self) = (wchar_t *)data; |
| 14320 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14321 | |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14322 | Py_MEMCPY(data, PyUnicode_DATA(unicode), |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 14323 | kind * (length + 1)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 14324 | assert(_PyUnicode_CheckConsistency(self, 1)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 14325 | #ifdef Py_DEBUG |
| 14326 | _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode); |
| 14327 | #endif |
Victor Stinner | dd18d3a | 2011-10-22 11:08:10 +0200 | [diff] [blame] | 14328 | Py_DECREF(unicode); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 14329 | return self; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14330 | |
| 14331 | onError: |
| 14332 | Py_DECREF(unicode); |
| 14333 | Py_DECREF(self); |
| 14334 | return NULL; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 14335 | } |
| 14336 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 14337 | PyDoc_STRVAR(unicode_doc, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14338 | "str(string[, encoding[, errors]]) -> str\n\ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14339 | \n\ |
Collin Winter | d474ce8 | 2007-08-07 19:42:11 +0000 | [diff] [blame] | 14340 | Create a new string object from the given encoded string.\n\ |
Skip Montanaro | 35b37a5 | 2002-07-26 16:22:46 +0000 | [diff] [blame] | 14341 | encoding defaults to the current default string encoding.\n\ |
| 14342 | errors can be 'strict', 'replace' or 'ignore' and defaults to 'strict'."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14343 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14344 | static PyObject *unicode_iter(PyObject *seq); |
| 14345 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14346 | PyTypeObject PyUnicode_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 14347 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14348 | "str", /* tp_name */ |
| 14349 | sizeof(PyUnicodeObject), /* tp_size */ |
| 14350 | 0, /* tp_itemsize */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14351 | /* Slots */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14352 | (destructor)unicode_dealloc, /* tp_dealloc */ |
| 14353 | 0, /* tp_print */ |
| 14354 | 0, /* tp_getattr */ |
| 14355 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 14356 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14357 | unicode_repr, /* tp_repr */ |
| 14358 | &unicode_as_number, /* tp_as_number */ |
| 14359 | &unicode_as_sequence, /* tp_as_sequence */ |
| 14360 | &unicode_as_mapping, /* tp_as_mapping */ |
| 14361 | (hashfunc) unicode_hash, /* tp_hash*/ |
| 14362 | 0, /* tp_call*/ |
| 14363 | (reprfunc) unicode_str, /* tp_str */ |
| 14364 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 14365 | 0, /* tp_setattro */ |
| 14366 | 0, /* tp_as_buffer */ |
| 14367 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14368 | Py_TPFLAGS_UNICODE_SUBCLASS, /* tp_flags */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14369 | unicode_doc, /* tp_doc */ |
| 14370 | 0, /* tp_traverse */ |
| 14371 | 0, /* tp_clear */ |
| 14372 | PyUnicode_RichCompare, /* tp_richcompare */ |
| 14373 | 0, /* tp_weaklistoffset */ |
| 14374 | unicode_iter, /* tp_iter */ |
| 14375 | 0, /* tp_iternext */ |
| 14376 | unicode_methods, /* tp_methods */ |
| 14377 | 0, /* tp_members */ |
| 14378 | 0, /* tp_getset */ |
| 14379 | &PyBaseObject_Type, /* tp_base */ |
| 14380 | 0, /* tp_dict */ |
| 14381 | 0, /* tp_descr_get */ |
| 14382 | 0, /* tp_descr_set */ |
| 14383 | 0, /* tp_dictoffset */ |
| 14384 | 0, /* tp_init */ |
| 14385 | 0, /* tp_alloc */ |
| 14386 | unicode_new, /* tp_new */ |
| 14387 | PyObject_Del, /* tp_free */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14388 | }; |
| 14389 | |
| 14390 | /* Initialize the Unicode implementation */ |
| 14391 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 14392 | int _PyUnicode_Init(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14393 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 14394 | int i; |
| 14395 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14396 | /* XXX - move this array to unicodectype.c ? */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14397 | Py_UCS2 linebreak[] = { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14398 | 0x000A, /* LINE FEED */ |
| 14399 | 0x000D, /* CARRIAGE RETURN */ |
| 14400 | 0x001C, /* FILE SEPARATOR */ |
| 14401 | 0x001D, /* GROUP SEPARATOR */ |
| 14402 | 0x001E, /* RECORD SEPARATOR */ |
| 14403 | 0x0085, /* NEXT LINE */ |
| 14404 | 0x2028, /* LINE SEPARATOR */ |
| 14405 | 0x2029, /* PARAGRAPH SEPARATOR */ |
| 14406 | }; |
| 14407 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 14408 | /* Init the implementation */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 14409 | unicode_empty = PyUnicode_New(0, 0); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 14410 | if (!unicode_empty) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14411 | Py_FatalError("Can't create empty string"); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 14412 | assert(_PyUnicode_CheckConsistency(unicode_empty, 1)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 14413 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 14414 | for (i = 0; i < 256; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14415 | unicode_latin1[i] = NULL; |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 14416 | if (PyType_Ready(&PyUnicode_Type) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14417 | Py_FatalError("Can't initialize 'unicode'"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14418 | |
| 14419 | /* initialize the linebreak bloom filter */ |
| 14420 | bloom_linebreak = make_bloom_mask( |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14421 | PyUnicode_2BYTE_KIND, linebreak, |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 14422 | Py_ARRAY_LENGTH(linebreak)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 14423 | |
| 14424 | PyType_Ready(&EncodingMapType); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 14425 | |
| 14426 | #ifdef HAVE_MBCS |
| 14427 | winver.dwOSVersionInfoSize = sizeof(winver); |
| 14428 | if (!GetVersionEx((OSVERSIONINFO*)&winver)) { |
| 14429 | PyErr_SetFromWindowsErr(0); |
| 14430 | return -1; |
| 14431 | } |
| 14432 | #endif |
| 14433 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14434 | } |
| 14435 | |
| 14436 | /* Finalize the Unicode implementation */ |
| 14437 | |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 14438 | int |
| 14439 | PyUnicode_ClearFreeList(void) |
| 14440 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14441 | return 0; |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 14442 | } |
| 14443 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14444 | void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 14445 | _PyUnicode_Fini(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14446 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 14447 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14448 | |
Guido van Rossum | 4ae8ef8 | 2000-10-03 18:09:04 +0000 | [diff] [blame] | 14449 | Py_XDECREF(unicode_empty); |
| 14450 | unicode_empty = NULL; |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 14451 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 14452 | for (i = 0; i < 256; i++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14453 | if (unicode_latin1[i]) { |
| 14454 | Py_DECREF(unicode_latin1[i]); |
| 14455 | unicode_latin1[i] = NULL; |
| 14456 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 14457 | } |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 14458 | _PyUnicode_ClearStaticStrings(); |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 14459 | (void)PyUnicode_ClearFreeList(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14460 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 14461 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14462 | void |
| 14463 | PyUnicode_InternInPlace(PyObject **p) |
| 14464 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14465 | register PyObject *s = *p; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14466 | PyObject *t; |
Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 14467 | #ifdef Py_DEBUG |
| 14468 | assert(s != NULL); |
| 14469 | assert(_PyUnicode_CHECK(s)); |
| 14470 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14471 | if (s == NULL || !PyUnicode_Check(s)) |
Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 14472 | return; |
| 14473 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14474 | /* If it's a subclass, we don't really know what putting |
| 14475 | it in the interned dict might do. */ |
| 14476 | if (!PyUnicode_CheckExact(s)) |
| 14477 | return; |
| 14478 | if (PyUnicode_CHECK_INTERNED(s)) |
| 14479 | return; |
| 14480 | if (interned == NULL) { |
| 14481 | interned = PyDict_New(); |
| 14482 | if (interned == NULL) { |
| 14483 | PyErr_Clear(); /* Don't leave an exception */ |
| 14484 | return; |
| 14485 | } |
| 14486 | } |
| 14487 | /* It might be that the GetItem call fails even |
| 14488 | though the key is present in the dictionary, |
| 14489 | namely when this happens during a stack overflow. */ |
| 14490 | Py_ALLOW_RECURSION |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 14491 | t = PyDict_GetItem(interned, s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14492 | Py_END_ALLOW_RECURSION |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 14493 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14494 | if (t) { |
| 14495 | Py_INCREF(t); |
| 14496 | Py_DECREF(*p); |
| 14497 | *p = t; |
| 14498 | return; |
| 14499 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14500 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14501 | PyThreadState_GET()->recursion_critical = 1; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 14502 | if (PyDict_SetItem(interned, s, s) < 0) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14503 | PyErr_Clear(); |
| 14504 | PyThreadState_GET()->recursion_critical = 0; |
| 14505 | return; |
| 14506 | } |
| 14507 | PyThreadState_GET()->recursion_critical = 0; |
| 14508 | /* The two references in interned are not counted by refcnt. |
| 14509 | The deallocator will take care of this */ |
| 14510 | Py_REFCNT(s) -= 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14511 | _PyUnicode_STATE(s).interned = SSTATE_INTERNED_MORTAL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14512 | } |
| 14513 | |
| 14514 | void |
| 14515 | PyUnicode_InternImmortal(PyObject **p) |
| 14516 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14517 | PyUnicode_InternInPlace(p); |
| 14518 | if (PyUnicode_CHECK_INTERNED(*p) != SSTATE_INTERNED_IMMORTAL) { |
Victor Stinner | af9e4b8 | 2011-10-23 20:07:00 +0200 | [diff] [blame] | 14519 | _PyUnicode_STATE(*p).interned = SSTATE_INTERNED_IMMORTAL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14520 | Py_INCREF(*p); |
| 14521 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14522 | } |
| 14523 | |
| 14524 | PyObject * |
| 14525 | PyUnicode_InternFromString(const char *cp) |
| 14526 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14527 | PyObject *s = PyUnicode_FromString(cp); |
| 14528 | if (s == NULL) |
| 14529 | return NULL; |
| 14530 | PyUnicode_InternInPlace(&s); |
| 14531 | return s; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14532 | } |
| 14533 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 14534 | void |
| 14535 | _Py_ReleaseInternedUnicodeStrings(void) |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14536 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14537 | PyObject *keys; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14538 | PyObject *s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14539 | Py_ssize_t i, n; |
| 14540 | Py_ssize_t immortal_size = 0, mortal_size = 0; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14541 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14542 | if (interned == NULL || !PyDict_Check(interned)) |
| 14543 | return; |
| 14544 | keys = PyDict_Keys(interned); |
| 14545 | if (keys == NULL || !PyList_Check(keys)) { |
| 14546 | PyErr_Clear(); |
| 14547 | return; |
| 14548 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14549 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14550 | /* Since _Py_ReleaseInternedUnicodeStrings() is intended to help a leak |
| 14551 | detector, interned unicode strings are not forcibly deallocated; |
| 14552 | rather, we give them their stolen references back, and then clear |
| 14553 | and DECREF the interned dict. */ |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14554 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14555 | n = PyList_GET_SIZE(keys); |
| 14556 | fprintf(stderr, "releasing %" PY_FORMAT_SIZE_T "d interned strings\n", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14557 | n); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14558 | for (i = 0; i < n; i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14559 | s = PyList_GET_ITEM(keys, i); |
Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 14560 | if (PyUnicode_READY(s) == -1) { |
| 14561 | assert(0 && "could not ready string"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14562 | fprintf(stderr, "could not ready string\n"); |
Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 14563 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14564 | switch (PyUnicode_CHECK_INTERNED(s)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14565 | case SSTATE_NOT_INTERNED: |
| 14566 | /* XXX Shouldn't happen */ |
| 14567 | break; |
| 14568 | case SSTATE_INTERNED_IMMORTAL: |
| 14569 | Py_REFCNT(s) += 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14570 | immortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14571 | break; |
| 14572 | case SSTATE_INTERNED_MORTAL: |
| 14573 | Py_REFCNT(s) += 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14574 | mortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14575 | break; |
| 14576 | default: |
| 14577 | Py_FatalError("Inconsistent interned string state."); |
| 14578 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14579 | _PyUnicode_STATE(s).interned = SSTATE_NOT_INTERNED; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14580 | } |
| 14581 | fprintf(stderr, "total size of all interned strings: " |
| 14582 | "%" PY_FORMAT_SIZE_T "d/%" PY_FORMAT_SIZE_T "d " |
| 14583 | "mortal/immortal\n", mortal_size, immortal_size); |
| 14584 | Py_DECREF(keys); |
| 14585 | PyDict_Clear(interned); |
| 14586 | Py_DECREF(interned); |
| 14587 | interned = NULL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14588 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14589 | |
| 14590 | |
| 14591 | /********************* Unicode Iterator **************************/ |
| 14592 | |
| 14593 | typedef struct { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14594 | PyObject_HEAD |
| 14595 | Py_ssize_t it_index; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14596 | PyObject *it_seq; /* Set to NULL when iterator is exhausted */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14597 | } unicodeiterobject; |
| 14598 | |
| 14599 | static void |
| 14600 | unicodeiter_dealloc(unicodeiterobject *it) |
| 14601 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14602 | _PyObject_GC_UNTRACK(it); |
| 14603 | Py_XDECREF(it->it_seq); |
| 14604 | PyObject_GC_Del(it); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14605 | } |
| 14606 | |
| 14607 | static int |
| 14608 | unicodeiter_traverse(unicodeiterobject *it, visitproc visit, void *arg) |
| 14609 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14610 | Py_VISIT(it->it_seq); |
| 14611 | return 0; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14612 | } |
| 14613 | |
| 14614 | static PyObject * |
| 14615 | unicodeiter_next(unicodeiterobject *it) |
| 14616 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14617 | PyObject *seq, *item; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14618 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14619 | assert(it != NULL); |
| 14620 | seq = it->it_seq; |
| 14621 | if (seq == NULL) |
| 14622 | return NULL; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 14623 | assert(_PyUnicode_CHECK(seq)); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14624 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14625 | if (it->it_index < PyUnicode_GET_LENGTH(seq)) { |
| 14626 | int kind = PyUnicode_KIND(seq); |
| 14627 | void *data = PyUnicode_DATA(seq); |
| 14628 | Py_UCS4 chr = PyUnicode_READ(kind, data, it->it_index); |
| 14629 | item = PyUnicode_FromOrdinal(chr); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14630 | if (item != NULL) |
| 14631 | ++it->it_index; |
| 14632 | return item; |
| 14633 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14634 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14635 | Py_DECREF(seq); |
| 14636 | it->it_seq = NULL; |
| 14637 | return NULL; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14638 | } |
| 14639 | |
| 14640 | static PyObject * |
| 14641 | unicodeiter_len(unicodeiterobject *it) |
| 14642 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14643 | Py_ssize_t len = 0; |
| 14644 | if (it->it_seq) |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 14645 | len = PyUnicode_GET_LENGTH(it->it_seq) - it->it_index; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14646 | return PyLong_FromSsize_t(len); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14647 | } |
| 14648 | |
| 14649 | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
| 14650 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 14651 | static PyObject * |
| 14652 | unicodeiter_reduce(unicodeiterobject *it) |
| 14653 | { |
| 14654 | if (it->it_seq != NULL) { |
Antoine Pitrou | a701388 | 2012-04-05 00:04:20 +0200 | [diff] [blame] | 14655 | return Py_BuildValue("N(O)n", _PyObject_GetBuiltin("iter"), |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 14656 | it->it_seq, it->it_index); |
| 14657 | } else { |
| 14658 | PyObject *u = PyUnicode_FromUnicode(NULL, 0); |
| 14659 | if (u == NULL) |
| 14660 | return NULL; |
Antoine Pitrou | a701388 | 2012-04-05 00:04:20 +0200 | [diff] [blame] | 14661 | return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), u); |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 14662 | } |
| 14663 | } |
| 14664 | |
| 14665 | PyDoc_STRVAR(reduce_doc, "Return state information for pickling."); |
| 14666 | |
| 14667 | static PyObject * |
| 14668 | unicodeiter_setstate(unicodeiterobject *it, PyObject *state) |
| 14669 | { |
| 14670 | Py_ssize_t index = PyLong_AsSsize_t(state); |
| 14671 | if (index == -1 && PyErr_Occurred()) |
| 14672 | return NULL; |
| 14673 | if (index < 0) |
| 14674 | index = 0; |
| 14675 | it->it_index = index; |
| 14676 | Py_RETURN_NONE; |
| 14677 | } |
| 14678 | |
| 14679 | PyDoc_STRVAR(setstate_doc, "Set state information for unpickling."); |
| 14680 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14681 | static PyMethodDef unicodeiter_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14682 | {"__length_hint__", (PyCFunction)unicodeiter_len, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14683 | length_hint_doc}, |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 14684 | {"__reduce__", (PyCFunction)unicodeiter_reduce, METH_NOARGS, |
| 14685 | reduce_doc}, |
| 14686 | {"__setstate__", (PyCFunction)unicodeiter_setstate, METH_O, |
| 14687 | setstate_doc}, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14688 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14689 | }; |
| 14690 | |
| 14691 | PyTypeObject PyUnicodeIter_Type = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14692 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 14693 | "str_iterator", /* tp_name */ |
| 14694 | sizeof(unicodeiterobject), /* tp_basicsize */ |
| 14695 | 0, /* tp_itemsize */ |
| 14696 | /* methods */ |
| 14697 | (destructor)unicodeiter_dealloc, /* tp_dealloc */ |
| 14698 | 0, /* tp_print */ |
| 14699 | 0, /* tp_getattr */ |
| 14700 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 14701 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14702 | 0, /* tp_repr */ |
| 14703 | 0, /* tp_as_number */ |
| 14704 | 0, /* tp_as_sequence */ |
| 14705 | 0, /* tp_as_mapping */ |
| 14706 | 0, /* tp_hash */ |
| 14707 | 0, /* tp_call */ |
| 14708 | 0, /* tp_str */ |
| 14709 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 14710 | 0, /* tp_setattro */ |
| 14711 | 0, /* tp_as_buffer */ |
| 14712 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 14713 | 0, /* tp_doc */ |
| 14714 | (traverseproc)unicodeiter_traverse, /* tp_traverse */ |
| 14715 | 0, /* tp_clear */ |
| 14716 | 0, /* tp_richcompare */ |
| 14717 | 0, /* tp_weaklistoffset */ |
| 14718 | PyObject_SelfIter, /* tp_iter */ |
| 14719 | (iternextfunc)unicodeiter_next, /* tp_iternext */ |
| 14720 | unicodeiter_methods, /* tp_methods */ |
| 14721 | 0, |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14722 | }; |
| 14723 | |
| 14724 | static PyObject * |
| 14725 | unicode_iter(PyObject *seq) |
| 14726 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14727 | unicodeiterobject *it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14728 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14729 | if (!PyUnicode_Check(seq)) { |
| 14730 | PyErr_BadInternalCall(); |
| 14731 | return NULL; |
| 14732 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14733 | if (PyUnicode_READY(seq) == -1) |
| 14734 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14735 | it = PyObject_GC_New(unicodeiterobject, &PyUnicodeIter_Type); |
| 14736 | if (it == NULL) |
| 14737 | return NULL; |
| 14738 | it->it_index = 0; |
| 14739 | Py_INCREF(seq); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14740 | it->it_seq = seq; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14741 | _PyObject_GC_TRACK(it); |
| 14742 | return (PyObject *)it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14743 | } |
| 14744 | |
Martin v. Löwis | 0d3072e | 2011-10-31 08:40:56 +0100 | [diff] [blame] | 14745 | |
| 14746 | size_t |
| 14747 | Py_UNICODE_strlen(const Py_UNICODE *u) |
| 14748 | { |
| 14749 | int res = 0; |
| 14750 | while(*u++) |
| 14751 | res++; |
| 14752 | return res; |
| 14753 | } |
| 14754 | |
| 14755 | Py_UNICODE* |
| 14756 | Py_UNICODE_strcpy(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 14757 | { |
| 14758 | Py_UNICODE *u = s1; |
| 14759 | while ((*u++ = *s2++)); |
| 14760 | return s1; |
| 14761 | } |
| 14762 | |
| 14763 | Py_UNICODE* |
| 14764 | Py_UNICODE_strncpy(Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 14765 | { |
| 14766 | Py_UNICODE *u = s1; |
| 14767 | while ((*u++ = *s2++)) |
| 14768 | if (n-- == 0) |
| 14769 | break; |
| 14770 | return s1; |
| 14771 | } |
| 14772 | |
| 14773 | Py_UNICODE* |
| 14774 | Py_UNICODE_strcat(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 14775 | { |
| 14776 | Py_UNICODE *u1 = s1; |
| 14777 | u1 += Py_UNICODE_strlen(u1); |
| 14778 | Py_UNICODE_strcpy(u1, s2); |
| 14779 | return s1; |
| 14780 | } |
| 14781 | |
| 14782 | int |
| 14783 | Py_UNICODE_strcmp(const Py_UNICODE *s1, const Py_UNICODE *s2) |
| 14784 | { |
| 14785 | while (*s1 && *s2 && *s1 == *s2) |
| 14786 | s1++, s2++; |
| 14787 | if (*s1 && *s2) |
| 14788 | return (*s1 < *s2) ? -1 : +1; |
| 14789 | if (*s1) |
| 14790 | return 1; |
| 14791 | if (*s2) |
| 14792 | return -1; |
| 14793 | return 0; |
| 14794 | } |
| 14795 | |
| 14796 | int |
| 14797 | Py_UNICODE_strncmp(const Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 14798 | { |
| 14799 | register Py_UNICODE u1, u2; |
| 14800 | for (; n != 0; n--) { |
| 14801 | u1 = *s1; |
| 14802 | u2 = *s2; |
| 14803 | if (u1 != u2) |
| 14804 | return (u1 < u2) ? -1 : +1; |
| 14805 | if (u1 == '\0') |
| 14806 | return 0; |
| 14807 | s1++; |
| 14808 | s2++; |
| 14809 | } |
| 14810 | return 0; |
| 14811 | } |
| 14812 | |
| 14813 | Py_UNICODE* |
| 14814 | Py_UNICODE_strchr(const Py_UNICODE *s, Py_UNICODE c) |
| 14815 | { |
| 14816 | const Py_UNICODE *p; |
| 14817 | for (p = s; *p; p++) |
| 14818 | if (*p == c) |
| 14819 | return (Py_UNICODE*)p; |
| 14820 | return NULL; |
| 14821 | } |
| 14822 | |
| 14823 | Py_UNICODE* |
| 14824 | Py_UNICODE_strrchr(const Py_UNICODE *s, Py_UNICODE c) |
| 14825 | { |
| 14826 | const Py_UNICODE *p; |
| 14827 | p = s + Py_UNICODE_strlen(s); |
| 14828 | while (p != s) { |
| 14829 | p--; |
| 14830 | if (*p == c) |
| 14831 | return (Py_UNICODE*)p; |
| 14832 | } |
| 14833 | return NULL; |
| 14834 | } |
Victor Stinner | 331ea92 | 2010-08-10 16:37:20 +0000 | [diff] [blame] | 14835 | |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14836 | Py_UNICODE* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14837 | PyUnicode_AsUnicodeCopy(PyObject *unicode) |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14838 | { |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 14839 | Py_UNICODE *u, *copy; |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14840 | Py_ssize_t len, size; |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14841 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14842 | if (!PyUnicode_Check(unicode)) { |
| 14843 | PyErr_BadArgument(); |
| 14844 | return NULL; |
| 14845 | } |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14846 | u = PyUnicode_AsUnicodeAndSize(unicode, &len); |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 14847 | if (u == NULL) |
| 14848 | return NULL; |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14849 | /* Ensure we won't overflow the size. */ |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14850 | if (len > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14851 | PyErr_NoMemory(); |
| 14852 | return NULL; |
| 14853 | } |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14854 | size = len + 1; /* copy the null character */ |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14855 | size *= sizeof(Py_UNICODE); |
| 14856 | copy = PyMem_Malloc(size); |
| 14857 | if (copy == NULL) { |
| 14858 | PyErr_NoMemory(); |
| 14859 | return NULL; |
| 14860 | } |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 14861 | memcpy(copy, u, size); |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14862 | return copy; |
| 14863 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 14864 | |
Georg Brandl | 66c221e | 2010-10-14 07:04:07 +0000 | [diff] [blame] | 14865 | /* A _string module, to export formatter_parser and formatter_field_name_split |
| 14866 | to the string.Formatter class implemented in Python. */ |
| 14867 | |
| 14868 | static PyMethodDef _string_methods[] = { |
| 14869 | {"formatter_field_name_split", (PyCFunction) formatter_field_name_split, |
| 14870 | METH_O, PyDoc_STR("split the argument as a field name")}, |
| 14871 | {"formatter_parser", (PyCFunction) formatter_parser, |
| 14872 | METH_O, PyDoc_STR("parse the argument as a format string")}, |
| 14873 | {NULL, NULL} |
| 14874 | }; |
| 14875 | |
| 14876 | static struct PyModuleDef _string_module = { |
| 14877 | PyModuleDef_HEAD_INIT, |
| 14878 | "_string", |
| 14879 | PyDoc_STR("string helper module"), |
| 14880 | 0, |
| 14881 | _string_methods, |
| 14882 | NULL, |
| 14883 | NULL, |
| 14884 | NULL, |
| 14885 | NULL |
| 14886 | }; |
| 14887 | |
| 14888 | PyMODINIT_FUNC |
| 14889 | PyInit__string(void) |
| 14890 | { |
| 14891 | return PyModule_Create(&_string_module); |
| 14892 | } |
| 14893 | |
| 14894 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 14895 | #ifdef __cplusplus |
| 14896 | } |
| 14897 | #endif |