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 | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 114 | #undef PyUnicode_READY |
| 115 | #define PyUnicode_READY(op) \ |
| 116 | (assert(_PyUnicode_CHECK(op)), \ |
| 117 | (PyUnicode_IS_READY(op) ? \ |
Victor Stinner | a5f9163 | 2011-10-04 01:07:11 +0200 | [diff] [blame] | 118 | 0 : \ |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 119 | _PyUnicode_Ready(op))) |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 120 | |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 121 | #define _PyUnicode_SHARE_UTF8(op) \ |
| 122 | (assert(_PyUnicode_CHECK(op)), \ |
| 123 | assert(!PyUnicode_IS_COMPACT_ASCII(op)), \ |
| 124 | (_PyUnicode_UTF8(op) == PyUnicode_DATA(op))) |
| 125 | #define _PyUnicode_SHARE_WSTR(op) \ |
| 126 | (assert(_PyUnicode_CHECK(op)), \ |
| 127 | (_PyUnicode_WSTR(unicode) == PyUnicode_DATA(op))) |
| 128 | |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 129 | /* true if the Unicode object has an allocated UTF-8 memory block |
| 130 | (not shared with other data) */ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 131 | #define _PyUnicode_HAS_UTF8_MEMORY(op) \ |
| 132 | (assert(_PyUnicode_CHECK(op)), \ |
| 133 | (!PyUnicode_IS_COMPACT_ASCII(op) \ |
| 134 | && _PyUnicode_UTF8(op) \ |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 135 | && _PyUnicode_UTF8(op) != PyUnicode_DATA(op))) |
| 136 | |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 137 | /* true if the Unicode object has an allocated wstr memory block |
| 138 | (not shared with other data) */ |
| 139 | #define _PyUnicode_HAS_WSTR_MEMORY(op) \ |
| 140 | (assert(_PyUnicode_CHECK(op)), \ |
| 141 | (_PyUnicode_WSTR(op) && \ |
| 142 | (!PyUnicode_IS_READY(op) || \ |
| 143 | _PyUnicode_WSTR(op) != PyUnicode_DATA(op)))) |
| 144 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 145 | /* Generic helper macro to convert characters of different types. |
| 146 | from_type and to_type have to be valid type names, begin and end |
| 147 | are pointers to the source characters which should be of type |
| 148 | "from_type *". to is a pointer of type "to_type *" and points to the |
| 149 | buffer where the result characters are written to. */ |
| 150 | #define _PyUnicode_CONVERT_BYTES(from_type, to_type, begin, end, to) \ |
| 151 | do { \ |
Antoine Pitrou | e459a08 | 2011-10-11 20:58:41 +0200 | [diff] [blame] | 152 | to_type *_to = (to_type *) to; \ |
| 153 | const from_type *_iter = (begin); \ |
| 154 | const from_type *_end = (end); \ |
| 155 | Py_ssize_t n = (_end) - (_iter); \ |
| 156 | const from_type *_unrolled_end = \ |
| 157 | _iter + (n & ~ (Py_ssize_t) 3); \ |
| 158 | while (_iter < (_unrolled_end)) { \ |
| 159 | _to[0] = (to_type) _iter[0]; \ |
| 160 | _to[1] = (to_type) _iter[1]; \ |
| 161 | _to[2] = (to_type) _iter[2]; \ |
| 162 | _to[3] = (to_type) _iter[3]; \ |
| 163 | _iter += 4; _to += 4; \ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 164 | } \ |
Antoine Pitrou | e459a08 | 2011-10-11 20:58:41 +0200 | [diff] [blame] | 165 | while (_iter < (_end)) \ |
| 166 | *_to++ = (to_type) *_iter++; \ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 167 | } while (0) |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 168 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 169 | /* This dictionary holds all interned unicode strings. Note that references |
| 170 | to strings in this dictionary are *not* counted in the string's ob_refcnt. |
| 171 | When the interned string reaches a refcnt of 0 the string deallocation |
| 172 | function will delete the reference from this dictionary. |
| 173 | |
| 174 | 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] | 175 | 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] | 176 | */ |
| 177 | static PyObject *interned; |
| 178 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 179 | /* The empty Unicode object is shared to improve performance. */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 180 | static PyObject *unicode_empty; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 181 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 182 | /* List of static strings. */ |
| 183 | static _Py_Identifier *static_strings; |
| 184 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 185 | /* Single character Unicode strings in the Latin-1 range are being |
| 186 | shared as well. */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 187 | static PyObject *unicode_latin1[256]; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 188 | |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 189 | /* Fast detection of the most frequent whitespace characters */ |
| 190 | const unsigned char _Py_ascii_whitespace[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 191 | 0, 0, 0, 0, 0, 0, 0, 0, |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 192 | /* case 0x0009: * CHARACTER TABULATION */ |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 193 | /* case 0x000A: * LINE FEED */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 194 | /* case 0x000B: * LINE TABULATION */ |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 195 | /* case 0x000C: * FORM FEED */ |
| 196 | /* case 0x000D: * CARRIAGE RETURN */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 197 | 0, 1, 1, 1, 1, 1, 0, 0, |
| 198 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 199 | /* case 0x001C: * FILE SEPARATOR */ |
| 200 | /* case 0x001D: * GROUP SEPARATOR */ |
| 201 | /* case 0x001E: * RECORD SEPARATOR */ |
| 202 | /* case 0x001F: * UNIT SEPARATOR */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 203 | 0, 0, 0, 0, 1, 1, 1, 1, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 204 | /* case 0x0020: * SPACE */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 205 | 1, 0, 0, 0, 0, 0, 0, 0, |
| 206 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 207 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 208 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 209 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 210 | 0, 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, |
| 214 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 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 |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 218 | }; |
| 219 | |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 220 | /* forward */ |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 221 | static PyUnicodeObject *_PyUnicode_New(Py_ssize_t length); |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 222 | static PyObject* get_latin1_char(unsigned char ch); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 223 | static void copy_characters( |
| 224 | PyObject *to, Py_ssize_t to_start, |
| 225 | PyObject *from, Py_ssize_t from_start, |
| 226 | Py_ssize_t how_many); |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 227 | static int unicode_modifiable(PyObject *unicode); |
| 228 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 229 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 230 | static PyObject * |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 231 | unicode_fromascii(const unsigned char *s, Py_ssize_t size); |
| 232 | static PyObject * |
| 233 | _PyUnicode_FromUCS1(const unsigned char *s, Py_ssize_t size); |
| 234 | static PyObject * |
| 235 | _PyUnicode_FromUCS2(const Py_UCS2 *s, Py_ssize_t size); |
| 236 | static PyObject * |
| 237 | _PyUnicode_FromUCS4(const Py_UCS4 *s, Py_ssize_t size); |
| 238 | |
| 239 | static PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 240 | unicode_encode_call_errorhandler(const char *errors, |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 241 | PyObject **errorHandler,const char *encoding, const char *reason, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 242 | PyObject *unicode, PyObject **exceptionObject, |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 243 | Py_ssize_t startpos, Py_ssize_t endpos, Py_ssize_t *newpos); |
| 244 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 245 | static void |
| 246 | raise_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 247 | const char *encoding, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 248 | PyObject *unicode, |
| 249 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 250 | const char *reason); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 251 | |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 252 | /* Same for linebreaks */ |
| 253 | static unsigned char ascii_linebreak[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 254 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 255 | /* 0x000A, * LINE FEED */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 256 | /* 0x000B, * LINE TABULATION */ |
| 257 | /* 0x000C, * FORM FEED */ |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 258 | /* 0x000D, * CARRIAGE RETURN */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 259 | 0, 0, 1, 1, 1, 1, 0, 0, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 260 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 261 | /* 0x001C, * FILE SEPARATOR */ |
| 262 | /* 0x001D, * GROUP SEPARATOR */ |
| 263 | /* 0x001E, * RECORD SEPARATOR */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 264 | 0, 0, 0, 0, 1, 1, 1, 0, |
| 265 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 266 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 267 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 268 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 269 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 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, |
| 274 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 275 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 276 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 277 | 0, 0, 0, 0, 0, 0, 0, 0 |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 278 | }; |
| 279 | |
Ezio Melotti | 48a2f8f | 2011-09-29 00:18:19 +0300 | [diff] [blame] | 280 | /* The max unicode value is always 0x10FFFF while using the PEP-393 API. |
| 281 | 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] | 282 | Py_UNICODE |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 283 | PyUnicode_GetMax(void) |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 284 | { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 285 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 286 | return 0x10FFFF; |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 287 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 288 | /* This is actually an illegal character, so it should |
| 289 | not be passed to unichr. */ |
| 290 | return 0xFFFF; |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 291 | #endif |
| 292 | } |
| 293 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 294 | #ifdef Py_DEBUG |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 295 | int |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 296 | _PyUnicode_CheckConsistency(PyObject *op, int check_content) |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 297 | { |
| 298 | PyASCIIObject *ascii; |
| 299 | unsigned int kind; |
| 300 | |
| 301 | assert(PyUnicode_Check(op)); |
| 302 | |
| 303 | ascii = (PyASCIIObject *)op; |
| 304 | kind = ascii->state.kind; |
| 305 | |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 306 | if (ascii->state.ascii == 1 && ascii->state.compact == 1) { |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 307 | assert(kind == PyUnicode_1BYTE_KIND); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 308 | assert(ascii->state.ready == 1); |
| 309 | } |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 310 | else { |
Victor Stinner | 85041a5 | 2011-10-03 14:42:39 +0200 | [diff] [blame] | 311 | PyCompactUnicodeObject *compact = (PyCompactUnicodeObject *)op; |
Victor Stinner | 7f11ad4 | 2011-10-04 00:00:20 +0200 | [diff] [blame] | 312 | void *data; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 313 | |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 314 | if (ascii->state.compact == 1) { |
| 315 | data = compact + 1; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 316 | assert(kind == PyUnicode_1BYTE_KIND |
| 317 | || kind == PyUnicode_2BYTE_KIND |
| 318 | || kind == PyUnicode_4BYTE_KIND); |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 319 | assert(ascii->state.ascii == 0); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 320 | assert(ascii->state.ready == 1); |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 321 | assert (compact->utf8 != data); |
Victor Stinner | e30c0a1 | 2011-11-04 20:54:05 +0100 | [diff] [blame] | 322 | } |
| 323 | else { |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 324 | PyUnicodeObject *unicode = (PyUnicodeObject *)op; |
| 325 | |
| 326 | data = unicode->data.any; |
| 327 | if (kind == PyUnicode_WCHAR_KIND) { |
Victor Stinner | e30c0a1 | 2011-11-04 20:54:05 +0100 | [diff] [blame] | 328 | assert(ascii->length == 0); |
| 329 | assert(ascii->hash == -1); |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 330 | assert(ascii->state.compact == 0); |
| 331 | assert(ascii->state.ascii == 0); |
| 332 | assert(ascii->state.ready == 0); |
Victor Stinner | e30c0a1 | 2011-11-04 20:54:05 +0100 | [diff] [blame] | 333 | assert(ascii->state.interned == SSTATE_NOT_INTERNED); |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 334 | assert(ascii->wstr != NULL); |
| 335 | assert(data == NULL); |
| 336 | assert(compact->utf8 == NULL); |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 337 | } |
| 338 | else { |
| 339 | assert(kind == PyUnicode_1BYTE_KIND |
| 340 | || kind == PyUnicode_2BYTE_KIND |
| 341 | || kind == PyUnicode_4BYTE_KIND); |
| 342 | assert(ascii->state.compact == 0); |
| 343 | assert(ascii->state.ready == 1); |
| 344 | assert(data != NULL); |
| 345 | if (ascii->state.ascii) { |
| 346 | assert (compact->utf8 == data); |
| 347 | assert (compact->utf8_length == ascii->length); |
| 348 | } |
| 349 | else |
| 350 | assert (compact->utf8 != data); |
| 351 | } |
| 352 | } |
| 353 | if (kind != PyUnicode_WCHAR_KIND) { |
Victor Stinner | 7f11ad4 | 2011-10-04 00:00:20 +0200 | [diff] [blame] | 354 | if ( |
| 355 | #if SIZEOF_WCHAR_T == 2 |
| 356 | kind == PyUnicode_2BYTE_KIND |
| 357 | #else |
| 358 | kind == PyUnicode_4BYTE_KIND |
| 359 | #endif |
| 360 | ) |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 361 | { |
| 362 | assert(ascii->wstr == data); |
| 363 | assert(compact->wstr_length == ascii->length); |
| 364 | } else |
| 365 | assert(ascii->wstr != data); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 366 | } |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 367 | |
| 368 | if (compact->utf8 == NULL) |
| 369 | assert(compact->utf8_length == 0); |
| 370 | if (ascii->wstr == NULL) |
| 371 | assert(compact->wstr_length == 0); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 372 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 373 | /* check that the best kind is used */ |
| 374 | if (check_content && kind != PyUnicode_WCHAR_KIND) |
| 375 | { |
| 376 | Py_ssize_t i; |
| 377 | Py_UCS4 maxchar = 0; |
| 378 | void *data = PyUnicode_DATA(ascii); |
| 379 | for (i=0; i < ascii->length; i++) |
| 380 | { |
| 381 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 382 | if (ch > maxchar) |
| 383 | maxchar = ch; |
| 384 | } |
| 385 | if (kind == PyUnicode_1BYTE_KIND) { |
Victor Stinner | 77faf69 | 2011-11-20 18:56:05 +0100 | [diff] [blame] | 386 | if (ascii->state.ascii == 0) { |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 387 | assert(maxchar >= 128); |
Victor Stinner | 77faf69 | 2011-11-20 18:56:05 +0100 | [diff] [blame] | 388 | assert(maxchar <= 255); |
| 389 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 390 | else |
| 391 | assert(maxchar < 128); |
| 392 | } |
Victor Stinner | 77faf69 | 2011-11-20 18:56:05 +0100 | [diff] [blame] | 393 | else if (kind == PyUnicode_2BYTE_KIND) { |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 394 | assert(maxchar >= 0x100); |
Victor Stinner | 77faf69 | 2011-11-20 18:56:05 +0100 | [diff] [blame] | 395 | assert(maxchar <= 0xFFFF); |
| 396 | } |
| 397 | else { |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 398 | assert(maxchar >= 0x10000); |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 399 | assert(maxchar <= MAX_UNICODE); |
Victor Stinner | 77faf69 | 2011-11-20 18:56:05 +0100 | [diff] [blame] | 400 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 401 | } |
Benjamin Peterson | ccc51c1 | 2011-10-03 19:34:12 -0400 | [diff] [blame] | 402 | return 1; |
| 403 | } |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 404 | #endif |
| 405 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 406 | static PyObject* |
| 407 | unicode_result_wchar(PyObject *unicode) |
| 408 | { |
| 409 | #ifndef Py_DEBUG |
| 410 | Py_ssize_t len; |
| 411 | |
| 412 | assert(Py_REFCNT(unicode) == 1); |
| 413 | |
| 414 | len = _PyUnicode_WSTR_LENGTH(unicode); |
| 415 | if (len == 0) { |
| 416 | Py_INCREF(unicode_empty); |
| 417 | Py_DECREF(unicode); |
| 418 | return unicode_empty; |
| 419 | } |
| 420 | |
| 421 | if (len == 1) { |
| 422 | wchar_t ch = _PyUnicode_WSTR(unicode)[0]; |
| 423 | if (ch < 256) { |
| 424 | PyObject *latin1_char = get_latin1_char((unsigned char)ch); |
| 425 | Py_DECREF(unicode); |
| 426 | return latin1_char; |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | if (_PyUnicode_Ready(unicode) < 0) { |
| 431 | Py_XDECREF(unicode); |
| 432 | return NULL; |
| 433 | } |
| 434 | #else |
| 435 | /* don't make the result ready in debug mode to ensure that the caller |
| 436 | makes the string ready before using it */ |
| 437 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
| 438 | #endif |
| 439 | return unicode; |
| 440 | } |
| 441 | |
| 442 | static PyObject* |
| 443 | unicode_result_ready(PyObject *unicode) |
| 444 | { |
| 445 | Py_ssize_t length; |
| 446 | |
| 447 | length = PyUnicode_GET_LENGTH(unicode); |
| 448 | if (length == 0) { |
| 449 | if (unicode != unicode_empty) { |
| 450 | Py_INCREF(unicode_empty); |
| 451 | Py_DECREF(unicode); |
| 452 | } |
| 453 | return unicode_empty; |
| 454 | } |
| 455 | |
| 456 | if (length == 1) { |
| 457 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, 0); |
| 458 | if (ch < 256) { |
| 459 | PyObject *latin1_char = unicode_latin1[ch]; |
| 460 | if (latin1_char != NULL) { |
| 461 | if (unicode != latin1_char) { |
| 462 | Py_INCREF(latin1_char); |
| 463 | Py_DECREF(unicode); |
| 464 | } |
| 465 | return latin1_char; |
| 466 | } |
| 467 | else { |
| 468 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
| 469 | Py_INCREF(unicode); |
| 470 | unicode_latin1[ch] = unicode; |
| 471 | return unicode; |
| 472 | } |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
| 477 | return unicode; |
| 478 | } |
| 479 | |
| 480 | static PyObject* |
| 481 | unicode_result(PyObject *unicode) |
| 482 | { |
| 483 | assert(_PyUnicode_CHECK(unicode)); |
| 484 | if (PyUnicode_IS_READY(unicode)) |
| 485 | return unicode_result_ready(unicode); |
| 486 | else |
| 487 | return unicode_result_wchar(unicode); |
| 488 | } |
| 489 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 490 | static PyObject* |
| 491 | unicode_result_unchanged(PyObject *unicode) |
| 492 | { |
| 493 | if (PyUnicode_CheckExact(unicode)) { |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 494 | if (PyUnicode_READY(unicode) == -1) |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 495 | return NULL; |
| 496 | Py_INCREF(unicode); |
| 497 | return unicode; |
| 498 | } |
| 499 | else |
| 500 | /* Subtype -- return genuine unicode string with the same value. */ |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 501 | return _PyUnicode_Copy(unicode); |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 502 | } |
| 503 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 504 | #ifdef HAVE_MBCS |
| 505 | static OSVERSIONINFOEX winver; |
| 506 | #endif |
| 507 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 508 | /* --- Bloom Filters ----------------------------------------------------- */ |
| 509 | |
| 510 | /* stuff to implement simple "bloom filters" for Unicode characters. |
| 511 | to keep things simple, we use a single bitmask, using the least 5 |
| 512 | bits from each unicode characters as the bit index. */ |
| 513 | |
| 514 | /* the linebreak mask is set up by Unicode_Init below */ |
| 515 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 516 | #if LONG_BIT >= 128 |
| 517 | #define BLOOM_WIDTH 128 |
| 518 | #elif LONG_BIT >= 64 |
| 519 | #define BLOOM_WIDTH 64 |
| 520 | #elif LONG_BIT >= 32 |
| 521 | #define BLOOM_WIDTH 32 |
| 522 | #else |
| 523 | #error "LONG_BIT is smaller than 32" |
| 524 | #endif |
| 525 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 526 | #define BLOOM_MASK unsigned long |
| 527 | |
| 528 | static BLOOM_MASK bloom_linebreak; |
| 529 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 530 | #define BLOOM_ADD(mask, ch) ((mask |= (1UL << ((ch) & (BLOOM_WIDTH - 1))))) |
| 531 | #define BLOOM(mask, ch) ((mask & (1UL << ((ch) & (BLOOM_WIDTH - 1))))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 532 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 533 | #define BLOOM_LINEBREAK(ch) \ |
| 534 | ((ch) < 128U ? ascii_linebreak[(ch)] : \ |
| 535 | (BLOOM(bloom_linebreak, (ch)) && Py_UNICODE_ISLINEBREAK(ch))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 536 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 537 | Py_LOCAL_INLINE(BLOOM_MASK) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 538 | make_bloom_mask(int kind, void* ptr, Py_ssize_t len) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 539 | { |
| 540 | /* calculate simple bloom-style bitmask for a given unicode string */ |
| 541 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 542 | BLOOM_MASK mask; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 543 | Py_ssize_t i; |
| 544 | |
| 545 | mask = 0; |
| 546 | for (i = 0; i < len; i++) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 547 | BLOOM_ADD(mask, PyUnicode_READ(kind, ptr, i)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 548 | |
| 549 | return mask; |
| 550 | } |
| 551 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 552 | #define BLOOM_MEMBER(mask, chr, str) \ |
| 553 | (BLOOM(mask, chr) \ |
| 554 | && (PyUnicode_FindChar(str, chr, 0, PyUnicode_GET_LENGTH(str), 1) >= 0)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 555 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 556 | /* Compilation of templated routines */ |
| 557 | |
| 558 | #include "stringlib/asciilib.h" |
| 559 | #include "stringlib/fastsearch.h" |
| 560 | #include "stringlib/partition.h" |
| 561 | #include "stringlib/split.h" |
| 562 | #include "stringlib/count.h" |
| 563 | #include "stringlib/find.h" |
| 564 | #include "stringlib/find_max_char.h" |
| 565 | #include "stringlib/localeutil.h" |
| 566 | #include "stringlib/undef.h" |
| 567 | |
| 568 | #include "stringlib/ucs1lib.h" |
| 569 | #include "stringlib/fastsearch.h" |
| 570 | #include "stringlib/partition.h" |
| 571 | #include "stringlib/split.h" |
| 572 | #include "stringlib/count.h" |
| 573 | #include "stringlib/find.h" |
| 574 | #include "stringlib/find_max_char.h" |
| 575 | #include "stringlib/localeutil.h" |
| 576 | #include "stringlib/undef.h" |
| 577 | |
| 578 | #include "stringlib/ucs2lib.h" |
| 579 | #include "stringlib/fastsearch.h" |
| 580 | #include "stringlib/partition.h" |
| 581 | #include "stringlib/split.h" |
| 582 | #include "stringlib/count.h" |
| 583 | #include "stringlib/find.h" |
| 584 | #include "stringlib/find_max_char.h" |
| 585 | #include "stringlib/localeutil.h" |
| 586 | #include "stringlib/undef.h" |
| 587 | |
| 588 | #include "stringlib/ucs4lib.h" |
| 589 | #include "stringlib/fastsearch.h" |
| 590 | #include "stringlib/partition.h" |
| 591 | #include "stringlib/split.h" |
| 592 | #include "stringlib/count.h" |
| 593 | #include "stringlib/find.h" |
| 594 | #include "stringlib/find_max_char.h" |
| 595 | #include "stringlib/localeutil.h" |
| 596 | #include "stringlib/undef.h" |
| 597 | |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 598 | #include "stringlib/unicodedefs.h" |
| 599 | #include "stringlib/fastsearch.h" |
| 600 | #include "stringlib/count.h" |
| 601 | #include "stringlib/find.h" |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 602 | #include "stringlib/undef.h" |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 603 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 604 | /* --- Unicode Object ----------------------------------------------------- */ |
| 605 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 606 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 607 | fixup(PyObject *self, Py_UCS4 (*fixfct)(PyObject *s)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 608 | |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 609 | Py_LOCAL_INLINE(Py_ssize_t) findchar(void *s, int kind, |
| 610 | Py_ssize_t size, Py_UCS4 ch, |
| 611 | int direction) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 612 | { |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 613 | int mode = (direction == 1) ? FAST_SEARCH : FAST_RSEARCH; |
| 614 | |
| 615 | switch (kind) { |
| 616 | case PyUnicode_1BYTE_KIND: |
| 617 | { |
| 618 | Py_UCS1 ch1 = (Py_UCS1) ch; |
| 619 | if (ch1 == ch) |
| 620 | return ucs1lib_fastsearch((Py_UCS1 *) s, size, &ch1, 1, 0, mode); |
| 621 | else |
| 622 | return -1; |
| 623 | } |
| 624 | case PyUnicode_2BYTE_KIND: |
| 625 | { |
| 626 | Py_UCS2 ch2 = (Py_UCS2) ch; |
| 627 | if (ch2 == ch) |
| 628 | return ucs2lib_fastsearch((Py_UCS2 *) s, size, &ch2, 1, 0, mode); |
| 629 | else |
| 630 | return -1; |
| 631 | } |
| 632 | case PyUnicode_4BYTE_KIND: |
| 633 | return ucs4lib_fastsearch((Py_UCS4 *) s, size, &ch, 1, 0, mode); |
| 634 | default: |
| 635 | assert(0); |
| 636 | return -1; |
Victor Stinner | 9e7a1bc | 2011-10-13 00:18:12 +0200 | [diff] [blame] | 637 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 638 | } |
| 639 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 640 | static PyObject* |
| 641 | resize_compact(PyObject *unicode, Py_ssize_t length) |
| 642 | { |
| 643 | Py_ssize_t char_size; |
| 644 | Py_ssize_t struct_size; |
| 645 | Py_ssize_t new_size; |
| 646 | int share_wstr; |
Victor Stinner | 84def37 | 2011-12-11 20:04:56 +0100 | [diff] [blame] | 647 | PyObject *new_unicode; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 648 | assert(PyUnicode_IS_READY(unicode)); |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 649 | assert(PyUnicode_IS_COMPACT(unicode)); |
| 650 | |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 651 | char_size = PyUnicode_KIND(unicode); |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 652 | if (PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 653 | struct_size = sizeof(PyASCIIObject); |
| 654 | else |
| 655 | struct_size = sizeof(PyCompactUnicodeObject); |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 656 | share_wstr = _PyUnicode_SHARE_WSTR(unicode); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 657 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 658 | if (length > ((PY_SSIZE_T_MAX - struct_size) / char_size - 1)) { |
| 659 | PyErr_NoMemory(); |
| 660 | return NULL; |
| 661 | } |
| 662 | new_size = (struct_size + (length + 1) * char_size); |
| 663 | |
Victor Stinner | 84def37 | 2011-12-11 20:04:56 +0100 | [diff] [blame] | 664 | _Py_DEC_REFTOTAL; |
| 665 | _Py_ForgetReference(unicode); |
| 666 | |
| 667 | new_unicode = (PyObject *)PyObject_REALLOC((char *)unicode, new_size); |
| 668 | if (new_unicode == NULL) { |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 669 | _Py_NewReference(unicode); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 670 | PyErr_NoMemory(); |
| 671 | return NULL; |
| 672 | } |
Victor Stinner | 84def37 | 2011-12-11 20:04:56 +0100 | [diff] [blame] | 673 | unicode = new_unicode; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 674 | _Py_NewReference(unicode); |
Victor Stinner | 84def37 | 2011-12-11 20:04:56 +0100 | [diff] [blame] | 675 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 676 | _PyUnicode_LENGTH(unicode) = length; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 677 | if (share_wstr) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 678 | _PyUnicode_WSTR(unicode) = PyUnicode_DATA(unicode); |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 679 | if (!PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 680 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 681 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 682 | PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode), |
| 683 | length, 0); |
| 684 | return unicode; |
| 685 | } |
| 686 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 687 | static int |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 688 | resize_inplace(PyObject *unicode, Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 689 | { |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 690 | wchar_t *wstr; |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 691 | Py_ssize_t new_size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 692 | assert(!PyUnicode_IS_COMPACT(unicode)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 693 | assert(Py_REFCNT(unicode) == 1); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 694 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 695 | if (PyUnicode_IS_READY(unicode)) { |
| 696 | Py_ssize_t char_size; |
Victor Stinner | 1c8d0c7 | 2011-10-03 12:11:00 +0200 | [diff] [blame] | 697 | int share_wstr, share_utf8; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 698 | void *data; |
| 699 | |
| 700 | data = _PyUnicode_DATA_ANY(unicode); |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 701 | char_size = PyUnicode_KIND(unicode); |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 702 | share_wstr = _PyUnicode_SHARE_WSTR(unicode); |
| 703 | share_utf8 = _PyUnicode_SHARE_UTF8(unicode); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 704 | |
| 705 | if (length > (PY_SSIZE_T_MAX / char_size - 1)) { |
| 706 | PyErr_NoMemory(); |
| 707 | return -1; |
| 708 | } |
| 709 | new_size = (length + 1) * char_size; |
| 710 | |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 711 | if (!share_utf8 && _PyUnicode_HAS_UTF8_MEMORY(unicode)) |
| 712 | { |
| 713 | PyObject_DEL(_PyUnicode_UTF8(unicode)); |
| 714 | _PyUnicode_UTF8(unicode) = NULL; |
| 715 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
| 716 | } |
| 717 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 718 | data = (PyObject *)PyObject_REALLOC(data, new_size); |
| 719 | if (data == NULL) { |
| 720 | PyErr_NoMemory(); |
| 721 | return -1; |
| 722 | } |
| 723 | _PyUnicode_DATA_ANY(unicode) = data; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 724 | if (share_wstr) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 725 | _PyUnicode_WSTR(unicode) = data; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 726 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 727 | } |
| 728 | if (share_utf8) { |
Victor Stinner | 1c8d0c7 | 2011-10-03 12:11:00 +0200 | [diff] [blame] | 729 | _PyUnicode_UTF8(unicode) = data; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 730 | _PyUnicode_UTF8_LENGTH(unicode) = length; |
| 731 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 732 | _PyUnicode_LENGTH(unicode) = length; |
| 733 | PyUnicode_WRITE(PyUnicode_KIND(unicode), data, length, 0); |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 734 | if (share_wstr || _PyUnicode_WSTR(unicode) == NULL) { |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 735 | assert(_PyUnicode_CheckConsistency(unicode, 0)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 736 | return 0; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 737 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 738 | } |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 739 | assert(_PyUnicode_WSTR(unicode) != NULL); |
| 740 | |
| 741 | /* check for integer overflow */ |
| 742 | if (length > PY_SSIZE_T_MAX / sizeof(wchar_t) - 1) { |
| 743 | PyErr_NoMemory(); |
| 744 | return -1; |
| 745 | } |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 746 | new_size = sizeof(wchar_t) * (length + 1); |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 747 | wstr = _PyUnicode_WSTR(unicode); |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 748 | wstr = PyObject_REALLOC(wstr, new_size); |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 749 | if (!wstr) { |
| 750 | PyErr_NoMemory(); |
| 751 | return -1; |
| 752 | } |
| 753 | _PyUnicode_WSTR(unicode) = wstr; |
| 754 | _PyUnicode_WSTR(unicode)[length] = 0; |
| 755 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 756 | assert(_PyUnicode_CheckConsistency(unicode, 0)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 757 | return 0; |
| 758 | } |
| 759 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 760 | static PyObject* |
| 761 | resize_copy(PyObject *unicode, Py_ssize_t length) |
| 762 | { |
| 763 | Py_ssize_t copy_length; |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 764 | if (_PyUnicode_KIND(unicode) != PyUnicode_WCHAR_KIND) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 765 | PyObject *copy; |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 766 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 767 | if (PyUnicode_READY(unicode) == -1) |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 768 | return NULL; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 769 | |
| 770 | copy = PyUnicode_New(length, PyUnicode_MAX_CHAR_VALUE(unicode)); |
| 771 | if (copy == NULL) |
| 772 | return NULL; |
| 773 | |
| 774 | copy_length = Py_MIN(length, PyUnicode_GET_LENGTH(unicode)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 775 | copy_characters(copy, 0, unicode, 0, copy_length); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 776 | return copy; |
Victor Stinner | 8cfcbed | 2011-10-03 23:19:21 +0200 | [diff] [blame] | 777 | } |
| 778 | else { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 779 | PyObject *w; |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 780 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 781 | w = (PyObject*)_PyUnicode_New(length); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 782 | if (w == NULL) |
| 783 | return NULL; |
| 784 | copy_length = _PyUnicode_WSTR_LENGTH(unicode); |
| 785 | copy_length = Py_MIN(copy_length, length); |
| 786 | Py_UNICODE_COPY(_PyUnicode_WSTR(w), _PyUnicode_WSTR(unicode), |
| 787 | copy_length); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 788 | return w; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 789 | } |
| 790 | } |
| 791 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 792 | /* 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] | 793 | Ux0000 terminated; some code (e.g. new_identifier) |
| 794 | relies on that. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 795 | |
| 796 | XXX This allocator could further be enhanced by assuring that the |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 797 | free list never reduces its size below 1. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 798 | |
| 799 | */ |
| 800 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 801 | #ifdef Py_DEBUG |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 802 | static int unicode_old_new_calls = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 803 | #endif |
| 804 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 805 | static PyUnicodeObject * |
| 806 | _PyUnicode_New(Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 807 | { |
| 808 | register PyUnicodeObject *unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 809 | size_t new_size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 810 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 811 | /* Optimization for empty strings */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 812 | if (length == 0 && unicode_empty != NULL) { |
| 813 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 814 | return (PyUnicodeObject*)unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 815 | } |
| 816 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 817 | /* Ensure we won't overflow the size. */ |
| 818 | if (length > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
| 819 | return (PyUnicodeObject *)PyErr_NoMemory(); |
| 820 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 821 | if (length < 0) { |
| 822 | PyErr_SetString(PyExc_SystemError, |
| 823 | "Negative size passed to _PyUnicode_New"); |
| 824 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 825 | } |
| 826 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 827 | #ifdef Py_DEBUG |
| 828 | ++unicode_old_new_calls; |
| 829 | #endif |
| 830 | |
| 831 | unicode = PyObject_New(PyUnicodeObject, &PyUnicode_Type); |
| 832 | if (unicode == NULL) |
| 833 | return NULL; |
| 834 | new_size = sizeof(Py_UNICODE) * ((size_t)length + 1); |
| 835 | _PyUnicode_WSTR(unicode) = (Py_UNICODE*) PyObject_MALLOC(new_size); |
| 836 | if (!_PyUnicode_WSTR(unicode)) { |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 837 | Py_DECREF(unicode); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 838 | PyErr_NoMemory(); |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 839 | return NULL; |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 840 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 841 | |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 842 | /* Initialize the first element to guard against cases where |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 843 | * the caller fails before initializing str -- unicode_resize() |
| 844 | * reads str[0], and the Keep-Alive optimization can keep memory |
| 845 | * allocated for str alive across a call to unicode_dealloc(unicode). |
| 846 | * We don't want unicode_resize to read uninitialized memory in |
| 847 | * that case. |
| 848 | */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 849 | _PyUnicode_WSTR(unicode)[0] = 0; |
| 850 | _PyUnicode_WSTR(unicode)[length] = 0; |
| 851 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 852 | _PyUnicode_HASH(unicode) = -1; |
| 853 | _PyUnicode_STATE(unicode).interned = 0; |
| 854 | _PyUnicode_STATE(unicode).kind = 0; |
| 855 | _PyUnicode_STATE(unicode).compact = 0; |
| 856 | _PyUnicode_STATE(unicode).ready = 0; |
| 857 | _PyUnicode_STATE(unicode).ascii = 0; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 858 | _PyUnicode_DATA_ANY(unicode) = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 859 | _PyUnicode_LENGTH(unicode) = 0; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 860 | _PyUnicode_UTF8(unicode) = NULL; |
| 861 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 862 | assert(_PyUnicode_CheckConsistency((PyObject *)unicode, 0)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 863 | return unicode; |
| 864 | } |
| 865 | |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 866 | static const char* |
| 867 | unicode_kind_name(PyObject *unicode) |
| 868 | { |
Victor Stinner | 42dfd71 | 2011-10-03 14:41:45 +0200 | [diff] [blame] | 869 | /* don't check consistency: unicode_kind_name() is called from |
| 870 | _PyUnicode_Dump() */ |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 871 | if (!PyUnicode_IS_COMPACT(unicode)) |
| 872 | { |
| 873 | if (!PyUnicode_IS_READY(unicode)) |
| 874 | return "wstr"; |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 875 | switch (PyUnicode_KIND(unicode)) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 876 | { |
| 877 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 878 | if (PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 879 | return "legacy ascii"; |
| 880 | else |
| 881 | return "legacy latin1"; |
| 882 | case PyUnicode_2BYTE_KIND: |
| 883 | return "legacy UCS2"; |
| 884 | case PyUnicode_4BYTE_KIND: |
| 885 | return "legacy UCS4"; |
| 886 | default: |
| 887 | return "<legacy invalid kind>"; |
| 888 | } |
| 889 | } |
| 890 | assert(PyUnicode_IS_READY(unicode)); |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 891 | switch (PyUnicode_KIND(unicode)) { |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 892 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 893 | if (PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 894 | return "ascii"; |
| 895 | else |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 896 | return "latin1"; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 897 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 898 | return "UCS2"; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 899 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 900 | return "UCS4"; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 901 | default: |
| 902 | return "<invalid compact kind>"; |
| 903 | } |
| 904 | } |
| 905 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 906 | #ifdef Py_DEBUG |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 907 | static int unicode_new_new_calls = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 908 | |
| 909 | /* Functions wrapping macros for use in debugger */ |
| 910 | char *_PyUnicode_utf8(void *unicode){ |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 911 | return PyUnicode_UTF8(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 912 | } |
| 913 | |
| 914 | void *_PyUnicode_compact_data(void *unicode) { |
| 915 | return _PyUnicode_COMPACT_DATA(unicode); |
| 916 | } |
| 917 | void *_PyUnicode_data(void *unicode){ |
| 918 | printf("obj %p\n", unicode); |
| 919 | printf("compact %d\n", PyUnicode_IS_COMPACT(unicode)); |
| 920 | printf("compact ascii %d\n", PyUnicode_IS_COMPACT_ASCII(unicode)); |
| 921 | printf("ascii op %p\n", ((void*)((PyASCIIObject*)(unicode) + 1))); |
| 922 | printf("compact op %p\n", ((void*)((PyCompactUnicodeObject*)(unicode) + 1))); |
| 923 | printf("compact data %p\n", _PyUnicode_COMPACT_DATA(unicode)); |
| 924 | return PyUnicode_DATA(unicode); |
| 925 | } |
Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 926 | |
| 927 | void |
| 928 | _PyUnicode_Dump(PyObject *op) |
| 929 | { |
| 930 | PyASCIIObject *ascii = (PyASCIIObject *)op; |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 931 | PyCompactUnicodeObject *compact = (PyCompactUnicodeObject *)op; |
| 932 | PyUnicodeObject *unicode = (PyUnicodeObject *)op; |
| 933 | void *data; |
Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 934 | |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 935 | if (ascii->state.compact) |
Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 936 | { |
| 937 | if (ascii->state.ascii) |
| 938 | data = (ascii + 1); |
| 939 | else |
| 940 | data = (compact + 1); |
| 941 | } |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 942 | else |
| 943 | data = unicode->data.any; |
Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 944 | printf("%s: len=%zu, ",unicode_kind_name(op), ascii->length); |
| 945 | |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 946 | if (ascii->wstr == data) |
| 947 | printf("shared "); |
| 948 | printf("wstr=%p", ascii->wstr); |
Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 949 | |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 950 | if (!(ascii->state.ascii == 1 && ascii->state.compact == 1)) { |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 951 | printf(" (%zu), ", compact->wstr_length); |
| 952 | if (!ascii->state.compact && compact->utf8 == unicode->data.any) |
| 953 | printf("shared "); |
| 954 | printf("utf8=%p (%zu)", compact->utf8, compact->utf8_length); |
Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 955 | } |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 956 | printf(", data=%p\n", data); |
Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 957 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 958 | #endif |
| 959 | |
| 960 | PyObject * |
| 961 | PyUnicode_New(Py_ssize_t size, Py_UCS4 maxchar) |
| 962 | { |
| 963 | PyObject *obj; |
| 964 | PyCompactUnicodeObject *unicode; |
| 965 | void *data; |
| 966 | int kind_state; |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 967 | int is_sharing, is_ascii; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 968 | Py_ssize_t char_size; |
| 969 | Py_ssize_t struct_size; |
| 970 | |
| 971 | /* Optimization for empty strings */ |
| 972 | if (size == 0 && unicode_empty != NULL) { |
| 973 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 974 | return unicode_empty; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 975 | } |
| 976 | |
| 977 | #ifdef Py_DEBUG |
| 978 | ++unicode_new_new_calls; |
| 979 | #endif |
| 980 | |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 981 | is_ascii = 0; |
| 982 | is_sharing = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 983 | struct_size = sizeof(PyCompactUnicodeObject); |
| 984 | if (maxchar < 128) { |
| 985 | kind_state = PyUnicode_1BYTE_KIND; |
| 986 | char_size = 1; |
| 987 | is_ascii = 1; |
| 988 | struct_size = sizeof(PyASCIIObject); |
| 989 | } |
| 990 | else if (maxchar < 256) { |
| 991 | kind_state = PyUnicode_1BYTE_KIND; |
| 992 | char_size = 1; |
| 993 | } |
| 994 | else if (maxchar < 65536) { |
| 995 | kind_state = PyUnicode_2BYTE_KIND; |
| 996 | char_size = 2; |
| 997 | if (sizeof(wchar_t) == 2) |
| 998 | is_sharing = 1; |
| 999 | } |
| 1000 | else { |
Victor Stinner | 15e9ed2 | 2012-02-22 13:36:20 +0100 | [diff] [blame] | 1001 | assert(maxchar <= MAX_UNICODE); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1002 | kind_state = PyUnicode_4BYTE_KIND; |
| 1003 | char_size = 4; |
| 1004 | if (sizeof(wchar_t) == 4) |
| 1005 | is_sharing = 1; |
| 1006 | } |
| 1007 | |
| 1008 | /* Ensure we won't overflow the size. */ |
| 1009 | if (size < 0) { |
| 1010 | PyErr_SetString(PyExc_SystemError, |
| 1011 | "Negative size passed to PyUnicode_New"); |
| 1012 | return NULL; |
| 1013 | } |
| 1014 | if (size > ((PY_SSIZE_T_MAX - struct_size) / char_size - 1)) |
| 1015 | return PyErr_NoMemory(); |
| 1016 | |
| 1017 | /* Duplicated allocation code from _PyObject_New() instead of a call to |
| 1018 | * PyObject_New() so we are able to allocate space for the object and |
| 1019 | * it's data buffer. |
| 1020 | */ |
| 1021 | obj = (PyObject *) PyObject_MALLOC(struct_size + (size + 1) * char_size); |
| 1022 | if (obj == NULL) |
| 1023 | return PyErr_NoMemory(); |
| 1024 | obj = PyObject_INIT(obj, &PyUnicode_Type); |
| 1025 | if (obj == NULL) |
| 1026 | return NULL; |
| 1027 | |
| 1028 | unicode = (PyCompactUnicodeObject *)obj; |
| 1029 | if (is_ascii) |
| 1030 | data = ((PyASCIIObject*)obj) + 1; |
| 1031 | else |
| 1032 | data = unicode + 1; |
| 1033 | _PyUnicode_LENGTH(unicode) = size; |
| 1034 | _PyUnicode_HASH(unicode) = -1; |
| 1035 | _PyUnicode_STATE(unicode).interned = 0; |
| 1036 | _PyUnicode_STATE(unicode).kind = kind_state; |
| 1037 | _PyUnicode_STATE(unicode).compact = 1; |
| 1038 | _PyUnicode_STATE(unicode).ready = 1; |
| 1039 | _PyUnicode_STATE(unicode).ascii = is_ascii; |
| 1040 | if (is_ascii) { |
| 1041 | ((char*)data)[size] = 0; |
| 1042 | _PyUnicode_WSTR(unicode) = NULL; |
| 1043 | } |
| 1044 | else if (kind_state == PyUnicode_1BYTE_KIND) { |
| 1045 | ((char*)data)[size] = 0; |
| 1046 | _PyUnicode_WSTR(unicode) = NULL; |
| 1047 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1048 | unicode->utf8 = NULL; |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 1049 | unicode->utf8_length = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1050 | } |
| 1051 | else { |
| 1052 | unicode->utf8 = NULL; |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 1053 | unicode->utf8_length = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1054 | if (kind_state == PyUnicode_2BYTE_KIND) |
| 1055 | ((Py_UCS2*)data)[size] = 0; |
| 1056 | else /* kind_state == PyUnicode_4BYTE_KIND */ |
| 1057 | ((Py_UCS4*)data)[size] = 0; |
| 1058 | if (is_sharing) { |
| 1059 | _PyUnicode_WSTR_LENGTH(unicode) = size; |
| 1060 | _PyUnicode_WSTR(unicode) = (wchar_t *)data; |
| 1061 | } |
| 1062 | else { |
| 1063 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1064 | _PyUnicode_WSTR(unicode) = NULL; |
| 1065 | } |
| 1066 | } |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 1067 | assert(_PyUnicode_CheckConsistency((PyObject*)unicode, 0)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1068 | return obj; |
| 1069 | } |
| 1070 | |
| 1071 | #if SIZEOF_WCHAR_T == 2 |
| 1072 | /* Helper function to convert a 16-bits wchar_t representation to UCS4, this |
| 1073 | will decode surrogate pairs, the other conversions are implemented as macros |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 1074 | for efficiency. |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1075 | |
| 1076 | This function assumes that unicode can hold one more code point than wstr |
| 1077 | characters for a terminating null character. */ |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1078 | static void |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1079 | 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] | 1080 | PyObject *unicode) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1081 | { |
| 1082 | const wchar_t *iter; |
| 1083 | Py_UCS4 *ucs4_out; |
| 1084 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1085 | assert(unicode != NULL); |
| 1086 | assert(_PyUnicode_CHECK(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1087 | assert(_PyUnicode_KIND(unicode) == PyUnicode_4BYTE_KIND); |
| 1088 | ucs4_out = PyUnicode_4BYTE_DATA(unicode); |
| 1089 | |
| 1090 | for (iter = begin; iter < end; ) { |
| 1091 | assert(ucs4_out < (PyUnicode_4BYTE_DATA(unicode) + |
| 1092 | _PyUnicode_GET_LENGTH(unicode))); |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 1093 | if (Py_UNICODE_IS_HIGH_SURROGATE(iter[0]) |
| 1094 | && (iter+1) < end |
| 1095 | && Py_UNICODE_IS_LOW_SURROGATE(iter[1])) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1096 | { |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 1097 | *ucs4_out++ = Py_UNICODE_JOIN_SURROGATES(iter[0], iter[1]); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1098 | iter += 2; |
| 1099 | } |
| 1100 | else { |
| 1101 | *ucs4_out++ = *iter; |
| 1102 | iter++; |
| 1103 | } |
| 1104 | } |
| 1105 | assert(ucs4_out == (PyUnicode_4BYTE_DATA(unicode) + |
| 1106 | _PyUnicode_GET_LENGTH(unicode))); |
| 1107 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1108 | } |
| 1109 | #endif |
| 1110 | |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1111 | static int |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1112 | unicode_check_modifiable(PyObject *unicode) |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1113 | { |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1114 | if (!unicode_modifiable(unicode)) { |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 1115 | PyErr_SetString(PyExc_SystemError, |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1116 | "Cannot modify a string currently used"); |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1117 | return -1; |
| 1118 | } |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1119 | return 0; |
| 1120 | } |
| 1121 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1122 | static int |
| 1123 | _copy_characters(PyObject *to, Py_ssize_t to_start, |
| 1124 | PyObject *from, Py_ssize_t from_start, |
| 1125 | Py_ssize_t how_many, int check_maxchar) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1126 | { |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1127 | unsigned int from_kind, to_kind; |
| 1128 | void *from_data, *to_data; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1129 | int fast; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1130 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1131 | assert(PyUnicode_Check(from)); |
| 1132 | assert(PyUnicode_Check(to)); |
| 1133 | assert(PyUnicode_IS_READY(from)); |
| 1134 | assert(PyUnicode_IS_READY(to)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1135 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1136 | assert(PyUnicode_GET_LENGTH(from) >= how_many); |
| 1137 | assert(to_start + how_many <= PyUnicode_GET_LENGTH(to)); |
| 1138 | assert(0 <= how_many); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1139 | |
Victor Stinner | f5ca1a2 | 2011-09-28 23:54:59 +0200 | [diff] [blame] | 1140 | if (how_many == 0) |
| 1141 | return 0; |
| 1142 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1143 | from_kind = PyUnicode_KIND(from); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1144 | from_data = PyUnicode_DATA(from); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1145 | to_kind = PyUnicode_KIND(to); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1146 | to_data = PyUnicode_DATA(to); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1147 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1148 | #ifdef Py_DEBUG |
| 1149 | if (!check_maxchar |
| 1150 | && (from_kind > to_kind |
| 1151 | || (!PyUnicode_IS_ASCII(from) && PyUnicode_IS_ASCII(to)))) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 1152 | { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1153 | const Py_UCS4 to_maxchar = PyUnicode_MAX_CHAR_VALUE(to); |
| 1154 | Py_UCS4 ch; |
| 1155 | Py_ssize_t i; |
| 1156 | for (i=0; i < how_many; i++) { |
| 1157 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
| 1158 | assert(ch <= to_maxchar); |
| 1159 | } |
| 1160 | } |
| 1161 | #endif |
| 1162 | fast = (from_kind == to_kind); |
| 1163 | if (check_maxchar |
| 1164 | && (!PyUnicode_IS_ASCII(from) && PyUnicode_IS_ASCII(to))) |
| 1165 | { |
| 1166 | /* deny latin1 => ascii */ |
| 1167 | fast = 0; |
| 1168 | } |
| 1169 | |
| 1170 | if (fast) { |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 1171 | Py_MEMCPY((char*)to_data + to_kind * to_start, |
| 1172 | (char*)from_data + from_kind * from_start, |
| 1173 | to_kind * how_many); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1174 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1175 | else if (from_kind == PyUnicode_1BYTE_KIND |
| 1176 | && to_kind == PyUnicode_2BYTE_KIND) |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1177 | { |
| 1178 | _PyUnicode_CONVERT_BYTES( |
| 1179 | Py_UCS1, Py_UCS2, |
| 1180 | PyUnicode_1BYTE_DATA(from) + from_start, |
| 1181 | PyUnicode_1BYTE_DATA(from) + from_start + how_many, |
| 1182 | PyUnicode_2BYTE_DATA(to) + to_start |
| 1183 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1184 | } |
Victor Stinner | 157f83f | 2011-09-28 21:41:31 +0200 | [diff] [blame] | 1185 | else if (from_kind == PyUnicode_1BYTE_KIND |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1186 | && to_kind == PyUnicode_4BYTE_KIND) |
| 1187 | { |
| 1188 | _PyUnicode_CONVERT_BYTES( |
| 1189 | Py_UCS1, Py_UCS4, |
| 1190 | PyUnicode_1BYTE_DATA(from) + from_start, |
| 1191 | PyUnicode_1BYTE_DATA(from) + from_start + how_many, |
| 1192 | PyUnicode_4BYTE_DATA(to) + to_start |
| 1193 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1194 | } |
| 1195 | else if (from_kind == PyUnicode_2BYTE_KIND |
| 1196 | && to_kind == PyUnicode_4BYTE_KIND) |
| 1197 | { |
| 1198 | _PyUnicode_CONVERT_BYTES( |
| 1199 | Py_UCS2, Py_UCS4, |
| 1200 | PyUnicode_2BYTE_DATA(from) + from_start, |
| 1201 | PyUnicode_2BYTE_DATA(from) + from_start + how_many, |
| 1202 | PyUnicode_4BYTE_DATA(to) + to_start |
| 1203 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1204 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1205 | else { |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 1206 | /* check if max_char(from substring) <= max_char(to) */ |
| 1207 | if (from_kind > to_kind |
| 1208 | /* latin1 => ascii */ |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1209 | || (!PyUnicode_IS_ASCII(from) && PyUnicode_IS_ASCII(to))) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 1210 | { |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1211 | /* slow path to check for character overflow */ |
| 1212 | const Py_UCS4 to_maxchar = PyUnicode_MAX_CHAR_VALUE(to); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1213 | Py_UCS4 ch; |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1214 | Py_ssize_t i; |
| 1215 | |
Victor Stinner | 56c161a | 2011-10-06 02:47:11 +0200 | [diff] [blame] | 1216 | #ifdef Py_DEBUG |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1217 | for (i=0; i < how_many; i++) { |
| 1218 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
Victor Stinner | 56c161a | 2011-10-06 02:47:11 +0200 | [diff] [blame] | 1219 | assert(ch <= to_maxchar); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1220 | PyUnicode_WRITE(to_kind, to_data, to_start + i, ch); |
| 1221 | } |
Victor Stinner | 56c161a | 2011-10-06 02:47:11 +0200 | [diff] [blame] | 1222 | #else |
| 1223 | if (!check_maxchar) { |
| 1224 | for (i=0; i < how_many; i++) { |
| 1225 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
| 1226 | PyUnicode_WRITE(to_kind, to_data, to_start + i, ch); |
| 1227 | } |
| 1228 | } |
| 1229 | else { |
| 1230 | for (i=0; i < how_many; i++) { |
| 1231 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
| 1232 | if (ch > to_maxchar) |
| 1233 | return 1; |
| 1234 | PyUnicode_WRITE(to_kind, to_data, to_start + i, ch); |
| 1235 | } |
| 1236 | } |
| 1237 | #endif |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1238 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1239 | else { |
Victor Stinner | 56c161a | 2011-10-06 02:47:11 +0200 | [diff] [blame] | 1240 | assert(0 && "inconsistent state"); |
| 1241 | return 1; |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1242 | } |
| 1243 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1244 | return 0; |
| 1245 | } |
| 1246 | |
| 1247 | static void |
| 1248 | copy_characters(PyObject *to, Py_ssize_t to_start, |
| 1249 | PyObject *from, Py_ssize_t from_start, |
| 1250 | Py_ssize_t how_many) |
| 1251 | { |
| 1252 | (void)_copy_characters(to, to_start, from, from_start, how_many, 0); |
| 1253 | } |
| 1254 | |
| 1255 | Py_ssize_t |
| 1256 | PyUnicode_CopyCharacters(PyObject *to, Py_ssize_t to_start, |
| 1257 | PyObject *from, Py_ssize_t from_start, |
| 1258 | Py_ssize_t how_many) |
| 1259 | { |
| 1260 | int err; |
| 1261 | |
| 1262 | if (!PyUnicode_Check(from) || !PyUnicode_Check(to)) { |
| 1263 | PyErr_BadInternalCall(); |
| 1264 | return -1; |
| 1265 | } |
| 1266 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 1267 | if (PyUnicode_READY(from) == -1) |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1268 | return -1; |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 1269 | if (PyUnicode_READY(to) == -1) |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1270 | return -1; |
| 1271 | |
| 1272 | how_many = Py_MIN(PyUnicode_GET_LENGTH(from), how_many); |
| 1273 | if (to_start + how_many > PyUnicode_GET_LENGTH(to)) { |
| 1274 | PyErr_Format(PyExc_SystemError, |
| 1275 | "Cannot write %zi characters at %zi " |
| 1276 | "in a string of %zi characters", |
| 1277 | how_many, to_start, PyUnicode_GET_LENGTH(to)); |
| 1278 | return -1; |
| 1279 | } |
| 1280 | |
| 1281 | if (how_many == 0) |
| 1282 | return 0; |
| 1283 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1284 | if (unicode_check_modifiable(to)) |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1285 | return -1; |
| 1286 | |
| 1287 | err = _copy_characters(to, to_start, from, from_start, how_many, 1); |
| 1288 | if (err) { |
| 1289 | PyErr_Format(PyExc_SystemError, |
| 1290 | "Cannot copy %s characters " |
| 1291 | "into a string of %s characters", |
| 1292 | unicode_kind_name(from), |
| 1293 | unicode_kind_name(to)); |
| 1294 | return -1; |
| 1295 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1296 | return how_many; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1297 | } |
| 1298 | |
Victor Stinner | 1722216 | 2011-09-28 22:15:37 +0200 | [diff] [blame] | 1299 | /* Find the maximum code point and count the number of surrogate pairs so a |
| 1300 | correct string length can be computed before converting a string to UCS4. |
| 1301 | This function counts single surrogates as a character and not as a pair. |
| 1302 | |
| 1303 | Return 0 on success, or -1 on error. */ |
| 1304 | static int |
| 1305 | find_maxchar_surrogates(const wchar_t *begin, const wchar_t *end, |
| 1306 | Py_UCS4 *maxchar, Py_ssize_t *num_surrogates) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1307 | { |
| 1308 | const wchar_t *iter; |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1309 | Py_UCS4 ch; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1310 | |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1311 | assert(num_surrogates != NULL && maxchar != NULL); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1312 | *num_surrogates = 0; |
| 1313 | *maxchar = 0; |
| 1314 | |
| 1315 | for (iter = begin; iter < end; ) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1316 | #if SIZEOF_WCHAR_T == 2 |
Victor Stinner | ca4f207 | 2011-11-22 03:38:40 +0100 | [diff] [blame] | 1317 | if (Py_UNICODE_IS_HIGH_SURROGATE(iter[0]) |
| 1318 | && (iter+1) < end |
| 1319 | && Py_UNICODE_IS_LOW_SURROGATE(iter[1])) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1320 | { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1321 | ch = Py_UNICODE_JOIN_SURROGATES(iter[0], iter[1]); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1322 | ++(*num_surrogates); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1323 | iter += 2; |
| 1324 | } |
| 1325 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1326 | #endif |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1327 | { |
| 1328 | ch = *iter; |
| 1329 | iter++; |
| 1330 | } |
| 1331 | if (ch > *maxchar) { |
| 1332 | *maxchar = ch; |
| 1333 | if (*maxchar > MAX_UNICODE) { |
| 1334 | PyErr_Format(PyExc_ValueError, |
| 1335 | "character U+%x is not in range [U+0000; U+10ffff]", |
| 1336 | ch); |
| 1337 | return -1; |
| 1338 | } |
| 1339 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1340 | } |
| 1341 | return 0; |
| 1342 | } |
| 1343 | |
| 1344 | #ifdef Py_DEBUG |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 1345 | static int unicode_ready_calls = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1346 | #endif |
| 1347 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1348 | int |
| 1349 | _PyUnicode_Ready(PyObject *unicode) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1350 | { |
| 1351 | wchar_t *end; |
| 1352 | Py_UCS4 maxchar = 0; |
| 1353 | Py_ssize_t num_surrogates; |
| 1354 | #if SIZEOF_WCHAR_T == 2 |
| 1355 | Py_ssize_t length_wo_surrogates; |
| 1356 | #endif |
| 1357 | |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 1358 | /* _PyUnicode_Ready() is only intended for old-style API usage where |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1359 | strings were created using _PyObject_New() and where no canonical |
| 1360 | representation (the str field) has been set yet aka strings |
| 1361 | which are not yet ready. */ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1362 | assert(_PyUnicode_CHECK(unicode)); |
| 1363 | assert(_PyUnicode_KIND(unicode) == PyUnicode_WCHAR_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1364 | assert(_PyUnicode_WSTR(unicode) != NULL); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1365 | assert(_PyUnicode_DATA_ANY(unicode) == NULL); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1366 | assert(_PyUnicode_UTF8(unicode) == NULL); |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1367 | /* Actually, it should neither be interned nor be anything else: */ |
| 1368 | assert(_PyUnicode_STATE(unicode).interned == SSTATE_NOT_INTERNED); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1369 | |
| 1370 | #ifdef Py_DEBUG |
| 1371 | ++unicode_ready_calls; |
| 1372 | #endif |
| 1373 | |
| 1374 | end = _PyUnicode_WSTR(unicode) + _PyUnicode_WSTR_LENGTH(unicode); |
Victor Stinner | 1722216 | 2011-09-28 22:15:37 +0200 | [diff] [blame] | 1375 | if (find_maxchar_surrogates(_PyUnicode_WSTR(unicode), end, |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1376 | &maxchar, &num_surrogates) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1377 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1378 | |
| 1379 | if (maxchar < 256) { |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1380 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC(_PyUnicode_WSTR_LENGTH(unicode) + 1); |
| 1381 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1382 | PyErr_NoMemory(); |
| 1383 | return -1; |
| 1384 | } |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1385 | _PyUnicode_CONVERT_BYTES(wchar_t, unsigned char, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1386 | _PyUnicode_WSTR(unicode), end, |
| 1387 | PyUnicode_1BYTE_DATA(unicode)); |
| 1388 | PyUnicode_1BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1389 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1390 | _PyUnicode_STATE(unicode).kind = PyUnicode_1BYTE_KIND; |
| 1391 | if (maxchar < 128) { |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1392 | _PyUnicode_STATE(unicode).ascii = 1; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1393 | _PyUnicode_UTF8(unicode) = _PyUnicode_DATA_ANY(unicode); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1394 | _PyUnicode_UTF8_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1395 | } |
| 1396 | else { |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1397 | _PyUnicode_STATE(unicode).ascii = 0; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1398 | _PyUnicode_UTF8(unicode) = NULL; |
| 1399 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1400 | } |
| 1401 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1402 | _PyUnicode_WSTR(unicode) = NULL; |
| 1403 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1404 | } |
| 1405 | /* In this case we might have to convert down from 4-byte native |
| 1406 | wchar_t to 2-byte unicode. */ |
| 1407 | else if (maxchar < 65536) { |
| 1408 | assert(num_surrogates == 0 && |
| 1409 | "FindMaxCharAndNumSurrogatePairs() messed up"); |
| 1410 | |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1411 | #if SIZEOF_WCHAR_T == 2 |
| 1412 | /* We can share representations and are done. */ |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1413 | _PyUnicode_DATA_ANY(unicode) = _PyUnicode_WSTR(unicode); |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1414 | PyUnicode_2BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1415 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1416 | _PyUnicode_STATE(unicode).kind = PyUnicode_2BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1417 | _PyUnicode_UTF8(unicode) = NULL; |
| 1418 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1419 | #else |
| 1420 | /* sizeof(wchar_t) == 4 */ |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1421 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC( |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1422 | 2 * (_PyUnicode_WSTR_LENGTH(unicode) + 1)); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1423 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1424 | PyErr_NoMemory(); |
| 1425 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1426 | } |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1427 | _PyUnicode_CONVERT_BYTES(wchar_t, Py_UCS2, |
| 1428 | _PyUnicode_WSTR(unicode), end, |
| 1429 | PyUnicode_2BYTE_DATA(unicode)); |
| 1430 | PyUnicode_2BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1431 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1432 | _PyUnicode_STATE(unicode).kind = PyUnicode_2BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1433 | _PyUnicode_UTF8(unicode) = NULL; |
| 1434 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1435 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1436 | _PyUnicode_WSTR(unicode) = NULL; |
| 1437 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1438 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1439 | } |
| 1440 | /* maxchar exeeds 16 bit, wee need 4 bytes for unicode characters */ |
| 1441 | else { |
| 1442 | #if SIZEOF_WCHAR_T == 2 |
| 1443 | /* in case the native representation is 2-bytes, we need to allocate a |
| 1444 | new normalized 4-byte version. */ |
| 1445 | length_wo_surrogates = _PyUnicode_WSTR_LENGTH(unicode) - num_surrogates; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1446 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC(4 * (length_wo_surrogates + 1)); |
| 1447 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1448 | PyErr_NoMemory(); |
| 1449 | return -1; |
| 1450 | } |
| 1451 | _PyUnicode_LENGTH(unicode) = length_wo_surrogates; |
| 1452 | _PyUnicode_STATE(unicode).kind = PyUnicode_4BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1453 | _PyUnicode_UTF8(unicode) = NULL; |
| 1454 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 126c559 | 2011-10-03 04:17:10 +0200 | [diff] [blame] | 1455 | /* unicode_convert_wchar_to_ucs4() requires a ready string */ |
| 1456 | _PyUnicode_STATE(unicode).ready = 1; |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1457 | unicode_convert_wchar_to_ucs4(_PyUnicode_WSTR(unicode), end, unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1458 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1459 | _PyUnicode_WSTR(unicode) = NULL; |
| 1460 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1461 | #else |
| 1462 | assert(num_surrogates == 0); |
| 1463 | |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1464 | _PyUnicode_DATA_ANY(unicode) = _PyUnicode_WSTR(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1465 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1466 | _PyUnicode_UTF8(unicode) = NULL; |
| 1467 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1468 | _PyUnicode_STATE(unicode).kind = PyUnicode_4BYTE_KIND; |
| 1469 | #endif |
| 1470 | PyUnicode_4BYTE_DATA(unicode)[_PyUnicode_LENGTH(unicode)] = '\0'; |
| 1471 | } |
| 1472 | _PyUnicode_STATE(unicode).ready = 1; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1473 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1474 | return 0; |
| 1475 | } |
| 1476 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1477 | static void |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1478 | unicode_dealloc(register PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1479 | { |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1480 | switch (PyUnicode_CHECK_INTERNED(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1481 | case SSTATE_NOT_INTERNED: |
| 1482 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1483 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1484 | case SSTATE_INTERNED_MORTAL: |
| 1485 | /* revive dead object temporarily for DelItem */ |
| 1486 | Py_REFCNT(unicode) = 3; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 1487 | if (PyDict_DelItem(interned, unicode) != 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1488 | Py_FatalError( |
| 1489 | "deletion of interned string failed"); |
| 1490 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1491 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1492 | case SSTATE_INTERNED_IMMORTAL: |
| 1493 | Py_FatalError("Immortal interned string died."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1494 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1495 | default: |
| 1496 | Py_FatalError("Inconsistent interned string state."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1497 | } |
| 1498 | |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 1499 | if (_PyUnicode_HAS_WSTR_MEMORY(unicode)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1500 | PyObject_DEL(_PyUnicode_WSTR(unicode)); |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 1501 | if (_PyUnicode_HAS_UTF8_MEMORY(unicode)) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1502 | PyObject_DEL(_PyUnicode_UTF8(unicode)); |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1503 | if (!PyUnicode_IS_COMPACT(unicode) && _PyUnicode_DATA_ANY(unicode)) |
| 1504 | PyObject_DEL(_PyUnicode_DATA_ANY(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1505 | |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1506 | Py_TYPE(unicode)->tp_free(unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1507 | } |
| 1508 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1509 | #ifdef Py_DEBUG |
| 1510 | static int |
| 1511 | unicode_is_singleton(PyObject *unicode) |
| 1512 | { |
| 1513 | PyASCIIObject *ascii = (PyASCIIObject *)unicode; |
| 1514 | if (unicode == unicode_empty) |
| 1515 | return 1; |
| 1516 | if (ascii->state.kind != PyUnicode_WCHAR_KIND && ascii->length == 1) |
| 1517 | { |
| 1518 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, 0); |
| 1519 | if (ch < 256 && unicode_latin1[ch] == unicode) |
| 1520 | return 1; |
| 1521 | } |
| 1522 | return 0; |
| 1523 | } |
| 1524 | #endif |
| 1525 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1526 | static int |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1527 | unicode_modifiable(PyObject *unicode) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1528 | { |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1529 | assert(_PyUnicode_CHECK(unicode)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1530 | if (Py_REFCNT(unicode) != 1) |
| 1531 | return 0; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1532 | if (_PyUnicode_HASH(unicode) != -1) |
| 1533 | return 0; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1534 | if (PyUnicode_CHECK_INTERNED(unicode)) |
| 1535 | return 0; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1536 | if (!PyUnicode_CheckExact(unicode)) |
| 1537 | return 0; |
Victor Stinner | 77bb47b | 2011-10-03 20:06:05 +0200 | [diff] [blame] | 1538 | #ifdef Py_DEBUG |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1539 | /* singleton refcount is greater than 1 */ |
| 1540 | assert(!unicode_is_singleton(unicode)); |
Victor Stinner | 77bb47b | 2011-10-03 20:06:05 +0200 | [diff] [blame] | 1541 | #endif |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1542 | return 1; |
| 1543 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1544 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1545 | static int |
| 1546 | unicode_resize(PyObject **p_unicode, Py_ssize_t length) |
| 1547 | { |
| 1548 | PyObject *unicode; |
| 1549 | Py_ssize_t old_length; |
| 1550 | |
| 1551 | assert(p_unicode != NULL); |
| 1552 | unicode = *p_unicode; |
| 1553 | |
| 1554 | assert(unicode != NULL); |
| 1555 | assert(PyUnicode_Check(unicode)); |
| 1556 | assert(0 <= length); |
| 1557 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1558 | if (_PyUnicode_KIND(unicode) == PyUnicode_WCHAR_KIND) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1559 | old_length = PyUnicode_WSTR_LENGTH(unicode); |
| 1560 | else |
| 1561 | old_length = PyUnicode_GET_LENGTH(unicode); |
| 1562 | if (old_length == length) |
| 1563 | return 0; |
| 1564 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1565 | if (length == 0) { |
| 1566 | Py_DECREF(*p_unicode); |
| 1567 | *p_unicode = unicode_empty; |
| 1568 | Py_INCREF(*p_unicode); |
| 1569 | return 0; |
| 1570 | } |
| 1571 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1572 | if (!unicode_modifiable(unicode)) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1573 | PyObject *copy = resize_copy(unicode, length); |
| 1574 | if (copy == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1575 | return -1; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1576 | Py_DECREF(*p_unicode); |
| 1577 | *p_unicode = copy; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1578 | return 0; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1579 | } |
| 1580 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1581 | if (PyUnicode_IS_COMPACT(unicode)) { |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1582 | PyObject *new_unicode = resize_compact(unicode, length); |
| 1583 | if (new_unicode == NULL) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1584 | return -1; |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1585 | *p_unicode = new_unicode; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1586 | assert(_PyUnicode_CheckConsistency(*p_unicode, 0)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1587 | return 0; |
Benjamin Peterson | 4bfce8f | 2011-10-03 19:35:07 -0400 | [diff] [blame] | 1588 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1589 | return resize_inplace(unicode, length); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1590 | } |
| 1591 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1592 | int |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1593 | PyUnicode_Resize(PyObject **p_unicode, Py_ssize_t length) |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 1594 | { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1595 | PyObject *unicode; |
| 1596 | if (p_unicode == NULL) { |
| 1597 | PyErr_BadInternalCall(); |
| 1598 | return -1; |
| 1599 | } |
| 1600 | unicode = *p_unicode; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1601 | if (unicode == NULL || !PyUnicode_Check(unicode) || length < 0) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1602 | { |
| 1603 | PyErr_BadInternalCall(); |
| 1604 | return -1; |
| 1605 | } |
| 1606 | return unicode_resize(p_unicode, length); |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 1607 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1608 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1609 | static int |
Victor Stinner | 0a045ef | 2011-11-09 00:02:42 +0100 | [diff] [blame] | 1610 | unicode_widen(PyObject **p_unicode, unsigned int maxchar) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1611 | { |
| 1612 | PyObject *result; |
| 1613 | assert(PyUnicode_IS_READY(*p_unicode)); |
| 1614 | if (maxchar <= PyUnicode_MAX_CHAR_VALUE(*p_unicode)) |
| 1615 | return 0; |
| 1616 | result = PyUnicode_New(PyUnicode_GET_LENGTH(*p_unicode), |
| 1617 | maxchar); |
| 1618 | if (result == NULL) |
| 1619 | return -1; |
| 1620 | PyUnicode_CopyCharacters(result, 0, *p_unicode, 0, |
| 1621 | PyUnicode_GET_LENGTH(*p_unicode)); |
| 1622 | Py_DECREF(*p_unicode); |
| 1623 | *p_unicode = result; |
| 1624 | return 0; |
| 1625 | } |
| 1626 | |
| 1627 | static int |
| 1628 | unicode_putchar(PyObject **p_unicode, Py_ssize_t *pos, |
| 1629 | Py_UCS4 ch) |
| 1630 | { |
Victor Stinner | 15e9ed2 | 2012-02-22 13:36:20 +0100 | [diff] [blame] | 1631 | assert(ch <= MAX_UNICODE); |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1632 | if (unicode_widen(p_unicode, ch) < 0) |
| 1633 | return -1; |
| 1634 | PyUnicode_WRITE(PyUnicode_KIND(*p_unicode), |
| 1635 | PyUnicode_DATA(*p_unicode), |
| 1636 | (*pos)++, ch); |
| 1637 | return 0; |
| 1638 | } |
| 1639 | |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1640 | /* Copy a ASCII or latin1 char* string into a Python Unicode string. |
| 1641 | Return the length of the input string. |
| 1642 | |
Victor Stinner | b429d3b | 2012-02-22 21:22:20 +0100 | [diff] [blame] | 1643 | WARNING: The function doesn't copy the terminating null character and |
| 1644 | doesn't check the maximum character (may write a latin1 character in an |
| 1645 | ASCII string). */ |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1646 | static Py_ssize_t |
| 1647 | unicode_write_cstr(PyObject *unicode, Py_ssize_t index, const char *str) |
| 1648 | { |
| 1649 | enum PyUnicode_Kind kind = PyUnicode_KIND(unicode); |
| 1650 | void *data = PyUnicode_DATA(unicode); |
| 1651 | |
| 1652 | switch (kind) { |
| 1653 | case PyUnicode_1BYTE_KIND: { |
| 1654 | Py_ssize_t len = strlen(str); |
| 1655 | assert(index + len <= PyUnicode_GET_LENGTH(unicode)); |
Antoine Pitrou | ba6bafc | 2012-02-22 16:41:50 +0100 | [diff] [blame] | 1656 | memcpy((char *) data + index, str, len); |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1657 | return len; |
| 1658 | } |
| 1659 | case PyUnicode_2BYTE_KIND: { |
| 1660 | Py_UCS2 *start = (Py_UCS2 *)data + index; |
| 1661 | Py_UCS2 *ucs2 = start; |
| 1662 | assert(index <= PyUnicode_GET_LENGTH(unicode)); |
| 1663 | |
| 1664 | for (; *str; ++ucs2, ++str) |
| 1665 | *ucs2 = (Py_UCS2)*str; |
| 1666 | |
| 1667 | assert((ucs2 - start) <= PyUnicode_GET_LENGTH(unicode)); |
| 1668 | return ucs2 - start; |
| 1669 | } |
| 1670 | default: { |
| 1671 | Py_UCS4 *start = (Py_UCS4 *)data + index; |
| 1672 | Py_UCS4 *ucs4 = start; |
| 1673 | assert(kind == PyUnicode_4BYTE_KIND); |
| 1674 | assert(index <= PyUnicode_GET_LENGTH(unicode)); |
| 1675 | |
| 1676 | for (; *str; ++ucs4, ++str) |
| 1677 | *ucs4 = (Py_UCS4)*str; |
| 1678 | |
| 1679 | assert((ucs4 - start) <= PyUnicode_GET_LENGTH(unicode)); |
| 1680 | return ucs4 - start; |
| 1681 | } |
| 1682 | } |
| 1683 | } |
| 1684 | |
| 1685 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1686 | static PyObject* |
| 1687 | get_latin1_char(unsigned char ch) |
| 1688 | { |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1689 | PyObject *unicode = unicode_latin1[ch]; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1690 | if (!unicode) { |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1691 | unicode = PyUnicode_New(1, ch); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1692 | if (!unicode) |
| 1693 | return NULL; |
| 1694 | PyUnicode_1BYTE_DATA(unicode)[0] = ch; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1695 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1696 | unicode_latin1[ch] = unicode; |
| 1697 | } |
| 1698 | Py_INCREF(unicode); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1699 | return unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1700 | } |
| 1701 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1702 | PyObject * |
| 1703 | PyUnicode_FromUnicode(const Py_UNICODE *u, Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1704 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1705 | PyObject *unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1706 | Py_UCS4 maxchar = 0; |
| 1707 | Py_ssize_t num_surrogates; |
| 1708 | |
| 1709 | if (u == NULL) |
| 1710 | return (PyObject*)_PyUnicode_New(size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1711 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1712 | /* If the Unicode data is known at construction time, we can apply |
| 1713 | some optimizations which share commonly used objects. */ |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1714 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1715 | /* Optimization for empty strings */ |
| 1716 | if (size == 0 && unicode_empty != NULL) { |
| 1717 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1718 | return unicode_empty; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1719 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1720 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1721 | /* Single character Unicode objects in the Latin-1 range are |
| 1722 | shared when using this constructor */ |
| 1723 | if (size == 1 && *u < 256) |
| 1724 | return get_latin1_char((unsigned char)*u); |
| 1725 | |
| 1726 | /* If not empty and not single character, copy the Unicode data |
| 1727 | into the new object */ |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1728 | if (find_maxchar_surrogates(u, u + size, |
| 1729 | &maxchar, &num_surrogates) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1730 | return NULL; |
| 1731 | |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1732 | unicode = PyUnicode_New(size - num_surrogates, maxchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1733 | if (!unicode) |
| 1734 | return NULL; |
| 1735 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1736 | switch (PyUnicode_KIND(unicode)) { |
| 1737 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1738 | _PyUnicode_CONVERT_BYTES(Py_UNICODE, unsigned char, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1739 | u, u + size, PyUnicode_1BYTE_DATA(unicode)); |
| 1740 | break; |
| 1741 | case PyUnicode_2BYTE_KIND: |
| 1742 | #if Py_UNICODE_SIZE == 2 |
| 1743 | Py_MEMCPY(PyUnicode_2BYTE_DATA(unicode), u, size * 2); |
| 1744 | #else |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1745 | _PyUnicode_CONVERT_BYTES(Py_UNICODE, Py_UCS2, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1746 | u, u + size, PyUnicode_2BYTE_DATA(unicode)); |
| 1747 | #endif |
| 1748 | break; |
| 1749 | case PyUnicode_4BYTE_KIND: |
| 1750 | #if SIZEOF_WCHAR_T == 2 |
| 1751 | /* This is the only case which has to process surrogates, thus |
| 1752 | a simple copy loop is not enough and we need a function. */ |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1753 | unicode_convert_wchar_to_ucs4(u, u + size, unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1754 | #else |
| 1755 | assert(num_surrogates == 0); |
| 1756 | Py_MEMCPY(PyUnicode_4BYTE_DATA(unicode), u, size * 4); |
| 1757 | #endif |
| 1758 | break; |
| 1759 | default: |
| 1760 | assert(0 && "Impossible state"); |
| 1761 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1762 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1763 | return unicode_result(unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1764 | } |
| 1765 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1766 | PyObject * |
| 1767 | PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size) |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1768 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1769 | if (size < 0) { |
| 1770 | PyErr_SetString(PyExc_SystemError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1771 | "Negative size passed to PyUnicode_FromStringAndSize"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1772 | return NULL; |
| 1773 | } |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 1774 | if (u != NULL) |
| 1775 | return PyUnicode_DecodeUTF8Stateful(u, size, NULL, NULL); |
| 1776 | else |
| 1777 | return (PyObject *)_PyUnicode_New(size); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1778 | } |
| 1779 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1780 | PyObject * |
| 1781 | PyUnicode_FromString(const char *u) |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1782 | { |
| 1783 | size_t size = strlen(u); |
| 1784 | if (size > PY_SSIZE_T_MAX) { |
| 1785 | PyErr_SetString(PyExc_OverflowError, "input too long"); |
| 1786 | return NULL; |
| 1787 | } |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 1788 | return PyUnicode_DecodeUTF8Stateful(u, (Py_ssize_t)size, NULL, NULL); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1789 | } |
| 1790 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1791 | PyObject * |
| 1792 | _PyUnicode_FromId(_Py_Identifier *id) |
| 1793 | { |
| 1794 | if (!id->object) { |
Victor Stinner | d1cd99b | 2012-02-07 23:05:55 +0100 | [diff] [blame] | 1795 | id->object = PyUnicode_DecodeUTF8Stateful(id->string, |
| 1796 | strlen(id->string), |
| 1797 | NULL, NULL); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1798 | if (!id->object) |
| 1799 | return NULL; |
| 1800 | PyUnicode_InternInPlace(&id->object); |
| 1801 | assert(!id->next); |
| 1802 | id->next = static_strings; |
| 1803 | static_strings = id; |
| 1804 | } |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1805 | return id->object; |
| 1806 | } |
| 1807 | |
| 1808 | void |
| 1809 | _PyUnicode_ClearStaticStrings() |
| 1810 | { |
| 1811 | _Py_Identifier *i; |
| 1812 | for (i = static_strings; i; i = i->next) { |
| 1813 | Py_DECREF(i->object); |
| 1814 | i->object = NULL; |
| 1815 | i->next = NULL; |
| 1816 | } |
| 1817 | } |
| 1818 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1819 | /* Internal function, don't check maximum character */ |
| 1820 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1821 | static PyObject* |
Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 1822 | unicode_fromascii(const unsigned char* s, Py_ssize_t size) |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1823 | { |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 1824 | PyObject *unicode; |
Victor Stinner | e6b2d44 | 2011-12-11 21:54:30 +0100 | [diff] [blame] | 1825 | if (size == 1) { |
Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 1826 | #ifdef Py_DEBUG |
Victor Stinner | e6b2d44 | 2011-12-11 21:54:30 +0100 | [diff] [blame] | 1827 | assert(s[0] < 128); |
Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 1828 | #endif |
Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 1829 | return get_latin1_char(s[0]); |
Victor Stinner | e6b2d44 | 2011-12-11 21:54:30 +0100 | [diff] [blame] | 1830 | } |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 1831 | unicode = PyUnicode_New(size, 127); |
| 1832 | if (!unicode) |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1833 | return NULL; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 1834 | memcpy(PyUnicode_1BYTE_DATA(unicode), s, size); |
| 1835 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
| 1836 | return unicode; |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1837 | } |
| 1838 | |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 1839 | static Py_UCS4 |
| 1840 | kind_maxchar_limit(unsigned int kind) |
| 1841 | { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 1842 | switch (kind) { |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 1843 | case PyUnicode_1BYTE_KIND: |
| 1844 | return 0x80; |
| 1845 | case PyUnicode_2BYTE_KIND: |
| 1846 | return 0x100; |
| 1847 | case PyUnicode_4BYTE_KIND: |
| 1848 | return 0x10000; |
| 1849 | default: |
| 1850 | assert(0 && "invalid kind"); |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1851 | return MAX_UNICODE; |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 1852 | } |
| 1853 | } |
| 1854 | |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1855 | static PyObject* |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1856 | _PyUnicode_FromUCS1(const unsigned char* u, Py_ssize_t size) |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1857 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1858 | PyObject *res; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1859 | unsigned char max_char; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1860 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1861 | if (size == 0) { |
| 1862 | Py_INCREF(unicode_empty); |
| 1863 | return unicode_empty; |
| 1864 | } |
| 1865 | assert(size > 0); |
Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 1866 | if (size == 1) |
| 1867 | return get_latin1_char(u[0]); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1868 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1869 | max_char = ucs1lib_find_max_char(u, u + size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1870 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1871 | if (!res) |
| 1872 | return NULL; |
| 1873 | memcpy(PyUnicode_1BYTE_DATA(res), u, size); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1874 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1875 | return res; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1876 | } |
| 1877 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1878 | static PyObject* |
| 1879 | _PyUnicode_FromUCS2(const Py_UCS2 *u, Py_ssize_t size) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1880 | { |
| 1881 | PyObject *res; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1882 | Py_UCS2 max_char; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1883 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1884 | if (size == 0) { |
| 1885 | Py_INCREF(unicode_empty); |
| 1886 | return unicode_empty; |
| 1887 | } |
| 1888 | assert(size > 0); |
Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 1889 | if (size == 1 && u[0] < 256) |
Victor Stinner | 4e10100 | 2011-10-11 23:27:52 +0200 | [diff] [blame] | 1890 | return get_latin1_char((unsigned char)u[0]); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1891 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1892 | max_char = ucs2lib_find_max_char(u, u + size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1893 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1894 | if (!res) |
| 1895 | return NULL; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1896 | if (max_char >= 256) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1897 | memcpy(PyUnicode_2BYTE_DATA(res), u, sizeof(Py_UCS2)*size); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1898 | else { |
| 1899 | _PyUnicode_CONVERT_BYTES( |
| 1900 | Py_UCS2, Py_UCS1, u, u + size, PyUnicode_1BYTE_DATA(res)); |
| 1901 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1902 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1903 | return res; |
| 1904 | } |
| 1905 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1906 | static PyObject* |
| 1907 | _PyUnicode_FromUCS4(const Py_UCS4 *u, Py_ssize_t size) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1908 | { |
| 1909 | PyObject *res; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1910 | Py_UCS4 max_char; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1911 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1912 | if (size == 0) { |
| 1913 | Py_INCREF(unicode_empty); |
| 1914 | return unicode_empty; |
| 1915 | } |
| 1916 | assert(size > 0); |
Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 1917 | if (size == 1 && u[0] < 256) |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1918 | return get_latin1_char((unsigned char)u[0]); |
| 1919 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1920 | max_char = ucs4lib_find_max_char(u, u + size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1921 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1922 | if (!res) |
| 1923 | return NULL; |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 1924 | if (max_char < 256) |
| 1925 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS1, u, u + size, |
| 1926 | PyUnicode_1BYTE_DATA(res)); |
| 1927 | else if (max_char < 0x10000) |
| 1928 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS2, u, u + size, |
| 1929 | PyUnicode_2BYTE_DATA(res)); |
| 1930 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1931 | memcpy(PyUnicode_4BYTE_DATA(res), u, sizeof(Py_UCS4)*size); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1932 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1933 | return res; |
| 1934 | } |
| 1935 | |
| 1936 | PyObject* |
| 1937 | PyUnicode_FromKindAndData(int kind, const void *buffer, Py_ssize_t size) |
| 1938 | { |
Victor Stinner | cfed46e | 2011-11-22 01:29:14 +0100 | [diff] [blame] | 1939 | if (size < 0) { |
| 1940 | PyErr_SetString(PyExc_ValueError, "size must be positive"); |
| 1941 | return NULL; |
| 1942 | } |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 1943 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1944 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1945 | return _PyUnicode_FromUCS1(buffer, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1946 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1947 | return _PyUnicode_FromUCS2(buffer, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1948 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1949 | return _PyUnicode_FromUCS4(buffer, size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1950 | default: |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1951 | PyErr_SetString(PyExc_SystemError, "invalid kind"); |
| 1952 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1953 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1954 | } |
| 1955 | |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1956 | /* Ensure that a string uses the most efficient storage, if it is not the |
| 1957 | case: create a new string with of the right kind. Write NULL into *p_unicode |
| 1958 | on error. */ |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 1959 | static void |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1960 | unicode_adjust_maxchar(PyObject **p_unicode) |
| 1961 | { |
| 1962 | PyObject *unicode, *copy; |
| 1963 | Py_UCS4 max_char; |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1964 | Py_ssize_t len; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1965 | unsigned int kind; |
| 1966 | |
| 1967 | assert(p_unicode != NULL); |
| 1968 | unicode = *p_unicode; |
| 1969 | assert(PyUnicode_IS_READY(unicode)); |
| 1970 | if (PyUnicode_IS_ASCII(unicode)) |
| 1971 | return; |
| 1972 | |
| 1973 | len = PyUnicode_GET_LENGTH(unicode); |
| 1974 | kind = PyUnicode_KIND(unicode); |
| 1975 | if (kind == PyUnicode_1BYTE_KIND) { |
| 1976 | const Py_UCS1 *u = PyUnicode_1BYTE_DATA(unicode); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1977 | max_char = ucs1lib_find_max_char(u, u + len); |
| 1978 | if (max_char >= 128) |
| 1979 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1980 | } |
| 1981 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 1982 | const Py_UCS2 *u = PyUnicode_2BYTE_DATA(unicode); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1983 | max_char = ucs2lib_find_max_char(u, u + len); |
| 1984 | if (max_char >= 256) |
| 1985 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1986 | } |
| 1987 | else { |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1988 | const Py_UCS4 *u = PyUnicode_4BYTE_DATA(unicode); |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1989 | assert(kind == PyUnicode_4BYTE_KIND); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1990 | max_char = ucs4lib_find_max_char(u, u + len); |
| 1991 | if (max_char >= 0x10000) |
| 1992 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1993 | } |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1994 | copy = PyUnicode_New(len, max_char); |
| 1995 | copy_characters(copy, 0, unicode, 0, len); |
| 1996 | Py_DECREF(unicode); |
| 1997 | *p_unicode = copy; |
| 1998 | } |
| 1999 | |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2000 | PyObject* |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 2001 | _PyUnicode_Copy(PyObject *unicode) |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2002 | { |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 2003 | Py_ssize_t length; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2004 | PyObject *copy; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2005 | |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2006 | if (!PyUnicode_Check(unicode)) { |
| 2007 | PyErr_BadInternalCall(); |
| 2008 | return NULL; |
| 2009 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 2010 | if (PyUnicode_READY(unicode) == -1) |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2011 | return NULL; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2012 | |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 2013 | length = PyUnicode_GET_LENGTH(unicode); |
| 2014 | copy = PyUnicode_New(length, PyUnicode_MAX_CHAR_VALUE(unicode)); |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2015 | if (!copy) |
| 2016 | return NULL; |
| 2017 | assert(PyUnicode_KIND(copy) == PyUnicode_KIND(unicode)); |
| 2018 | |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 2019 | Py_MEMCPY(PyUnicode_DATA(copy), PyUnicode_DATA(unicode), |
| 2020 | length * PyUnicode_KIND(unicode)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2021 | assert(_PyUnicode_CheckConsistency(copy, 1)); |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2022 | return copy; |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2023 | } |
| 2024 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2025 | |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2026 | /* Widen Unicode objects to larger buffers. Don't write terminating null |
| 2027 | character. Return NULL on error. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2028 | |
| 2029 | void* |
| 2030 | _PyUnicode_AsKind(PyObject *s, unsigned int kind) |
| 2031 | { |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2032 | Py_ssize_t len; |
| 2033 | void *result; |
| 2034 | unsigned int skind; |
| 2035 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 2036 | if (PyUnicode_READY(s) == -1) |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2037 | return NULL; |
| 2038 | |
| 2039 | len = PyUnicode_GET_LENGTH(s); |
| 2040 | skind = PyUnicode_KIND(s); |
| 2041 | if (skind >= kind) { |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 2042 | PyErr_SetString(PyExc_SystemError, "invalid widening attempt"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2043 | return NULL; |
| 2044 | } |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 2045 | switch (kind) { |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2046 | case PyUnicode_2BYTE_KIND: |
| 2047 | result = PyMem_Malloc(len * sizeof(Py_UCS2)); |
| 2048 | if (!result) |
| 2049 | return PyErr_NoMemory(); |
| 2050 | assert(skind == PyUnicode_1BYTE_KIND); |
| 2051 | _PyUnicode_CONVERT_BYTES( |
| 2052 | Py_UCS1, Py_UCS2, |
| 2053 | PyUnicode_1BYTE_DATA(s), |
| 2054 | PyUnicode_1BYTE_DATA(s) + len, |
| 2055 | result); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2056 | return result; |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2057 | case PyUnicode_4BYTE_KIND: |
| 2058 | result = PyMem_Malloc(len * sizeof(Py_UCS4)); |
| 2059 | if (!result) |
| 2060 | return PyErr_NoMemory(); |
| 2061 | if (skind == PyUnicode_2BYTE_KIND) { |
| 2062 | _PyUnicode_CONVERT_BYTES( |
| 2063 | Py_UCS2, Py_UCS4, |
| 2064 | PyUnicode_2BYTE_DATA(s), |
| 2065 | PyUnicode_2BYTE_DATA(s) + len, |
| 2066 | result); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2067 | } |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2068 | else { |
| 2069 | assert(skind == PyUnicode_1BYTE_KIND); |
| 2070 | _PyUnicode_CONVERT_BYTES( |
| 2071 | Py_UCS1, Py_UCS4, |
| 2072 | PyUnicode_1BYTE_DATA(s), |
| 2073 | PyUnicode_1BYTE_DATA(s) + len, |
| 2074 | result); |
| 2075 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2076 | return result; |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2077 | default: |
| 2078 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2079 | } |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 2080 | PyErr_SetString(PyExc_SystemError, "invalid kind"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2081 | return NULL; |
| 2082 | } |
| 2083 | |
| 2084 | static Py_UCS4* |
| 2085 | as_ucs4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, |
| 2086 | int copy_null) |
| 2087 | { |
| 2088 | int kind; |
| 2089 | void *data; |
| 2090 | Py_ssize_t len, targetlen; |
| 2091 | if (PyUnicode_READY(string) == -1) |
| 2092 | return NULL; |
| 2093 | kind = PyUnicode_KIND(string); |
| 2094 | data = PyUnicode_DATA(string); |
| 2095 | len = PyUnicode_GET_LENGTH(string); |
| 2096 | targetlen = len; |
| 2097 | if (copy_null) |
| 2098 | targetlen++; |
| 2099 | if (!target) { |
| 2100 | if (PY_SSIZE_T_MAX / sizeof(Py_UCS4) < targetlen) { |
| 2101 | PyErr_NoMemory(); |
| 2102 | return NULL; |
| 2103 | } |
| 2104 | target = PyMem_Malloc(targetlen * sizeof(Py_UCS4)); |
| 2105 | if (!target) { |
| 2106 | PyErr_NoMemory(); |
| 2107 | return NULL; |
| 2108 | } |
| 2109 | } |
| 2110 | else { |
| 2111 | if (targetsize < targetlen) { |
| 2112 | PyErr_Format(PyExc_SystemError, |
| 2113 | "string is longer than the buffer"); |
| 2114 | if (copy_null && 0 < targetsize) |
| 2115 | target[0] = 0; |
| 2116 | return NULL; |
| 2117 | } |
| 2118 | } |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2119 | if (kind == PyUnicode_1BYTE_KIND) { |
| 2120 | Py_UCS1 *start = (Py_UCS1 *) data; |
| 2121 | _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] | 2122 | } |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2123 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 2124 | Py_UCS2 *start = (Py_UCS2 *) data; |
| 2125 | _PyUnicode_CONVERT_BYTES(Py_UCS2, Py_UCS4, start, start + len, target); |
| 2126 | } |
| 2127 | else { |
| 2128 | assert(kind == PyUnicode_4BYTE_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2129 | Py_MEMCPY(target, data, len * sizeof(Py_UCS4)); |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2130 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2131 | if (copy_null) |
| 2132 | target[len] = 0; |
| 2133 | return target; |
| 2134 | } |
| 2135 | |
| 2136 | Py_UCS4* |
| 2137 | PyUnicode_AsUCS4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, |
| 2138 | int copy_null) |
| 2139 | { |
Antoine Pitrou | de20b0b | 2011-11-10 21:47:38 +0100 | [diff] [blame] | 2140 | if (target == NULL || targetsize < 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2141 | PyErr_BadInternalCall(); |
| 2142 | return NULL; |
| 2143 | } |
| 2144 | return as_ucs4(string, target, targetsize, copy_null); |
| 2145 | } |
| 2146 | |
| 2147 | Py_UCS4* |
| 2148 | PyUnicode_AsUCS4Copy(PyObject *string) |
| 2149 | { |
| 2150 | return as_ucs4(string, NULL, 0, 1); |
| 2151 | } |
| 2152 | |
| 2153 | #ifdef HAVE_WCHAR_H |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 2154 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2155 | PyObject * |
| 2156 | PyUnicode_FromWideChar(register const wchar_t *w, Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2157 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2158 | if (w == NULL) { |
Victor Stinner | 382955f | 2011-12-11 21:44:00 +0100 | [diff] [blame] | 2159 | if (size == 0) { |
| 2160 | Py_INCREF(unicode_empty); |
| 2161 | return unicode_empty; |
| 2162 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2163 | PyErr_BadInternalCall(); |
| 2164 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2165 | } |
| 2166 | |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 2167 | if (size == -1) { |
| 2168 | size = wcslen(w); |
| 2169 | } |
| 2170 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2171 | return PyUnicode_FromUnicode(w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2172 | } |
| 2173 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2174 | #endif /* HAVE_WCHAR_H */ |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 2175 | |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2176 | static void |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2177 | makefmt(char *fmt, int longflag, int longlongflag, int size_tflag, |
| 2178 | int zeropad, int width, int precision, char c) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2179 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2180 | *fmt++ = '%'; |
| 2181 | if (width) { |
| 2182 | if (zeropad) |
| 2183 | *fmt++ = '0'; |
| 2184 | fmt += sprintf(fmt, "%d", width); |
| 2185 | } |
| 2186 | if (precision) |
| 2187 | fmt += sprintf(fmt, ".%d", precision); |
| 2188 | if (longflag) |
| 2189 | *fmt++ = 'l'; |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2190 | else if (longlongflag) { |
| 2191 | /* longlongflag should only ever be nonzero on machines with |
| 2192 | HAVE_LONG_LONG defined */ |
| 2193 | #ifdef HAVE_LONG_LONG |
| 2194 | char *f = PY_FORMAT_LONG_LONG; |
| 2195 | while (*f) |
| 2196 | *fmt++ = *f++; |
| 2197 | #else |
| 2198 | /* we shouldn't ever get here */ |
| 2199 | assert(0); |
| 2200 | *fmt++ = 'l'; |
| 2201 | #endif |
| 2202 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2203 | else if (size_tflag) { |
| 2204 | char *f = PY_FORMAT_SIZE_T; |
| 2205 | while (*f) |
| 2206 | *fmt++ = *f++; |
| 2207 | } |
| 2208 | *fmt++ = c; |
| 2209 | *fmt = '\0'; |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2210 | } |
| 2211 | |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2212 | /* helper for PyUnicode_FromFormatV() */ |
| 2213 | |
| 2214 | static const char* |
| 2215 | parse_format_flags(const char *f, |
| 2216 | int *p_width, int *p_precision, |
| 2217 | int *p_longflag, int *p_longlongflag, int *p_size_tflag) |
| 2218 | { |
| 2219 | int width, precision, longflag, longlongflag, size_tflag; |
| 2220 | |
| 2221 | /* parse the width.precision part, e.g. "%2.5s" => width=2, precision=5 */ |
| 2222 | f++; |
| 2223 | width = 0; |
| 2224 | while (Py_ISDIGIT((unsigned)*f)) |
| 2225 | width = (width*10) + *f++ - '0'; |
| 2226 | precision = 0; |
| 2227 | if (*f == '.') { |
| 2228 | f++; |
| 2229 | while (Py_ISDIGIT((unsigned)*f)) |
| 2230 | precision = (precision*10) + *f++ - '0'; |
| 2231 | if (*f == '%') { |
| 2232 | /* "%.3%s" => f points to "3" */ |
| 2233 | f--; |
| 2234 | } |
| 2235 | } |
| 2236 | if (*f == '\0') { |
| 2237 | /* bogus format "%.1" => go backward, f points to "1" */ |
| 2238 | f--; |
| 2239 | } |
| 2240 | if (p_width != NULL) |
| 2241 | *p_width = width; |
| 2242 | if (p_precision != NULL) |
| 2243 | *p_precision = precision; |
| 2244 | |
| 2245 | /* Handle %ld, %lu, %lld and %llu. */ |
| 2246 | longflag = 0; |
| 2247 | longlongflag = 0; |
Victor Stinner | e7faec1 | 2011-03-02 00:01:53 +0000 | [diff] [blame] | 2248 | size_tflag = 0; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2249 | |
| 2250 | if (*f == 'l') { |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2251 | if (f[1] == 'd' || f[1] == 'u' || f[1] == 'i') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2252 | longflag = 1; |
| 2253 | ++f; |
| 2254 | } |
| 2255 | #ifdef HAVE_LONG_LONG |
| 2256 | else if (f[1] == 'l' && |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2257 | (f[2] == 'd' || f[2] == 'u' || f[2] == 'i')) { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2258 | longlongflag = 1; |
| 2259 | f += 2; |
| 2260 | } |
| 2261 | #endif |
| 2262 | } |
| 2263 | /* handle the size_t flag. */ |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2264 | 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] | 2265 | size_tflag = 1; |
| 2266 | ++f; |
| 2267 | } |
| 2268 | if (p_longflag != NULL) |
| 2269 | *p_longflag = longflag; |
| 2270 | if (p_longlongflag != NULL) |
| 2271 | *p_longlongflag = longlongflag; |
| 2272 | if (p_size_tflag != NULL) |
| 2273 | *p_size_tflag = size_tflag; |
| 2274 | return f; |
| 2275 | } |
| 2276 | |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2277 | /* maximum number of characters required for output of %ld. 21 characters |
| 2278 | allows for 64-bit integers (in decimal) and an optional sign. */ |
| 2279 | #define MAX_LONG_CHARS 21 |
| 2280 | /* maximum number of characters required for output of %lld. |
| 2281 | We need at most ceil(log10(256)*SIZEOF_LONG_LONG) digits, |
| 2282 | plus 1 for the sign. 53/22 is an upper bound for log10(256). */ |
| 2283 | #define MAX_LONG_LONG_CHARS (2 + (SIZEOF_LONG_LONG*53-1) / 22) |
| 2284 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2285 | PyObject * |
| 2286 | PyUnicode_FromFormatV(const char *format, va_list vargs) |
| 2287 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2288 | va_list count; |
| 2289 | Py_ssize_t callcount = 0; |
| 2290 | PyObject **callresults = NULL; |
| 2291 | PyObject **callresult = NULL; |
| 2292 | Py_ssize_t n = 0; |
| 2293 | int width = 0; |
| 2294 | int precision = 0; |
| 2295 | int zeropad; |
| 2296 | const char* f; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2297 | PyObject *string; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2298 | /* used by sprintf */ |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2299 | char fmt[61]; /* should be enough for %0width.precisionlld */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2300 | Py_UCS4 maxchar = 127; /* result is ASCII by default */ |
| 2301 | Py_UCS4 argmaxchar; |
| 2302 | Py_ssize_t numbersize = 0; |
| 2303 | char *numberresults = NULL; |
| 2304 | char *numberresult = NULL; |
| 2305 | Py_ssize_t i; |
| 2306 | int kind; |
| 2307 | void *data; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2308 | |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 2309 | Py_VA_COPY(count, vargs); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2310 | /* step 1: count the number of %S/%R/%A/%s format specifications |
| 2311 | * (we call PyObject_Str()/PyObject_Repr()/PyObject_ASCII()/ |
| 2312 | * 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] | 2313 | * result in an array) |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 2314 | * also estimate a upper bound for all the number formats in the string, |
| 2315 | * 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] | 2316 | * buffer before putting everything together. */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2317 | for (f = format; *f; f++) { |
| 2318 | if (*f == '%') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2319 | int longlongflag; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2320 | /* skip width or width.precision (eg. "1.2" of "%1.2f") */ |
| 2321 | f = parse_format_flags(f, &width, NULL, NULL, &longlongflag, NULL); |
| 2322 | if (*f == 's' || *f=='S' || *f=='R' || *f=='A' || *f=='V') |
| 2323 | ++callcount; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2324 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2325 | else if (*f == 'd' || *f=='u' || *f=='i' || *f=='x' || *f=='p') { |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2326 | #ifdef HAVE_LONG_LONG |
| 2327 | if (longlongflag) { |
| 2328 | if (width < MAX_LONG_LONG_CHARS) |
| 2329 | width = MAX_LONG_LONG_CHARS; |
| 2330 | } |
| 2331 | else |
| 2332 | #endif |
| 2333 | /* MAX_LONG_CHARS is enough to hold a 64-bit integer, |
| 2334 | including sign. Decimal takes the most space. This |
| 2335 | isn't enough for octal. If a width is specified we |
| 2336 | need more (which we allocate later). */ |
| 2337 | if (width < MAX_LONG_CHARS) |
| 2338 | width = MAX_LONG_CHARS; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2339 | |
| 2340 | /* account for the size + '\0' to separate numbers |
| 2341 | inside of the numberresults buffer */ |
| 2342 | numbersize += (width + 1); |
| 2343 | } |
| 2344 | } |
| 2345 | else if ((unsigned char)*f > 127) { |
| 2346 | PyErr_Format(PyExc_ValueError, |
| 2347 | "PyUnicode_FromFormatV() expects an ASCII-encoded format " |
| 2348 | "string, got a non-ASCII byte: 0x%02x", |
| 2349 | (unsigned char)*f); |
| 2350 | return NULL; |
| 2351 | } |
| 2352 | } |
| 2353 | /* step 2: allocate memory for the results of |
| 2354 | * PyObject_Str()/PyObject_Repr()/PyUnicode_DecodeUTF8() calls */ |
| 2355 | if (callcount) { |
| 2356 | callresults = PyObject_Malloc(sizeof(PyObject *) * callcount); |
| 2357 | if (!callresults) { |
| 2358 | PyErr_NoMemory(); |
| 2359 | return NULL; |
| 2360 | } |
| 2361 | callresult = callresults; |
| 2362 | } |
| 2363 | /* step 2.5: allocate memory for the results of formating numbers */ |
| 2364 | if (numbersize) { |
| 2365 | numberresults = PyObject_Malloc(numbersize); |
| 2366 | if (!numberresults) { |
| 2367 | PyErr_NoMemory(); |
| 2368 | goto fail; |
| 2369 | } |
| 2370 | numberresult = numberresults; |
| 2371 | } |
| 2372 | |
| 2373 | /* step 3: format numbers and figure out how large a buffer we need */ |
| 2374 | for (f = format; *f; f++) { |
| 2375 | if (*f == '%') { |
| 2376 | const char* p; |
| 2377 | int longflag; |
| 2378 | int longlongflag; |
| 2379 | int size_tflag; |
| 2380 | int numprinted; |
| 2381 | |
| 2382 | p = f; |
| 2383 | zeropad = (f[1] == '0'); |
| 2384 | f = parse_format_flags(f, &width, &precision, |
| 2385 | &longflag, &longlongflag, &size_tflag); |
| 2386 | switch (*f) { |
| 2387 | case 'c': |
| 2388 | { |
| 2389 | Py_UCS4 ordinal = va_arg(count, int); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2390 | maxchar = Py_MAX(maxchar, ordinal); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2391 | n++; |
| 2392 | break; |
| 2393 | } |
| 2394 | case '%': |
| 2395 | n++; |
| 2396 | break; |
| 2397 | case 'i': |
| 2398 | case 'd': |
| 2399 | makefmt(fmt, longflag, longlongflag, size_tflag, zeropad, |
| 2400 | width, precision, *f); |
| 2401 | if (longflag) |
| 2402 | numprinted = sprintf(numberresult, fmt, |
| 2403 | va_arg(count, long)); |
| 2404 | #ifdef HAVE_LONG_LONG |
| 2405 | else if (longlongflag) |
| 2406 | numprinted = sprintf(numberresult, fmt, |
| 2407 | va_arg(count, PY_LONG_LONG)); |
| 2408 | #endif |
| 2409 | else if (size_tflag) |
| 2410 | numprinted = sprintf(numberresult, fmt, |
| 2411 | va_arg(count, Py_ssize_t)); |
| 2412 | else |
| 2413 | numprinted = sprintf(numberresult, fmt, |
| 2414 | va_arg(count, int)); |
| 2415 | n += numprinted; |
| 2416 | /* advance by +1 to skip over the '\0' */ |
| 2417 | numberresult += (numprinted + 1); |
| 2418 | assert(*(numberresult - 1) == '\0'); |
| 2419 | assert(*(numberresult - 2) != '\0'); |
| 2420 | assert(numprinted >= 0); |
| 2421 | assert(numberresult <= numberresults + numbersize); |
| 2422 | break; |
| 2423 | case 'u': |
| 2424 | makefmt(fmt, longflag, longlongflag, size_tflag, zeropad, |
| 2425 | width, precision, 'u'); |
| 2426 | if (longflag) |
| 2427 | numprinted = sprintf(numberresult, fmt, |
| 2428 | va_arg(count, unsigned long)); |
| 2429 | #ifdef HAVE_LONG_LONG |
| 2430 | else if (longlongflag) |
| 2431 | numprinted = sprintf(numberresult, fmt, |
| 2432 | va_arg(count, unsigned PY_LONG_LONG)); |
| 2433 | #endif |
| 2434 | else if (size_tflag) |
| 2435 | numprinted = sprintf(numberresult, fmt, |
| 2436 | va_arg(count, size_t)); |
| 2437 | else |
| 2438 | numprinted = sprintf(numberresult, fmt, |
| 2439 | va_arg(count, unsigned int)); |
| 2440 | n += numprinted; |
| 2441 | numberresult += (numprinted + 1); |
| 2442 | assert(*(numberresult - 1) == '\0'); |
| 2443 | assert(*(numberresult - 2) != '\0'); |
| 2444 | assert(numprinted >= 0); |
| 2445 | assert(numberresult <= numberresults + numbersize); |
| 2446 | break; |
| 2447 | case 'x': |
| 2448 | makefmt(fmt, 0, 0, 0, zeropad, width, precision, 'x'); |
| 2449 | numprinted = sprintf(numberresult, fmt, va_arg(count, int)); |
| 2450 | n += numprinted; |
| 2451 | numberresult += (numprinted + 1); |
| 2452 | assert(*(numberresult - 1) == '\0'); |
| 2453 | assert(*(numberresult - 2) != '\0'); |
| 2454 | assert(numprinted >= 0); |
| 2455 | assert(numberresult <= numberresults + numbersize); |
| 2456 | break; |
| 2457 | case 'p': |
| 2458 | numprinted = sprintf(numberresult, "%p", va_arg(count, void*)); |
| 2459 | /* %p is ill-defined: ensure leading 0x. */ |
| 2460 | if (numberresult[1] == 'X') |
| 2461 | numberresult[1] = 'x'; |
| 2462 | else if (numberresult[1] != 'x') { |
| 2463 | memmove(numberresult + 2, numberresult, |
| 2464 | strlen(numberresult) + 1); |
| 2465 | numberresult[0] = '0'; |
| 2466 | numberresult[1] = 'x'; |
| 2467 | numprinted += 2; |
| 2468 | } |
| 2469 | n += numprinted; |
| 2470 | numberresult += (numprinted + 1); |
| 2471 | assert(*(numberresult - 1) == '\0'); |
| 2472 | assert(*(numberresult - 2) != '\0'); |
| 2473 | assert(numprinted >= 0); |
| 2474 | assert(numberresult <= numberresults + numbersize); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2475 | break; |
| 2476 | case 's': |
| 2477 | { |
| 2478 | /* UTF-8 */ |
Georg Brandl | 780b2a6 | 2009-05-05 09:19:59 +0000 | [diff] [blame] | 2479 | const char *s = va_arg(count, const char*); |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 2480 | PyObject *str = PyUnicode_DecodeUTF8Stateful(s, strlen(s), "replace", NULL); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2481 | if (!str) |
| 2482 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2483 | /* since PyUnicode_DecodeUTF8 returns already flexible |
| 2484 | unicode objects, there is no need to call ready on them */ |
| 2485 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2486 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2487 | n += PyUnicode_GET_LENGTH(str); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2488 | /* Remember the str and switch to the next slot */ |
| 2489 | *callresult++ = str; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2490 | break; |
| 2491 | } |
| 2492 | case 'U': |
| 2493 | { |
| 2494 | PyObject *obj = va_arg(count, PyObject *); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 2495 | assert(obj && _PyUnicode_CHECK(obj)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2496 | if (PyUnicode_READY(obj) == -1) |
| 2497 | goto fail; |
| 2498 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2499 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2500 | n += PyUnicode_GET_LENGTH(obj); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2501 | break; |
| 2502 | } |
| 2503 | case 'V': |
| 2504 | { |
| 2505 | PyObject *obj = va_arg(count, PyObject *); |
| 2506 | const char *str = va_arg(count, const char *); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2507 | PyObject *str_obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2508 | assert(obj || str); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 2509 | assert(!obj || _PyUnicode_CHECK(obj)); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2510 | if (obj) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2511 | if (PyUnicode_READY(obj) == -1) |
| 2512 | goto fail; |
| 2513 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2514 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2515 | n += PyUnicode_GET_LENGTH(obj); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2516 | *callresult++ = NULL; |
| 2517 | } |
| 2518 | else { |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 2519 | str_obj = PyUnicode_DecodeUTF8Stateful(str, strlen(str), "replace", NULL); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2520 | if (!str_obj) |
| 2521 | goto fail; |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 2522 | if (PyUnicode_READY(str_obj) == -1) { |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 2523 | Py_DECREF(str_obj); |
| 2524 | goto fail; |
| 2525 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2526 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str_obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2527 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2528 | n += PyUnicode_GET_LENGTH(str_obj); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2529 | *callresult++ = str_obj; |
| 2530 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2531 | break; |
| 2532 | } |
| 2533 | case 'S': |
| 2534 | { |
| 2535 | PyObject *obj = va_arg(count, PyObject *); |
| 2536 | PyObject *str; |
| 2537 | assert(obj); |
| 2538 | str = PyObject_Str(obj); |
Benjamin Peterson | c8d8b88 | 2012-01-14 13:37:31 -0500 | [diff] [blame] | 2539 | if (!str) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2540 | goto fail; |
Benjamin Peterson | c8d8b88 | 2012-01-14 13:37:31 -0500 | [diff] [blame] | 2541 | if (PyUnicode_READY(str) == -1) { |
| 2542 | Py_DECREF(str); |
| 2543 | goto fail; |
| 2544 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2545 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2546 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2547 | n += PyUnicode_GET_LENGTH(str); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2548 | /* Remember the str and switch to the next slot */ |
| 2549 | *callresult++ = str; |
| 2550 | break; |
| 2551 | } |
| 2552 | case 'R': |
| 2553 | { |
| 2554 | PyObject *obj = va_arg(count, PyObject *); |
| 2555 | PyObject *repr; |
| 2556 | assert(obj); |
| 2557 | repr = PyObject_Repr(obj); |
Benjamin Peterson | c8d8b88 | 2012-01-14 13:37:31 -0500 | [diff] [blame] | 2558 | if (!repr) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2559 | goto fail; |
Benjamin Peterson | c8d8b88 | 2012-01-14 13:37:31 -0500 | [diff] [blame] | 2560 | if (PyUnicode_READY(repr) == -1) { |
| 2561 | Py_DECREF(repr); |
| 2562 | goto fail; |
| 2563 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2564 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(repr); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2565 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2566 | n += PyUnicode_GET_LENGTH(repr); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2567 | /* Remember the repr and switch to the next slot */ |
| 2568 | *callresult++ = repr; |
| 2569 | break; |
| 2570 | } |
| 2571 | case 'A': |
| 2572 | { |
| 2573 | PyObject *obj = va_arg(count, PyObject *); |
| 2574 | PyObject *ascii; |
| 2575 | assert(obj); |
| 2576 | ascii = PyObject_ASCII(obj); |
Benjamin Peterson | c8d8b88 | 2012-01-14 13:37:31 -0500 | [diff] [blame] | 2577 | if (!ascii) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2578 | goto fail; |
Benjamin Peterson | c8d8b88 | 2012-01-14 13:37:31 -0500 | [diff] [blame] | 2579 | if (PyUnicode_READY(ascii) == -1) { |
| 2580 | Py_DECREF(ascii); |
| 2581 | goto fail; |
| 2582 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2583 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(ascii); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2584 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2585 | n += PyUnicode_GET_LENGTH(ascii); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2586 | /* Remember the repr and switch to the next slot */ |
| 2587 | *callresult++ = ascii; |
| 2588 | break; |
| 2589 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2590 | default: |
| 2591 | /* if we stumble upon an unknown |
| 2592 | formatting code, copy the rest of |
| 2593 | the format string to the output |
| 2594 | string. (we cannot just skip the |
| 2595 | code, since there's no way to know |
| 2596 | what's in the argument list) */ |
| 2597 | n += strlen(p); |
| 2598 | goto expand; |
| 2599 | } |
| 2600 | } else |
| 2601 | n++; |
| 2602 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2603 | expand: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2604 | /* step 4: fill the buffer */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2605 | /* Since we've analyzed how much space we need, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2606 | we don't have to resize the string. |
| 2607 | There can be no errors beyond this point. */ |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2608 | string = PyUnicode_New(n, maxchar); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2609 | if (!string) |
| 2610 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2611 | kind = PyUnicode_KIND(string); |
| 2612 | data = PyUnicode_DATA(string); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2613 | callresult = callresults; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2614 | numberresult = numberresults; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2615 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2616 | for (i = 0, f = format; *f; f++) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2617 | if (*f == '%') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2618 | const char* p; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2619 | |
| 2620 | p = f; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2621 | f = parse_format_flags(f, NULL, NULL, NULL, NULL, NULL); |
| 2622 | /* checking for == because the last argument could be a empty |
| 2623 | string, which causes i to point to end, the assert at the end of |
| 2624 | the loop */ |
| 2625 | assert(i <= PyUnicode_GET_LENGTH(string)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2626 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2627 | switch (*f) { |
| 2628 | case 'c': |
Victor Stinner | 5ed8b2c | 2011-02-21 21:13:44 +0000 | [diff] [blame] | 2629 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2630 | const int ordinal = va_arg(vargs, int); |
| 2631 | PyUnicode_WRITE(kind, data, i++, ordinal); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2632 | break; |
Victor Stinner | 5ed8b2c | 2011-02-21 21:13:44 +0000 | [diff] [blame] | 2633 | } |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2634 | case 'i': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2635 | case 'd': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2636 | case 'u': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2637 | case 'x': |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2638 | case 'p': |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 2639 | { |
| 2640 | Py_ssize_t written; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2641 | /* unused, since we already have the result */ |
| 2642 | if (*f == 'p') |
| 2643 | (void) va_arg(vargs, void *); |
| 2644 | else |
| 2645 | (void) va_arg(vargs, int); |
| 2646 | /* extract the result from numberresults and append. */ |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 2647 | written = unicode_write_cstr(string, i, numberresult); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2648 | /* skip over the separating '\0' */ |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 2649 | i += written; |
| 2650 | numberresult += written; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2651 | assert(*numberresult == '\0'); |
| 2652 | numberresult++; |
| 2653 | assert(numberresult <= numberresults + numbersize); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2654 | break; |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 2655 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2656 | case 's': |
| 2657 | { |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2658 | /* unused, since we already have the result */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2659 | Py_ssize_t size; |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2660 | (void) va_arg(vargs, char *); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2661 | size = PyUnicode_GET_LENGTH(*callresult); |
| 2662 | assert(PyUnicode_KIND(*callresult) <= PyUnicode_KIND(string)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2663 | copy_characters(string, i, *callresult, 0, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2664 | i += size; |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2665 | /* We're done with the unicode()/repr() => forget it */ |
| 2666 | Py_DECREF(*callresult); |
| 2667 | /* switch to next unicode()/repr() result */ |
| 2668 | ++callresult; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2669 | break; |
| 2670 | } |
| 2671 | case 'U': |
| 2672 | { |
| 2673 | PyObject *obj = va_arg(vargs, PyObject *); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2674 | Py_ssize_t size; |
| 2675 | assert(PyUnicode_KIND(obj) <= PyUnicode_KIND(string)); |
| 2676 | size = PyUnicode_GET_LENGTH(obj); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2677 | copy_characters(string, i, obj, 0, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2678 | i += size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2679 | break; |
| 2680 | } |
| 2681 | case 'V': |
| 2682 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2683 | Py_ssize_t size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2684 | PyObject *obj = va_arg(vargs, PyObject *); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2685 | va_arg(vargs, const char *); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2686 | if (obj) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2687 | size = PyUnicode_GET_LENGTH(obj); |
| 2688 | assert(PyUnicode_KIND(obj) <= PyUnicode_KIND(string)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2689 | copy_characters(string, i, obj, 0, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2690 | i += size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2691 | } else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2692 | size = PyUnicode_GET_LENGTH(*callresult); |
| 2693 | assert(PyUnicode_KIND(*callresult) <= |
| 2694 | PyUnicode_KIND(string)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2695 | copy_characters(string, i, *callresult, 0, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2696 | i += size; |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2697 | Py_DECREF(*callresult); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2698 | } |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2699 | ++callresult; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2700 | break; |
| 2701 | } |
| 2702 | case 'S': |
| 2703 | case 'R': |
Victor Stinner | 9a90900 | 2010-10-18 20:59:24 +0000 | [diff] [blame] | 2704 | case 'A': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2705 | { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2706 | Py_ssize_t size = PyUnicode_GET_LENGTH(*callresult); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2707 | /* unused, since we already have the result */ |
| 2708 | (void) va_arg(vargs, PyObject *); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2709 | assert(PyUnicode_KIND(*callresult) <= PyUnicode_KIND(string)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2710 | copy_characters(string, i, *callresult, 0, size); |
| 2711 | i += size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2712 | /* We're done with the unicode()/repr() => forget it */ |
| 2713 | Py_DECREF(*callresult); |
| 2714 | /* switch to next unicode()/repr() result */ |
| 2715 | ++callresult; |
| 2716 | break; |
| 2717 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2718 | case '%': |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2719 | PyUnicode_WRITE(kind, data, i++, '%'); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2720 | break; |
| 2721 | default: |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 2722 | i += unicode_write_cstr(string, i, p); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2723 | assert(i == PyUnicode_GET_LENGTH(string)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2724 | goto end; |
| 2725 | } |
Victor Stinner | 1205f27 | 2010-09-11 00:54:47 +0000 | [diff] [blame] | 2726 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2727 | else { |
| 2728 | assert(i < PyUnicode_GET_LENGTH(string)); |
| 2729 | PyUnicode_WRITE(kind, data, i++, *f); |
| 2730 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2731 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2732 | assert(i == PyUnicode_GET_LENGTH(string)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2733 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2734 | end: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2735 | if (callresults) |
| 2736 | PyObject_Free(callresults); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2737 | if (numberresults) |
| 2738 | PyObject_Free(numberresults); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 2739 | return unicode_result(string); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2740 | fail: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2741 | if (callresults) { |
| 2742 | PyObject **callresult2 = callresults; |
| 2743 | while (callresult2 < callresult) { |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2744 | Py_XDECREF(*callresult2); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2745 | ++callresult2; |
| 2746 | } |
| 2747 | PyObject_Free(callresults); |
| 2748 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2749 | if (numberresults) |
| 2750 | PyObject_Free(numberresults); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2751 | return NULL; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2752 | } |
| 2753 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2754 | PyObject * |
| 2755 | PyUnicode_FromFormat(const char *format, ...) |
| 2756 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2757 | PyObject* ret; |
| 2758 | va_list vargs; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2759 | |
| 2760 | #ifdef HAVE_STDARG_PROTOTYPES |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2761 | va_start(vargs, format); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2762 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2763 | va_start(vargs); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2764 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2765 | ret = PyUnicode_FromFormatV(format, vargs); |
| 2766 | va_end(vargs); |
| 2767 | return ret; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2768 | } |
| 2769 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2770 | #ifdef HAVE_WCHAR_H |
| 2771 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2772 | /* Helper function for PyUnicode_AsWideChar() and PyUnicode_AsWideCharString(): |
| 2773 | convert a Unicode object to a wide character string. |
| 2774 | |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2775 | - 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] | 2776 | character) required to convert the unicode object. Ignore size argument. |
| 2777 | |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2778 | - Otherwise: return the number of wide characters (excluding the null |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2779 | character) written into w. Write at most size wide characters (including |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2780 | the null character). */ |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2781 | static Py_ssize_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2782 | unicode_aswidechar(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2783 | wchar_t *w, |
| 2784 | Py_ssize_t size) |
| 2785 | { |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2786 | Py_ssize_t res; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2787 | const wchar_t *wstr; |
| 2788 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2789 | wstr = PyUnicode_AsUnicodeAndSize(unicode, &res); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2790 | if (wstr == NULL) |
| 2791 | return -1; |
| 2792 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2793 | if (w != NULL) { |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2794 | if (size > res) |
| 2795 | size = res + 1; |
| 2796 | else |
| 2797 | res = size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2798 | Py_MEMCPY(w, wstr, size * sizeof(wchar_t)); |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2799 | return res; |
| 2800 | } |
| 2801 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2802 | return res + 1; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2803 | } |
| 2804 | |
| 2805 | Py_ssize_t |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2806 | PyUnicode_AsWideChar(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2807 | wchar_t *w, |
| 2808 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2809 | { |
| 2810 | if (unicode == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2811 | PyErr_BadInternalCall(); |
| 2812 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2813 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2814 | return unicode_aswidechar(unicode, w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2815 | } |
| 2816 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2817 | wchar_t* |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 2818 | PyUnicode_AsWideCharString(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2819 | Py_ssize_t *size) |
| 2820 | { |
| 2821 | wchar_t* buffer; |
| 2822 | Py_ssize_t buflen; |
| 2823 | |
| 2824 | if (unicode == NULL) { |
| 2825 | PyErr_BadInternalCall(); |
| 2826 | return NULL; |
| 2827 | } |
| 2828 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2829 | buflen = unicode_aswidechar(unicode, NULL, 0); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2830 | if (buflen == -1) |
| 2831 | return NULL; |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2832 | if (PY_SSIZE_T_MAX / sizeof(wchar_t) < buflen) { |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2833 | PyErr_NoMemory(); |
| 2834 | return NULL; |
| 2835 | } |
| 2836 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2837 | buffer = PyMem_MALLOC(buflen * sizeof(wchar_t)); |
| 2838 | if (buffer == NULL) { |
| 2839 | PyErr_NoMemory(); |
| 2840 | return NULL; |
| 2841 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2842 | buflen = unicode_aswidechar(unicode, buffer, buflen); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2843 | if (buflen == -1) |
| 2844 | return NULL; |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2845 | if (size != NULL) |
| 2846 | *size = buflen; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2847 | return buffer; |
| 2848 | } |
| 2849 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2850 | #endif /* HAVE_WCHAR_H */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2851 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2852 | PyObject * |
| 2853 | PyUnicode_FromOrdinal(int ordinal) |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2854 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2855 | PyObject *v; |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 2856 | if (ordinal < 0 || ordinal > MAX_UNICODE) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2857 | PyErr_SetString(PyExc_ValueError, |
| 2858 | "chr() arg not in range(0x110000)"); |
| 2859 | return NULL; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2860 | } |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 2861 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2862 | if (ordinal < 256) |
| 2863 | return get_latin1_char(ordinal); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2864 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2865 | v = PyUnicode_New(1, ordinal); |
| 2866 | if (v == NULL) |
| 2867 | return NULL; |
| 2868 | PyUnicode_WRITE(PyUnicode_KIND(v), PyUnicode_DATA(v), 0, ordinal); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2869 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2870 | return v; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2871 | } |
| 2872 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2873 | PyObject * |
| 2874 | PyUnicode_FromObject(register PyObject *obj) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2875 | { |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2876 | /* XXX Perhaps we should make this API an alias of |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2877 | PyObject_Str() instead ?! */ |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2878 | if (PyUnicode_CheckExact(obj)) { |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 2879 | if (PyUnicode_READY(obj) == -1) |
Victor Stinner | d3a83d5 | 2011-10-01 03:09:33 +0200 | [diff] [blame] | 2880 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2881 | Py_INCREF(obj); |
| 2882 | return obj; |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2883 | } |
| 2884 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2885 | /* For a Unicode subtype that's not a Unicode object, |
| 2886 | return a true Unicode object with the same data. */ |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 2887 | return _PyUnicode_Copy(obj); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2888 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2889 | PyErr_Format(PyExc_TypeError, |
| 2890 | "Can't convert '%.100s' object to str implicitly", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2891 | Py_TYPE(obj)->tp_name); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2892 | return NULL; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2893 | } |
| 2894 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2895 | PyObject * |
| 2896 | PyUnicode_FromEncodedObject(register PyObject *obj, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2897 | const char *encoding, |
| 2898 | const char *errors) |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2899 | { |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2900 | Py_buffer buffer; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2901 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2902 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2903 | if (obj == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2904 | PyErr_BadInternalCall(); |
| 2905 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2906 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2907 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2908 | /* Decoding bytes objects is the most common case and should be fast */ |
| 2909 | if (PyBytes_Check(obj)) { |
| 2910 | if (PyBytes_GET_SIZE(obj) == 0) { |
| 2911 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 2912 | v = unicode_empty; |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2913 | } |
| 2914 | else { |
| 2915 | v = PyUnicode_Decode( |
| 2916 | PyBytes_AS_STRING(obj), PyBytes_GET_SIZE(obj), |
| 2917 | encoding, errors); |
| 2918 | } |
| 2919 | return v; |
| 2920 | } |
| 2921 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2922 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2923 | PyErr_SetString(PyExc_TypeError, |
| 2924 | "decoding str is not supported"); |
| 2925 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2926 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2927 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2928 | /* Retrieve a bytes buffer view through the PEP 3118 buffer interface */ |
| 2929 | if (PyObject_GetBuffer(obj, &buffer, PyBUF_SIMPLE) < 0) { |
| 2930 | PyErr_Format(PyExc_TypeError, |
| 2931 | "coercing to str: need bytes, bytearray " |
| 2932 | "or buffer-like object, %.80s found", |
| 2933 | Py_TYPE(obj)->tp_name); |
| 2934 | return NULL; |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 2935 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2936 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2937 | if (buffer.len == 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2938 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 2939 | v = unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2940 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2941 | else |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2942 | v = PyUnicode_Decode((char*) buffer.buf, buffer.len, encoding, errors); |
Marc-André Lemburg | ad7c98e | 2001-01-17 17:09:53 +0000 | [diff] [blame] | 2943 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2944 | PyBuffer_Release(&buffer); |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2945 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2946 | } |
| 2947 | |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2948 | /* Convert encoding to lower case and replace '_' with '-' in order to |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2949 | catch e.g. UTF_8. Return 0 on error (encoding is longer than lower_len-1), |
| 2950 | 1 on success. */ |
| 2951 | static int |
| 2952 | normalize_encoding(const char *encoding, |
| 2953 | char *lower, |
| 2954 | size_t lower_len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2955 | { |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2956 | const char *e; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2957 | char *l; |
| 2958 | char *l_end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2959 | |
Benjamin Peterson | 7a6debe | 2011-10-15 09:25:28 -0400 | [diff] [blame] | 2960 | if (encoding == NULL) { |
| 2961 | strcpy(lower, "utf-8"); |
| 2962 | return 1; |
| 2963 | } |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2964 | e = encoding; |
| 2965 | l = lower; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2966 | l_end = &lower[lower_len - 1]; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2967 | while (*e) { |
| 2968 | if (l == l_end) |
| 2969 | return 0; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 2970 | if (Py_ISUPPER(*e)) { |
| 2971 | *l++ = Py_TOLOWER(*e++); |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2972 | } |
| 2973 | else if (*e == '_') { |
| 2974 | *l++ = '-'; |
| 2975 | e++; |
| 2976 | } |
| 2977 | else { |
| 2978 | *l++ = *e++; |
| 2979 | } |
| 2980 | } |
| 2981 | *l = '\0'; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2982 | return 1; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2983 | } |
| 2984 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2985 | PyObject * |
| 2986 | PyUnicode_Decode(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2987 | Py_ssize_t size, |
| 2988 | const char *encoding, |
| 2989 | const char *errors) |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2990 | { |
| 2991 | PyObject *buffer = NULL, *unicode; |
| 2992 | Py_buffer info; |
| 2993 | char lower[11]; /* Enough for any encoding shortcut */ |
| 2994 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 2995 | /* Shortcuts for common default encodings */ |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2996 | if (normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2997 | if ((strcmp(lower, "utf-8") == 0) || |
| 2998 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 2999 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3000 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3001 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3002 | (strcmp(lower, "iso-8859-1") == 0)) |
| 3003 | return PyUnicode_DecodeLatin1(s, size, errors); |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3004 | #ifdef HAVE_MBCS |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3005 | else if (strcmp(lower, "mbcs") == 0) |
| 3006 | return PyUnicode_DecodeMBCS(s, size, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 3007 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3008 | else if (strcmp(lower, "ascii") == 0) |
| 3009 | return PyUnicode_DecodeASCII(s, size, errors); |
| 3010 | else if (strcmp(lower, "utf-16") == 0) |
| 3011 | return PyUnicode_DecodeUTF16(s, size, errors, 0); |
| 3012 | else if (strcmp(lower, "utf-32") == 0) |
| 3013 | return PyUnicode_DecodeUTF32(s, size, errors, 0); |
| 3014 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3015 | |
| 3016 | /* Decode via the codec registry */ |
Guido van Rossum | be801ac | 2007-10-08 03:32:34 +0000 | [diff] [blame] | 3017 | buffer = NULL; |
Antoine Pitrou | c3b3924 | 2009-01-03 16:59:18 +0000 | [diff] [blame] | 3018 | 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] | 3019 | goto onError; |
Antoine Pitrou | ee58fa4 | 2008-08-19 18:22:14 +0000 | [diff] [blame] | 3020 | buffer = PyMemoryView_FromBuffer(&info); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3021 | if (buffer == NULL) |
| 3022 | goto onError; |
| 3023 | unicode = PyCodec_Decode(buffer, encoding, errors); |
| 3024 | if (unicode == NULL) |
| 3025 | goto onError; |
| 3026 | if (!PyUnicode_Check(unicode)) { |
| 3027 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3028 | "decoder did not return a str object (type=%.400s)", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 3029 | Py_TYPE(unicode)->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3030 | Py_DECREF(unicode); |
| 3031 | goto onError; |
| 3032 | } |
| 3033 | Py_DECREF(buffer); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 3034 | return unicode_result(unicode); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3035 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3036 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3037 | Py_XDECREF(buffer); |
| 3038 | return NULL; |
| 3039 | } |
| 3040 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3041 | PyObject * |
| 3042 | PyUnicode_AsDecodedObject(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3043 | const char *encoding, |
| 3044 | const char *errors) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3045 | { |
| 3046 | PyObject *v; |
| 3047 | |
| 3048 | if (!PyUnicode_Check(unicode)) { |
| 3049 | PyErr_BadArgument(); |
| 3050 | goto onError; |
| 3051 | } |
| 3052 | |
| 3053 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3054 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3055 | |
| 3056 | /* Decode via the codec registry */ |
| 3057 | v = PyCodec_Decode(unicode, encoding, errors); |
| 3058 | if (v == NULL) |
| 3059 | goto onError; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 3060 | return unicode_result(v); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3061 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3062 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3063 | return NULL; |
| 3064 | } |
| 3065 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3066 | PyObject * |
| 3067 | PyUnicode_AsDecodedUnicode(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3068 | const char *encoding, |
| 3069 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3070 | { |
| 3071 | PyObject *v; |
| 3072 | |
| 3073 | if (!PyUnicode_Check(unicode)) { |
| 3074 | PyErr_BadArgument(); |
| 3075 | goto onError; |
| 3076 | } |
| 3077 | |
| 3078 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3079 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3080 | |
| 3081 | /* Decode via the codec registry */ |
| 3082 | v = PyCodec_Decode(unicode, encoding, errors); |
| 3083 | if (v == NULL) |
| 3084 | goto onError; |
| 3085 | if (!PyUnicode_Check(v)) { |
| 3086 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3087 | "decoder did not return a str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3088 | Py_TYPE(v)->tp_name); |
| 3089 | Py_DECREF(v); |
| 3090 | goto onError; |
| 3091 | } |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 3092 | return unicode_result(v); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3093 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3094 | onError: |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3095 | return NULL; |
| 3096 | } |
| 3097 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3098 | PyObject * |
| 3099 | PyUnicode_Encode(const Py_UNICODE *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3100 | Py_ssize_t size, |
| 3101 | const char *encoding, |
| 3102 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3103 | { |
| 3104 | PyObject *v, *unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3105 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3106 | unicode = PyUnicode_FromUnicode(s, size); |
| 3107 | if (unicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3108 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3109 | v = PyUnicode_AsEncodedString(unicode, encoding, errors); |
| 3110 | Py_DECREF(unicode); |
| 3111 | return v; |
| 3112 | } |
| 3113 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3114 | PyObject * |
| 3115 | PyUnicode_AsEncodedObject(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3116 | const char *encoding, |
| 3117 | const char *errors) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3118 | { |
| 3119 | PyObject *v; |
| 3120 | |
| 3121 | if (!PyUnicode_Check(unicode)) { |
| 3122 | PyErr_BadArgument(); |
| 3123 | goto onError; |
| 3124 | } |
| 3125 | |
| 3126 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3127 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3128 | |
| 3129 | /* Encode via the codec registry */ |
| 3130 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3131 | if (v == NULL) |
| 3132 | goto onError; |
| 3133 | return v; |
| 3134 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3135 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3136 | return NULL; |
| 3137 | } |
| 3138 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3139 | static size_t |
| 3140 | wcstombs_errorpos(const wchar_t *wstr) |
| 3141 | { |
| 3142 | size_t len; |
| 3143 | #if SIZEOF_WCHAR_T == 2 |
| 3144 | wchar_t buf[3]; |
| 3145 | #else |
| 3146 | wchar_t buf[2]; |
| 3147 | #endif |
| 3148 | char outbuf[MB_LEN_MAX]; |
| 3149 | const wchar_t *start, *previous; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3150 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3151 | #if SIZEOF_WCHAR_T == 2 |
| 3152 | buf[2] = 0; |
| 3153 | #else |
| 3154 | buf[1] = 0; |
| 3155 | #endif |
| 3156 | start = wstr; |
| 3157 | while (*wstr != L'\0') |
| 3158 | { |
| 3159 | previous = wstr; |
| 3160 | #if SIZEOF_WCHAR_T == 2 |
| 3161 | if (Py_UNICODE_IS_HIGH_SURROGATE(wstr[0]) |
| 3162 | && Py_UNICODE_IS_LOW_SURROGATE(wstr[1])) |
| 3163 | { |
| 3164 | buf[0] = wstr[0]; |
| 3165 | buf[1] = wstr[1]; |
| 3166 | wstr += 2; |
| 3167 | } |
| 3168 | else { |
| 3169 | buf[0] = *wstr; |
| 3170 | buf[1] = 0; |
| 3171 | wstr++; |
| 3172 | } |
| 3173 | #else |
| 3174 | buf[0] = *wstr; |
| 3175 | wstr++; |
| 3176 | #endif |
| 3177 | len = wcstombs(outbuf, buf, sizeof(outbuf)); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3178 | if (len == (size_t)-1) |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3179 | return previous - start; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3180 | } |
| 3181 | |
| 3182 | /* failed to find the unencodable character */ |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3183 | return 0; |
| 3184 | } |
| 3185 | |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3186 | static int |
| 3187 | locale_error_handler(const char *errors, int *surrogateescape) |
| 3188 | { |
| 3189 | if (errors == NULL) { |
| 3190 | *surrogateescape = 0; |
| 3191 | return 0; |
| 3192 | } |
| 3193 | |
| 3194 | if (strcmp(errors, "strict") == 0) { |
| 3195 | *surrogateescape = 0; |
| 3196 | return 0; |
| 3197 | } |
| 3198 | if (strcmp(errors, "surrogateescape") == 0) { |
| 3199 | *surrogateescape = 1; |
| 3200 | return 0; |
| 3201 | } |
| 3202 | PyErr_Format(PyExc_ValueError, |
| 3203 | "only 'strict' and 'surrogateescape' error handlers " |
| 3204 | "are supported, not '%s'", |
| 3205 | errors); |
| 3206 | return -1; |
| 3207 | } |
| 3208 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3209 | PyObject * |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3210 | PyUnicode_EncodeLocale(PyObject *unicode, const char *errors) |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3211 | { |
| 3212 | Py_ssize_t wlen, wlen2; |
| 3213 | wchar_t *wstr; |
| 3214 | PyObject *bytes = NULL; |
| 3215 | char *errmsg; |
Victor Stinner | 1f33f2b | 2011-12-17 04:45:09 +0100 | [diff] [blame] | 3216 | PyObject *reason; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3217 | PyObject *exc; |
| 3218 | size_t error_pos; |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3219 | int surrogateescape; |
| 3220 | |
| 3221 | if (locale_error_handler(errors, &surrogateescape) < 0) |
| 3222 | return NULL; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3223 | |
| 3224 | wstr = PyUnicode_AsWideCharString(unicode, &wlen); |
| 3225 | if (wstr == NULL) |
| 3226 | return NULL; |
| 3227 | |
| 3228 | wlen2 = wcslen(wstr); |
| 3229 | if (wlen2 != wlen) { |
| 3230 | PyMem_Free(wstr); |
| 3231 | PyErr_SetString(PyExc_TypeError, "embedded null character"); |
| 3232 | return NULL; |
| 3233 | } |
| 3234 | |
| 3235 | if (surrogateescape) { |
| 3236 | /* locale encoding with surrogateescape */ |
| 3237 | char *str; |
| 3238 | |
| 3239 | str = _Py_wchar2char(wstr, &error_pos); |
| 3240 | if (str == NULL) { |
| 3241 | if (error_pos == (size_t)-1) { |
| 3242 | PyErr_NoMemory(); |
| 3243 | PyMem_Free(wstr); |
| 3244 | return NULL; |
| 3245 | } |
| 3246 | else { |
| 3247 | goto encode_error; |
| 3248 | } |
| 3249 | } |
| 3250 | PyMem_Free(wstr); |
| 3251 | |
| 3252 | bytes = PyBytes_FromString(str); |
| 3253 | PyMem_Free(str); |
| 3254 | } |
| 3255 | else { |
| 3256 | size_t len, len2; |
| 3257 | |
| 3258 | len = wcstombs(NULL, wstr, 0); |
| 3259 | if (len == (size_t)-1) { |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3260 | error_pos = (size_t)-1; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3261 | goto encode_error; |
| 3262 | } |
| 3263 | |
| 3264 | bytes = PyBytes_FromStringAndSize(NULL, len); |
| 3265 | if (bytes == NULL) { |
| 3266 | PyMem_Free(wstr); |
| 3267 | return NULL; |
| 3268 | } |
| 3269 | |
| 3270 | len2 = wcstombs(PyBytes_AS_STRING(bytes), wstr, len+1); |
| 3271 | if (len2 == (size_t)-1 || len2 > len) { |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3272 | error_pos = (size_t)-1; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3273 | goto encode_error; |
| 3274 | } |
| 3275 | PyMem_Free(wstr); |
| 3276 | } |
| 3277 | return bytes; |
| 3278 | |
| 3279 | encode_error: |
| 3280 | errmsg = strerror(errno); |
| 3281 | assert(errmsg != NULL); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3282 | |
| 3283 | if (error_pos == (size_t)-1) |
| 3284 | error_pos = wcstombs_errorpos(wstr); |
| 3285 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3286 | PyMem_Free(wstr); |
| 3287 | Py_XDECREF(bytes); |
| 3288 | |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3289 | if (errmsg != NULL) { |
| 3290 | size_t errlen; |
| 3291 | wstr = _Py_char2wchar(errmsg, &errlen); |
| 3292 | if (wstr != NULL) { |
| 3293 | reason = PyUnicode_FromWideChar(wstr, errlen); |
| 3294 | PyMem_Free(wstr); |
| 3295 | } else |
| 3296 | errmsg = NULL; |
| 3297 | } |
| 3298 | if (errmsg == NULL) |
Victor Stinner | 1f33f2b | 2011-12-17 04:45:09 +0100 | [diff] [blame] | 3299 | reason = PyUnicode_FromString( |
| 3300 | "wcstombs() encountered an unencodable " |
| 3301 | "wide character"); |
| 3302 | if (reason == NULL) |
| 3303 | return NULL; |
| 3304 | |
| 3305 | exc = PyObject_CallFunction(PyExc_UnicodeEncodeError, "sOnnO", |
| 3306 | "locale", unicode, |
| 3307 | (Py_ssize_t)error_pos, |
| 3308 | (Py_ssize_t)(error_pos+1), |
| 3309 | reason); |
| 3310 | Py_DECREF(reason); |
| 3311 | if (exc != NULL) { |
| 3312 | PyCodec_StrictErrors(exc); |
| 3313 | Py_XDECREF(exc); |
| 3314 | } |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3315 | return NULL; |
| 3316 | } |
| 3317 | |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3318 | PyObject * |
| 3319 | PyUnicode_EncodeFSDefault(PyObject *unicode) |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3320 | { |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3321 | #ifdef HAVE_MBCS |
Victor Stinner | ac931b1 | 2011-11-20 18:27:03 +0100 | [diff] [blame] | 3322 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, NULL); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3323 | #elif defined(__APPLE__) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3324 | return _PyUnicode_AsUTF8String(unicode, "surrogateescape"); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3325 | #else |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 3326 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 3327 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 3328 | cannot use it to encode and decode filenames before it is loaded. Load |
| 3329 | the Python codec requires to encode at least its own filename. Use the C |
| 3330 | version of the locale codec until the codec registry is initialized and |
| 3331 | the Python codec is loaded. |
| 3332 | |
| 3333 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 3334 | cannot only rely on it: check also interp->fscodec_initialized for |
| 3335 | subinterpreters. */ |
| 3336 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3337 | return PyUnicode_AsEncodedString(unicode, |
| 3338 | Py_FileSystemDefaultEncoding, |
| 3339 | "surrogateescape"); |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 3340 | } |
| 3341 | else { |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3342 | return PyUnicode_EncodeLocale(unicode, "surrogateescape"); |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 3343 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3344 | #endif |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3345 | } |
| 3346 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3347 | PyObject * |
| 3348 | PyUnicode_AsEncodedString(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3349 | const char *encoding, |
| 3350 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3351 | { |
| 3352 | PyObject *v; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3353 | char lower[11]; /* Enough for any encoding shortcut */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3354 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3355 | if (!PyUnicode_Check(unicode)) { |
| 3356 | PyErr_BadArgument(); |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3357 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3358 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3359 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3360 | /* Shortcuts for common default encodings */ |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3361 | if (normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3362 | if ((strcmp(lower, "utf-8") == 0) || |
| 3363 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 3364 | { |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3365 | if (errors == NULL || strcmp(errors, "strict") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3366 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3367 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3368 | return _PyUnicode_AsUTF8String(unicode, errors); |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 3369 | } |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3370 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3371 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3372 | (strcmp(lower, "iso-8859-1") == 0)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3373 | return _PyUnicode_AsLatin1String(unicode, errors); |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3374 | #ifdef HAVE_MBCS |
Victor Stinner | ac931b1 | 2011-11-20 18:27:03 +0100 | [diff] [blame] | 3375 | else if (strcmp(lower, "mbcs") == 0) |
| 3376 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 3377 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3378 | else if (strcmp(lower, "ascii") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3379 | return _PyUnicode_AsASCIIString(unicode, errors); |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3380 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3381 | |
| 3382 | /* Encode via the codec registry */ |
| 3383 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3384 | if (v == NULL) |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3385 | return NULL; |
| 3386 | |
| 3387 | /* The normal path */ |
| 3388 | if (PyBytes_Check(v)) |
| 3389 | return v; |
| 3390 | |
| 3391 | /* 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] | 3392 | if (PyByteArray_Check(v)) { |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 3393 | int error; |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3394 | PyObject *b; |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 3395 | |
| 3396 | error = PyErr_WarnFormat(PyExc_RuntimeWarning, 1, |
| 3397 | "encoder %s returned bytearray instead of bytes", |
| 3398 | encoding); |
| 3399 | if (error) { |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3400 | Py_DECREF(v); |
| 3401 | return NULL; |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3402 | } |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3403 | |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3404 | b = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(v), Py_SIZE(v)); |
| 3405 | Py_DECREF(v); |
| 3406 | return b; |
| 3407 | } |
| 3408 | |
| 3409 | PyErr_Format(PyExc_TypeError, |
| 3410 | "encoder did not return a bytes object (type=%.400s)", |
| 3411 | Py_TYPE(v)->tp_name); |
| 3412 | Py_DECREF(v); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3413 | return NULL; |
| 3414 | } |
| 3415 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3416 | PyObject * |
| 3417 | PyUnicode_AsEncodedUnicode(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3418 | const char *encoding, |
| 3419 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3420 | { |
| 3421 | PyObject *v; |
| 3422 | |
| 3423 | if (!PyUnicode_Check(unicode)) { |
| 3424 | PyErr_BadArgument(); |
| 3425 | goto onError; |
| 3426 | } |
| 3427 | |
| 3428 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3429 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3430 | |
| 3431 | /* Encode via the codec registry */ |
| 3432 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3433 | if (v == NULL) |
| 3434 | goto onError; |
| 3435 | if (!PyUnicode_Check(v)) { |
| 3436 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3437 | "encoder did not return an str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3438 | Py_TYPE(v)->tp_name); |
| 3439 | Py_DECREF(v); |
| 3440 | goto onError; |
| 3441 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3442 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3443 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3444 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3445 | return NULL; |
| 3446 | } |
| 3447 | |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3448 | static size_t |
| 3449 | mbstowcs_errorpos(const char *str, size_t len) |
| 3450 | { |
| 3451 | #ifdef HAVE_MBRTOWC |
| 3452 | const char *start = str; |
| 3453 | mbstate_t mbs; |
| 3454 | size_t converted; |
| 3455 | wchar_t ch; |
| 3456 | |
| 3457 | memset(&mbs, 0, sizeof mbs); |
| 3458 | while (len) |
| 3459 | { |
| 3460 | converted = mbrtowc(&ch, (char*)str, len, &mbs); |
| 3461 | if (converted == 0) |
| 3462 | /* Reached end of string */ |
| 3463 | break; |
| 3464 | if (converted == (size_t)-1 || converted == (size_t)-2) { |
| 3465 | /* Conversion error or incomplete character */ |
| 3466 | return str - start; |
| 3467 | } |
| 3468 | else { |
| 3469 | str += converted; |
| 3470 | len -= converted; |
| 3471 | } |
| 3472 | } |
| 3473 | /* failed to find the undecodable byte sequence */ |
| 3474 | return 0; |
| 3475 | #endif |
| 3476 | return 0; |
| 3477 | } |
| 3478 | |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3479 | PyObject* |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3480 | PyUnicode_DecodeLocaleAndSize(const char *str, Py_ssize_t len, |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3481 | const char *errors) |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3482 | { |
| 3483 | wchar_t smallbuf[256]; |
| 3484 | size_t smallbuf_len = Py_ARRAY_LENGTH(smallbuf); |
| 3485 | wchar_t *wstr; |
| 3486 | size_t wlen, wlen2; |
| 3487 | PyObject *unicode; |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3488 | int surrogateescape; |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3489 | size_t error_pos; |
| 3490 | char *errmsg; |
| 3491 | PyObject *reason, *exc; |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3492 | |
| 3493 | if (locale_error_handler(errors, &surrogateescape) < 0) |
| 3494 | return NULL; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3495 | |
| 3496 | if (str[len] != '\0' || len != strlen(str)) { |
| 3497 | PyErr_SetString(PyExc_TypeError, "embedded null character"); |
| 3498 | return NULL; |
| 3499 | } |
| 3500 | |
| 3501 | if (surrogateescape) |
| 3502 | { |
| 3503 | wstr = _Py_char2wchar(str, &wlen); |
| 3504 | if (wstr == NULL) { |
| 3505 | if (wlen == (size_t)-1) |
| 3506 | PyErr_NoMemory(); |
| 3507 | else |
| 3508 | PyErr_SetFromErrno(PyExc_OSError); |
| 3509 | return NULL; |
| 3510 | } |
| 3511 | |
| 3512 | unicode = PyUnicode_FromWideChar(wstr, wlen); |
| 3513 | PyMem_Free(wstr); |
| 3514 | } |
| 3515 | else { |
| 3516 | #ifndef HAVE_BROKEN_MBSTOWCS |
| 3517 | wlen = mbstowcs(NULL, str, 0); |
| 3518 | #else |
| 3519 | wlen = len; |
| 3520 | #endif |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3521 | if (wlen == (size_t)-1) |
| 3522 | goto decode_error; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3523 | if (wlen+1 <= smallbuf_len) { |
| 3524 | wstr = smallbuf; |
| 3525 | } |
| 3526 | else { |
| 3527 | if (wlen > PY_SSIZE_T_MAX / sizeof(wchar_t) - 1) |
| 3528 | return PyErr_NoMemory(); |
| 3529 | |
| 3530 | wstr = PyMem_Malloc((wlen+1) * sizeof(wchar_t)); |
| 3531 | if (!wstr) |
| 3532 | return PyErr_NoMemory(); |
| 3533 | } |
| 3534 | |
| 3535 | /* This shouldn't fail now */ |
| 3536 | wlen2 = mbstowcs(wstr, str, wlen+1); |
| 3537 | if (wlen2 == (size_t)-1) { |
| 3538 | if (wstr != smallbuf) |
| 3539 | PyMem_Free(wstr); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3540 | goto decode_error; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3541 | } |
| 3542 | #ifdef HAVE_BROKEN_MBSTOWCS |
| 3543 | assert(wlen2 == wlen); |
| 3544 | #endif |
| 3545 | unicode = PyUnicode_FromWideChar(wstr, wlen2); |
| 3546 | if (wstr != smallbuf) |
| 3547 | PyMem_Free(wstr); |
| 3548 | } |
| 3549 | return unicode; |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3550 | |
| 3551 | decode_error: |
| 3552 | errmsg = strerror(errno); |
| 3553 | assert(errmsg != NULL); |
| 3554 | |
| 3555 | error_pos = mbstowcs_errorpos(str, len); |
| 3556 | if (errmsg != NULL) { |
| 3557 | size_t errlen; |
| 3558 | wstr = _Py_char2wchar(errmsg, &errlen); |
| 3559 | if (wstr != NULL) { |
| 3560 | reason = PyUnicode_FromWideChar(wstr, errlen); |
| 3561 | PyMem_Free(wstr); |
| 3562 | } else |
| 3563 | errmsg = NULL; |
| 3564 | } |
| 3565 | if (errmsg == NULL) |
| 3566 | reason = PyUnicode_FromString( |
| 3567 | "mbstowcs() encountered an invalid multibyte sequence"); |
| 3568 | if (reason == NULL) |
| 3569 | return NULL; |
| 3570 | |
| 3571 | exc = PyObject_CallFunction(PyExc_UnicodeDecodeError, "sy#nnO", |
| 3572 | "locale", str, len, |
| 3573 | (Py_ssize_t)error_pos, |
| 3574 | (Py_ssize_t)(error_pos+1), |
| 3575 | reason); |
| 3576 | Py_DECREF(reason); |
| 3577 | if (exc != NULL) { |
| 3578 | PyCodec_StrictErrors(exc); |
| 3579 | Py_XDECREF(exc); |
| 3580 | } |
| 3581 | return NULL; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3582 | } |
| 3583 | |
| 3584 | PyObject* |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3585 | PyUnicode_DecodeLocale(const char *str, const char *errors) |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3586 | { |
| 3587 | Py_ssize_t size = (Py_ssize_t)strlen(str); |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3588 | return PyUnicode_DecodeLocaleAndSize(str, size, errors); |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3589 | } |
| 3590 | |
| 3591 | |
| 3592 | PyObject* |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3593 | PyUnicode_DecodeFSDefault(const char *s) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3594 | Py_ssize_t size = (Py_ssize_t)strlen(s); |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3595 | return PyUnicode_DecodeFSDefaultAndSize(s, size); |
| 3596 | } |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3597 | |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3598 | PyObject* |
| 3599 | PyUnicode_DecodeFSDefaultAndSize(const char *s, Py_ssize_t size) |
| 3600 | { |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3601 | #ifdef HAVE_MBCS |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3602 | return PyUnicode_DecodeMBCS(s, size, NULL); |
| 3603 | #elif defined(__APPLE__) |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 3604 | return PyUnicode_DecodeUTF8Stateful(s, size, "surrogateescape", NULL); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3605 | #else |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 3606 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 3607 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 3608 | cannot use it to encode and decode filenames before it is loaded. Load |
| 3609 | the Python codec requires to encode at least its own filename. Use the C |
| 3610 | version of the locale codec until the codec registry is initialized and |
| 3611 | the Python codec is loaded. |
| 3612 | |
| 3613 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 3614 | cannot only rely on it: check also interp->fscodec_initialized for |
| 3615 | subinterpreters. */ |
| 3616 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3617 | return PyUnicode_Decode(s, size, |
| 3618 | Py_FileSystemDefaultEncoding, |
Victor Stinner | b9a20ad | 2010-04-30 16:37:52 +0000 | [diff] [blame] | 3619 | "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3620 | } |
| 3621 | else { |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3622 | return PyUnicode_DecodeLocaleAndSize(s, size, "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3623 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3624 | #endif |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3625 | } |
| 3626 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3627 | |
| 3628 | int |
Antoine Pitrou | 1334884 | 2012-01-29 18:36:34 +0100 | [diff] [blame] | 3629 | _PyUnicode_HasNULChars(PyObject* s) |
| 3630 | { |
| 3631 | static PyObject *nul = NULL; |
| 3632 | |
| 3633 | if (nul == NULL) |
| 3634 | nul = PyUnicode_FromStringAndSize("\0", 1); |
| 3635 | if (nul == NULL) |
| 3636 | return -1; |
| 3637 | return PyUnicode_Contains(s, nul); |
| 3638 | } |
| 3639 | |
| 3640 | |
| 3641 | int |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3642 | PyUnicode_FSConverter(PyObject* arg, void* addr) |
| 3643 | { |
| 3644 | PyObject *output = NULL; |
| 3645 | Py_ssize_t size; |
| 3646 | void *data; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3647 | if (arg == NULL) { |
| 3648 | Py_DECREF(*(PyObject**)addr); |
| 3649 | return 1; |
| 3650 | } |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 3651 | if (PyBytes_Check(arg)) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3652 | output = arg; |
| 3653 | Py_INCREF(output); |
| 3654 | } |
| 3655 | else { |
| 3656 | arg = PyUnicode_FromObject(arg); |
| 3657 | if (!arg) |
| 3658 | return 0; |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3659 | output = PyUnicode_EncodeFSDefault(arg); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3660 | Py_DECREF(arg); |
| 3661 | if (!output) |
| 3662 | return 0; |
| 3663 | if (!PyBytes_Check(output)) { |
| 3664 | Py_DECREF(output); |
| 3665 | PyErr_SetString(PyExc_TypeError, "encoder failed to return bytes"); |
| 3666 | return 0; |
| 3667 | } |
| 3668 | } |
Victor Stinner | 0ea2a46 | 2010-04-30 00:22:08 +0000 | [diff] [blame] | 3669 | size = PyBytes_GET_SIZE(output); |
| 3670 | data = PyBytes_AS_STRING(output); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3671 | if (size != strlen(data)) { |
Benjamin Peterson | 7a6b44a | 2011-08-18 13:51:47 -0500 | [diff] [blame] | 3672 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3673 | Py_DECREF(output); |
| 3674 | return 0; |
| 3675 | } |
| 3676 | *(PyObject**)addr = output; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3677 | return Py_CLEANUP_SUPPORTED; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3678 | } |
| 3679 | |
| 3680 | |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3681 | int |
| 3682 | PyUnicode_FSDecoder(PyObject* arg, void* addr) |
| 3683 | { |
| 3684 | PyObject *output = NULL; |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3685 | if (arg == NULL) { |
| 3686 | Py_DECREF(*(PyObject**)addr); |
| 3687 | return 1; |
| 3688 | } |
| 3689 | if (PyUnicode_Check(arg)) { |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 3690 | if (PyUnicode_READY(arg) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3691 | return 0; |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3692 | output = arg; |
| 3693 | Py_INCREF(output); |
| 3694 | } |
| 3695 | else { |
| 3696 | arg = PyBytes_FromObject(arg); |
| 3697 | if (!arg) |
| 3698 | return 0; |
| 3699 | output = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AS_STRING(arg), |
| 3700 | PyBytes_GET_SIZE(arg)); |
| 3701 | Py_DECREF(arg); |
| 3702 | if (!output) |
| 3703 | return 0; |
| 3704 | if (!PyUnicode_Check(output)) { |
| 3705 | Py_DECREF(output); |
| 3706 | PyErr_SetString(PyExc_TypeError, "decoder failed to return unicode"); |
| 3707 | return 0; |
| 3708 | } |
| 3709 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 3710 | if (PyUnicode_READY(output) == -1) { |
Victor Stinner | 065836e | 2011-10-27 01:56:33 +0200 | [diff] [blame] | 3711 | Py_DECREF(output); |
| 3712 | return 0; |
| 3713 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3714 | if (findchar(PyUnicode_DATA(output), PyUnicode_KIND(output), |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 3715 | PyUnicode_GET_LENGTH(output), 0, 1) >= 0) { |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3716 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
| 3717 | Py_DECREF(output); |
| 3718 | return 0; |
| 3719 | } |
| 3720 | *(PyObject**)addr = output; |
| 3721 | return Py_CLEANUP_SUPPORTED; |
| 3722 | } |
| 3723 | |
| 3724 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3725 | char* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3726 | PyUnicode_AsUTF8AndSize(PyObject *unicode, Py_ssize_t *psize) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3727 | { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 3728 | PyObject *bytes; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3729 | |
Neal Norwitz | e0a0a6e | 2007-08-25 01:04:21 +0000 | [diff] [blame] | 3730 | if (!PyUnicode_Check(unicode)) { |
| 3731 | PyErr_BadArgument(); |
| 3732 | return NULL; |
| 3733 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3734 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3735 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3736 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3737 | if (PyUnicode_UTF8(unicode) == NULL) { |
| 3738 | assert(!PyUnicode_IS_COMPACT_ASCII(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3739 | bytes = _PyUnicode_AsUTF8String(unicode, "strict"); |
| 3740 | if (bytes == NULL) |
| 3741 | return NULL; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3742 | _PyUnicode_UTF8(unicode) = PyObject_MALLOC(PyBytes_GET_SIZE(bytes) + 1); |
| 3743 | if (_PyUnicode_UTF8(unicode) == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3744 | Py_DECREF(bytes); |
| 3745 | return NULL; |
| 3746 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3747 | _PyUnicode_UTF8_LENGTH(unicode) = PyBytes_GET_SIZE(bytes); |
| 3748 | Py_MEMCPY(_PyUnicode_UTF8(unicode), |
| 3749 | PyBytes_AS_STRING(bytes), |
| 3750 | _PyUnicode_UTF8_LENGTH(unicode) + 1); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3751 | Py_DECREF(bytes); |
| 3752 | } |
| 3753 | |
| 3754 | if (psize) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3755 | *psize = PyUnicode_UTF8_LENGTH(unicode); |
| 3756 | return PyUnicode_UTF8(unicode); |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 3757 | } |
| 3758 | |
| 3759 | char* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3760 | PyUnicode_AsUTF8(PyObject *unicode) |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 3761 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3762 | return PyUnicode_AsUTF8AndSize(unicode, NULL); |
| 3763 | } |
| 3764 | |
| 3765 | #ifdef Py_DEBUG |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 3766 | static int unicode_as_unicode_calls = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3767 | #endif |
| 3768 | |
| 3769 | |
| 3770 | Py_UNICODE * |
| 3771 | PyUnicode_AsUnicodeAndSize(PyObject *unicode, Py_ssize_t *size) |
| 3772 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3773 | const unsigned char *one_byte; |
| 3774 | #if SIZEOF_WCHAR_T == 4 |
| 3775 | const Py_UCS2 *two_bytes; |
| 3776 | #else |
| 3777 | const Py_UCS4 *four_bytes; |
| 3778 | const Py_UCS4 *ucs4_end; |
| 3779 | Py_ssize_t num_surrogates; |
| 3780 | #endif |
| 3781 | wchar_t *w; |
| 3782 | wchar_t *wchar_end; |
| 3783 | |
| 3784 | if (!PyUnicode_Check(unicode)) { |
| 3785 | PyErr_BadArgument(); |
| 3786 | return NULL; |
| 3787 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3788 | if (_PyUnicode_WSTR(unicode) == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3789 | /* Non-ASCII compact unicode object */ |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3790 | assert(_PyUnicode_KIND(unicode) != 0); |
| 3791 | assert(PyUnicode_IS_READY(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3792 | |
| 3793 | #ifdef Py_DEBUG |
| 3794 | ++unicode_as_unicode_calls; |
| 3795 | #endif |
| 3796 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3797 | if (PyUnicode_KIND(unicode) == PyUnicode_4BYTE_KIND) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3798 | #if SIZEOF_WCHAR_T == 2 |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3799 | four_bytes = PyUnicode_4BYTE_DATA(unicode); |
| 3800 | ucs4_end = four_bytes + _PyUnicode_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3801 | num_surrogates = 0; |
| 3802 | |
| 3803 | for (; four_bytes < ucs4_end; ++four_bytes) { |
| 3804 | if (*four_bytes > 0xFFFF) |
| 3805 | ++num_surrogates; |
| 3806 | } |
| 3807 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3808 | _PyUnicode_WSTR(unicode) = (wchar_t *) PyObject_MALLOC( |
| 3809 | sizeof(wchar_t) * (_PyUnicode_LENGTH(unicode) + 1 + num_surrogates)); |
| 3810 | if (!_PyUnicode_WSTR(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3811 | PyErr_NoMemory(); |
| 3812 | return NULL; |
| 3813 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3814 | _PyUnicode_WSTR_LENGTH(unicode) = _PyUnicode_LENGTH(unicode) + num_surrogates; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3815 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3816 | w = _PyUnicode_WSTR(unicode); |
| 3817 | wchar_end = w + _PyUnicode_WSTR_LENGTH(unicode); |
| 3818 | four_bytes = PyUnicode_4BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3819 | for (; four_bytes < ucs4_end; ++four_bytes, ++w) { |
| 3820 | if (*four_bytes > 0xFFFF) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 3821 | assert(*four_bytes <= MAX_UNICODE); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3822 | /* encode surrogate pair in this case */ |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 3823 | *w++ = Py_UNICODE_HIGH_SURROGATE(*four_bytes); |
| 3824 | *w = Py_UNICODE_LOW_SURROGATE(*four_bytes); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3825 | } |
| 3826 | else |
| 3827 | *w = *four_bytes; |
| 3828 | |
| 3829 | if (w > wchar_end) { |
| 3830 | assert(0 && "Miscalculated string end"); |
| 3831 | } |
| 3832 | } |
| 3833 | *w = 0; |
| 3834 | #else |
| 3835 | /* sizeof(wchar_t) == 4 */ |
| 3836 | Py_FatalError("Impossible unicode object state, wstr and str " |
| 3837 | "should share memory already."); |
| 3838 | return NULL; |
| 3839 | #endif |
| 3840 | } |
| 3841 | else { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3842 | _PyUnicode_WSTR(unicode) = (wchar_t *) PyObject_MALLOC(sizeof(wchar_t) * |
| 3843 | (_PyUnicode_LENGTH(unicode) + 1)); |
| 3844 | if (!_PyUnicode_WSTR(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3845 | PyErr_NoMemory(); |
| 3846 | return NULL; |
| 3847 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3848 | if (!PyUnicode_IS_COMPACT_ASCII(unicode)) |
| 3849 | _PyUnicode_WSTR_LENGTH(unicode) = _PyUnicode_LENGTH(unicode); |
| 3850 | w = _PyUnicode_WSTR(unicode); |
| 3851 | wchar_end = w + _PyUnicode_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3852 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3853 | if (PyUnicode_KIND(unicode) == PyUnicode_1BYTE_KIND) { |
| 3854 | one_byte = PyUnicode_1BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3855 | for (; w < wchar_end; ++one_byte, ++w) |
| 3856 | *w = *one_byte; |
| 3857 | /* null-terminate the wstr */ |
| 3858 | *w = 0; |
| 3859 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3860 | else if (PyUnicode_KIND(unicode) == PyUnicode_2BYTE_KIND) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3861 | #if SIZEOF_WCHAR_T == 4 |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3862 | two_bytes = PyUnicode_2BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3863 | for (; w < wchar_end; ++two_bytes, ++w) |
| 3864 | *w = *two_bytes; |
| 3865 | /* null-terminate the wstr */ |
| 3866 | *w = 0; |
| 3867 | #else |
| 3868 | /* sizeof(wchar_t) == 2 */ |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3869 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 3870 | _PyUnicode_WSTR(unicode) = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3871 | Py_FatalError("Impossible unicode object state, wstr " |
| 3872 | "and str should share memory already."); |
| 3873 | return NULL; |
| 3874 | #endif |
| 3875 | } |
| 3876 | else { |
| 3877 | assert(0 && "This should never happen."); |
| 3878 | } |
| 3879 | } |
| 3880 | } |
| 3881 | if (size != NULL) |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3882 | *size = PyUnicode_WSTR_LENGTH(unicode); |
| 3883 | return _PyUnicode_WSTR(unicode); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3884 | } |
| 3885 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3886 | Py_UNICODE * |
| 3887 | PyUnicode_AsUnicode(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3888 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3889 | return PyUnicode_AsUnicodeAndSize(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3890 | } |
| 3891 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3892 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3893 | Py_ssize_t |
| 3894 | PyUnicode_GetSize(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3895 | { |
| 3896 | if (!PyUnicode_Check(unicode)) { |
| 3897 | PyErr_BadArgument(); |
| 3898 | goto onError; |
| 3899 | } |
| 3900 | return PyUnicode_GET_SIZE(unicode); |
| 3901 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3902 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3903 | return -1; |
| 3904 | } |
| 3905 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3906 | Py_ssize_t |
| 3907 | PyUnicode_GetLength(PyObject *unicode) |
| 3908 | { |
Victor Stinner | 5a706cf | 2011-10-02 00:36:53 +0200 | [diff] [blame] | 3909 | if (!PyUnicode_Check(unicode) || PyUnicode_READY(unicode) == -1) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3910 | PyErr_BadArgument(); |
| 3911 | return -1; |
| 3912 | } |
| 3913 | |
| 3914 | return PyUnicode_GET_LENGTH(unicode); |
| 3915 | } |
| 3916 | |
| 3917 | Py_UCS4 |
| 3918 | PyUnicode_ReadChar(PyObject *unicode, Py_ssize_t index) |
| 3919 | { |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 3920 | if (!PyUnicode_Check(unicode) || PyUnicode_READY(unicode) == -1) { |
| 3921 | PyErr_BadArgument(); |
| 3922 | return (Py_UCS4)-1; |
| 3923 | } |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 3924 | if (index < 0 || index >= PyUnicode_GET_LENGTH(unicode)) { |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 3925 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3926 | return (Py_UCS4)-1; |
| 3927 | } |
| 3928 | return PyUnicode_READ_CHAR(unicode, index); |
| 3929 | } |
| 3930 | |
| 3931 | int |
| 3932 | PyUnicode_WriteChar(PyObject *unicode, Py_ssize_t index, Py_UCS4 ch) |
| 3933 | { |
| 3934 | if (!PyUnicode_Check(unicode) || !PyUnicode_IS_COMPACT(unicode)) { |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 3935 | PyErr_BadArgument(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3936 | return -1; |
| 3937 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 3938 | assert(PyUnicode_IS_READY(unicode)); |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 3939 | if (index < 0 || index >= PyUnicode_GET_LENGTH(unicode)) { |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 3940 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 3941 | return -1; |
| 3942 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 3943 | if (unicode_check_modifiable(unicode)) |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 3944 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3945 | PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode), |
| 3946 | index, ch); |
| 3947 | return 0; |
| 3948 | } |
| 3949 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3950 | const char * |
| 3951 | PyUnicode_GetDefaultEncoding(void) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3952 | { |
Victor Stinner | 42cb462 | 2010-09-01 19:39:01 +0000 | [diff] [blame] | 3953 | return "utf-8"; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3954 | } |
| 3955 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 3956 | /* create or adjust a UnicodeDecodeError */ |
| 3957 | static void |
| 3958 | make_decode_exception(PyObject **exceptionObject, |
| 3959 | const char *encoding, |
| 3960 | const char *input, Py_ssize_t length, |
| 3961 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 3962 | const char *reason) |
| 3963 | { |
| 3964 | if (*exceptionObject == NULL) { |
| 3965 | *exceptionObject = PyUnicodeDecodeError_Create( |
| 3966 | encoding, input, length, startpos, endpos, reason); |
| 3967 | } |
| 3968 | else { |
| 3969 | if (PyUnicodeDecodeError_SetStart(*exceptionObject, startpos)) |
| 3970 | goto onError; |
| 3971 | if (PyUnicodeDecodeError_SetEnd(*exceptionObject, endpos)) |
| 3972 | goto onError; |
| 3973 | if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) |
| 3974 | goto onError; |
| 3975 | } |
| 3976 | return; |
| 3977 | |
| 3978 | onError: |
| 3979 | Py_DECREF(*exceptionObject); |
| 3980 | *exceptionObject = NULL; |
| 3981 | } |
| 3982 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3983 | /* error handling callback helper: |
| 3984 | build arguments, call the callback and check the arguments, |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 3985 | if no exception occurred, copy the replacement to the output |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3986 | and adjust various state variables. |
| 3987 | return 0 on success, -1 on error |
| 3988 | */ |
| 3989 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3990 | static int |
| 3991 | unicode_decode_call_errorhandler(const char *errors, PyObject **errorHandler, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3992 | const char *encoding, const char *reason, |
| 3993 | const char **input, const char **inend, Py_ssize_t *startinpos, |
| 3994 | Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 3995 | PyObject **output, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3996 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3997 | 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] | 3998 | |
| 3999 | PyObject *restuple = NULL; |
| 4000 | PyObject *repunicode = NULL; |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 4001 | Py_ssize_t outsize; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4002 | Py_ssize_t insize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4003 | Py_ssize_t requiredsize; |
| 4004 | Py_ssize_t newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4005 | PyObject *inputobj = NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4006 | int res = -1; |
| 4007 | |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 4008 | if (_PyUnicode_KIND(*output) != PyUnicode_WCHAR_KIND) |
| 4009 | outsize = PyUnicode_GET_LENGTH(*output); |
| 4010 | else |
| 4011 | outsize = _PyUnicode_WSTR_LENGTH(*output); |
| 4012 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4013 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4014 | *errorHandler = PyCodec_LookupError(errors); |
| 4015 | if (*errorHandler == NULL) |
| 4016 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4017 | } |
| 4018 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4019 | make_decode_exception(exceptionObject, |
| 4020 | encoding, |
| 4021 | *input, *inend - *input, |
| 4022 | *startinpos, *endinpos, |
| 4023 | reason); |
| 4024 | if (*exceptionObject == NULL) |
| 4025 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4026 | |
| 4027 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 4028 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4029 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4030 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 4031 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4032 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4033 | } |
| 4034 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4035 | goto onError; |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 4036 | if (PyUnicode_READY(repunicode) == -1) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4037 | goto onError; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4038 | |
| 4039 | /* Copy back the bytes variables, which might have been modified by the |
| 4040 | callback */ |
| 4041 | inputobj = PyUnicodeDecodeError_GetObject(*exceptionObject); |
| 4042 | if (!inputobj) |
| 4043 | goto onError; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4044 | if (!PyBytes_Check(inputobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4045 | PyErr_Format(PyExc_TypeError, "exception attribute object must be bytes"); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4046 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4047 | *input = PyBytes_AS_STRING(inputobj); |
| 4048 | insize = PyBytes_GET_SIZE(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4049 | *inend = *input + insize; |
Walter Dörwald | 36f938f | 2007-08-10 10:11:43 +0000 | [diff] [blame] | 4050 | /* we can DECREF safely, as the exception has another reference, |
| 4051 | so the object won't go away. */ |
| 4052 | Py_DECREF(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4053 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4054 | if (newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4055 | newpos = insize+newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4056 | if (newpos<0 || newpos>insize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4057 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", newpos); |
| 4058 | goto onError; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4059 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4060 | |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 4061 | if (_PyUnicode_KIND(*output) != PyUnicode_WCHAR_KIND) { |
| 4062 | /* need more space? (at least enough for what we |
| 4063 | have+the replacement+the rest of the string (starting |
| 4064 | at the new input position), so we won't have to check space |
| 4065 | when there are no errors in the rest of the string) */ |
| 4066 | Py_ssize_t replen = PyUnicode_GET_LENGTH(repunicode); |
| 4067 | requiredsize = *outpos + replen + insize-newpos; |
| 4068 | if (requiredsize > outsize) { |
| 4069 | if (requiredsize<2*outsize) |
| 4070 | requiredsize = 2*outsize; |
| 4071 | if (unicode_resize(output, requiredsize) < 0) |
| 4072 | goto onError; |
| 4073 | } |
| 4074 | if (unicode_widen(output, PyUnicode_MAX_CHAR_VALUE(repunicode)) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4075 | goto onError; |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 4076 | copy_characters(*output, *outpos, repunicode, 0, replen); |
| 4077 | *outpos += replen; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4078 | } |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 4079 | else { |
| 4080 | wchar_t *repwstr; |
| 4081 | Py_ssize_t repwlen; |
| 4082 | repwstr = PyUnicode_AsUnicodeAndSize(repunicode, &repwlen); |
| 4083 | if (repwstr == NULL) |
| 4084 | goto onError; |
| 4085 | /* need more space? (at least enough for what we |
| 4086 | have+the replacement+the rest of the string (starting |
| 4087 | at the new input position), so we won't have to check space |
| 4088 | when there are no errors in the rest of the string) */ |
| 4089 | requiredsize = *outpos + repwlen + insize-newpos; |
| 4090 | if (requiredsize > outsize) { |
| 4091 | if (requiredsize < 2*outsize) |
| 4092 | requiredsize = 2*outsize; |
| 4093 | if (unicode_resize(output, requiredsize) < 0) |
| 4094 | goto onError; |
| 4095 | } |
| 4096 | wcsncpy(_PyUnicode_WSTR(*output) + *outpos, repwstr, repwlen); |
| 4097 | *outpos += repwlen; |
| 4098 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4099 | *endinpos = newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4100 | *inptr = *input + newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4101 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4102 | /* we made it! */ |
| 4103 | res = 0; |
| 4104 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4105 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4106 | Py_XDECREF(restuple); |
| 4107 | return res; |
| 4108 | } |
| 4109 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4110 | /* --- UTF-7 Codec -------------------------------------------------------- */ |
| 4111 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4112 | /* See RFC2152 for details. We encode conservatively and decode liberally. */ |
| 4113 | |
| 4114 | /* Three simple macros defining base-64. */ |
| 4115 | |
| 4116 | /* Is c a base-64 character? */ |
| 4117 | |
| 4118 | #define IS_BASE64(c) \ |
| 4119 | (((c) >= 'A' && (c) <= 'Z') || \ |
| 4120 | ((c) >= 'a' && (c) <= 'z') || \ |
| 4121 | ((c) >= '0' && (c) <= '9') || \ |
| 4122 | (c) == '+' || (c) == '/') |
| 4123 | |
| 4124 | /* given that c is a base-64 character, what is its base-64 value? */ |
| 4125 | |
| 4126 | #define FROM_BASE64(c) \ |
| 4127 | (((c) >= 'A' && (c) <= 'Z') ? (c) - 'A' : \ |
| 4128 | ((c) >= 'a' && (c) <= 'z') ? (c) - 'a' + 26 : \ |
| 4129 | ((c) >= '0' && (c) <= '9') ? (c) - '0' + 52 : \ |
| 4130 | (c) == '+' ? 62 : 63) |
| 4131 | |
| 4132 | /* What is the base-64 character of the bottom 6 bits of n? */ |
| 4133 | |
| 4134 | #define TO_BASE64(n) \ |
| 4135 | ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) |
| 4136 | |
| 4137 | /* DECODE_DIRECT: this byte encountered in a UTF-7 string should be |
| 4138 | * decoded as itself. We are permissive on decoding; the only ASCII |
| 4139 | * byte not decoding to itself is the + which begins a base64 |
| 4140 | * string. */ |
| 4141 | |
| 4142 | #define DECODE_DIRECT(c) \ |
| 4143 | ((c) <= 127 && (c) != '+') |
| 4144 | |
| 4145 | /* The UTF-7 encoder treats ASCII characters differently according to |
| 4146 | * whether they are Set D, Set O, Whitespace, or special (i.e. none of |
| 4147 | * the above). See RFC2152. This array identifies these different |
| 4148 | * sets: |
| 4149 | * 0 : "Set D" |
| 4150 | * alphanumeric and '(),-./:? |
| 4151 | * 1 : "Set O" |
| 4152 | * !"#$%&*;<=>@[]^_`{|} |
| 4153 | * 2 : "whitespace" |
| 4154 | * ht nl cr sp |
| 4155 | * 3 : special (must be base64 encoded) |
| 4156 | * everything else (i.e. +\~ and non-printing codes 0-8 11-12 14-31 127) |
| 4157 | */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4158 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4159 | static |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4160 | char utf7_category[128] = { |
| 4161 | /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */ |
| 4162 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 3, 3, 2, 3, 3, |
| 4163 | /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */ |
| 4164 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 4165 | /* sp ! " # $ % & ' ( ) * + , - . / */ |
| 4166 | 2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 3, 0, 0, 0, 0, |
| 4167 | /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */ |
| 4168 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, |
| 4169 | /* @ A B C D E F G H I J K L M N O */ |
| 4170 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4171 | /* P Q R S T U V W X Y Z [ \ ] ^ _ */ |
| 4172 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 1, 1, 1, |
| 4173 | /* ` a b c d e f g h i j k l m n o */ |
| 4174 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4175 | /* p q r s t u v w x y z { | } ~ del */ |
| 4176 | 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] | 4177 | }; |
| 4178 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4179 | /* ENCODE_DIRECT: this character should be encoded as itself. The |
| 4180 | * answer depends on whether we are encoding set O as itself, and also |
| 4181 | * on whether we are encoding whitespace as itself. RFC2152 makes it |
| 4182 | * clear that the answers to these questions vary between |
| 4183 | * applications, so this code needs to be flexible. */ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 4184 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4185 | #define ENCODE_DIRECT(c, directO, directWS) \ |
| 4186 | ((c) < 128 && (c) > 0 && \ |
| 4187 | ((utf7_category[(c)] == 0) || \ |
| 4188 | (directWS && (utf7_category[(c)] == 2)) || \ |
| 4189 | (directO && (utf7_category[(c)] == 1)))) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4190 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4191 | PyObject * |
| 4192 | PyUnicode_DecodeUTF7(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4193 | Py_ssize_t size, |
| 4194 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4195 | { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4196 | return PyUnicode_DecodeUTF7Stateful(s, size, errors, NULL); |
| 4197 | } |
| 4198 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4199 | /* The decoder. The only state we preserve is our read position, |
| 4200 | * i.e. how many characters we have consumed. So if we end in the |
| 4201 | * middle of a shift sequence we have to back off the read position |
| 4202 | * and the output to the beginning of the sequence, otherwise we lose |
| 4203 | * all the shift state (seen bits, number of bits seen, high |
| 4204 | * surrogate). */ |
| 4205 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4206 | PyObject * |
| 4207 | PyUnicode_DecodeUTF7Stateful(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4208 | Py_ssize_t size, |
| 4209 | const char *errors, |
| 4210 | Py_ssize_t *consumed) |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4211 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4212 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4213 | Py_ssize_t startinpos; |
| 4214 | Py_ssize_t endinpos; |
| 4215 | Py_ssize_t outpos; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4216 | const char *e; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4217 | PyObject *unicode; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4218 | const char *errmsg = ""; |
| 4219 | int inShift = 0; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4220 | Py_ssize_t shiftOutStart; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4221 | unsigned int base64bits = 0; |
| 4222 | unsigned long base64buffer = 0; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 4223 | Py_UCS4 surrogate = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4224 | PyObject *errorHandler = NULL; |
| 4225 | PyObject *exc = NULL; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4226 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4227 | /* Start off assuming it's all ASCII. Widen later as necessary. */ |
| 4228 | unicode = PyUnicode_New(size, 127); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4229 | if (!unicode) |
| 4230 | return NULL; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4231 | if (size == 0) { |
| 4232 | if (consumed) |
| 4233 | *consumed = 0; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4234 | return unicode; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4235 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4236 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4237 | shiftOutStart = outpos = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4238 | e = s + size; |
| 4239 | |
| 4240 | while (s < e) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4241 | Py_UCS4 ch; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4242 | restart: |
Antoine Pitrou | 5ffd9e9 | 2008-07-25 18:05:24 +0000 | [diff] [blame] | 4243 | ch = (unsigned char) *s; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4244 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4245 | if (inShift) { /* in a base-64 section */ |
| 4246 | if (IS_BASE64(ch)) { /* consume a base-64 character */ |
| 4247 | base64buffer = (base64buffer << 6) | FROM_BASE64(ch); |
| 4248 | base64bits += 6; |
| 4249 | s++; |
| 4250 | if (base64bits >= 16) { |
| 4251 | /* we have enough bits for a UTF-16 value */ |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 4252 | Py_UCS4 outCh = (Py_UCS4)(base64buffer >> (base64bits-16)); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4253 | base64bits -= 16; |
| 4254 | base64buffer &= (1 << base64bits) - 1; /* clear high bits */ |
| 4255 | if (surrogate) { |
| 4256 | /* expecting a second surrogate */ |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 4257 | if (Py_UNICODE_IS_LOW_SURROGATE(outCh)) { |
| 4258 | Py_UCS4 ch2 = Py_UNICODE_JOIN_SURROGATES(surrogate, outCh); |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4259 | if (unicode_putchar(&unicode, &outpos, ch2) < 0) |
| 4260 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4261 | surrogate = 0; |
Antoine Pitrou | 5418ee0 | 2011-11-15 01:42:21 +0100 | [diff] [blame] | 4262 | continue; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4263 | } |
| 4264 | else { |
Antoine Pitrou | 78edf75 | 2011-11-15 01:44:16 +0100 | [diff] [blame] | 4265 | if (unicode_putchar(&unicode, &outpos, surrogate) < 0) |
| 4266 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4267 | surrogate = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4268 | } |
| 4269 | } |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 4270 | if (Py_UNICODE_IS_HIGH_SURROGATE(outCh)) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4271 | /* first surrogate */ |
| 4272 | surrogate = outCh; |
| 4273 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4274 | else { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4275 | if (unicode_putchar(&unicode, &outpos, outCh) < 0) |
| 4276 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4277 | } |
| 4278 | } |
| 4279 | } |
| 4280 | else { /* now leaving a base-64 section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4281 | inShift = 0; |
| 4282 | s++; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4283 | if (surrogate) { |
Antoine Pitrou | 78edf75 | 2011-11-15 01:44:16 +0100 | [diff] [blame] | 4284 | if (unicode_putchar(&unicode, &outpos, surrogate) < 0) |
| 4285 | goto onError; |
Antoine Pitrou | 5418ee0 | 2011-11-15 01:42:21 +0100 | [diff] [blame] | 4286 | surrogate = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4287 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4288 | if (base64bits > 0) { /* left-over bits */ |
| 4289 | if (base64bits >= 6) { |
| 4290 | /* We've seen at least one base-64 character */ |
| 4291 | errmsg = "partial character in shift sequence"; |
| 4292 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4293 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4294 | else { |
| 4295 | /* Some bits remain; they should be zero */ |
| 4296 | if (base64buffer != 0) { |
| 4297 | errmsg = "non-zero padding bits in shift sequence"; |
| 4298 | goto utf7Error; |
| 4299 | } |
| 4300 | } |
| 4301 | } |
| 4302 | if (ch != '-') { |
| 4303 | /* '-' is absorbed; other terminating |
| 4304 | characters are preserved */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4305 | if (unicode_putchar(&unicode, &outpos, ch) < 0) |
| 4306 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4307 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4308 | } |
| 4309 | } |
| 4310 | else if ( ch == '+' ) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4311 | startinpos = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4312 | s++; /* consume '+' */ |
| 4313 | if (s < e && *s == '-') { /* '+-' encodes '+' */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4314 | s++; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4315 | if (unicode_putchar(&unicode, &outpos, '+') < 0) |
| 4316 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4317 | } |
| 4318 | else { /* begin base64-encoded section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4319 | inShift = 1; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4320 | shiftOutStart = outpos; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4321 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4322 | } |
| 4323 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4324 | else if (DECODE_DIRECT(ch)) { /* character decodes as itself */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4325 | if (unicode_putchar(&unicode, &outpos, ch) < 0) |
| 4326 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4327 | s++; |
| 4328 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4329 | else { |
| 4330 | startinpos = s-starts; |
| 4331 | s++; |
| 4332 | errmsg = "unexpected special character"; |
| 4333 | goto utf7Error; |
| 4334 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4335 | continue; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4336 | utf7Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4337 | endinpos = s-starts; |
| 4338 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4339 | errors, &errorHandler, |
| 4340 | "utf7", errmsg, |
| 4341 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4342 | &unicode, &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4343 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4344 | } |
| 4345 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4346 | /* end of string */ |
| 4347 | |
| 4348 | if (inShift && !consumed) { /* in shift sequence, no more to follow */ |
| 4349 | /* if we're in an inconsistent state, that's an error */ |
| 4350 | if (surrogate || |
| 4351 | (base64bits >= 6) || |
| 4352 | (base64bits > 0 && base64buffer != 0)) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4353 | endinpos = size; |
| 4354 | if (unicode_decode_call_errorhandler( |
| 4355 | errors, &errorHandler, |
| 4356 | "utf7", "unterminated shift sequence", |
| 4357 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4358 | &unicode, &outpos)) |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4359 | goto onError; |
| 4360 | if (s < e) |
| 4361 | goto restart; |
| 4362 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4363 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4364 | |
| 4365 | /* return state */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4366 | if (consumed) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4367 | if (inShift) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4368 | outpos = shiftOutStart; /* back off output */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4369 | *consumed = startinpos; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4370 | } |
| 4371 | else { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4372 | *consumed = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4373 | } |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4374 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4375 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4376 | if (unicode_resize(&unicode, outpos) < 0) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4377 | goto onError; |
| 4378 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4379 | Py_XDECREF(errorHandler); |
| 4380 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 4381 | return unicode_result(unicode); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4382 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4383 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4384 | Py_XDECREF(errorHandler); |
| 4385 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4386 | Py_DECREF(unicode); |
| 4387 | return NULL; |
| 4388 | } |
| 4389 | |
| 4390 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4391 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4392 | _PyUnicode_EncodeUTF7(PyObject *str, |
| 4393 | int base64SetO, |
| 4394 | int base64WhiteSpace, |
| 4395 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4396 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4397 | int kind; |
| 4398 | void *data; |
| 4399 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4400 | PyObject *v; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4401 | Py_ssize_t allocated; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4402 | int inShift = 0; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4403 | Py_ssize_t i; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4404 | unsigned int base64bits = 0; |
| 4405 | unsigned long base64buffer = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4406 | char * out; |
| 4407 | char * start; |
| 4408 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 4409 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4410 | return NULL; |
| 4411 | kind = PyUnicode_KIND(str); |
| 4412 | data = PyUnicode_DATA(str); |
| 4413 | len = PyUnicode_GET_LENGTH(str); |
| 4414 | |
| 4415 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4416 | return PyBytes_FromStringAndSize(NULL, 0); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4417 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4418 | /* It might be possible to tighten this worst case */ |
| 4419 | allocated = 8 * len; |
| 4420 | if (allocated / 8 != len) |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4421 | return PyErr_NoMemory(); |
| 4422 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4423 | v = PyBytes_FromStringAndSize(NULL, allocated); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4424 | if (v == NULL) |
| 4425 | return NULL; |
| 4426 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4427 | start = out = PyBytes_AS_STRING(v); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4428 | for (i = 0; i < len; ++i) { |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 4429 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4430 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4431 | if (inShift) { |
| 4432 | if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 4433 | /* shifting out */ |
| 4434 | if (base64bits) { /* output remaining bits */ |
| 4435 | *out++ = TO_BASE64(base64buffer << (6-base64bits)); |
| 4436 | base64buffer = 0; |
| 4437 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4438 | } |
| 4439 | inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4440 | /* Characters not in the BASE64 set implicitly unshift the sequence |
| 4441 | so no '-' is required, except if the character is itself a '-' */ |
| 4442 | if (IS_BASE64(ch) || ch == '-') { |
| 4443 | *out++ = '-'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4444 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4445 | *out++ = (char) ch; |
| 4446 | } |
| 4447 | else { |
| 4448 | goto encode_char; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4449 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4450 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4451 | else { /* not in a shift sequence */ |
| 4452 | if (ch == '+') { |
| 4453 | *out++ = '+'; |
| 4454 | *out++ = '-'; |
| 4455 | } |
| 4456 | else if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 4457 | *out++ = (char) ch; |
| 4458 | } |
| 4459 | else { |
| 4460 | *out++ = '+'; |
| 4461 | inShift = 1; |
| 4462 | goto encode_char; |
| 4463 | } |
| 4464 | } |
| 4465 | continue; |
| 4466 | encode_char: |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4467 | if (ch >= 0x10000) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 4468 | assert(ch <= MAX_UNICODE); |
Victor Stinner | 0d3721d | 2011-11-22 03:27:53 +0100 | [diff] [blame] | 4469 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4470 | /* code first surrogate */ |
| 4471 | base64bits += 16; |
| 4472 | base64buffer = (base64buffer << 16) | 0xd800 | ((ch-0x10000) >> 10); |
| 4473 | while (base64bits >= 6) { |
| 4474 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 4475 | base64bits -= 6; |
| 4476 | } |
| 4477 | /* prepare second surrogate */ |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 4478 | ch = Py_UNICODE_LOW_SURROGATE(ch); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4479 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4480 | base64bits += 16; |
| 4481 | base64buffer = (base64buffer << 16) | ch; |
| 4482 | while (base64bits >= 6) { |
| 4483 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 4484 | base64bits -= 6; |
| 4485 | } |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 4486 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4487 | if (base64bits) |
| 4488 | *out++= TO_BASE64(base64buffer << (6-base64bits) ); |
| 4489 | if (inShift) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4490 | *out++ = '-'; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4491 | if (_PyBytes_Resize(&v, out - start) < 0) |
| 4492 | return NULL; |
| 4493 | return v; |
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 | PyObject * |
| 4496 | PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
| 4497 | Py_ssize_t size, |
| 4498 | int base64SetO, |
| 4499 | int base64WhiteSpace, |
| 4500 | const char *errors) |
| 4501 | { |
| 4502 | PyObject *result; |
| 4503 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 4504 | if (tmp == NULL) |
| 4505 | return NULL; |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 4506 | result = _PyUnicode_EncodeUTF7(tmp, base64SetO, |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4507 | base64WhiteSpace, errors); |
| 4508 | Py_DECREF(tmp); |
| 4509 | return result; |
| 4510 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4511 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4512 | #undef IS_BASE64 |
| 4513 | #undef FROM_BASE64 |
| 4514 | #undef TO_BASE64 |
| 4515 | #undef DECODE_DIRECT |
| 4516 | #undef ENCODE_DIRECT |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4517 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4518 | /* --- UTF-8 Codec -------------------------------------------------------- */ |
| 4519 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4520 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4521 | char utf8_code_length[256] = { |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4522 | /* Map UTF-8 encoded prefix byte to sequence length. Zero means |
| 4523 | illegal prefix. See RFC 3629 for details */ |
| 4524 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 00-0F */ |
| 4525 | 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] | 4526 | 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] | 4527 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 4528 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 4529 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 4530 | 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] | 4531 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 70-7F */ |
| 4532 | 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] | 4533 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4534 | 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] | 4535 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* B0-BF */ |
| 4536 | 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* C0-C1 + C2-CF */ |
| 4537 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* D0-DF */ |
| 4538 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* E0-EF */ |
| 4539 | 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] | 4540 | }; |
| 4541 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4542 | PyObject * |
| 4543 | PyUnicode_DecodeUTF8(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4544 | Py_ssize_t size, |
| 4545 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4546 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4547 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
| 4548 | } |
| 4549 | |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4550 | #include "stringlib/ucs1lib.h" |
| 4551 | #include "stringlib/codecs.h" |
| 4552 | #include "stringlib/undef.h" |
| 4553 | |
| 4554 | #include "stringlib/ucs2lib.h" |
| 4555 | #include "stringlib/codecs.h" |
| 4556 | #include "stringlib/undef.h" |
| 4557 | |
| 4558 | #include "stringlib/ucs4lib.h" |
| 4559 | #include "stringlib/codecs.h" |
| 4560 | #include "stringlib/undef.h" |
| 4561 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4562 | /* Mask to check or force alignment of a pointer to C 'long' boundaries */ |
| 4563 | #define LONG_PTR_MASK (size_t) (SIZEOF_LONG - 1) |
| 4564 | |
| 4565 | /* Mask to quickly check whether a C 'long' contains a |
| 4566 | non-ASCII, UTF8-encoded char. */ |
| 4567 | #if (SIZEOF_LONG == 8) |
| 4568 | # define ASCII_CHAR_MASK 0x8080808080808080L |
| 4569 | #elif (SIZEOF_LONG == 4) |
| 4570 | # define ASCII_CHAR_MASK 0x80808080L |
| 4571 | #else |
| 4572 | # error C 'long' size should be either 4 or 8! |
| 4573 | #endif |
| 4574 | |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4575 | /* Scans a UTF-8 string and returns the maximum character to be expected |
| 4576 | and the size of the decoded unicode string. |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4577 | |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4578 | 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] | 4579 | PyUnicode_DecodeUTF8Stateful. |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4580 | */ |
| 4581 | static Py_UCS4 |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 4582 | 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] | 4583 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4584 | Py_ssize_t char_count = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4585 | const unsigned char *end = p + string_size; |
| 4586 | 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] | 4587 | |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4588 | assert(unicode_size != NULL); |
| 4589 | |
| 4590 | /* By having a cascade of independent loops which fallback onto each |
| 4591 | other, we minimize the amount of work done in the average loop |
| 4592 | iteration, and we also maximize the CPU's ability to predict |
| 4593 | branches correctly (because a given condition will have always the |
| 4594 | same boolean outcome except perhaps in the last iteration of the |
| 4595 | corresponding loop). |
| 4596 | In the general case this brings us rather close to decoding |
| 4597 | performance pre-PEP 393, despite the two-pass decoding. |
| 4598 | |
| 4599 | Note that the pure ASCII loop is not duplicated once a non-ASCII |
| 4600 | character has been encountered. It is actually a pessimization (by |
| 4601 | a significant factor) to use this loop on text with many non-ASCII |
| 4602 | characters, and it is important to avoid bad performance on valid |
| 4603 | utf-8 data (invalid utf-8 being a different can of worms). |
| 4604 | */ |
| 4605 | |
| 4606 | /* ASCII */ |
| 4607 | for (; p < end; ++p) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4608 | /* Only check value if it's not a ASCII char... */ |
| 4609 | if (*p < 0x80) { |
| 4610 | /* Fast path, see below in PyUnicode_DecodeUTF8Stateful for |
| 4611 | an explanation. */ |
| 4612 | if (!((size_t) p & LONG_PTR_MASK)) { |
| 4613 | /* Help register allocation */ |
| 4614 | register const unsigned char *_p = p; |
| 4615 | while (_p < aligned_end) { |
| 4616 | unsigned long value = *(unsigned long *) _p; |
| 4617 | if (value & ASCII_CHAR_MASK) |
| 4618 | break; |
| 4619 | _p += SIZEOF_LONG; |
| 4620 | char_count += SIZEOF_LONG; |
| 4621 | } |
| 4622 | p = _p; |
| 4623 | if (p == end) |
| 4624 | break; |
| 4625 | } |
| 4626 | } |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4627 | if (*p < 0x80) |
| 4628 | ++char_count; |
| 4629 | else |
| 4630 | goto _ucs1loop; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4631 | } |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4632 | *unicode_size = char_count; |
| 4633 | return 127; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4634 | |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4635 | _ucs1loop: |
| 4636 | for (; p < end; ++p) { |
| 4637 | if (*p < 0xc4) |
| 4638 | char_count += ((*p & 0xc0) != 0x80); |
| 4639 | else |
| 4640 | goto _ucs2loop; |
| 4641 | } |
| 4642 | *unicode_size = char_count; |
| 4643 | return 255; |
| 4644 | |
| 4645 | _ucs2loop: |
| 4646 | for (; p < end; ++p) { |
| 4647 | if (*p < 0xf0) |
| 4648 | char_count += ((*p & 0xc0) != 0x80); |
| 4649 | else |
| 4650 | goto _ucs4loop; |
| 4651 | } |
| 4652 | *unicode_size = char_count; |
| 4653 | return 65535; |
| 4654 | |
| 4655 | _ucs4loop: |
| 4656 | for (; p < end; ++p) { |
| 4657 | char_count += ((*p & 0xc0) != 0x80); |
| 4658 | } |
| 4659 | *unicode_size = char_count; |
| 4660 | return 65537; |
| 4661 | } |
| 4662 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4663 | /* 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] | 4664 | in case of errors. Implicit parameters: unicode, kind, data, onError. |
| 4665 | 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] | 4666 | */ |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4667 | #define WRITE_MAYBE_FAIL(index, value) \ |
| 4668 | do { \ |
| 4669 | Py_ssize_t pos = index; \ |
| 4670 | if (pos > PyUnicode_GET_LENGTH(unicode) && \ |
| 4671 | unicode_resize(&unicode, pos + pos/8) < 0) \ |
| 4672 | goto onError; \ |
| 4673 | if (unicode_putchar(&unicode, &pos, value) < 0) \ |
| 4674 | goto onError; \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4675 | } while (0) |
| 4676 | |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 4677 | static PyObject * |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4678 | decode_utf8_errors(const char *starts, |
| 4679 | Py_ssize_t size, |
| 4680 | const char *errors, |
| 4681 | Py_ssize_t *consumed, |
| 4682 | const char *s, |
| 4683 | PyObject *unicode, |
| 4684 | Py_ssize_t i) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4685 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4686 | int n; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4687 | int k; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4688 | Py_ssize_t startinpos; |
| 4689 | Py_ssize_t endinpos; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4690 | const char *e = starts + size; |
| 4691 | const char *aligned_end; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4692 | const char *errmsg = ""; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4693 | PyObject *errorHandler = NULL; |
| 4694 | PyObject *exc = NULL; |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4695 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4696 | aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4697 | |
| 4698 | while (s < e) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 4699 | Py_UCS4 ch = (unsigned char)*s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4700 | |
| 4701 | if (ch < 0x80) { |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4702 | /* Fast path for runs of ASCII characters. Given that common UTF-8 |
| 4703 | input will consist of an overwhelming majority of ASCII |
| 4704 | characters, we try to optimize for this case by checking |
| 4705 | as many characters as a C 'long' can contain. |
| 4706 | First, check if we can do an aligned read, as most CPUs have |
| 4707 | a penalty for unaligned reads. |
| 4708 | */ |
| 4709 | if (!((size_t) s & LONG_PTR_MASK)) { |
| 4710 | /* Help register allocation */ |
| 4711 | register const char *_s = s; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4712 | register Py_ssize_t _i = i; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4713 | while (_s < aligned_end) { |
| 4714 | /* Read a whole long at a time (either 4 or 8 bytes), |
| 4715 | and do a fast unrolled copy if it only contains ASCII |
| 4716 | characters. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4717 | unsigned long value = *(unsigned long *) _s; |
| 4718 | if (value & ASCII_CHAR_MASK) |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4719 | break; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4720 | WRITE_MAYBE_FAIL(_i+0, _s[0]); |
| 4721 | WRITE_MAYBE_FAIL(_i+1, _s[1]); |
| 4722 | WRITE_MAYBE_FAIL(_i+2, _s[2]); |
| 4723 | WRITE_MAYBE_FAIL(_i+3, _s[3]); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4724 | #if (SIZEOF_LONG == 8) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4725 | WRITE_MAYBE_FAIL(_i+4, _s[4]); |
| 4726 | WRITE_MAYBE_FAIL(_i+5, _s[5]); |
| 4727 | WRITE_MAYBE_FAIL(_i+6, _s[6]); |
| 4728 | WRITE_MAYBE_FAIL(_i+7, _s[7]); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4729 | #endif |
| 4730 | _s += SIZEOF_LONG; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4731 | _i += SIZEOF_LONG; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4732 | } |
| 4733 | s = _s; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4734 | i = _i; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4735 | if (s == e) |
| 4736 | break; |
| 4737 | ch = (unsigned char)*s; |
| 4738 | } |
| 4739 | } |
| 4740 | |
| 4741 | if (ch < 0x80) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4742 | WRITE_MAYBE_FAIL(i++, ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4743 | s++; |
| 4744 | continue; |
| 4745 | } |
| 4746 | |
| 4747 | n = utf8_code_length[ch]; |
| 4748 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4749 | if (s + n > e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4750 | if (consumed) |
| 4751 | break; |
| 4752 | else { |
| 4753 | errmsg = "unexpected end of data"; |
| 4754 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4755 | endinpos = startinpos+1; |
| 4756 | for (k=1; (k < size-startinpos) && ((s[k]&0xC0) == 0x80); k++) |
| 4757 | endinpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4758 | goto utf8Error; |
| 4759 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4760 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4761 | |
| 4762 | switch (n) { |
| 4763 | |
| 4764 | case 0: |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4765 | errmsg = "invalid start byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4766 | startinpos = s-starts; |
| 4767 | endinpos = startinpos+1; |
| 4768 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4769 | |
| 4770 | case 1: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4771 | errmsg = "internal error"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4772 | startinpos = s-starts; |
| 4773 | endinpos = startinpos+1; |
| 4774 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4775 | |
| 4776 | case 2: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4777 | if ((s[1] & 0xc0) != 0x80) { |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4778 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4779 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4780 | endinpos = startinpos + 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4781 | goto utf8Error; |
| 4782 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4783 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4784 | assert ((ch > 0x007F) && (ch <= 0x07FF)); |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4785 | WRITE_MAYBE_FAIL(i++, ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4786 | break; |
| 4787 | |
| 4788 | case 3: |
Ezio Melotti | 9bf2b3a | 2010-07-03 04:52:19 +0000 | [diff] [blame] | 4789 | /* Decoding UTF-8 sequences in range \xed\xa0\x80-\xed\xbf\xbf |
| 4790 | will result in surrogates in range d800-dfff. Surrogates are |
| 4791 | not valid UTF-8 so they are rejected. |
| 4792 | See http://www.unicode.org/versions/Unicode5.2.0/ch03.pdf |
| 4793 | (table 3-7) and http://www.rfc-editor.org/rfc/rfc3629.txt */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4794 | if ((s[1] & 0xc0) != 0x80 || |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4795 | (s[2] & 0xc0) != 0x80 || |
| 4796 | ((unsigned char)s[0] == 0xE0 && |
| 4797 | (unsigned char)s[1] < 0xA0) || |
| 4798 | ((unsigned char)s[0] == 0xED && |
| 4799 | (unsigned char)s[1] > 0x9F)) { |
| 4800 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4801 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4802 | endinpos = startinpos + 1; |
| 4803 | |
| 4804 | /* if s[1] first two bits are 1 and 0, then the invalid |
| 4805 | continuation byte is s[2], so increment endinpos by 1, |
| 4806 | if not, s[1] is invalid and endinpos doesn't need to |
| 4807 | be incremented. */ |
| 4808 | if ((s[1] & 0xC0) == 0x80) |
| 4809 | endinpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4810 | goto utf8Error; |
| 4811 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4812 | ch = ((s[0] & 0x0f) << 12) + ((s[1] & 0x3f) << 6) + (s[2] & 0x3f); |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4813 | assert ((ch > 0x07FF) && (ch <= 0xFFFF)); |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4814 | WRITE_MAYBE_FAIL(i++, ch); |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 4815 | break; |
| 4816 | |
| 4817 | case 4: |
| 4818 | if ((s[1] & 0xc0) != 0x80 || |
| 4819 | (s[2] & 0xc0) != 0x80 || |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4820 | (s[3] & 0xc0) != 0x80 || |
| 4821 | ((unsigned char)s[0] == 0xF0 && |
| 4822 | (unsigned char)s[1] < 0x90) || |
| 4823 | ((unsigned char)s[0] == 0xF4 && |
| 4824 | (unsigned char)s[1] > 0x8F)) { |
| 4825 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4826 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4827 | endinpos = startinpos + 1; |
| 4828 | if ((s[1] & 0xC0) == 0x80) { |
| 4829 | endinpos++; |
| 4830 | if ((s[2] & 0xC0) == 0x80) |
| 4831 | endinpos++; |
| 4832 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4833 | goto utf8Error; |
| 4834 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 4835 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4836 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 4837 | assert ((ch > 0xFFFF) && (ch <= MAX_UNICODE)); |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4838 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4839 | WRITE_MAYBE_FAIL(i++, ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4840 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4841 | } |
| 4842 | s += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4843 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4844 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4845 | utf8Error: |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4846 | if (unicode_decode_call_errorhandler( |
| 4847 | errors, &errorHandler, |
Victor Stinner | cbe0134 | 2012-02-14 01:17:45 +0100 | [diff] [blame] | 4848 | "utf-8", errmsg, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4849 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4850 | &unicode, &i)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4851 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4852 | /* Update data because unicode_decode_call_errorhandler might have |
| 4853 | re-created or resized the unicode object. */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4854 | aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4855 | } |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4856 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4857 | *consumed = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4858 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4859 | /* Adjust length and ready string when it contained errors and |
| 4860 | is of the old resizable kind. */ |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4861 | if (unicode_resize(&unicode, i) < 0) |
| 4862 | goto onError; |
| 4863 | unicode_adjust_maxchar(&unicode); |
| 4864 | if (unicode == NULL) |
| 4865 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4866 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4867 | Py_XDECREF(errorHandler); |
| 4868 | Py_XDECREF(exc); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 4869 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4870 | return unicode; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4871 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4872 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4873 | Py_XDECREF(errorHandler); |
| 4874 | Py_XDECREF(exc); |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4875 | Py_XDECREF(unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4876 | return NULL; |
| 4877 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4878 | #undef WRITE_MAYBE_FAIL |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4879 | |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4880 | PyObject * |
| 4881 | PyUnicode_DecodeUTF8Stateful(const char *s, |
| 4882 | Py_ssize_t size, |
| 4883 | const char *errors, |
| 4884 | Py_ssize_t *consumed) |
| 4885 | { |
| 4886 | Py_UCS4 maxchar = 0; |
| 4887 | Py_ssize_t unicode_size; |
| 4888 | int has_errors = 0; |
| 4889 | PyObject *unicode; |
| 4890 | int kind; |
| 4891 | void *data; |
| 4892 | const char *starts = s; |
| 4893 | const char *e; |
| 4894 | Py_ssize_t i; |
| 4895 | |
| 4896 | if (size == 0) { |
| 4897 | if (consumed) |
| 4898 | *consumed = 0; |
Victor Stinner | 382955f | 2011-12-11 21:44:00 +0100 | [diff] [blame] | 4899 | Py_INCREF(unicode_empty); |
| 4900 | return unicode_empty; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4901 | } |
| 4902 | |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 4903 | maxchar = utf8_scanner((const unsigned char *)s, size, &unicode_size); |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4904 | |
| 4905 | /* When the string is ASCII only, just use memcpy and return. |
| 4906 | unicode_size may be != size if there is an incomplete UTF-8 |
| 4907 | sequence at the end of the ASCII block. */ |
| 4908 | if (maxchar < 128 && size == unicode_size) { |
| 4909 | if (consumed) |
| 4910 | *consumed = size; |
Victor Stinner | ab87021 | 2011-12-17 22:39:43 +0100 | [diff] [blame] | 4911 | return unicode_fromascii((const unsigned char *)s, size); |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4912 | } |
| 4913 | |
| 4914 | unicode = PyUnicode_New(unicode_size, maxchar); |
| 4915 | if (!unicode) |
| 4916 | return NULL; |
| 4917 | kind = PyUnicode_KIND(unicode); |
| 4918 | data = PyUnicode_DATA(unicode); |
| 4919 | |
| 4920 | /* Unpack UTF-8 encoded data */ |
| 4921 | i = 0; |
| 4922 | e = starts + size; |
| 4923 | switch (kind) { |
| 4924 | case PyUnicode_1BYTE_KIND: |
| 4925 | has_errors = ucs1lib_utf8_try_decode(s, e, (Py_UCS1 *) data, &s, &i); |
| 4926 | break; |
| 4927 | case PyUnicode_2BYTE_KIND: |
| 4928 | has_errors = ucs2lib_utf8_try_decode(s, e, (Py_UCS2 *) data, &s, &i); |
| 4929 | break; |
| 4930 | case PyUnicode_4BYTE_KIND: |
| 4931 | has_errors = ucs4lib_utf8_try_decode(s, e, (Py_UCS4 *) data, &s, &i); |
| 4932 | break; |
| 4933 | } |
| 4934 | if (!has_errors) { |
| 4935 | /* Ensure the unicode size calculation was correct */ |
| 4936 | assert(i == unicode_size); |
| 4937 | assert(s == e); |
| 4938 | if (consumed) |
| 4939 | *consumed = size; |
| 4940 | return unicode; |
| 4941 | } |
| 4942 | |
| 4943 | /* In case of errors, maxchar and size computation might be incorrect; |
| 4944 | code below refits and resizes as necessary. */ |
| 4945 | return decode_utf8_errors(starts, size, errors, consumed, s, unicode, i); |
| 4946 | } |
| 4947 | |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4948 | #ifdef __APPLE__ |
| 4949 | |
| 4950 | /* Simplified UTF-8 decoder using surrogateescape error handler, |
| 4951 | used to decode the command line arguments on Mac OS X. */ |
| 4952 | |
| 4953 | wchar_t* |
| 4954 | _Py_DecodeUTF8_surrogateescape(const char *s, Py_ssize_t size) |
| 4955 | { |
| 4956 | int n; |
| 4957 | const char *e; |
| 4958 | wchar_t *unicode, *p; |
| 4959 | |
| 4960 | /* Note: size will always be longer than the resulting Unicode |
| 4961 | character count */ |
| 4962 | if (PY_SSIZE_T_MAX / sizeof(wchar_t) < (size + 1)) { |
| 4963 | PyErr_NoMemory(); |
| 4964 | return NULL; |
| 4965 | } |
| 4966 | unicode = PyMem_Malloc((size + 1) * sizeof(wchar_t)); |
| 4967 | if (!unicode) |
| 4968 | return NULL; |
| 4969 | |
| 4970 | /* Unpack UTF-8 encoded data */ |
| 4971 | p = unicode; |
| 4972 | e = s + size; |
| 4973 | while (s < e) { |
| 4974 | Py_UCS4 ch = (unsigned char)*s; |
| 4975 | |
| 4976 | if (ch < 0x80) { |
| 4977 | *p++ = (wchar_t)ch; |
| 4978 | s++; |
| 4979 | continue; |
| 4980 | } |
| 4981 | |
| 4982 | n = utf8_code_length[ch]; |
| 4983 | if (s + n > e) { |
| 4984 | goto surrogateescape; |
| 4985 | } |
| 4986 | |
| 4987 | switch (n) { |
| 4988 | case 0: |
| 4989 | case 1: |
| 4990 | goto surrogateescape; |
| 4991 | |
| 4992 | case 2: |
| 4993 | if ((s[1] & 0xc0) != 0x80) |
| 4994 | goto surrogateescape; |
| 4995 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
| 4996 | assert ((ch > 0x007F) && (ch <= 0x07FF)); |
| 4997 | *p++ = (wchar_t)ch; |
| 4998 | break; |
| 4999 | |
| 5000 | case 3: |
| 5001 | /* Decoding UTF-8 sequences in range \xed\xa0\x80-\xed\xbf\xbf |
| 5002 | will result in surrogates in range d800-dfff. Surrogates are |
| 5003 | not valid UTF-8 so they are rejected. |
| 5004 | See http://www.unicode.org/versions/Unicode5.2.0/ch03.pdf |
| 5005 | (table 3-7) and http://www.rfc-editor.org/rfc/rfc3629.txt */ |
| 5006 | if ((s[1] & 0xc0) != 0x80 || |
| 5007 | (s[2] & 0xc0) != 0x80 || |
| 5008 | ((unsigned char)s[0] == 0xE0 && |
| 5009 | (unsigned char)s[1] < 0xA0) || |
| 5010 | ((unsigned char)s[0] == 0xED && |
| 5011 | (unsigned char)s[1] > 0x9F)) { |
| 5012 | |
| 5013 | goto surrogateescape; |
| 5014 | } |
| 5015 | ch = ((s[0] & 0x0f) << 12) + ((s[1] & 0x3f) << 6) + (s[2] & 0x3f); |
| 5016 | assert ((ch > 0x07FF) && (ch <= 0xFFFF)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5017 | *p++ = (wchar_t)ch; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 5018 | break; |
| 5019 | |
| 5020 | case 4: |
| 5021 | if ((s[1] & 0xc0) != 0x80 || |
| 5022 | (s[2] & 0xc0) != 0x80 || |
| 5023 | (s[3] & 0xc0) != 0x80 || |
| 5024 | ((unsigned char)s[0] == 0xF0 && |
| 5025 | (unsigned char)s[1] < 0x90) || |
| 5026 | ((unsigned char)s[0] == 0xF4 && |
| 5027 | (unsigned char)s[1] > 0x8F)) { |
| 5028 | goto surrogateescape; |
| 5029 | } |
| 5030 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
| 5031 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 5032 | assert ((ch > 0xFFFF) && (ch <= MAX_UNICODE)); |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 5033 | |
| 5034 | #if SIZEOF_WCHAR_T == 4 |
| 5035 | *p++ = (wchar_t)ch; |
| 5036 | #else |
| 5037 | /* compute and append the two surrogates: */ |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 5038 | *p++ = (wchar_t)Py_UNICODE_HIGH_SURROGATE(ch); |
| 5039 | *p++ = (wchar_t)Py_UNICODE_LOW_SURROGATE(ch); |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 5040 | #endif |
| 5041 | break; |
| 5042 | } |
| 5043 | s += n; |
| 5044 | continue; |
| 5045 | |
| 5046 | surrogateescape: |
| 5047 | *p++ = 0xDC00 + ch; |
| 5048 | s++; |
| 5049 | } |
| 5050 | *p = L'\0'; |
| 5051 | return unicode; |
| 5052 | } |
| 5053 | |
| 5054 | #endif /* __APPLE__ */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5055 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5056 | /* Primary internal function which creates utf8 encoded bytes objects. |
| 5057 | |
| 5058 | Allocation strategy: if the string is short, convert into a stack buffer |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 5059 | and allocate exactly as much space needed at the end. Else allocate the |
| 5060 | maximum possible needed (4 result bytes per Unicode character), and return |
| 5061 | the excess memory at the end. |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 5062 | */ |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 5063 | PyObject * |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5064 | _PyUnicode_AsUTF8String(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5065 | { |
Victor Stinner | 6099a03 | 2011-12-18 14:22:26 +0100 | [diff] [blame] | 5066 | enum PyUnicode_Kind kind; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5067 | void *data; |
| 5068 | Py_ssize_t size; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 5069 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5070 | if (!PyUnicode_Check(unicode)) { |
| 5071 | PyErr_BadArgument(); |
| 5072 | return NULL; |
| 5073 | } |
| 5074 | |
| 5075 | if (PyUnicode_READY(unicode) == -1) |
| 5076 | return NULL; |
| 5077 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 5078 | if (PyUnicode_UTF8(unicode)) |
| 5079 | return PyBytes_FromStringAndSize(PyUnicode_UTF8(unicode), |
| 5080 | PyUnicode_UTF8_LENGTH(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5081 | |
| 5082 | kind = PyUnicode_KIND(unicode); |
| 5083 | data = PyUnicode_DATA(unicode); |
| 5084 | size = PyUnicode_GET_LENGTH(unicode); |
| 5085 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 5086 | switch (kind) { |
Victor Stinner | 6099a03 | 2011-12-18 14:22:26 +0100 | [diff] [blame] | 5087 | default: |
| 5088 | assert(0); |
| 5089 | case PyUnicode_1BYTE_KIND: |
| 5090 | /* the string cannot be ASCII, or PyUnicode_UTF8() would be set */ |
| 5091 | assert(!PyUnicode_IS_ASCII(unicode)); |
| 5092 | return ucs1lib_utf8_encoder(unicode, data, size, errors); |
| 5093 | case PyUnicode_2BYTE_KIND: |
| 5094 | return ucs2lib_utf8_encoder(unicode, data, size, errors); |
| 5095 | case PyUnicode_4BYTE_KIND: |
| 5096 | return ucs4lib_utf8_encoder(unicode, data, size, errors); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 5097 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5098 | } |
| 5099 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5100 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5101 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
| 5102 | Py_ssize_t size, |
| 5103 | const char *errors) |
| 5104 | { |
| 5105 | PyObject *v, *unicode; |
| 5106 | |
| 5107 | unicode = PyUnicode_FromUnicode(s, size); |
| 5108 | if (unicode == NULL) |
| 5109 | return NULL; |
| 5110 | v = _PyUnicode_AsUTF8String(unicode, errors); |
| 5111 | Py_DECREF(unicode); |
| 5112 | return v; |
| 5113 | } |
| 5114 | |
| 5115 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5116 | PyUnicode_AsUTF8String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5117 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5118 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5119 | } |
| 5120 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5121 | /* --- UTF-32 Codec ------------------------------------------------------- */ |
| 5122 | |
| 5123 | PyObject * |
| 5124 | PyUnicode_DecodeUTF32(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5125 | Py_ssize_t size, |
| 5126 | const char *errors, |
| 5127 | int *byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5128 | { |
| 5129 | return PyUnicode_DecodeUTF32Stateful(s, size, errors, byteorder, NULL); |
| 5130 | } |
| 5131 | |
| 5132 | PyObject * |
| 5133 | PyUnicode_DecodeUTF32Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5134 | Py_ssize_t size, |
| 5135 | const char *errors, |
| 5136 | int *byteorder, |
| 5137 | Py_ssize_t *consumed) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5138 | { |
| 5139 | const char *starts = s; |
| 5140 | Py_ssize_t startinpos; |
| 5141 | Py_ssize_t endinpos; |
| 5142 | Py_ssize_t outpos; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5143 | PyObject *unicode; |
Mark Dickinson | 7db923c | 2010-06-12 09:10:14 +0000 | [diff] [blame] | 5144 | const unsigned char *q, *e; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5145 | int bo = 0; /* assume native ordering by default */ |
| 5146 | const char *errmsg = ""; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5147 | /* Offsets from q for retrieving bytes in the right order. */ |
| 5148 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5149 | int iorder[] = {0, 1, 2, 3}; |
| 5150 | #else |
| 5151 | int iorder[] = {3, 2, 1, 0}; |
| 5152 | #endif |
| 5153 | PyObject *errorHandler = NULL; |
| 5154 | PyObject *exc = NULL; |
Victor Stinner | 313a120 | 2010-06-11 23:56:51 +0000 | [diff] [blame] | 5155 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5156 | q = (unsigned char *)s; |
| 5157 | e = q + size; |
| 5158 | |
| 5159 | if (byteorder) |
| 5160 | bo = *byteorder; |
| 5161 | |
| 5162 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 5163 | byte order setting accordingly. In native mode, the leading BOM |
| 5164 | mark is skipped, in all other modes, it is copied to the output |
| 5165 | stream as-is (giving a ZWNBSP character). */ |
| 5166 | if (bo == 0) { |
| 5167 | if (size >= 4) { |
| 5168 | const Py_UCS4 bom = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5169 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5170 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5171 | if (bom == 0x0000FEFF) { |
| 5172 | q += 4; |
| 5173 | bo = -1; |
| 5174 | } |
| 5175 | else if (bom == 0xFFFE0000) { |
| 5176 | q += 4; |
| 5177 | bo = 1; |
| 5178 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5179 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5180 | if (bom == 0x0000FEFF) { |
| 5181 | q += 4; |
| 5182 | bo = 1; |
| 5183 | } |
| 5184 | else if (bom == 0xFFFE0000) { |
| 5185 | q += 4; |
| 5186 | bo = -1; |
| 5187 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5188 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5189 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5190 | } |
| 5191 | |
| 5192 | if (bo == -1) { |
| 5193 | /* force LE */ |
| 5194 | iorder[0] = 0; |
| 5195 | iorder[1] = 1; |
| 5196 | iorder[2] = 2; |
| 5197 | iorder[3] = 3; |
| 5198 | } |
| 5199 | else if (bo == 1) { |
| 5200 | /* force BE */ |
| 5201 | iorder[0] = 3; |
| 5202 | iorder[1] = 2; |
| 5203 | iorder[2] = 1; |
| 5204 | iorder[3] = 0; |
| 5205 | } |
| 5206 | |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 5207 | /* This might be one to much, because of a BOM */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5208 | unicode = PyUnicode_New((size+3)/4, 127); |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 5209 | if (!unicode) |
| 5210 | return NULL; |
| 5211 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5212 | return unicode; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5213 | outpos = 0; |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 5214 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5215 | while (q < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5216 | Py_UCS4 ch; |
| 5217 | /* remaining bytes at the end? (size should be divisible by 4) */ |
| 5218 | if (e-q<4) { |
| 5219 | if (consumed) |
| 5220 | break; |
| 5221 | errmsg = "truncated data"; |
| 5222 | startinpos = ((const char *)q)-starts; |
| 5223 | endinpos = ((const char *)e)-starts; |
| 5224 | goto utf32Error; |
| 5225 | /* The remaining input chars are ignored if the callback |
| 5226 | chooses to skip the input */ |
| 5227 | } |
| 5228 | ch = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
| 5229 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5230 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5231 | if (ch >= 0x110000) |
| 5232 | { |
| 5233 | errmsg = "codepoint not in range(0x110000)"; |
| 5234 | startinpos = ((const char *)q)-starts; |
| 5235 | endinpos = startinpos+4; |
| 5236 | goto utf32Error; |
| 5237 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5238 | if (unicode_putchar(&unicode, &outpos, ch) < 0) |
| 5239 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5240 | q += 4; |
| 5241 | continue; |
| 5242 | utf32Error: |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5243 | if (unicode_decode_call_errorhandler( |
| 5244 | errors, &errorHandler, |
| 5245 | "utf32", errmsg, |
| 5246 | &starts, (const char **)&e, &startinpos, &endinpos, &exc, (const char **)&q, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5247 | &unicode, &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5248 | goto onError; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5249 | } |
| 5250 | |
| 5251 | if (byteorder) |
| 5252 | *byteorder = bo; |
| 5253 | |
| 5254 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5255 | *consumed = (const char *)q-starts; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5256 | |
| 5257 | /* Adjust length */ |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 5258 | if (unicode_resize(&unicode, outpos) < 0) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5259 | goto onError; |
| 5260 | |
| 5261 | Py_XDECREF(errorHandler); |
| 5262 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 5263 | return unicode_result(unicode); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5264 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5265 | onError: |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5266 | Py_DECREF(unicode); |
| 5267 | Py_XDECREF(errorHandler); |
| 5268 | Py_XDECREF(exc); |
| 5269 | return NULL; |
| 5270 | } |
| 5271 | |
| 5272 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5273 | _PyUnicode_EncodeUTF32(PyObject *str, |
| 5274 | const char *errors, |
| 5275 | int byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5276 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5277 | int kind; |
| 5278 | void *data; |
| 5279 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5280 | PyObject *v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5281 | unsigned char *p; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5282 | Py_ssize_t nsize, bytesize, i; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5283 | /* Offsets from p for storing byte pairs in the right order. */ |
| 5284 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5285 | int iorder[] = {0, 1, 2, 3}; |
| 5286 | #else |
| 5287 | int iorder[] = {3, 2, 1, 0}; |
| 5288 | #endif |
| 5289 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5290 | #define STORECHAR(CH) \ |
| 5291 | do { \ |
| 5292 | p[iorder[3]] = ((CH) >> 24) & 0xff; \ |
| 5293 | p[iorder[2]] = ((CH) >> 16) & 0xff; \ |
| 5294 | p[iorder[1]] = ((CH) >> 8) & 0xff; \ |
| 5295 | p[iorder[0]] = (CH) & 0xff; \ |
| 5296 | p += 4; \ |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5297 | } while(0) |
| 5298 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5299 | if (!PyUnicode_Check(str)) { |
| 5300 | PyErr_BadArgument(); |
| 5301 | return NULL; |
| 5302 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 5303 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5304 | return NULL; |
| 5305 | kind = PyUnicode_KIND(str); |
| 5306 | data = PyUnicode_DATA(str); |
| 5307 | len = PyUnicode_GET_LENGTH(str); |
| 5308 | |
| 5309 | nsize = len + (byteorder == 0); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5310 | bytesize = nsize * 4; |
| 5311 | if (bytesize / 4 != nsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5312 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5313 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5314 | if (v == NULL) |
| 5315 | return NULL; |
| 5316 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5317 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5318 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5319 | STORECHAR(0xFEFF); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5320 | if (len == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5321 | goto done; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5322 | |
| 5323 | if (byteorder == -1) { |
| 5324 | /* force LE */ |
| 5325 | iorder[0] = 0; |
| 5326 | iorder[1] = 1; |
| 5327 | iorder[2] = 2; |
| 5328 | iorder[3] = 3; |
| 5329 | } |
| 5330 | else if (byteorder == 1) { |
| 5331 | /* force BE */ |
| 5332 | iorder[0] = 3; |
| 5333 | iorder[1] = 2; |
| 5334 | iorder[2] = 1; |
| 5335 | iorder[3] = 0; |
| 5336 | } |
| 5337 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5338 | for (i = 0; i < len; i++) |
| 5339 | STORECHAR(PyUnicode_READ(kind, data, i)); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5340 | |
| 5341 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5342 | return v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5343 | #undef STORECHAR |
| 5344 | } |
| 5345 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5346 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5347 | PyUnicode_EncodeUTF32(const Py_UNICODE *s, |
| 5348 | Py_ssize_t size, |
| 5349 | const char *errors, |
| 5350 | int byteorder) |
| 5351 | { |
| 5352 | PyObject *result; |
| 5353 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 5354 | if (tmp == NULL) |
| 5355 | return NULL; |
| 5356 | result = _PyUnicode_EncodeUTF32(tmp, errors, byteorder); |
| 5357 | Py_DECREF(tmp); |
| 5358 | return result; |
| 5359 | } |
| 5360 | |
| 5361 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5362 | PyUnicode_AsUTF32String(PyObject *unicode) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5363 | { |
Victor Stinner | b960b34 | 2011-11-20 19:12:52 +0100 | [diff] [blame] | 5364 | return _PyUnicode_EncodeUTF32(unicode, NULL, 0); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5365 | } |
| 5366 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5367 | /* --- UTF-16 Codec ------------------------------------------------------- */ |
| 5368 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5369 | PyObject * |
| 5370 | PyUnicode_DecodeUTF16(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5371 | Py_ssize_t size, |
| 5372 | const char *errors, |
| 5373 | int *byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5374 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5375 | return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL); |
| 5376 | } |
| 5377 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5378 | /* Two masks for fast checking of whether a C 'long' may contain |
| 5379 | UTF16-encoded surrogate characters. This is an efficient heuristic, |
| 5380 | assuming that non-surrogate characters with a code point >= 0x8000 are |
| 5381 | rare in most input. |
| 5382 | FAST_CHAR_MASK is used when the input is in native byte ordering, |
| 5383 | SWAPPED_FAST_CHAR_MASK when the input is in byteswapped ordering. |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5384 | */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5385 | #if (SIZEOF_LONG == 8) |
| 5386 | # define FAST_CHAR_MASK 0x8000800080008000L |
| 5387 | # define SWAPPED_FAST_CHAR_MASK 0x0080008000800080L |
| 5388 | #elif (SIZEOF_LONG == 4) |
| 5389 | # define FAST_CHAR_MASK 0x80008000L |
| 5390 | # define SWAPPED_FAST_CHAR_MASK 0x00800080L |
| 5391 | #else |
| 5392 | # error C 'long' size should be either 4 or 8! |
| 5393 | #endif |
| 5394 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5395 | PyObject * |
| 5396 | PyUnicode_DecodeUTF16Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5397 | Py_ssize_t size, |
| 5398 | const char *errors, |
| 5399 | int *byteorder, |
| 5400 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5401 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5402 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5403 | Py_ssize_t startinpos; |
| 5404 | Py_ssize_t endinpos; |
| 5405 | Py_ssize_t outpos; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5406 | PyObject *unicode; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5407 | const unsigned char *q, *e, *aligned_end; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5408 | int bo = 0; /* assume native ordering by default */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5409 | int native_ordering = 0; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 5410 | const char *errmsg = ""; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5411 | /* Offsets from q for retrieving byte pairs in the right order. */ |
| 5412 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5413 | int ihi = 1, ilo = 0; |
| 5414 | #else |
| 5415 | int ihi = 0, ilo = 1; |
| 5416 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5417 | PyObject *errorHandler = NULL; |
| 5418 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5419 | |
| 5420 | /* Note: size will always be longer than the resulting Unicode |
| 5421 | character count */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5422 | unicode = PyUnicode_New(size, 127); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5423 | if (!unicode) |
| 5424 | return NULL; |
| 5425 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5426 | return unicode; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5427 | outpos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5428 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5429 | q = (unsigned char *)s; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5430 | e = q + size - 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5431 | |
| 5432 | if (byteorder) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5433 | bo = *byteorder; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5434 | |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5435 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 5436 | byte order setting accordingly. In native mode, the leading BOM |
| 5437 | mark is skipped, in all other modes, it is copied to the output |
| 5438 | stream as-is (giving a ZWNBSP character). */ |
| 5439 | if (bo == 0) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5440 | if (size >= 2) { |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 5441 | const Py_UCS4 bom = (q[ihi] << 8) | q[ilo]; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5442 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5443 | if (bom == 0xFEFF) { |
| 5444 | q += 2; |
| 5445 | bo = -1; |
| 5446 | } |
| 5447 | else if (bom == 0xFFFE) { |
| 5448 | q += 2; |
| 5449 | bo = 1; |
| 5450 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5451 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5452 | if (bom == 0xFEFF) { |
| 5453 | q += 2; |
| 5454 | bo = 1; |
| 5455 | } |
| 5456 | else if (bom == 0xFFFE) { |
| 5457 | q += 2; |
| 5458 | bo = -1; |
| 5459 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5460 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5461 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5462 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5463 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5464 | if (bo == -1) { |
| 5465 | /* force LE */ |
| 5466 | ihi = 1; |
| 5467 | ilo = 0; |
| 5468 | } |
| 5469 | else if (bo == 1) { |
| 5470 | /* force BE */ |
| 5471 | ihi = 0; |
| 5472 | ilo = 1; |
| 5473 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5474 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5475 | native_ordering = ilo < ihi; |
| 5476 | #else |
| 5477 | native_ordering = ilo > ihi; |
| 5478 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5479 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5480 | aligned_end = (const unsigned char *) ((size_t) e & ~LONG_PTR_MASK); |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5481 | while (q < e) { |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 5482 | Py_UCS4 ch; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5483 | /* First check for possible aligned read of a C 'long'. Unaligned |
| 5484 | reads are more expensive, better to defer to another iteration. */ |
| 5485 | if (!((size_t) q & LONG_PTR_MASK)) { |
| 5486 | /* Fast path for runs of non-surrogate chars. */ |
| 5487 | register const unsigned char *_q = q; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5488 | int kind = PyUnicode_KIND(unicode); |
| 5489 | void *data = PyUnicode_DATA(unicode); |
| 5490 | while (_q < aligned_end) { |
| 5491 | unsigned long block = * (unsigned long *) _q; |
| 5492 | unsigned short *pblock = (unsigned short*)█ |
| 5493 | Py_UCS4 maxch; |
| 5494 | if (native_ordering) { |
| 5495 | /* Can use buffer directly */ |
| 5496 | if (block & FAST_CHAR_MASK) |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5497 | break; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5498 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5499 | else { |
| 5500 | /* Need to byte-swap */ |
| 5501 | unsigned char *_p = (unsigned char*)pblock; |
| 5502 | if (block & SWAPPED_FAST_CHAR_MASK) |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5503 | break; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5504 | _p[0] = _q[1]; |
| 5505 | _p[1] = _q[0]; |
| 5506 | _p[2] = _q[3]; |
| 5507 | _p[3] = _q[2]; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5508 | #if (SIZEOF_LONG == 8) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5509 | _p[4] = _q[5]; |
| 5510 | _p[5] = _q[4]; |
| 5511 | _p[6] = _q[7]; |
| 5512 | _p[7] = _q[6]; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5513 | #endif |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5514 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5515 | maxch = Py_MAX(pblock[0], pblock[1]); |
| 5516 | #if SIZEOF_LONG == 8 |
| 5517 | maxch = Py_MAX(maxch, Py_MAX(pblock[2], pblock[3])); |
| 5518 | #endif |
| 5519 | if (maxch > PyUnicode_MAX_CHAR_VALUE(unicode)) { |
| 5520 | if (unicode_widen(&unicode, maxch) < 0) |
| 5521 | goto onError; |
| 5522 | kind = PyUnicode_KIND(unicode); |
| 5523 | data = PyUnicode_DATA(unicode); |
| 5524 | } |
| 5525 | PyUnicode_WRITE(kind, data, outpos++, pblock[0]); |
| 5526 | PyUnicode_WRITE(kind, data, outpos++, pblock[1]); |
| 5527 | #if SIZEOF_LONG == 8 |
| 5528 | PyUnicode_WRITE(kind, data, outpos++, pblock[2]); |
| 5529 | PyUnicode_WRITE(kind, data, outpos++, pblock[3]); |
| 5530 | #endif |
| 5531 | _q += SIZEOF_LONG; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5532 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5533 | q = _q; |
| 5534 | if (q >= e) |
| 5535 | break; |
| 5536 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5537 | ch = (q[ihi] << 8) | q[ilo]; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5538 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5539 | q += 2; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5540 | |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 5541 | if (!Py_UNICODE_IS_SURROGATE(ch)) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5542 | if (unicode_putchar(&unicode, &outpos, ch) < 0) |
| 5543 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5544 | continue; |
| 5545 | } |
| 5546 | |
| 5547 | /* UTF-16 code pair: */ |
| 5548 | if (q > e) { |
| 5549 | errmsg = "unexpected end of data"; |
| 5550 | startinpos = (((const char *)q) - 2) - starts; |
| 5551 | endinpos = ((const char *)e) + 1 - starts; |
| 5552 | goto utf16Error; |
| 5553 | } |
Victor Stinner | 2e9cfad | 2011-11-20 18:40:27 +0100 | [diff] [blame] | 5554 | if (Py_UNICODE_IS_HIGH_SURROGATE(ch)) { |
| 5555 | Py_UCS4 ch2 = (q[ihi] << 8) | q[ilo]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5556 | q += 2; |
Victor Stinner | 2e9cfad | 2011-11-20 18:40:27 +0100 | [diff] [blame] | 5557 | if (Py_UNICODE_IS_LOW_SURROGATE(ch2)) { |
Victor Stinner | 62aa4d0 | 2011-11-09 00:03:45 +0100 | [diff] [blame] | 5558 | if (unicode_putchar(&unicode, &outpos, |
Victor Stinner | 2e9cfad | 2011-11-20 18:40:27 +0100 | [diff] [blame] | 5559 | Py_UNICODE_JOIN_SURROGATES(ch, ch2)) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5560 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5561 | continue; |
| 5562 | } |
| 5563 | else { |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5564 | errmsg = "illegal UTF-16 surrogate"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5565 | startinpos = (((const char *)q)-4)-starts; |
| 5566 | endinpos = startinpos+2; |
| 5567 | goto utf16Error; |
| 5568 | } |
| 5569 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5570 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5571 | errmsg = "illegal encoding"; |
| 5572 | startinpos = (((const char *)q)-2)-starts; |
| 5573 | endinpos = startinpos+2; |
| 5574 | /* Fall through to report the error */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5575 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5576 | utf16Error: |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5577 | if (unicode_decode_call_errorhandler( |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5578 | errors, |
| 5579 | &errorHandler, |
| 5580 | "utf16", errmsg, |
| 5581 | &starts, |
| 5582 | (const char **)&e, |
| 5583 | &startinpos, |
| 5584 | &endinpos, |
| 5585 | &exc, |
| 5586 | (const char **)&q, |
| 5587 | &unicode, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5588 | &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5589 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5590 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5591 | /* remaining byte at the end? (size should be even) */ |
| 5592 | if (e == q) { |
| 5593 | if (!consumed) { |
| 5594 | errmsg = "truncated data"; |
| 5595 | startinpos = ((const char *)q) - starts; |
| 5596 | endinpos = ((const char *)e) + 1 - starts; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5597 | if (unicode_decode_call_errorhandler( |
| 5598 | errors, |
| 5599 | &errorHandler, |
| 5600 | "utf16", errmsg, |
| 5601 | &starts, |
| 5602 | (const char **)&e, |
| 5603 | &startinpos, |
| 5604 | &endinpos, |
| 5605 | &exc, |
| 5606 | (const char **)&q, |
| 5607 | &unicode, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5608 | &outpos)) |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5609 | goto onError; |
| 5610 | /* The remaining input chars are ignored if the callback |
| 5611 | chooses to skip the input */ |
| 5612 | } |
| 5613 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5614 | |
| 5615 | if (byteorder) |
| 5616 | *byteorder = bo; |
| 5617 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5618 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5619 | *consumed = (const char *)q-starts; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5620 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5621 | /* Adjust length */ |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 5622 | if (unicode_resize(&unicode, outpos) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5623 | goto onError; |
| 5624 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5625 | Py_XDECREF(errorHandler); |
| 5626 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 5627 | return unicode_result(unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5628 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5629 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5630 | Py_DECREF(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5631 | Py_XDECREF(errorHandler); |
| 5632 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5633 | return NULL; |
| 5634 | } |
| 5635 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5636 | #undef FAST_CHAR_MASK |
| 5637 | #undef SWAPPED_FAST_CHAR_MASK |
| 5638 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5639 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5640 | _PyUnicode_EncodeUTF16(PyObject *str, |
| 5641 | const char *errors, |
| 5642 | int byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5643 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5644 | int kind; |
| 5645 | void *data; |
| 5646 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5647 | PyObject *v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5648 | unsigned char *p; |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5649 | Py_ssize_t nsize, bytesize; |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5650 | Py_ssize_t i, pairs; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5651 | /* Offsets from p for storing byte pairs in the right order. */ |
| 5652 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5653 | int ihi = 1, ilo = 0; |
| 5654 | #else |
| 5655 | int ihi = 0, ilo = 1; |
| 5656 | #endif |
| 5657 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5658 | #define STORECHAR(CH) \ |
| 5659 | do { \ |
| 5660 | p[ihi] = ((CH) >> 8) & 0xff; \ |
| 5661 | p[ilo] = (CH) & 0xff; \ |
| 5662 | p += 2; \ |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5663 | } while(0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5664 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5665 | if (!PyUnicode_Check(str)) { |
| 5666 | PyErr_BadArgument(); |
| 5667 | return NULL; |
| 5668 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 5669 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5670 | return NULL; |
| 5671 | kind = PyUnicode_KIND(str); |
| 5672 | data = PyUnicode_DATA(str); |
| 5673 | len = PyUnicode_GET_LENGTH(str); |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 5674 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5675 | pairs = 0; |
| 5676 | if (kind == PyUnicode_4BYTE_KIND) |
| 5677 | for (i = 0; i < len; i++) |
| 5678 | if (PyUnicode_READ(kind, data, i) >= 0x10000) |
| 5679 | pairs++; |
| 5680 | /* 2 * (len + pairs + (byteorder == 0)) */ |
| 5681 | if (len > PY_SSIZE_T_MAX - pairs - (byteorder == 0)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5682 | return PyErr_NoMemory(); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5683 | nsize = len + pairs + (byteorder == 0); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5684 | bytesize = nsize * 2; |
| 5685 | if (bytesize / 2 != nsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5686 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5687 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5688 | if (v == NULL) |
| 5689 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5690 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5691 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5692 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5693 | STORECHAR(0xFEFF); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5694 | if (len == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5695 | goto done; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5696 | |
| 5697 | if (byteorder == -1) { |
| 5698 | /* force LE */ |
| 5699 | ihi = 1; |
| 5700 | ilo = 0; |
| 5701 | } |
| 5702 | else if (byteorder == 1) { |
| 5703 | /* force BE */ |
| 5704 | ihi = 0; |
| 5705 | ilo = 1; |
| 5706 | } |
| 5707 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5708 | for (i = 0; i < len; i++) { |
| 5709 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 5710 | Py_UCS4 ch2 = 0; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5711 | if (ch >= 0x10000) { |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 5712 | ch2 = Py_UNICODE_LOW_SURROGATE(ch); |
| 5713 | ch = Py_UNICODE_HIGH_SURROGATE(ch); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5714 | } |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5715 | STORECHAR(ch); |
| 5716 | if (ch2) |
| 5717 | STORECHAR(ch2); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5718 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5719 | |
| 5720 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5721 | return v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5722 | #undef STORECHAR |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5723 | } |
| 5724 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5725 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5726 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
| 5727 | Py_ssize_t size, |
| 5728 | const char *errors, |
| 5729 | int byteorder) |
| 5730 | { |
| 5731 | PyObject *result; |
| 5732 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 5733 | if (tmp == NULL) |
| 5734 | return NULL; |
| 5735 | result = _PyUnicode_EncodeUTF16(tmp, errors, byteorder); |
| 5736 | Py_DECREF(tmp); |
| 5737 | return result; |
| 5738 | } |
| 5739 | |
| 5740 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5741 | PyUnicode_AsUTF16String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5742 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5743 | return _PyUnicode_EncodeUTF16(unicode, NULL, 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5744 | } |
| 5745 | |
| 5746 | /* --- Unicode Escape Codec ----------------------------------------------- */ |
| 5747 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5748 | /* Helper function for PyUnicode_DecodeUnicodeEscape, determines |
| 5749 | if all the escapes in the string make it still a valid ASCII string. |
| 5750 | Returns -1 if any escapes were found which cause the string to |
| 5751 | pop out of ASCII range. Otherwise returns the length of the |
| 5752 | required buffer to hold the string. |
| 5753 | */ |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 5754 | static Py_ssize_t |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5755 | length_of_escaped_ascii_string(const char *s, Py_ssize_t size) |
| 5756 | { |
| 5757 | const unsigned char *p = (const unsigned char *)s; |
| 5758 | const unsigned char *end = p + size; |
| 5759 | Py_ssize_t length = 0; |
| 5760 | |
| 5761 | if (size < 0) |
| 5762 | return -1; |
| 5763 | |
| 5764 | for (; p < end; ++p) { |
| 5765 | if (*p > 127) { |
| 5766 | /* Non-ASCII */ |
| 5767 | return -1; |
| 5768 | } |
| 5769 | else if (*p != '\\') { |
| 5770 | /* Normal character */ |
| 5771 | ++length; |
| 5772 | } |
| 5773 | else { |
| 5774 | /* Backslash-escape, check next char */ |
| 5775 | ++p; |
| 5776 | /* Escape sequence reaches till end of string or |
| 5777 | non-ASCII follow-up. */ |
| 5778 | if (p >= end || *p > 127) |
| 5779 | return -1; |
| 5780 | switch (*p) { |
| 5781 | case '\n': |
| 5782 | /* backslash + \n result in zero characters */ |
| 5783 | break; |
| 5784 | case '\\': case '\'': case '\"': |
| 5785 | case 'b': case 'f': case 't': |
| 5786 | case 'n': case 'r': case 'v': case 'a': |
| 5787 | ++length; |
| 5788 | break; |
| 5789 | case '0': case '1': case '2': case '3': |
| 5790 | case '4': case '5': case '6': case '7': |
| 5791 | case 'x': case 'u': case 'U': case 'N': |
| 5792 | /* these do not guarantee ASCII characters */ |
| 5793 | return -1; |
| 5794 | default: |
| 5795 | /* count the backslash + the other character */ |
| 5796 | length += 2; |
| 5797 | } |
| 5798 | } |
| 5799 | } |
| 5800 | return length; |
| 5801 | } |
| 5802 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 5803 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; |
Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 5804 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5805 | PyObject * |
| 5806 | PyUnicode_DecodeUnicodeEscape(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5807 | Py_ssize_t size, |
Victor Stinner | c17f540 | 2011-09-29 00:16:58 +0200 | [diff] [blame] | 5808 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5809 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5810 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5811 | Py_ssize_t startinpos; |
| 5812 | Py_ssize_t endinpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5813 | int j; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5814 | PyObject *v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5815 | const char *end; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5816 | char* message; |
| 5817 | Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5818 | PyObject *errorHandler = NULL; |
| 5819 | PyObject *exc = NULL; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5820 | Py_ssize_t len; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5821 | Py_ssize_t i; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5822 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5823 | len = length_of_escaped_ascii_string(s, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5824 | |
| 5825 | /* After length_of_escaped_ascii_string() there are two alternatives, |
| 5826 | either the string is pure ASCII with named escapes like \n, etc. |
| 5827 | and we determined it's exact size (common case) |
| 5828 | or it contains \x, \u, ... escape sequences. then we create a |
| 5829 | 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] | 5830 | if (len >= 0) { |
| 5831 | v = PyUnicode_New(len, 127); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5832 | if (!v) |
| 5833 | goto onError; |
| 5834 | assert(PyUnicode_KIND(v) == PyUnicode_1BYTE_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5835 | } |
| 5836 | else { |
| 5837 | /* Escaped strings will always be longer than the resulting |
| 5838 | Unicode string, so we start with size here and then reduce the |
| 5839 | length after conversion to the true value. |
| 5840 | (but if the error callback returns a long replacement string |
| 5841 | we'll have to allocate more space) */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5842 | v = PyUnicode_New(size, 127); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5843 | if (!v) |
| 5844 | goto onError; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5845 | len = size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5846 | } |
| 5847 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5848 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5849 | return v; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5850 | i = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5851 | end = s + size; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5852 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5853 | while (s < end) { |
| 5854 | unsigned char c; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 5855 | Py_UCS4 x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5856 | int digits; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5857 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5858 | /* The only case in which i == ascii_length is a backslash |
| 5859 | followed by a newline. */ |
| 5860 | assert(i <= len); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5861 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5862 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 5863 | if (*s != '\\') { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5864 | if (unicode_putchar(&v, &i, (unsigned char) *s++) < 0) |
| 5865 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5866 | continue; |
| 5867 | } |
| 5868 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5869 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5870 | /* \ - Escapes */ |
| 5871 | s++; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5872 | c = *s++; |
| 5873 | if (s > end) |
| 5874 | c = '\0'; /* Invalid after \ */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5875 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5876 | /* The only case in which i == ascii_length is a backslash |
| 5877 | followed by a newline. */ |
| 5878 | assert(i < len || (i == len && c == '\n')); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5879 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5880 | switch (c) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5881 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5882 | /* \x escapes */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5883 | #define WRITECHAR(ch) \ |
| 5884 | do { \ |
| 5885 | if (unicode_putchar(&v, &i, ch) < 0) \ |
| 5886 | goto onError; \ |
| 5887 | }while(0) |
| 5888 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5889 | case '\n': break; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5890 | case '\\': WRITECHAR('\\'); break; |
| 5891 | case '\'': WRITECHAR('\''); break; |
| 5892 | case '\"': WRITECHAR('\"'); break; |
| 5893 | case 'b': WRITECHAR('\b'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5894 | /* FF */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5895 | case 'f': WRITECHAR('\014'); break; |
| 5896 | case 't': WRITECHAR('\t'); break; |
| 5897 | case 'n': WRITECHAR('\n'); break; |
| 5898 | case 'r': WRITECHAR('\r'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5899 | /* VT */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5900 | case 'v': WRITECHAR('\013'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5901 | /* BEL, not classic C */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5902 | case 'a': WRITECHAR('\007'); break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5903 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5904 | /* \OOO (octal) escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5905 | case '0': case '1': case '2': case '3': |
| 5906 | case '4': case '5': case '6': case '7': |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5907 | x = s[-1] - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5908 | if (s < end && '0' <= *s && *s <= '7') { |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5909 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5910 | if (s < end && '0' <= *s && *s <= '7') |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5911 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5912 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5913 | WRITECHAR(x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5914 | break; |
| 5915 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5916 | /* hex escapes */ |
| 5917 | /* \xXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5918 | case 'x': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5919 | digits = 2; |
| 5920 | message = "truncated \\xXX escape"; |
| 5921 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5922 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5923 | /* \uXXXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5924 | case 'u': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5925 | digits = 4; |
| 5926 | message = "truncated \\uXXXX escape"; |
| 5927 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5928 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5929 | /* \UXXXXXXXX */ |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5930 | case 'U': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5931 | digits = 8; |
| 5932 | message = "truncated \\UXXXXXXXX escape"; |
| 5933 | hexescape: |
| 5934 | chr = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5935 | if (s+digits>end) { |
| 5936 | endinpos = size; |
| 5937 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5938 | errors, &errorHandler, |
| 5939 | "unicodeescape", "end of string in escape sequence", |
| 5940 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5941 | &v, &i)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5942 | goto onError; |
| 5943 | goto nextByte; |
| 5944 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5945 | for (j = 0; j < digits; ++j) { |
| 5946 | c = (unsigned char) s[j]; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 5947 | if (!Py_ISXDIGIT(c)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5948 | endinpos = (s+j+1)-starts; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5949 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5950 | errors, &errorHandler, |
| 5951 | "unicodeescape", message, |
| 5952 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5953 | &v, &i)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5954 | goto onError; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5955 | len = PyUnicode_GET_LENGTH(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5956 | goto nextByte; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5957 | } |
| 5958 | chr = (chr<<4) & ~0xF; |
| 5959 | if (c >= '0' && c <= '9') |
| 5960 | chr += c - '0'; |
| 5961 | else if (c >= 'a' && c <= 'f') |
| 5962 | chr += 10 + c - 'a'; |
| 5963 | else |
| 5964 | chr += 10 + c - 'A'; |
| 5965 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5966 | s += j; |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 5967 | if (chr == 0xffffffff && PyErr_Occurred()) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5968 | /* _decoding_error will have already written into the |
| 5969 | target buffer. */ |
| 5970 | break; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5971 | store: |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5972 | /* when we get here, chr is a 32-bit unicode character */ |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 5973 | if (chr <= MAX_UNICODE) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5974 | WRITECHAR(chr); |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5975 | } else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5976 | endinpos = s-starts; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5977 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5978 | errors, &errorHandler, |
| 5979 | "unicodeescape", "illegal Unicode character", |
| 5980 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5981 | &v, &i)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5982 | goto onError; |
| 5983 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5984 | break; |
| 5985 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5986 | /* \N{name} */ |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5987 | case 'N': |
| 5988 | message = "malformed \\N character escape"; |
| 5989 | if (ucnhash_CAPI == NULL) { |
| 5990 | /* load the unicode data module */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5991 | ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCapsule_Import( |
| 5992 | PyUnicodeData_CAPSULE_NAME, 1); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5993 | if (ucnhash_CAPI == NULL) |
| 5994 | goto ucnhashError; |
| 5995 | } |
| 5996 | if (*s == '{') { |
| 5997 | const char *start = s+1; |
| 5998 | /* look for the closing brace */ |
| 5999 | while (*s != '}' && s < end) |
| 6000 | s++; |
| 6001 | if (s > start && s < end && *s == '}') { |
| 6002 | /* found a name. look it up in the unicode database */ |
| 6003 | message = "unknown Unicode character name"; |
| 6004 | s++; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6005 | if (ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), |
Ezio Melotti | 931b8aa | 2011-10-21 21:57:36 +0300 | [diff] [blame] | 6006 | &chr, 0)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 6007 | goto store; |
| 6008 | } |
| 6009 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6010 | endinpos = s-starts; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6011 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6012 | errors, &errorHandler, |
| 6013 | "unicodeescape", message, |
| 6014 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6015 | &v, &i)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 6016 | goto onError; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 6017 | break; |
| 6018 | |
| 6019 | default: |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 6020 | if (s > end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6021 | message = "\\ at end of string"; |
| 6022 | s--; |
| 6023 | endinpos = s-starts; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6024 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6025 | errors, &errorHandler, |
| 6026 | "unicodeescape", message, |
| 6027 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6028 | &v, &i)) |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 6029 | goto onError; |
| 6030 | } |
| 6031 | else { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6032 | WRITECHAR('\\'); |
| 6033 | WRITECHAR(s[-1]); |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 6034 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 6035 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6036 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6037 | nextByte: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6038 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6039 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6040 | #undef WRITECHAR |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6041 | |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 6042 | if (unicode_resize(&v, i) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6043 | goto onError; |
Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 6044 | Py_XDECREF(errorHandler); |
| 6045 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 6046 | return unicode_result(v); |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 6047 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6048 | ucnhashError: |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 6049 | PyErr_SetString( |
| 6050 | PyExc_UnicodeError, |
| 6051 | "\\N escapes not supported (can't load unicodedata module)" |
| 6052 | ); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 6053 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6054 | Py_XDECREF(errorHandler); |
| 6055 | Py_XDECREF(exc); |
Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 6056 | return NULL; |
| 6057 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6058 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6059 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6060 | Py_XDECREF(errorHandler); |
| 6061 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6062 | return NULL; |
| 6063 | } |
| 6064 | |
| 6065 | /* Return a Unicode-Escape string version of the Unicode object. |
| 6066 | |
| 6067 | If quotes is true, the string is enclosed in u"" or u'' quotes as |
| 6068 | appropriate. |
| 6069 | |
| 6070 | */ |
| 6071 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6072 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6073 | PyUnicode_AsUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6074 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6075 | Py_ssize_t i, len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6076 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6077 | char *p; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6078 | int kind; |
| 6079 | void *data; |
| 6080 | Py_ssize_t expandsize = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6081 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6082 | /* Initial allocation is based on the longest-possible unichr |
| 6083 | escape. |
| 6084 | |
| 6085 | In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source |
| 6086 | unichr, so in this case it's the longest unichr escape. In |
| 6087 | narrow (UTF-16) builds this is five chars per source unichr |
| 6088 | since there are two unichrs in the surrogate pair, so in narrow |
| 6089 | (UTF-16) builds it's not the longest unichr escape. |
| 6090 | |
| 6091 | In wide or narrow builds '\uxxxx' is 6 chars per source unichr, |
| 6092 | so in the narrow (UTF-16) build case it's the longest unichr |
| 6093 | escape. |
| 6094 | */ |
| 6095 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6096 | if (!PyUnicode_Check(unicode)) { |
| 6097 | PyErr_BadArgument(); |
| 6098 | return NULL; |
| 6099 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6100 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6101 | return NULL; |
| 6102 | len = PyUnicode_GET_LENGTH(unicode); |
| 6103 | kind = PyUnicode_KIND(unicode); |
| 6104 | data = PyUnicode_DATA(unicode); |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 6105 | switch (kind) { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6106 | case PyUnicode_1BYTE_KIND: expandsize = 4; break; |
| 6107 | case PyUnicode_2BYTE_KIND: expandsize = 6; break; |
| 6108 | case PyUnicode_4BYTE_KIND: expandsize = 10; break; |
| 6109 | } |
| 6110 | |
| 6111 | if (len == 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6112 | return PyBytes_FromStringAndSize(NULL, 0); |
| 6113 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6114 | if (len > (PY_SSIZE_T_MAX - 2 - 1) / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6115 | return PyErr_NoMemory(); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 6116 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6117 | repr = PyBytes_FromStringAndSize(NULL, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6118 | 2 |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6119 | + expandsize*len |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6120 | + 1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6121 | if (repr == NULL) |
| 6122 | return NULL; |
| 6123 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6124 | p = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6125 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6126 | for (i = 0; i < len; i++) { |
Victor Stinner | 3326cb6 | 2011-11-10 20:15:25 +0100 | [diff] [blame] | 6127 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 6128 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 6129 | /* Escape backslashes */ |
| 6130 | if (ch == '\\') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6131 | *p++ = '\\'; |
| 6132 | *p++ = (char) ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 6133 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6134 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 6135 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 6136 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 6137 | else if (ch >= 0x10000) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 6138 | assert(ch <= MAX_UNICODE); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 6139 | *p++ = '\\'; |
| 6140 | *p++ = 'U'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6141 | *p++ = Py_hexdigits[(ch >> 28) & 0x0000000F]; |
| 6142 | *p++ = Py_hexdigits[(ch >> 24) & 0x0000000F]; |
| 6143 | *p++ = Py_hexdigits[(ch >> 20) & 0x0000000F]; |
| 6144 | *p++ = Py_hexdigits[(ch >> 16) & 0x0000000F]; |
| 6145 | *p++ = Py_hexdigits[(ch >> 12) & 0x0000000F]; |
| 6146 | *p++ = Py_hexdigits[(ch >> 8) & 0x0000000F]; |
| 6147 | *p++ = Py_hexdigits[(ch >> 4) & 0x0000000F]; |
| 6148 | *p++ = Py_hexdigits[ch & 0x0000000F]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6149 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 6150 | } |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 6151 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6152 | /* Map 16-bit characters to '\uxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 6153 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6154 | *p++ = '\\'; |
| 6155 | *p++ = 'u'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6156 | *p++ = Py_hexdigits[(ch >> 12) & 0x000F]; |
| 6157 | *p++ = Py_hexdigits[(ch >> 8) & 0x000F]; |
| 6158 | *p++ = Py_hexdigits[(ch >> 4) & 0x000F]; |
| 6159 | *p++ = Py_hexdigits[ch & 0x000F]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6160 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 6161 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 6162 | /* Map special whitespace to '\t', \n', '\r' */ |
| 6163 | else if (ch == '\t') { |
| 6164 | *p++ = '\\'; |
| 6165 | *p++ = 't'; |
| 6166 | } |
| 6167 | else if (ch == '\n') { |
| 6168 | *p++ = '\\'; |
| 6169 | *p++ = 'n'; |
| 6170 | } |
| 6171 | else if (ch == '\r') { |
| 6172 | *p++ = '\\'; |
| 6173 | *p++ = 'r'; |
| 6174 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 6175 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 6176 | /* Map non-printable US ASCII to '\xhh' */ |
Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 6177 | else if (ch < ' ' || ch >= 0x7F) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6178 | *p++ = '\\'; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 6179 | *p++ = 'x'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6180 | *p++ = Py_hexdigits[(ch >> 4) & 0x000F]; |
| 6181 | *p++ = Py_hexdigits[ch & 0x000F]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6182 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 6183 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6184 | /* Copy everything else as-is */ |
| 6185 | else |
| 6186 | *p++ = (char) ch; |
| 6187 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6188 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6189 | assert(p - PyBytes_AS_STRING(repr) > 0); |
| 6190 | if (_PyBytes_Resize(&repr, p - PyBytes_AS_STRING(repr)) < 0) |
| 6191 | return NULL; |
| 6192 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6193 | } |
| 6194 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6195 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6196 | PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, |
| 6197 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6198 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6199 | PyObject *result; |
| 6200 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 6201 | if (tmp == NULL) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6202 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6203 | result = PyUnicode_AsUnicodeEscapeString(tmp); |
| 6204 | Py_DECREF(tmp); |
| 6205 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6206 | } |
| 6207 | |
| 6208 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ |
| 6209 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6210 | PyObject * |
| 6211 | PyUnicode_DecodeRawUnicodeEscape(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6212 | Py_ssize_t size, |
| 6213 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6214 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6215 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6216 | Py_ssize_t startinpos; |
| 6217 | Py_ssize_t endinpos; |
| 6218 | Py_ssize_t outpos; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6219 | PyObject *v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6220 | const char *end; |
| 6221 | const char *bs; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6222 | PyObject *errorHandler = NULL; |
| 6223 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6224 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6225 | /* Escaped strings will always be longer than the resulting |
| 6226 | 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] | 6227 | length after conversion to the true value. (But decoding error |
| 6228 | handler might have to resize the string) */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6229 | v = PyUnicode_New(size, 127); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6230 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6231 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6232 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6233 | return v; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6234 | outpos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6235 | end = s + size; |
| 6236 | while (s < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6237 | unsigned char c; |
| 6238 | Py_UCS4 x; |
| 6239 | int i; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6240 | int count; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6241 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6242 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 6243 | if (*s != '\\') { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6244 | if (unicode_putchar(&v, &outpos, (unsigned char)*s++) < 0) |
| 6245 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6246 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6247 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6248 | startinpos = s-starts; |
| 6249 | |
| 6250 | /* \u-escapes are only interpreted iff the number of leading |
| 6251 | backslashes if odd */ |
| 6252 | bs = s; |
| 6253 | for (;s < end;) { |
| 6254 | if (*s != '\\') |
| 6255 | break; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6256 | if (unicode_putchar(&v, &outpos, (unsigned char)*s++) < 0) |
| 6257 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6258 | } |
| 6259 | if (((s - bs) & 1) == 0 || |
| 6260 | s >= end || |
| 6261 | (*s != 'u' && *s != 'U')) { |
| 6262 | continue; |
| 6263 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6264 | outpos--; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6265 | count = *s=='u' ? 4 : 8; |
| 6266 | s++; |
| 6267 | |
| 6268 | /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6269 | for (x = 0, i = 0; i < count; ++i, ++s) { |
| 6270 | c = (unsigned char)*s; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 6271 | if (!Py_ISXDIGIT(c)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6272 | endinpos = s-starts; |
| 6273 | if (unicode_decode_call_errorhandler( |
| 6274 | errors, &errorHandler, |
| 6275 | "rawunicodeescape", "truncated \\uXXXX", |
| 6276 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6277 | &v, &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6278 | goto onError; |
| 6279 | goto nextByte; |
| 6280 | } |
| 6281 | x = (x<<4) & ~0xF; |
| 6282 | if (c >= '0' && c <= '9') |
| 6283 | x += c - '0'; |
| 6284 | else if (c >= 'a' && c <= 'f') |
| 6285 | x += 10 + c - 'a'; |
| 6286 | else |
| 6287 | x += 10 + c - 'A'; |
| 6288 | } |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 6289 | if (x <= MAX_UNICODE) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6290 | if (unicode_putchar(&v, &outpos, x) < 0) |
| 6291 | goto onError; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 6292 | } else { |
| 6293 | endinpos = s-starts; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6294 | if (unicode_decode_call_errorhandler( |
| 6295 | errors, &errorHandler, |
| 6296 | "rawunicodeescape", "\\Uxxxxxxxx out of range", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6297 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6298 | &v, &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6299 | goto onError; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6300 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6301 | nextByte: |
| 6302 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6303 | } |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 6304 | if (unicode_resize(&v, outpos) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6305 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6306 | Py_XDECREF(errorHandler); |
| 6307 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 6308 | return unicode_result(v); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6309 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6310 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6311 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6312 | Py_XDECREF(errorHandler); |
| 6313 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6314 | return NULL; |
| 6315 | } |
| 6316 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6317 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6318 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6319 | PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6320 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6321 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6322 | char *p; |
| 6323 | char *q; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6324 | Py_ssize_t expandsize, pos; |
| 6325 | int kind; |
| 6326 | void *data; |
| 6327 | Py_ssize_t len; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6328 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6329 | if (!PyUnicode_Check(unicode)) { |
| 6330 | PyErr_BadArgument(); |
| 6331 | return NULL; |
| 6332 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6333 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6334 | return NULL; |
| 6335 | kind = PyUnicode_KIND(unicode); |
| 6336 | data = PyUnicode_DATA(unicode); |
| 6337 | len = PyUnicode_GET_LENGTH(unicode); |
Benjamin Peterson | 1518e87 | 2011-11-23 10:44:52 -0600 | [diff] [blame] | 6338 | /* 4 byte characters can take up 10 bytes, 2 byte characters can take up 6 |
| 6339 | bytes, and 1 byte characters 4. */ |
| 6340 | expandsize = kind * 2 + 2; |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 6341 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6342 | if (len > PY_SSIZE_T_MAX / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6343 | return PyErr_NoMemory(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6344 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6345 | repr = PyBytes_FromStringAndSize(NULL, expandsize * len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6346 | if (repr == NULL) |
| 6347 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6348 | if (len == 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6349 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6350 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6351 | p = q = PyBytes_AS_STRING(repr); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6352 | for (pos = 0; pos < len; pos++) { |
| 6353 | Py_UCS4 ch = PyUnicode_READ(kind, data, pos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6354 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 6355 | if (ch >= 0x10000) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 6356 | assert(ch <= MAX_UNICODE); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6357 | *p++ = '\\'; |
| 6358 | *p++ = 'U'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6359 | *p++ = Py_hexdigits[(ch >> 28) & 0xf]; |
| 6360 | *p++ = Py_hexdigits[(ch >> 24) & 0xf]; |
| 6361 | *p++ = Py_hexdigits[(ch >> 20) & 0xf]; |
| 6362 | *p++ = Py_hexdigits[(ch >> 16) & 0xf]; |
| 6363 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 6364 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 6365 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 6366 | *p++ = Py_hexdigits[ch & 15]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6367 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6368 | /* Map 16-bit characters to '\uxxxx' */ |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6369 | else if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6370 | *p++ = '\\'; |
| 6371 | *p++ = 'u'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6372 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 6373 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 6374 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 6375 | *p++ = Py_hexdigits[ch & 15]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6376 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6377 | /* Copy everything else as-is */ |
| 6378 | else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6379 | *p++ = (char) ch; |
| 6380 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6381 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6382 | assert(p > q); |
| 6383 | if (_PyBytes_Resize(&repr, p - q) < 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6384 | return NULL; |
| 6385 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6386 | } |
| 6387 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6388 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6389 | PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
| 6390 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6391 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6392 | PyObject *result; |
| 6393 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 6394 | if (tmp == NULL) |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 6395 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6396 | result = PyUnicode_AsRawUnicodeEscapeString(tmp); |
| 6397 | Py_DECREF(tmp); |
| 6398 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6399 | } |
| 6400 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6401 | /* --- Unicode Internal Codec ------------------------------------------- */ |
| 6402 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6403 | PyObject * |
| 6404 | _PyUnicode_DecodeUnicodeInternal(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6405 | Py_ssize_t size, |
| 6406 | const char *errors) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6407 | { |
| 6408 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6409 | Py_ssize_t startinpos; |
| 6410 | Py_ssize_t endinpos; |
| 6411 | Py_ssize_t outpos; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6412 | PyObject *v; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6413 | const char *end; |
| 6414 | const char *reason; |
| 6415 | PyObject *errorHandler = NULL; |
| 6416 | PyObject *exc = NULL; |
| 6417 | |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6418 | if (PyErr_WarnEx(PyExc_DeprecationWarning, |
Ezio Melotti | 11060a4 | 2011-11-16 09:39:10 +0200 | [diff] [blame] | 6419 | "unicode_internal codec has been deprecated", |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6420 | 1)) |
| 6421 | return NULL; |
| 6422 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6423 | /* XXX overflow detection missing */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6424 | 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] | 6425 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6426 | goto onError; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6427 | if (PyUnicode_GET_LENGTH(v) == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6428 | return v; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6429 | outpos = 0; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6430 | end = s + size; |
| 6431 | |
| 6432 | while (s < end) { |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6433 | Py_UNICODE uch; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6434 | Py_UCS4 ch; |
| 6435 | /* We copy the raw representation one byte at a time because the |
| 6436 | pointer may be unaligned (see test_codeccallbacks). */ |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6437 | ((char *) &uch)[0] = s[0]; |
| 6438 | ((char *) &uch)[1] = s[1]; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6439 | #ifdef Py_UNICODE_WIDE |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6440 | ((char *) &uch)[2] = s[2]; |
| 6441 | ((char *) &uch)[3] = s[3]; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6442 | #endif |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6443 | ch = uch; |
| 6444 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6445 | /* We have to sanity check the raw data, otherwise doom looms for |
| 6446 | some malformed UCS-4 data. */ |
| 6447 | if ( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6448 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6449 | ch > 0x10ffff || |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6450 | #endif |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6451 | end-s < Py_UNICODE_SIZE |
| 6452 | ) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6453 | { |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6454 | startinpos = s - starts; |
| 6455 | if (end-s < Py_UNICODE_SIZE) { |
| 6456 | endinpos = end-starts; |
| 6457 | reason = "truncated input"; |
| 6458 | } |
| 6459 | else { |
| 6460 | endinpos = s - starts + Py_UNICODE_SIZE; |
| 6461 | reason = "illegal code point (> 0x10FFFF)"; |
| 6462 | } |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6463 | if (unicode_decode_call_errorhandler( |
| 6464 | errors, &errorHandler, |
| 6465 | "unicode_internal", reason, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 6466 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6467 | &v, &outpos)) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6468 | goto onError; |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6469 | continue; |
| 6470 | } |
| 6471 | |
| 6472 | s += Py_UNICODE_SIZE; |
| 6473 | #ifndef Py_UNICODE_WIDE |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 6474 | if (Py_UNICODE_IS_HIGH_SURROGATE(ch) && s < end) |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6475 | { |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6476 | Py_UNICODE uch2; |
| 6477 | ((char *) &uch2)[0] = s[0]; |
| 6478 | ((char *) &uch2)[1] = s[1]; |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 6479 | if (Py_UNICODE_IS_LOW_SURROGATE(uch2)) |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6480 | { |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 6481 | ch = Py_UNICODE_JOIN_SURROGATES(uch, uch2); |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6482 | s += Py_UNICODE_SIZE; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6483 | } |
| 6484 | } |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6485 | #endif |
| 6486 | |
| 6487 | if (unicode_putchar(&v, &outpos, ch) < 0) |
| 6488 | goto onError; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6489 | } |
| 6490 | |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 6491 | if (unicode_resize(&v, outpos) < 0) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6492 | goto onError; |
| 6493 | Py_XDECREF(errorHandler); |
| 6494 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 6495 | return unicode_result(v); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6496 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6497 | onError: |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6498 | Py_XDECREF(v); |
| 6499 | Py_XDECREF(errorHandler); |
| 6500 | Py_XDECREF(exc); |
| 6501 | return NULL; |
| 6502 | } |
| 6503 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6504 | /* --- Latin-1 Codec ------------------------------------------------------ */ |
| 6505 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6506 | PyObject * |
| 6507 | PyUnicode_DecodeLatin1(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6508 | Py_ssize_t size, |
| 6509 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6510 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6511 | /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 6512 | return _PyUnicode_FromUCS1((unsigned char*)s, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6513 | } |
| 6514 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6515 | /* create or adjust a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6516 | static void |
| 6517 | make_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6518 | const char *encoding, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6519 | PyObject *unicode, |
| 6520 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6521 | const char *reason) |
| 6522 | { |
| 6523 | if (*exceptionObject == NULL) { |
| 6524 | *exceptionObject = PyObject_CallFunction( |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6525 | PyExc_UnicodeEncodeError, "sOnns", |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6526 | encoding, unicode, startpos, endpos, reason); |
| 6527 | } |
| 6528 | else { |
| 6529 | if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 6530 | goto onError; |
| 6531 | if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) |
| 6532 | goto onError; |
| 6533 | if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) |
| 6534 | goto onError; |
| 6535 | return; |
| 6536 | onError: |
| 6537 | Py_DECREF(*exceptionObject); |
| 6538 | *exceptionObject = NULL; |
| 6539 | } |
| 6540 | } |
| 6541 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6542 | /* raises a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6543 | static void |
| 6544 | raise_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6545 | const char *encoding, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6546 | PyObject *unicode, |
| 6547 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6548 | const char *reason) |
| 6549 | { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6550 | make_encode_exception(exceptionObject, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6551 | encoding, unicode, startpos, endpos, reason); |
| 6552 | if (*exceptionObject != NULL) |
| 6553 | PyCodec_StrictErrors(*exceptionObject); |
| 6554 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6555 | |
| 6556 | /* error handling callback helper: |
| 6557 | build arguments, call the callback and check the arguments, |
| 6558 | put the result into newpos and return the replacement string, which |
| 6559 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6560 | static PyObject * |
| 6561 | unicode_encode_call_errorhandler(const char *errors, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6562 | PyObject **errorHandler, |
| 6563 | const char *encoding, const char *reason, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6564 | PyObject *unicode, PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6565 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6566 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6567 | { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6568 | 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] | 6569 | Py_ssize_t len; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6570 | PyObject *restuple; |
| 6571 | PyObject *resunicode; |
| 6572 | |
| 6573 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6574 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6575 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6576 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6577 | } |
| 6578 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6579 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6580 | return NULL; |
| 6581 | len = PyUnicode_GET_LENGTH(unicode); |
| 6582 | |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6583 | make_encode_exception(exceptionObject, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6584 | encoding, unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6585 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6586 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6587 | |
| 6588 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6589 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6590 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6591 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6592 | if (!PyTuple_Check(restuple)) { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6593 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6594 | Py_DECREF(restuple); |
| 6595 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6596 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6597 | if (!PyArg_ParseTuple(restuple, argparse, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6598 | &resunicode, newpos)) { |
| 6599 | Py_DECREF(restuple); |
| 6600 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6601 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6602 | if (!PyUnicode_Check(resunicode) && !PyBytes_Check(resunicode)) { |
| 6603 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
| 6604 | Py_DECREF(restuple); |
| 6605 | return NULL; |
| 6606 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6607 | if (*newpos<0) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6608 | *newpos = len + *newpos; |
| 6609 | if (*newpos<0 || *newpos>len) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6610 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 6611 | Py_DECREF(restuple); |
| 6612 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 6613 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6614 | Py_INCREF(resunicode); |
| 6615 | Py_DECREF(restuple); |
| 6616 | return resunicode; |
| 6617 | } |
| 6618 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6619 | static PyObject * |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6620 | unicode_encode_ucs1(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6621 | const char *errors, |
Victor Stinner | fcd9653 | 2011-11-04 00:28:50 +0100 | [diff] [blame] | 6622 | unsigned int limit) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6623 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6624 | /* input state */ |
| 6625 | Py_ssize_t pos=0, size; |
| 6626 | int kind; |
| 6627 | void *data; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6628 | /* output object */ |
| 6629 | PyObject *res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6630 | /* pointer into the output */ |
| 6631 | char *str; |
| 6632 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6633 | Py_ssize_t ressize; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6634 | const char *encoding = (limit == 256) ? "latin-1" : "ascii"; |
| 6635 | 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] | 6636 | PyObject *errorHandler = NULL; |
| 6637 | PyObject *exc = NULL; |
| 6638 | /* the following variable is used for caching string comparisons |
| 6639 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 6640 | int known_errorHandler = -1; |
| 6641 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6642 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6643 | return NULL; |
| 6644 | size = PyUnicode_GET_LENGTH(unicode); |
| 6645 | kind = PyUnicode_KIND(unicode); |
| 6646 | data = PyUnicode_DATA(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6647 | /* allocate enough for a simple encoding without |
| 6648 | replacements, if we need more, we'll resize */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6649 | if (size == 0) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 6650 | return PyBytes_FromStringAndSize(NULL, 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6651 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6652 | if (res == NULL) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6653 | return NULL; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6654 | str = PyBytes_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6655 | ressize = size; |
| 6656 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6657 | while (pos < size) { |
| 6658 | Py_UCS4 c = PyUnicode_READ(kind, data, pos); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6659 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6660 | /* can we encode this? */ |
| 6661 | if (c<limit) { |
| 6662 | /* no overflow check, because we know that the space is enough */ |
| 6663 | *str++ = (char)c; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6664 | ++pos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6665 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6666 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6667 | Py_ssize_t requiredsize; |
| 6668 | PyObject *repunicode; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6669 | Py_ssize_t repsize, newpos, respos, i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6670 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6671 | Py_ssize_t collstart = pos; |
| 6672 | Py_ssize_t collend = pos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6673 | /* find all unecodable characters */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6674 | while ((collend < size) && (PyUnicode_READ(kind, data, collend)>=limit)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6675 | ++collend; |
| 6676 | /* cache callback name lookup (if not done yet, i.e. it's the first error) */ |
| 6677 | if (known_errorHandler==-1) { |
| 6678 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 6679 | known_errorHandler = 1; |
| 6680 | else if (!strcmp(errors, "replace")) |
| 6681 | known_errorHandler = 2; |
| 6682 | else if (!strcmp(errors, "ignore")) |
| 6683 | known_errorHandler = 3; |
| 6684 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 6685 | known_errorHandler = 4; |
| 6686 | else |
| 6687 | known_errorHandler = 0; |
| 6688 | } |
| 6689 | switch (known_errorHandler) { |
| 6690 | case 1: /* strict */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6691 | raise_encode_exception(&exc, encoding, unicode, collstart, collend, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6692 | goto onError; |
| 6693 | case 2: /* replace */ |
| 6694 | while (collstart++<collend) |
| 6695 | *str++ = '?'; /* fall through */ |
| 6696 | case 3: /* ignore */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6697 | pos = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6698 | break; |
| 6699 | case 4: /* xmlcharrefreplace */ |
| 6700 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6701 | /* determine replacement size */ |
| 6702 | for (i = collstart, repsize = 0; i < collend; ++i) { |
| 6703 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 6704 | if (ch < 10) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6705 | repsize += 2+1+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6706 | else if (ch < 100) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6707 | repsize += 2+2+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6708 | else if (ch < 1000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6709 | repsize += 2+3+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6710 | else if (ch < 10000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6711 | repsize += 2+4+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6712 | else if (ch < 100000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6713 | repsize += 2+5+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6714 | else if (ch < 1000000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6715 | repsize += 2+6+1; |
Victor Stinner | 0d3721d | 2011-11-22 03:27:53 +0100 | [diff] [blame] | 6716 | else { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 6717 | assert(ch <= MAX_UNICODE); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6718 | repsize += 2+7+1; |
Victor Stinner | 0d3721d | 2011-11-22 03:27:53 +0100 | [diff] [blame] | 6719 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6720 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6721 | requiredsize = respos+repsize+(size-collend); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6722 | if (requiredsize > ressize) { |
| 6723 | if (requiredsize<2*ressize) |
| 6724 | requiredsize = 2*ressize; |
| 6725 | if (_PyBytes_Resize(&res, requiredsize)) |
| 6726 | goto onError; |
| 6727 | str = PyBytes_AS_STRING(res) + respos; |
| 6728 | ressize = requiredsize; |
| 6729 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6730 | /* generate replacement */ |
| 6731 | for (i = collstart; i < collend; ++i) { |
| 6732 | str += sprintf(str, "&#%d;", PyUnicode_READ(kind, data, i)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6733 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6734 | pos = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6735 | break; |
| 6736 | default: |
| 6737 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6738 | encoding, reason, unicode, &exc, |
| 6739 | collstart, collend, &newpos); |
| 6740 | if (repunicode == NULL || (PyUnicode_Check(repunicode) && |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6741 | PyUnicode_READY(repunicode) == -1)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6742 | goto onError; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6743 | if (PyBytes_Check(repunicode)) { |
| 6744 | /* Directly copy bytes result to output. */ |
| 6745 | repsize = PyBytes_Size(repunicode); |
| 6746 | if (repsize > 1) { |
| 6747 | /* Make room for all additional bytes. */ |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 6748 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6749 | if (_PyBytes_Resize(&res, ressize+repsize-1)) { |
| 6750 | Py_DECREF(repunicode); |
| 6751 | goto onError; |
| 6752 | } |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 6753 | str = PyBytes_AS_STRING(res) + respos; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6754 | ressize += repsize-1; |
| 6755 | } |
| 6756 | memcpy(str, PyBytes_AsString(repunicode), repsize); |
| 6757 | str += repsize; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6758 | pos = newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6759 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6760 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6761 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6762 | /* need more space? (at least enough for what we |
| 6763 | have+the replacement+the rest of the string, so |
| 6764 | we won't have to check space for encodable characters) */ |
| 6765 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6766 | repsize = PyUnicode_GET_LENGTH(repunicode); |
| 6767 | requiredsize = respos+repsize+(size-collend); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6768 | if (requiredsize > ressize) { |
| 6769 | if (requiredsize<2*ressize) |
| 6770 | requiredsize = 2*ressize; |
| 6771 | if (_PyBytes_Resize(&res, requiredsize)) { |
| 6772 | Py_DECREF(repunicode); |
| 6773 | goto onError; |
| 6774 | } |
| 6775 | str = PyBytes_AS_STRING(res) + respos; |
| 6776 | ressize = requiredsize; |
| 6777 | } |
| 6778 | /* check if there is anything unencodable in the replacement |
| 6779 | and copy it to the output */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6780 | for (i = 0; repsize-->0; ++i, ++str) { |
| 6781 | c = PyUnicode_READ_CHAR(repunicode, i); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6782 | if (c >= limit) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6783 | raise_encode_exception(&exc, encoding, unicode, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6784 | pos, pos+1, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6785 | Py_DECREF(repunicode); |
| 6786 | goto onError; |
| 6787 | } |
| 6788 | *str = (char)c; |
| 6789 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6790 | pos = newpos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6791 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6792 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6793 | } |
| 6794 | } |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6795 | /* Resize if we allocated to much */ |
| 6796 | size = str - PyBytes_AS_STRING(res); |
| 6797 | if (size < ressize) { /* If this falls res will be NULL */ |
Alexandre Vassalotti | bad1b92 | 2008-12-27 09:49:09 +0000 | [diff] [blame] | 6798 | assert(size >= 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6799 | if (_PyBytes_Resize(&res, size) < 0) |
| 6800 | goto onError; |
| 6801 | } |
| 6802 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6803 | Py_XDECREF(errorHandler); |
| 6804 | Py_XDECREF(exc); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6805 | return res; |
| 6806 | |
| 6807 | onError: |
| 6808 | Py_XDECREF(res); |
| 6809 | Py_XDECREF(errorHandler); |
| 6810 | Py_XDECREF(exc); |
| 6811 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6812 | } |
| 6813 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6814 | /* Deprecated */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6815 | PyObject * |
| 6816 | PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6817 | Py_ssize_t size, |
| 6818 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6819 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6820 | PyObject *result; |
| 6821 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 6822 | if (unicode == NULL) |
| 6823 | return NULL; |
| 6824 | result = unicode_encode_ucs1(unicode, errors, 256); |
| 6825 | Py_DECREF(unicode); |
| 6826 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6827 | } |
| 6828 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6829 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6830 | _PyUnicode_AsLatin1String(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6831 | { |
| 6832 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6833 | PyErr_BadArgument(); |
| 6834 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6835 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6836 | if (PyUnicode_READY(unicode) == -1) |
| 6837 | return NULL; |
| 6838 | /* Fast path: if it is a one-byte string, construct |
| 6839 | bytes object directly. */ |
| 6840 | if (PyUnicode_KIND(unicode) == PyUnicode_1BYTE_KIND) |
| 6841 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 6842 | PyUnicode_GET_LENGTH(unicode)); |
| 6843 | /* Non-Latin-1 characters present. Defer to above function to |
| 6844 | raise the exception. */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6845 | return unicode_encode_ucs1(unicode, errors, 256); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6846 | } |
| 6847 | |
| 6848 | PyObject* |
| 6849 | PyUnicode_AsLatin1String(PyObject *unicode) |
| 6850 | { |
| 6851 | return _PyUnicode_AsLatin1String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6852 | } |
| 6853 | |
| 6854 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ |
| 6855 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6856 | PyObject * |
| 6857 | PyUnicode_DecodeASCII(const char *s, |
| 6858 | Py_ssize_t size, |
| 6859 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6860 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6861 | const char *starts = s; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6862 | PyObject *v; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6863 | int kind; |
| 6864 | void *data; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6865 | Py_ssize_t startinpos; |
| 6866 | Py_ssize_t endinpos; |
| 6867 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6868 | const char *e; |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6869 | int has_error; |
| 6870 | const unsigned char *p = (const unsigned char *)s; |
| 6871 | const unsigned char *end = p + size; |
| 6872 | 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] | 6873 | PyObject *errorHandler = NULL; |
| 6874 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6875 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 6876 | if (size == 0) { |
| 6877 | Py_INCREF(unicode_empty); |
| 6878 | return unicode_empty; |
| 6879 | } |
| 6880 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6881 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6882 | if (size == 1 && (unsigned char)s[0] < 128) |
| 6883 | return get_latin1_char((unsigned char)s[0]); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6884 | |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6885 | has_error = 0; |
| 6886 | while (p < end && !has_error) { |
| 6887 | /* Fast path, see below in PyUnicode_DecodeUTF8Stateful for |
| 6888 | an explanation. */ |
| 6889 | if (!((size_t) p & LONG_PTR_MASK)) { |
| 6890 | /* Help register allocation */ |
| 6891 | register const unsigned char *_p = p; |
| 6892 | while (_p < aligned_end) { |
| 6893 | unsigned long value = *(unsigned long *) _p; |
| 6894 | if (value & ASCII_CHAR_MASK) { |
| 6895 | has_error = 1; |
| 6896 | break; |
| 6897 | } |
| 6898 | _p += SIZEOF_LONG; |
| 6899 | } |
| 6900 | if (_p == end) |
| 6901 | break; |
| 6902 | if (has_error) |
| 6903 | break; |
| 6904 | p = _p; |
| 6905 | } |
| 6906 | if (*p & 0x80) { |
| 6907 | has_error = 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6908 | break; |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6909 | } |
| 6910 | else { |
| 6911 | ++p; |
| 6912 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 6913 | } |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6914 | if (!has_error) |
| 6915 | return unicode_fromascii((const unsigned char *)s, size); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6916 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6917 | v = PyUnicode_New(size, 127); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6918 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6919 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6920 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6921 | return v; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6922 | kind = PyUnicode_KIND(v); |
| 6923 | data = PyUnicode_DATA(v); |
| 6924 | outpos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6925 | e = s + size; |
| 6926 | while (s < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6927 | register unsigned char c = (unsigned char)*s; |
| 6928 | if (c < 128) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6929 | PyUnicode_WRITE(kind, data, outpos++, c); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6930 | ++s; |
| 6931 | } |
| 6932 | else { |
| 6933 | startinpos = s-starts; |
| 6934 | endinpos = startinpos + 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6935 | if (unicode_decode_call_errorhandler( |
| 6936 | errors, &errorHandler, |
| 6937 | "ascii", "ordinal not in range(128)", |
| 6938 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6939 | &v, &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6940 | goto onError; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6941 | kind = PyUnicode_KIND(v); |
| 6942 | data = PyUnicode_DATA(v); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6943 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6944 | } |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 6945 | if (unicode_resize(&v, outpos) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6946 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6947 | Py_XDECREF(errorHandler); |
| 6948 | Py_XDECREF(exc); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 6949 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6950 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6951 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6952 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6953 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6954 | Py_XDECREF(errorHandler); |
| 6955 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6956 | return NULL; |
| 6957 | } |
| 6958 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6959 | /* Deprecated */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6960 | PyObject * |
| 6961 | PyUnicode_EncodeASCII(const Py_UNICODE *p, |
| 6962 | Py_ssize_t size, |
| 6963 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6964 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6965 | PyObject *result; |
| 6966 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 6967 | if (unicode == NULL) |
| 6968 | return NULL; |
| 6969 | result = unicode_encode_ucs1(unicode, errors, 128); |
| 6970 | Py_DECREF(unicode); |
| 6971 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6972 | } |
| 6973 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6974 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6975 | _PyUnicode_AsASCIIString(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6976 | { |
| 6977 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6978 | PyErr_BadArgument(); |
| 6979 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6980 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6981 | if (PyUnicode_READY(unicode) == -1) |
| 6982 | return NULL; |
| 6983 | /* Fast path: if it is an ASCII-only string, construct bytes object |
| 6984 | directly. Else defer to above function to raise the exception. */ |
| 6985 | if (PyUnicode_MAX_CHAR_VALUE(unicode) < 128) |
| 6986 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 6987 | PyUnicode_GET_LENGTH(unicode)); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6988 | return unicode_encode_ucs1(unicode, errors, 128); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6989 | } |
| 6990 | |
| 6991 | PyObject * |
| 6992 | PyUnicode_AsASCIIString(PyObject *unicode) |
| 6993 | { |
| 6994 | return _PyUnicode_AsASCIIString(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6995 | } |
| 6996 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 6997 | #ifdef HAVE_MBCS |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6998 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6999 | /* --- MBCS codecs for Windows -------------------------------------------- */ |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 7000 | |
Hirokazu Yamamoto | 3530246 | 2009-03-21 13:23:27 +0000 | [diff] [blame] | 7001 | #if SIZEOF_INT < SIZEOF_SIZE_T |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7002 | #define NEED_RETRY |
| 7003 | #endif |
| 7004 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7005 | #ifndef WC_ERR_INVALID_CHARS |
| 7006 | # define WC_ERR_INVALID_CHARS 0x0080 |
| 7007 | #endif |
| 7008 | |
| 7009 | static char* |
| 7010 | code_page_name(UINT code_page, PyObject **obj) |
| 7011 | { |
| 7012 | *obj = NULL; |
| 7013 | if (code_page == CP_ACP) |
| 7014 | return "mbcs"; |
| 7015 | if (code_page == CP_UTF7) |
| 7016 | return "CP_UTF7"; |
| 7017 | if (code_page == CP_UTF8) |
| 7018 | return "CP_UTF8"; |
| 7019 | |
| 7020 | *obj = PyBytes_FromFormat("cp%u", code_page); |
| 7021 | if (*obj == NULL) |
| 7022 | return NULL; |
| 7023 | return PyBytes_AS_STRING(*obj); |
| 7024 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7025 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7026 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7027 | is_dbcs_lead_byte(UINT code_page, const char *s, int offset) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7028 | { |
| 7029 | const char *curr = s + offset; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7030 | const char *prev; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7031 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7032 | if (!IsDBCSLeadByteEx(code_page, *curr)) |
| 7033 | return 0; |
| 7034 | |
| 7035 | prev = CharPrevExA(code_page, s, curr, 0); |
| 7036 | if (prev == curr) |
| 7037 | return 1; |
| 7038 | /* FIXME: This code is limited to "true" double-byte encodings, |
| 7039 | as it assumes an incomplete character consists of a single |
| 7040 | byte. */ |
| 7041 | if (curr - prev == 2) |
| 7042 | return 1; |
| 7043 | if (!IsDBCSLeadByteEx(code_page, *prev)) |
| 7044 | return 1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7045 | return 0; |
| 7046 | } |
| 7047 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7048 | static DWORD |
| 7049 | decode_code_page_flags(UINT code_page) |
| 7050 | { |
| 7051 | if (code_page == CP_UTF7) { |
| 7052 | /* The CP_UTF7 decoder only supports flags=0 */ |
| 7053 | return 0; |
| 7054 | } |
| 7055 | else |
| 7056 | return MB_ERR_INVALID_CHARS; |
| 7057 | } |
| 7058 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7059 | /* |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7060 | * Decode a byte string from a Windows code page into unicode object in strict |
| 7061 | * mode. |
| 7062 | * |
| 7063 | * Returns consumed size if succeed, returns -2 on decode error, or raise a |
| 7064 | * WindowsError and returns -1 on other error. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7065 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7066 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7067 | decode_code_page_strict(UINT code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7068 | PyObject **v, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7069 | const char *in, |
| 7070 | int insize) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7071 | { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7072 | const DWORD flags = decode_code_page_flags(code_page); |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 7073 | wchar_t *out; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7074 | DWORD outsize; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7075 | |
| 7076 | /* First get the size of the result */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7077 | assert(insize > 0); |
| 7078 | outsize = MultiByteToWideChar(code_page, flags, in, insize, NULL, 0); |
| 7079 | if (outsize <= 0) |
| 7080 | goto error; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7081 | |
| 7082 | if (*v == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7083 | /* Create unicode object */ |
Victor Stinner | ab59594 | 2011-12-17 04:59:06 +0100 | [diff] [blame] | 7084 | /* FIXME: don't use _PyUnicode_New(), but allocate a wchar_t* buffer */ |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7085 | *v = (PyObject*)_PyUnicode_New(outsize); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7086 | if (*v == NULL) |
| 7087 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7088 | out = PyUnicode_AS_UNICODE(*v); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7089 | } |
| 7090 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7091 | /* Extend unicode object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7092 | Py_ssize_t n = PyUnicode_GET_SIZE(*v); |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 7093 | if (unicode_resize(v, n + outsize) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7094 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7095 | out = PyUnicode_AS_UNICODE(*v) + n; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7096 | } |
| 7097 | |
| 7098 | /* Do the conversion */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7099 | outsize = MultiByteToWideChar(code_page, flags, in, insize, out, outsize); |
| 7100 | if (outsize <= 0) |
| 7101 | goto error; |
| 7102 | return insize; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7103 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7104 | error: |
| 7105 | if (GetLastError() == ERROR_NO_UNICODE_TRANSLATION) |
| 7106 | return -2; |
| 7107 | PyErr_SetFromWindowsErr(0); |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7108 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7109 | } |
| 7110 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7111 | /* |
| 7112 | * Decode a byte string from a code page into unicode object with an error |
| 7113 | * handler. |
| 7114 | * |
| 7115 | * Returns consumed size if succeed, or raise a WindowsError or |
| 7116 | * UnicodeDecodeError exception and returns -1 on error. |
| 7117 | */ |
| 7118 | static int |
| 7119 | decode_code_page_errors(UINT code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7120 | PyObject **v, |
| 7121 | const char *in, const int size, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7122 | const char *errors) |
| 7123 | { |
| 7124 | const char *startin = in; |
| 7125 | const char *endin = in + size; |
| 7126 | const DWORD flags = decode_code_page_flags(code_page); |
| 7127 | /* Ideally, we should get reason from FormatMessage. This is the Windows |
| 7128 | 2000 English version of the message. */ |
| 7129 | const char *reason = "No mapping for the Unicode character exists " |
| 7130 | "in the target code page."; |
| 7131 | /* each step cannot decode more than 1 character, but a character can be |
| 7132 | represented as a surrogate pair */ |
| 7133 | wchar_t buffer[2], *startout, *out; |
| 7134 | int insize, outsize; |
| 7135 | PyObject *errorHandler = NULL; |
| 7136 | PyObject *exc = NULL; |
| 7137 | PyObject *encoding_obj = NULL; |
| 7138 | char *encoding; |
| 7139 | DWORD err; |
| 7140 | int ret = -1; |
| 7141 | |
| 7142 | assert(size > 0); |
| 7143 | |
| 7144 | encoding = code_page_name(code_page, &encoding_obj); |
| 7145 | if (encoding == NULL) |
| 7146 | return -1; |
| 7147 | |
| 7148 | if (errors == NULL || strcmp(errors, "strict") == 0) { |
| 7149 | /* The last error was ERROR_NO_UNICODE_TRANSLATION, then we raise a |
| 7150 | UnicodeDecodeError. */ |
| 7151 | make_decode_exception(&exc, encoding, in, size, 0, 0, reason); |
| 7152 | if (exc != NULL) { |
| 7153 | PyCodec_StrictErrors(exc); |
| 7154 | Py_CLEAR(exc); |
| 7155 | } |
| 7156 | goto error; |
| 7157 | } |
| 7158 | |
| 7159 | if (*v == NULL) { |
| 7160 | /* Create unicode object */ |
| 7161 | if (size > PY_SSIZE_T_MAX / (Py_ssize_t)Py_ARRAY_LENGTH(buffer)) { |
| 7162 | PyErr_NoMemory(); |
| 7163 | goto error; |
| 7164 | } |
Victor Stinner | ab59594 | 2011-12-17 04:59:06 +0100 | [diff] [blame] | 7165 | /* FIXME: don't use _PyUnicode_New(), but allocate a wchar_t* buffer */ |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7166 | *v = (PyObject*)_PyUnicode_New(size * Py_ARRAY_LENGTH(buffer)); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7167 | if (*v == NULL) |
| 7168 | goto error; |
| 7169 | startout = PyUnicode_AS_UNICODE(*v); |
| 7170 | } |
| 7171 | else { |
| 7172 | /* Extend unicode object */ |
| 7173 | Py_ssize_t n = PyUnicode_GET_SIZE(*v); |
| 7174 | if (size > (PY_SSIZE_T_MAX - n) / (Py_ssize_t)Py_ARRAY_LENGTH(buffer)) { |
| 7175 | PyErr_NoMemory(); |
| 7176 | goto error; |
| 7177 | } |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 7178 | if (unicode_resize(v, n + size * Py_ARRAY_LENGTH(buffer)) < 0) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7179 | goto error; |
| 7180 | startout = PyUnicode_AS_UNICODE(*v) + n; |
| 7181 | } |
| 7182 | |
| 7183 | /* Decode the byte string character per character */ |
| 7184 | out = startout; |
| 7185 | while (in < endin) |
| 7186 | { |
| 7187 | /* Decode a character */ |
| 7188 | insize = 1; |
| 7189 | do |
| 7190 | { |
| 7191 | outsize = MultiByteToWideChar(code_page, flags, |
| 7192 | in, insize, |
| 7193 | buffer, Py_ARRAY_LENGTH(buffer)); |
| 7194 | if (outsize > 0) |
| 7195 | break; |
| 7196 | err = GetLastError(); |
| 7197 | if (err != ERROR_NO_UNICODE_TRANSLATION |
| 7198 | && err != ERROR_INSUFFICIENT_BUFFER) |
| 7199 | { |
| 7200 | PyErr_SetFromWindowsErr(0); |
| 7201 | goto error; |
| 7202 | } |
| 7203 | insize++; |
| 7204 | } |
| 7205 | /* 4=maximum length of a UTF-8 sequence */ |
| 7206 | while (insize <= 4 && (in + insize) <= endin); |
| 7207 | |
| 7208 | if (outsize <= 0) { |
| 7209 | Py_ssize_t startinpos, endinpos, outpos; |
| 7210 | |
| 7211 | startinpos = in - startin; |
| 7212 | endinpos = startinpos + 1; |
| 7213 | outpos = out - PyUnicode_AS_UNICODE(*v); |
| 7214 | if (unicode_decode_call_errorhandler( |
| 7215 | errors, &errorHandler, |
| 7216 | encoding, reason, |
| 7217 | &startin, &endin, &startinpos, &endinpos, &exc, &in, |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 7218 | v, &outpos)) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7219 | { |
| 7220 | goto error; |
| 7221 | } |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 7222 | out = PyUnicode_AS_UNICODE(*v) + outpos; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7223 | } |
| 7224 | else { |
| 7225 | in += insize; |
| 7226 | memcpy(out, buffer, outsize * sizeof(wchar_t)); |
| 7227 | out += outsize; |
| 7228 | } |
| 7229 | } |
| 7230 | |
| 7231 | /* write a NUL character at the end */ |
| 7232 | *out = 0; |
| 7233 | |
| 7234 | /* Extend unicode object */ |
| 7235 | outsize = out - startout; |
| 7236 | assert(outsize <= PyUnicode_WSTR_LENGTH(*v)); |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 7237 | if (unicode_resize(v, outsize) < 0) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7238 | goto error; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7239 | ret = size; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7240 | |
| 7241 | error: |
| 7242 | Py_XDECREF(encoding_obj); |
| 7243 | Py_XDECREF(errorHandler); |
| 7244 | Py_XDECREF(exc); |
| 7245 | return ret; |
| 7246 | } |
| 7247 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7248 | static PyObject * |
| 7249 | decode_code_page_stateful(int code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7250 | const char *s, Py_ssize_t size, |
| 7251 | const char *errors, Py_ssize_t *consumed) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7252 | { |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7253 | PyObject *v = NULL; |
| 7254 | int chunk_size, final, converted, done; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7255 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7256 | if (code_page < 0) { |
| 7257 | PyErr_SetString(PyExc_ValueError, "invalid code page number"); |
| 7258 | return NULL; |
| 7259 | } |
| 7260 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7261 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7262 | *consumed = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7263 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7264 | do |
| 7265 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7266 | #ifdef NEED_RETRY |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7267 | if (size > INT_MAX) { |
| 7268 | chunk_size = INT_MAX; |
| 7269 | final = 0; |
| 7270 | done = 0; |
| 7271 | } |
| 7272 | else |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7273 | #endif |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7274 | { |
| 7275 | chunk_size = (int)size; |
| 7276 | final = (consumed == NULL); |
| 7277 | done = 1; |
| 7278 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7279 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7280 | /* Skip trailing lead-byte unless 'final' is set */ |
| 7281 | if (!final && is_dbcs_lead_byte(code_page, s, chunk_size - 1)) |
| 7282 | --chunk_size; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7283 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7284 | if (chunk_size == 0 && done) { |
| 7285 | if (v != NULL) |
| 7286 | break; |
| 7287 | Py_INCREF(unicode_empty); |
| 7288 | return unicode_empty; |
| 7289 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7290 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7291 | |
| 7292 | converted = decode_code_page_strict(code_page, &v, |
| 7293 | s, chunk_size); |
| 7294 | if (converted == -2) |
| 7295 | converted = decode_code_page_errors(code_page, &v, |
| 7296 | s, chunk_size, |
| 7297 | errors); |
| 7298 | assert(converted != 0); |
| 7299 | |
| 7300 | if (converted < 0) { |
| 7301 | Py_XDECREF(v); |
| 7302 | return NULL; |
| 7303 | } |
| 7304 | |
| 7305 | if (consumed) |
| 7306 | *consumed += converted; |
| 7307 | |
| 7308 | s += converted; |
| 7309 | size -= converted; |
| 7310 | } while (!done); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7311 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 7312 | return unicode_result(v); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7313 | } |
| 7314 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7315 | PyObject * |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7316 | PyUnicode_DecodeCodePageStateful(int code_page, |
| 7317 | const char *s, |
| 7318 | Py_ssize_t size, |
| 7319 | const char *errors, |
| 7320 | Py_ssize_t *consumed) |
| 7321 | { |
| 7322 | return decode_code_page_stateful(code_page, s, size, errors, consumed); |
| 7323 | } |
| 7324 | |
| 7325 | PyObject * |
| 7326 | PyUnicode_DecodeMBCSStateful(const char *s, |
| 7327 | Py_ssize_t size, |
| 7328 | const char *errors, |
| 7329 | Py_ssize_t *consumed) |
| 7330 | { |
| 7331 | return decode_code_page_stateful(CP_ACP, s, size, errors, consumed); |
| 7332 | } |
| 7333 | |
| 7334 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7335 | PyUnicode_DecodeMBCS(const char *s, |
| 7336 | Py_ssize_t size, |
| 7337 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7338 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7339 | return PyUnicode_DecodeMBCSStateful(s, size, errors, NULL); |
| 7340 | } |
| 7341 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7342 | static DWORD |
| 7343 | encode_code_page_flags(UINT code_page, const char *errors) |
| 7344 | { |
| 7345 | if (code_page == CP_UTF8) { |
| 7346 | if (winver.dwMajorVersion >= 6) |
| 7347 | /* CP_UTF8 supports WC_ERR_INVALID_CHARS on Windows Vista |
| 7348 | and later */ |
| 7349 | return WC_ERR_INVALID_CHARS; |
| 7350 | else |
| 7351 | /* CP_UTF8 only supports flags=0 on Windows older than Vista */ |
| 7352 | return 0; |
| 7353 | } |
| 7354 | else if (code_page == CP_UTF7) { |
| 7355 | /* CP_UTF7 only supports flags=0 */ |
| 7356 | return 0; |
| 7357 | } |
| 7358 | else { |
| 7359 | if (errors != NULL && strcmp(errors, "replace") == 0) |
| 7360 | return 0; |
| 7361 | else |
| 7362 | return WC_NO_BEST_FIT_CHARS; |
| 7363 | } |
| 7364 | } |
| 7365 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7366 | /* |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7367 | * Encode a Unicode string to a Windows code page into a byte string in strict |
| 7368 | * mode. |
| 7369 | * |
| 7370 | * Returns consumed characters if succeed, returns -2 on encode error, or raise |
| 7371 | * a WindowsError and returns -1 on other error. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7372 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7373 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7374 | encode_code_page_strict(UINT code_page, PyObject **outbytes, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7375 | PyObject *unicode, Py_ssize_t offset, int len, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7376 | const char* errors) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7377 | { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7378 | BOOL usedDefaultChar = FALSE; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7379 | BOOL *pusedDefaultChar = &usedDefaultChar; |
| 7380 | int outsize; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7381 | PyObject *exc = NULL; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 7382 | wchar_t *p; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7383 | Py_ssize_t size; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7384 | const DWORD flags = encode_code_page_flags(code_page, NULL); |
| 7385 | char *out; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7386 | /* Create a substring so that we can get the UTF-16 representation |
| 7387 | of just the slice under consideration. */ |
| 7388 | PyObject *substring; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7389 | |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7390 | assert(len > 0); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7391 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7392 | if (code_page != CP_UTF8 && code_page != CP_UTF7) |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7393 | pusedDefaultChar = &usedDefaultChar; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7394 | else |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7395 | pusedDefaultChar = NULL; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7396 | |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7397 | substring = PyUnicode_Substring(unicode, offset, offset+len); |
| 7398 | if (substring == NULL) |
| 7399 | return -1; |
| 7400 | p = PyUnicode_AsUnicodeAndSize(substring, &size); |
| 7401 | if (p == NULL) { |
| 7402 | Py_DECREF(substring); |
| 7403 | return -1; |
| 7404 | } |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7405 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7406 | /* First get the size of the result */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7407 | outsize = WideCharToMultiByte(code_page, flags, |
| 7408 | p, size, |
| 7409 | NULL, 0, |
| 7410 | NULL, pusedDefaultChar); |
| 7411 | if (outsize <= 0) |
| 7412 | goto error; |
| 7413 | /* If we used a default char, then we failed! */ |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7414 | if (pusedDefaultChar && *pusedDefaultChar) { |
| 7415 | Py_DECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7416 | return -2; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7417 | } |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7418 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7419 | if (*outbytes == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7420 | /* Create string object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7421 | *outbytes = PyBytes_FromStringAndSize(NULL, outsize); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7422 | if (*outbytes == NULL) { |
| 7423 | Py_DECREF(substring); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7424 | return -1; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7425 | } |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7426 | out = PyBytes_AS_STRING(*outbytes); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7427 | } |
| 7428 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7429 | /* Extend string object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7430 | const Py_ssize_t n = PyBytes_Size(*outbytes); |
| 7431 | if (outsize > PY_SSIZE_T_MAX - n) { |
| 7432 | PyErr_NoMemory(); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7433 | Py_DECREF(substring); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7434 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7435 | } |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7436 | if (_PyBytes_Resize(outbytes, n + outsize) < 0) { |
| 7437 | Py_DECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7438 | return -1; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7439 | } |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7440 | out = PyBytes_AS_STRING(*outbytes) + n; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7441 | } |
| 7442 | |
| 7443 | /* Do the conversion */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7444 | outsize = WideCharToMultiByte(code_page, flags, |
| 7445 | p, size, |
| 7446 | out, outsize, |
| 7447 | NULL, pusedDefaultChar); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7448 | Py_CLEAR(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7449 | if (outsize <= 0) |
| 7450 | goto error; |
| 7451 | if (pusedDefaultChar && *pusedDefaultChar) |
| 7452 | return -2; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7453 | return 0; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7454 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7455 | error: |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7456 | Py_XDECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7457 | if (GetLastError() == ERROR_NO_UNICODE_TRANSLATION) |
| 7458 | return -2; |
| 7459 | PyErr_SetFromWindowsErr(0); |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7460 | return -1; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7461 | } |
| 7462 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7463 | /* |
| 7464 | * Encode a Unicode string to a Windows code page into a byte string using a |
| 7465 | * error handler. |
| 7466 | * |
| 7467 | * Returns consumed characters if succeed, or raise a WindowsError and returns |
| 7468 | * -1 on other error. |
| 7469 | */ |
| 7470 | static int |
| 7471 | encode_code_page_errors(UINT code_page, PyObject **outbytes, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7472 | PyObject *unicode, Py_ssize_t unicode_offset, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7473 | Py_ssize_t insize, const char* errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7474 | { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7475 | const DWORD flags = encode_code_page_flags(code_page, errors); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7476 | Py_ssize_t pos = unicode_offset; |
| 7477 | Py_ssize_t endin = unicode_offset + insize; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7478 | /* Ideally, we should get reason from FormatMessage. This is the Windows |
| 7479 | 2000 English version of the message. */ |
| 7480 | const char *reason = "invalid character"; |
| 7481 | /* 4=maximum length of a UTF-8 sequence */ |
| 7482 | char buffer[4]; |
| 7483 | BOOL usedDefaultChar = FALSE, *pusedDefaultChar; |
| 7484 | Py_ssize_t outsize; |
| 7485 | char *out; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7486 | PyObject *errorHandler = NULL; |
| 7487 | PyObject *exc = NULL; |
| 7488 | PyObject *encoding_obj = NULL; |
| 7489 | char *encoding; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7490 | Py_ssize_t newpos, newoutsize; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7491 | PyObject *rep; |
| 7492 | int ret = -1; |
| 7493 | |
| 7494 | assert(insize > 0); |
| 7495 | |
| 7496 | encoding = code_page_name(code_page, &encoding_obj); |
| 7497 | if (encoding == NULL) |
| 7498 | return -1; |
| 7499 | |
| 7500 | if (errors == NULL || strcmp(errors, "strict") == 0) { |
| 7501 | /* The last error was ERROR_NO_UNICODE_TRANSLATION, |
| 7502 | then we raise a UnicodeEncodeError. */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 7503 | make_encode_exception(&exc, encoding, unicode, 0, 0, reason); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7504 | if (exc != NULL) { |
| 7505 | PyCodec_StrictErrors(exc); |
| 7506 | Py_DECREF(exc); |
| 7507 | } |
| 7508 | Py_XDECREF(encoding_obj); |
| 7509 | return -1; |
| 7510 | } |
| 7511 | |
| 7512 | if (code_page != CP_UTF8 && code_page != CP_UTF7) |
| 7513 | pusedDefaultChar = &usedDefaultChar; |
| 7514 | else |
| 7515 | pusedDefaultChar = NULL; |
| 7516 | |
| 7517 | if (Py_ARRAY_LENGTH(buffer) > PY_SSIZE_T_MAX / insize) { |
| 7518 | PyErr_NoMemory(); |
| 7519 | goto error; |
| 7520 | } |
| 7521 | outsize = insize * Py_ARRAY_LENGTH(buffer); |
| 7522 | |
| 7523 | if (*outbytes == NULL) { |
| 7524 | /* Create string object */ |
| 7525 | *outbytes = PyBytes_FromStringAndSize(NULL, outsize); |
| 7526 | if (*outbytes == NULL) |
| 7527 | goto error; |
| 7528 | out = PyBytes_AS_STRING(*outbytes); |
| 7529 | } |
| 7530 | else { |
| 7531 | /* Extend string object */ |
| 7532 | Py_ssize_t n = PyBytes_Size(*outbytes); |
| 7533 | if (n > PY_SSIZE_T_MAX - outsize) { |
| 7534 | PyErr_NoMemory(); |
| 7535 | goto error; |
| 7536 | } |
| 7537 | if (_PyBytes_Resize(outbytes, n + outsize) < 0) |
| 7538 | goto error; |
| 7539 | out = PyBytes_AS_STRING(*outbytes) + n; |
| 7540 | } |
| 7541 | |
| 7542 | /* Encode the string character per character */ |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7543 | while (pos < endin) |
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 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, pos); |
| 7546 | wchar_t chars[2]; |
| 7547 | int charsize; |
| 7548 | if (ch < 0x10000) { |
| 7549 | chars[0] = (wchar_t)ch; |
| 7550 | charsize = 1; |
| 7551 | } |
| 7552 | else { |
| 7553 | ch -= 0x10000; |
| 7554 | chars[0] = 0xd800 + (ch >> 10); |
| 7555 | chars[1] = 0xdc00 + (ch & 0x3ff); |
| 7556 | charsize = 2; |
| 7557 | } |
| 7558 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7559 | outsize = WideCharToMultiByte(code_page, flags, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7560 | chars, charsize, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7561 | buffer, Py_ARRAY_LENGTH(buffer), |
| 7562 | NULL, pusedDefaultChar); |
| 7563 | if (outsize > 0) { |
| 7564 | if (pusedDefaultChar == NULL || !(*pusedDefaultChar)) |
| 7565 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7566 | pos++; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7567 | memcpy(out, buffer, outsize); |
| 7568 | out += outsize; |
| 7569 | continue; |
| 7570 | } |
| 7571 | } |
| 7572 | else if (GetLastError() != ERROR_NO_UNICODE_TRANSLATION) { |
| 7573 | PyErr_SetFromWindowsErr(0); |
| 7574 | goto error; |
| 7575 | } |
| 7576 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7577 | rep = unicode_encode_call_errorhandler( |
| 7578 | errors, &errorHandler, encoding, reason, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7579 | unicode, &exc, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7580 | pos, pos + 1, &newpos); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7581 | if (rep == NULL) |
| 7582 | goto error; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7583 | pos = newpos; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7584 | |
| 7585 | if (PyBytes_Check(rep)) { |
| 7586 | outsize = PyBytes_GET_SIZE(rep); |
| 7587 | if (outsize != 1) { |
| 7588 | Py_ssize_t offset = out - PyBytes_AS_STRING(*outbytes); |
| 7589 | newoutsize = PyBytes_GET_SIZE(*outbytes) + (outsize - 1); |
| 7590 | if (_PyBytes_Resize(outbytes, newoutsize) < 0) { |
| 7591 | Py_DECREF(rep); |
| 7592 | goto error; |
| 7593 | } |
| 7594 | out = PyBytes_AS_STRING(*outbytes) + offset; |
| 7595 | } |
| 7596 | memcpy(out, PyBytes_AS_STRING(rep), outsize); |
| 7597 | out += outsize; |
| 7598 | } |
| 7599 | else { |
| 7600 | Py_ssize_t i; |
| 7601 | enum PyUnicode_Kind kind; |
| 7602 | void *data; |
| 7603 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7604 | if (PyUnicode_READY(rep) == -1) { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7605 | Py_DECREF(rep); |
| 7606 | goto error; |
| 7607 | } |
| 7608 | |
| 7609 | outsize = PyUnicode_GET_LENGTH(rep); |
| 7610 | if (outsize != 1) { |
| 7611 | Py_ssize_t offset = out - PyBytes_AS_STRING(*outbytes); |
| 7612 | newoutsize = PyBytes_GET_SIZE(*outbytes) + (outsize - 1); |
| 7613 | if (_PyBytes_Resize(outbytes, newoutsize) < 0) { |
| 7614 | Py_DECREF(rep); |
| 7615 | goto error; |
| 7616 | } |
| 7617 | out = PyBytes_AS_STRING(*outbytes) + offset; |
| 7618 | } |
| 7619 | kind = PyUnicode_KIND(rep); |
| 7620 | data = PyUnicode_DATA(rep); |
| 7621 | for (i=0; i < outsize; i++) { |
| 7622 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 7623 | if (ch > 127) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 7624 | raise_encode_exception(&exc, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7625 | encoding, unicode, |
| 7626 | pos, pos + 1, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7627 | "unable to encode error handler result to ASCII"); |
| 7628 | Py_DECREF(rep); |
| 7629 | goto error; |
| 7630 | } |
| 7631 | *out = (unsigned char)ch; |
| 7632 | out++; |
| 7633 | } |
| 7634 | } |
| 7635 | Py_DECREF(rep); |
| 7636 | } |
| 7637 | /* write a NUL byte */ |
| 7638 | *out = 0; |
| 7639 | outsize = out - PyBytes_AS_STRING(*outbytes); |
| 7640 | assert(outsize <= PyBytes_GET_SIZE(*outbytes)); |
| 7641 | if (_PyBytes_Resize(outbytes, outsize) < 0) |
| 7642 | goto error; |
| 7643 | ret = 0; |
| 7644 | |
| 7645 | error: |
| 7646 | Py_XDECREF(encoding_obj); |
| 7647 | Py_XDECREF(errorHandler); |
| 7648 | Py_XDECREF(exc); |
| 7649 | return ret; |
| 7650 | } |
| 7651 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7652 | static PyObject * |
| 7653 | encode_code_page(int code_page, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7654 | PyObject *unicode, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7655 | const char *errors) |
| 7656 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7657 | Py_ssize_t len; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7658 | PyObject *outbytes = NULL; |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7659 | Py_ssize_t offset; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7660 | int chunk_len, ret, done; |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7661 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7662 | if (PyUnicode_READY(unicode) == -1) |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7663 | return NULL; |
| 7664 | len = PyUnicode_GET_LENGTH(unicode); |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 7665 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7666 | if (code_page < 0) { |
| 7667 | PyErr_SetString(PyExc_ValueError, "invalid code page number"); |
| 7668 | return NULL; |
| 7669 | } |
| 7670 | |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7671 | if (len == 0) |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7672 | return PyBytes_FromStringAndSize(NULL, 0); |
| 7673 | |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7674 | offset = 0; |
| 7675 | do |
| 7676 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7677 | #ifdef NEED_RETRY |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7678 | /* 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] | 7679 | chunks. */ |
| 7680 | if (len > INT_MAX/2) { |
| 7681 | chunk_len = INT_MAX/2; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7682 | done = 0; |
| 7683 | } |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7684 | else |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7685 | #endif |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7686 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7687 | chunk_len = (int)len; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7688 | done = 1; |
| 7689 | } |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7690 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7691 | ret = encode_code_page_strict(code_page, &outbytes, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7692 | unicode, offset, chunk_len, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7693 | errors); |
| 7694 | if (ret == -2) |
| 7695 | ret = encode_code_page_errors(code_page, &outbytes, |
| 7696 | unicode, offset, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7697 | chunk_len, errors); |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7698 | if (ret < 0) { |
| 7699 | Py_XDECREF(outbytes); |
| 7700 | return NULL; |
| 7701 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7702 | |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7703 | offset += chunk_len; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7704 | len -= chunk_len; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7705 | } while (!done); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7706 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7707 | return outbytes; |
| 7708 | } |
| 7709 | |
| 7710 | PyObject * |
| 7711 | PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
| 7712 | Py_ssize_t size, |
| 7713 | const char *errors) |
| 7714 | { |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7715 | PyObject *unicode, *res; |
| 7716 | unicode = PyUnicode_FromUnicode(p, size); |
| 7717 | if (unicode == NULL) |
| 7718 | return NULL; |
| 7719 | res = encode_code_page(CP_ACP, unicode, errors); |
| 7720 | Py_DECREF(unicode); |
| 7721 | return res; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7722 | } |
| 7723 | |
| 7724 | PyObject * |
| 7725 | PyUnicode_EncodeCodePage(int code_page, |
| 7726 | PyObject *unicode, |
| 7727 | const char *errors) |
| 7728 | { |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7729 | return encode_code_page(code_page, unicode, errors); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7730 | } |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 7731 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7732 | PyObject * |
| 7733 | PyUnicode_AsMBCSString(PyObject *unicode) |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 7734 | { |
| 7735 | if (!PyUnicode_Check(unicode)) { |
| 7736 | PyErr_BadArgument(); |
| 7737 | return NULL; |
| 7738 | } |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7739 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, NULL); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 7740 | } |
| 7741 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7742 | #undef NEED_RETRY |
| 7743 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 7744 | #endif /* HAVE_MBCS */ |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7745 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7746 | /* --- Character Mapping Codec -------------------------------------------- */ |
| 7747 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7748 | PyObject * |
| 7749 | PyUnicode_DecodeCharmap(const char *s, |
| 7750 | Py_ssize_t size, |
| 7751 | PyObject *mapping, |
| 7752 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7753 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7754 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7755 | Py_ssize_t startinpos; |
| 7756 | Py_ssize_t endinpos; |
| 7757 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7758 | const char *e; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 7759 | PyObject *v; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7760 | Py_ssize_t extrachars = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7761 | PyObject *errorHandler = NULL; |
| 7762 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7763 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7764 | /* Default to Latin-1 */ |
| 7765 | if (mapping == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7766 | return PyUnicode_DecodeLatin1(s, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7767 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7768 | v = PyUnicode_New(size, 127); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7769 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7770 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7771 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 7772 | return v; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7773 | outpos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7774 | e = s + size; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7775 | if (PyUnicode_CheckExact(mapping)) { |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7776 | Py_ssize_t maplen; |
| 7777 | enum PyUnicode_Kind kind; |
| 7778 | void *data; |
| 7779 | Py_UCS4 x; |
| 7780 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7781 | if (PyUnicode_READY(mapping) == -1) |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7782 | return NULL; |
| 7783 | |
| 7784 | maplen = PyUnicode_GET_LENGTH(mapping); |
| 7785 | data = PyUnicode_DATA(mapping); |
| 7786 | kind = PyUnicode_KIND(mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7787 | while (s < e) { |
| 7788 | unsigned char ch = *s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7789 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7790 | if (ch < maplen) |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7791 | x = PyUnicode_READ(kind, data, ch); |
| 7792 | else |
| 7793 | x = 0xfffe; /* invalid value */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7794 | |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7795 | if (x == 0xfffe) |
| 7796 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7797 | /* undefined mapping */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7798 | startinpos = s-starts; |
| 7799 | endinpos = startinpos+1; |
| 7800 | if (unicode_decode_call_errorhandler( |
| 7801 | errors, &errorHandler, |
| 7802 | "charmap", "character maps to <undefined>", |
| 7803 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7804 | &v, &outpos)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7805 | goto onError; |
| 7806 | } |
| 7807 | continue; |
| 7808 | } |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7809 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7810 | if (unicode_putchar(&v, &outpos, x) < 0) |
| 7811 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7812 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7813 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7814 | } |
| 7815 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7816 | while (s < e) { |
| 7817 | unsigned char ch = *s; |
| 7818 | PyObject *w, *x; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7819 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7820 | /* Get mapping (char ordinal -> integer, Unicode char or None) */ |
| 7821 | w = PyLong_FromLong((long)ch); |
| 7822 | if (w == NULL) |
| 7823 | goto onError; |
| 7824 | x = PyObject_GetItem(mapping, w); |
| 7825 | Py_DECREF(w); |
| 7826 | if (x == NULL) { |
| 7827 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 7828 | /* No mapping found means: mapping is undefined. */ |
| 7829 | PyErr_Clear(); |
| 7830 | x = Py_None; |
| 7831 | Py_INCREF(x); |
| 7832 | } else |
| 7833 | goto onError; |
| 7834 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7835 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7836 | /* Apply mapping */ |
| 7837 | if (PyLong_Check(x)) { |
| 7838 | long value = PyLong_AS_LONG(x); |
| 7839 | if (value < 0 || value > 65535) { |
| 7840 | PyErr_SetString(PyExc_TypeError, |
| 7841 | "character mapping must be in range(65536)"); |
| 7842 | Py_DECREF(x); |
| 7843 | goto onError; |
| 7844 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7845 | if (unicode_putchar(&v, &outpos, value) < 0) |
| 7846 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7847 | } |
| 7848 | else if (x == Py_None) { |
| 7849 | /* undefined mapping */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7850 | startinpos = s-starts; |
| 7851 | endinpos = startinpos+1; |
| 7852 | if (unicode_decode_call_errorhandler( |
| 7853 | errors, &errorHandler, |
| 7854 | "charmap", "character maps to <undefined>", |
| 7855 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7856 | &v, &outpos)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7857 | Py_DECREF(x); |
| 7858 | goto onError; |
| 7859 | } |
| 7860 | Py_DECREF(x); |
| 7861 | continue; |
| 7862 | } |
| 7863 | else if (PyUnicode_Check(x)) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7864 | Py_ssize_t targetsize; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7865 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7866 | if (PyUnicode_READY(x) == -1) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7867 | goto onError; |
| 7868 | targetsize = PyUnicode_GET_LENGTH(x); |
| 7869 | |
| 7870 | if (targetsize == 1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7871 | /* 1-1 mapping */ |
Victor Stinner | 62aa4d0 | 2011-11-09 00:03:45 +0100 | [diff] [blame] | 7872 | if (unicode_putchar(&v, &outpos, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7873 | PyUnicode_READ_CHAR(x, 0)) < 0) |
| 7874 | goto onError; |
| 7875 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7876 | else if (targetsize > 1) { |
| 7877 | /* 1-n mapping */ |
| 7878 | if (targetsize > extrachars) { |
| 7879 | /* resize first */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7880 | Py_ssize_t needed = (targetsize - extrachars) + \ |
| 7881 | (targetsize << 2); |
| 7882 | extrachars += needed; |
| 7883 | /* XXX overflow detection missing */ |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 7884 | if (unicode_resize(&v, |
| 7885 | PyUnicode_GET_LENGTH(v) + needed) < 0) |
| 7886 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7887 | Py_DECREF(x); |
| 7888 | goto onError; |
| 7889 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7890 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7891 | if (unicode_widen(&v, PyUnicode_MAX_CHAR_VALUE(x)) < 0) |
| 7892 | goto onError; |
| 7893 | PyUnicode_CopyCharacters(v, outpos, x, 0, targetsize); |
| 7894 | outpos += targetsize; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7895 | extrachars -= targetsize; |
| 7896 | } |
| 7897 | /* 1-0 mapping: skip the character */ |
| 7898 | } |
| 7899 | else { |
| 7900 | /* wrong return value */ |
| 7901 | PyErr_SetString(PyExc_TypeError, |
| 7902 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7903 | Py_DECREF(x); |
| 7904 | goto onError; |
| 7905 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7906 | Py_DECREF(x); |
| 7907 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7908 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7909 | } |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 7910 | if (unicode_resize(&v, outpos) < 0) |
Antoine Pitrou | a8f63c0 | 2011-11-08 18:37:16 +0100 | [diff] [blame] | 7911 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7912 | Py_XDECREF(errorHandler); |
| 7913 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 7914 | return unicode_result(v); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7915 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7916 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7917 | Py_XDECREF(errorHandler); |
| 7918 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7919 | Py_XDECREF(v); |
| 7920 | return NULL; |
| 7921 | } |
| 7922 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7923 | /* Charmap encoding: the lookup table */ |
| 7924 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7925 | struct encoding_map { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7926 | PyObject_HEAD |
| 7927 | unsigned char level1[32]; |
| 7928 | int count2, count3; |
| 7929 | unsigned char level23[1]; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7930 | }; |
| 7931 | |
| 7932 | static PyObject* |
| 7933 | encoding_map_size(PyObject *obj, PyObject* args) |
| 7934 | { |
| 7935 | struct encoding_map *map = (struct encoding_map*)obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7936 | return PyLong_FromLong(sizeof(*map) - 1 + 16*map->count2 + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7937 | 128*map->count3); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7938 | } |
| 7939 | |
| 7940 | static PyMethodDef encoding_map_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7941 | {"size", encoding_map_size, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7942 | PyDoc_STR("Return the size (in bytes) of this object") }, |
| 7943 | { 0 } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7944 | }; |
| 7945 | |
| 7946 | static void |
| 7947 | encoding_map_dealloc(PyObject* o) |
| 7948 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7949 | PyObject_FREE(o); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7950 | } |
| 7951 | |
| 7952 | static PyTypeObject EncodingMapType = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7953 | PyVarObject_HEAD_INIT(NULL, 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7954 | "EncodingMap", /*tp_name*/ |
| 7955 | sizeof(struct encoding_map), /*tp_basicsize*/ |
| 7956 | 0, /*tp_itemsize*/ |
| 7957 | /* methods */ |
| 7958 | encoding_map_dealloc, /*tp_dealloc*/ |
| 7959 | 0, /*tp_print*/ |
| 7960 | 0, /*tp_getattr*/ |
| 7961 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 7962 | 0, /*tp_reserved*/ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7963 | 0, /*tp_repr*/ |
| 7964 | 0, /*tp_as_number*/ |
| 7965 | 0, /*tp_as_sequence*/ |
| 7966 | 0, /*tp_as_mapping*/ |
| 7967 | 0, /*tp_hash*/ |
| 7968 | 0, /*tp_call*/ |
| 7969 | 0, /*tp_str*/ |
| 7970 | 0, /*tp_getattro*/ |
| 7971 | 0, /*tp_setattro*/ |
| 7972 | 0, /*tp_as_buffer*/ |
| 7973 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 7974 | 0, /*tp_doc*/ |
| 7975 | 0, /*tp_traverse*/ |
| 7976 | 0, /*tp_clear*/ |
| 7977 | 0, /*tp_richcompare*/ |
| 7978 | 0, /*tp_weaklistoffset*/ |
| 7979 | 0, /*tp_iter*/ |
| 7980 | 0, /*tp_iternext*/ |
| 7981 | encoding_map_methods, /*tp_methods*/ |
| 7982 | 0, /*tp_members*/ |
| 7983 | 0, /*tp_getset*/ |
| 7984 | 0, /*tp_base*/ |
| 7985 | 0, /*tp_dict*/ |
| 7986 | 0, /*tp_descr_get*/ |
| 7987 | 0, /*tp_descr_set*/ |
| 7988 | 0, /*tp_dictoffset*/ |
| 7989 | 0, /*tp_init*/ |
| 7990 | 0, /*tp_alloc*/ |
| 7991 | 0, /*tp_new*/ |
| 7992 | 0, /*tp_free*/ |
| 7993 | 0, /*tp_is_gc*/ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7994 | }; |
| 7995 | |
| 7996 | PyObject* |
| 7997 | PyUnicode_BuildEncodingMap(PyObject* string) |
| 7998 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7999 | PyObject *result; |
| 8000 | struct encoding_map *mresult; |
| 8001 | int i; |
| 8002 | int need_dict = 0; |
| 8003 | unsigned char level1[32]; |
| 8004 | unsigned char level2[512]; |
| 8005 | unsigned char *mlevel1, *mlevel2, *mlevel3; |
| 8006 | int count2 = 0, count3 = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8007 | int kind; |
| 8008 | void *data; |
| 8009 | Py_UCS4 ch; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8010 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8011 | if (!PyUnicode_Check(string) || PyUnicode_GET_LENGTH(string) != 256) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8012 | PyErr_BadArgument(); |
| 8013 | return NULL; |
| 8014 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8015 | kind = PyUnicode_KIND(string); |
| 8016 | data = PyUnicode_DATA(string); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8017 | memset(level1, 0xFF, sizeof level1); |
| 8018 | memset(level2, 0xFF, sizeof level2); |
| 8019 | |
| 8020 | /* If there isn't a one-to-one mapping of NULL to \0, |
| 8021 | or if there are non-BMP characters, we need to use |
| 8022 | a mapping dictionary. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8023 | if (PyUnicode_READ(kind, data, 0) != 0) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8024 | need_dict = 1; |
| 8025 | for (i = 1; i < 256; i++) { |
| 8026 | int l1, l2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8027 | ch = PyUnicode_READ(kind, data, i); |
| 8028 | if (ch == 0 || ch > 0xFFFF) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8029 | need_dict = 1; |
| 8030 | break; |
| 8031 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8032 | if (ch == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8033 | /* unmapped character */ |
| 8034 | continue; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8035 | l1 = ch >> 11; |
| 8036 | l2 = ch >> 7; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8037 | if (level1[l1] == 0xFF) |
| 8038 | level1[l1] = count2++; |
| 8039 | if (level2[l2] == 0xFF) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8040 | level2[l2] = count3++; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8041 | } |
| 8042 | |
| 8043 | if (count2 >= 0xFF || count3 >= 0xFF) |
| 8044 | need_dict = 1; |
| 8045 | |
| 8046 | if (need_dict) { |
| 8047 | PyObject *result = PyDict_New(); |
| 8048 | PyObject *key, *value; |
| 8049 | if (!result) |
| 8050 | return NULL; |
| 8051 | for (i = 0; i < 256; i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8052 | key = PyLong_FromLong(PyUnicode_READ(kind, data, i)); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8053 | value = PyLong_FromLong(i); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8054 | if (!key || !value) |
| 8055 | goto failed1; |
| 8056 | if (PyDict_SetItem(result, key, value) == -1) |
| 8057 | goto failed1; |
| 8058 | Py_DECREF(key); |
| 8059 | Py_DECREF(value); |
| 8060 | } |
| 8061 | return result; |
| 8062 | failed1: |
| 8063 | Py_XDECREF(key); |
| 8064 | Py_XDECREF(value); |
| 8065 | Py_DECREF(result); |
| 8066 | return NULL; |
| 8067 | } |
| 8068 | |
| 8069 | /* Create a three-level trie */ |
| 8070 | result = PyObject_MALLOC(sizeof(struct encoding_map) + |
| 8071 | 16*count2 + 128*count3 - 1); |
| 8072 | if (!result) |
| 8073 | return PyErr_NoMemory(); |
| 8074 | PyObject_Init(result, &EncodingMapType); |
| 8075 | mresult = (struct encoding_map*)result; |
| 8076 | mresult->count2 = count2; |
| 8077 | mresult->count3 = count3; |
| 8078 | mlevel1 = mresult->level1; |
| 8079 | mlevel2 = mresult->level23; |
| 8080 | mlevel3 = mresult->level23 + 16*count2; |
| 8081 | memcpy(mlevel1, level1, 32); |
| 8082 | memset(mlevel2, 0xFF, 16*count2); |
| 8083 | memset(mlevel3, 0, 128*count3); |
| 8084 | count3 = 0; |
| 8085 | for (i = 1; i < 256; i++) { |
| 8086 | int o1, o2, o3, i2, i3; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8087 | if (PyUnicode_READ(kind, data, i) == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8088 | /* unmapped character */ |
| 8089 | continue; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8090 | o1 = PyUnicode_READ(kind, data, i)>>11; |
| 8091 | o2 = (PyUnicode_READ(kind, data, i)>>7) & 0xF; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8092 | i2 = 16*mlevel1[o1] + o2; |
| 8093 | if (mlevel2[i2] == 0xFF) |
| 8094 | mlevel2[i2] = count3++; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8095 | o3 = PyUnicode_READ(kind, data, i) & 0x7F; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8096 | i3 = 128*mlevel2[i2] + o3; |
| 8097 | mlevel3[i3] = i; |
| 8098 | } |
| 8099 | return result; |
| 8100 | } |
| 8101 | |
| 8102 | static int |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 8103 | encoding_map_lookup(Py_UCS4 c, PyObject *mapping) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8104 | { |
| 8105 | struct encoding_map *map = (struct encoding_map*)mapping; |
| 8106 | int l1 = c>>11; |
| 8107 | int l2 = (c>>7) & 0xF; |
| 8108 | int l3 = c & 0x7F; |
| 8109 | int i; |
| 8110 | |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 8111 | if (c > 0xFFFF) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8112 | return -1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8113 | if (c == 0) |
| 8114 | return 0; |
| 8115 | /* level 1*/ |
| 8116 | i = map->level1[l1]; |
| 8117 | if (i == 0xFF) { |
| 8118 | return -1; |
| 8119 | } |
| 8120 | /* level 2*/ |
| 8121 | i = map->level23[16*i+l2]; |
| 8122 | if (i == 0xFF) { |
| 8123 | return -1; |
| 8124 | } |
| 8125 | /* level 3 */ |
| 8126 | i = map->level23[16*map->count2 + 128*i + l3]; |
| 8127 | if (i == 0) { |
| 8128 | return -1; |
| 8129 | } |
| 8130 | return i; |
| 8131 | } |
| 8132 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8133 | /* Lookup the character ch in the mapping. If the character |
| 8134 | can't be found, Py_None is returned (or NULL, if another |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 8135 | error occurred). */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8136 | static PyObject * |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 8137 | charmapencode_lookup(Py_UCS4 c, PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8138 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8139 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8140 | PyObject *x; |
| 8141 | |
| 8142 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8143 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8144 | x = PyObject_GetItem(mapping, w); |
| 8145 | Py_DECREF(w); |
| 8146 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8147 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 8148 | /* No mapping found means: mapping is undefined. */ |
| 8149 | PyErr_Clear(); |
| 8150 | x = Py_None; |
| 8151 | Py_INCREF(x); |
| 8152 | return x; |
| 8153 | } else |
| 8154 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8155 | } |
Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 8156 | else if (x == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8157 | return x; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8158 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8159 | long value = PyLong_AS_LONG(x); |
| 8160 | if (value < 0 || value > 255) { |
| 8161 | PyErr_SetString(PyExc_TypeError, |
| 8162 | "character mapping must be in range(256)"); |
| 8163 | Py_DECREF(x); |
| 8164 | return NULL; |
| 8165 | } |
| 8166 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8167 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8168 | else if (PyBytes_Check(x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8169 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8170 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8171 | /* wrong return value */ |
| 8172 | PyErr_Format(PyExc_TypeError, |
| 8173 | "character mapping must return integer, bytes or None, not %.400s", |
| 8174 | x->ob_type->tp_name); |
| 8175 | Py_DECREF(x); |
| 8176 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8177 | } |
| 8178 | } |
| 8179 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8180 | static int |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 8181 | charmapencode_resize(PyObject **outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8182 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8183 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
| 8184 | /* exponentially overallocate to minimize reallocations */ |
| 8185 | if (requiredsize < 2*outsize) |
| 8186 | requiredsize = 2*outsize; |
| 8187 | if (_PyBytes_Resize(outobj, requiredsize)) |
| 8188 | return -1; |
| 8189 | return 0; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8190 | } |
| 8191 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8192 | typedef enum charmapencode_result { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8193 | enc_SUCCESS, enc_FAILED, enc_EXCEPTION |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8194 | } charmapencode_result; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8195 | /* 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] | 8196 | various state variables. Resize the output bytes object if not enough |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8197 | space is available. Return a new reference to the object that |
| 8198 | was put in the output buffer, or Py_None, if the mapping was undefined |
| 8199 | (in which case no character was written) or NULL, if a |
Andrew M. Kuchling | 8294de5 | 2005-11-02 16:36:12 +0000 | [diff] [blame] | 8200 | reallocation error occurred. The caller must decref the result */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8201 | static charmapencode_result |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 8202 | charmapencode_output(Py_UCS4 c, PyObject *mapping, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8203 | PyObject **outobj, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8204 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8205 | PyObject *rep; |
| 8206 | char *outstart; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8207 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8208 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 8209 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8210 | int res = encoding_map_lookup(c, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8211 | Py_ssize_t requiredsize = *outpos+1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8212 | if (res == -1) |
| 8213 | return enc_FAILED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8214 | if (outsize<requiredsize) |
| 8215 | if (charmapencode_resize(outobj, outpos, requiredsize)) |
| 8216 | return enc_EXCEPTION; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8217 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8218 | outstart[(*outpos)++] = (char)res; |
| 8219 | return enc_SUCCESS; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8220 | } |
| 8221 | |
| 8222 | rep = charmapencode_lookup(c, mapping); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8223 | if (rep==NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8224 | return enc_EXCEPTION; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8225 | else if (rep==Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8226 | Py_DECREF(rep); |
| 8227 | return enc_FAILED; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8228 | } else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8229 | if (PyLong_Check(rep)) { |
| 8230 | Py_ssize_t requiredsize = *outpos+1; |
| 8231 | if (outsize<requiredsize) |
| 8232 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 8233 | Py_DECREF(rep); |
| 8234 | return enc_EXCEPTION; |
| 8235 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8236 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8237 | outstart[(*outpos)++] = (char)PyLong_AS_LONG(rep); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8238 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8239 | else { |
| 8240 | const char *repchars = PyBytes_AS_STRING(rep); |
| 8241 | Py_ssize_t repsize = PyBytes_GET_SIZE(rep); |
| 8242 | Py_ssize_t requiredsize = *outpos+repsize; |
| 8243 | if (outsize<requiredsize) |
| 8244 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 8245 | Py_DECREF(rep); |
| 8246 | return enc_EXCEPTION; |
| 8247 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8248 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8249 | memcpy(outstart + *outpos, repchars, repsize); |
| 8250 | *outpos += repsize; |
| 8251 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8252 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8253 | Py_DECREF(rep); |
| 8254 | return enc_SUCCESS; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8255 | } |
| 8256 | |
| 8257 | /* handle an error in PyUnicode_EncodeCharmap |
| 8258 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8259 | static int |
| 8260 | charmap_encoding_error( |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8261 | PyObject *unicode, Py_ssize_t *inpos, PyObject *mapping, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8262 | PyObject **exceptionObject, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 8263 | int *known_errorHandler, PyObject **errorHandler, const char *errors, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 8264 | PyObject **res, Py_ssize_t *respos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8265 | { |
| 8266 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8267 | Py_ssize_t size, repsize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8268 | Py_ssize_t newpos; |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8269 | enum PyUnicode_Kind kind; |
| 8270 | void *data; |
| 8271 | Py_ssize_t index; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8272 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8273 | Py_ssize_t collstartpos = *inpos; |
| 8274 | Py_ssize_t collendpos = *inpos+1; |
| 8275 | Py_ssize_t collpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8276 | char *encoding = "charmap"; |
| 8277 | char *reason = "character maps to <undefined>"; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8278 | charmapencode_result x; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8279 | Py_UCS4 ch; |
Brian Curtin | 2787ea4 | 2011-11-02 15:09:37 -0500 | [diff] [blame] | 8280 | int val; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8281 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 8282 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8283 | return -1; |
| 8284 | size = PyUnicode_GET_LENGTH(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8285 | /* find all unencodable characters */ |
| 8286 | while (collendpos < size) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8287 | PyObject *rep; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 8288 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8289 | ch = PyUnicode_READ_CHAR(unicode, collendpos); |
Brian Curtin | 2787ea4 | 2011-11-02 15:09:37 -0500 | [diff] [blame] | 8290 | val = encoding_map_lookup(ch, mapping); |
| 8291 | if (val != -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8292 | break; |
| 8293 | ++collendpos; |
| 8294 | continue; |
| 8295 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8296 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8297 | ch = PyUnicode_READ_CHAR(unicode, collendpos); |
| 8298 | rep = charmapencode_lookup(ch, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8299 | if (rep==NULL) |
| 8300 | return -1; |
| 8301 | else if (rep!=Py_None) { |
| 8302 | Py_DECREF(rep); |
| 8303 | break; |
| 8304 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8305 | Py_DECREF(rep); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8306 | ++collendpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8307 | } |
| 8308 | /* cache callback name lookup |
| 8309 | * (if not done yet, i.e. it's the first error) */ |
| 8310 | if (*known_errorHandler==-1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8311 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 8312 | *known_errorHandler = 1; |
| 8313 | else if (!strcmp(errors, "replace")) |
| 8314 | *known_errorHandler = 2; |
| 8315 | else if (!strcmp(errors, "ignore")) |
| 8316 | *known_errorHandler = 3; |
| 8317 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 8318 | *known_errorHandler = 4; |
| 8319 | else |
| 8320 | *known_errorHandler = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8321 | } |
| 8322 | switch (*known_errorHandler) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8323 | case 1: /* strict */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8324 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8325 | return -1; |
| 8326 | case 2: /* replace */ |
| 8327 | for (collpos = collstartpos; collpos<collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8328 | x = charmapencode_output('?', mapping, res, respos); |
| 8329 | if (x==enc_EXCEPTION) { |
| 8330 | return -1; |
| 8331 | } |
| 8332 | else if (x==enc_FAILED) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8333 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8334 | return -1; |
| 8335 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8336 | } |
| 8337 | /* fall through */ |
| 8338 | case 3: /* ignore */ |
| 8339 | *inpos = collendpos; |
| 8340 | break; |
| 8341 | case 4: /* xmlcharrefreplace */ |
| 8342 | /* generate replacement (temporarily (mis)uses p) */ |
| 8343 | for (collpos = collstartpos; collpos < collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8344 | char buffer[2+29+1+1]; |
| 8345 | char *cp; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8346 | sprintf(buffer, "&#%d;", (int)PyUnicode_READ_CHAR(unicode, collpos)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8347 | for (cp = buffer; *cp; ++cp) { |
| 8348 | x = charmapencode_output(*cp, mapping, res, respos); |
| 8349 | if (x==enc_EXCEPTION) |
| 8350 | return -1; |
| 8351 | else if (x==enc_FAILED) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8352 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8353 | return -1; |
| 8354 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8355 | } |
| 8356 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8357 | *inpos = collendpos; |
| 8358 | break; |
| 8359 | default: |
| 8360 | repunicode = unicode_encode_call_errorhandler(errors, errorHandler, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8361 | encoding, reason, unicode, exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8362 | collstartpos, collendpos, &newpos); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8363 | if (repunicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8364 | return -1; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8365 | if (PyBytes_Check(repunicode)) { |
| 8366 | /* Directly copy bytes result to output. */ |
| 8367 | Py_ssize_t outsize = PyBytes_Size(*res); |
| 8368 | Py_ssize_t requiredsize; |
| 8369 | repsize = PyBytes_Size(repunicode); |
| 8370 | requiredsize = *respos + repsize; |
| 8371 | if (requiredsize > outsize) |
| 8372 | /* Make room for all additional bytes. */ |
| 8373 | if (charmapencode_resize(res, respos, requiredsize)) { |
| 8374 | Py_DECREF(repunicode); |
| 8375 | return -1; |
| 8376 | } |
| 8377 | memcpy(PyBytes_AsString(*res) + *respos, |
| 8378 | PyBytes_AsString(repunicode), repsize); |
| 8379 | *respos += repsize; |
| 8380 | *inpos = newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 8381 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8382 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 8383 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8384 | /* generate replacement */ |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 8385 | if (PyUnicode_READY(repunicode) == -1) { |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8386 | Py_DECREF(repunicode); |
| 8387 | return -1; |
| 8388 | } |
Victor Stinner | 9e30aa5 | 2011-11-21 02:49:52 +0100 | [diff] [blame] | 8389 | repsize = PyUnicode_GET_LENGTH(repunicode); |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8390 | data = PyUnicode_DATA(repunicode); |
| 8391 | kind = PyUnicode_KIND(repunicode); |
| 8392 | for (index = 0; index < repsize; index++) { |
| 8393 | Py_UCS4 repch = PyUnicode_READ(kind, data, index); |
| 8394 | x = charmapencode_output(repch, mapping, res, respos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8395 | if (x==enc_EXCEPTION) { |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8396 | Py_DECREF(repunicode); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8397 | return -1; |
| 8398 | } |
| 8399 | else if (x==enc_FAILED) { |
| 8400 | Py_DECREF(repunicode); |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8401 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8402 | return -1; |
| 8403 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8404 | } |
| 8405 | *inpos = newpos; |
| 8406 | Py_DECREF(repunicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8407 | } |
| 8408 | return 0; |
| 8409 | } |
| 8410 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8411 | PyObject * |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8412 | _PyUnicode_EncodeCharmap(PyObject *unicode, |
| 8413 | PyObject *mapping, |
| 8414 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8415 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8416 | /* output object */ |
| 8417 | PyObject *res = NULL; |
| 8418 | /* current input position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8419 | Py_ssize_t inpos = 0; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8420 | Py_ssize_t size; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8421 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8422 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8423 | PyObject *errorHandler = NULL; |
| 8424 | PyObject *exc = NULL; |
| 8425 | /* the following variable is used for caching string comparisons |
| 8426 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 8427 | * 3=ignore, 4=xmlcharrefreplace */ |
| 8428 | int known_errorHandler = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8429 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 8430 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8431 | return NULL; |
| 8432 | size = PyUnicode_GET_LENGTH(unicode); |
| 8433 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8434 | /* Default to Latin-1 */ |
| 8435 | if (mapping == NULL) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8436 | return unicode_encode_ucs1(unicode, errors, 256); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8437 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8438 | /* allocate enough for a simple encoding without |
| 8439 | replacements, if we need more, we'll resize */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8440 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8441 | if (res == NULL) |
| 8442 | goto onError; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 8443 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8444 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8445 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8446 | while (inpos<size) { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8447 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, inpos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8448 | /* try to encode it */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8449 | charmapencode_result x = charmapencode_output(ch, mapping, &res, &respos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8450 | if (x==enc_EXCEPTION) /* error */ |
| 8451 | goto onError; |
| 8452 | if (x==enc_FAILED) { /* unencodable character */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8453 | if (charmap_encoding_error(unicode, &inpos, mapping, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8454 | &exc, |
| 8455 | &known_errorHandler, &errorHandler, errors, |
| 8456 | &res, &respos)) { |
| 8457 | goto onError; |
| 8458 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8459 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8460 | else |
| 8461 | /* done with this character => adjust input position */ |
| 8462 | ++inpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8463 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8464 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8465 | /* Resize if we allocated to much */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8466 | if (respos<PyBytes_GET_SIZE(res)) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 8467 | if (_PyBytes_Resize(&res, respos) < 0) |
| 8468 | goto onError; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 8469 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8470 | Py_XDECREF(exc); |
| 8471 | Py_XDECREF(errorHandler); |
| 8472 | return res; |
| 8473 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8474 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8475 | Py_XDECREF(res); |
| 8476 | Py_XDECREF(exc); |
| 8477 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8478 | return NULL; |
| 8479 | } |
| 8480 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8481 | /* Deprecated */ |
| 8482 | PyObject * |
| 8483 | PyUnicode_EncodeCharmap(const Py_UNICODE *p, |
| 8484 | Py_ssize_t size, |
| 8485 | PyObject *mapping, |
| 8486 | const char *errors) |
| 8487 | { |
| 8488 | PyObject *result; |
| 8489 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 8490 | if (unicode == NULL) |
| 8491 | return NULL; |
| 8492 | result = _PyUnicode_EncodeCharmap(unicode, mapping, errors); |
| 8493 | Py_DECREF(unicode); |
Victor Stinner | fc026c9 | 2011-11-04 00:24:51 +0100 | [diff] [blame] | 8494 | return result; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8495 | } |
| 8496 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8497 | PyObject * |
| 8498 | PyUnicode_AsCharmapString(PyObject *unicode, |
| 8499 | PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8500 | { |
| 8501 | if (!PyUnicode_Check(unicode) || mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8502 | PyErr_BadArgument(); |
| 8503 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8504 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8505 | return _PyUnicode_EncodeCharmap(unicode, mapping, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8506 | } |
| 8507 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8508 | /* create or adjust a UnicodeTranslateError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8509 | static void |
| 8510 | make_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8511 | PyObject *unicode, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8512 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 8513 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8514 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8515 | if (*exceptionObject == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8516 | *exceptionObject = _PyUnicodeTranslateError_Create( |
| 8517 | unicode, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8518 | } |
| 8519 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8520 | if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
| 8521 | goto onError; |
| 8522 | if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) |
| 8523 | goto onError; |
| 8524 | if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) |
| 8525 | goto onError; |
| 8526 | return; |
| 8527 | onError: |
| 8528 | Py_DECREF(*exceptionObject); |
| 8529 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8530 | } |
| 8531 | } |
| 8532 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8533 | /* raises a UnicodeTranslateError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8534 | static void |
| 8535 | raise_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8536 | PyObject *unicode, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8537 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 8538 | const char *reason) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8539 | { |
| 8540 | make_translate_exception(exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8541 | unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8542 | if (*exceptionObject != NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8543 | PyCodec_StrictErrors(*exceptionObject); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8544 | } |
| 8545 | |
| 8546 | /* error handling callback helper: |
| 8547 | build arguments, call the callback and check the arguments, |
| 8548 | put the result into newpos and return the replacement string, which |
| 8549 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8550 | static PyObject * |
| 8551 | unicode_translate_call_errorhandler(const char *errors, |
| 8552 | PyObject **errorHandler, |
| 8553 | const char *reason, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8554 | PyObject *unicode, PyObject **exceptionObject, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8555 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 8556 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8557 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 8558 | 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] | 8559 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8560 | Py_ssize_t i_newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8561 | PyObject *restuple; |
| 8562 | PyObject *resunicode; |
| 8563 | |
| 8564 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8565 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8566 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8567 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8568 | } |
| 8569 | |
| 8570 | make_translate_exception(exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8571 | unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8572 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8573 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8574 | |
| 8575 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8576 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8577 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8578 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8579 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 8580 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8581 | Py_DECREF(restuple); |
| 8582 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8583 | } |
| 8584 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8585 | &resunicode, &i_newpos)) { |
| 8586 | Py_DECREF(restuple); |
| 8587 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8588 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8589 | if (i_newpos<0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8590 | *newpos = PyUnicode_GET_LENGTH(unicode)+i_newpos; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8591 | else |
| 8592 | *newpos = i_newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8593 | if (*newpos<0 || *newpos>PyUnicode_GET_LENGTH(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8594 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 8595 | Py_DECREF(restuple); |
| 8596 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 8597 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8598 | Py_INCREF(resunicode); |
| 8599 | Py_DECREF(restuple); |
| 8600 | return resunicode; |
| 8601 | } |
| 8602 | |
| 8603 | /* Lookup the character ch in the mapping and put the result in result, |
| 8604 | which must be decrefed by the caller. |
| 8605 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8606 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8607 | charmaptranslate_lookup(Py_UCS4 c, PyObject *mapping, PyObject **result) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8608 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8609 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8610 | PyObject *x; |
| 8611 | |
| 8612 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8613 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8614 | x = PyObject_GetItem(mapping, w); |
| 8615 | Py_DECREF(w); |
| 8616 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8617 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 8618 | /* No mapping found means: use 1:1 mapping. */ |
| 8619 | PyErr_Clear(); |
| 8620 | *result = NULL; |
| 8621 | return 0; |
| 8622 | } else |
| 8623 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8624 | } |
| 8625 | else if (x == Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8626 | *result = x; |
| 8627 | return 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8628 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8629 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8630 | long value = PyLong_AS_LONG(x); |
| 8631 | long max = PyUnicode_GetMax(); |
| 8632 | if (value < 0 || value > max) { |
| 8633 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5a2f7e60 | 2007-10-24 21:13:09 +0000 | [diff] [blame] | 8634 | "character mapping must be in range(0x%x)", max+1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8635 | Py_DECREF(x); |
| 8636 | return -1; |
| 8637 | } |
| 8638 | *result = x; |
| 8639 | return 0; |
| 8640 | } |
| 8641 | else if (PyUnicode_Check(x)) { |
| 8642 | *result = x; |
| 8643 | return 0; |
| 8644 | } |
| 8645 | else { |
| 8646 | /* wrong return value */ |
| 8647 | PyErr_SetString(PyExc_TypeError, |
| 8648 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8649 | Py_DECREF(x); |
| 8650 | return -1; |
| 8651 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8652 | } |
| 8653 | /* ensure that *outobj is at least requiredsize characters long, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8654 | if not reallocate and adjust various state variables. |
| 8655 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8656 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8657 | charmaptranslate_makespace(Py_UCS4 **outobj, Py_ssize_t *psize, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8658 | Py_ssize_t requiredsize) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8659 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8660 | Py_ssize_t oldsize = *psize; |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 8661 | if (requiredsize > oldsize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8662 | /* exponentially overallocate to minimize reallocations */ |
| 8663 | if (requiredsize < 2 * oldsize) |
| 8664 | requiredsize = 2 * oldsize; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8665 | *outobj = PyMem_Realloc(*outobj, requiredsize * sizeof(Py_UCS4)); |
| 8666 | if (*outobj == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8667 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8668 | *psize = requiredsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8669 | } |
| 8670 | return 0; |
| 8671 | } |
| 8672 | /* lookup the character, put the result in the output string and adjust |
| 8673 | various state variables. Return a new reference to the object that |
| 8674 | was put in the output buffer in *result, or Py_None, if the mapping was |
| 8675 | undefined (in which case no character was written). |
| 8676 | The called must decref result. |
| 8677 | Return 0 on success, -1 on error. */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8678 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8679 | charmaptranslate_output(PyObject *input, Py_ssize_t ipos, |
| 8680 | PyObject *mapping, Py_UCS4 **output, |
| 8681 | Py_ssize_t *osize, Py_ssize_t *opos, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8682 | PyObject **res) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8683 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8684 | Py_UCS4 curinp = PyUnicode_READ_CHAR(input, ipos); |
| 8685 | if (charmaptranslate_lookup(curinp, mapping, res)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8686 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8687 | if (*res==NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8688 | /* not found => default to 1:1 mapping */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8689 | (*output)[(*opos)++] = curinp; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8690 | } |
| 8691 | else if (*res==Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8692 | ; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8693 | else if (PyLong_Check(*res)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8694 | /* 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] | 8695 | (*output)[(*opos)++] = (Py_UCS4)PyLong_AS_LONG(*res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8696 | } |
| 8697 | else if (PyUnicode_Check(*res)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8698 | Py_ssize_t repsize; |
| 8699 | if (PyUnicode_READY(*res) == -1) |
| 8700 | return -1; |
| 8701 | repsize = PyUnicode_GET_LENGTH(*res); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8702 | if (repsize==1) { |
| 8703 | /* 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] | 8704 | (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8705 | } |
| 8706 | else if (repsize!=0) { |
| 8707 | /* more than one character */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8708 | Py_ssize_t requiredsize = *opos + |
| 8709 | (PyUnicode_GET_LENGTH(input) - ipos) + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8710 | repsize - 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8711 | Py_ssize_t i; |
| 8712 | if (charmaptranslate_makespace(output, osize, requiredsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8713 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8714 | for(i = 0; i < repsize; i++) |
| 8715 | (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, i); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8716 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8717 | } |
| 8718 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8719 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8720 | return 0; |
| 8721 | } |
| 8722 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8723 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8724 | _PyUnicode_TranslateCharmap(PyObject *input, |
| 8725 | PyObject *mapping, |
| 8726 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8727 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8728 | /* input object */ |
| 8729 | char *idata; |
| 8730 | Py_ssize_t size, i; |
| 8731 | int kind; |
| 8732 | /* output buffer */ |
| 8733 | Py_UCS4 *output = NULL; |
| 8734 | Py_ssize_t osize; |
| 8735 | PyObject *res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8736 | /* current output position */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8737 | Py_ssize_t opos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8738 | char *reason = "character maps to <undefined>"; |
| 8739 | PyObject *errorHandler = NULL; |
| 8740 | PyObject *exc = NULL; |
| 8741 | /* the following variable is used for caching string comparisons |
| 8742 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 8743 | * 3=ignore, 4=xmlcharrefreplace */ |
| 8744 | int known_errorHandler = -1; |
| 8745 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8746 | if (mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8747 | PyErr_BadArgument(); |
| 8748 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8749 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8750 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8751 | if (PyUnicode_READY(input) == -1) |
| 8752 | return NULL; |
| 8753 | idata = (char*)PyUnicode_DATA(input); |
| 8754 | kind = PyUnicode_KIND(input); |
| 8755 | size = PyUnicode_GET_LENGTH(input); |
| 8756 | i = 0; |
| 8757 | |
| 8758 | if (size == 0) { |
| 8759 | Py_INCREF(input); |
| 8760 | return input; |
| 8761 | } |
| 8762 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8763 | /* allocate enough for a simple 1:1 translation without |
| 8764 | replacements, if we need more, we'll resize */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8765 | osize = size; |
| 8766 | output = PyMem_Malloc(osize * sizeof(Py_UCS4)); |
| 8767 | opos = 0; |
| 8768 | if (output == NULL) { |
| 8769 | PyErr_NoMemory(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8770 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8771 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8772 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8773 | while (i<size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8774 | /* try to encode it */ |
| 8775 | PyObject *x = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8776 | if (charmaptranslate_output(input, i, mapping, |
| 8777 | &output, &osize, &opos, &x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8778 | Py_XDECREF(x); |
| 8779 | goto onError; |
| 8780 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8781 | Py_XDECREF(x); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8782 | if (x!=Py_None) /* it worked => adjust input pointer */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8783 | ++i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8784 | else { /* untranslatable character */ |
| 8785 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
| 8786 | Py_ssize_t repsize; |
| 8787 | Py_ssize_t newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8788 | Py_ssize_t uni2; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8789 | /* startpos for collecting untranslatable chars */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8790 | Py_ssize_t collstart = i; |
| 8791 | Py_ssize_t collend = i+1; |
| 8792 | Py_ssize_t coll; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8793 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8794 | /* find all untranslatable characters */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8795 | while (collend < size) { |
| 8796 | if (charmaptranslate_lookup(PyUnicode_READ(kind,idata, collend), mapping, &x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8797 | goto onError; |
| 8798 | Py_XDECREF(x); |
| 8799 | if (x!=Py_None) |
| 8800 | break; |
| 8801 | ++collend; |
| 8802 | } |
| 8803 | /* cache callback name lookup |
| 8804 | * (if not done yet, i.e. it's the first error) */ |
| 8805 | if (known_errorHandler==-1) { |
| 8806 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 8807 | known_errorHandler = 1; |
| 8808 | else if (!strcmp(errors, "replace")) |
| 8809 | known_errorHandler = 2; |
| 8810 | else if (!strcmp(errors, "ignore")) |
| 8811 | known_errorHandler = 3; |
| 8812 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 8813 | known_errorHandler = 4; |
| 8814 | else |
| 8815 | known_errorHandler = 0; |
| 8816 | } |
| 8817 | switch (known_errorHandler) { |
| 8818 | case 1: /* strict */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8819 | raise_translate_exception(&exc, input, collstart, |
| 8820 | collend, reason); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8821 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8822 | case 2: /* replace */ |
| 8823 | /* 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] | 8824 | for (coll = collstart; coll<collend; coll++) |
| 8825 | output[opos++] = '?'; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8826 | /* fall through */ |
| 8827 | case 3: /* ignore */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8828 | i = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8829 | break; |
| 8830 | case 4: /* xmlcharrefreplace */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8831 | /* generate replacement (temporarily (mis)uses i) */ |
| 8832 | for (i = collstart; i < collend; ++i) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8833 | char buffer[2+29+1+1]; |
| 8834 | char *cp; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8835 | sprintf(buffer, "&#%d;", PyUnicode_READ(kind, idata, i)); |
| 8836 | if (charmaptranslate_makespace(&output, &osize, |
| 8837 | opos+strlen(buffer)+(size-collend))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8838 | goto onError; |
| 8839 | for (cp = buffer; *cp; ++cp) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8840 | output[opos++] = *cp; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8841 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8842 | i = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8843 | break; |
| 8844 | default: |
| 8845 | repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8846 | reason, input, &exc, |
| 8847 | collstart, collend, &newpos); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 8848 | if (repunicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8849 | goto onError; |
Benjamin Peterson | 9ca3ffa | 2012-01-01 16:04:29 -0600 | [diff] [blame] | 8850 | if (PyUnicode_READY(repunicode) == -1) { |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 8851 | Py_DECREF(repunicode); |
| 8852 | goto onError; |
| 8853 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8854 | /* generate replacement */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8855 | repsize = PyUnicode_GET_LENGTH(repunicode); |
| 8856 | if (charmaptranslate_makespace(&output, &osize, |
| 8857 | opos+repsize+(size-collend))) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8858 | Py_DECREF(repunicode); |
| 8859 | goto onError; |
| 8860 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8861 | for (uni2 = 0; repsize-->0; ++uni2) |
| 8862 | output[opos++] = PyUnicode_READ_CHAR(repunicode, uni2); |
| 8863 | i = newpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8864 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8865 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8866 | } |
| 8867 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8868 | res = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, output, opos); |
| 8869 | if (!res) |
| 8870 | goto onError; |
| 8871 | PyMem_Free(output); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8872 | Py_XDECREF(exc); |
| 8873 | Py_XDECREF(errorHandler); |
| 8874 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8875 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8876 | onError: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8877 | PyMem_Free(output); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8878 | Py_XDECREF(exc); |
| 8879 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8880 | return NULL; |
| 8881 | } |
| 8882 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8883 | /* Deprecated. Use PyUnicode_Translate instead. */ |
| 8884 | PyObject * |
| 8885 | PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
| 8886 | Py_ssize_t size, |
| 8887 | PyObject *mapping, |
| 8888 | const char *errors) |
| 8889 | { |
| 8890 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 8891 | if (!unicode) |
| 8892 | return NULL; |
| 8893 | return _PyUnicode_TranslateCharmap(unicode, mapping, errors); |
| 8894 | } |
| 8895 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8896 | PyObject * |
| 8897 | PyUnicode_Translate(PyObject *str, |
| 8898 | PyObject *mapping, |
| 8899 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8900 | { |
| 8901 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8902 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8903 | str = PyUnicode_FromObject(str); |
| 8904 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8905 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8906 | result = _PyUnicode_TranslateCharmap(str, mapping, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8907 | Py_DECREF(str); |
| 8908 | return result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8909 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8910 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8911 | Py_XDECREF(str); |
| 8912 | return NULL; |
| 8913 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8914 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8915 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8916 | fix_decimal_and_space_to_ascii(PyObject *self) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8917 | { |
| 8918 | /* No need to call PyUnicode_READY(self) because this function is only |
| 8919 | called as a callback from fixup() which does it already. */ |
| 8920 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 8921 | const int kind = PyUnicode_KIND(self); |
| 8922 | void *data = PyUnicode_DATA(self); |
| 8923 | Py_UCS4 maxchar = 0, ch, fixed; |
Benjamin Peterson | 821e4cf | 2012-01-12 15:40:18 -0500 | [diff] [blame] | 8924 | int modified = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8925 | Py_ssize_t i; |
| 8926 | |
| 8927 | for (i = 0; i < len; ++i) { |
| 8928 | ch = PyUnicode_READ(kind, data, i); |
| 8929 | fixed = 0; |
| 8930 | if (ch > 127) { |
| 8931 | if (Py_UNICODE_ISSPACE(ch)) |
| 8932 | fixed = ' '; |
| 8933 | else { |
| 8934 | const int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8935 | if (decimal >= 0) |
| 8936 | fixed = '0' + decimal; |
| 8937 | } |
| 8938 | if (fixed != 0) { |
Benjamin Peterson | 821e4cf | 2012-01-12 15:40:18 -0500 | [diff] [blame] | 8939 | modified = 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8940 | if (fixed > maxchar) |
| 8941 | maxchar = fixed; |
| 8942 | PyUnicode_WRITE(kind, data, i, fixed); |
| 8943 | } |
| 8944 | else if (ch > maxchar) |
| 8945 | maxchar = ch; |
| 8946 | } |
| 8947 | else if (ch > maxchar) |
| 8948 | maxchar = ch; |
| 8949 | } |
| 8950 | |
Benjamin Peterson | 821e4cf | 2012-01-12 15:40:18 -0500 | [diff] [blame] | 8951 | return (modified) ? maxchar : 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8952 | } |
| 8953 | |
| 8954 | PyObject * |
| 8955 | _PyUnicode_TransformDecimalAndSpaceToASCII(PyObject *unicode) |
| 8956 | { |
| 8957 | if (!PyUnicode_Check(unicode)) { |
| 8958 | PyErr_BadInternalCall(); |
| 8959 | return NULL; |
| 8960 | } |
| 8961 | if (PyUnicode_READY(unicode) == -1) |
| 8962 | return NULL; |
| 8963 | if (PyUnicode_MAX_CHAR_VALUE(unicode) <= 127) { |
| 8964 | /* If the string is already ASCII, just return the same string */ |
| 8965 | Py_INCREF(unicode); |
| 8966 | return unicode; |
| 8967 | } |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8968 | return fixup(unicode, fix_decimal_and_space_to_ascii); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8969 | } |
| 8970 | |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8971 | PyObject * |
| 8972 | PyUnicode_TransformDecimalToASCII(Py_UNICODE *s, |
| 8973 | Py_ssize_t length) |
| 8974 | { |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8975 | PyObject *decimal; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8976 | Py_ssize_t i; |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8977 | Py_UCS4 maxchar; |
| 8978 | enum PyUnicode_Kind kind; |
| 8979 | void *data; |
| 8980 | |
Victor Stinner | 99d7ad0 | 2012-02-22 13:37:39 +0100 | [diff] [blame] | 8981 | maxchar = 127; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8982 | for (i = 0; i < length; i++) { |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8983 | Py_UNICODE ch = s[i]; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8984 | if (ch > 127) { |
| 8985 | int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8986 | if (decimal >= 0) |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8987 | ch = '0' + decimal; |
Victor Stinner | 99d7ad0 | 2012-02-22 13:37:39 +0100 | [diff] [blame] | 8988 | maxchar = Py_MAX(maxchar, ch); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8989 | } |
| 8990 | } |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8991 | |
| 8992 | /* Copy to a new string */ |
| 8993 | decimal = PyUnicode_New(length, maxchar); |
| 8994 | if (decimal == NULL) |
| 8995 | return decimal; |
| 8996 | kind = PyUnicode_KIND(decimal); |
| 8997 | data = PyUnicode_DATA(decimal); |
| 8998 | /* Iterate over code points */ |
| 8999 | for (i = 0; i < length; i++) { |
| 9000 | Py_UNICODE ch = s[i]; |
| 9001 | if (ch > 127) { |
| 9002 | int decimal = Py_UNICODE_TODECIMAL(ch); |
| 9003 | if (decimal >= 0) |
| 9004 | ch = '0' + decimal; |
| 9005 | } |
| 9006 | PyUnicode_WRITE(kind, data, i, ch); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9007 | } |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 9008 | return unicode_result(decimal); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 9009 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 9010 | /* --- Decimal Encoder ---------------------------------------------------- */ |
| 9011 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9012 | int |
| 9013 | PyUnicode_EncodeDecimal(Py_UNICODE *s, |
| 9014 | Py_ssize_t length, |
| 9015 | char *output, |
| 9016 | const char *errors) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 9017 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 9018 | PyObject *unicode; |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 9019 | Py_ssize_t i; |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 9020 | enum PyUnicode_Kind kind; |
| 9021 | void *data; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 9022 | |
| 9023 | if (output == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9024 | PyErr_BadArgument(); |
| 9025 | return -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 9026 | } |
| 9027 | |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 9028 | unicode = PyUnicode_FromUnicode(s, length); |
| 9029 | if (unicode == NULL) |
| 9030 | return -1; |
| 9031 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 9032 | if (PyUnicode_READY(unicode) == -1) { |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 9033 | Py_DECREF(unicode); |
| 9034 | return -1; |
| 9035 | } |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 9036 | kind = PyUnicode_KIND(unicode); |
| 9037 | data = PyUnicode_DATA(unicode); |
| 9038 | |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 9039 | for (i=0; i < length; ) { |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 9040 | PyObject *exc; |
| 9041 | Py_UCS4 ch; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9042 | int decimal; |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 9043 | Py_ssize_t startpos; |
| 9044 | |
| 9045 | ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9046 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9047 | if (Py_UNICODE_ISSPACE(ch)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9048 | *output++ = ' '; |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 9049 | i++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9050 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9051 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9052 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 9053 | if (decimal >= 0) { |
| 9054 | *output++ = '0' + decimal; |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 9055 | i++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9056 | continue; |
| 9057 | } |
| 9058 | if (0 < ch && ch < 256) { |
| 9059 | *output++ = (char)ch; |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 9060 | i++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9061 | continue; |
| 9062 | } |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 9063 | |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 9064 | startpos = i; |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 9065 | exc = NULL; |
| 9066 | raise_encode_exception(&exc, "decimal", unicode, |
| 9067 | startpos, startpos+1, |
| 9068 | "invalid decimal Unicode string"); |
| 9069 | Py_XDECREF(exc); |
| 9070 | Py_DECREF(unicode); |
| 9071 | return -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 9072 | } |
| 9073 | /* 0-terminate the output string */ |
| 9074 | *output++ = '\0'; |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 9075 | Py_DECREF(unicode); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 9076 | return 0; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 9077 | } |
| 9078 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9079 | /* --- Helpers ------------------------------------------------------------ */ |
| 9080 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9081 | static Py_ssize_t |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 9082 | any_find_slice(int direction, PyObject* s1, PyObject* s2, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9083 | Py_ssize_t start, |
| 9084 | Py_ssize_t end) |
| 9085 | { |
| 9086 | int kind1, kind2, kind; |
| 9087 | void *buf1, *buf2; |
| 9088 | Py_ssize_t len1, len2, result; |
| 9089 | |
| 9090 | kind1 = PyUnicode_KIND(s1); |
| 9091 | kind2 = PyUnicode_KIND(s2); |
| 9092 | kind = kind1 > kind2 ? kind1 : kind2; |
| 9093 | buf1 = PyUnicode_DATA(s1); |
| 9094 | buf2 = PyUnicode_DATA(s2); |
| 9095 | if (kind1 != kind) |
| 9096 | buf1 = _PyUnicode_AsKind(s1, kind); |
| 9097 | if (!buf1) |
| 9098 | return -2; |
| 9099 | if (kind2 != kind) |
| 9100 | buf2 = _PyUnicode_AsKind(s2, kind); |
| 9101 | if (!buf2) { |
| 9102 | if (kind1 != kind) PyMem_Free(buf1); |
| 9103 | return -2; |
| 9104 | } |
| 9105 | len1 = PyUnicode_GET_LENGTH(s1); |
| 9106 | len2 = PyUnicode_GET_LENGTH(s2); |
| 9107 | |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 9108 | if (direction > 0) { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9109 | switch (kind) { |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 9110 | case PyUnicode_1BYTE_KIND: |
| 9111 | if (PyUnicode_IS_ASCII(s1) && PyUnicode_IS_ASCII(s2)) |
| 9112 | result = asciilib_find_slice(buf1, len1, buf2, len2, start, end); |
| 9113 | else |
| 9114 | result = ucs1lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 9115 | break; |
| 9116 | case PyUnicode_2BYTE_KIND: |
| 9117 | result = ucs2lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 9118 | break; |
| 9119 | case PyUnicode_4BYTE_KIND: |
| 9120 | result = ucs4lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 9121 | break; |
| 9122 | default: |
| 9123 | assert(0); result = -2; |
| 9124 | } |
| 9125 | } |
| 9126 | else { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9127 | switch (kind) { |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 9128 | case PyUnicode_1BYTE_KIND: |
| 9129 | if (PyUnicode_IS_ASCII(s1) && PyUnicode_IS_ASCII(s2)) |
| 9130 | result = asciilib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 9131 | else |
| 9132 | result = ucs1lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 9133 | break; |
| 9134 | case PyUnicode_2BYTE_KIND: |
| 9135 | result = ucs2lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 9136 | break; |
| 9137 | case PyUnicode_4BYTE_KIND: |
| 9138 | result = ucs4lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 9139 | break; |
| 9140 | default: |
| 9141 | assert(0); result = -2; |
| 9142 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9143 | } |
| 9144 | |
| 9145 | if (kind1 != kind) |
| 9146 | PyMem_Free(buf1); |
| 9147 | if (kind2 != kind) |
| 9148 | PyMem_Free(buf2); |
| 9149 | |
| 9150 | return result; |
| 9151 | } |
| 9152 | |
| 9153 | Py_ssize_t |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9154 | _PyUnicode_InsertThousandsGrouping( |
| 9155 | PyObject *unicode, Py_ssize_t index, |
| 9156 | Py_ssize_t n_buffer, |
| 9157 | void *digits, Py_ssize_t n_digits, |
| 9158 | Py_ssize_t min_width, |
| 9159 | const char *grouping, PyObject *thousands_sep, |
| 9160 | Py_UCS4 *maxchar) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9161 | { |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9162 | unsigned int kind, thousands_sep_kind; |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9163 | char *data, *thousands_sep_data; |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9164 | Py_ssize_t thousands_sep_len; |
| 9165 | Py_ssize_t len; |
| 9166 | |
| 9167 | if (unicode != NULL) { |
| 9168 | kind = PyUnicode_KIND(unicode); |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9169 | data = (char *) PyUnicode_DATA(unicode) + index * kind; |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9170 | } |
| 9171 | else { |
| 9172 | kind = PyUnicode_1BYTE_KIND; |
| 9173 | data = NULL; |
| 9174 | } |
| 9175 | thousands_sep_kind = PyUnicode_KIND(thousands_sep); |
| 9176 | thousands_sep_data = PyUnicode_DATA(thousands_sep); |
| 9177 | thousands_sep_len = PyUnicode_GET_LENGTH(thousands_sep); |
| 9178 | if (unicode != NULL && thousands_sep_kind != kind) { |
Victor Stinner | 90f50d4 | 2012-02-24 01:44:47 +0100 | [diff] [blame] | 9179 | if (thousands_sep_kind < kind) { |
| 9180 | thousands_sep_data = _PyUnicode_AsKind(thousands_sep, kind); |
| 9181 | if (!thousands_sep_data) |
| 9182 | return -1; |
| 9183 | } |
| 9184 | else { |
| 9185 | data = _PyUnicode_AsKind(unicode, thousands_sep_kind); |
| 9186 | if (!data) |
| 9187 | return -1; |
| 9188 | } |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9189 | } |
| 9190 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9191 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9192 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9193 | if (unicode != NULL && PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9194 | len = asciilib_InsertThousandsGrouping( |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9195 | (Py_UCS1 *) data, n_buffer, (Py_UCS1 *) digits, n_digits, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9196 | min_width, grouping, |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9197 | (Py_UCS1 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9198 | else |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9199 | len = ucs1lib_InsertThousandsGrouping( |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9200 | (Py_UCS1*)data, n_buffer, (Py_UCS1*)digits, n_digits, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9201 | min_width, grouping, |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9202 | (Py_UCS1 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9203 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9204 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9205 | len = ucs2lib_InsertThousandsGrouping( |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9206 | (Py_UCS2 *) data, n_buffer, (Py_UCS2 *) digits, n_digits, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9207 | min_width, grouping, |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9208 | (Py_UCS2 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9209 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9210 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9211 | len = ucs4lib_InsertThousandsGrouping( |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9212 | (Py_UCS4 *) data, n_buffer, (Py_UCS4 *) digits, n_digits, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9213 | min_width, grouping, |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9214 | (Py_UCS4 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9215 | break; |
| 9216 | default: |
| 9217 | assert(0); |
| 9218 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9219 | } |
Victor Stinner | 90f50d4 | 2012-02-24 01:44:47 +0100 | [diff] [blame] | 9220 | if (unicode != NULL && thousands_sep_kind != kind) { |
| 9221 | if (thousands_sep_kind < kind) |
| 9222 | PyMem_Free(thousands_sep_data); |
| 9223 | else |
| 9224 | PyMem_Free(data); |
| 9225 | } |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9226 | if (unicode == NULL) { |
| 9227 | *maxchar = 127; |
| 9228 | if (len != n_digits) { |
| 9229 | *maxchar = Py_MAX(*maxchar, |
| 9230 | PyUnicode_MAX_CHAR_VALUE(thousands_sep)); |
| 9231 | } |
| 9232 | } |
| 9233 | return len; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9234 | } |
| 9235 | |
| 9236 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9237 | /* helper macro to fixup start/end slice values */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9238 | #define ADJUST_INDICES(start, end, len) \ |
| 9239 | if (end > len) \ |
| 9240 | end = len; \ |
| 9241 | else if (end < 0) { \ |
| 9242 | end += len; \ |
| 9243 | if (end < 0) \ |
| 9244 | end = 0; \ |
| 9245 | } \ |
| 9246 | if (start < 0) { \ |
| 9247 | start += len; \ |
| 9248 | if (start < 0) \ |
| 9249 | start = 0; \ |
| 9250 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9251 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9252 | Py_ssize_t |
| 9253 | PyUnicode_Count(PyObject *str, |
| 9254 | PyObject *substr, |
| 9255 | Py_ssize_t start, |
| 9256 | Py_ssize_t end) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9257 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9258 | Py_ssize_t result; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9259 | PyObject* str_obj; |
| 9260 | PyObject* sub_obj; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9261 | int kind1, kind2, kind; |
| 9262 | void *buf1 = NULL, *buf2 = NULL; |
| 9263 | Py_ssize_t len1, len2; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9264 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9265 | str_obj = PyUnicode_FromObject(str); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9266 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9267 | return -1; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9268 | sub_obj = PyUnicode_FromObject(substr); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9269 | if (!sub_obj) { |
| 9270 | Py_DECREF(str_obj); |
| 9271 | return -1; |
| 9272 | } |
Benjamin Peterson | 4c13a4a | 2012-01-02 09:07:38 -0600 | [diff] [blame] | 9273 | if (PyUnicode_READY(sub_obj) == -1 || PyUnicode_READY(str_obj) == -1) { |
Benjamin Peterson | 5e458f5 | 2012-01-02 10:12:13 -0600 | [diff] [blame] | 9274 | Py_DECREF(sub_obj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9275 | Py_DECREF(str_obj); |
| 9276 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9277 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9278 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9279 | kind1 = PyUnicode_KIND(str_obj); |
| 9280 | kind2 = PyUnicode_KIND(sub_obj); |
| 9281 | kind = kind1 > kind2 ? kind1 : kind2; |
| 9282 | buf1 = PyUnicode_DATA(str_obj); |
| 9283 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9284 | buf1 = _PyUnicode_AsKind(str_obj, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9285 | if (!buf1) |
| 9286 | goto onError; |
| 9287 | buf2 = PyUnicode_DATA(sub_obj); |
| 9288 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9289 | buf2 = _PyUnicode_AsKind(sub_obj, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9290 | if (!buf2) |
| 9291 | goto onError; |
| 9292 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 9293 | len2 = PyUnicode_GET_LENGTH(sub_obj); |
| 9294 | |
| 9295 | ADJUST_INDICES(start, end, len1); |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9296 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9297 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9298 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sub_obj)) |
| 9299 | result = asciilib_count( |
| 9300 | ((Py_UCS1*)buf1) + start, end - start, |
| 9301 | buf2, len2, PY_SSIZE_T_MAX |
| 9302 | ); |
| 9303 | else |
| 9304 | result = ucs1lib_count( |
| 9305 | ((Py_UCS1*)buf1) + start, end - start, |
| 9306 | buf2, len2, PY_SSIZE_T_MAX |
| 9307 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9308 | break; |
| 9309 | case PyUnicode_2BYTE_KIND: |
| 9310 | result = ucs2lib_count( |
| 9311 | ((Py_UCS2*)buf1) + start, end - start, |
| 9312 | buf2, len2, PY_SSIZE_T_MAX |
| 9313 | ); |
| 9314 | break; |
| 9315 | case PyUnicode_4BYTE_KIND: |
| 9316 | result = ucs4lib_count( |
| 9317 | ((Py_UCS4*)buf1) + start, end - start, |
| 9318 | buf2, len2, PY_SSIZE_T_MAX |
| 9319 | ); |
| 9320 | break; |
| 9321 | default: |
| 9322 | assert(0); result = 0; |
| 9323 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9324 | |
| 9325 | Py_DECREF(sub_obj); |
| 9326 | Py_DECREF(str_obj); |
| 9327 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9328 | if (kind1 != kind) |
| 9329 | PyMem_Free(buf1); |
| 9330 | if (kind2 != kind) |
| 9331 | PyMem_Free(buf2); |
| 9332 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9333 | return result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9334 | onError: |
| 9335 | Py_DECREF(sub_obj); |
| 9336 | Py_DECREF(str_obj); |
| 9337 | if (kind1 != kind && buf1) |
| 9338 | PyMem_Free(buf1); |
| 9339 | if (kind2 != kind && buf2) |
| 9340 | PyMem_Free(buf2); |
| 9341 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9342 | } |
| 9343 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9344 | Py_ssize_t |
| 9345 | PyUnicode_Find(PyObject *str, |
| 9346 | PyObject *sub, |
| 9347 | Py_ssize_t start, |
| 9348 | Py_ssize_t end, |
| 9349 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9350 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9351 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9352 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9353 | str = PyUnicode_FromObject(str); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9354 | if (!str) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9355 | return -2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9356 | sub = PyUnicode_FromObject(sub); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9357 | if (!sub) { |
| 9358 | Py_DECREF(str); |
| 9359 | return -2; |
| 9360 | } |
| 9361 | if (PyUnicode_READY(sub) == -1 || PyUnicode_READY(str) == -1) { |
| 9362 | Py_DECREF(sub); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9363 | Py_DECREF(str); |
| 9364 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9365 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9366 | |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 9367 | result = any_find_slice(direction, |
| 9368 | str, sub, start, end |
| 9369 | ); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9370 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9371 | Py_DECREF(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9372 | Py_DECREF(sub); |
| 9373 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9374 | return result; |
| 9375 | } |
| 9376 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9377 | Py_ssize_t |
| 9378 | PyUnicode_FindChar(PyObject *str, Py_UCS4 ch, |
| 9379 | Py_ssize_t start, Py_ssize_t end, |
| 9380 | int direction) |
| 9381 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9382 | int kind; |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 9383 | Py_ssize_t result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9384 | if (PyUnicode_READY(str) == -1) |
| 9385 | return -2; |
Victor Stinner | 267aa24 | 2011-10-02 01:08:37 +0200 | [diff] [blame] | 9386 | if (start < 0 || end < 0) { |
| 9387 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 9388 | return -2; |
| 9389 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9390 | if (end > PyUnicode_GET_LENGTH(str)) |
| 9391 | end = PyUnicode_GET_LENGTH(str); |
| 9392 | kind = PyUnicode_KIND(str); |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 9393 | result = findchar(PyUnicode_1BYTE_DATA(str) + kind*start, |
| 9394 | kind, end-start, ch, direction); |
| 9395 | if (result == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9396 | return -1; |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 9397 | else |
| 9398 | return start + result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9399 | } |
| 9400 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9401 | static int |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9402 | tailmatch(PyObject *self, |
| 9403 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9404 | Py_ssize_t start, |
| 9405 | Py_ssize_t end, |
| 9406 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9407 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9408 | int kind_self; |
| 9409 | int kind_sub; |
| 9410 | void *data_self; |
| 9411 | void *data_sub; |
| 9412 | Py_ssize_t offset; |
| 9413 | Py_ssize_t i; |
| 9414 | Py_ssize_t end_sub; |
| 9415 | |
| 9416 | if (PyUnicode_READY(self) == -1 || |
| 9417 | PyUnicode_READY(substring) == -1) |
| 9418 | return 0; |
| 9419 | |
| 9420 | if (PyUnicode_GET_LENGTH(substring) == 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9421 | return 1; |
| 9422 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9423 | ADJUST_INDICES(start, end, PyUnicode_GET_LENGTH(self)); |
| 9424 | end -= PyUnicode_GET_LENGTH(substring); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9425 | if (end < start) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9426 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9427 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9428 | kind_self = PyUnicode_KIND(self); |
| 9429 | data_self = PyUnicode_DATA(self); |
| 9430 | kind_sub = PyUnicode_KIND(substring); |
| 9431 | data_sub = PyUnicode_DATA(substring); |
| 9432 | end_sub = PyUnicode_GET_LENGTH(substring) - 1; |
| 9433 | |
| 9434 | if (direction > 0) |
| 9435 | offset = end; |
| 9436 | else |
| 9437 | offset = start; |
| 9438 | |
| 9439 | if (PyUnicode_READ(kind_self, data_self, offset) == |
| 9440 | PyUnicode_READ(kind_sub, data_sub, 0) && |
| 9441 | PyUnicode_READ(kind_self, data_self, offset + end_sub) == |
| 9442 | PyUnicode_READ(kind_sub, data_sub, end_sub)) { |
| 9443 | /* If both are of the same kind, memcmp is sufficient */ |
| 9444 | if (kind_self == kind_sub) { |
| 9445 | return ! memcmp((char *)data_self + |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9446 | (offset * PyUnicode_KIND(substring)), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9447 | data_sub, |
| 9448 | PyUnicode_GET_LENGTH(substring) * |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9449 | PyUnicode_KIND(substring)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9450 | } |
| 9451 | /* otherwise we have to compare each character by first accesing it */ |
| 9452 | else { |
| 9453 | /* We do not need to compare 0 and len(substring)-1 because |
| 9454 | the if statement above ensured already that they are equal |
| 9455 | when we end up here. */ |
| 9456 | // TODO: honor direction and do a forward or backwards search |
| 9457 | for (i = 1; i < end_sub; ++i) { |
| 9458 | if (PyUnicode_READ(kind_self, data_self, offset + i) != |
| 9459 | PyUnicode_READ(kind_sub, data_sub, i)) |
| 9460 | return 0; |
| 9461 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9462 | return 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9463 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9464 | } |
| 9465 | |
| 9466 | return 0; |
| 9467 | } |
| 9468 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9469 | Py_ssize_t |
| 9470 | PyUnicode_Tailmatch(PyObject *str, |
| 9471 | PyObject *substr, |
| 9472 | Py_ssize_t start, |
| 9473 | Py_ssize_t end, |
| 9474 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9475 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9476 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9477 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9478 | str = PyUnicode_FromObject(str); |
| 9479 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9480 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9481 | substr = PyUnicode_FromObject(substr); |
| 9482 | if (substr == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9483 | Py_DECREF(str); |
| 9484 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9485 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9486 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9487 | result = tailmatch(str, substr, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9488 | start, end, direction); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9489 | Py_DECREF(str); |
| 9490 | Py_DECREF(substr); |
| 9491 | return result; |
| 9492 | } |
| 9493 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9494 | /* Apply fixfct filter to the Unicode object self and return a |
| 9495 | reference to the modified object */ |
| 9496 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9497 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9498 | fixup(PyObject *self, |
| 9499 | Py_UCS4 (*fixfct)(PyObject *s)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9500 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9501 | PyObject *u; |
| 9502 | Py_UCS4 maxchar_old, maxchar_new = 0; |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9503 | PyObject *v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9504 | |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 9505 | u = _PyUnicode_Copy(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9506 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9507 | return NULL; |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 9508 | maxchar_old = PyUnicode_MAX_CHAR_VALUE(u); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9509 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9510 | /* fix functions return the new maximum character in a string, |
| 9511 | if the kind of the resulting unicode object does not change, |
| 9512 | everything is fine. Otherwise we need to change the string kind |
| 9513 | and re-run the fix function. */ |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9514 | maxchar_new = fixfct(u); |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9515 | |
| 9516 | if (maxchar_new == 0) { |
| 9517 | /* no changes */; |
| 9518 | if (PyUnicode_CheckExact(self)) { |
| 9519 | Py_DECREF(u); |
| 9520 | Py_INCREF(self); |
| 9521 | return self; |
| 9522 | } |
| 9523 | else |
| 9524 | return u; |
| 9525 | } |
| 9526 | |
| 9527 | if (maxchar_new <= 127) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9528 | maxchar_new = 127; |
| 9529 | else if (maxchar_new <= 255) |
| 9530 | maxchar_new = 255; |
| 9531 | else if (maxchar_new <= 65535) |
| 9532 | maxchar_new = 65535; |
| 9533 | else |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 9534 | maxchar_new = MAX_UNICODE; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9535 | |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9536 | if (maxchar_new == maxchar_old) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9537 | return u; |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9538 | |
| 9539 | /* In case the maximum character changed, we need to |
| 9540 | convert the string to the new category. */ |
| 9541 | v = PyUnicode_New(PyUnicode_GET_LENGTH(self), maxchar_new); |
| 9542 | if (v == NULL) { |
| 9543 | Py_DECREF(u); |
| 9544 | return NULL; |
| 9545 | } |
| 9546 | if (maxchar_new > maxchar_old) { |
| 9547 | /* If the maxchar increased so that the kind changed, not all |
| 9548 | characters are representable anymore and we need to fix the |
| 9549 | string again. This only happens in very few cases. */ |
| 9550 | copy_characters(v, 0, self, 0, PyUnicode_GET_LENGTH(self)); |
| 9551 | maxchar_old = fixfct(v); |
| 9552 | assert(maxchar_old > 0 && maxchar_old <= maxchar_new); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9553 | } |
| 9554 | else { |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9555 | copy_characters(v, 0, u, 0, PyUnicode_GET_LENGTH(self)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9556 | } |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9557 | Py_DECREF(u); |
| 9558 | assert(_PyUnicode_CheckConsistency(v, 1)); |
| 9559 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9560 | } |
| 9561 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9562 | static PyObject * |
| 9563 | ascii_upper_or_lower(PyObject *self, int lower) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9564 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9565 | Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 9566 | char *resdata, *data = PyUnicode_DATA(self); |
| 9567 | PyObject *res; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9568 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9569 | res = PyUnicode_New(len, 127); |
| 9570 | if (res == NULL) |
| 9571 | return NULL; |
| 9572 | resdata = PyUnicode_DATA(res); |
| 9573 | if (lower) |
| 9574 | _Py_bytes_lower(resdata, data, len); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9575 | else |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9576 | _Py_bytes_upper(resdata, data, len); |
| 9577 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9578 | } |
| 9579 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9580 | static Py_UCS4 |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9581 | 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] | 9582 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9583 | Py_ssize_t j; |
| 9584 | int final_sigma; |
| 9585 | Py_UCS4 c; |
| 9586 | /* 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] | 9587 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9588 | \p{cased}\p{case-ignorable}*U+03A3!(\p{case-ignorable}*\p{cased}) |
| 9589 | |
| 9590 | where ! is a negation and \p{xxx} is a character with property xxx. |
| 9591 | */ |
| 9592 | for (j = i - 1; j >= 0; j--) { |
| 9593 | c = PyUnicode_READ(kind, data, j); |
| 9594 | if (!_PyUnicode_IsCaseIgnorable(c)) |
| 9595 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9596 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9597 | final_sigma = j >= 0 && _PyUnicode_IsCased(c); |
| 9598 | if (final_sigma) { |
| 9599 | for (j = i + 1; j < length; j++) { |
| 9600 | c = PyUnicode_READ(kind, data, j); |
| 9601 | if (!_PyUnicode_IsCaseIgnorable(c)) |
| 9602 | break; |
| 9603 | } |
| 9604 | final_sigma = j == length || !_PyUnicode_IsCased(c); |
| 9605 | } |
| 9606 | return (final_sigma) ? 0x3C2 : 0x3C3; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9607 | } |
| 9608 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9609 | static int |
| 9610 | lower_ucs4(int kind, void *data, Py_ssize_t length, Py_ssize_t i, |
| 9611 | Py_UCS4 c, Py_UCS4 *mapped) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9612 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9613 | /* Obscure special case. */ |
| 9614 | if (c == 0x3A3) { |
| 9615 | mapped[0] = handle_capital_sigma(kind, data, length, i); |
| 9616 | return 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9617 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9618 | return _PyUnicode_ToLowerFull(c, mapped); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9619 | } |
| 9620 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9621 | static Py_ssize_t |
| 9622 | 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] | 9623 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9624 | Py_ssize_t i, k = 0; |
| 9625 | int n_res, j; |
| 9626 | Py_UCS4 c, mapped[3]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9627 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9628 | c = PyUnicode_READ(kind, data, 0); |
| 9629 | n_res = _PyUnicode_ToUpperFull(c, mapped); |
| 9630 | for (j = 0; j < n_res; j++) { |
| 9631 | if (mapped[j] > *maxchar) |
| 9632 | *maxchar = mapped[j]; |
| 9633 | res[k++] = mapped[j]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9634 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9635 | for (i = 1; i < length; i++) { |
| 9636 | c = PyUnicode_READ(kind, data, i); |
| 9637 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9638 | for (j = 0; j < n_res; j++) { |
| 9639 | if (mapped[j] > *maxchar) |
| 9640 | *maxchar = mapped[j]; |
| 9641 | res[k++] = mapped[j]; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9642 | } |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9643 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9644 | return k; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9645 | } |
| 9646 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9647 | static Py_ssize_t |
| 9648 | do_swapcase(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) { |
| 9649 | Py_ssize_t i, k = 0; |
| 9650 | |
| 9651 | for (i = 0; i < length; i++) { |
| 9652 | Py_UCS4 c = PyUnicode_READ(kind, data, i), mapped[3]; |
| 9653 | int n_res, j; |
| 9654 | if (Py_UNICODE_ISUPPER(c)) { |
| 9655 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9656 | } |
| 9657 | else if (Py_UNICODE_ISLOWER(c)) { |
| 9658 | n_res = _PyUnicode_ToUpperFull(c, mapped); |
| 9659 | } |
| 9660 | else { |
| 9661 | n_res = 1; |
| 9662 | mapped[0] = c; |
| 9663 | } |
| 9664 | for (j = 0; j < n_res; j++) { |
| 9665 | if (mapped[j] > *maxchar) |
| 9666 | *maxchar = mapped[j]; |
| 9667 | res[k++] = mapped[j]; |
| 9668 | } |
| 9669 | } |
| 9670 | return k; |
| 9671 | } |
| 9672 | |
| 9673 | static Py_ssize_t |
| 9674 | do_upper_or_lower(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, |
| 9675 | Py_UCS4 *maxchar, int lower) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9676 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9677 | Py_ssize_t i, k = 0; |
| 9678 | |
| 9679 | for (i = 0; i < length; i++) { |
| 9680 | Py_UCS4 c = PyUnicode_READ(kind, data, i), mapped[3]; |
| 9681 | int n_res, j; |
| 9682 | if (lower) |
| 9683 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9684 | else |
| 9685 | n_res = _PyUnicode_ToUpperFull(c, mapped); |
| 9686 | for (j = 0; j < n_res; j++) { |
| 9687 | if (mapped[j] > *maxchar) |
| 9688 | *maxchar = mapped[j]; |
| 9689 | res[k++] = mapped[j]; |
| 9690 | } |
| 9691 | } |
| 9692 | return k; |
| 9693 | } |
| 9694 | |
| 9695 | static Py_ssize_t |
| 9696 | do_upper(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9697 | { |
| 9698 | return do_upper_or_lower(kind, data, length, res, maxchar, 0); |
| 9699 | } |
| 9700 | |
| 9701 | static Py_ssize_t |
| 9702 | do_lower(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9703 | { |
| 9704 | return do_upper_or_lower(kind, data, length, res, maxchar, 1); |
| 9705 | } |
| 9706 | |
Benjamin Peterson | e51757f | 2012-01-12 21:10:29 -0500 | [diff] [blame] | 9707 | static Py_ssize_t |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 9708 | do_casefold(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9709 | { |
| 9710 | Py_ssize_t i, k = 0; |
| 9711 | |
| 9712 | for (i = 0; i < length; i++) { |
| 9713 | Py_UCS4 c = PyUnicode_READ(kind, data, i); |
| 9714 | Py_UCS4 mapped[3]; |
| 9715 | int j, n_res = _PyUnicode_ToFoldedFull(c, mapped); |
| 9716 | for (j = 0; j < n_res; j++) { |
| 9717 | if (mapped[j] > *maxchar) |
| 9718 | *maxchar = mapped[j]; |
| 9719 | res[k++] = mapped[j]; |
| 9720 | } |
| 9721 | } |
| 9722 | return k; |
| 9723 | } |
| 9724 | |
| 9725 | static Py_ssize_t |
Benjamin Peterson | e51757f | 2012-01-12 21:10:29 -0500 | [diff] [blame] | 9726 | do_title(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9727 | { |
| 9728 | Py_ssize_t i, k = 0; |
| 9729 | int previous_is_cased; |
| 9730 | |
| 9731 | previous_is_cased = 0; |
| 9732 | for (i = 0; i < length; i++) { |
| 9733 | const Py_UCS4 c = PyUnicode_READ(kind, data, i); |
| 9734 | Py_UCS4 mapped[3]; |
| 9735 | int n_res, j; |
| 9736 | |
| 9737 | if (previous_is_cased) |
| 9738 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9739 | else |
| 9740 | n_res = _PyUnicode_ToTitleFull(c, mapped); |
| 9741 | |
| 9742 | for (j = 0; j < n_res; j++) { |
| 9743 | if (mapped[j] > *maxchar) |
| 9744 | *maxchar = mapped[j]; |
| 9745 | res[k++] = mapped[j]; |
| 9746 | } |
| 9747 | |
| 9748 | previous_is_cased = _PyUnicode_IsCased(c); |
| 9749 | } |
| 9750 | return k; |
| 9751 | } |
| 9752 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9753 | static PyObject * |
| 9754 | case_operation(PyObject *self, |
| 9755 | Py_ssize_t (*perform)(int, void *, Py_ssize_t, Py_UCS4 *, Py_UCS4 *)) |
| 9756 | { |
| 9757 | PyObject *res = NULL; |
| 9758 | Py_ssize_t length, newlength = 0; |
| 9759 | int kind, outkind; |
| 9760 | void *data, *outdata; |
| 9761 | Py_UCS4 maxchar = 0, *tmp, *tmpend; |
| 9762 | |
Benjamin Peterson | eea4846 | 2012-01-16 14:28:50 -0500 | [diff] [blame] | 9763 | assert(PyUnicode_IS_READY(self)); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9764 | |
| 9765 | kind = PyUnicode_KIND(self); |
| 9766 | data = PyUnicode_DATA(self); |
| 9767 | length = PyUnicode_GET_LENGTH(self); |
| 9768 | tmp = PyMem_MALLOC(sizeof(Py_UCS4) * 3 * length); |
| 9769 | if (tmp == NULL) |
| 9770 | return PyErr_NoMemory(); |
| 9771 | newlength = perform(kind, data, length, tmp, &maxchar); |
| 9772 | res = PyUnicode_New(newlength, maxchar); |
| 9773 | if (res == NULL) |
| 9774 | goto leave; |
| 9775 | tmpend = tmp + newlength; |
| 9776 | outdata = PyUnicode_DATA(res); |
| 9777 | outkind = PyUnicode_KIND(res); |
| 9778 | switch (outkind) { |
| 9779 | case PyUnicode_1BYTE_KIND: |
| 9780 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS1, tmp, tmpend, outdata); |
| 9781 | break; |
| 9782 | case PyUnicode_2BYTE_KIND: |
| 9783 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS2, tmp, tmpend, outdata); |
| 9784 | break; |
| 9785 | case PyUnicode_4BYTE_KIND: |
| 9786 | memcpy(outdata, tmp, sizeof(Py_UCS4) * newlength); |
| 9787 | break; |
| 9788 | default: |
| 9789 | assert(0); |
| 9790 | break; |
| 9791 | } |
| 9792 | leave: |
| 9793 | PyMem_FREE(tmp); |
| 9794 | return res; |
| 9795 | } |
| 9796 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9797 | PyObject * |
| 9798 | PyUnicode_Join(PyObject *separator, PyObject *seq) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9799 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9800 | PyObject *sep = NULL; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9801 | Py_ssize_t seplen; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9802 | PyObject *res = NULL; /* the result */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9803 | PyObject *fseq; /* PySequence_Fast(seq) */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9804 | Py_ssize_t seqlen; /* len(fseq) -- number of items in sequence */ |
| 9805 | PyObject **items; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9806 | PyObject *item; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9807 | Py_ssize_t sz, i, res_offset; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9808 | Py_UCS4 maxchar; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9809 | Py_UCS4 item_maxchar; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9810 | int use_memcpy; |
| 9811 | unsigned char *res_data = NULL, *sep_data = NULL; |
| 9812 | PyObject *last_obj; |
| 9813 | unsigned int kind = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9814 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9815 | fseq = PySequence_Fast(seq, ""); |
| 9816 | if (fseq == NULL) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9817 | return NULL; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9818 | } |
| 9819 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9820 | /* NOTE: the following code can't call back into Python code, |
| 9821 | * so we are sure that fseq won't be mutated. |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 9822 | */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9823 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9824 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 9825 | /* If empty sequence, return u"". */ |
| 9826 | if (seqlen == 0) { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9827 | Py_DECREF(fseq); |
| 9828 | Py_INCREF(unicode_empty); |
| 9829 | res = unicode_empty; |
| 9830 | return res; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9831 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9832 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9833 | /* If singleton sequence with an exact Unicode, return that. */ |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9834 | last_obj = NULL; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9835 | items = PySequence_Fast_ITEMS(fseq); |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9836 | if (seqlen == 1) { |
| 9837 | if (PyUnicode_CheckExact(items[0])) { |
| 9838 | res = items[0]; |
| 9839 | Py_INCREF(res); |
| 9840 | Py_DECREF(fseq); |
| 9841 | return res; |
| 9842 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9843 | seplen = 0; |
Victor Stinner | c6f0df7 | 2011-10-06 15:58:54 +0200 | [diff] [blame] | 9844 | maxchar = 0; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9845 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9846 | else { |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9847 | /* Set up sep and seplen */ |
| 9848 | if (separator == NULL) { |
| 9849 | /* fall back to a blank space separator */ |
| 9850 | sep = PyUnicode_FromOrdinal(' '); |
| 9851 | if (!sep) |
| 9852 | goto onError; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9853 | seplen = 1; |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9854 | maxchar = 32; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9855 | } |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9856 | else { |
| 9857 | if (!PyUnicode_Check(separator)) { |
| 9858 | PyErr_Format(PyExc_TypeError, |
| 9859 | "separator: expected str instance," |
| 9860 | " %.80s found", |
| 9861 | Py_TYPE(separator)->tp_name); |
| 9862 | goto onError; |
| 9863 | } |
| 9864 | if (PyUnicode_READY(separator)) |
| 9865 | goto onError; |
| 9866 | sep = separator; |
| 9867 | seplen = PyUnicode_GET_LENGTH(separator); |
| 9868 | maxchar = PyUnicode_MAX_CHAR_VALUE(separator); |
| 9869 | /* inc refcount to keep this code path symmetric with the |
| 9870 | above case of a blank separator */ |
| 9871 | Py_INCREF(sep); |
| 9872 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9873 | last_obj = sep; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9874 | } |
| 9875 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9876 | /* There are at least two things to join, or else we have a subclass |
| 9877 | * of str in the sequence. |
| 9878 | * Do a pre-pass to figure out the total amount of space we'll |
| 9879 | * need (sz), and see whether all argument are strings. |
| 9880 | */ |
| 9881 | sz = 0; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9882 | #ifdef Py_DEBUG |
| 9883 | use_memcpy = 0; |
| 9884 | #else |
| 9885 | use_memcpy = 1; |
| 9886 | #endif |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9887 | for (i = 0; i < seqlen; i++) { |
| 9888 | const Py_ssize_t old_sz = sz; |
| 9889 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9890 | if (!PyUnicode_Check(item)) { |
| 9891 | PyErr_Format(PyExc_TypeError, |
| 9892 | "sequence item %zd: expected str instance," |
| 9893 | " %.80s found", |
| 9894 | i, Py_TYPE(item)->tp_name); |
| 9895 | goto onError; |
| 9896 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9897 | if (PyUnicode_READY(item) == -1) |
| 9898 | goto onError; |
| 9899 | sz += PyUnicode_GET_LENGTH(item); |
| 9900 | item_maxchar = PyUnicode_MAX_CHAR_VALUE(item); |
Victor Stinner | c6f0df7 | 2011-10-06 15:58:54 +0200 | [diff] [blame] | 9901 | maxchar = Py_MAX(maxchar, item_maxchar); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9902 | if (i != 0) |
| 9903 | sz += seplen; |
| 9904 | if (sz < old_sz || sz > PY_SSIZE_T_MAX) { |
| 9905 | PyErr_SetString(PyExc_OverflowError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9906 | "join() result is too long for a Python string"); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9907 | goto onError; |
| 9908 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9909 | if (use_memcpy && last_obj != NULL) { |
| 9910 | if (PyUnicode_KIND(last_obj) != PyUnicode_KIND(item)) |
| 9911 | use_memcpy = 0; |
| 9912 | } |
| 9913 | last_obj = item; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9914 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9915 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9916 | res = PyUnicode_New(sz, maxchar); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9917 | if (res == NULL) |
| 9918 | goto onError; |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 9919 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9920 | /* Catenate everything. */ |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9921 | #ifdef Py_DEBUG |
| 9922 | use_memcpy = 0; |
| 9923 | #else |
| 9924 | if (use_memcpy) { |
| 9925 | res_data = PyUnicode_1BYTE_DATA(res); |
| 9926 | kind = PyUnicode_KIND(res); |
| 9927 | if (seplen != 0) |
| 9928 | sep_data = PyUnicode_1BYTE_DATA(sep); |
| 9929 | } |
| 9930 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9931 | for (i = 0, res_offset = 0; i < seqlen; ++i) { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9932 | Py_ssize_t itemlen; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9933 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9934 | /* Copy item, and maybe the separator. */ |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 9935 | if (i && seplen != 0) { |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9936 | if (use_memcpy) { |
| 9937 | Py_MEMCPY(res_data, |
| 9938 | sep_data, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9939 | kind * seplen); |
| 9940 | res_data += kind * seplen; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9941 | } |
| 9942 | else { |
| 9943 | copy_characters(res, res_offset, sep, 0, seplen); |
| 9944 | res_offset += seplen; |
| 9945 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9946 | } |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 9947 | itemlen = PyUnicode_GET_LENGTH(item); |
| 9948 | if (itemlen != 0) { |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9949 | if (use_memcpy) { |
| 9950 | Py_MEMCPY(res_data, |
| 9951 | PyUnicode_DATA(item), |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9952 | kind * itemlen); |
| 9953 | res_data += kind * itemlen; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9954 | } |
| 9955 | else { |
| 9956 | copy_characters(res, res_offset, item, 0, itemlen); |
| 9957 | res_offset += itemlen; |
| 9958 | } |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 9959 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9960 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9961 | if (use_memcpy) |
| 9962 | assert(res_data == PyUnicode_1BYTE_DATA(res) |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9963 | + kind * PyUnicode_GET_LENGTH(res)); |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9964 | else |
| 9965 | assert(res_offset == PyUnicode_GET_LENGTH(res)); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9966 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9967 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9968 | Py_XDECREF(sep); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 9969 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9970 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9971 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9972 | onError: |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9973 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9974 | Py_XDECREF(sep); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9975 | Py_XDECREF(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9976 | return NULL; |
| 9977 | } |
| 9978 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9979 | #define FILL(kind, data, value, start, length) \ |
| 9980 | do { \ |
| 9981 | Py_ssize_t i_ = 0; \ |
| 9982 | assert(kind != PyUnicode_WCHAR_KIND); \ |
| 9983 | switch ((kind)) { \ |
| 9984 | case PyUnicode_1BYTE_KIND: { \ |
| 9985 | unsigned char * to_ = (unsigned char *)((data)) + (start); \ |
| 9986 | memset(to_, (unsigned char)value, length); \ |
| 9987 | break; \ |
| 9988 | } \ |
| 9989 | case PyUnicode_2BYTE_KIND: { \ |
| 9990 | Py_UCS2 * to_ = (Py_UCS2 *)((data)) + (start); \ |
| 9991 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 9992 | break; \ |
| 9993 | } \ |
Benjamin Peterson | e157cf1 | 2012-01-01 15:56:20 -0600 | [diff] [blame] | 9994 | case PyUnicode_4BYTE_KIND: { \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9995 | Py_UCS4 * to_ = (Py_UCS4 *)((data)) + (start); \ |
| 9996 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 9997 | break; \ |
Benjamin Peterson | e157cf1 | 2012-01-01 15:56:20 -0600 | [diff] [blame] | 9998 | default: assert(0); \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9999 | } \ |
| 10000 | } \ |
| 10001 | } while (0) |
| 10002 | |
Victor Stinner | 3fe5531 | 2012-01-04 00:33:50 +0100 | [diff] [blame] | 10003 | Py_ssize_t |
| 10004 | PyUnicode_Fill(PyObject *unicode, Py_ssize_t start, Py_ssize_t length, |
| 10005 | Py_UCS4 fill_char) |
| 10006 | { |
| 10007 | Py_ssize_t maxlen; |
| 10008 | enum PyUnicode_Kind kind; |
| 10009 | void *data; |
| 10010 | |
| 10011 | if (!PyUnicode_Check(unicode)) { |
| 10012 | PyErr_BadInternalCall(); |
| 10013 | return -1; |
| 10014 | } |
| 10015 | if (PyUnicode_READY(unicode) == -1) |
| 10016 | return -1; |
| 10017 | if (unicode_check_modifiable(unicode)) |
| 10018 | return -1; |
| 10019 | |
| 10020 | if (fill_char > PyUnicode_MAX_CHAR_VALUE(unicode)) { |
| 10021 | PyErr_SetString(PyExc_ValueError, |
| 10022 | "fill character is bigger than " |
| 10023 | "the string maximum character"); |
| 10024 | return -1; |
| 10025 | } |
| 10026 | |
| 10027 | maxlen = PyUnicode_GET_LENGTH(unicode) - start; |
| 10028 | length = Py_MIN(maxlen, length); |
| 10029 | if (length <= 0) |
| 10030 | return 0; |
| 10031 | |
| 10032 | kind = PyUnicode_KIND(unicode); |
| 10033 | data = PyUnicode_DATA(unicode); |
| 10034 | FILL(kind, data, fill_char, start, length); |
| 10035 | return length; |
| 10036 | } |
| 10037 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10038 | static PyObject * |
| 10039 | pad(PyObject *self, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10040 | Py_ssize_t left, |
| 10041 | Py_ssize_t right, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10042 | Py_UCS4 fill) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10043 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10044 | PyObject *u; |
| 10045 | Py_UCS4 maxchar; |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 10046 | int kind; |
| 10047 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10048 | |
| 10049 | if (left < 0) |
| 10050 | left = 0; |
| 10051 | if (right < 0) |
| 10052 | right = 0; |
| 10053 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10054 | if (left == 0 && right == 0) |
| 10055 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10056 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10057 | if (left > PY_SSIZE_T_MAX - _PyUnicode_LENGTH(self) || |
| 10058 | right > PY_SSIZE_T_MAX - (left + _PyUnicode_LENGTH(self))) { |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 10059 | PyErr_SetString(PyExc_OverflowError, "padded string is too long"); |
| 10060 | return NULL; |
| 10061 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10062 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
| 10063 | if (fill > maxchar) |
| 10064 | maxchar = fill; |
| 10065 | u = PyUnicode_New(left + _PyUnicode_LENGTH(self) + right, maxchar); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 10066 | if (!u) |
| 10067 | return NULL; |
| 10068 | |
| 10069 | kind = PyUnicode_KIND(u); |
| 10070 | data = PyUnicode_DATA(u); |
| 10071 | if (left) |
| 10072 | FILL(kind, data, fill, 0, left); |
| 10073 | if (right) |
| 10074 | FILL(kind, data, fill, left + _PyUnicode_LENGTH(self), right); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 10075 | copy_characters(u, left, self, 0, _PyUnicode_LENGTH(self)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10076 | assert(_PyUnicode_CheckConsistency(u, 1)); |
| 10077 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10078 | } |
| 10079 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10080 | PyObject * |
| 10081 | PyUnicode_Splitlines(PyObject *string, int keepends) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10082 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10083 | PyObject *list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10084 | |
| 10085 | string = PyUnicode_FromObject(string); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 10086 | if (string == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10087 | return NULL; |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 10088 | if (PyUnicode_READY(string) == -1) { |
| 10089 | Py_DECREF(string); |
| 10090 | return NULL; |
| 10091 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10092 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10093 | switch (PyUnicode_KIND(string)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10094 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10095 | if (PyUnicode_IS_ASCII(string)) |
| 10096 | list = asciilib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10097 | string, PyUnicode_1BYTE_DATA(string), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10098 | PyUnicode_GET_LENGTH(string), keepends); |
| 10099 | else |
| 10100 | list = ucs1lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10101 | string, PyUnicode_1BYTE_DATA(string), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10102 | PyUnicode_GET_LENGTH(string), keepends); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10103 | break; |
| 10104 | case PyUnicode_2BYTE_KIND: |
| 10105 | list = ucs2lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10106 | string, PyUnicode_2BYTE_DATA(string), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10107 | PyUnicode_GET_LENGTH(string), keepends); |
| 10108 | break; |
| 10109 | case PyUnicode_4BYTE_KIND: |
| 10110 | list = ucs4lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10111 | string, PyUnicode_4BYTE_DATA(string), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10112 | PyUnicode_GET_LENGTH(string), keepends); |
| 10113 | break; |
| 10114 | default: |
| 10115 | assert(0); |
| 10116 | list = 0; |
| 10117 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10118 | Py_DECREF(string); |
| 10119 | return list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10120 | } |
| 10121 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10122 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10123 | split(PyObject *self, |
| 10124 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10125 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10126 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10127 | int kind1, kind2, kind; |
| 10128 | void *buf1, *buf2; |
| 10129 | Py_ssize_t len1, len2; |
| 10130 | PyObject* out; |
| 10131 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10132 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10133 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10134 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10135 | if (PyUnicode_READY(self) == -1) |
| 10136 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10137 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10138 | if (substring == NULL) |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10139 | switch (PyUnicode_KIND(self)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10140 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10141 | if (PyUnicode_IS_ASCII(self)) |
| 10142 | return asciilib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10143 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10144 | PyUnicode_GET_LENGTH(self), maxcount |
| 10145 | ); |
| 10146 | else |
| 10147 | return ucs1lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10148 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10149 | PyUnicode_GET_LENGTH(self), maxcount |
| 10150 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10151 | case PyUnicode_2BYTE_KIND: |
| 10152 | return ucs2lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10153 | self, PyUnicode_2BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10154 | PyUnicode_GET_LENGTH(self), maxcount |
| 10155 | ); |
| 10156 | case PyUnicode_4BYTE_KIND: |
| 10157 | return ucs4lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10158 | self, PyUnicode_4BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10159 | PyUnicode_GET_LENGTH(self), maxcount |
| 10160 | ); |
| 10161 | default: |
| 10162 | assert(0); |
| 10163 | return NULL; |
| 10164 | } |
| 10165 | |
| 10166 | if (PyUnicode_READY(substring) == -1) |
| 10167 | return NULL; |
| 10168 | |
| 10169 | kind1 = PyUnicode_KIND(self); |
| 10170 | kind2 = PyUnicode_KIND(substring); |
| 10171 | kind = kind1 > kind2 ? kind1 : kind2; |
| 10172 | buf1 = PyUnicode_DATA(self); |
| 10173 | buf2 = PyUnicode_DATA(substring); |
| 10174 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10175 | buf1 = _PyUnicode_AsKind(self, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10176 | if (!buf1) |
| 10177 | return NULL; |
| 10178 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10179 | buf2 = _PyUnicode_AsKind(substring, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10180 | if (!buf2) { |
| 10181 | if (kind1 != kind) PyMem_Free(buf1); |
| 10182 | return NULL; |
| 10183 | } |
| 10184 | len1 = PyUnicode_GET_LENGTH(self); |
| 10185 | len2 = PyUnicode_GET_LENGTH(substring); |
| 10186 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10187 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10188 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10189 | if (PyUnicode_IS_ASCII(self) && PyUnicode_IS_ASCII(substring)) |
| 10190 | out = asciilib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10191 | self, buf1, len1, buf2, len2, maxcount); |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10192 | else |
| 10193 | out = ucs1lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10194 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10195 | break; |
| 10196 | case PyUnicode_2BYTE_KIND: |
| 10197 | out = ucs2lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10198 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10199 | break; |
| 10200 | case PyUnicode_4BYTE_KIND: |
| 10201 | out = ucs4lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10202 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10203 | break; |
| 10204 | default: |
| 10205 | out = NULL; |
| 10206 | } |
| 10207 | if (kind1 != kind) |
| 10208 | PyMem_Free(buf1); |
| 10209 | if (kind2 != kind) |
| 10210 | PyMem_Free(buf2); |
| 10211 | return out; |
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 | rsplit(PyObject *self, |
| 10216 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10217 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +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 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +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; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +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; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +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_rsplit_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_rsplit_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_rsplit_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_rsplit_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_rsplit( |
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_rsplit( |
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_rsplit( |
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_rsplit( |
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; |
| 10304 | } |
| 10305 | |
| 10306 | static Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10307 | anylib_find(int kind, PyObject *str1, void *buf1, Py_ssize_t len1, |
| 10308 | 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] | 10309 | { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10310 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10311 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10312 | if (PyUnicode_IS_ASCII(str1) && PyUnicode_IS_ASCII(str2)) |
| 10313 | return asciilib_find(buf1, len1, buf2, len2, offset); |
| 10314 | else |
| 10315 | return ucs1lib_find(buf1, len1, buf2, len2, offset); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10316 | case PyUnicode_2BYTE_KIND: |
| 10317 | return ucs2lib_find(buf1, len1, buf2, len2, offset); |
| 10318 | case PyUnicode_4BYTE_KIND: |
| 10319 | return ucs4lib_find(buf1, len1, buf2, len2, offset); |
| 10320 | } |
| 10321 | assert(0); |
| 10322 | return -1; |
| 10323 | } |
| 10324 | |
| 10325 | static Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10326 | anylib_count(int kind, PyObject *sstr, void* sbuf, Py_ssize_t slen, |
| 10327 | 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] | 10328 | { |
Benjamin Peterson | c0b95d1 | 2011-12-20 17:24:05 -0600 | [diff] [blame] | 10329 | switch (kind) { |
| 10330 | case PyUnicode_1BYTE_KIND: |
| 10331 | if (PyUnicode_IS_ASCII(sstr) && PyUnicode_IS_ASCII(str1)) |
| 10332 | return asciilib_count(sbuf, slen, buf1, len1, maxcount); |
| 10333 | else |
| 10334 | return ucs1lib_count(sbuf, slen, buf1, len1, maxcount); |
| 10335 | case PyUnicode_2BYTE_KIND: |
| 10336 | return ucs2lib_count(sbuf, slen, buf1, len1, maxcount); |
| 10337 | case PyUnicode_4BYTE_KIND: |
| 10338 | return ucs4lib_count(sbuf, slen, buf1, len1, maxcount); |
| 10339 | } |
| 10340 | assert(0); |
| 10341 | return 0; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10342 | } |
| 10343 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10344 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10345 | replace(PyObject *self, PyObject *str1, |
| 10346 | PyObject *str2, Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10347 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10348 | PyObject *u; |
| 10349 | char *sbuf = PyUnicode_DATA(self); |
| 10350 | char *buf1 = PyUnicode_DATA(str1); |
| 10351 | char *buf2 = PyUnicode_DATA(str2); |
| 10352 | int srelease = 0, release1 = 0, release2 = 0; |
| 10353 | int skind = PyUnicode_KIND(self); |
| 10354 | int kind1 = PyUnicode_KIND(str1); |
| 10355 | int kind2 = PyUnicode_KIND(str2); |
| 10356 | Py_ssize_t slen = PyUnicode_GET_LENGTH(self); |
| 10357 | Py_ssize_t len1 = PyUnicode_GET_LENGTH(str1); |
| 10358 | Py_ssize_t len2 = PyUnicode_GET_LENGTH(str2); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10359 | int mayshrink; |
| 10360 | Py_UCS4 maxchar, maxchar_str2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10361 | |
| 10362 | if (maxcount < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10363 | maxcount = PY_SSIZE_T_MAX; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10364 | else if (maxcount == 0 || slen == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10365 | goto nothing; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10366 | |
Victor Stinner | 59de0ee | 2011-10-07 10:01:28 +0200 | [diff] [blame] | 10367 | if (str1 == str2) |
| 10368 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10369 | if (skind < kind1) |
| 10370 | /* substring too wide to be present */ |
| 10371 | goto nothing; |
| 10372 | |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10373 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
| 10374 | maxchar_str2 = PyUnicode_MAX_CHAR_VALUE(str2); |
| 10375 | /* Replacing str1 with str2 may cause a maxchar reduction in the |
| 10376 | result string. */ |
| 10377 | mayshrink = (maxchar_str2 < maxchar); |
| 10378 | maxchar = Py_MAX(maxchar, maxchar_str2); |
| 10379 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10380 | if (len1 == len2) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10381 | /* same length */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10382 | if (len1 == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10383 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10384 | if (len1 == 1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10385 | /* replace characters */ |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10386 | Py_UCS4 u1, u2; |
| 10387 | int rkind; |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10388 | Py_ssize_t index, pos; |
| 10389 | char *src; |
| 10390 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10391 | u1 = PyUnicode_READ_CHAR(str1, 0); |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10392 | pos = findchar(sbuf, PyUnicode_KIND(self), slen, u1, 1); |
| 10393 | if (pos < 0) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10394 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10395 | u2 = PyUnicode_READ_CHAR(str2, 0); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10396 | u = PyUnicode_New(slen, maxchar); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10397 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10398 | goto error; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 10399 | copy_characters(u, 0, self, 0, slen); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10400 | rkind = PyUnicode_KIND(u); |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10401 | |
| 10402 | PyUnicode_WRITE(rkind, PyUnicode_DATA(u), pos, u2); |
| 10403 | index = 0; |
| 10404 | src = sbuf; |
| 10405 | while (--maxcount) |
| 10406 | { |
| 10407 | pos++; |
| 10408 | src += pos * PyUnicode_KIND(self); |
| 10409 | slen -= pos; |
| 10410 | index += pos; |
| 10411 | pos = findchar(src, PyUnicode_KIND(self), slen, u1, 1); |
| 10412 | if (pos < 0) |
| 10413 | break; |
| 10414 | PyUnicode_WRITE(rkind, PyUnicode_DATA(u), index + pos, u2); |
| 10415 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10416 | } |
| 10417 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10418 | int rkind = skind; |
| 10419 | char *res; |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10420 | Py_ssize_t i; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10421 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10422 | if (kind1 < rkind) { |
| 10423 | /* widen substring */ |
| 10424 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10425 | if (!buf1) goto error; |
| 10426 | release1 = 1; |
| 10427 | } |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10428 | i = anylib_find(rkind, self, sbuf, slen, str1, buf1, len1, 0); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10429 | if (i < 0) |
| 10430 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10431 | if (rkind > kind2) { |
| 10432 | /* widen replacement */ |
| 10433 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 10434 | if (!buf2) goto error; |
| 10435 | release2 = 1; |
| 10436 | } |
| 10437 | else if (rkind < kind2) { |
| 10438 | /* widen self and buf1 */ |
| 10439 | rkind = kind2; |
| 10440 | if (release1) PyMem_Free(buf1); |
| 10441 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 10442 | if (!sbuf) goto error; |
| 10443 | srelease = 1; |
| 10444 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10445 | if (!buf1) goto error; |
| 10446 | release1 = 1; |
| 10447 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10448 | u = PyUnicode_New(slen, maxchar); |
| 10449 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10450 | goto error; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10451 | assert(PyUnicode_KIND(u) == rkind); |
| 10452 | res = PyUnicode_DATA(u); |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10453 | |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10454 | memcpy(res, sbuf, rkind * slen); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10455 | /* change everything in-place, starting with this one */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10456 | memcpy(res + rkind * i, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10457 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10458 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10459 | i += len1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10460 | |
| 10461 | while ( --maxcount > 0) { |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10462 | i = anylib_find(rkind, self, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10463 | sbuf+rkind*i, slen-i, |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10464 | str1, buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10465 | if (i == -1) |
| 10466 | break; |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10467 | memcpy(res + rkind * i, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10468 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10469 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10470 | i += len1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10471 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10472 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10473 | } |
| 10474 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10475 | Py_ssize_t n, i, j, ires; |
| 10476 | Py_ssize_t product, new_size; |
| 10477 | int rkind = skind; |
| 10478 | char *res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10479 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10480 | if (kind1 < rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10481 | /* widen substring */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10482 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10483 | if (!buf1) goto error; |
| 10484 | release1 = 1; |
| 10485 | } |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10486 | n = anylib_count(rkind, self, sbuf, slen, str1, buf1, len1, maxcount); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10487 | if (n == 0) |
| 10488 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10489 | if (kind2 < rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10490 | /* widen replacement */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10491 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 10492 | if (!buf2) goto error; |
| 10493 | release2 = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10494 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10495 | else if (kind2 > rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10496 | /* widen self and buf1 */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10497 | rkind = kind2; |
| 10498 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 10499 | if (!sbuf) goto error; |
| 10500 | srelease = 1; |
| 10501 | if (release1) PyMem_Free(buf1); |
| 10502 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10503 | if (!buf1) goto error; |
| 10504 | release1 = 1; |
| 10505 | } |
| 10506 | /* new_size = PyUnicode_GET_LENGTH(self) + n * (PyUnicode_GET_LENGTH(str2) - |
| 10507 | PyUnicode_GET_LENGTH(str1))); */ |
| 10508 | product = n * (len2-len1); |
| 10509 | if ((product / (len2-len1)) != n) { |
| 10510 | PyErr_SetString(PyExc_OverflowError, |
| 10511 | "replace string is too long"); |
| 10512 | goto error; |
| 10513 | } |
| 10514 | new_size = slen + product; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10515 | if (new_size == 0) { |
| 10516 | Py_INCREF(unicode_empty); |
| 10517 | u = unicode_empty; |
| 10518 | goto done; |
| 10519 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10520 | if (new_size < 0 || new_size > (PY_SSIZE_T_MAX >> (rkind-1))) { |
| 10521 | PyErr_SetString(PyExc_OverflowError, |
| 10522 | "replace string is too long"); |
| 10523 | goto error; |
| 10524 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10525 | u = PyUnicode_New(new_size, maxchar); |
| 10526 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10527 | goto error; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10528 | assert(PyUnicode_KIND(u) == rkind); |
| 10529 | res = PyUnicode_DATA(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10530 | ires = i = 0; |
| 10531 | if (len1 > 0) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10532 | while (n-- > 0) { |
| 10533 | /* look for next match */ |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10534 | j = anylib_find(rkind, self, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10535 | sbuf + rkind * i, slen-i, |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10536 | str1, buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10537 | if (j == -1) |
| 10538 | break; |
| 10539 | else if (j > i) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10540 | /* copy unchanged part [i:j] */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10541 | memcpy(res + rkind * ires, |
| 10542 | sbuf + rkind * i, |
| 10543 | rkind * (j-i)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10544 | ires += j - i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10545 | } |
| 10546 | /* copy substitution string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10547 | if (len2 > 0) { |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10548 | memcpy(res + rkind * ires, |
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 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10552 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10553 | i = j + len1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10554 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10555 | if (i < slen) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10556 | /* copy tail [i:] */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10557 | memcpy(res + rkind * ires, |
| 10558 | sbuf + rkind * i, |
| 10559 | rkind * (slen-i)); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10560 | } |
| 10561 | else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10562 | /* interleave */ |
| 10563 | while (n > 0) { |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10564 | memcpy(res + rkind * ires, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10565 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10566 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10567 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10568 | if (--n <= 0) |
| 10569 | break; |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10570 | memcpy(res + rkind * ires, |
| 10571 | sbuf + rkind * i, |
| 10572 | rkind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10573 | ires++; |
| 10574 | i++; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10575 | } |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10576 | memcpy(res + rkind * ires, |
| 10577 | sbuf + rkind * i, |
| 10578 | rkind * (slen-i)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10579 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10580 | } |
| 10581 | |
| 10582 | if (mayshrink) { |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10583 | unicode_adjust_maxchar(&u); |
| 10584 | if (u == NULL) |
| 10585 | goto error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10586 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10587 | |
| 10588 | done: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10589 | if (srelease) |
| 10590 | PyMem_FREE(sbuf); |
| 10591 | if (release1) |
| 10592 | PyMem_FREE(buf1); |
| 10593 | if (release2) |
| 10594 | PyMem_FREE(buf2); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10595 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10596 | return u; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10597 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10598 | nothing: |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10599 | /* nothing to replace; return original string (when possible) */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10600 | if (srelease) |
| 10601 | PyMem_FREE(sbuf); |
| 10602 | if (release1) |
| 10603 | PyMem_FREE(buf1); |
| 10604 | if (release2) |
| 10605 | PyMem_FREE(buf2); |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10606 | return unicode_result_unchanged(self); |
| 10607 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10608 | error: |
| 10609 | if (srelease && sbuf) |
| 10610 | PyMem_FREE(sbuf); |
| 10611 | if (release1 && buf1) |
| 10612 | PyMem_FREE(buf1); |
| 10613 | if (release2 && buf2) |
| 10614 | PyMem_FREE(buf2); |
| 10615 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10616 | } |
| 10617 | |
| 10618 | /* --- Unicode Object Methods --------------------------------------------- */ |
| 10619 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10620 | PyDoc_STRVAR(title__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10621 | "S.title() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10622 | \n\ |
| 10623 | 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] | 10624 | characters, all remaining cased characters have lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10625 | |
| 10626 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10627 | unicode_title(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10628 | { |
Benjamin Peterson | eea4846 | 2012-01-16 14:28:50 -0500 | [diff] [blame] | 10629 | if (PyUnicode_READY(self) == -1) |
| 10630 | return NULL; |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 10631 | return case_operation(self, do_title); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10632 | } |
| 10633 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10634 | PyDoc_STRVAR(capitalize__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10635 | "S.capitalize() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10636 | \n\ |
| 10637 | 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] | 10638 | have upper case and the rest lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10639 | |
| 10640 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10641 | unicode_capitalize(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10642 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 10643 | if (PyUnicode_READY(self) == -1) |
| 10644 | return NULL; |
| 10645 | if (PyUnicode_GET_LENGTH(self) == 0) |
| 10646 | return unicode_result_unchanged(self); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 10647 | return case_operation(self, do_capitalize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10648 | } |
| 10649 | |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 10650 | PyDoc_STRVAR(casefold__doc__, |
| 10651 | "S.casefold() -> str\n\ |
| 10652 | \n\ |
| 10653 | Return a version of S suitable for caseless comparisons."); |
| 10654 | |
| 10655 | static PyObject * |
| 10656 | unicode_casefold(PyObject *self) |
| 10657 | { |
| 10658 | if (PyUnicode_READY(self) == -1) |
| 10659 | return NULL; |
| 10660 | if (PyUnicode_IS_ASCII(self)) |
| 10661 | return ascii_upper_or_lower(self, 1); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 10662 | return case_operation(self, do_casefold); |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 10663 | } |
| 10664 | |
| 10665 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10666 | /* Argument converter. Coerces to a single unicode character */ |
| 10667 | |
| 10668 | static int |
| 10669 | convert_uc(PyObject *obj, void *addr) |
| 10670 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10671 | Py_UCS4 *fillcharloc = (Py_UCS4 *)addr; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10672 | PyObject *uniobj; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10673 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10674 | uniobj = PyUnicode_FromObject(obj); |
| 10675 | if (uniobj == NULL) { |
| 10676 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10677 | "The fill character cannot be converted to Unicode"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10678 | return 0; |
| 10679 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10680 | if (PyUnicode_GET_LENGTH(uniobj) != 1) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10681 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10682 | "The fill character must be exactly one character long"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10683 | Py_DECREF(uniobj); |
| 10684 | return 0; |
| 10685 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10686 | *fillcharloc = PyUnicode_READ_CHAR(uniobj, 0); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10687 | Py_DECREF(uniobj); |
| 10688 | return 1; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10689 | } |
| 10690 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10691 | PyDoc_STRVAR(center__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10692 | "S.center(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10693 | \n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 10694 | Return S centered in a string of length width. Padding is\n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10695 | done using the specified fill character (default is a space)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10696 | |
| 10697 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10698 | unicode_center(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10699 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10700 | Py_ssize_t marg, left; |
| 10701 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10702 | Py_UCS4 fillchar = ' '; |
| 10703 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 10704 | 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] | 10705 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10706 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 10707 | if (PyUnicode_READY(self) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10708 | return NULL; |
| 10709 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10710 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 10711 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10712 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10713 | marg = width - PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10714 | left = marg / 2 + (marg & width & 1); |
| 10715 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10716 | return pad(self, left, marg - left, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10717 | } |
| 10718 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10719 | /* This function assumes that str1 and str2 are readied by the caller. */ |
| 10720 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10721 | static int |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10722 | unicode_compare(PyObject *str1, PyObject *str2) |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10723 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10724 | int kind1, kind2; |
| 10725 | void *data1, *data2; |
| 10726 | Py_ssize_t len1, len2, i; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10727 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10728 | kind1 = PyUnicode_KIND(str1); |
| 10729 | kind2 = PyUnicode_KIND(str2); |
| 10730 | data1 = PyUnicode_DATA(str1); |
| 10731 | data2 = PyUnicode_DATA(str2); |
| 10732 | len1 = PyUnicode_GET_LENGTH(str1); |
| 10733 | len2 = PyUnicode_GET_LENGTH(str2); |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10734 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10735 | for (i = 0; i < len1 && i < len2; ++i) { |
| 10736 | Py_UCS4 c1, c2; |
| 10737 | c1 = PyUnicode_READ(kind1, data1, i); |
| 10738 | c2 = PyUnicode_READ(kind2, data2, i); |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 10739 | |
| 10740 | if (c1 != c2) |
| 10741 | return (c1 < c2) ? -1 : 1; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10742 | } |
| 10743 | |
| 10744 | return (len1 < len2) ? -1 : (len1 != len2); |
| 10745 | } |
| 10746 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10747 | int |
| 10748 | PyUnicode_Compare(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10749 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10750 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 10751 | if (PyUnicode_READY(left) == -1 || |
| 10752 | PyUnicode_READY(right) == -1) |
| 10753 | return -1; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10754 | return unicode_compare(left, right); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10755 | } |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 10756 | PyErr_Format(PyExc_TypeError, |
| 10757 | "Can't compare %.100s and %.100s", |
| 10758 | left->ob_type->tp_name, |
| 10759 | right->ob_type->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10760 | return -1; |
| 10761 | } |
| 10762 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10763 | int |
| 10764 | PyUnicode_CompareWithASCIIString(PyObject* uni, const char* str) |
| 10765 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10766 | Py_ssize_t i; |
| 10767 | int kind; |
| 10768 | void *data; |
| 10769 | Py_UCS4 chr; |
| 10770 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 10771 | assert(_PyUnicode_CHECK(uni)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10772 | if (PyUnicode_READY(uni) == -1) |
| 10773 | return -1; |
| 10774 | kind = PyUnicode_KIND(uni); |
| 10775 | data = PyUnicode_DATA(uni); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10776 | /* Compare Unicode string and source character set string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10777 | for (i = 0; (chr = PyUnicode_READ(kind, data, i)) && str[i]; i++) |
| 10778 | if (chr != str[i]) |
| 10779 | return (chr < (unsigned char)(str[i])) ? -1 : 1; |
Benjamin Peterson | 8667a9b | 2010-01-09 21:45:28 +0000 | [diff] [blame] | 10780 | /* This check keeps Python strings that end in '\0' from comparing equal |
| 10781 | to C strings identical up to that point. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10782 | if (PyUnicode_GET_LENGTH(uni) != i || chr) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10783 | return 1; /* uni is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10784 | if (str[i]) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10785 | return -1; /* str is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10786 | return 0; |
| 10787 | } |
| 10788 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10789 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10790 | #define TEST_COND(cond) \ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10791 | ((cond) ? Py_True : Py_False) |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10792 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10793 | PyObject * |
| 10794 | PyUnicode_RichCompare(PyObject *left, PyObject *right, int op) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10795 | { |
| 10796 | int result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10797 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10798 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 10799 | PyObject *v; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10800 | if (PyUnicode_READY(left) == -1 || |
| 10801 | PyUnicode_READY(right) == -1) |
| 10802 | return NULL; |
| 10803 | if (PyUnicode_GET_LENGTH(left) != PyUnicode_GET_LENGTH(right) || |
| 10804 | PyUnicode_KIND(left) != PyUnicode_KIND(right)) { |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10805 | if (op == Py_EQ) { |
| 10806 | Py_INCREF(Py_False); |
| 10807 | return Py_False; |
| 10808 | } |
| 10809 | if (op == Py_NE) { |
| 10810 | Py_INCREF(Py_True); |
| 10811 | return Py_True; |
| 10812 | } |
| 10813 | } |
| 10814 | if (left == right) |
| 10815 | result = 0; |
| 10816 | else |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10817 | result = unicode_compare(left, right); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10818 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10819 | /* Convert the return value to a Boolean */ |
| 10820 | switch (op) { |
| 10821 | case Py_EQ: |
| 10822 | v = TEST_COND(result == 0); |
| 10823 | break; |
| 10824 | case Py_NE: |
| 10825 | v = TEST_COND(result != 0); |
| 10826 | break; |
| 10827 | case Py_LE: |
| 10828 | v = TEST_COND(result <= 0); |
| 10829 | break; |
| 10830 | case Py_GE: |
| 10831 | v = TEST_COND(result >= 0); |
| 10832 | break; |
| 10833 | case Py_LT: |
| 10834 | v = TEST_COND(result == -1); |
| 10835 | break; |
| 10836 | case Py_GT: |
| 10837 | v = TEST_COND(result == 1); |
| 10838 | break; |
| 10839 | default: |
| 10840 | PyErr_BadArgument(); |
| 10841 | return NULL; |
| 10842 | } |
| 10843 | Py_INCREF(v); |
| 10844 | return v; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10845 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10846 | |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 10847 | Py_RETURN_NOTIMPLEMENTED; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10848 | } |
| 10849 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10850 | int |
| 10851 | PyUnicode_Contains(PyObject *container, PyObject *element) |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10852 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10853 | PyObject *str, *sub; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10854 | int kind1, kind2, kind; |
| 10855 | void *buf1, *buf2; |
| 10856 | Py_ssize_t len1, len2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10857 | int result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10858 | |
| 10859 | /* Coerce the two arguments */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10860 | sub = PyUnicode_FromObject(element); |
| 10861 | if (!sub) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10862 | PyErr_Format(PyExc_TypeError, |
| 10863 | "'in <string>' requires string as left operand, not %s", |
| 10864 | element->ob_type->tp_name); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10865 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10866 | } |
| 10867 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10868 | str = PyUnicode_FromObject(container); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 10869 | if (!str) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10870 | Py_DECREF(sub); |
| 10871 | return -1; |
| 10872 | } |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 10873 | if (PyUnicode_READY(sub) == -1 || PyUnicode_READY(str) == -1) { |
| 10874 | Py_DECREF(sub); |
| 10875 | Py_DECREF(str); |
| 10876 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10877 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10878 | kind1 = PyUnicode_KIND(str); |
| 10879 | kind2 = PyUnicode_KIND(sub); |
| 10880 | kind = kind1 > kind2 ? kind1 : kind2; |
| 10881 | buf1 = PyUnicode_DATA(str); |
| 10882 | buf2 = PyUnicode_DATA(sub); |
| 10883 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10884 | buf1 = _PyUnicode_AsKind(str, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10885 | if (!buf1) { |
| 10886 | Py_DECREF(sub); |
| 10887 | return -1; |
| 10888 | } |
| 10889 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10890 | buf2 = _PyUnicode_AsKind(sub, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10891 | if (!buf2) { |
| 10892 | Py_DECREF(sub); |
| 10893 | if (kind1 != kind) PyMem_Free(buf1); |
| 10894 | return -1; |
| 10895 | } |
| 10896 | len1 = PyUnicode_GET_LENGTH(str); |
| 10897 | len2 = PyUnicode_GET_LENGTH(sub); |
| 10898 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10899 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10900 | case PyUnicode_1BYTE_KIND: |
| 10901 | result = ucs1lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10902 | break; |
| 10903 | case PyUnicode_2BYTE_KIND: |
| 10904 | result = ucs2lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10905 | break; |
| 10906 | case PyUnicode_4BYTE_KIND: |
| 10907 | result = ucs4lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10908 | break; |
| 10909 | default: |
| 10910 | result = -1; |
| 10911 | assert(0); |
| 10912 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10913 | |
| 10914 | Py_DECREF(str); |
| 10915 | Py_DECREF(sub); |
| 10916 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10917 | if (kind1 != kind) |
| 10918 | PyMem_Free(buf1); |
| 10919 | if (kind2 != kind) |
| 10920 | PyMem_Free(buf2); |
| 10921 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10922 | return result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10923 | } |
| 10924 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10925 | /* Concat to string or Unicode object giving a new Unicode object. */ |
| 10926 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10927 | PyObject * |
| 10928 | PyUnicode_Concat(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10929 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10930 | PyObject *u = NULL, *v = NULL, *w; |
Victor Stinner | 127226b | 2011-10-13 01:12:34 +0200 | [diff] [blame] | 10931 | Py_UCS4 maxchar, maxchar2; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10932 | Py_ssize_t u_len, v_len, new_len; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10933 | |
| 10934 | /* Coerce the two arguments */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10935 | u = PyUnicode_FromObject(left); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10936 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10937 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10938 | v = PyUnicode_FromObject(right); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10939 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10940 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10941 | |
| 10942 | /* Shortcuts */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 10943 | if (v == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10944 | Py_DECREF(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10945 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10946 | } |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 10947 | if (u == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10948 | Py_DECREF(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10949 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10950 | } |
| 10951 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10952 | u_len = PyUnicode_GET_LENGTH(u); |
| 10953 | v_len = PyUnicode_GET_LENGTH(v); |
| 10954 | if (u_len > PY_SSIZE_T_MAX - v_len) { |
| 10955 | PyErr_SetString(PyExc_OverflowError, |
| 10956 | "strings are too large to concat"); |
| 10957 | goto onError; |
| 10958 | } |
| 10959 | new_len = u_len + v_len; |
| 10960 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10961 | maxchar = PyUnicode_MAX_CHAR_VALUE(u); |
Victor Stinner | 127226b | 2011-10-13 01:12:34 +0200 | [diff] [blame] | 10962 | maxchar2 = PyUnicode_MAX_CHAR_VALUE(v); |
| 10963 | maxchar = Py_MAX(maxchar, maxchar2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10964 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10965 | /* Concat the two Unicode strings */ |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10966 | w = PyUnicode_New(new_len, maxchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10967 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10968 | goto onError; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10969 | copy_characters(w, 0, u, 0, u_len); |
| 10970 | copy_characters(w, u_len, v, 0, v_len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10971 | Py_DECREF(u); |
| 10972 | Py_DECREF(v); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10973 | assert(_PyUnicode_CheckConsistency(w, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10974 | return w; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10975 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10976 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10977 | Py_XDECREF(u); |
| 10978 | Py_XDECREF(v); |
| 10979 | return NULL; |
| 10980 | } |
| 10981 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10982 | void |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10983 | PyUnicode_Append(PyObject **p_left, PyObject *right) |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10984 | { |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10985 | PyObject *left, *res; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10986 | Py_UCS4 maxchar, maxchar2; |
| 10987 | Py_ssize_t left_len, right_len, new_len; |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10988 | |
| 10989 | if (p_left == NULL) { |
| 10990 | if (!PyErr_Occurred()) |
| 10991 | PyErr_BadInternalCall(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10992 | return; |
| 10993 | } |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10994 | left = *p_left; |
| 10995 | if (right == NULL || !PyUnicode_Check(left)) { |
| 10996 | if (!PyErr_Occurred()) |
| 10997 | PyErr_BadInternalCall(); |
| 10998 | goto error; |
| 10999 | } |
| 11000 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 11001 | if (PyUnicode_READY(left) == -1) |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 11002 | goto error; |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 11003 | if (PyUnicode_READY(right) == -1) |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 11004 | goto error; |
| 11005 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11006 | /* Shortcuts */ |
| 11007 | if (left == unicode_empty) { |
| 11008 | Py_DECREF(left); |
| 11009 | Py_INCREF(right); |
| 11010 | *p_left = right; |
| 11011 | return; |
| 11012 | } |
| 11013 | if (right == unicode_empty) |
| 11014 | return; |
| 11015 | |
| 11016 | left_len = PyUnicode_GET_LENGTH(left); |
| 11017 | right_len = PyUnicode_GET_LENGTH(right); |
| 11018 | if (left_len > PY_SSIZE_T_MAX - right_len) { |
| 11019 | PyErr_SetString(PyExc_OverflowError, |
| 11020 | "strings are too large to concat"); |
| 11021 | goto error; |
| 11022 | } |
| 11023 | new_len = left_len + right_len; |
| 11024 | |
| 11025 | if (unicode_modifiable(left) |
| 11026 | && PyUnicode_CheckExact(right) |
| 11027 | && PyUnicode_KIND(right) <= PyUnicode_KIND(left) |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 11028 | /* Don't resize for ascii += latin1. Convert ascii to latin1 requires |
| 11029 | to change the structure size, but characters are stored just after |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 11030 | the structure, and so it requires to move all characters which is |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 11031 | not so different than duplicating the string. */ |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11032 | && !(PyUnicode_IS_ASCII(left) && !PyUnicode_IS_ASCII(right))) |
| 11033 | { |
| 11034 | /* append inplace */ |
| 11035 | if (unicode_resize(p_left, new_len) != 0) { |
| 11036 | /* XXX if _PyUnicode_Resize() fails, 'left' has been |
| 11037 | * deallocated so it cannot be put back into |
| 11038 | * 'variable'. The MemoryError is raised when there |
| 11039 | * is no value in 'variable', which might (very |
| 11040 | * remotely) be a cause of incompatibilities. |
| 11041 | */ |
| 11042 | goto error; |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11043 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11044 | /* copy 'right' into the newly allocated area of 'left' */ |
| 11045 | copy_characters(*p_left, left_len, right, 0, right_len); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11046 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11047 | else { |
| 11048 | maxchar = PyUnicode_MAX_CHAR_VALUE(left); |
| 11049 | maxchar2 = PyUnicode_MAX_CHAR_VALUE(right); |
| 11050 | maxchar = Py_MAX(maxchar, maxchar2); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11051 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11052 | /* Concat the two Unicode strings */ |
| 11053 | res = PyUnicode_New(new_len, maxchar); |
| 11054 | if (res == NULL) |
| 11055 | goto error; |
| 11056 | copy_characters(res, 0, left, 0, left_len); |
| 11057 | copy_characters(res, left_len, right, 0, right_len); |
| 11058 | Py_DECREF(left); |
| 11059 | *p_left = res; |
| 11060 | } |
| 11061 | assert(_PyUnicode_CheckConsistency(*p_left, 1)); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11062 | return; |
| 11063 | |
| 11064 | error: |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11065 | Py_CLEAR(*p_left); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 11066 | } |
| 11067 | |
| 11068 | void |
| 11069 | PyUnicode_AppendAndDel(PyObject **pleft, PyObject *right) |
| 11070 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11071 | PyUnicode_Append(pleft, right); |
| 11072 | Py_XDECREF(right); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 11073 | } |
| 11074 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11075 | PyDoc_STRVAR(count__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11076 | "S.count(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11077 | \n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11078 | Return the number of non-overlapping occurrences of substring sub in\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11079 | 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] | 11080 | interpreted as in slice notation."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11081 | |
| 11082 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11083 | unicode_count(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11084 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11085 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11086 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11087 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11088 | PyObject *result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11089 | int kind1, kind2, kind; |
| 11090 | void *buf1, *buf2; |
| 11091 | Py_ssize_t len1, len2, iresult; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11092 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11093 | if (!stringlib_parse_args_finds_unicode("count", args, &substring, |
| 11094 | &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11095 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11096 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11097 | kind1 = PyUnicode_KIND(self); |
| 11098 | kind2 = PyUnicode_KIND(substring); |
| 11099 | kind = kind1 > kind2 ? kind1 : kind2; |
| 11100 | buf1 = PyUnicode_DATA(self); |
| 11101 | buf2 = PyUnicode_DATA(substring); |
| 11102 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11103 | buf1 = _PyUnicode_AsKind(self, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11104 | if (!buf1) { |
| 11105 | Py_DECREF(substring); |
| 11106 | return NULL; |
| 11107 | } |
| 11108 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11109 | buf2 = _PyUnicode_AsKind(substring, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11110 | if (!buf2) { |
| 11111 | Py_DECREF(substring); |
| 11112 | if (kind1 != kind) PyMem_Free(buf1); |
| 11113 | return NULL; |
| 11114 | } |
| 11115 | len1 = PyUnicode_GET_LENGTH(self); |
| 11116 | len2 = PyUnicode_GET_LENGTH(substring); |
| 11117 | |
| 11118 | ADJUST_INDICES(start, end, len1); |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 11119 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11120 | case PyUnicode_1BYTE_KIND: |
| 11121 | iresult = ucs1lib_count( |
| 11122 | ((Py_UCS1*)buf1) + start, end - start, |
| 11123 | buf2, len2, PY_SSIZE_T_MAX |
| 11124 | ); |
| 11125 | break; |
| 11126 | case PyUnicode_2BYTE_KIND: |
| 11127 | iresult = ucs2lib_count( |
| 11128 | ((Py_UCS2*)buf1) + start, end - start, |
| 11129 | buf2, len2, PY_SSIZE_T_MAX |
| 11130 | ); |
| 11131 | break; |
| 11132 | case PyUnicode_4BYTE_KIND: |
| 11133 | iresult = ucs4lib_count( |
| 11134 | ((Py_UCS4*)buf1) + start, end - start, |
| 11135 | buf2, len2, PY_SSIZE_T_MAX |
| 11136 | ); |
| 11137 | break; |
| 11138 | default: |
| 11139 | assert(0); iresult = 0; |
| 11140 | } |
| 11141 | |
| 11142 | result = PyLong_FromSsize_t(iresult); |
| 11143 | |
| 11144 | if (kind1 != kind) |
| 11145 | PyMem_Free(buf1); |
| 11146 | if (kind2 != kind) |
| 11147 | PyMem_Free(buf2); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11148 | |
| 11149 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11150 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11151 | return result; |
| 11152 | } |
| 11153 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11154 | PyDoc_STRVAR(encode__doc__, |
Victor Stinner | c911bbf | 2010-11-07 19:04:46 +0000 | [diff] [blame] | 11155 | "S.encode(encoding='utf-8', errors='strict') -> bytes\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11156 | \n\ |
Victor Stinner | e14e212 | 2010-11-07 18:41:46 +0000 | [diff] [blame] | 11157 | Encode S using the codec registered for encoding. Default encoding\n\ |
| 11158 | 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] | 11159 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 11160 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 11161 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 11162 | codecs.register_error that can handle UnicodeEncodeErrors."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11163 | |
| 11164 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11165 | unicode_encode(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11166 | { |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 11167 | static char *kwlist[] = {"encoding", "errors", 0}; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11168 | char *encoding = NULL; |
| 11169 | char *errors = NULL; |
Guido van Rossum | 35d9428 | 2007-08-27 18:20:11 +0000 | [diff] [blame] | 11170 | |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 11171 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:encode", |
| 11172 | kwlist, &encoding, &errors)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11173 | return NULL; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11174 | return PyUnicode_AsEncodedString(self, encoding, errors); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 11175 | } |
| 11176 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11177 | PyDoc_STRVAR(expandtabs__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11178 | "S.expandtabs([tabsize]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11179 | \n\ |
| 11180 | 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] | 11181 | 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] | 11182 | |
| 11183 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11184 | unicode_expandtabs(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11185 | { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11186 | Py_ssize_t i, j, line_pos, src_len, incr; |
| 11187 | Py_UCS4 ch; |
| 11188 | PyObject *u; |
| 11189 | void *src_data, *dest_data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11190 | int tabsize = 8; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11191 | int kind; |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 11192 | int found; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11193 | |
| 11194 | if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11195 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11196 | |
Antoine Pitrou | 2242522 | 2011-10-04 19:10:51 +0200 | [diff] [blame] | 11197 | if (PyUnicode_READY(self) == -1) |
| 11198 | return NULL; |
| 11199 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 11200 | /* First pass: determine size of output string */ |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11201 | src_len = PyUnicode_GET_LENGTH(self); |
| 11202 | i = j = line_pos = 0; |
| 11203 | kind = PyUnicode_KIND(self); |
| 11204 | src_data = PyUnicode_DATA(self); |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 11205 | found = 0; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11206 | for (; i < src_len; i++) { |
| 11207 | ch = PyUnicode_READ(kind, src_data, i); |
| 11208 | if (ch == '\t') { |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 11209 | found = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11210 | if (tabsize > 0) { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11211 | incr = tabsize - (line_pos % tabsize); /* cannot overflow */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11212 | if (j > PY_SSIZE_T_MAX - incr) |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11213 | goto overflow; |
| 11214 | line_pos += incr; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11215 | j += incr; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11216 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11217 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11218 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11219 | if (j > PY_SSIZE_T_MAX - 1) |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11220 | goto overflow; |
| 11221 | line_pos++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11222 | j++; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11223 | if (ch == '\n' || ch == '\r') |
| 11224 | line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11225 | } |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11226 | } |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11227 | if (!found) |
| 11228 | return unicode_result_unchanged(self); |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 11229 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11230 | /* Second pass: create output string and fill it */ |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11231 | u = PyUnicode_New(j, PyUnicode_MAX_CHAR_VALUE(self)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11232 | if (!u) |
| 11233 | return NULL; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11234 | dest_data = PyUnicode_DATA(u); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11235 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11236 | i = j = line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11237 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11238 | for (; i < src_len; i++) { |
| 11239 | ch = PyUnicode_READ(kind, src_data, i); |
| 11240 | if (ch == '\t') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11241 | if (tabsize > 0) { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11242 | incr = tabsize - (line_pos % tabsize); |
| 11243 | line_pos += incr; |
Victor Stinner | da79e63 | 2012-02-22 13:37:04 +0100 | [diff] [blame] | 11244 | FILL(kind, dest_data, ' ', j, incr); |
| 11245 | j += incr; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11246 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11247 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11248 | else { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11249 | line_pos++; |
| 11250 | PyUnicode_WRITE(kind, dest_data, j, ch); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11251 | j++; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11252 | if (ch == '\n' || ch == '\r') |
| 11253 | line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11254 | } |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11255 | } |
| 11256 | assert (j == PyUnicode_GET_LENGTH(u)); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 11257 | return unicode_result(u); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11258 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11259 | overflow: |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11260 | PyErr_SetString(PyExc_OverflowError, "new string is too long"); |
| 11261 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11262 | } |
| 11263 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11264 | PyDoc_STRVAR(find__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11265 | "S.find(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11266 | \n\ |
| 11267 | Return the lowest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 11268 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11269 | arguments start and end are interpreted as in slice notation.\n\ |
| 11270 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11271 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11272 | |
| 11273 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11274 | unicode_find(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11275 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11276 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 11277 | Py_ssize_t start; |
| 11278 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11279 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11280 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11281 | if (!stringlib_parse_args_finds_unicode("find", args, &substring, |
| 11282 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11283 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11284 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11285 | if (PyUnicode_READY(self) == -1) |
| 11286 | return NULL; |
| 11287 | if (PyUnicode_READY(substring) == -1) |
| 11288 | return NULL; |
| 11289 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11290 | result = any_find_slice(1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11291 | |
| 11292 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11293 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11294 | if (result == -2) |
| 11295 | return NULL; |
| 11296 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 11297 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11298 | } |
| 11299 | |
| 11300 | static PyObject * |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 11301 | unicode_getitem(PyObject *self, Py_ssize_t index) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11302 | { |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 11303 | Py_UCS4 ch = PyUnicode_ReadChar(self, index); |
| 11304 | if (ch == (Py_UCS4)-1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11305 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11306 | return PyUnicode_FromOrdinal(ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11307 | } |
| 11308 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11309 | /* Believe it or not, this produces the same value for ASCII strings |
Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 11310 | as bytes_hash(). */ |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 11311 | static Py_hash_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11312 | unicode_hash(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11313 | { |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11314 | Py_ssize_t len; |
Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 11315 | Py_uhash_t x; |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11316 | |
Benjamin Peterson | 69e9727 | 2012-02-21 11:08:50 -0500 | [diff] [blame] | 11317 | assert(_Py_HashSecret_Initialized); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11318 | if (_PyUnicode_HASH(self) != -1) |
| 11319 | return _PyUnicode_HASH(self); |
| 11320 | if (PyUnicode_READY(self) == -1) |
| 11321 | return -1; |
| 11322 | len = PyUnicode_GET_LENGTH(self); |
Georg Brandl | 16fa2a1 | 2012-02-21 00:50:13 +0100 | [diff] [blame] | 11323 | /* |
| 11324 | We make the hash of the empty string be 0, rather than using |
| 11325 | (prefix ^ suffix), since this slightly obfuscates the hash secret |
| 11326 | */ |
| 11327 | if (len == 0) { |
| 11328 | _PyUnicode_HASH(self) = 0; |
| 11329 | return 0; |
| 11330 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11331 | |
| 11332 | /* The hash function as a macro, gets expanded three times below. */ |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 11333 | #define HASH(P) \ |
| 11334 | x ^= (Py_uhash_t) *P << 7; \ |
| 11335 | while (--len >= 0) \ |
| 11336 | x = (_PyHASH_MULTIPLIER * x) ^ (Py_uhash_t) *P++; \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11337 | |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 11338 | x = (Py_uhash_t) _Py_HashSecret.prefix; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11339 | switch (PyUnicode_KIND(self)) { |
| 11340 | case PyUnicode_1BYTE_KIND: { |
| 11341 | const unsigned char *c = PyUnicode_1BYTE_DATA(self); |
| 11342 | HASH(c); |
| 11343 | break; |
| 11344 | } |
| 11345 | case PyUnicode_2BYTE_KIND: { |
| 11346 | const Py_UCS2 *s = PyUnicode_2BYTE_DATA(self); |
| 11347 | HASH(s); |
| 11348 | break; |
| 11349 | } |
| 11350 | default: { |
| 11351 | Py_UCS4 *l; |
| 11352 | assert(PyUnicode_KIND(self) == PyUnicode_4BYTE_KIND && |
| 11353 | "Impossible switch case in unicode_hash"); |
| 11354 | l = PyUnicode_4BYTE_DATA(self); |
| 11355 | HASH(l); |
| 11356 | break; |
| 11357 | } |
| 11358 | } |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 11359 | x ^= (Py_uhash_t) PyUnicode_GET_LENGTH(self); |
| 11360 | x ^= (Py_uhash_t) _Py_HashSecret.suffix; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11361 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11362 | if (x == -1) |
| 11363 | x = -2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11364 | _PyUnicode_HASH(self) = x; |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11365 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11366 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11367 | #undef HASH |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11368 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11369 | PyDoc_STRVAR(index__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11370 | "S.index(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11371 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11372 | 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] | 11373 | |
| 11374 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11375 | unicode_index(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11376 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11377 | Py_ssize_t result; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11378 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 11379 | Py_ssize_t start; |
| 11380 | Py_ssize_t end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11381 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11382 | if (!stringlib_parse_args_finds_unicode("index", args, &substring, |
| 11383 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11384 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11385 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11386 | if (PyUnicode_READY(self) == -1) |
| 11387 | return NULL; |
| 11388 | if (PyUnicode_READY(substring) == -1) |
| 11389 | return NULL; |
| 11390 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11391 | result = any_find_slice(1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11392 | |
| 11393 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11394 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11395 | if (result == -2) |
| 11396 | return NULL; |
| 11397 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11398 | if (result < 0) { |
| 11399 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 11400 | return NULL; |
| 11401 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11402 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 11403 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11404 | } |
| 11405 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11406 | PyDoc_STRVAR(islower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11407 | "S.islower() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11408 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11409 | 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] | 11410 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11411 | |
| 11412 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11413 | unicode_islower(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11414 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11415 | Py_ssize_t i, length; |
| 11416 | int kind; |
| 11417 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11418 | int cased; |
| 11419 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11420 | if (PyUnicode_READY(self) == -1) |
| 11421 | return NULL; |
| 11422 | length = PyUnicode_GET_LENGTH(self); |
| 11423 | kind = PyUnicode_KIND(self); |
| 11424 | data = PyUnicode_DATA(self); |
| 11425 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11426 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11427 | if (length == 1) |
| 11428 | return PyBool_FromLong( |
| 11429 | Py_UNICODE_ISLOWER(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11430 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11431 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11432 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11433 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11434 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11435 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11436 | for (i = 0; i < length; i++) { |
| 11437 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11438 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11439 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 11440 | return PyBool_FromLong(0); |
| 11441 | else if (!cased && Py_UNICODE_ISLOWER(ch)) |
| 11442 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11443 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11444 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11445 | } |
| 11446 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11447 | PyDoc_STRVAR(isupper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11448 | "S.isupper() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11449 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11450 | 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] | 11451 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11452 | |
| 11453 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11454 | unicode_isupper(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11455 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11456 | Py_ssize_t i, length; |
| 11457 | int kind; |
| 11458 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11459 | int cased; |
| 11460 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11461 | if (PyUnicode_READY(self) == -1) |
| 11462 | return NULL; |
| 11463 | length = PyUnicode_GET_LENGTH(self); |
| 11464 | kind = PyUnicode_KIND(self); |
| 11465 | data = PyUnicode_DATA(self); |
| 11466 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11467 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11468 | if (length == 1) |
| 11469 | return PyBool_FromLong( |
| 11470 | Py_UNICODE_ISUPPER(PyUnicode_READ(kind, data, 0)) != 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11471 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11472 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11473 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11474 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11475 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11476 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11477 | for (i = 0; i < length; i++) { |
| 11478 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11479 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11480 | if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 11481 | return PyBool_FromLong(0); |
| 11482 | else if (!cased && Py_UNICODE_ISUPPER(ch)) |
| 11483 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11484 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11485 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11486 | } |
| 11487 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11488 | PyDoc_STRVAR(istitle__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11489 | "S.istitle() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11490 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11491 | Return True if S is a titlecased string and there is at least one\n\ |
| 11492 | character in S, i.e. upper- and titlecase characters may only\n\ |
| 11493 | follow uncased characters and lowercase characters only cased ones.\n\ |
| 11494 | Return False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11495 | |
| 11496 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11497 | unicode_istitle(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11498 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11499 | Py_ssize_t i, length; |
| 11500 | int kind; |
| 11501 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11502 | int cased, previous_is_cased; |
| 11503 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11504 | if (PyUnicode_READY(self) == -1) |
| 11505 | return NULL; |
| 11506 | length = PyUnicode_GET_LENGTH(self); |
| 11507 | kind = PyUnicode_KIND(self); |
| 11508 | data = PyUnicode_DATA(self); |
| 11509 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11510 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11511 | if (length == 1) { |
| 11512 | Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11513 | return PyBool_FromLong((Py_UNICODE_ISTITLE(ch) != 0) || |
| 11514 | (Py_UNICODE_ISUPPER(ch) != 0)); |
| 11515 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11516 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11517 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11518 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11519 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11520 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11521 | cased = 0; |
| 11522 | previous_is_cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11523 | for (i = 0; i < length; i++) { |
| 11524 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11525 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11526 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { |
| 11527 | if (previous_is_cased) |
| 11528 | return PyBool_FromLong(0); |
| 11529 | previous_is_cased = 1; |
| 11530 | cased = 1; |
| 11531 | } |
| 11532 | else if (Py_UNICODE_ISLOWER(ch)) { |
| 11533 | if (!previous_is_cased) |
| 11534 | return PyBool_FromLong(0); |
| 11535 | previous_is_cased = 1; |
| 11536 | cased = 1; |
| 11537 | } |
| 11538 | else |
| 11539 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11540 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11541 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11542 | } |
| 11543 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11544 | PyDoc_STRVAR(isspace__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11545 | "S.isspace() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11546 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11547 | Return True if all characters in S are whitespace\n\ |
| 11548 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11549 | |
| 11550 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11551 | unicode_isspace(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11552 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11553 | Py_ssize_t i, length; |
| 11554 | int kind; |
| 11555 | void *data; |
| 11556 | |
| 11557 | if (PyUnicode_READY(self) == -1) |
| 11558 | return NULL; |
| 11559 | length = PyUnicode_GET_LENGTH(self); |
| 11560 | kind = PyUnicode_KIND(self); |
| 11561 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11562 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11563 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11564 | if (length == 1) |
| 11565 | return PyBool_FromLong( |
| 11566 | Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11567 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11568 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11569 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11570 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11571 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11572 | for (i = 0; i < length; i++) { |
| 11573 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11574 | if (!Py_UNICODE_ISSPACE(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11575 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11576 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11577 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11578 | } |
| 11579 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11580 | PyDoc_STRVAR(isalpha__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11581 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11582 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11583 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11584 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11585 | |
| 11586 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11587 | unicode_isalpha(PyObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11588 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11589 | Py_ssize_t i, length; |
| 11590 | int kind; |
| 11591 | void *data; |
| 11592 | |
| 11593 | if (PyUnicode_READY(self) == -1) |
| 11594 | return NULL; |
| 11595 | length = PyUnicode_GET_LENGTH(self); |
| 11596 | kind = PyUnicode_KIND(self); |
| 11597 | data = PyUnicode_DATA(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11598 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11599 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11600 | if (length == 1) |
| 11601 | return PyBool_FromLong( |
| 11602 | Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, 0))); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11603 | |
| 11604 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11605 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11606 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11607 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11608 | for (i = 0; i < length; i++) { |
| 11609 | if (!Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11610 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11611 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11612 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11613 | } |
| 11614 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11615 | PyDoc_STRVAR(isalnum__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11616 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11617 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11618 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11619 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11620 | |
| 11621 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11622 | unicode_isalnum(PyObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11623 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11624 | int kind; |
| 11625 | void *data; |
| 11626 | Py_ssize_t len, i; |
| 11627 | |
| 11628 | if (PyUnicode_READY(self) == -1) |
| 11629 | return NULL; |
| 11630 | |
| 11631 | kind = PyUnicode_KIND(self); |
| 11632 | data = PyUnicode_DATA(self); |
| 11633 | len = PyUnicode_GET_LENGTH(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11634 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11635 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11636 | if (len == 1) { |
| 11637 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11638 | return PyBool_FromLong(Py_UNICODE_ISALNUM(ch)); |
| 11639 | } |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11640 | |
| 11641 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11642 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11643 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11644 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11645 | for (i = 0; i < len; i++) { |
| 11646 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11647 | if (!Py_UNICODE_ISALNUM(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11648 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11649 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11650 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11651 | } |
| 11652 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11653 | PyDoc_STRVAR(isdecimal__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11654 | "S.isdecimal() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11655 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11656 | 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] | 11657 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11658 | |
| 11659 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11660 | unicode_isdecimal(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11661 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11662 | Py_ssize_t i, length; |
| 11663 | int kind; |
| 11664 | void *data; |
| 11665 | |
| 11666 | if (PyUnicode_READY(self) == -1) |
| 11667 | return NULL; |
| 11668 | length = PyUnicode_GET_LENGTH(self); |
| 11669 | kind = PyUnicode_KIND(self); |
| 11670 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11671 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11672 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11673 | if (length == 1) |
| 11674 | return PyBool_FromLong( |
| 11675 | Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11676 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11677 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11678 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11679 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11680 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11681 | for (i = 0; i < length; i++) { |
| 11682 | if (!Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11683 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11684 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11685 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11686 | } |
| 11687 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11688 | PyDoc_STRVAR(isdigit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11689 | "S.isdigit() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11690 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11691 | Return True if all characters in S are digits\n\ |
| 11692 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11693 | |
| 11694 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11695 | unicode_isdigit(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11696 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11697 | Py_ssize_t i, length; |
| 11698 | int kind; |
| 11699 | void *data; |
| 11700 | |
| 11701 | if (PyUnicode_READY(self) == -1) |
| 11702 | return NULL; |
| 11703 | length = PyUnicode_GET_LENGTH(self); |
| 11704 | kind = PyUnicode_KIND(self); |
| 11705 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11706 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11707 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11708 | if (length == 1) { |
| 11709 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11710 | return PyBool_FromLong(Py_UNICODE_ISDIGIT(ch)); |
| 11711 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11712 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11713 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11714 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11715 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11716 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11717 | for (i = 0; i < length; i++) { |
| 11718 | if (!Py_UNICODE_ISDIGIT(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11719 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11720 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11721 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11722 | } |
| 11723 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11724 | PyDoc_STRVAR(isnumeric__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11725 | "S.isnumeric() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11726 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11727 | 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] | 11728 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11729 | |
| 11730 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11731 | unicode_isnumeric(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11732 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11733 | Py_ssize_t i, length; |
| 11734 | int kind; |
| 11735 | void *data; |
| 11736 | |
| 11737 | if (PyUnicode_READY(self) == -1) |
| 11738 | return NULL; |
| 11739 | length = PyUnicode_GET_LENGTH(self); |
| 11740 | kind = PyUnicode_KIND(self); |
| 11741 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11742 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11743 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11744 | if (length == 1) |
| 11745 | return PyBool_FromLong( |
| 11746 | Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11747 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11748 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11749 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11750 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11751 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11752 | for (i = 0; i < length; i++) { |
| 11753 | if (!Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11754 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11755 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11756 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11757 | } |
| 11758 | |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11759 | int |
| 11760 | PyUnicode_IsIdentifier(PyObject *self) |
| 11761 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11762 | int kind; |
| 11763 | void *data; |
| 11764 | Py_ssize_t i; |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11765 | Py_UCS4 first; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11766 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11767 | if (PyUnicode_READY(self) == -1) { |
| 11768 | Py_FatalError("identifier not ready"); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11769 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11770 | } |
| 11771 | |
| 11772 | /* Special case for empty strings */ |
| 11773 | if (PyUnicode_GET_LENGTH(self) == 0) |
| 11774 | return 0; |
| 11775 | kind = PyUnicode_KIND(self); |
| 11776 | data = PyUnicode_DATA(self); |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11777 | |
| 11778 | /* PEP 3131 says that the first character must be in |
| 11779 | XID_Start and subsequent characters in XID_Continue, |
| 11780 | and for the ASCII range, the 2.x rules apply (i.e |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11781 | start with letters and underscore, continue with |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11782 | letters, digits, underscore). However, given the current |
| 11783 | definition of XID_Start and XID_Continue, it is sufficient |
| 11784 | to check just for these, except that _ must be allowed |
| 11785 | as starting an identifier. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11786 | first = PyUnicode_READ(kind, data, 0); |
Benjamin Peterson | f413b80 | 2011-08-12 22:17:18 -0500 | [diff] [blame] | 11787 | if (!_PyUnicode_IsXidStart(first) && first != 0x5F /* LOW LINE */) |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11788 | return 0; |
| 11789 | |
Benjamin Peterson | 9c6e6a0 | 2011-09-28 08:09:05 -0400 | [diff] [blame] | 11790 | for (i = 1; i < PyUnicode_GET_LENGTH(self); i++) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11791 | if (!_PyUnicode_IsXidContinue(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11792 | return 0; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11793 | return 1; |
| 11794 | } |
| 11795 | |
| 11796 | PyDoc_STRVAR(isidentifier__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11797 | "S.isidentifier() -> bool\n\ |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11798 | \n\ |
| 11799 | Return True if S is a valid identifier according\n\ |
| 11800 | to the language definition."); |
| 11801 | |
| 11802 | static PyObject* |
| 11803 | unicode_isidentifier(PyObject *self) |
| 11804 | { |
| 11805 | return PyBool_FromLong(PyUnicode_IsIdentifier(self)); |
| 11806 | } |
| 11807 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11808 | PyDoc_STRVAR(isprintable__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11809 | "S.isprintable() -> bool\n\ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11810 | \n\ |
| 11811 | Return True if all characters in S are considered\n\ |
| 11812 | printable in repr() or S is empty, False otherwise."); |
| 11813 | |
| 11814 | static PyObject* |
| 11815 | unicode_isprintable(PyObject *self) |
| 11816 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11817 | Py_ssize_t i, length; |
| 11818 | int kind; |
| 11819 | void *data; |
| 11820 | |
| 11821 | if (PyUnicode_READY(self) == -1) |
| 11822 | return NULL; |
| 11823 | length = PyUnicode_GET_LENGTH(self); |
| 11824 | kind = PyUnicode_KIND(self); |
| 11825 | data = PyUnicode_DATA(self); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11826 | |
| 11827 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11828 | if (length == 1) |
| 11829 | return PyBool_FromLong( |
| 11830 | Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, 0))); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11831 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11832 | for (i = 0; i < length; i++) { |
| 11833 | if (!Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, i))) { |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11834 | Py_RETURN_FALSE; |
| 11835 | } |
| 11836 | } |
| 11837 | Py_RETURN_TRUE; |
| 11838 | } |
| 11839 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11840 | PyDoc_STRVAR(join__doc__, |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 11841 | "S.join(iterable) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11842 | \n\ |
| 11843 | 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] | 11844 | iterable. The separator between elements is S."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11845 | |
| 11846 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11847 | unicode_join(PyObject *self, PyObject *data) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11848 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11849 | return PyUnicode_Join(self, data); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11850 | } |
| 11851 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11852 | static Py_ssize_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11853 | unicode_length(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11854 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11855 | if (PyUnicode_READY(self) == -1) |
| 11856 | return -1; |
| 11857 | return PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11858 | } |
| 11859 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11860 | PyDoc_STRVAR(ljust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11861 | "S.ljust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11862 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 11863 | 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] | 11864 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11865 | |
| 11866 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11867 | unicode_ljust(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11868 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11869 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11870 | Py_UCS4 fillchar = ' '; |
| 11871 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11872 | if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11873 | return NULL; |
| 11874 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 11875 | if (PyUnicode_READY(self) == -1) |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11876 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11877 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11878 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 11879 | return unicode_result_unchanged(self); |
| 11880 | |
| 11881 | return pad(self, 0, width - PyUnicode_GET_LENGTH(self), fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11882 | } |
| 11883 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11884 | PyDoc_STRVAR(lower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11885 | "S.lower() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11886 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11887 | Return a copy of the string S converted to lowercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11888 | |
| 11889 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11890 | unicode_lower(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11891 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 11892 | if (PyUnicode_READY(self) == -1) |
| 11893 | return NULL; |
| 11894 | if (PyUnicode_IS_ASCII(self)) |
| 11895 | return ascii_upper_or_lower(self, 1); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 11896 | return case_operation(self, do_lower); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11897 | } |
| 11898 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11899 | #define LEFTSTRIP 0 |
| 11900 | #define RIGHTSTRIP 1 |
| 11901 | #define BOTHSTRIP 2 |
| 11902 | |
| 11903 | /* Arrays indexed by above */ |
| 11904 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 11905 | |
| 11906 | #define STRIPNAME(i) (stripformat[i]+3) |
| 11907 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11908 | /* externally visible for str.strip(unicode) */ |
| 11909 | PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11910 | _PyUnicode_XStrip(PyObject *self, int striptype, PyObject *sepobj) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11911 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11912 | void *data; |
| 11913 | int kind; |
| 11914 | Py_ssize_t i, j, len; |
| 11915 | BLOOM_MASK sepmask; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11916 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11917 | if (PyUnicode_READY(self) == -1 || PyUnicode_READY(sepobj) == -1) |
| 11918 | return NULL; |
| 11919 | |
| 11920 | kind = PyUnicode_KIND(self); |
| 11921 | data = PyUnicode_DATA(self); |
| 11922 | len = PyUnicode_GET_LENGTH(self); |
| 11923 | sepmask = make_bloom_mask(PyUnicode_KIND(sepobj), |
| 11924 | PyUnicode_DATA(sepobj), |
| 11925 | PyUnicode_GET_LENGTH(sepobj)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11926 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11927 | i = 0; |
| 11928 | if (striptype != RIGHTSTRIP) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11929 | while (i < len && |
| 11930 | BLOOM_MEMBER(sepmask, PyUnicode_READ(kind, data, i), sepobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11931 | i++; |
| 11932 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11933 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11934 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11935 | j = len; |
| 11936 | if (striptype != LEFTSTRIP) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11937 | do { |
| 11938 | j--; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11939 | } while (j >= i && |
| 11940 | BLOOM_MEMBER(sepmask, PyUnicode_READ(kind, data, j), sepobj)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11941 | j++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11942 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11943 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11944 | return PyUnicode_Substring(self, i, j); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11945 | } |
| 11946 | |
| 11947 | PyObject* |
| 11948 | PyUnicode_Substring(PyObject *self, Py_ssize_t start, Py_ssize_t end) |
| 11949 | { |
| 11950 | unsigned char *data; |
| 11951 | int kind; |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11952 | Py_ssize_t length; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11953 | |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 11954 | if (PyUnicode_READY(self) == -1) |
| 11955 | return NULL; |
| 11956 | |
| 11957 | end = Py_MIN(end, PyUnicode_GET_LENGTH(self)); |
| 11958 | |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11959 | if (start == 0 && end == PyUnicode_GET_LENGTH(self)) |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11960 | return unicode_result_unchanged(self); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11961 | |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11962 | length = end - start; |
| 11963 | if (length == 1) |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 11964 | return unicode_getitem(self, start); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11965 | |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 11966 | if (start < 0 || end < 0) { |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11967 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 11968 | return NULL; |
| 11969 | } |
| 11970 | |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 11971 | if (PyUnicode_IS_ASCII(self)) { |
| 11972 | kind = PyUnicode_KIND(self); |
| 11973 | data = PyUnicode_1BYTE_DATA(self); |
| 11974 | return unicode_fromascii(data + start, length); |
| 11975 | } |
| 11976 | else { |
| 11977 | kind = PyUnicode_KIND(self); |
| 11978 | data = PyUnicode_1BYTE_DATA(self); |
| 11979 | return PyUnicode_FromKindAndData(kind, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 11980 | data + kind * start, |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 11981 | length); |
| 11982 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11983 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11984 | |
| 11985 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11986 | do_strip(PyObject *self, int striptype) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11987 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11988 | int kind; |
| 11989 | void *data; |
| 11990 | Py_ssize_t len, i, j; |
| 11991 | |
| 11992 | if (PyUnicode_READY(self) == -1) |
| 11993 | return NULL; |
| 11994 | |
| 11995 | kind = PyUnicode_KIND(self); |
| 11996 | data = PyUnicode_DATA(self); |
| 11997 | len = PyUnicode_GET_LENGTH(self); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11998 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11999 | i = 0; |
| 12000 | if (striptype != RIGHTSTRIP) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12001 | while (i < len && Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, i))) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12002 | i++; |
| 12003 | } |
| 12004 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12005 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12006 | j = len; |
| 12007 | if (striptype != LEFTSTRIP) { |
| 12008 | do { |
| 12009 | j--; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12010 | } while (j >= i && Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, j))); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12011 | j++; |
| 12012 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12013 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12014 | return PyUnicode_Substring(self, i, j); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12015 | } |
| 12016 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12017 | |
| 12018 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12019 | do_argstrip(PyObject *self, int striptype, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12020 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12021 | PyObject *sep = NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12022 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12023 | if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) |
| 12024 | return NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12025 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12026 | if (sep != NULL && sep != Py_None) { |
| 12027 | if (PyUnicode_Check(sep)) |
| 12028 | return _PyUnicode_XStrip(self, striptype, sep); |
| 12029 | else { |
| 12030 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12031 | "%s arg must be None or str", |
| 12032 | STRIPNAME(striptype)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12033 | return NULL; |
| 12034 | } |
| 12035 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12036 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12037 | return do_strip(self, striptype); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12038 | } |
| 12039 | |
| 12040 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12041 | PyDoc_STRVAR(strip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12042 | "S.strip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12043 | \n\ |
| 12044 | Return a copy of the string S with leading and trailing\n\ |
| 12045 | whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12046 | 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] | 12047 | |
| 12048 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12049 | unicode_strip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12050 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12051 | if (PyTuple_GET_SIZE(args) == 0) |
| 12052 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 12053 | else |
| 12054 | return do_argstrip(self, BOTHSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12055 | } |
| 12056 | |
| 12057 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12058 | PyDoc_STRVAR(lstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12059 | "S.lstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12060 | \n\ |
| 12061 | Return a copy of the string S with leading whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12062 | 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] | 12063 | |
| 12064 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12065 | unicode_lstrip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12066 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12067 | if (PyTuple_GET_SIZE(args) == 0) |
| 12068 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 12069 | else |
| 12070 | return do_argstrip(self, LEFTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12071 | } |
| 12072 | |
| 12073 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12074 | PyDoc_STRVAR(rstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12075 | "S.rstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12076 | \n\ |
| 12077 | Return a copy of the string S with trailing whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12078 | 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] | 12079 | |
| 12080 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12081 | unicode_rstrip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12082 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12083 | if (PyTuple_GET_SIZE(args) == 0) |
| 12084 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 12085 | else |
| 12086 | return do_argstrip(self, RIGHTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12087 | } |
| 12088 | |
| 12089 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12090 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12091 | unicode_repeat(PyObject *str, Py_ssize_t len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12092 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12093 | PyObject *u; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12094 | Py_ssize_t nchars, n; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12095 | |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 12096 | if (len < 1) { |
| 12097 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 12098 | return unicode_empty; |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 12099 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12100 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12101 | /* no repeat, return original string */ |
| 12102 | if (len == 1) |
| 12103 | return unicode_result_unchanged(str); |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 12104 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 12105 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12106 | return NULL; |
| 12107 | |
Victor Stinner | c759f3e | 2011-10-01 03:09:58 +0200 | [diff] [blame] | 12108 | if (PyUnicode_GET_LENGTH(str) > PY_SSIZE_T_MAX / len) { |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 12109 | PyErr_SetString(PyExc_OverflowError, |
| 12110 | "repeated string is too long"); |
| 12111 | return NULL; |
| 12112 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12113 | nchars = len * PyUnicode_GET_LENGTH(str); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 12114 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12115 | u = PyUnicode_New(nchars, PyUnicode_MAX_CHAR_VALUE(str)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12116 | if (!u) |
| 12117 | return NULL; |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 12118 | assert(PyUnicode_KIND(u) == PyUnicode_KIND(str)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12119 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12120 | if (PyUnicode_GET_LENGTH(str) == 1) { |
| 12121 | const int kind = PyUnicode_KIND(str); |
| 12122 | const Py_UCS4 fill_char = PyUnicode_READ(kind, PyUnicode_DATA(str), 0); |
Victor Stinner | 73f53b5 | 2011-12-18 03:26:31 +0100 | [diff] [blame] | 12123 | if (kind == PyUnicode_1BYTE_KIND) { |
| 12124 | void *to = PyUnicode_DATA(u); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 12125 | memset(to, (unsigned char)fill_char, len); |
Victor Stinner | 73f53b5 | 2011-12-18 03:26:31 +0100 | [diff] [blame] | 12126 | } |
| 12127 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 12128 | Py_UCS2 *ucs2 = PyUnicode_2BYTE_DATA(u); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 12129 | for (n = 0; n < len; ++n) |
Victor Stinner | 73f53b5 | 2011-12-18 03:26:31 +0100 | [diff] [blame] | 12130 | ucs2[n] = fill_char; |
| 12131 | } else { |
| 12132 | Py_UCS4 *ucs4 = PyUnicode_4BYTE_DATA(u); |
| 12133 | assert(kind == PyUnicode_4BYTE_KIND); |
| 12134 | for (n = 0; n < len; ++n) |
| 12135 | ucs4[n] = fill_char; |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 12136 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12137 | } |
| 12138 | else { |
| 12139 | /* number of characters copied this far */ |
| 12140 | Py_ssize_t done = PyUnicode_GET_LENGTH(str); |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 12141 | const Py_ssize_t char_size = PyUnicode_KIND(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12142 | char *to = (char *) PyUnicode_DATA(u); |
| 12143 | Py_MEMCPY(to, PyUnicode_DATA(str), |
| 12144 | PyUnicode_GET_LENGTH(str) * char_size); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12145 | while (done < nchars) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12146 | n = (done <= nchars-done) ? done : nchars-done; |
| 12147 | Py_MEMCPY(to + (done * char_size), to, n * char_size); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12148 | done += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12149 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12150 | } |
| 12151 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12152 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12153 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12154 | } |
| 12155 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12156 | PyObject * |
| 12157 | PyUnicode_Replace(PyObject *obj, |
| 12158 | PyObject *subobj, |
| 12159 | PyObject *replobj, |
| 12160 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12161 | { |
| 12162 | PyObject *self; |
| 12163 | PyObject *str1; |
| 12164 | PyObject *str2; |
| 12165 | PyObject *result; |
| 12166 | |
| 12167 | self = PyUnicode_FromObject(obj); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12168 | if (self == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12169 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12170 | str1 = PyUnicode_FromObject(subobj); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12171 | if (str1 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12172 | Py_DECREF(self); |
| 12173 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12174 | } |
| 12175 | str2 = PyUnicode_FromObject(replobj); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12176 | if (str2 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12177 | Py_DECREF(self); |
| 12178 | Py_DECREF(str1); |
| 12179 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12180 | } |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12181 | if (PyUnicode_READY(self) == -1 || |
| 12182 | PyUnicode_READY(str1) == -1 || |
| 12183 | PyUnicode_READY(str2) == -1) |
| 12184 | result = NULL; |
| 12185 | else |
| 12186 | result = replace(self, str1, str2, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12187 | Py_DECREF(self); |
| 12188 | Py_DECREF(str1); |
| 12189 | Py_DECREF(str2); |
| 12190 | return result; |
| 12191 | } |
| 12192 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12193 | PyDoc_STRVAR(replace__doc__, |
Ezio Melotti | c1897e7 | 2010-06-26 18:50:39 +0000 | [diff] [blame] | 12194 | "S.replace(old, new[, count]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12195 | \n\ |
| 12196 | Return a copy of S with all occurrences of substring\n\ |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 12197 | old replaced by new. If the optional argument count is\n\ |
| 12198 | given, only the first count occurrences are replaced."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12199 | |
| 12200 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12201 | unicode_replace(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12202 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12203 | PyObject *str1; |
| 12204 | PyObject *str2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12205 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12206 | PyObject *result; |
| 12207 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12208 | if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12209 | return NULL; |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12210 | if (PyUnicode_READY(self) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12211 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12212 | str1 = PyUnicode_FromObject(str1); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12213 | if (str1 == NULL) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12214 | return NULL; |
| 12215 | str2 = PyUnicode_FromObject(str2); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12216 | if (str2 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12217 | Py_DECREF(str1); |
| 12218 | return NULL; |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 12219 | } |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12220 | if (PyUnicode_READY(str1) == -1 || PyUnicode_READY(str2) == -1) |
| 12221 | result = NULL; |
| 12222 | else |
| 12223 | result = replace(self, str1, str2, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12224 | |
| 12225 | Py_DECREF(str1); |
| 12226 | Py_DECREF(str2); |
| 12227 | return result; |
| 12228 | } |
| 12229 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12230 | static PyObject * |
| 12231 | unicode_repr(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12232 | { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12233 | PyObject *repr; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12234 | Py_ssize_t isize; |
| 12235 | Py_ssize_t osize, squote, dquote, i, o; |
| 12236 | Py_UCS4 max, quote; |
| 12237 | int ikind, okind; |
| 12238 | void *idata, *odata; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12239 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12240 | if (PyUnicode_READY(unicode) == -1) |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12241 | return NULL; |
| 12242 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12243 | isize = PyUnicode_GET_LENGTH(unicode); |
| 12244 | idata = PyUnicode_DATA(unicode); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12245 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12246 | /* Compute length of output, quote characters, and |
| 12247 | maximum character */ |
| 12248 | osize = 2; /* quotes */ |
| 12249 | max = 127; |
| 12250 | squote = dquote = 0; |
| 12251 | ikind = PyUnicode_KIND(unicode); |
| 12252 | for (i = 0; i < isize; i++) { |
| 12253 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
| 12254 | switch (ch) { |
| 12255 | case '\'': squote++; osize++; break; |
| 12256 | case '"': dquote++; osize++; break; |
| 12257 | case '\\': case '\t': case '\r': case '\n': |
| 12258 | osize += 2; break; |
| 12259 | default: |
| 12260 | /* Fast-path ASCII */ |
| 12261 | if (ch < ' ' || ch == 0x7f) |
| 12262 | osize += 4; /* \xHH */ |
| 12263 | else if (ch < 0x7f) |
| 12264 | osize++; |
| 12265 | else if (Py_UNICODE_ISPRINTABLE(ch)) { |
| 12266 | osize++; |
| 12267 | max = ch > max ? ch : max; |
| 12268 | } |
| 12269 | else if (ch < 0x100) |
| 12270 | osize += 4; /* \xHH */ |
| 12271 | else if (ch < 0x10000) |
| 12272 | osize += 6; /* \uHHHH */ |
| 12273 | else |
| 12274 | osize += 10; /* \uHHHHHHHH */ |
| 12275 | } |
| 12276 | } |
| 12277 | |
| 12278 | quote = '\''; |
| 12279 | if (squote) { |
| 12280 | if (dquote) |
| 12281 | /* Both squote and dquote present. Use squote, |
| 12282 | and escape them */ |
| 12283 | osize += squote; |
| 12284 | else |
| 12285 | quote = '"'; |
| 12286 | } |
| 12287 | |
| 12288 | repr = PyUnicode_New(osize, max); |
| 12289 | if (repr == NULL) |
| 12290 | return NULL; |
| 12291 | okind = PyUnicode_KIND(repr); |
| 12292 | odata = PyUnicode_DATA(repr); |
| 12293 | |
| 12294 | PyUnicode_WRITE(okind, odata, 0, quote); |
| 12295 | PyUnicode_WRITE(okind, odata, osize-1, quote); |
| 12296 | |
| 12297 | for (i = 0, o = 1; i < isize; i++) { |
| 12298 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12299 | |
| 12300 | /* Escape quotes and backslashes */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12301 | if ((ch == quote) || (ch == '\\')) { |
| 12302 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12303 | PyUnicode_WRITE(okind, odata, o++, ch); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12304 | continue; |
| 12305 | } |
| 12306 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12307 | /* Map special whitespace to '\t', \n', '\r' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12308 | if (ch == '\t') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12309 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12310 | PyUnicode_WRITE(okind, odata, o++, 't'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12311 | } |
| 12312 | else if (ch == '\n') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12313 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12314 | PyUnicode_WRITE(okind, odata, o++, 'n'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12315 | } |
| 12316 | else if (ch == '\r') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12317 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12318 | PyUnicode_WRITE(okind, odata, o++, 'r'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12319 | } |
| 12320 | |
| 12321 | /* Map non-printable US ASCII to '\xhh' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12322 | else if (ch < ' ' || ch == 0x7F) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12323 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12324 | PyUnicode_WRITE(okind, odata, o++, 'x'); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 12325 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0x000F]); |
| 12326 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0x000F]); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12327 | } |
| 12328 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12329 | /* Copy ASCII characters as-is */ |
| 12330 | else if (ch < 0x7F) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12331 | PyUnicode_WRITE(okind, odata, o++, ch); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12332 | } |
| 12333 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12334 | /* Non-ASCII characters */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12335 | else { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12336 | /* Map Unicode whitespace and control characters |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12337 | (categories Z* and C* except ASCII space) |
| 12338 | */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12339 | if (!Py_UNICODE_ISPRINTABLE(ch)) { |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12340 | /* Map 8-bit characters to '\xhh' */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12341 | if (ch <= 0xff) { |
| 12342 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12343 | PyUnicode_WRITE(okind, odata, o++, 'x'); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 12344 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0x000F]); |
| 12345 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0x000F]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12346 | } |
| 12347 | /* Map 21-bit characters to '\U00xxxxxx' */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12348 | else if (ch >= 0x10000) { |
| 12349 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12350 | PyUnicode_WRITE(okind, odata, o++, 'U'); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 12351 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 28) & 0xF]); |
| 12352 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 24) & 0xF]); |
| 12353 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 20) & 0xF]); |
| 12354 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 16) & 0xF]); |
| 12355 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 12) & 0xF]); |
| 12356 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 8) & 0xF]); |
| 12357 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0xF]); |
| 12358 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0xF]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12359 | } |
| 12360 | /* Map 16-bit characters to '\uxxxx' */ |
| 12361 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12362 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12363 | PyUnicode_WRITE(okind, odata, o++, 'u'); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 12364 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 12) & 0xF]); |
| 12365 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 8) & 0xF]); |
| 12366 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0xF]); |
| 12367 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0xF]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12368 | } |
| 12369 | } |
| 12370 | /* Copy characters as-is */ |
| 12371 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12372 | PyUnicode_WRITE(okind, odata, o++, ch); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12373 | } |
| 12374 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12375 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12376 | /* Closing quote already added at the beginning */ |
Victor Stinner | 05d1189 | 2011-10-06 01:13:58 +0200 | [diff] [blame] | 12377 | assert(_PyUnicode_CheckConsistency(repr, 1)); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12378 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12379 | } |
| 12380 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12381 | PyDoc_STRVAR(rfind__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12382 | "S.rfind(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12383 | \n\ |
| 12384 | Return the highest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 12385 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12386 | arguments start and end are interpreted as in slice notation.\n\ |
| 12387 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12388 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12389 | |
| 12390 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12391 | unicode_rfind(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12392 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12393 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 12394 | Py_ssize_t start; |
| 12395 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12396 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12397 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12398 | if (!stringlib_parse_args_finds_unicode("rfind", args, &substring, |
| 12399 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12400 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12401 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12402 | if (PyUnicode_READY(self) == -1) |
| 12403 | return NULL; |
| 12404 | if (PyUnicode_READY(substring) == -1) |
| 12405 | return NULL; |
| 12406 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12407 | result = any_find_slice(-1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12408 | |
| 12409 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12410 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12411 | if (result == -2) |
| 12412 | return NULL; |
| 12413 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12414 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12415 | } |
| 12416 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12417 | PyDoc_STRVAR(rindex__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12418 | "S.rindex(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12419 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12420 | 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] | 12421 | |
| 12422 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12423 | unicode_rindex(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12424 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12425 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 12426 | Py_ssize_t start; |
| 12427 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12428 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12429 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12430 | if (!stringlib_parse_args_finds_unicode("rindex", args, &substring, |
| 12431 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12432 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12433 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12434 | if (PyUnicode_READY(self) == -1) |
| 12435 | return NULL; |
| 12436 | if (PyUnicode_READY(substring) == -1) |
| 12437 | return NULL; |
| 12438 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12439 | result = any_find_slice(-1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12440 | |
| 12441 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12442 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12443 | if (result == -2) |
| 12444 | return NULL; |
| 12445 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12446 | if (result < 0) { |
| 12447 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 12448 | return NULL; |
| 12449 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12450 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12451 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12452 | } |
| 12453 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12454 | PyDoc_STRVAR(rjust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12455 | "S.rjust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12456 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 12457 | 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] | 12458 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12459 | |
| 12460 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12461 | unicode_rjust(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12462 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12463 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12464 | Py_UCS4 fillchar = ' '; |
| 12465 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 12466 | 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] | 12467 | return NULL; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 12468 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 12469 | if (PyUnicode_READY(self) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12470 | return NULL; |
| 12471 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12472 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 12473 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12474 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12475 | return pad(self, width - PyUnicode_GET_LENGTH(self), 0, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12476 | } |
| 12477 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12478 | PyObject * |
| 12479 | PyUnicode_Split(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12480 | { |
| 12481 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 12482 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12483 | s = PyUnicode_FromObject(s); |
| 12484 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12485 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12486 | if (sep != NULL) { |
| 12487 | sep = PyUnicode_FromObject(sep); |
| 12488 | if (sep == NULL) { |
| 12489 | Py_DECREF(s); |
| 12490 | return NULL; |
| 12491 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12492 | } |
| 12493 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12494 | result = split(s, sep, maxsplit); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12495 | |
| 12496 | Py_DECREF(s); |
| 12497 | Py_XDECREF(sep); |
| 12498 | return result; |
| 12499 | } |
| 12500 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12501 | PyDoc_STRVAR(split__doc__, |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12502 | "S.split(sep=None, maxsplit=-1) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12503 | \n\ |
| 12504 | Return a list of the words in S, using sep as the\n\ |
| 12505 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 12506 | 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] | 12507 | whitespace string is a separator and empty strings are\n\ |
| 12508 | removed from the result."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12509 | |
| 12510 | static PyObject* |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12511 | unicode_split(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12512 | { |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12513 | static char *kwlist[] = {"sep", "maxsplit", 0}; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12514 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12515 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12516 | |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12517 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|On:split", |
| 12518 | kwlist, &substring, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12519 | return NULL; |
| 12520 | |
| 12521 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12522 | return split(self, NULL, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12523 | else if (PyUnicode_Check(substring)) |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12524 | return split(self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12525 | else |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12526 | return PyUnicode_Split(self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12527 | } |
| 12528 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12529 | PyObject * |
| 12530 | PyUnicode_Partition(PyObject *str_in, PyObject *sep_in) |
| 12531 | { |
| 12532 | PyObject* str_obj; |
| 12533 | PyObject* sep_obj; |
| 12534 | PyObject* out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12535 | int kind1, kind2, kind; |
| 12536 | void *buf1 = NULL, *buf2 = NULL; |
| 12537 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12538 | |
| 12539 | str_obj = PyUnicode_FromObject(str_in); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12540 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12541 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12542 | sep_obj = PyUnicode_FromObject(sep_in); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12543 | if (!sep_obj) { |
| 12544 | Py_DECREF(str_obj); |
| 12545 | return NULL; |
| 12546 | } |
| 12547 | if (PyUnicode_READY(sep_obj) == -1 || PyUnicode_READY(str_obj) == -1) { |
| 12548 | Py_DECREF(sep_obj); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12549 | Py_DECREF(str_obj); |
| 12550 | return NULL; |
| 12551 | } |
| 12552 | |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12553 | kind1 = PyUnicode_KIND(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12554 | kind2 = PyUnicode_KIND(sep_obj); |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12555 | kind = Py_MAX(kind1, kind2); |
| 12556 | buf1 = PyUnicode_DATA(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12557 | if (kind1 != kind) |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12558 | buf1 = _PyUnicode_AsKind(str_obj, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12559 | if (!buf1) |
| 12560 | goto onError; |
| 12561 | buf2 = PyUnicode_DATA(sep_obj); |
| 12562 | if (kind2 != kind) |
| 12563 | buf2 = _PyUnicode_AsKind(sep_obj, kind); |
| 12564 | if (!buf2) |
| 12565 | goto onError; |
| 12566 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 12567 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
| 12568 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 12569 | switch (PyUnicode_KIND(str_obj)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12570 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 12571 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sep_obj)) |
| 12572 | out = asciilib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12573 | else |
| 12574 | 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] | 12575 | break; |
| 12576 | case PyUnicode_2BYTE_KIND: |
| 12577 | out = ucs2lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12578 | break; |
| 12579 | case PyUnicode_4BYTE_KIND: |
| 12580 | out = ucs4lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12581 | break; |
| 12582 | default: |
| 12583 | assert(0); |
| 12584 | out = 0; |
| 12585 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12586 | |
| 12587 | Py_DECREF(sep_obj); |
| 12588 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12589 | if (kind1 != kind) |
| 12590 | PyMem_Free(buf1); |
| 12591 | if (kind2 != kind) |
| 12592 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12593 | |
| 12594 | return out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12595 | onError: |
| 12596 | Py_DECREF(sep_obj); |
| 12597 | Py_DECREF(str_obj); |
| 12598 | if (kind1 != kind && buf1) |
| 12599 | PyMem_Free(buf1); |
| 12600 | if (kind2 != kind && buf2) |
| 12601 | PyMem_Free(buf2); |
| 12602 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12603 | } |
| 12604 | |
| 12605 | |
| 12606 | PyObject * |
| 12607 | PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in) |
| 12608 | { |
| 12609 | PyObject* str_obj; |
| 12610 | PyObject* sep_obj; |
| 12611 | PyObject* out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12612 | int kind1, kind2, kind; |
| 12613 | void *buf1 = NULL, *buf2 = NULL; |
| 12614 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12615 | |
| 12616 | str_obj = PyUnicode_FromObject(str_in); |
| 12617 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12618 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12619 | sep_obj = PyUnicode_FromObject(sep_in); |
| 12620 | if (!sep_obj) { |
| 12621 | Py_DECREF(str_obj); |
| 12622 | return NULL; |
| 12623 | } |
| 12624 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12625 | kind1 = PyUnicode_KIND(str_in); |
| 12626 | kind2 = PyUnicode_KIND(sep_obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 12627 | kind = Py_MAX(kind1, kind2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12628 | buf1 = PyUnicode_DATA(str_in); |
| 12629 | if (kind1 != kind) |
| 12630 | buf1 = _PyUnicode_AsKind(str_in, kind); |
| 12631 | if (!buf1) |
| 12632 | goto onError; |
| 12633 | buf2 = PyUnicode_DATA(sep_obj); |
| 12634 | if (kind2 != kind) |
| 12635 | buf2 = _PyUnicode_AsKind(sep_obj, kind); |
| 12636 | if (!buf2) |
| 12637 | goto onError; |
| 12638 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 12639 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
| 12640 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 12641 | switch (PyUnicode_KIND(str_in)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12642 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 12643 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sep_obj)) |
| 12644 | out = asciilib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12645 | else |
| 12646 | 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] | 12647 | break; |
| 12648 | case PyUnicode_2BYTE_KIND: |
| 12649 | out = ucs2lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12650 | break; |
| 12651 | case PyUnicode_4BYTE_KIND: |
| 12652 | out = ucs4lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12653 | break; |
| 12654 | default: |
| 12655 | assert(0); |
| 12656 | out = 0; |
| 12657 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12658 | |
| 12659 | Py_DECREF(sep_obj); |
| 12660 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12661 | if (kind1 != kind) |
| 12662 | PyMem_Free(buf1); |
| 12663 | if (kind2 != kind) |
| 12664 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12665 | |
| 12666 | return out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12667 | onError: |
| 12668 | Py_DECREF(sep_obj); |
| 12669 | Py_DECREF(str_obj); |
| 12670 | if (kind1 != kind && buf1) |
| 12671 | PyMem_Free(buf1); |
| 12672 | if (kind2 != kind && buf2) |
| 12673 | PyMem_Free(buf2); |
| 12674 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12675 | } |
| 12676 | |
| 12677 | PyDoc_STRVAR(partition__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12678 | "S.partition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12679 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 12680 | 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] | 12681 | 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] | 12682 | found, return S and two empty strings."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12683 | |
| 12684 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12685 | unicode_partition(PyObject *self, PyObject *separator) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12686 | { |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12687 | return PyUnicode_Partition(self, separator); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12688 | } |
| 12689 | |
| 12690 | PyDoc_STRVAR(rpartition__doc__, |
Ezio Melotti | 5b2b242 | 2010-01-25 11:58:28 +0000 | [diff] [blame] | 12691 | "S.rpartition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12692 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 12693 | 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] | 12694 | 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] | 12695 | separator is not found, return two empty strings and S."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12696 | |
| 12697 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12698 | unicode_rpartition(PyObject *self, PyObject *separator) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12699 | { |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12700 | return PyUnicode_RPartition(self, separator); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12701 | } |
| 12702 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12703 | PyObject * |
| 12704 | PyUnicode_RSplit(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12705 | { |
| 12706 | PyObject *result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12707 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12708 | s = PyUnicode_FromObject(s); |
| 12709 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12710 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12711 | if (sep != NULL) { |
| 12712 | sep = PyUnicode_FromObject(sep); |
| 12713 | if (sep == NULL) { |
| 12714 | Py_DECREF(s); |
| 12715 | return NULL; |
| 12716 | } |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12717 | } |
| 12718 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12719 | result = rsplit(s, sep, maxsplit); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12720 | |
| 12721 | Py_DECREF(s); |
| 12722 | Py_XDECREF(sep); |
| 12723 | return result; |
| 12724 | } |
| 12725 | |
| 12726 | PyDoc_STRVAR(rsplit__doc__, |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12727 | "S.rsplit(sep=None, maxsplit=-1) -> list of strings\n\ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12728 | \n\ |
| 12729 | Return a list of the words in S, using sep as the\n\ |
| 12730 | delimiter string, starting at the end of the string and\n\ |
| 12731 | working to the front. If maxsplit is given, at most maxsplit\n\ |
| 12732 | splits are done. If sep is not specified, any whitespace string\n\ |
| 12733 | is a separator."); |
| 12734 | |
| 12735 | static PyObject* |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12736 | unicode_rsplit(PyObject *self, PyObject *args, PyObject *kwds) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12737 | { |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12738 | static char *kwlist[] = {"sep", "maxsplit", 0}; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12739 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12740 | Py_ssize_t maxcount = -1; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12741 | |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12742 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|On:rsplit", |
| 12743 | kwlist, &substring, &maxcount)) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12744 | return NULL; |
| 12745 | |
| 12746 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12747 | return rsplit(self, NULL, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12748 | else if (PyUnicode_Check(substring)) |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12749 | return rsplit(self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12750 | else |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12751 | return PyUnicode_RSplit(self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12752 | } |
| 12753 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12754 | PyDoc_STRVAR(splitlines__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12755 | "S.splitlines([keepends]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12756 | \n\ |
| 12757 | 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] | 12758 | 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] | 12759 | is given and true."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12760 | |
| 12761 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12762 | unicode_splitlines(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12763 | { |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12764 | static char *kwlist[] = {"keepends", 0}; |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 12765 | int keepends = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12766 | |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12767 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|i:splitlines", |
| 12768 | kwlist, &keepends)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12769 | return NULL; |
| 12770 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12771 | return PyUnicode_Splitlines(self, keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12772 | } |
| 12773 | |
| 12774 | static |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 12775 | PyObject *unicode_str(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12776 | { |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12777 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12778 | } |
| 12779 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12780 | PyDoc_STRVAR(swapcase__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12781 | "S.swapcase() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12782 | \n\ |
| 12783 | 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] | 12784 | and vice versa."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12785 | |
| 12786 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12787 | unicode_swapcase(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12788 | { |
Benjamin Peterson | eea4846 | 2012-01-16 14:28:50 -0500 | [diff] [blame] | 12789 | if (PyUnicode_READY(self) == -1) |
| 12790 | return NULL; |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 12791 | return case_operation(self, do_swapcase); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12792 | } |
| 12793 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12794 | PyDoc_STRVAR(maketrans__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12795 | "str.maketrans(x[, y[, z]]) -> dict (static method)\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12796 | \n\ |
| 12797 | Return a translation table usable for str.translate().\n\ |
| 12798 | If there is only one argument, it must be a dictionary mapping Unicode\n\ |
| 12799 | ordinals (integers) or characters to Unicode ordinals, strings or None.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12800 | Character keys will be then converted to ordinals.\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12801 | If there are two arguments, they must be strings of equal length, and\n\ |
| 12802 | in the resulting dictionary, each character in x will be mapped to the\n\ |
| 12803 | character at the same position in y. If there is a third argument, it\n\ |
| 12804 | must be a string, whose characters will be mapped to None in the result."); |
| 12805 | |
| 12806 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12807 | unicode_maketrans(PyObject *null, PyObject *args) |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12808 | { |
| 12809 | PyObject *x, *y = NULL, *z = NULL; |
| 12810 | PyObject *new = NULL, *key, *value; |
| 12811 | Py_ssize_t i = 0; |
| 12812 | int res; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12813 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12814 | if (!PyArg_ParseTuple(args, "O|UU:maketrans", &x, &y, &z)) |
| 12815 | return NULL; |
| 12816 | new = PyDict_New(); |
| 12817 | if (!new) |
| 12818 | return NULL; |
| 12819 | if (y != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12820 | int x_kind, y_kind, z_kind; |
| 12821 | void *x_data, *y_data, *z_data; |
| 12822 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12823 | /* x must be a string too, of equal length */ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12824 | if (!PyUnicode_Check(x)) { |
| 12825 | PyErr_SetString(PyExc_TypeError, "first maketrans argument must " |
| 12826 | "be a string if there is a second argument"); |
| 12827 | goto err; |
| 12828 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12829 | if (PyUnicode_GET_LENGTH(x) != PyUnicode_GET_LENGTH(y)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12830 | PyErr_SetString(PyExc_ValueError, "the first two maketrans " |
| 12831 | "arguments must have equal length"); |
| 12832 | goto err; |
| 12833 | } |
| 12834 | /* 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] | 12835 | x_kind = PyUnicode_KIND(x); |
| 12836 | y_kind = PyUnicode_KIND(y); |
| 12837 | x_data = PyUnicode_DATA(x); |
| 12838 | y_data = PyUnicode_DATA(y); |
| 12839 | for (i = 0; i < PyUnicode_GET_LENGTH(x); i++) { |
| 12840 | key = PyLong_FromLong(PyUnicode_READ(x_kind, x_data, i)); |
Benjamin Peterson | 53aa1d7 | 2011-12-20 13:29:45 -0600 | [diff] [blame] | 12841 | if (!key) |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12842 | goto err; |
Benjamin Peterson | 822c790 | 2011-12-20 13:32:50 -0600 | [diff] [blame] | 12843 | value = PyLong_FromLong(PyUnicode_READ(y_kind, y_data, i)); |
Benjamin Peterson | 53aa1d7 | 2011-12-20 13:29:45 -0600 | [diff] [blame] | 12844 | if (!value) { |
| 12845 | Py_DECREF(key); |
| 12846 | goto err; |
| 12847 | } |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12848 | res = PyDict_SetItem(new, key, value); |
| 12849 | Py_DECREF(key); |
| 12850 | Py_DECREF(value); |
| 12851 | if (res < 0) |
| 12852 | goto err; |
| 12853 | } |
| 12854 | /* create entries for deleting chars in z */ |
| 12855 | if (z != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12856 | z_kind = PyUnicode_KIND(z); |
| 12857 | z_data = PyUnicode_DATA(z); |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 12858 | for (i = 0; i < PyUnicode_GET_LENGTH(z); i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12859 | key = PyLong_FromLong(PyUnicode_READ(z_kind, z_data, i)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12860 | if (!key) |
| 12861 | goto err; |
| 12862 | res = PyDict_SetItem(new, key, Py_None); |
| 12863 | Py_DECREF(key); |
| 12864 | if (res < 0) |
| 12865 | goto err; |
| 12866 | } |
| 12867 | } |
| 12868 | } else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12869 | int kind; |
| 12870 | void *data; |
| 12871 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12872 | /* x must be a dict */ |
Raymond Hettinger | 3ad0576 | 2009-05-29 22:11:22 +0000 | [diff] [blame] | 12873 | if (!PyDict_CheckExact(x)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12874 | PyErr_SetString(PyExc_TypeError, "if you give only one argument " |
| 12875 | "to maketrans it must be a dict"); |
| 12876 | goto err; |
| 12877 | } |
| 12878 | /* copy entries into the new dict, converting string keys to int keys */ |
| 12879 | while (PyDict_Next(x, &i, &key, &value)) { |
| 12880 | if (PyUnicode_Check(key)) { |
| 12881 | /* convert string keys to integer keys */ |
| 12882 | PyObject *newkey; |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 12883 | if (PyUnicode_GET_LENGTH(key) != 1) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12884 | PyErr_SetString(PyExc_ValueError, "string keys in translate " |
| 12885 | "table must be of length 1"); |
| 12886 | goto err; |
| 12887 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12888 | kind = PyUnicode_KIND(key); |
| 12889 | data = PyUnicode_DATA(key); |
| 12890 | newkey = PyLong_FromLong(PyUnicode_READ(kind, data, 0)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12891 | if (!newkey) |
| 12892 | goto err; |
| 12893 | res = PyDict_SetItem(new, newkey, value); |
| 12894 | Py_DECREF(newkey); |
| 12895 | if (res < 0) |
| 12896 | goto err; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12897 | } else if (PyLong_Check(key)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12898 | /* just keep integer keys */ |
| 12899 | if (PyDict_SetItem(new, key, value) < 0) |
| 12900 | goto err; |
| 12901 | } else { |
| 12902 | PyErr_SetString(PyExc_TypeError, "keys in translate table must " |
| 12903 | "be strings or integers"); |
| 12904 | goto err; |
| 12905 | } |
| 12906 | } |
| 12907 | } |
| 12908 | return new; |
| 12909 | err: |
| 12910 | Py_DECREF(new); |
| 12911 | return NULL; |
| 12912 | } |
| 12913 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12914 | PyDoc_STRVAR(translate__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12915 | "S.translate(table) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12916 | \n\ |
| 12917 | Return a copy of the string S, where all characters have been mapped\n\ |
| 12918 | through the given translation table, which must be a mapping of\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12919 | Unicode ordinals to Unicode ordinals, strings, or None.\n\ |
Walter Dörwald | 5c1ee17 | 2002-09-04 20:31:32 +0000 | [diff] [blame] | 12920 | Unmapped characters are left untouched. Characters mapped to None\n\ |
| 12921 | are deleted."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12922 | |
| 12923 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12924 | unicode_translate(PyObject *self, PyObject *table) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12925 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12926 | return _PyUnicode_TranslateCharmap(self, table, "ignore"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12927 | } |
| 12928 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12929 | PyDoc_STRVAR(upper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12930 | "S.upper() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12931 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12932 | Return a copy of S converted to uppercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12933 | |
| 12934 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12935 | unicode_upper(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12936 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 12937 | if (PyUnicode_READY(self) == -1) |
| 12938 | return NULL; |
| 12939 | if (PyUnicode_IS_ASCII(self)) |
| 12940 | return ascii_upper_or_lower(self, 0); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 12941 | return case_operation(self, do_upper); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12942 | } |
| 12943 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12944 | PyDoc_STRVAR(zfill__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12945 | "S.zfill(width) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12946 | \n\ |
Benjamin Peterson | 9aa4299 | 2008-09-10 21:57:34 +0000 | [diff] [blame] | 12947 | Pad a numeric string S with zeros on the left, to fill a field\n\ |
| 12948 | of the specified width. The string S is never truncated."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12949 | |
| 12950 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12951 | unicode_zfill(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12952 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12953 | Py_ssize_t fill; |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12954 | PyObject *u; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12955 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12956 | int kind; |
| 12957 | void *data; |
| 12958 | Py_UCS4 chr; |
| 12959 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12960 | if (!PyArg_ParseTuple(args, "n:zfill", &width)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12961 | return NULL; |
| 12962 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 12963 | if (PyUnicode_READY(self) == -1) |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12964 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12965 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12966 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 12967 | return unicode_result_unchanged(self); |
| 12968 | |
| 12969 | fill = width - PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12970 | |
| 12971 | u = pad(self, fill, 0, '0'); |
| 12972 | |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 12973 | if (u == NULL) |
| 12974 | return NULL; |
| 12975 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12976 | kind = PyUnicode_KIND(u); |
| 12977 | data = PyUnicode_DATA(u); |
| 12978 | chr = PyUnicode_READ(kind, data, fill); |
| 12979 | |
| 12980 | if (chr == '+' || chr == '-') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12981 | /* move sign to beginning of string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12982 | PyUnicode_WRITE(kind, data, 0, chr); |
| 12983 | PyUnicode_WRITE(kind, data, fill, '0'); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12984 | } |
| 12985 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12986 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12987 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12988 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12989 | |
| 12990 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12991 | static PyObject * |
| 12992 | unicode__decimal2ascii(PyObject *self) |
| 12993 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12994 | return PyUnicode_TransformDecimalAndSpaceToASCII(self); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12995 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12996 | #endif |
| 12997 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12998 | PyDoc_STRVAR(startswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12999 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13000 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 13001 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 13002 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13003 | With optional end, stop comparing S at that position.\n\ |
| 13004 | prefix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13005 | |
| 13006 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13007 | unicode_startswith(PyObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13008 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13009 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13010 | PyObject *subobj; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13011 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13012 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 13013 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13014 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13015 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 13016 | if (!stringlib_parse_args_finds("startswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13017 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13018 | if (PyTuple_Check(subobj)) { |
| 13019 | Py_ssize_t i; |
| 13020 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13021 | substring = PyUnicode_FromObject(PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13022 | if (substring == NULL) |
| 13023 | return NULL; |
| 13024 | result = tailmatch(self, substring, start, end, -1); |
| 13025 | Py_DECREF(substring); |
| 13026 | if (result) { |
| 13027 | Py_RETURN_TRUE; |
| 13028 | } |
| 13029 | } |
| 13030 | /* nothing matched */ |
| 13031 | Py_RETURN_FALSE; |
| 13032 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13033 | substring = PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 13034 | if (substring == NULL) { |
| 13035 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 13036 | PyErr_Format(PyExc_TypeError, "startswith first arg must be str or " |
| 13037 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13038 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 13039 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13040 | result = tailmatch(self, substring, start, end, -1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13041 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13042 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13043 | } |
| 13044 | |
| 13045 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 13046 | PyDoc_STRVAR(endswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13047 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13048 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 13049 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 13050 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13051 | With optional end, stop comparing S at that position.\n\ |
| 13052 | suffix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13053 | |
| 13054 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13055 | unicode_endswith(PyObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13056 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13057 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13058 | PyObject *subobj; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13059 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13060 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 13061 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13062 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13063 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 13064 | if (!stringlib_parse_args_finds("endswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13065 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13066 | if (PyTuple_Check(subobj)) { |
| 13067 | Py_ssize_t i; |
| 13068 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13069 | substring = PyUnicode_FromObject( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13070 | PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13071 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13072 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13073 | result = tailmatch(self, substring, start, end, +1); |
| 13074 | Py_DECREF(substring); |
| 13075 | if (result) { |
| 13076 | Py_RETURN_TRUE; |
| 13077 | } |
| 13078 | } |
| 13079 | Py_RETURN_FALSE; |
| 13080 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13081 | substring = PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 13082 | if (substring == NULL) { |
| 13083 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 13084 | PyErr_Format(PyExc_TypeError, "endswith first arg must be str or " |
| 13085 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13086 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 13087 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13088 | result = tailmatch(self, substring, start, end, +1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13089 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13090 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13091 | } |
| 13092 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13093 | #include "stringlib/unicode_format.h" |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13094 | |
| 13095 | PyDoc_STRVAR(format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13096 | "S.format(*args, **kwargs) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13097 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 13098 | Return a formatted version of S, using substitutions from args and kwargs.\n\ |
| 13099 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13100 | |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 13101 | PyDoc_STRVAR(format_map__doc__, |
| 13102 | "S.format_map(mapping) -> str\n\ |
| 13103 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 13104 | Return a formatted version of S, using substitutions from mapping.\n\ |
| 13105 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 13106 | |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 13107 | static PyObject * |
| 13108 | unicode__format__(PyObject* self, PyObject* args) |
| 13109 | { |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13110 | PyObject *format_spec, *out; |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 13111 | |
| 13112 | if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) |
| 13113 | return NULL; |
| 13114 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13115 | out = _PyUnicode_FormatAdvanced(self, format_spec, 0, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13116 | PyUnicode_GET_LENGTH(format_spec)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13117 | return out; |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 13118 | } |
| 13119 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13120 | PyDoc_STRVAR(p_format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13121 | "S.__format__(format_spec) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13122 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 13123 | Return a formatted version of S as described by format_spec."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13124 | |
| 13125 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13126 | unicode__sizeof__(PyObject *v) |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13127 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13128 | Py_ssize_t size; |
| 13129 | |
| 13130 | /* If it's a compact object, account for base structure + |
| 13131 | character data. */ |
| 13132 | if (PyUnicode_IS_COMPACT_ASCII(v)) |
| 13133 | size = sizeof(PyASCIIObject) + PyUnicode_GET_LENGTH(v) + 1; |
| 13134 | else if (PyUnicode_IS_COMPACT(v)) |
| 13135 | size = sizeof(PyCompactUnicodeObject) + |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 13136 | (PyUnicode_GET_LENGTH(v) + 1) * PyUnicode_KIND(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13137 | else { |
| 13138 | /* If it is a two-block object, account for base object, and |
| 13139 | for character block if present. */ |
| 13140 | size = sizeof(PyUnicodeObject); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 13141 | if (_PyUnicode_DATA_ANY(v)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13142 | size += (PyUnicode_GET_LENGTH(v) + 1) * |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 13143 | PyUnicode_KIND(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13144 | } |
| 13145 | /* 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] | 13146 | with the data pointer. Check if the data is not shared. */ |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 13147 | if (_PyUnicode_HAS_WSTR_MEMORY(v)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13148 | size += (PyUnicode_WSTR_LENGTH(v) + 1) * sizeof(wchar_t); |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 13149 | if (_PyUnicode_HAS_UTF8_MEMORY(v)) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 13150 | size += PyUnicode_UTF8_LENGTH(v) + 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13151 | |
| 13152 | return PyLong_FromSsize_t(size); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13153 | } |
| 13154 | |
| 13155 | PyDoc_STRVAR(sizeof__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13156 | "S.__sizeof__() -> size of S in memory, in bytes"); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13157 | |
| 13158 | static PyObject * |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 13159 | unicode_getnewargs(PyObject *v) |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 13160 | { |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 13161 | PyObject *copy = _PyUnicode_Copy(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13162 | if (!copy) |
| 13163 | return NULL; |
| 13164 | return Py_BuildValue("(N)", copy); |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 13165 | } |
| 13166 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13167 | static PyMethodDef unicode_methods[] = { |
| 13168 | |
| 13169 | /* Order is according to common usage: often used methods should |
| 13170 | appear first, since lookup is done sequentially. */ |
| 13171 | |
Benjamin Peterson | 28a4dce | 2010-12-12 01:33:04 +0000 | [diff] [blame] | 13172 | {"encode", (PyCFunction) unicode_encode, METH_VARARGS | METH_KEYWORDS, encode__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13173 | {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 13174 | {"split", (PyCFunction) unicode_split, METH_VARARGS | METH_KEYWORDS, split__doc__}, |
| 13175 | {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS | METH_KEYWORDS, rsplit__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13176 | {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, |
| 13177 | {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 13178 | {"casefold", (PyCFunction) unicode_casefold, METH_NOARGS, casefold__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13179 | {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, |
| 13180 | {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, |
| 13181 | {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, |
| 13182 | {"expandtabs", (PyCFunction) unicode_expandtabs, METH_VARARGS, expandtabs__doc__}, |
| 13183 | {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13184 | {"partition", (PyCFunction) unicode_partition, METH_O, partition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13185 | {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, |
| 13186 | {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, |
| 13187 | {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 13188 | {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13189 | {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, |
| 13190 | {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, |
| 13191 | {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 13192 | {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13193 | {"rpartition", (PyCFunction) unicode_rpartition, METH_O, rpartition__doc__}, |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 13194 | {"splitlines", (PyCFunction) unicode_splitlines, METH_VARARGS | METH_KEYWORDS, splitlines__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 13195 | {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13196 | {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, |
| 13197 | {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, |
| 13198 | {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, |
| 13199 | {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, |
| 13200 | {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, |
| 13201 | {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, |
| 13202 | {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, |
| 13203 | {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, |
| 13204 | {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, |
| 13205 | {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, |
| 13206 | {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, |
| 13207 | {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, |
| 13208 | {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, |
| 13209 | {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 13210 | {"isidentifier", (PyCFunction) unicode_isidentifier, METH_NOARGS, isidentifier__doc__}, |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 13211 | {"isprintable", (PyCFunction) unicode_isprintable, METH_NOARGS, isprintable__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13212 | {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, |
Eric Smith | 9cd1e09 | 2007-08-31 18:39:38 +0000 | [diff] [blame] | 13213 | {"format", (PyCFunction) do_string_format, METH_VARARGS | METH_KEYWORDS, format__doc__}, |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 13214 | {"format_map", (PyCFunction) do_string_format_map, METH_O, format_map__doc__}, |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 13215 | {"__format__", (PyCFunction) unicode__format__, METH_VARARGS, p_format__doc__}, |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 13216 | {"maketrans", (PyCFunction) unicode_maketrans, |
| 13217 | METH_VARARGS | METH_STATIC, maketrans__doc__}, |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13218 | {"__sizeof__", (PyCFunction) unicode__sizeof__, METH_NOARGS, sizeof__doc__}, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 13219 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 13220 | /* These methods are just used for debugging the implementation. */ |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 13221 | {"_decimal2ascii", (PyCFunction) unicode__decimal2ascii, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13222 | #endif |
| 13223 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13224 | {"__getnewargs__", (PyCFunction)unicode_getnewargs, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13225 | {NULL, NULL} |
| 13226 | }; |
| 13227 | |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 13228 | static PyObject * |
| 13229 | unicode_mod(PyObject *v, PyObject *w) |
| 13230 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 13231 | if (!PyUnicode_Check(v)) |
| 13232 | Py_RETURN_NOTIMPLEMENTED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13233 | return PyUnicode_Format(v, w); |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 13234 | } |
| 13235 | |
| 13236 | static PyNumberMethods unicode_as_number = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13237 | 0, /*nb_add*/ |
| 13238 | 0, /*nb_subtract*/ |
| 13239 | 0, /*nb_multiply*/ |
| 13240 | unicode_mod, /*nb_remainder*/ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 13241 | }; |
| 13242 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13243 | static PySequenceMethods unicode_as_sequence = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13244 | (lenfunc) unicode_length, /* sq_length */ |
| 13245 | PyUnicode_Concat, /* sq_concat */ |
| 13246 | (ssizeargfunc) unicode_repeat, /* sq_repeat */ |
| 13247 | (ssizeargfunc) unicode_getitem, /* sq_item */ |
| 13248 | 0, /* sq_slice */ |
| 13249 | 0, /* sq_ass_item */ |
| 13250 | 0, /* sq_ass_slice */ |
| 13251 | PyUnicode_Contains, /* sq_contains */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13252 | }; |
| 13253 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13254 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13255 | unicode_subscript(PyObject* self, PyObject* item) |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13256 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13257 | if (PyUnicode_READY(self) == -1) |
| 13258 | return NULL; |
| 13259 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 13260 | if (PyIndex_Check(item)) { |
| 13261 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13262 | if (i == -1 && PyErr_Occurred()) |
| 13263 | return NULL; |
| 13264 | if (i < 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13265 | i += PyUnicode_GET_LENGTH(self); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13266 | return unicode_getitem(self, i); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13267 | } else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13268 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13269 | PyObject *result; |
| 13270 | void *src_data, *dest_data; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13271 | int src_kind, dest_kind; |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 13272 | Py_UCS4 ch, max_char, kind_limit; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13273 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13274 | if (PySlice_GetIndicesEx(item, PyUnicode_GET_LENGTH(self), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13275 | &start, &stop, &step, &slicelength) < 0) { |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13276 | return NULL; |
| 13277 | } |
| 13278 | |
| 13279 | if (slicelength <= 0) { |
Victor Stinner | 382955f | 2011-12-11 21:44:00 +0100 | [diff] [blame] | 13280 | Py_INCREF(unicode_empty); |
| 13281 | return unicode_empty; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13282 | } else if (start == 0 && step == 1 && |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 13283 | slicelength == PyUnicode_GET_LENGTH(self)) { |
| 13284 | return unicode_result_unchanged(self); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 13285 | } else if (step == 1) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13286 | return PyUnicode_Substring(self, |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 13287 | start, start + slicelength); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13288 | } |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13289 | /* General case */ |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13290 | src_kind = PyUnicode_KIND(self); |
| 13291 | src_data = PyUnicode_DATA(self); |
Victor Stinner | 55c9911 | 2011-10-13 01:17:06 +0200 | [diff] [blame] | 13292 | if (!PyUnicode_IS_ASCII(self)) { |
| 13293 | kind_limit = kind_maxchar_limit(src_kind); |
| 13294 | max_char = 0; |
| 13295 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
| 13296 | ch = PyUnicode_READ(src_kind, src_data, cur); |
| 13297 | if (ch > max_char) { |
| 13298 | max_char = ch; |
| 13299 | if (max_char >= kind_limit) |
| 13300 | break; |
| 13301 | } |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 13302 | } |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13303 | } |
Victor Stinner | 55c9911 | 2011-10-13 01:17:06 +0200 | [diff] [blame] | 13304 | else |
| 13305 | max_char = 127; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13306 | result = PyUnicode_New(slicelength, max_char); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13307 | if (result == NULL) |
| 13308 | return NULL; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13309 | dest_kind = PyUnicode_KIND(result); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13310 | dest_data = PyUnicode_DATA(result); |
| 13311 | |
| 13312 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13313 | Py_UCS4 ch = PyUnicode_READ(src_kind, src_data, cur); |
| 13314 | PyUnicode_WRITE(dest_kind, dest_data, i, ch); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13315 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13316 | assert(_PyUnicode_CheckConsistency(result, 1)); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13317 | return result; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13318 | } else { |
| 13319 | PyErr_SetString(PyExc_TypeError, "string indices must be integers"); |
| 13320 | return NULL; |
| 13321 | } |
| 13322 | } |
| 13323 | |
| 13324 | static PyMappingMethods unicode_as_mapping = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13325 | (lenfunc)unicode_length, /* mp_length */ |
| 13326 | (binaryfunc)unicode_subscript, /* mp_subscript */ |
| 13327 | (objobjargproc)0, /* mp_ass_subscript */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13328 | }; |
| 13329 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13330 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13331 | /* Helpers for PyUnicode_Format() */ |
| 13332 | |
| 13333 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13334 | 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] | 13335 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13336 | Py_ssize_t argidx = *p_argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13337 | if (argidx < arglen) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13338 | (*p_argidx)++; |
| 13339 | if (arglen < 0) |
| 13340 | return args; |
| 13341 | else |
| 13342 | return PyTuple_GetItem(args, argidx); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13343 | } |
| 13344 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13345 | "not enough arguments for format string"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13346 | return NULL; |
| 13347 | } |
| 13348 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13349 | /* 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] | 13350 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13351 | static PyObject * |
| 13352 | formatfloat(PyObject *v, int flags, int prec, int type) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13353 | { |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13354 | char *p; |
| 13355 | PyObject *result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13356 | double x; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 13357 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13358 | x = PyFloat_AsDouble(v); |
| 13359 | if (x == -1.0 && PyErr_Occurred()) |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13360 | return NULL; |
| 13361 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13362 | if (prec < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13363 | prec = 6; |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 13364 | |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 13365 | p = PyOS_double_to_string(x, type, prec, |
| 13366 | (flags & F_ALT) ? Py_DTSF_ALT : 0, NULL); |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13367 | if (p == NULL) |
| 13368 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13369 | result = PyUnicode_DecodeASCII(p, strlen(p), NULL); |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 13370 | PyMem_Free(p); |
| 13371 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13372 | } |
| 13373 | |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13374 | static PyObject* |
| 13375 | formatlong(PyObject *val, int flags, int prec, int type) |
| 13376 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13377 | char *buf; |
| 13378 | int len; |
| 13379 | PyObject *str; /* temporary string object. */ |
| 13380 | PyObject *result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13381 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13382 | str = _PyBytes_FormatLong(val, flags, prec, type, &buf, &len); |
| 13383 | if (!str) |
| 13384 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13385 | result = PyUnicode_DecodeASCII(buf, len, NULL); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13386 | Py_DECREF(str); |
| 13387 | return result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13388 | } |
| 13389 | |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13390 | static Py_UCS4 |
| 13391 | formatchar(PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13392 | { |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 13393 | /* presume that the buffer is at least 3 characters long */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 13394 | if (PyUnicode_Check(v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13395 | if (PyUnicode_GET_LENGTH(v) == 1) { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13396 | return PyUnicode_READ_CHAR(v, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13397 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13398 | goto onError; |
| 13399 | } |
| 13400 | else { |
| 13401 | /* Integer input truncated to a character */ |
| 13402 | long x; |
| 13403 | x = PyLong_AsLong(v); |
| 13404 | if (x == -1 && PyErr_Occurred()) |
| 13405 | goto onError; |
| 13406 | |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 13407 | if (x < 0 || x > MAX_UNICODE) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13408 | PyErr_SetString(PyExc_OverflowError, |
| 13409 | "%c arg not in range(0x110000)"); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13410 | return (Py_UCS4) -1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13411 | } |
| 13412 | |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13413 | return (Py_UCS4) x; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13414 | } |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 13415 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13416 | onError: |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 13417 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13418 | "%c requires int or char"); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13419 | return (Py_UCS4) -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13420 | } |
| 13421 | |
Antoine Pitrou | 978b9d2 | 2011-10-07 12:35:48 +0200 | [diff] [blame] | 13422 | static int |
| 13423 | repeat_accumulate(_PyAccu *acc, PyObject *obj, Py_ssize_t count) |
| 13424 | { |
| 13425 | int r; |
| 13426 | assert(count > 0); |
| 13427 | assert(PyUnicode_Check(obj)); |
| 13428 | if (count > 5) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13429 | PyObject *repeated = unicode_repeat(obj, count); |
Antoine Pitrou | 978b9d2 | 2011-10-07 12:35:48 +0200 | [diff] [blame] | 13430 | if (repeated == NULL) |
| 13431 | return -1; |
| 13432 | r = _PyAccu_Accumulate(acc, repeated); |
| 13433 | Py_DECREF(repeated); |
| 13434 | return r; |
| 13435 | } |
| 13436 | else { |
| 13437 | do { |
| 13438 | if (_PyAccu_Accumulate(acc, obj)) |
| 13439 | return -1; |
| 13440 | } while (--count); |
| 13441 | return 0; |
| 13442 | } |
| 13443 | } |
| 13444 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 13445 | PyObject * |
| 13446 | PyUnicode_Format(PyObject *format, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13447 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13448 | void *fmt; |
| 13449 | int fmtkind; |
| 13450 | PyObject *result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13451 | int kind; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13452 | int r; |
| 13453 | Py_ssize_t fmtcnt, fmtpos, arglen, argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13454 | int args_owned = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13455 | PyObject *dict = NULL; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13456 | PyObject *temp = NULL; |
| 13457 | PyObject *second = NULL; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13458 | PyObject *uformat; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13459 | _PyAccu acc; |
| 13460 | static PyObject *plus, *minus, *blank, *zero, *percent; |
| 13461 | |
| 13462 | if (!plus && !(plus = get_latin1_char('+'))) |
| 13463 | return NULL; |
| 13464 | if (!minus && !(minus = get_latin1_char('-'))) |
| 13465 | return NULL; |
| 13466 | if (!blank && !(blank = get_latin1_char(' '))) |
| 13467 | return NULL; |
| 13468 | if (!zero && !(zero = get_latin1_char('0'))) |
| 13469 | return NULL; |
| 13470 | if (!percent && !(percent = get_latin1_char('%'))) |
| 13471 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 13472 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13473 | if (format == NULL || args == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13474 | PyErr_BadInternalCall(); |
| 13475 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13476 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13477 | uformat = PyUnicode_FromObject(format); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 13478 | if (uformat == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13479 | return NULL; |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 13480 | if (PyUnicode_READY(uformat) == -1) |
| 13481 | Py_DECREF(uformat); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13482 | if (_PyAccu_Init(&acc)) |
| 13483 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13484 | fmt = PyUnicode_DATA(uformat); |
| 13485 | fmtkind = PyUnicode_KIND(uformat); |
| 13486 | fmtcnt = PyUnicode_GET_LENGTH(uformat); |
| 13487 | fmtpos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13488 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13489 | if (PyTuple_Check(args)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13490 | arglen = PyTuple_Size(args); |
| 13491 | argidx = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13492 | } |
| 13493 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13494 | arglen = -1; |
| 13495 | argidx = -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13496 | } |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 13497 | if (Py_TYPE(args)->tp_as_mapping && !PyTuple_Check(args) && |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 13498 | !PyUnicode_Check(args)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13499 | dict = args; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13500 | |
| 13501 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13502 | if (PyUnicode_READ(fmtkind, fmt, fmtpos) != '%') { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13503 | PyObject *nonfmt; |
| 13504 | Py_ssize_t nonfmtpos; |
| 13505 | nonfmtpos = fmtpos++; |
| 13506 | while (fmtcnt >= 0 && |
| 13507 | PyUnicode_READ(fmtkind, fmt, fmtpos) != '%') { |
| 13508 | fmtpos++; |
| 13509 | fmtcnt--; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13510 | } |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 13511 | nonfmt = PyUnicode_Substring(uformat, nonfmtpos, fmtpos); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13512 | if (nonfmt == NULL) |
| 13513 | goto onError; |
| 13514 | r = _PyAccu_Accumulate(&acc, nonfmt); |
| 13515 | Py_DECREF(nonfmt); |
| 13516 | if (r) |
| 13517 | goto onError; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13518 | } |
| 13519 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13520 | /* Got a format specifier */ |
| 13521 | int flags = 0; |
| 13522 | Py_ssize_t width = -1; |
| 13523 | int prec = -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13524 | Py_UCS4 c = '\0'; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13525 | Py_UCS4 fill, sign; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13526 | int isnumok; |
| 13527 | PyObject *v = NULL; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13528 | void *pbuf = NULL; |
| 13529 | Py_ssize_t pindex, len; |
| 13530 | PyObject *signobj = NULL, *fillobj = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13531 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13532 | fmtpos++; |
| 13533 | if (PyUnicode_READ(fmtkind, fmt, fmtpos) == '(') { |
| 13534 | Py_ssize_t keystart; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13535 | Py_ssize_t keylen; |
| 13536 | PyObject *key; |
| 13537 | int pcount = 1; |
Christian Heimes | a612dc0 | 2008-02-24 13:08:18 +0000 | [diff] [blame] | 13538 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13539 | if (dict == NULL) { |
| 13540 | PyErr_SetString(PyExc_TypeError, |
| 13541 | "format requires a mapping"); |
| 13542 | goto onError; |
| 13543 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13544 | ++fmtpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13545 | --fmtcnt; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13546 | keystart = fmtpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13547 | /* Skip over balanced parentheses */ |
| 13548 | while (pcount > 0 && --fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13549 | if (PyUnicode_READ(fmtkind, fmt, fmtpos) == ')') |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13550 | --pcount; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13551 | else if (PyUnicode_READ(fmtkind, fmt, fmtpos) == '(') |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13552 | ++pcount; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13553 | fmtpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13554 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13555 | keylen = fmtpos - keystart - 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13556 | if (fmtcnt < 0 || pcount > 0) { |
| 13557 | PyErr_SetString(PyExc_ValueError, |
| 13558 | "incomplete format key"); |
| 13559 | goto onError; |
| 13560 | } |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 13561 | key = PyUnicode_Substring(uformat, |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 13562 | keystart, keystart + keylen); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13563 | if (key == NULL) |
| 13564 | goto onError; |
| 13565 | if (args_owned) { |
| 13566 | Py_DECREF(args); |
| 13567 | args_owned = 0; |
| 13568 | } |
| 13569 | args = PyObject_GetItem(dict, key); |
| 13570 | Py_DECREF(key); |
| 13571 | if (args == NULL) { |
| 13572 | goto onError; |
| 13573 | } |
| 13574 | args_owned = 1; |
| 13575 | arglen = -1; |
| 13576 | argidx = -2; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13577 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13578 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13579 | switch (c = PyUnicode_READ(fmtkind, fmt, fmtpos++)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13580 | case '-': flags |= F_LJUST; continue; |
| 13581 | case '+': flags |= F_SIGN; continue; |
| 13582 | case ' ': flags |= F_BLANK; continue; |
| 13583 | case '#': flags |= F_ALT; continue; |
| 13584 | case '0': flags |= F_ZERO; continue; |
| 13585 | } |
| 13586 | break; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13587 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13588 | if (c == '*') { |
| 13589 | v = getnextarg(args, arglen, &argidx); |
| 13590 | if (v == NULL) |
| 13591 | goto onError; |
| 13592 | if (!PyLong_Check(v)) { |
| 13593 | PyErr_SetString(PyExc_TypeError, |
| 13594 | "* wants int"); |
| 13595 | goto onError; |
| 13596 | } |
| 13597 | width = PyLong_AsLong(v); |
| 13598 | if (width == -1 && PyErr_Occurred()) |
| 13599 | goto onError; |
| 13600 | if (width < 0) { |
| 13601 | flags |= F_LJUST; |
| 13602 | width = -width; |
| 13603 | } |
| 13604 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13605 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13606 | } |
| 13607 | else if (c >= '0' && c <= '9') { |
| 13608 | width = c - '0'; |
| 13609 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13610 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13611 | if (c < '0' || c > '9') |
| 13612 | break; |
| 13613 | if ((width*10) / 10 != width) { |
| 13614 | PyErr_SetString(PyExc_ValueError, |
| 13615 | "width too big"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13616 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13617 | } |
| 13618 | width = width*10 + (c - '0'); |
| 13619 | } |
| 13620 | } |
| 13621 | if (c == '.') { |
| 13622 | prec = 0; |
| 13623 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13624 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13625 | if (c == '*') { |
| 13626 | v = getnextarg(args, arglen, &argidx); |
| 13627 | if (v == NULL) |
| 13628 | goto onError; |
| 13629 | if (!PyLong_Check(v)) { |
| 13630 | PyErr_SetString(PyExc_TypeError, |
| 13631 | "* wants int"); |
| 13632 | goto onError; |
| 13633 | } |
| 13634 | prec = PyLong_AsLong(v); |
| 13635 | if (prec == -1 && PyErr_Occurred()) |
| 13636 | goto onError; |
| 13637 | if (prec < 0) |
| 13638 | prec = 0; |
| 13639 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13640 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13641 | } |
| 13642 | else if (c >= '0' && c <= '9') { |
| 13643 | prec = c - '0'; |
| 13644 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13645 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13646 | if (c < '0' || c > '9') |
| 13647 | break; |
| 13648 | if ((prec*10) / 10 != prec) { |
| 13649 | PyErr_SetString(PyExc_ValueError, |
| 13650 | "prec too big"); |
| 13651 | goto onError; |
| 13652 | } |
| 13653 | prec = prec*10 + (c - '0'); |
| 13654 | } |
| 13655 | } |
| 13656 | } /* prec */ |
| 13657 | if (fmtcnt >= 0) { |
| 13658 | if (c == 'h' || c == 'l' || c == 'L') { |
| 13659 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13660 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13661 | } |
| 13662 | } |
| 13663 | if (fmtcnt < 0) { |
| 13664 | PyErr_SetString(PyExc_ValueError, |
| 13665 | "incomplete format"); |
| 13666 | goto onError; |
| 13667 | } |
| 13668 | if (c != '%') { |
| 13669 | v = getnextarg(args, arglen, &argidx); |
| 13670 | if (v == NULL) |
| 13671 | goto onError; |
| 13672 | } |
| 13673 | sign = 0; |
| 13674 | fill = ' '; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13675 | fillobj = blank; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13676 | switch (c) { |
| 13677 | |
| 13678 | case '%': |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13679 | _PyAccu_Accumulate(&acc, percent); |
| 13680 | continue; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13681 | |
| 13682 | case 's': |
| 13683 | case 'r': |
| 13684 | case 'a': |
Victor Stinner | 808fc0a | 2010-03-22 12:50:40 +0000 | [diff] [blame] | 13685 | if (PyUnicode_CheckExact(v) && c == 's') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13686 | temp = v; |
| 13687 | Py_INCREF(temp); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13688 | } |
| 13689 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13690 | if (c == 's') |
| 13691 | temp = PyObject_Str(v); |
| 13692 | else if (c == 'r') |
| 13693 | temp = PyObject_Repr(v); |
| 13694 | else |
| 13695 | temp = PyObject_ASCII(v); |
| 13696 | if (temp == NULL) |
| 13697 | goto onError; |
| 13698 | if (PyUnicode_Check(temp)) |
| 13699 | /* nothing to do */; |
| 13700 | else { |
| 13701 | Py_DECREF(temp); |
| 13702 | PyErr_SetString(PyExc_TypeError, |
| 13703 | "%s argument has non-string str()"); |
| 13704 | goto onError; |
| 13705 | } |
| 13706 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13707 | if (PyUnicode_READY(temp) == -1) { |
| 13708 | Py_CLEAR(temp); |
| 13709 | goto onError; |
| 13710 | } |
| 13711 | pbuf = PyUnicode_DATA(temp); |
| 13712 | kind = PyUnicode_KIND(temp); |
| 13713 | len = PyUnicode_GET_LENGTH(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13714 | if (prec >= 0 && len > prec) |
| 13715 | len = prec; |
| 13716 | break; |
| 13717 | |
| 13718 | case 'i': |
| 13719 | case 'd': |
| 13720 | case 'u': |
| 13721 | case 'o': |
| 13722 | case 'x': |
| 13723 | case 'X': |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13724 | isnumok = 0; |
| 13725 | if (PyNumber_Check(v)) { |
| 13726 | PyObject *iobj=NULL; |
| 13727 | |
| 13728 | if (PyLong_Check(v)) { |
| 13729 | iobj = v; |
| 13730 | Py_INCREF(iobj); |
| 13731 | } |
| 13732 | else { |
| 13733 | iobj = PyNumber_Long(v); |
| 13734 | } |
| 13735 | if (iobj!=NULL) { |
| 13736 | if (PyLong_Check(iobj)) { |
| 13737 | isnumok = 1; |
Senthil Kumaran | 9ebe08d | 2011-07-03 21:03:16 -0700 | [diff] [blame] | 13738 | temp = formatlong(iobj, flags, prec, (c == 'i'? 'd': c)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13739 | Py_DECREF(iobj); |
| 13740 | if (!temp) |
| 13741 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13742 | if (PyUnicode_READY(temp) == -1) { |
| 13743 | Py_CLEAR(temp); |
| 13744 | goto onError; |
| 13745 | } |
| 13746 | pbuf = PyUnicode_DATA(temp); |
| 13747 | kind = PyUnicode_KIND(temp); |
| 13748 | len = PyUnicode_GET_LENGTH(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13749 | sign = 1; |
| 13750 | } |
| 13751 | else { |
| 13752 | Py_DECREF(iobj); |
| 13753 | } |
| 13754 | } |
| 13755 | } |
| 13756 | if (!isnumok) { |
| 13757 | PyErr_Format(PyExc_TypeError, |
| 13758 | "%%%c format: a number is required, " |
| 13759 | "not %.200s", (char)c, Py_TYPE(v)->tp_name); |
| 13760 | goto onError; |
| 13761 | } |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13762 | if (flags & F_ZERO) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13763 | fill = '0'; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13764 | fillobj = zero; |
| 13765 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13766 | break; |
| 13767 | |
| 13768 | case 'e': |
| 13769 | case 'E': |
| 13770 | case 'f': |
| 13771 | case 'F': |
| 13772 | case 'g': |
| 13773 | case 'G': |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13774 | temp = formatfloat(v, flags, prec, c); |
| 13775 | if (!temp) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13776 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13777 | if (PyUnicode_READY(temp) == -1) { |
| 13778 | Py_CLEAR(temp); |
| 13779 | goto onError; |
| 13780 | } |
| 13781 | pbuf = PyUnicode_DATA(temp); |
| 13782 | kind = PyUnicode_KIND(temp); |
| 13783 | len = PyUnicode_GET_LENGTH(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13784 | sign = 1; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13785 | if (flags & F_ZERO) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13786 | fill = '0'; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13787 | fillobj = zero; |
| 13788 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13789 | break; |
| 13790 | |
| 13791 | case 'c': |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13792 | { |
| 13793 | Py_UCS4 ch = formatchar(v); |
| 13794 | if (ch == (Py_UCS4) -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13795 | goto onError; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13796 | temp = _PyUnicode_FromUCS4(&ch, 1); |
| 13797 | if (temp == NULL) |
| 13798 | goto onError; |
| 13799 | pbuf = PyUnicode_DATA(temp); |
| 13800 | kind = PyUnicode_KIND(temp); |
| 13801 | len = PyUnicode_GET_LENGTH(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13802 | break; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13803 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13804 | |
| 13805 | default: |
| 13806 | PyErr_Format(PyExc_ValueError, |
| 13807 | "unsupported format character '%c' (0x%x) " |
| 13808 | "at index %zd", |
| 13809 | (31<=c && c<=126) ? (char)c : '?', |
| 13810 | (int)c, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13811 | fmtpos - 1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13812 | goto onError; |
| 13813 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13814 | /* pbuf is initialized here. */ |
| 13815 | pindex = 0; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13816 | if (sign) { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13817 | if (PyUnicode_READ(kind, pbuf, pindex) == '-') { |
| 13818 | signobj = minus; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13819 | len--; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13820 | pindex++; |
| 13821 | } |
| 13822 | else if (PyUnicode_READ(kind, pbuf, pindex) == '+') { |
| 13823 | signobj = plus; |
| 13824 | len--; |
| 13825 | pindex++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13826 | } |
| 13827 | else if (flags & F_SIGN) |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13828 | signobj = plus; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13829 | else if (flags & F_BLANK) |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13830 | signobj = blank; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13831 | else |
| 13832 | sign = 0; |
| 13833 | } |
| 13834 | if (width < len) |
| 13835 | width = len; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13836 | if (sign) { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13837 | if (fill != ' ') { |
| 13838 | assert(signobj != NULL); |
| 13839 | if (_PyAccu_Accumulate(&acc, signobj)) |
| 13840 | goto onError; |
| 13841 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13842 | if (width > len) |
| 13843 | width--; |
| 13844 | } |
| 13845 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13846 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13847 | assert(PyUnicode_READ(kind, pbuf, pindex + 1) == c); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13848 | if (fill != ' ') { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13849 | second = get_latin1_char( |
| 13850 | PyUnicode_READ(kind, pbuf, pindex + 1)); |
| 13851 | pindex += 2; |
| 13852 | if (second == NULL || |
| 13853 | _PyAccu_Accumulate(&acc, zero) || |
| 13854 | _PyAccu_Accumulate(&acc, second)) |
| 13855 | goto onError; |
| 13856 | Py_CLEAR(second); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13857 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13858 | width -= 2; |
| 13859 | if (width < 0) |
| 13860 | width = 0; |
| 13861 | len -= 2; |
| 13862 | } |
| 13863 | if (width > len && !(flags & F_LJUST)) { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13864 | assert(fillobj != NULL); |
Antoine Pitrou | 978b9d2 | 2011-10-07 12:35:48 +0200 | [diff] [blame] | 13865 | if (repeat_accumulate(&acc, fillobj, width - len)) |
| 13866 | goto onError; |
| 13867 | width = len; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13868 | } |
| 13869 | if (fill == ' ') { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13870 | if (sign) { |
| 13871 | assert(signobj != NULL); |
| 13872 | if (_PyAccu_Accumulate(&acc, signobj)) |
| 13873 | goto onError; |
| 13874 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13875 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13876 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
| 13877 | assert(PyUnicode_READ(kind, pbuf, pindex+1) == c); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13878 | second = get_latin1_char( |
| 13879 | PyUnicode_READ(kind, pbuf, pindex + 1)); |
| 13880 | pindex += 2; |
| 13881 | if (second == NULL || |
| 13882 | _PyAccu_Accumulate(&acc, zero) || |
| 13883 | _PyAccu_Accumulate(&acc, second)) |
| 13884 | goto onError; |
| 13885 | Py_CLEAR(second); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13886 | } |
| 13887 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13888 | /* Copy all characters, preserving len */ |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13889 | if (temp != NULL) { |
| 13890 | assert(pbuf == PyUnicode_DATA(temp)); |
| 13891 | v = PyUnicode_Substring(temp, pindex, pindex + len); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13892 | } |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13893 | else { |
| 13894 | const char *p = (const char *) pbuf; |
| 13895 | assert(pbuf != NULL); |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 13896 | p += kind * pindex; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13897 | v = PyUnicode_FromKindAndData(kind, p, len); |
| 13898 | } |
| 13899 | if (v == NULL) |
| 13900 | goto onError; |
| 13901 | r = _PyAccu_Accumulate(&acc, v); |
| 13902 | Py_DECREF(v); |
| 13903 | if (r) |
| 13904 | goto onError; |
Antoine Pitrou | 978b9d2 | 2011-10-07 12:35:48 +0200 | [diff] [blame] | 13905 | if (width > len && repeat_accumulate(&acc, blank, width - len)) |
| 13906 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13907 | if (dict && (argidx < arglen) && c != '%') { |
| 13908 | PyErr_SetString(PyExc_TypeError, |
| 13909 | "not all arguments converted during string formatting"); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13910 | goto onError; |
| 13911 | } |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13912 | Py_CLEAR(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13913 | } /* '%' */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13914 | } /* until end */ |
| 13915 | if (argidx < arglen && !dict) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13916 | PyErr_SetString(PyExc_TypeError, |
| 13917 | "not all arguments converted during string formatting"); |
| 13918 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13919 | } |
| 13920 | |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13921 | result = _PyAccu_Finish(&acc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13922 | if (args_owned) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13923 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13924 | } |
| 13925 | Py_DECREF(uformat); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13926 | Py_XDECREF(temp); |
| 13927 | Py_XDECREF(second); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 13928 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13929 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13930 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13931 | Py_DECREF(uformat); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13932 | Py_XDECREF(temp); |
| 13933 | Py_XDECREF(second); |
| 13934 | _PyAccu_Destroy(&acc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13935 | if (args_owned) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13936 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13937 | } |
| 13938 | return NULL; |
| 13939 | } |
| 13940 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 13941 | static PyObject * |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 13942 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 13943 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13944 | static PyObject * |
| 13945 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 13946 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13947 | PyObject *x = NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13948 | static char *kwlist[] = {"object", "encoding", "errors", 0}; |
| 13949 | char *encoding = NULL; |
| 13950 | char *errors = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13951 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13952 | if (type != &PyUnicode_Type) |
| 13953 | return unicode_subtype_new(type, args, kwds); |
| 13954 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:str", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13955 | kwlist, &x, &encoding, &errors)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13956 | return NULL; |
Victor Stinner | 382955f | 2011-12-11 21:44:00 +0100 | [diff] [blame] | 13957 | if (x == NULL) { |
| 13958 | Py_INCREF(unicode_empty); |
| 13959 | return unicode_empty; |
| 13960 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13961 | if (encoding == NULL && errors == NULL) |
| 13962 | return PyObject_Str(x); |
| 13963 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13964 | return PyUnicode_FromEncodedObject(x, encoding, errors); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13965 | } |
| 13966 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 13967 | static PyObject * |
| 13968 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 13969 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13970 | PyObject *unicode, *self; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13971 | Py_ssize_t length, char_size; |
| 13972 | int share_wstr, share_utf8; |
| 13973 | unsigned int kind; |
| 13974 | void *data; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 13975 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13976 | assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13977 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13978 | unicode = unicode_new(&PyUnicode_Type, args, kwds); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13979 | if (unicode == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13980 | return NULL; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 13981 | assert(_PyUnicode_CHECK(unicode)); |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 13982 | if (PyUnicode_READY(unicode) == -1) { |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 13983 | Py_DECREF(unicode); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13984 | return NULL; |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 13985 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13986 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13987 | self = type->tp_alloc(type, 0); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13988 | if (self == NULL) { |
| 13989 | Py_DECREF(unicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13990 | return NULL; |
| 13991 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13992 | kind = PyUnicode_KIND(unicode); |
| 13993 | length = PyUnicode_GET_LENGTH(unicode); |
| 13994 | |
| 13995 | _PyUnicode_LENGTH(self) = length; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 13996 | #ifdef Py_DEBUG |
| 13997 | _PyUnicode_HASH(self) = -1; |
| 13998 | #else |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13999 | _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 14000 | #endif |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14001 | _PyUnicode_STATE(self).interned = 0; |
| 14002 | _PyUnicode_STATE(self).kind = kind; |
| 14003 | _PyUnicode_STATE(self).compact = 0; |
Victor Stinner | 3cf4637 | 2011-10-03 14:42:15 +0200 | [diff] [blame] | 14004 | _PyUnicode_STATE(self).ascii = _PyUnicode_STATE(unicode).ascii; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14005 | _PyUnicode_STATE(self).ready = 1; |
| 14006 | _PyUnicode_WSTR(self) = NULL; |
| 14007 | _PyUnicode_UTF8_LENGTH(self) = 0; |
| 14008 | _PyUnicode_UTF8(self) = NULL; |
| 14009 | _PyUnicode_WSTR_LENGTH(self) = 0; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 14010 | _PyUnicode_DATA_ANY(self) = NULL; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14011 | |
| 14012 | share_utf8 = 0; |
| 14013 | share_wstr = 0; |
| 14014 | if (kind == PyUnicode_1BYTE_KIND) { |
| 14015 | char_size = 1; |
| 14016 | if (PyUnicode_MAX_CHAR_VALUE(unicode) < 128) |
| 14017 | share_utf8 = 1; |
| 14018 | } |
| 14019 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 14020 | char_size = 2; |
| 14021 | if (sizeof(wchar_t) == 2) |
| 14022 | share_wstr = 1; |
| 14023 | } |
| 14024 | else { |
| 14025 | assert(kind == PyUnicode_4BYTE_KIND); |
| 14026 | char_size = 4; |
| 14027 | if (sizeof(wchar_t) == 4) |
| 14028 | share_wstr = 1; |
| 14029 | } |
| 14030 | |
| 14031 | /* Ensure we won't overflow the length. */ |
| 14032 | if (length > (PY_SSIZE_T_MAX / char_size - 1)) { |
| 14033 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14034 | goto onError; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14035 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14036 | data = PyObject_MALLOC((length + 1) * char_size); |
| 14037 | if (data == NULL) { |
| 14038 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14039 | goto onError; |
| 14040 | } |
| 14041 | |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 14042 | _PyUnicode_DATA_ANY(self) = data; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14043 | if (share_utf8) { |
| 14044 | _PyUnicode_UTF8_LENGTH(self) = length; |
| 14045 | _PyUnicode_UTF8(self) = data; |
| 14046 | } |
| 14047 | if (share_wstr) { |
| 14048 | _PyUnicode_WSTR_LENGTH(self) = length; |
| 14049 | _PyUnicode_WSTR(self) = (wchar_t *)data; |
| 14050 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14051 | |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14052 | Py_MEMCPY(data, PyUnicode_DATA(unicode), |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 14053 | kind * (length + 1)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 14054 | assert(_PyUnicode_CheckConsistency(self, 1)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 14055 | #ifdef Py_DEBUG |
| 14056 | _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode); |
| 14057 | #endif |
Victor Stinner | dd18d3a | 2011-10-22 11:08:10 +0200 | [diff] [blame] | 14058 | Py_DECREF(unicode); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 14059 | return self; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14060 | |
| 14061 | onError: |
| 14062 | Py_DECREF(unicode); |
| 14063 | Py_DECREF(self); |
| 14064 | return NULL; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 14065 | } |
| 14066 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 14067 | PyDoc_STRVAR(unicode_doc, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14068 | "str(string[, encoding[, errors]]) -> str\n\ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14069 | \n\ |
Collin Winter | d474ce8 | 2007-08-07 19:42:11 +0000 | [diff] [blame] | 14070 | Create a new string object from the given encoded string.\n\ |
Skip Montanaro | 35b37a5 | 2002-07-26 16:22:46 +0000 | [diff] [blame] | 14071 | encoding defaults to the current default string encoding.\n\ |
| 14072 | errors can be 'strict', 'replace' or 'ignore' and defaults to 'strict'."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14073 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14074 | static PyObject *unicode_iter(PyObject *seq); |
| 14075 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14076 | PyTypeObject PyUnicode_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 14077 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14078 | "str", /* tp_name */ |
| 14079 | sizeof(PyUnicodeObject), /* tp_size */ |
| 14080 | 0, /* tp_itemsize */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14081 | /* Slots */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14082 | (destructor)unicode_dealloc, /* tp_dealloc */ |
| 14083 | 0, /* tp_print */ |
| 14084 | 0, /* tp_getattr */ |
| 14085 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 14086 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14087 | unicode_repr, /* tp_repr */ |
| 14088 | &unicode_as_number, /* tp_as_number */ |
| 14089 | &unicode_as_sequence, /* tp_as_sequence */ |
| 14090 | &unicode_as_mapping, /* tp_as_mapping */ |
| 14091 | (hashfunc) unicode_hash, /* tp_hash*/ |
| 14092 | 0, /* tp_call*/ |
| 14093 | (reprfunc) unicode_str, /* tp_str */ |
| 14094 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 14095 | 0, /* tp_setattro */ |
| 14096 | 0, /* tp_as_buffer */ |
| 14097 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14098 | Py_TPFLAGS_UNICODE_SUBCLASS, /* tp_flags */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14099 | unicode_doc, /* tp_doc */ |
| 14100 | 0, /* tp_traverse */ |
| 14101 | 0, /* tp_clear */ |
| 14102 | PyUnicode_RichCompare, /* tp_richcompare */ |
| 14103 | 0, /* tp_weaklistoffset */ |
| 14104 | unicode_iter, /* tp_iter */ |
| 14105 | 0, /* tp_iternext */ |
| 14106 | unicode_methods, /* tp_methods */ |
| 14107 | 0, /* tp_members */ |
| 14108 | 0, /* tp_getset */ |
| 14109 | &PyBaseObject_Type, /* tp_base */ |
| 14110 | 0, /* tp_dict */ |
| 14111 | 0, /* tp_descr_get */ |
| 14112 | 0, /* tp_descr_set */ |
| 14113 | 0, /* tp_dictoffset */ |
| 14114 | 0, /* tp_init */ |
| 14115 | 0, /* tp_alloc */ |
| 14116 | unicode_new, /* tp_new */ |
| 14117 | PyObject_Del, /* tp_free */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14118 | }; |
| 14119 | |
| 14120 | /* Initialize the Unicode implementation */ |
| 14121 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 14122 | int _PyUnicode_Init(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14123 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 14124 | int i; |
| 14125 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14126 | /* XXX - move this array to unicodectype.c ? */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14127 | Py_UCS2 linebreak[] = { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14128 | 0x000A, /* LINE FEED */ |
| 14129 | 0x000D, /* CARRIAGE RETURN */ |
| 14130 | 0x001C, /* FILE SEPARATOR */ |
| 14131 | 0x001D, /* GROUP SEPARATOR */ |
| 14132 | 0x001E, /* RECORD SEPARATOR */ |
| 14133 | 0x0085, /* NEXT LINE */ |
| 14134 | 0x2028, /* LINE SEPARATOR */ |
| 14135 | 0x2029, /* PARAGRAPH SEPARATOR */ |
| 14136 | }; |
| 14137 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 14138 | /* Init the implementation */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 14139 | unicode_empty = PyUnicode_New(0, 0); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 14140 | if (!unicode_empty) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14141 | Py_FatalError("Can't create empty string"); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 14142 | assert(_PyUnicode_CheckConsistency(unicode_empty, 1)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 14143 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 14144 | for (i = 0; i < 256; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14145 | unicode_latin1[i] = NULL; |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 14146 | if (PyType_Ready(&PyUnicode_Type) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14147 | Py_FatalError("Can't initialize 'unicode'"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14148 | |
| 14149 | /* initialize the linebreak bloom filter */ |
| 14150 | bloom_linebreak = make_bloom_mask( |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14151 | PyUnicode_2BYTE_KIND, linebreak, |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 14152 | Py_ARRAY_LENGTH(linebreak)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 14153 | |
| 14154 | PyType_Ready(&EncodingMapType); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 14155 | |
| 14156 | #ifdef HAVE_MBCS |
| 14157 | winver.dwOSVersionInfoSize = sizeof(winver); |
| 14158 | if (!GetVersionEx((OSVERSIONINFO*)&winver)) { |
| 14159 | PyErr_SetFromWindowsErr(0); |
| 14160 | return -1; |
| 14161 | } |
| 14162 | #endif |
| 14163 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14164 | } |
| 14165 | |
| 14166 | /* Finalize the Unicode implementation */ |
| 14167 | |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 14168 | int |
| 14169 | PyUnicode_ClearFreeList(void) |
| 14170 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14171 | return 0; |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 14172 | } |
| 14173 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14174 | void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 14175 | _PyUnicode_Fini(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14176 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 14177 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14178 | |
Guido van Rossum | 4ae8ef8 | 2000-10-03 18:09:04 +0000 | [diff] [blame] | 14179 | Py_XDECREF(unicode_empty); |
| 14180 | unicode_empty = NULL; |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 14181 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 14182 | for (i = 0; i < 256; i++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14183 | if (unicode_latin1[i]) { |
| 14184 | Py_DECREF(unicode_latin1[i]); |
| 14185 | unicode_latin1[i] = NULL; |
| 14186 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 14187 | } |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 14188 | _PyUnicode_ClearStaticStrings(); |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 14189 | (void)PyUnicode_ClearFreeList(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14190 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 14191 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14192 | void |
| 14193 | PyUnicode_InternInPlace(PyObject **p) |
| 14194 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14195 | register PyObject *s = *p; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14196 | PyObject *t; |
Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 14197 | #ifdef Py_DEBUG |
| 14198 | assert(s != NULL); |
| 14199 | assert(_PyUnicode_CHECK(s)); |
| 14200 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14201 | if (s == NULL || !PyUnicode_Check(s)) |
Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 14202 | return; |
| 14203 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14204 | /* If it's a subclass, we don't really know what putting |
| 14205 | it in the interned dict might do. */ |
| 14206 | if (!PyUnicode_CheckExact(s)) |
| 14207 | return; |
| 14208 | if (PyUnicode_CHECK_INTERNED(s)) |
| 14209 | return; |
| 14210 | if (interned == NULL) { |
| 14211 | interned = PyDict_New(); |
| 14212 | if (interned == NULL) { |
| 14213 | PyErr_Clear(); /* Don't leave an exception */ |
| 14214 | return; |
| 14215 | } |
| 14216 | } |
| 14217 | /* It might be that the GetItem call fails even |
| 14218 | though the key is present in the dictionary, |
| 14219 | namely when this happens during a stack overflow. */ |
| 14220 | Py_ALLOW_RECURSION |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 14221 | t = PyDict_GetItem(interned, s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14222 | Py_END_ALLOW_RECURSION |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 14223 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14224 | if (t) { |
| 14225 | Py_INCREF(t); |
| 14226 | Py_DECREF(*p); |
| 14227 | *p = t; |
| 14228 | return; |
| 14229 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14230 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14231 | PyThreadState_GET()->recursion_critical = 1; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 14232 | if (PyDict_SetItem(interned, s, s) < 0) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14233 | PyErr_Clear(); |
| 14234 | PyThreadState_GET()->recursion_critical = 0; |
| 14235 | return; |
| 14236 | } |
| 14237 | PyThreadState_GET()->recursion_critical = 0; |
| 14238 | /* The two references in interned are not counted by refcnt. |
| 14239 | The deallocator will take care of this */ |
| 14240 | Py_REFCNT(s) -= 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14241 | _PyUnicode_STATE(s).interned = SSTATE_INTERNED_MORTAL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14242 | } |
| 14243 | |
| 14244 | void |
| 14245 | PyUnicode_InternImmortal(PyObject **p) |
| 14246 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14247 | PyUnicode_InternInPlace(p); |
| 14248 | if (PyUnicode_CHECK_INTERNED(*p) != SSTATE_INTERNED_IMMORTAL) { |
Victor Stinner | af9e4b8 | 2011-10-23 20:07:00 +0200 | [diff] [blame] | 14249 | _PyUnicode_STATE(*p).interned = SSTATE_INTERNED_IMMORTAL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14250 | Py_INCREF(*p); |
| 14251 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14252 | } |
| 14253 | |
| 14254 | PyObject * |
| 14255 | PyUnicode_InternFromString(const char *cp) |
| 14256 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14257 | PyObject *s = PyUnicode_FromString(cp); |
| 14258 | if (s == NULL) |
| 14259 | return NULL; |
| 14260 | PyUnicode_InternInPlace(&s); |
| 14261 | return s; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14262 | } |
| 14263 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 14264 | void |
| 14265 | _Py_ReleaseInternedUnicodeStrings(void) |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14266 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14267 | PyObject *keys; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14268 | PyObject *s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14269 | Py_ssize_t i, n; |
| 14270 | Py_ssize_t immortal_size = 0, mortal_size = 0; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14271 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14272 | if (interned == NULL || !PyDict_Check(interned)) |
| 14273 | return; |
| 14274 | keys = PyDict_Keys(interned); |
| 14275 | if (keys == NULL || !PyList_Check(keys)) { |
| 14276 | PyErr_Clear(); |
| 14277 | return; |
| 14278 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14279 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14280 | /* Since _Py_ReleaseInternedUnicodeStrings() is intended to help a leak |
| 14281 | detector, interned unicode strings are not forcibly deallocated; |
| 14282 | rather, we give them their stolen references back, and then clear |
| 14283 | and DECREF the interned dict. */ |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14284 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14285 | n = PyList_GET_SIZE(keys); |
| 14286 | fprintf(stderr, "releasing %" PY_FORMAT_SIZE_T "d interned strings\n", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14287 | n); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14288 | for (i = 0; i < n; i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14289 | s = PyList_GET_ITEM(keys, i); |
Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 14290 | if (PyUnicode_READY(s) == -1) { |
| 14291 | assert(0 && "could not ready string"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14292 | fprintf(stderr, "could not ready string\n"); |
Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 14293 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14294 | switch (PyUnicode_CHECK_INTERNED(s)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14295 | case SSTATE_NOT_INTERNED: |
| 14296 | /* XXX Shouldn't happen */ |
| 14297 | break; |
| 14298 | case SSTATE_INTERNED_IMMORTAL: |
| 14299 | Py_REFCNT(s) += 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14300 | immortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14301 | break; |
| 14302 | case SSTATE_INTERNED_MORTAL: |
| 14303 | Py_REFCNT(s) += 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14304 | mortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14305 | break; |
| 14306 | default: |
| 14307 | Py_FatalError("Inconsistent interned string state."); |
| 14308 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14309 | _PyUnicode_STATE(s).interned = SSTATE_NOT_INTERNED; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14310 | } |
| 14311 | fprintf(stderr, "total size of all interned strings: " |
| 14312 | "%" PY_FORMAT_SIZE_T "d/%" PY_FORMAT_SIZE_T "d " |
| 14313 | "mortal/immortal\n", mortal_size, immortal_size); |
| 14314 | Py_DECREF(keys); |
| 14315 | PyDict_Clear(interned); |
| 14316 | Py_DECREF(interned); |
| 14317 | interned = NULL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14318 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14319 | |
| 14320 | |
| 14321 | /********************* Unicode Iterator **************************/ |
| 14322 | |
| 14323 | typedef struct { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14324 | PyObject_HEAD |
| 14325 | Py_ssize_t it_index; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14326 | PyObject *it_seq; /* Set to NULL when iterator is exhausted */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14327 | } unicodeiterobject; |
| 14328 | |
| 14329 | static void |
| 14330 | unicodeiter_dealloc(unicodeiterobject *it) |
| 14331 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14332 | _PyObject_GC_UNTRACK(it); |
| 14333 | Py_XDECREF(it->it_seq); |
| 14334 | PyObject_GC_Del(it); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14335 | } |
| 14336 | |
| 14337 | static int |
| 14338 | unicodeiter_traverse(unicodeiterobject *it, visitproc visit, void *arg) |
| 14339 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14340 | Py_VISIT(it->it_seq); |
| 14341 | return 0; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14342 | } |
| 14343 | |
| 14344 | static PyObject * |
| 14345 | unicodeiter_next(unicodeiterobject *it) |
| 14346 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14347 | PyObject *seq, *item; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14348 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14349 | assert(it != NULL); |
| 14350 | seq = it->it_seq; |
| 14351 | if (seq == NULL) |
| 14352 | return NULL; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 14353 | assert(_PyUnicode_CHECK(seq)); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14354 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14355 | if (it->it_index < PyUnicode_GET_LENGTH(seq)) { |
| 14356 | int kind = PyUnicode_KIND(seq); |
| 14357 | void *data = PyUnicode_DATA(seq); |
| 14358 | Py_UCS4 chr = PyUnicode_READ(kind, data, it->it_index); |
| 14359 | item = PyUnicode_FromOrdinal(chr); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14360 | if (item != NULL) |
| 14361 | ++it->it_index; |
| 14362 | return item; |
| 14363 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14364 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14365 | Py_DECREF(seq); |
| 14366 | it->it_seq = NULL; |
| 14367 | return NULL; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14368 | } |
| 14369 | |
| 14370 | static PyObject * |
| 14371 | unicodeiter_len(unicodeiterobject *it) |
| 14372 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14373 | Py_ssize_t len = 0; |
| 14374 | if (it->it_seq) |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 14375 | len = PyUnicode_GET_LENGTH(it->it_seq) - it->it_index; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14376 | return PyLong_FromSsize_t(len); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14377 | } |
| 14378 | |
| 14379 | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
| 14380 | |
| 14381 | static PyMethodDef unicodeiter_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14382 | {"__length_hint__", (PyCFunction)unicodeiter_len, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14383 | length_hint_doc}, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14384 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14385 | }; |
| 14386 | |
| 14387 | PyTypeObject PyUnicodeIter_Type = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14388 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 14389 | "str_iterator", /* tp_name */ |
| 14390 | sizeof(unicodeiterobject), /* tp_basicsize */ |
| 14391 | 0, /* tp_itemsize */ |
| 14392 | /* methods */ |
| 14393 | (destructor)unicodeiter_dealloc, /* tp_dealloc */ |
| 14394 | 0, /* tp_print */ |
| 14395 | 0, /* tp_getattr */ |
| 14396 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 14397 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14398 | 0, /* tp_repr */ |
| 14399 | 0, /* tp_as_number */ |
| 14400 | 0, /* tp_as_sequence */ |
| 14401 | 0, /* tp_as_mapping */ |
| 14402 | 0, /* tp_hash */ |
| 14403 | 0, /* tp_call */ |
| 14404 | 0, /* tp_str */ |
| 14405 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 14406 | 0, /* tp_setattro */ |
| 14407 | 0, /* tp_as_buffer */ |
| 14408 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 14409 | 0, /* tp_doc */ |
| 14410 | (traverseproc)unicodeiter_traverse, /* tp_traverse */ |
| 14411 | 0, /* tp_clear */ |
| 14412 | 0, /* tp_richcompare */ |
| 14413 | 0, /* tp_weaklistoffset */ |
| 14414 | PyObject_SelfIter, /* tp_iter */ |
| 14415 | (iternextfunc)unicodeiter_next, /* tp_iternext */ |
| 14416 | unicodeiter_methods, /* tp_methods */ |
| 14417 | 0, |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14418 | }; |
| 14419 | |
| 14420 | static PyObject * |
| 14421 | unicode_iter(PyObject *seq) |
| 14422 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14423 | unicodeiterobject *it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14424 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14425 | if (!PyUnicode_Check(seq)) { |
| 14426 | PyErr_BadInternalCall(); |
| 14427 | return NULL; |
| 14428 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14429 | if (PyUnicode_READY(seq) == -1) |
| 14430 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14431 | it = PyObject_GC_New(unicodeiterobject, &PyUnicodeIter_Type); |
| 14432 | if (it == NULL) |
| 14433 | return NULL; |
| 14434 | it->it_index = 0; |
| 14435 | Py_INCREF(seq); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14436 | it->it_seq = seq; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14437 | _PyObject_GC_TRACK(it); |
| 14438 | return (PyObject *)it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14439 | } |
| 14440 | |
Martin v. Löwis | 0d3072e | 2011-10-31 08:40:56 +0100 | [diff] [blame] | 14441 | |
| 14442 | size_t |
| 14443 | Py_UNICODE_strlen(const Py_UNICODE *u) |
| 14444 | { |
| 14445 | int res = 0; |
| 14446 | while(*u++) |
| 14447 | res++; |
| 14448 | return res; |
| 14449 | } |
| 14450 | |
| 14451 | Py_UNICODE* |
| 14452 | Py_UNICODE_strcpy(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 14453 | { |
| 14454 | Py_UNICODE *u = s1; |
| 14455 | while ((*u++ = *s2++)); |
| 14456 | return s1; |
| 14457 | } |
| 14458 | |
| 14459 | Py_UNICODE* |
| 14460 | Py_UNICODE_strncpy(Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 14461 | { |
| 14462 | Py_UNICODE *u = s1; |
| 14463 | while ((*u++ = *s2++)) |
| 14464 | if (n-- == 0) |
| 14465 | break; |
| 14466 | return s1; |
| 14467 | } |
| 14468 | |
| 14469 | Py_UNICODE* |
| 14470 | Py_UNICODE_strcat(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 14471 | { |
| 14472 | Py_UNICODE *u1 = s1; |
| 14473 | u1 += Py_UNICODE_strlen(u1); |
| 14474 | Py_UNICODE_strcpy(u1, s2); |
| 14475 | return s1; |
| 14476 | } |
| 14477 | |
| 14478 | int |
| 14479 | Py_UNICODE_strcmp(const Py_UNICODE *s1, const Py_UNICODE *s2) |
| 14480 | { |
| 14481 | while (*s1 && *s2 && *s1 == *s2) |
| 14482 | s1++, s2++; |
| 14483 | if (*s1 && *s2) |
| 14484 | return (*s1 < *s2) ? -1 : +1; |
| 14485 | if (*s1) |
| 14486 | return 1; |
| 14487 | if (*s2) |
| 14488 | return -1; |
| 14489 | return 0; |
| 14490 | } |
| 14491 | |
| 14492 | int |
| 14493 | Py_UNICODE_strncmp(const Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 14494 | { |
| 14495 | register Py_UNICODE u1, u2; |
| 14496 | for (; n != 0; n--) { |
| 14497 | u1 = *s1; |
| 14498 | u2 = *s2; |
| 14499 | if (u1 != u2) |
| 14500 | return (u1 < u2) ? -1 : +1; |
| 14501 | if (u1 == '\0') |
| 14502 | return 0; |
| 14503 | s1++; |
| 14504 | s2++; |
| 14505 | } |
| 14506 | return 0; |
| 14507 | } |
| 14508 | |
| 14509 | Py_UNICODE* |
| 14510 | Py_UNICODE_strchr(const Py_UNICODE *s, Py_UNICODE c) |
| 14511 | { |
| 14512 | const Py_UNICODE *p; |
| 14513 | for (p = s; *p; p++) |
| 14514 | if (*p == c) |
| 14515 | return (Py_UNICODE*)p; |
| 14516 | return NULL; |
| 14517 | } |
| 14518 | |
| 14519 | Py_UNICODE* |
| 14520 | Py_UNICODE_strrchr(const Py_UNICODE *s, Py_UNICODE c) |
| 14521 | { |
| 14522 | const Py_UNICODE *p; |
| 14523 | p = s + Py_UNICODE_strlen(s); |
| 14524 | while (p != s) { |
| 14525 | p--; |
| 14526 | if (*p == c) |
| 14527 | return (Py_UNICODE*)p; |
| 14528 | } |
| 14529 | return NULL; |
| 14530 | } |
Victor Stinner | 331ea92 | 2010-08-10 16:37:20 +0000 | [diff] [blame] | 14531 | |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14532 | Py_UNICODE* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14533 | PyUnicode_AsUnicodeCopy(PyObject *unicode) |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14534 | { |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 14535 | Py_UNICODE *u, *copy; |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14536 | Py_ssize_t len, size; |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14537 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14538 | if (!PyUnicode_Check(unicode)) { |
| 14539 | PyErr_BadArgument(); |
| 14540 | return NULL; |
| 14541 | } |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14542 | u = PyUnicode_AsUnicodeAndSize(unicode, &len); |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 14543 | if (u == NULL) |
| 14544 | return NULL; |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14545 | /* Ensure we won't overflow the size. */ |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14546 | if (len > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14547 | PyErr_NoMemory(); |
| 14548 | return NULL; |
| 14549 | } |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14550 | size = len + 1; /* copy the null character */ |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14551 | size *= sizeof(Py_UNICODE); |
| 14552 | copy = PyMem_Malloc(size); |
| 14553 | if (copy == NULL) { |
| 14554 | PyErr_NoMemory(); |
| 14555 | return NULL; |
| 14556 | } |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 14557 | memcpy(copy, u, size); |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14558 | return copy; |
| 14559 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 14560 | |
Georg Brandl | 66c221e | 2010-10-14 07:04:07 +0000 | [diff] [blame] | 14561 | /* A _string module, to export formatter_parser and formatter_field_name_split |
| 14562 | to the string.Formatter class implemented in Python. */ |
| 14563 | |
| 14564 | static PyMethodDef _string_methods[] = { |
| 14565 | {"formatter_field_name_split", (PyCFunction) formatter_field_name_split, |
| 14566 | METH_O, PyDoc_STR("split the argument as a field name")}, |
| 14567 | {"formatter_parser", (PyCFunction) formatter_parser, |
| 14568 | METH_O, PyDoc_STR("parse the argument as a format string")}, |
| 14569 | {NULL, NULL} |
| 14570 | }; |
| 14571 | |
| 14572 | static struct PyModuleDef _string_module = { |
| 14573 | PyModuleDef_HEAD_INIT, |
| 14574 | "_string", |
| 14575 | PyDoc_STR("string helper module"), |
| 14576 | 0, |
| 14577 | _string_methods, |
| 14578 | NULL, |
| 14579 | NULL, |
| 14580 | NULL, |
| 14581 | NULL |
| 14582 | }; |
| 14583 | |
| 14584 | PyMODINIT_FUNC |
| 14585 | PyInit__string(void) |
| 14586 | { |
| 14587 | return PyModule_Create(&_string_module); |
| 14588 | } |
| 14589 | |
| 14590 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 14591 | #ifdef __cplusplus |
| 14592 | } |
| 14593 | #endif |